@fgv/ts-extras 5.0.0-12 → 5.0.0-15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/.vscode/settings.json +27 -0
  2. package/config/api-extractor.json +343 -0
  3. package/config/rig.json +16 -0
  4. package/dist/tsdoc-metadata.json +1 -1
  5. package/lib/test/unit/converters.test.d.ts +2 -0
  6. package/lib/test/unit/csvHelpers.test.d.ts +2 -0
  7. package/lib/test/unit/extendedArray.test.d.ts +2 -0
  8. package/lib/test/unit/formatter.test.d.ts +2 -0
  9. package/lib/test/unit/md5Normalizer.test.d.ts +2 -0
  10. package/lib/test/unit/rangeOf.test.d.ts +2 -0
  11. package/lib/test/unit/recordJarHelpers.test.d.ts +2 -0
  12. package/lib/test/unit/zipFileTreeAccessors.test.d.ts +2 -0
  13. package/package.json +13 -13
  14. package/src/index.ts +31 -0
  15. package/src/packlets/conversion/converters.ts +124 -0
  16. package/src/packlets/conversion/index.ts +25 -0
  17. package/src/packlets/csv/csvHelpers.ts +58 -0
  18. package/src/packlets/csv/index.ts +23 -0
  19. package/src/packlets/experimental/extendedArray.ts +110 -0
  20. package/src/packlets/experimental/formatter.ts +114 -0
  21. package/src/packlets/experimental/index.ts +25 -0
  22. package/src/packlets/experimental/rangeOf.ts +222 -0
  23. package/src/packlets/hash/index.ts +23 -0
  24. package/src/packlets/hash/md5Normalizer.ts +38 -0
  25. package/src/packlets/record-jar/index.ts +23 -0
  26. package/src/packlets/record-jar/recordJarHelpers.ts +278 -0
  27. package/src/packlets/zip-file-tree/index.ts +33 -0
  28. package/src/packlets/zip-file-tree/zipFileTreeAccessors.ts +375 -0
  29. package/CHANGELOG.md +0 -98
  30. package/lib/index.d.ts.map +0 -1
  31. package/lib/index.js.map +0 -1
  32. package/lib/packlets/conversion/converters.d.ts.map +0 -1
  33. package/lib/packlets/conversion/converters.js.map +0 -1
  34. package/lib/packlets/conversion/index.d.ts.map +0 -1
  35. package/lib/packlets/conversion/index.js.map +0 -1
  36. package/lib/packlets/csv/csvHelpers.d.ts.map +0 -1
  37. package/lib/packlets/csv/csvHelpers.js.map +0 -1
  38. package/lib/packlets/csv/index.d.ts.map +0 -1
  39. package/lib/packlets/csv/index.js.map +0 -1
  40. package/lib/packlets/experimental/extendedArray.d.ts.map +0 -1
  41. package/lib/packlets/experimental/extendedArray.js.map +0 -1
  42. package/lib/packlets/experimental/formatter.d.ts.map +0 -1
  43. package/lib/packlets/experimental/formatter.js.map +0 -1
  44. package/lib/packlets/experimental/index.d.ts.map +0 -1
  45. package/lib/packlets/experimental/index.js.map +0 -1
  46. package/lib/packlets/experimental/rangeOf.d.ts.map +0 -1
  47. package/lib/packlets/experimental/rangeOf.js.map +0 -1
  48. package/lib/packlets/hash/index.d.ts.map +0 -1
  49. package/lib/packlets/hash/index.js.map +0 -1
  50. package/lib/packlets/hash/md5Normalizer.d.ts.map +0 -1
  51. package/lib/packlets/hash/md5Normalizer.js.map +0 -1
  52. package/lib/packlets/record-jar/index.d.ts.map +0 -1
  53. package/lib/packlets/record-jar/index.js.map +0 -1
  54. package/lib/packlets/record-jar/recordJarHelpers.d.ts.map +0 -1
  55. package/lib/packlets/record-jar/recordJarHelpers.js.map +0 -1
  56. package/lib/packlets/zip-file-tree/index.d.ts.map +0 -1
  57. package/lib/packlets/zip-file-tree/index.js.map +0 -1
  58. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.d.ts.map +0 -1
  59. package/lib/packlets/zip-file-tree/zipFileTreeAccessors.js.map +0 -1
@@ -0,0 +1,278 @@
1
+ /*
2
+ * Copyright (c) 2022 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ import * as fs from 'fs';
24
+ import * as path from 'path';
25
+
26
+ import { Result, captureResult, fail, isKeyOf, succeed } from '@fgv/ts-utils';
27
+
28
+ interface IRecordBody {
29
+ body: string;
30
+ isContinuation: boolean;
31
+ }
32
+
33
+ /**
34
+ * Represents a single record in a JAR file
35
+ * @public
36
+ */
37
+ export type JarRecord = Record<string, string | string[]>;
38
+
39
+ /**
40
+ * @public
41
+ */
42
+ export type JarFieldPicker<T extends JarRecord = JarRecord> = (record: T) => (keyof T)[];
43
+
44
+ /**
45
+ * Options for a JAR record parser.
46
+ * @public
47
+ */
48
+ // eslint-disable-next-line @typescript-eslint/naming-convention
49
+ export interface JarRecordParserOptions {
50
+ readonly arrayFields?: string[] | JarFieldPicker;
51
+ readonly fixedContinuationSize?: number;
52
+ }
53
+
54
+ class RecordParser {
55
+ public readonly records: JarRecord[] = [];
56
+ public readonly options: JarRecordParserOptions;
57
+
58
+ protected _fields: JarRecord = {};
59
+ protected _name: string | undefined = undefined;
60
+ protected _body: IRecordBody | undefined = undefined;
61
+
62
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
63
+ private constructor(options?: JarRecordParserOptions) {
64
+ this.options = options ?? {};
65
+ }
66
+
67
+ public static parse(lines: string[], options?: JarRecordParserOptions): Result<JarRecord[]> {
68
+ return new RecordParser(options)._parse(lines);
69
+ }
70
+
71
+ protected static _parseRecordBody(body: string): Result<IRecordBody> {
72
+ const isContinuation = body.endsWith('\\');
73
+ if (isContinuation) {
74
+ body = body.slice(0, body.length - 1);
75
+ }
76
+ if (this._hasEscapes(body)) {
77
+ const result = this._replaceEscapes(body);
78
+ if (result.isFailure()) {
79
+ return fail(result.message);
80
+ }
81
+ body = result.value;
82
+ }
83
+ return succeed({ body, isContinuation });
84
+ }
85
+
86
+ protected static _hasEscapes(from: string): boolean {
87
+ return from.includes('\\') || from.includes('&');
88
+ }
89
+
90
+ protected static _replaceEscapes(body: string): Result<string> {
91
+ const invalid: string[] = [];
92
+ const escaped = body.replace(/(\\.)|(&#x[a-fA-F0-9]{2,6};)/g, (match) => {
93
+ switch (match) {
94
+ case '\\\\':
95
+ return '\\';
96
+ case '\\&':
97
+ return '&';
98
+ case '\\r':
99
+ return '\r';
100
+ case '\\n':
101
+ return '\n';
102
+ case '\\t':
103
+ return '\t';
104
+ }
105
+ if (match.startsWith('&')) {
106
+ const hexCode = `0x${match.slice(3, match.length - 1)}`;
107
+ const charCode = Number.parseInt(hexCode, 16);
108
+ return String.fromCharCode(charCode);
109
+ }
110
+ invalid.push(match);
111
+ return '\\';
112
+ });
113
+ if (invalid.length > 0) {
114
+ return fail(`unrecognized escape "${invalid.join(', ')}" in record-jar body`);
115
+ }
116
+ return succeed(escaped);
117
+ }
118
+
119
+ protected static _applyOptions(record: JarRecord, options: JarRecordParserOptions): JarRecord {
120
+ if (options.arrayFields) {
121
+ record = { ...record }; // don't edit incoming values
122
+ const arrayFields = Array.isArray(options.arrayFields)
123
+ ? options.arrayFields
124
+ : options.arrayFields(record);
125
+
126
+ for (const field of arrayFields) {
127
+ if (isKeyOf(field, record) && typeof record[field] === 'string') {
128
+ const current = record[field] as string;
129
+ record[field] = [current];
130
+ }
131
+ }
132
+ }
133
+ return record;
134
+ }
135
+
136
+ protected _parse(lines: string[]): Result<JarRecord[]> {
137
+ for (let n = 0; n < lines.length; n++) {
138
+ const line = lines[n];
139
+ if (line.startsWith('%%') && !this._body?.isContinuation) {
140
+ const result = this._writePendingRecord();
141
+ if (result.isFailure()) {
142
+ return fail(`${n}: ${result.message}`);
143
+ }
144
+ } else if (/^\s*$/.test(line)) {
145
+ // ignore blank lines but cancel continuation
146
+ if (this._body) {
147
+ this._body.isContinuation = false;
148
+ }
149
+ continue;
150
+ } else if (this._body?.isContinuation || /^\s+/.test(line)) {
151
+ // explicit continuation on previous line or implicit starts with whitespace
152
+ if (this._body === undefined) {
153
+ return fail(`${n}: continuation ("${line}") without prior value.`);
154
+ }
155
+ const result = this._parseContinuation(line);
156
+ if (result.isFailure()) {
157
+ return fail(`${n}: ${result.message}`);
158
+ }
159
+ this._body = result.value;
160
+ } else {
161
+ const result = this._parseField(line);
162
+ if (result.isFailure()) {
163
+ return fail(`${n}: ${result.message}`);
164
+ }
165
+ }
166
+ }
167
+
168
+ const result = this._writePendingRecord();
169
+ if (result.isFailure()) {
170
+ return fail(`${lines.length}: ${result.message}`);
171
+ }
172
+ return succeed(this.records);
173
+ }
174
+
175
+ protected _parseField(line: string): Result<boolean> {
176
+ const separatorIndex = line.indexOf(':');
177
+ if (separatorIndex < 1) {
178
+ return fail(`malformed line ("${line}") in record-jar.`);
179
+ }
180
+ const parts = [line.slice(0, separatorIndex), line.slice(separatorIndex + 1)];
181
+
182
+ return this._writePendingField().onSuccess(() => {
183
+ this._name = parts[0].trimEnd();
184
+ return RecordParser._parseRecordBody(parts[1].trim()).onSuccess((body) => {
185
+ this._body = body;
186
+ return succeed(true);
187
+ });
188
+ });
189
+ }
190
+
191
+ protected _parseContinuation(line: string): Result<IRecordBody> {
192
+ let trimmed = line.trim();
193
+ if (!this._body!.isContinuation) {
194
+ /* c8 ignore next */
195
+ const fixedSize = this.options?.fixedContinuationSize ?? 0;
196
+ if (fixedSize > 0) {
197
+ if (trimmed.length < line.length - fixedSize) {
198
+ // oops, took too much
199
+ trimmed = line.slice(fixedSize);
200
+ }
201
+ }
202
+ }
203
+ return RecordParser._parseRecordBody(trimmed).onSuccess((newBody) => {
204
+ return succeed({
205
+ body: `${this._body!.body}${newBody.body}`,
206
+ isContinuation: newBody.isContinuation
207
+ });
208
+ });
209
+ }
210
+
211
+ protected _havePendingRecord(): boolean {
212
+ return Object.keys(this._fields).length > 0;
213
+ }
214
+
215
+ protected _writePendingRecord(): Result<JarRecord | undefined> {
216
+ return this._writePendingField().onSuccess(() => {
217
+ let record = this._havePendingRecord() ? this._fields : undefined;
218
+ if (record !== undefined) {
219
+ record = RecordParser._applyOptions(record, this.options);
220
+ this.records.push(record);
221
+ this._fields = {};
222
+ }
223
+ return succeed(undefined);
224
+ });
225
+ }
226
+
227
+ protected _writePendingField(): Result<boolean> {
228
+ if (this._name !== undefined) {
229
+ if (this._body!.body.length < 1) {
230
+ return fail('empty body value not allowed');
231
+ }
232
+ if (!isKeyOf(this._name, this._fields)) {
233
+ this._fields[this._name] = this._body!.body;
234
+ } else if (typeof this._fields[this._name] === 'string') {
235
+ const current = this._fields[this._name] as string;
236
+ this._fields[this._name] = [current, this._body!.body];
237
+ } else {
238
+ const current = this._fields[this._name] as string[];
239
+ current.push(this._body!.body);
240
+ }
241
+ this._name = undefined;
242
+ this._body = undefined;
243
+ }
244
+ return succeed(true);
245
+ }
246
+ }
247
+
248
+ /**
249
+ * Reads a record-jar from an array of strings, each of which represents one
250
+ * line in the source file.
251
+ * @param lines - the array of strings to be parsed
252
+ * @param options - Optional parser configuration
253
+ * @returns a corresponding array of `Record<string, string>`
254
+ * @public
255
+ */
256
+ export function parseRecordJarLines(lines: string[], options?: JarRecordParserOptions): Result<JarRecord[]> {
257
+ return RecordParser.parse(lines, options);
258
+ }
259
+
260
+ /**
261
+ * Reads a record-jar file from a supplied path.
262
+ * @param srcPath - Source path from which the file is read.
263
+ * @param options - Optional parser configuration
264
+ * @returns The contents of the file as an array of `Record<string, string>`
265
+ * @see https://datatracker.ietf.org/doc/html/draft-phillips-record-jar-01
266
+ * @public
267
+ */
268
+ export function readRecordJarFileSync(
269
+ srcPath: string,
270
+ options?: JarRecordParserOptions
271
+ ): Result<JarRecord[]> {
272
+ return captureResult(() => {
273
+ const fullPath = path.resolve(srcPath);
274
+ return fs.readFileSync(fullPath, 'utf8').toString().split(/\r?\n/);
275
+ }).onSuccess((lines) => {
276
+ return parseRecordJarLines(lines, options);
277
+ });
278
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Copyright (c) 2025 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ /**
24
+ * ZIP-based FileTree implementation for ts-extras.
25
+ *
26
+ * This packlet provides a FileTree accessor implementation that can read from ZIP archives,
27
+ * making it useful for browser environments where files need to be bundled and transferred
28
+ * as a single archive.
29
+ *
30
+ * @packageDocumentation
31
+ */
32
+
33
+ export { ZipFileTreeAccessors, ZipFileItem, ZipDirectoryItem } from './zipFileTreeAccessors';
@@ -0,0 +1,375 @@
1
+ /*
2
+ * Copyright (c) 2025 Erik Fortune
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all
12
+ * copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+ import AdmZip from 'adm-zip';
24
+ import { Result, succeed, fail, captureResult, Converter, Validator, FileTree } from '@fgv/ts-utils';
25
+
26
+ /**
27
+ * Implementation of `FileTree.IFileTreeFileItem` for files in a ZIP archive.
28
+ * @public
29
+ */
30
+ export class ZipFileItem implements FileTree.IFileTreeFileItem {
31
+ /**
32
+ * Indicates that this `FileTree.FileTreeItem` is a file.
33
+ */
34
+ public readonly type: 'file' = 'file';
35
+
36
+ /**
37
+ * The absolute path of the file within the ZIP archive.
38
+ */
39
+ public readonly absolutePath: string;
40
+
41
+ /**
42
+ * The name of the file
43
+ */
44
+ public readonly name: string;
45
+
46
+ /**
47
+ * The base name of the file (without extension)
48
+ */
49
+ public readonly baseName: string;
50
+
51
+ /**
52
+ * The extension of the file
53
+ */
54
+ public readonly extension: string;
55
+
56
+ /**
57
+ * The pre-loaded contents of the file.
58
+ */
59
+ private readonly _contents: string;
60
+
61
+ /**
62
+ * The ZIP file tree accessors that created this item.
63
+ */
64
+ private readonly _accessors: ZipFileTreeAccessors;
65
+
66
+ /**
67
+ * Constructor for ZipFileItem.
68
+ * @param zipFilePath - The path of the file within the ZIP.
69
+ * @param contents - The pre-loaded contents of the file.
70
+ * @param accessors - The ZIP file tree accessors.
71
+ */
72
+ public constructor(zipFilePath: string, contents: string, accessors: ZipFileTreeAccessors) {
73
+ this._contents = contents;
74
+ this._accessors = accessors;
75
+ this.absolutePath = '/' + zipFilePath;
76
+ this.name = accessors.getBaseName(zipFilePath);
77
+ this.extension = accessors.getExtension(zipFilePath);
78
+ this.baseName = accessors.getBaseName(zipFilePath, this.extension);
79
+ }
80
+
81
+ /**
82
+ * Gets the contents of the file as parsed JSON.
83
+ */
84
+ public getContents(): Result<unknown>;
85
+ public getContents<T>(converter: Validator<T> | Converter<T>): Result<T>;
86
+ public getContents<T>(converter?: Validator<T> | Converter<T>): Result<T | unknown> {
87
+ return this.getRawContents()
88
+ .onSuccess((contents) => {
89
+ return captureResult(() => {
90
+ const parsed = JSON.parse(contents);
91
+ if (converter) {
92
+ if ('convert' in converter) {
93
+ return converter.convert(parsed);
94
+ } /* c8 ignore next 3 - validator branch functionally tested but coverage tool misses due to interface complexity */ else {
95
+ return (converter as Validator<T>).validate(parsed);
96
+ }
97
+ }
98
+ return succeed(parsed);
99
+ }).onFailure(() => {
100
+ return fail(`Failed to parse JSON from file: ${this.absolutePath}`);
101
+ });
102
+ })
103
+ .onFailure((error) => {
104
+ return fail(`Failed to get contents from file: ${error}`);
105
+ });
106
+ }
107
+
108
+ /**
109
+ * Gets the raw contents of the file as a string.
110
+ */
111
+ public getRawContents(): Result<string> {
112
+ return succeed(this._contents);
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Implementation of `IFileTreeDirectoryItem` for directories in a ZIP archive.
118
+ * @public
119
+ */
120
+ export class ZipDirectoryItem implements FileTree.IFileTreeDirectoryItem {
121
+ /**
122
+ * Indicates that this `FileTree.FileTreeItem` is a directory.
123
+ */
124
+ public readonly type: 'directory' = 'directory';
125
+
126
+ /**
127
+ * The absolute path of the directory within the ZIP archive.
128
+ */
129
+ public readonly absolutePath: string;
130
+
131
+ /**
132
+ * The name of the directory
133
+ */
134
+ public readonly name: string;
135
+
136
+ /**
137
+ * The ZIP file tree accessors that created this item.
138
+ */
139
+ private readonly _accessors: ZipFileTreeAccessors;
140
+
141
+ /**
142
+ * Constructor for ZipDirectoryItem.
143
+ * @param directoryPath - The path of the directory within the ZIP.
144
+ * @param accessors - The ZIP file tree accessors.
145
+ */
146
+ public constructor(directoryPath: string, accessors: ZipFileTreeAccessors) {
147
+ this._accessors = accessors;
148
+ this.absolutePath = '/' + directoryPath.replace(/\/$/, ''); // Normalize path
149
+ this.name = accessors.getBaseName(directoryPath);
150
+ }
151
+
152
+ /**
153
+ * Gets the children of the directory.
154
+ */
155
+ public getChildren(): Result<ReadonlyArray<FileTree.FileTreeItem>> {
156
+ return this._accessors.getChildren(this.absolutePath);
157
+ }
158
+ }
159
+
160
+ /**
161
+ * File tree accessors for ZIP archives.
162
+ * @public
163
+ */
164
+ export class ZipFileTreeAccessors implements FileTree.IFileTreeAccessors {
165
+ /**
166
+ * The AdmZip instance containing the archive.
167
+ */
168
+ private readonly _zip: AdmZip;
169
+
170
+ /**
171
+ * Optional prefix to prepend to paths.
172
+ */
173
+ private readonly _prefix: string;
174
+
175
+ /**
176
+ * Cache of all items in the ZIP for efficient lookups.
177
+ */
178
+ private readonly _itemCache: Map<string, FileTree.FileTreeItem> = new Map();
179
+
180
+ /**
181
+ * Constructor for ZipFileTreeAccessors.
182
+ * @param zip - The AdmZip instance.
183
+ * @param prefix - Optional prefix to prepend to paths.
184
+ */
185
+ private constructor(zip: AdmZip, prefix?: string) {
186
+ this._zip = zip;
187
+ this._prefix = prefix || '';
188
+ this._buildItemCache();
189
+ }
190
+
191
+ /**
192
+ * Creates a new ZipFileTreeAccessors instance from a ZIP file buffer.
193
+ * @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
194
+ * @param prefix - Optional prefix to prepend to paths.
195
+ * @returns Result containing the ZipFileTreeAccessors instance.
196
+ */
197
+ public static fromBuffer(
198
+ zipBuffer: ArrayBuffer | Uint8Array,
199
+ prefix?: string
200
+ ): Result<ZipFileTreeAccessors> {
201
+ try {
202
+ const buffer = Buffer.from(zipBuffer);
203
+ const zip = new AdmZip(buffer);
204
+ return succeed(new ZipFileTreeAccessors(zip, prefix));
205
+ } catch (error) {
206
+ /* c8 ignore next 1 - defensive coding: AdmZip always throws Error objects in practice */
207
+ return fail(`Failed to load ZIP archive: ${error instanceof Error ? error.message : String(error)}`);
208
+ }
209
+ }
210
+
211
+ /**
212
+ * Creates a new ZipFileTreeAccessors instance from a File object (browser environment).
213
+ * @param file - The File object containing ZIP data.
214
+ * @param prefix - Optional prefix to prepend to paths.
215
+ * @returns Result containing the ZipFileTreeAccessors instance.
216
+ */
217
+ public static async fromFile(file: File, prefix?: string): Promise<Result<ZipFileTreeAccessors>> {
218
+ try {
219
+ const arrayBuffer = await file.arrayBuffer();
220
+ return ZipFileTreeAccessors.fromBuffer(new Uint8Array(arrayBuffer), prefix);
221
+ } catch (error) {
222
+ return fail(`Failed to read file: ${error instanceof Error ? error.message : String(error)}`);
223
+ }
224
+ }
225
+
226
+ /**
227
+ * Builds the cache of all items in the ZIP archive.
228
+ */
229
+ private _buildItemCache(): void {
230
+ const directories = new Set<string>();
231
+ const entries = this._zip.getEntries();
232
+
233
+ // First pass: collect all directories from file paths
234
+ entries.forEach((entry) => {
235
+ if (!entry.isDirectory) {
236
+ // Extract directory path from file path
237
+ const pathParts = entry.entryName.split('/');
238
+ for (let i = 1; i < pathParts.length; i++) {
239
+ const dirPath = pathParts.slice(0, i).join('/');
240
+ directories.add(dirPath);
241
+ }
242
+ } else {
243
+ // Also add explicit directories
244
+ const dirPath = entry.entryName.replace(/\/$/, ''); // Remove trailing slash
245
+ if (dirPath) {
246
+ directories.add(dirPath);
247
+ }
248
+ }
249
+ });
250
+
251
+ // Add directory items to cache
252
+ directories.forEach((dirPath) => {
253
+ const absolutePath = this.resolveAbsolutePath(dirPath);
254
+ const item = new ZipDirectoryItem(dirPath, this);
255
+ this._itemCache.set(absolutePath, item);
256
+ });
257
+
258
+ // Add file items to cache
259
+ entries.forEach((entry) => {
260
+ if (!entry.isDirectory) {
261
+ const absolutePath = this.resolveAbsolutePath(entry.entryName);
262
+ const contents = entry.getData().toString('utf8');
263
+ const item = new ZipFileItem(entry.entryName, contents, this);
264
+ this._itemCache.set(absolutePath, item);
265
+ }
266
+ });
267
+ }
268
+
269
+ /**
270
+ * Resolves paths to an absolute path.
271
+ */
272
+ public resolveAbsolutePath(...paths: string[]): string {
273
+ const joinedPath = this.joinPaths(...paths);
274
+ const prefixed = this._prefix ? this.joinPaths(this._prefix, joinedPath) : joinedPath;
275
+ return prefixed.startsWith('/') ? prefixed : '/' + prefixed;
276
+ }
277
+
278
+ /**
279
+ * Gets the extension of a path.
280
+ */
281
+ public getExtension(path: string): string {
282
+ const name = this.getBaseName(path);
283
+ const lastDotIndex = name.lastIndexOf('.');
284
+ return lastDotIndex >= 0 ? name.substring(lastDotIndex) : '';
285
+ }
286
+
287
+ /**
288
+ * Gets the base name of a path.
289
+ */
290
+ public getBaseName(path: string, suffix?: string): string {
291
+ const normalizedPath = path.replace(/\/$/, ''); // Remove trailing slash
292
+ const parts = normalizedPath.split('/');
293
+ let baseName = parts[parts.length - 1] || '';
294
+
295
+ if (suffix && baseName.endsWith(suffix)) {
296
+ baseName = baseName.substring(0, baseName.length - suffix.length);
297
+ }
298
+
299
+ return baseName;
300
+ }
301
+
302
+ /**
303
+ * Joins paths together.
304
+ */
305
+ public joinPaths(...paths: string[]): string {
306
+ return paths
307
+ .filter((p) => p && p.length > 0)
308
+ .map((p) => p.replace(/^\/+|\/+$/g, '')) // Remove leading/trailing slashes
309
+ .join('/')
310
+ .replace(/\/+/g, '/'); // Normalize multiple slashes
311
+ }
312
+
313
+ /**
314
+ * Gets an item from the file tree.
315
+ */
316
+ public getItem(path: string): Result<FileTree.FileTreeItem> {
317
+ const absolutePath = this.resolveAbsolutePath(path);
318
+ const item = this._itemCache.get(absolutePath);
319
+
320
+ if (item) {
321
+ return succeed(item);
322
+ }
323
+
324
+ return fail(`Item not found: ${absolutePath}`);
325
+ }
326
+
327
+ /**
328
+ * Gets the contents of a file in the file tree.
329
+ */
330
+ public getFileContents(path: string): Result<string> {
331
+ return this.getItem(path).onSuccess((item) => {
332
+ if (item.type !== 'file') {
333
+ return fail(`Path is not a file: ${path}`);
334
+ }
335
+ return item.getRawContents();
336
+ });
337
+ }
338
+
339
+ /**
340
+ * Gets the children of a directory in the file tree.
341
+ */
342
+ public getChildren(path: string): Result<ReadonlyArray<FileTree.FileTreeItem>> {
343
+ const absolutePath = this.resolveAbsolutePath(path);
344
+ const children: FileTree.FileTreeItem[] = [];
345
+
346
+ // Find all items that are direct children of this directory
347
+ for (const [itemPath, item] of this._itemCache) {
348
+ if (this._isDirectChild(absolutePath, itemPath)) {
349
+ children.push(item);
350
+ }
351
+ }
352
+
353
+ return succeed(children);
354
+ }
355
+
356
+ /**
357
+ * Checks if childPath is a direct child of parentPath.
358
+ */
359
+ private _isDirectChild(parentPath: string, childPath: string): boolean {
360
+ // Normalize paths
361
+ const normalizedParent = parentPath.replace(/\/$/, '');
362
+ const normalizedChild = childPath.replace(/\/$/, '');
363
+
364
+ // Child must start with parent path
365
+ if (!normalizedChild.startsWith(normalizedParent + '/')) {
366
+ return false;
367
+ }
368
+
369
+ // Get the relative path from parent to child
370
+ const relativePath = normalizedChild.substring(normalizedParent.length + 1);
371
+
372
+ // Direct child means no additional slashes in the relative path
373
+ return !relativePath.includes('/');
374
+ }
375
+ }