-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathindex.mjs
More file actions
548 lines (498 loc) · 15.7 KB
/
index.mjs
File metadata and controls
548 lines (498 loc) · 15.7 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import * as crypto from 'node:crypto';
import * as fs from 'node:fs';
import * as path from 'node:path';
import debug from 'debug';
import * as minimatch from 'minimatch';
/**
* @import {Encoding, HashedElement, Options, RuleFn, RuleOption} from './types/public';
* @import {InnerOptions, MatchRules, ParsedArgs} from './types/internal';
*/
/**
* @type {Options}
*/
const defaultOptions = {
algo: 'sha1', // see crypto.getHashes() for options
algoOptions: undefined,
encoding: 'base64url', // 'base64', 'base64url', 'hex' or 'binary'
files: {
exclude: [],
include: [],
matchBasename: true,
matchPath: false,
ignoreBasename: false,
ignoreRootName: false,
},
folders: {
exclude: [],
include: [],
matchBasename: true,
matchPath: false,
ignoreBasename: false,
ignoreRootName: false,
},
symbolicLinks: {
include: true,
ignoreBasename: false,
ignoreTargetPath: true,
ignoreTargetContent: false,
ignoreTargetContentAfterError: false,
},
};
// Use the environment variable DEBUG to log output, e.g. `set DEBUG=fhash:*`
const log = {
match: debug('fhash:match'),
params: (/** @type {any} */ params) => {
debug('fhash:parameters')(params);
return params;
},
err: debug('fhash:err'),
symlink: debug('fhash:symlink'),
queue: debug('fhash:queue'),
glob: debug('fhash:glob'),
};
/**
* @param {typeof import("node:fs")} fs
*/
export function prep(fs) {
/**
* @type {(() => any)[]}
*/
let queue = [];
/**
* @type {NodeJS.Timeout | undefined}
*/
let queueTimer = undefined;
/**
* @param {string} name
* @param {string} dir
* @param {Options} options
* @param {(err?: Error, ok?: unknown) => void} callback
*/
function hashElement(name, dir, options, callback) {
callback = arguments[arguments.length - 1];
return parseParameters(arguments)
.then(({ basename, dir, options }) => {
// this is only used for the root level
options.skipMatching = true;
return fs.promises
.lstat(path.join(dir, basename))
.then(stats => hashElementPromise(basename, stats, dir, options, true));
})
.then(result => {
if (isFunction(callback)) {
return callback(undefined, result);
} else {
return result;
}
})
.catch(reason => {
log.err('Fatal error:', reason);
if (isFunction(callback)) {
return callback(reason);
} else {
throw reason;
}
});
}
/**
* @param {string} name
* @param {import('node:fs').Stats} stats
* @param {string} dirname
* @param {InnerOptions} options
* @param {boolean} isRootElement
* @returns {Promise<HashedElement>}
*/
function hashElementPromise(name, stats, dirname, options, isRootElement = false) {
let promise = undefined;
if (stats.isDirectory()) {
promise = hashFolderPromise(name, dirname, options, isRootElement);
} else if (stats.isFile()) {
promise = hashFilePromise(name, dirname, options, isRootElement);
} else if (stats.isSymbolicLink()) {
promise = hashSymLinkPromise(name, dirname, options, isRootElement);
} else {
log.err('hashElementPromise cannot handle ', stats);
return Promise.resolve({ name, hash: 'Error: unknown element type' });
}
return promise.catch((/** @type {{ code: string; }} */ err) => {
if (err.code && (err.code === 'EMFILE' || err.code === 'ENFILE')) {
log.queue(`queued ${dirname}/${name} because of ${err.code}`);
const promise = new Promise((resolve, reject) => {
queue.push(() => {
log.queue(`Will processs queued ${dirname}/${name}`);
return hashElementPromise(name, stats, dirname, options, isRootElement)
.then((/** @type {any} */ ok) => resolve(ok))
.catch((/** @type {any} */ err) => reject(err));
});
});
if (queueTimer === undefined) {
queueTimer = setTimeout(processQueue, 0);
}
return promise;
}
throw err;
});
}
function processQueue() {
queueTimer = undefined;
const runnables = queue;
queue = [];
runnables.forEach(run => run());
}
/**
* @param {string} name
* @param {string} dir
* @param {InnerOptions} options
* @returns {Promise<HashedFolder|undefined>}
*/
async function hashFolderPromise(name, dir, options, isRootElement = false) {
const folderPath = path.join(dir, name);
let ignoreBasenameOnce = options.ignoreBasenameOnce;
delete options.ignoreBasenameOnce;
if (options.skipMatching) {
// this is currently only used for the root folder
log.match(`skipped '${folderPath}'`);
delete options.skipMatching;
} else if (ignore(name, folderPath, options.folders)) {
return undefined;
}
const files = await fs.promises.readdir(folderPath, { withFileTypes: true });
const children = await Promise.all(
files
.sort((/** @type {{ name: string; }} */ a, /** @type {{ name: any; }} */ b) =>
a.name.localeCompare(b.name),
)
.map((/** @type {any} */ child) =>
hashElementPromise(child.name, child, folderPath, options),
),
);
if (ignoreBasenameOnce) options.ignoreBasenameOnce = true;
const hash = new HashedFolder(name, children.filter(notUndefined), options, isRootElement);
return hash;
}
/**
* @param {string} name
* @param {string} dir
* @param {InnerOptions} options
* @returns {Promise<HashedFile|undefined>}
*/
function hashFilePromise(name, dir, options, isRootElement = false) {
const filePath = path.join(dir, name);
if (options.skipMatching) {
// this is currently only used for the root folder
log.match(`skipped '${filePath}'`);
delete options.skipMatching;
} else if (ignore(name, filePath, options.files)) {
return Promise.resolve(undefined);
}
return new Promise((resolve, reject) => {
try {
const hash = crypto.createHash(options.algo, options.algoOptions);
if (
options.files.ignoreBasename ||
options.ignoreBasenameOnce ||
(isRootElement && options.files.ignoreRootName)
) {
delete options.ignoreBasenameOnce;
log.match(`omitted name of ${filePath} from hash`);
} else {
hash.update(name);
}
const f = fs.createReadStream(filePath);
f.on('error', (/** @type {any} */ err) => {
reject(err);
});
f.pipe(hash, { end: false });
f.on('end', () => {
const hashedFile = new HashedFile(name, hash, options.encoding);
return resolve(hashedFile);
});
} catch (ex) {
return reject(ex);
}
});
}
/**
* @param {string} name
* @param {string} dir
* @param {InnerOptions} options
* @param {boolean} isRootElement
*
* @returns {Promise<HashedFile|undefined>}
*/
async function hashSymLinkPromise(name, dir, options, isRootElement = false) {
const target = await fs.promises.readlink(path.join(dir, name));
log.symlink(`handling symbolic link ${name} -> ${target}`);
if (options.symbolicLinks.include) {
if (options.symbolicLinks.ignoreTargetContent) {
return symLinkIgnoreTargetContent(name, target, options, isRootElement);
} else {
return symLinkResolve(name, dir, target, options, isRootElement);
}
} else {
log.symlink('skipping symbolic link');
return Promise.resolve(undefined);
}
}
/**
* @param {string} name
* @param {crypto.BinaryLike} target
* @param {InnerOptions} options
* @param {boolean} isRootElement
*
* @returns {Promise<HashedFile>}
*/
function symLinkIgnoreTargetContent(name, target, options, isRootElement) {
delete options.skipMatching; // only used for the root level
log.symlink('ignoring symbolic link target content');
const hash = crypto.createHash(options.algo, options.algoOptions);
if (!options.symbolicLinks.ignoreBasename && !(isRootElement && options.files.ignoreRootName)) {
log.symlink('hash basename');
hash.update(name);
}
if (!options.symbolicLinks.ignoreTargetPath) {
log.symlink('hash targetpath');
hash.update(target);
}
return Promise.resolve(new HashedFile(name, hash, options.encoding));
}
/**
* @param {string} name
* @param {string} dir
* @param {crypto.BinaryLike} target
* @param {InnerOptions} options
* @param {boolean} isRootElement
*
* @returns {Promise<HashedFile>}
*/
async function symLinkResolve(name, dir, target, options, isRootElement) {
delete options.skipMatching; // only used for the root level
if (options.symbolicLinks.ignoreBasename) {
options.ignoreBasenameOnce = true;
}
try {
const stats = await fs.promises.stat(path.join(dir, name));
const temp = await hashElementPromise(name, stats, dir, options, isRootElement);
if (!options.symbolicLinks.ignoreTargetPath) {
const hash = crypto.createHash(options.algo, options.algoOptions);
hash.update(temp.hash);
log.symlink('hash targetpath');
hash.update(target);
temp.hash = hash.digest(options.encoding);
}
return temp;
} catch (err) {
if (options.symbolicLinks.ignoreTargetContentAfterError) {
log.symlink(`Ignoring error when hashing symbolic link ${name}`, err);
const hash = crypto.createHash(options.algo, options.algoOptions);
if (
!options.symbolicLinks.ignoreBasename &&
!(isRootElement && options.files.ignoreRootName)
) {
hash.update(name);
}
if (!options.symbolicLinks.ignoreTargetPath) {
hash.update(target);
}
return new HashedFile(name, hash, options.encoding);
} else {
log.symlink(`Fatal error when hashing symbolic link ${name}`, err);
throw err;
}
}
}
/**
* @param {string} name
* @param {string} path
* @param {MatchRules} rules
*/
function ignore(name, path, rules) {
if (rules.exclude) {
if (rules.matchBasename && rules.exclude(name)) {
log.match(`exclude basename '${name}'`);
return true;
} else if (rules.matchPath && rules.exclude(path)) {
log.match(`exclude path '${path}'`);
return true;
}
}
if (rules.include) {
if (rules.matchBasename && rules.include(name)) {
log.match(`include basename '${name}'`);
return false;
} else if (rules.matchPath && rules.include(path)) {
log.match(`include path '${path}'`);
return false;
} else {
log.match(`include rule failed for path '${path}'`);
return true;
}
}
log.match(`Will not ignore unmatched '${path}'`);
return false;
}
return hashElement;
}
/**
* @param {IArguments | Array<string|Options>} args
* @returns {Promise<ParsedArgs>}
*/
export function parseParameters(args) {
let basename = args[0],
dir = args[1],
options = args[2];
if (!isString(basename)) {
return Promise.reject(new TypeError('First argument must be a string'));
}
if (!isString(dir)) {
dir = path.dirname(basename);
basename = path.basename(basename);
options = args[1];
}
/** @type {Options} */
let combined;
if (options && typeof options === 'object') {
combined = {
algo: 'algo' in options ? options.algo : defaultOptions.algo,
algoOptions: 'algoOptions' in options ? options.algoOptions : defaultOptions.algoOptions,
encoding: 'encoding' in options ? options.encoding : defaultOptions.encoding,
// files: { ...structuredClone(defaultOptions.files), ...options.files },
files:
'files' in options
? { ...structuredClone(defaultOptions.files), ...options.files }
: structuredClone(defaultOptions.files),
folders:
'folders' in options
? { ...structuredClone(defaultOptions.folders), ...options.folders }
: structuredClone(defaultOptions.folders),
symbolicLinks: {
...structuredClone(defaultOptions.symbolicLinks),
...options.symbolicLinks,
},
};
} else {
combined = structuredClone(defaultOptions);
}
/** @type {InnerOptions} */
const inner = {
...combined,
files: {
...combined.files,
exclude: reduceGlobPatterns(combined.files.exclude, 'exclude files'),
include: reduceGlobPatterns(combined.files.include, 'include files'),
},
folders: {
...combined.folders,
exclude: reduceGlobPatterns(combined.folders.exclude, 'exclude folders'),
include: reduceGlobPatterns(combined.folders.include, 'include folders'),
},
skipMatching: false,
ignoreBasenameOnce: false,
};
return Promise.resolve(log.params({ basename, dir, options: inner }));
}
const HashedFolder = function HashedFolder(
/** @type {string} */ name,
/** @type {HashedElement[]} */ children,
/** @type {InnerOptions} */ options,
isRootElement = false,
) {
this.name = name;
this.children = children;
const hash = crypto.createHash(options.algo, options.algoOptions);
if (
options.folders.ignoreBasename ||
options.ignoreBasenameOnce ||
(isRootElement && options.folders.ignoreRootName)
) {
delete options.ignoreBasenameOnce;
log.match(`omitted name of folder ${name} from hash`);
} else {
hash.update(name);
}
children.forEach((/** @type {{ hash: crypto.BinaryLike; }} */ child) => {
if (child.hash) {
hash.update(child.hash);
}
});
this.hash = hash.digest(options.encoding);
};
HashedFolder.prototype.toString = function (padding = '') {
const first = `${padding}{ name: '${this.name}', hash: '${this.hash}',\n`;
padding += ' ';
return `${first}${padding}children: ${this.childrenToString(padding)}}`;
};
HashedFolder.prototype.childrenToString = function (padding = '') {
if (this.children.length === 0) {
return '[]';
} else {
const nextPadding = padding + ' ';
const children = this.children
.map((/** @type {{ toString: (arg0: string) => any; }} */ child) =>
child.toString(nextPadding),
)
.join('\n');
return `[\n${children}\n${padding}]`;
}
};
const HashedFile = function HashedFile(
/** @type {string} */ name,
/** @type {crypto.Hash} */ hash,
/** @type {Encoding} */ encoding,
) {
this.name = name;
this.hash = hash.digest(encoding);
};
HashedFile.prototype.toString = function (padding = '') {
return padding + "{ name: '" + this.name + "', hash: '" + this.hash + "' }";
};
/**
* @param {unknown} any
*/
function isFunction(any) {
return typeof any === 'function';
}
/**
* @param {unknown} str
*/
function isString(str) {
return typeof str === 'string';
}
/**
* @param {unknown} obj
*/
function notUndefined(obj) {
return typeof obj !== 'undefined';
}
/**
* @param {RuleOption} globs
* @param {string} name
* @returns {RuleFn|undefined}
*/
function reduceGlobPatterns(globs, name) {
if (isFunction(globs)) {
log.glob(`Using function to ${name}`);
return globs;
} else if (!globs || !Array.isArray(globs) || globs.length === 0) {
log.glob(`Invalid glob pattern to ${name}`, { globs, typeof: typeof globs });
return undefined;
} else {
// combine globs into one single RegEx
const regex = new RegExp(
globs
.reduce((acc, exclude) => {
const built = minimatch.makeRe(exclude);
if (!built) return acc;
else return acc + '|' + built.source;
}, '')
.substring(1),
);
log.glob(`Reduced glob patterns to ${name}`, { from: globs, to: regex });
return (/** @type {string} */ param) => regex.test(param);
}
}
export const hashElement = prep(fs);
export default {
defaults: defaultOptions,
hashElement,
};