Skip to content

Commit 4b30d47

Browse files
authored
fix(fetch): use structuredClone in clone body steps (nodejs#1697)
1 parent 23fbc08 commit 4b30d47

File tree

5 files changed

+163
-3
lines changed

5 files changed

+163
-3
lines changed

lib/fetch/body.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { ReadableStreamFrom, toUSVString, isBlobLike } = require('./util')
66
const { FormData } = require('./formdata')
77
const { kState } = require('./symbols')
88
const { webidl } = require('./webidl')
9-
const { DOMException } = require('./constants')
9+
const { DOMException, structuredClone } = require('./constants')
1010
const { Blob } = require('buffer')
1111
const { kBodyUsed } = require('../core/symbols')
1212
const assert = require('assert')
@@ -260,13 +260,14 @@ function cloneBody (body) {
260260

261261
// 1. Let « out1, out2 » be the result of teeing body’s stream.
262262
const [out1, out2] = body.stream.tee()
263+
const out2Clone = structuredClone(out2, { transfer: [out2] })
263264

264265
// 2. Set body’s stream to out1.
265266
body.stream = out1
266267

267268
// 3. Return a body whose stream is out2 and other members are copied from body.
268269
return {
269-
stream: out2,
270+
stream: out2Clone,
270271
length: body.length,
271272
source: body.source
272273
}

lib/fetch/constants.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const { MessageChannel, receiveMessageOnPort } = require('worker_threads')
4+
35
const corsSafeListedMethods = ['GET', 'HEAD', 'POST']
46

57
const nullBodyStatus = [101, 204, 205, 304]
@@ -71,8 +73,30 @@ const DOMException = globalThis.DOMException ?? (() => {
7173
}
7274
})()
7375

76+
let channel
77+
78+
/** @type {globalThis['structuredClone']} */
79+
const structuredClone =
80+
globalThis.structuredClone ??
81+
// https://github.com/nodejs/node/blob/b27ae24dcc4251bad726d9d84baf678d1f707fed/lib/internal/structured_clone.js
82+
// structuredClone was added in v17.0.0, but fetch supports v16.8
83+
function structuredClone (value, options = undefined) {
84+
if (arguments.length === 0) {
85+
throw new TypeError('missing argument')
86+
}
87+
88+
if (!channel) {
89+
channel = new MessageChannel()
90+
}
91+
channel.port1.unref()
92+
channel.port2.unref()
93+
channel.port1.postMessage(value, options?.transfer)
94+
return receiveMessageOnPort(channel.port2).message
95+
}
96+
7497
module.exports = {
7598
DOMException,
99+
structuredClone,
76100
subresource,
77101
forbiddenMethods,
78102
requestBodyHeader,

test/fetch/response.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test, teardown } = require('tap')
44
const {
55
Response
66
} = require('../../')
@@ -248,3 +248,6 @@ test('constructing Response with third party FormData body', async (t) => {
248248
t.equal(contentType[0], 'multipart/form-data; boundary')
249249
t.ok((await res.text()).startsWith(`--${contentType[1]}`))
250250
})
251+
252+
// This is needed due to https://github.com/nodejs/node/issues/44985
253+
teardown(() => process.exit(0))

test/wpt/status/fetch.status.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,11 @@
2929
"Response interface: operation json(any, optional ResponseInit)",
3030
"Window interface: operation fetch(RequestInfo, optional RequestInit)"
3131
]
32+
},
33+
"response-clone.any.js": {
34+
"fail": [
35+
"Check response clone use structureClone for teed ReadableStreams (ArrayBufferchunk)",
36+
"Check response clone use structureClone for teed ReadableStreams (DataViewchunk)"
37+
]
3238
}
3339
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// META: global=window,worker
2+
// META: title=Response clone
3+
// META: script=../resources/utils.js
4+
5+
var defaultValues = { "type" : "default",
6+
"url" : "",
7+
"ok" : true,
8+
"status" : 200,
9+
"statusText" : ""
10+
};
11+
12+
var response = new Response();
13+
var clonedResponse = response.clone();
14+
test(function() {
15+
for (var attributeName in defaultValues) {
16+
var expectedValue = defaultValues[attributeName];
17+
assert_equals(clonedResponse[attributeName], expectedValue,
18+
"Expect default response." + attributeName + " is " + expectedValue);
19+
}
20+
}, "Check Response's clone with default values, without body");
21+
22+
var body = "This is response body";
23+
var headersInit = { "name" : "value" };
24+
var responseInit = { "status" : 200,
25+
"statusText" : "GOOD",
26+
"headers" : headersInit
27+
};
28+
var response = new Response(body, responseInit);
29+
var clonedResponse = response.clone();
30+
test(function() {
31+
assert_equals(clonedResponse.status, responseInit["status"],
32+
"Expect response.status is " + responseInit["status"]);
33+
assert_equals(clonedResponse.statusText, responseInit["statusText"],
34+
"Expect response.statusText is " + responseInit["statusText"]);
35+
assert_equals(clonedResponse.headers.get("name"), "value",
36+
"Expect response.headers has name:value header");
37+
}, "Check Response's clone has the expected attribute values");
38+
39+
promise_test(function(test) {
40+
return validateStreamFromString(response.body.getReader(), body);
41+
}, "Check orginal response's body after cloning");
42+
43+
promise_test(function(test) {
44+
return validateStreamFromString(clonedResponse.body.getReader(), body);
45+
}, "Check cloned response's body");
46+
47+
promise_test(function(test) {
48+
var disturbedResponse = new Response("data");
49+
return disturbedResponse.text().then(function() {
50+
assert_true(disturbedResponse.bodyUsed, "response is disturbed");
51+
assert_throws_js(TypeError, function() { disturbedResponse.clone(); },
52+
"Expect TypeError exception");
53+
});
54+
}, "Cannot clone a disturbed response");
55+
56+
promise_test(function(t) {
57+
var clone;
58+
var result;
59+
var response;
60+
return fetch('../resources/trickle.py?count=2&delay=100').then(function(res) {
61+
clone = res.clone();
62+
response = res;
63+
return clone.text();
64+
}).then(function(r) {
65+
assert_equals(r.length, 26);
66+
result = r;
67+
return response.text();
68+
}).then(function(r) {
69+
assert_equals(r, result, "cloned responses should provide the same data");
70+
});
71+
}, 'Cloned responses should provide the same data');
72+
73+
promise_test(function(t) {
74+
var clone;
75+
return fetch('../resources/trickle.py?count=2&delay=100').then(function(res) {
76+
clone = res.clone();
77+
res.body.cancel();
78+
assert_true(res.bodyUsed);
79+
assert_false(clone.bodyUsed);
80+
return clone.arrayBuffer();
81+
}).then(function(r) {
82+
assert_equals(r.byteLength, 26);
83+
assert_true(clone.bodyUsed);
84+
});
85+
}, 'Cancelling stream should not affect cloned one');
86+
87+
function testReadableStreamClone(initialBuffer, bufferType)
88+
{
89+
promise_test(function(test) {
90+
var response = new Response(new ReadableStream({start : function(controller) {
91+
controller.enqueue(initialBuffer);
92+
controller.close();
93+
}}));
94+
95+
var clone = response.clone();
96+
var stream1 = response.body;
97+
var stream2 = clone.body;
98+
99+
var buffer;
100+
return stream1.getReader().read().then(function(data) {
101+
assert_false(data.done);
102+
assert_equals(data.value, initialBuffer, "Buffer of being-cloned response stream is the same as the original buffer");
103+
return stream2.getReader().read();
104+
}).then(function(data) {
105+
assert_false(data.done);
106+
assert_array_equals(data.value, initialBuffer, "Cloned buffer chunks have the same content");
107+
assert_equals(Object.getPrototypeOf(data.value), Object.getPrototypeOf(initialBuffer), "Cloned buffers have the same type");
108+
assert_not_equals(data.value, initialBuffer, "Buffer of cloned response stream is a clone of the original buffer");
109+
});
110+
}, "Check response clone use structureClone for teed ReadableStreams (" + bufferType + "chunk)");
111+
}
112+
113+
var arrayBuffer = new ArrayBuffer(16);
114+
testReadableStreamClone(new Int8Array(arrayBuffer, 1), "Int8Array");
115+
testReadableStreamClone(new Int16Array(arrayBuffer, 2, 2), "Int16Array");
116+
testReadableStreamClone(new Int32Array(arrayBuffer), "Int32Array");
117+
testReadableStreamClone(arrayBuffer, "ArrayBuffer");
118+
testReadableStreamClone(new Uint8Array(arrayBuffer), "Uint8Array");
119+
testReadableStreamClone(new Uint8ClampedArray(arrayBuffer), "Uint8ClampedArray");
120+
testReadableStreamClone(new Uint16Array(arrayBuffer, 2), "Uint16Array");
121+
testReadableStreamClone(new Uint32Array(arrayBuffer), "Uint32Array");
122+
testReadableStreamClone(typeof BigInt64Array === "function" ? new BigInt64Array(arrayBuffer) : undefined, "BigInt64Array");
123+
testReadableStreamClone(typeof BigUint64Array === "function" ? new BigUint64Array(arrayBuffer) : undefined, "BigUint64Array");
124+
testReadableStreamClone(new Float32Array(arrayBuffer), "Float32Array");
125+
testReadableStreamClone(new Float64Array(arrayBuffer), "Float64Array");
126+
testReadableStreamClone(new DataView(arrayBuffer, 2, 8), "DataView");

0 commit comments

Comments
 (0)