Skip to content

Commit 53a9f12

Browse files
authored
[Browser] Do not use the Buffer class in web browsers (#2469)
Ensures the WritablePolyfill class uses TextEncoder instead of the Buffer class when used in web browser context. Before this PR, triggering calling write() in the browser resulted in a `Buffer is not defined` error. ## Testing instructions Run this Blueprint locally and confirm it completes successfully ```json { "$schema": "https://playground.wordpress.net/blueprint-schema.json", "landingPage": "/wp-admin/admin.php?page=wc-admin", "login": true, "features": { "networking": true, "intl": true }, "steps": [ { "step": "installPlugin", "pluginData": { "resource": "url", "url": "https://github-proxy.com/proxy/?repo=woocommerce/woocommerce&release=latest&asset=woocommerce.zip" } }, { "step": "installTheme", "themeData": { "resource": "url", "url": "https://github-proxy.com/proxy/?repo=woocommerce/woo-themes&branch=trunk&directory=purple" } }, { "step": "writeFile", "path": "/wordpress/wp-content/mu-plugins/rewrite.php", "data": "<?php /* Use pretty permalinks */ add_action( 'after_setup_theme', function() { global $wp_rewrite; $wp_rewrite->set_permalink_structure('/%postname%/'); $wp_rewrite->flush_rules(); } );" }, { "step": "setSiteOptions", "options": { "blogname": "WooCommerce", "woocommerce_store_city": "Los Angeles", "woocommerce_store_address": "123 Main St", "woocommerce_store_postcode": "90038", "woocommerce_default_country": "United States", "woocommerce_onboarding_profile": { "skipped": true }, "woocommerce_currency": "USD", "woocommerce_weight_unit": "lbs", "woocommerce_dimension_unit": "in", "woocommerce_allow_tracking": "no", "woocommerce_cheque_settings": { "enabled": "yes" }, "woocommerce_coming_soon": "no" } }, { "step": "installPlugin", "pluginData": { "resource": "url", "url": "https://github-proxy.com/proxy/?repo=woocommerce/wc-smooth-generator&release=latest&asset=wc-smooth-generator.zip" }, "options": { "activate": true, "targetFolderName": "wc-smooth-generator" } }, { "step": "wp-cli", "command": "wp wc generate terms product_cat 10 --max-depth=2" }, { "step": "wp-cli", "command": "wp wc generate products 20" } ] } ``` cc @bacoords @fellyph
1 parent 41585bd commit 53a9f12

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

packages/php-wasm/util/src/lib/writable-polyfill.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,30 @@ export class WritablePolyfill extends EventEmitterPolyfill {
6262

6363
if (this.ended) {
6464
const err = new Error('write after end');
65-
this.defer(() => cb(err));
65+
// We can't call this.defer() directly. If this.defer is
66+
// `queueMicrotask`, a `this.defer()` call will pass the
67+
// WritablePolyfill instance as `this` argument and cause
68+
// the browser to throw an error similar to "Invalid
69+
// invocation".
70+
const defer = this.defer;
71+
defer(() => cb(err));
6672
this.emit('error', err);
6773
return false;
6874
}
6975

7076
if (this.decodeStrings && typeof chunk === 'string') {
71-
chunk = Buffer.from(chunk, encoding as BufferEncoding);
77+
if (
78+
typeof Buffer !== 'undefined' &&
79+
typeof (Buffer as any).from === 'function'
80+
) {
81+
chunk = Buffer.from(chunk, encoding as BufferEncoding);
82+
} else if (typeof TextEncoder !== 'undefined') {
83+
chunk = new TextEncoder().encode(chunk);
84+
} else {
85+
throw new Error(
86+
'String chunks are not supported in this environment: Buffer and TextEncoder are unavailable.'
87+
);
88+
}
7289
encoding = 'buffer' as BufferEncoding;
7390
}
7491

0 commit comments

Comments
 (0)