Skip to content

Commit

Permalink
✨ (transport): Extracted MockServer + mockserver-client
Browse files Browse the repository at this point in the history
Also exported the factories to make it easier to pass to addTransport
  • Loading branch information
valpinkman committed Nov 6, 2024
1 parent ddb41ca commit 158aebb
Show file tree
Hide file tree
Showing 57 changed files with 272 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .changeset/cool-dancers-join.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"@ledgerhq/device-signer-kit-ethereum": patch
"@ledgerhq/tsconfig-dsdk": patch
"@ledgerhq/prettier-config-dsdk": patch
"@ledgerhq/device-transport-kit-mock-client": patch
"@ledgerhq/device-mockserver-client": patch
"@ledgerhq/eslint-config-dsdk": patch
"@ledgerhq/device-sdk-trusted-apps": patch
"@ledgerhq/jest-config-dsdk": patch
Expand Down
2 changes: 1 addition & 1 deletion .changeset/lemon-suits-notice.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"@ledgerhq/device-management-kit-flipper-plugin-client": patch
"@ledgerhq/device-signer-kit-solana": patch
"@ledgerhq/device-signer-kit-ethereum": patch
"@ledgerhq/device-transport-kit-mock-client": patch
"@ledgerhq/device-mockserver-client": patch
"@ledgerhq/device-management-kit": patch
"@ledgerhq/device-management-kit-sample": patch
---
Expand Down
2 changes: 1 addition & 1 deletion .changeset/smart-games-brush.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"@ledgerhq/device-transport-kit-mock-client": patch
"@ledgerhq/device-mockserver-client": patch
"@ledgerhq/device-management-kit": patch
"@ledgerhq/device-management-kit-sample": patch
---
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ A brief description of this project packages:
| @ledgerhq/device-management-kit-flipper-plugin-client | packages/flipper-plugin-client | external package that contains [flipper](https://github.com/facebook/flipper) logger for Device Management Kit |
| @ledgerhq/device-transport-kit-web-hid | packages/transport/web-hid | external package that contains the Web Hid transport implementation |
| @ledgerhq/device-transport-kit-web-ble | packages/transport/web-ble | external package that contains the Web Ble transport implementation |
| @ledgerhq/device-mockserver-client | packages/mockserver-client | external package that contains the client to interact with the mock-server |

# Getting started

Expand Down Expand Up @@ -180,7 +181,7 @@ Please configure JEST extention accordingly.

Each package is built using the following command (at the root of the monorepo).

### Core
### Devie Management Kit

Device Management Kit main module.

Expand Down
3 changes: 2 additions & 1 deletion apps/sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"@ledgerhq/context-module": "workspace:*",
"@ledgerhq/device-management-kit": "workspace:*",
"@ledgerhq/device-management-kit-flipper-plugin-client": "workspace:*",
"@ledgerhq/device-mockserver-client": "workspace:*",
"@ledgerhq/device-signer-kit-ethereum": "workspace:*",
"@ledgerhq/device-signer-kit-solana": "workspace:*",
"@ledgerhq/device-transport-kit-mock-client": "workspace:*",
"@ledgerhq/device-transport-kit-mockserver": "workspace:*",
"@ledgerhq/device-transport-kit-web-ble": "workspace:*",
"@ledgerhq/device-transport-kit-web-hid": "workspace:*",
"@ledgerhq/react-ui": "^0.16.2",
Expand Down
2 changes: 1 addition & 1 deletion apps/sample/src/components/MockView/MockDeviceDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type Mock,
type MockClient,
type Session,
} from "@ledgerhq/device-transport-kit-mock-client";
} from "@ledgerhq/device-mockserver-client";
import { Button, Divider, Flex, Input, Text } from "@ledgerhq/react-ui";
import styled from "styled-components";

Expand Down
2 changes: 1 addition & 1 deletion apps/sample/src/components/MockView/MockItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from "react";
import { type Mock } from "@ledgerhq/device-transport-kit-mock-client";
import { type Mock } from "@ledgerhq/device-mockserver-client";
import { Button, Flex, Icons, Input, Text } from "@ledgerhq/react-ui";

type MockItemProps = {
Expand Down
2 changes: 1 addition & 1 deletion apps/sample/src/components/MockView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useEffect, useState } from "react";
import { type Session } from "@ledgerhq/device-transport-kit-mock-client";
import { type Session } from "@ledgerhq/device-mockserver-client";
import { Button, Flex, Text } from "@ledgerhq/react-ui";
import styled from "styled-components";

Expand Down
2 changes: 1 addition & 1 deletion apps/sample/src/hooks/useMockClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from "react";
import { MockClient } from "@ledgerhq/device-transport-kit-mock-client";
import { MockClient } from "@ledgerhq/device-mockserver-client";

export const useMockClient = (url: string): MockClient => {
const [client, setClient] = useState(new MockClient(url));
Expand Down
67 changes: 20 additions & 47 deletions apps/sample/src/providers/DeviceManagementKitProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
WebLogsExporterLogger,
} from "@ledgerhq/device-management-kit";
import { FlipperDmkLogger } from "@ledgerhq/device-management-kit-flipper-plugin-client";
import { WebBleTransport } from "@ledgerhq/device-transport-kit-web-ble";
import { WebHidTransport } from "@ledgerhq/device-transport-kit-web-hid";
import { mockserverTransportFactory } from "@ledgerhq/device-transport-kit-mockserver";
import { webBleTransportFactory } from "@ledgerhq/device-transport-kit-web-ble";
import { webHidTransportFactory } from "@ledgerhq/device-transport-kit-web-hid";

import { useHasChanged } from "@/hooks/useHasChanged";
import { useDmkConfigContext } from "@/providers/DmkConfig";
Expand All @@ -19,49 +20,23 @@ const LogsExporterContext = createContext<WebLogsExporterLogger | null>(null);

function buildDefaultDmk(logsExporter: WebLogsExporterLogger) {
return new DeviceManagementKitBuilder()
.addTransport(
({
deviceModelDataSource,
loggerServiceFactory,
apduSenderServiceFactory,
apduReceiverServiceFactory,
}) =>
new WebHidTransport(
deviceModelDataSource,
loggerServiceFactory,
apduSenderServiceFactory,
apduReceiverServiceFactory,
),
)
.addTransport(
({
deviceModelDataSource,
loggerServiceFactory,
apduSenderServiceFactory,
apduReceiverServiceFactory,
}) =>
new WebBleTransport(
deviceModelDataSource,
loggerServiceFactory,
apduSenderServiceFactory,
apduReceiverServiceFactory,
),
)
.addTransport(webHidTransportFactory)
.addTransport(webBleTransportFactory)
.addLogger(new ConsoleLogger())
.addLogger(logsExporter)
.addLogger(new FlipperDmkLogger())
.build();
}

// function buildMockDmk(url: string, logsExporter: WebLogsExporterLogger) {
// return new DeviceManagementKitBuilder()
// .addTransport(BuiltinTransports.MOCK_SERVER)
// .addLogger(new ConsoleLogger())
// .addLogger(logsExporter)
// .addLogger(new FlipperDmkLogger())
// .addConfig({ mockUrl: url })
// .build();
// }
function buildMockDmk(url: string, logsExporter: WebLogsExporterLogger) {
return new DeviceManagementKitBuilder()
.addTransport(mockserverTransportFactory)
.addLogger(new ConsoleLogger())
.addLogger(logsExporter)
.addLogger(new FlipperDmkLogger())
.addConfig({ mockUrl: url })
.build();
}

export const DmkProvider: React.FC<PropsWithChildren> = ({ children }) => {
const {
Expand All @@ -71,10 +46,9 @@ export const DmkProvider: React.FC<PropsWithChildren> = ({ children }) => {
const mockServerEnabled = transport === BuiltinTransports.MOCK_SERVER;
const [state, setState] = useState(() => {
const logsExporter = new WebLogsExporterLogger();
// const dmk = mockServerEnabled
// ? buildMockDmk(mockServerUrl, logsExporter)
// : buildDefaultDmk(logsExporter);
const dmk = buildDefaultDmk(logsExporter);
const dmk = mockServerEnabled
? buildMockDmk(mockServerUrl, logsExporter)
: buildDefaultDmk(logsExporter);
return { dmk, logsExporter };
});

Expand All @@ -84,10 +58,9 @@ export const DmkProvider: React.FC<PropsWithChildren> = ({ children }) => {
if (mockServerEnabledChanged || mockServerUrlChanged) {
setState(({ logsExporter }) => {
return {
dmk: buildDefaultDmk(logsExporter),
// dmk: mockServerEnabled
// ? buildMockDmk(mockServerUrl, logsExporter)
// : buildDefaultDmk(logsExporter),
dmk: mockServerEnabled
? buildMockDmk(mockServerUrl, logsExporter)
: buildDefaultDmk(logsExporter),
logsExporter,
};
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
"signer-eth": "pnpm --filter @ledgerhq/device-signer-kit-ethereum",
"signer-solana": "pnpm --filter @ledgerhq/device-signer-kit-solana",
"signer-utils": "pnpm --filter @ledgerhq/signer-utils",
"mock-client": "pnpm --filter @ledgerhq/device-transport-kit-mock-client",
"mockserver-client": "pnpm --filter @ledgerhq/device-mockserver-client",
"trusted-apps": "pnpm --filter @ledgerhq/device-sdk-trusted-apps",
"ui": "pnpm --filter @ledgerhq/device-sdk-ui",
"transports": "pnpm --filter @ledgerhq/device-transport-*",
"transport-web-hid": "pnpm --filter @ledgerhq/device-transport-kit-web-hid",
"transport-web-ble": "pnpm --filter @ledgerhq/device-transport-kit-web-ble",
"transport-mockserver": "pnpm --filter @ledgerhq/device-transport-kit-mockserver",
"flipper": "pnpm --filter @ledgerhq/device-management-kit-flipper-plugin-client",
"sample": "pnpm --filter @ledgerhq/device-management-kit-sample",
"bump": "changeset version",
Expand Down
2 changes: 1 addition & 1 deletion packages/device-management-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"module:create": "pnpm hygen core-module with-prompt"
},
"dependencies": {
"@ledgerhq/device-transport-kit-mock-client": "workspace:*",
"@ledgerhq/device-mockserver-client": "workspace:*",
"@sentry/minimal": "^6.19.7",
"@statelyai/inspect": "^0.4.0",
"axios": "^1.7.7",
Expand Down
1 change: 1 addition & 0 deletions packages/device-management-kit/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export { defaultApduSenderServiceStubBuilder } from "@api/device-session/service
export { FramerUtils } from "@api/device-session/utils/FramerUtils";
export { DeviceManagementKit } from "@api/DeviceManagementKit";
export { DeviceManagementKitBuilder } from "@api/DeviceManagementKitBuilder";
export { type DmkConfig } from "@api/DmkConfig";
export { DeviceExchangeError, UnknownDeviceExchangeError } from "@api/Error";
export { type DmkError } from "@api/Error";
export { type LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService";
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@ledgerhq/device-transport-kit-mock-client",
"name": "@ledgerhq/device-mockserver-client",
"private": true,
"version": "1.0.0",
"license": "Apache-2.0",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/transport/mockserver/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib/*
coverage/*
3 changes: 3 additions & 0 deletions packages/transport/mockserver/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require("@ledgerhq/prettier-config-dsdk"),
};
13 changes: 13 additions & 0 deletions packages/transport/mockserver/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import config from "@ledgerhq/eslint-config-dsdk";

export default [
...config,
{
ignores: ["eslint.config.mjs", "lib/*"],
languageOptions: {
parserOptions: {
project: "./tsconfig.json",
},
},
},
];
25 changes: 25 additions & 0 deletions packages/transport/mockserver/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint no-restricted-syntax: 0 */
import { type JestConfigWithTsJest, pathsToModuleNameMapper } from "ts-jest";

import { compilerOptions } from "./tsconfig.json";

const paths = pathsToModuleNameMapper(compilerOptions.paths, {
prefix: "<rootDir>/",
});

const config: JestConfigWithTsJest = {
preset: "@ledgerhq/jest-config-dsdk",
// setupFiles: ["<rootDir>/jest.setup.ts"],
testPathIgnorePatterns: ["<rootDir>/lib/esm/", "<rootDir>/lib/cjs/"],
collectCoverageFrom: [
"src/**/*.ts",
"!src/**/*.stub.ts",
"!src/index.ts",
"!src/api/index.ts",
],
moduleNameMapper: {
...paths,
},
};

export default config;
56 changes: 56 additions & 0 deletions packages/transport/mockserver/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@ledgerhq/device-transport-kit-mockserver",
"version": "0.0.1",
"license": "Apache-2.0",
"private": true,
"exports": {
".": {
"types": "./lib/types/index.d.ts",
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js"
},
"./*": {
"types": "./lib/types/*",
"import": "./lib/esm/*",
"require": "./lib/cjs/*"
}
},
"files": [
"./lib"
],
"scripts": {
"prebuild": "rimraf lib",
"build": "pnpm lmdk-build --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json --platform web",
"dev": "concurrently \"pnpm watch:builds\" \"pnpm watch:types\"",
"watch:builds": "pnpm lmdk-watch --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json --platform web",
"watch:types": "concurrently \"tsc --watch -p tsconfig.prod.json\" \"tsc-alias --watch -p tsconfig.prod.json\"",
"lint": "eslint",
"lint:fix": "pnpm lint --fix",
"postpack": "find . -name '*.tgz' -exec cp {} ../../../dist/ \\; ",
"prettier": "prettier . --check",
"prettier:fix": "prettier . --write",
"typecheck": "tsc --noEmit",
"test": "jest --passWithNoTests",
"test:watch": "pnpm test -- --watch",
"test:coverage": "pnpm test -- --coverage"
},
"dependencies": {
"@sentry/minimal": "^6.19.7",
"purify-ts": "^2.1.0"
},
"devDependencies": {
"@ledgerhq/device-management-kit": "workspace:*",
"@ledgerhq/device-mockserver-client": "workspace:*",
"@ledgerhq/esbuild-tools": "workspace:*",
"@ledgerhq/eslint-config-dsdk": "workspace:*",
"@ledgerhq/jest-config-dsdk": "workspace:*",
"@ledgerhq/prettier-config-dsdk": "workspace:*",
"@ledgerhq/tsconfig-dsdk": "workspace:*",
"rxjs": "^7.8.1",
"ts-node": "^10.9.2"
},
"peerDependencies": {
"@ledgerhq/device-management-kit": "workspace:*",
"rxjs": "^7.8.1"
}
}
Loading

0 comments on commit 158aebb

Please sign in to comment.