Skip to content

Commit

Permalink
Polyfill Blob methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed May 3, 2024
1 parent 804dddb commit 8dc0498
Showing 1 changed file with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ global.ReadableStream =
global.TextEncoder = require('util').TextEncoder;
global.TextDecoder = require('util').TextDecoder;

// Polyfill stream methods on JSDOM.
global.Blob.prototype.stream = function () {
const impl = Object.getOwnPropertySymbols(this)[0];
const buffer = this[impl]._buffer;
return new ReadableStream({
start(c) {
c.enqueue(new Uint8Array(buffer));
c.close();
},
});
};

global.Blob.prototype.text = async function () {
const impl = Object.getOwnPropertySymbols(this)[0];
return this[impl]._buffer.toString('utf8');
};

// Don't wait before processing work on the server.
// TODO: we can replace this with FlightServer.act().
global.setTimeout = cb => cb();
Expand Down Expand Up @@ -975,10 +992,12 @@ describe('ReactFlightDOMForm', () => {

function Form({action}) {
const [errorMsg, dispatch] = useActionState(action, null);
let text;
if (errorMsg) {
blob = errorMsg;
text = React.use(blob.text());
}
return <form action={dispatch} />;
return <form action={dispatch}>{text}</form>;
}

const FormRef = await clientExports(Form);
Expand Down Expand Up @@ -1008,21 +1027,19 @@ describe('ReactFlightDOMForm', () => {
container.innerHTML = '';

const postbackRscStream = ReactServerDOMServer.renderToReadableStream(
<FormRef action={serverAction} />,
{formState, root: <FormRef action={serverAction} />},
webpackMap,
);
const postbackResponse = ReactServerDOMClient.createFromReadableStream(
postbackRscStream,
{
const postbackResponse =
await ReactServerDOMClient.createFromReadableStream(postbackRscStream, {
ssrManifest: {
moduleMap: null,
moduleLoading: null,
},
},
);
});
const postbackSsrStream = await ReactDOMServer.renderToReadableStream(
postbackResponse,
{formState: formState},
postbackResponse.root,
{formState: postbackResponse.formState},
);
await readIntoContainer(postbackSsrStream);
}
Expand All @@ -1034,5 +1051,8 @@ describe('ReactFlightDOMForm', () => {

expect(blob instanceof Blob).toBe(true);
expect(blob.size).toBe(2);

const form2 = container.getElementsByTagName('form')[0];
expect(form2.textContent).toBe('hi');
});
});

0 comments on commit 8dc0498

Please sign in to comment.