@leofcoin/peernet 1.1.79 → 1.1.81

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.
Files changed (85) hide show
  1. package/.esdoc.json +10 -10
  2. package/.gitattributes +2 -2
  3. package/.prettierrc +7 -7
  4. package/.travis.yml +27 -27
  5. package/BREAKING_CHANGES.md +34 -34
  6. package/LICENSE +21 -21
  7. package/README.md +72 -72
  8. package/deploy.js +8 -8
  9. package/exports/browser/{browser-WUe24rfW.js → browser-DQJ6xf_F.js} +3 -3
  10. package/exports/browser/browser-store.js +168 -22
  11. package/exports/browser/{client-I9x7CFr1.js → client-C0VVXIWm.js} +66 -32
  12. package/exports/browser/{peernet-DULgegxE.js → identity-CQ_ieRiz.js} +2232 -13425
  13. package/exports/browser/identity.d.ts +1 -1
  14. package/exports/browser/identity.js +1 -0
  15. package/exports/browser/{index-sw14JvKD.js → index-BeqbCwUk.js} +1 -2
  16. package/exports/browser/{index-In1Jzp-v.js → index-CEwkDK9g.js} +10 -489
  17. package/exports/browser/{messages-lzTD4EMU.js → messages-BdevLRCA.js} +167 -166
  18. package/exports/browser/peernet-DEIKLS2i.js +13220 -0
  19. package/exports/browser/peernet.d.ts +7 -7
  20. package/exports/browser/peernet.js +3 -2
  21. package/exports/identity.js +92 -0
  22. package/exports/{messages-T3M-Ff1E.js → messages-BmpgEM4y.js} +163 -163
  23. package/exports/peernet.js +189 -273
  24. package/exports/src/prompts/password.js +3 -3
  25. package/exports/store.js +9 -2
  26. package/exports/types/identity.d.ts +1 -1
  27. package/exports/types/peernet.d.ts +7 -7
  28. package/index.html +19 -19
  29. package/package.json +71 -62
  30. package/rollup.config.js +63 -63
  31. package/src/dht/dht.ts +147 -147
  32. package/src/discovery/peer-discovery.js +75 -75
  33. package/src/errors/errors.js +12 -12
  34. package/src/handlers/data.js +15 -15
  35. package/src/handlers/message.js +34 -34
  36. package/src/identity.ts +104 -104
  37. package/src/messages/chat.js +13 -13
  38. package/src/messages/data-response.js +13 -13
  39. package/src/messages/data.js +17 -17
  40. package/src/messages/dht-response.js +13 -13
  41. package/src/messages/dht.js +21 -21
  42. package/src/messages/file-link.js +17 -17
  43. package/src/messages/file.js +17 -17
  44. package/src/messages/peer-response.js +13 -13
  45. package/src/messages/peer.js +13 -13
  46. package/src/messages/peernet.js +13 -13
  47. package/src/messages/ps.js +13 -13
  48. package/src/messages/request.js +13 -13
  49. package/src/messages/response.js +13 -13
  50. package/src/messages.js +13 -13
  51. package/src/peer-info.js +9 -9
  52. package/src/peernet.ts +817 -817
  53. package/src/prompts/password/browser.js +1 -1
  54. package/src/prompts/password/node.js +6 -6
  55. package/src/proto/chat-message.proto.js +6 -6
  56. package/src/proto/data-response.proto.js +4 -4
  57. package/src/proto/data.proto.js +4 -4
  58. package/src/proto/dht-response.proto.js +4 -4
  59. package/src/proto/dht.proto.js +4 -4
  60. package/src/proto/file-link.proto.js +5 -5
  61. package/src/proto/file.proto.js +5 -5
  62. package/src/proto/peer-response.proto.js +3 -3
  63. package/src/proto/peer.proto.js +3 -3
  64. package/src/proto/peernet.proto.js +7 -7
  65. package/src/proto/ps.proto.js +4 -4
  66. package/src/proto/request.proto.js +4 -4
  67. package/src/proto/response.proto.js +3 -3
  68. package/src/types.ts +25 -25
  69. package/src/utils/utils.js +77 -77
  70. package/test/client.js +14 -14
  71. package/test/codec.js +56 -56
  72. package/test/hash.js +13 -13
  73. package/test/index.js +3 -3
  74. package/test/lastBlock.js +7 -7
  75. package/test/messages.js +26 -26
  76. package/test/peernet.js +17 -17
  77. package/test/peernet.test.js +159 -0
  78. package/test.js +62 -62
  79. package/test2.js +13 -13
  80. package/test3.js +15 -15
  81. package/test4.js +7 -7
  82. package/tsconfig.json +11 -13
  83. /package/exports/browser/{browser-AyxSBUXj.js → browser-pguCHlVu.js} +0 -0
  84. /package/exports/browser/{qr-scanner-worker.min-RaSiJc_R.js → qr-scanner-worker.min-Dy0qkKA4.js} +0 -0
  85. /package/exports/browser/{value-wzPYMxsX.js → value-C3vAp-wb.js} +0 -0
package/.esdoc.json CHANGED
@@ -1,10 +1,10 @@
1
- {
2
- "source": "./src",
3
- "destination": "./docs",
4
- "plugins": [{
5
- "name": "esdoc-standard-plugin"
6
- }, {
7
- "name": "esdoc-ecmascript-proposal-plugin",
8
- "option": {"all": true}
9
- }]
10
- }
1
+ {
2
+ "source": "./src",
3
+ "destination": "./docs",
4
+ "plugins": [{
5
+ "name": "esdoc-standard-plugin"
6
+ }, {
7
+ "name": "esdoc-ecmascript-proposal-plugin",
8
+ "option": {"all": true}
9
+ }]
10
+ }
package/.gitattributes CHANGED
@@ -1,2 +1,2 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
package/.prettierrc CHANGED
@@ -1,7 +1,7 @@
1
- {
2
- "tabWidth": 2,
3
- "semi": false,
4
- "singleQuote": true,
5
- "printWidth": 120,
6
- "trailingComma": "none"
7
- }
1
+ {
2
+ "tabWidth": 2,
3
+ "semi": false,
4
+ "singleQuote": true,
5
+ "printWidth": 120,
6
+ "trailingComma": "none"
7
+ }
package/.travis.yml CHANGED
@@ -1,27 +1,27 @@
1
- os:
2
- - linux
3
- - windows
4
- - osx
5
-
6
- language: node_js
7
- node_js:
8
- - 14
9
- - 16
10
- - 19
11
-
12
- script:
13
- - npm run c
14
- - npm run doc
15
- - npm run test
16
-
17
- after_success:
18
- - npm run coverage
19
- - npm run coveralls
20
- - node deploy.js success $WEBHOOK_URL
21
-
22
- after_failure:
23
- - node deploy.js failure $WEBHOOK_URL
24
-
25
- branches:
26
- only:
27
- - main
1
+ os:
2
+ - linux
3
+ - windows
4
+ - osx
5
+
6
+ language: node_js
7
+ node_js:
8
+ - 14
9
+ - 16
10
+ - 19
11
+
12
+ script:
13
+ - npm run c
14
+ - npm run doc
15
+ - npm run test
16
+
17
+ after_success:
18
+ - npm run coverage
19
+ - npm run coveralls
20
+ - node deploy.js success $WEBHOOK_URL
21
+
22
+ after_failure:
23
+ - node deploy.js failure $WEBHOOK_URL
24
+
25
+ branches:
26
+ only:
27
+ - main
@@ -1,35 +1,35 @@
1
- # breaking changes
2
- ## 1.0.10
3
- ### start
4
- #### before
5
- ```js
6
- const node = await new Node()
7
- ```
8
-
9
- #### now
10
- ```js
11
- const node = await new Node()
12
- // gives time to setup things...
13
- await node.start()
14
- ```
15
-
16
- ## 1.1.0
17
- ### autoStart
18
- #### before
19
- ```js
20
- const node = await new Node()
21
- // gives time to setup things...
22
- await node.start()
23
- ```
24
-
25
- #### now
26
- ```js
27
- const node = await new Node()
28
- ```
29
- or
30
-
31
- ```js
32
- const node = await new Node({autoStart: false}) // defaults to true
33
- // gives time to setup things...
34
- await node.start()
1
+ # breaking changes
2
+ ## 1.0.10
3
+ ### start
4
+ #### before
5
+ ```js
6
+ const node = await new Node()
7
+ ```
8
+
9
+ #### now
10
+ ```js
11
+ const node = await new Node()
12
+ // gives time to setup things...
13
+ await node.start()
14
+ ```
15
+
16
+ ## 1.1.0
17
+ ### autoStart
18
+ #### before
19
+ ```js
20
+ const node = await new Node()
21
+ // gives time to setup things...
22
+ await node.start()
23
+ ```
24
+
25
+ #### now
26
+ ```js
27
+ const node = await new Node()
28
+ ```
29
+ or
30
+
31
+ ```js
32
+ const node = await new Node({autoStart: false}) // defaults to true
33
+ // gives time to setup things...
34
+ await node.start()
35
35
  ```
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 vandeurenglenn
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2020 vandeurenglenn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,72 +1,72 @@
1
- # peernet
2
-
3
- ## Usage
4
- ```js
5
- import Peernet from '@leofcoin/peernet'
6
-
7
-
8
- const config = {
9
- network: 'leofcoin:peach',
10
- networkVersion: 'peach',
11
- stars: ['wss://peach.leofcoin.org']
12
- }
13
-
14
- const node = await new Peernet(config)
15
- // ... setup some things
16
- await node.start()
17
-
18
- console.log(globalThis.peernet)
19
- ```
20
-
21
-
22
-
23
- ## API
24
- #### addRequestHandler
25
-
26
- examples
27
- ```js
28
- peernet.addRequestHandler('lastBlock', () => {
29
- let response;
30
- const height = await chainStore.get('localIndex')
31
- const hash = await chainStore.get('localBlock')
32
- response = JSON.stringify({ height: height.toString(), hash: hash.toString() })
33
- return new ResponseMessage({ response })
34
- // or
35
- return new peernet.protos['peernet-response']({ response })
36
- })
37
- ```
38
-
39
- ```js
40
- peernet.addRequestHandler('hello', () => {
41
- return new ResponseMessage({ response: 'hi' })
42
- // or
43
- return new peernet.protos['peernet-response']({ response: 'hi' })
44
- })
45
- ```
46
- #### add version to peer
47
- ```js
48
- peernet.addRequestHandler('version', () => {
49
- return new ResponseMessage({ response: {version: 1} })
50
- })
51
- ```
52
-
53
- ## Development
54
-
55
- `note: you need to install jsproject`
56
- ```sh
57
- npm i -g @vandeurenglenn/project
58
- ```
59
-
60
- ### watch
61
- ```sh
62
- npm run w
63
- ```
64
- ### compile
65
- ```sh
66
- npm run c
67
- ```
68
- ### serve html
69
- ```sh
70
- npm run demo
71
- ```
72
-
1
+ # peernet
2
+
3
+ ## Usage
4
+ ```js
5
+ import Peernet from '@leofcoin/peernet'
6
+
7
+
8
+ const config = {
9
+ network: 'leofcoin:peach',
10
+ networkVersion: 'peach',
11
+ stars: ['wss://peach.leofcoin.org']
12
+ }
13
+
14
+ const node = await new Peernet(config)
15
+ // ... setup some things
16
+ await node.start()
17
+
18
+ console.log(globalThis.peernet)
19
+ ```
20
+
21
+
22
+
23
+ ## API
24
+ #### addRequestHandler
25
+
26
+ examples
27
+ ```js
28
+ peernet.addRequestHandler('lastBlock', () => {
29
+ let response;
30
+ const height = await chainStore.get('localIndex')
31
+ const hash = await chainStore.get('localBlock')
32
+ response = JSON.stringify({ height: height.toString(), hash: hash.toString() })
33
+ return new ResponseMessage({ response })
34
+ // or
35
+ return new peernet.protos['peernet-response']({ response })
36
+ })
37
+ ```
38
+
39
+ ```js
40
+ peernet.addRequestHandler('hello', () => {
41
+ return new ResponseMessage({ response: 'hi' })
42
+ // or
43
+ return new peernet.protos['peernet-response']({ response: 'hi' })
44
+ })
45
+ ```
46
+ #### add version to peer
47
+ ```js
48
+ peernet.addRequestHandler('version', () => {
49
+ return new ResponseMessage({ response: {version: 1} })
50
+ })
51
+ ```
52
+
53
+ ## Development
54
+
55
+ `note: you need to install jsproject`
56
+ ```sh
57
+ npm i -g @vandeurenglenn/project
58
+ ```
59
+
60
+ ### watch
61
+ ```sh
62
+ npm run w
63
+ ```
64
+ ### compile
65
+ ```sh
66
+ npm run c
67
+ ```
68
+ ### serve html
69
+ ```sh
70
+ npm run demo
71
+ ```
72
+
package/deploy.js CHANGED
@@ -1,8 +1,8 @@
1
- const {execSync} = require('child_process');
2
- const status = process.argv[2]
3
-
4
- if (process.version.split('.')[0] === 'v19' && process.platform === 'linux') {
5
- execSync('wget https://raw.githubusercontent.com/DiscordHooks/travis-ci-discord-webhook/master/send.sh')
6
- execSync('chmod +x send.sh')
7
- execSync(`./send.sh ${status} ` + process.argv[3])
8
- }
1
+ const {execSync} = require('child_process');
2
+ const status = process.argv[2]
3
+
4
+ if (process.version.split('.')[0] === 'v19' && process.platform === 'linux') {
5
+ execSync('wget https://raw.githubusercontent.com/DiscordHooks/travis-ci-discord-webhook/master/send.sh')
6
+ execSync('chmod +x send.sh')
7
+ execSync(`./send.sh ${status} ` + process.argv[3])
8
+ }
@@ -1,4 +1,4 @@
1
- import { g as getDefaultExportFromCjs } from './peernet-DULgegxE.js';
1
+ import { g as getDefaultExportFromCjs } from './identity-CQ_ieRiz.js';
2
2
 
3
3
  var global;
4
4
  var hasRequiredGlobal;
@@ -62,7 +62,7 @@ var author = "Brian McKelvey <theturtle32@gmail.com> (https://github.com/theturt
62
62
  var contributors = [
63
63
  "Iñaki Baz Castillo <ibc@aliax.net> (http://dev.sipdoc.net)"
64
64
  ];
65
- var version$1 = "1.0.34";
65
+ var version$1 = "1.0.35";
66
66
  var repository = {
67
67
  type: "git",
68
68
  url: "https://github.com/theturtle32/WebSocket-Node.git"
@@ -74,7 +74,7 @@ var engines = {
74
74
  var dependencies = {
75
75
  bufferutil: "^4.0.1",
76
76
  debug: "^2.2.0",
77
- "es5-ext": "^0.10.50",
77
+ "es5-ext": "^0.10.63",
78
78
  "typedarray-to-buffer": "^3.1.5",
79
79
  "utf-8-validate": "^5.0.2",
80
80
  yaeti: "^0.0.6"
@@ -1,22 +1,57 @@
1
- import { K as KeyPath, a as KeyValue } from './value-wzPYMxsX.js';
1
+ import { K as KeyPath, a as KeyValue } from './value-C3vAp-wb.js';
2
2
 
3
+ if (!globalThis.DEBUG) {
4
+ let DEBUG = [];
5
+ if (globalThis.localStorage) {
6
+ DEBUG = globalThis.localStorage.getItem('DEBUG');
7
+ globalThis.DEBUG = DEBUG ? DEBUG.split(',') : [DEBUG];
8
+ }
9
+ }
10
+
11
+ const debug$1 = (target, text) => {
12
+ if (!globalThis.DEBUG && globalThis.DEBUG.length === 0) return;
13
+ if (
14
+ globalThis.DEBUG === 'true' ||
15
+ globalThis.DEBUG === true ||
16
+ globalThis.DEBUG?.indexOf(target) !== -1 ||
17
+ globalThis.DEBUG?.indexOf('*') !== -1 ||
18
+ globalThis.DEBUG?.indexOf(target.split('/')[0]) !== -1
19
+ )
20
+ if (text) console.log('\x1b[34m\x1b[1m%s', `${target}: ${text}`, '\x1b[0m');
21
+ else console.log('\x1b[34m\x1b[1m%s', `${target}`, '\x1b[0m');
22
+ };
23
+
24
+ if (!globalThis.debug) {
25
+ globalThis.debug = debug$1;
26
+
27
+ globalThis.createDebugger = (target) => (text) => debug$1(target, text);
28
+ }
29
+
30
+ const debug = globalThis.createDebugger('leofcoin/storage');
3
31
  const opfsRoot = await navigator.storage.getDirectory();
4
32
  class BrowerStore {
5
33
  db;
6
34
  name;
7
35
  root;
36
+ inWorker;
8
37
  version;
9
- async init(name = 'storage', root = '.leofcoin', version = '1') {
10
- console.log('init');
38
+ busy;
39
+ queue = [];
40
+ async init(name = 'storage', root = '.leofcoin', version = '1', inWorker = false) {
11
41
  this.version = version;
12
42
  this.name = name;
13
43
  this.root = opfsRoot;
14
- console.log(`${this.root}/${this.name}`);
44
+ this.inWorker = inWorker;
15
45
  let directoryHandle;
16
46
  try {
17
47
  directoryHandle = await opfsRoot.getDirectoryHandle(this.name, {
18
48
  create: true
19
49
  });
50
+ if (inWorker) {
51
+ // it's in a worker so that's why typings invalid?
52
+ // @ts-ignore
53
+ directoryHandle = await directoryHandle.createSyncAccessHandle();
54
+ }
20
55
  }
21
56
  catch (error) {
22
57
  console.error(error);
@@ -35,6 +70,7 @@ class BrowerStore {
35
70
  return value.uint8Array;
36
71
  }
37
72
  async has(key) {
73
+ debug(`has ${this.toKeyPath(key)}`);
38
74
  try {
39
75
  await this.db.getFileHandle(this.toKeyPath(key));
40
76
  return true;
@@ -44,39 +80,149 @@ class BrowerStore {
44
80
  }
45
81
  }
46
82
  async get(key) {
47
- const handle = await this.db.getFileHandle(this.toKeyPath(key));
48
- const file = await handle.getFile();
49
- return new Uint8Array(await file.arrayBuffer());
83
+ let promiseResolve;
84
+ let promiseReject;
85
+ let result = new Promise((resolve, reject) => {
86
+ promiseResolve = resolve;
87
+ promiseReject = reject;
88
+ });
89
+ const promise = () => new Promise(async (resolve, reject) => {
90
+ debug(`get ${this.toKeyPath(key)}`);
91
+ setTimeout(async () => {
92
+ try {
93
+ let handle = await this.db.getFileHandle(this.toKeyPath(key));
94
+ let readBuffer;
95
+ if (this.inWorker) {
96
+ // it's in a worker so that's why typings invalid?
97
+ // @ts-ignore
98
+ handle = await handle.createSyncAccessHandle();
99
+ // @ts-ignore
100
+ const fileSize = handle.getSize();
101
+ // Read file content to a buffer.
102
+ const buffer = new DataView(new ArrayBuffer(fileSize));
103
+ // @ts-ignore
104
+ readBuffer = handle.read(buffer, { at: 0 });
105
+ // @ts-ignore
106
+ handle.close();
107
+ }
108
+ else {
109
+ const file = await handle.getFile();
110
+ readBuffer = await file.arrayBuffer();
111
+ }
112
+ this.runQueue();
113
+ promiseResolve(new Uint8Array(readBuffer));
114
+ resolve(new Uint8Array(readBuffer));
115
+ }
116
+ catch (error) {
117
+ promiseReject(error);
118
+ resolve(false);
119
+ }
120
+ }, 1);
121
+ });
122
+ this.queue.push(promise);
123
+ this.runQueue();
124
+ return result;
50
125
  }
51
126
  async put(key, value) {
52
- const handle = await this.db.getFileHandle(this.toKeyPath(key), { create: true });
53
- const writeable = handle.createWritable();
54
- (await writeable).write(this.toKeyValue(value));
55
- (await writeable).close();
127
+ let promiseResolve;
128
+ let promiseReject;
129
+ let result = new Promise((resolve, reject) => {
130
+ promiseResolve = resolve;
131
+ promiseReject = reject;
132
+ });
133
+ const promise = () => new Promise(async (resolve, reject) => {
134
+ debug(`put ${this.toKeyPath(key)}`);
135
+ setTimeout(async () => {
136
+ try {
137
+ let handle = await this.db.getFileHandle(this.toKeyPath(key), { create: true });
138
+ let writeable;
139
+ if (this.inWorker) {
140
+ // it's in a worker so that's why typings invalid?
141
+ // @ts-ignore
142
+ writeable = await handle.createSyncAccessHandle();
143
+ }
144
+ else {
145
+ writeable = await handle.createWritable();
146
+ }
147
+ ;
148
+ (await writeable).write(this.toKeyValue(value));
149
+ (await writeable).close();
150
+ this.runQueue();
151
+ resolve(true);
152
+ promiseResolve(true);
153
+ }
154
+ catch (error) {
155
+ promiseReject(error);
156
+ resolve(false);
157
+ }
158
+ }, 5);
159
+ });
160
+ this.queue.push(promise);
161
+ this.runQueue();
162
+ return result;
163
+ }
164
+ async runQueue() {
165
+ if (this.queue.length > 0 && !this.busy) {
166
+ this.busy = true;
167
+ const next = this.queue.shift();
168
+ await next();
169
+ this.busy = false;
170
+ return this.runQueue();
171
+ }
172
+ else if (this.queue.length === 0 && this.busy) {
173
+ this.busy = false;
174
+ }
56
175
  }
57
176
  async delete(key) {
58
- return this.db.removeEntry(this.toKeyPath(key));
177
+ debug(`delete ${this.toKeyPath(key)}`);
178
+ return new Promise(async (resolve, reject) => {
179
+ try {
180
+ await this.db.getFileHandle(`${this.toKeyPath(key)}.crswap`);
181
+ setTimeout(() => resolve(this.delete(key)), 5);
182
+ }
183
+ catch (error) {
184
+ try {
185
+ await this.db.removeEntry(this.toKeyPath(key));
186
+ resolve(true);
187
+ }
188
+ catch (error) {
189
+ if (error.name === 'NoModificationAllowedError')
190
+ setTimeout(() => resolve(this.delete(key)), 5);
191
+ else
192
+ reject(error);
193
+ }
194
+ }
195
+ });
59
196
  }
60
197
  async clear() {
61
198
  for await (const key of this.db.keys()) {
62
- await this.db.removeEntry(key);
199
+ debug(`clear ${this.toKeyPath(key)}`);
200
+ await this.delete(key);
201
+ }
202
+ }
203
+ async values() {
204
+ let values = [];
205
+ for await (const cursor of this.db.values()) {
206
+ // huh? Outdated typings?
207
+ // @ts-ignore
208
+ values.push(cursor.getFile());
63
209
  }
210
+ values = await Promise.all(values);
211
+ return Promise.all(values.map(async (file) => new Uint8Array(await file.arrayBuffer())));
64
212
  }
65
- async values(limit = -1) {
66
- const values = [];
213
+ async size() {
214
+ let size = 0;
67
215
  for await (const cursor of this.db.values()) {
68
- values.push(cursor);
69
- if (limit && values.length === limit)
70
- return values;
216
+ // huh? Outdated typings?
217
+ // @ts-ignore
218
+ size += (await cursor.getFile()).size;
71
219
  }
72
- return values;
220
+ return size;
73
221
  }
74
- async keys(limit = -1) {
222
+ async keys() {
75
223
  const keys = [];
76
224
  for await (const cursor of this.db.keys()) {
77
225
  keys.push(cursor);
78
- if (limit && keys.length === limit)
79
- return keys;
80
226
  }
81
227
  return keys;
82
228
  }