-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamExample.js
executable file
·90 lines (71 loc) · 2.08 KB
/
streamExample.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
/* eslint no-console:off */
async function readFile(file) {
const { ReadableStream, TransformStream } = await import('node:stream/web');
const { Readable } = await import('stream');
const { Decoder } = await import('./sources/ebml/decoder.js');
const fs = await import('fs');
console.log(Decoder);
class NodeToWebStream {
constructor(stream) {
this.stream = stream
this._disturbed = false
stream.pause()
}
getReader() {
return new StreamReader(this.stream)
}
}
class StreamReader {
constructor(stream) {
this.stream = stream
}
[Symbol.asyncIterator]() {
return {
next: this.read.bind(this),
}
}
read() {
const {stream} = this
stream._disturbed = true
return new Promise((resolve, reject) => {
stream.resume()
stream[Symbol.asyncIterator]()
.next()
.then(resolve, reject)
})
}
releaseLock() {
this.stream = null
}
}
const transform = new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk.toUpperCase());
}
});
const writer = transform.writable.getWriter();
const reader = transform.readable.getReader();
const ws = new ReadableStream(new NodeToWebStream(fs.createReadStream(file)));
//const transformedStream = ws.pipeThrough(ebmlDecoder);
//console.log(transformedStream);
for await (const chunk of ws) writer.write(chunk);
console.log(await reader.read());
}
//readFile('./data/movTags.mkv')
async function readFileX(file) {
const { ReadableStream, TransformStream } = await import('node:stream/web');
const { Readable } = await import('stream');
const { Decoder } = await import('./sources/ebml/decoder.js');
const fs = await import('fs');
const ebmlDecoder = new Decoder();
const ebmlWriter = ebmlDecoder.writable.getWriter();
const ebmlReader = ebmlDecoder.readable.getReader();
fs.readFile(file, (err, data) => {
if (err) {
throw err;
}
ebmlWriter.write(data);
});
console.log(await ebmlReader.read());
}
readFileX('./data/movTags.mkv')