@fgv/ts-extras 5.0.0-2 → 5.0.0-21
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/.vscode/settings.json +27 -0
- package/config/api-extractor.json +343 -0
- package/config/rig.json +16 -0
- package/dist/ts-extras.d.ts +11 -4
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/zip-file-tree/zipFileTreeAccessors.d.ts +11 -4
- package/lib/packlets/zip-file-tree/zipFileTreeAccessors.js +57 -35
- package/lib/test/unit/converters.test.d.ts +2 -0
- package/lib/test/unit/csvHelpers.test.d.ts +2 -0
- package/lib/test/unit/extendedArray.test.d.ts +2 -0
- package/lib/test/unit/formatter.test.d.ts +2 -0
- package/lib/test/unit/md5Normalizer.test.d.ts +2 -0
- package/lib/test/unit/rangeOf.test.d.ts +2 -0
- package/lib/test/unit/recordJarHelpers.test.d.ts +2 -0
- package/lib/test/unit/zipFileTreeAccessors.test.d.ts +2 -0
- package/package.json +14 -15
- package/src/index.ts +31 -0
- package/src/packlets/conversion/converters.ts +124 -0
- package/src/packlets/conversion/index.ts +25 -0
- package/src/packlets/csv/csvHelpers.ts +58 -0
- package/src/packlets/csv/index.ts +23 -0
- package/src/packlets/experimental/extendedArray.ts +110 -0
- package/src/packlets/experimental/formatter.ts +114 -0
- package/src/packlets/experimental/index.ts +25 -0
- package/src/packlets/experimental/rangeOf.ts +222 -0
- package/src/packlets/hash/index.ts +23 -0
- package/src/packlets/hash/md5Normalizer.ts +38 -0
- package/src/packlets/record-jar/index.ts +23 -0
- package/src/packlets/record-jar/recordJarHelpers.ts +278 -0
- package/src/packlets/zip-file-tree/index.ts +33 -0
- package/src/packlets/zip-file-tree/zipFileTreeAccessors.ts +404 -0
- package/CHANGELOG.md +0 -98
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/packlets/conversion/converters.d.ts.map +0 -1
- package/lib/packlets/conversion/converters.js.map +0 -1
- package/lib/packlets/conversion/index.d.ts.map +0 -1
- package/lib/packlets/conversion/index.js.map +0 -1
- package/lib/packlets/csv/csvHelpers.d.ts.map +0 -1
- package/lib/packlets/csv/csvHelpers.js.map +0 -1
- package/lib/packlets/csv/index.d.ts.map +0 -1
- package/lib/packlets/csv/index.js.map +0 -1
- package/lib/packlets/experimental/extendedArray.d.ts.map +0 -1
- package/lib/packlets/experimental/extendedArray.js.map +0 -1
- package/lib/packlets/experimental/formatter.d.ts.map +0 -1
- package/lib/packlets/experimental/formatter.js.map +0 -1
- package/lib/packlets/experimental/index.d.ts.map +0 -1
- package/lib/packlets/experimental/index.js.map +0 -1
- package/lib/packlets/experimental/rangeOf.d.ts.map +0 -1
- package/lib/packlets/experimental/rangeOf.js.map +0 -1
- package/lib/packlets/hash/index.d.ts.map +0 -1
- package/lib/packlets/hash/index.js.map +0 -1
- package/lib/packlets/hash/md5Normalizer.d.ts.map +0 -1
- package/lib/packlets/hash/md5Normalizer.js.map +0 -1
- package/lib/packlets/record-jar/index.d.ts.map +0 -1
- package/lib/packlets/record-jar/index.js.map +0 -1
- package/lib/packlets/record-jar/recordJarHelpers.d.ts.map +0 -1
- package/lib/packlets/record-jar/recordJarHelpers.js.map +0 -1
- package/lib/packlets/zip-file-tree/index.d.ts.map +0 -1
- package/lib/packlets/zip-file-tree/index.js.map +0 -1
- package/lib/packlets/zip-file-tree/zipFileTreeAccessors.d.ts.map +0 -1
- package/lib/packlets/zip-file-tree/zipFileTreeAccessors.js.map +0 -1
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 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 { Result, captureResult } from '@fgv/ts-utils';
|
|
24
|
+
import Mustache from 'mustache';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Represents a generic range of some comparable type `<T>`.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
31
|
+
export interface RangeOfProperties<T> {
|
|
32
|
+
readonly min?: T;
|
|
33
|
+
readonly max?: T;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Format strings (in mustache format) to
|
|
38
|
+
* use for both open-ended and complete
|
|
39
|
+
* {@link Experimental.RangeOf | RangeOf<T>}.
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
43
|
+
export interface RangeOfFormats {
|
|
44
|
+
minOnly: string;
|
|
45
|
+
maxOnly: string;
|
|
46
|
+
minMax: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Default {@link Experimental.RangeOfFormats | formats} to use for both
|
|
51
|
+
* open-ended and complete {@link Experimental.RangeOf | RangeOf<T>}.
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
export const DEFAULT_RANGEOF_FORMATS: RangeOfFormats = {
|
|
55
|
+
minOnly: '{{min}}-',
|
|
56
|
+
maxOnly: '-{{max}}',
|
|
57
|
+
minMax: '{{min}}-{{max}}'
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Simple implementation of a possibly open-ended range of some comparable
|
|
62
|
+
* type `<T>` with test and formatting.
|
|
63
|
+
* @public
|
|
64
|
+
*/
|
|
65
|
+
export class RangeOf<T> implements RangeOfProperties<T> {
|
|
66
|
+
/**
|
|
67
|
+
* Minimum extent of the range.
|
|
68
|
+
*/
|
|
69
|
+
public readonly min?: T;
|
|
70
|
+
/**
|
|
71
|
+
* Maximum extent of the range.
|
|
72
|
+
*/
|
|
73
|
+
public readonly max?: T;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Creates a new {@link Experimental.RangeOf | RangeOf<T>}.
|
|
77
|
+
* @param min - Optional minimum extent of the range.
|
|
78
|
+
* @param max - Optional maximum extent of the range.
|
|
79
|
+
*/
|
|
80
|
+
public constructor(min?: T, max?: T) {
|
|
81
|
+
if (min !== undefined && max !== undefined && this._compare(min, max) === 'greater') {
|
|
82
|
+
throw new Error(`Inverted range - ${JSON.stringify(min)} must be <= ${JSON.stringify(max)}.`);
|
|
83
|
+
}
|
|
84
|
+
this.min = min;
|
|
85
|
+
this.max = max;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Static constructor for a {@link Experimental.RangeOf | RangeOf<T>}.
|
|
90
|
+
* @param init - {@link Experimental.RangeOfProperties | Range initializer}.
|
|
91
|
+
* @returns A new {@link Experimental.RangeOf | RangeOf<T>}.
|
|
92
|
+
*/
|
|
93
|
+
public static createRange<T>(init?: RangeOfProperties<T>): Result<RangeOf<T>> {
|
|
94
|
+
return captureResult(() => new RangeOf<T>(init?.min, init?.max));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Gets a formatted description of a {@link Experimental.RangeOfProperties | RangeOfProperties<T>} given an
|
|
99
|
+
* optional set of formats and 'empty' value to use.
|
|
100
|
+
* @param range - The {@link Experimental.RangeOfProperties | RangeOfProperties<T>} to be formatted.
|
|
101
|
+
* @param formats - Optional {@link Experimental.RangeOfFormats | formats} to use. Default is
|
|
102
|
+
* {@link Experimental.DEFAULT_RANGEOF_FORMATS | DEFAULT_RANGEOF_FORMATS}.
|
|
103
|
+
* @param emptyValue - Value which represents unbounded minimum or maximum for this range. Default is `undefined`.
|
|
104
|
+
* @returns A string representation of the range.
|
|
105
|
+
*/
|
|
106
|
+
public static propertiesToString<T>(
|
|
107
|
+
range: RangeOfProperties<T>,
|
|
108
|
+
formats?: RangeOfFormats,
|
|
109
|
+
emptyValue?: T
|
|
110
|
+
): string | undefined {
|
|
111
|
+
formats = formats ?? DEFAULT_RANGEOF_FORMATS;
|
|
112
|
+
if (range.min !== undefined && range.min !== emptyValue) {
|
|
113
|
+
if (range.max !== undefined && range.max !== emptyValue) {
|
|
114
|
+
return Mustache.render(formats.minMax, range);
|
|
115
|
+
} else {
|
|
116
|
+
return Mustache.render(formats.minOnly, range);
|
|
117
|
+
}
|
|
118
|
+
} else if (range.max !== undefined && range.max !== emptyValue) {
|
|
119
|
+
return Mustache.render(formats.maxOnly, range);
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Default comparison uses javascript built-in comparison.
|
|
126
|
+
* @param t1 - First value to be compared.
|
|
127
|
+
* @param t2 - Second value to be compared.
|
|
128
|
+
* @returns `'less'` if `t1` is less than `t2`, `'greater'` if `t1` is larger
|
|
129
|
+
* and `'equal'` if `t1` and `t2` are equal.
|
|
130
|
+
* @internal
|
|
131
|
+
*/
|
|
132
|
+
protected static _defaultCompare<T>(t1: T, t2: T): 'less' | 'equal' | 'greater' {
|
|
133
|
+
if (t1 < t2) {
|
|
134
|
+
return 'less';
|
|
135
|
+
} else if (t1 > t2) {
|
|
136
|
+
return 'greater';
|
|
137
|
+
}
|
|
138
|
+
return 'equal';
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Checks if a supplied value is within this range.
|
|
143
|
+
* @param t - The value to be tested.
|
|
144
|
+
* @returns `'included'` if `t` falls within the range, `'less'` if `t` falls
|
|
145
|
+
* below the minimum extent of the range and `'greater'` if `t` is above the
|
|
146
|
+
* maximum extent.
|
|
147
|
+
*/
|
|
148
|
+
public check(t: T): 'less' | 'included' | 'greater' {
|
|
149
|
+
if (this.min !== undefined && this._compare(t, this.min) === 'less') {
|
|
150
|
+
return 'less';
|
|
151
|
+
}
|
|
152
|
+
if (this.max !== undefined && this._compare(t, this.max) !== 'less') {
|
|
153
|
+
return 'greater';
|
|
154
|
+
}
|
|
155
|
+
return 'included';
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Determines if a supplied value is within this range.
|
|
160
|
+
* @param t - The value to be tested.
|
|
161
|
+
* @returns Returns `true` if `t` falls within the range, `false` otherwise.
|
|
162
|
+
*/
|
|
163
|
+
public includes(t: T): boolean {
|
|
164
|
+
return this.check(t) === 'included';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Finds the transition value that would bring a supplied value `t` into
|
|
169
|
+
* range.
|
|
170
|
+
* @param t - The value to be tested.
|
|
171
|
+
* @returns The minimum extent of the range if `t` is below the range or
|
|
172
|
+
* the maximum extent of the range if `t` is above the range. Returns
|
|
173
|
+
* `undefined` if `t` already falls within the range.
|
|
174
|
+
*/
|
|
175
|
+
public findTransition(t: T): T | undefined {
|
|
176
|
+
switch (this.check(t)) {
|
|
177
|
+
case 'less':
|
|
178
|
+
return this.min;
|
|
179
|
+
case 'included':
|
|
180
|
+
return this.max;
|
|
181
|
+
}
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Formats the minimum and maximum values of this range.
|
|
187
|
+
* @param format - A format function used to format the values.
|
|
188
|
+
* @returns A {@link Experimental.RangeOfProperties | RangeOfProperties<string>} containing the
|
|
189
|
+
* formatted representation of the {@link Experimental.RangeOf.min | minimum} and
|
|
190
|
+
* {@link Experimental.RangeOf.max | maximum}
|
|
191
|
+
* extent of the range, or `undefined` for an extent that is not present.
|
|
192
|
+
*/
|
|
193
|
+
public toFormattedProperties(format: (value: T) => string | undefined): RangeOfProperties<string> {
|
|
194
|
+
return {
|
|
195
|
+
min: this.min !== undefined ? format(this.min) : undefined,
|
|
196
|
+
max: this.max !== undefined ? format(this.max) : undefined
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Formats this range using the supplied format function.
|
|
202
|
+
* @param format - Format function used to format minimum and maximum extent values.
|
|
203
|
+
* @param formats - The {@link Experimental.RangeOfFormats | format strings} used to format the range
|
|
204
|
+
* (default {@link Experimental.DEFAULT_RANGEOF_FORMATS}).
|
|
205
|
+
* @returns Returns a formatted representation of this range.
|
|
206
|
+
*/
|
|
207
|
+
public format(format: (value: T) => string | undefined, formats?: RangeOfFormats): string | undefined {
|
|
208
|
+
return RangeOf.propertiesToString(this.toFormattedProperties(format), formats);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Inner compare method can be overridden by a derived class.
|
|
213
|
+
* @param t1 - First value to compare.
|
|
214
|
+
* @param t2 - Second value to compare.
|
|
215
|
+
* @returns `'less'` if `t1` is less than `t2`, `'greater'` if `t1` is larger
|
|
216
|
+
* and `'equal'` if `t1` and `t2` are equal.
|
|
217
|
+
* @internal
|
|
218
|
+
*/
|
|
219
|
+
protected _compare(t1: T, t2: T): 'less' | 'equal' | 'greater' {
|
|
220
|
+
return RangeOf._defaultCompare(t1, t2);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 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
|
+
export * from './md5Normalizer';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023 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 { Hash } from '@fgv/ts-utils';
|
|
24
|
+
import * as crypto from 'crypto';
|
|
25
|
+
/**
|
|
26
|
+
* A hashing normalizer which computes object
|
|
27
|
+
* hash using the MD5 algorithm.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export class Md5Normalizer extends Hash.HashingNormalizer {
|
|
31
|
+
public constructor() {
|
|
32
|
+
super(Md5Normalizer.md5Hash);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public static md5Hash(parts: string[]): string {
|
|
36
|
+
return crypto.createHash('md5').update(parts.join('|'), 'utf8').digest('hex');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023 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
|
+
export * from './recordJarHelpers';
|
|
@@ -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';
|