@polkadot-api/raw-client 0.2.1-canary.f1c6573 → 0.3.0
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/dist/{esm/DestroyedError.mjs → DestroyedError.js} +1 -1
- package/dist/DestroyedError.js.map +1 -0
- package/dist/{esm/RpcError.mjs → RpcError.js} +1 -1
- package/dist/RpcError.js.map +1 -0
- package/dist/{esm/createClient.mjs → createClient.js} +4 -4
- package/dist/createClient.js.map +1 -0
- package/dist/index.js +4 -123
- package/dist/index.js.map +1 -1
- package/dist/{esm/subscriptions-manager.mjs → subscriptions-manager.js} +1 -1
- package/dist/subscriptions-manager.js.map +1 -0
- package/package.json +9 -19
- package/dist/esm/DestroyedError.mjs.map +0 -1
- package/dist/esm/RpcError.mjs.map +0 -1
- package/dist/esm/createClient.mjs.map +0 -1
- package/dist/esm/index.mjs +0 -5
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/subscriptions-manager.mjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DestroyedError.js","sources":["../src/DestroyedError.ts"],"sourcesContent":["export class DestroyedError extends Error {\n constructor() {\n super(\"Client destroyed\")\n this.name = \"DestroyedError\"\n }\n}\n"],"names":[],"mappings":"AAAO,MAAM,uBAAuB,KAAA,CAAM;AAAA,EACxC,WAAA,GAAc;AACZ,IAAA,KAAA,CAAM,kBAAkB,CAAA;AACxB,IAAA,IAAA,CAAK,IAAA,GAAO,gBAAA;AAAA,EACd;AACF;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RpcError.js","sources":["../src/RpcError.ts"],"sourcesContent":["export interface IRpcError {\n code: number\n message: string\n data?: any\n}\n\nexport class RpcError extends Error implements IRpcError {\n code\n data\n constructor(e: IRpcError) {\n super(e.message)\n this.code = e.code\n this.data = e.data\n this.name = \"RpcError\"\n }\n}\n"],"names":[],"mappings":";;;AAMO,MAAM,iBAAiB,KAAA,CAA2B;AAAA,EAGvD,YAAY,CAAA,EAAc;AACxB,IAAA,KAAA,CAAM,EAAE,OAAO,CAAA;AAHjB,IAAA,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AACA,IAAA,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AAGE,IAAA,IAAA,CAAK,OAAO,CAAA,CAAE,IAAA;AACd,IAAA,IAAA,CAAK,OAAO,CAAA,CAAE,IAAA;AACd,IAAA,IAAA,CAAK,IAAA,GAAO,UAAA;AAAA,EACd;AACF;;;;"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isResponse } from '@polkadot-api/json-rpc-provider';
|
|
2
|
-
import { RpcError } from './RpcError.
|
|
3
|
-
import { getSubscriptionsManager } from './subscriptions-manager.
|
|
4
|
-
import { DestroyedError } from './DestroyedError.
|
|
2
|
+
import { RpcError } from './RpcError.js';
|
|
3
|
+
import { getSubscriptionsManager } from './subscriptions-manager.js';
|
|
4
|
+
import { DestroyedError } from './DestroyedError.js';
|
|
5
5
|
|
|
6
6
|
let nextClientId = 1;
|
|
7
7
|
const createClient = (gProvider, onNotification) => {
|
|
@@ -68,4 +68,4 @@ const createClient = (gProvider, onNotification) => {
|
|
|
68
68
|
};
|
|
69
69
|
|
|
70
70
|
export { createClient };
|
|
71
|
-
//# sourceMappingURL=createClient.
|
|
71
|
+
//# sourceMappingURL=createClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createClient.js","sources":["../src/createClient.ts"],"sourcesContent":["import {\n isResponse,\n JsonRpcRequest,\n type JsonRpcConnection,\n type JsonRpcId,\n type JsonRpcMessage,\n type JsonRpcProvider,\n} from \"@polkadot-api/json-rpc-provider\"\nimport { RpcError } from \"./RpcError\"\nimport { getSubscriptionsManager, Subscriber } from \"./subscriptions-manager\"\nimport { DestroyedError } from \"./DestroyedError\"\n\ntype UnsubscribeFn = () => void\nexport type FollowSubscriptionCb<T> = (\n subscriptionId: string,\n cb: Subscriber<T>,\n) => UnsubscribeFn\n\nexport type ClientRequestCb<T, TT> = {\n onSuccess: (result: T, followSubscription: FollowSubscriptionCb<TT>) => void\n onError: (e: Error) => void\n}\n\nexport type ClientRequest<T, TT> = (\n method: string,\n params: Array<any>,\n cb?: ClientRequestCb<T, TT>,\n) => UnsubscribeFn\n\nexport interface Client {\n disconnect: () => void\n request: ClientRequest<any, any>\n}\n\nlet nextClientId = 1\nexport const createClient = (\n gProvider: JsonRpcProvider,\n onNotification?: (req: JsonRpcRequest) => void,\n): Client => {\n let clientId = nextClientId++\n const responses = new Map<JsonRpcId, ClientRequestCb<any, any>>()\n const subscriptions = getSubscriptionsManager()\n\n let connection: JsonRpcConnection | null = null\n const send = (\n id: string,\n method: string,\n params: Array<boolean | string | number | null>,\n ) => {\n connection!.send({\n jsonrpc: \"2.0\",\n id,\n method,\n params,\n })\n }\n\n function onMessage(parsed: JsonRpcMessage): void {\n if (isResponse(parsed)) {\n const { id } = parsed\n const cb = responses.get(id)\n if (!cb) return\n\n responses.delete(id)\n\n return \"error\" in parsed\n ? cb.onError(new RpcError(parsed.error))\n : cb.onSuccess(parsed.result, (opaqueId, subscriber) => {\n const subscriptionId = opaqueId\n subscriptions.subscribe(subscriptionId, subscriber)\n return () => {\n subscriptions.unsubscribe(subscriptionId)\n }\n })\n }\n\n if (parsed.id === undefined) {\n // at this point, it means that it should be a notification\n const { params } = parsed\n const { subscription: subscriptionId, result, error } = params\n if (\n subscriptionId &&\n subscriptions.has(subscriptionId) &&\n (\"result\" in params || error)\n ) {\n if (error) subscriptions.error(subscriptionId, new RpcError(error!))\n else subscriptions.next(subscriptionId, result)\n } else {\n onNotification?.(parsed)\n }\n } else\n console.warn(\"Error parsing incomming message: \" + JSON.stringify(parsed))\n }\n\n connection = gProvider(onMessage)\n\n const disconnect = () => {\n connection?.disconnect()\n connection = null\n subscriptions.errorAll(new DestroyedError())\n responses.forEach((r) => r.onError(new DestroyedError()))\n responses.clear()\n }\n\n let nextId = 1\n const request = <T, TT>(\n method: string,\n params: Array<any>,\n cb?: ClientRequestCb<T, TT>,\n ): UnsubscribeFn => {\n if (!connection) throw new Error(\"Not connected\")\n const id = `${clientId}-${nextId++}`\n\n if (cb) responses.set(id, cb)\n send(id, method, params)\n\n return (): void => {\n responses.delete(id)\n }\n }\n\n return {\n request,\n disconnect,\n }\n}\n"],"names":[],"mappings":";;;;;AAkCA,IAAI,YAAA,GAAe,CAAA;AACZ,MAAM,YAAA,GAAe,CAC1B,SAAA,EACA,cAAA,KACW;AACX,EAAA,IAAI,QAAA,GAAW,YAAA,EAAA;AACf,EAAA,MAAM,SAAA,uBAAgB,GAAA,EAA0C;AAChE,EAAA,MAAM,gBAAgB,uBAAA,EAAwB;AAE9C,EAAA,IAAI,UAAA,GAAuC,IAAA;AAC3C,EAAA,MAAM,IAAA,GAAO,CACX,EAAA,EACA,MAAA,EACA,MAAA,KACG;AACH,IAAA,UAAA,CAAY,IAAA,CAAK;AAAA,MACf,OAAA,EAAS,KAAA;AAAA,MACT,EAAA;AAAA,MACA,MAAA;AAAA,MACA;AAAA,KACD,CAAA;AAAA,EACH,CAAA;AAEA,EAAA,SAAS,UAAU,MAAA,EAA8B;AAC/C,IAAA,IAAI,UAAA,CAAW,MAAM,CAAA,EAAG;AACtB,MAAA,MAAM,EAAE,IAAG,GAAI,MAAA;AACf,MAAA,MAAM,EAAA,GAAK,SAAA,CAAU,GAAA,CAAI,EAAE,CAAA;AAC3B,MAAA,IAAI,CAAC,EAAA,EAAI;AAET,MAAA,SAAA,CAAU,OAAO,EAAE,CAAA;AAEnB,MAAA,OAAO,WAAW,MAAA,GACd,EAAA,CAAG,OAAA,CAAQ,IAAI,SAAS,MAAA,CAAO,KAAK,CAAC,CAAA,GACrC,GAAG,SAAA,CAAU,MAAA,CAAO,MAAA,EAAQ,CAAC,UAAU,UAAA,KAAe;AACpD,QAAA,MAAM,cAAA,GAAiB,QAAA;AACvB,QAAA,aAAA,CAAc,SAAA,CAAU,gBAAgB,UA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,CAAC,UAAA,EAAY,MAAM,IAAI,MAAM,eAAe,CAAA;AAChD,IAAA,MAAM,EAAA,GAAK,CAAA,EAAG,QAAQ,CAAA,CAAA,EAAI,MAAA,EAAQ,CAAA,CAAA;AAElC,IAAA,IAAI,EAAA,EAAI,SAAA,CAAU,GAAA,CAAI,EAAA,EAAI,EAAE,CAAA;AAC5B,IAAA,IAAA,CAAK,EAAA,EAAI,QAAQ,MAAM,CAAA;AAEvB,IAAA,OAAO,MAAY;AACjB,MAAA,SAAA,CAAU,OAAO,EAAE,CAAA;AAAA,IACrB,CAAA;AAAA,EACF,CAAA;AAEA,EAAA,OAAO;AAAA,IACL,OAAA;AAAA,IACA;AAAA,GACF;AACF;;;;"}
|
package/dist/index.js
CHANGED
|
@@ -1,124 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __defProp = Object.defineProperty;
|
|
6
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
|
-
class RpcError extends Error {
|
|
9
|
-
constructor(e) {
|
|
10
|
-
super(e.message);
|
|
11
|
-
__publicField(this, "code");
|
|
12
|
-
__publicField(this, "data");
|
|
13
|
-
this.code = e.code;
|
|
14
|
-
this.data = e.data;
|
|
15
|
-
this.name = "RpcError";
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const getSubscriptionsManager = () => {
|
|
20
|
-
const subscriptions = /* @__PURE__ */ new Map();
|
|
21
|
-
return {
|
|
22
|
-
has: subscriptions.has.bind(subscriptions),
|
|
23
|
-
subscribe(id, subscriber) {
|
|
24
|
-
subscriptions.set(id, subscriber);
|
|
25
|
-
},
|
|
26
|
-
unsubscribe(id) {
|
|
27
|
-
subscriptions.delete(id);
|
|
28
|
-
},
|
|
29
|
-
next(id, data) {
|
|
30
|
-
subscriptions.get(id)?.next(data);
|
|
31
|
-
},
|
|
32
|
-
error(id, e) {
|
|
33
|
-
const subscriber = subscriptions.get(id);
|
|
34
|
-
if (subscriber) {
|
|
35
|
-
subscriptions.delete(id);
|
|
36
|
-
subscriber.error(e);
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
errorAll(e) {
|
|
40
|
-
const subscribers = [...subscriptions.values()];
|
|
41
|
-
subscriptions.clear();
|
|
42
|
-
subscribers.forEach((s) => {
|
|
43
|
-
s.error(e);
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
class DestroyedError extends Error {
|
|
50
|
-
constructor() {
|
|
51
|
-
super("Client destroyed");
|
|
52
|
-
this.name = "DestroyedError";
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let nextClientId = 1;
|
|
57
|
-
const createClient = (gProvider, onNotification) => {
|
|
58
|
-
let clientId = nextClientId++;
|
|
59
|
-
const responses = /* @__PURE__ */ new Map();
|
|
60
|
-
const subscriptions = getSubscriptionsManager();
|
|
61
|
-
let connection = null;
|
|
62
|
-
const send = (id, method, params) => {
|
|
63
|
-
connection.send({
|
|
64
|
-
jsonrpc: "2.0",
|
|
65
|
-
id,
|
|
66
|
-
method,
|
|
67
|
-
params
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
function onMessage(parsed) {
|
|
71
|
-
if (jsonRpcProvider.isResponse(parsed)) {
|
|
72
|
-
const { id } = parsed;
|
|
73
|
-
const cb = responses.get(id);
|
|
74
|
-
if (!cb) return;
|
|
75
|
-
responses.delete(id);
|
|
76
|
-
return "error" in parsed ? cb.onError(new RpcError(parsed.error)) : cb.onSuccess(parsed.result, (opaqueId, subscriber) => {
|
|
77
|
-
const subscriptionId = opaqueId;
|
|
78
|
-
subscriptions.subscribe(subscriptionId, subscriber);
|
|
79
|
-
return () => {
|
|
80
|
-
subscriptions.unsubscribe(subscriptionId);
|
|
81
|
-
};
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
if (parsed.id === void 0) {
|
|
85
|
-
const { params } = parsed;
|
|
86
|
-
const { subscription: subscriptionId, result, error } = params;
|
|
87
|
-
if (subscriptionId && subscriptions.has(subscriptionId) && ("result" in params || error)) {
|
|
88
|
-
if (error) subscriptions.error(subscriptionId, new RpcError(error));
|
|
89
|
-
else subscriptions.next(subscriptionId, result);
|
|
90
|
-
} else {
|
|
91
|
-
onNotification?.(parsed);
|
|
92
|
-
}
|
|
93
|
-
} else
|
|
94
|
-
console.warn("Error parsing incomming message: " + JSON.stringify(parsed));
|
|
95
|
-
}
|
|
96
|
-
connection = gProvider(onMessage);
|
|
97
|
-
const disconnect = () => {
|
|
98
|
-
connection?.disconnect();
|
|
99
|
-
connection = null;
|
|
100
|
-
subscriptions.errorAll(new DestroyedError());
|
|
101
|
-
responses.forEach((r) => r.onError(new DestroyedError()));
|
|
102
|
-
responses.clear();
|
|
103
|
-
};
|
|
104
|
-
let nextId = 1;
|
|
105
|
-
const request = (method, params, cb) => {
|
|
106
|
-
if (!connection) throw new Error("Not connected");
|
|
107
|
-
const id = `${clientId}-${nextId++}`;
|
|
108
|
-
if (cb) responses.set(id, cb);
|
|
109
|
-
send(id, method, params);
|
|
110
|
-
return () => {
|
|
111
|
-
responses.delete(id);
|
|
112
|
-
};
|
|
113
|
-
};
|
|
114
|
-
return {
|
|
115
|
-
request,
|
|
116
|
-
disconnect
|
|
117
|
-
};
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
exports.DestroyedError = DestroyedError;
|
|
121
|
-
exports.RpcError = RpcError;
|
|
122
|
-
exports.createClient = createClient;
|
|
123
|
-
exports.getSubscriptionsManager = getSubscriptionsManager;
|
|
1
|
+
export { RpcError } from './RpcError.js';
|
|
2
|
+
export { createClient } from './createClient.js';
|
|
3
|
+
export { DestroyedError } from './DestroyedError.js';
|
|
4
|
+
export { getSubscriptionsManager } from './subscriptions-manager.js';
|
|
124
5
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscriptions-manager.js","sources":["../src/subscriptions-manager.ts"],"sourcesContent":["export interface Subscriber<T> {\n next: (data: T) => void\n error: (e: Error) => void\n}\n\nexport const getSubscriptionsManager = <T>() => {\n const subscriptions = new Map<string, Subscriber<T>>()\n\n return {\n has: subscriptions.has.bind(subscriptions),\n subscribe(id: string, subscriber: Subscriber<T>) {\n subscriptions.set(id, subscriber)\n },\n unsubscribe(id: string) {\n subscriptions.delete(id)\n },\n next(id: string, data: T) {\n subscriptions.get(id)?.next(data)\n },\n error(id: string, e: Error) {\n const subscriber = subscriptions.get(id)\n if (subscriber) {\n subscriptions.delete(id)\n subscriber.error(e)\n }\n },\n errorAll(e: Error) {\n const subscribers = [...subscriptions.values()]\n subscriptions.clear()\n subscribers.forEach((s) => {\n s.error(e)\n })\n },\n }\n}\n\nexport type SubscriptionManager<T> = ReturnType<\n typeof getSubscriptionsManager<T>\n>\n"],"names":[],"mappings":"AAKO,MAAM,0BAA0B,MAAS;AAC9C,EAAA,MAAM,aAAA,uBAAoB,GAAA,EAA2B;AAErD,EAAA,OAAO;AAAA,IACL,GAAA,EAAK,aAAA,CAAc,GAAA,CAAI,IAAA,CAAK,aAAa,CAAA;AAAA,IACzC,SAAA,CAAU,IAAY,UAAA,EAA2B;AAC/C,MAAA,aAAA,CAAc,GAAA,CAAI,IAAI,UAAU,CAAA;AAAA,IAClC,CAAA;AAAA,IACA,YAAY,EAAA,EAAY;AACtB,MAAA,aAAA,CAAc,OAAO,EAAE,CAAA;AAAA,IACzB,CAAA;AAAA,IACA,IAAA,CAAK,IAAY,IAAA,EAAS;AACxB,MAAA,aAAA,CAAc,GAAA,CAAI,EAAE,CAAA,EAAG,IAAA,CAAK,IAAI,CAAA;AAAA,IAClC,CAAA;AAAA,IACA,KAAA,CAAM,IAAY,CAAA,EAAU;AAC1B,MAAA,MAAM,UAAA,GAAa,aAAA,CAAc,GAAA,CAAI,EAAE,CAAA;AACvC,MAAA,IAAI,UAAA,EAAY;AACd,QAAA,aAAA,CAAc,OAAO,EAAE,CAAA;AACvB,QAAA,UAAA,CAAW,MAAM,CAAC,CAAA;AAAA,MACpB;AAAA,IACF,CAAA;AAAA,IACA,SAAS,CAAA,EAAU;AACjB,MAAA,MAAM,WAAA,GAAc,CAAC,GAAG,aAAA,CAAc,QAAQ,CAAA;AAC9C,MAAA,aAAA,CAAc,KAAA,EAAM;AACpB,MAAA,WAAA,CAAY,OAAA,CAAQ,CAAC,CAAA,KAAM;AACzB,QAAA,CAAA,CAAE,MAAM,CAAC,CAAA;AAAA,MACX,CAAC,CAAA;AAAA,IACH;AAAA,GACF;AACF;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polkadot-api/raw-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"author": "Josep M Sobrepere (https://github.com/josepot)",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,34 +8,24 @@
|
|
|
8
8
|
},
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"sideEffects": false,
|
|
11
|
+
"type": "module",
|
|
11
12
|
"exports": {
|
|
12
13
|
".": {
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"require": "./dist/min/index.js",
|
|
17
|
-
"default": "./dist/index.js"
|
|
18
|
-
},
|
|
19
|
-
"import": "./dist/esm/index.mjs",
|
|
20
|
-
"require": "./dist/index.js",
|
|
21
|
-
"default": "./dist/index.js"
|
|
22
|
-
},
|
|
23
|
-
"module": "./dist/esm/index.mjs",
|
|
24
|
-
"import": "./dist/esm/index.mjs",
|
|
25
|
-
"require": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"import": "./dist/index.js",
|
|
26
17
|
"default": "./dist/index.js"
|
|
27
|
-
}
|
|
28
|
-
"./package.json": "./package.json"
|
|
18
|
+
}
|
|
29
19
|
},
|
|
30
20
|
"main": "./dist/index.js",
|
|
31
|
-
"module": "./dist/
|
|
32
|
-
"browser": "./dist/
|
|
21
|
+
"module": "./dist/index.js",
|
|
22
|
+
"browser": "./dist/index.js",
|
|
33
23
|
"types": "./dist/index.d.ts",
|
|
34
24
|
"files": [
|
|
35
25
|
"dist"
|
|
36
26
|
],
|
|
37
27
|
"dependencies": {
|
|
38
|
-
"@polkadot-api/json-rpc-provider": "0.
|
|
28
|
+
"@polkadot-api/json-rpc-provider": "0.2.0"
|
|
39
29
|
},
|
|
40
30
|
"scripts": {
|
|
41
31
|
"build-core": "tsc --noEmit && rollup -c ../../rollup.config.js",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"DestroyedError.mjs","sources":["../../src/DestroyedError.ts"],"sourcesContent":["export class DestroyedError extends Error {\n constructor() {\n super(\"Client destroyed\")\n this.name = \"DestroyedError\"\n }\n}\n"],"names":[],"mappings":"AAAO,MAAM,uBAAuB,KAAA,CAAM;AAAA,EACxC,WAAA,GAAc;AACZ,IAAA,KAAA,CAAM,kBAAkB,CAAA;AACxB,IAAA,IAAA,CAAK,IAAA,GAAO,gBAAA;AAAA,EACd;AACF;;;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RpcError.mjs","sources":["../../src/RpcError.ts"],"sourcesContent":["export interface IRpcError {\n code: number\n message: string\n data?: any\n}\n\nexport class RpcError extends Error implements IRpcError {\n code\n data\n constructor(e: IRpcError) {\n super(e.message)\n this.code = e.code\n this.data = e.data\n this.name = \"RpcError\"\n }\n}\n"],"names":[],"mappings":";;;AAMO,MAAM,iBAAiB,KAAA,CAA2B;AAAA,EAGvD,YAAY,CAAA,EAAc;AACxB,IAAA,KAAA,CAAM,EAAE,OAAO,CAAA;AAHjB,IAAA,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AACA,IAAA,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA;AAGE,IAAA,IAAA,CAAK,OAAO,CAAA,CAAE,IAAA;AACd,IAAA,IAAA,CAAK,OAAO,CAAA,CAAE,IAAA;AACd,IAAA,IAAA,CAAK,IAAA,GAAO,UAAA;AAAA,EACd;AACF;;;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createClient.mjs","sources":["../../src/createClient.ts"],"sourcesContent":["import {\n isResponse,\n JsonRpcRequest,\n type JsonRpcConnection,\n type JsonRpcId,\n type JsonRpcMessage,\n type JsonRpcProvider,\n} from \"@polkadot-api/json-rpc-provider\"\nimport { RpcError } from \"./RpcError\"\nimport { getSubscriptionsManager, Subscriber } from \"./subscriptions-manager\"\nimport { DestroyedError } from \"./DestroyedError\"\n\ntype UnsubscribeFn = () => void\nexport type FollowSubscriptionCb<T> = (\n subscriptionId: string,\n cb: Subscriber<T>,\n) => UnsubscribeFn\n\nexport type ClientRequestCb<T, TT> = {\n onSuccess: (result: T, followSubscription: FollowSubscriptionCb<TT>) => void\n onError: (e: Error) => void\n}\n\nexport type ClientRequest<T, TT> = (\n method: string,\n params: Array<any>,\n cb?: ClientRequestCb<T, TT>,\n) => UnsubscribeFn\n\nexport interface Client {\n disconnect: () => void\n request: ClientRequest<any, any>\n}\n\nlet nextClientId = 1\nexport const createClient = (\n gProvider: JsonRpcProvider,\n onNotification?: (req: JsonRpcRequest) => void,\n): Client => {\n let clientId = nextClientId++\n const responses = new Map<JsonRpcId, ClientRequestCb<any, any>>()\n const subscriptions = getSubscriptionsManager()\n\n let connection: JsonRpcConnection | null = null\n const send = (\n id: string,\n method: string,\n params: Array<boolean | string | number | null>,\n ) => {\n connection!.send({\n jsonrpc: \"2.0\",\n id,\n method,\n params,\n })\n }\n\n function onMessage(parsed: JsonRpcMessage): void {\n if (isResponse(parsed)) {\n const { id } = parsed\n const cb = responses.get(id)\n if (!cb) return\n\n responses.delete(id)\n\n return \"error\" in parsed\n ? cb.onError(new RpcError(parsed.error))\n : cb.onSuccess(parsed.result, (opaqueId, subscriber) => {\n const subscriptionId = opaqueId\n subscriptions.subscribe(subscriptionId, subscriber)\n return () => {\n subscriptions.unsubscribe(subscriptionId)\n }\n })\n }\n\n if (parsed.id === undefined) {\n // at this point, it means that it should be a notification\n const { params } = parsed\n const { subscription: subscriptionId, result, error } = params\n if (\n subscriptionId &&\n subscriptions.has(subscriptionId) &&\n (\"result\" in params || error)\n ) {\n if (error) subscriptions.error(subscriptionId, new RpcError(error!))\n else subscriptions.next(subscriptionId, result)\n } else {\n onNotification?.(parsed)\n }\n } else\n console.warn(\"Error parsing incomming message: \" + JSON.stringify(parsed))\n }\n\n connection = gProvider(onMessage)\n\n const disconnect = () => {\n connection?.disconnect()\n connection = null\n subscriptions.errorAll(new DestroyedError())\n responses.forEach((r) => r.onError(new DestroyedError()))\n responses.clear()\n }\n\n let nextId = 1\n const request = <T, TT>(\n method: string,\n params: Array<any>,\n cb?: ClientRequestCb<T, TT>,\n ): UnsubscribeFn => {\n if (!connection) throw new Error(\"Not connected\")\n const id = `${clientId}-${nextId++}`\n\n if (cb) responses.set(id, cb)\n send(id, method, params)\n\n return (): void => {\n responses.delete(id)\n }\n }\n\n return {\n request,\n disconnect,\n }\n}\n"],"names":[],"mappings":";;;;;AAkCA,IAAI,YAAA,GAAe,CAAA;AACZ,MAAM,YAAA,GAAe,CAC1B,SAAA,EACA,cAAA,KACW;AACX,EAAA,IAAI,QAAA,GAAW,YAAA,EAAA;AACf,EAAA,MAAM,SAAA,uBAAgB,GAAA,EAA0C;AAChE,EAAA,MAAM,gBAAgB,uBAAA,EAAwB;AAE9C,EAAA,IAAI,UAAA,GAA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esm/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"subscriptions-manager.mjs","sources":["../../src/subscriptions-manager.ts"],"sourcesContent":["export interface Subscriber<T> {\n next: (data: T) => void\n error: (e: Error) => void\n}\n\nexport const getSubscriptionsManager = <T>() => {\n const subscriptions = new Map<string, Subscriber<T>>()\n\n return {\n has: subscriptions.has.bind(subscriptions),\n subscribe(id: string, subscriber: Subscriber<T>) {\n subscriptions.set(id, subscriber)\n },\n unsubscribe(id: string) {\n subscriptions.delete(id)\n },\n next(id: string, data: T) {\n subscriptions.get(id)?.next(data)\n },\n error(id: string, e: Error) {\n const subscriber = subscriptions.get(id)\n if (subscriber) {\n subscriptions.delete(id)\n subscriber.error(e)\n }\n },\n errorAll(e: Error) {\n const subscribers = [...subscriptions.values()]\n subscriptions.clear()\n subscribers.forEach((s) => {\n s.error(e)\n })\n },\n }\n}\n\nexport type SubscriptionManager<T> = ReturnType<\n typeof getSubscriptionsManager<T>\n>\n"],"names":[],"mappings":"AAKO,MAAM,0BAA0B,MAAS;AAC9C,EAAA,MAAM,aAAA,uBAAoB,GAAA,EAA2B;AAErD,EAAA,OAAO;AAAA,IACL,GAAA,EAAK,aAAA,CAAc,GAAA,CAAI,IAAA,CAAK,aAAa,CAAA;AAAA,IACzC,SAAA,CAAU,IAAY,UAAA,EAA2B;AAC/C,MAAA,aAAA,CAAc,GAAA,CAAI,IAAI,UAAU,CAAA;AAAA,IAClC,CAAA;AAAA,IACA,YAAY,EAAA,EAAY;AACtB,MAAA,aAAA,CAAc,OAAO,EAAE,CAAA;AAAA,IACzB,CAAA;AAAA,IACA,IAAA,CAAK,IAAY,IAAA,EAAS;AACxB,MAAA,aAAA,CAAc,GAAA,CAAI,EAAE,CAAA,EAAG,IAAA,CAAK,IAAI,CAAA;AAAA,IAClC,CAAA;AAAA,IACA,KAAA,CAAM,IAAY,CAAA,EAAU;AAC1B,MAAA,MAAM,UAAA,GAAa,aAAA,CAAc,GAAA,CAAI,EAAE,CAAA;AACvC,MAAA,IAAI,UAAA,EAAY;AACd,QAAA,aAAA,CAAc,OAAO,EAAE,CAAA;AACvB,QAAA,UAAA,CAAW,MAAM,CAAC,CAAA;AAAA,MACpB;AAAA,IACF,CAAA;AAAA,IACA,SAAS,CAAA,EAAU;AACjB,MAAA,MAAM,WAAA,GAAc,CAAC,GAAG,aAAA,CAAc,QAAQ,CAAA;AAC9C,MAAA,aAAA,CAAc,KAAA,EAAM;AACpB,MAAA,WAAA,CAAY,OAAA,CAAQ,CAAC,CAAA,KAAM;AACzB,QAAA,CAAA,CAAE,MAAM,CAAC,CAAA;AAAA,MACX,CAAC,CAAA;AAAA,IACH;AAAA,GACF;AACF;;;;"}
|