-
Notifications
You must be signed in to change notification settings - Fork 48
/
compiler.ts
915 lines (766 loc) · 31 KB
/
compiler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
import { Buffer } from "buffer";
import * as crosspath from "@frida/crosspath";
import EventEmitter from "events";
import process from "process";
import { check as checkIdentifier } from "@frida/reserved-words";
import { minify, MinifyOptions, SourceMapOptions } from "@frida/terser";
import TypedEmitter from "typed-emitter";
import ts from "../ext/typescript.js";
const compilerRoot = detectCompilerRoot();
const sourceTransformers: ts.CustomTransformers = {
after: [
useStrictRemovalTransformer(),
]
};
export function build(options: BuildOptions): string {
options = normalizeOptions(options);
const entrypoint = deriveEntrypoint(options);
const outputOptions = makeOutputOptions(options);
const { projectRoot, assets, system, onDiagnostic } = options;
const compilerOpts = makeCompilerOptions(projectRoot, system, outputOptions);
const compilerHost = ts.createIncrementalCompilerHost(compilerOpts, system);
options.onCompilerHostCreated?.(compilerHost);
const program = ts.createProgram({
rootNames: [entrypoint.input],
options: compilerOpts,
host: compilerHost
});
const preEmitDiagnostics = ts.getPreEmitDiagnostics(program);
if (onDiagnostic !== undefined) {
for (const diagnostic of preEmitDiagnostics) {
onDiagnostic(diagnostic);
}
}
if (preEmitDiagnostics.some(({ category }) => category === ts.DiagnosticCategory.Error)) {
throw new Error("compilation failed");
}
const bundler = createBundler(entrypoint, projectRoot, assets, system, outputOptions);
const emitResult = program.emit(undefined, undefined, undefined, undefined, sourceTransformers);
if (onDiagnostic !== undefined) {
for (const diagnostic of emitResult.diagnostics) {
onDiagnostic(diagnostic);
}
}
if (emitResult.emitSkipped || emitResult.diagnostics.some(({ category }) => category === ts.DiagnosticCategory.Error)) {
throw new Error("compilation failed");
}
return bundler.bundle(program);
}
export function watch(options: WatchOptions): TypedEmitter<WatcherEvents> {
options = normalizeOptions(options);
const entrypoint = deriveEntrypoint(options);
const outputOptions = makeOutputOptions(options);
const { projectRoot, assets, system, onDiagnostic } = options;
const events = new EventEmitter() as TypedEmitter<WatcherEvents>;
const origCreateProgram: any = ts.createEmitAndSemanticDiagnosticsBuilderProgram;
const createProgram: ts.CreateProgram<ts.EmitAndSemanticDiagnosticsBuilderProgram> = (...args: any[]): ts.EmitAndSemanticDiagnosticsBuilderProgram => {
events.emit("compilationStarting");
const program: ts.EmitAndSemanticDiagnosticsBuilderProgram = origCreateProgram(...args);
if (onDiagnostic !== undefined) {
const preEmitDiagnostics = ts.getPreEmitDiagnostics(program.getProgram());
for (const diagnostic of preEmitDiagnostics) {
onDiagnostic(diagnostic);
}
}
const origEmit = program.emit;
program.emit = (targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers) => {
const emitResult = origEmit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, sourceTransformers);
if (onDiagnostic !== undefined) {
for (const diagnostic of emitResult.diagnostics) {
onDiagnostic(diagnostic);
}
}
return emitResult;
};
return program;
};
const compilerOpts = makeCompilerOptions(projectRoot, system, outputOptions);
const compilerHost = ts.createWatchCompilerHost([entrypoint.input], compilerOpts, system, createProgram);
options.onWatchCompilerHostCreated?.(compilerHost);
let state: "dirty" | "clean" = "dirty";
let timer: NodeJS.Timeout | null = null;
const bundler = createBundler(entrypoint, projectRoot, assets, system, outputOptions);
bundler.events.on("externalSourceFileAdded", file => {
compilerHost.watchFile(file.fileName, () => {
state = "dirty";
bundler.invalidate(file.fileName);
if (timer !== null) {
return;
}
timer = setTimeout(() => {
timer = null;
rebundle();
}, 250);
});
});
const origPostProgramCreate = compilerHost.afterProgramCreate!;
compilerHost.afterProgramCreate = program => {
origPostProgramCreate(program);
process.nextTick(rebundle);
};
let watchProgram: ts.WatchOfFilesAndCompilerOptions<ts.EmitAndSemanticDiagnosticsBuilderProgram>;
process.nextTick(() => {
watchProgram = ts.createWatchProgram(compilerHost);
});
let previousBundle: string | null = null;
function rebundle(): void {
state = "clean";
try {
const bundle = bundler.bundle(watchProgram.getProgram().getProgram());
if (bundle !== previousBundle) {
events.emit("bundleUpdated", bundle);
previousBundle = bundle;
}
} catch (e) {
console.error("Failed to bundle:", e);
}
events.emit("compilationFinished");
}
return events;
}
export interface Options {
projectRoot: string;
entrypoint: string;
assets: Assets;
system: ts.System;
sourceMaps?: SourceMaps;
compression?: Compression;
onDiagnostic?(diagnostic: ts.Diagnostic): void;
}
export interface BuildOptions extends Options {
onCompilerHostCreated?(compilerHost: ts.CompilerHost): void;
}
export interface WatchOptions extends Options {
onWatchCompilerHostCreated?(compilerHost: ts.WatchCompilerHostOfFilesAndCompilerOptions<ts.EmitAndSemanticDiagnosticsBuilderProgram>): void;
}
export type SourceMaps = "included" | "omitted";
export type Compression = "none" | "terser";
export interface Assets {
projectNodeModulesDir: string;
compilerNodeModulesDir: string;
shimDir: string;
shims: Map<string, string>;
}
export type WatcherEvents = {
compilationStarting: () => void,
compilationFinished: () => void,
bundleUpdated: (bundle: string) => void,
};
interface EntrypointName {
input: string;
output: string;
}
interface OutputOptions {
sourceMaps: SourceMaps;
compression: Compression;
}
type ModuleType = "cjs" | "esm";
interface JSModule {
type: ModuleType;
path: string;
file: ts.SourceFile;
aliases: Set<string>;
}
interface ModuleReference {
name: string;
referrer: JSModule;
}
function normalizeOptions<T extends Options>(options: T): T {
return Object.assign({}, options, {
projectRoot: crosspath.ensurePosix(options.projectRoot),
entrypoint: crosspath.ensurePosix(options.entrypoint),
});
}
function deriveEntrypoint(options: Options): EntrypointName {
const { projectRoot, entrypoint } = options;
const input = crosspath.isAbsolute(entrypoint) ? entrypoint : crosspath.join(projectRoot, entrypoint);
if (!input.startsWith(projectRoot)) {
throw new Error("entrypoint must be inside the project root");
}
let output = input.substring(projectRoot.length);
if (output.endsWith(".ts")) {
output = output.substring(0, output.length - 2) + "js";
}
return { input, output };
}
function makeOutputOptions(options: Options): OutputOptions {
const {
sourceMaps = "included",
compression = "none",
} = options;
return { sourceMaps, compression };
}
export function queryDefaultAssets(projectRoot: string, sys: ts.System): Assets {
const projectNodeModulesDir = crosspath.join(crosspath.ensurePosix(projectRoot), "node_modules");
const compilerNodeModulesDir = crosspath.join(compilerRoot, "node_modules");
let shimDir: string;
if (sys.directoryExists(crosspath.join(compilerNodeModulesDir, "@frida"))) {
shimDir = compilerNodeModulesDir;
} else if (sys.directoryExists(crosspath.join(projectNodeModulesDir, "@frida"))) {
shimDir = projectNodeModulesDir;
} else {
const compilerParent = crosspath.dirname(compilerRoot);
if (crosspath.basename(compilerParent) === "node_modules" &&
sys.directoryExists(crosspath.join(compilerParent, "@frida"))) {
shimDir = compilerParent;
} else {
throw new Error("Unable to detect shim directory; please file a bug");
}
}
const shims = new Map([
["assert", crosspath.join(shimDir, "@frida", "assert")],
["base64-js", crosspath.join(shimDir, "@frida", "base64-js")],
["buffer", crosspath.join(shimDir, "@frida", "buffer")],
["crypto", crosspath.join(shimDir, "@frida", "crypto")],
["diagnostics_channel", crosspath.join(shimDir, "@frida", "diagnostics_channel")],
["events", crosspath.join(shimDir, "@frida", "events")],
["fs", crosspath.join(shimDir, "frida-fs")],
["http", crosspath.join(shimDir, "@frida", "http")],
["https", crosspath.join(shimDir, "@frida", "https")],
["http-parser-js", crosspath.join(shimDir, "@frida", "http-parser-js")],
["ieee754", crosspath.join(shimDir, "@frida", "ieee754")],
["net", crosspath.join(shimDir, "@frida", "net")],
["os", crosspath.join(shimDir, "@frida", "os")],
["path", crosspath.join(shimDir, "@frida", "path")],
["process", crosspath.join(shimDir, "@frida", "process")],
["punycode", crosspath.join(shimDir, "@frida", "punycode")],
["querystring", crosspath.join(shimDir, "@frida", "querystring")],
["readable-stream", crosspath.join(shimDir, "@frida", "readable-stream")],
["stream", crosspath.join(shimDir, "@frida", "stream")],
["string_decoder", crosspath.join(shimDir, "@frida", "string_decoder")],
["timers", crosspath.join(shimDir, "@frida", "timers")],
["tty", crosspath.join(shimDir, "@frida", "tty")],
["url", crosspath.join(shimDir, "@frida", "url")],
["util", crosspath.join(shimDir, "@frida", "util")],
["vm", crosspath.join(shimDir, "@frida", "vm")],
]);
const nodeShimNames = [
"assert",
"buffer",
"crypto",
"diagnostics_channel",
"events",
"fs",
"http",
"https",
"net",
"os",
"path",
"process",
"punycode",
"querystring",
"stream",
"string_decoder",
"timers",
"tty",
"url",
"util",
"vm",
];
for (const name of nodeShimNames) {
const path = shims.get(name)!;
shims.set("node:" + name, path);
}
return {
projectNodeModulesDir,
compilerNodeModulesDir,
shimDir,
shims,
};
}
function makeCompilerOptions(projectRoot: string, system: ts.System, options: OutputOptions): ts.CompilerOptions {
const defaultTsOptions = makeDefaultCompilerOptions();
const softOptionNames = ["target", "lib", "strict"];
const fixedTsOptions = Object.assign({}, defaultTsOptions);
for (const name of softOptionNames) {
delete fixedTsOptions[name];
}
let opts: ts.CompilerOptions;
const configFileHost = new FridaConfigFileHost(projectRoot, system);
const userOpts = ts.getParsedCommandLineOfConfigFile(crosspath.join(projectRoot, "tsconfig.json"), fixedTsOptions, configFileHost)?.options;
if (userOpts !== undefined) {
for (const name of softOptionNames) {
const val = userOpts[name];
if (val === undefined) {
userOpts[name] = defaultTsOptions[name];
}
}
delete userOpts.noEmit;
opts = userOpts;
} else {
opts = defaultTsOptions;
}
opts.rootDir = projectRoot;
opts.outDir = "/";
if (options.sourceMaps === "included") {
opts.sourceRoot = projectRoot;
opts.sourceMap = true;
opts.inlineSourceMap = false;
}
return opts;
}
export function makeDefaultCompilerOptions(): ts.CompilerOptions {
return {
target: ts.ScriptTarget.ES2020,
lib: ["lib.es2020.d.ts"],
module: ts.ModuleKind.ES2020,
moduleResolution: ts.ModuleResolutionKind.Node16,
allowSyntheticDefaultImports: true,
resolveJsonModule: true,
allowJs: true,
strict: true
};
}
function createBundler(entrypoint: EntrypointName, projectRoot: string, assets: Assets, system: ts.System, options: OutputOptions): Bundler {
const {
sourceMaps,
compression,
} = options;
const events = new EventEmitter() as TypedEmitter<BundlerEvents>;
const output = new Map<string, string>();
const pendingModules: ModuleReference[] = [];
const processedModules = new Set<string>();
const jsonFilePaths = new Set<string>();
const modules = new Map<string, JSModule>();
const externalSources = new Map<string, ts.SourceFile>();
system.writeFile = (path, data, writeByteOrderMark) => {
output.set(path, data);
};
function markAllProgramSourcesAsProcessed(program: ts.Program): void {
for (const sf of program.getSourceFiles()) {
if (!sf.isDeclarationFile) {
const outPath = changeFileExtension(sf.fileName, "js");
processedModules.add(outPath);
}
}
}
function getExternalSourceFile(path: string): ts.SourceFile {
let file = externalSources.get(path);
if (file !== undefined) {
return file;
}
const sourceText = system.readFile(path, "utf-8");
if (sourceText === undefined) {
throw new Error(`unable to open ${path}`);
}
file = ts.createSourceFile(path, sourceText, ts.ScriptTarget.ES2020, true, ts.ScriptKind.JS);
externalSources.set(path, file);
events.emit("externalSourceFileAdded", file);
return file;
}
function assetNameFromFilePath(path: string): string {
if (path.startsWith(compilerRoot)) {
return path.substring(compilerRoot.length);
}
if (path.startsWith(projectRoot)) {
return path.substring(projectRoot.length);
}
throw new Error(`unexpected file path: ${path}`);
}
return {
events,
bundle(program: ts.Program): string {
markAllProgramSourcesAsProcessed(program);
for (const sf of program.getSourceFiles()) {
if (!sf.isDeclarationFile) {
const { fileName } = sf;
const path = changeFileExtension(fileName, "js");
const mod: JSModule = {
type: "esm",
path,
file: sf,
aliases: new Set<string>(),
};
modules.set(assetNameFromFilePath(path), mod);
processJSModule(mod, processedModules, pendingModules, jsonFilePaths);
}
}
const missing = new Set<string>();
let ref: ModuleReference | undefined;
while ((ref = pendingModules.shift()) !== undefined) {
const refName = ref.name;
processedModules.add(ref.name);
let resolveRes: ResolveModuleReferenceResult;
try {
resolveRes = resolveModuleReference(ref, assets, system);
} catch (e) {
missing.add(refName);
continue;
}
const [modPath, needsAlias] = resolveRes;
const assetName = assetNameFromFilePath(modPath);
let mod = modules.get(assetName);
if (mod === undefined) {
const sourceFile = getExternalSourceFile(modPath);
mod = {
type: detectModuleType(modPath, system),
path: modPath,
file: sourceFile,
aliases: new Set<string>(),
};
output.set(assetName, sourceFile.text);
modules.set(assetName, mod);
processedModules.add(modPath);
processJSModule(mod, processedModules, pendingModules, jsonFilePaths);
}
if (needsAlias) {
let alias: string;
if (crosspath.isAbsolute(refName)) {
alias = refName.substring(projectRoot.length);
} else {
alias = refName;
}
mod.aliases.add(alias);
}
}
if (missing.size > 0) {
throw new Error(`unable to resolve:\n\t${Array.from(missing).sort().join("\n\t")}`);
}
const legacyModules = Array.from(modules.values()).filter(m => m.type === "cjs").map(m => m.path).sort();
if (legacyModules.length > 0) {
throw new Error(`only able to bundle ECMAScript modules, detected CommonJS:\n\t${legacyModules.join("\n\t")}`);
}
for (const path of jsonFilePaths) {
const assetName = assetNameFromFilePath(path);
if (!output.has(assetName)) {
output.set(assetName, system.readFile(path)!);
}
}
for (const [name, data] of output) {
if (name.endsWith(".js")) {
let code = data;
const lines = code.split("\n");
const n = lines.length;
const lastLine = lines[n - 1];
const sourceMapToken = "//# sourceMappingURL=";
if (lastLine.startsWith(sourceMapToken)) {
const precedingLines = lines.slice(0, n - 1);
code = precedingLines.join("\n");
if (sourceMaps === "included") {
const inlinedSourceMapOrPath = lastLine.substring(sourceMapToken.length);
const dataUrlToken = "data:application/json;base64,";
const isInlined = inlinedSourceMapOrPath.startsWith(dataUrlToken);
const sourceMapPath = isInlined
? `${name}.map`
: crosspath.join(crosspath.dirname(name), inlinedSourceMapOrPath);
if (!output.has(sourceMapPath)) {
const content = isInlined
? system.base64decode?.(inlinedSourceMapOrPath.substring(dataUrlToken.length))
: system.readFile(`.${sourceMapPath}`);
if (content !== undefined) {
output.set(sourceMapPath, content);
}
}
}
}
if (compression === "terser") {
const mod = modules.get(name)!;
const originPath = mod.path;
const originFilename = crosspath.basename(originPath);
const minifySources: { [name: string]: string } = {};
minifySources[originFilename] = code;
const minifyOpts: MinifyOptions = {
ecma: 2020,
compress: {
module: true,
global_defs: {
"process.env.FRIDA_COMPILE": true
},
},
mangle: {
module: true,
},
};
const mapName = name + ".map";
if (sourceMaps === "included") {
const mapOpts: SourceMapOptions = {
asObject: true,
root: crosspath.dirname(originPath) + "/",
filename: name.substring(name.lastIndexOf("/") + 1),
} as SourceMapOptions;
const inputMap = output.get(mapName);
if (inputMap !== undefined) {
mapOpts.content = inputMap;
}
minifyOpts.sourceMap = mapOpts;
}
const result = minify(minifySources, minifyOpts);
code = result.code!;
if (sourceMaps === "included") {
const map = result.map as { [key: string]: any };
const prefixLength: number = map.sourceRoot.length;
map.sources = map.sources.map((s: string) => s.substring(prefixLength));
output.set(mapName, JSON.stringify(map));
}
}
output.set(name, code);
} else if (name.endsWith(".json")) {
output.set(name, jsonToModule(data));
}
}
const names: string[] = [];
const orderedNames = Array.from(output.keys());
orderedNames.sort();
const maps = new Set(orderedNames.filter(name => name.endsWith(".map")));
const entrypointNormalized = crosspath.normalize(entrypoint.output);
for (const name of orderedNames.filter(name => !name.endsWith(".map"))) {
let index = (crosspath.normalize(name) === entrypointNormalized) ? 0 : names.length;
const mapName = name + ".map";
if (maps.has(mapName)) {
names.splice(index, 0, mapName);
index++;
}
names.splice(index, 0, name);
}
const chunks: string[] = [];
chunks.push("📦\n")
for (const name of names) {
const rawData = Buffer.from(output.get(name)!);
chunks.push(`${rawData.length} ${name}\n`);
const mod = modules.get(name);
if (mod !== undefined) {
for (const alias of mod.aliases) {
chunks.push(`↻ ${alias}\n`)
}
}
}
chunks.push("✄\n");
let i = 0;
for (const name of names) {
if (i !== 0) {
chunks.push("\n✄\n");
}
const data = output.get(name)!;
chunks.push(data);
i++;
}
return chunks.join("");
},
invalidate(path: string): void {
output.delete(assetNameFromFilePath(path));
processedModules.clear();
externalSources.delete(path);
}
};
}
interface Bundler {
events: TypedEmitter<BundlerEvents>;
bundle(program: ts.Program): string;
invalidate(path: string): void;
}
type BundlerEvents = {
externalSourceFileAdded: (file: ts.SourceFile) => void,
};
function detectModuleType(modPath: string, sys: ts.System): ModuleType {
let curDir = crosspath.dirname(modPath);
while (true) {
const rawPkgMeta = sys.readFile(crosspath.join(curDir, "package.json"));
if (rawPkgMeta !== undefined) {
const pkgMeta = JSON.parse(rawPkgMeta);
if (pkgMeta.type === "module" || pkgMeta.module !== undefined) {
return "esm";
}
break;
}
const nextDir = crosspath.dirname(curDir);
if (nextDir === curDir) {
break;
}
curDir = nextDir;
}
return "cjs";
}
type ResolveModuleReferenceResult = [path: string, needsAlias: boolean];
function resolveModuleReference(ref: ModuleReference, assets: Assets, system: ts.System): ResolveModuleReferenceResult {
const refName = ref.name;
const requesterPath = ref.referrer.path;
let modPath: string;
let needsAlias = false;
if (crosspath.isAbsolute(refName)) {
modPath = refName;
} else {
const tokens = refName.split("/");
let pkgName: string;
let subPath: string[];
if (tokens[0].startsWith("@")) {
pkgName = tokens[0] + "/" + tokens[1];
subPath = tokens.slice(2);
} else {
pkgName = tokens[0];
subPath = tokens.slice(1);
}
const shimPath = assets.shims.get(pkgName);
if (shimPath !== undefined) {
if (shimPath.endsWith(".js")) {
modPath = shimPath;
} else {
modPath = crosspath.join(shimPath, ...subPath);
}
needsAlias = true;
} else {
const linkedCompilerRoot = crosspath.join(assets.projectNodeModulesDir, "frida-compile");
const {shimDir} = assets;
if (requesterPath.startsWith(compilerRoot) ||
requesterPath.startsWith(linkedCompilerRoot) ||
requesterPath.startsWith(shimDir)) {
modPath = crosspath.join(shimDir, ...tokens);
} else {
modPath = crosspath.join(assets.projectNodeModulesDir, ...tokens);
}
needsAlias = subPath.length > 0;
}
}
if (system.directoryExists(modPath)) {
const rawPkgMeta = system.readFile(crosspath.join(modPath, "package.json"));
if (rawPkgMeta !== undefined) {
const pkgMeta = JSON.parse(rawPkgMeta);
const pkgMain = pkgMeta.module ?? pkgMeta.main ?? "index.js";
let pkgEntrypoint = crosspath.join(modPath, pkgMain);
if (system.directoryExists(pkgEntrypoint)) {
pkgEntrypoint = crosspath.join(pkgEntrypoint, "index.js");
}
modPath = pkgEntrypoint;
needsAlias = true;
} else {
modPath = crosspath.join(modPath, "index.js");
}
}
if (!system.fileExists(modPath)) {
modPath += ".js";
if (!system.fileExists(modPath)) {
throw new Error("unable to resolve module");
}
}
return [modPath, needsAlias];
}
function processJSModule(mod: JSModule, processedModules: Set<string>, pendingModules: ModuleReference[], jsonFilePaths: Set<string>): void {
const moduleDir = crosspath.dirname(mod.path);
const isCJS = mod.type === "cjs";
ts.forEachChild(mod.file, visit);
function visit(node: ts.Node) {
if (ts.isImportDeclaration(node)) {
visitImportDeclaration(node);
} else if (ts.isExportDeclaration(node)) {
visitExportDeclaration(node);
} else if (isCJS && ts.isCallExpression(node)) {
visitCallExpression(node);
ts.forEachChild(node, visit);
} else {
ts.forEachChild(node, visit);
}
}
function visitImportDeclaration(imp: ts.ImportDeclaration) {
const depName = (imp.moduleSpecifier as ts.StringLiteral).text;
maybeAddModuleToPending(depName);
}
function visitExportDeclaration(exp: ts.ExportDeclaration) {
const specifier = exp.moduleSpecifier;
if (specifier === undefined) {
return;
}
const depName = (specifier as ts.StringLiteral).text;
maybeAddModuleToPending(depName);
}
function visitCallExpression(call: ts.CallExpression) {
const expr: ts.LeftHandSideExpression = call.expression;
if (!ts.isIdentifier(expr)) {
return;
}
if (expr.escapedText !== "require") {
return;
}
const args = call.arguments;
if (args.length !== 1) {
return;
}
const arg = args[0];
if (!ts.isStringLiteral(arg)) {
return;
}
const depName = arg.text;
maybeAddModuleToPending(depName);
}
function maybeAddModuleToPending(name: string) {
const ref = name.startsWith(".") ? crosspath.join(moduleDir, name) : name;
if (name.endsWith(".json")) {
jsonFilePaths.add(ref);
} else if (!processedModules.has(ref)) {
pendingModules.push({ name: ref, referrer: mod });
}
}
}
function useStrictRemovalTransformer(): ts.TransformerFactory<ts.SourceFile> {
return context => {
return sourceFile => {
const visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {
if (ts.isExpressionStatement(node)) {
const { expression } = node;
if (ts.isStringLiteral(expression) && expression.text === "use strict") {
return [];
}
}
return ts.visitEachChild(node, visitor, context);
};
return ts.visitNode(sourceFile, visitor) as ts.SourceFile;
};
};
}
function jsonToModule(json: string): string {
const result: string[] = [];
const data = JSON.parse(json);
if (typeof data === "object" && data !== null) {
const obj: [string, any] = data;
let identifier = "d";
let candidate = identifier;
let serial = 1;
while (obj.hasOwnProperty(candidate)) {
candidate = identifier + serial;
serial++;
}
identifier = candidate;
result.push(`const ${identifier} = ${json.trim()};`);
result.push(`export default ${identifier};`);
for (const member of Object.keys(data).filter(identifier => !checkIdentifier(identifier, "es2015", true))) {
result.push(`export const ${member} = ${identifier}.${member};`);
}
} else {
result.push(`export default ${json.trim()};`);
}
return result.join("\n");
}
class FridaConfigFileHost implements ts.ParseConfigFileHost {
useCaseSensitiveFileNames = true;
constructor(
private projectRoot: string,
private sys: ts.System,
) {
}
readDirectory(rootDir: string, extensions: readonly string[], excludes: readonly string[] | undefined, includes: readonly string[], depth?: number): readonly string[] {
return this.sys.readDirectory(rootDir, extensions, excludes, includes, depth);
}
fileExists(path: string): boolean {
return this.sys.fileExists(path);
}
readFile(path: string): string | undefined {
return this.sys.readFile(path);
}
trace?(s: string): void {
console.log(s);
}
getCurrentDirectory(): string {
return this.projectRoot;
}
onUnRecoverableConfigFileDiagnostic(diagnostic: ts.Diagnostic) {
}
}
function detectCompilerRoot(): string {
if (process.env.FRIDA_COMPILE !== undefined) {
return "/frida-compile";
} else {
return crosspath.dirname(crosspath.dirname(crosspath.urlToFilename(import.meta.url)));
}
}
function changeFileExtension(path: string, ext: string): string {
const pathWithoutExtension = path.substring(0, path.lastIndexOf("."));
return pathWithoutExtension + "." + ext;
}