-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
64 lines (52 loc) · 1.43 KB
/
Copy pathutil.js
File metadata and controls
64 lines (52 loc) · 1.43 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
import {stat} from "node:fs/promises";
import {createReadStream} from "node:fs";
import {Readable} from "node:stream";
import {makeByteReadableStreamFromNodeReadable, makeDefaultReadableStreamFromNodeReadable} from "../lib/index.js";
export async function makeDefaultReadableStreamFromFile(filename) {
const fileInfo = await stat(filename);
const nodeStream = createReadStream(filename);
return {
fileSize: fileInfo.size,
stream: makeDefaultReadableStreamFromNodeReadable(nodeStream)
};
}
export async function makeByteReadableStreamFromFile(filename) {
const fileInfo = await stat(filename);
const nodeStream = createReadStream(filename);
return {
fileSize: fileInfo.size,
stream: makeByteReadableStreamFromNodeReadable(nodeStream)
};
}
/**
* A mock Node.js readable-stream, using string to read from
*/
export class SourceStream extends Readable {
delay = 0;
constructor(str = '', delay = 0) {
super();
if (delay !== undefined) {
this.delay = delay;
}
this.buf = new TextEncoder().encode(str);
}
_read() {
setTimeout(() => {
this.push(this.buf);
this.push(null); // Signal end of stream
}, this.delay);
}
}
/**
* Convert callback to Promise, on closing a node stream
*/
export function closeNodeStream(stream) {
return new Promise((resolve, reject) => {
stream.close(err => {
if(err)
reject(err);
else
resolve();
});
})
}