Skip to content

Commit cbb6f2b

Browse files
authored
[Flight] Support Blobs from Server to Client (#28755)
We currently support Blobs when passing from Client to Server so this adds it in the other direction for parity - when `enableFlightBinary` is enabled. We intentionally only support the `Blob` type to pass-through, not subtype `File`. That's because passing additional meta data like filename might be an accidental leak. You can still pass a `File` through but it'll appear as a `Blob` on the other side. It's also not possible to create a faithful File subclass in all environments without it actually being backed by a file. This implementation isn't great but at least it works. It creates a few indirections. This is because we need to be able to asynchronously emit the buffers but we have to "block" the parent object from resolving while it's loading. Ideally, we should be able to create the Blob on the client early and then stream in it lazily. Because the Blob API doesn't guarantee that the data is available synchronously. Unfortunately, the native APIs doesn't have this. We could implement custom versions of all the data read APIs but then the blobs still wouldn't work with native APIs. So we just have to wait until Blob accepts a stream in the constructor. We should be able to stream each chunk early in the protocol though even though we can't unblock the parent until they've all loaded. I didn't do this yet mostly because of code structure and I'm lazy.
1 parent f33a6b6 commit cbb6f2b

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

packages/react-client/src/ReactFlightClient.js

+9
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,15 @@ function parseModelString(
737737
const data = getOutlinedModel(response, id);
738738
return new Set(data);
739739
}
740+
case 'B': {
741+
// Blob
742+
if (enableBinaryFlight) {
743+
const id = parseInt(value.slice(2), 16);
744+
const data = getOutlinedModel(response, id);
745+
return new Blob(data.slice(1), {type: data[0]});
746+
}
747+
return undefined;
748+
}
740749
case 'I': {
741750
// $Infinity
742751
return Infinity;

packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMEdge-test.js

+26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @emails react-core
8+
* @jest-environment ./scripts/jest/ReactDOMServerIntegrationEnvironment
89
*/
910

1011
'use strict';
@@ -14,6 +15,9 @@ global.ReadableStream =
1415
require('web-streams-polyfill/ponyfill/es6').ReadableStream;
1516
global.TextEncoder = require('util').TextEncoder;
1617
global.TextDecoder = require('util').TextDecoder;
18+
if (typeof Blob === 'undefined') {
19+
global.Blob = require('buffer').Blob;
20+
}
1721

1822
// Don't wait before processing work on the server.
1923
// TODO: we can replace this with FlightServer.act().
@@ -326,6 +330,28 @@ describe('ReactFlightDOMEdge', () => {
326330
expect(result).toEqual(buffers);
327331
});
328332

333+
// @gate enableBinaryFlight
334+
it('should be able to serialize a blob', async () => {
335+
const bytes = new Uint8Array([
336+
123, 4, 10, 5, 100, 255, 244, 45, 56, 67, 43, 124, 67, 89, 100, 20,
337+
]);
338+
const blob = new Blob([bytes, bytes], {
339+
type: 'application/x-test',
340+
});
341+
const stream = passThrough(
342+
ReactServerDOMServer.renderToReadableStream(blob),
343+
);
344+
const result = await ReactServerDOMClient.createFromReadableStream(stream, {
345+
ssrManifest: {
346+
moduleMap: null,
347+
moduleLoading: null,
348+
},
349+
});
350+
expect(result instanceof Blob).toBe(true);
351+
expect(result.size).toBe(bytes.length * 2);
352+
expect(await result.arrayBuffer()).toEqual(await blob.arrayBuffer());
353+
});
354+
329355
it('warns if passing a this argument to bind() of a server reference', async () => {
330356
const ServerModule = serverExports({
331357
greet: function () {},

packages/react-server/src/ReactFlightServer.js

+50
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ export type ReactClientValue =
239239
| Array<ReactClientValue>
240240
| Map<ReactClientValue, ReactClientValue>
241241
| Set<ReactClientValue>
242+
| $ArrayBufferView
243+
| ArrayBuffer
242244
| Date
243245
| ReactClientObject
244246
| Promise<ReactClientValue>; // Thenable<ReactClientValue>
@@ -1229,6 +1231,46 @@ function serializeTypedArray(
12291231
return serializeByValueID(bufferId);
12301232
}
12311233

1234+
function serializeBlob(request: Request, blob: Blob): string {
1235+
const id = request.nextChunkId++;
1236+
request.pendingChunks++;
1237+
1238+
const reader = blob.stream().getReader();
1239+
1240+
const model: Array<string | Uint8Array> = [blob.type];
1241+
1242+
function progress(
1243+
entry: {done: false, value: Uint8Array} | {done: true, value: void},
1244+
): Promise<void> | void {
1245+
if (entry.done) {
1246+
const blobId = outlineModel(request, model);
1247+
const blobReference = '$B' + blobId.toString(16);
1248+
const processedChunk = encodeReferenceChunk(request, id, blobReference);
1249+
request.completedRegularChunks.push(processedChunk);
1250+
if (request.destination !== null) {
1251+
flushCompletedChunks(request, request.destination);
1252+
}
1253+
return;
1254+
}
1255+
// TODO: Emit the chunk early and refer to it later.
1256+
model.push(entry.value);
1257+
// $FlowFixMe[incompatible-call]
1258+
return reader.read().then(progress).catch(error);
1259+
}
1260+
1261+
function error(reason: mixed) {
1262+
const digest = logRecoverableError(request, reason);
1263+
emitErrorChunk(request, id, digest, reason);
1264+
if (request.destination !== null) {
1265+
flushCompletedChunks(request, request.destination);
1266+
}
1267+
}
1268+
// $FlowFixMe[incompatible-call]
1269+
reader.read().then(progress).catch(error);
1270+
1271+
return '$' + id.toString(16);
1272+
}
1273+
12321274
function escapeStringValue(value: string): string {
12331275
if (value[0] === '$') {
12341276
// We need to escape $ prefixed strings since we use those to encode
@@ -1606,6 +1648,10 @@ function renderModelDestructive(
16061648
if (value instanceof DataView) {
16071649
return serializeTypedArray(request, 'V', value);
16081650
}
1651+
// TODO: Blob is not available in old Node. Remove the typeof check later.
1652+
if (typeof Blob === 'function' && value instanceof Blob) {
1653+
return serializeBlob(request, value);
1654+
}
16091655
}
16101656

16111657
const iteratorFn = getIteratorFn(value);
@@ -2146,6 +2192,10 @@ function renderConsoleValue(
21462192
if (value instanceof DataView) {
21472193
return serializeTypedArray(request, 'V', value);
21482194
}
2195+
// TODO: Blob is not available in old Node. Remove the typeof check later.
2196+
if (typeof Blob === 'function' && value instanceof Blob) {
2197+
return serializeBlob(request, value);
2198+
}
21492199
}
21502200

21512201
const iteratorFn = getIteratorFn(value);

0 commit comments

Comments
 (0)