Skip to content

Commit

Permalink
Use Node.js Polyfills in the Blueprints package (#882)
Browse files Browse the repository at this point in the history
Makes Playground Blueprints compatible with Node.js v18 by using a set
of polyfills shiped in
#875.

## Test plan

Confirm the CI checks passed
  • Loading branch information
adamziel authored Dec 19, 2023
1 parent c363cfb commit 2bb959a
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 30 deletions.
3 changes: 3 additions & 0 deletions packages/playground/blueprints/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Blueprints require WordPress Playground's Node polyfills.
import '@php-wasm/node-polyfills';

export * from './lib/steps';
export * from './lib/steps/handlers';
export { runBlueprintSteps, compileBlueprint } from './lib/compile';
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/blueprints/src/lib/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@php-wasm/progress';
import { UniversalPHP } from '@php-wasm/universal';
import { Semaphore } from '@php-wasm/util';
import { File, zipNameToHumanName } from './steps/common';
import { zipNameToHumanName } from './steps/common';

export const ResourceTypes = [
'vfs',
Expand Down
28 changes: 0 additions & 28 deletions packages/playground/blueprints/src/lib/steps/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,6 @@ export async function fileToUint8Array(file: File) {
return new Uint8Array(await file.arrayBuffer());
}

/**
* Polyfill the File class in JSDOM which lacks arrayBuffer() method
*
* - [Implement Blob.stream, Blob.text and Blob.arrayBuffer](https://github.com/jsdom/jsdom/issues/2555)
*
* When a Resource (../resources.ts) resolves to an instance of File, the
* resulting object is missing the arrayBuffer() method in JSDOM environment
* during tests.
*
* Import the polyfilled File class below to ensure its buffer is available to
* functions like writeFile (./client-methods.ts) and fileToUint8Array (above).
*/
class FilePolyfill extends File {
buffers: BlobPart[];
constructor(buffers: BlobPart[], name: string) {
super(buffers, name);
this.buffers = buffers;
}
override async arrayBuffer(): Promise<ArrayBuffer> {
return this.buffers[0] as ArrayBuffer;
}
}

const FileWithArrayBuffer =
File.prototype.arrayBuffer instanceof Function ? File : FilePolyfill;

export { FileWithArrayBuffer as File };

export async function runPhpWithZipFunctions(
playground: UniversalPHP,
code: string
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/blueprints/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export default defineConfig({

test: {
globals: true,
setupFiles: ['./src/vitest-setup-file.ts'],
cache: {
dir: '../../../node_modules/.vitest',
},
setupFiles: ['./src/vitest-setup-file.ts'],
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
Expand Down

0 comments on commit 2bb959a

Please sign in to comment.