-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathindex.js
77 lines (71 loc) · 1.86 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
import crypto from "node:crypto";
import { readFileSync, WriteStream } from "node:fs";
import { inherits } from "node:util";
import { Stream, Readable, Writable } from "node:stream";
export function adjustDateByOffset(d, offset) {
d = d instanceof Date ? d : new Date();
if (offset >= 1) {
d.setMinutes(d.getMinutes() - offset);
} else {
d.setMinutes(d.getMinutes() + Math.abs(offset));
}
return d;
}
export function binaryBuffer(n) {
var buffer = Buffer.alloc(n);
for (var i = 0; i < n; i++) {
buffer.writeUInt8(i & 255, i);
}
return buffer;
}
function BinaryStream(size, options) {
Readable.call(this, options);
var buf = Buffer.alloc(size);
for (var i = 0; i < size; i++) {
buf.writeUInt8(i & 255, i);
}
this.push(buf);
this.push(null);
}
inherits(BinaryStream, Readable);
BinaryStream.prototype._read = function (size) {};
function DeadEndStream(options) {
Writable.call(this, options);
}
inherits(DeadEndStream, Writable);
DeadEndStream.prototype._write = function (chuck, encoding, callback) {
callback();
};
export function readJSON(filepath) {
var contents;
try {
contents = readFileSync(String(filepath));
contents = JSON.parse(contents);
} catch (e) {
contents = null;
}
return contents;
}
function UnBufferedStream() {
this.readable = true;
}
inherits(UnBufferedStream, Stream);
function WriteHashStream(path, options) {
WriteStream.call(this, path, options);
this.hash = crypto.createHash("sha1");
this.digest = null;
this.on("close", function () {
this.digest = this.hash.digest("hex");
});
}
inherits(WriteHashStream, WriteStream);
WriteHashStream.prototype.write = function (chunk) {
if (chunk) {
this.hash.update(chunk);
}
return WriteStream.prototype.write.call(this, chunk);
};
export { BinaryStream };
export { DeadEndStream };
export { UnBufferedStream };
export { WriteHashStream };