@notabene/javascript-sdk 1.34.0 → 2.0.0-next.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,54 @@
1
+ import { ComponentMessage, HostMessage } from '../types';
2
+
3
+ export type MessageCallback = (message: ComponentMessage) => void;
4
+
5
+ export class MessageEventManager {
6
+ private listeners: Map<string, Set<MessageCallback>> = new Map();
7
+ private port?: MessagePort;
8
+
9
+ constructor() {
10
+ this.handleMessage = this.handleMessage.bind(this);
11
+ }
12
+
13
+ setPort(port: MessagePort): void {
14
+ this.port = port;
15
+ this.port.onmessage = this.handleMessage;
16
+ this.port.start();
17
+ }
18
+
19
+ on(messageType: string, callback: MessageCallback): void {
20
+ if (!this.listeners.has(messageType)) {
21
+ this.listeners.set(messageType, new Set());
22
+ }
23
+ this.listeners.get(messageType)!.add(callback);
24
+ }
25
+
26
+ off(messageType: string, callback: MessageCallback): void {
27
+ const callbacks = this.listeners.get(messageType);
28
+ if (callbacks) {
29
+ callbacks.delete(callback);
30
+ if (callbacks.size === 0) {
31
+ this.listeners.delete(messageType);
32
+ }
33
+ }
34
+ }
35
+
36
+ private handleMessage(event: MessageEvent): void {
37
+ console.log('received message', event.data);
38
+ const message = event.data;
39
+ if (typeof message === 'object' && message !== null && 'type' in message) {
40
+ const messageType = message.type as string;
41
+ const callbacks = this.listeners.get(messageType);
42
+ if (callbacks) {
43
+ callbacks.forEach((callback) => callback(message));
44
+ }
45
+ }
46
+ }
47
+
48
+ // Method to send messages through the port
49
+ send(message: HostMessage): void {
50
+ if (this.port) {
51
+ this.port.postMessage(message);
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,95 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import { HMType, UpdateValue } from '../../types';
3
+ import { MessageEventManager } from '../MessageEventManager';
4
+
5
+ describe('MessageEventManager', () => {
6
+ let messageEventManager: MessageEventManager;
7
+ let mockPort: MessagePort;
8
+
9
+ beforeEach(() => {
10
+ mockPort = {
11
+ onmessage: null,
12
+ postMessage: vi.fn(),
13
+ start: vi.fn(),
14
+ } as unknown as MessagePort;
15
+
16
+ messageEventManager = new MessageEventManager();
17
+ });
18
+
19
+ describe('setPort', () => {
20
+ beforeEach(() => {
21
+ messageEventManager.setPort(mockPort);
22
+ });
23
+
24
+ it('should start the port when initialized', () => {
25
+ expect(mockPort.start).toHaveBeenCalled();
26
+ });
27
+
28
+ it('should subscribe and receive messages of a specific type', () => {
29
+ const readyHandler = vi.fn();
30
+ messageEventManager.on('ready', readyHandler);
31
+
32
+ const readyMessage = { type: 'ready' };
33
+ mockPort.onmessage!({ data: readyMessage } as MessageEvent);
34
+
35
+ expect(readyHandler).toHaveBeenCalledWith(readyMessage);
36
+ });
37
+
38
+ it('should not call handlers for different message types', () => {
39
+ const updateHandler = vi.fn();
40
+ const errorHandler = vi.fn();
41
+ messageEventManager.on('update', updateHandler);
42
+ messageEventManager.on('error', errorHandler);
43
+
44
+ const errorMessage = { type: 'error', message: 'Something went wrong' };
45
+ mockPort.onmessage!({ data: errorMessage } as MessageEvent);
46
+
47
+ expect(updateHandler).not.toHaveBeenCalled();
48
+ expect(errorHandler).toHaveBeenCalledWith(errorMessage);
49
+ });
50
+
51
+ it('should unsubscribe handlers', () => {
52
+ const updateHandler = vi.fn();
53
+ messageEventManager.on('update', updateHandler);
54
+ messageEventManager.off('update', updateHandler);
55
+
56
+ const updateMessage = { type: 'update', data: { foo: 'bar' } };
57
+ mockPort.onmessage!({ data: updateMessage } as MessageEvent);
58
+
59
+ expect(updateHandler).not.toHaveBeenCalled();
60
+ });
61
+
62
+ it('should allow multiple subscribers for the same message type', () => {
63
+ const updateHandler1 = vi.fn();
64
+ const updateHandler2 = vi.fn();
65
+ messageEventManager.on('update', updateHandler1);
66
+ messageEventManager.on('update', updateHandler2);
67
+
68
+ const updateMessage = { type: 'update', data: { foo: 'bar' } };
69
+ mockPort.onmessage!({ data: updateMessage } as MessageEvent);
70
+
71
+ expect(updateHandler1).toHaveBeenCalledWith(updateMessage);
72
+ expect(updateHandler2).toHaveBeenCalledWith(updateMessage);
73
+ });
74
+
75
+ it('should send messages through the port', () => {
76
+ const message: UpdateValue = {
77
+ type: HMType.UPDATE,
78
+ value: { requestId: 'id' },
79
+ };
80
+ messageEventManager.send(message);
81
+
82
+ expect(mockPort.postMessage).toHaveBeenCalledWith(message);
83
+ });
84
+
85
+ it('should ignore messages without a type', () => {
86
+ const handler = vi.fn();
87
+ messageEventManager.on('update', handler);
88
+
89
+ const invalidMessage = { data: 'no type field' };
90
+ mockPort.onmessage!({ data: invalidMessage } as MessageEvent);
91
+
92
+ expect(handler).not.toHaveBeenCalled();
93
+ });
94
+ });
95
+ });
@@ -0,0 +1,26 @@
1
+ import fc from 'fast-check';
2
+ import type { CAIP10, CAIP19, CAIP2, CAIP220 } from '../types';
3
+ import {
4
+ CAIP10_MATCHER,
5
+ CAIP19_MATCHER,
6
+ CAIP220_MATCHER,
7
+ CAIP2_MATCHER,
8
+ } from './caip';
9
+
10
+ const LOCALE = new RegExp(/^[a-z]{2}(-[A-Z]{2})?$/);
11
+
12
+ // Arbitraries
13
+ export const arbitraryCAIP10 = (): fc.Arbitrary<CAIP10> =>
14
+ fc.stringMatching(CAIP10_MATCHER) as fc.Arbitrary<CAIP10>;
15
+
16
+ export const arbitraryCAIP2 = (): fc.Arbitrary<CAIP2> =>
17
+ fc.stringMatching(CAIP2_MATCHER) as fc.Arbitrary<CAIP2>;
18
+
19
+ export const arbitraryCAIP19 = (): fc.Arbitrary<CAIP19> =>
20
+ fc.stringMatching(CAIP19_MATCHER) as fc.Arbitrary<CAIP19>;
21
+
22
+ export const arbitraryCAIP220 = (): fc.Arbitrary<CAIP220> =>
23
+ fc.stringMatching(CAIP220_MATCHER) as fc.Arbitrary<CAIP220>;
24
+
25
+ export const arbitraryLocale = (): fc.Arbitrary<string> =>
26
+ fc.stringMatching(LOCALE) as fc.Arbitrary<string>;
@@ -0,0 +1,16 @@
1
+ // Regexes
2
+ export const ACCOUNT_ADDRESS_REGEX = '[-.%a-zA-Z0-9]{1,128}';
3
+ export const CAIP2_REGEX = '[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}';
4
+ export const CAIP10_REGEX = `^${CAIP2_REGEX}:${ACCOUNT_ADDRESS_REGEX}$`;
5
+ export const CAIP19_REGEX = `^${CAIP2_REGEX}/[-a-z0-9]{3,8}:[-.%a-zA-Z0-9]{1,128}(/[-.%a-zA-Z0-9]{1,78})?$`;
6
+ export const CAIP220_REGEX = `^${CAIP2_REGEX}:[block:]?tx/[-.%a-zA-Z0-9]{1,128}$`;
7
+ export const CRYPTO_SYMBOL_REGEX = '^[A-Z0-9]{2,10}$';
8
+ export const DTI_REGEX = '^[A-Za-z0-9]{10}$';
9
+ export const COMBINED_ASSET_REGEX = `(${CRYPTO_SYMBOL_REGEX})|(${CAIP19_REGEX})|(${DTI_REGEX})`;
10
+
11
+ // Matchers
12
+ export const CAIP2_MATCHER = new RegExp(CAIP2_REGEX);
13
+ export const CAIP10_MATCHER = new RegExp(CAIP10_REGEX);
14
+ export const CAIP19_MATCHER = new RegExp(CAIP19_REGEX);
15
+ export const CAIP220_MATCHER = new RegExp(CAIP220_REGEX);
16
+ export const COMBINED_ASSET_MATCHER = new RegExp(COMBINED_ASSET_REGEX);
@@ -0,0 +1,15 @@
1
+ export function encodeObjectToFragment(obj: any) {
2
+ return Object.entries(obj)
3
+ .map(([key, value]) => {
4
+ if (!(key && value)) return undefined;
5
+ const encodedKey = encodeURIComponent(key);
6
+ const encodedValue = encodeURIComponent(
7
+ typeof value === 'object'
8
+ ? JSON.stringify(value)
9
+ : (value as string | number | boolean),
10
+ );
11
+ return `${encodedKey}=${encodedValue}`;
12
+ })
13
+ .filter((entry) => !!entry)
14
+ .join('&');
15
+ }
package/.editorconfig DELETED
@@ -1,10 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- end_of_line = lf
5
- insert_final_newline = true
6
-
7
- [*.{js,json,yml}]
8
- charset = utf-8
9
- indent_style = space
10
- indent_size = 2
package/.envrc.template DELETED
@@ -1 +0,0 @@
1
- export GITLAB_TOKEN="your-gitlab-token"
package/.eslintignore DELETED
@@ -1,3 +0,0 @@
1
- dist
2
- .eslintrc.js
3
- jest.config.js
package/.eslintrc.js DELETED
@@ -1,13 +0,0 @@
1
- module.exports = {
2
- root: true,
3
- parser: '@typescript-eslint/parser',
4
- plugins: ['@typescript-eslint'],
5
- extends: [
6
- 'eslint:recommended',
7
- 'plugin:@typescript-eslint/recommended',
8
- 'plugin:prettier/recommended',
9
- ],
10
- rules: {
11
- '@typescript-eslint/ban-ts-comment': 'off',
12
- },
13
- };
package/.gitlab-ci.yml DELETED
@@ -1,74 +0,0 @@
1
- image: registry.gitlab.com/notabene/devops/build-node-16-image:latest
2
-
3
- variables:
4
- NPM_TOKEN: ${CI_JOB_TOKEN}
5
-
6
- stages:
7
- - install
8
- - lint
9
- - test
10
- - build
11
- - release
12
-
13
- install:
14
- stage: install
15
- artifacts:
16
- paths:
17
- - .yarn
18
- - .pnp.cjs
19
- - .pnp.loader.mjs
20
- script:
21
- - yarn
22
-
23
- lint:
24
- stage: lint
25
- needs:
26
- - install
27
- script:
28
- - yarn lint
29
-
30
- test:
31
- stage: test
32
- needs:
33
- - install
34
- script:
35
- - yarn test
36
-
37
- build:
38
- stage: build
39
- needs:
40
- - install
41
- script:
42
- - yarn build
43
- artifacts:
44
- paths:
45
- - dist
46
-
47
- publish-gitlab:
48
- stage: release
49
- needs:
50
- - install
51
- - build
52
- variables:
53
- GITLAB_USER_NAME: 'gitlab-ci'
54
- GITLAB_USER_EMAIL: 'gitlab-ci@example.com'
55
- before_script:
56
- - git config --global user.email "$GITLAB_USER_EMAIL"
57
- - git config --global user.name "$GITLAB_USER_NAME"
58
- - |
59
- {
60
- echo "@${CI_PROJECT_ROOT_NAMESPACE}:registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/"
61
- echo "${CI_API_V4_URL#https?}/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=\${CI_JOB_TOKEN}"
62
- } | tee -a .npmrc
63
- script:
64
- - yarn semantic-release
65
- only:
66
- - master
67
-
68
- publish-npm:
69
- stage: release
70
- when: manual
71
- script:
72
- - yarn npm publish
73
- only:
74
- - tags
package/.husky/commit-msg DELETED
@@ -1,4 +0,0 @@
1
- #!/bin/sh
2
- . "$(dirname "$0")/_/husky.sh"
3
-
4
- yarn commitlint -e "${1}"
package/.husky/pre-commit DELETED
@@ -1,4 +0,0 @@
1
- #!/bin/sh
2
- . "$(dirname "$0")/_/husky.sh"
3
-
4
- yarn lint-staged
package/.husky/pre-push DELETED
@@ -1,4 +0,0 @@
1
- #!/bin/sh
2
- . "$(dirname "$0")/_/husky.sh"
3
-
4
- yarn test
package/.nvmrc DELETED
@@ -1 +0,0 @@
1
- v16
package/.prettierrc DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "semi": true,
3
- "singleQuote": true
4
- }
package/.releaserc.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "branches": ["master"],
3
- "plugins": [
4
- "@semantic-release/commit-analyzer",
5
- "@semantic-release/release-notes-generator",
6
- "@semantic-release/gitlab",
7
- "@semantic-release/npm",
8
- [
9
- "@semantic-release/git",
10
- {
11
- "assets": ["package.json"],
12
- "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
13
- }
14
- ]
15
- ]
16
- }
package/.tool-versions DELETED
@@ -1,3 +0,0 @@
1
- nodejs 16.16.0
2
- yarn 1.22.19
3
- direnv 2.32.1
@@ -1,7 +0,0 @@
1
- {
2
- "recommendations": [
3
- "arcanis.vscode-zipfs",
4
- "dbaeumer.vscode-eslint",
5
- "esbenp.prettier-vscode"
6
- ]
7
- }
@@ -1,11 +0,0 @@
1
- {
2
- "search.exclude": {
3
- "**/.yarn": true,
4
- "**/.pnp.*": true
5
- },
6
- "eslint.nodePath": ".yarn/sdks",
7
- "prettier.prettierPath": ".yarn/sdks/prettier/index.js",
8
- "typescript.tsdk": ".yarn/sdks/typescript/lib",
9
- "typescript.enablePromptUseWorkspaceTsdk": true,
10
- "editor.defaultFormatter": "esbenp.prettier-vscode"
11
- }
package/CODEOWNERS DELETED
@@ -1 +0,0 @@
1
- * @ajunge @brunosimplicio @dw1284 @richardcrosby @david1250 @marieh_mai @timothy4915912 @graham9961840