Skip to content

Commit

Permalink
Use base64 instead of json_encode to pass strings to php
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Nov 5, 2023
1 parent 3809cf3 commit ce943cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
20 changes: 10 additions & 10 deletions packages/php-wasm/util/src/lib/php-vars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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==')`
);
});

Expand All @@ -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==')`
);
});

Expand All @@ -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')`);
});
});
18 changes: 14 additions & 4 deletions packages/php-wasm/util/src/lib/php-vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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}`);
}
Expand All @@ -37,3 +38,12 @@ export function phpVars<T extends Record<string, unknown>>(
}
return result as Record<keyof T, string>;
}

function stringToBase64(str: string) {
return bytesToBase64(new TextEncoder().encode(str));
}

function bytesToBase64(bytes: Uint8Array) {
const binString = String.fromCodePoint(...bytes);
return btoa(binString);
}

0 comments on commit ce943cd

Please sign in to comment.