-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathindex.js
151 lines (118 loc) · 3.15 KB
/
index.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
import fs from 'node:fs';
import crypto from 'node:crypto';
import {isStream} from 'is-stream';
const {Worker} = await (async () => {
try {
return await import('node:worker_threads');
} catch {
return {};
}
})();
let worker; // Lazy
let taskIdCounter = 0;
const tasks = new Map();
const recreateWorkerError = sourceError => {
const error = new Error(sourceError.message);
for (const [key, value] of Object.entries(sourceError)) {
if (key !== 'message') {
error[key] = value;
}
}
return error;
};
const createWorker = () => {
worker = new Worker(new URL('thread.js', import.meta.url));
worker.on('message', message => {
const task = tasks.get(message.id);
tasks.delete(message.id);
if (tasks.size === 0) {
worker.unref();
}
if (message.error === undefined) {
task.resolve(message.value);
} else {
task.reject(recreateWorkerError(message.error));
}
});
worker.on('error', error => {
// Any error here is effectively an equivalent of segfault, and have no scope, so we just throw it on callback level
throw error;
});
};
const taskWorker = (method, arguments_, transferList) => new Promise((resolve, reject) => {
const id = taskIdCounter++;
tasks.set(id, {resolve, reject});
if (worker === undefined) {
createWorker();
}
worker.ref();
worker.postMessage({id, method, arguments_}, transferList);
});
export async function hash(input, options = {}) {
if (isStream(input)) {
return new Promise((resolve, reject) => {
// TODO: Use `stream.compose` and `.toArray()`.
input
.on('error', reject)
.pipe(hashingStream(options))
.on('error', reject)
.on('finish', function () {
resolve(this.read());
});
});
}
if (Worker === undefined) {
return hashSync(input, options);
}
let {
encoding = 'hex',
algorithm = 'sha512',
} = options;
if (encoding === 'buffer') {
encoding = undefined;
}
const hash = await taskWorker('hash', [algorithm, input]);
if (encoding === undefined) {
return Buffer.from(hash);
}
return Buffer.from(hash).toString(encoding);
}
export function hashSync(input, {encoding = 'hex', algorithm = 'sha512'} = {}) {
if (encoding === 'buffer') {
encoding = undefined;
}
const hash = crypto.createHash(algorithm);
const update = buffer => {
const inputEncoding = typeof buffer === 'string' ? 'utf8' : undefined;
hash.update(buffer, inputEncoding);
};
for (const element of [input].flat()) {
update(element);
}
return hash.digest(encoding);
}
export async function hashFile(filePath, options = {}) {
if (Worker === undefined) {
return hash(fs.createReadStream(filePath), options);
}
const {
encoding = 'hex',
algorithm = 'sha512',
} = options;
const hash = await taskWorker('hashFile', [algorithm, filePath]);
if (encoding === 'buffer') {
return Buffer.from(hash);
}
return Buffer.from(hash).toString(encoding);
}
export function hashFileSync(filePath, options) {
return hashSync(fs.readFileSync(filePath), options);
}
export function hashingStream({encoding = 'hex', algorithm = 'sha512'} = {}) {
if (encoding === 'buffer') {
encoding = undefined;
}
const stream = crypto.createHash(algorithm);
stream.setEncoding(encoding);
return stream;
}