@polkadot-api/substrate-client 0.1.4 → 0.2.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/chainhead/body.mjs +15 -0
- package/dist/esm/chainhead/body.mjs.map +1 -0
- package/dist/esm/chainhead/call.mjs +15 -0
- package/dist/esm/chainhead/call.mjs.map +1 -0
- package/dist/esm/chainhead/chainhead.mjs +145 -0
- package/dist/esm/chainhead/chainhead.mjs.map +1 -0
- package/dist/esm/chainhead/errors.mjs +33 -0
- package/dist/esm/chainhead/errors.mjs.map +1 -0
- package/dist/esm/chainhead/header.mjs +11 -0
- package/dist/esm/chainhead/header.mjs.map +1 -0
- package/dist/esm/chainhead/operation-promise.mjs +58 -0
- package/dist/esm/chainhead/operation-promise.mjs.map +1 -0
- package/dist/esm/chainhead/storage-subscription.mjs +72 -0
- package/dist/esm/chainhead/storage-subscription.mjs.map +1 -0
- package/dist/esm/chainhead/storage.mjs +42 -0
- package/dist/esm/chainhead/storage.mjs.map +1 -0
- package/dist/esm/chainhead/unpin.mjs +13 -0
- package/dist/esm/chainhead/unpin.mjs.map +1 -0
- package/dist/esm/chainspec.mjs +24 -0
- package/dist/esm/chainspec.mjs.map +1 -0
- package/dist/esm/client/DestroyedError.mjs +9 -0
- package/dist/esm/client/DestroyedError.mjs.map +1 -0
- package/dist/esm/client/RpcError.mjs +16 -0
- package/dist/esm/client/RpcError.mjs.map +1 -0
- package/dist/esm/client/createClient.mjs +78 -0
- package/dist/esm/client/createClient.mjs.map +1 -0
- package/dist/esm/index.mjs +26 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/internal-utils/abortablePromiseFn.mjs +20 -0
- package/dist/esm/internal-utils/abortablePromiseFn.mjs.map +1 -0
- package/dist/esm/internal-utils/deferred-promise.mjs +14 -0
- package/dist/esm/internal-utils/deferred-promise.mjs.map +1 -0
- package/dist/esm/internal-utils/noop.mjs +5 -0
- package/dist/esm/internal-utils/noop.mjs.map +1 -0
- package/dist/esm/internal-utils/subscriptions-manager.mjs +32 -0
- package/dist/esm/internal-utils/subscriptions-manager.mjs.map +1 -0
- package/dist/esm/methods.mjs +31 -0
- package/dist/esm/methods.mjs.map +1 -0
- package/dist/esm/transaction/transaction.mjs +23 -0
- package/dist/esm/transaction/transaction.mjs.map +1 -0
- package/dist/index.d.ts +0 -3
- package/dist/index.js +73 -184
- package/dist/index.js.map +1 -1
- package/package.json +10 -10
- package/dist/index.d.mts +0 -239
- package/dist/index.mjs +0 -686
- package/dist/index.mjs.map +0 -1
- package/dist/min/index.d.ts +0 -239
- package/dist/min/index.js +0 -2
- package/dist/min/index.js.map +0 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { chainHead } from '../methods.mjs';
|
|
2
|
+
import { createOperationPromise } from './operation-promise.mjs';
|
|
3
|
+
|
|
4
|
+
const createBodyFn = createOperationPromise(
|
|
5
|
+
chainHead.body,
|
|
6
|
+
(hash) => [
|
|
7
|
+
[hash],
|
|
8
|
+
(e, res) => {
|
|
9
|
+
res(e.value);
|
|
10
|
+
}
|
|
11
|
+
]
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
export { createBodyFn };
|
|
15
|
+
//# sourceMappingURL=body.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body.mjs","sources":["../../../src/chainhead/body.ts"],"sourcesContent":["import { chainHead } from \"@/methods\"\nimport type { OperationBodyDoneRpc } from \"./json-rpc-types\"\nimport { createOperationPromise } from \"./operation-promise\"\n\nexport const createBodyFn = createOperationPromise(\n chainHead.body,\n (hash: string) => [\n [hash],\n (e: OperationBodyDoneRpc, res: (x: Array<string>) => void) => {\n res(e.value)\n },\n ],\n)\n"],"names":[],"mappings":";;;AAIO,MAAM,YAAe,GAAA,sBAAA;AAAA,EAC1B,SAAU,CAAA,IAAA;AAAA,EACV,CAAC,IAAiB,KAAA;AAAA,IAChB,CAAC,IAAI,CAAA;AAAA,IACL,CAAC,GAAyB,GAAoC,KAAA;AAC5D,MAAA,GAAA,CAAI,EAAE,KAAK,CAAA,CAAA;AAAA,KACb;AAAA,GACF;AACF;;;;"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { chainHead } from '../methods.mjs';
|
|
2
|
+
import { createOperationPromise } from './operation-promise.mjs';
|
|
3
|
+
|
|
4
|
+
const createCallFn = createOperationPromise(
|
|
5
|
+
chainHead.call,
|
|
6
|
+
(hash, fnName, callParameters) => [
|
|
7
|
+
[hash, fnName, callParameters],
|
|
8
|
+
(e, res) => {
|
|
9
|
+
res(e.output);
|
|
10
|
+
}
|
|
11
|
+
]
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
export { createCallFn };
|
|
15
|
+
//# sourceMappingURL=call.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"call.mjs","sources":["../../../src/chainhead/call.ts"],"sourcesContent":["import { chainHead } from \"@/methods\"\nimport type { OperationCallDoneRpc } from \"./json-rpc-types\"\nimport { createOperationPromise } from \"./operation-promise\"\n\nexport const createCallFn = createOperationPromise(\n chainHead.call,\n (hash: string, fnName: string, callParameters: string) => [\n [hash, fnName, callParameters],\n (e: OperationCallDoneRpc, res: (output: string) => void) => {\n res(e.output)\n },\n ],\n)\n"],"names":[],"mappings":";;;AAIO,MAAM,YAAe,GAAA,sBAAA;AAAA,EAC1B,SAAU,CAAA,IAAA;AAAA,EACV,CAAC,IAAc,EAAA,MAAA,EAAgB,cAA2B,KAAA;AAAA,IACxD,CAAC,IAAM,EAAA,MAAA,EAAQ,cAAc,CAAA;AAAA,IAC7B,CAAC,GAAyB,GAAkC,KAAA;AAC1D,MAAA,GAAA,CAAI,EAAE,MAAM,CAAA,CAAA;AAAA,KACd;AAAA,GACF;AACF;;;;"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import '@polkadot-api/utils';
|
|
2
|
+
import { deferred } from '../internal-utils/deferred-promise.mjs';
|
|
3
|
+
import { noop } from '../internal-utils/noop.mjs';
|
|
4
|
+
import { getSubscriptionsManager } from '../internal-utils/subscriptions-manager.mjs';
|
|
5
|
+
import { createBodyFn } from './body.mjs';
|
|
6
|
+
import { createCallFn } from './call.mjs';
|
|
7
|
+
import { createHeaderFn } from './header.mjs';
|
|
8
|
+
import { createStorageFn } from './storage.mjs';
|
|
9
|
+
import { createUnpinFn } from './unpin.mjs';
|
|
10
|
+
import { DisjointError, StopError } from './errors.mjs';
|
|
11
|
+
import { createStorageCb } from './storage-subscription.mjs';
|
|
12
|
+
import { DestroyedError } from '../client/DestroyedError.mjs';
|
|
13
|
+
import { chainHead } from '../methods.mjs';
|
|
14
|
+
|
|
15
|
+
function isOperationEvent(event) {
|
|
16
|
+
return event.operationId !== void 0;
|
|
17
|
+
}
|
|
18
|
+
function getChainHead(request) {
|
|
19
|
+
return (withRuntime, onFollowEvent, onFollowError) => {
|
|
20
|
+
const subscriptions = getSubscriptionsManager();
|
|
21
|
+
const ongoingRequests = /* @__PURE__ */ new Set();
|
|
22
|
+
const deferredFollow = deferred();
|
|
23
|
+
let followSubscription = deferredFollow.promise;
|
|
24
|
+
const onAllFollowEventsNext = (event) => {
|
|
25
|
+
if (isOperationEvent(event)) {
|
|
26
|
+
if (!subscriptions.has(event.operationId))
|
|
27
|
+
console.warn("Uknown operationId on", event);
|
|
28
|
+
return subscriptions.next(event.operationId, event);
|
|
29
|
+
}
|
|
30
|
+
if (event.event !== "stop") {
|
|
31
|
+
if (event.event === "initialized") {
|
|
32
|
+
return onFollowEvent({
|
|
33
|
+
type: event.event,
|
|
34
|
+
finalizedBlockHashes: event.finalizedBlockHashes,
|
|
35
|
+
finalizedBlockRuntime: event.finalizedBlockRuntime
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const { event: type, ...rest } = event;
|
|
39
|
+
return onFollowEvent({ type, ...rest });
|
|
40
|
+
}
|
|
41
|
+
onFollowError(new StopError());
|
|
42
|
+
unfollow(false);
|
|
43
|
+
};
|
|
44
|
+
const onAllFollowEventsError = (error) => {
|
|
45
|
+
onFollowError(error);
|
|
46
|
+
unfollow(!(error instanceof DestroyedError));
|
|
47
|
+
};
|
|
48
|
+
const onFollowRequestSuccess = (subscriptionId, follow) => {
|
|
49
|
+
const done = follow(subscriptionId, {
|
|
50
|
+
next: onAllFollowEventsNext,
|
|
51
|
+
error: onAllFollowEventsError
|
|
52
|
+
});
|
|
53
|
+
unfollow = (sendUnfollow = true) => {
|
|
54
|
+
followSubscription = null;
|
|
55
|
+
unfollow = noop;
|
|
56
|
+
done();
|
|
57
|
+
sendUnfollow && request(chainHead.unfollow, [subscriptionId]);
|
|
58
|
+
subscriptions.errorAll(new DisjointError());
|
|
59
|
+
ongoingRequests.forEach((cb) => {
|
|
60
|
+
cb();
|
|
61
|
+
});
|
|
62
|
+
ongoingRequests.clear();
|
|
63
|
+
};
|
|
64
|
+
followSubscription = subscriptionId;
|
|
65
|
+
deferredFollow.res(subscriptionId);
|
|
66
|
+
};
|
|
67
|
+
const onFollowRequestError = (e) => {
|
|
68
|
+
if (e instanceof DestroyedError) {
|
|
69
|
+
unfollow(false);
|
|
70
|
+
} else {
|
|
71
|
+
onFollowError(e);
|
|
72
|
+
}
|
|
73
|
+
followSubscription = null;
|
|
74
|
+
deferredFollow.res(e);
|
|
75
|
+
};
|
|
76
|
+
let unfollow = request(
|
|
77
|
+
chainHead.follow,
|
|
78
|
+
[withRuntime],
|
|
79
|
+
{ onSuccess: onFollowRequestSuccess, onError: onFollowRequestError }
|
|
80
|
+
);
|
|
81
|
+
const fRequest = (method, params, cb) => {
|
|
82
|
+
const disjoint = () => {
|
|
83
|
+
cb?.onError(new DisjointError());
|
|
84
|
+
};
|
|
85
|
+
if (followSubscription === null) {
|
|
86
|
+
disjoint();
|
|
87
|
+
return noop;
|
|
88
|
+
}
|
|
89
|
+
const onSubscription = (subscription) => {
|
|
90
|
+
if (!cb) return request(method, [subscription, ...params]);
|
|
91
|
+
ongoingRequests.add(disjoint);
|
|
92
|
+
const onSubscribeOperation = (operationId, subscriber) => {
|
|
93
|
+
if (followSubscription === null) {
|
|
94
|
+
subscriber.error(new DisjointError());
|
|
95
|
+
return noop;
|
|
96
|
+
}
|
|
97
|
+
subscriptions.subscribe(operationId, subscriber);
|
|
98
|
+
return () => {
|
|
99
|
+
subscriptions.unsubscribe(operationId);
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
const cleanup = request(method, [subscription, ...params], {
|
|
103
|
+
onSuccess: (response) => {
|
|
104
|
+
ongoingRequests.delete(disjoint);
|
|
105
|
+
cb.onSuccess(response, onSubscribeOperation);
|
|
106
|
+
},
|
|
107
|
+
onError: (e) => {
|
|
108
|
+
ongoingRequests.delete(disjoint);
|
|
109
|
+
cb.onError(e);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
return () => {
|
|
113
|
+
ongoingRequests.delete(disjoint);
|
|
114
|
+
cleanup();
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
if (typeof followSubscription === "string")
|
|
118
|
+
return onSubscription(followSubscription);
|
|
119
|
+
let onCancel = noop;
|
|
120
|
+
followSubscription.then((x) => {
|
|
121
|
+
if (x instanceof Error) return disjoint();
|
|
122
|
+
if (followSubscription) onCancel = onSubscription(x);
|
|
123
|
+
});
|
|
124
|
+
return () => {
|
|
125
|
+
onCancel();
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
return {
|
|
129
|
+
unfollow() {
|
|
130
|
+
unfollow();
|
|
131
|
+
followSubscription = null;
|
|
132
|
+
},
|
|
133
|
+
body: createBodyFn(fRequest),
|
|
134
|
+
call: createCallFn(fRequest),
|
|
135
|
+
header: createHeaderFn(fRequest),
|
|
136
|
+
storage: createStorageFn(fRequest),
|
|
137
|
+
storageSubscription: createStorageCb(fRequest),
|
|
138
|
+
unpin: createUnpinFn(fRequest),
|
|
139
|
+
_request: fRequest
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { getChainHead };
|
|
145
|
+
//# sourceMappingURL=chainhead.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chainhead.mjs","sources":["../../../src/chainhead/chainhead.ts"],"sourcesContent":["import type { ClientRequest, FollowSubscriptionCb } from \"@/client\"\nimport type {\n FollowEventWithRuntimeRpc,\n FollowEventWithoutRuntimeRpc,\n OperationEventsRpc,\n StopRpc,\n} from \"./json-rpc-types\"\nimport type {\n ChainHead,\n ClientInnerRequest,\n FollowEventWithoutRuntime,\n FollowEventWithRuntime,\n FollowResponse,\n} from \"./public-types\"\nimport {\n Subscriber,\n getSubscriptionsManager,\n noop,\n deferred,\n} from \"@/internal-utils\"\nimport { createBodyFn } from \"./body\"\nimport { createCallFn } from \"./call\"\nimport { createHeaderFn } from \"./header\"\nimport { createStorageFn } from \"./storage\"\nimport { createUnpinFn } from \"./unpin\"\nimport { DisjointError, StopError } from \"./errors\"\nimport { createStorageCb } from \"./storage-subscription\"\nimport { DestroyedError } from \"@/client/DestroyedError\"\nimport { chainHead } from \"@/methods\"\n\ntype FollowEventRpc =\n | FollowEventWithRuntimeRpc\n | FollowEventWithoutRuntimeRpc\n | OperationEventsRpc\n | StopRpc\n\nfunction isOperationEvent(event: FollowEventRpc): event is OperationEventsRpc {\n return (event as OperationEventsRpc).operationId !== undefined\n}\n\nexport function getChainHead(\n request: ClientRequest<string, FollowEventRpc>,\n): ChainHead {\n return (\n withRuntime: boolean,\n onFollowEvent:\n | ((event: FollowEventWithoutRuntime) => void)\n | ((event: FollowEventWithRuntime) => void),\n onFollowError: (e: Error) => void,\n ): FollowResponse => {\n const subscriptions = getSubscriptionsManager<OperationEventsRpc>()\n\n const ongoingRequests = new Set<() => void>()\n const deferredFollow = deferred<string | Error>()\n let followSubscription: Promise<string | Error> | string | null =\n deferredFollow.promise\n\n const onAllFollowEventsNext = (event: FollowEventRpc) => {\n if (isOperationEvent(event)) {\n if (!subscriptions.has(event.operationId))\n console.warn(\"Uknown operationId on\", event)\n\n return subscriptions.next(event.operationId, event)\n }\n\n if (event.event !== \"stop\") {\n if (event.event === \"initialized\") {\n return onFollowEvent({\n type: event.event,\n finalizedBlockHashes: event.finalizedBlockHashes,\n finalizedBlockRuntime: (event as any).finalizedBlockRuntime,\n })\n }\n\n const { event: type, ...rest } = event\n // This is kinda dangerous, but YOLO\n return onFollowEvent({ type, ...rest } as any)\n }\n\n onFollowError(new StopError())\n unfollow(false)\n }\n\n const onAllFollowEventsError = (error: Error) => {\n onFollowError(error)\n unfollow(!(error instanceof DestroyedError))\n }\n\n const onFollowRequestSuccess = (\n subscriptionId: string,\n follow: FollowSubscriptionCb<FollowEventRpc>,\n ) => {\n const done = follow(subscriptionId, {\n next: onAllFollowEventsNext,\n error: onAllFollowEventsError,\n })\n\n unfollow = (sendUnfollow = true) => {\n followSubscription = null\n unfollow = noop\n done()\n sendUnfollow && request(chainHead.unfollow, [subscriptionId])\n subscriptions.errorAll(new DisjointError())\n ongoingRequests.forEach((cb) => {\n cb()\n })\n ongoingRequests.clear()\n }\n\n followSubscription = subscriptionId\n deferredFollow.res(subscriptionId)\n }\n\n const onFollowRequestError = (e: Error) => {\n if (e instanceof DestroyedError) {\n unfollow(false)\n } else {\n onFollowError(e)\n }\n followSubscription = null\n deferredFollow.res(e)\n }\n\n let unfollow: (internal?: boolean) => void = request(\n chainHead.follow,\n [withRuntime],\n { onSuccess: onFollowRequestSuccess, onError: onFollowRequestError },\n )\n\n const fRequest: ClientInnerRequest<any, any> = (method, params, cb) => {\n const disjoint = () => {\n cb?.onError(new DisjointError())\n }\n\n if (followSubscription === null) {\n disjoint()\n return noop\n }\n\n const onSubscription = (subscription: string) => {\n if (!cb) return request(method, [subscription, ...params])\n\n ongoingRequests.add(disjoint)\n\n const onSubscribeOperation = (\n operationId: string,\n subscriber: Subscriber<any>,\n ) => {\n if (followSubscription === null) {\n subscriber.error(new DisjointError())\n return noop\n }\n\n subscriptions.subscribe(operationId, subscriber)\n\n return () => {\n subscriptions.unsubscribe(operationId)\n }\n }\n\n const cleanup = request(method, [subscription, ...params], {\n onSuccess: (response) => {\n ongoingRequests.delete(disjoint)\n cb.onSuccess(response, onSubscribeOperation)\n },\n onError: (e) => {\n ongoingRequests.delete(disjoint)\n cb.onError(e)\n },\n })\n\n return () => {\n ongoingRequests.delete(disjoint)\n cleanup()\n }\n }\n\n if (typeof followSubscription === \"string\")\n return onSubscription(followSubscription)\n\n let onCancel = noop\n followSubscription.then((x) => {\n if (x instanceof Error) return disjoint()\n if (followSubscription) onCancel = onSubscription(x)\n })\n\n return () => {\n onCancel()\n }\n }\n\n return {\n unfollow() {\n unfollow()\n followSubscription = null\n },\n body: createBodyFn(fRequest),\n call: createCallFn(fRequest),\n header: createHeaderFn(fRequest),\n storage: createStorageFn(fRequest),\n storageSubscription: createStorageCb(fRequest),\n unpin: createUnpinFn(fRequest),\n _request: fRequest,\n }\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAoCA,SAAS,iBAAiB,KAAoD,EAAA;AAC5E,EAAA,OAAQ,MAA6B,WAAgB,KAAA,KAAA,CAAA,CAAA;AACvD,CAAA;AAEO,SAAS,aACd,OACW,EAAA;AACX,EAAO,OAAA,CACL,WACA,EAAA,aAAA,EAGA,aACmB,KAAA;AACnB,IAAA,MAAM,gBAAgB,uBAA4C,EAAA,CAAA;AAElE,IAAM,MAAA,eAAA,uBAAsB,GAAgB,EAAA,CAAA;AAC5C,IAAA,MAAM,iBAAiB,QAAyB,EAAA,CAAA;AAChD,IAAA,IAAI,qBACF,cAAe,CAAA,OAAA,CAAA;AAEjB,IAAM,MAAA,qBAAA,GAAwB,CAAC,KAA0B,KAAA;AACvD,MAAI,IAAA,gBAAA,CAAiB,KAAK,CAAG,EAAA;AAC3B,QAAA,IAAI,CAAC,aAAA,CAAc,GAAI,CAAA,KAAA,CAAM,WAAW,CAAA;AACtC,UAAQ,OAAA,CAAA,IAAA,CAAK,yBAAyB,KAAK,CAAA,CAAA;AAE7C,QAAA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AA,CAAA,IAAA,CAAK,CAAC,CAAM,KAAA;AAC7B,QAAI,IAAA,CAAA,YAAa,KAAO,EAAA,OAAO,QAAS,EAAA,CAAA;AACxC,QAAI,IAAA,kBAAA,EAA+B,QAAA,GAAA,cAAA,CAAe,CAAC,CAAA,CAAA;AAAA,OACpD,CAAA,CAAA;AAED,MAAA,OAAO,MAAM;AACX,QAAS,QAAA,EAAA,CAAA;AAAA,OACX,CAAA;AAAA,KACF,CAAA;AAEA,IAAO,OAAA;AAAA,MACL,QAAW,GAAA;AACT,QAAS,QAAA,EAAA,CAAA;AACT,QAAqB,kBAAA,GAAA,IAAA,CAAA;AAAA,OACvB;AAAA,MACA,IAAA,EAAM,aAAa,QAAQ,CAAA;AAAA,MAC3B,IAAA,EAAM,aAAa,QAAQ,CAAA;AAAA,MAC3B,MAAA,EAAQ,eAAe,QAAQ,CAAA;AAAA,MAC/B,OAAA,EAAS,gBAAgB,QAAQ,CAAA;AAAA,MACjC,mBAAA,EAAqB,gBAAgB,QAAQ,CAAA;AAAA,MAC7C,KAAA,EAAO,cAAc,QAAQ,CAAA;AAAA,MAC7B,QAAU,EAAA,QAAA;AAAA,KACZ,CAAA;AAAA,GACF,CAAA;AACF;;;;"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
class StopError extends Error {
|
|
2
|
+
constructor() {
|
|
3
|
+
super("ChainHead stopped");
|
|
4
|
+
this.name = "StopError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
class DisjointError extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("ChainHead disjointed");
|
|
10
|
+
this.name = "DisjointError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
class OperationLimitError extends Error {
|
|
14
|
+
constructor() {
|
|
15
|
+
super("ChainHead operations limit reached");
|
|
16
|
+
this.name = "OperationLimitError";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
class OperationError extends Error {
|
|
20
|
+
constructor(error) {
|
|
21
|
+
super(error);
|
|
22
|
+
this.name = "OperationError";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
class OperationInaccessibleError extends Error {
|
|
26
|
+
constructor() {
|
|
27
|
+
super("ChainHead operation inaccessible");
|
|
28
|
+
this.name = "OperationInaccessibleError";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { DisjointError, OperationError, OperationInaccessibleError, OperationLimitError, StopError };
|
|
33
|
+
//# sourceMappingURL=errors.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.mjs","sources":["../../../src/chainhead/errors.ts"],"sourcesContent":["export class StopError extends Error {\n constructor() {\n super(\"ChainHead stopped\")\n this.name = \"StopError\"\n }\n}\n\nexport class DisjointError extends Error {\n constructor() {\n super(\"ChainHead disjointed\")\n this.name = \"DisjointError\"\n }\n}\n\nexport class OperationLimitError extends Error {\n constructor() {\n super(\"ChainHead operations limit reached\")\n this.name = \"OperationLimitError\"\n }\n}\n\nexport class OperationError extends Error {\n constructor(error: string) {\n super(error)\n this.name = \"OperationError\"\n }\n}\n\nexport class OperationInaccessibleError extends Error {\n constructor() {\n super(\"ChainHead operation inaccessible\")\n this.name = \"OperationInaccessibleError\"\n }\n}\n"],"names":[],"mappings":"AAAO,MAAM,kBAAkB,KAAM,CAAA;AAAA,EACnC,WAAc,GAAA;AACZ,IAAA,KAAA,CAAM,mBAAmB,CAAA,CAAA;AACzB,IAAA,IAAA,CAAK,IAAO,GAAA,WAAA,CAAA;AAAA,GACd;AACF,CAAA;AAEO,MAAM,sBAAsB,KAAM,CAAA;AAAA,EACvC,WAAc,GAAA;AACZ,IAAA,KAAA,CAAM,sBAAsB,CAAA,CAAA;AAC5B,IAAA,IAAA,CAAK,IAAO,GAAA,eAAA,CAAA;AAAA,GACd;AACF,CAAA;AAEO,MAAM,4BAA4B,KAAM,CAAA;AAAA,EAC7C,WAAc,GAAA;AACZ,IAAA,KAAA,CAAM,oCAAoC,CAAA,CAAA;AAC1C,IAAA,IAAA,CAAK,IAAO,GAAA,qBAAA,CAAA;AAAA,GACd;AACF,CAAA;AAEO,MAAM,uBAAuB,KAAM,CAAA;AAAA,EACxC,YAAY,KAAe,EAAA;AACzB,IAAA,KAAA,CAAM,KAAK,CAAA,CAAA;AACX,IAAA,IAAA,CAAK,IAAO,GAAA,gBAAA,CAAA;AAAA,GACd;AACF,CAAA;AAEO,MAAM,mCAAmC,KAAM,CAAA;AAAA,EACpD,WAAc,GAAA;AACZ,IAAA,KAAA,CAAM,kCAAkC,CAAA,CAAA;AACxC,IAAA,IAAA,CAAK,IAAO,GAAA,4BAAA,CAAA;AAAA,GACd;AACF;;;;"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { chainHead } from '../methods.mjs';
|
|
2
|
+
|
|
3
|
+
const createHeaderFn = (request) => (hash) => new Promise((res, rej) => {
|
|
4
|
+
request(chainHead.header, [hash], {
|
|
5
|
+
onSuccess: res,
|
|
6
|
+
onError: rej
|
|
7
|
+
});
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export { createHeaderFn };
|
|
11
|
+
//# sourceMappingURL=header.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"header.mjs","sources":["../../../src/chainhead/header.ts"],"sourcesContent":["import { chainHead } from \"@/methods\"\nimport { ClientInnerRequest } from \"./public-types\"\n\nexport const createHeaderFn =\n (request: ClientInnerRequest<string, unknown>) => (hash: string) =>\n new Promise<string>((res, rej) => {\n request(chainHead.header, [hash], {\n onSuccess: res,\n onError: rej,\n })\n })\n"],"names":[],"mappings":";;AAGa,MAAA,cAAA,GACX,CAAC,OAAiD,KAAA,CAAC,SACjD,IAAI,OAAA,CAAgB,CAAC,GAAA,EAAK,GAAQ,KAAA;AAChC,EAAA,OAAA,CAAQ,SAAU,CAAA,MAAA,EAAQ,CAAC,IAAI,CAAG,EAAA;AAAA,IAChC,SAAW,EAAA,GAAA;AAAA,IACX,OAAS,EAAA,GAAA;AAAA,GACV,CAAA,CAAA;AACH,CAAC;;;;"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { abortablePromiseFn } from '../internal-utils/abortablePromiseFn.mjs';
|
|
2
|
+
import { noop } from '../internal-utils/noop.mjs';
|
|
3
|
+
import { OperationLimitError, OperationError, OperationInaccessibleError } from './errors.mjs';
|
|
4
|
+
import { chainHead } from '../methods.mjs';
|
|
5
|
+
|
|
6
|
+
const createOperationPromise = (operationName, factory) => (request) => abortablePromiseFn((res, rej, ...args) => {
|
|
7
|
+
let isRunning = true;
|
|
8
|
+
let cancel = () => {
|
|
9
|
+
isRunning = false;
|
|
10
|
+
};
|
|
11
|
+
const [requestArgs, logicCb] = factory(...args);
|
|
12
|
+
request(operationName, requestArgs, {
|
|
13
|
+
onSuccess: (response, followSubscription) => {
|
|
14
|
+
if (response.result === "limitReached")
|
|
15
|
+
return rej(new OperationLimitError());
|
|
16
|
+
const { operationId } = response;
|
|
17
|
+
const stopOperation = () => {
|
|
18
|
+
request(chainHead.stopOperation, [operationId]);
|
|
19
|
+
};
|
|
20
|
+
if (!isRunning) return stopOperation();
|
|
21
|
+
let done = noop;
|
|
22
|
+
const _res = (x) => {
|
|
23
|
+
isRunning = false;
|
|
24
|
+
done();
|
|
25
|
+
res(x);
|
|
26
|
+
};
|
|
27
|
+
const _rej = (x) => {
|
|
28
|
+
isRunning = false;
|
|
29
|
+
done();
|
|
30
|
+
rej(x);
|
|
31
|
+
};
|
|
32
|
+
done = followSubscription(operationId, {
|
|
33
|
+
next: (e) => {
|
|
34
|
+
const _e = e;
|
|
35
|
+
if (_e.event === "operationError")
|
|
36
|
+
rej(new OperationError(_e.error));
|
|
37
|
+
else if (_e.event === "operationInaccessible")
|
|
38
|
+
rej(new OperationInaccessibleError());
|
|
39
|
+
else logicCb(e, _res, _rej);
|
|
40
|
+
},
|
|
41
|
+
error: _rej
|
|
42
|
+
});
|
|
43
|
+
cancel = () => {
|
|
44
|
+
if (isRunning) {
|
|
45
|
+
done();
|
|
46
|
+
stopOperation();
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
onError: rej
|
|
51
|
+
});
|
|
52
|
+
return () => {
|
|
53
|
+
cancel();
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export { createOperationPromise };
|
|
58
|
+
//# sourceMappingURL=operation-promise.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operation-promise.mjs","sources":["../../../src/chainhead/operation-promise.ts"],"sourcesContent":["import { abortablePromiseFn, noop } from \"@/internal-utils\"\nimport {\n CommonOperationEventsRpc,\n OperationResponseRpc,\n} from \"./json-rpc-types\"\nimport {\n OperationError,\n OperationInaccessibleError,\n OperationLimitError,\n} from \"./errors\"\nimport { ClientInnerRequest } from \"./public-types\"\nimport { chainHead } from \"@/methods\"\n\nexport const createOperationPromise =\n <I extends { operationId: string; event: string }, O, A extends Array<any>>(\n operationName: string,\n factory: (\n ...args: A\n ) => [\n Array<any>,\n (e: I, res: (x: O) => void, rej: (e: Error) => void) => void,\n ],\n ) =>\n (\n request: ClientInnerRequest<\n OperationResponseRpc,\n I | CommonOperationEventsRpc\n >,\n ) =>\n abortablePromiseFn<O, A>((res, rej, ...args) => {\n let isRunning = true\n let cancel = () => {\n isRunning = false\n }\n\n const [requestArgs, logicCb] = factory(...args)\n request(operationName, requestArgs, {\n onSuccess: (response, followSubscription) => {\n if (response.result === \"limitReached\")\n return rej(new OperationLimitError())\n\n const { operationId } = response\n const stopOperation = () => {\n request(chainHead.stopOperation, [operationId])\n }\n\n if (!isRunning) return stopOperation()\n\n let done = noop\n const _res = (x: O) => {\n isRunning = false\n done()\n res(x)\n }\n const _rej = (x: Error) => {\n isRunning = false\n done()\n rej(x)\n }\n\n done = followSubscription(operationId, {\n next: (e) => {\n const _e = e as CommonOperationEventsRpc\n if (_e.event === \"operationError\")\n rej(new OperationError(_e.error))\n else if (_e.event === \"operationInaccessible\")\n rej(new OperationInaccessibleError())\n else logicCb(e as I, _res, _rej)\n },\n error: _rej,\n })\n\n cancel = () => {\n if (isRunning) {\n done()\n stopOperation()\n }\n }\n },\n onError: rej,\n })\n\n return () => {\n cancel()\n }\n })\n"],"names":[],"mappings":";;;;;AAaa,MAAA,sBAAA,GACX,CACE,aAAA,EACA,OAOF,KAAA,CACE,YAKA,kBAAyB,CAAA,CAAC,GAAK,EAAA,GAAA,EAAA,GAAQ,IAAS,KAAA;AAC9C,EAAA,IAAI,SAAY,GAAA,IAAA,CAAA;AAChB,EAAA,IAAI,SAAS,MAAM;AACjB,IAAY,SAAA,GAAA,KAAA,CAAA;AAAA,GACd,CAAA;AAEA,EAAA,MAAM,CAAC,WAAa,EAAA,OAAO,CAAI,GAAA,OAAA,CAAQ,GAAG,IAAI,CAAA,CAAA;AAC9C,EAAA,OAAA,CAAQ,eAAe,WAAa,EAAA;AAAA,IAClC,SAAA,EAAW,CAAC,QAAA,EAAU,kBAAuB,KAAA;AAC3C,MAAA,IAAI,SAAS,MAAW,KAAA,cAAA;AACtB,QAAO,OAAA,GAAA,CAAI,IAAI,mBAAA,EAAqB,CAAA,CAAA;AAEtC,MAAM,MAAA,EAAE,aAAgB,GAAA,QAAA,CAAA;AACxB,MAAA,MAAM,gBAAgB,MAAM;AAC1B,QAAA,OAAA,CAAQ,SAAU,CAAA,aAAA,EAAe,CAAC,WAAW,CAAC,CAAA,CAAA;AAAA,OAChD,CAAA;AAEA,MAAI,IAAA,CAAC,SAAW,EAAA,OAAO,aAAc,EAAA,CAAA;AAErC,MAAA,IAAI,IAAO,GAAA,IAAA,CAAA;AACX,MAAM,MAAA,IAAA,GAAO,CAAC,CAAS,KAAA;AACrB,QAAY,SAAA,GAAA,KAAA,CAAA;AACZ,QAAK,IAAA,EAAA,CAAA;AACL,QAAA,GAAA,CAAI,CAAC,CAAA,CAAA;AAAA,OACP,CAAA;AACA,MAAM,MAAA,IAAA,GAAO,CAAC,CAAa,KAAA;AACzB,QAAY,SAAA,GAAA,KAAA,CAAA;AACZ,QAAK,IAAA,EAAA,CAAA;AACL,QAAA,GAAA,CAAI,CAAC,CAAA,CAAA;AAAA,OACP,CAAA;AAEA,MAAA,IAAA,GAAO,mBAAmB,WAAa,EAAA;AAAA,QACrC,IAAA,EAAM,CAAC,CAAM,KAAA;AACX,UAAA,MAAM,EAAK,GAAA,CAAA,CAAA;AACX,UAAA,IAAI,GAAG,KAAU,KAAA,gBAAA;AACf,YAAA,GAAA,CAAI,IAAI,cAAA,CAAe,EAAG,CAAA,KAAK,CAAC,CAAA,CAAA;AAAA,eAAA,IACzB,GAAG,KAAU,KAAA,uBAAA;AACpB,YAAI,GAAA,CAAA,IAAI,4BAA4B,CAAA,CAAA;AAAA,eACjC,OAAA,CAAQ,CAAQ,EAAA,IAAA,EAAM,IAAI,CAAA,CAAA;AAAA,SACjC;AAAA,QACA,KAAO,EAAA,IAAA;AAAA,OACR,CAAA,CAAA;AAED,MAAA,MAAA,GAAS,MAAM;AACb,QAAA,IAAI,SAAW,EAAA;AACb,UAAK,IAAA,EAAA,CAAA;AACL,UAAc,aAAA,EAAA,CAAA;AAAA,SAChB;AAAA,OACF,CAAA;AAAA,KACF;AAAA,IACA,OAAS,EAAA,GAAA;AAAA,GACV,CAAA,CAAA;AAED,EAAA,OAAO,MAAM;AACX,IAAO,MAAA,EAAA,CAAA;AAAA,GACT,CAAA;AACF,CAAC;;;;"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { noop } from '@polkadot-api/utils';
|
|
2
|
+
import { chainHead } from '../methods.mjs';
|
|
3
|
+
import { OperationLimitError, OperationInaccessibleError, OperationError } from './errors.mjs';
|
|
4
|
+
|
|
5
|
+
const createStorageCb = (request) => (hash, inputs, childTrie, onItems, onError, onDone, onDiscardedItems) => {
|
|
6
|
+
if (inputs.length === 0) {
|
|
7
|
+
onDone();
|
|
8
|
+
return noop;
|
|
9
|
+
}
|
|
10
|
+
let isRunning = true;
|
|
11
|
+
let cancel = () => {
|
|
12
|
+
isRunning = false;
|
|
13
|
+
};
|
|
14
|
+
request(chainHead.storage, [hash, inputs, childTrie], {
|
|
15
|
+
onSuccess: (response, followSubscription) => {
|
|
16
|
+
if (response.result === "limitReached" || response.discardedItems === inputs.length)
|
|
17
|
+
return onError(new OperationLimitError());
|
|
18
|
+
const { operationId } = response;
|
|
19
|
+
const stopOperation = () => {
|
|
20
|
+
request(chainHead.stopOperation, [operationId]);
|
|
21
|
+
};
|
|
22
|
+
if (!isRunning) return stopOperation();
|
|
23
|
+
const doneListening = followSubscription(response.operationId, {
|
|
24
|
+
next: (event) => {
|
|
25
|
+
switch (event.event) {
|
|
26
|
+
case "operationStorageItems": {
|
|
27
|
+
onItems(event.items);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case "operationStorageDone": {
|
|
31
|
+
_onDone();
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
case "operationError": {
|
|
35
|
+
_onError(new OperationError(event.error));
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
case "operationInaccessible": {
|
|
39
|
+
_onError(new OperationInaccessibleError());
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
default:
|
|
43
|
+
request(chainHead.continue, [event.operationId]);
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
error: onError
|
|
47
|
+
});
|
|
48
|
+
cancel = () => {
|
|
49
|
+
doneListening();
|
|
50
|
+
request(chainHead.stopOperation, [response.operationId]);
|
|
51
|
+
};
|
|
52
|
+
const _onError = (e) => {
|
|
53
|
+
cancel = noop;
|
|
54
|
+
doneListening();
|
|
55
|
+
onError(e);
|
|
56
|
+
};
|
|
57
|
+
const _onDone = () => {
|
|
58
|
+
cancel = noop;
|
|
59
|
+
doneListening();
|
|
60
|
+
onDone();
|
|
61
|
+
};
|
|
62
|
+
onDiscardedItems(response.discardedItems);
|
|
63
|
+
},
|
|
64
|
+
onError
|
|
65
|
+
});
|
|
66
|
+
return () => {
|
|
67
|
+
cancel();
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export { createStorageCb };
|
|
72
|
+
//# sourceMappingURL=storage-subscription.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage-subscription.mjs","sources":["../../../src/chainhead/storage-subscription.ts"],"sourcesContent":["import { noop } from \"@polkadot-api/utils\"\nimport {\n ClientInnerRequest,\n FollowResponse,\n OperationError,\n OperationInaccessibleError,\n OperationLimitError,\n} from \"..\"\nimport {\n CommonOperationEventsRpc,\n LimitReachedRpc,\n OperationStorageDoneRpc,\n OperationStorageItemsRpc,\n OperationWaitingForContinueRpc,\n OperationStorageStartedRpc,\n} from \"./json-rpc-types\"\nimport { chainHead } from \"@/methods\"\n\nexport const createStorageCb =\n (\n request: ClientInnerRequest<\n OperationStorageStartedRpc | LimitReachedRpc,\n | CommonOperationEventsRpc\n | OperationStorageItemsRpc\n | OperationStorageDoneRpc\n | OperationWaitingForContinueRpc\n >,\n ): FollowResponse[\"storageSubscription\"] =>\n (hash, inputs, childTrie, onItems, onError, onDone, onDiscardedItems) => {\n if (inputs.length === 0) {\n onDone()\n return noop\n }\n\n let isRunning = true\n let cancel = () => {\n isRunning = false\n }\n\n request(chainHead.storage, [hash, inputs, childTrie], {\n onSuccess: (response, followSubscription) => {\n if (\n response.result === \"limitReached\" ||\n response.discardedItems === inputs.length\n )\n return onError(new OperationLimitError())\n\n const { operationId } = response\n const stopOperation = () => {\n request(chainHead.stopOperation, [operationId])\n }\n\n if (!isRunning) return stopOperation()\n\n const doneListening = followSubscription(response.operationId, {\n next: (event) => {\n switch (event.event) {\n case \"operationStorageItems\": {\n onItems(event.items)\n break\n }\n case \"operationStorageDone\": {\n _onDone()\n break\n }\n case \"operationError\": {\n _onError(new OperationError(event.error))\n break\n }\n case \"operationInaccessible\": {\n _onError(new OperationInaccessibleError())\n break\n }\n default:\n request(chainHead.continue, [event.operationId])\n }\n },\n error: onError,\n })\n\n cancel = () => {\n doneListening()\n request(chainHead.stopOperation, [response.operationId])\n }\n\n const _onError = (e: Error) => {\n cancel = noop\n doneListening()\n onError(e)\n }\n\n const _onDone = () => {\n cancel = noop\n doneListening()\n onDone()\n }\n\n onDiscardedItems(response.discardedItems)\n },\n onError,\n })\n\n return () => {\n cancel()\n }\n }\n"],"names":[],"mappings":";;;;AAkBa,MAAA,eAAA,GACX,CACE,OAAA,KAQF,CAAC,IAAA,EAAM,QAAQ,SAAW,EAAA,OAAA,EAAS,OAAS,EAAA,MAAA,EAAQ,gBAAqB,KAAA;AACvE,EAAI,IAAA,MAAA,CAAO,WAAW,CAAG,EAAA;AACvB,IAAO,MAAA,EAAA,CAAA;AACP,IAAO,OAAA,IAAA,CAAA;AAAA,GACT;AAEA,EAAA,IAAI,SAAY,GAAA,IAAA,CAAA;AAChB,EAAA,IAAI,SAAS,MAAM;AACjB,IAAY,SAAA,GAAA,KAAA,CAAA;AAAA,GACd,CAAA;AAEA,EAAA,OAAA,CAAQ,UAAU,OAAS,EAAA,CAAC,IAAM,EAAA,MAAA,EAAQ,SAAS,CAAG,EAAA;AAAA,IACpD,SAAA,EAAW,CAAC,QAAA,EAAU,kBAAuB,KAAA;AAC3C,MAAA,IACE,QAAS,CAAA,MAAA,KAAW,cACpB,IAAA,QAAA,CAAS,mBAAmB,MAAO,CAAA,MAAA;AAEnC,QAAO,OAAA,OAAA,CAAQ,IAAI,mBAAA,EAAqB,CAAA,CAAA;AAE1C,MAAM,MAAA,EAAE,aAAgB,GAAA,QAAA,CAAA;AACxB,MAAA,MAAM,gBAAgB,MAAM;AAC1B,QAAA,OAAA,CAAQ,SAAU,CAAA,aAAA,EAAe,CAAC,WAAW,CAAC,CAAA,CAAA;AAAA,OAChD,CAAA;AAEA,MAAI,IAAA,CAAC,SAAW,EAAA,OAAO,aAAc,EAAA,CAAA;AAErC,MAAM,MAAA,aAAA,GAAgB,kBAAmB,CAAA,QAAA,CAAS,WAAa,EAAA;AAAA,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,KAC1C;AAAA,IACA,OAAA;AAAA,GACD,CAAA,CAAA;AAED,EAAA,OAAO,MAAM;AACX,IAAO,MAAA,EAAA,CAAA;AAAA,GACT,CAAA;AACF;;;;"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { abortablePromiseFn } from '../internal-utils/abortablePromiseFn.mjs';
|
|
2
|
+
import '../methods.mjs';
|
|
3
|
+
import { OperationLimitError } from './errors.mjs';
|
|
4
|
+
import { createStorageCb } from './storage-subscription.mjs';
|
|
5
|
+
import '@polkadot-api/utils';
|
|
6
|
+
|
|
7
|
+
const createStorageFn = (request) => {
|
|
8
|
+
const cbStore = createStorageCb(request);
|
|
9
|
+
return abortablePromiseFn((resolve, reject, hash, type, key, childTrie) => {
|
|
10
|
+
const isDescendants = type.startsWith("descendants");
|
|
11
|
+
let result = isDescendants ? [] : null;
|
|
12
|
+
const onItems = isDescendants ? (items) => {
|
|
13
|
+
result.push(items);
|
|
14
|
+
} : (items) => {
|
|
15
|
+
result = items[0]?.[type];
|
|
16
|
+
};
|
|
17
|
+
const cancel = cbStore(
|
|
18
|
+
hash,
|
|
19
|
+
[{ key, type }],
|
|
20
|
+
childTrie ?? null,
|
|
21
|
+
onItems,
|
|
22
|
+
reject,
|
|
23
|
+
() => {
|
|
24
|
+
try {
|
|
25
|
+
resolve(isDescendants ? result.flat() : result);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
reject(e);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
(nDiscarded) => {
|
|
31
|
+
if (nDiscarded > 0) {
|
|
32
|
+
cancel();
|
|
33
|
+
reject(new OperationLimitError());
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
return cancel;
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export { createStorageFn };
|
|
42
|
+
//# sourceMappingURL=storage.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.mjs","sources":["../../../src/chainhead/storage.ts"],"sourcesContent":["import { ClientInnerRequest, FollowResponse, OperationLimitError } from \"..\"\nimport type {\n CommonOperationEventsRpc,\n LimitReachedRpc,\n OperationStorageDoneRpc,\n OperationStorageItemsRpc,\n OperationWaitingForContinueRpc,\n OperationStorageStartedRpc,\n} from \"./json-rpc-types\"\nimport { abortablePromiseFn } from \"@/internal-utils\"\nimport { createStorageCb } from \"./storage-subscription\"\n\nexport const createStorageFn = (\n request: ClientInnerRequest<\n OperationStorageStartedRpc | LimitReachedRpc,\n | CommonOperationEventsRpc\n | OperationStorageItemsRpc\n | OperationStorageDoneRpc\n | OperationWaitingForContinueRpc\n >,\n): FollowResponse[\"storage\"] => {\n const cbStore = createStorageCb(request)\n return abortablePromiseFn((resolve, reject, hash, type, key, childTrie) => {\n const isDescendants = type.startsWith(\"descendants\")\n let result: any = isDescendants ? [] : null\n\n const onItems: Parameters<typeof cbStore>[3] = isDescendants\n ? (items) => {\n result.push(items)\n }\n : (items) => {\n result = items[0]?.[type as \"value\"]\n }\n\n const cancel = cbStore(\n hash,\n [{ key, type }],\n childTrie ?? null,\n onItems,\n reject,\n () => {\n try {\n resolve(isDescendants ? result.flat() : result)\n } catch (e) {\n reject(e)\n }\n },\n (nDiscarded) => {\n if (nDiscarded > 0) {\n cancel()\n reject(new OperationLimitError())\n }\n },\n )\n return cancel\n })\n}\n"],"names":[],"mappings":";;;;;;AAYa,MAAA,eAAA,GAAkB,CAC7B,OAO8B,KAAA;AAC9B,EAAM,MAAA,OAAA,GAAU,gBAAgB,OAAO,CAAA,CAAA;AACvC,EAAA,OAAO,mBAAmB,CAAC,OAAA,EAAS,QAAQ,IAAM,EAAA,IAAA,EAAM,KAAK,SAAc,KAAA;AACzE,IAAM,MAAA,aAAA,GAAgB,IAAK,CAAA,UAAA,CAAW,aAAa,CAAA,CAAA;AACnD,IAAI,IAAA,MAAA,GAAc,aAAgB,GAAA,EAAK,GAAA,IAAA,CAAA;AAEvC,IAAM,MAAA,OAAA,GAAyC,aAC3C,GAAA,CAAC,KAAU,KAAA;AACT,MAAA,MAAA,CAAO,KAAK,KAAK,CAAA,CAAA;AAAA,KACnB,GACA,CAAC,KAAU,KAAA;AACT,MAAS,MAAA,GAAA,KAAA,CAAM,CAAC,CAAA,GAAI,IAAe,CAAA,CAAA;AAAA,KACrC,CAAA;AAEJ,IAAA,MAAM,MAAS,GAAA,OAAA;AAAA,MACb,IAAA;AAAA,MACA,CAAC,EAAE,GAAK,EAAA,IAAA,EAAM,CAAA;AAAA,MACd,SAAa,IAAA,IAAA;AAAA,MACb,OAAA;AAAA,MACA,MAAA;AAAA,MACA,MAAM;AACJ,QAAI,IAAA;AACF,UAAA,OAAA,CAAQ,aAAgB,GAAA,MAAA,CAAO,IAAK,EAAA,GAAI,MAAM,CAAA,CAAA;AAAA,iBACvC,CAAG,EAAA;AACV,UAAA,MAAA,CAAO,CAAC,CAAA,CAAA;AAAA,SACV;AAAA,OACF;AAAA,MACA,CAAC,UAAe,KAAA;AACd,QAAA,IAAI,aAAa,CAAG,EAAA;AAClB,UAAO,MAAA,EAAA,CAAA;AACP,UAAO,MAAA,CAAA,IAAI,qBAAqB,CAAA,CAAA;AAAA,SAClC;AAAA,OACF;AAAA,KACF,CAAA;AACA,IAAO,OAAA,MAAA,CAAA;AAAA,GACR,CAAA,CAAA;AACH;;;;"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { chainHead } from '../methods.mjs';
|
|
2
|
+
|
|
3
|
+
const createUnpinFn = (request) => (hashes) => hashes.length > 0 ? new Promise((res, rej) => {
|
|
4
|
+
request(chainHead.unpin, [hashes], {
|
|
5
|
+
onSuccess() {
|
|
6
|
+
res();
|
|
7
|
+
},
|
|
8
|
+
onError: rej
|
|
9
|
+
});
|
|
10
|
+
}) : Promise.resolve();
|
|
11
|
+
|
|
12
|
+
export { createUnpinFn };
|
|
13
|
+
//# sourceMappingURL=unpin.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unpin.mjs","sources":["../../../src/chainhead/unpin.ts"],"sourcesContent":["import { chainHead } from \"@/methods\"\nimport { ClientInnerRequest } from \"./public-types\"\n\nexport const createUnpinFn =\n (request: ClientInnerRequest<null, unknown>) => (hashes: string[]) =>\n hashes.length > 0\n ? new Promise<void>((res, rej) => {\n request(chainHead.unpin, [hashes], {\n onSuccess() {\n res()\n },\n onError: rej,\n })\n })\n : Promise.resolve()\n"],"names":[],"mappings":";;AAGO,MAAM,aACX,GAAA,CAAC,OAA+C,KAAA,CAAC,MAC/C,KAAA,MAAA,CAAO,MAAS,GAAA,CAAA,GACZ,IAAI,OAAA,CAAc,CAAC,GAAA,EAAK,GAAQ,KAAA;AAC9B,EAAA,OAAA,CAAQ,SAAU,CAAA,KAAA,EAAO,CAAC,MAAM,CAAG,EAAA;AAAA,IACjC,SAAY,GAAA;AACV,MAAI,GAAA,EAAA,CAAA;AAAA,KACN;AAAA,IACA,OAAS,EAAA,GAAA;AAAA,GACV,CAAA,CAAA;AACH,CAAC,CAAA,GACD,QAAQ,OAAQ;;;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { abortablePromiseFn } from './internal-utils/abortablePromiseFn.mjs';
|
|
2
|
+
import { chainSpec } from './methods.mjs';
|
|
3
|
+
|
|
4
|
+
const createGetChainSpec = (clientRequest) => {
|
|
5
|
+
const request = abortablePromiseFn(
|
|
6
|
+
(onSuccess, onError, method, params) => clientRequest(method, params, { onSuccess, onError })
|
|
7
|
+
);
|
|
8
|
+
let cachedPromise = null;
|
|
9
|
+
return async () => {
|
|
10
|
+
if (cachedPromise) return cachedPromise;
|
|
11
|
+
return cachedPromise = Promise.all([
|
|
12
|
+
request(chainSpec.chainName, []),
|
|
13
|
+
request(chainSpec.genesisHash, []),
|
|
14
|
+
request(chainSpec.properties, [])
|
|
15
|
+
]).then(([name, genesisHash, properties]) => ({
|
|
16
|
+
name,
|
|
17
|
+
genesisHash,
|
|
18
|
+
properties
|
|
19
|
+
}));
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { createGetChainSpec };
|
|
24
|
+
//# sourceMappingURL=chainspec.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chainspec.mjs","sources":["../../src/chainspec.ts"],"sourcesContent":["import { ClientRequest } from \"./client\"\nimport { abortablePromiseFn } from \"./internal-utils\"\nimport { chainSpec } from \"./methods\"\n\nexport interface ChainSpecData {\n name: string\n genesisHash: string\n properties: any\n}\n\nexport const createGetChainSpec = (clientRequest: ClientRequest<any, any>) => {\n const request = abortablePromiseFn(\n <T>(\n onSuccess: (value: T) => void,\n onError: (e: any) => void,\n method: string,\n params: any[],\n ) => clientRequest(method, params, { onSuccess, onError }),\n )\n let cachedPromise: null | Promise<ChainSpecData> = null\n\n return async (): Promise<ChainSpecData> => {\n if (cachedPromise) return cachedPromise\n return (cachedPromise = Promise.all([\n request<string>(chainSpec.chainName, []),\n request<string>(chainSpec.genesisHash, []),\n request<any>(chainSpec.properties, []),\n ]).then(([name, genesisHash, properties]) => ({\n name,\n genesisHash,\n properties,\n })))\n }\n}\n"],"names":[],"mappings":";;;AAUa,MAAA,kBAAA,GAAqB,CAAC,aAA2C,KAAA;AAC5E,EAAA,MAAM,OAAU,GAAA,kBAAA;AAAA,IACd,CACE,SACA,EAAA,OAAA,EACA,MACA,EAAA,MAAA,KACG,aAAc,CAAA,MAAA,EAAQ,MAAQ,EAAA,EAAE,SAAW,EAAA,OAAA,EAAS,CAAA;AAAA,GAC3D,CAAA;AACA,EAAA,IAAI,aAA+C,GAAA,IAAA,CAAA;AAEnD,EAAA,OAAO,YAAoC;AACzC,IAAA,IAAI,eAAsB,OAAA,aAAA,CAAA;AAC1B,IAAQ,OAAA,aAAA,GAAgB,QAAQ,GAAI,CAAA;AAAA,MAClC,OAAgB,CAAA,SAAA,CAAU,SAAW,EAAA,EAAE,CAAA;AAAA,MACvC,OAAgB,CAAA,SAAA,CAAU,WAAa,EAAA,EAAE,CAAA;AAAA,MACzC,OAAa,CAAA,SAAA,CAAU,UAAY,EAAA,EAAE,CAAA;AAAA,KACtC,EAAE,IAAK,CAAA,CAAC,CAAC,IAAM,EAAA,WAAA,EAAa,UAAU,CAAO,MAAA;AAAA,MAC5C,IAAA;AAAA,MACA,WAAA;AAAA,MACA,UAAA;AAAA,KACA,CAAA,CAAA,CAAA;AAAA,GACJ,CAAA;AACF;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DestroyedError.mjs","sources":["../../../src/client/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,KAAM,CAAA;AAAA,EACxC,WAAc,GAAA;AACZ,IAAA,KAAA,CAAM,kBAAkB,CAAA,CAAA;AACxB,IAAA,IAAA,CAAK,IAAO,GAAA,gBAAA,CAAA;AAAA,GACd;AACF;;;;"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
class RpcError extends Error {
|
|
5
|
+
constructor(e) {
|
|
6
|
+
super(e.message);
|
|
7
|
+
__publicField(this, "code");
|
|
8
|
+
__publicField(this, "data");
|
|
9
|
+
this.code = e.code;
|
|
10
|
+
this.data = e.data;
|
|
11
|
+
this.name = "RpcError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { RpcError };
|
|
16
|
+
//# sourceMappingURL=RpcError.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
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,CAAA;AAHjB,IAAA,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AACA,IAAA,aAAA,CAAA,IAAA,EAAA,MAAA,CAAA,CAAA;AAGE,IAAA,IAAA,CAAK,OAAO,CAAE,CAAA,IAAA,CAAA;AACd,IAAA,IAAA,CAAK,OAAO,CAAE,CAAA,IAAA,CAAA;AACd,IAAA,IAAA,CAAK,IAAO,GAAA,UAAA,CAAA;AAAA,GACd;AACF;;;;"}
|
|
@@ -0,0 +1,78 @@
|
|
|
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
|