@d0paminedriven/fs 10.0.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.
Files changed (60) hide show
  1. package/dist/docs/index.d.ts +119 -0
  2. package/dist/docs/index.js +1027 -0
  3. package/dist/extract/client.d.ts +54 -0
  4. package/dist/extract/client.js +118 -0
  5. package/dist/extract/index.d.ts +69 -0
  6. package/dist/extract/index.js +622 -0
  7. package/dist/fs/index.d.ts +19 -55
  8. package/dist/fs/index.js +88 -19
  9. package/dist/fs-atomic/index.d.ts +28 -36
  10. package/dist/fs-atomic/index.js +88 -13
  11. package/dist/fs-base/index.d.ts +105 -111
  12. package/dist/fs-base/index.js +188 -12
  13. package/dist/fs-core/index.d.ts +20 -24
  14. package/dist/fs-core/index.js +44 -16
  15. package/dist/fs-fetch/index.d.ts +48 -62
  16. package/dist/fs-fetch/index.js +146 -18
  17. package/dist/fs-size/index.d.ts +16 -28
  18. package/dist/fs-size/index.js +64 -15
  19. package/dist/fs-tmp/index.d.ts +179 -192
  20. package/dist/fs-tmp/index.js +321 -17
  21. package/dist/images/index.d.ts +447 -0
  22. package/dist/images/index.js +899 -0
  23. package/dist/images/workup.d.ts +30 -0
  24. package/dist/images/workup.js +317 -0
  25. package/dist/index.d.ts +20 -22
  26. package/dist/index.js +20 -64
  27. package/dist/ld/index.d.ts +15 -13
  28. package/dist/ld/index.js +56 -6
  29. package/dist/mime/index.d.ts +299 -294
  30. package/dist/mime/index.js +348 -13
  31. package/dist/mixins/index.d.ts +34 -0
  32. package/dist/mixins/index.js +54 -0
  33. package/dist/types/index.d.ts +279 -215
  34. package/dist/types/index.js +11 -6
  35. package/dist/types/stream.d.ts +6 -4
  36. package/dist/types/stream.js +1 -1
  37. package/dist/url/index.d.ts +15 -18
  38. package/dist/url/index.js +21 -8
  39. package/dist/utils/index.d.ts +19 -23
  40. package/dist/utils/index.js +82 -7
  41. package/package.json +33 -13
  42. package/dist/chunk-5CACVZVF.js +0 -386
  43. package/dist/chunk-5H6GE5AO.js +0 -14
  44. package/dist/chunk-7JQ56XIH.js +0 -187
  45. package/dist/chunk-7LOAFIZM.js +0 -77
  46. package/dist/chunk-7QVYU63E.js +0 -7
  47. package/dist/chunk-DNR5ZI2T.js +0 -75
  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
@@ -1,20 +1,148 @@
1
- import {
2
- FsFetch
3
- } from "../chunk-7JQ56XIH.js";
4
- import "../chunk-PATMSIQK.js";
5
- import "../chunk-OHKRQJZH.js";
6
- import "../chunk-DNR5ZI2T.js";
7
- import "../chunk-5H6GE5AO.js";
8
- import "../chunk-YRCJWFZD.js";
9
- import "../chunk-VDKUXJE7.js";
10
- import "../chunk-Y5QWE4EG.js";
11
- import "../chunk-5CACVZVF.js";
12
- import "../chunk-L2QMQOEJ.js";
13
- import "../chunk-NJHN5OE3.js";
14
- import "../chunk-OT5JEQKW.js";
15
- import "../chunk-PB5A7ZFD.js";
16
- import "../chunk-7QVYU63E.js";
17
- export {
18
- FsFetch
1
+ import { FsTmp } from "../fs-tmp/index.js";
2
+ import fsSync from "fs";
3
+ import { relative } from "path";
4
+
5
+ //#region src/fs-fetch/index.ts
6
+ var FsFetch = class extends FsTmp {
7
+ constructor(cwd) {
8
+ super(cwd ??= process.cwd());
9
+ this.cwd = cwd;
10
+ }
11
+ async assetToBuffer(path) {
12
+ const [fetcher] = await Promise.all([fetch(path).then((t) => t.arrayBuffer())]);
13
+ return {
14
+ b64encodedData: `data:${this.getMimeTypeForPath(path)};base64, ${Buffer.from(fetcher).toString("base64")}`,
15
+ extension: this.assetType(path) ?? "txt"
16
+ };
17
+ }
18
+ async assetToBufferView(path) {
19
+ const response = await fetch(path, { method: "GET" });
20
+ if (!response.ok) throw new Error(`Failed to fetch asset: ${response.statusText}`);
21
+ const reader = response.body?.getReader();
22
+ if (reader) {
23
+ const chunks = Array.of();
24
+ while (true) {
25
+ const { done, value } = await reader.read();
26
+ if (done) break;
27
+ if (value) chunks.push(value);
28
+ }
29
+ const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
30
+ const completeBuffer = new Uint8Array(totalLength);
31
+ let offset = 0;
32
+ for (const chunk of chunks) {
33
+ completeBuffer.set(chunk, offset);
34
+ offset += chunk.length;
35
+ }
36
+ return {
37
+ b64encodedData: `data:${this.getMimeTypeForPath(path)};base64, ${Buffer.from(completeBuffer).toString("base64")}`,
38
+ extension: this.assetType(path)
39
+ };
40
+ } else {
41
+ const arrayBuffer = await response.arrayBuffer();
42
+ return {
43
+ b64encodedData: `data:${this.getMimeTypeForPath(path)};base64, ${Buffer.from(arrayBuffer).toString("base64")}`,
44
+ extension: this.assetType(path)
45
+ };
46
+ }
47
+ }
48
+ /**
49
+ *
50
+ * @param inputUrl remote url to fetch data from
51
+ * @param outputPath desired output path relative to the cwd
52
+ * @param useDetectedExtension optional, defaults to true
53
+ *
54
+ * if `useDetectedExtension` is false you must include the file extension in your output path →
55
+ *
56
+ * 🚫 'public/assets/image-1'
57
+ *
58
+ * ✅ 'public/assets/image-1.png'
59
+ */
60
+ async fetchRemoteWriteLocal(inputUrl, outputPath, useDetectedExtension = true) {
61
+ try {
62
+ const result = await this.assetToBufferView(inputUrl);
63
+ const cleanData = this.cleanDataUrl(result.b64encodedData);
64
+ const formattedPath = useDetectedExtension ? `${outputPath}.${result.extension}` : outputPath;
65
+ if (/\./g.test(formattedPath) === false) throw new Error("either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension) or provide an output path without a file extension");
66
+ this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
67
+ } catch (err) {
68
+ return console.error(err);
69
+ }
70
+ }
71
+ knownToBlockHead(inputUrl) {
72
+ if (inputUrl.startsWith("https://github.com")) return "GET";
73
+ else if (inputUrl.startsWith("https://raw.githubusercontent.com")) return "GET";
74
+ else return "HEAD";
75
+ }
76
+ getUrlExt(url) {
77
+ const pathname = new URL(url).pathname;
78
+ const lastDot = pathname.lastIndexOf(".");
79
+ if (lastDot === -1 || lastDot === pathname.length - 1) return null;
80
+ return pathname.slice(lastDot + 1).toLowerCase();
81
+ }
82
+ fuzzyExtEquality(a, b) {
83
+ const aNorm = a.toLowerCase();
84
+ const bNorm = b.toLowerCase();
85
+ return this.calculateLD(aNorm, bNorm) / (Math.max(aNorm.length, bNorm.length) || 1) <= .34;
86
+ }
87
+ /**
88
+ *
89
+ * @param inputUrl remote url to fetch data from
90
+ * @param outputPath desired output path relative to the cwd
91
+ * @param useDetectedExtension optional, defaults to true
92
+ *
93
+ * if `useDetectedExtension` is false you must include the file extension in your output path &rarr;
94
+ *
95
+ * 🚫 'public/assets/image-1'
96
+ *
97
+ * ✅ 'public/assets/image-1.png'
98
+ *
99
+ * @description
100
+ * This method is designed for all files regardless of size, but especially for
101
+ * larger files that may not fit into memory (it intelligently determines the best approach on the fly)
102
+ * It streams the data directly to disk for files of all sizes instead of loading it all into memory
103
+ */
104
+ async fetchRemoteWriteLocalLargeFiles(inputUrl, outputPathI, useDetectedExtension = true) {
105
+ if (!URL.canParse(inputUrl)) throw new Error(`invalid URL ${inputUrl} is unable to be parsed`);
106
+ try {
107
+ const meta = await this.extractRemote(inputUrl, 4096 * 48);
108
+ const ext = meta.format && meta.format !== "bin" ? meta.format : this.mimeToExt(this.mimeTypeObj[inputUrl.slice(inputUrl.lastIndexOf(".") + 1)][0]);
109
+ const size = meta.byteSize ?? 0;
110
+ const { unit, value } = this.autoFileSizeRaw(size);
111
+ console.log(`fetchRemoteWriteLocalLargeFiles extracting a ${ext} file of size ${value} ${unit}`);
112
+ let formattedPath;
113
+ if (useDetectedExtension) formattedPath = `${outputPathI}.${ext}`;
114
+ else {
115
+ const lastDot = outputPathI.lastIndexOf(".");
116
+ if (lastDot === 1 || lastDot === -1) formattedPath = `${outputPathI}.${ext}`;
117
+ const outputPathExt = outputPathI.slice(outputPathI.lastIndexOf(".") + 1);
118
+ if (this.fuzzyExtEquality(ext, outputPathExt) === true) formattedPath = outputPathI;
119
+ else formattedPath = outputPathI.replace(outputPathExt, ext);
120
+ }
121
+ this.generateDirIfDNE(this.pathHandler(formattedPath), { recursive: true });
122
+ const writeStream = fsSync.createWriteStream(relative(this.cwd, formattedPath));
123
+ const res = await fetch(inputUrl);
124
+ if (!res.ok || !res.body) throw new Error(`Failed to fetch asset: ${res.statusText}`);
125
+ const { promise, reject, resolve: resolve$1 } = Promise.withResolvers();
126
+ writeStream.on("finish", () => resolve$1({}));
127
+ writeStream.on("error", () => reject({}));
128
+ const reader = res.body.getReader();
129
+ const pump = async () => {
130
+ while (true) {
131
+ const { done, value: value$1 } = await reader.read();
132
+ if (done) break;
133
+ writeStream.write(value$1);
134
+ }
135
+ writeStream.end();
136
+ };
137
+ await pump();
138
+ await promise.then(() => {});
139
+ return;
140
+ } catch (err) {
141
+ console.error(`[fetchRemoteWriteLocalLargeFiles error]:`, err);
142
+ }
143
+ }
19
144
  };
145
+
146
+ //#endregion
147
+ export { FsFetch };
20
148
  //# sourceMappingURL=index.js.map
@@ -1,32 +1,20 @@
1
- import { Unit, SizeOpts } from '../types/index.js';
2
- import { FsAtomic } from '../fs-atomic/index.js';
3
- import 'stream';
4
- import 'fs';
5
- import '../fs-base/index.js';
6
- import 'node:stream';
7
- import 'stream/web';
8
- import 'dotenv-expand';
9
- import '../types/stream.js';
10
- import '../image/index.js';
11
- import '@d0paminedriven/metadata';
12
- import '../image/workup.js';
13
- import '../mime/index.js';
14
- import '../url/index.js';
15
- import '../utils/index.js';
16
- import '../ld/index.js';
1
+ import { SizeOpts, Unit } from "../types/index.js";
2
+ import { FsAtomic } from "../fs-atomic/index.js";
17
3
 
4
+ //#region src/fs-size/index.d.ts
18
5
  declare class FsSize extends FsAtomic {
19
- cwd: string;
20
- constructor(cwd: string);
21
- private units;
22
- private u;
23
- autoFileSizeRaw(size: number | bigint): {
24
- value: number;
25
- unit: "B" | "KB" | "MB" | "GB" | "TB" | "PB" | undefined;
26
- };
27
- getSize<const S extends Unit | Lowercase<Unit> | "auto">(size: number | bigint, target: S, opts?: SizeOpts): string | number;
28
- fileSizeMb<const T extends string>(path: T): number;
29
- fileSize<const T extends string, const S extends Unit | Lowercase<Unit> | "auto">(path: T, target: S, opts?: SizeOpts): string | number;
6
+ cwd: string;
7
+ constructor(cwd: string);
8
+ private units;
9
+ private u;
10
+ autoFileSizeRaw(size: number | bigint): {
11
+ value: number;
12
+ unit: "B" | "GB" | "KB" | "MB" | "PB" | "TB" | undefined;
13
+ };
14
+ getSize<const S extends Unit | Lowercase<Unit> | "auto">(size: number | bigint, target: S, opts?: SizeOpts): string | number;
15
+ fileSizeMb<const T extends string>(path: T): number;
16
+ fileSize<const T extends string, const S extends Unit | Lowercase<Unit> | "auto">(path: T, target: S, opts?: SizeOpts): string | number;
30
17
  }
31
-
18
+ //#endregion
32
19
  export { FsSize };
20
+ //# sourceMappingURL=index.d.ts.map
@@ -1,17 +1,66 @@
1
- import {
2
- FsSize
3
- } from "../chunk-DNR5ZI2T.js";
4
- import "../chunk-5H6GE5AO.js";
5
- import "../chunk-YRCJWFZD.js";
6
- import "../chunk-VDKUXJE7.js";
7
- import "../chunk-Y5QWE4EG.js";
8
- import "../chunk-5CACVZVF.js";
9
- import "../chunk-L2QMQOEJ.js";
10
- import "../chunk-NJHN5OE3.js";
11
- import "../chunk-OT5JEQKW.js";
12
- import "../chunk-PB5A7ZFD.js";
13
- import "../chunk-7QVYU63E.js";
14
- export {
15
- FsSize
1
+ import { FsAtomic } from "../fs-atomic/index.js";
2
+ import { unitsObj } from "../types/index.js";
3
+ import { statSync } from "fs";
4
+ import { relative } from "path";
5
+
6
+ //#region src/fs-size/index.ts
7
+ var FsSize = class extends FsAtomic {
8
+ constructor(cwd) {
9
+ super(cwd ??= process.cwd());
10
+ this.cwd = cwd;
11
+ }
12
+ units = unitsObj;
13
+ u = [
14
+ "B",
15
+ "KB",
16
+ "MB",
17
+ "GB",
18
+ "TB",
19
+ "PB"
20
+ ];
21
+ autoFileSizeRaw(size) {
22
+ let s = typeof size === "bigint" ? Number(size) : size;
23
+ let i = 0;
24
+ while (s >= 1024 && i < this.u.length - 1) {
25
+ s /= 1024;
26
+ i++;
27
+ }
28
+ return {
29
+ value: s,
30
+ unit: this.u[i]
31
+ };
32
+ }
33
+ getSize(size, target, opts = {
34
+ decimals: 4,
35
+ includeUnits: true
36
+ }) {
37
+ const { decimals, includeUnits } = opts;
38
+ if (target === "auto") {
39
+ const { value, unit } = this.autoFileSizeRaw(size);
40
+ const rounded$1 = value.toFixed(decimals);
41
+ return includeUnits ? `${rounded$1} ${unit}` : Number.parseFloat(rounded$1);
42
+ }
43
+ const key = target.toUpperCase();
44
+ const exp = this.units[key];
45
+ const divisor = typeof size === "bigint" ? 1024n ** BigInt(exp) : 1024 ** exp;
46
+ let v = 0;
47
+ if (typeof size === "bigint" || typeof divisor === "bigint") {
48
+ if (typeof size === "bigint" && typeof divisor === "bigint") v = Number(size / divisor);
49
+ else if (typeof size !== "bigint" && typeof divisor === "bigint") v = size / Number(divisor);
50
+ else if (typeof size === "bigint" && typeof divisor !== "bigint") v = Number(size) / divisor;
51
+ } else if (typeof size === "number" && typeof divisor === "number") v = size / divisor;
52
+ const rounded = v.toFixed(decimals);
53
+ return includeUnits ? `${rounded} ${key}` : Number.parseFloat(rounded);
54
+ }
55
+ fileSizeMb(path) {
56
+ return statSync(relative(this.cwd, path)).size / (1024 * 1024);
57
+ }
58
+ fileSize(path, target, opts) {
59
+ if (!this.exists(path)) throw new Error(`path ${path} does not exist`);
60
+ else return this.getSize(statSync(relative(this.cwd, path)).size, target, opts);
61
+ }
16
62
  };
63
+
64
+ //#endregion
65
+ export { FsSize };
17
66
  //# sourceMappingURL=index.js.map