@fgv/ts-extras 5.0.0-0 → 5.0.0-3

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/README.md CHANGED
@@ -1,17 +1,23 @@
1
1
  <div align="center">
2
- <h1>ts-utils</h1>
3
- Assorted Typescript Utilities
2
+ <h1>ts-extras</h1>
3
+ Assorted TypeScript Utilities
4
4
  </div>
5
5
 
6
6
  <hr/>
7
7
 
8
8
  ## Summary
9
9
 
10
- Assorted less-developed or more specialized odds-and-ends borrowed from one project or another - much less polished and more likely change or disappear:
11
- * ExtendedArray\<T\> - adds a few useful operations to the built-in Array
12
- * Formattable\<T\> - simple helpers to create mustache wrappers for objects and make them easily printable
13
- * Logger - A very basic logger suitable for hobby projects
14
- * RangeOf\<T\> - Generic open or closed ranges of orderable items (numbers, dates, etc)
10
+ Assorted less-developed or more specialized utilities borrowed from various projects - much less polished and more likely to change or disappear:
11
+
12
+ * **ExtendedArray\<T\>** - adds useful operations to the built-in Array
13
+ * **Formattable\<T\>** - simple helpers to create mustache wrappers for objects and make them easily printable
14
+ * **Logger** - A very basic logger suitable for hobby projects
15
+ * **RangeOf\<T\>** - Generic open or closed ranges of orderable items (numbers, dates, etc)
16
+ * **ZIP FileTree** - FileTree implementation for reading from ZIP archives (Node.js)
17
+ * **Converters** - Type-safe data conversion utilities
18
+ * **CSV Helpers** - Utilities for CSV processing
19
+ * **Hash Utilities** - MD5 normalization and hashing utilities
20
+ * **RecordJar Helpers** - Utilities for record collection management
15
21
 
16
22
  ---
17
23
 
@@ -24,6 +30,9 @@ Assorted less-developed or more specialized odds-and-ends borrowed from one proj
24
30
  - [Formattable\<T\>](#formattablet)
25
31
  - [Logger](#logger)
26
32
  - [RangeOf\<T\>](#rangeoft)
33
+ - [ZIP FileTree](#zip-filetree)
34
+ - [Converters](#converters)
35
+ - [Other Utilities](#other-utilities)
27
36
 
28
37
  ## Installation
29
38
 
@@ -37,12 +46,65 @@ Extracted API documentation is [here](./docs/ts-extras.md).
37
46
 
38
47
  ## Overview
39
48
 
49
+ This package provides various utility functions and classes that are commonly needed across TypeScript projects, particularly those working with data processing, file handling, and type-safe operations.
50
+
40
51
  ## API
41
52
 
42
53
  ### ExtendedArray\<T\>
43
54
 
55
+ Extended array functionality with additional operations beyond the built-in Array methods.
56
+
44
57
  ### Formattable\<T\>
45
58
 
59
+ Simple helpers for creating mustache-style wrappers around objects to make them easily printable and templatable.
60
+
46
61
  ### Logger
47
62
 
63
+ A basic logging utility suitable for development and hobby projects.
64
+
48
65
  ### RangeOf\<T\>
66
+
67
+ Generic implementation for representing open or closed ranges of orderable items like numbers, dates, or other comparable values.
68
+
69
+ ### ZIP FileTree
70
+
71
+ **Node.js-compatible** FileTree implementation for reading from ZIP archives using AdmZip:
72
+
73
+ ```typescript
74
+ import { ZipFileTree } from '@fgv/ts-extras';
75
+ import { FileTree } from '@fgv/ts-utils';
76
+
77
+ // Create ZIP FileTree from buffer
78
+ const zipAccessors = ZipFileTree.ZipFileTreeAccessors.fromBuffer(zipBuffer);
79
+ const fileTree = FileTree.FileTree.create(zipAccessors.value);
80
+
81
+ // Access files and directories
82
+ const file = fileTree.value.getFile('/path/to/file.json');
83
+ const contents = file.value.getContents(); // Parsed JSON
84
+ const rawContents = file.value.getRawContents(); // Raw string
85
+ ```
86
+
87
+ **Note**: This implementation uses Node.js-specific dependencies (AdmZip, Buffer). For browser environments, see the browser-specific implementations in individual projects.
88
+
89
+ ### Converters
90
+
91
+ Type-safe data conversion utilities for transforming between different data formats while maintaining type safety.
92
+
93
+ ### Other Utilities
94
+
95
+ - **CSV Helpers**: Utilities for processing CSV data
96
+ - **Hash Utilities**: MD5 normalization and hashing functions
97
+ - **RecordJar Helpers**: Utilities for managing record collections and data structures
98
+
99
+ ## Dependencies
100
+
101
+ This package depends on:
102
+ - `@fgv/ts-utils` - Core utilities and Result pattern
103
+ - `adm-zip` - ZIP file processing (Node.js only)
104
+ - Various other utility packages for specific functionality
105
+
106
+ ## Platform Notes
107
+
108
+ - **ZIP FileTree**: Node.js only (uses AdmZip and Buffer)
109
+ - **Other utilities**: Cross-platform compatible
110
+ - **Browser usage**: Most utilities work in browsers, but ZIP functionality requires browser-specific implementations
@@ -1,7 +1,9 @@
1
1
  import { Conversion } from '@fgv/ts-utils';
2
2
  import { Converter } from '@fgv/ts-utils';
3
+ import { FileTree } from '@fgv/ts-utils';
3
4
  import { Hash as Hash_2 } from '@fgv/ts-utils';
4
5
  import { Result } from '@fgv/ts-utils';
6
+ import { Validator } from '@fgv/ts-utils';
5
7
 
6
8
  declare namespace Converters {
7
9
  export {
@@ -422,4 +424,172 @@ export { RecordJar }
422
424
  */
423
425
  declare function templateString(defaultContext?: unknown): Conversion.StringConverter<string, unknown>;
424
426
 
427
+ /**
428
+ * Implementation of `IFileTreeDirectoryItem` for directories in a ZIP archive.
429
+ * @public
430
+ */
431
+ declare class ZipDirectoryItem implements FileTree.IFileTreeDirectoryItem {
432
+ /**
433
+ * Indicates that this `FileTree.FileTreeItem` is a directory.
434
+ */
435
+ readonly type: 'directory';
436
+ /**
437
+ * The absolute path of the directory within the ZIP archive.
438
+ */
439
+ readonly absolutePath: string;
440
+ /**
441
+ * The name of the directory
442
+ */
443
+ readonly name: string;
444
+ /**
445
+ * The ZIP file tree accessors that created this item.
446
+ */
447
+ private readonly _accessors;
448
+ /**
449
+ * Constructor for ZipDirectoryItem.
450
+ * @param directoryPath - The path of the directory within the ZIP.
451
+ * @param accessors - The ZIP file tree accessors.
452
+ */
453
+ constructor(directoryPath: string, accessors: ZipFileTreeAccessors);
454
+ /**
455
+ * Gets the children of the directory.
456
+ */
457
+ getChildren(): Result<ReadonlyArray<FileTree.FileTreeItem>>;
458
+ }
459
+
460
+ /**
461
+ * Implementation of `FileTree.IFileTreeFileItem` for files in a ZIP archive.
462
+ * @public
463
+ */
464
+ declare class ZipFileItem implements FileTree.IFileTreeFileItem {
465
+ /**
466
+ * Indicates that this `FileTree.FileTreeItem` is a file.
467
+ */
468
+ readonly type: 'file';
469
+ /**
470
+ * The absolute path of the file within the ZIP archive.
471
+ */
472
+ readonly absolutePath: string;
473
+ /**
474
+ * The name of the file
475
+ */
476
+ readonly name: string;
477
+ /**
478
+ * The base name of the file (without extension)
479
+ */
480
+ readonly baseName: string;
481
+ /**
482
+ * The extension of the file
483
+ */
484
+ readonly extension: string;
485
+ /**
486
+ * The pre-loaded contents of the file.
487
+ */
488
+ private readonly _contents;
489
+ /**
490
+ * The ZIP file tree accessors that created this item.
491
+ */
492
+ private readonly _accessors;
493
+ /**
494
+ * Constructor for ZipFileItem.
495
+ * @param zipFilePath - The path of the file within the ZIP.
496
+ * @param contents - The pre-loaded contents of the file.
497
+ * @param accessors - The ZIP file tree accessors.
498
+ */
499
+ constructor(zipFilePath: string, contents: string, accessors: ZipFileTreeAccessors);
500
+ /**
501
+ * Gets the contents of the file as parsed JSON.
502
+ */
503
+ getContents(): Result<unknown>;
504
+ getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;
505
+ /**
506
+ * Gets the raw contents of the file as a string.
507
+ */
508
+ getRawContents(): Result<string>;
509
+ }
510
+
511
+ declare namespace ZipFileTree {
512
+ export {
513
+ ZipFileTreeAccessors,
514
+ ZipFileItem,
515
+ ZipDirectoryItem
516
+ }
517
+ }
518
+ export { ZipFileTree }
519
+
520
+ /**
521
+ * File tree accessors for ZIP archives.
522
+ * @public
523
+ */
524
+ declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors {
525
+ /**
526
+ * The AdmZip instance containing the archive.
527
+ */
528
+ private readonly _zip;
529
+ /**
530
+ * Optional prefix to prepend to paths.
531
+ */
532
+ private readonly _prefix;
533
+ /**
534
+ * Cache of all items in the ZIP for efficient lookups.
535
+ */
536
+ private readonly _itemCache;
537
+ /**
538
+ * Constructor for ZipFileTreeAccessors.
539
+ * @param zip - The AdmZip instance.
540
+ * @param prefix - Optional prefix to prepend to paths.
541
+ */
542
+ private constructor();
543
+ /**
544
+ * Creates a new ZipFileTreeAccessors instance from a ZIP file buffer.
545
+ * @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
546
+ * @param prefix - Optional prefix to prepend to paths.
547
+ * @returns Result containing the ZipFileTreeAccessors instance.
548
+ */
549
+ static fromBuffer(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Result<ZipFileTreeAccessors>;
550
+ /**
551
+ * Creates a new ZipFileTreeAccessors instance from a File object (browser environment).
552
+ * @param file - The File object containing ZIP data.
553
+ * @param prefix - Optional prefix to prepend to paths.
554
+ * @returns Result containing the ZipFileTreeAccessors instance.
555
+ */
556
+ static fromFile(file: File, prefix?: string): Promise<Result<ZipFileTreeAccessors>>;
557
+ /**
558
+ * Builds the cache of all items in the ZIP archive.
559
+ */
560
+ private _buildItemCache;
561
+ /**
562
+ * Resolves paths to an absolute path.
563
+ */
564
+ resolveAbsolutePath(...paths: string[]): string;
565
+ /**
566
+ * Gets the extension of a path.
567
+ */
568
+ getExtension(path: string): string;
569
+ /**
570
+ * Gets the base name of a path.
571
+ */
572
+ getBaseName(path: string, suffix?: string): string;
573
+ /**
574
+ * Joins paths together.
575
+ */
576
+ joinPaths(...paths: string[]): string;
577
+ /**
578
+ * Gets an item from the file tree.
579
+ */
580
+ getItem(path: string): Result<FileTree.FileTreeItem>;
581
+ /**
582
+ * Gets the contents of a file in the file tree.
583
+ */
584
+ getFileContents(path: string): Result<string>;
585
+ /**
586
+ * Gets the children of a directory in the file tree.
587
+ */
588
+ getChildren(path: string): Result<ReadonlyArray<FileTree.FileTreeItem>>;
589
+ /**
590
+ * Checks if childPath is a direct child of parentPath.
591
+ */
592
+ private _isDirectChild;
593
+ }
594
+
425
595
  export { }
package/lib/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import * as Csv from './packlets/csv';
2
2
  import * as Experimental from './packlets/experimental';
3
3
  import * as Hash from './packlets/hash';
4
4
  import * as RecordJar from './packlets/record-jar';
5
+ import * as ZipFileTree from './packlets/zip-file-tree';
5
6
  import { Converters } from './packlets/conversion';
6
- export { Converters, Csv, Experimental, Hash, RecordJar };
7
+ export { Converters, Csv, Experimental, Hash, RecordJar, ZipFileTree };
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,YAAY,MAAM,yBAAyB,CAAC;AACxD,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAC;AACxC,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAC;AAEnD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AACtC,OAAO,KAAK,YAAY,MAAM,yBAAyB,CAAC;AACxD,OAAO,KAAK,IAAI,MAAM,iBAAiB,CAAC;AACxC,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAC;AACnD,OAAO,KAAK,WAAW,MAAM,0BAA0B,CAAC;AAExD,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AAEnD,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,CAAC"}
package/lib/index.js CHANGED
@@ -54,7 +54,7 @@ var __importStar = (this && this.__importStar) || (function () {
54
54
  };
55
55
  })();
56
56
  Object.defineProperty(exports, "__esModule", { value: true });
57
- exports.RecordJar = exports.Hash = exports.Experimental = exports.Csv = exports.Converters = void 0;
57
+ exports.ZipFileTree = exports.RecordJar = exports.Hash = exports.Experimental = exports.Csv = exports.Converters = void 0;
58
58
  const Csv = __importStar(require("./packlets/csv"));
59
59
  exports.Csv = Csv;
60
60
  const Experimental = __importStar(require("./packlets/experimental"));
@@ -63,6 +63,8 @@ const Hash = __importStar(require("./packlets/hash"));
63
63
  exports.Hash = Hash;
64
64
  const RecordJar = __importStar(require("./packlets/record-jar"));
65
65
  exports.RecordJar = RecordJar;
66
+ const ZipFileTree = __importStar(require("./packlets/zip-file-tree"));
67
+ exports.ZipFileTree = ZipFileTree;
66
68
  const conversion_1 = require("./packlets/conversion");
67
69
  Object.defineProperty(exports, "Converters", { enumerable: true, get: function () { return conversion_1.Converters; } });
68
70
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,oDAAsC;AAOjB,kBAAG;AANxB,sEAAwD;AAM9B,oCAAY;AALtC,sDAAwC;AAKA,oBAAI;AAJ5C,iEAAmD;AAIL,8BAAS;AAFvD,sDAAmD;AAE1C,2FAFA,uBAAU,OAEA","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport * as Csv from './packlets/csv';\nimport * as Experimental from './packlets/experimental';\nimport * as Hash from './packlets/hash';\nimport * as RecordJar from './packlets/record-jar';\n\nimport { Converters } from './packlets/conversion';\n\nexport { Converters, Csv, Experimental, Hash, RecordJar };\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,oDAAsC;AAQjB,kBAAG;AAPxB,sEAAwD;AAO9B,oCAAY;AANtC,sDAAwC;AAMA,oBAAI;AAL5C,iEAAmD;AAKL,8BAAS;AAJvD,sEAAwD;AAIC,kCAAW;AAFpE,sDAAmD;AAE1C,2FAFA,uBAAU,OAEA","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport * as Csv from './packlets/csv';\nimport * as Experimental from './packlets/experimental';\nimport * as Hash from './packlets/hash';\nimport * as RecordJar from './packlets/record-jar';\nimport * as ZipFileTree from './packlets/zip-file-tree';\n\nimport { Converters } from './packlets/conversion';\n\nexport { Converters, Csv, Experimental, Hash, RecordJar, ZipFileTree };\n"]}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * ZIP-based FileTree implementation for ts-extras.
3
+ *
4
+ * This packlet provides a FileTree accessor implementation that can read from ZIP archives,
5
+ * making it useful for browser environments where files need to be bundled and transferred
6
+ * as a single archive.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+ export { ZipFileTreeAccessors, ZipFileItem, ZipDirectoryItem } from './zipFileTreeAccessors';
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/packlets/zip-file-tree/index.ts"],"names":[],"mappings":"AAsBA;;;;;;;;GAQG;AAEH,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC"}
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.ZipDirectoryItem = exports.ZipFileItem = exports.ZipFileTreeAccessors = void 0;
25
+ /**
26
+ * ZIP-based FileTree implementation for ts-extras.
27
+ *
28
+ * This packlet provides a FileTree accessor implementation that can read from ZIP archives,
29
+ * making it useful for browser environments where files need to be bundled and transferred
30
+ * as a single archive.
31
+ *
32
+ * @packageDocumentation
33
+ */
34
+ var zipFileTreeAccessors_1 = require("./zipFileTreeAccessors");
35
+ Object.defineProperty(exports, "ZipFileTreeAccessors", { enumerable: true, get: function () { return zipFileTreeAccessors_1.ZipFileTreeAccessors; } });
36
+ Object.defineProperty(exports, "ZipFileItem", { enumerable: true, get: function () { return zipFileTreeAccessors_1.ZipFileItem; } });
37
+ Object.defineProperty(exports, "ZipDirectoryItem", { enumerable: true, get: function () { return zipFileTreeAccessors_1.ZipDirectoryItem; } });
38
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/packlets/zip-file-tree/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH;;;;;;;;GAQG;AAEH,+DAA6F;AAApF,4HAAA,oBAAoB,OAAA;AAAE,mHAAA,WAAW,OAAA;AAAE,wHAAA,gBAAgB,OAAA","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/**\n * ZIP-based FileTree implementation for ts-extras.\n *\n * This packlet provides a FileTree accessor implementation that can read from ZIP archives,\n * making it useful for browser environments where files need to be bundled and transferred\n * as a single archive.\n *\n * @packageDocumentation\n */\n\nexport { ZipFileTreeAccessors, ZipFileItem, ZipDirectoryItem } from './zipFileTreeAccessors';\n"]}
@@ -0,0 +1,158 @@
1
+ import { Result, Converter, Validator, FileTree } from '@fgv/ts-utils';
2
+ /**
3
+ * Implementation of `FileTree.IFileTreeFileItem` for files in a ZIP archive.
4
+ * @public
5
+ */
6
+ export declare class ZipFileItem implements FileTree.IFileTreeFileItem {
7
+ /**
8
+ * Indicates that this `FileTree.FileTreeItem` is a file.
9
+ */
10
+ readonly type: 'file';
11
+ /**
12
+ * The absolute path of the file within the ZIP archive.
13
+ */
14
+ readonly absolutePath: string;
15
+ /**
16
+ * The name of the file
17
+ */
18
+ readonly name: string;
19
+ /**
20
+ * The base name of the file (without extension)
21
+ */
22
+ readonly baseName: string;
23
+ /**
24
+ * The extension of the file
25
+ */
26
+ readonly extension: string;
27
+ /**
28
+ * The pre-loaded contents of the file.
29
+ */
30
+ private readonly _contents;
31
+ /**
32
+ * The ZIP file tree accessors that created this item.
33
+ */
34
+ private readonly _accessors;
35
+ /**
36
+ * Constructor for ZipFileItem.
37
+ * @param zipFilePath - The path of the file within the ZIP.
38
+ * @param contents - The pre-loaded contents of the file.
39
+ * @param accessors - The ZIP file tree accessors.
40
+ */
41
+ constructor(zipFilePath: string, contents: string, accessors: ZipFileTreeAccessors);
42
+ /**
43
+ * Gets the contents of the file as parsed JSON.
44
+ */
45
+ getContents(): Result<unknown>;
46
+ getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;
47
+ /**
48
+ * Gets the raw contents of the file as a string.
49
+ */
50
+ getRawContents(): Result<string>;
51
+ }
52
+ /**
53
+ * Implementation of `IFileTreeDirectoryItem` for directories in a ZIP archive.
54
+ * @public
55
+ */
56
+ export declare class ZipDirectoryItem implements FileTree.IFileTreeDirectoryItem {
57
+ /**
58
+ * Indicates that this `FileTree.FileTreeItem` is a directory.
59
+ */
60
+ readonly type: 'directory';
61
+ /**
62
+ * The absolute path of the directory within the ZIP archive.
63
+ */
64
+ readonly absolutePath: string;
65
+ /**
66
+ * The name of the directory
67
+ */
68
+ readonly name: string;
69
+ /**
70
+ * The ZIP file tree accessors that created this item.
71
+ */
72
+ private readonly _accessors;
73
+ /**
74
+ * Constructor for ZipDirectoryItem.
75
+ * @param directoryPath - The path of the directory within the ZIP.
76
+ * @param accessors - The ZIP file tree accessors.
77
+ */
78
+ constructor(directoryPath: string, accessors: ZipFileTreeAccessors);
79
+ /**
80
+ * Gets the children of the directory.
81
+ */
82
+ getChildren(): Result<ReadonlyArray<FileTree.FileTreeItem>>;
83
+ }
84
+ /**
85
+ * File tree accessors for ZIP archives.
86
+ * @public
87
+ */
88
+ export declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors {
89
+ /**
90
+ * The AdmZip instance containing the archive.
91
+ */
92
+ private readonly _zip;
93
+ /**
94
+ * Optional prefix to prepend to paths.
95
+ */
96
+ private readonly _prefix;
97
+ /**
98
+ * Cache of all items in the ZIP for efficient lookups.
99
+ */
100
+ private readonly _itemCache;
101
+ /**
102
+ * Constructor for ZipFileTreeAccessors.
103
+ * @param zip - The AdmZip instance.
104
+ * @param prefix - Optional prefix to prepend to paths.
105
+ */
106
+ private constructor();
107
+ /**
108
+ * Creates a new ZipFileTreeAccessors instance from a ZIP file buffer.
109
+ * @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
110
+ * @param prefix - Optional prefix to prepend to paths.
111
+ * @returns Result containing the ZipFileTreeAccessors instance.
112
+ */
113
+ static fromBuffer(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Result<ZipFileTreeAccessors>;
114
+ /**
115
+ * Creates a new ZipFileTreeAccessors instance from a File object (browser environment).
116
+ * @param file - The File object containing ZIP data.
117
+ * @param prefix - Optional prefix to prepend to paths.
118
+ * @returns Result containing the ZipFileTreeAccessors instance.
119
+ */
120
+ static fromFile(file: File, prefix?: string): Promise<Result<ZipFileTreeAccessors>>;
121
+ /**
122
+ * Builds the cache of all items in the ZIP archive.
123
+ */
124
+ private _buildItemCache;
125
+ /**
126
+ * Resolves paths to an absolute path.
127
+ */
128
+ resolveAbsolutePath(...paths: string[]): string;
129
+ /**
130
+ * Gets the extension of a path.
131
+ */
132
+ getExtension(path: string): string;
133
+ /**
134
+ * Gets the base name of a path.
135
+ */
136
+ getBaseName(path: string, suffix?: string): string;
137
+ /**
138
+ * Joins paths together.
139
+ */
140
+ joinPaths(...paths: string[]): string;
141
+ /**
142
+ * Gets an item from the file tree.
143
+ */
144
+ getItem(path: string): Result<FileTree.FileTreeItem>;
145
+ /**
146
+ * Gets the contents of a file in the file tree.
147
+ */
148
+ getFileContents(path: string): Result<string>;
149
+ /**
150
+ * Gets the children of a directory in the file tree.
151
+ */
152
+ getChildren(path: string): Result<ReadonlyArray<FileTree.FileTreeItem>>;
153
+ /**
154
+ * Checks if childPath is a direct child of parentPath.
155
+ */
156
+ private _isDirectChild;
157
+ }
158
+ //# sourceMappingURL=zipFileTreeAccessors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zipFileTreeAccessors.d.ts","sourceRoot":"","sources":["../../../src/packlets/zip-file-tree/zipFileTreeAccessors.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,MAAM,EAAgC,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAErG;;;GAGG;AACH,qBAAa,WAAY,YAAW,QAAQ,CAAC,iBAAiB;IAC5D;;OAEG;IACH,SAAgB,IAAI,EAAE,MAAM,CAAU;IAEtC;;OAEG;IACH,SAAgB,YAAY,EAAE,MAAM,CAAC;IAErC;;OAEG;IACH,SAAgB,IAAI,EAAE,MAAM,CAAC;IAE7B;;OAEG;IACH,SAAgB,QAAQ,EAAE,MAAM,CAAC;IAEjC;;OAEG;IACH,SAAgB,SAAS,EAAE,MAAM,CAAC;IAElC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IAEnC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAuB;IAElD;;;;;OAKG;gBACgB,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,oBAAoB;IASzF;;OAEG;IACI,WAAW,IAAI,MAAM,CAAC,OAAO,CAAC;IAC9B,WAAW,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;IAuBxE;;OAEG;IACI,cAAc,IAAI,MAAM,CAAC,MAAM,CAAC;CAGxC;AAED;;;GAGG;AACH,qBAAa,gBAAiB,YAAW,QAAQ,CAAC,sBAAsB;IACtE;;OAEG;IACH,SAAgB,IAAI,EAAE,WAAW,CAAe;IAEhD;;OAEG;IACH,SAAgB,YAAY,EAAE,MAAM,CAAC;IAErC;;OAEG;IACH,SAAgB,IAAI,EAA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AM,GAAG,MAAM,CAAC,MAAM,CAAC;IASpD;;OAEG;IACI,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;IAc9E;;OAEG;IACH,OAAO,CAAC,cAAc;CAgBvB"}
@@ -0,0 +1,293 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Erik Fortune
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all
13
+ * copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+ var __importDefault = (this && this.__importDefault) || function (mod) {
24
+ return (mod && mod.__esModule) ? mod : { "default": mod };
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.ZipFileTreeAccessors = exports.ZipDirectoryItem = exports.ZipFileItem = void 0;
28
+ const adm_zip_1 = __importDefault(require("adm-zip"));
29
+ const ts_utils_1 = require("@fgv/ts-utils");
30
+ /**
31
+ * Implementation of `FileTree.IFileTreeFileItem` for files in a ZIP archive.
32
+ * @public
33
+ */
34
+ class ZipFileItem {
35
+ /**
36
+ * Constructor for ZipFileItem.
37
+ * @param zipFilePath - The path of the file within the ZIP.
38
+ * @param contents - The pre-loaded contents of the file.
39
+ * @param accessors - The ZIP file tree accessors.
40
+ */
41
+ constructor(zipFilePath, contents, accessors) {
42
+ /**
43
+ * Indicates that this `FileTree.FileTreeItem` is a file.
44
+ */
45
+ this.type = 'file';
46
+ this._contents = contents;
47
+ this._accessors = accessors;
48
+ this.absolutePath = '/' + zipFilePath;
49
+ this.name = accessors.getBaseName(zipFilePath);
50
+ this.extension = accessors.getExtension(zipFilePath);
51
+ this.baseName = accessors.getBaseName(zipFilePath, this.extension);
52
+ }
53
+ getContents(converter) {
54
+ return this.getRawContents()
55
+ .onSuccess((contents) => {
56
+ return (0, ts_utils_1.captureResult)(() => {
57
+ const parsed = JSON.parse(contents);
58
+ if (converter) {
59
+ if ('convert' in converter) {
60
+ return converter.convert(parsed);
61
+ } /* c8 ignore next 3 - validator branch functionally tested but coverage tool misses due to interface complexity */
62
+ else {
63
+ return converter.validate(parsed);
64
+ }
65
+ }
66
+ return (0, ts_utils_1.succeed)(parsed);
67
+ }).onFailure(() => {
68
+ return (0, ts_utils_1.fail)(`Failed to parse JSON from file: ${this.absolutePath}`);
69
+ });
70
+ })
71
+ .onFailure((error) => {
72
+ return (0, ts_utils_1.fail)(`Failed to get contents from file: ${error}`);
73
+ });
74
+ }
75
+ /**
76
+ * Gets the raw contents of the file as a string.
77
+ */
78
+ getRawContents() {
79
+ return (0, ts_utils_1.succeed)(this._contents);
80
+ }
81
+ }
82
+ exports.ZipFileItem = ZipFileItem;
83
+ /**
84
+ * Implementation of `IFileTreeDirectoryItem` for directories in a ZIP archive.
85
+ * @public
86
+ */
87
+ class ZipDirectoryItem {
88
+ /**
89
+ * Constructor for ZipDirectoryItem.
90
+ * @param directoryPath - The path of the directory within the ZIP.
91
+ * @param accessors - The ZIP file tree accessors.
92
+ */
93
+ constructor(directoryPath, accessors) {
94
+ /**
95
+ * Indicates that this `FileTree.FileTreeItem` is a directory.
96
+ */
97
+ this.type = 'directory';
98
+ this._accessors = accessors;
99
+ this.absolutePath = '/' + directoryPath.replace(/\/$/, ''); // Normalize path
100
+ this.name = accessors.getBaseName(directoryPath);
101
+ }
102
+ /**
103
+ * Gets the children of the directory.
104
+ */
105
+ getChildren() {
106
+ return this._accessors.getChildren(this.absolutePath);
107
+ }
108
+ }
109
+ exports.ZipDirectoryItem = ZipDirectoryItem;
110
+ /**
111
+ * File tree accessors for ZIP archives.
112
+ * @public
113
+ */
114
+ class ZipFileTreeAccessors {
115
+ /**
116
+ * Constructor for ZipFileTreeAccessors.
117
+ * @param zip - The AdmZip instance.
118
+ * @param prefix - Optional prefix to prepend to paths.
119
+ */
120
+ constructor(zip, prefix) {
121
+ /**
122
+ * Cache of all items in the ZIP for efficient lookups.
123
+ */
124
+ this._itemCache = new Map();
125
+ this._zip = zip;
126
+ this._prefix = prefix || '';
127
+ this._buildItemCache();
128
+ }
129
+ /**
130
+ * Creates a new ZipFileTreeAccessors instance from a ZIP file buffer.
131
+ * @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
132
+ * @param prefix - Optional prefix to prepend to paths.
133
+ * @returns Result containing the ZipFileTreeAccessors instance.
134
+ */
135
+ static fromBuffer(zipBuffer, prefix) {
136
+ try {
137
+ const buffer = Buffer.from(zipBuffer);
138
+ const zip = new adm_zip_1.default(buffer);
139
+ return (0, ts_utils_1.succeed)(new ZipFileTreeAccessors(zip, prefix));
140
+ }
141
+ catch (error) {
142
+ /* c8 ignore next 1 - defensive coding: AdmZip always throws Error objects in practice */
143
+ return (0, ts_utils_1.fail)(`Failed to load ZIP archive: ${error instanceof Error ? error.message : String(error)}`);
144
+ }
145
+ }
146
+ /**
147
+ * Creates a new ZipFileTreeAccessors instance from a File object (browser environment).
148
+ * @param file - The File object containing ZIP data.
149
+ * @param prefix - Optional prefix to prepend to paths.
150
+ * @returns Result containing the ZipFileTreeAccessors instance.
151
+ */
152
+ static async fromFile(file, prefix) {
153
+ try {
154
+ const arrayBuffer = await file.arrayBuffer();
155
+ return ZipFileTreeAccessors.fromBuffer(new Uint8Array(arrayBuffer), prefix);
156
+ }
157
+ catch (error) {
158
+ return (0, ts_utils_1.fail)(`Failed to read file: ${error instanceof Error ? error.message : String(error)}`);
159
+ }
160
+ }
161
+ /**
162
+ * Builds the cache of all items in the ZIP archive.
163
+ */
164
+ _buildItemCache() {
165
+ const directories = new Set();
166
+ const entries = this._zip.getEntries();
167
+ // First pass: collect all directories from file paths
168
+ entries.forEach((entry) => {
169
+ if (!entry.isDirectory) {
170
+ // Extract directory path from file path
171
+ const pathParts = entry.entryName.split('/');
172
+ for (let i = 1; i < pathParts.length; i++) {
173
+ const dirPath = pathParts.slice(0, i).join('/');
174
+ directories.add(dirPath);
175
+ }
176
+ }
177
+ else {
178
+ // Also add explicit directories
179
+ const dirPath = entry.entryName.replace(/\/$/, ''); // Remove trailing slash
180
+ if (dirPath) {
181
+ directories.add(dirPath);
182
+ }
183
+ }
184
+ });
185
+ // Add directory items to cache
186
+ directories.forEach((dirPath) => {
187
+ const absolutePath = this.resolveAbsolutePath(dirPath);
188
+ const item = new ZipDirectoryItem(dirPath, this);
189
+ this._itemCache.set(absolutePath, item);
190
+ });
191
+ // Add file items to cache
192
+ entries.forEach((entry) => {
193
+ if (!entry.isDirectory) {
194
+ const absolutePath = this.resolveAbsolutePath(entry.entryName);
195
+ const contents = entry.getData().toString('utf8');
196
+ const item = new ZipFileItem(entry.entryName, contents, this);
197
+ this._itemCache.set(absolutePath, item);
198
+ }
199
+ });
200
+ }
201
+ /**
202
+ * Resolves paths to an absolute path.
203
+ */
204
+ resolveAbsolutePath(...paths) {
205
+ const joinedPath = this.joinPaths(...paths);
206
+ const prefixed = this._prefix ? this.joinPaths(this._prefix, joinedPath) : joinedPath;
207
+ return prefixed.startsWith('/') ? prefixed : '/' + prefixed;
208
+ }
209
+ /**
210
+ * Gets the extension of a path.
211
+ */
212
+ getExtension(path) {
213
+ const name = this.getBaseName(path);
214
+ const lastDotIndex = name.lastIndexOf('.');
215
+ return lastDotIndex >= 0 ? name.substring(lastDotIndex) : '';
216
+ }
217
+ /**
218
+ * Gets the base name of a path.
219
+ */
220
+ getBaseName(path, suffix) {
221
+ const normalizedPath = path.replace(/\/$/, ''); // Remove trailing slash
222
+ const parts = normalizedPath.split('/');
223
+ let baseName = parts[parts.length - 1] || '';
224
+ if (suffix && baseName.endsWith(suffix)) {
225
+ baseName = baseName.substring(0, baseName.length - suffix.length);
226
+ }
227
+ return baseName;
228
+ }
229
+ /**
230
+ * Joins paths together.
231
+ */
232
+ joinPaths(...paths) {
233
+ return paths
234
+ .filter((p) => p && p.length > 0)
235
+ .map((p) => p.replace(/^\/+|\/+$/g, '')) // Remove leading/trailing slashes
236
+ .join('/')
237
+ .replace(/\/+/g, '/'); // Normalize multiple slashes
238
+ }
239
+ /**
240
+ * Gets an item from the file tree.
241
+ */
242
+ getItem(path) {
243
+ const absolutePath = this.resolveAbsolutePath(path);
244
+ const item = this._itemCache.get(absolutePath);
245
+ if (item) {
246
+ return (0, ts_utils_1.succeed)(item);
247
+ }
248
+ return (0, ts_utils_1.fail)(`Item not found: ${absolutePath}`);
249
+ }
250
+ /**
251
+ * Gets the contents of a file in the file tree.
252
+ */
253
+ getFileContents(path) {
254
+ return this.getItem(path).onSuccess((item) => {
255
+ if (item.type !== 'file') {
256
+ return (0, ts_utils_1.fail)(`Path is not a file: ${path}`);
257
+ }
258
+ return item.getRawContents();
259
+ });
260
+ }
261
+ /**
262
+ * Gets the children of a directory in the file tree.
263
+ */
264
+ getChildren(path) {
265
+ const absolutePath = this.resolveAbsolutePath(path);
266
+ const children = [];
267
+ // Find all items that are direct children of this directory
268
+ for (const [itemPath, item] of this._itemCache) {
269
+ if (this._isDirectChild(absolutePath, itemPath)) {
270
+ children.push(item);
271
+ }
272
+ }
273
+ return (0, ts_utils_1.succeed)(children);
274
+ }
275
+ /**
276
+ * Checks if childPath is a direct child of parentPath.
277
+ */
278
+ _isDirectChild(parentPath, childPath) {
279
+ // Normalize paths
280
+ const normalizedParent = parentPath.replace(/\/$/, '');
281
+ const normalizedChild = childPath.replace(/\/$/, '');
282
+ // Child must start with parent path
283
+ if (!normalizedChild.startsWith(normalizedParent + '/')) {
284
+ return false;
285
+ }
286
+ // Get the relative path from parent to child
287
+ const relativePath = normalizedChild.substring(normalizedParent.length + 1);
288
+ // Direct child means no additional slashes in the relative path
289
+ return !relativePath.includes('/');
290
+ }
291
+ }
292
+ exports.ZipFileTreeAccessors = ZipFileTreeAccessors;
293
+ //# sourceMappingURL=zipFileTreeAccessors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zipFileTreeAccessors.js","sourceRoot":"","sources":["../../../src/packlets/zip-file-tree/zipFileTreeAccessors.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;AAEH,sDAA6B;AAC7B,4CAAqG;AAErG;;;GAGG;AACH,MAAa,WAAW;IAoCtB;;;;;OAKG;IACH,YAAmB,WAAmB,EAAE,QAAgB,EAAE,SAA+B;QAzCzF;;WAEG;QACa,SAAI,GAAW,MAAM,CAAC;QAuCpC,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,IAAI,CAAC,YAAY,GAAG,GAAG,GAAG,WAAW,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;QAC/C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC,YAAY,CAAC,WAAW,CAAC,CAAC;QACrD,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC,WAAW,CAAC,WAAW,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;IACrE,CAAC;IAOM,WAAW,CAAI,SAAuC;QAC3D,OAAO,IAAI,CAAC,cAAc,EAAE;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CAAC,UAAU,CAAC,gBAAgB,GAAG,GAAG,CAAC,EAAE,CAAC;YACxD,OAAO,KAAK,CAAC;QACf,CAAC;QAED,6CAA6C;QAC7C,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,CAAC,gBAAgB,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAE5E,gEAAgE;QAChE,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IACrC,CAAC;CACF;AAnND,oDAmNC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport AdmZip from 'adm-zip';\nimport { Result, succeed, fail, captureResult, Converter, Validator, FileTree } from '@fgv/ts-utils';\n\n/**\n * Implementation of `FileTree.IFileTreeFileItem` for files in a ZIP archive.\n * @public\n */\nexport class ZipFileItem implements FileTree.IFileTreeFileItem {\n /**\n * Indicates that this `FileTree.FileTreeItem` is a file.\n */\n public readonly type: 'file' = 'file';\n\n /**\n * The absolute path of the file within the ZIP archive.\n */\n public readonly absolutePath: string;\n\n /**\n * The name of the file\n */\n public readonly name: string;\n\n /**\n * The base name of the file (without extension)\n */\n public readonly baseName: string;\n\n /**\n * The extension of the file\n */\n public readonly extension: string;\n\n /**\n * The pre-loaded contents of the file.\n */\n private readonly _contents: string;\n\n /**\n * The ZIP file tree accessors that created this item.\n */\n private readonly _accessors: ZipFileTreeAccessors;\n\n /**\n * Constructor for ZipFileItem.\n * @param zipFilePath - The path of the file within the ZIP.\n * @param contents - The pre-loaded contents of the file.\n * @param accessors - The ZIP file tree accessors.\n */\n public constructor(zipFilePath: string, contents: string, accessors: ZipFileTreeAccessors) {\n this._contents = contents;\n this._accessors = accessors;\n this.absolutePath = '/' + zipFilePath;\n this.name = accessors.getBaseName(zipFilePath);\n this.extension = accessors.getExtension(zipFilePath);\n this.baseName = accessors.getBaseName(zipFilePath, this.extension);\n }\n\n /**\n * Gets the contents of the file as parsed JSON.\n */\n public getContents(): Result<unknown>;\n public getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;\n public getContents<T>(converter?: Validator<T> | Converter<T>): Result<T | unknown> {\n return this.getRawContents()\n .onSuccess((contents) => {\n return captureResult(() => {\n const parsed = JSON.parse(contents);\n if (converter) {\n if ('convert' in converter) {\n return converter.convert(parsed);\n } /* c8 ignore next 3 - validator branch functionally tested but coverage tool misses due to interface complexity */ else {\n return (converter as Validator<T>).validate(parsed);\n }\n }\n return succeed(parsed);\n }).onFailure(() => {\n return fail(`Failed to parse JSON from file: ${this.absolutePath}`);\n });\n })\n .onFailure((error) => {\n return fail(`Failed to get contents from file: ${error}`);\n });\n }\n\n /**\n * Gets the raw contents of the file as a string.\n */\n public getRawContents(): Result<string> {\n return succeed(this._contents);\n }\n}\n\n/**\n * Implementation of `IFileTreeDirectoryItem` for directories in a ZIP archive.\n * @public\n */\nexport class ZipDirectoryItem implements FileTree.IFileTreeDirectoryItem {\n /**\n * Indicates that this `FileTree.FileTreeItem` is a directory.\n */\n public readonly type: 'directory' = 'directory';\n\n /**\n * The absolute path of the directory within the ZIP archive.\n */\n public readonly absolutePath: string;\n\n /**\n * The name of the directory\n */\n public readonly name: string;\n\n /**\n * The ZIP file tree accessors that created this item.\n */\n private readonly _accessors: ZipFileTreeAccessors;\n\n /**\n * Constructor for ZipDirectoryItem.\n * @param directoryPath - The path of the directory within the ZIP.\n * @param accessors - The ZIP file tree accessors.\n */\n public constructor(directoryPath: string, accessors: ZipFileTreeAccessors) {\n this._accessors = accessors;\n this.absolutePath = '/' + directoryPath.replace(/\\/$/, ''); // Normalize path\n this.name = accessors.getBaseName(directoryPath);\n }\n\n /**\n * Gets the children of the directory.\n */\n public getChildren(): Result<ReadonlyArray<FileTree.FileTreeItem>> {\n return this._accessors.getChildren(this.absolutePath);\n }\n}\n\n/**\n * File tree accessors for ZIP archives.\n * @public\n */\nexport class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors {\n /**\n * The AdmZip instance containing the archive.\n */\n private readonly _zip: AdmZip;\n\n /**\n * Optional prefix to prepend to paths.\n */\n private readonly _prefix: string;\n\n /**\n * Cache of all items in the ZIP for efficient lookups.\n */\n private readonly _itemCache: Map<string, FileTree.FileTreeItem> = new Map();\n\n /**\n * Constructor for ZipFileTreeAccessors.\n * @param zip - The AdmZip instance.\n * @param prefix - Optional prefix to prepend to paths.\n */\n private constructor(zip: AdmZip, prefix?: string) {\n this._zip = zip;\n this._prefix = prefix || '';\n this._buildItemCache();\n }\n\n /**\n * Creates a new ZipFileTreeAccessors instance from a ZIP file buffer.\n * @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.\n * @param prefix - Optional prefix to prepend to paths.\n * @returns Result containing the ZipFileTreeAccessors instance.\n */\n public static fromBuffer(\n zipBuffer: ArrayBuffer | Uint8Array,\n prefix?: string\n ): Result<ZipFileTreeAccessors> {\n try {\n const buffer = Buffer.from(zipBuffer);\n const zip = new AdmZip(buffer);\n return succeed(new ZipFileTreeAccessors(zip, prefix));\n } catch (error) {\n /* c8 ignore next 1 - defensive coding: AdmZip always throws Error objects in practice */\n return fail(`Failed to load ZIP archive: ${error instanceof Error ? error.message : String(error)}`);\n }\n }\n\n /**\n * Creates a new ZipFileTreeAccessors instance from a File object (browser environment).\n * @param file - The File object containing ZIP data.\n * @param prefix - Optional prefix to prepend to paths.\n * @returns Result containing the ZipFileTreeAccessors instance.\n */\n public static async fromFile(file: File, prefix?: string): Promise<Result<ZipFileTreeAccessors>> {\n try {\n const arrayBuffer = await file.arrayBuffer();\n return ZipFileTreeAccessors.fromBuffer(new Uint8Array(arrayBuffer), prefix);\n } catch (error) {\n return fail(`Failed to read file: ${error instanceof Error ? error.message : String(error)}`);\n }\n }\n\n /**\n * Builds the cache of all items in the ZIP archive.\n */\n private _buildItemCache(): void {\n const directories = new Set<string>();\n const entries = this._zip.getEntries();\n\n // First pass: collect all directories from file paths\n entries.forEach((entry) => {\n if (!entry.isDirectory) {\n // Extract directory path from file path\n const pathParts = entry.entryName.split('/');\n for (let i = 1; i < pathParts.length; i++) {\n const dirPath = pathParts.slice(0, i).join('/');\n directories.add(dirPath);\n }\n } else {\n // Also add explicit directories\n const dirPath = entry.entryName.replace(/\\/$/, ''); // Remove trailing slash\n if (dirPath) {\n directories.add(dirPath);\n }\n }\n });\n\n // Add directory items to cache\n directories.forEach((dirPath) => {\n const absolutePath = this.resolveAbsolutePath(dirPath);\n const item = new ZipDirectoryItem(dirPath, this);\n this._itemCache.set(absolutePath, item);\n });\n\n // Add file items to cache\n entries.forEach((entry) => {\n if (!entry.isDirectory) {\n const absolutePath = this.resolveAbsolutePath(entry.entryName);\n const contents = entry.getData().toString('utf8');\n const item = new ZipFileItem(entry.entryName, contents, this);\n this._itemCache.set(absolutePath, item);\n }\n });\n }\n\n /**\n * Resolves paths to an absolute path.\n */\n public resolveAbsolutePath(...paths: string[]): string {\n const joinedPath = this.joinPaths(...paths);\n const prefixed = this._prefix ? this.joinPaths(this._prefix, joinedPath) : joinedPath;\n return prefixed.startsWith('/') ? prefixed : '/' + prefixed;\n }\n\n /**\n * Gets the extension of a path.\n */\n public getExtension(path: string): string {\n const name = this.getBaseName(path);\n const lastDotIndex = name.lastIndexOf('.');\n return lastDotIndex >= 0 ? name.substring(lastDotIndex) : '';\n }\n\n /**\n * Gets the base name of a path.\n */\n public getBaseName(path: string, suffix?: string): string {\n const normalizedPath = path.replace(/\\/$/, ''); // Remove trailing slash\n const parts = normalizedPath.split('/');\n let baseName = parts[parts.length - 1] || '';\n\n if (suffix && baseName.endsWith(suffix)) {\n baseName = baseName.substring(0, baseName.length - suffix.length);\n }\n\n return baseName;\n }\n\n /**\n * Joins paths together.\n */\n public joinPaths(...paths: string[]): string {\n return paths\n .filter((p) => p && p.length > 0)\n .map((p) => p.replace(/^\\/+|\\/+$/g, '')) // Remove leading/trailing slashes\n .join('/')\n .replace(/\\/+/g, '/'); // Normalize multiple slashes\n }\n\n /**\n * Gets an item from the file tree.\n */\n public getItem(path: string): Result<FileTree.FileTreeItem> {\n const absolutePath = this.resolveAbsolutePath(path);\n const item = this._itemCache.get(absolutePath);\n\n if (item) {\n return succeed(item);\n }\n\n return fail(`Item not found: ${absolutePath}`);\n }\n\n /**\n * Gets the contents of a file in the file tree.\n */\n public getFileContents(path: string): Result<string> {\n return this.getItem(path).onSuccess((item) => {\n if (item.type !== 'file') {\n return fail(`Path is not a file: ${path}`);\n }\n return item.getRawContents();\n });\n }\n\n /**\n * Gets the children of a directory in the file tree.\n */\n public getChildren(path: string): Result<ReadonlyArray<FileTree.FileTreeItem>> {\n const absolutePath = this.resolveAbsolutePath(path);\n const children: FileTree.FileTreeItem[] = [];\n\n // Find all items that are direct children of this directory\n for (const [itemPath, item] of this._itemCache) {\n if (this._isDirectChild(absolutePath, itemPath)) {\n children.push(item);\n }\n }\n\n return succeed(children);\n }\n\n /**\n * Checks if childPath is a direct child of parentPath.\n */\n private _isDirectChild(parentPath: string, childPath: string): boolean {\n // Normalize paths\n const normalizedParent = parentPath.replace(/\\/$/, '');\n const normalizedChild = childPath.replace(/\\/$/, '');\n\n // Child must start with parent path\n if (!normalizedChild.startsWith(normalizedParent + '/')) {\n return false;\n }\n\n // Get the relative path from parent to child\n const relativePath = normalizedChild.substring(normalizedParent.length + 1);\n\n // Direct child means no additional slashes in the relative path\n return !relativePath.includes('/');\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-extras",
3
- "version": "5.0.0-0",
3
+ "version": "5.0.0-3",
4
4
  "description": "Assorted Typescript Utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-extras.d.ts",
@@ -45,16 +45,18 @@
45
45
  "@types/heft-jest": "1.0.6",
46
46
  "@rushstack/heft-jest-plugin": "~0.16.9",
47
47
  "eslint-plugin-tsdoc": "~0.4.0",
48
- "@fgv/ts-utils-jest": "5.0.0-0",
49
- "@fgv/ts-utils": "5.0.0-0"
48
+ "@fgv/ts-utils-jest": "5.0.0-3",
49
+ "@fgv/ts-utils": "5.0.0-3"
50
50
  },
51
51
  "dependencies": {
52
52
  "luxon": "^3.6.1",
53
53
  "mustache": "^4.2.0",
54
- "papaparse": "^5.4.1"
54
+ "papaparse": "^5.4.1",
55
+ "@types/adm-zip": "~0.5.7",
56
+ "adm-zip": "~0.5.16"
55
57
  },
56
58
  "peerDependencies": {
57
- "@fgv/ts-utils": "5.0.0-0"
59
+ "@fgv/ts-utils": "5.0.0-3"
58
60
  },
59
61
  "scripts": {
60
62
  "build": "heft test --clean",