/
stream.ts
164 lines (144 loc) · 4.54 KB
/
stream.ts
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
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Readable as NodeReadableNative, ReadableOptions as NodeReadableOptions } from 'stream'
import { isStrictlyObject } from './type'
import { ReadableStream } from 'web-streams-polyfill'
import { Readable } from '../types'
const NodeReadable = NodeReadableNative || class {}
/**
* Validates if passed object is either browser's ReadableStream
* or Node's Readable.
*
* @param entry
*/
export function isReadable(entry: unknown): entry is Readable {
return isReadableStream(entry) || isNodeReadable(entry)
}
export function isReadableStream(entry: unknown): entry is ReadableStream {
if (!isStrictlyObject(entry)) {
return false
}
const browserReadable = entry as ReadableStream
if (
typeof browserReadable.getReader === 'function' &&
browserReadable.locked !== undefined &&
typeof browserReadable.cancel === 'function' &&
typeof browserReadable.pipeTo === 'function' &&
typeof browserReadable.pipeThrough === 'function'
) {
return true
}
return false
}
export function isNodeReadable(entry: unknown): entry is NodeReadableNative {
if (!isStrictlyObject(entry)) {
return false
}
const nodeReadable = entry as NodeReadableNative
if (typeof nodeReadable.pipe === 'function' && nodeReadable.readable && typeof nodeReadable._read === 'function') {
return true
}
return false
}
/**
* Function that converts Node's Readable into WHATWG ReadableStream
*
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
* Because it uses forked web-streams-polyfill that are outdated.
*
* @author https://github.com/gwicke
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
* @param nodeStream
*/
export function readableNodeToWeb(nodeStream: NodeReadableNative): ReadableStream<Uint8Array> {
return new ReadableStream({
start(controller) {
nodeStream.pause()
nodeStream.on('data', chunk => {
if (Buffer.isBuffer(chunk)) {
controller.enqueue(new Uint8Array(chunk.buffer))
} else {
controller.enqueue(chunk)
}
nodeStream.pause()
})
nodeStream.on('end', () => controller.close())
nodeStream.on('error', e => controller.error(e))
},
pull() {
nodeStream.resume()
},
cancel() {
nodeStream.pause()
},
})
}
/**
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
* Because it uses forked web-streams-polyfill that are outdated.
*
* @author https://github.com/gwicke
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
*/
class NodeReadableWrapper extends NodeReadable {
private _webStream: ReadableStream
private _reader: ReadableStreamDefaultReader<any>
private _reading: boolean
constructor(webStream: ReadableStream, options?: NodeReadableOptions) {
super(options)
this._webStream = webStream
this._reader = webStream.getReader()
this._reading = false
}
_read() {
if (this._reading) {
return
}
this._reading = true
const doRead = () => {
this._reader.read().then(res => {
if (res.done) {
this.push(null)
return
}
if (this.push(res.value)) {
return doRead()
} else {
this._reading = false
}
})
}
doRead()
}
}
/**
* Function that converts WHATWG ReadableStream into Node's Readable
*
* Taken over from https://github.com/gwicke/node-web-streams/blob/master/lib/conversions.js
* Because it uses forked web-streams-polyfill that is outdated.
*
* **Warning!**
* If you want to use this function in browser you have to polyfill `stream` package with your bundler.
*
* @author https://github.com/gwicke
* @licence Apache License 2.0 https://github.com/gwicke/node-web-streams/blob/master/LICENSE
* @param webStream
* @param options
*/
export function readableWebToNode(
webStream: ReadableStream<unknown>,
options?: NodeReadableOptions,
): NodeReadableNative {
if (!NodeReadableNative) {
throw new Error(
"The Node's Readable is not available! If you are running this in browser you have to polyfill 'stream' package!",
)
}
return new NodeReadableWrapper(webStream, options) as unknown as NodeReadableNative
}
export function normalizeToReadableStream(stream: Readable): ReadableStream {
if (isNodeReadable(stream)) {
return readableNodeToWeb(stream)
} else if (isReadableStream(stream)) {
return stream
}
throw new TypeError('Passed stream is not Node Readable nor ReadableStream!')
}