@d0paminedriven/fs 1.0.0 → 3.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/{chunk-MFKSUNKN.js → chunk-SPP3NFBL.js} +160 -25
- package/dist/fs/index.cjs +158 -23
- package/dist/fs/index.d.cts +75 -10
- package/dist/fs/index.d.ts +75 -10
- package/dist/fs/index.js +1 -1
- package/dist/index.cjs +158 -23
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/package.json +9 -9
|
@@ -87,8 +87,9 @@ var import_dotenv_expand = __toESM(require_main(), 1);
|
|
|
87
87
|
import { execSync } from "child_process";
|
|
88
88
|
import fsSync from "fs";
|
|
89
89
|
import fsAsync from "fs/promises";
|
|
90
|
-
import { relative } from "path";
|
|
90
|
+
import { join, relative, resolve } from "path";
|
|
91
91
|
import * as dotenv from "dotenv";
|
|
92
|
+
import sharp from "sharp";
|
|
92
93
|
dotenv.config();
|
|
93
94
|
var Fs = class {
|
|
94
95
|
constructor(cwd) {
|
|
@@ -108,20 +109,20 @@ var Fs = class {
|
|
|
108
109
|
);
|
|
109
110
|
}
|
|
110
111
|
excludeTargeted = /* @__PURE__ */ __name((obj, props, sort) => {
|
|
111
|
-
const
|
|
112
|
+
const resolve2 = Object.fromEntries(
|
|
112
113
|
Object.entries(obj).map(([key, val]) => {
|
|
113
114
|
if (props.includes(key)) {
|
|
114
115
|
return ["omit", "omit"];
|
|
115
116
|
} else return [key, val];
|
|
116
117
|
}).filter(([t, _v]) => /omit/.test(t) === false)
|
|
117
118
|
);
|
|
118
|
-
return typeof sort !== "undefined" ? this.sort(
|
|
119
|
+
return typeof sort !== "undefined" ? this.sort(resolve2, sort) : resolve2;
|
|
119
120
|
}, "excludeTargeted");
|
|
120
121
|
includeTargeted = /* @__PURE__ */ __name((obj, props, sort) => {
|
|
121
|
-
const
|
|
122
|
+
const resolve2 = Object.fromEntries(
|
|
122
123
|
props.map((val) => this.extractTuple(obj, val))
|
|
123
124
|
);
|
|
124
|
-
return typeof sort !== "undefined" ? this.sort(
|
|
125
|
+
return typeof sort !== "undefined" ? this.sort(resolve2, sort) : resolve2;
|
|
125
126
|
}, "includeTargeted");
|
|
126
127
|
get myEnv() {
|
|
127
128
|
return dotenv.config({ processEnv: {} });
|
|
@@ -145,6 +146,7 @@ var Fs = class {
|
|
|
145
146
|
return { ...this.omitFields(targ, keys) };
|
|
146
147
|
}
|
|
147
148
|
exists(target) {
|
|
149
|
+
if (!/\//gm.test(target)) return fsSync.existsSync(resolve(join(this.cwd, target)));
|
|
148
150
|
return fsSync.existsSync(relative(this.cwd ?? process.cwd(), target));
|
|
149
151
|
}
|
|
150
152
|
arrToArrOfArrs = /* @__PURE__ */ __name(({
|
|
@@ -152,7 +154,7 @@ var Fs = class {
|
|
|
152
154
|
arrOfArrsAggregator = Array.of(),
|
|
153
155
|
interval
|
|
154
156
|
}) => new Promise(
|
|
155
|
-
(
|
|
157
|
+
(resolve2, _reject) => resolve2(
|
|
156
158
|
((interval2) => {
|
|
157
159
|
for (let i = 0; i <= arrToFragment.length; i++) {
|
|
158
160
|
if ((i % interval2 === 0 || i === 0) && i <= arrToFragment.length) {
|
|
@@ -164,7 +166,7 @@ var Fs = class {
|
|
|
164
166
|
)
|
|
165
167
|
).then((_) => arrOfArrsAggregator), "arrToArrOfArrs");
|
|
166
168
|
wait(ms) {
|
|
167
|
-
return new Promise((
|
|
169
|
+
return new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
168
170
|
}
|
|
169
171
|
countsSorter = /* @__PURE__ */ __name(({
|
|
170
172
|
counter,
|
|
@@ -182,8 +184,15 @@ var Fs = class {
|
|
|
182
184
|
(v) => Buffer.isBuffer(v) ? Buffer.from(v).toString("utf-8") : v
|
|
183
185
|
);
|
|
184
186
|
}
|
|
187
|
+
isRootPathTargeted(path) {
|
|
188
|
+
if (/^(?:\.\/|\.|\/|root|\.\.\/|~\/\.?|cwd|\.\/\.)$/g.test(path)) {
|
|
189
|
+
return true;
|
|
190
|
+
} else return false;
|
|
191
|
+
}
|
|
185
192
|
readDir(path, options) {
|
|
186
|
-
|
|
193
|
+
if (this.isRootPathTargeted(path)) {
|
|
194
|
+
return this.handleBuffStrArrUnion(fsSync.readdirSync(resolve(join(this.cwd, "./")), options));
|
|
195
|
+
} else return this.handleBuffStrArrUnion(
|
|
187
196
|
fsSync.readdirSync(
|
|
188
197
|
relative(this.cwd ??= process.cwd(), path),
|
|
189
198
|
options
|
|
@@ -193,9 +202,7 @@ var Fs = class {
|
|
|
193
202
|
executeCommand = /* @__PURE__ */ __name(({
|
|
194
203
|
command,
|
|
195
204
|
...options
|
|
196
|
-
}) => Buffer.from(
|
|
197
|
-
Buffer.from(execSync(command, { ...options }).toJSON().data)
|
|
198
|
-
).toString("utf-8"), "executeCommand");
|
|
205
|
+
}) => Buffer.from(execSync(command, { ...options }).toJSON().data).toString("utf-8"), "executeCommand");
|
|
199
206
|
fileGenTimestamp(d) {
|
|
200
207
|
const date = d.toISOString();
|
|
201
208
|
return `/* file-autogenerated by @d0paminedriven/fs on ${date.split(/([T])/gm)?.[0]} at ${date.split(/([T])/gm)[2]?.split(/([Z])/gm)?.[0]} UTC */`;
|
|
@@ -207,6 +214,7 @@ var Fs = class {
|
|
|
207
214
|
return this.fileGenTimestamp(d).replace("/*", "<!--").replace("*/", "-->");
|
|
208
215
|
}
|
|
209
216
|
existsSync(path) {
|
|
217
|
+
if (!/\//gm.test(path)) return fsSync.existsSync(resolve(join(this.cwd, path)));
|
|
210
218
|
return fsSync.existsSync(relative(this.cwd ?? process.cwd(), path));
|
|
211
219
|
}
|
|
212
220
|
pathHandler(path) {
|
|
@@ -269,18 +277,11 @@ var Fs = class {
|
|
|
269
277
|
}
|
|
270
278
|
}, "writeFileAsync");
|
|
271
279
|
fileToBuffer = /* @__PURE__ */ __name((path) => Buffer.from(
|
|
272
|
-
|
|
273
|
-
fsSync.readFileSync(relative(this.cwd ?? process.cwd(), path)).toJSON().data
|
|
274
|
-
)
|
|
280
|
+
fsSync.readFileSync(relative(this.cwd ?? process.cwd(), path)).toJSON().data
|
|
275
281
|
), "fileToBuffer");
|
|
276
282
|
dirContainsDir(readDir, targetDir, options) {
|
|
277
283
|
return this.readDir(readDir, options).filter((t) => t.split(".").length === 1).includes(targetDir);
|
|
278
284
|
}
|
|
279
|
-
parseJsonBuffer(p) {
|
|
280
|
-
return JSON.parse(
|
|
281
|
-
Buffer.from(Buffer.from(p).toJSON().data).toString("utf-8")
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
285
|
/* begin url */
|
|
285
286
|
isServerSide() {
|
|
286
287
|
return typeof window === "undefined";
|
|
@@ -317,11 +318,21 @@ var Fs = class {
|
|
|
317
318
|
};
|
|
318
319
|
}
|
|
319
320
|
/* end url */
|
|
321
|
+
get cjsVal() {
|
|
322
|
+
return ["application/node", "text/javascript"].reduce(
|
|
323
|
+
(union) => union
|
|
324
|
+
);
|
|
325
|
+
}
|
|
320
326
|
get gzVal() {
|
|
321
327
|
return ["application/x-gzip", "application/gzip"].reduce(
|
|
322
328
|
(union) => union
|
|
323
329
|
);
|
|
324
330
|
}
|
|
331
|
+
get jsVal() {
|
|
332
|
+
return ["application/node", "text/javascript"].reduce(
|
|
333
|
+
(union) => union
|
|
334
|
+
);
|
|
335
|
+
}
|
|
325
336
|
get objVal() {
|
|
326
337
|
return ["application/octet-stream", "text/plain", "model/obj"].reduce((union) => union);
|
|
327
338
|
}
|
|
@@ -340,8 +351,10 @@ var Fs = class {
|
|
|
340
351
|
return {
|
|
341
352
|
aac: "audio/aac",
|
|
342
353
|
abw: "application/x-abiword",
|
|
354
|
+
aces: "image/aces",
|
|
343
355
|
apng: "image/apng",
|
|
344
356
|
arc: "application/x-freearc",
|
|
357
|
+
avci: "image/avci",
|
|
345
358
|
avif: "image/avif",
|
|
346
359
|
avi: "video/x-msvideo",
|
|
347
360
|
azw: "application/vnd.amazon.ebook",
|
|
@@ -350,26 +363,31 @@ var Fs = class {
|
|
|
350
363
|
bz: "application/x-bzip",
|
|
351
364
|
bz2: "application/x-bzip2",
|
|
352
365
|
cda: "application/x-cdf",
|
|
353
|
-
cjs:
|
|
366
|
+
cjs: this.cjsVal,
|
|
354
367
|
csh: "application/x-csh",
|
|
355
368
|
css: "text/css",
|
|
356
369
|
csv: "text/csv",
|
|
357
370
|
doc: "application/msword",
|
|
358
371
|
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
372
|
+
dpx: "image/dpx",
|
|
373
|
+
emf: "image/emf",
|
|
359
374
|
eot: "application/vnd.ms-fontobject",
|
|
360
375
|
epub: "application/epub+zip",
|
|
361
376
|
gif: "image/gif",
|
|
362
377
|
glb: "model/gltf-binary",
|
|
363
378
|
gltf: "model/gltf+json",
|
|
364
379
|
gz: this.gzVal,
|
|
380
|
+
hjif: "haptics/hjif",
|
|
381
|
+
hmpg: "haptics/hmpg",
|
|
365
382
|
htm: "text/html",
|
|
366
383
|
html: "text/html",
|
|
367
384
|
ico: "image/vnd.microsoft.icon",
|
|
368
385
|
ics: "text/calendar",
|
|
386
|
+
ivs: "haptics/ivs",
|
|
369
387
|
jar: "application/java-archive",
|
|
370
388
|
jpeg: "image/jpeg",
|
|
371
389
|
jpg: "image/jpeg",
|
|
372
|
-
js:
|
|
390
|
+
js: this.jsVal,
|
|
373
391
|
json: "application/json",
|
|
374
392
|
jsonld: "application/ld+json",
|
|
375
393
|
m3u8: "application/vnd.apple.mpegurl",
|
|
@@ -406,13 +424,16 @@ var Fs = class {
|
|
|
406
424
|
rar: "application/vnd.rar",
|
|
407
425
|
rtf: "application/rtf",
|
|
408
426
|
sh: "application/x-sh",
|
|
427
|
+
sql: "application/sql",
|
|
409
428
|
svg: "image/svg+xml",
|
|
410
429
|
tar: "application/x-tar",
|
|
411
430
|
tif: "image/tiff",
|
|
412
431
|
tiff: "image/tiff",
|
|
432
|
+
toml: "application/toml",
|
|
413
433
|
ts: this.tsVal,
|
|
414
434
|
ttf: this.ttfVal,
|
|
415
435
|
txt: "text/plain",
|
|
436
|
+
usdz: "model/vnd.usdz+zip",
|
|
416
437
|
vsd: "application/vnd.visio",
|
|
417
438
|
vtt: "text/vtt",
|
|
418
439
|
wasm: "application/wasm",
|
|
@@ -426,6 +447,8 @@ var Fs = class {
|
|
|
426
447
|
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
427
448
|
xml: "application/xml",
|
|
428
449
|
xul: "application/vnd.mozilla.xul+xml",
|
|
450
|
+
yaml: "application/yaml",
|
|
451
|
+
yml: "application/yaml",
|
|
429
452
|
zip: this.zipVal,
|
|
430
453
|
"3gp": "video/3gpp",
|
|
431
454
|
"3g2": "video/3gpp2",
|
|
@@ -454,6 +477,12 @@ var Fs = class {
|
|
|
454
477
|
};
|
|
455
478
|
}
|
|
456
479
|
async assetToBufferView(path) {
|
|
480
|
+
const p = this.parseUrl(path);
|
|
481
|
+
const doesUrlPathContainFileExtension = /\./g.test(
|
|
482
|
+
p.pathname.split(/\//g).reverse()[0] ?? ""
|
|
483
|
+
);
|
|
484
|
+
if (doesUrlPathContainFileExtension === false)
|
|
485
|
+
console.warn(`no file extension detected in input url ${path}, attempting fetch`);
|
|
457
486
|
const response = await fetch(path);
|
|
458
487
|
if (!response.ok) {
|
|
459
488
|
throw new Error(`Failed to fetch asset: ${response.statusText}`);
|
|
@@ -530,7 +559,7 @@ var Fs = class {
|
|
|
530
559
|
{
|
|
531
560
|
const sliceSize = 512;
|
|
532
561
|
const typeMatch = b64Data.match(
|
|
533
|
-
/^data:(image|application|video|text|model|
|
|
562
|
+
/^data:(?:image|application|haptics|video|text|font|model|audio|multipart)\/[A-Za-z0-9+-.]+(?:;[^,]+)*;base64,/i
|
|
534
563
|
);
|
|
535
564
|
const type = typeMatch?.[1];
|
|
536
565
|
if (!typeMatch) {
|
|
@@ -554,10 +583,19 @@ var Fs = class {
|
|
|
554
583
|
}
|
|
555
584
|
cleanDataUrl(props) {
|
|
556
585
|
return props.replace(
|
|
557
|
-
/^data:(image|application|video|text|font|model|audio)\/[A-Za-z0-9+-.]
|
|
586
|
+
/^data:(?:image|application|haptics|video|text|font|model|audio|multipart)\/[A-Za-z0-9+-.]+(?:;[^,]+)*;base64,/i,
|
|
587
|
+
""
|
|
588
|
+
);
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* public cleanDataUrl<const C extends string>(props: C) {
|
|
592
|
+
return props.replace(
|
|
593
|
+
/^data:(?:image|application|video|text|font|model|audio|haptics|multipart)\/[A-Za-z0-9+-.]+(?:;[^,]+)*;base64,/i,
|
|
558
594
|
""
|
|
559
595
|
);
|
|
560
596
|
}
|
|
597
|
+
|
|
598
|
+
*/
|
|
561
599
|
/**
|
|
562
600
|
*
|
|
563
601
|
* @param inputUrl remote url to fetch data from
|
|
@@ -577,7 +615,7 @@ var Fs = class {
|
|
|
577
615
|
const formattedPath = useDetectedExtension ? `${outputPath}.${result.extension}` : outputPath;
|
|
578
616
|
if (/\./g.test(formattedPath) === false) {
|
|
579
617
|
throw new Error(
|
|
580
|
-
"either add false as the third argument in `fetchRemoteWriteLocal`
|
|
618
|
+
"either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension) or provide an output path without a file extension"
|
|
581
619
|
);
|
|
582
620
|
}
|
|
583
621
|
this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
@@ -585,9 +623,106 @@ var Fs = class {
|
|
|
585
623
|
return console.error(err);
|
|
586
624
|
}
|
|
587
625
|
}
|
|
626
|
+
async fetchRemoteWriteLocalLargeFiles(inputUrl, outputPath, useDetectedExtension = true) {
|
|
627
|
+
const MAX_SAFE_IN_MEMORY_MB = 100;
|
|
628
|
+
try {
|
|
629
|
+
const head = await fetch(inputUrl, { method: "HEAD" });
|
|
630
|
+
const contentLength = head.headers.get("content-length");
|
|
631
|
+
const fileSizeMb = contentLength ? parseInt(contentLength, 10) / (1024 * 1024) : null;
|
|
632
|
+
const formattedPath = useDetectedExtension ? `${outputPath}.${this.assetType(inputUrl)}` : outputPath;
|
|
633
|
+
this.generateDirIfDNE(this.pathHandler(formattedPath), {
|
|
634
|
+
recursive: true
|
|
635
|
+
});
|
|
636
|
+
if (fileSizeMb !== null && fileSizeMb > MAX_SAFE_IN_MEMORY_MB) {
|
|
637
|
+
const res = await fetch(inputUrl);
|
|
638
|
+
if (!res.ok || !res.body) {
|
|
639
|
+
throw new Error(`Failed to fetch asset: ${res.statusText}`);
|
|
640
|
+
}
|
|
641
|
+
const writeStream = fsSync.createWriteStream(
|
|
642
|
+
relative(this.cwd ?? process.cwd(), formattedPath)
|
|
643
|
+
);
|
|
644
|
+
const reader = res.body.getReader();
|
|
645
|
+
const pump = /* @__PURE__ */ __name(async () => {
|
|
646
|
+
while (true) {
|
|
647
|
+
const { done, value } = await reader.read();
|
|
648
|
+
if (done) break;
|
|
649
|
+
writeStream.write(value);
|
|
650
|
+
}
|
|
651
|
+
writeStream.end();
|
|
652
|
+
}, "pump");
|
|
653
|
+
await pump();
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
const result = await this.assetToBufferView(inputUrl);
|
|
657
|
+
const cleanData = this.cleanDataUrl(result.b64encodedData);
|
|
658
|
+
this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
659
|
+
} catch (err) {
|
|
660
|
+
console.error(`[fetchRemoteWriteLocal error]:`, err);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
// USE fluent-ffmpeg for video/animated image transforms (apng, etc)
|
|
664
|
+
// https://www.npmjs.com/package/fluent-ffmpeg
|
|
665
|
+
// https://www.npmjs.com/package/@types/fluent-ffmpeg
|
|
666
|
+
async imageTransform({
|
|
667
|
+
format,
|
|
668
|
+
target,
|
|
669
|
+
quality = 80,
|
|
670
|
+
tint,
|
|
671
|
+
resize
|
|
672
|
+
}) {
|
|
673
|
+
if (tint && !resize) {
|
|
674
|
+
return await sharp(target).toFormat(format, { quality }).tint(tint).toBuffer();
|
|
675
|
+
} else if (!tint && resize) {
|
|
676
|
+
return await sharp(target).toFormat(format, { quality }).resize(resize.widthOrOptions, resize.height, resize.options).toBuffer();
|
|
677
|
+
} else if (tint && resize) {
|
|
678
|
+
return await sharp(target).toFormat(format, { quality }).tint(tint).resize(resize.widthOrOptions, resize.height, resize.options).toBuffer();
|
|
679
|
+
} else return await sharp(target).toFormat(format, { quality }).toBuffer();
|
|
680
|
+
}
|
|
681
|
+
// public async convertImage<
|
|
682
|
+
// const L extends "local" | "remote",
|
|
683
|
+
// const F extends "webp" | "avif" | "jpg" | "png",
|
|
684
|
+
// const I extends string,
|
|
685
|
+
// const O extends string
|
|
686
|
+
// >(isLocal: L, inputUrl: I, outputPath: O, useDetectedExtension = true) {
|
|
687
|
+
// if (isLocal === "remote") {
|
|
688
|
+
// try {
|
|
689
|
+
// const result = await this.assetToBufferView(inputUrl);
|
|
690
|
+
// const cleanData = this.cleanDataUrl(result.b64encodedData);
|
|
691
|
+
// const imageBuffer = Buffer.from(cleanData, "base64");
|
|
692
|
+
// const s = this.imageTransform({format: "webp", target: imageBuffer, quality: 100, resize: {options: {width: 1.2, height: 1.2}}} )
|
|
693
|
+
// const formattedPath = useDetectedExtension
|
|
694
|
+
// ? `${outputPath}.${result.extension}`
|
|
695
|
+
// : outputPath;
|
|
696
|
+
// if (/\./g.test(formattedPath) === false) {
|
|
697
|
+
// throw new Error(
|
|
698
|
+
// "either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension)"
|
|
699
|
+
// );
|
|
700
|
+
// }
|
|
701
|
+
// this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
702
|
+
// } catch (err) {
|
|
703
|
+
// return console.error(err);
|
|
704
|
+
// }
|
|
705
|
+
// } else {
|
|
706
|
+
// try {
|
|
707
|
+
// const result = await this.assetToBufferView(inputUrl);
|
|
708
|
+
// const cleanData = this.cleanDataUrl(result.b64encodedData);
|
|
709
|
+
// const formattedPath = useDetectedExtension
|
|
710
|
+
// ? `${outputPath}.${result.extension}`
|
|
711
|
+
// : outputPath;
|
|
712
|
+
// if (/\./g.test(formattedPath) === false) {
|
|
713
|
+
// throw new Error(
|
|
714
|
+
// "either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension)"
|
|
715
|
+
// );
|
|
716
|
+
// }
|
|
717
|
+
// this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
718
|
+
// } catch (err) {
|
|
719
|
+
// return console.error(err);
|
|
720
|
+
// }
|
|
721
|
+
// }
|
|
722
|
+
// }
|
|
588
723
|
};
|
|
589
724
|
|
|
590
725
|
export {
|
|
591
726
|
Fs
|
|
592
727
|
};
|
|
593
|
-
//# sourceMappingURL=chunk-
|
|
728
|
+
//# sourceMappingURL=chunk-SPP3NFBL.js.map
|