File tree Expand file tree Collapse file tree 1 file changed +19
-2
lines changed
packages/php-wasm/util/src/lib Expand file tree Collapse file tree 1 file changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -62,13 +62,30 @@ export class WritablePolyfill extends EventEmitterPolyfill {
62
62
63
63
if ( this . ended ) {
64
64
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 ) ) ;
66
72
this . emit ( 'error' , err ) ;
67
73
return false ;
68
74
}
69
75
70
76
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
+ }
72
89
encoding = 'buffer' as BufferEncoding ;
73
90
}
74
91
You can’t perform that action at this time.
0 commit comments