Skip to content

Commit 1b7f509

Browse files
committed
fixup: bodyMixin
1 parent bb368bf commit 1b7f509

File tree

2 files changed

+92
-48
lines changed

2 files changed

+92
-48
lines changed

lib/internal/streams/readable.js

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,26 +1138,6 @@ Readable.prototype.iterator = function(options) {
11381138
return streamToAsyncIterator(this, options);
11391139
};
11401140

1141-
// https://fetch.spec.whatwg.org/#dom-body-text
1142-
Readable.prototype.text = function text() {
1143-
return consume(this, kTextType);
1144-
};
1145-
1146-
// https://fetch.spec.whatwg.org/#dom-body-json
1147-
Readable.prototype.json = function json() {
1148-
return consume(this, kJSONType);
1149-
};
1150-
1151-
// https://fetch.spec.whatwg.org/#dom-body-blob
1152-
Readable.prototype.blob = function blob() {
1153-
return consume(this, kBlobType);
1154-
};
1155-
1156-
// https://fetch.spec.whatwg.org/#dom-body-arraybuffer
1157-
Readable.prototype.arrayBuffer = function arrayBuffer() {
1158-
return consume(this, kArrayBufferType);
1159-
};
1160-
11611141
function streamToAsyncIterator(stream, options) {
11621142
if (typeof stream.read !== 'function') {
11631143
// v1 stream
@@ -1325,34 +1305,6 @@ ObjectDefineProperties(Readable.prototype, {
13251305
get() {
13261306
return this._readableState ? this._readableState.endEmitted : false;
13271307
}
1328-
},
1329-
1330-
// https://fetch.spec.whatwg.org/#dom-body-bodyused
1331-
bodyUsed: {
1332-
get() {
1333-
return isDisturbed(this);
1334-
}
1335-
},
1336-
1337-
// https://fetch.spec.whatwg.org/#dom-body-body
1338-
body: {
1339-
get() {
1340-
// Compat
1341-
if (this[kHasBody]) {
1342-
return this[kBody];
1343-
}
1344-
1345-
if (this[kConsume] && this[kConsume].type === kWebStreamType) {
1346-
return this[kConsume].stream;
1347-
}
1348-
1349-
return consume(this, kWebStreamType);
1350-
},
1351-
// Compat
1352-
set(value) {
1353-
this[kBody] = value;
1354-
this[kHasBody] = true;
1355-
}
13561308
}
13571309
});
13581310

@@ -1696,6 +1648,63 @@ function endWritableNT(state, stream) {
16961648
}
16971649
}
16981650

1651+
function bodyMixin(readable) {
1652+
// https://fetch.spec.whatwg.org/#dom-body-text
1653+
readable.text = function text() {
1654+
return consume(this, kTextType);
1655+
};
1656+
1657+
// https://fetch.spec.whatwg.org/#dom-body-json
1658+
readable.json = function json() {
1659+
return consume(this, kJSONType);
1660+
};
1661+
1662+
// https://fetch.spec.whatwg.org/#dom-body-blob
1663+
readable.blob = function blob() {
1664+
return consume(this, kBlobType);
1665+
};
1666+
1667+
// https://fetch.spec.whatwg.org/#dom-body-arraybuffer
1668+
readable.arrayBuffer = function arrayBuffer() {
1669+
return consume(this, kArrayBufferType);
1670+
};
1671+
1672+
1673+
ObjectDefineProperties(readable, {
1674+
// https://fetch.spec.whatwg.org/#dom-body-bodyused
1675+
bodyUsed: {
1676+
get() {
1677+
return isDisturbed(this);
1678+
}
1679+
},
1680+
1681+
// https://fetch.spec.whatwg.org/#dom-body-body
1682+
body: {
1683+
get() {
1684+
// Compat
1685+
if (this[kHasBody]) {
1686+
return this[kBody];
1687+
}
1688+
1689+
if (this[kConsume] && this[kConsume].type === kWebStreamType) {
1690+
return this[kConsume].stream;
1691+
}
1692+
1693+
return consume(this, kWebStreamType);
1694+
},
1695+
// Compat
1696+
set(value) {
1697+
this[kBody] = value;
1698+
this[kHasBody] = true;
1699+
}
1700+
}
1701+
});
1702+
1703+
return readable;
1704+
}
1705+
1706+
Readable.bodyMixin = bodyMixin;
1707+
16991708
Readable.from = function(iterable, opts) {
17001709
return from(Readable, iterable, opts);
17011710
};

tmp.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
const {
3+
Transform,
4+
Writable,
5+
pipeline,
6+
} = require('stream');
7+
const assert = require('assert');
8+
9+
function createTransformStream(tf, context) {
10+
return new Transform({
11+
readableObjectMode: true,
12+
writableObjectMode: true,
13+
14+
transform(chunk, encoding, done) {
15+
tf(chunk, context, done);
16+
}
17+
});
18+
}
19+
20+
const write = new Writable({
21+
write(data, enc, cb) {
22+
cb();
23+
}
24+
});
25+
26+
const ts = createTransformStream((chunk, _, done) => {
27+
return done(new Error('Artificial error'));
28+
});
29+
30+
pipeline(ts, write, (err) => {
31+
assert.ok(err, 'should have an error');
32+
console.log(err);
33+
});
34+
35+
ts.write('test');

0 commit comments

Comments
 (0)