Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function ReactPromise(status: any, value: any, reason: any) {
// We subclass Promise.prototype so that we get other methods like .catch
ReactPromise.prototype = Object.create(Promise.prototype) as any;
// TODO: This doesn't return a new Promise chain unlike the real .then
ReactPromise.prototype.then = function <T>(
function reactPromiseThen<T>(
this: SomeChunk<T>,
resolve: (value: T) => mixed,
reject?: (reason: mixed) => mixed,
Expand Down Expand Up @@ -326,7 +326,17 @@ ReactPromise.prototype.then = function <T>(
}
break;
}
};
}
// The shadowing `then` must be defined with `Object.defineProperty` instead of
// assignment. Assignment would throw when `Promise.prototype` is frozen (e.g.
// by SES lockdown) because assigning over an inherited non-writable property
// is rejected.
Object.defineProperty(ReactPromise.prototype, 'then', {
writable: true,
enumerable: true,
configurable: true,
value: reactPromiseThen,
});

export type FindSourceMapURLCallback = (
fileName: string,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

import {patchMessageChannel} from '../../../../scripts/jest/patchMessageChannel';

// Polyfills for test environment
global.ReadableStream =
require('web-streams-polyfill/ponyfill/es6').ReadableStream;
global.TextEncoder = require('util').TextEncoder;
global.TextDecoder = require('util').TextDecoder;

describe('ReactFlight with a non-writable Promise.prototype.then', () => {
let originalThen;

beforeEach(() => {
jest.resetModules();
originalThen = Object.getOwnPropertyDescriptor(Promise.prototype, 'then');
// eslint-disable-next-line no-extend-native
Object.defineProperty(Promise.prototype, 'then', {
...originalThen,
writable: false,
});
});

afterEach(() => {
// eslint-disable-next-line no-extend-native
Object.defineProperty(Promise.prototype, 'then', originalThen);
});

it('can require Server and Client entrypoints', () => {
patchMessageChannel(require('scheduler'));
// Simulate the condition resolution
jest.mock('react', () => require('react/react.react-server'));
jest.mock('react-server-dom-webpack/server', () =>
require('react-server-dom-webpack/server.browser'),
);
require('./utils/WebpackMock');
const ReactServerDOMServer = require('react-server-dom-webpack/server');
expect(typeof ReactServerDOMServer.decodeReply).toBe('function');

__unmockReact();
jest.resetModules();
const ReactServerDOMClient = require('react-server-dom-webpack/client');
expect(typeof ReactServerDOMClient.createFromReadableStream).toBe(
'function',
);
});
});
14 changes: 12 additions & 2 deletions packages/react-server/src/ReactFlightReplyServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function ReactPromise(status: any, value: any, reason: any) {
// We subclass Promise.prototype so that we get other methods like .catch
ReactPromise.prototype = Object.create(Promise.prototype) as any;
// TODO: This doesn't return a new Promise chain unlike the real .then
ReactPromise.prototype.then = function <T>(
function reactPromiseThen<T>(
this: SomeChunk<T>,
resolve: (value: T) => mixed,
reject: ?(reason: mixed) => mixed,
Expand Down Expand Up @@ -197,7 +197,17 @@ ReactPromise.prototype.then = function <T>(
}
break;
}
};
}
// The shadowing `then` must be defined with `Object.defineProperty` instead of
// assignment. Assignment would throw when `Promise.prototype` is frozen (e.g.
// by SES lockdown) because assigning over an inherited non-writable property
// is rejected.
Object.defineProperty(ReactPromise.prototype, 'then', {
writable: true,
enumerable: true,
configurable: true,
value: reactPromiseThen,
});

const ObjectPrototype = Object.prototype;
const ArrayPrototype = Array.prototype;
Expand Down
Loading