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