@skathi-native/cli 0.0.0 → 0.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/aot-inputs.ts +294 -0
- package/ascii.ts +397 -0
- package/build.ts +1236 -0
- package/check/builtins.json +1219 -0
- package/check/builtins.ts +112 -0
- package/check/codes.ts +178 -0
- package/check/command.ts +105 -0
- package/check/config.ts +76 -0
- package/check/finding.ts +30 -0
- package/check/graph.ts +69 -0
- package/check/highlight.ts +40 -0
- package/check/index.ts +308 -0
- package/check/packages.ts +54 -0
- package/check/program.ts +209 -0
- package/check/report.ts +219 -0
- package/check/rules.ts +307 -0
- package/check/schema.ts +69 -0
- package/check/typing.ts +703 -0
- package/cli/assets.ts +114 -0
- package/cli/build-app.ts +587 -0
- package/cli/config.ts +646 -0
- package/cli/create.ts +327 -0
- package/cli/dev.ts +92 -0
- package/cli/elf.ts +142 -0
- package/cli/help.ts +160 -0
- package/cli/host-fetch.ts +178 -0
- package/cli/host.ts +378 -0
- package/cli/icon.ts +303 -0
- package/cli/macos-bundle.ts +451 -0
- package/cli/pe.ts +429 -0
- package/cli/platform.ts +74 -0
- package/cli/sk.ts +290 -0
- package/cli/skpk.ts +268 -0
- package/cli/stamp.ts +254 -0
- package/cli/term.ts +176 -0
- package/cli/versioninfo.ts +123 -0
- package/css-prefixes.ts +118 -0
- package/devtools/apps.ts +68 -0
- package/devtools/commands.ts +860 -0
- package/devtools/ctl.ts +166 -0
- package/package.json +52 -3
- package/prod.ts +701 -0
- package/runtime-path.ts +24 -0
- package/tailwind.ts +76 -0
- package/templates/minimal/assets/hello.txt +1 -0
- package/templates/minimal/src/App.vue +142 -0
- package/templates/minimal/src/main.ts +4 -0
- package/templates/showcase/assets/hello.txt +1 -0
- package/templates/showcase/assets/media/aurora.png +0 -0
- package/templates/showcase/assets/media/clip.mp4 +0 -0
- package/templates/showcase/assets/media/sticker.tgs +0 -0
- package/templates/showcase/src/App.vue +128 -0
- package/templates/showcase/src/components/Card.vue +41 -0
- package/templates/showcase/src/components/Icon.vue +29 -0
- package/templates/showcase/src/components/Missing.vue +26 -0
- package/templates/showcase/src/components/Page.vue +41 -0
- package/templates/showcase/src/components/Titlebar.vue +128 -0
- package/templates/showcase/src/engine.ts +33 -0
- package/templates/showcase/src/icons.ts +22 -0
- package/templates/showcase/src/main.ts +7 -0
- package/templates/showcase/src/pages/AudioPage.vue +124 -0
- package/templates/showcase/src/pages/FilesPage.vue +91 -0
- package/templates/showcase/src/pages/FormsPage.vue +139 -0
- package/templates/showcase/src/pages/GraphicsPage.vue +156 -0
- package/templates/showcase/src/pages/HomePage.vue +141 -0
- package/templates/showcase/src/pages/LocalePage.vue +71 -0
- package/templates/showcase/src/pages/MediaPage.vue +85 -0
- package/templates/showcase/src/pages/MotionPage.vue +199 -0
- package/templates/showcase/src/pages/NetworkPage.vue +103 -0
- package/templates/showcase/src/pages/PermissionsPage.vue +80 -0
- package/templates/showcase/src/pages/StoragePage.vue +117 -0
- package/templates/showcase/src/pages/StylesPage.vue +199 -0
- package/templates/showcase/src/pages/SystemPage.vue +89 -0
- package/templates/showcase/src/pages/TrayPage.vue +66 -0
- package/templates/showcase/src/pages/WindowPage.vue +128 -0
- package/templates/showcase/src/product.ts +2 -0
- package/templates/showcase/src/router.ts +149 -0
- package/templates/showcase/src/stores/counter.ts +9 -0
- package/templates/showcase/src/stores/desktop.ts +115 -0
- package/templates/showcase/src/styles.css +140 -0
- package/vitecompat.ts +1151 -0
package/aot-inputs.ts
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// Build-time input archive for a future typed AOT frontend. This is deliberately
|
|
2
|
+
// separate from SKPK: source inputs are neither executable code nor JSC bytecode.
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { mkdirSync, renameSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
6
|
+
import type ts from "typescript";
|
|
7
|
+
|
|
8
|
+
type TypeScript = typeof ts;
|
|
9
|
+
type InputKind = "source" | "vue" | "vue-module" | "vue-script" | "transformed-module" | "config";
|
|
10
|
+
interface Input {
|
|
11
|
+
path: string;
|
|
12
|
+
kind: InputKind;
|
|
13
|
+
text: string;
|
|
14
|
+
loader?: string | undefined;
|
|
15
|
+
}
|
|
16
|
+
const sha256 = (text: string | Uint8Array) => createHash("sha256").update(text).digest("hex");
|
|
17
|
+
const slash = (path: string) => path.split(sep).join("/");
|
|
18
|
+
const compare = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0);
|
|
19
|
+
|
|
20
|
+
export interface AotEngineIdentity {
|
|
21
|
+
jscPin: string | null;
|
|
22
|
+
asset: string | null;
|
|
23
|
+
aot: boolean | null;
|
|
24
|
+
sha256: string | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function aotEngineIdentity(pins: string | null): AotEngineIdentity {
|
|
28
|
+
const values = new Map<string, string>();
|
|
29
|
+
for (const line of (pins ?? "").split(/\r?\n/)) {
|
|
30
|
+
const match = /^\s*(webkit\.[a-z0-9]+)\s*=\s*([^#]*?)\s*(?:#.*)?$/.exec(line);
|
|
31
|
+
if (match) values.set(match[1]!, match[2]!.trim());
|
|
32
|
+
}
|
|
33
|
+
const flag = values.get("webkit.aot");
|
|
34
|
+
if (flag !== undefined && flag !== "0" && flag !== "1") throw new Error(`Invalid webkit.aot pin: ${flag}`);
|
|
35
|
+
return {
|
|
36
|
+
jscPin: values.get("webkit.version") || null,
|
|
37
|
+
asset: values.get("webkit.asset") || null,
|
|
38
|
+
aot: flag === undefined ? null : flag === "1",
|
|
39
|
+
sha256: values.get("webkit.sha256") || null,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface AotInputsOptions {
|
|
44
|
+
out: string;
|
|
45
|
+
appRoot: string;
|
|
46
|
+
entry: string;
|
|
47
|
+
typescript: TypeScript;
|
|
48
|
+
aliases: Record<string, string>;
|
|
49
|
+
declarations?: string[];
|
|
50
|
+
sourceRoots?: Record<string, string>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class AotInputs {
|
|
54
|
+
private readonly inputs = new Map<string, Input>();
|
|
55
|
+
private readonly modules = new Map<string, { path: string; text: string }>();
|
|
56
|
+
private readonly virtual = new Map<string, { path: string; text: string }>();
|
|
57
|
+
private readonly unresolved = new Map<string, { importer: string; specifier: string }>();
|
|
58
|
+
private readonly key: (path: string) => string;
|
|
59
|
+
|
|
60
|
+
constructor(private readonly options: AotInputsOptions) {
|
|
61
|
+
this.key = options.typescript.sys.useCaseSensitiveFileNames
|
|
62
|
+
? (path) => resolve(path)
|
|
63
|
+
: (path) => resolve(path).toLowerCase();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private pathId(path: string): string {
|
|
67
|
+
const appPath = relative(this.options.appRoot, path);
|
|
68
|
+
if (!isAbsolute(appPath) && appPath !== ".." && !appPath.startsWith(`..${sep}`)) {
|
|
69
|
+
const first = slash(appPath).split("/", 1)[0]!;
|
|
70
|
+
const reserved = Object.keys(this.options.sourceRoots ?? {}).some(
|
|
71
|
+
(name) => first.toLowerCase() === `@${name}`.toLowerCase(),
|
|
72
|
+
);
|
|
73
|
+
if (reserved) throw new Error(`AOT app input ${appPath} collides with a named source-root anchor`);
|
|
74
|
+
return slash(appPath);
|
|
75
|
+
}
|
|
76
|
+
for (const [name, root] of Object.entries(this.options.sourceRoots ?? {})) {
|
|
77
|
+
if (!/^[a-z][a-z0-9-]*$/i.test(name)) throw new Error(`Invalid AOT source-root name: ${name}`);
|
|
78
|
+
const rootPath = relative(root, path);
|
|
79
|
+
if (!isAbsolute(rootPath) && rootPath !== ".." && !rootPath.startsWith(`..${sep}`))
|
|
80
|
+
return `@${name}/${slash(rootPath)}`;
|
|
81
|
+
}
|
|
82
|
+
if (isAbsolute(appPath)) throw new Error(`AOT input ${path} is on another volume; add a stable sourceRoots anchor`);
|
|
83
|
+
return slash(appPath);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private add(path: string, text: string, kind: InputKind, loader?: string): void {
|
|
87
|
+
this.inputs.set(`${kind}:${this.key(path)}`, { path: resolve(path), kind, text, loader });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
source(path: string, original: string, transformed = original): void {
|
|
91
|
+
if (this.virtual.has(this.key(path))) throw new Error(`AOT source ${path} collides with a virtual Vue module`);
|
|
92
|
+
this.add(path, original, "source");
|
|
93
|
+
if (transformed !== original) this.add(path, transformed, "transformed-module");
|
|
94
|
+
this.modules.set(this.key(path), { path: resolve(path), text: transformed });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private addVirtual(path: string, text: string): void {
|
|
98
|
+
if (this.options.typescript.sys.fileExists(path) || this.modules.has(this.key(path))) {
|
|
99
|
+
throw new Error(`AOT virtual Vue module ${path} collides with a real source file`);
|
|
100
|
+
}
|
|
101
|
+
const existing = this.virtual.get(this.key(path));
|
|
102
|
+
if (existing && existing.text !== text) throw new Error(`AOT virtual Vue module ${path} has conflicting contents`);
|
|
103
|
+
this.virtual.set(this.key(path), { path, text });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
vue(path: string, original: string, compiled: string, loader: "js" | "ts", originalScript = ""): void {
|
|
107
|
+
this.add(path, original, "vue");
|
|
108
|
+
this.add(path, compiled, "vue-module", loader);
|
|
109
|
+
const virtualPath = `${resolve(path)}.${loader}`;
|
|
110
|
+
this.addVirtual(virtualPath, compiled);
|
|
111
|
+
// compileScript can erase type imports used only by defineProps macros.
|
|
112
|
+
if (originalScript) {
|
|
113
|
+
this.add(path, originalScript, "vue-script", loader);
|
|
114
|
+
const scriptPath = `${resolve(path)}.original.${loader}`;
|
|
115
|
+
this.addVirtual(scriptPath, originalScript);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private collectTypes(): { version: string; config: string | null; diagnostics: { code: number; message: string }[] } {
|
|
120
|
+
const { typescript, appRoot, entry, aliases, declarations = [] } = this.options;
|
|
121
|
+
const read = (path: string): string | undefined => {
|
|
122
|
+
const known = this.virtual.get(this.key(path)) ?? this.modules.get(this.key(path));
|
|
123
|
+
if (known) return known.text;
|
|
124
|
+
const text = typescript.sys.readFile(path);
|
|
125
|
+
if (text !== undefined) this.add(path, text, /\.json$/i.test(path) ? "config" : "source");
|
|
126
|
+
return text;
|
|
127
|
+
};
|
|
128
|
+
const configPath = typescript.findConfigFile(appRoot, typescript.sys.fileExists);
|
|
129
|
+
let roots: string[] = [];
|
|
130
|
+
let options: ts.CompilerOptions = {
|
|
131
|
+
target: typescript.ScriptTarget.ESNext,
|
|
132
|
+
module: typescript.ModuleKind.ESNext,
|
|
133
|
+
moduleResolution: typescript.ModuleResolutionKind.Bundler,
|
|
134
|
+
};
|
|
135
|
+
if (configPath) {
|
|
136
|
+
const visited = new Set<string>();
|
|
137
|
+
const projects: ts.ParsedCommandLine[] = [];
|
|
138
|
+
const visit = (path: string): void => {
|
|
139
|
+
if (visited.has(this.key(path))) return;
|
|
140
|
+
visited.add(this.key(path));
|
|
141
|
+
const config = typescript.readConfigFile(path, read);
|
|
142
|
+
if (config.error) throw new Error(typescript.flattenDiagnosticMessageText(config.error.messageText, "\n"));
|
|
143
|
+
const parsed = typescript.parseJsonConfigFileContent(
|
|
144
|
+
config.config,
|
|
145
|
+
{ ...typescript.sys, readFile: read },
|
|
146
|
+
dirname(path),
|
|
147
|
+
);
|
|
148
|
+
const errors = parsed.errors.filter((diagnostic) => diagnostic.code !== 18003);
|
|
149
|
+
if (errors.length)
|
|
150
|
+
throw new Error(
|
|
151
|
+
errors
|
|
152
|
+
.map((diagnostic) => typescript.flattenDiagnosticMessageText(diagnostic.messageText, "\n"))
|
|
153
|
+
.join("\n"),
|
|
154
|
+
);
|
|
155
|
+
projects.push(parsed);
|
|
156
|
+
for (const reference of parsed.projectReferences ?? []) {
|
|
157
|
+
visit(typescript.resolveProjectReferencePath(reference));
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
visit(configPath);
|
|
161
|
+
// Use the entry's project (a Vue/Vite solution tsconfig has files: []) so its aliases and ambient types survive.
|
|
162
|
+
const selected =
|
|
163
|
+
projects.find((project) => project.fileNames.some((path) => this.key(path) === this.key(entry))) ??
|
|
164
|
+
projects[0]!;
|
|
165
|
+
options = selected.options;
|
|
166
|
+
roots = selected.fileNames.filter((path) => /\.[cm]?[jt]sx?$/i.test(path));
|
|
167
|
+
}
|
|
168
|
+
const host = typescript.createCompilerHost({ ...options, noEmit: true, allowJs: true });
|
|
169
|
+
host.readFile = read;
|
|
170
|
+
host.fileExists = (path) => this.virtual.has(this.key(path)) || typescript.sys.fileExists(path);
|
|
171
|
+
host.resolveModuleNames = (names, importer) =>
|
|
172
|
+
names.map((specifier) => {
|
|
173
|
+
const alias = aliases[specifier];
|
|
174
|
+
// Bun's resolved runtime modules are already roots; keep the package's declaration entry too,
|
|
175
|
+
// or .d.ts files and every type-only dependency behind them are dropped.
|
|
176
|
+
const resolved =
|
|
177
|
+
typescript.resolveModuleName(specifier, importer, options, host).resolvedModule ??
|
|
178
|
+
(specifier.endsWith(".vue")
|
|
179
|
+
? typescript.resolveModuleName(`${specifier}.ts`, importer, options, host).resolvedModule
|
|
180
|
+
: undefined) ??
|
|
181
|
+
(alias ? typescript.resolveModuleName(specifier, alias, options, host).resolvedModule : undefined) ??
|
|
182
|
+
(alias
|
|
183
|
+
? {
|
|
184
|
+
resolvedFileName: alias,
|
|
185
|
+
extension: /\.[cm]?tsx?$/.test(alias) ? typescript.Extension.Ts : typescript.Extension.Js,
|
|
186
|
+
}
|
|
187
|
+
: undefined);
|
|
188
|
+
if (!resolved) {
|
|
189
|
+
const value = { importer: this.pathId(importer), specifier };
|
|
190
|
+
this.unresolved.set(`${value.importer}:${specifier}`, value);
|
|
191
|
+
}
|
|
192
|
+
return resolved;
|
|
193
|
+
});
|
|
194
|
+
// Override CompilerHost's getSourceFile so virtual Vue modules and post-plugin TS participate.
|
|
195
|
+
host.getSourceFile = (path, languageVersion) => {
|
|
196
|
+
const text = read(path);
|
|
197
|
+
return text === undefined ? undefined : typescript.createSourceFile(path, text, languageVersion, true);
|
|
198
|
+
};
|
|
199
|
+
const program = typescript.createProgram({
|
|
200
|
+
rootNames: [
|
|
201
|
+
...new Set([
|
|
202
|
+
entry,
|
|
203
|
+
...roots,
|
|
204
|
+
...declarations,
|
|
205
|
+
...[...this.modules.values()].map((value) => value.path),
|
|
206
|
+
...[...this.virtual.values()].map((value) => value.path),
|
|
207
|
+
]),
|
|
208
|
+
].sort(compare),
|
|
209
|
+
options: { ...options, noEmit: true, allowJs: true },
|
|
210
|
+
host,
|
|
211
|
+
});
|
|
212
|
+
// Force resolution without running type checking or changing build acceptance.
|
|
213
|
+
program.getSourceFiles();
|
|
214
|
+
return {
|
|
215
|
+
version: typescript.version,
|
|
216
|
+
config: configPath ? this.pathId(configPath) : null,
|
|
217
|
+
diagnostics: program.getOptionsDiagnostics().map((diagnostic) => ({
|
|
218
|
+
code: diagnostic.code,
|
|
219
|
+
message: typescript.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
|
|
220
|
+
})),
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
write(details: {
|
|
225
|
+
script: string;
|
|
226
|
+
scriptUrl: string;
|
|
227
|
+
defines: Record<string, string>;
|
|
228
|
+
engine: AotEngineIdentity;
|
|
229
|
+
sdkVersion: string;
|
|
230
|
+
}): string {
|
|
231
|
+
const typescript = this.collectTypes();
|
|
232
|
+
const { out, entry, aliases } = this.options;
|
|
233
|
+
const inputs = [...this.inputs.values()].map((input) => ({ ...input, id: this.pathId(input.path) }));
|
|
234
|
+
const identities = new Set<string>();
|
|
235
|
+
for (const input of inputs) {
|
|
236
|
+
const identity = `${input.kind}:${input.id}`;
|
|
237
|
+
if (identities.has(identity)) throw new Error(`AOT input identity collision: ${identity}`);
|
|
238
|
+
identities.add(identity);
|
|
239
|
+
}
|
|
240
|
+
const blobs = join(out, "blobs");
|
|
241
|
+
mkdirSync(blobs, { recursive: true });
|
|
242
|
+
const written = new Set<string>();
|
|
243
|
+
const files = inputs
|
|
244
|
+
.map((input) => {
|
|
245
|
+
const hash = sha256(input.text);
|
|
246
|
+
const blob = `blobs/${hash}`;
|
|
247
|
+
if (!written.has(hash)) {
|
|
248
|
+
const path = join(out, blob);
|
|
249
|
+
writeFileSync(`${path}.tmp`, input.text);
|
|
250
|
+
renameSync(`${path}.tmp`, path);
|
|
251
|
+
written.add(hash);
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
path: input.id,
|
|
255
|
+
kind: input.kind,
|
|
256
|
+
...(input.loader ? { loader: input.loader } : {}),
|
|
257
|
+
sha256: hash,
|
|
258
|
+
bytes: Buffer.byteLength(input.text),
|
|
259
|
+
blob,
|
|
260
|
+
};
|
|
261
|
+
})
|
|
262
|
+
.sort((a, b) => compare(a.path, b.path) || compare(a.kind, b.kind));
|
|
263
|
+
const manifest = {
|
|
264
|
+
schema: "skathi.aot-inputs",
|
|
265
|
+
version: 1,
|
|
266
|
+
stage: "typed-source-inputs",
|
|
267
|
+
entry: this.pathId(entry),
|
|
268
|
+
typescript,
|
|
269
|
+
sdkVersion: details.sdkVersion,
|
|
270
|
+
engine: details.engine,
|
|
271
|
+
script: { url: details.scriptUrl, sha256: sha256(details.script), bytes: Buffer.byteLength(details.script) },
|
|
272
|
+
defines: details.defines,
|
|
273
|
+
aliases: Object.fromEntries(
|
|
274
|
+
Object.entries(aliases)
|
|
275
|
+
.sort(([a], [b]) => compare(a, b))
|
|
276
|
+
.map(([key, value]) => [key, this.pathId(value)]),
|
|
277
|
+
),
|
|
278
|
+
files,
|
|
279
|
+
unresolvedImports: [...this.unresolved.values()].sort(
|
|
280
|
+
(a, b) => compare(a.importer, b.importer) || compare(a.specifier, b.specifier),
|
|
281
|
+
),
|
|
282
|
+
limitations: [
|
|
283
|
+
"Source archive, not a native AOT image; no host ABI or builtin-effect descriptors are inferred from declarations.",
|
|
284
|
+
"Original paths are app-root-relative or use a named source-root anchor; blob names are content hashes. An AOT frontend needs a resolver that maps these paths to blobs.",
|
|
285
|
+
"Vue modules and TS/JS source rewrites are retained before Bun transpilation. Bun-generated asset/query/HTML modules, CSS installation and module linking still require a frontend adapter.",
|
|
286
|
+
"TypeScript file-module resolution is recorded in addition to Bun's runtime graph; unresolvedImports also includes ambient module names that have no file mapping. A frontend adapter must handle them. This archive does not assert type-checking or AOT compatibility.",
|
|
287
|
+
],
|
|
288
|
+
};
|
|
289
|
+
const manifestPath = join(out, "manifest.json");
|
|
290
|
+
writeFileSync(`${manifestPath}.tmp`, JSON.stringify(manifest, null, 2) + "\n");
|
|
291
|
+
renameSync(`${manifestPath}.tmp`, manifestPath);
|
|
292
|
+
return manifestPath;
|
|
293
|
+
}
|
|
294
|
+
}
|
package/ascii.ts
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
// Makes a production bundle pure ASCII, so JavaScriptCore holds its source as 8-bit: one non-ASCII
|
|
2
|
+
// character makes WTF keep the whole source as UTF-16 for the life of the process, and Bun's bundler
|
|
3
|
+
// has no ASCII-only output. This pass rewrites the finished bundle, turning each non-ASCII character
|
|
4
|
+
// into the escape its context reads as the same thing (`\uXXXX` in strings, templates, regexes,
|
|
5
|
+
// comments; `\u{…}` for astral identifiers; LS / PS outside a literal become line feeds).
|
|
6
|
+
//
|
|
7
|
+
// A `\` before the character goes with it; a `\` before LS / PS in a string is a line continuation,
|
|
8
|
+
// so the pair is dropped. In a TAGGED template the tag reads `strings.raw`, so those characters are
|
|
9
|
+
// left (the result counts them). The lexer must guess whether a `/` divides or starts a regex; if it
|
|
10
|
+
// ends in an impossible state the bundle is returned unchanged. prod.ts also checks the result with
|
|
11
|
+
// Bun's parser (`checkAscii`).
|
|
12
|
+
|
|
13
|
+
export interface AsciiResult {
|
|
14
|
+
code: string;
|
|
15
|
+
escaped: number;
|
|
16
|
+
kept: number;
|
|
17
|
+
failed?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const LS = 0x2028;
|
|
21
|
+
const PS = 0x2029;
|
|
22
|
+
|
|
23
|
+
const hex4 = (u: number) => "\\u" + u.toString(16).toUpperCase().padStart(4, "0");
|
|
24
|
+
|
|
25
|
+
/** `\uXXXX`, or a surrogate pair: one escape that a string, template and regular expression all read as `cp`. */
|
|
26
|
+
function escapeUnits(cp: number): string {
|
|
27
|
+
if (cp <= 0xffff) return hex4(cp);
|
|
28
|
+
const v = cp - 0x10000;
|
|
29
|
+
return hex4(0xd800 + (v >> 10)) + hex4(0xdc00 + (v & 0x3ff));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function escapeIdent(cp: number): string {
|
|
33
|
+
return cp <= 0xffff ? hex4(cp) : "\\u{" + cp.toString(16).toUpperCase() + "}";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Keywords after which a `/` starts a regular expression. Not `of`: a minifier may name a variable `of`. */
|
|
37
|
+
const BEFORE_EXPRESSION = new Set([
|
|
38
|
+
"return",
|
|
39
|
+
"typeof",
|
|
40
|
+
"instanceof",
|
|
41
|
+
"in",
|
|
42
|
+
"new",
|
|
43
|
+
"delete",
|
|
44
|
+
"void",
|
|
45
|
+
"throw",
|
|
46
|
+
"case",
|
|
47
|
+
"do",
|
|
48
|
+
"else",
|
|
49
|
+
"yield",
|
|
50
|
+
"await",
|
|
51
|
+
"extends",
|
|
52
|
+
]);
|
|
53
|
+
const PAREN_STATEMENT = new Set(["if", "while", "for", "with"]);
|
|
54
|
+
|
|
55
|
+
const isIdentPart = (c: number) =>
|
|
56
|
+
(c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c === 36 || c === 95 || c === 92;
|
|
57
|
+
|
|
58
|
+
export function asciiOnly(src: string): AsciiResult {
|
|
59
|
+
const out: string[] = [];
|
|
60
|
+
let copied = 0; // src[copied, i) is still to be copied verbatim
|
|
61
|
+
let escaped = 0;
|
|
62
|
+
let kept = 0;
|
|
63
|
+
let i = 0;
|
|
64
|
+
const n = src.length;
|
|
65
|
+
|
|
66
|
+
const put = (from: number, to: number, text: string) => {
|
|
67
|
+
if (from > copied) out.push(src.slice(copied, from));
|
|
68
|
+
out.push(text);
|
|
69
|
+
copied = to;
|
|
70
|
+
escaped++;
|
|
71
|
+
};
|
|
72
|
+
const cpAt = (at: number): [number, number] => {
|
|
73
|
+
const cp = src.codePointAt(at)!;
|
|
74
|
+
return [cp, cp > 0xffff ? 2 : 1];
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
let regexOk = true;
|
|
78
|
+
const parens: boolean[] = [];
|
|
79
|
+
const braces: ("block" | "expr" | "tpl")[] = [];
|
|
80
|
+
const templates: boolean[] = []; // tagged? per open `${`
|
|
81
|
+
let lastWord = "";
|
|
82
|
+
let last: "word" | "value" | "punct" = "punct";
|
|
83
|
+
let lastPunct = "";
|
|
84
|
+
|
|
85
|
+
const literalChar = (at: number, backslash: boolean, kind: "string" | "template" | "regex"): number => {
|
|
86
|
+
const from = backslash ? at - 1 : at;
|
|
87
|
+
const [cp, len] = cpAt(at);
|
|
88
|
+
if (backslash && (cp === LS || cp === PS) && kind !== "regex") put(from, at + len, "");
|
|
89
|
+
else put(from, at + len, escapeUnits(cp));
|
|
90
|
+
return at + len;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const templateText = (tagged: boolean): boolean => {
|
|
94
|
+
while (i < n) {
|
|
95
|
+
const c = src.charCodeAt(i);
|
|
96
|
+
if (c === 92) {
|
|
97
|
+
const d = src.charCodeAt(i + 1);
|
|
98
|
+
if (d > 0x7f) {
|
|
99
|
+
if (tagged) {
|
|
100
|
+
kept++;
|
|
101
|
+
i += cpAt(i + 1)[1] + 1;
|
|
102
|
+
} else i = literalChar(i + 1, true, "template");
|
|
103
|
+
} else i += 2;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (c === 96) {
|
|
107
|
+
i++;
|
|
108
|
+
last = "value";
|
|
109
|
+
regexOk = false;
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
if (c === 36 && src.charCodeAt(i + 1) === 123) {
|
|
113
|
+
i += 2;
|
|
114
|
+
braces.push("tpl");
|
|
115
|
+
templates.push(tagged);
|
|
116
|
+
last = "punct";
|
|
117
|
+
lastPunct = "${";
|
|
118
|
+
regexOk = true;
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
if (c > 0x7f) {
|
|
122
|
+
if (tagged) {
|
|
123
|
+
kept++;
|
|
124
|
+
i += cpAt(i)[1];
|
|
125
|
+
} else i = literalChar(i, false, "template");
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
i++;
|
|
129
|
+
}
|
|
130
|
+
return false; // unterminated
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
while (i < n) {
|
|
134
|
+
const c = src.charCodeAt(i);
|
|
135
|
+
|
|
136
|
+
if (c === 32 || c === 9 || c === 10 || c === 13 || c === 11 || c === 12) {
|
|
137
|
+
i++;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (c === 47 && src.charCodeAt(i + 1) === 47) {
|
|
142
|
+
i += 2;
|
|
143
|
+
while (i < n) {
|
|
144
|
+
const d = src.charCodeAt(i);
|
|
145
|
+
if (d === 10 || d === 13 || d === LS || d === PS) break;
|
|
146
|
+
if (d > 0x7f) {
|
|
147
|
+
const [cp, len] = cpAt(i);
|
|
148
|
+
put(i, i + len, escapeUnits(cp));
|
|
149
|
+
i += len;
|
|
150
|
+
} else i++;
|
|
151
|
+
}
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (c === 47 && src.charCodeAt(i + 1) === 42) {
|
|
155
|
+
const end = src.indexOf("*/", i + 2);
|
|
156
|
+
if (end < 0) return { code: src, escaped: 0, kept: 0, failed: "an unterminated /* comment" };
|
|
157
|
+
for (let k = i + 2; k < end;) {
|
|
158
|
+
const d = src.charCodeAt(k);
|
|
159
|
+
if (d > 0x7f) {
|
|
160
|
+
const [cp, len] = cpAt(k);
|
|
161
|
+
put(k, k + len, cp === LS || cp === PS ? "\n" : escapeUnits(cp));
|
|
162
|
+
k += len;
|
|
163
|
+
} else k++;
|
|
164
|
+
}
|
|
165
|
+
i = end + 2;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (c === 34 || c === 39) {
|
|
170
|
+
i++;
|
|
171
|
+
for (;;) {
|
|
172
|
+
if (i >= n) return { code: src, escaped: 0, kept: 0, failed: "an unterminated string" };
|
|
173
|
+
const d = src.charCodeAt(i);
|
|
174
|
+
if (d === c) break;
|
|
175
|
+
if (d === 92) {
|
|
176
|
+
if (src.charCodeAt(i + 1) > 0x7f) i = literalChar(i + 1, true, "string");
|
|
177
|
+
else i += 2;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (d === 10 || d === 13) return { code: src, escaped: 0, kept: 0, failed: "a line break inside a string" };
|
|
181
|
+
if (d > 0x7f) i = literalChar(i, false, "string");
|
|
182
|
+
else i++;
|
|
183
|
+
}
|
|
184
|
+
i++;
|
|
185
|
+
last = "value";
|
|
186
|
+
regexOk = false;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (c === 96) {
|
|
191
|
+
const tagged = last === "word" ? !BEFORE_EXPRESSION.has(lastWord) : last === "value";
|
|
192
|
+
i++;
|
|
193
|
+
if (!templateText(tagged)) return { code: src, escaped: 0, kept: 0, failed: "an unterminated template" };
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (c === 47) {
|
|
198
|
+
if (!regexOk) {
|
|
199
|
+
i++;
|
|
200
|
+
if (src.charCodeAt(i) === 61) i++; // `/=`
|
|
201
|
+
last = "punct";
|
|
202
|
+
lastPunct = "/";
|
|
203
|
+
regexOk = true;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
i++;
|
|
207
|
+
let inClass = false;
|
|
208
|
+
for (;;) {
|
|
209
|
+
if (i >= n) return { code: src, escaped: 0, kept: 0, failed: "an unterminated regular expression" };
|
|
210
|
+
const d = src.charCodeAt(i);
|
|
211
|
+
if (d === 10 || d === 13 || d === LS || d === PS) {
|
|
212
|
+
return { code: src, escaped: 0, kept: 0, failed: `a line break inside a regular expression at ${i}` };
|
|
213
|
+
}
|
|
214
|
+
if (d === 92) {
|
|
215
|
+
if (src.charCodeAt(i + 1) > 0x7f) i = literalChar(i + 1, true, "regex");
|
|
216
|
+
else i += 2;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (d === 91) inClass = true;
|
|
220
|
+
else if (d === 93) inClass = false;
|
|
221
|
+
else if (d === 47 && !inClass) break;
|
|
222
|
+
if (d > 0x7f) i = literalChar(i, false, "regex");
|
|
223
|
+
else i++;
|
|
224
|
+
}
|
|
225
|
+
i++;
|
|
226
|
+
while (i < n && isIdentPart(src.charCodeAt(i))) i++; // flags
|
|
227
|
+
last = "value";
|
|
228
|
+
regexOk = false;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (c === 40) {
|
|
233
|
+
parens.push(last === "word" && PAREN_STATEMENT.has(lastWord));
|
|
234
|
+
i++;
|
|
235
|
+
last = "punct";
|
|
236
|
+
lastPunct = "(";
|
|
237
|
+
regexOk = true;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
if (c === 41) {
|
|
241
|
+
if (!parens.length) return { code: src, escaped: 0, kept: 0, failed: `an unmatched ) at ${i}` };
|
|
242
|
+
const statement = parens.pop()!;
|
|
243
|
+
i++;
|
|
244
|
+
last = statement ? "punct" : "value";
|
|
245
|
+
lastPunct = ")";
|
|
246
|
+
regexOk = statement;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (c === 123) {
|
|
250
|
+
const block =
|
|
251
|
+
last === "word"
|
|
252
|
+
? !BEFORE_EXPRESSION.has(lastWord) || lastWord === "else" || lastWord === "do"
|
|
253
|
+
: last === "punct"
|
|
254
|
+
? [")", ";", "{", "}", "=>", ""].includes(lastPunct)
|
|
255
|
+
: false;
|
|
256
|
+
braces.push(block ? "block" : "expr");
|
|
257
|
+
i++;
|
|
258
|
+
last = "punct";
|
|
259
|
+
lastPunct = "{";
|
|
260
|
+
regexOk = true;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (c === 125) {
|
|
264
|
+
const kind = braces.pop();
|
|
265
|
+
if (!kind) return { code: src, escaped: 0, kept: 0, failed: `an unmatched } at ${i}` };
|
|
266
|
+
i++;
|
|
267
|
+
if (kind === "tpl") {
|
|
268
|
+
if (!templateText(templates.pop()!)) {
|
|
269
|
+
return { code: src, escaped: 0, kept: 0, failed: "an unterminated template" };
|
|
270
|
+
}
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
last = kind === "block" ? "punct" : "value";
|
|
274
|
+
lastPunct = "}";
|
|
275
|
+
regexOk = kind === "block";
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (c === 91) {
|
|
279
|
+
parens.push(false);
|
|
280
|
+
i++;
|
|
281
|
+
last = "punct";
|
|
282
|
+
lastPunct = "[";
|
|
283
|
+
regexOk = true;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (c === 93) {
|
|
287
|
+
if (!parens.length) return { code: src, escaped: 0, kept: 0, failed: `an unmatched ] at ${i}` };
|
|
288
|
+
parens.pop();
|
|
289
|
+
i++;
|
|
290
|
+
last = "value";
|
|
291
|
+
lastPunct = "]";
|
|
292
|
+
regexOk = false;
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (isIdentPart(c) || c > 0x7f) {
|
|
297
|
+
if (c > 0x7f) {
|
|
298
|
+
const [cp, len] = cpAt(i);
|
|
299
|
+
if (cp === LS || cp === PS) {
|
|
300
|
+
put(i, i + len, "\n");
|
|
301
|
+
i += len;
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (/\s/u.test(String.fromCodePoint(cp))) {
|
|
305
|
+
put(i, i + len, " ");
|
|
306
|
+
i += len;
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const start = i;
|
|
311
|
+
while (i < n) {
|
|
312
|
+
const d = src.charCodeAt(i);
|
|
313
|
+
if (d > 0x7f) {
|
|
314
|
+
const [cp, len] = cpAt(i);
|
|
315
|
+
if (cp === LS || cp === PS || /\s/u.test(String.fromCodePoint(cp))) break;
|
|
316
|
+
put(i, i + len, escapeIdent(cp));
|
|
317
|
+
i += len;
|
|
318
|
+
} else if (isIdentPart(d)) i++;
|
|
319
|
+
else break;
|
|
320
|
+
}
|
|
321
|
+
const word = src.slice(start, i);
|
|
322
|
+
if (c >= 48 && c <= 57) {
|
|
323
|
+
while (i < n) {
|
|
324
|
+
const d = src.charCodeAt(i);
|
|
325
|
+
if (d === 46 || isIdentPart(d)) i++;
|
|
326
|
+
else if ((d === 43 || d === 45) && /[eE]$/.test(src.slice(start, i)) && !/^0[xX]/.test(word)) i++;
|
|
327
|
+
else break;
|
|
328
|
+
}
|
|
329
|
+
last = "value";
|
|
330
|
+
regexOk = false;
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
// After a `.` it is a property name, never a keyword: `Symbol.for(x)`, `it.return()`.
|
|
334
|
+
const property = last === "punct" && lastPunct === ".";
|
|
335
|
+
lastWord = property ? "" : word;
|
|
336
|
+
last = "word";
|
|
337
|
+
regexOk = !property && BEFORE_EXPRESSION.has(word);
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (c === 46 && src.charCodeAt(i + 1) >= 48 && src.charCodeAt(i + 1) <= 57) {
|
|
342
|
+
i++;
|
|
343
|
+
while (i < n && (isIdentPart(src.charCodeAt(i)) || src.charCodeAt(i) === 46)) i++;
|
|
344
|
+
last = "value";
|
|
345
|
+
regexOk = false;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if ((c === 43 || c === 45) && src.charCodeAt(i + 1) === c) {
|
|
350
|
+
i += 2;
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
let p = src[i]!;
|
|
355
|
+
if (c === 61 && src.charCodeAt(i + 1) === 62) p = "=>";
|
|
356
|
+
i += p.length;
|
|
357
|
+
last = "punct";
|
|
358
|
+
lastPunct = p;
|
|
359
|
+
regexOk = true;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (parens.length || braces.length) {
|
|
363
|
+
return { code: src, escaped: 0, kept: 0, failed: `${parens.length} ( / [ and ${braces.length} { left open` };
|
|
364
|
+
}
|
|
365
|
+
if (copied < n) out.push(src.slice(copied));
|
|
366
|
+
return { code: out.join(""), escaped, kept };
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Checks an escaped bundle against the original with Bun's parser: `null` when both are the same
|
|
371
|
+
* program, otherwise why not. Regexes and tagged templates print as written, so those are compared
|
|
372
|
+
* after both prints go through [`asciiOnly`].
|
|
373
|
+
*/
|
|
374
|
+
export function checkAscii(before: string, after: string): string | null {
|
|
375
|
+
try {
|
|
376
|
+
new Function(after);
|
|
377
|
+
} catch (e) {
|
|
378
|
+
return `the escaped bundle does not parse (${e instanceof Error ? e.message : String(e)})`;
|
|
379
|
+
}
|
|
380
|
+
const t = new Bun.Transpiler({ loader: "js" });
|
|
381
|
+
const a = t.transformSync(before);
|
|
382
|
+
const b = t.transformSync(after);
|
|
383
|
+
if (a === b) return null;
|
|
384
|
+
const na = asciiOnly(a);
|
|
385
|
+
const nb = asciiOnly(b);
|
|
386
|
+
if (na.failed || nb.failed) return `the re-printed bundle did not lex (${na.failed ?? nb.failed})`;
|
|
387
|
+
if (na.code === nb.code) return null;
|
|
388
|
+
let at = 0;
|
|
389
|
+
while (at < na.code.length && na.code[at] === nb.code[at]) at++;
|
|
390
|
+
return `the escaped bundle reads differently near ${JSON.stringify(na.code.slice(Math.max(0, at - 40), at + 40))}`;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function nonAsciiCount(s: string): number {
|
|
394
|
+
let count = 0;
|
|
395
|
+
for (let i = 0; i < s.length; i++) if (s.charCodeAt(i) > 0x7f) count++;
|
|
396
|
+
return count;
|
|
397
|
+
}
|