@fgv/ts-res 5.0.0-16 → 5.0.0-17
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-res.d.ts +461 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +3 -1
- package/lib/packlets/zip-archive/convert.d.ts +52 -0
- package/lib/packlets/zip-archive/convert.js +104 -0
- package/lib/packlets/zip-archive/index.d.ts +23 -0
- package/lib/packlets/zip-archive/index.js +95 -0
- package/lib/packlets/zip-archive/json.d.ts +64 -0
- package/lib/packlets/zip-archive/json.js +24 -0
- package/lib/packlets/zip-archive/types.d.ts +98 -0
- package/lib/packlets/zip-archive/types.js +39 -0
- package/lib/packlets/zip-archive/zipArchiveCreator.d.ts +35 -0
- package/lib/packlets/zip-archive/zipArchiveCreator.js +194 -0
- package/lib/packlets/zip-archive/zipArchiveFormat.d.ts +70 -0
- package/lib/packlets/zip-archive/zipArchiveFormat.js +184 -0
- package/lib/packlets/zip-archive/zipArchiveLoader.d.ts +70 -0
- package/lib/packlets/zip-archive/zipArchiveLoader.js +289 -0
- package/lib/test/unit/zip-archive/convert.test.d.ts +2 -0
- package/lib/test/unit/zip-archive/json.test.d.ts +2 -0
- package/lib/test/unit/zip-archive/zipArchiveCreator.test.d.ts +2 -0
- package/lib/test/unit/zip-archive/zipArchiveFormat.test.d.ts +2 -0
- package/lib/test/unit/zip-archive/zipArchiveIdempotency.test.d.ts +2 -0
- package/lib/test/unit/zip-archive/zipArchiveLoader.test.d.ts +2 -0
- package/package.json +8 -7
- package/src/index.ts +3 -1
- package/src/packlets/zip-archive/convert.ts +121 -0
- package/src/packlets/zip-archive/index.ts +76 -0
- package/src/packlets/zip-archive/json.ts +91 -0
- package/src/packlets/zip-archive/types.ts +140 -0
- package/src/packlets/zip-archive/zipArchiveCreator.ts +229 -0
- package/src/packlets/zip-archive/zipArchiveFormat.ts +158 -0
- package/src/packlets/zip-archive/zipArchiveLoader.ts +374 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.ZipArchiveCreator = void 0;
|
|
25
|
+
const fflate_1 = require("fflate");
|
|
26
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
27
|
+
const zipArchiveFormat_1 = require("./zipArchiveFormat");
|
|
28
|
+
/**
|
|
29
|
+
* ZIP archive creator using fflate for universal compatibility
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
class ZipArchiveCreator {
|
|
33
|
+
/**
|
|
34
|
+
* Create a ZIP archive buffer from a supplied buffer
|
|
35
|
+
* @param options - Input paths and configuration
|
|
36
|
+
* @param onProgress - Optional progress callback
|
|
37
|
+
* @returns Result containing ZIP buffer and manifest
|
|
38
|
+
*/
|
|
39
|
+
async createFromBuffer(options, onProgress) {
|
|
40
|
+
try {
|
|
41
|
+
onProgress === null || onProgress === void 0 ? void 0 : onProgress('creating-zip', 0, 'Starting ZIP archive creation');
|
|
42
|
+
const files = {};
|
|
43
|
+
const manifest = {
|
|
44
|
+
timestamp: new Date().toISOString()
|
|
45
|
+
};
|
|
46
|
+
const { value: inputItem, message: inputItemError } = this._getInputFileTreeItem(options);
|
|
47
|
+
if (inputItemError !== undefined) {
|
|
48
|
+
return (0, ts_utils_1.fail)(inputItemError);
|
|
49
|
+
}
|
|
50
|
+
if (inputItem !== undefined) {
|
|
51
|
+
onProgress === null || onProgress === void 0 ? void 0 : onProgress('reading-file', 10, `Processing input: ${inputItem.absolutePath}`);
|
|
52
|
+
if (inputItem.type === 'directory') {
|
|
53
|
+
// Add entire directory recursively, preserving structure
|
|
54
|
+
const archivePath = `input/${inputItem.name}`;
|
|
55
|
+
const addDirResult = await this._addDirectoryTreeToZip(files, inputItem, archivePath, onProgress);
|
|
56
|
+
/* c8 ignore next 3 - defense in depth against internal error */
|
|
57
|
+
if (addDirResult.isFailure()) {
|
|
58
|
+
return (0, ts_utils_1.fail)(addDirResult.message);
|
|
59
|
+
}
|
|
60
|
+
manifest.input = {
|
|
61
|
+
type: 'directory',
|
|
62
|
+
originalPath: inputItem.absolutePath,
|
|
63
|
+
archivePath
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
else if (inputItem.type === 'file') {
|
|
67
|
+
// Add single file
|
|
68
|
+
const archivePath = `input/${inputItem.name}`;
|
|
69
|
+
const addFileResult = await this._addFileTreeItemToZip(files, inputItem, archivePath);
|
|
70
|
+
/* c8 ignore next 3 - defense in depth against internal error */
|
|
71
|
+
if (addFileResult.isFailure()) {
|
|
72
|
+
return (0, ts_utils_1.fail)(addFileResult.message);
|
|
73
|
+
}
|
|
74
|
+
manifest.input = {
|
|
75
|
+
type: 'file',
|
|
76
|
+
originalPath: inputItem.absolutePath,
|
|
77
|
+
archivePath
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const { value: configItem, message: configItemError } = this._getConfigFileTreeItem(options);
|
|
82
|
+
if (configItemError !== undefined) {
|
|
83
|
+
return (0, ts_utils_1.fail)(configItemError);
|
|
84
|
+
}
|
|
85
|
+
if (configItem !== undefined) {
|
|
86
|
+
onProgress === null || onProgress === void 0 ? void 0 : onProgress('reading-file', 40, `Processing config: ${configItem.absolutePath}`);
|
|
87
|
+
const archivePath = `config/${configItem.name}`;
|
|
88
|
+
const addConfigResult = await this._addFileTreeItemToZip(files, configItem, archivePath);
|
|
89
|
+
/* c8 ignore next 3 - defense in depth against internal error */
|
|
90
|
+
if (addConfigResult.isFailure()) {
|
|
91
|
+
return (0, ts_utils_1.fail)(addConfigResult.message);
|
|
92
|
+
}
|
|
93
|
+
// Update manifest with config info
|
|
94
|
+
manifest.config = {
|
|
95
|
+
type: 'file',
|
|
96
|
+
originalPath: configItem.absolutePath,
|
|
97
|
+
archivePath
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
// Add manifest to ZIP
|
|
101
|
+
onProgress === null || onProgress === void 0 ? void 0 : onProgress('creating-zip', 70, 'Adding manifest');
|
|
102
|
+
const manifestJson = JSON.stringify(manifest, null, 2);
|
|
103
|
+
files['manifest.json'] = new TextEncoder().encode(manifestJson);
|
|
104
|
+
// Create ZIP buffer using fflate
|
|
105
|
+
onProgress === null || onProgress === void 0 ? void 0 : onProgress('creating-zip', 80, 'Compressing files');
|
|
106
|
+
const zipBuffer = (0, fflate_1.zipSync)(files, { level: 6 });
|
|
107
|
+
const result = {
|
|
108
|
+
zipBuffer,
|
|
109
|
+
manifest,
|
|
110
|
+
size: zipBuffer.length
|
|
111
|
+
};
|
|
112
|
+
onProgress === null || onProgress === void 0 ? void 0 : onProgress('creating-zip', 100, 'ZIP archive buffer created');
|
|
113
|
+
return (0, ts_utils_1.succeed)(result);
|
|
114
|
+
/* c8 ignore next 3 - defense in depth against internal error */
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
return (0, ts_utils_1.fail)(`Failed to create ZIP archive: ${error instanceof Error ? error.message : String(error)}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Add a single FileTree item to the ZIP archive
|
|
122
|
+
* @param files - ZIP files collection
|
|
123
|
+
* @param fileItem - FileTree file item to add
|
|
124
|
+
* @param archivePath - Path within the archive
|
|
125
|
+
* @returns Result indicating success or failure
|
|
126
|
+
*/
|
|
127
|
+
async _addFileTreeItemToZip(files, fileItem, archivePath) {
|
|
128
|
+
return fileItem.getRawContents().onSuccess((content) => {
|
|
129
|
+
const contentBuffer = new TextEncoder().encode(content);
|
|
130
|
+
files[(0, zipArchiveFormat_1.normalizePath)(archivePath)] = contentBuffer;
|
|
131
|
+
return (0, ts_utils_1.succeed)(undefined);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Add a directory recursively to the ZIP archive using FileTree
|
|
136
|
+
* @param files - ZIP files collection
|
|
137
|
+
* @param directoryItem - FileTree directory item to add
|
|
138
|
+
* @param archivePrefix - Prefix path within the archive
|
|
139
|
+
* @param onProgress - Optional progress callback
|
|
140
|
+
* @returns Result indicating success or failure
|
|
141
|
+
*/
|
|
142
|
+
async _addDirectoryTreeToZip(files, directoryItem, archivePrefix, onProgress) {
|
|
143
|
+
const childrenResult = directoryItem.getChildren();
|
|
144
|
+
/* c8 ignore next 3 - defense in depth against internal error */
|
|
145
|
+
if (childrenResult.isFailure()) {
|
|
146
|
+
return (0, ts_utils_1.fail)(`Failed to read directory ${directoryItem.absolutePath}: ${childrenResult.message}`);
|
|
147
|
+
}
|
|
148
|
+
for (const child of childrenResult.value) {
|
|
149
|
+
const itemArchivePath = (0, zipArchiveFormat_1.normalizePath)(`${archivePrefix}/${child.name}`);
|
|
150
|
+
if (child.type === 'directory') {
|
|
151
|
+
// Recursively add subdirectory
|
|
152
|
+
const addDirResult = await this._addDirectoryTreeToZip(files, child, itemArchivePath, onProgress);
|
|
153
|
+
/* c8 ignore next 3 - defense in depth against internal error */
|
|
154
|
+
if (addDirResult.isFailure()) {
|
|
155
|
+
return (0, ts_utils_1.fail)(addDirResult.message);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else if (child.type === 'file') {
|
|
159
|
+
// Add file
|
|
160
|
+
const addFileResult = await this._addFileTreeItemToZip(files, child, itemArchivePath);
|
|
161
|
+
/* c8 ignore next 3 - defense in depth against internal error */
|
|
162
|
+
if (addFileResult.isFailure()) {
|
|
163
|
+
return (0, ts_utils_1.fail)(addFileResult.message);
|
|
164
|
+
}
|
|
165
|
+
onProgress === null || onProgress === void 0 ? void 0 : onProgress('reading-file', 0, `Added file: ${itemArchivePath}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return (0, ts_utils_1.succeed)(undefined);
|
|
169
|
+
}
|
|
170
|
+
_getInputFileTreeItem(options) {
|
|
171
|
+
if ('inputPath' in options && options.inputPath !== undefined) {
|
|
172
|
+
return ts_utils_1.FileTree.forFilesystem()
|
|
173
|
+
.withErrorFormat((msg) => `Failed to create file tree: ${msg}`)
|
|
174
|
+
.onSuccess((fileTree) => fileTree.getItem(options.inputPath).withErrorFormat((msg) => `Failed to get item: ${msg}`));
|
|
175
|
+
}
|
|
176
|
+
else if ('inputItem' in options) {
|
|
177
|
+
return (0, ts_utils_1.succeed)(options.inputItem);
|
|
178
|
+
}
|
|
179
|
+
return (0, ts_utils_1.succeed)(undefined);
|
|
180
|
+
}
|
|
181
|
+
_getConfigFileTreeItem(options) {
|
|
182
|
+
if ('configPath' in options && options.configPath !== undefined) {
|
|
183
|
+
return ts_utils_1.FileTree.forFilesystem()
|
|
184
|
+
.withErrorFormat((msg) => `Failed to create file tree: ${msg}`)
|
|
185
|
+
.onSuccess((fileTree) => fileTree.getFile(options.configPath).withErrorFormat((msg) => `Failed to get config file: ${msg}`));
|
|
186
|
+
}
|
|
187
|
+
else if ('configItem' in options && options.configItem !== undefined) {
|
|
188
|
+
return (0, ts_utils_1.succeed)(options.configItem);
|
|
189
|
+
}
|
|
190
|
+
return (0, ts_utils_1.succeed)(undefined);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.ZipArchiveCreator = ZipArchiveCreator;
|
|
194
|
+
//# sourceMappingURL=zipArchiveCreator.js.map
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Result } from '@fgv/ts-utils';
|
|
2
|
+
import { Model as ConfigModel } from '../config';
|
|
3
|
+
import * as Json from './json';
|
|
4
|
+
/**
|
|
5
|
+
* Create a ZIP archive manifest object
|
|
6
|
+
* @param inputType - Type of input (file or directory)
|
|
7
|
+
* @param originalPath - Original file/directory path
|
|
8
|
+
* @param archivePath - Path within the archive
|
|
9
|
+
* @param configPath - Optional configuration file path
|
|
10
|
+
* @returns ZIP archive manifest
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export declare function createZipArchiveManifest(inputType: 'file' | 'directory', originalPath: string, archivePath: string, configPath?: string): Json.IZipArchiveManifest;
|
|
14
|
+
/**
|
|
15
|
+
* Parse and validate a ZIP archive manifest
|
|
16
|
+
* @param manifestData - JSON string containing manifest data
|
|
17
|
+
* @returns Result containing validated manifest
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseZipArchiveManifest(manifestData: string): Result<Json.IZipArchiveManifest>;
|
|
21
|
+
/**
|
|
22
|
+
* Validate a ZIP archive manifest object
|
|
23
|
+
* @param manifest - Object to validate as manifest
|
|
24
|
+
* @returns Result containing validated manifest
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export declare function validateZipArchiveManifest(manifest: unknown): Result<Json.IZipArchiveManifest>;
|
|
28
|
+
/**
|
|
29
|
+
* Parse and validate configuration JSON
|
|
30
|
+
* @param configData - JSON string containing configuration
|
|
31
|
+
* @returns Result containing validated configuration
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
export declare function parseZipArchiveConfiguration(configData: string): Result<ConfigModel.ISystemConfiguration>;
|
|
35
|
+
/**
|
|
36
|
+
* Generate a timestamp-based filename for ZIP archives
|
|
37
|
+
* @param customName - Optional custom name prefix
|
|
38
|
+
* @returns Generated filename
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export declare function generateZipArchiveFilename(customName?: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Normalize path separators for cross-platform compatibility
|
|
44
|
+
* @param path - Path to normalize
|
|
45
|
+
* @returns Normalized path
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export declare function normalizePath(path: string): string;
|
|
49
|
+
/**
|
|
50
|
+
* Extract directory name from a file path
|
|
51
|
+
* @param path - File path
|
|
52
|
+
* @returns Directory name
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export declare function getDirectoryName(path: string): string;
|
|
56
|
+
/**
|
|
57
|
+
* Create a safe filename by removing invalid characters
|
|
58
|
+
* @param filename - Original filename
|
|
59
|
+
* @returns Sanitized filename
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export declare function sanitizeFilename(filename: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* Validate ZIP file extension
|
|
65
|
+
* @param filename - Filename to validate
|
|
66
|
+
* @returns True if filename has .zip extension
|
|
67
|
+
* @public
|
|
68
|
+
*/
|
|
69
|
+
export declare function isZipFile(filename: string): boolean;
|
|
70
|
+
//# sourceMappingURL=zipArchiveFormat.d.ts.map
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
var __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.createZipArchiveManifest = createZipArchiveManifest;
|
|
58
|
+
exports.parseZipArchiveManifest = parseZipArchiveManifest;
|
|
59
|
+
exports.validateZipArchiveManifest = validateZipArchiveManifest;
|
|
60
|
+
exports.parseZipArchiveConfiguration = parseZipArchiveConfiguration;
|
|
61
|
+
exports.generateZipArchiveFilename = generateZipArchiveFilename;
|
|
62
|
+
exports.normalizePath = normalizePath;
|
|
63
|
+
exports.getDirectoryName = getDirectoryName;
|
|
64
|
+
exports.sanitizeFilename = sanitizeFilename;
|
|
65
|
+
exports.isZipFile = isZipFile;
|
|
66
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
67
|
+
const Convert = __importStar(require("./convert"));
|
|
68
|
+
/**
|
|
69
|
+
* Create a ZIP archive manifest object
|
|
70
|
+
* @param inputType - Type of input (file or directory)
|
|
71
|
+
* @param originalPath - Original file/directory path
|
|
72
|
+
* @param archivePath - Path within the archive
|
|
73
|
+
* @param configPath - Optional configuration file path
|
|
74
|
+
* @returns ZIP archive manifest
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
77
|
+
function createZipArchiveManifest(inputType, originalPath, archivePath, configPath) {
|
|
78
|
+
const manifest = {
|
|
79
|
+
timestamp: new Date().toISOString(),
|
|
80
|
+
input: {
|
|
81
|
+
type: inputType,
|
|
82
|
+
originalPath,
|
|
83
|
+
archivePath
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
if (configPath) {
|
|
87
|
+
manifest.config = {
|
|
88
|
+
type: 'file',
|
|
89
|
+
originalPath: configPath,
|
|
90
|
+
archivePath: 'config.json'
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return manifest;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Parse and validate a ZIP archive manifest
|
|
97
|
+
* @param manifestData - JSON string containing manifest data
|
|
98
|
+
* @returns Result containing validated manifest
|
|
99
|
+
* @public
|
|
100
|
+
*/
|
|
101
|
+
function parseZipArchiveManifest(manifestData) {
|
|
102
|
+
return (0, ts_utils_1.captureResult)(() => {
|
|
103
|
+
const parsed = JSON.parse(manifestData);
|
|
104
|
+
return parsed;
|
|
105
|
+
})
|
|
106
|
+
.onSuccess((parsed) => Convert.zipArchiveManifest.validate(parsed))
|
|
107
|
+
.withErrorFormat((e) => `Failed to parse ZIP archive manifest: ${e}`);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Validate a ZIP archive manifest object
|
|
111
|
+
* @param manifest - Object to validate as manifest
|
|
112
|
+
* @returns Result containing validated manifest
|
|
113
|
+
* @public
|
|
114
|
+
*/
|
|
115
|
+
function validateZipArchiveManifest(manifest) {
|
|
116
|
+
return Convert.zipArchiveManifest.validate(manifest);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Parse and validate configuration JSON
|
|
120
|
+
* @param configData - JSON string containing configuration
|
|
121
|
+
* @returns Result containing validated configuration
|
|
122
|
+
* @public
|
|
123
|
+
*/
|
|
124
|
+
function parseZipArchiveConfiguration(configData) {
|
|
125
|
+
return (0, ts_utils_1.captureResult)(() => {
|
|
126
|
+
const parsed = JSON.parse(configData);
|
|
127
|
+
return parsed;
|
|
128
|
+
})
|
|
129
|
+
.onSuccess((parsed) => Convert.systemConfiguration.validate(parsed))
|
|
130
|
+
.withErrorFormat((e) => `Failed to parse ZIP archive configuration: ${e}`);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Generate a timestamp-based filename for ZIP archives
|
|
134
|
+
* @param customName - Optional custom name prefix
|
|
135
|
+
* @returns Generated filename
|
|
136
|
+
* @public
|
|
137
|
+
*/
|
|
138
|
+
function generateZipArchiveFilename(customName) {
|
|
139
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').replace('T', '_').slice(0, -5);
|
|
140
|
+
return customName ? `${customName}-${timestamp}.zip` : `ts-res-archive-${timestamp}.zip`;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Normalize path separators for cross-platform compatibility
|
|
144
|
+
* @param path - Path to normalize
|
|
145
|
+
* @returns Normalized path
|
|
146
|
+
* @public
|
|
147
|
+
*/
|
|
148
|
+
function normalizePath(path) {
|
|
149
|
+
return path.replace(/\\/g, '/').replace(/\/+/g, '/');
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Extract directory name from a file path
|
|
153
|
+
* @param path - File path
|
|
154
|
+
* @returns Directory name
|
|
155
|
+
* @public
|
|
156
|
+
*/
|
|
157
|
+
function getDirectoryName(path) {
|
|
158
|
+
const normalized = normalizePath(path);
|
|
159
|
+
const parts = normalized.split('/');
|
|
160
|
+
return parts[parts.length - 1] || 'archive';
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Create a safe filename by removing invalid characters
|
|
164
|
+
* @param filename - Original filename
|
|
165
|
+
* @returns Sanitized filename
|
|
166
|
+
* @public
|
|
167
|
+
*/
|
|
168
|
+
function sanitizeFilename(filename) {
|
|
169
|
+
return filename
|
|
170
|
+
.replace(/[<>:"/\\|?*]/g, '_')
|
|
171
|
+
.replace(/\s+/g, '_')
|
|
172
|
+
.replace(/_+/g, '_')
|
|
173
|
+
.trim();
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Validate ZIP file extension
|
|
177
|
+
* @param filename - Filename to validate
|
|
178
|
+
* @returns True if filename has .zip extension
|
|
179
|
+
* @public
|
|
180
|
+
*/
|
|
181
|
+
function isZipFile(filename) {
|
|
182
|
+
return filename.toLowerCase().endsWith('.zip');
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=zipArchiveFormat.js.map
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Result } from '@fgv/ts-utils';
|
|
2
|
+
import type { IZipArchiveLoadOptions, IZipArchiveLoadResult, ZipArchiveProgressCallback } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* ZIP archive loader extending ts-extras foundation
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export declare class ZipArchiveLoader {
|
|
8
|
+
/**
|
|
9
|
+
* Load ZIP archive from File object (Browser)
|
|
10
|
+
* @param file - File object from file input
|
|
11
|
+
* @param options - Loading options
|
|
12
|
+
* @param onProgress - Optional progress callback
|
|
13
|
+
* @returns Result containing loaded archive data
|
|
14
|
+
*/
|
|
15
|
+
loadFromFile(file: File, options?: IZipArchiveLoadOptions, onProgress?: ZipArchiveProgressCallback): Promise<Result<IZipArchiveLoadResult>>;
|
|
16
|
+
/**
|
|
17
|
+
* Load ZIP archive from ArrayBuffer (Universal)
|
|
18
|
+
* @param buffer - ZIP data buffer
|
|
19
|
+
* @param options - Loading options
|
|
20
|
+
* @param onProgress - Optional progress callback
|
|
21
|
+
* @returns Result containing loaded archive data
|
|
22
|
+
*/
|
|
23
|
+
loadFromBuffer(buffer: ArrayBuffer, options?: IZipArchiveLoadOptions, onProgress?: ZipArchiveProgressCallback): Promise<Result<IZipArchiveLoadResult>>;
|
|
24
|
+
/**
|
|
25
|
+
* Load manifest from ZIP using ZipFileTreeAccessors
|
|
26
|
+
* @param zipAccessors - ZIP file tree accessors
|
|
27
|
+
* @returns Result containing parsed manifest
|
|
28
|
+
*/
|
|
29
|
+
private _loadManifestFromAccessors;
|
|
30
|
+
/**
|
|
31
|
+
* Load configuration from ZIP using ZipFileTreeAccessors
|
|
32
|
+
* @param zipAccessors - ZIP file tree accessors
|
|
33
|
+
* @param manifest - Parsed manifest (may be undefined)
|
|
34
|
+
* @param options - Loading options
|
|
35
|
+
* @returns Result containing parsed configuration
|
|
36
|
+
*/
|
|
37
|
+
private _loadConfigurationFromAccessors;
|
|
38
|
+
/**
|
|
39
|
+
* Extract files and directory structure from ZIP
|
|
40
|
+
* @param zipAccessors - ZIP file tree accessors
|
|
41
|
+
* @param onProgress - Optional progress callback
|
|
42
|
+
* @returns Result containing files and directory structure
|
|
43
|
+
*/
|
|
44
|
+
private _extractFilesFromAccessors;
|
|
45
|
+
/**
|
|
46
|
+
* Recursively process file tree items
|
|
47
|
+
* @param zipAccessors - ZIP file tree accessors
|
|
48
|
+
* @param currentPath - Current directory path
|
|
49
|
+
* @param items - Items to process
|
|
50
|
+
* @param files - Files collection
|
|
51
|
+
* @param directories - Directories collection
|
|
52
|
+
* @param onProgress - Optional progress callback
|
|
53
|
+
* @param processed - Processing counter
|
|
54
|
+
*/
|
|
55
|
+
private _processFileTreeItems;
|
|
56
|
+
/**
|
|
57
|
+
* Build directory structure from files
|
|
58
|
+
* @param files - Extracted files
|
|
59
|
+
* @param directories - Directories map
|
|
60
|
+
* @returns Root directory structure or null
|
|
61
|
+
*/
|
|
62
|
+
private _buildDirectoryStructure;
|
|
63
|
+
/**
|
|
64
|
+
* Get file MIME type based on extension
|
|
65
|
+
* @param filename - Filename
|
|
66
|
+
* @returns MIME type
|
|
67
|
+
*/
|
|
68
|
+
private _getFileType;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=zipArchiveLoader.d.ts.map
|