@polkadot-api/smoldot 0.2.7 → 0.3.1

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,79 @@
1
+ const chainIds = /* @__PURE__ */ new WeakMap();
2
+ const startFromWorker = (worker, options = {}) => {
3
+ sendToWorker(worker, {
4
+ type: "start",
5
+ value: options
6
+ });
7
+ return {
8
+ async addChain(options2) {
9
+ const { potentialRelayChains, ...addChainOptions } = options2;
10
+ const id = await sendToWorker(worker, {
11
+ type: "add-chain",
12
+ value: {
13
+ ...addChainOptions,
14
+ potentialRelayChainIds: potentialRelayChains?.map((chain2) => {
15
+ const id2 = chainIds.get(chain2);
16
+ if (id2 == null) {
17
+ throw new Error("Only chains created with `addChain` can be used");
18
+ }
19
+ return id2;
20
+ })
21
+ }
22
+ });
23
+ const chain = {
24
+ nextJsonRpcResponse() {
25
+ return sendToWorker(worker, {
26
+ type: "chain",
27
+ value: {
28
+ id,
29
+ type: "receive"
30
+ }
31
+ });
32
+ },
33
+ remove() {
34
+ return sendToWorker(worker, {
35
+ type: "chain",
36
+ value: {
37
+ id,
38
+ type: "remove"
39
+ }
40
+ });
41
+ },
42
+ sendJsonRpc(value) {
43
+ return sendToWorker(worker, {
44
+ type: "chain",
45
+ value: {
46
+ id,
47
+ type: "send",
48
+ value
49
+ }
50
+ });
51
+ }
52
+ };
53
+ chainIds.set(chain, id);
54
+ return chain;
55
+ },
56
+ async terminate() {
57
+ await sendToWorker(worker, {
58
+ type: "terminate"
59
+ });
60
+ }
61
+ };
62
+ };
63
+ let msgId = 0;
64
+ function sendToWorker(worker, msg) {
65
+ const id = msgId++;
66
+ worker.postMessage({ ...msg, id });
67
+ return new Promise((resolve) => {
68
+ const msgHandler = (response) => {
69
+ if (response.id === id) {
70
+ resolve(response.value);
71
+ worker.off("message", msgHandler);
72
+ }
73
+ };
74
+ worker.on("message", msgHandler);
75
+ });
76
+ }
77
+
78
+ export { startFromWorker };
79
+ //# sourceMappingURL=from-node-worker.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-node-worker.mjs","sources":["../../../src/from-node-worker.ts"],"sourcesContent":["import { Worker } from \"node:worker_threads\"\nimport type { Chain, Client } from \"smoldot\"\nimport type { RequestMessage, SmoldotOptions } from \"./node-worker\"\n\nconst chainIds = new WeakMap<Chain, number>()\nexport const startFromWorker = (\n worker: Worker,\n options: SmoldotOptions = {},\n): Client => {\n sendToWorker(worker, {\n type: \"start\",\n value: options,\n })\n\n return {\n async addChain(options) {\n const { potentialRelayChains, ...addChainOptions } = options\n const id = await sendToWorker(worker, {\n type: \"add-chain\",\n value: {\n ...addChainOptions,\n potentialRelayChainIds: potentialRelayChains?.map((chain) => {\n const id = chainIds.get(chain)\n if (id == null) {\n throw new Error(\"Only chains created with `addChain` can be used\")\n }\n return id\n }),\n },\n })\n\n const chain: Chain = {\n nextJsonRpcResponse() {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"receive\",\n },\n })\n },\n remove() {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"remove\",\n },\n })\n },\n sendJsonRpc(value) {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"send\",\n value,\n },\n })\n },\n }\n chainIds.set(chain, id)\n\n return chain\n },\n async terminate() {\n await sendToWorker(worker, {\n type: \"terminate\",\n })\n },\n }\n}\n\nlet msgId = 0\nfunction sendToWorker(worker: Worker, msg: RequestMessage): Promise<any> {\n const id = msgId++\n worker.postMessage({ ...msg, id })\n return new Promise((resolve) => {\n const msgHandler = (response: any) => {\n if (response.id === id) {\n resolve(response.value)\n worker.off(\"message\", msgHandler)\n }\n }\n worker.on(\"message\", msgHandler)\n })\n}\n"],"names":["options","chain","id"],"mappings":"AAIA,MAAM,QAAA,uBAAe,OAAuB,EAAA,CAAA;AACrC,MAAM,eAAkB,GAAA,CAC7B,MACA,EAAA,OAAA,GAA0B,EACf,KAAA;AACX,EAAA,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,EAAA,OAAA;AAAA,YACN,KAAO,EAAA;AAAA,cACL,EAAA;AAAA,cACA,IAAM,EAAA,MAAA;AAAA,cACN,KAAA;AAAA,aACF;AAAA,WACD,CAAA,CAAA;AAAA,SACH;AAAA,OACF,CAAA;AACA,MAAS,QAAA,CAAA,GAAA,CAAI,OAAO,EAAE,CAAA,CAAA;AAEtB,MAAO,OAAA,KAAA,CAAA;AAAA,KACT;AAAA,IACA,MAAM,SAAY,GAAA;AAChB,MAAA,MAAM,aAAa,MAAQ,EAAA;AAAA,QACzB,IAAM,EAAA,WAAA;AAAA,OACP,CAAA,CAAA;AAAA,KACH;AAAA,GACF,CAAA;AACF,EAAA;AAEA,IAAI,KAAQ,GAAA,CAAA,CAAA;AACZ,SAAS,YAAA,CAAa,QAAgB,GAAmC,EAAA;AACvE,EAAA,MAAM,EAAK,GAAA,KAAA,EAAA,CAAA;AACX,EAAA,MAAA,CAAO,WAAY,CAAA,EAAE,GAAG,GAAA,EAAK,IAAI,CAAA,CAAA;AACjC,EAAO,OAAA,IAAI,OAAQ,CAAA,CAAC,OAAY,KAAA;AAC9B,IAAM,MAAA,UAAA,GAAa,CAAC,QAAkB,KAAA;AACpC,MAAI,IAAA,QAAA,CAAS,OAAO,EAAI,EAAA;AACtB,QAAA,OAAA,CAAQ,SAAS,KAAK,CAAA,CAAA;AACtB,QAAO,MAAA,CAAA,GAAA,CAAI,WAAW,UAAU,CAAA,CAAA;AAAA,OAClC;AAAA,KACF,CAAA;AACA,IAAO,MAAA,CAAA,EAAA,CAAG,WAAW,UAAU,CAAA,CAAA;AAAA,GAChC,CAAA,CAAA;AACH;;;;"}
@@ -0,0 +1,8 @@
1
+ import { Worker } from 'node:worker_threads';
2
+ import { ClientOptions, Client } from 'smoldot';
3
+
4
+ type SmoldotOptions = Omit<ClientOptions, "portToWorker">;
5
+
6
+ declare const startFromWorker: (worker: Worker, options?: SmoldotOptions) => Client;
7
+
8
+ export { startFromWorker };
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ const chainIds = /* @__PURE__ */ new WeakMap();
4
+ const startFromWorker = (worker, options = {}) => {
5
+ sendToWorker(worker, {
6
+ type: "start",
7
+ value: options
8
+ });
9
+ return {
10
+ async addChain(options2) {
11
+ const { potentialRelayChains, ...addChainOptions } = options2;
12
+ const id = await sendToWorker(worker, {
13
+ type: "add-chain",
14
+ value: {
15
+ ...addChainOptions,
16
+ potentialRelayChainIds: potentialRelayChains?.map((chain2) => {
17
+ const id2 = chainIds.get(chain2);
18
+ if (id2 == null) {
19
+ throw new Error("Only chains created with `addChain` can be used");
20
+ }
21
+ return id2;
22
+ })
23
+ }
24
+ });
25
+ const chain = {
26
+ nextJsonRpcResponse() {
27
+ return sendToWorker(worker, {
28
+ type: "chain",
29
+ value: {
30
+ id,
31
+ type: "receive"
32
+ }
33
+ });
34
+ },
35
+ remove() {
36
+ return sendToWorker(worker, {
37
+ type: "chain",
38
+ value: {
39
+ id,
40
+ type: "remove"
41
+ }
42
+ });
43
+ },
44
+ sendJsonRpc(value) {
45
+ return sendToWorker(worker, {
46
+ type: "chain",
47
+ value: {
48
+ id,
49
+ type: "send",
50
+ value
51
+ }
52
+ });
53
+ }
54
+ };
55
+ chainIds.set(chain, id);
56
+ return chain;
57
+ },
58
+ async terminate() {
59
+ await sendToWorker(worker, {
60
+ type: "terminate"
61
+ });
62
+ }
63
+ };
64
+ };
65
+ let msgId = 0;
66
+ function sendToWorker(worker, msg) {
67
+ const id = msgId++;
68
+ worker.postMessage({ ...msg, id });
69
+ return new Promise((resolve) => {
70
+ const msgHandler = (response) => {
71
+ if (response.id === id) {
72
+ resolve(response.value);
73
+ worker.off("message", msgHandler);
74
+ }
75
+ };
76
+ worker.on("message", msgHandler);
77
+ });
78
+ }
79
+
80
+ exports.startFromWorker = startFromWorker;
81
+ //# sourceMappingURL=from-node-worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-node-worker.js","sources":["../../src/from-node-worker.ts"],"sourcesContent":["import { Worker } from \"node:worker_threads\"\nimport type { Chain, Client } from \"smoldot\"\nimport type { RequestMessage, SmoldotOptions } from \"./node-worker\"\n\nconst chainIds = new WeakMap<Chain, number>()\nexport const startFromWorker = (\n worker: Worker,\n options: SmoldotOptions = {},\n): Client => {\n sendToWorker(worker, {\n type: \"start\",\n value: options,\n })\n\n return {\n async addChain(options) {\n const { potentialRelayChains, ...addChainOptions } = options\n const id = await sendToWorker(worker, {\n type: \"add-chain\",\n value: {\n ...addChainOptions,\n potentialRelayChainIds: potentialRelayChains?.map((chain) => {\n const id = chainIds.get(chain)\n if (id == null) {\n throw new Error(\"Only chains created with `addChain` can be used\")\n }\n return id\n }),\n },\n })\n\n const chain: Chain = {\n nextJsonRpcResponse() {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"receive\",\n },\n })\n },\n remove() {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"remove\",\n },\n })\n },\n sendJsonRpc(value) {\n return sendToWorker(worker, {\n type: \"chain\",\n value: {\n id,\n type: \"send\",\n value,\n },\n })\n },\n }\n chainIds.set(chain, id)\n\n return chain\n },\n async terminate() {\n await sendToWorker(worker, {\n type: \"terminate\",\n })\n },\n }\n}\n\nlet msgId = 0\nfunction sendToWorker(worker: Worker, msg: RequestMessage): Promise<any> {\n const id = msgId++\n worker.postMessage({ ...msg, id })\n return new Promise((resolve) => {\n const msgHandler = (response: any) => {\n if (response.id === id) {\n resolve(response.value)\n worker.off(\"message\", msgHandler)\n }\n }\n worker.on(\"message\", msgHandler)\n })\n}\n"],"names":["options","chain","id"],"mappings":";;AAIA,MAAM,QAAA,uBAAe,OAAuB,EAAA,CAAA;AACrC,MAAM,eAAkB,GAAA,CAC7B,MACA,EAAA,OAAA,GAA0B,EACf,KAAA;AACX,EAAA,YAAA,CAAa,MAAQ,EAAA;AAAA,IACnB,IAAM,EAAA,OAAA;AAAA,IACN,KAAO,EAAA,OAAA;AAAA,GACR,CAAA,CAAA;AAED,EAAO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ACD,CAAA,CAAA;AAAA,SACH;AAAA,OACF,CAAA;AACA,MAAS,QAAA,CAAA,GAAA,CAAI,OAAO,EAAE,CAAA,CAAA;AAEtB,MAAO,OAAA,KAAA,CAAA;AAAA,KACT;AAAA,IACA,MAAM,SAAY,GAAA;AAChB,MAAA,MAAM,aAAa,MAAQ,EAAA;AAAA,QACzB,IAAM,EAAA,WAAA;AAAA,OACP,CAAA,CAAA;AAAA,KACH;AAAA,GACF,CAAA;AACF,EAAA;AAEA,IAAI,KAAQ,GAAA,CAAA,CAAA;AACZ,SAAS,YAAA,CAAa,QAAgB,GAAmC,EAAA;AACvE,EAAA,MAAM,EAAK,GAAA,KAAA,EAAA,CAAA;AACX,EAAA,MAAA,CAAO,WAAY,CAAA,EAAE,GAAG,GAAA,EAAK,IAAI,CAAA,CAAA;AACjC,EAAO,OAAA,IAAI,OAAQ,CAAA,CAAC,OAAY,KAAA;AAC9B,IAAM,MAAA,UAAA,GAAa,CAAC,QAAkB,KAAA;AACpC,MAAI,IAAA,QAAA,CAAS,OAAO,EAAI,EAAA;AACtB,QAAA,OAAA,CAAQ,SAAS,KAAK,CAAA,CAAA;AACtB,QAAO,MAAA,CAAA,GAAA,CAAI,WAAW,UAAU,CAAA,CAAA;AAAA,OAClC;AAAA,KACF,CAAA;AACA,IAAO,MAAA,CAAA,EAAA,CAAG,WAAW,UAAU,CAAA,CAAA;AAAA,GAChC,CAAA,CAAA;AACH;;;;"}
@@ -0,0 +1,73 @@
1
+ import { parentPort } from 'node:worker_threads';
2
+ import { start } from 'smoldot';
3
+
4
+ let smoldot = null;
5
+ let chainId = 0;
6
+ const chains = /* @__PURE__ */ new Map();
7
+ parentPort.on("message", async (msg) => {
8
+ if (msg.type === "start") {
9
+ if (smoldot !== null) {
10
+ throw new Error("Can't call start on a client already started");
11
+ }
12
+ smoldot = start(msg.value);
13
+ return parentPort?.postMessage({
14
+ id: msg.id
15
+ });
16
+ }
17
+ if (smoldot === null) {
18
+ throw new Error("Smoldot not started");
19
+ }
20
+ switch (msg.type) {
21
+ case "add-chain": {
22
+ const potentialRelayChains = msg.value.potentialRelayChainIds?.map(
23
+ (id2) => {
24
+ const chain2 = chains.get(id2);
25
+ if (!chain2) throw new Error("Can't reference removed chain");
26
+ return chain2;
27
+ }
28
+ );
29
+ const chain = await smoldot.addChain({
30
+ ...msg.value,
31
+ potentialRelayChains
32
+ });
33
+ const id = chainId++;
34
+ chains.set(id, chain);
35
+ parentPort?.postMessage({
36
+ id: msg.id,
37
+ value: id
38
+ });
39
+ break;
40
+ }
41
+ case "terminate":
42
+ await smoldot.terminate();
43
+ parentPort?.postMessage({
44
+ id: msg.id
45
+ });
46
+ smoldot = null;
47
+ chains.clear();
48
+ break;
49
+ case "chain":
50
+ handleChainMessage(msg, msg.id);
51
+ break;
52
+ }
53
+ });
54
+ async function handleChainMessage(msg, id) {
55
+ const chain = chains.get(msg.value.id);
56
+ if (!chain) throw new Error("Can't reference removed chain");
57
+ switch (msg.value.type) {
58
+ case "receive":
59
+ parentPort?.postMessage({
60
+ id,
61
+ value: await chain.nextJsonRpcResponse()
62
+ });
63
+ break;
64
+ case "send":
65
+ chain.sendJsonRpc(msg.value.value);
66
+ break;
67
+ case "remove":
68
+ chain.remove();
69
+ chains.delete(msg.value.id);
70
+ break;
71
+ }
72
+ }
73
+ //# sourceMappingURL=node-worker.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-worker.mjs","sources":["../../../src/node-worker.ts"],"sourcesContent":["import { parentPort } from \"node:worker_threads\"\nimport { AddChainOptions, Chain, Client, ClientOptions, start } from \"smoldot\"\n\nexport type SmoldotOptions = Omit<ClientOptions, \"portToWorker\">\nexport interface StartMsg {\n type: \"start\"\n value: SmoldotOptions\n}\nexport interface AddChainMsg {\n type: \"add-chain\"\n value: Omit<AddChainOptions, \"potentialRelayChains\"> & {\n potentialRelayChainIds?: number[]\n }\n}\nexport interface TerminateMsg {\n type: \"terminate\"\n}\nexport interface ChainMsg {\n type: \"chain\"\n value: {\n id: number\n } & (\n | {\n type: \"remove\" | \"receive\"\n }\n | {\n type: \"send\"\n value: string\n }\n )\n}\nexport type RequestMessage = StartMsg | AddChainMsg | TerminateMsg | ChainMsg\nexport type CorrelatedRequestMessage = { id: number } & RequestMessage\n\nlet smoldot: Client | null = null\n\nlet chainId = 0\nconst chains = new Map<number, Chain>()\n\nparentPort!.on(\"message\", async (msg: CorrelatedRequestMessage) => {\n if (msg.type === \"start\") {\n if (smoldot !== null) {\n throw new Error(\"Can't call start on a client already started\")\n }\n smoldot = start(msg.value)\n return parentPort?.postMessage({\n id: msg.id,\n })\n }\n\n if (smoldot === null) {\n throw new Error(\"Smoldot not started\")\n }\n\n switch (msg.type) {\n case \"add-chain\": {\n const potentialRelayChains = msg.value.potentialRelayChainIds?.map(\n (id) => {\n const chain = chains.get(id)\n if (!chain) throw new Error(\"Can't reference removed chain\")\n return chain\n },\n )\n const chain = await smoldot.addChain({\n ...msg.value,\n potentialRelayChains,\n })\n const id = chainId++\n chains.set(id, chain)\n parentPort?.postMessage({\n id: msg.id,\n value: id,\n })\n break\n }\n case \"terminate\":\n await smoldot.terminate()\n parentPort?.postMessage({\n id: msg.id,\n })\n smoldot = null\n chains.clear()\n break\n case \"chain\":\n handleChainMessage(msg, msg.id)\n break\n }\n})\n\nasync function handleChainMessage(msg: ChainMsg, id: number) {\n const chain = chains.get(msg.value.id)\n if (!chain) throw new Error(\"Can't reference removed chain\")\n\n switch (msg.value.type) {\n case \"receive\":\n parentPort?.postMessage({\n id,\n value: await chain.nextJsonRpcResponse(),\n })\n break\n case \"send\":\n chain.sendJsonRpc(msg.value.value)\n break\n case \"remove\":\n chain.remove()\n chains.delete(msg.value.id)\n break\n }\n}\n"],"names":["id","chain"],"mappings":";;;AAkCA,IAAI,OAAyB,GAAA,IAAA,CAAA;AAE7B,IAAI,OAAU,GAAA,CAAA,CAAA;AACd,MAAM,MAAA,uBAAa,GAAmB,EAAA,CAAA;AAEtC,UAAY,CAAA,EAAA,C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
+ type: "start";
6
+ value: SmoldotOptions;
7
+ }
8
+ interface AddChainMsg {
9
+ type: "add-chain";
10
+ value: Omit<AddChainOptions, "potentialRelayChains"> & {
11
+ potentialRelayChainIds?: number[];
12
+ };
13
+ }
14
+ interface TerminateMsg {
15
+ type: "terminate";
16
+ }
17
+ interface ChainMsg {
18
+ type: "chain";
19
+ value: {
20
+ id: number;
21
+ } & ({
22
+ type: "remove" | "receive";
23
+ } | {
24
+ type: "send";
25
+ value: string;
26
+ });
27
+ }
28
+ type RequestMessage = StartMsg | AddChainMsg | TerminateMsg | ChainMsg;
29
+ type CorrelatedRequestMessage = {
30
+ id: number;
31
+ } & RequestMessage;
32
+
33
+ export type { AddChainMsg, ChainMsg, CorrelatedRequestMessage, RequestMessage, SmoldotOptions, StartMsg, TerminateMsg };
@@ -0,0 +1,75 @@
1
+ 'use strict';
2
+
3
+ var node_worker_threads = require('node:worker_threads');
4
+ var smoldot$1 = require('smoldot');
5
+
6
+ let smoldot = null;
7
+ let chainId = 0;
8
+ const chains = /* @__PURE__ */ new Map();
9
+ node_worker_threads.parentPort.on("message", async (msg) => {
10
+ if (msg.type === "start") {
11
+ if (smoldot !== null) {
12
+ throw new Error("Can't call start on a client already started");
13
+ }
14
+ smoldot = smoldot$1.start(msg.value);
15
+ return node_worker_threads.parentPort?.postMessage({
16
+ id: msg.id
17
+ });
18
+ }
19
+ if (smoldot === null) {
20
+ throw new Error("Smoldot not started");
21
+ }
22
+ switch (msg.type) {
23
+ case "add-chain": {
24
+ const potentialRelayChains = msg.value.potentialRelayChainIds?.map(
25
+ (id2) => {
26
+ const chain2 = chains.get(id2);
27
+ if (!chain2) throw new Error("Can't reference removed chain");
28
+ return chain2;
29
+ }
30
+ );
31
+ const chain = await smoldot.addChain({
32
+ ...msg.value,
33
+ potentialRelayChains
34
+ });
35
+ const id = chainId++;
36
+ chains.set(id, chain);
37
+ node_worker_threads.parentPort?.postMessage({
38
+ id: msg.id,
39
+ value: id
40
+ });
41
+ break;
42
+ }
43
+ case "terminate":
44
+ await smoldot.terminate();
45
+ node_worker_threads.parentPort?.postMessage({
46
+ id: msg.id
47
+ });
48
+ smoldot = null;
49
+ chains.clear();
50
+ break;
51
+ case "chain":
52
+ handleChainMessage(msg, msg.id);
53
+ break;
54
+ }
55
+ });
56
+ async function handleChainMessage(msg, id) {
57
+ const chain = chains.get(msg.value.id);
58
+ if (!chain) throw new Error("Can't reference removed chain");
59
+ switch (msg.value.type) {
60
+ case "receive":
61
+ node_worker_threads.parentPort?.postMessage({
62
+ id,
63
+ value: await chain.nextJsonRpcResponse()
64
+ });
65
+ break;
66
+ case "send":
67
+ chain.sendJsonRpc(msg.value.value);
68
+ break;
69
+ case "remove":
70
+ chain.remove();
71
+ chains.delete(msg.value.id);
72
+ break;
73
+ }
74
+ }
75
+ //# sourceMappingURL=node-worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-worker.js","sources":["../../src/node-worker.ts"],"sourcesContent":["import { parentPort } from \"node:worker_threads\"\nimport { AddChainOptions, Chain, Client, ClientOptions, start } from \"smoldot\"\n\nexport type SmoldotOptions = Omit<ClientOptions, \"portToWorker\">\nexport interface StartMsg {\n type: \"start\"\n value: SmoldotOptions\n}\nexport interface AddChainMsg {\n type: \"add-chain\"\n value: Omit<AddChainOptions, \"potentialRelayChains\"> & {\n potentialRelayChainIds?: number[]\n }\n}\nexport interface TerminateMsg {\n type: \"terminate\"\n}\nexport interface ChainMsg {\n type: \"chain\"\n value: {\n id: number\n } & (\n | {\n type: \"remove\" | \"receive\"\n }\n | {\n type: \"send\"\n value: string\n }\n )\n}\nexport type RequestMessage = StartMsg | AddChainMsg | TerminateMsg | ChainMsg\nexport type CorrelatedRequestMessage = { id: number } & RequestMessage\n\nlet smoldot: Client | null = null\n\nlet chainId = 0\nconst chains = new Map<number, Chain>()\n\nparentPort!.on(\"message\", async (msg: CorrelatedRequestMessage) => {\n if (msg.type === \"start\") {\n if (smoldot !== null) {\n throw new Error(\"Can't call start on a client already started\")\n }\n smoldot = start(msg.value)\n return parentPort?.postMessage({\n id: msg.id,\n })\n }\n\n if (smoldot === null) {\n throw new Error(\"Smoldot not started\")\n }\n\n switch (msg.type) {\n case \"add-chain\": {\n const potentialRelayChains = msg.value.potentialRelayChainIds?.map(\n (id) => {\n const chain = chains.get(id)\n if (!chain) throw new Error(\"Can't reference removed chain\")\n return chain\n },\n )\n const chain = await smoldot.addChain({\n ...msg.value,\n potentialRelayChains,\n })\n const id = chainId++\n chains.set(id, chain)\n parentPort?.postMessage({\n id: msg.id,\n value: id,\n })\n break\n }\n case \"terminate\":\n await smoldot.terminate()\n parentPort?.postMessage({\n id: msg.id,\n })\n smoldot = null\n chains.clear()\n break\n case \"chain\":\n handleChainMessage(msg, msg.id)\n break\n }\n})\n\nasync function handleChainMessage(msg: ChainMsg, id: number) {\n const chain = chains.get(msg.value.id)\n if (!chain) throw new Error(\"Can't reference removed chain\")\n\n switch (msg.value.type) {\n case \"receive\":\n parentPort?.postMessage({\n id,\n value: await chain.nextJsonRpcResponse(),\n })\n break\n case \"send\":\n chain.sendJsonRpc(msg.value.value)\n break\n case \"remove\":\n chain.remove()\n chains.delete(msg.value.id)\n break\n }\n}\n"],"names":["parentPort","start","id","chain"],"mappings":";;;;;AAkCA,IAAI,OAAyB,GAAA,IAAA,CAAA;AAE7B,IAAI,OAAU,GAAA,CAAA,CAAA;AACd,MAAM,MAAA,uBAAa,GAAmB,EAAA,CAAA;AAEtCA,8BAAY,CAAA,EAAA,CAAG,SAAW,EAAA,OAAO,GAAkC,KAAA;AACjE,EAAI,IAAA,GAAA,CAAI,SAAS,OAAS,EAAA;AACxB,IAAA,IAAI,YAAY,IAAM,EAAA;AACpB,MAAM,MAAA,IAAI,MAAM,8CAA8C,CAAA,CAAA;AAAA,KAChE;AACA,IAAU,OAAA,GAAAC,eAAA,CAAM,IAAI,KAAK,CAAA,CAAA;AACzB,IAAA,OAAOD,gCAAY,WAAY,CAAA;AAAA,MAC7B,IAAI,GAAI,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GAAA,EAAK,IAAI,EAAE,CAAA,CAAA;AAC9B,MAAA,MAAA;AAAA,GACJ;AACF,CAAC,CAAA,CAAA;AAED,eAAe,kBAAA,CAAmB,KAAe,EAAY,EAAA;AAC3D,EAAA,MAAM,KAAQ,GAAA,MAAA,CAAO,GAAI,CAAA,GAAA,CAAI,MAAM,EAAE,CAAA,CAAA;AACrC,EAAA,IAAI,CAAC,KAAA,EAAa,MAAA,IAAI,MAAM,+BAA+B,CAAA,CAAA;AAE3D,EAAQ,QAAA,GAAA,CAAI,MAAM,IAAM;AAAA,IACtB,KAAK,SAAA;AACH,MAAAA,8BAAA,EAAY,WAAY,CAAA;AAAA,QACtB,EAAA;AAAA,QACA,KAAA,EAAO,MAAM,KAAA,CAAM,mBAAoB,EAAA;AAAA,OACxC,CAAA,CAAA;AACD,MAAA,MAAA;AAAA,IACF,KAAK,MAAA;AACH,MAAM,KAAA,CAAA,WAAA,CAAY,GAAI,CAAA,KAAA,CAAM,KAAK,CAAA,CAAA;AACjC,MAAA,MAAA;AAAA,IACF,KAAK,QAAA;AACH,MAAA,KAAA,CAAM,MAAO,EAAA,CAAA;AACb,MAAO,MAAA,CAAA,MAAA,CAAO,GAAI,CAAA,KAAA,CAAM,EAAE,CAAA,CAAA;AAC1B,MAAA,MAAA;AAAA,GACJ;AACF;;"}
@@ -3,6 +3,7 @@
3
3
  "types": "../dist/from-worker/from-worker.d.ts",
4
4
  "module": "../dist/from-worker/esm/from-worker.mjs",
5
5
  "import": "../dist/from-worker/esm/from-worker.mjs",
6
+ "browser": "../dist/node-worker/esm/node-worker.mjs",
6
7
  "require": "../dist/from-worker/from-worker.js",
7
8
  "default": "../dist/from-worker/from-worker.js"
8
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polkadot-api/smoldot",
3
- "version": "0.2.7",
3
+ "version": "0.3.1",
4
4
  "author": "Josep M Sobrepere (https://github.com/josepot)",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,6 +19,17 @@
19
19
  "require": "./dist/index.js",
20
20
  "default": "./dist/index.js"
21
21
  },
22
+ "./from-node-worker": {
23
+ "node": {
24
+ "import": "./dist/from-node-worker/esm/from-node-worker.mjs",
25
+ "require": "./dist/from-node-worker/from-node-worker.js",
26
+ "default": "./dist/from-node-worker/from-node-worker.js"
27
+ },
28
+ "module": "./dist/from-node-worker/esm/from-node-worker.mjs",
29
+ "import": "./dist/from-node-worker/esm/from-node-worker.mjs",
30
+ "require": "./dist/from-node-worker/from-node-worker.js",
31
+ "default": "./dist/from-node-worker/from-node-worker.js"
32
+ },
22
33
  "./from-worker": {
23
34
  "node": {
24
35
  "import": "./dist/from-worker/esm/from-worker.mjs",
@@ -30,6 +41,17 @@
30
41
  "require": "./dist/from-worker/from-worker.js",
31
42
  "default": "./dist/from-worker/from-worker.js"
32
43
  },
44
+ "./node-worker": {
45
+ "node": {
46
+ "import": "./dist/node-worker/esm/node-worker.mjs",
47
+ "require": "./dist/node-worker/node-worker.js",
48
+ "default": "./dist/node-worker/node-worker.js"
49
+ },
50
+ "module": "./dist/node-worker/esm/node-worker.mjs",
51
+ "import": "./dist/node-worker/esm/node-worker.mjs",
52
+ "require": "./dist/node-worker/node-worker.js",
53
+ "default": "./dist/node-worker/node-worker.js"
54
+ },
33
55
  "./worker": {
34
56
  "node": {
35
57
  "import": "./dist/worker/esm/worker.mjs",
@@ -44,8 +66,8 @@
44
66
  "./package.json": "./package.json"
45
67
  },
46
68
  "main": "./dist/index.js",
47
- "module": "./dist/index.mjs",
48
- "browser": "./dist/index.mjs",
69
+ "module": "./dist/esm/index.mjs",
70
+ "browser": "./dist/esm/index.mjs",
49
71
  "types": "./dist/index.d.ts",
50
72
  "files": [
51
73
  "dist",
@@ -53,11 +75,9 @@
53
75
  "worker"
54
76
  ],
55
77
  "dependencies": {
78
+ "@types/node": "^22.2.0",
56
79
  "smoldot": "2.0.29"
57
80
  },
58
- "devDependencies": {
59
- "@types/node": "^20.14.12"
60
- },
61
81
  "scripts": {
62
82
  "build-core": "tsc --noEmit && rollup -c",
63
83
  "build": "pnpm build-core",