@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
package/dist/index.mjs
DELETED
|
@@ -1,686 +0,0 @@
|
|
|
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
|
-
|
|
5
|
-
// src/internal-utils/abortablePromiseFn.ts
|
|
6
|
-
import { AbortError, noop } from "@polkadot-api/utils";
|
|
7
|
-
var abortablePromiseFn = (fn) => (...args) => new Promise((res, rej) => {
|
|
8
|
-
let cancel = noop;
|
|
9
|
-
const [actualArgs, abortSignal] = args[args.length - 1] instanceof AbortSignal ? [args.slice(0, args.length - 1), args[args.length - 1]] : [args];
|
|
10
|
-
const onAbort = () => {
|
|
11
|
-
cancel();
|
|
12
|
-
rej(new AbortError());
|
|
13
|
-
};
|
|
14
|
-
abortSignal?.addEventListener("abort", onAbort, { once: true });
|
|
15
|
-
const withCleanup = (fn2) => (x) => {
|
|
16
|
-
cancel = noop;
|
|
17
|
-
abortSignal?.removeEventListener("abort", onAbort);
|
|
18
|
-
fn2(x);
|
|
19
|
-
};
|
|
20
|
-
cancel = fn(...[withCleanup(res), withCleanup(rej), ...actualArgs]);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
// src/internal-utils/deferred-promise.ts
|
|
24
|
-
function deferred() {
|
|
25
|
-
let res = () => {
|
|
26
|
-
};
|
|
27
|
-
let rej = () => {
|
|
28
|
-
};
|
|
29
|
-
const promise = new Promise((_res, _rej) => {
|
|
30
|
-
res = _res;
|
|
31
|
-
rej = _rej;
|
|
32
|
-
});
|
|
33
|
-
return { promise, res, rej };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// src/internal-utils/noop.ts
|
|
37
|
-
var noop2 = () => {
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// src/internal-utils/subscriptions-manager.ts
|
|
41
|
-
var getSubscriptionsManager = () => {
|
|
42
|
-
const subscriptions = /* @__PURE__ */ new Map();
|
|
43
|
-
return {
|
|
44
|
-
has: subscriptions.has.bind(subscriptions),
|
|
45
|
-
subscribe(id, subscriber) {
|
|
46
|
-
subscriptions.set(id, subscriber);
|
|
47
|
-
},
|
|
48
|
-
unsubscribe(id) {
|
|
49
|
-
subscriptions.delete(id);
|
|
50
|
-
},
|
|
51
|
-
next(id, data) {
|
|
52
|
-
subscriptions.get(id)?.next(data);
|
|
53
|
-
},
|
|
54
|
-
error(id, e) {
|
|
55
|
-
const subscriber = subscriptions.get(id);
|
|
56
|
-
if (subscriber) {
|
|
57
|
-
subscriptions.delete(id);
|
|
58
|
-
subscriber.error(e);
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
errorAll(e) {
|
|
62
|
-
const subscribers = [...subscriptions.values()];
|
|
63
|
-
subscriptions.clear();
|
|
64
|
-
subscribers.forEach((s) => {
|
|
65
|
-
s.error(e);
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
// src/methods.ts
|
|
72
|
-
var chainHead = {
|
|
73
|
-
body: "",
|
|
74
|
-
call: "",
|
|
75
|
-
continue: "",
|
|
76
|
-
follow: "",
|
|
77
|
-
header: "",
|
|
78
|
-
stopOperation: "",
|
|
79
|
-
storage: "",
|
|
80
|
-
unfollow: "",
|
|
81
|
-
unpin: "",
|
|
82
|
-
followEvent: ""
|
|
83
|
-
};
|
|
84
|
-
var chainSpec = {
|
|
85
|
-
chainName: "",
|
|
86
|
-
genesisHash: "",
|
|
87
|
-
properties: ""
|
|
88
|
-
};
|
|
89
|
-
var transaction = {
|
|
90
|
-
broadcast: "",
|
|
91
|
-
stop: ""
|
|
92
|
-
};
|
|
93
|
-
var transactionWatch = {
|
|
94
|
-
submitAndWatch: "",
|
|
95
|
-
unwatch: ""
|
|
96
|
-
};
|
|
97
|
-
Object.entries({ chainHead, chainSpec, transaction, transactionWatch }).forEach(
|
|
98
|
-
([fnGroupName, methods]) => {
|
|
99
|
-
Object.keys(methods).forEach((methodName) => {
|
|
100
|
-
;
|
|
101
|
-
methods[methodName] = `${fnGroupName}_v1_${methodName}`;
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
// src/transaction/transaction.ts
|
|
107
|
-
var getTransaction = (request) => (tx, error) => {
|
|
108
|
-
let cancel = request(transaction.broadcast, [tx], {
|
|
109
|
-
onSuccess: (subscriptionId) => {
|
|
110
|
-
cancel = subscriptionId === null ? noop2 : () => {
|
|
111
|
-
request(transaction.stop, [subscriptionId]);
|
|
112
|
-
};
|
|
113
|
-
if (subscriptionId === null) {
|
|
114
|
-
error(new Error("Max # of broadcasted transactions has been reached"));
|
|
115
|
-
}
|
|
116
|
-
},
|
|
117
|
-
onError: error
|
|
118
|
-
});
|
|
119
|
-
return () => {
|
|
120
|
-
cancel();
|
|
121
|
-
};
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
// src/chainhead/errors.ts
|
|
125
|
-
var StopError = class extends Error {
|
|
126
|
-
constructor() {
|
|
127
|
-
super("ChainHead stopped");
|
|
128
|
-
this.name = "StopError";
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
var DisjointError = class extends Error {
|
|
132
|
-
constructor() {
|
|
133
|
-
super("ChainHead disjointed");
|
|
134
|
-
this.name = "DisjointError";
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
var OperationLimitError = class extends Error {
|
|
138
|
-
constructor() {
|
|
139
|
-
super("ChainHead operations limit reached");
|
|
140
|
-
this.name = "OperationLimitError";
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
var OperationError = class extends Error {
|
|
144
|
-
constructor(error) {
|
|
145
|
-
super(error);
|
|
146
|
-
this.name = "OperationError";
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
var OperationInaccessibleError = class extends Error {
|
|
150
|
-
constructor() {
|
|
151
|
-
super("ChainHead operation inaccessible");
|
|
152
|
-
this.name = "OperationInaccessibleError";
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
// src/chainhead/operation-promise.ts
|
|
157
|
-
var createOperationPromise = (operationName, factory) => (request) => abortablePromiseFn((res, rej, ...args) => {
|
|
158
|
-
let isRunning = true;
|
|
159
|
-
let cancel = () => {
|
|
160
|
-
isRunning = false;
|
|
161
|
-
};
|
|
162
|
-
const [requestArgs, logicCb] = factory(...args);
|
|
163
|
-
request(operationName, requestArgs, {
|
|
164
|
-
onSuccess: (response, followSubscription) => {
|
|
165
|
-
if (response.result === "limitReached")
|
|
166
|
-
return rej(new OperationLimitError());
|
|
167
|
-
const { operationId } = response;
|
|
168
|
-
const stopOperation = () => {
|
|
169
|
-
request(chainHead.stopOperation, [operationId]);
|
|
170
|
-
};
|
|
171
|
-
if (!isRunning) return stopOperation();
|
|
172
|
-
let done = noop2;
|
|
173
|
-
const _res = (x) => {
|
|
174
|
-
isRunning = false;
|
|
175
|
-
done();
|
|
176
|
-
res(x);
|
|
177
|
-
};
|
|
178
|
-
const _rej = (x) => {
|
|
179
|
-
isRunning = false;
|
|
180
|
-
done();
|
|
181
|
-
rej(x);
|
|
182
|
-
};
|
|
183
|
-
done = followSubscription(operationId, {
|
|
184
|
-
next: (e) => {
|
|
185
|
-
const _e = e;
|
|
186
|
-
if (_e.event === "operationError")
|
|
187
|
-
rej(new OperationError(_e.error));
|
|
188
|
-
else if (_e.event === "operationInaccessible")
|
|
189
|
-
rej(new OperationInaccessibleError());
|
|
190
|
-
else logicCb(e, _res, _rej);
|
|
191
|
-
},
|
|
192
|
-
error: _rej
|
|
193
|
-
});
|
|
194
|
-
cancel = () => {
|
|
195
|
-
if (isRunning) {
|
|
196
|
-
done();
|
|
197
|
-
stopOperation();
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
},
|
|
201
|
-
onError: rej
|
|
202
|
-
});
|
|
203
|
-
return () => {
|
|
204
|
-
cancel();
|
|
205
|
-
};
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
// src/chainhead/body.ts
|
|
209
|
-
var createBodyFn = createOperationPromise(
|
|
210
|
-
chainHead.body,
|
|
211
|
-
(hash) => [
|
|
212
|
-
[hash],
|
|
213
|
-
(e, res) => {
|
|
214
|
-
res(e.value);
|
|
215
|
-
}
|
|
216
|
-
]
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
// src/chainhead/call.ts
|
|
220
|
-
var createCallFn = createOperationPromise(
|
|
221
|
-
chainHead.call,
|
|
222
|
-
(hash, fnName, callParameters) => [
|
|
223
|
-
[hash, fnName, callParameters],
|
|
224
|
-
(e, res) => {
|
|
225
|
-
res(e.output);
|
|
226
|
-
}
|
|
227
|
-
]
|
|
228
|
-
);
|
|
229
|
-
|
|
230
|
-
// src/chainhead/header.ts
|
|
231
|
-
var createHeaderFn = (request) => (hash) => new Promise((res, rej) => {
|
|
232
|
-
request(chainHead.header, [hash], {
|
|
233
|
-
onSuccess: res,
|
|
234
|
-
onError: rej
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
// src/chainhead/storage-subscription.ts
|
|
239
|
-
import { noop as noop3 } from "@polkadot-api/utils";
|
|
240
|
-
var createStorageCb = (request) => (hash, inputs, childTrie, onItems, onError, onDone, onDiscardedItems) => {
|
|
241
|
-
if (inputs.length === 0) {
|
|
242
|
-
onDone();
|
|
243
|
-
return noop3;
|
|
244
|
-
}
|
|
245
|
-
let isRunning = true;
|
|
246
|
-
let cancel = () => {
|
|
247
|
-
isRunning = false;
|
|
248
|
-
};
|
|
249
|
-
request(chainHead.storage, [hash, inputs, childTrie], {
|
|
250
|
-
onSuccess: (response, followSubscription) => {
|
|
251
|
-
if (response.result === "limitReached" || response.discardedItems === inputs.length)
|
|
252
|
-
return onError(new OperationLimitError());
|
|
253
|
-
const { operationId } = response;
|
|
254
|
-
const stopOperation = () => {
|
|
255
|
-
request(chainHead.stopOperation, [operationId]);
|
|
256
|
-
};
|
|
257
|
-
if (!isRunning) return stopOperation();
|
|
258
|
-
const doneListening = followSubscription(response.operationId, {
|
|
259
|
-
next: (event) => {
|
|
260
|
-
switch (event.event) {
|
|
261
|
-
case "operationStorageItems": {
|
|
262
|
-
onItems(event.items);
|
|
263
|
-
break;
|
|
264
|
-
}
|
|
265
|
-
case "operationStorageDone": {
|
|
266
|
-
_onDone();
|
|
267
|
-
break;
|
|
268
|
-
}
|
|
269
|
-
case "operationError": {
|
|
270
|
-
_onError(new OperationError(event.error));
|
|
271
|
-
break;
|
|
272
|
-
}
|
|
273
|
-
case "operationInaccessible": {
|
|
274
|
-
_onError(new OperationInaccessibleError());
|
|
275
|
-
break;
|
|
276
|
-
}
|
|
277
|
-
default:
|
|
278
|
-
request(chainHead.continue, [event.operationId]);
|
|
279
|
-
}
|
|
280
|
-
},
|
|
281
|
-
error: onError
|
|
282
|
-
});
|
|
283
|
-
cancel = () => {
|
|
284
|
-
doneListening();
|
|
285
|
-
request(chainHead.stopOperation, [response.operationId]);
|
|
286
|
-
};
|
|
287
|
-
const _onError = (e) => {
|
|
288
|
-
cancel = noop3;
|
|
289
|
-
doneListening();
|
|
290
|
-
onError(e);
|
|
291
|
-
};
|
|
292
|
-
const _onDone = () => {
|
|
293
|
-
cancel = noop3;
|
|
294
|
-
doneListening();
|
|
295
|
-
onDone();
|
|
296
|
-
};
|
|
297
|
-
onDiscardedItems(response.discardedItems);
|
|
298
|
-
},
|
|
299
|
-
onError
|
|
300
|
-
});
|
|
301
|
-
return () => {
|
|
302
|
-
cancel();
|
|
303
|
-
};
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
// src/chainhead/storage.ts
|
|
307
|
-
var createStorageFn = (request) => {
|
|
308
|
-
const cbStore = createStorageCb(request);
|
|
309
|
-
return abortablePromiseFn((resolve, reject, hash, type, key, childTrie) => {
|
|
310
|
-
const isDescendants = type.startsWith("descendants");
|
|
311
|
-
let result = isDescendants ? [] : null;
|
|
312
|
-
const onItems = isDescendants ? (items) => {
|
|
313
|
-
result.push(items);
|
|
314
|
-
} : (items) => {
|
|
315
|
-
result = items[0]?.[type];
|
|
316
|
-
};
|
|
317
|
-
const cancel = cbStore(
|
|
318
|
-
hash,
|
|
319
|
-
[{ key, type }],
|
|
320
|
-
childTrie ?? null,
|
|
321
|
-
onItems,
|
|
322
|
-
reject,
|
|
323
|
-
() => {
|
|
324
|
-
try {
|
|
325
|
-
resolve(isDescendants ? result.flat() : result);
|
|
326
|
-
} catch (e) {
|
|
327
|
-
reject(e);
|
|
328
|
-
}
|
|
329
|
-
},
|
|
330
|
-
(nDiscarded) => {
|
|
331
|
-
if (nDiscarded > 0) {
|
|
332
|
-
cancel();
|
|
333
|
-
reject(new OperationLimitError());
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
);
|
|
337
|
-
return cancel;
|
|
338
|
-
});
|
|
339
|
-
};
|
|
340
|
-
|
|
341
|
-
// src/chainhead/unpin.ts
|
|
342
|
-
var createUnpinFn = (request) => (hashes) => hashes.length > 0 ? new Promise((res, rej) => {
|
|
343
|
-
request(chainHead.unpin, [hashes], {
|
|
344
|
-
onSuccess() {
|
|
345
|
-
res();
|
|
346
|
-
},
|
|
347
|
-
onError: rej
|
|
348
|
-
});
|
|
349
|
-
}) : Promise.resolve();
|
|
350
|
-
|
|
351
|
-
// src/client/DestroyedError.ts
|
|
352
|
-
var DestroyedError = class extends Error {
|
|
353
|
-
constructor() {
|
|
354
|
-
super("Client destroyed");
|
|
355
|
-
this.name = "DestroyedError";
|
|
356
|
-
}
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
// src/chainhead/chainhead.ts
|
|
360
|
-
function isOperationEvent(event) {
|
|
361
|
-
return event.operationId !== void 0;
|
|
362
|
-
}
|
|
363
|
-
function getChainHead(request) {
|
|
364
|
-
return (withRuntime, onFollowEvent, onFollowError) => {
|
|
365
|
-
const subscriptions = getSubscriptionsManager();
|
|
366
|
-
const ongoingRequests = /* @__PURE__ */ new Set();
|
|
367
|
-
const deferredFollow = deferred();
|
|
368
|
-
let followSubscription = deferredFollow.promise;
|
|
369
|
-
const onAllFollowEventsNext = (event) => {
|
|
370
|
-
if (isOperationEvent(event)) {
|
|
371
|
-
if (!subscriptions.has(event.operationId))
|
|
372
|
-
console.warn("Uknown operationId on", event);
|
|
373
|
-
return subscriptions.next(event.operationId, event);
|
|
374
|
-
}
|
|
375
|
-
if (event.event !== "stop") {
|
|
376
|
-
if (event.event === "initialized") {
|
|
377
|
-
return onFollowEvent({
|
|
378
|
-
type: event.event,
|
|
379
|
-
finalizedBlockHashes: "finalizedBlockHash" in event ? [event.finalizedBlockHash] : event.finalizedBlockHashes,
|
|
380
|
-
finalizedBlockRuntime: event.finalizedBlockRuntime
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
const { event: type, ...rest } = event;
|
|
384
|
-
return onFollowEvent({ type, ...rest });
|
|
385
|
-
}
|
|
386
|
-
onFollowError(new StopError());
|
|
387
|
-
unfollow(false);
|
|
388
|
-
};
|
|
389
|
-
const onAllFollowEventsError = (error) => {
|
|
390
|
-
onFollowError(error);
|
|
391
|
-
unfollow(!(error instanceof DestroyedError));
|
|
392
|
-
};
|
|
393
|
-
const onFollowRequestSuccess = (subscriptionId, follow) => {
|
|
394
|
-
const done = follow(subscriptionId, {
|
|
395
|
-
next: onAllFollowEventsNext,
|
|
396
|
-
error: onAllFollowEventsError
|
|
397
|
-
});
|
|
398
|
-
unfollow = (sendUnfollow = true) => {
|
|
399
|
-
followSubscription = null;
|
|
400
|
-
unfollow = noop2;
|
|
401
|
-
done();
|
|
402
|
-
sendUnfollow && request(chainHead.unfollow, [subscriptionId]);
|
|
403
|
-
subscriptions.errorAll(new DisjointError());
|
|
404
|
-
ongoingRequests.forEach((cb) => {
|
|
405
|
-
cb();
|
|
406
|
-
});
|
|
407
|
-
ongoingRequests.clear();
|
|
408
|
-
};
|
|
409
|
-
followSubscription = subscriptionId;
|
|
410
|
-
deferredFollow.res(subscriptionId);
|
|
411
|
-
};
|
|
412
|
-
const onFollowRequestError = (e) => {
|
|
413
|
-
if (e instanceof DestroyedError) {
|
|
414
|
-
unfollow(false);
|
|
415
|
-
} else {
|
|
416
|
-
onFollowError(e);
|
|
417
|
-
}
|
|
418
|
-
followSubscription = null;
|
|
419
|
-
deferredFollow.res(e);
|
|
420
|
-
};
|
|
421
|
-
let unfollow = request(
|
|
422
|
-
chainHead.follow,
|
|
423
|
-
[withRuntime],
|
|
424
|
-
{ onSuccess: onFollowRequestSuccess, onError: onFollowRequestError }
|
|
425
|
-
);
|
|
426
|
-
const fRequest = (method, params, cb) => {
|
|
427
|
-
const disjoint = () => {
|
|
428
|
-
cb?.onError(new DisjointError());
|
|
429
|
-
};
|
|
430
|
-
if (followSubscription === null) {
|
|
431
|
-
disjoint();
|
|
432
|
-
return noop2;
|
|
433
|
-
}
|
|
434
|
-
const onSubscription = (subscription) => {
|
|
435
|
-
if (!cb) return request(method, [subscription, ...params]);
|
|
436
|
-
ongoingRequests.add(disjoint);
|
|
437
|
-
const onSubscribeOperation = (operationId, subscriber) => {
|
|
438
|
-
if (followSubscription === null) {
|
|
439
|
-
subscriber.error(new DisjointError());
|
|
440
|
-
return noop2;
|
|
441
|
-
}
|
|
442
|
-
subscriptions.subscribe(operationId, subscriber);
|
|
443
|
-
return () => {
|
|
444
|
-
subscriptions.unsubscribe(operationId);
|
|
445
|
-
};
|
|
446
|
-
};
|
|
447
|
-
const cleanup = request(method, [subscription, ...params], {
|
|
448
|
-
onSuccess: (response) => {
|
|
449
|
-
ongoingRequests.delete(disjoint);
|
|
450
|
-
cb.onSuccess(response, onSubscribeOperation);
|
|
451
|
-
},
|
|
452
|
-
onError: (e) => {
|
|
453
|
-
ongoingRequests.delete(disjoint);
|
|
454
|
-
cb.onError(e);
|
|
455
|
-
}
|
|
456
|
-
});
|
|
457
|
-
return () => {
|
|
458
|
-
ongoingRequests.delete(disjoint);
|
|
459
|
-
cleanup();
|
|
460
|
-
};
|
|
461
|
-
};
|
|
462
|
-
if (typeof followSubscription === "string")
|
|
463
|
-
return onSubscription(followSubscription);
|
|
464
|
-
let onCancel = noop2;
|
|
465
|
-
followSubscription.then((x) => {
|
|
466
|
-
if (x instanceof Error) return disjoint();
|
|
467
|
-
if (followSubscription) onCancel = onSubscription(x);
|
|
468
|
-
});
|
|
469
|
-
return () => {
|
|
470
|
-
onCancel();
|
|
471
|
-
};
|
|
472
|
-
};
|
|
473
|
-
return {
|
|
474
|
-
unfollow() {
|
|
475
|
-
unfollow();
|
|
476
|
-
followSubscription = null;
|
|
477
|
-
},
|
|
478
|
-
body: createBodyFn(fRequest),
|
|
479
|
-
call: createCallFn(fRequest),
|
|
480
|
-
header: createHeaderFn(fRequest),
|
|
481
|
-
storage: createStorageFn(fRequest),
|
|
482
|
-
storageSubscription: createStorageCb(fRequest),
|
|
483
|
-
unpin: createUnpinFn(fRequest),
|
|
484
|
-
_request: fRequest
|
|
485
|
-
};
|
|
486
|
-
};
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
// src/client/RpcError.ts
|
|
490
|
-
var RpcError = class extends Error {
|
|
491
|
-
constructor(e) {
|
|
492
|
-
super(e.message);
|
|
493
|
-
__publicField(this, "code");
|
|
494
|
-
__publicField(this, "data");
|
|
495
|
-
this.code = e.code;
|
|
496
|
-
this.data = e.data;
|
|
497
|
-
this.name = "RpcError";
|
|
498
|
-
}
|
|
499
|
-
};
|
|
500
|
-
|
|
501
|
-
// src/client/createClient.ts
|
|
502
|
-
var nextClientId = 1;
|
|
503
|
-
var createClient = (gProvider) => {
|
|
504
|
-
let clientId = nextClientId++;
|
|
505
|
-
const responses = /* @__PURE__ */ new Map();
|
|
506
|
-
const subscriptions = getSubscriptionsManager();
|
|
507
|
-
let connection = null;
|
|
508
|
-
const send = (id, method, params) => {
|
|
509
|
-
connection.send(
|
|
510
|
-
JSON.stringify({
|
|
511
|
-
jsonrpc: "2.0",
|
|
512
|
-
id,
|
|
513
|
-
method,
|
|
514
|
-
params
|
|
515
|
-
})
|
|
516
|
-
);
|
|
517
|
-
};
|
|
518
|
-
function onMessage(message) {
|
|
519
|
-
try {
|
|
520
|
-
let id, result, error, params, subscription;
|
|
521
|
-
const parsed = JSON.parse(message);
|
|
522
|
-
({ id, result, error, params } = parsed);
|
|
523
|
-
if (id) {
|
|
524
|
-
const cb = responses.get(id);
|
|
525
|
-
if (!cb) return;
|
|
526
|
-
responses.delete(id);
|
|
527
|
-
return error ? cb.onError(new RpcError(error)) : cb.onSuccess(result, (opaqueId, subscriber) => {
|
|
528
|
-
const subscriptionId2 = opaqueId;
|
|
529
|
-
subscriptions.subscribe(subscriptionId2, subscriber);
|
|
530
|
-
return () => {
|
|
531
|
-
subscriptions.unsubscribe(subscriptionId2);
|
|
532
|
-
};
|
|
533
|
-
});
|
|
534
|
-
}
|
|
535
|
-
;
|
|
536
|
-
({ subscription, result, error } = params);
|
|
537
|
-
if (!subscription || !error && !Object.hasOwn(params, "result")) throw 0;
|
|
538
|
-
const subscriptionId = subscription;
|
|
539
|
-
if (error) {
|
|
540
|
-
subscriptions.error(subscriptionId, new RpcError(error));
|
|
541
|
-
} else {
|
|
542
|
-
subscriptions.next(subscriptionId, result);
|
|
543
|
-
}
|
|
544
|
-
} catch (e) {
|
|
545
|
-
console.warn("Error parsing incomming message: " + message);
|
|
546
|
-
console.error(e);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
connection = gProvider(onMessage);
|
|
550
|
-
const disconnect = () => {
|
|
551
|
-
connection?.disconnect();
|
|
552
|
-
connection = null;
|
|
553
|
-
subscriptions.errorAll(new DestroyedError());
|
|
554
|
-
responses.forEach((r) => r.onError(new DestroyedError()));
|
|
555
|
-
responses.clear();
|
|
556
|
-
};
|
|
557
|
-
let nextId = 1;
|
|
558
|
-
const request = (method, params, cb) => {
|
|
559
|
-
if (!connection) throw new Error("Not connected");
|
|
560
|
-
const id = `${clientId}-${nextId++}`;
|
|
561
|
-
if (cb) responses.set(id, cb);
|
|
562
|
-
send(id, method, params);
|
|
563
|
-
return () => {
|
|
564
|
-
responses.delete(id);
|
|
565
|
-
};
|
|
566
|
-
};
|
|
567
|
-
return {
|
|
568
|
-
request,
|
|
569
|
-
disconnect
|
|
570
|
-
};
|
|
571
|
-
};
|
|
572
|
-
|
|
573
|
-
// src/chainspec.ts
|
|
574
|
-
var createGetChainSpec = (clientRequest) => {
|
|
575
|
-
const request = abortablePromiseFn(
|
|
576
|
-
(onSuccess, onError, method, params) => clientRequest(method, params, { onSuccess, onError })
|
|
577
|
-
);
|
|
578
|
-
let cachedPromise = null;
|
|
579
|
-
return async () => {
|
|
580
|
-
if (cachedPromise) return cachedPromise;
|
|
581
|
-
return cachedPromise = Promise.all([
|
|
582
|
-
request(chainSpec.chainName, []),
|
|
583
|
-
request(chainSpec.genesisHash, []),
|
|
584
|
-
request(chainSpec.properties, [])
|
|
585
|
-
]).then(([name, genesisHash, properties]) => ({
|
|
586
|
-
name,
|
|
587
|
-
genesisHash,
|
|
588
|
-
properties
|
|
589
|
-
}));
|
|
590
|
-
};
|
|
591
|
-
};
|
|
592
|
-
|
|
593
|
-
// src/request-compatibility-enhancer.ts
|
|
594
|
-
var getCompatibilityEnhancer = (rpcMethodsP, request) => (methods) => {
|
|
595
|
-
let translations = {};
|
|
596
|
-
let enhancedRequest = null;
|
|
597
|
-
return (method, ...rest) => {
|
|
598
|
-
if (enhancedRequest) return enhancedRequest(method, ...rest);
|
|
599
|
-
let isRunning = true;
|
|
600
|
-
let cleanup = () => {
|
|
601
|
-
isRunning = false;
|
|
602
|
-
};
|
|
603
|
-
rpcMethodsP.then((rpcMethods) => {
|
|
604
|
-
enhancedRequest = (method_, ...iRest) => {
|
|
605
|
-
const method2 = translations[method_] ?? method_;
|
|
606
|
-
if (rpcMethods.has(method2)) return request(method2, ...iRest);
|
|
607
|
-
iRest[1]?.onError(new Error(`Unsupported method ${method2}`));
|
|
608
|
-
return noop2;
|
|
609
|
-
};
|
|
610
|
-
if (rpcMethods.has(method)) return;
|
|
611
|
-
const parts = method.split("_");
|
|
612
|
-
if (parts[1] !== "v1") return;
|
|
613
|
-
parts[1] = "unstable";
|
|
614
|
-
if (rpcMethods.has(parts.join("_")))
|
|
615
|
-
Object.values(methods).forEach((value) => {
|
|
616
|
-
translations[value] = value.replace("_v1_", "_unstable_");
|
|
617
|
-
});
|
|
618
|
-
else if (parts[0] === "transaction") {
|
|
619
|
-
let unwatch;
|
|
620
|
-
let version;
|
|
621
|
-
const txGroup = ["transactionWatch", "transaction"].find(
|
|
622
|
-
(group) => {
|
|
623
|
-
version = ["v1", "unstable"].find(
|
|
624
|
-
(v) => rpcMethods.has(unwatch = `${group}_${v}_unwatch`)
|
|
625
|
-
);
|
|
626
|
-
return !!version;
|
|
627
|
-
}
|
|
628
|
-
);
|
|
629
|
-
if (txGroup) {
|
|
630
|
-
translations[methods.broadcast] = `${txGroup}_${version}_submitAndWatch`;
|
|
631
|
-
translations[methods.stop] = unwatch;
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
}).then(() => {
|
|
635
|
-
if (isRunning) cleanup = enhancedRequest(method, ...rest);
|
|
636
|
-
});
|
|
637
|
-
return () => {
|
|
638
|
-
cleanup();
|
|
639
|
-
};
|
|
640
|
-
};
|
|
641
|
-
};
|
|
642
|
-
|
|
643
|
-
// src/index.ts
|
|
644
|
-
import { AbortError as AbortError2 } from "@polkadot-api/utils";
|
|
645
|
-
var createClient2 = (provider) => {
|
|
646
|
-
const client = createClient(provider);
|
|
647
|
-
const request = abortablePromiseFn(
|
|
648
|
-
(onSuccess, onError, method, params) => client.request(method, params, { onSuccess, onError })
|
|
649
|
-
);
|
|
650
|
-
const rpcMethods = request("rpc_methods", []).then(
|
|
651
|
-
(x) => new Set(Array.isArray(x) ? x : x.methods),
|
|
652
|
-
() => /* @__PURE__ */ new Set()
|
|
653
|
-
);
|
|
654
|
-
const compatibilityEnhancer = getCompatibilityEnhancer(
|
|
655
|
-
rpcMethods,
|
|
656
|
-
client.request
|
|
657
|
-
);
|
|
658
|
-
return {
|
|
659
|
-
chainHead: getChainHead(
|
|
660
|
-
compatibilityEnhancer(chainHead)
|
|
661
|
-
),
|
|
662
|
-
transaction: getTransaction(
|
|
663
|
-
compatibilityEnhancer(transaction)
|
|
664
|
-
),
|
|
665
|
-
getChainSpecData: createGetChainSpec(
|
|
666
|
-
compatibilityEnhancer(chainSpec)
|
|
667
|
-
),
|
|
668
|
-
destroy: () => {
|
|
669
|
-
client.disconnect();
|
|
670
|
-
},
|
|
671
|
-
request,
|
|
672
|
-
_request: client.request
|
|
673
|
-
};
|
|
674
|
-
};
|
|
675
|
-
export {
|
|
676
|
-
AbortError2 as AbortError,
|
|
677
|
-
DestroyedError,
|
|
678
|
-
DisjointError,
|
|
679
|
-
OperationError,
|
|
680
|
-
OperationInaccessibleError,
|
|
681
|
-
OperationLimitError,
|
|
682
|
-
RpcError,
|
|
683
|
-
StopError,
|
|
684
|
-
createClient2 as createClient
|
|
685
|
-
};
|
|
686
|
-
//# sourceMappingURL=index.mjs.map
|