@d0paminedriven/fs 11.0.0 → 12.1.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 (41) hide show
  1. package/dist/docs/index.d.ts +42 -10
  2. package/dist/docs/index.js +177 -41
  3. package/dist/extract/client.d.ts +13 -11
  4. package/dist/extract/client.js +2 -4
  5. package/dist/extract/index.d.ts +7 -10
  6. package/dist/extract/index.js +6 -8
  7. package/dist/fs/index.d.ts +7 -11
  8. package/dist/fs/index.js +15 -14
  9. package/dist/fs-atomic/index.d.ts +3 -9
  10. package/dist/fs-atomic/index.js +17 -11
  11. package/dist/fs-base/index.d.ts +8 -10
  12. package/dist/fs-base/index.js +36 -27
  13. package/dist/fs-core/index.d.ts +5 -9
  14. package/dist/fs-core/index.js +5 -6
  15. package/dist/fs-fetch/index.d.ts +6 -9
  16. package/dist/fs-fetch/index.js +64 -27
  17. package/dist/fs-size/index.d.ts +2 -6
  18. package/dist/fs-size/index.js +8 -9
  19. package/dist/fs-tmp/index.d.ts +7 -11
  20. package/dist/fs-tmp/index.js +7 -8
  21. package/dist/images/index.d.ts +44 -48
  22. package/dist/images/index.js +9 -15
  23. package/dist/images/workup.d.ts +5 -9
  24. package/dist/images/workup.js +4 -5
  25. package/dist/index.d.ts +42 -14
  26. package/dist/index.js +14 -15
  27. package/dist/ld/index.d.ts +1 -5
  28. package/dist/ld/index.js +2 -4
  29. package/dist/mime/index.d.ts +20 -20
  30. package/dist/mime/index.js +4 -4
  31. package/dist/mixins/index.d.ts +9 -13
  32. package/dist/mixins/index.js +6 -6
  33. package/dist/types/index.d.ts +105 -90
  34. package/dist/types/index.js +2 -3
  35. package/dist/types/stream.d.ts +5 -9
  36. package/dist/types/stream.js +1 -1
  37. package/dist/url/index.d.ts +6 -10
  38. package/dist/url/index.js +2 -4
  39. package/dist/utils/index.d.ts +5 -9
  40. package/dist/utils/index.js +2 -4
  41. package/package.json +18 -18
package/dist/fs/index.js CHANGED
@@ -1,14 +1,14 @@
1
- import { FsFetch } from "../fs-fetch/index.js";
2
1
  import { createReadStream } from "node:fs";
3
2
  import { relative, resolve } from "node:path";
4
-
5
- //#region src/fs/index.ts
3
+ import { FsFetch } from "../fs-fetch/index.js";
4
+ // src/fs/index.ts
6
5
  var Fs = class extends FsFetch {
6
+ cwd;
7
7
  constructor(cwd) {
8
8
  super(cwd ??= process.cwd());
9
9
  this.cwd = cwd;
10
10
  }
11
- async getSpecs(filePath, size = 4096 * 36) {
11
+ async getSpecs(filePath, size = 147456) {
12
12
  if (URL.canParse(filePath)) return await this.extractRemote(filePath, size);
13
13
  else {
14
14
  const stream = createReadStream(relative(this.cwd, filePath), { highWaterMark: size });
@@ -16,7 +16,7 @@ var Fs = class extends FsFetch {
16
16
  return await this.extractRemote(buffer, size);
17
17
  }
18
18
  }
19
- async getSpecsFlexi(target, size = 4096 * 36) {
19
+ async getSpecsFlexi(target, size = 147456) {
20
20
  if (Buffer.isBuffer(target)) return this.extractRemote(target, size);
21
21
  else if (URL.canParse(target)) return await this.extractRemote(target, size);
22
22
  else {
@@ -25,8 +25,9 @@ var Fs = class extends FsFetch {
25
25
  return this.extractRemote(buffer, size);
26
26
  }
27
27
  }
28
- async getSpecsTmp(filename, size = 4096 * 36) {
29
- const rs = createReadStream(resolve(this.tmpDir, filename));
28
+ async getSpecsTmp(filename, size = 147456) {
29
+ const absPath = resolve(this.tmpDir, filename);
30
+ const rs = createReadStream(absPath);
30
31
  const arr = Array.of();
31
32
  const iterate = rs.iterator();
32
33
  for await (const chunk of iterate) arr.push(chunk);
@@ -37,13 +38,14 @@ var Fs = class extends FsFetch {
37
38
  * Extract image metadata using streaming (only reads ~4KB)
38
39
  * Much more memory efficient for large images
39
40
  */
40
- async getSpecsStream(filePath, size = 4096 * 36) {
41
+ async getSpecsStream(filePath, size = 147456) {
41
42
  const stream = createReadStream(relative(this.cwd, filePath), { highWaterMark: size });
42
43
  const buffer = await this.readBytes(stream, size);
43
44
  return await this.extractRemote(buffer, size);
44
45
  }
45
46
  async getSpecsStreamTmp(filename) {
46
- const rs = createReadStream(resolve(this.tmpDir, filename));
47
+ const absPath = resolve(this.tmpDir, filename);
48
+ const rs = createReadStream(absPath);
47
49
  const arr = Array.of();
48
50
  const iterate = rs.iterator();
49
51
  for await (const chunk of iterate) arr.push(chunk);
@@ -51,7 +53,7 @@ var Fs = class extends FsFetch {
51
53
  return await this.extractRemote(buffer);
52
54
  }
53
55
  async readBytes(stream, size) {
54
- return new Promise((resolve$1, reject) => {
56
+ return new Promise((resolve, reject) => {
55
57
  const chunks = Array.of();
56
58
  let totalBytes = 0;
57
59
  const cleanup = () => {
@@ -74,17 +76,16 @@ var Fs = class extends FsFetch {
74
76
  }
75
77
  if (totalBytes >= bytes) {
76
78
  cleanup();
77
- resolve$1(Buffer.concat(chunks));
79
+ resolve(Buffer.concat(chunks));
78
80
  }
79
81
  });
80
82
  stream.on("end", () => {
81
83
  cleanup();
82
- resolve$1(Buffer.concat(chunks));
84
+ resolve(Buffer.concat(chunks));
83
85
  });
84
86
  });
85
87
  }
86
88
  };
87
-
88
- //#endregion
89
89
  export { Fs as default };
90
+
90
91
  //# sourceMappingURL=index.js.map
@@ -1,15 +1,11 @@
1
1
  import { ExecuteCommandProps, MkDirSyncOptions, NoParamCallback, ReadDirOptions, RmOptions } from "../types/index.js";
2
2
  import { FsBase } from "../fs-base/index.js";
3
-
4
- //#region src/fs-atomic/index.d.ts
5
- declare class FsAtomic extends FsBase {
3
+ export declare class FsAtomic extends FsBase {
6
4
  cwd: string;
7
5
  constructor(cwd: string);
8
- executeCommand: <const T extends string>({
9
- command,
10
- ...options
11
- }: ExecuteCommandProps<T>) => Buffer<ArrayBuffer>;
6
+ executeCommand: <const T extends string>({ command, ...options }: ExecuteCommandProps<T>) => Buffer<ArrayBuffer>;
12
7
  mkdirSync<const T extends string>(path: T, options?: MkDirSyncOptions): string | undefined;
8
+ mkdirSyncAbs<const T extends string>(path: T, options?: MkDirSyncOptions): string | undefined;
13
9
  generateDirIfDNE<const T extends string>(path: T, options?: MkDirSyncOptions): string | undefined;
14
10
  fileToBuffer: <const T extends string>(path: T) => Buffer<ArrayBuffer>;
15
11
  fileToBufferAsync: <const T extends string>(path: T) => Promise<NonSharedBuffer>;
@@ -27,6 +23,4 @@ declare class FsAtomic extends FsBase {
27
23
  generateDirIfDNETmp<const T extends string>(path: T, options?: MkDirSyncOptions): string | undefined;
28
24
  rmTmpFile<const V extends string>(filename: V): void;
29
25
  }
30
- //#endregion
31
- export { FsAtomic };
32
26
  //# sourceMappingURL=index.d.ts.map
@@ -1,11 +1,11 @@
1
- import { FsBase } from "../fs-base/index.js";
2
- import fsSync from "fs";
3
- import { join, relative, resolve } from "path";
4
- import fsAsync from "fs/promises";
5
1
  import { execSync } from "node:child_process";
6
-
7
- //#region src/fs-atomic/index.ts
2
+ import fsSync from "node:fs";
3
+ import fsAsync from "node:fs/promises";
4
+ import { join, relative, resolve } from "node:path";
5
+ import { FsBase } from "../fs-base/index.js";
6
+ // src/fs-atomic/index.ts
8
7
  var FsAtomic = class extends FsBase {
8
+ cwd;
9
9
  constructor(cwd) {
10
10
  super(cwd ??= process.cwd());
11
11
  this.cwd = cwd;
@@ -17,6 +17,12 @@ var FsAtomic = class extends FsBase {
17
17
  }) {
18
18
  return fsSync.mkdirSync(relative(this.cwd, path), options);
19
19
  }
20
+ mkdirSyncAbs(path, options = {
21
+ mode: 511,
22
+ recursive: true
23
+ }) {
24
+ return fsSync.mkdirSync(this.absPath(path), options);
25
+ }
20
26
  generateDirIfDNE(path, options = {
21
27
  mode: 511,
22
28
  recursive: true
@@ -37,9 +43,10 @@ var FsAtomic = class extends FsBase {
37
43
  recursive: true,
38
44
  retryDelay: 100
39
45
  }) {
40
- if (this.exists(filePath)) if (this.isRootPathTargeted(filePath)) fsSync.rmSync(resolve(join(this.cwd, filePath)), options);
41
- else fsSync.rmSync(resolve(this.cwd, filePath), options);
42
- else return;
46
+ if (this.exists(filePath)) {
47
+ if (this.isRootPathTargeted(filePath)) fsSync.rmSync(resolve(join(this.cwd, filePath)), options);
48
+ else fsSync.rmSync(resolve(this.cwd, filePath), options);
49
+ } else return;
43
50
  }
44
51
  /**
45
52
  * Removes files and directories (modeled on the standard POSIX `rm` utility).
@@ -84,7 +91,6 @@ var FsAtomic = class extends FsBase {
84
91
  this.rmFile(tmpPath);
85
92
  }
86
93
  };
87
-
88
- //#endregion
89
94
  export { FsAtomic };
95
+
90
96
  //# sourceMappingURL=index.js.map
@@ -1,23 +1,23 @@
1
+ import { Readable } from "node:stream";
2
+ import { ReadableStream } from "node:stream/web";
3
+ import expand from "dotenv-expand";
1
4
  import { ReadDirOptions } from "../types/index.js";
2
5
  import { AsyncIter, Streamable } from "../types/stream.js";
3
6
  import { MimeService } from "../mime/index.js";
4
- import { Readable } from "node:stream";
5
- import { ReadableStream } from "stream/web";
6
- import expand from "dotenv-expand";
7
-
8
- //#region src/fs-base/index.d.ts
9
- declare class FsBase extends MimeService {
7
+ export declare class FsBase extends MimeService {
10
8
  cwd: string;
11
9
  constructor(cwd: string);
12
10
  get tmpDir(): string;
13
- get homeDir(): string;
11
+ get homedir(): string;
14
12
  get platform(): NodeJS.Platform;
15
13
  get hostname(): string;
14
+ get machine(): string;
16
15
  get devNull(): string;
17
16
  get eol(): string;
18
17
  private get env();
18
+ relPath<const T extends string>(path: T): string;
19
+ absPath<const T extends string>(path: T): string;
19
20
  parseDotEnv(): expand.DotenvParseOutput | undefined;
20
- relative(from: string, to: string): string;
21
21
  isHomeTargeted(path: string): boolean;
22
22
  isRootPathTargeted<const T extends string>(path: T): boolean;
23
23
  pathHandler<const T extends string>(path: T): string;
@@ -103,6 +103,4 @@ declare class FsBase extends MimeService {
103
103
  */
104
104
  readDir<const T extends string>(path: T, options?: ReadDirOptions): string[];
105
105
  }
106
- //#endregion
107
- export { FsBase };
108
106
  //# sourceMappingURL=index.d.ts.map
@@ -1,14 +1,14 @@
1
- import { MimeService } from "../mime/index.js";
2
- import fsSync from "fs";
3
- import { join, relative, resolve } from "path";
1
+ import fsSync from "node:fs";
2
+ import { EOL, devNull, homedir, hostname, machine, tmpdir } from "node:os";
3
+ import { join, relative, resolve } from "node:path";
4
4
  import { Readable } from "node:stream";
5
- import { EOL, devNull, homedir, hostname, platform, tmpdir } from "node:os";
6
- import { ReadableStream } from "stream/web";
5
+ import { ReadableStream } from "node:stream/web";
7
6
  import * as dotenv from "dotenv";
8
7
  import expand from "dotenv-expand";
9
-
10
- //#region src/fs-base/index.ts
8
+ import { MimeService } from "../mime/index.js";
9
+ // src/fs-base/index.ts
11
10
  var FsBase = class extends MimeService {
11
+ cwd;
12
12
  constructor(cwd) {
13
13
  super();
14
14
  this.cwd = cwd;
@@ -16,15 +16,20 @@ var FsBase = class extends MimeService {
16
16
  get tmpDir() {
17
17
  return tmpdir();
18
18
  }
19
- get homeDir() {
20
- return homedir();
19
+ get homedir() {
20
+ if (process.env.HOME) return process.env.HOME;
21
+ else return homedir();
21
22
  }
22
23
  get platform() {
23
- return platform();
24
+ return process.platform;
24
25
  }
25
26
  get hostname() {
26
27
  return hostname();
27
28
  }
29
+ get machine() {
30
+ if (process.env.HOSTTYPE) return process.env.HOSTTYPE;
31
+ else return machine();
32
+ }
28
33
  get devNull() {
29
34
  return devNull;
30
35
  }
@@ -37,18 +42,22 @@ var FsBase = class extends MimeService {
37
42
  quiet: true
38
43
  });
39
44
  }
45
+ relPath(path) {
46
+ return relative(this.cwd, path);
47
+ }
48
+ absPath(path) {
49
+ return resolve(this.relPath(path));
50
+ }
40
51
  parseDotEnv() {
41
52
  const env = expand.expand(this.env);
42
- if (typeof env !== "undefined") if (env.error) throw new Error(env.error.message);
43
- else return env.parsed;
44
- else throw new Error("env returned undefined");
45
- }
46
- relative(from, to) {
47
- return relative(from, to);
53
+ if (typeof env !== "undefined") {
54
+ if (env.error) throw new Error(env.error.message);
55
+ else return env.parsed;
56
+ } else throw new Error("env returned undefined");
48
57
  }
49
58
  isHomeTargeted(path) {
50
59
  if (/^(~\/\.?)$/g.test(path)) return true;
51
- else if (path.includes(this.homeDir)) return true;
60
+ else if (path.includes(this.homedir)) return true;
52
61
  else return false;
53
62
  }
54
63
  isRootPathTargeted(path) {
@@ -106,13 +115,14 @@ var FsBase = class extends MimeService {
106
115
  *
107
116
  */
108
117
  exists(target) {
109
- if (!/\//gm.test(target)) if (/\./g.test(target)) return fsSync.existsSync(resolve(join(this.cwd, target)));
110
- else {
111
- const isDir = fsSync.statSync(resolve(join(this.cwd, target)), { throwIfNoEntry: false })?.isDirectory();
112
- if (isDir) return isDir;
113
- else return false;
114
- }
115
- else if (/\./g.test(target)) return fsSync.existsSync(relative(this.cwd ?? process.cwd(), target));
118
+ if (!/\//gm.test(target)) {
119
+ if (/\./g.test(target)) return fsSync.existsSync(resolve(join(this.cwd, target)));
120
+ else {
121
+ const isDir = fsSync.statSync(resolve(join(this.cwd, target)), { throwIfNoEntry: false })?.isDirectory();
122
+ if (isDir) return isDir;
123
+ else return false;
124
+ }
125
+ } else if (/\./g.test(target)) return fsSync.existsSync(relative(this.cwd ?? process.cwd(), target));
116
126
  else {
117
127
  const isDir = fsSync.statSync(relative(this.cwd, target), { throwIfNoEntry: false })?.isDirectory();
118
128
  if (isDir) return isDir;
@@ -143,7 +153,7 @@ var FsBase = class extends MimeService {
143
153
  * This method is useful for delaying execution in asynchronous code, such as when you need to wait for a file operation to complete or when you want to introduce a delay in a loop.
144
154
  */
145
155
  wait(ms) {
146
- return new Promise((resolve$1) => setTimeout(resolve$1, ms));
156
+ return new Promise((resolve) => setTimeout(resolve, ms));
147
157
  }
148
158
  /**
149
159
  * Handles an array or readonly array of strings and/or Buffers, converting Buffers to strings
@@ -184,7 +194,6 @@ var FsBase = class extends MimeService {
184
194
  else return this.handleBuffStrArrUnion(fsSync.readdirSync(relative(this.cwd ??= process.cwd(), path), options));
185
195
  }
186
196
  };
187
-
188
- //#endregion
189
197
  export { FsBase };
198
+
190
199
  //# sourceMappingURL=index.js.map
@@ -1,9 +1,7 @@
1
- import { WriteFileAsyncDataType, WriteFileAsyncOptions, WriteStreamOptions, WriteableDataType } from "../types/index.js";
2
- import { FsSize } from "../fs-size/index.js";
3
1
  import { Abortable } from "node:events";
4
-
5
- //#region src/fs-core/index.d.ts
6
- interface CreateReadStreamOptions extends Abortable {
2
+ import { FsSize } from "../fs-size/index.js";
3
+ import { WriteFileAsyncDataType, WriteFileAsyncOptions, WriteStreamOptions, WriteableDataType } from "../types/index.js";
4
+ export interface CreateReadStreamOptions extends Abortable {
7
5
  encoding?: BufferEncoding | null | undefined;
8
6
  autoClose?: boolean | undefined;
9
7
  emitClose?: boolean | undefined;
@@ -11,13 +9,11 @@ interface CreateReadStreamOptions extends Abortable {
11
9
  end?: number | undefined;
12
10
  highWaterMark?: number | undefined;
13
11
  }
14
- declare class FsCore extends FsSize {
12
+ export declare class FsCore extends FsSize {
15
13
  cwd: string;
16
14
  constructor(cwd: string);
17
15
  withWs<const T extends string>(path: T, data: WriteableDataType, options?: WriteStreamOptions): string | boolean | undefined;
18
16
  withWsAsync<const T extends string>(path: T, data: WriteableDataType, options?: WriteStreamOptions): Promise<string | boolean | undefined>;
19
- writeFileAsync: <const T extends string>(path: T, data: WriteFileAsyncDataType, options?: WriteFileAsyncOptions) => Promise<void | string>;
17
+ writeFileAsync: <const T extends string>(path: T, data: WriteFileAsyncDataType, options?: WriteFileAsyncOptions) => Promise<string | void>;
20
18
  }
21
- //#endregion
22
- export { CreateReadStreamOptions, FsCore };
23
19
  //# sourceMappingURL=index.d.ts.map
@@ -1,10 +1,10 @@
1
- import { FsSize } from "../fs-size/index.js";
1
+ import fsAsync from "fs/promises";
2
2
  import fsSync from "node:fs";
3
3
  import { relative } from "path";
4
- import fsAsync from "fs/promises";
5
-
6
- //#region src/fs-core/index.ts
4
+ import { FsSize } from "../fs-size/index.js";
5
+ // src/fs-core/index.ts
7
6
  var FsCore = class extends FsSize {
7
+ cwd;
8
8
  constructor(cwd) {
9
9
  super(cwd ??= process.cwd());
10
10
  this.cwd = cwd;
@@ -40,7 +40,6 @@ var FsCore = class extends FsSize {
40
40
  }
41
41
  };
42
42
  };
43
-
44
- //#endregion
45
43
  export { FsCore };
44
+
46
45
  //# sourceMappingURL=index.js.map
@@ -1,16 +1,14 @@
1
1
  import { FsTmp } from "../fs-tmp/index.js";
2
-
3
- //#region src/fs-fetch/index.d.ts
4
- declare class FsFetch extends FsTmp {
2
+ export declare class FsFetch extends FsTmp {
5
3
  cwd: string;
6
4
  constructor(cwd: string);
7
5
  assetToBuffer<const T extends string>(path: T): Promise<{
8
- readonly b64encodedData: `data:application/dash+xml;base64, ${string}` | `data:application/epub+zip;base64, ${string}` | `data:application/font-sfnt;base64, ${string}` | `data:application/gzip;base64, ${string}` | `data:application/java-archive;base64, ${string}` | `data:application/json5;base64, ${string}` | `data:application/json;base64, ${string}` | `data:application/jsonc;base64, ${string}` | `data:application/jsonl;base64, ${string}` | `data:application/ld+json;base64, ${string}` | `data:application/manifest+json;base64, ${string}` | `data:application/msword;base64, ${string}` | `data:application/node;base64, ${string}` | `data:application/octet-stream;base64, ${string}` | `data:application/ogg;base64, ${string}` | `data:application/pdf;base64, ${string}` | `data:application/rtf;base64, ${string}` | `data:application/sql;base64, ${string}` | `data:application/toml;base64, ${string}` | `data:application/typescript;base64, ${string}` | `data:application/vnd.amazon.ebook;base64, ${string}` | `data:application/vnd.apple.installer+xml;base64, ${string}` | `data:application/vnd.apple.mpegurl;base64, ${string}` | `data:application/vnd.apple.pkpass;base64, ${string}` | `data:application/vnd.mozilla.xul+xml;base64, ${string}` | `data:application/vnd.ms-excel;base64, ${string}` | `data:application/vnd.ms-fontobject;base64, ${string}` | `data:application/vnd.ms-powerpoint;base64, ${string}` | `data:application/vnd.oasis.opendocument.presentation;base64, ${string}` | `data:application/vnd.oasis.opendocument.spreadsheet;base64, ${string}` | `data:application/vnd.oasis.opendocument.text;base64, ${string}` | `data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64, ${string}` | `data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64, ${string}` | `data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64, ${string}` | `data:application/vnd.rar;base64, ${string}` | `data:application/vnd.visio;base64, ${string}` | `data:application/wasm;base64, ${string}` | `data:application/x-7z-compressed;base64, ${string}` | `data:application/x-abiword;base64, ${string}` | `data:application/x-bzip2;base64, ${string}` | `data:application/x-bzip;base64, ${string}` | `data:application/x-cdf;base64, ${string}` | `data:application/x-csh;base64, ${string}` | `data:application/x-freearc;base64, ${string}` | `data:application/x-gzip;base64, ${string}` | `data:application/x-httpd-php;base64, ${string}` | `data:application/x-mdx;base64, ${string}` | `data:application/x-ndjson;base64, ${string}` | `data:application/x-python-code;base64, ${string}` | `data:application/x-sh;base64, ${string}` | `data:application/x-tar;base64, ${string}` | `data:application/x-zip-compressed;base64, ${string}` | `data:application/xhtml+xml;base64, ${string}` | `data:application/xml;base64, ${string}` | `data:application/yaml;base64, ${string}` | `data:application/zip;base64, ${string}` | `data:audio/aac;base64, ${string}` | `data:audio/midi;base64, ${string}` | `data:audio/mp4;base64, ${string}` | `data:audio/mpeg;base64, ${string}` | `data:audio/ogg;base64, ${string}` | `data:audio/wav;base64, ${string}` | `data:audio/webm;base64, ${string}` | `data:audio/x-midi;base64, ${string}` | `data:font/otf;base64, ${string}` | `data:font/ttf;base64, ${string}` | `data:font/woff2;base64, ${string}` | `data:font/woff;base64, ${string}` | `data:haptics/hjif;base64, ${string}` | `data:haptics/hmpg;base64, ${string}` | `data:haptics/ivs;base64, ${string}` | `data:image/aces;base64, ${string}` | `data:image/apng;base64, ${string}` | `data:image/avci;base64, ${string}` | `data:image/avif;base64, ${string}` | `data:image/bmp;base64, ${string}` | `data:image/dpx;base64, ${string}` | `data:image/emf;base64, ${string}` | `data:image/gif;base64, ${string}` | `data:image/heic;base64, ${string}` | `data:image/jpeg;base64, ${string}` | `data:image/ktx2;base64, ${string}` | `data:image/ktx;base64, ${string}` | `data:image/png;base64, ${string}` | `data:image/svg+xml;base64, ${string}` | `data:image/tiff;base64, ${string}` | `data:image/vnd.microsoft.icon;base64, ${string}` | `data:image/webp;base64, ${string}` | `data:image/x-icon;base64, ${string}` | `data:model/gltf+json;base64, ${string}` | `data:model/gltf-binary;base64, ${string}` | `data:model/obj;base64, ${string}` | `data:model/vnd.usdz+zip;base64, ${string}` | `data:multipart/voice-message;base64, ${string}` | `data:text/calendar;base64, ${string}` | `data:text/css;base64, ${string}` | `data:text/csv;base64, ${string}` | `data:text/event-stream;base64, ${string}` | `data:text/html;base64, ${string}` | `data:text/javascript;base64, ${string}` | `data:text/markdown;base64, ${string}` | `data:text/plain;base64, ${string}` | `data:text/rust;base64, ${string}` | `data:text/typescript;base64, ${string}` | `data:text/vtt;base64, ${string}` | `data:text/x-c++;base64, ${string}` | `data:text/x-c;base64, ${string}` | `data:text/x-csharp;base64, ${string}` | `data:text/x-java;base64, ${string}` | `data:text/x-jsonl;base64, ${string}` | `data:text/x-php;base64, ${string}` | `data:text/x-python;base64, ${string}` | `data:text/x-ruby;base64, ${string}` | `data:text/x-script.python;base64, ${string}` | `data:text/x-tex;base64, ${string}` | `data:text/xml;base64, ${string}` | `data:video/3gpp2;base64, ${string}` | `data:video/3gpp;base64, ${string}` | `data:video/mp2t;base64, ${string}` | `data:video/mp4;base64, ${string}` | `data:video/mpeg;base64, ${string}` | `data:video/ogg;base64, ${string}` | `data:video/vnd.dlna.mpeg-tts;base64, ${string}` | `data:video/webm;base64, ${string}` | `data:video/x-msvideo;base64, ${string}`;
9
- readonly extension: "3g2" | "3gp" | "7z" | "aac" | "abw" | "aces" | "apng" | "arc" | "avci" | "avi" | "avif" | "azw" | "bin" | "bmp" | "bz" | "bz2" | "c" | "cda" | "cjs" | "cpp" | "cs" | "csh" | "css" | "csv" | "cur" | "doc" | "docx" | "dpx" | "emf" | "eot" | "epub" | "gif" | "glb" | "gltf" | "gz" | "heic" | "hjif" | "hmpg" | "htm" | "html" | "ico" | "ics" | "ivs" | "ivt" | "jar" | "java" | "jfif" | "jif" | "jpe" | "jpeg" | "jpg" | "js" | "json" | "json5" | "jsonc" | "jsonl" | "jsonld" | "jsonlines" | "ktx" | "ktx2" | "m3u8" | "m4a" | "m4s" | "m4v" | "md" | "mdx" | "mid" | "midi" | "mjs" | "mp3" | "mp4" | "mpd" | "mpeg" | "mpkg" | "mtlx" | "ndjson" | "node" | "obj" | "odp" | "ods" | "odt" | "oga" | "ogg" | "ogv" | "ogx" | "opus" | "otf" | "pdf" | "php" | "pjp" | "pjpeg" | "pk1" | "pkpass" | "png" | "ppt" | "pptx" | "py" | "pyc" | "rar" | "rb" | "rs" | "rtf" | "sh" | "sql" | "sse" | "svg" | "tar" | "tex" | "tif" | "tiff" | "toml" | "ts" | "ttf" | "txt" | "usdz" | "vsd" | "vtt" | "wasm" | "wav" | "weba" | "webm" | "webmanifest" | "webp" | "woff" | "woff2" | "xhtml" | "xls" | "xlsx" | "xml" | "xul" | "yaml" | "yml" | "zip";
6
+ readonly b64encodedData: `data:audio/aac;base64, ${string}` | `data:application/x-abiword;base64, ${string}` | `data:image/aces;base64, ${string}` | `data:image/apng;base64, ${string}` | `data:application/x-freearc;base64, ${string}` | `data:image/avci;base64, ${string}` | `data:image/avif;base64, ${string}` | `data:video/x-msvideo;base64, ${string}` | `data:application/vnd.amazon.ebook;base64, ${string}` | `data:application/octet-stream;base64, ${string}` | `data:multipart/voice-message;base64, ${string}` | `data:image/bmp;base64, ${string}` | `data:application/x-bzip;base64, ${string}` | `data:application/x-bzip2;base64, ${string}` | `data:text/x-c;base64, ${string}` | `data:application/x-cdf;base64, ${string}` | `data:text/javascript;base64, ${string}` | `data:application/node;base64, ${string}` | `data:text/x-c++;base64, ${string}` | `data:text/x-csharp;base64, ${string}` | `data:application/x-csh;base64, ${string}` | `data:text/css;base64, ${string}` | `data:text/csv;base64, ${string}` | `data:image/x-icon;base64, ${string}` | `data:application/msword;base64, ${string}` | `data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64, ${string}` | `data:image/dpx;base64, ${string}` | `data:image/emf;base64, ${string}` | `data:application/vnd.ms-fontobject;base64, ${string}` | `data:application/epub+zip;base64, ${string}` | `data:image/gif;base64, ${string}` | `data:model/gltf-binary;base64, ${string}` | `data:model/gltf+json;base64, ${string}` | `data:text/x-go;base64, ${string}` | `data:application/gzip;base64, ${string}` | `data:application/x-gzip;base64, ${string}` | `data:image/heic;base64, ${string}` | `data:haptics/hjif;base64, ${string}` | `data:haptics/hmpg;base64, ${string}` | `data:text/html;base64, ${string}` | `data:image/vnd.microsoft.icon;base64, ${string}` | `data:text/calendar;base64, ${string}` | `data:haptics/ivs;base64, ${string}` | `data:application/java-archive;base64, ${string}` | `data:text/x-java;base64, ${string}` | `data:image/jpeg;base64, ${string}` | `data:application/json;base64, ${string}` | `data:application/jsonl;base64, ${string}` | `data:text/x-jsonl;base64, ${string}` | `data:application/ld+json;base64, ${string}` | `data:application/jsonc;base64, ${string}` | `data:application/json5;base64, ${string}` | `data:image/ktx;base64, ${string}` | `data:image/ktx2;base64, ${string}` | `data:application/vnd.apple.mpegurl;base64, ${string}` | `data:audio/mp4;base64, ${string}` | `data:video/mp4;base64, ${string}` | `data:text/markdown;base64, ${string}` | `data:application/x-mdx;base64, ${string}` | `data:audio/midi;base64, ${string}` | `data:audio/x-midi;base64, ${string}` | `data:audio/mpeg;base64, ${string}` | `data:application/dash+xml;base64, ${string}` | `data:video/mpeg;base64, ${string}` | `data:application/vnd.apple.installer+xml;base64, ${string}` | `data:application/xml;base64, ${string}` | `data:application/x-ndjson;base64, ${string}` | `data:model/obj;base64, ${string}` | `data:text/plain;base64, ${string}` | `data:application/vnd.oasis.opendocument.presentation;base64, ${string}` | `data:application/vnd.oasis.opendocument.spreadsheet;base64, ${string}` | `data:application/vnd.oasis.opendocument.text;base64, ${string}` | `data:audio/ogg;base64, ${string}` | `data:video/ogg;base64, ${string}` | `data:application/ogg;base64, ${string}` | `data:font/otf;base64, ${string}` | `data:image/png;base64, ${string}` | `data:application/pdf;base64, ${string}` | `data:text/x-php;base64, ${string}` | `data:application/x-httpd-php;base64, ${string}` | `data:application/vnd.apple.pkpass;base64, ${string}` | `data:application/vnd.ms-powerpoint;base64, ${string}` | `data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64, ${string}` | `data:text/x-python;base64, ${string}` | `data:text/x-script.python;base64, ${string}` | `data:application/x-python-code;base64, ${string}` | `data:application/vnd.rar;base64, ${string}` | `data:text/x-ruby;base64, ${string}` | `data:text/rust;base64, ${string}` | `data:application/rtf;base64, ${string}` | `data:application/x-sh;base64, ${string}` | `data:application/sql;base64, ${string}` | `data:text/event-stream;base64, ${string}` | `data:image/svg+xml;base64, ${string}` | `data:application/x-tar;base64, ${string}` | `data:text/x-tex;base64, ${string}` | `data:image/tiff;base64, ${string}` | `data:application/toml;base64, ${string}` | `data:text/typescript;base64, ${string}` | `data:application/typescript;base64, ${string}` | `data:video/mp2t;base64, ${string}` | `data:video/vnd.dlna.mpeg-tts;base64, ${string}` | `data:application/font-sfnt;base64, ${string}` | `data:font/ttf;base64, ${string}` | `data:model/vnd.usdz+zip;base64, ${string}` | `data:application/vnd.visio;base64, ${string}` | `data:text/vtt;base64, ${string}` | `data:application/wasm;base64, ${string}` | `data:audio/wav;base64, ${string}` | `data:audio/webm;base64, ${string}` | `data:video/webm;base64, ${string}` | `data:application/manifest+json;base64, ${string}` | `data:image/webp;base64, ${string}` | `data:font/woff;base64, ${string}` | `data:font/woff2;base64, ${string}` | `data:application/xhtml+xml;base64, ${string}` | `data:application/vnd.ms-excel;base64, ${string}` | `data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64, ${string}` | `data:text/xml;base64, ${string}` | `data:application/vnd.mozilla.xul+xml;base64, ${string}` | `data:application/yaml;base64, ${string}` | `data:application/zip;base64, ${string}` | `data:application/x-zip-compressed;base64, ${string}` | `data:video/3gpp;base64, ${string}` | `data:video/3gpp2;base64, ${string}` | `data:application/x-7z-compressed;base64, ${string}`;
7
+ readonly extension: "mpd" | "epub" | "ttf" | "gz" | "jar" | "json" | "jsonc" | "jsonl" | "jsonlines" | "json5" | "jsonld" | "webmanifest" | "doc" | "node" | "js" | "bin" | "obj" | "ogx" | "pdf" | "rtf" | "sql" | "md" | "toml" | "azw" | "mpkg" | "m3u8" | "pkpass" | "xul" | "xls" | "eot" | "ppt" | "odp" | "ods" | "odt" | "pptx" | "xlsx" | "docx" | "rar" | "vsd" | "wasm" | "7z" | "abw" | "bz" | "bz2" | "cda" | "csh" | "arc" | "php" | "mdx" | "ndjson" | "pyc" | "sh" | "tar" | "zip" | "xhtml" | "xml" | "yml" | "yaml" | "aac" | "mid" | "m4a" | "mp3" | "opus" | "ogg" | "oga" | "wav" | "weba" | "midi" | "otf" | "woff" | "woff2" | "hjif" | "hmpg" | "ivs" | "ivt" | "aces" | "apng" | "avci" | "avif" | "bmp" | "dpx" | "emf" | "gif" | "heic" | "jpg" | "jpeg" | "ktx" | "ktx2" | "png" | "svg" | "tiff" | "ico" | "webp" | "cur" | "glb" | "gltf" | "usdz" | "ics" | "css" | "csv" | "sse" | "ts" | "rs" | "py" | "txt" | "html" | "htm" | "cjs" | "mjs" | "vtt" | "c" | "cpp" | "cs" | "go" | "java" | "rb" | "tex" | "3gp" | "3g2" | "mp4" | "m4v" | "m4s" | "mpeg" | "ogv" | "avi" | "jfif" | "jif" | "jpe" | "mtlx" | "pk1" | "pjp" | "pjpeg" | "tif" | "webm";
10
8
  }>;
11
9
  assetToBufferView<const T extends string>(path: T): Promise<{
12
10
  readonly b64encodedData: string;
13
- readonly extension: "3g2" | "3gp" | "7z" | "aac" | "abw" | "aces" | "apng" | "arc" | "avci" | "avi" | "avif" | "azw" | "bin" | "bmp" | "bz" | "bz2" | "c" | "cda" | "cjs" | "cpp" | "cs" | "csh" | "css" | "csv" | "cur" | "doc" | "docx" | "dpx" | "emf" | "eot" | "epub" | "gif" | "glb" | "gltf" | "gz" | "heic" | "hjif" | "hmpg" | "htm" | "html" | "ico" | "ics" | "ivs" | "ivt" | "jar" | "java" | "jfif" | "jif" | "jpe" | "jpeg" | "jpg" | "js" | "json" | "json5" | "jsonc" | "jsonl" | "jsonld" | "jsonlines" | "ktx" | "ktx2" | "m3u8" | "m4a" | "m4s" | "m4v" | "md" | "mdx" | "mid" | "midi" | "mjs" | "mp3" | "mp4" | "mpd" | "mpeg" | "mpkg" | "mtlx" | "ndjson" | "node" | "obj" | "odp" | "ods" | "odt" | "oga" | "ogg" | "ogv" | "ogx" | "opus" | "otf" | "pdf" | "php" | "pjp" | "pjpeg" | "pk1" | "pkpass" | "png" | "ppt" | "pptx" | "py" | "pyc" | "rar" | "rb" | "rs" | "rtf" | "sh" | "sql" | "sse" | "svg" | "tar" | "tex" | "tif" | "tiff" | "toml" | "ts" | "ttf" | "txt" | "usdz" | "vsd" | "vtt" | "wasm" | "wav" | "weba" | "webm" | "webmanifest" | "webp" | "woff" | "woff2" | "xhtml" | "xls" | "xlsx" | "xml" | "xul" | "yaml" | "yml" | "zip" | undefined;
11
+ readonly extension: "mpd" | "epub" | "ttf" | "gz" | "jar" | "json" | "jsonc" | "jsonl" | "jsonlines" | "json5" | "jsonld" | "webmanifest" | "doc" | "node" | "js" | "bin" | "obj" | "ogx" | "pdf" | "rtf" | "sql" | "md" | "toml" | "azw" | "mpkg" | "m3u8" | "pkpass" | "xul" | "xls" | "eot" | "ppt" | "odp" | "ods" | "odt" | "pptx" | "xlsx" | "docx" | "rar" | "vsd" | "wasm" | "7z" | "abw" | "bz" | "bz2" | "cda" | "csh" | "arc" | "php" | "mdx" | "ndjson" | "pyc" | "sh" | "tar" | "zip" | "xhtml" | "xml" | "yml" | "yaml" | "aac" | "mid" | "m4a" | "mp3" | "opus" | "ogg" | "oga" | "wav" | "weba" | "midi" | "otf" | "woff" | "woff2" | "hjif" | "hmpg" | "ivs" | "ivt" | "aces" | "apng" | "avci" | "avif" | "bmp" | "dpx" | "emf" | "gif" | "heic" | "jpg" | "jpeg" | "ktx" | "ktx2" | "png" | "svg" | "tiff" | "ico" | "webp" | "cur" | "glb" | "gltf" | "usdz" | "ics" | "css" | "csv" | "sse" | "ts" | "rs" | "py" | "txt" | "html" | "htm" | "cjs" | "mjs" | "vtt" | "c" | "cpp" | "cs" | "go" | "java" | "rb" | "tex" | "3gp" | "3g2" | "mp4" | "m4v" | "m4s" | "mpeg" | "ogv" | "avi" | "jfif" | "jif" | "jpe" | "mtlx" | "pk1" | "pjp" | "pjpeg" | "tif" | "webm" | undefined;
14
12
  }>;
15
13
  /**
16
14
  *
@@ -28,6 +26,7 @@ declare class FsFetch extends FsTmp {
28
26
  private knownToBlockHead;
29
27
  getUrlExt(url: string): string | null;
30
28
  private fuzzyExtEquality;
29
+ private sniffFile;
31
30
  /**
32
31
  *
33
32
  * @param inputUrl remote url to fetch data from
@@ -45,8 +44,6 @@ declare class FsFetch extends FsTmp {
45
44
  * larger files that may not fit into memory (it intelligently determines the best approach on the fly)
46
45
  * It streams the data directly to disk for files of all sizes instead of loading it all into memory
47
46
  */
48
- fetchRemoteWriteLocalLargeFiles<const I extends string, const O extends string>(inputUrl: I, outputPathI: O, useDetectedExtension?: boolean): Promise<void>;
47
+ fetchRemoteWriteLocalLargeFiles<const I extends string, const O extends string>(inputUrl: I, outputPathI: O, useDetectedExtension?: boolean, verbose?: boolean): Promise<void>;
49
48
  }
50
- //#endregion
51
- export { FsFetch };
52
49
  //# sourceMappingURL=index.d.ts.map
@@ -1,9 +1,11 @@
1
- import { FsTmp } from "../fs-tmp/index.js";
2
1
  import fsSync from "fs";
2
+ import { open, rename } from "fs/promises";
3
3
  import { relative } from "path";
4
-
5
- //#region src/fs-fetch/index.ts
4
+ import { FsTmp } from "../fs-tmp/index.js";
5
+ import { sniffFromRanges } from "@d0paminedriven/mimedown";
6
+ // src/fs-fetch/index.ts
6
7
  var FsFetch = class extends FsTmp {
8
+ cwd;
7
9
  constructor(cwd) {
8
10
  super(cwd ??= process.cwd());
9
11
  this.cwd = cwd;
@@ -84,6 +86,34 @@ var FsFetch = class extends FsTmp {
84
86
  const bNorm = b.toLowerCase();
85
87
  return this.calculateLD(aNorm, bNorm) / (Math.max(aNorm.length, bNorm.length) || 1) <= .34;
86
88
  }
89
+ async sniffFile(path) {
90
+ const file = await open(path, "r");
91
+ try {
92
+ const { size } = await file.stat();
93
+ const pieces = Array.of();
94
+ const take = async (offset, length) => {
95
+ const bytes = Buffer.alloc(Math.max(0, Math.min(length, size - offset)));
96
+ const { bytesRead } = await file.read(bytes, 0, bytes.byteLength, offset);
97
+ pieces.push({
98
+ offset,
99
+ bytes: bytes.subarray(0, bytesRead)
100
+ });
101
+ };
102
+ await take(0, 4096);
103
+ let found = sniffFromRanges(pieces, size, { retryOnUnknown: true });
104
+ for (let asked = 0; found.needs !== null && asked < 8; asked++) {
105
+ const { offset, length, fromEnd } = found.needs;
106
+ await take(fromEnd ? Math.max(0, size - offset) : offset, length);
107
+ found = sniffFromRanges(pieces, size, { retryOnUnknown: true });
108
+ }
109
+ return {
110
+ ...found,
111
+ size
112
+ };
113
+ } finally {
114
+ await file.close();
115
+ }
116
+ }
87
117
  /**
88
118
  *
89
119
  * @param inputUrl remote url to fetch data from
@@ -101,48 +131,55 @@ var FsFetch = class extends FsTmp {
101
131
  * larger files that may not fit into memory (it intelligently determines the best approach on the fly)
102
132
  * It streams the data directly to disk for files of all sizes instead of loading it all into memory
103
133
  */
104
- async fetchRemoteWriteLocalLargeFiles(inputUrl, outputPathI, useDetectedExtension = true) {
134
+ async fetchRemoteWriteLocalLargeFiles(inputUrl, outputPathI, useDetectedExtension = true, verbose = false) {
105
135
  if (!URL.canParse(inputUrl)) throw new Error(`invalid URL ${inputUrl} is unable to be parsed`);
106
136
  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
137
  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({}));
138
+ if (!res.ok || !res.body) throw new Error(`Failed to fetch asset: ${res.status} ${res.statusText}`);
139
+ const partial = `${outputPathI}.part`;
140
+ this.generateDirIfDNE(this.pathHandler(partial), { recursive: true });
141
+ const writeStream = fsSync.createWriteStream(relative(this.cwd, partial));
142
+ const { promise, reject, resolve } = Promise.withResolvers();
143
+ writeStream.on("finish", () => resolve({}));
127
144
  writeStream.on("error", () => reject({}));
128
145
  const reader = res.body.getReader();
129
146
  const pump = async () => {
130
147
  while (true) {
131
- const { done, value: value$1 } = await reader.read();
148
+ const { done, value } = await reader.read();
132
149
  if (done) break;
133
- writeStream.write(value$1);
150
+ writeStream.write(value);
134
151
  }
135
152
  writeStream.end();
136
153
  };
137
154
  await pump();
138
155
  await promise.then(() => {});
156
+ const { evidence, ext, mime, parser, retry, topLevel, size } = await this.sniffFile(relative(this.cwd, partial));
157
+ const { unit, value } = this.autoFileSizeRaw(size);
158
+ if (verbose) console.log({
159
+ extension: ext,
160
+ topLevelMime: topLevel,
161
+ mime,
162
+ parser,
163
+ retry,
164
+ evidence,
165
+ size: `${value} ${unit}`
166
+ });
167
+ let formattedPath;
168
+ if (useDetectedExtension) formattedPath = `${outputPathI}.${ext}`;
169
+ else {
170
+ const lastDot = outputPathI.lastIndexOf(".");
171
+ if (lastDot === 1 || lastDot === -1) formattedPath = `${outputPathI}.${ext}`;
172
+ const outputPathExt = outputPathI.slice(outputPathI.lastIndexOf(".") + 1);
173
+ if (this.fuzzyExtEquality(ext, outputPathExt) === true) formattedPath = outputPathI;
174
+ else formattedPath = outputPathI.replace(outputPathExt, ext);
175
+ }
176
+ await rename(relative(this.cwd, partial), relative(this.cwd, formattedPath));
139
177
  return;
140
178
  } catch (err) {
141
179
  console.error(`[fetchRemoteWriteLocalLargeFiles error]:`, err);
142
180
  }
143
181
  }
144
182
  };
145
-
146
- //#endregion
147
183
  export { FsFetch };
184
+
148
185
  //# sourceMappingURL=index.js.map
@@ -1,20 +1,16 @@
1
1
  import { SizeOpts, Unit } from "../types/index.js";
2
2
  import { FsAtomic } from "../fs-atomic/index.js";
3
-
4
- //#region src/fs-size/index.d.ts
5
- declare class FsSize extends FsAtomic {
3
+ export declare class FsSize extends FsAtomic {
6
4
  cwd: string;
7
5
  constructor(cwd: string);
8
6
  private units;
9
7
  private u;
10
8
  autoFileSizeRaw(size: number | bigint): {
11
9
  value: number;
12
- unit: "B" | "GB" | "KB" | "MB" | "PB" | "TB" | undefined;
10
+ unit: "B" | "KB" | "MB" | "GB" | "TB" | "PB" | undefined;
13
11
  };
14
12
  getSize<const S extends Unit | Lowercase<Unit> | "auto">(size: number | bigint, target: S, opts?: SizeOpts): string | number;
15
13
  fileSizeMb<const T extends string>(path: T): number;
16
14
  fileSize<const T extends string, const S extends Unit | Lowercase<Unit> | "auto">(path: T, target: S, opts?: SizeOpts): string | number;
17
15
  }
18
- //#endregion
19
- export { FsSize };
20
16
  //# sourceMappingURL=index.d.ts.map