File size: 4,533 Bytes
bf237c2 |
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
const { Readable, getStreamError, isStreamx, isDisturbed } = require('streamx')
// https://streams.spec.whatwg.org/#readablestreamdefaultreader
exports.ReadableStreamDefaultReader = class ReadableStreamDefaultReader {
constructor(stream) {
this._stream = stream._stream
}
read() {
const stream = this._stream
return new Promise((resolve, reject) => {
const err = getStreamError(stream)
if (err) return reject(err)
if (stream.destroyed) {
return resolve({ value: undefined, done: true })
}
const value = stream.read()
if (value !== null) {
return resolve({ value, done: false })
}
stream
.once('readable', onreadable)
.once('close', onclose)
.once('error', onerror)
function onreadable() {
const value = stream.read()
ondone(
null,
value === null
? { value: undefined, done: true }
: { value, done: false }
)
}
function onclose() {
ondone(null, { value: undefined, done: true })
}
function onerror(err) {
ondone(err, null)
}
function ondone(err, value) {
stream
.off('readable', onreadable)
.off('close', onclose)
.off('error', onerror)
if (err) reject(err)
else resolve(value)
}
})
}
cancel(reason) {
if (this._stream.destroyed) return Promise.resolve()
return new Promise((resolve) =>
this._stream.once('close', resolve).destroy(reason)
)
}
}
// https://streams.spec.whatwg.org/#readablestreamdefaultcontroller
exports.ReadableStreamDefaultController = class ReadableStreamDefaultController {
constructor(stream) {
this._stream = stream._stream
}
get desiredSize() {
return (
this._stream._readableState.highWaterMark -
this._stream._readableState.buffered
)
}
enqueue(data) {
this._stream.push(data)
}
close() {
this._stream.push(null)
}
error(err) {
this._stream.destroy(err)
}
}
// https://streams.spec.whatwg.org/#readablestream
exports.ReadableStream = class ReadableStream {
constructor(underlyingSource = {}, queuingStrategy) {
if (isStreamx(underlyingSource)) {
this._stream = underlyingSource
} else {
if (queuingStrategy === undefined) {
queuingStrategy = new exports.CountQueuingStrategy()
}
const { start, pull } = underlyingSource
const { highWaterMark = 1, size = defaultSize } = queuingStrategy
this._stream = new Readable({ highWaterMark, byteLength: size })
const controller = new exports.ReadableStreamDefaultController(this)
if (start) {
this._stream._open = open.bind(this, start.call(this, controller))
}
if (pull) {
this._stream._read = read.bind(this, pull.bind(this, controller))
}
}
}
getReader() {
return new exports.ReadableStreamDefaultReader(this)
}
cancel(reason) {
if (this._stream.destroyed) return Promise.resolve()
return new Promise((resolve) =>
this._stream.once('close', resolve).destroy(reason)
)
}
pipeTo(destination) {
return new Promise((resolve, reject) =>
this._stream.pipe(destination, (err) => {
err ? reject(err) : resolve()
})
)
}
[Symbol.asyncIterator]() {
return this._stream[Symbol.asyncIterator]()
}
static from(iterable) {
return new ReadableStream(Readable.from(iterable))
}
}
async function open(starting, cb) {
try {
await starting
cb(null)
} catch (err) {
cb(err)
}
}
async function read(pull, cb) {
try {
await pull()
cb(null)
} catch (err) {
cb(err)
}
}
function defaultSize() {
return 1
}
// https://streams.spec.whatwg.org/#countqueuingstrategy
exports.CountQueuingStrategy = class CountQueuingStrategy {
constructor(opts = {}) {
const { highWaterMark = 1 } = opts
this.highWaterMark = highWaterMark
}
size(chunk) {
return 1
}
}
// https://streams.spec.whatwg.org/#bytelengthqueuingstrategy
exports.ByteLengthQueuingStrategy = class ByteLengthQueuingStrategy {
constructor(opts = {}) {
const { highWaterMark = 16384 } = opts
this.highWaterMark = highWaterMark
}
size(chunk) {
return chunk.byteLength
}
}
// https://streams.spec.whatwg.org/#is-readable-stream-disturbed
exports.isReadableStreamDisturbed = function isReadableStreamDisturbed(stream) {
return isDisturbed(stream._stream)
}
|