@polkadot-api/substrate-client 0.4.0 → 0.4.2
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/archive/archive.mjs.map +1 -1
- package/dist/esm/archive/errors.mjs.map +1 -1
- package/dist/esm/archive/storage-subscription.mjs.map +1 -1
- package/dist/esm/archive/storage.mjs.map +1 -1
- package/dist/esm/chainhead/body.mjs.map +1 -1
- package/dist/esm/chainhead/call.mjs.map +1 -1
- package/dist/esm/chainhead/chainhead.mjs +50 -48
- package/dist/esm/chainhead/chainhead.mjs.map +1 -1
- package/dist/esm/chainhead/errors.mjs.map +1 -1
- package/dist/esm/chainhead/header.mjs.map +1 -1
- package/dist/esm/chainhead/operation-promise.mjs.map +1 -1
- package/dist/esm/chainhead/storage-subscription.mjs.map +1 -1
- package/dist/esm/chainhead/storage.mjs.map +1 -1
- package/dist/esm/chainhead/unpin.mjs.map +1 -1
- package/dist/esm/chainspec.mjs.map +1 -1
- package/dist/esm/index.mjs +1 -2
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/internal-utils/abortablePromiseFn.mjs.map +1 -1
- package/dist/esm/internal-utils/deferred-promise.mjs.map +1 -1
- package/dist/esm/methods.mjs.map +1 -1
- package/dist/esm/substrate-client.mjs +1 -1
- package/dist/esm/substrate-client.mjs.map +1 -1
- package/dist/esm/transaction/transaction.mjs.map +1 -1
- package/dist/index.d.ts +5 -33
- package/dist/index.js +87 -199
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
- package/dist/esm/client/DestroyedError.mjs +0 -9
- package/dist/esm/client/DestroyedError.mjs.map +0 -1
- package/dist/esm/client/RpcError.mjs +0 -16
- package/dist/esm/client/RpcError.mjs.map +0 -1
- package/dist/esm/client/createClient.mjs +0 -78
- package/dist/esm/client/createClient.mjs.map +0 -1
- package/dist/esm/internal-utils/subscriptions-manager.mjs +0 -32
- package/dist/esm/internal-utils/subscriptions-manager.mjs.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RpcError.mjs","sources":["../../../src/client/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,KAA2B,CAAA;AAAA,EAGvD,YAAY,CAAc,EAAA;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,CAAE,CAAA,IAAA;AACd,IAAA,IAAA,CAAK,OAAO,CAAE,CAAA,IAAA;AACd,IAAA,IAAA,CAAK,IAAO,GAAA,UAAA;AAAA;AAEhB;;;;"}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { RpcError } from './RpcError.mjs';
|
|
2
|
-
import '@polkadot-api/utils';
|
|
3
|
-
import { getSubscriptionsManager } from '../internal-utils/subscriptions-manager.mjs';
|
|
4
|
-
import { DestroyedError } from './DestroyedError.mjs';
|
|
5
|
-
|
|
6
|
-
let nextClientId = 1;
|
|
7
|
-
const createClient = (gProvider) => {
|
|
8
|
-
let clientId = nextClientId++;
|
|
9
|
-
const responses = /* @__PURE__ */ new Map();
|
|
10
|
-
const subscriptions = getSubscriptionsManager();
|
|
11
|
-
let connection = null;
|
|
12
|
-
const send = (id, method, params) => {
|
|
13
|
-
connection.send(
|
|
14
|
-
JSON.stringify({
|
|
15
|
-
jsonrpc: "2.0",
|
|
16
|
-
id,
|
|
17
|
-
method,
|
|
18
|
-
params
|
|
19
|
-
})
|
|
20
|
-
);
|
|
21
|
-
};
|
|
22
|
-
function onMessage(message) {
|
|
23
|
-
try {
|
|
24
|
-
let id, result, error, params, subscription;
|
|
25
|
-
const parsed = JSON.parse(message);
|
|
26
|
-
({ id, result, error, params } = parsed);
|
|
27
|
-
if (id) {
|
|
28
|
-
const cb = responses.get(id);
|
|
29
|
-
if (!cb) return;
|
|
30
|
-
responses.delete(id);
|
|
31
|
-
return error ? cb.onError(new RpcError(error)) : cb.onSuccess(result, (opaqueId, subscriber) => {
|
|
32
|
-
const subscriptionId2 = opaqueId;
|
|
33
|
-
subscriptions.subscribe(subscriptionId2, subscriber);
|
|
34
|
-
return () => {
|
|
35
|
-
subscriptions.unsubscribe(subscriptionId2);
|
|
36
|
-
};
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
;
|
|
40
|
-
({ subscription, result, error } = params);
|
|
41
|
-
if (!subscription || !error && !Object.hasOwn(params, "result")) throw 0;
|
|
42
|
-
const subscriptionId = subscription;
|
|
43
|
-
if (error) {
|
|
44
|
-
subscriptions.error(subscriptionId, new RpcError(error));
|
|
45
|
-
} else {
|
|
46
|
-
subscriptions.next(subscriptionId, result);
|
|
47
|
-
}
|
|
48
|
-
} catch (e) {
|
|
49
|
-
console.warn("Error parsing incomming message: " + message);
|
|
50
|
-
console.error(e);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
connection = gProvider(onMessage);
|
|
54
|
-
const disconnect = () => {
|
|
55
|
-
connection?.disconnect();
|
|
56
|
-
connection = null;
|
|
57
|
-
subscriptions.errorAll(new DestroyedError());
|
|
58
|
-
responses.forEach((r) => r.onError(new DestroyedError()));
|
|
59
|
-
responses.clear();
|
|
60
|
-
};
|
|
61
|
-
let nextId = 1;
|
|
62
|
-
const request = (method, params, cb) => {
|
|
63
|
-
if (!connection) throw new Error("Not connected");
|
|
64
|
-
const id = `${clientId}-${nextId++}`;
|
|
65
|
-
if (cb) responses.set(id, cb);
|
|
66
|
-
send(id, method, params);
|
|
67
|
-
return () => {
|
|
68
|
-
responses.delete(id);
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
return {
|
|
72
|
-
request,
|
|
73
|
-
disconnect
|
|
74
|
-
};
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
export { createClient };
|
|
78
|
-
//# sourceMappingURL=createClient.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createClient.mjs","sources":["../../../src/client/createClient.ts"],"sourcesContent":["import type {\n JsonRpcConnection,\n JsonRpcProvider,\n} from \"@polkadot-api/json-rpc-provider\"\nimport { UnsubscribeFn } from \"../common-types\"\nimport { RpcError, IRpcError } from \"./RpcError\"\nimport { getSubscriptionsManager, Subscriber } from \"@/internal-utils\"\nimport { DestroyedError } from \"./DestroyedError\"\n\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 = (gProvider: JsonRpcProvider): Client => {\n let clientId = nextClientId++\n const responses = new Map<string, ClientRequestCb<any, any>>()\n const subscriptions = getSubscriptionsManager()\n\n let connection: JsonRpcConnection | null = null\n\n const send = (\n id: string,\n method: string,\n params: Array<boolean | string | number | null>,\n ) => {\n connection!.send(\n JSON.stringify({\n jsonrpc: \"2.0\",\n id,\n method,\n params,\n }),\n )\n }\n\n function onMessage(message: string): void {\n try {\n let id: string,\n result,\n error: IRpcError | undefined,\n params: { subscription: any; result: any; error?: IRpcError },\n subscription: string\n\n const parsed = JSON.parse(message)\n ;({ id, result, error, params } = parsed)\n\n if (id) {\n const cb = responses.get(id)\n if (!cb) return\n\n responses.delete(id)\n\n return error\n ? cb.onError(new RpcError(error))\n : cb.onSuccess(result, (opaqueId, subscriber) => {\n const subscriptionId = opaqueId\n subscriptions.subscribe(subscriptionId, subscriber)\n return () => {\n subscriptions.unsubscribe(subscriptionId)\n }\n })\n }\n\n // at this point, it means that it should be a notification\n ;({ subscription, result, error } = params)\n if (!subscription || (!error && !Object.hasOwn(params, \"result\"))) throw 0\n\n const subscriptionId = subscription\n\n if (error) {\n subscriptions.error(subscriptionId, new RpcError(error!))\n } else {\n subscriptions.next(subscriptionId, result)\n }\n } catch (e) {\n console.warn(\"Error parsing incomming message: \" + message)\n console.error(e)\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":["subscriptionId"],"mappings":";;;;;AA8BA,IAAI,YAAe,GAAA,CAAA;AACN,MAAA,YAAA,GAAe,CAAC,SAAuC,KAAA;AAClE,EAAA,IAAI,QAAW,GAAA,YAAA,EAAA;AACf,EAAM,MAAA,SAAA,uBAAgB,GAAuC,EAAA;AAC7D,EAAA,MAAM,gBAAgB,uBAAwB,EAAA;AAE9C,EAAA,IAAI,UAAuC,GAAA,IAAA;AAE3C,EAAA,MAAM,IAAO,GAAA,CACX,EACA,EAAA,MAAA,EACA,MACG,KAAA;AACH,IAAY,UAAA,CAAA,IAAA;AAAA,MACV,KAAK,SAAU,CAAA;AAAA,QACb,OAAS,EAAA,KAAA;AAAA,QACT,EAAA;AAAA,QACA,MAAA;AAAA,QACA;AAAA,OACD;AAAA,KACH;AAAA,GACF;AAEA,EAAA,SAAS,UAAU,OAAuB,EAAA;AACxC,IAAI,IAAA;AACF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IAAA,CAAK,sCAAsC,OAAO,CAAA;AAC1D,MAAA,OAAA,CAAQ,MAAM,CAAC,CAAA;AAAA;AACjB;AAEF,EAAA,UAAA,GAAa,UAAU,SAAS,CAAA;AAEhC,EAAA,MAAM,aAAa,MAAM;AACvB,IAAA,UAAA,EAAY,UAAW,EAAA;AACvB,IAAa,UAAA,GAAA,IAAA;AACb,IAAc,aAAA,CAAA,QAAA,CAAS,IAAI,cAAA,EAAgB,CAAA;AAC3C,IAAU,SAAA,CAAA,OAAA,CAAQ,CAAC,CAAM,KAAA,CAAA,CAAE,QAAQ,IAAI,cAAA,EAAgB,CAAC,CAAA;AACxD,IAAA,SAAA,CAAU,KAAM,EAAA;AAAA,GAClB;AAEA,EAAA,IAAI,MAAS,GAAA,CAAA;AACb,EAAA,MAAM,OAAU,GAAA,CACd,MACA,EAAA,MAAA,EACA,EACkB,KAAA;AAClB,IAAA,IAAI,CAAC,UAAA,EAAkB,MAAA,IAAI,MAAM,eAAe,CAAA;AAChD,IAAA,MAAM,EAAK,GAAA,CAAA,EAAG,QAAQ,CAAA,CAAA,EAAI,MAAQ,EAAA,CAAA,CAAA;AAElC,IAAA,IAAI,EAAI,EAAA,SAAA,CAAU,GAAI,CAAA,EAAA,EAAI,EAAE,CAAA;AAC5B,IAAK,IAAA,CAAA,EAAA,EAAI,QAAQ,MAAM,CAAA;AAEvB,IAAA,OAAO,MAAY;AACjB,MAAA,SAAA,CAAU,OAAO,EAAE,CAAA;AAAA,KACrB;AAAA,GACF;AAEA,EAAO,OAAA;AAAA,IACL,OAAA;AAAA,IACA;AAAA,GACF;AACF;;;;"}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const getSubscriptionsManager = () => {
|
|
2
|
-
const subscriptions = /* @__PURE__ */ new Map();
|
|
3
|
-
return {
|
|
4
|
-
has: subscriptions.has.bind(subscriptions),
|
|
5
|
-
subscribe(id, subscriber) {
|
|
6
|
-
subscriptions.set(id, subscriber);
|
|
7
|
-
},
|
|
8
|
-
unsubscribe(id) {
|
|
9
|
-
subscriptions.delete(id);
|
|
10
|
-
},
|
|
11
|
-
next(id, data) {
|
|
12
|
-
subscriptions.get(id)?.next(data);
|
|
13
|
-
},
|
|
14
|
-
error(id, e) {
|
|
15
|
-
const subscriber = subscriptions.get(id);
|
|
16
|
-
if (subscriber) {
|
|
17
|
-
subscriptions.delete(id);
|
|
18
|
-
subscriber.error(e);
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
errorAll(e) {
|
|
22
|
-
const subscribers = [...subscriptions.values()];
|
|
23
|
-
subscriptions.clear();
|
|
24
|
-
subscribers.forEach((s) => {
|
|
25
|
-
s.error(e);
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export { getSubscriptionsManager };
|
|
32
|
-
//# sourceMappingURL=subscriptions-manager.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"subscriptions-manager.mjs","sources":["../../../src/internal-utils/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,EAAM,MAAA,aAAA,uBAAoB,GAA2B,EAAA;AAErD,EAAO,OAAA;AAAA,IACL,GAAK,EAAA,aAAA,CAAc,GAAI,CAAA,IAAA,CAAK,aAAa,CAAA;AAAA,IACzC,SAAA,CAAU,IAAY,UAA2B,EAAA;AAC/C,MAAc,aAAA,CAAA,GAAA,CAAI,IAAI,UAAU,CAAA;AAAA,KAClC;AAAA,IACA,YAAY,EAAY,EAAA;AACtB,MAAA,aAAA,CAAc,OAAO,EAAE,CAAA;AAAA,KACzB;AAAA,IACA,IAAA,CAAK,IAAY,IAAS,EAAA;AACxB,MAAA,aAAA,CAAc,GAAI,CAAA,EAAE,CAAG,EAAA,IAAA,CAAK,IAAI,CAAA;AAAA,KAClC;AAAA,IACA,KAAA,CAAM,IAAY,CAAU,EAAA;AAC1B,MAAM,MAAA,UAAA,GAAa,aAAc,CAAA,GAAA,CAAI,EAAE,CAAA;AACvC,MAAA,IAAI,UAAY,EAAA;AACd,QAAA,aAAA,CAAc,OAAO,EAAE,CAAA;AACvB,QAAA,UAAA,CAAW,MAAM,CAAC,CAAA;AAAA;AACpB,KACF;AAAA,IACA,SAAS,CAAU,EAAA;AACjB,MAAA,MAAM,WAAc,GAAA,CAAC,GAAG,aAAA,CAAc,QAAQ,CAAA;AAC9C,MAAA,aAAA,CAAc,KAAM,EAAA;AACpB,MAAY,WAAA,CAAA,OAAA,CAAQ,CAAC,CAAM,KAAA;AACzB,QAAA,CAAA,CAAE,MAAM,CAAC,CAAA;AAAA,OACV,CAAA;AAAA;AACH,GACF;AACF;;;;"}
|