Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flight] Prevent non-Server imports of aliased Server entrypoints #20422

Merged
merged 3 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion fixtures/flight-browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h1>Flight Example</h1>
<script src="../../build/node_modules/react/umd/react.development.js"></script>
<script src="../../build/node_modules/react-dom/umd/react-dom.development.js"></script>
<script src="../../build/node_modules/react-dom/umd/react-dom-server.browser.development.js"></script>
<script src="../../build/node_modules/react-server-dom-webpack/umd/react-server-dom-webpack-writer.browser.server.development.js"></script>
<script src="../../build/node_modules/react-server-dom-webpack/umd/react-server-dom-webpack-writer.browser.development.server.js"></script>
<script src="../../build/node_modules/react-server-dom-webpack/umd/react-server-dom-webpack.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.js"></script>
<script type="text/babel">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,23 @@ export async function resolve(
);
}
}
// We intentionally check the specifier here instead of the resolved file.
// This allows package exports to configure non-server aliases that resolve to server files
// depending on environment. It's probably a bad idea to export a server file as "main" though.
if (specifier.endsWith('.server.js')) {
const resolved = defaultResolve(specifier, context, defaultResolve);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's something funky. The actual defaultResolve is sync. No promise. Is it supposed to be chainable, i.e. can this return a Promise?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird. You'd expect they'd want to do that asynchronously.

But it's supposed to be chainable so you can do stuff like this:

https://github.com/facebook/react/blob/master/fixtures/flight/loader/index.js#L30

gaearon marked this conversation as resolved.
Show resolved Hide resolved
if (resolved.url.endsWith('.server.js')) {
if (context.parentURL && !context.parentURL.endsWith('.server.js')) {
let reason;
if (specifier.endsWith('.server.js')) {
reason = `"${specifier}"`;
} else {
reason = `"${specifier}" (which expands to "${resolved.url}")`;
}
throw new Error(
`Cannot import "${specifier}" from "${context.parentURL}". ` +
`Cannot import ${reason} from "${context.parentURL}". ` +
'By react-server convention, .server.js files can only be imported from other .server.js files. ' +
'That way nobody accidentally sends these to the client by indirectly importing it.',
);
}
}
return defaultResolve(specifier, context, defaultResolve);
return resolved;
}

export async function getSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,26 @@ module.exports = function register() {
const originalResolveFilename = Module._resolveFilename;

Module._resolveFilename = function(request, parent, isMain, options) {
// We intentionally check the request here instead of the resolved file.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn’t make sense

// This allows package exports to configure non-server aliases that resolve to server files
// depending on environment. It's probably a bad idea to export a server file as "main" though.
if (request.endsWith('.server.js')) {
const resolved = originalResolveFilename.apply(this, arguments);
if (resolved.endsWith('.server.js')) {
if (
parent &&
parent.filename &&
!parent.filename.endsWith('.server.js')
) {
let reason;
if (request.endsWith('.server.js')) {
reason = `"${request}"`;
} else {
reason = `"${request}" (which expands to "${resolved}")`;
}
throw new Error(
`Cannot import "${request}" from "${parent.filename}". ` +
`Cannot import ${reason} from "${parent.filename}". ` +
'By react-server convention, .server.js files can only be imported from other .server.js files. ' +
'That way nobody accidentally sends these to the client by indirectly importing it.',
);
}
}
return originalResolveFilename.apply(this, arguments);
return resolved;
};
};