GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [Flight] Support Blobs from Server to Client (#28755) · react/react@cbb6f2b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • react-client/src
      • ReactFlightClient.js
    • react-server-dom-webpack/src/__tests__
      • ReactFlightDOMEdge-test.js
    • react-server/src
      • ReactFlightServer.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/react-client/src/ReactFlightClient.js‎

Copy file name to clipboardExpand all lines: packages/react-client/src/ReactFlightClient.js
+9Lines changed: 9 additions & 0 deletions
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;
Collapse file

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

Copy file name to clipboardExpand all lines: packages/react-server-dom-webpack/src/__tests__/ReactFlightDOMEdge-test.js
+26Lines changed: 26 additions & 0 deletions
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 () {},
Collapse file

‎packages/react-server/src/ReactFlightServer.js‎

Copy file name to clipboardExpand all lines: packages/react-server/src/ReactFlightServer.js
+50Lines changed: 50 additions & 0 deletions
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 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.