@d0paminedriven/fs 10.1.0 → 11.0.0
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/docs/index.d.ts +119 -0
- package/dist/docs/index.js +1027 -0
- package/dist/extract/client.d.ts +54 -0
- package/dist/extract/client.js +118 -0
- package/dist/extract/index.d.ts +69 -0
- package/dist/extract/index.js +622 -0
- package/dist/fs/index.d.ts +19 -55
- package/dist/fs/index.js +88 -19
- package/dist/fs-atomic/index.d.ts +28 -36
- package/dist/fs-atomic/index.js +88 -13
- package/dist/fs-base/index.d.ts +105 -111
- package/dist/fs-base/index.js +188 -12
- package/dist/fs-core/index.d.ts +20 -24
- package/dist/fs-core/index.js +44 -16
- package/dist/fs-fetch/index.d.ts +48 -62
- package/dist/fs-fetch/index.js +146 -18
- package/dist/fs-size/index.d.ts +16 -28
- package/dist/fs-size/index.js +64 -15
- package/dist/fs-tmp/index.d.ts +179 -192
- package/dist/fs-tmp/index.js +321 -17
- package/dist/images/index.d.ts +447 -0
- package/dist/images/index.js +899 -0
- package/dist/images/workup.d.ts +30 -0
- package/dist/images/workup.js +317 -0
- package/dist/index.d.ts +20 -22
- package/dist/index.js +20 -64
- package/dist/ld/index.d.ts +15 -13
- package/dist/ld/index.js +56 -6
- package/dist/mime/index.d.ts +299 -294
- package/dist/mime/index.js +348 -13
- package/dist/mixins/index.d.ts +34 -0
- package/dist/mixins/index.js +54 -0
- package/dist/types/index.d.ts +279 -215
- package/dist/types/index.js +11 -6
- package/dist/types/stream.d.ts +6 -4
- package/dist/types/stream.js +1 -1
- package/dist/url/index.d.ts +15 -18
- package/dist/url/index.js +21 -8
- package/dist/utils/index.d.ts +19 -23
- package/dist/utils/index.js +82 -7
- package/package.json +33 -13
- package/dist/chunk-4D2YBCI6.js +0 -163
- package/dist/chunk-5CACVZVF.js +0 -386
- package/dist/chunk-5H6GE5AO.js +0 -14
- package/dist/chunk-7QVYU63E.js +0 -7
- package/dist/chunk-DNR5ZI2T.js +0 -75
- package/dist/chunk-JZ6JNZZ6.js +0 -77
- package/dist/chunk-L2QMQOEJ.js +0 -366
- package/dist/chunk-NJHN5OE3.js +0 -33
- package/dist/chunk-OHKRQJZH.js +0 -82
- package/dist/chunk-OT5JEQKW.js +0 -139
- package/dist/chunk-PATMSIQK.js +0 -367
- package/dist/chunk-PB5A7ZFD.js +0 -79
- package/dist/chunk-VDKUXJE7.js +0 -240
- package/dist/chunk-Y5QWE4EG.js +0 -1335
- package/dist/chunk-YRCJWFZD.js +0 -107
- package/dist/image/index.d.ts +0 -182
- package/dist/image/index.js +0 -13
- package/dist/image/workup.d.ts +0 -40
- package/dist/image/workup.js +0 -12
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
import { DocMixin, ImgMixin } from "../mixins/index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/extract/index.ts
|
|
4
|
+
var Base = class {
|
|
5
|
+
constructor(_opts) {}
|
|
6
|
+
};
|
|
7
|
+
const Unified = ImgMixin(DocMixin(Base));
|
|
8
|
+
var Extract = class extends Unified {
|
|
9
|
+
badUrls = /* @__PURE__ */ new Map();
|
|
10
|
+
originFallback;
|
|
11
|
+
invalidateCF;
|
|
12
|
+
quarantineTtl;
|
|
13
|
+
ua;
|
|
14
|
+
debug;
|
|
15
|
+
constructor(opts) {
|
|
16
|
+
super(opts);
|
|
17
|
+
this.originFallback = opts?.originFallback;
|
|
18
|
+
this.invalidateCF = opts?.invalidateCloudFrontKey;
|
|
19
|
+
this.quarantineTtl = opts?.quarantineTtlMs ?? 360 * 60 * 1e3;
|
|
20
|
+
this.ua = opts?.userAgent ?? "slipstream-extractor/1.0";
|
|
21
|
+
this.debug = !!opts?.debug;
|
|
22
|
+
}
|
|
23
|
+
fmt(msg) {
|
|
24
|
+
return `[extractor ${new Date(Date.now()).toISOString()}]: ${msg}`;
|
|
25
|
+
}
|
|
26
|
+
log(msg) {
|
|
27
|
+
if (msg instanceof Error) return this.fmt(msg.message);
|
|
28
|
+
if (Array.isArray(msg)) return this.fmt(JSON.stringify(msg, null, 2));
|
|
29
|
+
if (msg instanceof RangeError) return this.fmt(msg.message);
|
|
30
|
+
switch (typeof msg) {
|
|
31
|
+
case "bigint": return this.fmt(Number(msg).toPrecision(14));
|
|
32
|
+
case "boolean": return this.fmt(msg);
|
|
33
|
+
case "number": return this.fmt(msg.toPrecision(14));
|
|
34
|
+
case "object": return this.fmt(JSON.stringify(msg, Object.getOwnPropertyNames(msg), 2));
|
|
35
|
+
case "string": return this.fmt(msg);
|
|
36
|
+
case "symbol": return this.fmt(msg.toString());
|
|
37
|
+
case "undefined": return this.fmt(`msg is undefined...`);
|
|
38
|
+
case "function": return this.fmt(msg.name);
|
|
39
|
+
default: return this.fmt("something went wrong...");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
dlog(...args) {
|
|
43
|
+
if (!this.debug) return;
|
|
44
|
+
this.log(args);
|
|
45
|
+
}
|
|
46
|
+
isQuarantined(url) {
|
|
47
|
+
const t = this.badUrls.get(url);
|
|
48
|
+
if (!t) return false;
|
|
49
|
+
if (t < Date.now()) {
|
|
50
|
+
this.badUrls.delete(url);
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
markQuarantined(url, ttl = this.quarantineTtl) {
|
|
56
|
+
this.badUrls.set(url, Date.now() + ttl);
|
|
57
|
+
}
|
|
58
|
+
sniffMime(buf) {
|
|
59
|
+
if (buf.length >= 8 && buf.subarray(0, 8).equals(Buffer.from([
|
|
60
|
+
137,
|
|
61
|
+
80,
|
|
62
|
+
78,
|
|
63
|
+
71,
|
|
64
|
+
13,
|
|
65
|
+
10,
|
|
66
|
+
26,
|
|
67
|
+
10
|
|
68
|
+
]))) return "image/png";
|
|
69
|
+
if (buf.length >= 4 && buf[0] === 255 && buf[1] === 216 && buf[2] === 255) return "image/jpeg";
|
|
70
|
+
if (buf.length >= 12 && buf.toString("ascii", 0, 4) === "RIFF" && buf.toString("ascii", 8, 12) === "WEBP") return "image/webp";
|
|
71
|
+
if (buf.length >= 6 && buf.toString("ascii", 0, 3) === "GIF") return "image/gif";
|
|
72
|
+
if (buf.length >= 5 && buf.toString("ascii", 0, 5) === "%PDF-") return "application/pdf";
|
|
73
|
+
}
|
|
74
|
+
async probeFirstChunk(url, bytes = 65536, deadlineMs = 2500, idleMs = 1200) {
|
|
75
|
+
const ctrl = new AbortController();
|
|
76
|
+
const deadline = setTimeout(() => ctrl.abort(), deadlineMs);
|
|
77
|
+
try {
|
|
78
|
+
this.dlog("probeFirstChunk:start", {
|
|
79
|
+
url,
|
|
80
|
+
bytes,
|
|
81
|
+
deadlineMs,
|
|
82
|
+
idleMs
|
|
83
|
+
});
|
|
84
|
+
const res = await fetch(url, {
|
|
85
|
+
headers: {
|
|
86
|
+
Range: `bytes=0-${bytes - 1}`,
|
|
87
|
+
"Accept-Encoding": "identity",
|
|
88
|
+
"User-Agent": this.ua
|
|
89
|
+
},
|
|
90
|
+
signal: ctrl.signal
|
|
91
|
+
});
|
|
92
|
+
if (!(res.ok || res.status === 206)) return {
|
|
93
|
+
ok: false,
|
|
94
|
+
reason: `HTTP_${res.status}`,
|
|
95
|
+
status: res.status
|
|
96
|
+
};
|
|
97
|
+
if (!res.body) return {
|
|
98
|
+
ok: false,
|
|
99
|
+
reason: "NO_BODY",
|
|
100
|
+
status: res.status
|
|
101
|
+
};
|
|
102
|
+
const reader = res.body.getReader();
|
|
103
|
+
const chunks = [];
|
|
104
|
+
let total = 0;
|
|
105
|
+
try {
|
|
106
|
+
for (;;) {
|
|
107
|
+
const read = reader.read();
|
|
108
|
+
const idle = new Promise((_, rej) => setTimeout(() => rej(/* @__PURE__ */ new Error("IDLE")), idleMs));
|
|
109
|
+
const { done, value } = await Promise.race([read, idle]);
|
|
110
|
+
if (done) break;
|
|
111
|
+
const n = Math.min(value.length, bytes - total);
|
|
112
|
+
chunks.push(value.subarray(0, n));
|
|
113
|
+
total += n;
|
|
114
|
+
if (total >= bytes) break;
|
|
115
|
+
}
|
|
116
|
+
} finally {
|
|
117
|
+
try {
|
|
118
|
+
reader.cancel();
|
|
119
|
+
} catch (e) {
|
|
120
|
+
console.error(e);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
this.dlog("probeFirstChunk:done", {
|
|
124
|
+
url,
|
|
125
|
+
status: res.status,
|
|
126
|
+
total
|
|
127
|
+
});
|
|
128
|
+
return {
|
|
129
|
+
ok: true,
|
|
130
|
+
buf: Buffer.concat(chunks, total),
|
|
131
|
+
status: res.status,
|
|
132
|
+
headers: res.headers
|
|
133
|
+
};
|
|
134
|
+
} catch (e) {
|
|
135
|
+
this.dlog("probeFirstChunk:error", {
|
|
136
|
+
url,
|
|
137
|
+
error: e instanceof Error ? e.message : e
|
|
138
|
+
});
|
|
139
|
+
return {
|
|
140
|
+
ok: false,
|
|
141
|
+
reason: e instanceof Error ? e?.name === "AbortError" ? "DEADLINE" : JSON.stringify(e?.message ?? e.stack) : "error in extractor"
|
|
142
|
+
};
|
|
143
|
+
} finally {
|
|
144
|
+
clearTimeout(deadline);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
parseContentRangeTotal(h) {
|
|
148
|
+
const m = h?.match(/\/(\d+)$/);
|
|
149
|
+
return m?.[1] ? parseInt(m[1], 10) : 0;
|
|
150
|
+
}
|
|
151
|
+
async head(url, timeout) {
|
|
152
|
+
return this.fetchWithDeadline(url, {
|
|
153
|
+
method: "HEAD",
|
|
154
|
+
headers: {
|
|
155
|
+
"Accept-Encoding": "identity",
|
|
156
|
+
"User-Agent": this.ua
|
|
157
|
+
}
|
|
158
|
+
}, timeout);
|
|
159
|
+
}
|
|
160
|
+
async fetchWithDeadline(url, init, deadlineMs) {
|
|
161
|
+
const ctrl = new AbortController();
|
|
162
|
+
const timer = setTimeout(() => ctrl.abort(), deadlineMs);
|
|
163
|
+
try {
|
|
164
|
+
const method = (init.method ?? "GET").toString();
|
|
165
|
+
const range = init.headers?.["Range"] ?? (init.headers instanceof Headers ? init.headers.get("Range") : void 0);
|
|
166
|
+
this.dlog("fetch:start", {
|
|
167
|
+
url,
|
|
168
|
+
method,
|
|
169
|
+
deadlineMs,
|
|
170
|
+
range
|
|
171
|
+
});
|
|
172
|
+
const res = await fetch(url, {
|
|
173
|
+
...init,
|
|
174
|
+
signal: ctrl.signal
|
|
175
|
+
});
|
|
176
|
+
this.dlog("fetch:done", {
|
|
177
|
+
url,
|
|
178
|
+
method,
|
|
179
|
+
status: res.status
|
|
180
|
+
});
|
|
181
|
+
return res;
|
|
182
|
+
} finally {
|
|
183
|
+
clearTimeout(timer);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async readFirstBytesWithIdle(res, maxBytes, idleMs) {
|
|
187
|
+
if (!res.body) throw new Error("No response body");
|
|
188
|
+
const reader = res.body.getReader();
|
|
189
|
+
const chunks = [];
|
|
190
|
+
let total = 0;
|
|
191
|
+
try {
|
|
192
|
+
this.dlog("stream:first-bytes:start", {
|
|
193
|
+
maxBytes,
|
|
194
|
+
idleMs
|
|
195
|
+
});
|
|
196
|
+
for (;;) {
|
|
197
|
+
const read = reader.read();
|
|
198
|
+
const idle = new Promise((_, rej) => setTimeout(() => rej(/* @__PURE__ */ new Error("IDLE")), idleMs));
|
|
199
|
+
const { done, value } = await Promise.race([read, idle]);
|
|
200
|
+
if (done) break;
|
|
201
|
+
const n = Math.min(value.length, maxBytes - total);
|
|
202
|
+
chunks.push(value.subarray(0, n));
|
|
203
|
+
total += n;
|
|
204
|
+
if (total >= maxBytes) break;
|
|
205
|
+
}
|
|
206
|
+
const out = Buffer.concat(chunks, total);
|
|
207
|
+
this.dlog("stream:first-bytes:done", { total });
|
|
208
|
+
return out;
|
|
209
|
+
} finally {
|
|
210
|
+
try {
|
|
211
|
+
reader.cancel();
|
|
212
|
+
} catch (e) {
|
|
213
|
+
console.error(e);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
async fetchMinimalBuffer(url, size = 16384, timeout = 5e3) {
|
|
218
|
+
if (this.isQuarantined(url)) throw new Error(`QUARANTINED: ${url}`);
|
|
219
|
+
this.dlog("fetchMinimalBuffer:probe:start", {
|
|
220
|
+
url,
|
|
221
|
+
size,
|
|
222
|
+
timeout
|
|
223
|
+
});
|
|
224
|
+
const probe = await this.probeFirstChunk(url, Math.max(size, 65536), Math.min(timeout, 2500), Math.min(1200, Math.max(600, Math.floor(timeout * .6))));
|
|
225
|
+
if (!probe.ok) {
|
|
226
|
+
this.dlog("fetchMinimalBuffer:probe:fail", {
|
|
227
|
+
url,
|
|
228
|
+
reason: probe.reason
|
|
229
|
+
});
|
|
230
|
+
if (this.originFallback) try {
|
|
231
|
+
const originUrl = await this.originFallback(url);
|
|
232
|
+
this.dlog("fetchMinimalBuffer:probe:originFallback", {
|
|
233
|
+
url,
|
|
234
|
+
originUrl
|
|
235
|
+
});
|
|
236
|
+
const alt = await this.probeFirstChunk(originUrl, Math.max(size, 65536), 2500, 1200);
|
|
237
|
+
if (alt.ok) {
|
|
238
|
+
this.dlog("fetchMinimalBuffer:probe:originSuccess", {
|
|
239
|
+
url,
|
|
240
|
+
originUrl
|
|
241
|
+
});
|
|
242
|
+
if (this.invalidateCF) try {
|
|
243
|
+
await this.invalidateCF(url);
|
|
244
|
+
} catch (e) {
|
|
245
|
+
this.dlog("fetchMinimalBuffer:invalidateCF:error", {
|
|
246
|
+
url,
|
|
247
|
+
error: e instanceof Error ? e.message : e
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
const length = alt.headers.get("Content-Length");
|
|
251
|
+
const buffer = alt.buf;
|
|
252
|
+
return {
|
|
253
|
+
contentType: this.sniffMime(alt.buf) ?? alt.headers.get("content-type") ?? void 0,
|
|
254
|
+
buffer,
|
|
255
|
+
source: originUrl,
|
|
256
|
+
reportedTotalBytes: buffer.length,
|
|
257
|
+
fetchedBytes: length ? Number.parseInt(length) : void 0
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
} catch {}
|
|
261
|
+
this.markQuarantined(url);
|
|
262
|
+
throw new Error(`PROBE_FAIL ${probe.reason}`);
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
this.dlog("fetchMinimalBuffer:probe:ok", {
|
|
266
|
+
url,
|
|
267
|
+
status: probe.status
|
|
268
|
+
});
|
|
269
|
+
const headers = probe.headers;
|
|
270
|
+
const contentType = (headers.get("content-type") ?? void 0)?.split(";")?.[0] ?? this.sniffMime(probe.buf);
|
|
271
|
+
const supportsRange = probe.status === 206;
|
|
272
|
+
let contentLength = supportsRange ? this.parseContentRangeTotal(headers.get("content-range")) : 0;
|
|
273
|
+
this.dlog("fetchMinimalBuffer:post-probe", {
|
|
274
|
+
url,
|
|
275
|
+
contentType,
|
|
276
|
+
supportsRange,
|
|
277
|
+
contentLength
|
|
278
|
+
});
|
|
279
|
+
if (!supportsRange) try {
|
|
280
|
+
const head = await this.head(url, Math.min(timeout, 2500));
|
|
281
|
+
const ce = head.headers.get("content-encoding") ?? "";
|
|
282
|
+
if (/gzip|br|deflate/i.test(ce)) this.markQuarantined(url, 1800 * 1e3);
|
|
283
|
+
contentLength = parseInt(head.headers.get("content-length") ?? "0");
|
|
284
|
+
this.dlog("fetchMinimalBuffer:head", {
|
|
285
|
+
url,
|
|
286
|
+
contentLength,
|
|
287
|
+
contentEncoding: ce
|
|
288
|
+
});
|
|
289
|
+
} catch {}
|
|
290
|
+
if (contentType && (contentType === "application/pdf" || contentType === "image/tiff") && supportsRange && contentLength > size * 2) {
|
|
291
|
+
const isTiff = contentType === "image/tiff";
|
|
292
|
+
const startSize = isTiff ? Math.min(32768, size) : Math.min(16384, size);
|
|
293
|
+
const endSize = isTiff ? Math.min(32768, contentLength) : Math.min(4e5, contentLength);
|
|
294
|
+
const endStart = Math.max(0, contentLength - endSize);
|
|
295
|
+
const [startRes, endRes] = await Promise.all([this.fetchWithDeadline(url, { headers: {
|
|
296
|
+
Range: `bytes=0-${startSize - 1}`,
|
|
297
|
+
"Accept-Encoding": "identity",
|
|
298
|
+
"User-Agent": this.ua
|
|
299
|
+
} }, timeout), this.fetchWithDeadline(url, { headers: {
|
|
300
|
+
Range: `bytes=${endStart}-${contentLength - 1}`,
|
|
301
|
+
"Accept-Encoding": "identity",
|
|
302
|
+
"User-Agent": this.ua
|
|
303
|
+
} }, timeout)]);
|
|
304
|
+
if (startRes.status === 206 && endRes.status === 206) {
|
|
305
|
+
const [startBuf, endBuf] = await Promise.all([startRes.arrayBuffer(), endRes.arrayBuffer()]);
|
|
306
|
+
this.dlog(`fetchMinimalBuffer:${contentType === "image/tiff" ? "tiff" : "pdf"}:ranged`, {
|
|
307
|
+
url,
|
|
308
|
+
startSize,
|
|
309
|
+
endSize
|
|
310
|
+
});
|
|
311
|
+
const buffer = Buffer.concat([Buffer.from(startBuf), Buffer.from(endBuf)]);
|
|
312
|
+
return {
|
|
313
|
+
contentType,
|
|
314
|
+
buffer,
|
|
315
|
+
source: url,
|
|
316
|
+
reportedTotalBytes: contentLength ?? void 0,
|
|
317
|
+
fetchedBytes: buffer.length,
|
|
318
|
+
totalFileSize: contentType === "image/tiff" ? contentLength : void 0
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (supportsRange) {
|
|
323
|
+
const rr = await this.fetchWithDeadline(url, { headers: {
|
|
324
|
+
Range: `bytes=0-${size - 1}`,
|
|
325
|
+
"Accept-Encoding": "identity",
|
|
326
|
+
"User-Agent": this.ua
|
|
327
|
+
} }, timeout);
|
|
328
|
+
if (rr.status === 206) {
|
|
329
|
+
const ab = await rr.arrayBuffer();
|
|
330
|
+
const buffer = Buffer.from(ab);
|
|
331
|
+
this.dlog("fetchMinimalBuffer:range:first-bytes", {
|
|
332
|
+
url,
|
|
333
|
+
size
|
|
334
|
+
});
|
|
335
|
+
return {
|
|
336
|
+
contentType,
|
|
337
|
+
buffer,
|
|
338
|
+
source: url,
|
|
339
|
+
reportedTotalBytes: contentLength ?? void 0,
|
|
340
|
+
fetchedBytes: buffer.length
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (contentLength && contentLength < 1 * 1024 * 1024) {
|
|
345
|
+
const ab = await (await this.fetchWithDeadline(url, { headers: {
|
|
346
|
+
"Accept-Encoding": "identity",
|
|
347
|
+
"User-Agent": this.ua
|
|
348
|
+
} }, timeout)).arrayBuffer();
|
|
349
|
+
const buffer = Buffer.from(ab);
|
|
350
|
+
this.dlog("fetchMinimalBuffer:get:small", {
|
|
351
|
+
url,
|
|
352
|
+
contentLength
|
|
353
|
+
});
|
|
354
|
+
return {
|
|
355
|
+
contentType,
|
|
356
|
+
buffer,
|
|
357
|
+
source: url,
|
|
358
|
+
reportedTotalBytes: contentLength ?? void 0,
|
|
359
|
+
fetchedBytes: buffer.length
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
const res = await this.fetchWithDeadline(url, { headers: {
|
|
363
|
+
"Accept-Encoding": "identity",
|
|
364
|
+
"User-Agent": this.ua
|
|
365
|
+
} }, timeout);
|
|
366
|
+
const buf = await this.readFirstBytesWithIdle(res, size, Math.min(1500, Math.max(600, Math.floor(timeout * .6))));
|
|
367
|
+
this.dlog("fetchMinimalBuffer:stream:first-bytes", {
|
|
368
|
+
url,
|
|
369
|
+
size
|
|
370
|
+
});
|
|
371
|
+
return {
|
|
372
|
+
contentType,
|
|
373
|
+
buffer: buf,
|
|
374
|
+
source: url,
|
|
375
|
+
reportedTotalBytes: contentLength ?? void 0,
|
|
376
|
+
fetchedBytes: buf.length
|
|
377
|
+
};
|
|
378
|
+
} catch (e) {
|
|
379
|
+
this.dlog("fetchMinimalBuffer:error", {
|
|
380
|
+
url,
|
|
381
|
+
error: e instanceof Error ? e.message : e
|
|
382
|
+
});
|
|
383
|
+
this.markQuarantined(url);
|
|
384
|
+
throw e;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
async extractRemote(source, size = 16384, timeout = 5e3) {
|
|
388
|
+
let buffer, contentType, url, fetchedBytes, reportedTotalBytes;
|
|
389
|
+
if (Buffer.isBuffer(source)) {
|
|
390
|
+
buffer = source;
|
|
391
|
+
contentType = this.sniffMime(buffer);
|
|
392
|
+
fetchedBytes = buffer.length;
|
|
393
|
+
reportedTotalBytes = buffer.length;
|
|
394
|
+
} else {
|
|
395
|
+
this.dlog("extractRemote:start", {
|
|
396
|
+
url: source,
|
|
397
|
+
size,
|
|
398
|
+
timeout
|
|
399
|
+
});
|
|
400
|
+
const out = await this.fetchMinimalBuffer(source, size, timeout);
|
|
401
|
+
buffer = out.buffer;
|
|
402
|
+
contentType = out.contentType;
|
|
403
|
+
url = out.source;
|
|
404
|
+
fetchedBytes = out.fetchedBytes;
|
|
405
|
+
reportedTotalBytes = out.reportedTotalBytes;
|
|
406
|
+
this.dlog("extractRemote:buf", {
|
|
407
|
+
url,
|
|
408
|
+
contentType,
|
|
409
|
+
bytes: buffer.length
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
if (contentType?.startsWith("image/")) try {
|
|
413
|
+
this.dlog("imageWorkup:start", {
|
|
414
|
+
url,
|
|
415
|
+
size,
|
|
416
|
+
contentType
|
|
417
|
+
});
|
|
418
|
+
const workupSize = contentType === "image/tiff" ? buffer.length : size;
|
|
419
|
+
const imgSpecs = this.img.getImageSpecsWorkup(buffer, workupSize);
|
|
420
|
+
this.dlog("imageWorkup:done", {
|
|
421
|
+
url,
|
|
422
|
+
w: imgSpecs.width,
|
|
423
|
+
h: imgSpecs.height,
|
|
424
|
+
fmt: imgSpecs.format
|
|
425
|
+
});
|
|
426
|
+
return {
|
|
427
|
+
source: url ?? "buffer",
|
|
428
|
+
byteSize: reportedTotalBytes,
|
|
429
|
+
contentType,
|
|
430
|
+
fetchedBytes,
|
|
431
|
+
...imgSpecs
|
|
432
|
+
};
|
|
433
|
+
} catch {
|
|
434
|
+
this.dlog("imageWorkup:fallback", {
|
|
435
|
+
url,
|
|
436
|
+
contentType
|
|
437
|
+
});
|
|
438
|
+
let dims = null;
|
|
439
|
+
let format;
|
|
440
|
+
switch (contentType) {
|
|
441
|
+
case "image/png":
|
|
442
|
+
dims = this.sniffPngDims(buffer);
|
|
443
|
+
format = "png";
|
|
444
|
+
break;
|
|
445
|
+
case "image/jpeg":
|
|
446
|
+
dims = this.sniffJpegDims(buffer);
|
|
447
|
+
format = "jpeg";
|
|
448
|
+
break;
|
|
449
|
+
case "image/webp":
|
|
450
|
+
dims = this.sniffWebpDims(buffer);
|
|
451
|
+
format = "webp";
|
|
452
|
+
break;
|
|
453
|
+
case "image/gif":
|
|
454
|
+
dims = this.sniffGifDims(buffer);
|
|
455
|
+
format = "gif";
|
|
456
|
+
break;
|
|
457
|
+
case "image/heic":
|
|
458
|
+
dims = this.sniffHeic(buffer);
|
|
459
|
+
format = "heic";
|
|
460
|
+
break;
|
|
461
|
+
case "image/tiff": {
|
|
462
|
+
const truncated = buffer.subarray(0, Math.min(buffer.length, 4096 * 6));
|
|
463
|
+
dims = this.sniffTiff(buffer, truncated) ?? null;
|
|
464
|
+
format = "tiff";
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (dims && format) {
|
|
469
|
+
const { width, height } = dims;
|
|
470
|
+
return {
|
|
471
|
+
type: "IMAGE",
|
|
472
|
+
source: url ?? "buffer",
|
|
473
|
+
width,
|
|
474
|
+
height,
|
|
475
|
+
format,
|
|
476
|
+
frames: 1,
|
|
477
|
+
animated: false,
|
|
478
|
+
hasAlpha: null,
|
|
479
|
+
orientation: null,
|
|
480
|
+
aspectRatio: height ? width / height : 0,
|
|
481
|
+
colorModel: "unknown",
|
|
482
|
+
colorSpace: "unknown",
|
|
483
|
+
iccProfile: null,
|
|
484
|
+
exifDateTimeOriginal: null,
|
|
485
|
+
byteSize: reportedTotalBytes,
|
|
486
|
+
contentType,
|
|
487
|
+
fetchedBytes,
|
|
488
|
+
metadata: {}
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
throw new Error("IMAGE_PARSE_FAILED");
|
|
492
|
+
}
|
|
493
|
+
else {
|
|
494
|
+
const docSpecs = this.docs.getDocumentSpecsWorkup(buffer, contentType ?? "application/pdf");
|
|
495
|
+
return {
|
|
496
|
+
source: url ?? "buffer",
|
|
497
|
+
byteSize: reportedTotalBytes,
|
|
498
|
+
contentType,
|
|
499
|
+
fetchedBytes,
|
|
500
|
+
...docSpecs
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
sniffPngDims(buf) {
|
|
505
|
+
const SIG = Buffer.from([
|
|
506
|
+
137,
|
|
507
|
+
80,
|
|
508
|
+
78,
|
|
509
|
+
71,
|
|
510
|
+
13,
|
|
511
|
+
10,
|
|
512
|
+
26,
|
|
513
|
+
10
|
|
514
|
+
]);
|
|
515
|
+
if (buf.length < 33 || !buf.subarray(0, 8).equals(SIG)) return null;
|
|
516
|
+
if (buf.readUInt32BE(8) !== 13 || buf.toString("ascii", 12, 16) !== "IHDR") return null;
|
|
517
|
+
return {
|
|
518
|
+
width: buf.readUInt32BE(16),
|
|
519
|
+
height: buf.readUInt32BE(20)
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
sniffTiff(rawbuffer, buffer) {
|
|
523
|
+
if (buffer.length >= 8 && (buffer?.[0] === 73 && buffer?.[1] === 73 && buffer?.[2] === 42 && buffer?.[3] === 0 || buffer?.[0] === 77 && buffer?.[1] === 77 && buffer?.[2] === 0 && buffer?.[3] === 42 || buffer?.[0] === 73 && buffer?.[1] === 73 && buffer?.[2] === 43 && buffer?.[3] === 0 || buffer?.[0] === 77 && buffer?.[1] === 77 && buffer?.[2] === 0 && buffer?.[3] === 43)) {
|
|
524
|
+
const data = this.img.tiff(rawbuffer, buffer);
|
|
525
|
+
if (!data) return null;
|
|
526
|
+
else return {
|
|
527
|
+
width: data.width,
|
|
528
|
+
height: data.height
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
sniffHeic(buffer) {
|
|
533
|
+
if (buffer.length >= 32 && buffer.toString("ascii", 4, 8) === "ftyp") {
|
|
534
|
+
const ftyp = this.img.findBox(buffer, "ftyp");
|
|
535
|
+
if (!ftyp) throw new Error("Invalid HEIC: No ftyp");
|
|
536
|
+
const brands = buffer.toString("ascii", ftyp.pos, ftyp.pos + ftyp.size).toLowerCase();
|
|
537
|
+
if (!(brands.includes("heic") || brands.includes("heix") || brands.includes("heim") || brands.includes("heis") || brands.includes("hevc") || brands.includes("hevx") || brands.includes("mif1"))) return null;
|
|
538
|
+
const meta = this.img.findBox(buffer, "meta");
|
|
539
|
+
if (!meta) throw new Error("Invalid HEIC: No meta");
|
|
540
|
+
const metaSubStart = meta.pos + 4;
|
|
541
|
+
const metaSubEnd = meta.pos + meta.size;
|
|
542
|
+
const iprp = this.img.findBox(buffer, "iprp", metaSubStart, metaSubEnd);
|
|
543
|
+
if (!iprp) throw new Error("Invalid HEIC: No iprp");
|
|
544
|
+
const ipco = this.img.findBox(buffer, "ipco", iprp.pos, iprp.pos + iprp.size);
|
|
545
|
+
if (!ipco) throw new Error("Invalid HEIC: No ipco");
|
|
546
|
+
const ispe = this.img.findBox(buffer, "ispe", ipco.pos, ipco.pos + ipco.size);
|
|
547
|
+
if (!ispe) throw new Error("Invalid HEIC: No ispe");
|
|
548
|
+
if (buffer[ispe.pos] !== 0) throw new Error("Invalid ispe version");
|
|
549
|
+
return {
|
|
550
|
+
width: buffer.readUInt32BE(ispe.pos + 4),
|
|
551
|
+
height: buffer.readUInt32BE(ispe.pos + 8)
|
|
552
|
+
};
|
|
553
|
+
} else return null;
|
|
554
|
+
}
|
|
555
|
+
sniffGifDims(buf) {
|
|
556
|
+
if (buf.length < 10 || buf.toString("ascii", 0, 3) !== "GIF") return null;
|
|
557
|
+
return {
|
|
558
|
+
width: buf.readUInt16LE(6),
|
|
559
|
+
height: buf.readUInt16LE(8)
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
sniffJpegDims(buf) {
|
|
563
|
+
if (buf.length < 4 || buf[0] !== 255 || buf[1] !== 216) return null;
|
|
564
|
+
let i = 2;
|
|
565
|
+
while (i + 9 < buf.length) {
|
|
566
|
+
if (buf[i] !== 255) {
|
|
567
|
+
i++;
|
|
568
|
+
continue;
|
|
569
|
+
}
|
|
570
|
+
const marker = buf[i + 1];
|
|
571
|
+
if (marker === 255) {
|
|
572
|
+
i++;
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
if (marker === 216 || marker === 217) {
|
|
576
|
+
i += 2;
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
579
|
+
if (i + 4 > buf.length) break;
|
|
580
|
+
const len = buf.readUInt16BE(i + 2);
|
|
581
|
+
if (len < 2) break;
|
|
582
|
+
if (marker && (marker >= 192 && marker <= 195 || marker >= 197 && marker <= 199 || marker >= 201 && marker <= 203 || marker >= 205 && marker <= 207)) {
|
|
583
|
+
if (i + 9 < buf.length) {
|
|
584
|
+
const height = buf.readUInt16BE(i + 5);
|
|
585
|
+
return {
|
|
586
|
+
width: buf.readUInt16BE(i + 7),
|
|
587
|
+
height
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
i += 2 + len;
|
|
593
|
+
}
|
|
594
|
+
return null;
|
|
595
|
+
}
|
|
596
|
+
sniffWebpDims(buf) {
|
|
597
|
+
if (buf.length < 30 || buf.toString("ascii", 0, 4) !== "RIFF" || buf.toString("ascii", 8, 12) !== "WEBP") return null;
|
|
598
|
+
const fourcc = buf.toString("ascii", 12, 16);
|
|
599
|
+
if (fourcc === "VP8X" && buf.length >= 30 && buf[24] && buf[25] && buf[26] && buf[27] && buf[28] && buf[29]) return {
|
|
600
|
+
width: 1 + (buf?.[24] | buf[25] << 8 | buf[26] << 16),
|
|
601
|
+
height: 1 + (buf[27] | buf[28] << 8 | buf[29] << 16)
|
|
602
|
+
};
|
|
603
|
+
if (fourcc === "VP8 " && buf.length >= 30) {
|
|
604
|
+
if (buf[23] === 157 && buf[24] === 1 && buf[25] === 42) return {
|
|
605
|
+
width: buf.readUInt16LE(26),
|
|
606
|
+
height: buf.readUInt16LE(28)
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
if (fourcc === "VP8L" && buf.length >= 25 && buf[21] && buf[22] && buf[23] && buf[24]) {
|
|
610
|
+
const b0 = buf[21], b1 = buf[22], b2 = buf[23], b3 = buf[24];
|
|
611
|
+
return {
|
|
612
|
+
width: (b0 | (b1 & 63) << 8) + 1,
|
|
613
|
+
height: (b1 >> 6 | b2 << 2 | (b3 & 15) << 10) + 1
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
return null;
|
|
617
|
+
}
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
//#endregion
|
|
621
|
+
export { Extract };
|
|
622
|
+
//# sourceMappingURL=index.js.map
|
package/dist/fs/index.d.ts
CHANGED
|
@@ -1,59 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import '../fs-atomic/index.js';
|
|
6
|
-
import '../fs-base/index.js';
|
|
7
|
-
import '../fs-core/index.js';
|
|
8
|
-
import '../fs-size/index.js';
|
|
9
|
-
import '../fs-tmp/index.js';
|
|
10
|
-
import '../image/index.js';
|
|
11
|
-
import '../image/workup.js';
|
|
12
|
-
import '../ld/index.js';
|
|
13
|
-
import '../mime/index.js';
|
|
14
|
-
import '../types/stream.js';
|
|
15
|
-
import '../url/index.js';
|
|
16
|
-
import '../utils/index.js';
|
|
17
|
-
import 'stream';
|
|
18
|
-
import 'fs';
|
|
19
|
-
import 'node:stream';
|
|
20
|
-
import 'stream/web';
|
|
21
|
-
import 'dotenv-expand';
|
|
1
|
+
import { ExpandedDocSpecs, ExpandedImgSpecs } from "../types/index.js";
|
|
2
|
+
import { FsFetch } from "../fs-fetch/index.js";
|
|
3
|
+
import "../index.js";
|
|
4
|
+
import { Readable } from "node:stream";
|
|
22
5
|
|
|
6
|
+
//#region src/fs/index.d.ts
|
|
23
7
|
declare class Fs extends FsFetch {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
*
|
|
37
|
-
* @access package-private
|
|
38
|
-
*
|
|
39
|
-
* work in progress, for internal package maintainer use only
|
|
40
|
-
*/
|
|
41
|
-
imageTransform<const F extends "webp" | "avif" | "jpg" | "gif" | "svg" | "png" | "tif" | "tiff" | "jp2" | "jpeg">({ format, target, quality, tint, resize }: {
|
|
42
|
-
format: F;
|
|
43
|
-
target: Buffer<ArrayBuffer>;
|
|
44
|
-
quality?: number;
|
|
45
|
-
tint?: string | {
|
|
46
|
-
r?: number | undefined;
|
|
47
|
-
g?: number | undefined;
|
|
48
|
-
b?: number | undefined;
|
|
49
|
-
alpha?: number | undefined;
|
|
50
|
-
};
|
|
51
|
-
resize?: {
|
|
52
|
-
widthOrOptions?: number | sharp.ResizeOptions | null;
|
|
53
|
-
height?: number | null;
|
|
54
|
-
options?: sharp.ResizeOptions;
|
|
55
|
-
};
|
|
56
|
-
}): Promise<Buffer<ArrayBufferLike>>;
|
|
8
|
+
cwd: string;
|
|
9
|
+
constructor(cwd: string);
|
|
10
|
+
getSpecs(filePath: string, size?: number): Promise<ExpandedDocSpecs | ExpandedImgSpecs>;
|
|
11
|
+
getSpecsFlexi(target: Buffer | string, size?: number): Promise<ExpandedDocSpecs | ExpandedImgSpecs>;
|
|
12
|
+
getSpecsTmp(filename: string, size?: number): Promise<ExpandedDocSpecs | ExpandedImgSpecs>;
|
|
13
|
+
/**
|
|
14
|
+
* Extract image metadata using streaming (only reads ~4KB)
|
|
15
|
+
* Much more memory efficient for large images
|
|
16
|
+
*/
|
|
17
|
+
getSpecsStream(filePath: string, size?: number): Promise<ExpandedDocSpecs | ExpandedImgSpecs>;
|
|
18
|
+
getSpecsStreamTmp(filename: string): Promise<ExpandedDocSpecs | ExpandedImgSpecs>;
|
|
19
|
+
protected readBytes(stream: Readable, size?: number): Promise<Buffer>;
|
|
57
20
|
}
|
|
58
|
-
|
|
21
|
+
//#endregion
|
|
59
22
|
export { Fs as default };
|
|
23
|
+
//# sourceMappingURL=index.d.ts.map
|