@fgv/ts-extras 5.0.0-30 → 5.0.0-31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ts-extras.d.ts +101 -18
- package/lib/packlets/csv/csvFileHelpers.d.ts +21 -0
- package/lib/packlets/csv/csvFileHelpers.js +92 -0
- package/lib/packlets/csv/csvHelpers.d.ts +4 -4
- package/lib/packlets/csv/csvHelpers.js +5 -42
- package/lib/packlets/csv/index.d.ts +1 -0
- package/lib/packlets/csv/index.js +3 -0
- package/lib/packlets/record-jar/index.d.ts +1 -0
- package/lib/packlets/record-jar/index.js +3 -0
- package/lib/packlets/record-jar/recordJarFileHelpers.d.ts +23 -0
- package/lib/packlets/record-jar/recordJarFileHelpers.js +97 -0
- package/lib/packlets/record-jar/recordJarHelpers.d.ts +0 -9
- package/lib/packlets/record-jar/recordJarHelpers.js +0 -52
- package/lib/packlets/zip-file-tree/zipFileTreeAccessors.d.ts +65 -15
- package/lib/packlets/zip-file-tree/zipFileTreeAccessors.js +63 -23
- package/package.json +6 -5
package/dist/ts-extras.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Conversion } from '@fgv/ts-utils';
|
|
2
2
|
import { Converter } from '@fgv/ts-utils';
|
|
3
|
-
import { FileTree } from '@fgv/ts-
|
|
3
|
+
import { FileTree } from '@fgv/ts-json-base';
|
|
4
4
|
import { Hash as Hash_2 } from '@fgv/ts-utils';
|
|
5
|
+
import { JsonValue } from '@fgv/ts-json-base';
|
|
5
6
|
import { Result } from '@fgv/ts-utils';
|
|
6
7
|
import { Validator } from '@fgv/ts-utils';
|
|
7
8
|
|
|
@@ -18,8 +19,10 @@ export { Converters }
|
|
|
18
19
|
|
|
19
20
|
declare namespace Csv {
|
|
20
21
|
export {
|
|
22
|
+
parseCsvString,
|
|
23
|
+
CsvOptions,
|
|
21
24
|
readCsvFileSync,
|
|
22
|
-
|
|
25
|
+
readCsvFromTree
|
|
23
26
|
}
|
|
24
27
|
}
|
|
25
28
|
export { Csv }
|
|
@@ -238,6 +241,15 @@ declare class Md5Normalizer extends Hash_2.HashingNormalizer {
|
|
|
238
241
|
static md5Hash(parts: string[]): string;
|
|
239
242
|
}
|
|
240
243
|
|
|
244
|
+
/**
|
|
245
|
+
* Parses CSV data from a string.
|
|
246
|
+
* @param body - The CSV string to parse.
|
|
247
|
+
* @param options - optional parameters to control the processing
|
|
248
|
+
* @returns The parsed CSV data.
|
|
249
|
+
* @beta
|
|
250
|
+
*/
|
|
251
|
+
declare function parseCsvString(body: string, options?: CsvOptions): Result<unknown>;
|
|
252
|
+
|
|
241
253
|
/**
|
|
242
254
|
* Reads a record-jar from an array of strings, each of which represents one
|
|
243
255
|
* line in the source file.
|
|
@@ -391,6 +403,16 @@ declare function rangeTypeOf<T, RT extends RangeOf<T>, TC = unknown>(converter:
|
|
|
391
403
|
*/
|
|
392
404
|
declare function readCsvFileSync(srcPath: string, options?: CsvOptions): Result<unknown>;
|
|
393
405
|
|
|
406
|
+
/**
|
|
407
|
+
* Reads a CSV file from a FileTree.
|
|
408
|
+
* @param fileTree - The FileTree to read from.
|
|
409
|
+
* @param filePath - Path of the file within the tree.
|
|
410
|
+
* @param options - optional parameters to control the processing
|
|
411
|
+
* @returns The parsed CSV data.
|
|
412
|
+
* @beta
|
|
413
|
+
*/
|
|
414
|
+
declare function readCsvFromTree(fileTree: FileTree.FileTree, filePath: string, options?: CsvOptions): Result<unknown>;
|
|
415
|
+
|
|
394
416
|
/**
|
|
395
417
|
* Reads a record-jar file from a supplied path.
|
|
396
418
|
* @param srcPath - Source path from which the file is read.
|
|
@@ -401,13 +423,25 @@ declare function readCsvFileSync(srcPath: string, options?: CsvOptions): Result<
|
|
|
401
423
|
*/
|
|
402
424
|
declare function readRecordJarFileSync(srcPath: string, options?: JarRecordParserOptions): Result<JarRecord[]>;
|
|
403
425
|
|
|
426
|
+
/**
|
|
427
|
+
* Reads a record-jar file from a FileTree.
|
|
428
|
+
* @param fileTree - The FileTree to read from.
|
|
429
|
+
* @param filePath - Path of the file within the tree.
|
|
430
|
+
* @param options - Optional parser configuration
|
|
431
|
+
* @returns The contents of the file as an array of `Record<string, string>`
|
|
432
|
+
* @see https://datatracker.ietf.org/doc/html/draft-phillips-record-jar-01
|
|
433
|
+
* @public
|
|
434
|
+
*/
|
|
435
|
+
declare function readRecordJarFromTree(fileTree: FileTree.FileTree, filePath: string, options?: JarRecordParserOptions): Result<JarRecord[]>;
|
|
436
|
+
|
|
404
437
|
declare namespace RecordJar {
|
|
405
438
|
export {
|
|
406
439
|
parseRecordJarLines,
|
|
407
|
-
readRecordJarFileSync,
|
|
408
440
|
JarRecord,
|
|
409
441
|
JarFieldPicker,
|
|
410
|
-
JarRecordParserOptions
|
|
442
|
+
JarRecordParserOptions,
|
|
443
|
+
readRecordJarFileSync,
|
|
444
|
+
readRecordJarFromTree
|
|
411
445
|
}
|
|
412
446
|
}
|
|
413
447
|
export { RecordJar }
|
|
@@ -428,7 +462,7 @@ declare function templateString(defaultContext?: unknown): Conversion.StringConv
|
|
|
428
462
|
* Implementation of `IFileTreeDirectoryItem` for directories in a ZIP archive.
|
|
429
463
|
* @public
|
|
430
464
|
*/
|
|
431
|
-
declare class ZipDirectoryItem implements FileTree.IFileTreeDirectoryItem {
|
|
465
|
+
declare class ZipDirectoryItem<TCT extends string = string> implements FileTree.IFileTreeDirectoryItem<TCT> {
|
|
432
466
|
/**
|
|
433
467
|
* Indicates that this `FileTree.FileTreeItem` is a directory.
|
|
434
468
|
*/
|
|
@@ -450,18 +484,18 @@ declare class ZipDirectoryItem implements FileTree.IFileTreeDirectoryItem {
|
|
|
450
484
|
* @param directoryPath - The path of the directory within the ZIP.
|
|
451
485
|
* @param accessors - The ZIP file tree accessors.
|
|
452
486
|
*/
|
|
453
|
-
constructor(directoryPath: string, accessors: ZipFileTreeAccessors);
|
|
487
|
+
constructor(directoryPath: string, accessors: ZipFileTreeAccessors<TCT>);
|
|
454
488
|
/**
|
|
455
489
|
* Gets the children of the directory.
|
|
456
490
|
*/
|
|
457
|
-
getChildren(): Result<ReadonlyArray<FileTree.FileTreeItem
|
|
491
|
+
getChildren(): Result<ReadonlyArray<FileTree.FileTreeItem<TCT>>>;
|
|
458
492
|
}
|
|
459
493
|
|
|
460
494
|
/**
|
|
461
495
|
* Implementation of `FileTree.IFileTreeFileItem` for files in a ZIP archive.
|
|
462
496
|
* @public
|
|
463
497
|
*/
|
|
464
|
-
declare class ZipFileItem implements FileTree.IFileTreeFileItem {
|
|
498
|
+
declare class ZipFileItem<TCT extends string = string> implements FileTree.IFileTreeFileItem<TCT> {
|
|
465
499
|
/**
|
|
466
500
|
* Indicates that this `FileTree.FileTreeItem` is a file.
|
|
467
501
|
*/
|
|
@@ -482,6 +516,10 @@ declare class ZipFileItem implements FileTree.IFileTreeFileItem {
|
|
|
482
516
|
* The extension of the file
|
|
483
517
|
*/
|
|
484
518
|
readonly extension: string;
|
|
519
|
+
/**
|
|
520
|
+
* The content type of the file.
|
|
521
|
+
*/
|
|
522
|
+
get contentType(): TCT | undefined;
|
|
485
523
|
/**
|
|
486
524
|
* The pre-loaded contents of the file.
|
|
487
525
|
*/
|
|
@@ -490,17 +528,30 @@ declare class ZipFileItem implements FileTree.IFileTreeFileItem {
|
|
|
490
528
|
* The ZIP file tree accessors that created this item.
|
|
491
529
|
*/
|
|
492
530
|
private readonly _accessors;
|
|
531
|
+
/**
|
|
532
|
+
* Mutable content type of the file.
|
|
533
|
+
*/
|
|
534
|
+
private _contentType;
|
|
535
|
+
/**
|
|
536
|
+
* Flag to track if content type should be inferred on first access.
|
|
537
|
+
*/
|
|
538
|
+
private _shouldInferContentType;
|
|
493
539
|
/**
|
|
494
540
|
* Constructor for ZipFileItem.
|
|
495
541
|
* @param zipFilePath - The path of the file within the ZIP.
|
|
496
542
|
* @param contents - The pre-loaded contents of the file.
|
|
497
543
|
* @param accessors - The ZIP file tree accessors.
|
|
498
544
|
*/
|
|
499
|
-
constructor(zipFilePath: string, contents: string, accessors: ZipFileTreeAccessors);
|
|
545
|
+
constructor(zipFilePath: string, contents: string, accessors: ZipFileTreeAccessors<TCT>);
|
|
546
|
+
/**
|
|
547
|
+
* Sets the content type of the file.
|
|
548
|
+
* @param contentType - The content type of the file.
|
|
549
|
+
*/
|
|
550
|
+
setContentType(contentType: TCT | undefined): void;
|
|
500
551
|
/**
|
|
501
552
|
* Gets the contents of the file as parsed JSON.
|
|
502
553
|
*/
|
|
503
|
-
getContents(): Result<
|
|
554
|
+
getContents(): Result<JsonValue>;
|
|
504
555
|
getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;
|
|
505
556
|
/**
|
|
506
557
|
* Gets the raw contents of the file as a string.
|
|
@@ -521,7 +572,7 @@ export { ZipFileTree }
|
|
|
521
572
|
* File tree accessors for ZIP archives.
|
|
522
573
|
* @public
|
|
523
574
|
*/
|
|
524
|
-
declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors {
|
|
575
|
+
declare class ZipFileTreeAccessors<TCT extends string = string> implements FileTree.IFileTreeAccessors<TCT> {
|
|
525
576
|
/**
|
|
526
577
|
* The unzipped file data.
|
|
527
578
|
*/
|
|
@@ -530,6 +581,10 @@ declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors {
|
|
|
530
581
|
* Optional prefix to prepend to paths.
|
|
531
582
|
*/
|
|
532
583
|
private readonly _prefix;
|
|
584
|
+
/**
|
|
585
|
+
* Content type inference function.
|
|
586
|
+
*/
|
|
587
|
+
private readonly _inferContentType;
|
|
533
588
|
/**
|
|
534
589
|
* Cache of all items in the ZIP for efficient lookups.
|
|
535
590
|
*/
|
|
@@ -537,30 +592,54 @@ declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors {
|
|
|
537
592
|
/**
|
|
538
593
|
* Constructor for ZipFileTreeAccessors.
|
|
539
594
|
* @param files - The unzipped file data from fflate.
|
|
540
|
-
* @param
|
|
595
|
+
* @param params - Optional initialization parameters.
|
|
541
596
|
*/
|
|
542
597
|
private constructor();
|
|
598
|
+
/**
|
|
599
|
+
* Default function to infer the content type of a file.
|
|
600
|
+
* @param filePath - The path of the file.
|
|
601
|
+
* @param provided - Optional supplied content type.
|
|
602
|
+
* @returns `Success` with the content type of the file if successful, or
|
|
603
|
+
* `Failure` with an error message otherwise.
|
|
604
|
+
* @remarks This default implementation always returns `Success` with `undefined`.
|
|
605
|
+
* @public
|
|
606
|
+
*/
|
|
607
|
+
static defaultInferContentType<TCT extends string = string>(__filePath: string, __provided?: string): Result<TCT | undefined>;
|
|
543
608
|
/**
|
|
544
609
|
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (synchronous).
|
|
545
610
|
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
546
611
|
* @param prefix - Optional prefix to prepend to paths.
|
|
547
612
|
* @returns Result containing the ZipFileTreeAccessors instance.
|
|
548
613
|
*/
|
|
549
|
-
static fromBuffer(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Result<ZipFileTreeAccessors
|
|
614
|
+
static fromBuffer<TCT extends string = string>(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Result<ZipFileTreeAccessors<TCT>>;
|
|
615
|
+
/**
|
|
616
|
+
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (synchronous).
|
|
617
|
+
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
618
|
+
* @param params - Optional initialization parameters.
|
|
619
|
+
* @returns Result containing the ZipFileTreeAccessors instance.
|
|
620
|
+
*/
|
|
621
|
+
static fromBuffer<TCT extends string = string>(zipBuffer: ArrayBuffer | Uint8Array, params?: FileTree.IFileTreeInitParams<TCT>): Result<ZipFileTreeAccessors<TCT>>;
|
|
550
622
|
/**
|
|
551
623
|
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (asynchronous).
|
|
552
624
|
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
553
625
|
* @param prefix - Optional prefix to prepend to paths.
|
|
554
626
|
* @returns Promise containing Result with the ZipFileTreeAccessors instance.
|
|
555
627
|
*/
|
|
556
|
-
static fromBufferAsync(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Promise<Result<ZipFileTreeAccessors
|
|
628
|
+
static fromBufferAsync<TCT extends string = string>(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Promise<Result<ZipFileTreeAccessors<TCT>>>;
|
|
629
|
+
/**
|
|
630
|
+
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (asynchronous).
|
|
631
|
+
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
632
|
+
* @param params - Optional initialization parameters.
|
|
633
|
+
* @returns Promise containing Result with the ZipFileTreeAccessors instance.
|
|
634
|
+
*/
|
|
635
|
+
static fromBufferAsync<TCT extends string = string>(zipBuffer: ArrayBuffer | Uint8Array, params?: FileTree.IFileTreeInitParams<TCT>): Promise<Result<ZipFileTreeAccessors<TCT>>>;
|
|
557
636
|
/**
|
|
558
637
|
* Creates a new ZipFileTreeAccessors instance from a File object (browser environment).
|
|
559
638
|
* @param file - The File object containing ZIP data.
|
|
560
|
-
* @param
|
|
639
|
+
* @param params - Optional initialization parameters.
|
|
561
640
|
* @returns Result containing the ZipFileTreeAccessors instance.
|
|
562
641
|
*/
|
|
563
|
-
static fromFile(file: File,
|
|
642
|
+
static fromFile<TCT extends string = string>(file: File, params?: FileTree.IFileTreeInitParams<TCT>): Promise<Result<ZipFileTreeAccessors<TCT>>>;
|
|
564
643
|
/**
|
|
565
644
|
* Builds the cache of all items in the ZIP archive.
|
|
566
645
|
*/
|
|
@@ -584,15 +663,19 @@ declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors {
|
|
|
584
663
|
/**
|
|
585
664
|
* Gets an item from the file tree.
|
|
586
665
|
*/
|
|
587
|
-
getItem(path: string): Result<FileTree.FileTreeItem
|
|
666
|
+
getItem(path: string): Result<FileTree.FileTreeItem<TCT>>;
|
|
588
667
|
/**
|
|
589
668
|
* Gets the contents of a file in the file tree.
|
|
590
669
|
*/
|
|
591
670
|
getFileContents(path: string): Result<string>;
|
|
671
|
+
/**
|
|
672
|
+
* Gets the content type of a file in the file tree.
|
|
673
|
+
*/
|
|
674
|
+
getFileContentType(path: string, provided?: string): Result<TCT | undefined>;
|
|
592
675
|
/**
|
|
593
676
|
* Gets the children of a directory in the file tree.
|
|
594
677
|
*/
|
|
595
|
-
getChildren(path: string): Result<ReadonlyArray<FileTree.FileTreeItem
|
|
678
|
+
getChildren(path: string): Result<ReadonlyArray<FileTree.FileTreeItem<TCT>>>;
|
|
596
679
|
/**
|
|
597
680
|
* Checks if childPath is a direct child of parentPath.
|
|
598
681
|
*/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Result } from '@fgv/ts-utils';
|
|
2
|
+
import { FileTree } from '@fgv/ts-json-base';
|
|
3
|
+
import { CsvOptions } from './csvHelpers';
|
|
4
|
+
/**
|
|
5
|
+
* Reads a CSV file from a supplied path.
|
|
6
|
+
* @param srcPath - Source path from which the file is read.
|
|
7
|
+
* @param options - optional parameters to control the processing
|
|
8
|
+
* @returns The contents of the file.
|
|
9
|
+
* @beta
|
|
10
|
+
*/
|
|
11
|
+
export declare function readCsvFileSync(srcPath: string, options?: CsvOptions): Result<unknown>;
|
|
12
|
+
/**
|
|
13
|
+
* Reads a CSV file from a FileTree.
|
|
14
|
+
* @param fileTree - The FileTree to read from.
|
|
15
|
+
* @param filePath - Path of the file within the tree.
|
|
16
|
+
* @param options - optional parameters to control the processing
|
|
17
|
+
* @returns The parsed CSV data.
|
|
18
|
+
* @beta
|
|
19
|
+
*/
|
|
20
|
+
export declare function readCsvFromTree(fileTree: FileTree.FileTree, filePath: string, options?: CsvOptions): Result<unknown>;
|
|
21
|
+
//# sourceMappingURL=csvFileHelpers.d.ts.map
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2020 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
24
|
+
if (k2 === undefined) k2 = k;
|
|
25
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
26
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
27
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
28
|
+
}
|
|
29
|
+
Object.defineProperty(o, k2, desc);
|
|
30
|
+
}) : (function(o, m, k, k2) {
|
|
31
|
+
if (k2 === undefined) k2 = k;
|
|
32
|
+
o[k2] = m[k];
|
|
33
|
+
}));
|
|
34
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
35
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
36
|
+
}) : function(o, v) {
|
|
37
|
+
o["default"] = v;
|
|
38
|
+
});
|
|
39
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
40
|
+
var ownKeys = function(o) {
|
|
41
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
42
|
+
var ar = [];
|
|
43
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
44
|
+
return ar;
|
|
45
|
+
};
|
|
46
|
+
return ownKeys(o);
|
|
47
|
+
};
|
|
48
|
+
return function (mod) {
|
|
49
|
+
if (mod && mod.__esModule) return mod;
|
|
50
|
+
var result = {};
|
|
51
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
52
|
+
__setModuleDefault(result, mod);
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
})();
|
|
56
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
+
exports.readCsvFileSync = readCsvFileSync;
|
|
58
|
+
exports.readCsvFromTree = readCsvFromTree;
|
|
59
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
60
|
+
const fs = __importStar(require("fs"));
|
|
61
|
+
const path = __importStar(require("path"));
|
|
62
|
+
const csvHelpers_1 = require("./csvHelpers");
|
|
63
|
+
/**
|
|
64
|
+
* Reads a CSV file from a supplied path.
|
|
65
|
+
* @param srcPath - Source path from which the file is read.
|
|
66
|
+
* @param options - optional parameters to control the processing
|
|
67
|
+
* @returns The contents of the file.
|
|
68
|
+
* @beta
|
|
69
|
+
*/
|
|
70
|
+
function readCsvFileSync(srcPath, options) {
|
|
71
|
+
return (0, ts_utils_1.captureResult)(() => {
|
|
72
|
+
const fullPath = path.resolve(srcPath);
|
|
73
|
+
return fs.readFileSync(fullPath, 'utf8').toString();
|
|
74
|
+
}).onSuccess((body) => {
|
|
75
|
+
return (0, csvHelpers_1.parseCsvString)(body, options);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Reads a CSV file from a FileTree.
|
|
80
|
+
* @param fileTree - The FileTree to read from.
|
|
81
|
+
* @param filePath - Path of the file within the tree.
|
|
82
|
+
* @param options - optional parameters to control the processing
|
|
83
|
+
* @returns The parsed CSV data.
|
|
84
|
+
* @beta
|
|
85
|
+
*/
|
|
86
|
+
function readCsvFromTree(fileTree, filePath, options) {
|
|
87
|
+
return fileTree
|
|
88
|
+
.getFile(filePath)
|
|
89
|
+
.onSuccess((file) => file.getRawContents())
|
|
90
|
+
.onSuccess((contents) => (0, csvHelpers_1.parseCsvString)(contents, options));
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=csvFileHelpers.js.map
|
|
@@ -7,11 +7,11 @@ export interface CsvOptions {
|
|
|
7
7
|
delimiter?: string;
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
* @param
|
|
10
|
+
* Parses CSV data from a string.
|
|
11
|
+
* @param body - The CSV string to parse.
|
|
12
12
|
* @param options - optional parameters to control the processing
|
|
13
|
-
* @returns The
|
|
13
|
+
* @returns The parsed CSV data.
|
|
14
14
|
* @beta
|
|
15
15
|
*/
|
|
16
|
-
export declare function
|
|
16
|
+
export declare function parseCsvString(body: string, options?: CsvOptions): Result<unknown>;
|
|
17
17
|
//# sourceMappingURL=csvHelpers.d.ts.map
|
|
@@ -20,56 +20,19 @@
|
|
|
20
20
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
* SOFTWARE.
|
|
22
22
|
*/
|
|
23
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
24
|
-
if (k2 === undefined) k2 = k;
|
|
25
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
26
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
27
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
28
|
-
}
|
|
29
|
-
Object.defineProperty(o, k2, desc);
|
|
30
|
-
}) : (function(o, m, k, k2) {
|
|
31
|
-
if (k2 === undefined) k2 = k;
|
|
32
|
-
o[k2] = m[k];
|
|
33
|
-
}));
|
|
34
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
35
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
36
|
-
}) : function(o, v) {
|
|
37
|
-
o["default"] = v;
|
|
38
|
-
});
|
|
39
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
40
|
-
var ownKeys = function(o) {
|
|
41
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
42
|
-
var ar = [];
|
|
43
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
44
|
-
return ar;
|
|
45
|
-
};
|
|
46
|
-
return ownKeys(o);
|
|
47
|
-
};
|
|
48
|
-
return function (mod) {
|
|
49
|
-
if (mod && mod.__esModule) return mod;
|
|
50
|
-
var result = {};
|
|
51
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
52
|
-
__setModuleDefault(result, mod);
|
|
53
|
-
return result;
|
|
54
|
-
};
|
|
55
|
-
})();
|
|
56
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
-
exports.
|
|
24
|
+
exports.parseCsvString = parseCsvString;
|
|
58
25
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
59
|
-
const fs = __importStar(require("fs"));
|
|
60
26
|
const papaparse_1 = require("papaparse");
|
|
61
|
-
const path = __importStar(require("path"));
|
|
62
27
|
/**
|
|
63
|
-
*
|
|
64
|
-
* @param
|
|
28
|
+
* Parses CSV data from a string.
|
|
29
|
+
* @param body - The CSV string to parse.
|
|
65
30
|
* @param options - optional parameters to control the processing
|
|
66
|
-
* @returns The
|
|
31
|
+
* @returns The parsed CSV data.
|
|
67
32
|
* @beta
|
|
68
33
|
*/
|
|
69
|
-
function
|
|
34
|
+
function parseCsvString(body, options) {
|
|
70
35
|
return (0, ts_utils_1.captureResult)(() => {
|
|
71
|
-
const fullPath = path.resolve(srcPath);
|
|
72
|
-
const body = fs.readFileSync(fullPath, 'utf8').toString();
|
|
73
36
|
options = options !== null && options !== void 0 ? options : {};
|
|
74
37
|
return (0, papaparse_1.parse)(body, Object.assign({ transform: (s) => s.trim(), header: false, dynamicTyping: false, skipEmptyLines: 'greedy' }, options)).data.slice(1);
|
|
75
38
|
});
|
|
@@ -35,5 +35,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
35
35
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
36
36
|
};
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
// Export tree-shakeable functions
|
|
38
39
|
__exportStar(require("./csvHelpers"), exports);
|
|
40
|
+
// Filesystem helpers in separate module for tree-shaking
|
|
41
|
+
__exportStar(require("./csvFileHelpers"), exports);
|
|
39
42
|
//# sourceMappingURL=index.js.map
|
|
@@ -35,5 +35,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
35
35
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
36
36
|
};
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
// Export tree-shakeable functions
|
|
38
39
|
__exportStar(require("./recordJarHelpers"), exports);
|
|
40
|
+
// Filesystem helpers in separate module for tree-shaking
|
|
41
|
+
__exportStar(require("./recordJarFileHelpers"), exports);
|
|
39
42
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Result } from '@fgv/ts-utils';
|
|
2
|
+
import { FileTree } from '@fgv/ts-json-base';
|
|
3
|
+
import { JarRecord, JarRecordParserOptions } from './recordJarHelpers';
|
|
4
|
+
/**
|
|
5
|
+
* Reads a record-jar file from a supplied path.
|
|
6
|
+
* @param srcPath - Source path from which the file is read.
|
|
7
|
+
* @param options - Optional parser configuration
|
|
8
|
+
* @returns The contents of the file as an array of `Record<string, string>`
|
|
9
|
+
* @see https://datatracker.ietf.org/doc/html/draft-phillips-record-jar-01
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
export declare function readRecordJarFileSync(srcPath: string, options?: JarRecordParserOptions): Result<JarRecord[]>;
|
|
13
|
+
/**
|
|
14
|
+
* Reads a record-jar file from a FileTree.
|
|
15
|
+
* @param fileTree - The FileTree to read from.
|
|
16
|
+
* @param filePath - Path of the file within the tree.
|
|
17
|
+
* @param options - Optional parser configuration
|
|
18
|
+
* @returns The contents of the file as an array of `Record<string, string>`
|
|
19
|
+
* @see https://datatracker.ietf.org/doc/html/draft-phillips-record-jar-01
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export declare function readRecordJarFromTree(fileTree: FileTree.FileTree, filePath: string, options?: JarRecordParserOptions): Result<JarRecord[]>;
|
|
23
|
+
//# sourceMappingURL=recordJarFileHelpers.d.ts.map
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2022 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
24
|
+
if (k2 === undefined) k2 = k;
|
|
25
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
26
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
27
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
28
|
+
}
|
|
29
|
+
Object.defineProperty(o, k2, desc);
|
|
30
|
+
}) : (function(o, m, k, k2) {
|
|
31
|
+
if (k2 === undefined) k2 = k;
|
|
32
|
+
o[k2] = m[k];
|
|
33
|
+
}));
|
|
34
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
35
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
36
|
+
}) : function(o, v) {
|
|
37
|
+
o["default"] = v;
|
|
38
|
+
});
|
|
39
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
40
|
+
var ownKeys = function(o) {
|
|
41
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
42
|
+
var ar = [];
|
|
43
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
44
|
+
return ar;
|
|
45
|
+
};
|
|
46
|
+
return ownKeys(o);
|
|
47
|
+
};
|
|
48
|
+
return function (mod) {
|
|
49
|
+
if (mod && mod.__esModule) return mod;
|
|
50
|
+
var result = {};
|
|
51
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
52
|
+
__setModuleDefault(result, mod);
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
})();
|
|
56
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
+
exports.readRecordJarFileSync = readRecordJarFileSync;
|
|
58
|
+
exports.readRecordJarFromTree = readRecordJarFromTree;
|
|
59
|
+
const fs = __importStar(require("fs"));
|
|
60
|
+
const path = __importStar(require("path"));
|
|
61
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
62
|
+
const recordJarHelpers_1 = require("./recordJarHelpers");
|
|
63
|
+
/**
|
|
64
|
+
* Reads a record-jar file from a supplied path.
|
|
65
|
+
* @param srcPath - Source path from which the file is read.
|
|
66
|
+
* @param options - Optional parser configuration
|
|
67
|
+
* @returns The contents of the file as an array of `Record<string, string>`
|
|
68
|
+
* @see https://datatracker.ietf.org/doc/html/draft-phillips-record-jar-01
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
function readRecordJarFileSync(srcPath, options) {
|
|
72
|
+
return (0, ts_utils_1.captureResult)(() => {
|
|
73
|
+
const fullPath = path.resolve(srcPath);
|
|
74
|
+
return fs.readFileSync(fullPath, 'utf8').toString().split(/\r?\n/);
|
|
75
|
+
}).onSuccess((lines) => {
|
|
76
|
+
return (0, recordJarHelpers_1.parseRecordJarLines)(lines, options);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Reads a record-jar file from a FileTree.
|
|
81
|
+
* @param fileTree - The FileTree to read from.
|
|
82
|
+
* @param filePath - Path of the file within the tree.
|
|
83
|
+
* @param options - Optional parser configuration
|
|
84
|
+
* @returns The contents of the file as an array of `Record<string, string>`
|
|
85
|
+
* @see https://datatracker.ietf.org/doc/html/draft-phillips-record-jar-01
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
88
|
+
function readRecordJarFromTree(fileTree, filePath, options) {
|
|
89
|
+
return fileTree
|
|
90
|
+
.getFile(filePath)
|
|
91
|
+
.onSuccess((file) => file.getRawContents())
|
|
92
|
+
.onSuccess((contents) => {
|
|
93
|
+
const lines = contents.split(/\r?\n/);
|
|
94
|
+
return (0, recordJarHelpers_1.parseRecordJarLines)(lines, options);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=recordJarFileHelpers.js.map
|
|
@@ -25,13 +25,4 @@ export interface JarRecordParserOptions {
|
|
|
25
25
|
* @public
|
|
26
26
|
*/
|
|
27
27
|
export declare function parseRecordJarLines(lines: string[], options?: JarRecordParserOptions): Result<JarRecord[]>;
|
|
28
|
-
/**
|
|
29
|
-
* Reads a record-jar file from a supplied path.
|
|
30
|
-
* @param srcPath - Source path from which the file is read.
|
|
31
|
-
* @param options - Optional parser configuration
|
|
32
|
-
* @returns The contents of the file as an array of `Record<string, string>`
|
|
33
|
-
* @see https://datatracker.ietf.org/doc/html/draft-phillips-record-jar-01
|
|
34
|
-
* @public
|
|
35
|
-
*/
|
|
36
|
-
export declare function readRecordJarFileSync(srcPath: string, options?: JarRecordParserOptions): Result<JarRecord[]>;
|
|
37
28
|
//# sourceMappingURL=recordJarHelpers.d.ts.map
|
|
@@ -20,44 +20,8 @@
|
|
|
20
20
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
* SOFTWARE.
|
|
22
22
|
*/
|
|
23
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
24
|
-
if (k2 === undefined) k2 = k;
|
|
25
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
26
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
27
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
28
|
-
}
|
|
29
|
-
Object.defineProperty(o, k2, desc);
|
|
30
|
-
}) : (function(o, m, k, k2) {
|
|
31
|
-
if (k2 === undefined) k2 = k;
|
|
32
|
-
o[k2] = m[k];
|
|
33
|
-
}));
|
|
34
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
35
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
36
|
-
}) : function(o, v) {
|
|
37
|
-
o["default"] = v;
|
|
38
|
-
});
|
|
39
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
40
|
-
var ownKeys = function(o) {
|
|
41
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
42
|
-
var ar = [];
|
|
43
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
44
|
-
return ar;
|
|
45
|
-
};
|
|
46
|
-
return ownKeys(o);
|
|
47
|
-
};
|
|
48
|
-
return function (mod) {
|
|
49
|
-
if (mod && mod.__esModule) return mod;
|
|
50
|
-
var result = {};
|
|
51
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
52
|
-
__setModuleDefault(result, mod);
|
|
53
|
-
return result;
|
|
54
|
-
};
|
|
55
|
-
})();
|
|
56
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
24
|
exports.parseRecordJarLines = parseRecordJarLines;
|
|
58
|
-
exports.readRecordJarFileSync = readRecordJarFileSync;
|
|
59
|
-
const fs = __importStar(require("fs"));
|
|
60
|
-
const path = __importStar(require("path"));
|
|
61
25
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
62
26
|
class RecordParser {
|
|
63
27
|
constructor(options) {
|
|
@@ -252,20 +216,4 @@ class RecordParser {
|
|
|
252
216
|
function parseRecordJarLines(lines, options) {
|
|
253
217
|
return RecordParser.parse(lines, options);
|
|
254
218
|
}
|
|
255
|
-
/**
|
|
256
|
-
* Reads a record-jar file from a supplied path.
|
|
257
|
-
* @param srcPath - Source path from which the file is read.
|
|
258
|
-
* @param options - Optional parser configuration
|
|
259
|
-
* @returns The contents of the file as an array of `Record<string, string>`
|
|
260
|
-
* @see https://datatracker.ietf.org/doc/html/draft-phillips-record-jar-01
|
|
261
|
-
* @public
|
|
262
|
-
*/
|
|
263
|
-
function readRecordJarFileSync(srcPath, options) {
|
|
264
|
-
return (0, ts_utils_1.captureResult)(() => {
|
|
265
|
-
const fullPath = path.resolve(srcPath);
|
|
266
|
-
return fs.readFileSync(fullPath, 'utf8').toString().split(/\r?\n/);
|
|
267
|
-
}).onSuccess((lines) => {
|
|
268
|
-
return parseRecordJarLines(lines, options);
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
219
|
//# sourceMappingURL=recordJarHelpers.js.map
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { Result, Converter, Validator
|
|
1
|
+
import { Result, Converter, Validator } from '@fgv/ts-utils';
|
|
2
|
+
import { FileTree, JsonValue } from '@fgv/ts-json-base';
|
|
2
3
|
/**
|
|
3
4
|
* Implementation of `FileTree.IFileTreeFileItem` for files in a ZIP archive.
|
|
4
5
|
* @public
|
|
5
6
|
*/
|
|
6
|
-
export declare class ZipFileItem implements FileTree.IFileTreeFileItem {
|
|
7
|
+
export declare class ZipFileItem<TCT extends string = string> implements FileTree.IFileTreeFileItem<TCT> {
|
|
7
8
|
/**
|
|
8
9
|
* Indicates that this `FileTree.FileTreeItem` is a file.
|
|
9
10
|
*/
|
|
@@ -24,6 +25,10 @@ export declare class ZipFileItem implements FileTree.IFileTreeFileItem {
|
|
|
24
25
|
* The extension of the file
|
|
25
26
|
*/
|
|
26
27
|
readonly extension: string;
|
|
28
|
+
/**
|
|
29
|
+
* The content type of the file.
|
|
30
|
+
*/
|
|
31
|
+
get contentType(): TCT | undefined;
|
|
27
32
|
/**
|
|
28
33
|
* The pre-loaded contents of the file.
|
|
29
34
|
*/
|
|
@@ -32,17 +37,30 @@ export declare class ZipFileItem implements FileTree.IFileTreeFileItem {
|
|
|
32
37
|
* The ZIP file tree accessors that created this item.
|
|
33
38
|
*/
|
|
34
39
|
private readonly _accessors;
|
|
40
|
+
/**
|
|
41
|
+
* Mutable content type of the file.
|
|
42
|
+
*/
|
|
43
|
+
private _contentType;
|
|
44
|
+
/**
|
|
45
|
+
* Flag to track if content type should be inferred on first access.
|
|
46
|
+
*/
|
|
47
|
+
private _shouldInferContentType;
|
|
35
48
|
/**
|
|
36
49
|
* Constructor for ZipFileItem.
|
|
37
50
|
* @param zipFilePath - The path of the file within the ZIP.
|
|
38
51
|
* @param contents - The pre-loaded contents of the file.
|
|
39
52
|
* @param accessors - The ZIP file tree accessors.
|
|
40
53
|
*/
|
|
41
|
-
constructor(zipFilePath: string, contents: string, accessors: ZipFileTreeAccessors);
|
|
54
|
+
constructor(zipFilePath: string, contents: string, accessors: ZipFileTreeAccessors<TCT>);
|
|
55
|
+
/**
|
|
56
|
+
* Sets the content type of the file.
|
|
57
|
+
* @param contentType - The content type of the file.
|
|
58
|
+
*/
|
|
59
|
+
setContentType(contentType: TCT | undefined): void;
|
|
42
60
|
/**
|
|
43
61
|
* Gets the contents of the file as parsed JSON.
|
|
44
62
|
*/
|
|
45
|
-
getContents(): Result<
|
|
63
|
+
getContents(): Result<JsonValue>;
|
|
46
64
|
getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;
|
|
47
65
|
/**
|
|
48
66
|
* Gets the raw contents of the file as a string.
|
|
@@ -53,7 +71,7 @@ export declare class ZipFileItem implements FileTree.IFileTreeFileItem {
|
|
|
53
71
|
* Implementation of `IFileTreeDirectoryItem` for directories in a ZIP archive.
|
|
54
72
|
* @public
|
|
55
73
|
*/
|
|
56
|
-
export declare class ZipDirectoryItem implements FileTree.IFileTreeDirectoryItem {
|
|
74
|
+
export declare class ZipDirectoryItem<TCT extends string = string> implements FileTree.IFileTreeDirectoryItem<TCT> {
|
|
57
75
|
/**
|
|
58
76
|
* Indicates that this `FileTree.FileTreeItem` is a directory.
|
|
59
77
|
*/
|
|
@@ -75,17 +93,17 @@ export declare class ZipDirectoryItem implements FileTree.IFileTreeDirectoryItem
|
|
|
75
93
|
* @param directoryPath - The path of the directory within the ZIP.
|
|
76
94
|
* @param accessors - The ZIP file tree accessors.
|
|
77
95
|
*/
|
|
78
|
-
constructor(directoryPath: string, accessors: ZipFileTreeAccessors);
|
|
96
|
+
constructor(directoryPath: string, accessors: ZipFileTreeAccessors<TCT>);
|
|
79
97
|
/**
|
|
80
98
|
* Gets the children of the directory.
|
|
81
99
|
*/
|
|
82
|
-
getChildren(): Result<ReadonlyArray<FileTree.FileTreeItem
|
|
100
|
+
getChildren(): Result<ReadonlyArray<FileTree.FileTreeItem<TCT>>>;
|
|
83
101
|
}
|
|
84
102
|
/**
|
|
85
103
|
* File tree accessors for ZIP archives.
|
|
86
104
|
* @public
|
|
87
105
|
*/
|
|
88
|
-
export declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors {
|
|
106
|
+
export declare class ZipFileTreeAccessors<TCT extends string = string> implements FileTree.IFileTreeAccessors<TCT> {
|
|
89
107
|
/**
|
|
90
108
|
* The unzipped file data.
|
|
91
109
|
*/
|
|
@@ -94,6 +112,10 @@ export declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors
|
|
|
94
112
|
* Optional prefix to prepend to paths.
|
|
95
113
|
*/
|
|
96
114
|
private readonly _prefix;
|
|
115
|
+
/**
|
|
116
|
+
* Content type inference function.
|
|
117
|
+
*/
|
|
118
|
+
private readonly _inferContentType;
|
|
97
119
|
/**
|
|
98
120
|
* Cache of all items in the ZIP for efficient lookups.
|
|
99
121
|
*/
|
|
@@ -101,30 +123,54 @@ export declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors
|
|
|
101
123
|
/**
|
|
102
124
|
* Constructor for ZipFileTreeAccessors.
|
|
103
125
|
* @param files - The unzipped file data from fflate.
|
|
104
|
-
* @param
|
|
126
|
+
* @param params - Optional initialization parameters.
|
|
105
127
|
*/
|
|
106
128
|
private constructor();
|
|
129
|
+
/**
|
|
130
|
+
* Default function to infer the content type of a file.
|
|
131
|
+
* @param filePath - The path of the file.
|
|
132
|
+
* @param provided - Optional supplied content type.
|
|
133
|
+
* @returns `Success` with the content type of the file if successful, or
|
|
134
|
+
* `Failure` with an error message otherwise.
|
|
135
|
+
* @remarks This default implementation always returns `Success` with `undefined`.
|
|
136
|
+
* @public
|
|
137
|
+
*/
|
|
138
|
+
static defaultInferContentType<TCT extends string = string>(__filePath: string, __provided?: string): Result<TCT | undefined>;
|
|
107
139
|
/**
|
|
108
140
|
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (synchronous).
|
|
109
141
|
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
110
142
|
* @param prefix - Optional prefix to prepend to paths.
|
|
111
143
|
* @returns Result containing the ZipFileTreeAccessors instance.
|
|
112
144
|
*/
|
|
113
|
-
static fromBuffer(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Result<ZipFileTreeAccessors
|
|
145
|
+
static fromBuffer<TCT extends string = string>(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Result<ZipFileTreeAccessors<TCT>>;
|
|
146
|
+
/**
|
|
147
|
+
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (synchronous).
|
|
148
|
+
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
149
|
+
* @param params - Optional initialization parameters.
|
|
150
|
+
* @returns Result containing the ZipFileTreeAccessors instance.
|
|
151
|
+
*/
|
|
152
|
+
static fromBuffer<TCT extends string = string>(zipBuffer: ArrayBuffer | Uint8Array, params?: FileTree.IFileTreeInitParams<TCT>): Result<ZipFileTreeAccessors<TCT>>;
|
|
114
153
|
/**
|
|
115
154
|
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (asynchronous).
|
|
116
155
|
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
117
156
|
* @param prefix - Optional prefix to prepend to paths.
|
|
118
157
|
* @returns Promise containing Result with the ZipFileTreeAccessors instance.
|
|
119
158
|
*/
|
|
120
|
-
static fromBufferAsync(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Promise<Result<ZipFileTreeAccessors
|
|
159
|
+
static fromBufferAsync<TCT extends string = string>(zipBuffer: ArrayBuffer | Uint8Array, prefix?: string): Promise<Result<ZipFileTreeAccessors<TCT>>>;
|
|
160
|
+
/**
|
|
161
|
+
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (asynchronous).
|
|
162
|
+
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
163
|
+
* @param params - Optional initialization parameters.
|
|
164
|
+
* @returns Promise containing Result with the ZipFileTreeAccessors instance.
|
|
165
|
+
*/
|
|
166
|
+
static fromBufferAsync<TCT extends string = string>(zipBuffer: ArrayBuffer | Uint8Array, params?: FileTree.IFileTreeInitParams<TCT>): Promise<Result<ZipFileTreeAccessors<TCT>>>;
|
|
121
167
|
/**
|
|
122
168
|
* Creates a new ZipFileTreeAccessors instance from a File object (browser environment).
|
|
123
169
|
* @param file - The File object containing ZIP data.
|
|
124
|
-
* @param
|
|
170
|
+
* @param params - Optional initialization parameters.
|
|
125
171
|
* @returns Result containing the ZipFileTreeAccessors instance.
|
|
126
172
|
*/
|
|
127
|
-
static fromFile(file: File,
|
|
173
|
+
static fromFile<TCT extends string = string>(file: File, params?: FileTree.IFileTreeInitParams<TCT>): Promise<Result<ZipFileTreeAccessors<TCT>>>;
|
|
128
174
|
/**
|
|
129
175
|
* Builds the cache of all items in the ZIP archive.
|
|
130
176
|
*/
|
|
@@ -148,15 +194,19 @@ export declare class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors
|
|
|
148
194
|
/**
|
|
149
195
|
* Gets an item from the file tree.
|
|
150
196
|
*/
|
|
151
|
-
getItem(path: string): Result<FileTree.FileTreeItem
|
|
197
|
+
getItem(path: string): Result<FileTree.FileTreeItem<TCT>>;
|
|
152
198
|
/**
|
|
153
199
|
* Gets the contents of a file in the file tree.
|
|
154
200
|
*/
|
|
155
201
|
getFileContents(path: string): Result<string>;
|
|
202
|
+
/**
|
|
203
|
+
* Gets the content type of a file in the file tree.
|
|
204
|
+
*/
|
|
205
|
+
getFileContentType(path: string, provided?: string): Result<TCT | undefined>;
|
|
156
206
|
/**
|
|
157
207
|
* Gets the children of a directory in the file tree.
|
|
158
208
|
*/
|
|
159
|
-
getChildren(path: string): Result<ReadonlyArray<FileTree.FileTreeItem
|
|
209
|
+
getChildren(path: string): Result<ReadonlyArray<FileTree.FileTreeItem<TCT>>>;
|
|
160
210
|
/**
|
|
161
211
|
* Checks if childPath is a direct child of parentPath.
|
|
162
212
|
*/
|
|
@@ -29,6 +29,16 @@ const ts_utils_1 = require("@fgv/ts-utils");
|
|
|
29
29
|
* @public
|
|
30
30
|
*/
|
|
31
31
|
class ZipFileItem {
|
|
32
|
+
/**
|
|
33
|
+
* The content type of the file.
|
|
34
|
+
*/
|
|
35
|
+
get contentType() {
|
|
36
|
+
if (this._contentType === undefined && this._shouldInferContentType) {
|
|
37
|
+
this._contentType = this._accessors.getFileContentType(this.absolutePath).orDefault();
|
|
38
|
+
this._shouldInferContentType = false;
|
|
39
|
+
}
|
|
40
|
+
return this._contentType;
|
|
41
|
+
}
|
|
32
42
|
/**
|
|
33
43
|
* Constructor for ZipFileItem.
|
|
34
44
|
* @param zipFilePath - The path of the file within the ZIP.
|
|
@@ -40,6 +50,10 @@ class ZipFileItem {
|
|
|
40
50
|
* Indicates that this `FileTree.FileTreeItem` is a file.
|
|
41
51
|
*/
|
|
42
52
|
this.type = 'file';
|
|
53
|
+
/**
|
|
54
|
+
* Flag to track if content type should be inferred on first access.
|
|
55
|
+
*/
|
|
56
|
+
this._shouldInferContentType = true;
|
|
43
57
|
this._contents = contents;
|
|
44
58
|
this._accessors = accessors;
|
|
45
59
|
this.absolutePath = '/' + zipFilePath;
|
|
@@ -47,16 +61,25 @@ class ZipFileItem {
|
|
|
47
61
|
this.extension = accessors.getExtension(zipFilePath);
|
|
48
62
|
this.baseName = accessors.getBaseName(zipFilePath, this.extension);
|
|
49
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Sets the content type of the file.
|
|
66
|
+
* @param contentType - The content type of the file.
|
|
67
|
+
*/
|
|
68
|
+
setContentType(contentType) {
|
|
69
|
+
this._contentType = contentType;
|
|
70
|
+
this._shouldInferContentType = false;
|
|
71
|
+
}
|
|
50
72
|
getContents(converter) {
|
|
51
73
|
return this.getRawContents()
|
|
52
74
|
.onSuccess((contents) => {
|
|
53
|
-
return (0, ts_utils_1.captureResult)(() =>
|
|
54
|
-
|
|
75
|
+
return (0, ts_utils_1.captureResult)(() => JSON.parse(contents))
|
|
76
|
+
.onSuccess((parsed) => {
|
|
55
77
|
if (converter) {
|
|
56
78
|
return converter.convert(parsed);
|
|
57
79
|
}
|
|
58
80
|
return (0, ts_utils_1.succeed)(parsed);
|
|
59
|
-
})
|
|
81
|
+
})
|
|
82
|
+
.onFailure(() => {
|
|
60
83
|
return (0, ts_utils_1.fail)(`Failed to parse JSON from file: ${this.absolutePath}`);
|
|
61
84
|
});
|
|
62
85
|
})
|
|
@@ -107,42 +130,45 @@ class ZipFileTreeAccessors {
|
|
|
107
130
|
/**
|
|
108
131
|
* Constructor for ZipFileTreeAccessors.
|
|
109
132
|
* @param files - The unzipped file data from fflate.
|
|
110
|
-
* @param
|
|
133
|
+
* @param params - Optional initialization parameters.
|
|
111
134
|
*/
|
|
112
|
-
constructor(files,
|
|
135
|
+
constructor(files, params) {
|
|
136
|
+
var _a;
|
|
113
137
|
/**
|
|
114
138
|
* Cache of all items in the ZIP for efficient lookups.
|
|
115
139
|
*/
|
|
116
140
|
this._itemCache = new Map();
|
|
117
141
|
this._files = files;
|
|
118
|
-
this._prefix = prefix || '';
|
|
142
|
+
this._prefix = (params === null || params === void 0 ? void 0 : params.prefix) || '';
|
|
143
|
+
this._inferContentType = (_a = params === null || params === void 0 ? void 0 : params.inferContentType) !== null && _a !== void 0 ? _a : ZipFileTreeAccessors.defaultInferContentType;
|
|
119
144
|
this._buildItemCache();
|
|
120
145
|
}
|
|
121
146
|
/**
|
|
122
|
-
*
|
|
123
|
-
* @param
|
|
124
|
-
* @param
|
|
125
|
-
* @returns
|
|
147
|
+
* Default function to infer the content type of a file.
|
|
148
|
+
* @param filePath - The path of the file.
|
|
149
|
+
* @param provided - Optional supplied content type.
|
|
150
|
+
* @returns `Success` with the content type of the file if successful, or
|
|
151
|
+
* `Failure` with an error message otherwise.
|
|
152
|
+
* @remarks This default implementation always returns `Success` with `undefined`.
|
|
153
|
+
* @public
|
|
126
154
|
*/
|
|
127
|
-
static
|
|
155
|
+
static defaultInferContentType(__filePath, __provided) {
|
|
156
|
+
return (0, ts_utils_1.succeed)(undefined);
|
|
157
|
+
}
|
|
158
|
+
static fromBuffer(zipBuffer, params) {
|
|
128
159
|
try {
|
|
129
160
|
/* c8 ignore next 1 - defense in depth */
|
|
130
161
|
const uint8Buffer = zipBuffer instanceof Uint8Array ? zipBuffer : new Uint8Array(zipBuffer);
|
|
131
162
|
const files = (0, fflate_1.unzipSync)(uint8Buffer);
|
|
132
|
-
|
|
163
|
+
const normalizedParams = typeof params === 'string' ? { prefix: params } : params;
|
|
164
|
+
return (0, ts_utils_1.succeed)(new ZipFileTreeAccessors(files, normalizedParams));
|
|
133
165
|
}
|
|
134
166
|
catch (error) {
|
|
135
167
|
/* c8 ignore next 1 - defensive coding: fflate always throws Error objects in practice */
|
|
136
168
|
return (0, ts_utils_1.fail)(`Failed to load ZIP archive: ${error instanceof Error ? error.message : String(error)}`);
|
|
137
169
|
}
|
|
138
170
|
}
|
|
139
|
-
|
|
140
|
-
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (asynchronous).
|
|
141
|
-
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
142
|
-
* @param prefix - Optional prefix to prepend to paths.
|
|
143
|
-
* @returns Promise containing Result with the ZipFileTreeAccessors instance.
|
|
144
|
-
*/
|
|
145
|
-
static async fromBufferAsync(zipBuffer, prefix) {
|
|
171
|
+
static async fromBufferAsync(zipBuffer, params) {
|
|
146
172
|
return new Promise((resolve) => {
|
|
147
173
|
try {
|
|
148
174
|
/* c8 ignore next 1 - defense in depth */
|
|
@@ -152,7 +178,8 @@ class ZipFileTreeAccessors {
|
|
|
152
178
|
resolve((0, ts_utils_1.fail)(`Failed to load ZIP archive: ${err.message}`));
|
|
153
179
|
}
|
|
154
180
|
else {
|
|
155
|
-
|
|
181
|
+
const normalizedParams = typeof params === 'string' ? { prefix: params } : params;
|
|
182
|
+
resolve((0, ts_utils_1.succeed)(new ZipFileTreeAccessors(files, normalizedParams)));
|
|
156
183
|
}
|
|
157
184
|
});
|
|
158
185
|
}
|
|
@@ -165,13 +192,14 @@ class ZipFileTreeAccessors {
|
|
|
165
192
|
/**
|
|
166
193
|
* Creates a new ZipFileTreeAccessors instance from a File object (browser environment).
|
|
167
194
|
* @param file - The File object containing ZIP data.
|
|
168
|
-
* @param
|
|
195
|
+
* @param params - Optional initialization parameters.
|
|
169
196
|
* @returns Result containing the ZipFileTreeAccessors instance.
|
|
170
197
|
*/
|
|
171
|
-
static async fromFile(file,
|
|
198
|
+
static async fromFile(file, params) {
|
|
172
199
|
try {
|
|
173
200
|
const arrayBuffer = await file.arrayBuffer();
|
|
174
|
-
|
|
201
|
+
const uint8Buffer = new Uint8Array(arrayBuffer);
|
|
202
|
+
return await ZipFileTreeAccessors.fromBufferAsync(uint8Buffer, params);
|
|
175
203
|
}
|
|
176
204
|
catch (error) {
|
|
177
205
|
return (0, ts_utils_1.fail)(`Failed to read file: ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -275,6 +303,18 @@ class ZipFileTreeAccessors {
|
|
|
275
303
|
return item.getRawContents();
|
|
276
304
|
});
|
|
277
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* Gets the content type of a file in the file tree.
|
|
308
|
+
*/
|
|
309
|
+
getFileContentType(path, provided) {
|
|
310
|
+
// If provided contentType is given, use it directly (highest priority)
|
|
311
|
+
if (provided !== undefined) {
|
|
312
|
+
return (0, ts_utils_1.succeed)(provided);
|
|
313
|
+
}
|
|
314
|
+
// For files that exist in the ZIP, we don't store explicit contentType
|
|
315
|
+
// so we always fall back to inference
|
|
316
|
+
return this._inferContentType(path);
|
|
317
|
+
}
|
|
278
318
|
/**
|
|
279
319
|
* Gets the children of a directory in the file tree.
|
|
280
320
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-extras",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-31",
|
|
4
4
|
"description": "Assorted Typescript Utilities",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "dist/ts-extras.d.ts",
|
|
@@ -45,17 +45,18 @@
|
|
|
45
45
|
"@types/heft-jest": "1.0.6",
|
|
46
46
|
"@rushstack/heft-jest-plugin": "0.16.13",
|
|
47
47
|
"eslint-plugin-tsdoc": "~0.4.0",
|
|
48
|
-
"@fgv/ts-utils
|
|
49
|
-
"@fgv/ts-utils": "5.0.0-
|
|
48
|
+
"@fgv/ts-utils": "5.0.0-31",
|
|
49
|
+
"@fgv/ts-utils-jest": "5.0.0-31"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"luxon": "^3.7.2",
|
|
53
53
|
"mustache": "^4.2.0",
|
|
54
54
|
"papaparse": "^5.4.1",
|
|
55
|
-
"fflate": "~0.8.2"
|
|
55
|
+
"fflate": "~0.8.2",
|
|
56
|
+
"@fgv/ts-json-base": "5.0.0-31"
|
|
56
57
|
},
|
|
57
58
|
"peerDependencies": {
|
|
58
|
-
"@fgv/ts-utils": "5.0.0-
|
|
59
|
+
"@fgv/ts-utils": "5.0.0-31"
|
|
59
60
|
},
|
|
60
61
|
"scripts": {
|
|
61
62
|
"build": "heft test --clean",
|