@leofcoin/chain 1.7.55 → 1.7.65
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 +24 -16
- package/exports/helpers/contract.d.ts +4 -0
- package/package.json +14 -14
- package/exports/browser/browser-DQJ6xf_F-BqXoTvHj.js +0 -188
- package/exports/browser/browser-pguCHlVu-pguCHlVu.js +0 -25
- package/exports/browser/browser-store.js +0 -306
- package/exports/browser/chain.js +0 -9295
- package/exports/browser/client-Depp28gl-CAZHe8Gn.js +0 -638
- package/exports/browser/index-BeqbCwUk-CO_3tWk0.js +0 -37
- package/exports/browser/index-DqPlTtAJ-C6Al_ebv.js +0 -7888
- package/exports/browser/index-jJAWNcIz.js +0 -8409
- package/exports/browser/messages-RYLqPGkg-B8phafAq.js +0 -215
- package/exports/browser/node-browser-CDiT4wAl.js +0 -30072
- package/exports/browser/node-browser.js +0 -2
- package/exports/browser/password-oDixGC8h.js +0 -3
- package/exports/browser/qr-scanner-worker.min-Dy0qkKA4-Dy0qkKA4.js +0 -100
- package/exports/browser/workers/block-worker.js +0 -33
- package/exports/browser/workers/machine-worker.js +0 -511
- package/exports/browser/workers/worker-2Vk145X_-2Vk145X_.js +0 -8444
- package/exports/workers/block-worker.js +0 -33
- package/exports/workers/machine-worker.js +0 -511
- package/exports/workers/worker-2Vk145X_-2Vk145X_.js +0 -8444
package/exports/chain.js
CHANGED
|
@@ -280,7 +280,7 @@ class Machine {
|
|
|
280
280
|
this.states = {
|
|
281
281
|
states: {},
|
|
282
282
|
lastBlock: {
|
|
283
|
-
index:
|
|
283
|
+
index: -1,
|
|
284
284
|
hash: ''
|
|
285
285
|
},
|
|
286
286
|
accounts: {},
|
|
@@ -429,7 +429,7 @@ class Machine {
|
|
|
429
429
|
await Promise.all(promises);
|
|
430
430
|
}
|
|
431
431
|
const tasks = [
|
|
432
|
-
stateStore.put('lastBlock', JSON.stringify(await this.lastBlock)),
|
|
432
|
+
stateStore.put('lastBlock', JSON.stringify(await this.lastBlock, jsonStringifyBigInt)),
|
|
433
433
|
stateStore.put('states', JSON.stringify(state, jsonStringifyBigInt)),
|
|
434
434
|
stateStore.put('accounts', JSON.stringify(accounts, jsonStringifyBigInt)),
|
|
435
435
|
stateStore.put('info', JSON.stringify({
|
|
@@ -476,16 +476,22 @@ class Machine {
|
|
|
476
476
|
type: 'module'
|
|
477
477
|
});
|
|
478
478
|
this.worker.onmessage(this.#onmessage.bind(this));
|
|
479
|
+
let rawLastBlock;
|
|
480
|
+
let rawStates;
|
|
481
|
+
let rawAccounts;
|
|
482
|
+
let rawInfo;
|
|
479
483
|
if (await stateStore.has('lastBlock')) {
|
|
480
|
-
|
|
481
|
-
|
|
484
|
+
const decoder = new TextDecoder();
|
|
485
|
+
const decode = (param) => decoder.decode(param);
|
|
486
|
+
rawLastBlock = decode(await stateStore.get('lastBlock'));
|
|
487
|
+
this.states.lastBlock = JSON.parse(rawLastBlock, jsonParseBigInt);
|
|
482
488
|
try {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
this.states.info =
|
|
489
|
+
rawStates = decode(await stateStore.get('states'));
|
|
490
|
+
rawAccounts = decode(await stateStore.get('accounts'));
|
|
491
|
+
rawInfo = decode(await stateStore.get('info'));
|
|
492
|
+
this.states.states = JSON.parse(rawStates, jsonParseBigInt);
|
|
493
|
+
this.states.accounts = JSON.parse(rawAccounts, jsonParseBigInt);
|
|
494
|
+
this.states.info = JSON.parse(rawInfo, jsonParseBigInt);
|
|
489
495
|
}
|
|
490
496
|
catch (error) {
|
|
491
497
|
console.error(error);
|
|
@@ -502,16 +508,18 @@ class Machine {
|
|
|
502
508
|
totalTransactions: BigInt(0),
|
|
503
509
|
totalBlocks: BigInt(0)
|
|
504
510
|
};
|
|
511
|
+
rawInfo = JSON.stringify(this.states.info, jsonStringifyBigInt);
|
|
512
|
+
await stateStore.put('info', new TextEncoder().encode(rawInfo));
|
|
505
513
|
}
|
|
506
514
|
}
|
|
507
515
|
const message = {
|
|
508
516
|
type: 'init',
|
|
509
517
|
input: {
|
|
510
518
|
blocks,
|
|
511
|
-
fromState: this.states.lastBlock
|
|
512
|
-
lastBlock:
|
|
513
|
-
state:
|
|
514
|
-
info:
|
|
519
|
+
fromState: this.states.lastBlock?.index >= 0,
|
|
520
|
+
lastBlock: rawLastBlock,
|
|
521
|
+
state: rawStates,
|
|
522
|
+
info: rawInfo,
|
|
515
523
|
// @ts-ignore
|
|
516
524
|
peerid: peernet.peerId
|
|
517
525
|
}
|
|
@@ -1764,7 +1772,7 @@ class Chain extends VersionControl {
|
|
|
1764
1772
|
fees: BigInt(0),
|
|
1765
1773
|
timestamp,
|
|
1766
1774
|
previousHash: '',
|
|
1767
|
-
reward:
|
|
1775
|
+
reward: BigInt(150),
|
|
1768
1776
|
index: 0
|
|
1769
1777
|
};
|
|
1770
1778
|
const latestTransactions = await this.machine.latestTransactions();
|
|
@@ -1821,7 +1829,7 @@ class Chain extends VersionControl {
|
|
|
1821
1829
|
block.validators = block.validators.map((validator) => {
|
|
1822
1830
|
validator.reward = block.fees;
|
|
1823
1831
|
validator.reward += block.reward;
|
|
1824
|
-
validator.reward /= block.validators.length;
|
|
1832
|
+
validator.reward /= BigInt(block.validators.length);
|
|
1825
1833
|
delete validator.bw;
|
|
1826
1834
|
return validator;
|
|
1827
1835
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/chain",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.65",
|
|
4
4
|
"description": "Official javascript implementation",
|
|
5
5
|
"private": false,
|
|
6
6
|
"exports": {
|
|
@@ -49,30 +49,30 @@
|
|
|
49
49
|
"author": "",
|
|
50
50
|
"license": "MIT",
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@rollup/plugin-commonjs": "^
|
|
52
|
+
"@rollup/plugin-commonjs": "^28.0.0",
|
|
53
53
|
"@rollup/plugin-json": "^6.1.0",
|
|
54
|
-
"@rollup/plugin-node-resolve": "^15.
|
|
55
|
-
"@rollup/plugin-typescript": "^
|
|
54
|
+
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
55
|
+
"@rollup/plugin-typescript": "^12.1.0",
|
|
56
56
|
"@types/semver": "^7.5.8",
|
|
57
57
|
"@vandeurenglenn/debug": "^1.2.5",
|
|
58
|
-
"rollup": "^4.
|
|
58
|
+
"rollup": "^4.23.0",
|
|
59
59
|
"rollup-plugin-modify": "^3.0.0",
|
|
60
60
|
"tape": "^5.9.0",
|
|
61
61
|
"tslib": "^2.7.0"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@leofcoin/addresses": "^1.0.
|
|
65
|
-
"@leofcoin/contracts": "^0.1.
|
|
66
|
-
"@leofcoin/crypto": "^0.2.
|
|
67
|
-
"@leofcoin/errors": "^1.0.
|
|
68
|
-
"@leofcoin/lib": "^1.2.
|
|
69
|
-
"@leofcoin/messages": "^1.4.
|
|
64
|
+
"@leofcoin/addresses": "^1.0.44",
|
|
65
|
+
"@leofcoin/contracts": "^0.1.14",
|
|
66
|
+
"@leofcoin/crypto": "^0.2.28",
|
|
67
|
+
"@leofcoin/errors": "^1.0.23",
|
|
68
|
+
"@leofcoin/lib": "^1.2.67",
|
|
69
|
+
"@leofcoin/messages": "^1.4.37",
|
|
70
70
|
"@leofcoin/multi-wallet": "^3.1.8",
|
|
71
|
-
"@leofcoin/networks": "^1.1.
|
|
71
|
+
"@leofcoin/networks": "^1.1.22",
|
|
72
72
|
"@leofcoin/peernet": "^1.1.80",
|
|
73
73
|
"@leofcoin/storage": "^3.5.32",
|
|
74
|
-
"@leofcoin/utils": "^1.1.
|
|
75
|
-
"@leofcoin/workers": "^1.5.
|
|
74
|
+
"@leofcoin/utils": "^1.1.36",
|
|
75
|
+
"@leofcoin/workers": "^1.5.19",
|
|
76
76
|
"@vandeurenglenn/base58": "^1.1.9",
|
|
77
77
|
"@vandeurenglenn/easy-worker": "^1.0.2",
|
|
78
78
|
"semver": "^7.6.3"
|
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import { g as getDefaultExportFromCjs } from './node-browser-CDiT4wAl.js';
|
|
2
|
-
import './index-jJAWNcIz.js';
|
|
3
|
-
|
|
4
|
-
var global;
|
|
5
|
-
var hasRequiredGlobal;
|
|
6
|
-
|
|
7
|
-
function requireGlobal () {
|
|
8
|
-
if (hasRequiredGlobal) return global;
|
|
9
|
-
hasRequiredGlobal = 1;
|
|
10
|
-
var naiveFallback = function () {
|
|
11
|
-
if (typeof self === "object" && self) return self;
|
|
12
|
-
if (typeof window === "object" && window) return window;
|
|
13
|
-
throw new Error("Unable to resolve global `this`");
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
global = (function () {
|
|
17
|
-
if (this) return this;
|
|
18
|
-
|
|
19
|
-
// Unexpected strict mode (may happen if e.g. bundled into ESM module)
|
|
20
|
-
|
|
21
|
-
// Fallback to standard globalThis if available
|
|
22
|
-
if (typeof globalThis === "object" && globalThis) return globalThis;
|
|
23
|
-
|
|
24
|
-
// Thanks @mathiasbynens -> https://mathiasbynens.be/notes/globalthis
|
|
25
|
-
// In all ES5+ engines global object inherits from Object.prototype
|
|
26
|
-
// (if you approached one that doesn't please report)
|
|
27
|
-
try {
|
|
28
|
-
Object.defineProperty(Object.prototype, "__global__", {
|
|
29
|
-
get: function () { return this; },
|
|
30
|
-
configurable: true
|
|
31
|
-
});
|
|
32
|
-
} catch (error) {
|
|
33
|
-
// Unfortunate case of updates to Object.prototype being restricted
|
|
34
|
-
// via preventExtensions, seal or freeze
|
|
35
|
-
return naiveFallback();
|
|
36
|
-
}
|
|
37
|
-
try {
|
|
38
|
-
// Safari case (window.__global__ works, but __global__ does not)
|
|
39
|
-
if (!__global__) return naiveFallback();
|
|
40
|
-
return __global__;
|
|
41
|
-
} finally {
|
|
42
|
-
delete Object.prototype.__global__;
|
|
43
|
-
}
|
|
44
|
-
})();
|
|
45
|
-
return global;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
var name = "websocket";
|
|
49
|
-
var description = "Websocket Client & Server Library implementing the WebSocket protocol as specified in RFC 6455.";
|
|
50
|
-
var keywords = [
|
|
51
|
-
"websocket",
|
|
52
|
-
"websockets",
|
|
53
|
-
"socket",
|
|
54
|
-
"networking",
|
|
55
|
-
"comet",
|
|
56
|
-
"push",
|
|
57
|
-
"RFC-6455",
|
|
58
|
-
"realtime",
|
|
59
|
-
"server",
|
|
60
|
-
"client"
|
|
61
|
-
];
|
|
62
|
-
var author = "Brian McKelvey <theturtle32@gmail.com> (https://github.com/theturtle32)";
|
|
63
|
-
var contributors = [
|
|
64
|
-
"Iñaki Baz Castillo <ibc@aliax.net> (http://dev.sipdoc.net)"
|
|
65
|
-
];
|
|
66
|
-
var version$1 = "1.0.35";
|
|
67
|
-
var repository = {
|
|
68
|
-
type: "git",
|
|
69
|
-
url: "https://github.com/theturtle32/WebSocket-Node.git"
|
|
70
|
-
};
|
|
71
|
-
var homepage = "https://github.com/theturtle32/WebSocket-Node";
|
|
72
|
-
var engines = {
|
|
73
|
-
node: ">=4.0.0"
|
|
74
|
-
};
|
|
75
|
-
var dependencies = {
|
|
76
|
-
bufferutil: "^4.0.1",
|
|
77
|
-
debug: "^2.2.0",
|
|
78
|
-
"es5-ext": "^0.10.63",
|
|
79
|
-
"typedarray-to-buffer": "^3.1.5",
|
|
80
|
-
"utf-8-validate": "^5.0.2",
|
|
81
|
-
yaeti: "^0.0.6"
|
|
82
|
-
};
|
|
83
|
-
var devDependencies = {
|
|
84
|
-
"buffer-equal": "^1.0.0",
|
|
85
|
-
gulp: "^4.0.2",
|
|
86
|
-
"gulp-jshint": "^2.0.4",
|
|
87
|
-
"jshint-stylish": "^2.2.1",
|
|
88
|
-
jshint: "^2.0.0",
|
|
89
|
-
tape: "^4.9.1"
|
|
90
|
-
};
|
|
91
|
-
var config = {
|
|
92
|
-
verbose: false
|
|
93
|
-
};
|
|
94
|
-
var scripts = {
|
|
95
|
-
test: "tape test/unit/*.js",
|
|
96
|
-
gulp: "gulp"
|
|
97
|
-
};
|
|
98
|
-
var main = "index";
|
|
99
|
-
var directories = {
|
|
100
|
-
lib: "./lib"
|
|
101
|
-
};
|
|
102
|
-
var browser$3 = "lib/browser.js";
|
|
103
|
-
var license = "Apache-2.0";
|
|
104
|
-
var require$$0 = {
|
|
105
|
-
name: name,
|
|
106
|
-
description: description,
|
|
107
|
-
keywords: keywords,
|
|
108
|
-
author: author,
|
|
109
|
-
contributors: contributors,
|
|
110
|
-
version: version$1,
|
|
111
|
-
repository: repository,
|
|
112
|
-
homepage: homepage,
|
|
113
|
-
engines: engines,
|
|
114
|
-
dependencies: dependencies,
|
|
115
|
-
devDependencies: devDependencies,
|
|
116
|
-
config: config,
|
|
117
|
-
scripts: scripts,
|
|
118
|
-
main: main,
|
|
119
|
-
directories: directories,
|
|
120
|
-
browser: browser$3,
|
|
121
|
-
license: license
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
var version = require$$0.version;
|
|
125
|
-
|
|
126
|
-
var _globalThis;
|
|
127
|
-
if (typeof globalThis === 'object') {
|
|
128
|
-
_globalThis = globalThis;
|
|
129
|
-
} else {
|
|
130
|
-
try {
|
|
131
|
-
_globalThis = requireGlobal();
|
|
132
|
-
} catch (error) {
|
|
133
|
-
} finally {
|
|
134
|
-
if (!_globalThis && typeof window !== 'undefined') { _globalThis = window; }
|
|
135
|
-
if (!_globalThis) { throw new Error('Could not determine global this'); }
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
var NativeWebSocket = _globalThis.WebSocket || _globalThis.MozWebSocket;
|
|
140
|
-
var websocket_version = version;
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Expose a W3C WebSocket class with just one or two arguments.
|
|
145
|
-
*/
|
|
146
|
-
function W3CWebSocket(uri, protocols) {
|
|
147
|
-
var native_instance;
|
|
148
|
-
|
|
149
|
-
if (protocols) {
|
|
150
|
-
native_instance = new NativeWebSocket(uri, protocols);
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
native_instance = new NativeWebSocket(uri);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* 'native_instance' is an instance of nativeWebSocket (the browser's WebSocket
|
|
158
|
-
* class). Since it is an Object it will be returned as it is when creating an
|
|
159
|
-
* instance of W3CWebSocket via 'new W3CWebSocket()'.
|
|
160
|
-
*
|
|
161
|
-
* ECMAScript 5: http://bclary.com/2004/11/07/#a-13.2.2
|
|
162
|
-
*/
|
|
163
|
-
return native_instance;
|
|
164
|
-
}
|
|
165
|
-
if (NativeWebSocket) {
|
|
166
|
-
['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'].forEach(function(prop) {
|
|
167
|
-
Object.defineProperty(W3CWebSocket, prop, {
|
|
168
|
-
get: function() { return NativeWebSocket[prop]; }
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Module exports.
|
|
175
|
-
*/
|
|
176
|
-
var browser = {
|
|
177
|
-
'w3cwebsocket' : NativeWebSocket ? W3CWebSocket : null,
|
|
178
|
-
'version' : websocket_version
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
var browser$1 = /*@__PURE__*/getDefaultExportFromCjs(browser);
|
|
182
|
-
|
|
183
|
-
var browser$2 = /*#__PURE__*/Object.freeze({
|
|
184
|
-
__proto__: null,
|
|
185
|
-
default: browser$1
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
export { browser$2 as b };
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
var browser$1 = {};
|
|
2
|
-
|
|
3
|
-
browser$1.MediaStream = window.MediaStream;
|
|
4
|
-
browser$1.MediaStreamTrack = window.MediaStreamTrack;
|
|
5
|
-
browser$1.RTCDataChannel = window.RTCDataChannel;
|
|
6
|
-
browser$1.RTCDataChannelEvent = window.RTCDataChannelEvent;
|
|
7
|
-
browser$1.RTCDtlsTransport = window.RTCDtlsTransport;
|
|
8
|
-
browser$1.RTCIceCandidate = window.RTCIceCandidate;
|
|
9
|
-
browser$1.RTCIceTransport = window.RTCIceTransport;
|
|
10
|
-
browser$1.RTCPeerConnection = window.RTCPeerConnection;
|
|
11
|
-
browser$1.RTCPeerConnectionIceEvent = window.RTCPeerConnectionIceEvent;
|
|
12
|
-
browser$1.RTCRtpReceiver = window.RTCRtpReceiver;
|
|
13
|
-
browser$1.RTCRtpSender = window.RTCRtpSender;
|
|
14
|
-
browser$1.RTCRtpTransceiver = window.RTCRtpTransceiver;
|
|
15
|
-
browser$1.RTCSctpTransport = window.RTCSctpTransport;
|
|
16
|
-
browser$1.RTCSessionDescription = window.RTCSessionDescription;
|
|
17
|
-
browser$1.getUserMedia = window.getUserMedia;
|
|
18
|
-
browser$1.mediaDevices = navigator.mediaDevices;
|
|
19
|
-
|
|
20
|
-
var browser = /*#__PURE__*/Object.freeze({
|
|
21
|
-
__proto__: null,
|
|
22
|
-
default: browser$1
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
export { browser as b };
|
|
@@ -1,306 +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
|
-
if (!globalThis.DEBUG) {
|
|
74
|
-
let DEBUG = [];
|
|
75
|
-
if (globalThis.localStorage) {
|
|
76
|
-
DEBUG = globalThis.localStorage.getItem('DEBUG');
|
|
77
|
-
globalThis.DEBUG = DEBUG ? DEBUG.split(',') : [DEBUG];
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const debug$1 = (target, text) => {
|
|
82
|
-
if (!globalThis.DEBUG && globalThis.DEBUG.length === 0) return;
|
|
83
|
-
if (
|
|
84
|
-
globalThis.DEBUG === 'true' ||
|
|
85
|
-
globalThis.DEBUG === true ||
|
|
86
|
-
globalThis.DEBUG?.indexOf(target) !== -1 ||
|
|
87
|
-
globalThis.DEBUG?.indexOf('*') !== -1 ||
|
|
88
|
-
globalThis.DEBUG?.indexOf(target.split('/')[0]) !== -1
|
|
89
|
-
)
|
|
90
|
-
if (text) console.log('\x1b[34m\x1b[1m%s', `${target}: ${text}`, '\x1b[0m');
|
|
91
|
-
else console.log('\x1b[34m\x1b[1m%s', `${target}`, '\x1b[0m');
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
if (!globalThis.debug) {
|
|
95
|
-
globalThis.debug = debug$1;
|
|
96
|
-
|
|
97
|
-
globalThis.createDebugger = (target) => (text) => debug$1(target, text);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const debug = globalThis.createDebugger('leofcoin/storage');
|
|
101
|
-
const opfsRoot = await navigator.storage.getDirectory();
|
|
102
|
-
class BrowerStore {
|
|
103
|
-
db;
|
|
104
|
-
name;
|
|
105
|
-
root;
|
|
106
|
-
inWorker;
|
|
107
|
-
version;
|
|
108
|
-
busy;
|
|
109
|
-
queue = [];
|
|
110
|
-
async init(name = 'storage', root = '.leofcoin', version = '1', inWorker = false) {
|
|
111
|
-
this.version = version;
|
|
112
|
-
this.name = name;
|
|
113
|
-
this.root = opfsRoot;
|
|
114
|
-
this.inWorker = inWorker;
|
|
115
|
-
let directoryHandle;
|
|
116
|
-
try {
|
|
117
|
-
directoryHandle = await opfsRoot.getDirectoryHandle(this.name, {
|
|
118
|
-
create: true
|
|
119
|
-
});
|
|
120
|
-
if (inWorker) {
|
|
121
|
-
// it's in a worker so that's why typings invalid?
|
|
122
|
-
// @ts-ignore
|
|
123
|
-
directoryHandle = await directoryHandle.createSyncAccessHandle();
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
catch (error) {
|
|
127
|
-
console.error(error);
|
|
128
|
-
}
|
|
129
|
-
this.db = directoryHandle;
|
|
130
|
-
}
|
|
131
|
-
toKeyPath(key) {
|
|
132
|
-
if (key instanceof KeyPath)
|
|
133
|
-
key = new KeyPath(key);
|
|
134
|
-
return key.toString();
|
|
135
|
-
}
|
|
136
|
-
toKeyValue(value) {
|
|
137
|
-
if (value instanceof KeyValue)
|
|
138
|
-
value = new KeyValue(value);
|
|
139
|
-
// @ts-ignore
|
|
140
|
-
return value.uint8Array;
|
|
141
|
-
}
|
|
142
|
-
async has(key) {
|
|
143
|
-
debug(`has ${this.toKeyPath(key)}`);
|
|
144
|
-
try {
|
|
145
|
-
await this.db.getFileHandle(this.toKeyPath(key));
|
|
146
|
-
return true;
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
return false;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
async get(key) {
|
|
153
|
-
let promiseResolve;
|
|
154
|
-
let promiseReject;
|
|
155
|
-
let result = new Promise((resolve, reject) => {
|
|
156
|
-
promiseResolve = resolve;
|
|
157
|
-
promiseReject = reject;
|
|
158
|
-
});
|
|
159
|
-
const promise = () => new Promise(async (resolve, reject) => {
|
|
160
|
-
debug(`get ${this.toKeyPath(key)}`);
|
|
161
|
-
setTimeout(async () => {
|
|
162
|
-
try {
|
|
163
|
-
let handle = await this.db.getFileHandle(this.toKeyPath(key));
|
|
164
|
-
let readBuffer;
|
|
165
|
-
if (this.inWorker) {
|
|
166
|
-
// it's in a worker so that's why typings invalid?
|
|
167
|
-
// @ts-ignore
|
|
168
|
-
handle = await handle.createSyncAccessHandle();
|
|
169
|
-
// @ts-ignore
|
|
170
|
-
const fileSize = handle.getSize();
|
|
171
|
-
// Read file content to a buffer.
|
|
172
|
-
const buffer = new DataView(new ArrayBuffer(fileSize));
|
|
173
|
-
// @ts-ignore
|
|
174
|
-
readBuffer = handle.read(buffer, { at: 0 });
|
|
175
|
-
// @ts-ignore
|
|
176
|
-
handle.close();
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
const file = await handle.getFile();
|
|
180
|
-
readBuffer = await file.arrayBuffer();
|
|
181
|
-
}
|
|
182
|
-
this.runQueue();
|
|
183
|
-
promiseResolve(new Uint8Array(readBuffer));
|
|
184
|
-
resolve(new Uint8Array(readBuffer));
|
|
185
|
-
}
|
|
186
|
-
catch (error) {
|
|
187
|
-
promiseReject(error);
|
|
188
|
-
resolve(false);
|
|
189
|
-
}
|
|
190
|
-
}, 1);
|
|
191
|
-
});
|
|
192
|
-
this.queue.push(promise);
|
|
193
|
-
this.runQueue();
|
|
194
|
-
return result;
|
|
195
|
-
}
|
|
196
|
-
async put(key, value) {
|
|
197
|
-
let promiseResolve;
|
|
198
|
-
let promiseReject;
|
|
199
|
-
let result = new Promise((resolve, reject) => {
|
|
200
|
-
promiseResolve = resolve;
|
|
201
|
-
promiseReject = reject;
|
|
202
|
-
});
|
|
203
|
-
const promise = () => new Promise(async (resolve, reject) => {
|
|
204
|
-
debug(`put ${this.toKeyPath(key)}`);
|
|
205
|
-
setTimeout(async () => {
|
|
206
|
-
try {
|
|
207
|
-
let handle = await this.db.getFileHandle(this.toKeyPath(key), { create: true });
|
|
208
|
-
let writeable;
|
|
209
|
-
if (this.inWorker) {
|
|
210
|
-
// it's in a worker so that's why typings invalid?
|
|
211
|
-
// @ts-ignore
|
|
212
|
-
writeable = await handle.createSyncAccessHandle();
|
|
213
|
-
}
|
|
214
|
-
else {
|
|
215
|
-
writeable = await handle.createWritable();
|
|
216
|
-
}
|
|
217
|
-
;
|
|
218
|
-
(await writeable).write(this.toKeyValue(value));
|
|
219
|
-
(await writeable).close();
|
|
220
|
-
this.runQueue();
|
|
221
|
-
resolve(true);
|
|
222
|
-
promiseResolve(true);
|
|
223
|
-
}
|
|
224
|
-
catch (error) {
|
|
225
|
-
promiseReject(error);
|
|
226
|
-
resolve(false);
|
|
227
|
-
}
|
|
228
|
-
}, 5);
|
|
229
|
-
});
|
|
230
|
-
this.queue.push(promise);
|
|
231
|
-
this.runQueue();
|
|
232
|
-
return result;
|
|
233
|
-
}
|
|
234
|
-
async runQueue() {
|
|
235
|
-
if (this.queue.length > 0 && !this.busy) {
|
|
236
|
-
this.busy = true;
|
|
237
|
-
const next = this.queue.shift();
|
|
238
|
-
await next();
|
|
239
|
-
this.busy = false;
|
|
240
|
-
return this.runQueue();
|
|
241
|
-
}
|
|
242
|
-
else if (this.queue.length === 0 && this.busy) {
|
|
243
|
-
this.busy = false;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
async delete(key) {
|
|
247
|
-
debug(`delete ${this.toKeyPath(key)}`);
|
|
248
|
-
return new Promise(async (resolve, reject) => {
|
|
249
|
-
try {
|
|
250
|
-
await this.db.getFileHandle(`${this.toKeyPath(key)}.crswap`);
|
|
251
|
-
setTimeout(() => resolve(this.delete(key)), 5);
|
|
252
|
-
}
|
|
253
|
-
catch (error) {
|
|
254
|
-
try {
|
|
255
|
-
await this.db.removeEntry(this.toKeyPath(key));
|
|
256
|
-
resolve(true);
|
|
257
|
-
}
|
|
258
|
-
catch (error) {
|
|
259
|
-
if (error.name === 'NoModificationAllowedError')
|
|
260
|
-
setTimeout(() => resolve(this.delete(key)), 5);
|
|
261
|
-
else
|
|
262
|
-
reject(error);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
async clear() {
|
|
268
|
-
for await (const key of this.db.keys()) {
|
|
269
|
-
debug(`clear ${this.toKeyPath(key)}`);
|
|
270
|
-
await this.delete(key);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
async values() {
|
|
274
|
-
let values = [];
|
|
275
|
-
for await (const cursor of this.db.values()) {
|
|
276
|
-
// huh? Outdated typings?
|
|
277
|
-
// @ts-ignore
|
|
278
|
-
values.push(cursor.getFile());
|
|
279
|
-
}
|
|
280
|
-
values = await Promise.all(values);
|
|
281
|
-
return Promise.all(values.map(async (file) => new Uint8Array(await file.arrayBuffer())));
|
|
282
|
-
}
|
|
283
|
-
async size() {
|
|
284
|
-
let size = 0;
|
|
285
|
-
for await (const cursor of this.db.values()) {
|
|
286
|
-
// huh? Outdated typings?
|
|
287
|
-
// @ts-ignore
|
|
288
|
-
console.log((await cursor.getFile()).size);
|
|
289
|
-
|
|
290
|
-
size += (await cursor.getFile()).size;
|
|
291
|
-
}
|
|
292
|
-
return size;
|
|
293
|
-
}
|
|
294
|
-
async keys() {
|
|
295
|
-
const keys = [];
|
|
296
|
-
for await (const cursor of this.db.keys()) {
|
|
297
|
-
keys.push(cursor);
|
|
298
|
-
}
|
|
299
|
-
return keys;
|
|
300
|
-
}
|
|
301
|
-
async iterate() {
|
|
302
|
-
return this.db.entries();
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
export { BrowerStore as default };
|