blob: b78ff5c5538940fc3da6304ab7e1584a631eaee2 (
plain)
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
|
'use strict'
const { Readable, Writable } = require('stream')
let ReadableStream
function createReadable (data) {
return new Readable({
read () {
this.push(Buffer.from(data))
this.push(null)
}
})
}
function createWritable (target) {
return new Writable({
write (chunk, _, callback) {
target.push(chunk.toString())
callback()
},
final (callback) {
callback()
}
})
}
class Source {
constructor (data) {
this.data = data
}
async start (controller) {
this.controller = controller
}
async pull (controller) {
controller.enqueue(this.data)
controller.close()
}
}
function createReadableStream (data) {
ReadableStream = require('stream/web').ReadableStream
return new ReadableStream(new Source(data))
}
module.exports = { createReadableStream, createReadable, createWritable }
|