-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmltest.js
executable file
·379 lines (356 loc) · 10.9 KB
/
xmltest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
const getStream = require("get-stream");
const path = require("path");
const { promisify } = require("util");
const yauzl = require("yauzl");
const { cache } = require("./cache");
// for type definitions
const { Entry } = require("yauzl");
/**
* @typedef Entries {Record<string, string | undefined>}
* @typedef PromiseResolve {function (response: Entries)}
* @typedef PromiseReject {function (reason: Error)}
* @typedef ReadFile {async function (response: Entry): Promise<Readable>}
* @typedef EntryHandler {async function (response: Entry, readFile: ReadFile): Promise<void>}
* @typedef LoaderInstance {{end: Function, entry: EntryHandler}}
* @typedef Loader {function (resolve: PromiseResolver, PromiseReject): LoaderInstance}
*/
const encodingMap = {
"xmltest/valid/sa/049.xml": "utf16le",
"xmltest/valid/sa/050.xml": "utf16le",
"xmltest/valid/sa/051.xml": "utf16le",
"xmltest/valid/sa/out/049.xml": "utf16le",
"xmltest/valid/sa/out/050.xml": "utf16le",
"xmltest/valid/sa/out/051.xml": "utf16le",
};
/**
* Loads all file content from the zip file.
*
* @param {PromiseResolve} resolve
* @param {PromiseReject} reject
* @param {BufferEncoding} [encoding]
* @returns {LoaderInstance}
*/
const contentLoader = (resolve, reject, encoding) => {
/** @type {Entries} */
const data = {};
const end = () => {
resolve(data);
};
const entry = async (entry, readFile) => {
if (!entry.fileName.endsWith("/")) {
const enc = encoding
? encoding
: encodingMap[entry.fileName]
? encodingMap[entry.fileName]
: "utf8";
data[entry.fileName] = await getStream(await readFile(entry), {
encoding: enc,
});
}
};
return { end, entry };
};
/**
* The module level cache for the zip file content.
*
* @type {null | Entries}
*/
contentLoader.CACHE = cache();
/**
* Loads the list of files and directories.
* The values contain the filename without path, or an empty string for directories.
*
* Calling `run` without arguments returns this data,
* so it can be dumped into `xmltest.json`
*
* @see run
*
* @param resolve {PromiseResolve}
* @param reject {PromiseReject}
* @returns {LoaderInstance}
*/
const entriesLoader = (resolve, reject) => {
/** @type {Entries} */
const data = {};
const end = () => {
resolve(data);
};
const entry = (entry) => {
data[entry.fileName] = entry.fileName.endsWith("/")
? ""
: path.basename(entry.fileName);
};
return { end, entry };
};
entriesLoader.CACHE = cache();
/**
* Uses `loader` to iterate entries in a zip file using `yauzl`.
* If `loader.CACHE` is set it is assumed to be an instance of `cache`,
* and is used to store the resolved result.
* If `loader.CACHE.has(location)` is true the zip file is not read again,
* since the cached result is returned.
* Use `loader.CACHE.delete(location)` or `loader.CACHE.clear()` when needed.
*
* @see contentLoader
* @see contentLoader.CACHE
* @see entriesLoader
* @see entriesLoader.CACHE
*
* @param loader {Loader} the loader to use (default: `contentLoader`)
* @param location {string} absolute path to zip file (default: xmltest.zip)
* @returns {Promise<Entries>}
*/
const load = async (
loader = contentLoader,
location = path.join(__dirname, "xmltest.zip"),
) => {
if (loader.CACHE && loader.CACHE.has(location)) {
return { ...loader.CACHE.get(location) };
}
const zipfile = await promisify(yauzl.open)(location, {
decodeStrings: true,
lazyEntries: true,
});
const readFile = promisify(zipfile.openReadStream.bind(zipfile));
return new Promise((resolve, reject) => {
const resolver = loader.CACHE
? (data) => {
loader.CACHE.set(location, data);
resolve(data);
}
: resolve;
const handler = loader(resolver, reject);
zipfile.on("end", handler.end);
zipfile.on("entry", async (entry) => {
await handler.entry(entry, readFile);
zipfile.readEntry();
});
zipfile.readEntry();
});
};
/**
* A function that can be passed to functions like `Array.prototype.filter`
*
* @typedef Predicate {function (string): boolean}
*/
/**
* Creates a predicate function ()
* from one or more of the following:
* - string (that the value needs to contain)
* - regular expression (that the value is `.test`ed with)
* - predicate function
*
* @param tests {string | RegExp | Predicate}
* @returns {Predicate}
*/
const combineFilters = (...tests) => {
const checks = tests.map((test) => {
if (typeof test === "function") {
return test;
}
let result;
if (typeof test.test === "function") {
result = (s) => test.test(s);
result.toString = () => `${test.toString}.test(str)`;
} else {
result = (s) => s.includes(test);
result.toString = () => `str.includes('${test}')`;
}
return result;
});
const result = (s) => checks.every((check) => check(s));
result.toString = () => `[combineFilters:(str) => ${checks.join(" && ")}]`;
return result;
};
/**
* Helpful filters based on the directory structure and content of `xmltest.zip`.
*
* Top level and nested UPPER_CASE keys represent the directory structure.
* Inner `files` filters only accept files from that directory (not from nested directories)
*
* Top level lower case predicates are for file extensions
* @see xml
* @see ent
*/
const FILTERS = {
INVALID: combineFilters("xmltest/invalid"),
NOT_WF: {
EXT_SA: {
files: combineFilters(/xmltest\/not-wf\/ext-sa\/[^/]+$/),
},
NOT_SA: {
files: combineFilters(/xmltest\/not-wf\/not-sa\/[^/]+$/),
},
SA: {
files: combineFilters(/xmltest\/not-wf\/sa\/[^/]+$/),
},
},
VALID: {
EXT_SA: {
files: combineFilters(/xmltest\/valid\/ext-sa\/[^/]+$/),
OUT: combineFilters("xmltest/valid/ext-sa/out"),
},
NOT_SA: {
files: combineFilters(/xmltest\/valid\/not-sa\/[^/]+$/),
OUT: combineFilters("xmltest/valid/not-sa/out"),
},
SA: {
files: combineFilters(/xmltest\/valid\/sa\/[^/]+$/),
OUT: combineFilters("xmltest/valid/sa/out"),
},
},
/**
* @param s {string}
* @returns {boolean}
*/
ent: (s) => s.endsWith(".ent"),
/**
* @param s {string}
* @returns {boolean}
*/
xml: (s) => s.endsWith(".xml"),
};
/**
* Converts path in zipfile (keys of entries or content)
* to resolve files that are related to a xml file.
*/
const RELATED = {
/**
* Returns the name of the `.ent` file with the same name as the given `.xml` file.
*
* @param pathInZip {string}
* @returns {string}
*/
ent: (pathInZip) => pathInZip.replace(/\.xml$/, ".ent"),
/**
* Returns the name of the related `./out/filename.xml` file with the same name as the given `.xml` file.
* Be aware that only the `valid` folders have such files.
*
* @param pathInZip {string}
* @returns {string}
*/
out: (pathInZip) =>
[path.dirname(pathInZip), "out", path.basename(pathInZip)].join("/"),
};
/**
* Filters `data` by applying `filters` to it's keys
*
* @see combineFilters
* @param data {Entries}
* @param filters {(string | RegExp | Predicate)[]}
* @returns {string | Entries} the value
* if the only filter only results a single entry,
* otherwise on object with all keys that match the filter.
*/
const getFiltered = (data, filters) => {
if (filters.length === 0) return { ...data };
const key = filters[0];
const isSingleExistingKey =
filters.length === 1 && typeof key === "string" && key in data;
const keys = isSingleExistingKey
? [key]
: Object.keys(data).filter(combineFilters.apply(null, filters));
return keys.length === 1 && filters.length === 1
? data[keys[0]]
: keys.reduce(
(acc, key) => {
acc[key] = data[key];
return acc;
},
/** @type {Entries} */ {},
);
};
/**
* Filters zip file content by applying `filters` to it's keys.
* It is async since the first invocation loads the content from the zipfile.
*
* @see getFiltered
* @see combineFilters
* @see load
*
* @param filters {string | RegExp | Predicate}
* @returns {Promise<string | Entries>} the value
* if the only filter only results a single entry,
* otherwise on object with all keys that match the filter.
*/
const getContent = async (...filters) => getFiltered(await load(), filters);
/**
* Filters content of `xmltest.json` by applying `filters` to it's keys.
*
* @see combineFilters
* @param filters {string | RegExp | Predicate}
* @returns {string | Entries} the value
* if the only filter only results a single entry,
* otherwise on object with all keys that match the filter.
*/
const getEntries = (...filters) =>
getFiltered(require("./xmltest.json"), filters);
/**
* Makes module executable using `runex`.
* If the first argument begins with `/`, `./` or `../` and ends with `.zip`,
* it is removed from the list of filter arguments and used as the path
* to the archive to load.
*
* With no filter arguments: Returns Object structure to store in `xmltest.json`
* `npx runex . > xmltest.json`
* With one filter argument: Returns content string if exact key match,
* or content dict with filtered keys
* With more filter arguments: Returns content dict with filtered keys
*
* @see getFiltered
* @see combineFilters
* @see load
* @see https://github.com/karfau/runex
*
* @param filters {string}
* @returns {Promise<string | Entries>}
*/
const run = async (...filters) => {
let file;
if (filters.length > 0 && /^\.?\.?\/.*\.zip$/.test(filters[0])) {
file = filters.shift();
}
return getFiltered(
await load(filters.length === 0 ? entriesLoader : contentLoader, file),
filters,
);
};
const replaceWithWrappedCodePointAt = (char) =>
`{!${char.codePointAt(0).toString(16)}!}`;
/**
* Some xml documents (purposely) contain characters that are not visible
* and make it hard to reason about a test result.
* Those characters also cause git to think a file is binary,
* when the test output is being committed (e.g. in a jest snapshot).
* By replacing them with a visual replacement using `codePointAt`
* (e.g. replacing NUL/`�` with `{!0!}`,
* it's makes those more obvious.
*
* @param value {string | {toString: function (): string} | undefined}
* @param wrapper {function (string): string}
*/
const replaceNonTextChars = (value, wrapper = replaceWithWrappedCodePointAt) =>
value === undefined || value === ""
? value
: value
.toString()
.replace(/[\u0000\u001B\u001F\uDC00\uD800\uFFFE\uFFFF]/gu, wrapper);
module.exports = {
combineFilters,
FILTERS,
RELATED,
getFiltered,
getContent,
getEntries,
load,
contentLoader,
entriesLoader,
replaceNonTextChars,
replaceWithWrappedCodePointAt,
run,
};
if (require.main === module) {
// if you don't want to use `runex` just "launch" this module/package:
// node xmltest ...
module.exports.run(...process.argv.slice(2)).then(console.log);
}