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

Polyfills for Node.js v18 and JSDOM runtimes #875

Merged
merged 5 commits into from
Dec 19, 2023
Merged

Conversation

adamziel
Copy link
Collaborator

@adamziel adamziel commented Dec 17, 2023

Description

Adds a new @php-wasm/node-polyfills package to polyfills the features missing in Node 18 and/or JSDOM environments. The goal is to make wp-now and other Playground-based Node.js packages work in Node 18, which is the current LTS release.

The polyfilled JavaScript features are:

  • CustomEvent class
  • File class
  • Blob.text() and Blob.arrayBuffer() methods
  • Blob.arrayBuffer() and File.text() methods
  • Blob.stream() and File.stream() methods
  • Ensures File.stream().getReader({ mode: 'byob' }) is supported – this is relevant for Explorations: Stream API #851

I adapted the Blob methods from https://github.com/bjornstar/blob-polyfill/blob/master/Blob.js as they seemed to provide just the logic needed here and they also worked right away.

This PR is a part of #851 split out into a separate PR to make it easier to review and reason about.

Supersedes #865

Testing instructions

Confirm the unit tests pass. This PR ships a set of vite tests to confirm the polyfills work both in vanilla Node.js and in jsdom runtime environments.

Alternatives considered

Related:

cc @danielbachhuber @sejas @eliot-akira @dmsnell

@adamziel adamziel added [Type] Enhancement New feature or request [Aspect] Node.js [Type] Reliability Playground uptime, reliability, not crashing labels Dec 17, 2023
@adamziel adamziel changed the title Polyfills: Node.js v18 and JSDOM runtimes Polyfills for Node.js v18 and JSDOM runtimes Dec 17, 2023
@adamziel adamziel marked this pull request as ready for review December 17, 2023 14:59
@adamziel adamziel requested a review from a team as a code owner December 17, 2023 14:59
@adamziel adamziel self-assigned this Dec 17, 2023
@adamziel adamziel merged commit db306f7 into trunk Dec 19, 2023
5 checks passed
@adamziel adamziel deleted the node-polyfills-only branch December 19, 2023 07:57
adamziel added a commit that referenced this pull request Dec 19, 2023
Makes Playground Blueprints compatible with Node.js v18 by using a
set of polyfills shiped in #875.

Test plan:

Confirm the CI checks passed
adamziel added a commit that referenced this pull request Dec 19, 2023
Makes Playground Blueprints compatible with Node.js v18 by using a set
of polyfills shiped in
#875.

## Test plan

Confirm the CI checks passed
adamziel pushed a commit that referenced this pull request Feb 5, 2024
…1000)

- Similar to #875
- Related to WordPress/playground-tools#113

## What is this PR doing?
It imports the native library `crypto` and makes it globally available
in the runtime.

## What problem is it solving?

When using blueprints that install plugins or themes, it generates a
random folder using
[`crypto`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto)`.
That library is available in the browsers by default, but for node we
need to import it.

Note that it do not install any dependency since Crypto is already built
in NodeJS, but it's not imported by default.
I decided to use [`node:crypto`](https://nodejs.org/api/webcrypto.html)
which has `webcrypto` and will be a better match. It's available sine
Node v15.

The other alternative is normal
[crypto](https://nodejs.org/api/crypto.html)
https://nodejs.org/docs/latest-v14.x/api/crypto.html, which also has
`crypto.randomUUID`. It's available since Node v14.

## How is the problem addressed?

It adds a new Polyfill for crypto.

## Testing Instructions

1. Comment the first line `import './crypto';` on
`packages/php-wasm/node-polyfills/src/lib/crypto.spec.ts`
2. Run `npx nx test php-wasm-node-polyfills`
3. Observe the tests fail
4. Uncomment first line `import './crypto';` on
`packages/php-wasm/node-polyfills/src/lib/crypto.spec.ts`
5. Run `npx nx test php-wasm-node-polyfills`
6. Observe the tests pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Aspect] Node.js [Type] Enhancement New feature or request [Type] Reliability Playground uptime, reliability, not crashing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant