diff options
Diffstat (limited to 'src/js/_codegen')
-rw-r--r-- | src/js/_codegen/build-functions.ts | 26 | ||||
-rw-r--r-- | src/js/_codegen/build-modules.ts | 4 | ||||
-rw-r--r-- | src/js/_codegen/client-js.ts | 2 | ||||
-rw-r--r-- | src/js/_codegen/replacements.ts | 38 |
4 files changed, 45 insertions, 25 deletions
diff --git a/src/js/_codegen/build-functions.ts b/src/js/_codegen/build-functions.ts index 761682d44..fcce0263a 100644 --- a/src/js/_codegen/build-functions.ts +++ b/src/js/_codegen/build-functions.ts @@ -1,7 +1,7 @@ -import { existsSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "fs"; +import { existsSync, mkdirSync, readdirSync, rmSync } from "fs"; import path from "path"; import { sliceSourceCode } from "./builtin-parser"; -import { applyGlobalReplacements, enums, globalsToPrefix } from "./replacements"; +import { applyGlobalReplacements, define } from "./replacements"; import { cap, fmtCPPString, low } from "./helpers"; import { spawn } from "bun"; @@ -48,24 +48,6 @@ const TMP_DIR = path.join(SRC_DIR, "../out/tmp/builtins"); if (existsSync(TMP_DIR)) rmSync(TMP_DIR, { recursive: true }); mkdirSync(TMP_DIR, { recursive: true }); -const define = { - "process.env.NODE_ENV": "production", - "IS_BUN_DEVELOPMENT": "false", -}; - -for (const name in enums) { - const value = enums[name]; - if (typeof value !== "object") throw new Error("Invalid enum object " + name + " defined in " + import.meta.file); - if (typeof value === null) throw new Error("Invalid enum object " + name + " defined in " + import.meta.file); - const keys = Array.isArray(value) ? value : Object.keys(value).filter(k => !k.match(/^[0-9]+$/)); - define[`__intrinsic__${name}IdToLabel`] = "[" + keys.map(k => `"${k}"`).join(", ") + "]"; - define[`__intrinsic__${name}LabelToId`] = "{" + keys.map(k => `"${k}": ${keys.indexOf(k)}`).join(", ") + "}"; -} - -for (const name of globalsToPrefix) { - define[name] = "__intrinsic__" + name; -} - interface ParsedBuiltin { name: string; params: string[]; @@ -218,7 +200,7 @@ $$capture_start$$(${fn.async ? "async " : ""}${ const build = await Bun.build({ entrypoints: [tmpFile], define, - minify: { syntax: true, whitespace: true }, + minify: { syntax: true, whitespace: false }, }); if (!build.success) { throw new AggregateError(build.logs, "Failed bundling builtin function " + fn.name + " from " + basename + ".ts"); @@ -231,7 +213,7 @@ $$capture_start$$(${fn.async ? "async " : ""}${ const finalReplacement = (fn.directives.sloppy ? captured : captured.replace(/function\s*\(.*?\)\s*{/, '$&"use strict";')) .replace(/^\((async )?function\(/, "($1function (") - .replace(/__intrinsic__lazy\(/g, "globalThis[globalThis.Symbol.for('Bun.lazy')](") + // .replace(/__intrinsic__lazy\(/g, "globalThis[globalThis.Symbol.for('Bun.lazy')](") .replace(/__intrinsic__/g, "@") + "\n"; bundledFunctions.push({ diff --git a/src/js/_codegen/build-modules.ts b/src/js/_codegen/build-modules.ts index 3443db6f6..2e49dfffe 100644 --- a/src/js/_codegen/build-modules.ts +++ b/src/js/_codegen/build-modules.ts @@ -5,6 +5,7 @@ import { cap, fmtCPPString, readdirRecursive, resolveSyncOrNull } from "./helper import { createAssertClientJS, createLogClientJS } from "./client-js"; import { builtinModules } from "node:module"; import { BuildConfig } from "bun"; +import { define } from "./replacements"; const t = new Bun.Transpiler({ loader: "tsx" }); @@ -173,6 +174,7 @@ const config = ({ platform, debug }: { platform: string; debug?: boolean }) => target: "bun", external: builtinModules, define: { + ...define, IS_BUN_DEVELOPMENT: String(!!debug), __intrinsic__debug: debug ? "$debug_log_enabled" : "false", "process.platform": JSON.stringify(platform), @@ -222,7 +224,7 @@ for (const [name, bundle, outputs] of [ .replace(/\$\$EXPORT\$\$\((.*)\).\$\$EXPORT_END\$\$;/, "return $1") .replace(/]\s*,\s*__(debug|assert)_end__\)/g, ")") .replace(/]\s*,\s*__debug_end__\)/g, ")") - .replace(/__intrinsic__lazy\(/g, "globalThis[globalThis.Symbol.for('Bun.lazy')](") + // .replace(/__intrinsic__lazy\(/g, "globalThis[globalThis.Symbol.for('Bun.lazy')](") .replace(/import.meta.require\((.*?)\)/g, (expr, specifier) => { try { const str = JSON.parse(specifier); diff --git a/src/js/_codegen/client-js.ts b/src/js/_codegen/client-js.ts index 849240c1f..2db3305fa 100644 --- a/src/js/_codegen/client-js.ts +++ b/src/js/_codegen/client-js.ts @@ -15,7 +15,7 @@ let $debug_log_enabled = ((env) => ( .split(/[-_./]/g) .join("_") .toUpperCase()}) -))(@Bun.env); +))(Bun.env); let $debug_log = $debug_log_enabled ? (...args) => { // warn goes to stderr without colorizing console.warn(Bun.enableANSIColors ? '\\x1b[90m[${publicName}]\\x1b[0m' : '[${publicName}]', ...args); diff --git a/src/js/_codegen/replacements.ts b/src/js/_codegen/replacements.ts index 4621d6134..5ce646ad5 100644 --- a/src/js/_codegen/replacements.ts +++ b/src/js/_codegen/replacements.ts @@ -24,13 +24,14 @@ export const globalReplacements: ReplacementRule[] = [ ]; // This is a list of globals we should access using @ notation +// This prevents a global override attacks. +// Note that the public `Bun` global is immutable. // undefined -> __intrinsic__undefined -> @undefined export const globalsToPrefix = [ "AbortSignal", "Array", "ArrayBuffer", "Buffer", - "Bun", "Infinity", "Loader", "Promise", @@ -79,6 +80,41 @@ export const warnOnIdentifiersNotPresentAtRuntime = [ "notImplementedIssueFn", ]; +// These are passed to --define to the bundler +export const define: Record<string, string> = { + "process.env.NODE_ENV": "production", + "IS_BUN_DEVELOPMENT": "false", + + $streamClosed: "1", + $streamClosing: "2", + $streamErrored: "3", + $streamReadable: "4", + $streamWaiting: "5", + $streamWritable: "6", +}; + +// ------------------------------ // + +for (const name in enums) { + const value = enums[name]; + if (typeof value !== "object") throw new Error("Invalid enum object " + name + " defined in " + import.meta.file); + if (typeof value === null) throw new Error("Invalid enum object " + name + " defined in " + import.meta.file); + const keys = Array.isArray(value) ? value : Object.keys(value).filter(k => !k.match(/^[0-9]+$/)); + define[`$${name}IdToLabel`] = "[" + keys.map(k => `"${k}"`).join(", ") + "]"; + define[`$${name}LabelToId`] = "{" + keys.map(k => `"${k}": ${keys.indexOf(k)}`).join(", ") + "}"; +} + +for (const name of globalsToPrefix) { + define[name] = "__intrinsic__" + name; +} + +for (const key in define) { + if (key.startsWith("$")) { + define["__intrinsic__" + key.slice(1)] = define[key]; + delete define[key]; + } +} + export interface ReplacementRule { from: RegExp; to: string; |