@fgv/ts-extras 5.0.0-21 → 5.0.0-23
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/package.json +4 -4
- package/src/index.ts +0 -31
- package/src/packlets/conversion/converters.ts +0 -124
- package/src/packlets/conversion/index.ts +0 -25
- package/src/packlets/csv/csvHelpers.ts +0 -58
- package/src/packlets/csv/index.ts +0 -23
- package/src/packlets/experimental/extendedArray.ts +0 -110
- package/src/packlets/experimental/formatter.ts +0 -114
- package/src/packlets/experimental/index.ts +0 -25
- package/src/packlets/experimental/rangeOf.ts +0 -222
- package/src/packlets/hash/index.ts +0 -23
- package/src/packlets/hash/md5Normalizer.ts +0 -38
- package/src/packlets/record-jar/index.ts +0 -23
- package/src/packlets/record-jar/recordJarHelpers.ts +0 -278
- package/src/packlets/zip-file-tree/index.ts +0 -33
- package/src/packlets/zip-file-tree/zipFileTreeAccessors.ts +0 -404
|
@@ -1,404 +0,0 @@
|
|
|
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 { unzipSync, unzip, Unzipped } from 'fflate';
|
|
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 unzipped file data.
|
|
167
|
-
*/
|
|
168
|
-
private readonly _files: Unzipped;
|
|
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 files - The unzipped file data from fflate.
|
|
183
|
-
* @param prefix - Optional prefix to prepend to paths.
|
|
184
|
-
*/
|
|
185
|
-
private constructor(files: Unzipped, prefix?: string) {
|
|
186
|
-
this._files = files;
|
|
187
|
-
this._prefix = prefix || '';
|
|
188
|
-
this._buildItemCache();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (synchronous).
|
|
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
|
-
/* c8 ignore next 1 - defense in depth */
|
|
203
|
-
const uint8Buffer = zipBuffer instanceof Uint8Array ? zipBuffer : new Uint8Array(zipBuffer);
|
|
204
|
-
const files = unzipSync(uint8Buffer);
|
|
205
|
-
return succeed(new ZipFileTreeAccessors(files, prefix));
|
|
206
|
-
} catch (error) {
|
|
207
|
-
/* c8 ignore next 1 - defensive coding: fflate always throws Error objects in practice */
|
|
208
|
-
return fail(`Failed to load ZIP archive: ${error instanceof Error ? error.message : String(error)}`);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Creates a new ZipFileTreeAccessors instance from a ZIP file buffer (asynchronous).
|
|
214
|
-
* @param zipBuffer - The ZIP file as an ArrayBuffer or Uint8Array.
|
|
215
|
-
* @param prefix - Optional prefix to prepend to paths.
|
|
216
|
-
* @returns Promise containing Result with the ZipFileTreeAccessors instance.
|
|
217
|
-
*/
|
|
218
|
-
public static async fromBufferAsync(
|
|
219
|
-
zipBuffer: ArrayBuffer | Uint8Array,
|
|
220
|
-
prefix?: string
|
|
221
|
-
): Promise<Result<ZipFileTreeAccessors>> {
|
|
222
|
-
return new Promise((resolve) => {
|
|
223
|
-
try {
|
|
224
|
-
/* c8 ignore next 1 - defense in depth */
|
|
225
|
-
const uint8Buffer = zipBuffer instanceof Uint8Array ? zipBuffer : new Uint8Array(zipBuffer);
|
|
226
|
-
unzip(uint8Buffer, (err, files) => {
|
|
227
|
-
if (err) {
|
|
228
|
-
resolve(fail(`Failed to load ZIP archive: ${err.message}`));
|
|
229
|
-
} else {
|
|
230
|
-
resolve(succeed(new ZipFileTreeAccessors(files, prefix)));
|
|
231
|
-
}
|
|
232
|
-
});
|
|
233
|
-
} catch (error) {
|
|
234
|
-
/* c8 ignore next 5 - defensive coding: fflate always throws Error objects in practice */
|
|
235
|
-
resolve(
|
|
236
|
-
fail(`Failed to load ZIP archive: ${error instanceof Error ? error.message : String(error)}`)
|
|
237
|
-
);
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Creates a new ZipFileTreeAccessors instance from a File object (browser environment).
|
|
244
|
-
* @param file - The File object containing ZIP data.
|
|
245
|
-
* @param prefix - Optional prefix to prepend to paths.
|
|
246
|
-
* @returns Result containing the ZipFileTreeAccessors instance.
|
|
247
|
-
*/
|
|
248
|
-
public static async fromFile(file: File, prefix?: string): Promise<Result<ZipFileTreeAccessors>> {
|
|
249
|
-
try {
|
|
250
|
-
const arrayBuffer = await file.arrayBuffer();
|
|
251
|
-
return await ZipFileTreeAccessors.fromBufferAsync(new Uint8Array(arrayBuffer), prefix);
|
|
252
|
-
} catch (error) {
|
|
253
|
-
return fail(`Failed to read file: ${error instanceof Error ? error.message : String(error)}`);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Builds the cache of all items in the ZIP archive.
|
|
259
|
-
*/
|
|
260
|
-
private _buildItemCache(): void {
|
|
261
|
-
const directories = new Set<string>();
|
|
262
|
-
|
|
263
|
-
// Process all files and collect directory paths
|
|
264
|
-
for (const [relativePath, fileData] of Object.entries(this._files)) {
|
|
265
|
-
// Skip directories in fflate output (they have null data)
|
|
266
|
-
/* c8 ignore next 5 - handles explicit directory entries from external ZIP tools (fflate doesn't create these) */
|
|
267
|
-
if (fileData === null || fileData === undefined) {
|
|
268
|
-
const dirPath = relativePath.replace(/\/$/, '');
|
|
269
|
-
if (dirPath) {
|
|
270
|
-
directories.add(dirPath);
|
|
271
|
-
}
|
|
272
|
-
} else {
|
|
273
|
-
// Extract directory paths from file paths
|
|
274
|
-
const pathParts = relativePath.split('/');
|
|
275
|
-
for (let i = 1; i < pathParts.length; i++) {
|
|
276
|
-
const dirPath = pathParts.slice(0, i).join('/');
|
|
277
|
-
if (dirPath) {
|
|
278
|
-
directories.add(dirPath);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// Add the file item with its contents
|
|
283
|
-
const absolutePath = this.resolveAbsolutePath(relativePath);
|
|
284
|
-
const contents = new TextDecoder().decode(fileData);
|
|
285
|
-
const item = new ZipFileItem(relativePath, contents, this);
|
|
286
|
-
this._itemCache.set(absolutePath, item);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
// Add directory items to cache
|
|
291
|
-
directories.forEach((dirPath) => {
|
|
292
|
-
const absolutePath = this.resolveAbsolutePath(dirPath);
|
|
293
|
-
const item = new ZipDirectoryItem(dirPath, this);
|
|
294
|
-
this._itemCache.set(absolutePath, item);
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Resolves paths to an absolute path.
|
|
300
|
-
*/
|
|
301
|
-
public resolveAbsolutePath(...paths: string[]): string {
|
|
302
|
-
const joinedPath = this.joinPaths(...paths);
|
|
303
|
-
const prefixed = this._prefix ? this.joinPaths(this._prefix, joinedPath) : joinedPath;
|
|
304
|
-
return prefixed.startsWith('/') ? prefixed : '/' + prefixed;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Gets the extension of a path.
|
|
309
|
-
*/
|
|
310
|
-
public getExtension(path: string): string {
|
|
311
|
-
const name = this.getBaseName(path);
|
|
312
|
-
const lastDotIndex = name.lastIndexOf('.');
|
|
313
|
-
return lastDotIndex >= 0 ? name.substring(lastDotIndex) : '';
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Gets the base name of a path.
|
|
318
|
-
*/
|
|
319
|
-
public getBaseName(path: string, suffix?: string): string {
|
|
320
|
-
const normalizedPath = path.replace(/\/$/, ''); // Remove trailing slash
|
|
321
|
-
const parts = normalizedPath.split('/');
|
|
322
|
-
let baseName = parts[parts.length - 1] || '';
|
|
323
|
-
|
|
324
|
-
if (suffix && baseName.endsWith(suffix)) {
|
|
325
|
-
baseName = baseName.substring(0, baseName.length - suffix.length);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
return baseName;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* Joins paths together.
|
|
333
|
-
*/
|
|
334
|
-
public joinPaths(...paths: string[]): string {
|
|
335
|
-
return paths
|
|
336
|
-
.filter((p) => p && p.length > 0)
|
|
337
|
-
.map((p) => p.replace(/^\/+|\/+$/g, '')) // Remove leading/trailing slashes
|
|
338
|
-
.join('/')
|
|
339
|
-
.replace(/\/+/g, '/'); // Normalize multiple slashes
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Gets an item from the file tree.
|
|
344
|
-
*/
|
|
345
|
-
public getItem(path: string): Result<FileTree.FileTreeItem> {
|
|
346
|
-
const absolutePath = this.resolveAbsolutePath(path);
|
|
347
|
-
const item = this._itemCache.get(absolutePath);
|
|
348
|
-
|
|
349
|
-
if (item) {
|
|
350
|
-
return succeed(item);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
return fail(`Item not found: ${absolutePath}`);
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* Gets the contents of a file in the file tree.
|
|
358
|
-
*/
|
|
359
|
-
public getFileContents(path: string): Result<string> {
|
|
360
|
-
return this.getItem(path).onSuccess((item) => {
|
|
361
|
-
if (item.type !== 'file') {
|
|
362
|
-
return fail(`Path is not a file: ${path}`);
|
|
363
|
-
}
|
|
364
|
-
return item.getRawContents();
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* Gets the children of a directory in the file tree.
|
|
370
|
-
*/
|
|
371
|
-
public getChildren(path: string): Result<ReadonlyArray<FileTree.FileTreeItem>> {
|
|
372
|
-
const absolutePath = this.resolveAbsolutePath(path);
|
|
373
|
-
const children: FileTree.FileTreeItem[] = [];
|
|
374
|
-
|
|
375
|
-
// Find all items that are direct children of this directory
|
|
376
|
-
for (const [itemPath, item] of this._itemCache) {
|
|
377
|
-
if (this._isDirectChild(absolutePath, itemPath)) {
|
|
378
|
-
children.push(item);
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
return succeed(children);
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
* Checks if childPath is a direct child of parentPath.
|
|
387
|
-
*/
|
|
388
|
-
private _isDirectChild(parentPath: string, childPath: string): boolean {
|
|
389
|
-
// Normalize paths
|
|
390
|
-
const normalizedParent = parentPath.replace(/\/$/, '');
|
|
391
|
-
const normalizedChild = childPath.replace(/\/$/, '');
|
|
392
|
-
|
|
393
|
-
// Child must start with parent path
|
|
394
|
-
if (!normalizedChild.startsWith(normalizedParent + '/')) {
|
|
395
|
-
return false;
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
// Get the relative path from parent to child
|
|
399
|
-
const relativePath = normalizedChild.substring(normalizedParent.length + 1);
|
|
400
|
-
|
|
401
|
-
// Direct child means no additional slashes in the relative path
|
|
402
|
-
return !relativePath.includes('/');
|
|
403
|
-
}
|
|
404
|
-
}
|