@webpodjs/net-worker 0.3.1 → 0.3.2
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/abi/ring.js +1 -0
- package/dist/worker.js +40 -23
- package/package.json +1 -1
package/dist/abi/ring.js
CHANGED
|
@@ -365,6 +365,7 @@ async function waitWord(words, index, value, options) {
|
|
|
365
365
|
}, ms);
|
|
366
366
|
signal?.addEventListener("abort", end, { once: true });
|
|
367
367
|
waiter?.sleepers.add(end);
|
|
368
|
+
options.onSleep?.(end);
|
|
368
369
|
});
|
|
369
370
|
if (timedOut && waiter && Atomics.load(words, index) !== value) {
|
|
370
371
|
const byIndex = waiters.get(words);
|
package/dist/worker.js
CHANGED
|
@@ -305,33 +305,51 @@ async function loadCa(name) {
|
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
307
|
|
|
308
|
+
/** `h`'s contents replaced by `bundle` at the stream's close. */
|
|
309
|
+
async function writeStream(h, bundle) {
|
|
310
|
+
const w = await h.createWritable();
|
|
311
|
+
try {
|
|
312
|
+
await w.write(bundle);
|
|
313
|
+
await w.close();
|
|
314
|
+
} catch (e) {
|
|
315
|
+
await w.abort().catch(() => {});
|
|
316
|
+
throw e;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/** The same through a sync access handle (Workers have them). */
|
|
320
|
+
async function writeHandle(h, bundle) {
|
|
321
|
+
const s = await h.createSyncAccessHandle();
|
|
322
|
+
try {
|
|
323
|
+
s.truncate(0);
|
|
324
|
+
s.write(new TextEncoder().encode(bundle), { at: 0 });
|
|
325
|
+
s.flush();
|
|
326
|
+
} finally {
|
|
327
|
+
s.close();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* WebKit's createWritable leaves a copy of the file in the browser's temporary directory, which it never removes (T385):
|
|
333
|
+
* saveCa writes through a sync access handle there first (older Safari has no createWritable), elsewhere with
|
|
334
|
+
* createWritable first; with the other if that fails.
|
|
335
|
+
*/
|
|
336
|
+
const ua = self.navigator?.userAgent ?? "";
|
|
337
|
+
const webkit = /AppleWebKit/.test(ua) && !/Chrome\/|Chromium\//.test(ua);
|
|
338
|
+
|
|
308
339
|
async function saveCa(name, bundle) {
|
|
309
340
|
if (!name) return;
|
|
310
341
|
let error = null;
|
|
311
342
|
try {
|
|
312
343
|
const dir = await (await navigator.storage.getDirectory()).getDirectoryHandle("webpod-net", { create: true });
|
|
313
344
|
const h = await dir.getFileHandle(`${name}.ca.pem`, { create: true });
|
|
314
|
-
|
|
315
|
-
const w = await h.createWritable();
|
|
345
|
+
for (const write of webkit ? [writeHandle, writeStream] : [writeStream, writeHandle]) {
|
|
316
346
|
try {
|
|
317
|
-
await
|
|
318
|
-
|
|
347
|
+
await write(h, bundle);
|
|
348
|
+
error = null;
|
|
349
|
+
break;
|
|
319
350
|
} catch (e) {
|
|
320
|
-
|
|
321
|
-
throw e;
|
|
322
|
-
}
|
|
323
|
-
} catch (e) {
|
|
324
|
-
// Older Safari has no createWritable; sync access handles work in Workers.
|
|
325
|
-
error = e;
|
|
326
|
-
const s = await h.createSyncAccessHandle();
|
|
327
|
-
try {
|
|
328
|
-
s.truncate(0);
|
|
329
|
-
s.write(new TextEncoder().encode(bundle), { at: 0 });
|
|
330
|
-
s.flush();
|
|
331
|
-
} finally {
|
|
332
|
-
s.close();
|
|
351
|
+
error = e;
|
|
333
352
|
}
|
|
334
|
-
error = null;
|
|
335
353
|
}
|
|
336
354
|
} catch (e) {
|
|
337
355
|
error = e;
|
|
@@ -381,12 +399,11 @@ async function pump(memory, fromEngine, toEngine) {
|
|
|
381
399
|
continue;
|
|
382
400
|
}
|
|
383
401
|
rounds = 0;
|
|
402
|
+
// Until a frame, wake() (the sleep's end: no AbortController and abort event for each sleep, which took 10% of
|
|
403
|
+
// this Worker's busy time at 150 MB/s of host downloads, T383), the watchdog or the stack's next timer (the
|
|
404
|
+
// sleep's bound: one timer, the wait's own).
|
|
384
405
|
const d = pod.delay(performance.now());
|
|
385
|
-
|
|
386
|
-
wake = () => ctl.abort();
|
|
387
|
-
const timer = d >= 0 ? setTimeout(() => ctl.abort(), Math.max(0, Math.min(d, 1000))) : null;
|
|
388
|
-
await waitReadable(rx, { signal: ctl.signal, watchdogMs: idleWatchdogMs });
|
|
389
|
-
if (timer) clearTimeout(timer);
|
|
406
|
+
await waitReadable(rx, { watchdogMs: d >= 0 ? Math.max(0, Math.min(d, idleWatchdogMs)) : idleWatchdogMs, onSleep: (end) => (wake = end) });
|
|
390
407
|
wake = null;
|
|
391
408
|
}
|
|
392
409
|
} catch (e) {
|