@novasamatech/product-sdk 0.5.0-18 → 0.5.0-19
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.
- package/README.md +39 -5
- package/dist/chat.d.ts +14 -0
- package/dist/chat.js +62 -0
- package/dist/{createSpektrMetaProvider.d.ts → createMetaProvider.d.ts} +2 -2
- package/dist/createMetaProvider.js +24 -0
- package/dist/{createSpektrPapiProvider.d.ts → createPapiProvider.d.ts} +1 -1
- package/dist/createPapiProvider.js +51 -0
- package/dist/defaultTransport.d.ts +2 -2
- package/dist/defaultTransport.js +1 -1
- package/dist/helpers.d.ts +7 -0
- package/dist/helpers.js +10 -0
- package/dist/hostApi.d.ts +641 -0
- package/dist/hostApi.js +3 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -3
- package/dist/injectSpektrExtension.d.ts +2 -2
- package/dist/injectSpektrExtension.js +69 -29
- package/package.json +3 -2
- package/dist/createSpektrMetaProvider.js +0 -21
- package/dist/createSpektrPapiProvider.js +0 -43
- package/dist/createTransport.d.ts +0 -21
- package/dist/createTransport.js +0 -111
- package/dist/inject.d.ts +0 -2
- package/dist/inject.js +0 -60
- package/dist/transport.d.ts +0 -3
- package/dist/transport.js +0 -51
- package/dist/types.d.ts +0 -8
- package/dist/types.js +0 -1
- package/dist/utils.d.ts +0 -2
- package/dist/utils.js +0 -14
|
@@ -1,43 +1,69 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { assertEnumVariant, createHostApi, enumValue, fromHex, toHex } from '@novasamatech/host-api';
|
|
2
2
|
import { injectExtension } from '@polkadot/extension-inject';
|
|
3
|
+
import { AccountId } from '@polkadot-api/substrate-bindings';
|
|
3
4
|
import { SpektrExtensionName, Version } from './constants.js';
|
|
4
5
|
import { defaultTransport } from './defaultTransport.js';
|
|
6
|
+
const UNSUPPORTED_VERSION_ERROR = 'Unsupported message version';
|
|
5
7
|
export async function createExtensionEnableFactory(transport) {
|
|
6
8
|
const ready = await transport.isReady();
|
|
7
9
|
if (!ready)
|
|
8
10
|
return null;
|
|
11
|
+
const hostApi = createHostApi(transport);
|
|
12
|
+
const accountId = AccountId();
|
|
9
13
|
async function enable() {
|
|
14
|
+
async function getAccounts() {
|
|
15
|
+
const response = await hostApi.get_non_product_accounts(enumValue('v1', undefined));
|
|
16
|
+
return response.match(response => {
|
|
17
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
18
|
+
return response.value.map(account => ({
|
|
19
|
+
name: account.name,
|
|
20
|
+
address: accountId.dec(account.publicKey),
|
|
21
|
+
}));
|
|
22
|
+
}, err => {
|
|
23
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
24
|
+
throw err.value;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
10
27
|
return {
|
|
11
28
|
accounts: {
|
|
12
|
-
get() {
|
|
13
|
-
return
|
|
14
|
-
.request({ tag: 'getAccountsRequestV1', value: undefined }, 'getAccountsResponseV1')
|
|
15
|
-
.then(v => unwrapResultOrThrow(v, e => new Error(e)));
|
|
29
|
+
async get() {
|
|
30
|
+
return getAccounts();
|
|
16
31
|
},
|
|
17
32
|
subscribe(callback) {
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
const accounts = unwrapResultOrThrow(payload, e => new Error(e));
|
|
21
|
-
callback(accounts);
|
|
22
|
-
}
|
|
23
|
-
catch {
|
|
24
|
-
transport.provider.logger.error('Failed response on account subscription', payload.value);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
transport.postMessage('_', { tag: 'accountSubscriptionV1', value: undefined });
|
|
33
|
+
getAccounts().then(callback);
|
|
28
34
|
return () => {
|
|
29
|
-
|
|
30
|
-
unsubscribe();
|
|
35
|
+
// empty
|
|
31
36
|
};
|
|
32
37
|
},
|
|
33
38
|
},
|
|
34
39
|
signer: {
|
|
35
|
-
signRaw(raw) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
.
|
|
40
|
+
async signRaw(raw) {
|
|
41
|
+
const payload = {
|
|
42
|
+
address: raw.address,
|
|
43
|
+
data: raw.type === 'bytes'
|
|
44
|
+
? {
|
|
45
|
+
tag: 'Bytes',
|
|
46
|
+
value: fromHex(raw.data),
|
|
47
|
+
}
|
|
48
|
+
: {
|
|
49
|
+
tag: 'Payload',
|
|
50
|
+
value: raw.data,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
const response = await hostApi.sign_raw(enumValue('v1', payload));
|
|
54
|
+
return response.match(response => {
|
|
55
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
56
|
+
return {
|
|
57
|
+
id: 0,
|
|
58
|
+
signature: response.value.signature,
|
|
59
|
+
signedTransaction: response.value.signedTransaction,
|
|
60
|
+
};
|
|
61
|
+
}, err => {
|
|
62
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
63
|
+
throw err.value;
|
|
64
|
+
});
|
|
39
65
|
},
|
|
40
|
-
signPayload(payload) {
|
|
66
|
+
async signPayload(payload) {
|
|
41
67
|
const codecPayload = {
|
|
42
68
|
...payload,
|
|
43
69
|
method: payload.method,
|
|
@@ -46,14 +72,28 @@ export async function createExtensionEnableFactory(transport) {
|
|
|
46
72
|
withSignedTransaction: payload.withSignedTransaction,
|
|
47
73
|
metadataHash: payload.metadataHash,
|
|
48
74
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
75
|
+
const response = await hostApi.sign_payload(enumValue('v1', codecPayload));
|
|
76
|
+
return response.match(response => {
|
|
77
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
78
|
+
return {
|
|
79
|
+
id: 0,
|
|
80
|
+
signature: response.value.signature,
|
|
81
|
+
signedTransaction: response.value.signedTransaction,
|
|
82
|
+
};
|
|
83
|
+
}, err => {
|
|
84
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
85
|
+
throw err.value;
|
|
86
|
+
});
|
|
52
87
|
},
|
|
53
|
-
createTransaction(payload) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
88
|
+
async createTransaction(payload) {
|
|
89
|
+
const response = await hostApi.create_transaction_with_non_product_account(enumValue('v1', payload));
|
|
90
|
+
return response.match(response => {
|
|
91
|
+
assertEnumVariant(response, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
92
|
+
return toHex(response.value);
|
|
93
|
+
}, err => {
|
|
94
|
+
assertEnumVariant(err, 'v1', UNSUPPORTED_VERSION_ERROR);
|
|
95
|
+
throw err.value;
|
|
96
|
+
});
|
|
57
97
|
},
|
|
58
98
|
},
|
|
59
99
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@novasamatech/product-sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.5.0-
|
|
4
|
+
"version": "0.5.0-19",
|
|
5
5
|
"description": "Polkadot product SDK: integrate and run your product inside Polkadot browser.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
@@ -27,9 +27,10 @@
|
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@polkadot/extension-inject": "^0.62.6",
|
|
30
|
+
"@polkadot-api/substrate-bindings": "^0.16.6",
|
|
30
31
|
"@polkadot-api/json-rpc-provider": "^0.0.4",
|
|
31
32
|
"@polkadot-api/json-rpc-provider-proxy": "^0.2.7",
|
|
32
|
-
"@novasamatech/host-api": "0.5.0-
|
|
33
|
+
"@novasamatech/host-api": "0.5.0-19"
|
|
33
34
|
},
|
|
34
35
|
"publishConfig": {
|
|
35
36
|
"access": "public"
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { defaultTransport } from './defaultTransport.js';
|
|
2
|
-
export function createSpektrMetaProvider(transport = defaultTransport) {
|
|
3
|
-
if (transport.isCorrectEnvironment() && typeof window !== 'undefined') {
|
|
4
|
-
const getUrl = () => {
|
|
5
|
-
return window.location.pathname + window.location.hash + window.location.search;
|
|
6
|
-
};
|
|
7
|
-
window.addEventListener('hashchange', () => {
|
|
8
|
-
transport.postMessage('_', { tag: 'locationChangedV1', value: getUrl() });
|
|
9
|
-
});
|
|
10
|
-
window.addEventListener('popstate', () => {
|
|
11
|
-
transport.postMessage('_', { tag: 'locationChangedV1', value: getUrl() });
|
|
12
|
-
});
|
|
13
|
-
transport.postMessage('_', { tag: 'locationChangedV1', value: getUrl() });
|
|
14
|
-
}
|
|
15
|
-
return {
|
|
16
|
-
subscribeConnectionStatus(callback) {
|
|
17
|
-
return transport.onConnectionStatusChange(callback);
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
export const spektrMetaProvider = createSpektrMetaProvider();
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { unwrapResultOrThrow } from '@novasamatech/host-api';
|
|
2
|
-
import { getSyncProvider } from '@polkadot-api/json-rpc-provider-proxy';
|
|
3
|
-
import { defaultTransport } from './defaultTransport.js';
|
|
4
|
-
export function createSpektrPapiProvider({ chainId: genesisHash, fallback }, internal) {
|
|
5
|
-
const transport = internal?.transport ?? defaultTransport;
|
|
6
|
-
if (!transport.isCorrectEnvironment())
|
|
7
|
-
return fallback;
|
|
8
|
-
const spektrProvider = onMessage => {
|
|
9
|
-
const unsubscribe = transport.subscribe('papiProviderReceiveMessageV1', (_, payload) => {
|
|
10
|
-
const unwrapped = unwrapResultOrThrow(payload, e => new Error(e));
|
|
11
|
-
if (unwrapped.genesisHash === genesisHash) {
|
|
12
|
-
onMessage(unwrapped.message);
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
return {
|
|
16
|
-
send(message) {
|
|
17
|
-
transport.postMessage('_', { tag: 'papiProviderSendMessageV1', value: { genesisHash, message } });
|
|
18
|
-
},
|
|
19
|
-
disconnect() {
|
|
20
|
-
unsubscribe();
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
function checkIfReady() {
|
|
25
|
-
return transport.isReady().then(ready => {
|
|
26
|
-
if (!ready)
|
|
27
|
-
return false;
|
|
28
|
-
return transport
|
|
29
|
-
.request({ tag: 'supportFeatureRequestV1', value: { tag: 'chain', value: { genesisHash } } }, 'supportFeatureResponseV1')
|
|
30
|
-
.then(payload => {
|
|
31
|
-
const result = unwrapResultOrThrow(payload, e => new Error(e));
|
|
32
|
-
if (result.tag === 'chain' && result.value.genesisHash === genesisHash) {
|
|
33
|
-
return result.value.result;
|
|
34
|
-
}
|
|
35
|
-
})
|
|
36
|
-
.catch(e => {
|
|
37
|
-
transport.provider.logger.error('Error checking chain support', e);
|
|
38
|
-
return false;
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
return getSyncProvider(() => checkIfReady().then(ready => (ready ? spektrProvider : fallback)));
|
|
43
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { type MessagePayloadSchema, type MessageType, type PickMessagePayload } from '@novasamatech/host-api';
|
|
2
|
-
export type Provider = {
|
|
3
|
-
send(message: Uint8Array): void;
|
|
4
|
-
on(callback: (message: Uint8Array) => void): () => void;
|
|
5
|
-
};
|
|
6
|
-
export declare const defaultProvider: Provider;
|
|
7
|
-
export type Transport = NonNullable<ReturnType<typeof createTransport>>;
|
|
8
|
-
export declare function createTransport(provider: Provider): {
|
|
9
|
-
isSpektrReady(): Promise<boolean>;
|
|
10
|
-
subscribeAny(callback: (id: string, payload: MessagePayloadSchema) => void): () => void;
|
|
11
|
-
subscribe<const Type extends MessageType>(type: Type, callback: (id: string, payload: PickMessagePayload<Type>) => void): () => void;
|
|
12
|
-
send(id: string, payload: MessagePayloadSchema): string;
|
|
13
|
-
request(payload: MessagePayloadSchema): Promise<MessagePayloadSchema>;
|
|
14
|
-
} | null;
|
|
15
|
-
export declare const defaultTransport: {
|
|
16
|
-
isSpektrReady(): Promise<boolean>;
|
|
17
|
-
subscribeAny(callback: (id: string, payload: MessagePayloadSchema) => void): () => void;
|
|
18
|
-
subscribe<const Type extends MessageType>(type: Type, callback: (id: string, payload: PickMessagePayload<Type>) => void): () => void;
|
|
19
|
-
send(id: string, payload: MessagePayloadSchema): string;
|
|
20
|
-
request(payload: MessagePayloadSchema): Promise<MessagePayloadSchema>;
|
|
21
|
-
} | null;
|
package/dist/createTransport.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { HANDSHAKE_INTERVAL, isValidMessage, promiseWithResolvers } from '@novasamatech/product-sdk-shared';
|
|
2
|
-
import { messageEncoder, } from '@novasamatech/host-api';
|
|
3
|
-
import { nanoid } from 'nanoid';
|
|
4
|
-
import { getParentWindow, inIframe } from './utils';
|
|
5
|
-
export const defaultProvider = {
|
|
6
|
-
send(message) {
|
|
7
|
-
getParentWindow().postMessage(message, '*', [message.buffer]);
|
|
8
|
-
},
|
|
9
|
-
on(callback) {
|
|
10
|
-
const handle = (event) => {
|
|
11
|
-
if (!isValidMessage(event, getParentWindow(), window))
|
|
12
|
-
return;
|
|
13
|
-
callback(event.data);
|
|
14
|
-
};
|
|
15
|
-
window.addEventListener('message', handle);
|
|
16
|
-
return () => {
|
|
17
|
-
window.removeEventListener('message', handle);
|
|
18
|
-
};
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
export function createTransport(provider) {
|
|
22
|
-
if (!inIframe()) {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
const readyAbortController = new AbortController();
|
|
26
|
-
let connected = null;
|
|
27
|
-
const api = {
|
|
28
|
-
isSpektrReady() {
|
|
29
|
-
if (connected !== null) {
|
|
30
|
-
return Promise.resolve(connected);
|
|
31
|
-
}
|
|
32
|
-
let resolved = false;
|
|
33
|
-
const request = new Promise(resolve => {
|
|
34
|
-
const interval = setInterval(() => {
|
|
35
|
-
if (readyAbortController.signal.aborted) {
|
|
36
|
-
clearInterval(interval);
|
|
37
|
-
resolve(false);
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
const id = nanoid();
|
|
41
|
-
const encoded = messageEncoder.enc({ id, payload: { tag: 'handshakeRequestV1', value: undefined } });
|
|
42
|
-
const unsubscribe = api.subscribe('handshakeResponseV1', receivedId => {
|
|
43
|
-
if (receivedId !== id)
|
|
44
|
-
return;
|
|
45
|
-
clearInterval(interval);
|
|
46
|
-
unsubscribe();
|
|
47
|
-
resolved = true;
|
|
48
|
-
resolve(true);
|
|
49
|
-
});
|
|
50
|
-
provider.send(encoded);
|
|
51
|
-
}, HANDSHAKE_INTERVAL);
|
|
52
|
-
});
|
|
53
|
-
return Promise.race([
|
|
54
|
-
request,
|
|
55
|
-
new Promise(resolve => {
|
|
56
|
-
setTimeout(() => {
|
|
57
|
-
if (!resolved) {
|
|
58
|
-
readyAbortController.abort();
|
|
59
|
-
resolve(false);
|
|
60
|
-
}
|
|
61
|
-
}, 1_000);
|
|
62
|
-
}).then(result => {
|
|
63
|
-
connected = result;
|
|
64
|
-
return result;
|
|
65
|
-
}),
|
|
66
|
-
]);
|
|
67
|
-
},
|
|
68
|
-
subscribeAny(callback) {
|
|
69
|
-
return provider.on(message => {
|
|
70
|
-
let result;
|
|
71
|
-
try {
|
|
72
|
-
result = messageEncoder.dec(message);
|
|
73
|
-
}
|
|
74
|
-
catch {
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
callback(result.id, result.payload);
|
|
78
|
-
});
|
|
79
|
-
},
|
|
80
|
-
subscribe(type, callback) {
|
|
81
|
-
return api.subscribeAny((id, message) => {
|
|
82
|
-
if (message.tag == type) {
|
|
83
|
-
callback(id, message);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
},
|
|
87
|
-
send(id, payload) {
|
|
88
|
-
const encoded = messageEncoder.enc({ id, payload });
|
|
89
|
-
provider.send(encoded);
|
|
90
|
-
return id;
|
|
91
|
-
},
|
|
92
|
-
async request(payload) {
|
|
93
|
-
const ready = await api.isSpektrReady();
|
|
94
|
-
if (!ready) {
|
|
95
|
-
throw new Error('Spektr is not ready');
|
|
96
|
-
}
|
|
97
|
-
const id = nanoid();
|
|
98
|
-
const { resolve, promise } = promiseWithResolvers();
|
|
99
|
-
const unsubscribe = api.subscribeAny((receivedId, message) => {
|
|
100
|
-
if (receivedId === id) {
|
|
101
|
-
unsubscribe();
|
|
102
|
-
resolve(message);
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
api.send(id, payload);
|
|
106
|
-
return promise;
|
|
107
|
-
},
|
|
108
|
-
};
|
|
109
|
-
return api;
|
|
110
|
-
}
|
|
111
|
-
export const defaultTransport = createTransport(defaultProvider);
|
package/dist/inject.d.ts
DELETED
package/dist/inject.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { injectExtension } from '@polkadot/extension-inject';
|
|
2
|
-
import { unwrapResponseOrThrow } from '@spektr/sdk-transport';
|
|
3
|
-
import { SpektrExtensionName, Version } from './constants';
|
|
4
|
-
import { createTransport, defaultProvider } from './createTransport';
|
|
5
|
-
function injectPolkadotExtension(transport) {
|
|
6
|
-
async function enable() {
|
|
7
|
-
return {
|
|
8
|
-
accounts: {
|
|
9
|
-
get() {
|
|
10
|
-
return transport.request({ tag: 'getAccountsRequestV1', value: undefined }).then(e => {
|
|
11
|
-
if (e.tag === 'getAccountsResponseV1') {
|
|
12
|
-
return unwrapResponseOrThrow(e.value);
|
|
13
|
-
}
|
|
14
|
-
throw new Error(`Invalid response, got ${e.tag} message`);
|
|
15
|
-
});
|
|
16
|
-
},
|
|
17
|
-
subscribe(callback) {
|
|
18
|
-
return transport.subscribe('getAccountsResponseV1', (_, message) => {
|
|
19
|
-
try {
|
|
20
|
-
const accounts = unwrapResponseOrThrow(message.value);
|
|
21
|
-
callback(accounts);
|
|
22
|
-
}
|
|
23
|
-
catch {
|
|
24
|
-
console.error('Invalid account response, got', message.value.value);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
signer: {
|
|
30
|
-
signRaw(raw) {
|
|
31
|
-
return transport.request({ tag: 'signRawRequestV1', value: raw }).then(response => {
|
|
32
|
-
if (response.tag === 'signPayloadResponseV1') {
|
|
33
|
-
return unwrapResponseOrThrow(response.value);
|
|
34
|
-
}
|
|
35
|
-
throw new Error(`Invalid response, got ${response.tag} message`);
|
|
36
|
-
});
|
|
37
|
-
},
|
|
38
|
-
signPayload(payload) {
|
|
39
|
-
return transport.request({ tag: 'signPayloadRequestV1', value: payload }).then(response => {
|
|
40
|
-
if (response.tag === 'signPayloadResponseV1') {
|
|
41
|
-
return unwrapResponseOrThrow(response.value);
|
|
42
|
-
}
|
|
43
|
-
throw new Error(`Invalid response, got ${response.tag} message`);
|
|
44
|
-
});
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
injectExtension(enable, { name: SpektrExtensionName, version: Version });
|
|
50
|
-
}
|
|
51
|
-
export async function inject(provider = defaultProvider) {
|
|
52
|
-
const transport = createTransport(provider);
|
|
53
|
-
if (!transport)
|
|
54
|
-
return false;
|
|
55
|
-
const ready = await transport.isSpektrReady();
|
|
56
|
-
if (!ready)
|
|
57
|
-
return false;
|
|
58
|
-
injectPolkadotExtension(transport);
|
|
59
|
-
return true;
|
|
60
|
-
}
|
package/dist/transport.d.ts
DELETED
package/dist/transport.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { isValidMessage } from '@novasamatech/product-sdk-shared';
|
|
2
|
-
import { createTransport } from '@novasamatech/host-api';
|
|
3
|
-
function getParentWindow() {
|
|
4
|
-
if (window.top) {
|
|
5
|
-
return window.top;
|
|
6
|
-
}
|
|
7
|
-
throw new Error('No parent window found');
|
|
8
|
-
}
|
|
9
|
-
function isIframe() {
|
|
10
|
-
try {
|
|
11
|
-
return window !== window.top;
|
|
12
|
-
}
|
|
13
|
-
catch {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function createDefaultSdkProvider() {
|
|
18
|
-
const subscribers = new Set();
|
|
19
|
-
const handleMessage = (event) => {
|
|
20
|
-
if (!isValidMessage(event, getParentWindow(), window))
|
|
21
|
-
return;
|
|
22
|
-
for (const subscriber of subscribers) {
|
|
23
|
-
subscriber(event.data);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
if (isIframe()) {
|
|
27
|
-
window.addEventListener('message', handleMessage);
|
|
28
|
-
}
|
|
29
|
-
return {
|
|
30
|
-
isCorrectEnvironment() {
|
|
31
|
-
return isIframe();
|
|
32
|
-
},
|
|
33
|
-
postMessage(message) {
|
|
34
|
-
getParentWindow().postMessage(message, '*', [message.buffer]);
|
|
35
|
-
},
|
|
36
|
-
subscribe(callback) {
|
|
37
|
-
subscribers.add(callback);
|
|
38
|
-
return () => {
|
|
39
|
-
subscribers.delete(callback);
|
|
40
|
-
};
|
|
41
|
-
},
|
|
42
|
-
dispose() {
|
|
43
|
-
subscribers.clear();
|
|
44
|
-
if (isIframe()) {
|
|
45
|
-
window.removeEventListener('message', handleMessage);
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
export const defaultProvider = createDefaultSdkProvider();
|
|
51
|
-
export const defaultTransport = createTransport(defaultProvider, { handshakeTimeout: 1_000 });
|
package/dist/types.d.ts
DELETED
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/utils.d.ts
DELETED
package/dist/utils.js
DELETED