diff --git a/packages/php-wasm/util/src/lib/php-vars.spec.ts b/packages/php-wasm/util/src/lib/php-vars.spec.ts index d5d088d628..16fc10f682 100644 --- a/packages/php-wasm/util/src/lib/php-vars.spec.ts +++ b/packages/php-wasm/util/src/lib/php-vars.spec.ts @@ -3,13 +3,13 @@ import { phpVar } from './php-vars'; describe('phpVar', () => { test('translates strings', () => { const result = phpVar('Hello, World!'); - expect(result).toBe(`json_decode('"Hello, World!"')`); + expect(result).toBe(`base64_decode('SGVsbG8sIFdvcmxkIQ==')`); }); test('escapes single quotes in strings (simple)', () => { const result = phpVar(`This is Playground's test suite`); expect(result).toBe( - `json_decode('"This is Playground\\'s test suite"')` + `base64_decode('VGhpcyBpcyBQbGF5Z3JvdW5kJ3MgdGVzdCBzdWl0ZQ==')` ); }); @@ -18,7 +18,7 @@ describe('phpVar', () => { `This is an escaped quot: \\'. This is an escaped backslash and a quot: \\\\'. This is an evil terminating backslash: \\` ); expect(result).toBe( - `json_decode('"This is an escaped quot: \\\\\\\\\\'. This is an escaped backslash and a quot: \\\\\\\\\\\\\\\\\\'. This is an evil terminating backslash: \\\\\\\\"')` + `base64_decode('VGhpcyBpcyBhbiBlc2NhcGVkIHF1b3Q6IFwnLiBUaGlzIGlzIGFuIGVzY2FwZWQgYmFja3NsYXNoIGFuZCBhIHF1b3Q6IFxcJy4gVGhpcyBpcyBhbiBldmlsIHRlcm1pbmF0aW5nIGJhY2tzbGFzaDogXA==')` ); }); @@ -29,42 +29,42 @@ describe('phpVar', () => { test('translates arrays', () => { const result = phpVar([5, 'test']); - expect(result).toBe(`array(5, json_decode('"test"'))`); + expect(result).toBe(`array(5, base64_decode('dGVzdA=='))`); }); test('translates objects', () => { const result = phpVar({ a: 5, b: 'test' }); expect(result).toBe( - `array(json_decode('"a"') => 5, json_decode('"b"') => json_decode('"test"'))` + `array(base64_decode('YQ==') => 5, base64_decode('Yg==') => base64_decode('dGVzdA=='))` ); }); test('translate nested arrays', () => { const result = phpVar([1, ['a', 'b'], 3]); expect(result).toBe( - `array(1, array(json_decode('"a"'), json_decode('"b"')), 3)` + `array(1, array(base64_decode('YQ=='), base64_decode('Yg==')), 3)` ); }); test('translate nested objects', () => { const result = phpVar({ outer: { inner: 'value' } }); expect(result).toBe( - `array(json_decode('"outer"') => array(json_decode('"inner"') => json_decode('"value"')))` + `array(base64_decode('b3V0ZXI=') => array(base64_decode('aW5uZXI=') => base64_decode('dmFsdWU=')))` ); }); test('properly encodes strings', () => { const result = phpVar('Hello, "World!"'); - expect(result).toBe(`json_decode('"Hello, \\\\"World!\\\\""')`); + expect(result).toBe(`base64_decode('SGVsbG8sICJXb3JsZCEi')`); }); test('properly encodes strings with special characters', () => { const result = phpVar('Hello,\nWorld!'); - expect(result).toBe(`json_decode('"Hello,\\\\nWorld!"')`); + expect(result).toBe(`base64_decode('SGVsbG8sCldvcmxkIQ==')`); }); test('properly enchodes strings with uniresult characters', () => { const result = phpVar('こんにちは'); - expect(result).toBe(`json_decode('"こんにちは"')`); + expect(result).toBe(`base64_decode('44GT44KT44Gr44Gh44Gv')`); }); }); diff --git a/packages/php-wasm/util/src/lib/php-vars.ts b/packages/php-wasm/util/src/lib/php-vars.ts index 59b36938df..c39f5d3e7b 100644 --- a/packages/php-wasm/util/src/lib/php-vars.ts +++ b/packages/php-wasm/util/src/lib/php-vars.ts @@ -2,10 +2,7 @@ const literal = Symbol('literal'); export function phpVar(value: unknown): string { if (typeof value === 'string') { - const escapedValue = JSON.stringify(value) - .replace(/\\/g, '\\\\') - .replace(/'/g, "\\'"); - return `json_decode('${escapedValue}')`; + return `base64_decode('${stringToBase64(value)}')`; } else if (typeof value === 'number') { return value.toString(); } else if (Array.isArray(value)) { @@ -24,6 +21,10 @@ export function phpVar(value: unknown): string { } } else if (typeof value === 'function') { return value(); + } else if (ArrayBuffer.isView(value)) { + return `base64_decode("'${bytesToBase64( + new Uint8Array(value as any) + )}'")`; } throw new Error(`Unsupported value: ${value}`); } @@ -37,3 +38,12 @@ export function phpVars>( } return result as Record; } + +function stringToBase64(str: string) { + return bytesToBase64(new TextEncoder().encode(str)); +} + +function bytesToBase64(bytes: Uint8Array) { + const binString = String.fromCodePoint(...bytes); + return btoa(binString); +}