diff options
Diffstat (limited to 'src/util/js-template.ts')
| -rw-r--r-- | src/util/js-template.ts | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util/js-template.ts b/src/util/js-template.ts new file mode 100644 index 0000000..e536b3d --- /dev/null +++ b/src/util/js-template.ts @@ -0,0 +1,20 @@ +/** + * Build a JavaScript source string with safely-interpolated values. + * Every interpolated value is JSON.stringify'd, so strings, numbers, + * objects, and special characters all become valid JS literals. + * + * This is the ONLY supported way to build evaluation source in the + * codebase — never use raw template literals for JS that crosses the + * CDP boundary. + */ +export const safeJs = ( + strings: TemplateStringsArray, + ...values: ReadonlyArray<unknown> +): string => { + let out = strings[0] ?? ""; + for (let i = 0; i < values.length; i++) { + out += JSON.stringify(values[i]); + out += strings[i + 1] ?? ""; + } + return out; +}; |
