From 6954b9dba1e30bdf9a2c623c2846a02ac22b7703 Mon Sep 17 00:00:00 2001 From: rwaldron Date: Mon, 25 Nov 2024 10:58:55 -0500 Subject: [PATCH] fix: @W-17310064 [LWS] Add TextDecoder to near-membrane maxPerfMode keys to fix "TypeError: parameter 1 is not of type 'ArrayBuffer'" --- packages/near-membrane-base/src/intrinsics.ts | 2 + test/membrane/binary-data.spec.js | 40 +++++++++++++++++++ .../untrusted/binary-data/text-decoder.js | 1 + .../untrusted/binary-data/text-encoder.js | 1 + 4 files changed, 44 insertions(+) create mode 100644 test/membrane/untrusted/binary-data/text-decoder.js create mode 100644 test/membrane/untrusted/binary-data/text-encoder.js diff --git a/packages/near-membrane-base/src/intrinsics.ts b/packages/near-membrane-base/src/intrinsics.ts index 6edb1c3a..b425aaff 100644 --- a/packages/near-membrane-base/src/intrinsics.ts +++ b/packages/near-membrane-base/src/intrinsics.ts @@ -124,6 +124,8 @@ function getESGlobalKeys(maxPerfMode: boolean) { 'Request', 'Response', 'SubtleCrypto', + 'TextDecoder', + 'TextEncoder', 'URL', 'XMLHttpRequest', ], diff --git a/test/membrane/binary-data.spec.js b/test/membrane/binary-data.spec.js index b1bcd680..4281f274 100644 --- a/test/membrane/binary-data.spec.js +++ b/test/membrane/binary-data.spec.js @@ -398,6 +398,46 @@ describe('Binary Data and maxPerfMode:', () => { }); }); + describe('TextDecoder:', () => { + beforeEach(createBaseLineMaxCompatModeEnvironment); + + it('can decode from typed array', () => { + const env = createVirtualEnvironment(window, { + endowments: Object.getOwnPropertyDescriptors({ expect }), + }); + + env.evaluate(untrusted('text-decoder.js')); + }); + it('can decode from typed array, when maxPerfMode is true', () => { + const env = createVirtualEnvironment(window, { + endowments: Object.getOwnPropertyDescriptors({ expect }), + maxPerfMode: true, + }); + + env.evaluate(untrusted('text-decoder.js')); + }); + }); + + describe('TextEncoder:', () => { + beforeEach(createBaseLineMaxCompatModeEnvironment); + + it('can encode into typed array', () => { + const env = createVirtualEnvironment(window, { + endowments: Object.getOwnPropertyDescriptors({ expect }), + }); + + env.evaluate(untrusted('text-encoder.js')); + }); + it('can encode into typed array, when maxPerfMode is true', () => { + const env = createVirtualEnvironment(window, { + endowments: Object.getOwnPropertyDescriptors({ expect }), + maxPerfMode: true, + }); + + env.evaluate(untrusted('text-encoder.js')); + }); + }); + describe('FileSaver library:', () => { beforeEach(createBaseLineMaxCompatModeEnvironment); diff --git a/test/membrane/untrusted/binary-data/text-decoder.js b/test/membrane/untrusted/binary-data/text-decoder.js new file mode 100644 index 00000000..b0eb8647 --- /dev/null +++ b/test/membrane/untrusted/binary-data/text-decoder.js @@ -0,0 +1 @@ +new TextDecoder().decode(Uint8Array.from('Hello World!'.split('').map(letter => letter.charCodeAt(0)))); diff --git a/test/membrane/untrusted/binary-data/text-encoder.js b/test/membrane/untrusted/binary-data/text-encoder.js new file mode 100644 index 00000000..6023c4c0 --- /dev/null +++ b/test/membrane/untrusted/binary-data/text-encoder.js @@ -0,0 +1 @@ +new TextEncoder().encodeInto('hello', new Uint8Array(8), 0); \ No newline at end of file