-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Contexts created with vm.createContext() do not define the URL() constructor #28823
Comments
new contexts don't contain anything node.js-specific (Buffer, URL, process, etc). Also you can require it, it's |
I’ve labelled this
That doesn’t yield an object in the Node.js main context, though, so it’s probably not quite as useful in a different vm |
Oh I didn't mean to suggest that |
Thanks for the Surpisingly, even when URL is required into the context that way, the URL API still ends up returning arrays that are not compatible with array literals created in the context. Here's my modified test case that still fails in the same way. Is this still "currently expected behavior"? I suppose that since I'm calling a require() passed in from outside, maybe it is requiring the same outside version of URL(). Is it expected behavior that assert.deepStrictEqual() would fail to compare arrays defined in two different contexts like this? Or is there a legitimate argument to be made that this is a bug in the assert module?
|
Yes. Node.js considers the “strict” in “deep strict equal” to mean that the objects have the same prototype (at least in recent versions), and that’s not the case for objects whose prototypes are from different contexts. This may be unintuitive for built-in types like plain objects and arrays, but it makes sense once you think of it as comparing instances of two different but identical-looking classes (e.g. |
And I see that the prototype comparison with === is clearly documented at https://nodejs.org/api/assert.html#assert_comparison_details_1, so modifying deepStrictEqual() would probably be a breaking change. I would expect Thanks again for the quick responses. I guess I agree that this is a feature request and not actually a bug. |
@davidflanagan I agree that that would be nice, but in the end the problem is that there’s no real difference between objects from different contexts and objects with different but structurally equivalent classes from the same context. So, yes, I think all that we can do about this particular issue would be considering to expose the URL constructor and/or other Node.js builtin features for multiple contexts. |
@rosaxny and I would be working on this. Thanks! |
For JSDOM, we don’t want any node‑specific things (e.g.: |
@ryzokuken @rosaxny any news? Being able to add Node's "extra" globals into a |
With Node 15 adding a few more globals ( |
I've added some code to Jest to try to inspect the current env and copy over globals (https://github.com/facebook/jest/blob/49393d01cdda7dfe75718aa1a6586210fa197c72/packages/jest-environment-node/src/index.ts#L74-L103, not sure if the semantics using |
Duplicating my comments in #31852 (comment), we are getting close to get this done now with the progress in the ShadowRealms integration (a good chunk of underlying work is being done, though we need to do additional churn for side-effect-ful builtins, fortunately URL is not one of them and is available in ShadowRealms so that makes it more prioritized) |
Seems to me the last comment before the bot indicates something like this is still planned, so I don't think it's stale 🙂 |
Maybe this can get the |
This pull request fixes a range of funky JavaScript issues we've had running inflight code in the Wing simulator by switching internally from running JS code in a `vm` to running JS code using Node.js child processes. The main issues with `vm` are: - several third-party JavaScript dependencies behave differently when executed inside a `vm` and outside. This may be related to differences in vm behavior as described in nodejs/node#28823 - we cannot easily stop/kill code executing in a `vm` process, making it impossible to simulate cloud.Function timeouts Fixes #1980 Fixes #4131 Fixes #4118 Fixes #4792 Closes #4725 ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
FWIW, node:repl has a reasonable workaround for this issue, copying properties over (with their descriptors) from the calling |
I've created a simple testing framework that runs tests using vm.Script.runInContext(). Now I'm writing tests for code that uses the whatwg URL API. If I use
vm.createContext()
, the created context does not define the URL() constructor. But if I pass in the URL constructor withvm.createContext({URL})
, then I have a situation where arrays returned by URLSearchParams methods are defined using the Array.prototype object from outside the context, and my tests are trying to compare those to arrays defined inside the context with a different Array.prototype object. So because I have two arrays with different prototypes,assert.deepStrictEqual()
thinks they are not the same.I'd argue that the underlying bug here is that URL should be automatically defined in newly created contexts without having to be passed in. Or maybe this is a bug in
assert.deepStrictEqual()
and it is stricter than it ought to be in this cross-context situation?In any case, here is an example that reproduces the issue for me:
The text was updated successfully, but these errors were encountered: