@leofcoin/chain 1.5.29 → 1.5.30

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/exports/chain.js CHANGED
@@ -1064,7 +1064,8 @@ class State extends Contract {
1064
1064
  get shouldSync() {
1065
1065
  if (this.#chainSyncing)
1066
1066
  return false;
1067
- if (this.#resolveErrored ||
1067
+ if (!this.#chainSyncing ||
1068
+ this.#resolveErrored ||
1068
1069
  this.#syncState === 'errored' ||
1069
1070
  this.#syncState === 'connectionless' ||
1070
1071
  this.#lastResolvedTime + this.resolveTimeout > Date.now())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.5.29",
3
+ "version": "1.5.30",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": {
@@ -1,451 +0,0 @@
1
- const encode = (string) => {
2
- if (typeof string === 'string')
3
- return new TextEncoder().encode(string);
4
- throw Error(`expected typeof String instead got ${string}`);
5
- };
6
- const decode = (uint8Array) => {
7
- if (uint8Array instanceof Uint8Array)
8
- return new TextDecoder().decode(uint8Array);
9
- throw Error(`expected typeof uint8Array instead got ${uint8Array}`);
10
- };
11
-
12
- const pathSepS = '/';
13
- class KeyPath {
14
- uint8Array;
15
- constructor(input) {
16
- if (typeof input === 'string') {
17
- this.uint8Array = encode(input);
18
- }
19
- else if (input instanceof Uint8Array) {
20
- this.uint8Array = input;
21
- }
22
- else if (input instanceof KeyPath) {
23
- this.uint8Array = input.uint8Array;
24
- }
25
- else {
26
- throw new Error('Invalid keyPath, should be a String, Uint8Array or KeyPath');
27
- }
28
- }
29
- isKeyPath() {
30
- return true;
31
- }
32
- toString() {
33
- return decode(this.uint8Array);
34
- }
35
- /**
36
- * Returns the `list` representation of this path.
37
- *
38
- * @example
39
- * ```js
40
- * new Key('/Comedy/MontyPython/Actor:JohnCleese').list()
41
- * // => ['Comedy', 'MontyPythong', 'Actor:JohnCleese']
42
- * ```
43
- */
44
- list() {
45
- return this.toString().split(pathSepS).slice(1);
46
- }
47
- }
48
-
49
- class KeyValue {
50
- uint8Array;
51
- constructor(input) {
52
- if (typeof input === 'string') {
53
- this.uint8Array = encode(input);
54
- }
55
- else if (input instanceof Uint8Array) {
56
- this.uint8Array = input;
57
- }
58
- else if (input instanceof KeyValue) {
59
- this.uint8Array = input.uint8Array;
60
- }
61
- else {
62
- throw new Error('Invalid KeyValue, should be a String, Uint8Array or KeyValue');
63
- }
64
- }
65
- isKeyValue() {
66
- return true;
67
- }
68
- toString() {
69
- return decode(this.uint8Array);
70
- }
71
- }
72
-
73
- const instanceOfAny = (object, constructors) => constructors.some((c) => object instanceof c);
74
-
75
- let idbProxyableTypes;
76
- let cursorAdvanceMethods;
77
- // This is a function to prevent it throwing up in node environments.
78
- function getIdbProxyableTypes() {
79
- return (idbProxyableTypes ||
80
- (idbProxyableTypes = [
81
- IDBDatabase,
82
- IDBObjectStore,
83
- IDBIndex,
84
- IDBCursor,
85
- IDBTransaction,
86
- ]));
87
- }
88
- // This is a function to prevent it throwing up in node environments.
89
- function getCursorAdvanceMethods() {
90
- return (cursorAdvanceMethods ||
91
- (cursorAdvanceMethods = [
92
- IDBCursor.prototype.advance,
93
- IDBCursor.prototype.continue,
94
- IDBCursor.prototype.continuePrimaryKey,
95
- ]));
96
- }
97
- const cursorRequestMap = new WeakMap();
98
- const transactionDoneMap = new WeakMap();
99
- const transactionStoreNamesMap = new WeakMap();
100
- const transformCache = new WeakMap();
101
- const reverseTransformCache = new WeakMap();
102
- function promisifyRequest(request) {
103
- const promise = new Promise((resolve, reject) => {
104
- const unlisten = () => {
105
- request.removeEventListener('success', success);
106
- request.removeEventListener('error', error);
107
- };
108
- const success = () => {
109
- resolve(wrap(request.result));
110
- unlisten();
111
- };
112
- const error = () => {
113
- reject(request.error);
114
- unlisten();
115
- };
116
- request.addEventListener('success', success);
117
- request.addEventListener('error', error);
118
- });
119
- promise
120
- .then((value) => {
121
- // Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval
122
- // (see wrapFunction).
123
- if (value instanceof IDBCursor) {
124
- cursorRequestMap.set(value, request);
125
- }
126
- // Catching to avoid "Uncaught Promise exceptions"
127
- })
128
- .catch(() => { });
129
- // This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
130
- // is because we create many promises from a single IDBRequest.
131
- reverseTransformCache.set(promise, request);
132
- return promise;
133
- }
134
- function cacheDonePromiseForTransaction(tx) {
135
- // Early bail if we've already created a done promise for this transaction.
136
- if (transactionDoneMap.has(tx))
137
- return;
138
- const done = new Promise((resolve, reject) => {
139
- const unlisten = () => {
140
- tx.removeEventListener('complete', complete);
141
- tx.removeEventListener('error', error);
142
- tx.removeEventListener('abort', error);
143
- };
144
- const complete = () => {
145
- resolve();
146
- unlisten();
147
- };
148
- const error = () => {
149
- reject(tx.error || new DOMException('AbortError', 'AbortError'));
150
- unlisten();
151
- };
152
- tx.addEventListener('complete', complete);
153
- tx.addEventListener('error', error);
154
- tx.addEventListener('abort', error);
155
- });
156
- // Cache it for later retrieval.
157
- transactionDoneMap.set(tx, done);
158
- }
159
- let idbProxyTraps = {
160
- get(target, prop, receiver) {
161
- if (target instanceof IDBTransaction) {
162
- // Special handling for transaction.done.
163
- if (prop === 'done')
164
- return transactionDoneMap.get(target);
165
- // Polyfill for objectStoreNames because of Edge.
166
- if (prop === 'objectStoreNames') {
167
- return target.objectStoreNames || transactionStoreNamesMap.get(target);
168
- }
169
- // Make tx.store return the only store in the transaction, or undefined if there are many.
170
- if (prop === 'store') {
171
- return receiver.objectStoreNames[1]
172
- ? undefined
173
- : receiver.objectStore(receiver.objectStoreNames[0]);
174
- }
175
- }
176
- // Else transform whatever we get back.
177
- return wrap(target[prop]);
178
- },
179
- set(target, prop, value) {
180
- target[prop] = value;
181
- return true;
182
- },
183
- has(target, prop) {
184
- if (target instanceof IDBTransaction &&
185
- (prop === 'done' || prop === 'store')) {
186
- return true;
187
- }
188
- return prop in target;
189
- },
190
- };
191
- function replaceTraps(callback) {
192
- idbProxyTraps = callback(idbProxyTraps);
193
- }
194
- function wrapFunction(func) {
195
- // Due to expected object equality (which is enforced by the caching in `wrap`), we
196
- // only create one new func per func.
197
- // Edge doesn't support objectStoreNames (booo), so we polyfill it here.
198
- if (func === IDBDatabase.prototype.transaction &&
199
- !('objectStoreNames' in IDBTransaction.prototype)) {
200
- return function (storeNames, ...args) {
201
- const tx = func.call(unwrap(this), storeNames, ...args);
202
- transactionStoreNamesMap.set(tx, storeNames.sort ? storeNames.sort() : [storeNames]);
203
- return wrap(tx);
204
- };
205
- }
206
- // Cursor methods are special, as the behaviour is a little more different to standard IDB. In
207
- // IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
208
- // cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
209
- // with real promises, so each advance methods returns a new promise for the cursor object, or
210
- // undefined if the end of the cursor has been reached.
211
- if (getCursorAdvanceMethods().includes(func)) {
212
- return function (...args) {
213
- // Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
214
- // the original object.
215
- func.apply(unwrap(this), args);
216
- return wrap(cursorRequestMap.get(this));
217
- };
218
- }
219
- return function (...args) {
220
- // Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
221
- // the original object.
222
- return wrap(func.apply(unwrap(this), args));
223
- };
224
- }
225
- function transformCachableValue(value) {
226
- if (typeof value === 'function')
227
- return wrapFunction(value);
228
- // This doesn't return, it just creates a 'done' promise for the transaction,
229
- // which is later returned for transaction.done (see idbObjectHandler).
230
- if (value instanceof IDBTransaction)
231
- cacheDonePromiseForTransaction(value);
232
- if (instanceOfAny(value, getIdbProxyableTypes()))
233
- return new Proxy(value, idbProxyTraps);
234
- // Return the same value back if we're not going to transform it.
235
- return value;
236
- }
237
- function wrap(value) {
238
- // We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
239
- // IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
240
- if (value instanceof IDBRequest)
241
- return promisifyRequest(value);
242
- // If we've already transformed this value before, reuse the transformed value.
243
- // This is faster, but it also provides object equality.
244
- if (transformCache.has(value))
245
- return transformCache.get(value);
246
- const newValue = transformCachableValue(value);
247
- // Not all types are transformed.
248
- // These may be primitive types, so they can't be WeakMap keys.
249
- if (newValue !== value) {
250
- transformCache.set(value, newValue);
251
- reverseTransformCache.set(newValue, value);
252
- }
253
- return newValue;
254
- }
255
- const unwrap = (value) => reverseTransformCache.get(value);
256
-
257
- /**
258
- * Open a database.
259
- *
260
- * @param name Name of the database.
261
- * @param version Schema version.
262
- * @param callbacks Additional callbacks.
263
- */
264
- function openDB(name, version, { blocked, upgrade, blocking, terminated } = {}) {
265
- const request = indexedDB.open(name, version);
266
- const openPromise = wrap(request);
267
- if (upgrade) {
268
- request.addEventListener('upgradeneeded', (event) => {
269
- upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction), event);
270
- });
271
- }
272
- if (blocked) {
273
- request.addEventListener('blocked', (event) => blocked(
274
- // Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
275
- event.oldVersion, event.newVersion, event));
276
- }
277
- openPromise
278
- .then((db) => {
279
- if (terminated)
280
- db.addEventListener('close', () => terminated());
281
- if (blocking) {
282
- db.addEventListener('versionchange', (event) => blocking(event.oldVersion, event.newVersion, event));
283
- }
284
- })
285
- .catch(() => { });
286
- return openPromise;
287
- }
288
-
289
- const readMethods = ['get', 'getKey', 'getAll', 'getAllKeys', 'count'];
290
- const writeMethods = ['put', 'add', 'delete', 'clear'];
291
- const cachedMethods = new Map();
292
- function getMethod(target, prop) {
293
- if (!(target instanceof IDBDatabase &&
294
- !(prop in target) &&
295
- typeof prop === 'string')) {
296
- return;
297
- }
298
- if (cachedMethods.get(prop))
299
- return cachedMethods.get(prop);
300
- const targetFuncName = prop.replace(/FromIndex$/, '');
301
- const useIndex = prop !== targetFuncName;
302
- const isWrite = writeMethods.includes(targetFuncName);
303
- if (
304
- // Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.
305
- !(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) ||
306
- !(isWrite || readMethods.includes(targetFuncName))) {
307
- return;
308
- }
309
- const method = async function (storeName, ...args) {
310
- // isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :(
311
- const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly');
312
- let target = tx.store;
313
- if (useIndex)
314
- target = target.index(args.shift());
315
- // Must reject if op rejects.
316
- // If it's a write operation, must reject if tx.done rejects.
317
- // Must reject with op rejection first.
318
- // Must resolve with op value.
319
- // Must handle both promises (no unhandled rejections)
320
- return (await Promise.all([
321
- target[targetFuncName](...args),
322
- isWrite && tx.done,
323
- ]))[0];
324
- };
325
- cachedMethods.set(prop, method);
326
- return method;
327
- }
328
- replaceTraps((oldTraps) => ({
329
- ...oldTraps,
330
- get: (target, prop, receiver) => getMethod(target, prop) || oldTraps.get(target, prop, receiver),
331
- has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop),
332
- }));
333
-
334
- const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];
335
- const methodMap = {};
336
- const advanceResults = new WeakMap();
337
- const ittrProxiedCursorToOriginalProxy = new WeakMap();
338
- const cursorIteratorTraps = {
339
- get(target, prop) {
340
- if (!advanceMethodProps.includes(prop))
341
- return target[prop];
342
- let cachedFunc = methodMap[prop];
343
- if (!cachedFunc) {
344
- cachedFunc = methodMap[prop] = function (...args) {
345
- advanceResults.set(this, ittrProxiedCursorToOriginalProxy.get(this)[prop](...args));
346
- };
347
- }
348
- return cachedFunc;
349
- },
350
- };
351
- async function* iterate(...args) {
352
- // tslint:disable-next-line:no-this-assignment
353
- let cursor = this;
354
- if (!(cursor instanceof IDBCursor)) {
355
- cursor = await cursor.openCursor(...args);
356
- }
357
- if (!cursor)
358
- return;
359
- cursor = cursor;
360
- const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);
361
- ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);
362
- // Map this double-proxy back to the original, so other cursor methods work.
363
- reverseTransformCache.set(proxiedCursor, unwrap(cursor));
364
- while (cursor) {
365
- yield proxiedCursor;
366
- // If one of the advancing methods was not called, call continue().
367
- cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());
368
- advanceResults.delete(proxiedCursor);
369
- }
370
- }
371
- function isIteratorProp(target, prop) {
372
- return ((prop === Symbol.asyncIterator &&
373
- instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||
374
- (prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore])));
375
- }
376
- replaceTraps((oldTraps) => ({
377
- ...oldTraps,
378
- get(target, prop, receiver) {
379
- if (isIteratorProp(target, prop))
380
- return iterate;
381
- return oldTraps.get(target, prop, receiver);
382
- },
383
- has(target, prop) {
384
- return isIteratorProp(target, prop) || oldTraps.has(target, prop);
385
- },
386
- }));
387
-
388
- class BrowerStore {
389
- db;
390
- name;
391
- root;
392
- version;
393
- constructor(name = 'storage', root = '.leofcoin', version = '1') {
394
- this.version = version;
395
- this.name = name;
396
- this.root = root;
397
- this.db = openDB(`${root}/${name}`, Number(version), {
398
- upgrade(db) {
399
- db.createObjectStore(name);
400
- }
401
- });
402
- }
403
- toKeyPath(key) {
404
- if (key instanceof KeyPath)
405
- key = new KeyPath(key);
406
- return key.toString();
407
- }
408
- toKeyValue(value) {
409
- if (value instanceof KeyValue)
410
- value = new KeyValue(value);
411
- // @ts-ignore
412
- return value.uint8Array;
413
- }
414
- async get(key) {
415
- return (await this.db).get(this.name, this.toKeyPath(key));
416
- }
417
- async put(key, value) {
418
- return (await this.db).put(this.name, this.toKeyValue(value), this.toKeyPath(key));
419
- }
420
- async delete(key) {
421
- return (await this.db).delete(this.name, this.toKeyPath(key));
422
- }
423
- async clear() {
424
- return (await this.db).clear(this.name);
425
- }
426
- async values(limit = -1) {
427
- const values = [];
428
- const tx = (await this.db).transaction(this.name);
429
- for await (const cursor of tx.store) {
430
- values.push(cursor.value);
431
- if (limit && values.length === limit)
432
- return values;
433
- }
434
- return values;
435
- }
436
- async keys(limit = -1) {
437
- const keys = [];
438
- const tx = (await this.db).transaction(this.name);
439
- for await (const cursor of tx.store) {
440
- keys.push(cursor.key);
441
- if (limit && keys.length === limit)
442
- return keys;
443
- }
444
- return keys;
445
- }
446
- async iterate() {
447
- return (await this.db).transaction(this.name).store;
448
- }
449
- }
450
-
451
- export { BrowerStore as default };