@bendyline/molen-pack 0.0.4 → 0.0.5

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/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
- import { _ as describePack, a as PackIntegrityError, b as sha256, c as openPack, d as PackRetryOptions, f as RangeReader, g as createPack, h as PackOptions, i as PackInput, l as packFromFiles, m as PackFile, n as Pack, o as PackProgress, p as BuiltPack, r as PackEntryNotFoundError, s as ReadOptions, t as OpenPackOptions, u as PackChangedError, v as isTextPath, y as mediaTypeOf } from "./pack-DjufOvAS.mjs";
1
+ import { n as PackRetryOptions, r as RangeReader, t as PackChangedError } from "./source-7Pr2tntF.mjs";
2
+ import { _ as sha256, a as PackIntegrityError, c as openPack, d as PackFile, f as PackOptions, g as mediaTypeOf, h as isTextPath, i as PackInput, l as packFromFiles, m as describePack, n as Pack, o as PackProgress, p as createPack, r as PackEntryNotFoundError, s as ReadOptions, t as OpenPackOptions, u as BuiltPack } from "./pack-CF6pPp9f.mjs";
2
3
  import { PackIndex, PackIndexEntry, PackManifest } from "@bendyline/molen-schema";
3
4
 
4
5
  //#region src/set.d.ts
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
- import { a as PackChangedError, c as isTextPath, i as packFromFiles, l as mediaTypeOf, n as PackIntegrityError, o as createPack, r as openPack, s as describePack, t as PackEntryNotFoundError, u as sha256 } from "./pack-DCQvXj1p.mjs";
1
+ import { a as createPack, c as mediaTypeOf, i as packFromFiles, l as sha256, n as PackIntegrityError, o as describePack, r as openPack, s as isTextPath, t as PackEntryNotFoundError } from "./pack-BIXihu9D.mjs";
2
+ import { t as PackChangedError } from "./source-MDAzioCi.mjs";
2
3
  //#region src/set.ts
3
4
  var Packs = class {
4
5
  list = [];
package/dist/node.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { h as PackOptions, m as PackFile, n as Pack, t as OpenPackOptions } from "./pack-DjufOvAS.mjs";
1
+ import { d as PackFile, f as PackOptions, n as Pack, t as OpenPackOptions } from "./pack-CF6pPp9f.mjs";
2
2
  import { PackIndexEntry, PackManifest, PackSourceConfig } from "@bendyline/molen-schema";
3
3
 
4
4
  //#region src/node.d.ts
package/dist/node.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { i as packFromFiles, o as createPack, r as openPack, u as sha256 } from "./pack-DCQvXj1p.mjs";
1
+ import { a as createPack, i as packFromFiles, l as sha256, r as openPack } from "./pack-BIXihu9D.mjs";
2
2
  import { validate } from "@bendyline/molen-schema";
3
3
  import { mkdir, open, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
4
4
  import { dirname, join, relative, resolve, sep } from "node:path";
@@ -1,3 +1,4 @@
1
+ import { a as openUrl, i as blobReader, n as ReadScheduler, r as TAIL_BYTES } from "./source-MDAzioCi.mjs";
1
2
  import { PACK_MANIFEST_ENTRY, PACK_RESERVED_PREFIX, validate } from "@bendyline/molen-schema";
2
3
  import { deflateSync, inflateSync } from "fflate";
3
4
  //#region src/zip.ts
@@ -418,273 +419,6 @@ async function createPack(files, options) {
418
419
  };
419
420
  }
420
421
  //#endregion
421
- //#region src/source.ts
422
- /** The server replaced the pack while it was being read (its ETag changed). */
423
- var PackChangedError = class extends Error {
424
- constructor(url) {
425
- super(`pack ${url} changed on the server while it was being read; open it again`);
426
- this.name = "PackChangedError";
427
- }
428
- };
429
- var HttpStatusError = class extends Error {
430
- url;
431
- status;
432
- constructor(url, status) {
433
- super(`HTTP ${status} for ${url}`);
434
- this.url = url;
435
- this.status = status;
436
- this.name = "HttpStatusError";
437
- }
438
- };
439
- function blobReader(blob) {
440
- return {
441
- size: blob.size,
442
- read: async (offset, length) => new Uint8Array(await blob.slice(offset, offset + length).arrayBuffer())
443
- };
444
- }
445
- function isRetryable(error) {
446
- if (error instanceof HttpStatusError) return error.status === 408 || error.status === 429 || error.status >= 500;
447
- if (error instanceof PackChangedError) return false;
448
- const name = error?.name;
449
- return error instanceof TypeError || name === "TimeoutError";
450
- }
451
- function sleep(ms, signal) {
452
- return new Promise((resolve, reject) => {
453
- if (signal.aborted) return reject(signal.reason);
454
- const timer = setTimeout(() => {
455
- signal.removeEventListener("abort", abort);
456
- resolve();
457
- }, ms);
458
- const abort = () => {
459
- clearTimeout(timer);
460
- reject(signal.reason);
461
- };
462
- signal.addEventListener("abort", abort, { once: true });
463
- });
464
- }
465
- /** Run `task` with a per-attempt deadline, retrying transient failures with backoff. */
466
- async function withRetry(task, retry, signal) {
467
- const attempts = Math.max(1, retry.attempts ?? 3);
468
- const base = retry.baseDelayMs ?? 250;
469
- for (let attempt = 1;; attempt++) {
470
- const deadline = AbortSignal.timeout(retry.timeoutMs ?? 3e4);
471
- try {
472
- return await task(AbortSignal.any([signal, deadline]));
473
- } catch (error) {
474
- if (signal.aborted) throw signal.reason;
475
- if (attempt >= attempts || !isRetryable(error)) throw error;
476
- await sleep(Math.min(base * 2 ** (attempt - 1), retry.maxDelayMs ?? 4e3), signal);
477
- }
478
- }
479
- }
480
- /** Bytes fetched when a remote pack is opened: the manifest and central directory live here. */
481
- const TAIL_BYTES = 64 * 1024;
482
- async function fetchBytes(response) {
483
- return new Uint8Array(await response.arrayBuffer());
484
- }
485
- /** Missing Content-Range is allowed for CORS hosts which do not expose this header. */
486
- function contentRange(value, url) {
487
- if (value === void 0) return void 0;
488
- const match = /^bytes (\d+)-(\d+)\/(\d+|\*)$/i.exec(value.trim());
489
- const start = Number(match?.[1]);
490
- const end = Number(match?.[2]);
491
- const size = match?.[3] === "*" ? void 0 : Number(match?.[3]);
492
- if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start < 0 || end < start || size !== void 0 && (!Number.isSafeInteger(size) || size <= end)) throw new Error(`invalid Content-Range for ${url}: ${value}`);
493
- return {
494
- start,
495
- end,
496
- size
497
- };
498
- }
499
- function strongEtag(value) {
500
- return value !== void 0 && !value.startsWith("W/") ? value : void 0;
501
- }
502
- /**
503
- * Open a pack URL. A server that honours Range gets range reads; one that ignores it (200 to a
504
- * range request) has already sent the whole file, which is used as is.
505
- */
506
- async function openUrl(url, options) {
507
- const { fetch: fetcher, retry, signal } = options;
508
- if (options.mode === "whole" || options.mode === "auto" && options.sizeHint !== void 0 && options.sizeHint <= options.wholeThreshold) {
509
- const bytes = await withRetry(async (attemptSignal) => {
510
- const response = await fetcher(url, { signal: attemptSignal });
511
- if (!response.ok) throw new HttpStatusError(url, response.status);
512
- return fetchBytes(response);
513
- }, retry, signal);
514
- options.onResponse(bytes.length);
515
- return {
516
- kind: "whole",
517
- bytes
518
- };
519
- }
520
- let first = await withRetry(async (attemptSignal) => {
521
- const response = await fetcher(url, {
522
- headers: { Range: `bytes=-${TAIL_BYTES}` },
523
- signal: attemptSignal
524
- });
525
- if (response.status !== 200 && response.status !== 206) throw new HttpStatusError(url, response.status);
526
- return {
527
- status: response.status,
528
- bytes: await fetchBytes(response),
529
- etag: response.headers.get("etag") ?? void 0,
530
- range: response.headers.get("content-range") ?? void 0
531
- };
532
- }, retry, signal);
533
- options.onResponse(first.bytes.length);
534
- if (first.status === 200) return {
535
- kind: "whole",
536
- bytes: first.bytes
537
- };
538
- let range = contentRange(first.range, url);
539
- const total = range?.size;
540
- if (total !== void 0 && (range?.start !== Math.max(0, total - 65536) || range?.end !== total - 1 || first.bytes.length !== Math.min(total, 65536))) {
541
- const validator = strongEtag(first.etag);
542
- const start = Math.max(0, total - TAIL_BYTES);
543
- first = await withRetry(async (attemptSignal) => {
544
- const headers = { Range: `bytes=${start}-${total - 1}` };
545
- if (validator !== void 0) headers["If-Range"] = validator;
546
- const response = await fetcher(url, {
547
- headers,
548
- signal: attemptSignal
549
- });
550
- if (validator !== void 0 && (response.status === 200 || response.headers.has("etag") && response.headers.get("etag") !== validator)) throw new PackChangedError(url);
551
- if (response.status !== 200 && response.status !== 206) throw new HttpStatusError(url, response.status);
552
- return {
553
- status: response.status,
554
- bytes: await fetchBytes(response),
555
- etag: response.headers.get("etag") ?? validator,
556
- range: response.headers.get("content-range") ?? void 0
557
- };
558
- }, retry, signal);
559
- options.onResponse(first.bytes.length);
560
- if (first.status === 200) return {
561
- kind: "whole",
562
- bytes: first.bytes
563
- };
564
- range = contentRange(first.range, url);
565
- if (first.bytes.length !== total - start || range !== void 0 && (range.start !== start || range.end !== total - 1 || range.size !== void 0 && range.size !== total)) throw new Error(`tail range ${start}-${total - 1} of ${url} was not honoured`);
566
- } else if (range !== void 0 && first.bytes.length !== range.end - range.start + 1) throw new Error(`Content-Range length does not match response bytes for ${url}`);
567
- const etag = strongEtag(first.etag);
568
- return {
569
- kind: "range",
570
- tail: first.bytes,
571
- ...total !== void 0 && Number.isFinite(total) ? { size: total } : {},
572
- reader: (size) => ({
573
- size,
574
- read: (offset, length, readSignal) => withRetry(async (attemptSignal) => {
575
- const headers = { Range: `bytes=${offset}-${offset + length - 1}` };
576
- if (etag !== void 0) headers["If-Range"] = etag;
577
- const response = await fetcher(url, {
578
- headers,
579
- signal: readSignal === void 0 ? attemptSignal : AbortSignal.any([attemptSignal, readSignal])
580
- });
581
- if (response.status === 200) {
582
- if (etag !== void 0) throw new PackChangedError(url);
583
- const all = await fetchBytes(response);
584
- options.onResponse(all.length);
585
- return all.subarray(offset, offset + length);
586
- }
587
- if (response.status !== 206) throw new HttpStatusError(url, response.status);
588
- const returnedRange = contentRange(response.headers.get("content-range") ?? void 0, url);
589
- if (returnedRange !== void 0 && (returnedRange.start !== offset || returnedRange.end !== offset + length - 1 || returnedRange.size !== void 0 && returnedRange.size !== size)) throw new Error(`range ${offset}+${length} of ${url} was not honoured`);
590
- const bytes = await fetchBytes(response);
591
- options.onResponse(bytes.length);
592
- if (bytes.length !== length) throw new Error(`range ${offset}+${length} of ${url} returned ${bytes.length} bytes`);
593
- return bytes;
594
- }, retry, signal)
595
- })
596
- };
597
- }
598
- /**
599
- * Queue reads for one source. Reads issued in the same tick are sorted, merged when close
600
- * together, and run with bounded concurrency, high priority first.
601
- */
602
- var ReadScheduler = class {
603
- reader;
604
- signal;
605
- queue = [];
606
- flushing = false;
607
- running = 0;
608
- batches = [];
609
- concurrency;
610
- mergeGap;
611
- maxSpan;
612
- constructor(reader, signal, options = {}) {
613
- this.reader = reader;
614
- this.signal = signal;
615
- this.concurrency = Math.max(1, options.concurrency ?? 6);
616
- this.mergeGap = options.mergeGap ?? 32 * 1024;
617
- this.maxSpan = options.maxSpan ?? 4 * 1024 * 1024;
618
- }
619
- read(offset, length, high = true, signal) {
620
- if (length === 0) return Promise.resolve(new Uint8Array(0));
621
- return new Promise((resolve, reject) => {
622
- if (signal?.aborted) return reject(signal.reason);
623
- const abort = () => reject(signal?.reason);
624
- signal?.addEventListener("abort", abort, { once: true });
625
- this.queue.push({
626
- offset,
627
- length,
628
- high,
629
- resolve: (bytes) => {
630
- signal?.removeEventListener("abort", abort);
631
- resolve(bytes);
632
- },
633
- reject: (error) => {
634
- signal?.removeEventListener("abort", abort);
635
- reject(error);
636
- }
637
- });
638
- if (!this.flushing) {
639
- this.flushing = true;
640
- queueMicrotask(() => this.flush());
641
- }
642
- });
643
- }
644
- flush() {
645
- this.flushing = false;
646
- const waiters = this.queue.sort((a, b) => a.offset - b.offset);
647
- this.queue = [];
648
- for (const waiter of waiters) {
649
- const last = this.batches.at(-1);
650
- const end = waiter.offset + waiter.length;
651
- if (last !== void 0 && waiter.offset - last.end <= this.mergeGap && Math.max(last.end, end) - last.start <= this.maxSpan && last.start <= waiter.offset) {
652
- last.end = Math.max(last.end, end);
653
- last.high ||= waiter.high;
654
- last.waiters.push(waiter);
655
- } else this.batches.push({
656
- start: waiter.offset,
657
- end,
658
- high: waiter.high,
659
- waiters: [waiter]
660
- });
661
- }
662
- this.batches.sort((a, b) => Number(b.high) - Number(a.high));
663
- this.pump();
664
- }
665
- pump() {
666
- if (this.signal.aborted) {
667
- for (const batch of this.batches.splice(0)) for (const waiter of batch.waiters) waiter.reject(this.signal.reason);
668
- return;
669
- }
670
- while (this.running < this.concurrency && this.batches.length > 0) {
671
- const batch = this.batches.shift();
672
- this.running++;
673
- this.reader.read(batch.start, batch.end - batch.start, this.signal).then((bytes) => {
674
- for (const waiter of batch.waiters) {
675
- const from = waiter.offset - batch.start;
676
- waiter.resolve(bytes.subarray(from, from + waiter.length));
677
- }
678
- }, (error) => {
679
- for (const waiter of batch.waiters) waiter.reject(error);
680
- }).finally(() => {
681
- this.running--;
682
- this.pump();
683
- });
684
- }
685
- }
686
- };
687
- //#endregion
688
422
  //#region src/pack.ts
689
423
  /**
690
424
  * Open a pack from any source and read its files. Only the tail of a remote pack is fetched up
@@ -1000,4 +734,4 @@ async function openPack(input, options = {}) {
1000
734
  }, scheduler, controller, options);
1001
735
  }
1002
736
  //#endregion
1003
- export { PackChangedError as a, isTextPath as c, packFromFiles as i, mediaTypeOf as l, PackIntegrityError as n, createPack as o, openPack as r, describePack as s, PackEntryNotFoundError as t, sha256 as u };
737
+ export { createPack as a, mediaTypeOf as c, packFromFiles as i, sha256 as l, PackIntegrityError as n, describePack as o, openPack as r, isTextPath as s, PackEntryNotFoundError as t };
@@ -1,3 +1,4 @@
1
+ import { n as PackRetryOptions, r as RangeReader } from "./source-7Pr2tntF.mjs";
1
2
  import { PackManifest } from "@bendyline/molen-schema";
2
3
 
3
4
  //#region src/build.d.ts
@@ -42,31 +43,6 @@ declare function describePack(files: readonly PackFile[], options: PackOptions):
42
43
  /** Build a pack: a deterministic zip whose last member is the manifest. */
43
44
  declare function createPack(files: readonly PackFile[], options: PackOptions): Promise<BuiltPack>;
44
45
  //#endregion
45
- //#region src/source.d.ts
46
- /**
47
- * Byte sources a pack can be read from, and the transport policy for remote ones: tail-first
48
- * opening, range reads merged when they are close together, bounded concurrency, and retries.
49
- */
50
- /** Random access to a pack's bytes. Supply one for storage Molen does not know about. */
51
- interface RangeReader {
52
- /** Total size in bytes. */
53
- readonly size: number;
54
- read(offset: number, length: number, signal?: AbortSignal): Promise<Uint8Array>;
55
- }
56
- interface PackRetryOptions {
57
- /** Attempts per request, including the first (default 3). */
58
- attempts?: number;
59
- /** Delay before the first retry; doubles each time (default 250 ms). */
60
- baseDelayMs?: number;
61
- maxDelayMs?: number;
62
- /** Per-request deadline (default 30 s). */
63
- timeoutMs?: number;
64
- }
65
- /** The server replaced the pack while it was being read (its ETag changed). */
66
- declare class PackChangedError extends Error {
67
- constructor(url: string);
68
- }
69
- //#endregion
70
46
  //#region src/pack.d.ts
71
47
  /** Where a pack's bytes come from: a URL, a file the user picked, bytes, or your own reader. */
72
48
  type PackInput = string | URL | Blob | ArrayBuffer | Uint8Array | RangeReader;
@@ -138,4 +114,4 @@ declare function packFromFiles(files: readonly PackFile[], options: PackOptions,
138
114
  /** Open a pack. Reads only the manifest and zip directory; files are fetched when read. */
139
115
  declare function openPack(input: PackInput, options?: OpenPackOptions): Promise<Pack>;
140
116
  //#endregion
141
- export { describePack as _, PackIntegrityError as a, sha256 as b, openPack as c, PackRetryOptions as d, RangeReader as f, createPack as g, PackOptions as h, PackInput as i, packFromFiles as l, PackFile as m, Pack as n, PackProgress as o, BuiltPack as p, PackEntryNotFoundError as r, ReadOptions as s, OpenPackOptions as t, PackChangedError as u, isTextPath as v, mediaTypeOf as y };
117
+ export { sha256 as _, PackIntegrityError as a, openPack as c, PackFile as d, PackOptions as f, mediaTypeOf as g, isTextPath as h, PackInput as i, packFromFiles as l, describePack as m, Pack as n, PackProgress as o, createPack as p, PackEntryNotFoundError as r, ReadOptions as s, OpenPackOptions as t, BuiltPack as u };
@@ -0,0 +1,41 @@
1
+ //#region src/source.d.ts
2
+ /**
3
+ * Byte sources a pack can be read from, and the transport policy for remote ones: tail-first
4
+ * opening, range reads merged when they are close together, bounded concurrency, and retries.
5
+ */
6
+ /** Random access to a pack's bytes. Supply one for storage Molen does not know about. */
7
+ interface RangeReader {
8
+ /** Total size in bytes. */
9
+ readonly size: number;
10
+ read(offset: number, length: number, signal?: AbortSignal): Promise<Uint8Array>;
11
+ }
12
+ interface PackRetryOptions {
13
+ /** Attempts per request, including the first (default 3). */
14
+ attempts?: number;
15
+ /** Delay before the first retry; doubles each time (default 250 ms). */
16
+ baseDelayMs?: number;
17
+ maxDelayMs?: number;
18
+ /** Per-request deadline (default 30 s). */
19
+ timeoutMs?: number;
20
+ }
21
+ /** The server replaced the pack while it was being read (its ETag changed). */
22
+ declare class PackChangedError extends Error {
23
+ constructor(url: string);
24
+ }
25
+ interface UrlRangeReaderOptions {
26
+ /** Total size in bytes (a pack index lists it). */
27
+ size: number;
28
+ fetch?: typeof fetch;
29
+ retry?: PackRetryOptions;
30
+ /** Aborts every read. */
31
+ signal?: AbortSignal;
32
+ }
33
+ /**
34
+ * A `RangeReader` over a URL whose size is already known, with no opening request: the first
35
+ * response's strong ETag becomes the If-Range validator for every later read, so a file replaced
36
+ * mid-read fails with `PackChangedError` instead of mixing versions. Pair it with
37
+ * `cachingRangeReader` to open a pack from cached bytes.
38
+ */
39
+ declare function urlRangeReader(url: string, options: UrlRangeReaderOptions): RangeReader;
40
+ //#endregion
41
+ export { urlRangeReader as a, UrlRangeReaderOptions as i, PackRetryOptions as n, RangeReader as r, PackChangedError as t };