@leofcoin/peernet 1.1.78 → 1.1.80

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.
@@ -1,4 +1,4 @@
1
- import { g as getDefaultExportFromCjs } from './peernet-x3p3hKA5.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
  }
@@ -1,5 +1,6 @@
1
- import { L as LittlePubSub } from './peernet-x3p3hKA5.js';
2
- import './value-wzPYMxsX.js';
1
+ import { L as LittlePubSub } from './peernet-B7TZP-Wg.js';
2
+ import './identity-CQ_ieRiz.js';
3
+ import './value-C3vAp-wb.js';
3
4
 
4
5
  class Api {
5
6
  _pubsub;
@@ -188,14 +189,17 @@ class SocketRequestClient {
188
189
  #options;
189
190
  #protocol;
190
191
  #url;
192
+ #experimentalWebsocket = false;
191
193
  constructor(url, protocol, options) {
192
- let { retry, timeout, times } = options || {};
194
+ let { retry, timeout, times, experimentalWebsocket } = options || {};
193
195
  if (retry !== undefined)
194
196
  this.#retry = retry;
195
197
  if (timeout !== undefined)
196
198
  this.#timeout = timeout;
197
199
  if (times !== undefined)
198
200
  this.#times = times;
201
+ if (experimentalWebsocket !== undefined)
202
+ this.#experimentalWebsocket;
199
203
  this.#url = url;
200
204
  this.#protocol = protocol;
201
205
  this.#options = options;
@@ -205,36 +209,62 @@ class SocketRequestClient {
205
209
  return new Promise(async (resolve, reject) => {
206
210
  const init = async () => {
207
211
  // @ts-ignore
208
- if (!globalThis.WebSocket)
209
- globalThis.WebSocket = (await import('./browser-P2HFBJsB.js').then(function (n) { return n.b; })).default.w3cwebsocket;
212
+ if (!globalThis.WebSocket && !this.#experimentalWebsocket)
213
+ globalThis.WebSocket = (await import('./browser-DQJ6xf_F.js').then(function (n) { return n.b; })).default.w3cwebsocket;
210
214
  const client = new WebSocket(this.#url, this.#protocol);
211
- client.onmessage = this.onmessage;
212
- client.onerror = this.onerror;
213
- client.onopen = () => {
214
- this.#tries = 0;
215
- resolve(new ClientConnection(client, this.api));
216
- };
217
- client.onclose = message => {
218
- this.#tries++;
219
- if (!this.#retry)
220
- return reject(this.#options);
221
- if (this.#tries > this.#times) {
222
- console.log(`${this.#options.protocol} Client Closed`);
223
- console.error(`could not connect to - ${this.#url}/`);
224
- return resolve(new ClientConnection(client, this.api));
225
- }
226
- if (message.code === 1006) {
227
- console.log(`Retrying in ${this.#timeout} ms`);
228
- setTimeout(() => {
229
- return init();
230
- }, this.#timeout);
231
- }
232
- };
215
+ if (this.#experimentalWebsocket) {
216
+ client.addEventListener('error', this.onerror);
217
+ client.addEventListener('message', this.onmessage);
218
+ client.addEventListener('open', () => {
219
+ this.#tries = 0;
220
+ resolve(new ClientConnection(client, this.api));
221
+ });
222
+ client.addEventListener('close', (client.onclose = (message) => {
223
+ this.#tries++;
224
+ if (!this.#retry)
225
+ return reject(this.#options);
226
+ if (this.#tries > this.#times) {
227
+ console.log(`${this.#options.protocol} Client Closed`);
228
+ console.error(`could not connect to - ${this.#url}/`);
229
+ return resolve(new ClientConnection(client, this.api));
230
+ }
231
+ if (message.code === 1006) {
232
+ console.log(`Retrying in ${this.#timeout} ms`);
233
+ setTimeout(() => {
234
+ return init();
235
+ }, this.#timeout);
236
+ }
237
+ }));
238
+ }
239
+ else {
240
+ client.onmessage = this.onmessage;
241
+ client.onerror = this.onerror;
242
+ client.onopen = () => {
243
+ this.#tries = 0;
244
+ resolve(new ClientConnection(client, this.api));
245
+ };
246
+ client.onclose = (message) => {
247
+ this.#tries++;
248
+ if (!this.#retry)
249
+ return reject(this.#options);
250
+ if (this.#tries > this.#times) {
251
+ console.log(`${this.#options.protocol} Client Closed`);
252
+ console.error(`could not connect to - ${this.#url}/`);
253
+ return resolve(new ClientConnection(client, this.api));
254
+ }
255
+ if (message.code === 1006) {
256
+ console.log(`Retrying in ${this.#timeout} ms`);
257
+ setTimeout(() => {
258
+ return init();
259
+ }, this.#timeout);
260
+ }
261
+ };
262
+ }
233
263
  };
234
264
  return init();
235
265
  });
236
266
  }
237
- onerror = error => {
267
+ onerror = (error) => {
238
268
  if (globalThis.pubsub.subscribers['error']) {
239
269
  globalThis.pubsub.publish('error', error);
240
270
  }
@@ -243,6 +273,10 @@ class SocketRequestClient {
243
273
  }
244
274
  };
245
275
  onmessage(message) {
276
+ if (!message.data) {
277
+ console.warn(`message ignored because it contained no data`);
278
+ return;
279
+ }
246
280
  const { value, url, status, id } = JSON.parse(message.data.toString());
247
281
  const publisher = id ? id : url;
248
282
  if (status === 200) {
@@ -280,7 +314,7 @@ const iceServers = [
280
314
  credential: 'openrelayproject'
281
315
  }
282
316
  ];
283
- const SimplePeer = (await import('./index-00QxgDks.js').then(function (n) { return n.i; })).default;
317
+ const SimplePeer = (await import('./index-DqPlTtAJ.js').then(function (n) { return n.i; })).default;
284
318
  class Peer extends SimplePeer {
285
319
  peerId;
286
320
  channelName;
@@ -347,7 +381,7 @@ class Peer extends SimplePeer {
347
381
  */
348
382
  request(data, id = crypto.randomUUID()) {
349
383
  return new Promise((resolve, reject) => {
350
- const timeout = setTimeout(() => reject(`request for ${id} timed out`), 30000);
384
+ const timeout = setTimeout(() => reject(`request for ${id} timed out`), 30_000);
351
385
  const onrequest = ({ data }) => {
352
386
  clearTimeout(timeout);
353
387
  resolve(data);
@@ -359,7 +393,7 @@ class Peer extends SimplePeer {
359
393
  }
360
394
  }
361
395
 
362
- const debug = globalThis.createDebugger('@peernet/swarm/client');
396
+ const debug = globalThis.createDebugger('@netpeer/swarm/client');
363
397
  class Client {
364
398
  #peerId;
365
399
  #connections = {};
@@ -408,7 +442,7 @@ class Client {
408
442
  async _init() {
409
443
  // reconnectJob()
410
444
  if (!globalThis.RTCPeerConnection)
411
- globalThis.wrtc = (await import('./browser-AyxSBUXj.js').then(function (n) { return n.b; })).default;
445
+ globalThis.wrtc = (await import('./browser-pguCHlVu.js').then(function (n) { return n.b; })).default;
412
446
  for (const star of this.starsConfig) {
413
447
  try {
414
448
  const client = new SocketRequestClient(star, this.networkVersion);