generic-filehandle2 2.1.7 → 2.1.9
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/README.md +1 -1
- package/dist/blobFile.d.ts +4 -8
- package/dist/blobFile.js +4 -10
- package/dist/blobFile.js.map +1 -1
- package/dist/browser.d.ts +4 -4
- package/dist/browser.js +6 -3
- package/dist/browser.js.map +1 -1
- package/dist/filehandle.d.ts +4 -11
- package/dist/localFile.d.ts +4 -11
- package/dist/localFile.js.map +1 -1
- package/dist/remoteFile.d.ts +5 -11
- package/dist/remoteFile.js +25 -30
- package/dist/remoteFile.js.map +1 -1
- package/dist/util.d.ts +1 -0
- package/dist/util.js +11 -0
- package/dist/util.js.map +1 -0
- package/esm/blobFile.d.ts +4 -8
- package/esm/blobFile.js +4 -10
- package/esm/blobFile.js.map +1 -1
- package/esm/browser.d.ts +4 -4
- package/esm/browser.js +6 -3
- package/esm/browser.js.map +1 -1
- package/esm/filehandle.d.ts +4 -11
- package/esm/localFile.d.ts +4 -11
- package/esm/localFile.js.map +1 -1
- package/esm/remoteFile.d.ts +5 -11
- package/esm/remoteFile.js +25 -30
- package/esm/remoteFile.js.map +1 -1
- package/esm/util.d.ts +1 -0
- package/esm/util.js +8 -0
- package/esm/util.js.map +1 -0
- package/package.json +6 -6
- package/src/blobFile.ts +11 -18
- package/src/browser.ts +9 -8
- package/src/filehandle.ts +6 -13
- package/src/localFile.ts +6 -13
- package/src/remoteFile.ts +40 -45
- package/src/util.ts +9 -0
package/README.md
CHANGED
package/dist/blobFile.d.ts
CHANGED
|
@@ -9,14 +9,10 @@ export default class BlobFile implements GenericFilehandle {
|
|
|
9
9
|
private blob;
|
|
10
10
|
constructor(blob: Blob);
|
|
11
11
|
read(length: number, position?: number): Promise<Uint8Array<ArrayBuffer>>;
|
|
12
|
-
readFile(): Promise<Uint8Array<ArrayBuffer>>;
|
|
13
|
-
readFile(options: BufferEncoding
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
})): Promise<Uint8Array<ArrayBuffer>>;
|
|
17
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
18
|
-
encoding: T;
|
|
19
|
-
}): Promise<string>;
|
|
12
|
+
readFile(options?: Omit<FilehandleOptions, 'encoding'>): Promise<Uint8Array<ArrayBuffer>>;
|
|
13
|
+
readFile(options: BufferEncoding | (Omit<FilehandleOptions, 'encoding'> & {
|
|
14
|
+
encoding: BufferEncoding;
|
|
15
|
+
})): Promise<string>;
|
|
20
16
|
stat(): Promise<Stats>;
|
|
21
17
|
close(): Promise<void>;
|
|
22
18
|
}
|
package/dist/blobFile.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const util_ts_1 = require("./util.js");
|
|
3
4
|
/**
|
|
4
5
|
* Blob of binary data fetched from a local file (with FileReader).
|
|
5
6
|
*
|
|
@@ -14,14 +15,10 @@ class BlobFile {
|
|
|
14
15
|
async read(length, position = 0) {
|
|
15
16
|
// short-circuit a read of 0 bytes here, because browsers actually sometimes
|
|
16
17
|
// crash if you try to read 0 bytes from a local file!
|
|
17
|
-
if (
|
|
18
|
+
if (length === 0) {
|
|
18
19
|
return new Uint8Array(0);
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
22
|
-
return slice.bytes
|
|
23
|
-
? slice.bytes()
|
|
24
|
-
: new Uint8Array(await slice.arrayBuffer());
|
|
21
|
+
return (0, util_ts_1.toBytes)(this.blob.slice(position, position + length));
|
|
25
22
|
}
|
|
26
23
|
async readFile(options) {
|
|
27
24
|
const encoding = typeof options === 'string' ? options : options?.encoding;
|
|
@@ -32,10 +29,7 @@ class BlobFile {
|
|
|
32
29
|
throw new Error(`unsupported encoding: ${encoding}`);
|
|
33
30
|
}
|
|
34
31
|
else {
|
|
35
|
-
|
|
36
|
-
return this.blob.bytes
|
|
37
|
-
? this.blob.bytes()
|
|
38
|
-
: new Uint8Array(await this.blob.arrayBuffer());
|
|
32
|
+
return (0, util_ts_1.toBytes)(this.blob);
|
|
39
33
|
}
|
|
40
34
|
}
|
|
41
35
|
stat() {
|
package/dist/blobFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"blobFile.js","sourceRoot":"","sources":["../src/blobFile.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"blobFile.js","sourceRoot":"","sources":["../src/blobFile.ts"],"names":[],"mappings":";;AAAA,uCAAmC;AASnC;;;;;GAKG;AACH,MAAqB,QAAQ;IACnB,IAAI,CAAM;IAElB,YAAmB,IAAU;QAC3B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;IAEM,KAAK,CAAC,IAAI,CACf,MAAc,EACd,QAAQ,GAAG,CAAC;QAEZ,4EAA4E;QAC5E,sDAAsD;QACtD,IAAI,MAAM,KAAK,CAAC,EAAE,CAAC;YACjB,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;QAC1B,CAAC;QAED,OAAO,IAAA,iBAAO,EAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAA;IAC9D,CAAC;IAUM,KAAK,CAAC,QAAQ,CACnB,OAA4C;QAE5C,MAAM,QAAQ,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAA;QAC1E,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAA;QACzB,CAAC;aAAM,IAAI,QAAQ,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,yBAAyB,QAAQ,EAAE,CAAC,CAAA;QACtD,CAAC;aAAM,CAAC;YACN,OAAO,IAAA,iBAAO,EAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAC3B,CAAC;IACH,CAAC;IAEM,IAAI;QACT,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAA;IAClD,CAAC;IAEM,KAAK;QACV,OAAO,OAAO,CAAC,OAAO,EAAE,CAAA;IAC1B,CAAC;CACF;AAhDD,2BAgDC"}
|
package/dist/browser.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ export * from './filehandle.ts';
|
|
|
2
2
|
export { default as BlobFile } from './blobFile.ts';
|
|
3
3
|
export { default as RemoteFile } from './remoteFile.ts';
|
|
4
4
|
export declare class LocalFile {
|
|
5
|
-
readFile():
|
|
6
|
-
read():
|
|
7
|
-
|
|
5
|
+
readFile(): Promise<never>;
|
|
6
|
+
read(): Promise<never>;
|
|
7
|
+
stat(): Promise<never>;
|
|
8
|
+
close(): Promise<never>;
|
|
8
9
|
}
|
|
9
|
-
export { type GenericFilehandle } from './filehandle.ts';
|
package/dist/browser.js
CHANGED
|
@@ -26,13 +26,16 @@ var remoteFile_ts_1 = require("./remoteFile.js");
|
|
|
26
26
|
Object.defineProperty(exports, "RemoteFile", { enumerable: true, get: function () { return __importDefault(remoteFile_ts_1).default; } });
|
|
27
27
|
class LocalFile {
|
|
28
28
|
readFile() {
|
|
29
|
-
|
|
29
|
+
return Promise.reject(new Error('unimplemented'));
|
|
30
30
|
}
|
|
31
31
|
read() {
|
|
32
|
-
|
|
32
|
+
return Promise.reject(new Error('unimplemented'));
|
|
33
|
+
}
|
|
34
|
+
stat() {
|
|
35
|
+
return Promise.reject(new Error('unimplemented'));
|
|
33
36
|
}
|
|
34
37
|
close() {
|
|
35
|
-
|
|
38
|
+
return Promise.reject(new Error('unimplemented'));
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
41
|
exports.LocalFile = LocalFile;
|
package/dist/browser.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,6DAA6D;AAC7D,kDAA+B;AAE/B,6CAAmD;AAA1C,wHAAA,OAAO,OAAY;AAC5B,iDAAuD;AAA9C,4HAAA,OAAO,OAAc;AAE9B,MAAa,SAAS;IACpB,QAAQ;QACN,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,6DAA6D;AAC7D,kDAA+B;AAE/B,6CAAmD;AAA1C,wHAAA,OAAO,OAAY;AAC5B,iDAAuD;AAA9C,4HAAA,OAAO,OAAc;AAE9B,MAAa,SAAS;IACpB,QAAQ;QACN,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAA;IACnD,CAAC;IACD,IAAI;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAA;IACnD,CAAC;IACD,IAAI;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAA;IACnD,CAAC;IACD,KAAK;QACH,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAA;IACnD,CAAC;CACF;AAbD,8BAaC"}
|
package/dist/filehandle.d.ts
CHANGED
|
@@ -12,17 +12,10 @@ export interface Stats {
|
|
|
12
12
|
}
|
|
13
13
|
export interface GenericFilehandle {
|
|
14
14
|
read(length: number, position: number, opts?: FilehandleOptions): Promise<Uint8Array<ArrayBuffer>>;
|
|
15
|
-
readFile(): Promise<Uint8Array<ArrayBuffer>>;
|
|
16
|
-
readFile(options: BufferEncoding
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
})): Promise<Uint8Array<ArrayBuffer>>;
|
|
20
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
21
|
-
encoding: T;
|
|
22
|
-
}): Promise<string>;
|
|
23
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
24
|
-
encoding: T;
|
|
25
|
-
}): T extends BufferEncoding ? Promise<string> : Promise<Uint8Array<ArrayBuffer>>;
|
|
15
|
+
readFile(options?: Omit<FilehandleOptions, 'encoding'>): Promise<Uint8Array<ArrayBuffer>>;
|
|
16
|
+
readFile(options: BufferEncoding | (Omit<FilehandleOptions, 'encoding'> & {
|
|
17
|
+
encoding: BufferEncoding;
|
|
18
|
+
})): Promise<string>;
|
|
26
19
|
stat(): Promise<Stats>;
|
|
27
20
|
close(): Promise<void>;
|
|
28
21
|
}
|
package/dist/localFile.d.ts
CHANGED
|
@@ -3,17 +3,10 @@ export default class LocalFile implements GenericFilehandle {
|
|
|
3
3
|
private filename;
|
|
4
4
|
constructor(source: string);
|
|
5
5
|
read(length: number, position?: number): Promise<Uint8Array<ArrayBuffer>>;
|
|
6
|
-
readFile(): Promise<Uint8Array<ArrayBuffer>>;
|
|
7
|
-
readFile(options: BufferEncoding
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
})): Promise<Uint8Array<ArrayBuffer>>;
|
|
11
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
12
|
-
encoding: T;
|
|
13
|
-
}): Promise<string>;
|
|
14
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
15
|
-
encoding: T;
|
|
16
|
-
}): T extends BufferEncoding ? Promise<string> : Promise<Uint8Array<ArrayBuffer>>;
|
|
6
|
+
readFile(options?: Omit<FilehandleOptions, 'encoding'>): Promise<Uint8Array<ArrayBuffer>>;
|
|
7
|
+
readFile(options: BufferEncoding | (Omit<FilehandleOptions, 'encoding'> & {
|
|
8
|
+
encoding: BufferEncoding;
|
|
9
|
+
})): Promise<string>;
|
|
17
10
|
stat(): Promise<Stats>;
|
|
18
11
|
close(): Promise<void>;
|
|
19
12
|
}
|
package/dist/localFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localFile.js","sourceRoot":"","sources":["../src/localFile.ts"],"names":[],"mappings":";;AAAA,0CAAkD;AASlD,MAAqB,SAAS;IACpB,QAAQ,CAAQ;IAExB,YAAmB,MAAc;QAC/B,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAA;IACxB,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,MAAc,EAAE,QAAQ,GAAG,CAAC;QAC5C,IAAI,MAAM,KAAK,CAAC,EAAE,CAAC;YACjB,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;QAC1B,CAAC;QACD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAA;QAClC,IAAI,EAAE,CAAA;QACN,IAAI,CAAC;YACH,EAAE,GAAG,MAAM,IAAA,eAAI,EAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAA;YACnC,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAA;YACnD,OAAO,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,SAAS,CAAC,CAAA;QAC9C,CAAC;gBAAS,CAAC;YACT,IAAI,EAAE,EAAE,CAAC;gBACP,IAAI,CAAC;oBACH,MAAM,EAAE,CAAC,KAAK,EAAE,CAAA;gBAClB,CAAC;gBAAC,MAAM,CAAC;oBACP,sEAAsE;oBACtE,oDAAoD;gBACtD,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;
|
|
1
|
+
{"version":3,"file":"localFile.js","sourceRoot":"","sources":["../src/localFile.ts"],"names":[],"mappings":";;AAAA,0CAAkD;AASlD,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 export default class RemoteFile implements GenericFilehandle {
|
|
|
6
6
|
private baseHeaders;
|
|
7
7
|
private baseOverrides;
|
|
8
8
|
constructor(source: string, opts?: FilehandleOptions);
|
|
9
|
+
private buildRequest;
|
|
9
10
|
fetch(input: RequestInfo, init: RequestInit | undefined): Promise<Response>;
|
|
10
11
|
read(length: number, position: number, opts?: FilehandleOptions): Promise<Uint8Array<ArrayBuffer>>;
|
|
11
|
-
readFile(): Promise<Uint8Array<ArrayBuffer>>;
|
|
12
|
-
readFile(options: BufferEncoding
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
})): Promise<Uint8Array<ArrayBuffer>>;
|
|
16
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
17
|
-
encoding: T;
|
|
18
|
-
}): Promise<string>;
|
|
19
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
20
|
-
encoding: T;
|
|
21
|
-
}): T extends BufferEncoding ? Promise<string> : Promise<Uint8Array<ArrayBuffer>>;
|
|
12
|
+
readFile(options?: Omit<FilehandleOptions, 'encoding'>): Promise<Uint8Array<ArrayBuffer>>;
|
|
13
|
+
readFile(options: BufferEncoding | (Omit<FilehandleOptions, 'encoding'> & {
|
|
14
|
+
encoding: BufferEncoding;
|
|
15
|
+
})): Promise<string>;
|
|
22
16
|
stat(): Promise<Stats>;
|
|
23
17
|
close(): Promise<void>;
|
|
24
18
|
}
|
package/dist/remoteFile.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const util_ts_1 = require("./util.js");
|
|
3
4
|
function getMessage(e) {
|
|
4
5
|
const r = typeof e === 'object' &&
|
|
5
6
|
e !== null &&
|
|
@@ -7,6 +8,7 @@ function getMessage(e) {
|
|
|
7
8
|
typeof e.message === 'string'
|
|
8
9
|
? e.message
|
|
9
10
|
: `${e}`;
|
|
11
|
+
// strip trailing period so the wrapped form `${msg} fetching ${url}` reads cleanly
|
|
10
12
|
return r.replace(/\.$/, '');
|
|
11
13
|
}
|
|
12
14
|
class RemoteFile {
|
|
@@ -19,9 +21,18 @@ class RemoteFile {
|
|
|
19
21
|
this.url = source;
|
|
20
22
|
this.baseHeaders = opts.headers ?? {};
|
|
21
23
|
this.baseOverrides = opts.overrides ?? {};
|
|
22
|
-
this.fetchImplementation =
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
this.fetchImplementation = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
25
|
+
}
|
|
26
|
+
buildRequest(opts, extraHeaders) {
|
|
27
|
+
return {
|
|
28
|
+
...this.baseOverrides,
|
|
29
|
+
...opts.overrides,
|
|
30
|
+
headers: { ...this.baseHeaders, ...opts.headers, ...extraHeaders },
|
|
31
|
+
method: 'GET',
|
|
32
|
+
redirect: 'follow',
|
|
33
|
+
mode: 'cors',
|
|
34
|
+
signal: opts.signal,
|
|
35
|
+
};
|
|
25
36
|
}
|
|
26
37
|
async fetch(input, init) {
|
|
27
38
|
const wrapError = (e) => new Error(`${getMessage(e)} fetching ${input}`, { cause: e });
|
|
@@ -59,17 +70,9 @@ class RemoteFile {
|
|
|
59
70
|
if (Number.isNaN(length) || Number.isNaN(position)) {
|
|
60
71
|
throw new TypeError(`read() called with NaN length or position (length=${length}, position=${position}). The index file may be corrupt.`);
|
|
61
72
|
}
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
...this.baseOverrides,
|
|
66
|
-
...overrides,
|
|
67
|
-
headers: { ...this.baseHeaders, ...headers },
|
|
68
|
-
method: 'GET',
|
|
69
|
-
redirect: 'follow',
|
|
70
|
-
mode: 'cors',
|
|
71
|
-
signal,
|
|
72
|
-
});
|
|
73
|
+
const res = await this.fetch(this.url, this.buildRequest(opts, {
|
|
74
|
+
range: `bytes=${position}-${position + length - 1}`,
|
|
75
|
+
}));
|
|
73
76
|
if (!res.ok) {
|
|
74
77
|
throw new Error(`HTTP ${res.status} fetching ${this.url}`);
|
|
75
78
|
}
|
|
@@ -82,10 +85,12 @@ class RemoteFile {
|
|
|
82
85
|
size: parseInt(sizeMatch[1], 10),
|
|
83
86
|
};
|
|
84
87
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
88
|
+
const resData = await (0, util_ts_1.toBytes)(res);
|
|
89
|
+
// server didn't honor the range request and returned the full file —
|
|
90
|
+
// the body length is the actual file size
|
|
91
|
+
if (!this._stat && res.status === 200) {
|
|
92
|
+
this._stat = { size: resData.byteLength };
|
|
93
|
+
}
|
|
89
94
|
return resData.byteLength <= length
|
|
90
95
|
? resData
|
|
91
96
|
: resData.subarray(0, length);
|
|
@@ -97,16 +102,7 @@ class RemoteFile {
|
|
|
97
102
|
async readFile(options = {}) {
|
|
98
103
|
const encoding = typeof options === 'string' ? options : options.encoding;
|
|
99
104
|
const opts = typeof options === 'string' ? {} : options;
|
|
100
|
-
const
|
|
101
|
-
const res = await this.fetch(this.url, {
|
|
102
|
-
...this.baseOverrides,
|
|
103
|
-
...overrides,
|
|
104
|
-
headers: { ...this.baseHeaders, ...headers },
|
|
105
|
-
method: 'GET',
|
|
106
|
-
redirect: 'follow',
|
|
107
|
-
mode: 'cors',
|
|
108
|
-
signal,
|
|
109
|
-
});
|
|
105
|
+
const res = await this.fetch(this.url, this.buildRequest(opts));
|
|
110
106
|
if (!res.ok) {
|
|
111
107
|
throw new Error(`HTTP ${res.status} fetching ${this.url}`);
|
|
112
108
|
}
|
|
@@ -117,8 +113,7 @@ class RemoteFile {
|
|
|
117
113
|
throw new Error(`unsupported encoding: ${encoding}`);
|
|
118
114
|
}
|
|
119
115
|
else {
|
|
120
|
-
|
|
121
|
-
return res.bytes ? res.bytes() : new Uint8Array(await res.arrayBuffer());
|
|
116
|
+
return (0, util_ts_1.toBytes)(res);
|
|
122
117
|
}
|
|
123
118
|
}
|
|
124
119
|
async stat() {
|
package/dist/remoteFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remoteFile.js","sourceRoot":"","sources":["../src/remoteFile.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"remoteFile.js","sourceRoot":"","sources":["../src/remoteFile.ts"],"names":[],"mappings":";;AAAA,uCAAmC;AAUnC,SAAS,UAAU,CAAC,CAAU;IAC5B,MAAM,CAAC,GACL,OAAO,CAAC,KAAK,QAAQ;QACrB,CAAC,KAAK,IAAI;QACV,SAAS,IAAI,CAAC;QACd,OAAO,CAAC,CAAC,OAAO,KAAK,QAAQ;QAC3B,CAAC,CAAC,CAAC,CAAC,OAAO;QACX,CAAC,CAAC,GAAG,CAAC,EAAE,CAAA;IACZ,mFAAmF;IACnF,OAAO,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAA;AAC7B,CAAC;AAED,MAAqB,UAAU;IACnB,GAAG,CAAQ;IACb,KAAK,CAAQ;IACb,mBAAmB,CAAS;IAC5B,WAAW,CAAwB;IACnC,aAAa,CAA8B;IAEnD,YAAmB,MAAc,EAAE,OAA0B,EAAE;QAC7D,IAAI,CAAC,GAAG,GAAG,MAAM,CAAA;QACjB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,OAAO,IAAI,EAAE,CAAA;QACrC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,SAAS,IAAI,EAAE,CAAA;QACzC,IAAI,CAAC,mBAAmB,GAAG,IAAI,CAAC,KAAK,IAAI,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;IAC5E,CAAC;IAEO,YAAY,CAClB,IAAuB,EACvB,YAAqC;QAErC,OAAO;YACL,GAAG,IAAI,CAAC,aAAa;YACrB,GAAG,IAAI,CAAC,SAAS;YACjB,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,OAAO,EAAE,GAAG,YAAY,EAAE;YAClE,MAAM,EAAE,KAAK;YAC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iBAAO,EAAC,GAAG,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;IAEM,KAAK,CAAC,IAAI;QACf,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;YAChB,MAAM,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;QACxB,CAAC;QACD,sEAAsE;QACtE,mDAAmD;QACnD,OAAO,IAAI,CAAC,KAAK,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,CAAA;IAClC,CAAC;IAEM,KAAK;QACV,OAAO,OAAO,CAAC,OAAO,EAAE,CAAA;IAC1B,CAAC;CACF;AAzJD,6BAyJC"}
|
package/dist/util.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function toBytes(src: Response | Blob): Promise<Uint8Array<ArrayBuffer>>;
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toBytes = toBytes;
|
|
4
|
+
// Response.bytes() / Blob.bytes() is widely available but not yet in all
|
|
5
|
+
// lib.dom.d.ts versions, so the optional-chain check is load-bearing for older
|
|
6
|
+
// runtimes despite TS thinking it's always defined.
|
|
7
|
+
async function toBytes(src) {
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
9
|
+
return src.bytes ? src.bytes() : new Uint8Array(await src.arrayBuffer());
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=util.js.map
|
package/dist/util.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;AAGA,0BAKC;AARD,yEAAyE;AACzE,+EAA+E;AAC/E,oDAAoD;AAC7C,KAAK,UAAU,OAAO,CAC3B,GAAoB;IAEpB,uEAAuE;IACvE,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,WAAW,EAAE,CAAC,CAAA;AAC1E,CAAC"}
|
package/esm/blobFile.d.ts
CHANGED
|
@@ -9,14 +9,10 @@ export default class BlobFile implements GenericFilehandle {
|
|
|
9
9
|
private blob;
|
|
10
10
|
constructor(blob: Blob);
|
|
11
11
|
read(length: number, position?: number): Promise<Uint8Array<ArrayBuffer>>;
|
|
12
|
-
readFile(): Promise<Uint8Array<ArrayBuffer>>;
|
|
13
|
-
readFile(options: BufferEncoding
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
})): Promise<Uint8Array<ArrayBuffer>>;
|
|
17
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
18
|
-
encoding: T;
|
|
19
|
-
}): Promise<string>;
|
|
12
|
+
readFile(options?: Omit<FilehandleOptions, 'encoding'>): Promise<Uint8Array<ArrayBuffer>>;
|
|
13
|
+
readFile(options: BufferEncoding | (Omit<FilehandleOptions, 'encoding'> & {
|
|
14
|
+
encoding: BufferEncoding;
|
|
15
|
+
})): Promise<string>;
|
|
20
16
|
stat(): Promise<Stats>;
|
|
21
17
|
close(): Promise<void>;
|
|
22
18
|
}
|
package/esm/blobFile.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { toBytes } from "./util.js";
|
|
1
2
|
/**
|
|
2
3
|
* Blob of binary data fetched from a local file (with FileReader).
|
|
3
4
|
*
|
|
@@ -12,14 +13,10 @@ export default class BlobFile {
|
|
|
12
13
|
async read(length, position = 0) {
|
|
13
14
|
// short-circuit a read of 0 bytes here, because browsers actually sometimes
|
|
14
15
|
// crash if you try to read 0 bytes from a local file!
|
|
15
|
-
if (
|
|
16
|
+
if (length === 0) {
|
|
16
17
|
return new Uint8Array(0);
|
|
17
18
|
}
|
|
18
|
-
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
20
|
-
return slice.bytes
|
|
21
|
-
? slice.bytes()
|
|
22
|
-
: new Uint8Array(await slice.arrayBuffer());
|
|
19
|
+
return toBytes(this.blob.slice(position, position + length));
|
|
23
20
|
}
|
|
24
21
|
async readFile(options) {
|
|
25
22
|
const encoding = typeof options === 'string' ? options : options?.encoding;
|
|
@@ -30,10 +27,7 @@ export default class BlobFile {
|
|
|
30
27
|
throw new Error(`unsupported encoding: ${encoding}`);
|
|
31
28
|
}
|
|
32
29
|
else {
|
|
33
|
-
|
|
34
|
-
return this.blob.bytes
|
|
35
|
-
? this.blob.bytes()
|
|
36
|
-
: new Uint8Array(await this.blob.arrayBuffer());
|
|
30
|
+
return toBytes(this.blob);
|
|
37
31
|
}
|
|
38
32
|
}
|
|
39
33
|
stat() {
|
package/esm/blobFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"blobFile.js","sourceRoot":"","sources":["../src/blobFile.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"blobFile.js","sourceRoot":"","sources":["../src/blobFile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AASnC;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,QAAQ;IACnB,IAAI,CAAM;IAElB,YAAmB,IAAU;QAC3B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;IAEM,KAAK,CAAC,IAAI,CACf,MAAc,EACd,QAAQ,GAAG,CAAC;QAEZ,4EAA4E;QAC5E,sDAAsD;QACtD,IAAI,MAAM,KAAK,CAAC,EAAE,CAAC;YACjB,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;QAC1B,CAAC;QAED,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,CAAC,CAAC,CAAA;IAC9D,CAAC;IAUM,KAAK,CAAC,QAAQ,CACnB,OAA4C;QAE5C,MAAM,QAAQ,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAA;QAC1E,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAA;QACzB,CAAC;aAAM,IAAI,QAAQ,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,yBAAyB,QAAQ,EAAE,CAAC,CAAA;QACtD,CAAC;aAAM,CAAC;YACN,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAC3B,CAAC;IACH,CAAC;IAEM,IAAI;QACT,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAA;IAClD,CAAC;IAEM,KAAK;QACV,OAAO,OAAO,CAAC,OAAO,EAAE,CAAA;IAC1B,CAAC;CACF"}
|
package/esm/browser.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ export * from './filehandle.ts';
|
|
|
2
2
|
export { default as BlobFile } from './blobFile.ts';
|
|
3
3
|
export { default as RemoteFile } from './remoteFile.ts';
|
|
4
4
|
export declare class LocalFile {
|
|
5
|
-
readFile():
|
|
6
|
-
read():
|
|
7
|
-
|
|
5
|
+
readFile(): Promise<never>;
|
|
6
|
+
read(): Promise<never>;
|
|
7
|
+
stat(): Promise<never>;
|
|
8
|
+
close(): Promise<never>;
|
|
8
9
|
}
|
|
9
|
-
export { type GenericFilehandle } from './filehandle.ts';
|
package/esm/browser.js
CHANGED
|
@@ -4,13 +4,16 @@ export { default as BlobFile } from "./blobFile.js";
|
|
|
4
4
|
export { default as RemoteFile } from "./remoteFile.js";
|
|
5
5
|
export class LocalFile {
|
|
6
6
|
readFile() {
|
|
7
|
-
|
|
7
|
+
return Promise.reject(new Error('unimplemented'));
|
|
8
8
|
}
|
|
9
9
|
read() {
|
|
10
|
-
|
|
10
|
+
return Promise.reject(new Error('unimplemented'));
|
|
11
|
+
}
|
|
12
|
+
stat() {
|
|
13
|
+
return Promise.reject(new Error('unimplemented'));
|
|
11
14
|
}
|
|
12
15
|
close() {
|
|
13
|
-
|
|
16
|
+
return Promise.reject(new Error('unimplemented'));
|
|
14
17
|
}
|
|
15
18
|
}
|
|
16
19
|
//# sourceMappingURL=browser.js.map
|
package/esm/browser.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,cAAc,iBAAiB,CAAA;AAE/B,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAA;AACnD,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEvD,MAAM,OAAO,SAAS;IACpB,QAAQ;QACN,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,cAAc,iBAAiB,CAAA;AAE/B,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAA;AACnD,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAEvD,MAAM,OAAO,SAAS;IACpB,QAAQ;QACN,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAA;IACnD,CAAC;IACD,IAAI;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAA;IACnD,CAAC;IACD,IAAI;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAA;IACnD,CAAC;IACD,KAAK;QACH,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAA;IACnD,CAAC;CACF"}
|
package/esm/filehandle.d.ts
CHANGED
|
@@ -12,17 +12,10 @@ export interface Stats {
|
|
|
12
12
|
}
|
|
13
13
|
export interface GenericFilehandle {
|
|
14
14
|
read(length: number, position: number, opts?: FilehandleOptions): Promise<Uint8Array<ArrayBuffer>>;
|
|
15
|
-
readFile(): Promise<Uint8Array<ArrayBuffer>>;
|
|
16
|
-
readFile(options: BufferEncoding
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
})): Promise<Uint8Array<ArrayBuffer>>;
|
|
20
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
21
|
-
encoding: T;
|
|
22
|
-
}): Promise<string>;
|
|
23
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
24
|
-
encoding: T;
|
|
25
|
-
}): T extends BufferEncoding ? Promise<string> : Promise<Uint8Array<ArrayBuffer>>;
|
|
15
|
+
readFile(options?: Omit<FilehandleOptions, 'encoding'>): Promise<Uint8Array<ArrayBuffer>>;
|
|
16
|
+
readFile(options: BufferEncoding | (Omit<FilehandleOptions, 'encoding'> & {
|
|
17
|
+
encoding: BufferEncoding;
|
|
18
|
+
})): Promise<string>;
|
|
26
19
|
stat(): Promise<Stats>;
|
|
27
20
|
close(): Promise<void>;
|
|
28
21
|
}
|
package/esm/localFile.d.ts
CHANGED
|
@@ -3,17 +3,10 @@ export default class LocalFile implements GenericFilehandle {
|
|
|
3
3
|
private filename;
|
|
4
4
|
constructor(source: string);
|
|
5
5
|
read(length: number, position?: number): Promise<Uint8Array<ArrayBuffer>>;
|
|
6
|
-
readFile(): Promise<Uint8Array<ArrayBuffer>>;
|
|
7
|
-
readFile(options: BufferEncoding
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
})): Promise<Uint8Array<ArrayBuffer>>;
|
|
11
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
12
|
-
encoding: T;
|
|
13
|
-
}): Promise<string>;
|
|
14
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
15
|
-
encoding: T;
|
|
16
|
-
}): T extends BufferEncoding ? Promise<string> : Promise<Uint8Array<ArrayBuffer>>;
|
|
6
|
+
readFile(options?: Omit<FilehandleOptions, 'encoding'>): Promise<Uint8Array<ArrayBuffer>>;
|
|
7
|
+
readFile(options: BufferEncoding | (Omit<FilehandleOptions, 'encoding'> & {
|
|
8
|
+
encoding: BufferEncoding;
|
|
9
|
+
})): Promise<string>;
|
|
17
10
|
stat(): Promise<Stats>;
|
|
18
11
|
close(): Promise<void>;
|
|
19
12
|
}
|
package/esm/localFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localFile.js","sourceRoot":"","sources":["../src/localFile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AASlD,MAAM,CAAC,OAAO,OAAO,SAAS;IACpB,QAAQ,CAAQ;IAExB,YAAmB,MAAc;QAC/B,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAA;IACxB,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,MAAc,EAAE,QAAQ,GAAG,CAAC;QAC5C,IAAI,MAAM,KAAK,CAAC,EAAE,CAAC;YACjB,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;QAC1B,CAAC;QACD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAA;QAClC,IAAI,EAAE,CAAA;QACN,IAAI,CAAC;YACH,EAAE,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAA;YACnC,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAA;YACnD,OAAO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,EAAE,CAAA;gBAClB,CAAC;gBAAC,MAAM,CAAC;oBACP,sEAAsE;oBACtE,oDAAoD;gBACtD,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAUM,KAAK,CAAC,QAAQ,CACnB,OAA4C;QAE5C,OAAO,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;IACzC,CAAC;IAEM,KAAK,CAAC,IAAI;QACf,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC5B,CAAC;IAEM,KAAK,CAAC,KAAK;QAChB,gBAAgB;IAClB,CAAC;CACF"}
|
package/esm/remoteFile.d.ts
CHANGED
|
@@ -6,19 +6,13 @@ export default class RemoteFile implements GenericFilehandle {
|
|
|
6
6
|
private baseHeaders;
|
|
7
7
|
private baseOverrides;
|
|
8
8
|
constructor(source: string, opts?: FilehandleOptions);
|
|
9
|
+
private buildRequest;
|
|
9
10
|
fetch(input: RequestInfo, init: RequestInit | undefined): Promise<Response>;
|
|
10
11
|
read(length: number, position: number, opts?: FilehandleOptions): Promise<Uint8Array<ArrayBuffer>>;
|
|
11
|
-
readFile(): Promise<Uint8Array<ArrayBuffer>>;
|
|
12
|
-
readFile(options: BufferEncoding
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
})): Promise<Uint8Array<ArrayBuffer>>;
|
|
16
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
17
|
-
encoding: T;
|
|
18
|
-
}): Promise<string>;
|
|
19
|
-
readFile<T extends BufferEncoding>(options: Omit<FilehandleOptions, 'encoding'> & {
|
|
20
|
-
encoding: T;
|
|
21
|
-
}): T extends BufferEncoding ? Promise<string> : Promise<Uint8Array<ArrayBuffer>>;
|
|
12
|
+
readFile(options?: Omit<FilehandleOptions, 'encoding'>): Promise<Uint8Array<ArrayBuffer>>;
|
|
13
|
+
readFile(options: BufferEncoding | (Omit<FilehandleOptions, 'encoding'> & {
|
|
14
|
+
encoding: BufferEncoding;
|
|
15
|
+
})): Promise<string>;
|
|
22
16
|
stat(): Promise<Stats>;
|
|
23
17
|
close(): Promise<void>;
|
|
24
18
|
}
|
package/esm/remoteFile.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { toBytes } from "./util.js";
|
|
1
2
|
function getMessage(e) {
|
|
2
3
|
const r = typeof e === 'object' &&
|
|
3
4
|
e !== null &&
|
|
@@ -5,6 +6,7 @@ function getMessage(e) {
|
|
|
5
6
|
typeof e.message === 'string'
|
|
6
7
|
? e.message
|
|
7
8
|
: `${e}`;
|
|
9
|
+
// strip trailing period so the wrapped form `${msg} fetching ${url}` reads cleanly
|
|
8
10
|
return r.replace(/\.$/, '');
|
|
9
11
|
}
|
|
10
12
|
export default class RemoteFile {
|
|
@@ -17,9 +19,18 @@ export default class RemoteFile {
|
|
|
17
19
|
this.url = source;
|
|
18
20
|
this.baseHeaders = opts.headers ?? {};
|
|
19
21
|
this.baseOverrides = opts.overrides ?? {};
|
|
20
|
-
this.fetchImplementation =
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
this.fetchImplementation = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
23
|
+
}
|
|
24
|
+
buildRequest(opts, extraHeaders) {
|
|
25
|
+
return {
|
|
26
|
+
...this.baseOverrides,
|
|
27
|
+
...opts.overrides,
|
|
28
|
+
headers: { ...this.baseHeaders, ...opts.headers, ...extraHeaders },
|
|
29
|
+
method: 'GET',
|
|
30
|
+
redirect: 'follow',
|
|
31
|
+
mode: 'cors',
|
|
32
|
+
signal: opts.signal,
|
|
33
|
+
};
|
|
23
34
|
}
|
|
24
35
|
async fetch(input, init) {
|
|
25
36
|
const wrapError = (e) => new Error(`${getMessage(e)} fetching ${input}`, { cause: e });
|
|
@@ -57,17 +68,9 @@ export default class RemoteFile {
|
|
|
57
68
|
if (Number.isNaN(length) || Number.isNaN(position)) {
|
|
58
69
|
throw new TypeError(`read() called with NaN length or position (length=${length}, position=${position}). The index file may be corrupt.`);
|
|
59
70
|
}
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
...this.baseOverrides,
|
|
64
|
-
...overrides,
|
|
65
|
-
headers: { ...this.baseHeaders, ...headers },
|
|
66
|
-
method: 'GET',
|
|
67
|
-
redirect: 'follow',
|
|
68
|
-
mode: 'cors',
|
|
69
|
-
signal,
|
|
70
|
-
});
|
|
71
|
+
const res = await this.fetch(this.url, this.buildRequest(opts, {
|
|
72
|
+
range: `bytes=${position}-${position + length - 1}`,
|
|
73
|
+
}));
|
|
71
74
|
if (!res.ok) {
|
|
72
75
|
throw new Error(`HTTP ${res.status} fetching ${this.url}`);
|
|
73
76
|
}
|
|
@@ -80,10 +83,12 @@ export default class RemoteFile {
|
|
|
80
83
|
size: parseInt(sizeMatch[1], 10),
|
|
81
84
|
};
|
|
82
85
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
86
|
+
const resData = await toBytes(res);
|
|
87
|
+
// server didn't honor the range request and returned the full file —
|
|
88
|
+
// the body length is the actual file size
|
|
89
|
+
if (!this._stat && res.status === 200) {
|
|
90
|
+
this._stat = { size: resData.byteLength };
|
|
91
|
+
}
|
|
87
92
|
return resData.byteLength <= length
|
|
88
93
|
? resData
|
|
89
94
|
: resData.subarray(0, length);
|
|
@@ -95,16 +100,7 @@ export default class RemoteFile {
|
|
|
95
100
|
async readFile(options = {}) {
|
|
96
101
|
const encoding = typeof options === 'string' ? options : options.encoding;
|
|
97
102
|
const opts = typeof options === 'string' ? {} : options;
|
|
98
|
-
const
|
|
99
|
-
const res = await this.fetch(this.url, {
|
|
100
|
-
...this.baseOverrides,
|
|
101
|
-
...overrides,
|
|
102
|
-
headers: { ...this.baseHeaders, ...headers },
|
|
103
|
-
method: 'GET',
|
|
104
|
-
redirect: 'follow',
|
|
105
|
-
mode: 'cors',
|
|
106
|
-
signal,
|
|
107
|
-
});
|
|
103
|
+
const res = await this.fetch(this.url, this.buildRequest(opts));
|
|
108
104
|
if (!res.ok) {
|
|
109
105
|
throw new Error(`HTTP ${res.status} fetching ${this.url}`);
|
|
110
106
|
}
|
|
@@ -115,8 +111,7 @@ export default class RemoteFile {
|
|
|
115
111
|
throw new Error(`unsupported encoding: ${encoding}`);
|
|
116
112
|
}
|
|
117
113
|
else {
|
|
118
|
-
|
|
119
|
-
return res.bytes ? res.bytes() : new Uint8Array(await res.arrayBuffer());
|
|
114
|
+
return toBytes(res);
|
|
120
115
|
}
|
|
121
116
|
}
|
|
122
117
|
async stat() {
|
package/esm/remoteFile.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remoteFile.js","sourceRoot":"","sources":["../src/remoteFile.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"remoteFile.js","sourceRoot":"","sources":["../src/remoteFile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAUnC,SAAS,UAAU,CAAC,CAAU;IAC5B,MAAM,CAAC,GACL,OAAO,CAAC,KAAK,QAAQ;QACrB,CAAC,KAAK,IAAI;QACV,SAAS,IAAI,CAAC;QACd,OAAO,CAAC,CAAC,OAAO,KAAK,QAAQ;QAC3B,CAAC,CAAC,CAAC,CAAC,OAAO;QACX,CAAC,CAAC,GAAG,CAAC,EAAE,CAAA;IACZ,mFAAmF;IACnF,OAAO,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAA;AAC7B,CAAC;AAED,MAAM,CAAC,OAAO,OAAO,UAAU;IACnB,GAAG,CAAQ;IACb,KAAK,CAAQ;IACb,mBAAmB,CAAS;IAC5B,WAAW,CAAwB;IACnC,aAAa,CAA8B;IAEnD,YAAmB,MAAc,EAAE,OAA0B,EAAE;QAC7D,IAAI,CAAC,GAAG,GAAG,MAAM,CAAA;QACjB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,OAAO,IAAI,EAAE,CAAA;QACrC,IAAI,CA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C,CAAC,CAAA;QAC/D,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,QAAQ,GAAG,CAAC,MAAM,aAAa,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;QAC5D,CAAC;QACD,IAAI,QAAQ,KAAK,MAAM,EAAE,CAAC;YACxB,OAAO,GAAG,CAAC,IAAI,EAAE,CAAA;QACnB,CAAC;aAAM,IAAI,QAAQ,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,yBAAyB,QAAQ,EAAE,CAAC,CAAA;QACtD,CAAC;aAAM,CAAC;YACN,OAAO,OAAO,CAAC,GAAG,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;IAEM,KAAK,CAAC,IAAI;QACf,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;YAChB,MAAM,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;QACxB,CAAC;QACD,sEAAsE;QACtE,mDAAmD;QACnD,OAAO,IAAI,CAAC,KAAK,IAAI,EAAE,IAAI,EAAE,CAAC,EAAE,CAAA;IAClC,CAAC;IAEM,KAAK;QACV,OAAO,OAAO,CAAC,OAAO,EAAE,CAAA;IAC1B,CAAC;CACF"}
|
package/esm/util.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function toBytes(src: Response | Blob): Promise<Uint8Array<ArrayBuffer>>;
|
package/esm/util.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Response.bytes() / Blob.bytes() is widely available but not yet in all
|
|
2
|
+
// lib.dom.d.ts versions, so the optional-chain check is load-bearing for older
|
|
3
|
+
// runtimes despite TS thinking it's always defined.
|
|
4
|
+
export async function toBytes(src) {
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
6
|
+
return src.bytes ? src.bytes() : new Uint8Array(await src.arrayBuffer());
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=util.js.map
|
package/esm/util.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,+EAA+E;AAC/E,oDAAoD;AACpD,MAAM,CAAC,KAAK,UAAU,OAAO,CAC3B,GAAoB;IAEpB,uEAAuE;IACvE,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,WAAW,EAAE,CAAC,CAAA;AAC1E,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "generic-filehandle2",
|
|
3
3
|
"description": "uniform interface for accessing binary data from local files, remote HTTP resources, and browser Blob data",
|
|
4
|
-
"version": "2.1.
|
|
4
|
+
"version": "2.1.9",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -49,10 +49,10 @@
|
|
|
49
49
|
],
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@eslint/js": "^10.0.1",
|
|
52
|
-
"@types/node": "^25.
|
|
52
|
+
"@types/node": "^25.8.0",
|
|
53
53
|
"@types/range-parser": "^1.2.7",
|
|
54
|
-
"@vitest/coverage-v8": "^4.1.
|
|
55
|
-
"eslint": "^10.
|
|
54
|
+
"@vitest/coverage-v8": "^4.1.6",
|
|
55
|
+
"eslint": "^10.4.0",
|
|
56
56
|
"eslint-plugin-import-x": "^4.16.2",
|
|
57
57
|
"eslint-plugin-unicorn": "^64.0.0",
|
|
58
58
|
"prettier": "^3.8.3",
|
|
@@ -60,8 +60,8 @@
|
|
|
60
60
|
"rimraf": "^6.1.3",
|
|
61
61
|
"standard-changelog": "^7.0.1",
|
|
62
62
|
"typescript": "^6.0.3",
|
|
63
|
-
"typescript-eslint": "^8.59.
|
|
64
|
-
"vitest": "^4.1.
|
|
63
|
+
"typescript-eslint": "^8.59.3",
|
|
64
|
+
"vitest": "^4.1.6"
|
|
65
65
|
},
|
|
66
66
|
"publishConfig": {
|
|
67
67
|
"access": "public"
|
package/src/blobFile.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { toBytes } from './util.ts'
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
BufferEncoding,
|
|
3
5
|
FilehandleOptions,
|
|
@@ -24,26 +26,20 @@ export default class BlobFile implements GenericFilehandle {
|
|
|
24
26
|
): Promise<Uint8Array<ArrayBuffer>> {
|
|
25
27
|
// short-circuit a read of 0 bytes here, because browsers actually sometimes
|
|
26
28
|
// crash if you try to read 0 bytes from a local file!
|
|
27
|
-
if (
|
|
29
|
+
if (length === 0) {
|
|
28
30
|
return new Uint8Array(0)
|
|
29
31
|
}
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
33
|
-
return slice.bytes
|
|
34
|
-
? slice.bytes()
|
|
35
|
-
: new Uint8Array(await slice.arrayBuffer())
|
|
33
|
+
return toBytes(this.blob.slice(position, position + length))
|
|
36
34
|
}
|
|
37
35
|
|
|
38
|
-
public async readFile(
|
|
39
|
-
|
|
40
|
-
public async readFile<T extends undefined>(
|
|
41
|
-
options:
|
|
42
|
-
| Omit<FilehandleOptions, 'encoding'>
|
|
43
|
-
| (Omit<FilehandleOptions, 'encoding'> & { encoding: T }),
|
|
36
|
+
public async readFile(
|
|
37
|
+
options?: Omit<FilehandleOptions, 'encoding'>,
|
|
44
38
|
): Promise<Uint8Array<ArrayBuffer>>
|
|
45
|
-
public async readFile
|
|
46
|
-
options:
|
|
39
|
+
public async readFile(
|
|
40
|
+
options:
|
|
41
|
+
| BufferEncoding
|
|
42
|
+
| (Omit<FilehandleOptions, 'encoding'> & { encoding: BufferEncoding }),
|
|
47
43
|
): Promise<string>
|
|
48
44
|
public async readFile(
|
|
49
45
|
options?: FilehandleOptions | BufferEncoding,
|
|
@@ -54,10 +50,7 @@ export default class BlobFile implements GenericFilehandle {
|
|
|
54
50
|
} else if (encoding) {
|
|
55
51
|
throw new Error(`unsupported encoding: ${encoding}`)
|
|
56
52
|
} else {
|
|
57
|
-
|
|
58
|
-
return this.blob.bytes
|
|
59
|
-
? this.blob.bytes()
|
|
60
|
-
: new Uint8Array(await this.blob.arrayBuffer())
|
|
53
|
+
return toBytes(this.blob)
|
|
61
54
|
}
|
|
62
55
|
}
|
|
63
56
|
|
package/src/browser.ts
CHANGED
|
@@ -5,15 +5,16 @@ export { default as BlobFile } from './blobFile.ts'
|
|
|
5
5
|
export { default as RemoteFile } from './remoteFile.ts'
|
|
6
6
|
|
|
7
7
|
export class LocalFile {
|
|
8
|
-
readFile() {
|
|
9
|
-
|
|
8
|
+
readFile(): Promise<never> {
|
|
9
|
+
return Promise.reject(new Error('unimplemented'))
|
|
10
10
|
}
|
|
11
|
-
read() {
|
|
12
|
-
|
|
11
|
+
read(): Promise<never> {
|
|
12
|
+
return Promise.reject(new Error('unimplemented'))
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
stat(): Promise<never> {
|
|
15
|
+
return Promise.reject(new Error('unimplemented'))
|
|
16
|
+
}
|
|
17
|
+
close(): Promise<never> {
|
|
18
|
+
return Promise.reject(new Error('unimplemented'))
|
|
16
19
|
}
|
|
17
20
|
}
|
|
18
|
-
|
|
19
|
-
export { type GenericFilehandle } from './filehandle.ts'
|
package/src/filehandle.ts
CHANGED
|
@@ -37,21 +37,14 @@ export interface GenericFilehandle {
|
|
|
37
37
|
opts?: FilehandleOptions,
|
|
38
38
|
): Promise<Uint8Array<ArrayBuffer>>
|
|
39
39
|
|
|
40
|
-
readFile(
|
|
41
|
-
|
|
42
|
-
readFile<T extends undefined>(
|
|
43
|
-
options:
|
|
44
|
-
| Omit<FilehandleOptions, 'encoding'>
|
|
45
|
-
| (Omit<FilehandleOptions, 'encoding'> & { encoding: T }),
|
|
40
|
+
readFile(
|
|
41
|
+
options?: Omit<FilehandleOptions, 'encoding'>,
|
|
46
42
|
): Promise<Uint8Array<ArrayBuffer>>
|
|
47
|
-
readFile
|
|
48
|
-
options:
|
|
43
|
+
readFile(
|
|
44
|
+
options:
|
|
45
|
+
| BufferEncoding
|
|
46
|
+
| (Omit<FilehandleOptions, 'encoding'> & { encoding: BufferEncoding }),
|
|
49
47
|
): Promise<string>
|
|
50
|
-
readFile<T extends BufferEncoding>(
|
|
51
|
-
options: Omit<FilehandleOptions, 'encoding'> & { encoding: T },
|
|
52
|
-
): T extends BufferEncoding
|
|
53
|
-
? Promise<string>
|
|
54
|
-
: Promise<Uint8Array<ArrayBuffer>>
|
|
55
48
|
stat(): Promise<Stats>
|
|
56
49
|
close(): Promise<void>
|
|
57
50
|
}
|
package/src/localFile.ts
CHANGED
|
@@ -36,21 +36,14 @@ export default class LocalFile implements GenericFilehandle {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
public async readFile(
|
|
40
|
-
|
|
41
|
-
public async readFile<T extends undefined>(
|
|
42
|
-
options:
|
|
43
|
-
| Omit<FilehandleOptions, 'encoding'>
|
|
44
|
-
| (Omit<FilehandleOptions, 'encoding'> & { encoding: T }),
|
|
39
|
+
public async readFile(
|
|
40
|
+
options?: Omit<FilehandleOptions, 'encoding'>,
|
|
45
41
|
): Promise<Uint8Array<ArrayBuffer>>
|
|
46
|
-
public async readFile
|
|
47
|
-
options:
|
|
42
|
+
public async readFile(
|
|
43
|
+
options:
|
|
44
|
+
| BufferEncoding
|
|
45
|
+
| (Omit<FilehandleOptions, 'encoding'> & { encoding: BufferEncoding }),
|
|
48
46
|
): Promise<string>
|
|
49
|
-
readFile<T extends BufferEncoding>(
|
|
50
|
-
options: Omit<FilehandleOptions, 'encoding'> & { encoding: T },
|
|
51
|
-
): T extends BufferEncoding
|
|
52
|
-
? Promise<string>
|
|
53
|
-
: Promise<Uint8Array<ArrayBuffer>>
|
|
54
47
|
public async readFile(
|
|
55
48
|
options?: FilehandleOptions | BufferEncoding,
|
|
56
49
|
): Promise<Uint8Array<ArrayBuffer> | string> {
|
package/src/remoteFile.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { toBytes } from './util.ts'
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
BufferEncoding,
|
|
3
5
|
Fetcher,
|
|
@@ -14,6 +16,7 @@ function getMessage(e: unknown) {
|
|
|
14
16
|
typeof e.message === 'string'
|
|
15
17
|
? e.message
|
|
16
18
|
: `${e}`
|
|
19
|
+
// strip trailing period so the wrapped form `${msg} fetching ${url}` reads cleanly
|
|
17
20
|
return r.replace(/\.$/, '')
|
|
18
21
|
}
|
|
19
22
|
|
|
@@ -28,10 +31,22 @@ export default class RemoteFile implements GenericFilehandle {
|
|
|
28
31
|
this.url = source
|
|
29
32
|
this.baseHeaders = opts.headers ?? {}
|
|
30
33
|
this.baseOverrides = opts.overrides ?? {}
|
|
31
|
-
this.fetchImplementation =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
this.fetchImplementation = opts.fetch ?? globalThis.fetch.bind(globalThis)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private buildRequest(
|
|
38
|
+
opts: FilehandleOptions,
|
|
39
|
+
extraHeaders?: Record<string, string>,
|
|
40
|
+
): RequestInit {
|
|
41
|
+
return {
|
|
42
|
+
...this.baseOverrides,
|
|
43
|
+
...opts.overrides,
|
|
44
|
+
headers: { ...this.baseHeaders, ...opts.headers, ...extraHeaders },
|
|
45
|
+
method: 'GET',
|
|
46
|
+
redirect: 'follow',
|
|
47
|
+
mode: 'cors',
|
|
48
|
+
signal: opts.signal,
|
|
49
|
+
}
|
|
35
50
|
}
|
|
36
51
|
|
|
37
52
|
public async fetch(
|
|
@@ -41,7 +56,7 @@ export default class RemoteFile implements GenericFilehandle {
|
|
|
41
56
|
const wrapError = (e: unknown) =>
|
|
42
57
|
new Error(`${getMessage(e)} fetching ${input}`, { cause: e })
|
|
43
58
|
|
|
44
|
-
let response
|
|
59
|
+
let response: Response
|
|
45
60
|
try {
|
|
46
61
|
response = await this.fetchImplementation(input, init)
|
|
47
62
|
} catch (e) {
|
|
@@ -81,17 +96,12 @@ export default class RemoteFile implements GenericFilehandle {
|
|
|
81
96
|
`read() called with NaN length or position (length=${length}, position=${position}). The index file may be corrupt.`,
|
|
82
97
|
)
|
|
83
98
|
}
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
method: 'GET',
|
|
91
|
-
redirect: 'follow',
|
|
92
|
-
mode: 'cors',
|
|
93
|
-
signal,
|
|
94
|
-
})
|
|
99
|
+
const res = await this.fetch(
|
|
100
|
+
this.url,
|
|
101
|
+
this.buildRequest(opts, {
|
|
102
|
+
range: `bytes=${position}-${position + length - 1}`,
|
|
103
|
+
}),
|
|
104
|
+
)
|
|
95
105
|
|
|
96
106
|
if (!res.ok) {
|
|
97
107
|
throw new Error(`HTTP ${res.status} fetching ${this.url}`)
|
|
@@ -107,10 +117,12 @@ export default class RemoteFile implements GenericFilehandle {
|
|
|
107
117
|
}
|
|
108
118
|
}
|
|
109
119
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
120
|
+
const resData = await toBytes(res)
|
|
121
|
+
// server didn't honor the range request and returned the full file —
|
|
122
|
+
// the body length is the actual file size
|
|
123
|
+
if (!this._stat && res.status === 200) {
|
|
124
|
+
this._stat = { size: resData.byteLength }
|
|
125
|
+
}
|
|
114
126
|
return resData.byteLength <= length
|
|
115
127
|
? resData
|
|
116
128
|
: resData.subarray(0, length)
|
|
@@ -123,36 +135,20 @@ export default class RemoteFile implements GenericFilehandle {
|
|
|
123
135
|
)
|
|
124
136
|
}
|
|
125
137
|
|
|
126
|
-
public async readFile(
|
|
127
|
-
|
|
128
|
-
public async readFile<T extends undefined>(
|
|
129
|
-
options:
|
|
130
|
-
| Omit<FilehandleOptions, 'encoding'>
|
|
131
|
-
| (Omit<FilehandleOptions, 'encoding'> & { encoding: T }),
|
|
138
|
+
public async readFile(
|
|
139
|
+
options?: Omit<FilehandleOptions, 'encoding'>,
|
|
132
140
|
): Promise<Uint8Array<ArrayBuffer>>
|
|
133
|
-
public async readFile
|
|
134
|
-
options:
|
|
141
|
+
public async readFile(
|
|
142
|
+
options:
|
|
143
|
+
| BufferEncoding
|
|
144
|
+
| (Omit<FilehandleOptions, 'encoding'> & { encoding: BufferEncoding }),
|
|
135
145
|
): Promise<string>
|
|
136
|
-
readFile<T extends BufferEncoding>(
|
|
137
|
-
options: Omit<FilehandleOptions, 'encoding'> & { encoding: T },
|
|
138
|
-
): T extends BufferEncoding
|
|
139
|
-
? Promise<string>
|
|
140
|
-
: Promise<Uint8Array<ArrayBuffer>>
|
|
141
146
|
public async readFile(
|
|
142
147
|
options: FilehandleOptions | BufferEncoding = {},
|
|
143
148
|
): Promise<Uint8Array<ArrayBuffer> | string> {
|
|
144
149
|
const encoding = typeof options === 'string' ? options : options.encoding
|
|
145
150
|
const opts = typeof options === 'string' ? {} : options
|
|
146
|
-
const
|
|
147
|
-
const res = await this.fetch(this.url, {
|
|
148
|
-
...this.baseOverrides,
|
|
149
|
-
...overrides,
|
|
150
|
-
headers: { ...this.baseHeaders, ...headers },
|
|
151
|
-
method: 'GET',
|
|
152
|
-
redirect: 'follow',
|
|
153
|
-
mode: 'cors',
|
|
154
|
-
signal,
|
|
155
|
-
})
|
|
151
|
+
const res = await this.fetch(this.url, this.buildRequest(opts))
|
|
156
152
|
if (!res.ok) {
|
|
157
153
|
throw new Error(`HTTP ${res.status} fetching ${this.url}`)
|
|
158
154
|
}
|
|
@@ -161,8 +157,7 @@ export default class RemoteFile implements GenericFilehandle {
|
|
|
161
157
|
} else if (encoding) {
|
|
162
158
|
throw new Error(`unsupported encoding: ${encoding}`)
|
|
163
159
|
} else {
|
|
164
|
-
|
|
165
|
-
return res.bytes ? res.bytes() : new Uint8Array(await res.arrayBuffer())
|
|
160
|
+
return toBytes(res)
|
|
166
161
|
}
|
|
167
162
|
}
|
|
168
163
|
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Response.bytes() / Blob.bytes() is widely available but not yet in all
|
|
2
|
+
// lib.dom.d.ts versions, so the optional-chain check is load-bearing for older
|
|
3
|
+
// runtimes despite TS thinking it's always defined.
|
|
4
|
+
export async function toBytes(
|
|
5
|
+
src: Response | Blob,
|
|
6
|
+
): Promise<Uint8Array<ArrayBuffer>> {
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
8
|
+
return src.bytes ? src.bytes() : new Uint8Array(await src.arrayBuffer())
|
|
9
|
+
}
|