-
-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathvite.extension.shared.ts
More file actions
1046 lines (926 loc) · 28.3 KB
/
vite.extension.shared.ts
File metadata and controls
1046 lines (926 loc) · 28.3 KB
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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import fs from "node:fs/promises";
import path from "node:path";
import { spawn } from "node:child_process";
import { fileURLToPath } from "node:url";
import { build as viteBuild } from "vite";
import { COMPRESSION_LEVEL, zip } from "zip-a-folder";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const rootDir = __dirname;
export const srcDir = path.join(rootDir, "src");
export const outBase = path.join(rootDir, "dist-ext");
export const outTmp = path.join(outBase, "_tmp");
const DEFAULT_EXTENSION_NAME = "Voice Over Translation";
const DEFAULT_EXTENSION_DESCRIPTION = "Voice Over Translation";
const DEFAULT_EXTENSION_VERSION = "0.0.0";
const EXTENSION_ICON_SIZES = [16, 32, 48, 64, 96, 128, 256] as const;
const EXTENSION_ASSET_FILES = [
"bridge.js",
"prelude.js",
"content.js",
"content.css",
] as const;
const GITHUB_DIST_EXT_RAW_BASE =
"https://raw.githubusercontent.com/ilyhalight/voice-over-translation/master/dist-ext";
const CHROME_UPDATES_MANIFEST_FILE = "vot-extension-chrome-updates.xml";
const FIREFOX_UPDATES_MANIFEST_FILE = "vot-extension-firefox-updates.json";
const FIREFOX_UPDATES_MANIFEST_URL = `${GITHUB_DIST_EXT_RAW_BASE}/${FIREFOX_UPDATES_MANIFEST_FILE}`;
export type ExtensionBuildTarget = "chrome" | "firefox" | "all";
export interface ExtensionHeaders {
name?: string;
description?: string;
version?: string;
author?: string;
match?: string[];
exclude?: string[];
connect?: string[];
}
export interface ExtensionBuildContext {
availableLocales: string[];
repoBranch: string;
}
interface ExtensionEntry {
entry: string;
format: "iife" | "es";
fileName: string;
emptyOutDir: boolean;
}
const extensionEntries: ExtensionEntry[] = [
{
entry: "src/index.ts",
format: "iife",
fileName: "content.js",
emptyOutDir: true,
},
{
entry: "src/extension/prelude.ts",
format: "iife",
fileName: "prelude.js",
emptyOutDir: false,
},
{
entry: "src/extension/bridge.ts",
format: "iife",
fileName: "bridge.js",
emptyOutDir: false,
},
{
entry: "src/extension/background.ts",
format: "es",
fileName: "background.js",
emptyOutDir: false,
},
{
entry: "src/extension/background.ts",
format: "iife",
fileName: "background-ff.js",
emptyOutDir: false,
},
];
export async function exists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
}
function parseNumericVersionParts(version: string): number[] | null {
const parts = String(version)
.trim()
.split(".")
.map((part) => part.trim());
if (!parts.length || parts.some((part) => !/^\d+$/.test(part))) {
return null;
}
return parts.map(Number);
}
function compareVersions(left: string, right: string): number {
const leftNumeric = parseNumericVersionParts(left);
const rightNumeric = parseNumericVersionParts(right);
if (leftNumeric && rightNumeric) {
const maxLength = Math.max(leftNumeric.length, rightNumeric.length);
for (let index = 0; index < maxLength; index += 1) {
const leftPart = leftNumeric[index] ?? 0;
const rightPart = rightNumeric[index] ?? 0;
if (leftPart > rightPart) return 1;
if (leftPart < rightPart) return -1;
}
return 0;
}
return left.localeCompare(right, undefined, {
numeric: true,
sensitivity: "base",
});
}
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
async function cleanupOlderVersionedArtifacts({
artifactPrefix,
fileExtension,
currentVersion,
}: {
artifactPrefix: string;
fileExtension: string;
currentVersion: string;
}): Promise<string[]> {
const removedFiles: string[] = [];
const versionedArtifactPattern = new RegExp(
`^${escapeRegExp(artifactPrefix)}-(.+)${escapeRegExp(fileExtension)}$`,
);
let entries: string[] = [];
try {
entries = await fs.readdir(outBase);
} catch {
return removedFiles;
}
for (const entry of entries) {
const match = versionedArtifactPattern.exec(entry);
if (!match) continue;
const artifactVersion = match[1];
if (compareVersions(artifactVersion, currentVersion) >= 0) continue;
await fs.rm(path.join(outBase, entry), { force: true });
removedFiles.push(entry);
}
return removedFiles;
}
export async function readJson<T>(filePath: string): Promise<T> {
return JSON.parse(await fs.readFile(filePath, "utf8")) as T;
}
export async function ensureCleanDir(dir: string): Promise<void> {
await fs.rm(dir, { recursive: true, force: true });
await fs.mkdir(dir, { recursive: true });
}
export async function getLocaleCodes(): Promise<string[]> {
const localesDir = path.join(srcDir, "localization", "locales");
const entries = await fs.readdir(localesDir, { withFileTypes: true });
const codes = entries
.filter((entry) => entry.isFile() && entry.name.endsWith(".json"))
.map((entry) => entry.name.replace(/\.json$/i, ""))
.sort((a, b) => a.localeCompare(b));
const priority = ["auto", "en", "ru"];
const normalized = [
...priority.filter((item) => item === "auto" || codes.includes(item)),
...codes.filter((code) => !priority.includes(code)),
];
return [...new Set(normalized)];
}
export function getRepoBranch(): string {
return process.env.GITHUB_REF_NAME || process.env.REPO_BRANCH || "master";
}
export async function createExtensionBuildContext(): Promise<ExtensionBuildContext> {
return {
availableLocales: await getLocaleCodes(),
repoBranch: getRepoBranch(),
};
}
export async function getExtensionHeaders(): Promise<ExtensionHeaders> {
return readJson<ExtensionHeaders>(path.join(srcDir, "headers.json"));
}
async function buildEntry({
entry,
format,
fileName,
emptyOutDir,
define,
}: {
entry: string;
format: "iife" | "es";
fileName: string;
emptyOutDir: boolean;
define: Record<string, string>;
}): Promise<void> {
await viteBuild({
root: rootDir,
configFile: false,
define,
css: {
transformer: "lightningcss",
},
build: {
target: "es2020",
outDir: outTmp,
emptyOutDir,
sourcemap: false,
minify: "esbuild",
lib: {
entry: path.join(rootDir, entry),
name: "VOT",
formats: [format],
fileName: () => fileName,
},
rollupOptions: {
output: {
inlineDynamicImports: true,
},
},
},
});
}
export async function buildExtensionBundles({
context,
headers,
}: {
context: ExtensionBuildContext;
headers: ExtensionHeaders;
}): Promise<void> {
const defineMeta = {
DEBUG_MODE: "false",
IS_EXTENSION: "true",
AVAILABLE_LOCALES: JSON.stringify(context.availableLocales),
REPO_BRANCH: JSON.stringify(context.repoBranch),
VOT_VERSION: JSON.stringify(String(headers.version || "")),
VOT_AUTHORS: JSON.stringify(String(headers.author || "")),
};
await ensureCleanDir(outTmp);
for (const entry of extensionEntries) {
await buildEntry({
...entry,
define: defineMeta,
});
}
}
async function renameContentCss(): Promise<void> {
const entries = await fs.readdir(outTmp);
const cssFiles = entries.filter((entry) => entry.endsWith(".css"));
if (!cssFiles.length) return;
if (cssFiles.includes("content.css")) return;
const from = path.join(outTmp, cssFiles[0]);
const to = path.join(outTmp, "content.css");
await fs.rename(from, to);
}
async function copyExtensionFiles(
targetDir: string,
{
backgroundSrc = "background.js",
backgroundDst = "background.js",
}: { backgroundSrc?: string; backgroundDst?: string } = {},
): Promise<void> {
await fs.mkdir(targetDir, { recursive: true });
for (const fileName of EXTENSION_ASSET_FILES) {
await fs.copyFile(path.join(outTmp, fileName), path.join(targetDir, fileName));
}
await fs.copyFile(
path.join(outTmp, backgroundSrc),
path.join(targetDir, backgroundDst),
);
const iconsSrcDir = path.join(srcDir, "extension", "icons");
const iconsDstDir = path.join(targetDir, "icons");
await fs.mkdir(iconsDstDir, { recursive: true });
const iconFiles = await fs.readdir(iconsSrcDir);
for (const iconFile of iconFiles) {
await fs.copyFile(
path.join(iconsSrcDir, iconFile),
path.join(iconsDstDir, iconFile),
);
}
}
function normalizeHostPermission(entry: string | undefined | null): string | null {
if (!entry) return null;
const value = String(entry).trim();
if (!value) return null;
if (value === "<all_urls>") return value;
if (/^[a-z*]+:\/\//i.test(value)) {
if (/^[a-z*]+:\/\/[^/]+\/?$/i.test(value)) {
return `${value.replace(/\/?$/, "")}/*`;
}
return value;
}
return `*://*.${value}/*`;
}
function normalizeHostPermissions(list: string[] = []): string[] {
const normalized = list
.map((item) => normalizeHostPermission(item))
.filter((value): value is string => Boolean(value));
return [...new Set(normalized)];
}
function splitMatchesForOriginFallback(matches: string[] = []): {
originFallbackMatches: string[];
directMatches: string[];
} {
const originFallbackMatches: string[] = [];
const directMatches: string[] = [];
const getPath = (pattern: string): string | null => {
if (pattern === "<all_urls>") return "/*";
const result = /^[^:]+:\/\/[^/]+(\/.*)$/.exec(pattern);
return result ? result[1] : null;
};
for (const match of matches) {
const pathPart = getPath(String(match));
if (pathPart === "/*") originFallbackMatches.push(String(match));
else directMatches.push(String(match));
}
return { originFallbackMatches, directMatches };
}
function buildIconsMap(sizes: readonly number[]): Record<number, string> {
return Object.fromEntries(
sizes.map((size) => [size, `icons/icon-${size}.png`]),
) as Record<number, string>;
}
function createContentScriptEntries({
matches,
excludeMatches,
includeWorld,
matchOriginAsFallback = false,
}: {
matches: string[];
excludeMatches: string[];
includeWorld: boolean;
matchOriginAsFallback?: boolean;
}): Record<string, unknown>[] {
if (!matches.length) return [];
const fallbackConfig = matchOriginAsFallback
? { match_origin_as_fallback: true }
: {};
return [
{
matches,
exclude_matches: excludeMatches,
js: ["bridge.js"],
all_frames: true,
match_about_blank: true,
run_at: "document_start",
...fallbackConfig,
},
{
matches,
exclude_matches: excludeMatches,
js: ["prelude.js", "content.js"],
css: ["content.css"],
all_frames: true,
match_about_blank: true,
run_at: "document_idle",
...(includeWorld ? { world: "MAIN" } : {}),
...fallbackConfig,
},
];
}
function buildManifestChrome({
headers,
includeWorld,
}: {
headers: ExtensionHeaders;
includeWorld: boolean;
}): Record<string, unknown> {
const name = headers.name || DEFAULT_EXTENSION_NAME;
const description = headers.description || DEFAULT_EXTENSION_DESCRIPTION;
const version = headers.version || DEFAULT_EXTENSION_VERSION;
const matches = headers.match || [];
const excludeMatches = headers.exclude || [];
const { originFallbackMatches, directMatches } =
splitMatchesForOriginFallback(matches);
const hostPermissions = normalizeHostPermissions(headers.connect || []);
const contentScripts = [
...createContentScriptEntries({
matches: originFallbackMatches,
excludeMatches,
includeWorld,
matchOriginAsFallback: true,
}),
...createContentScriptEntries({
matches: directMatches,
excludeMatches,
includeWorld,
}),
];
return {
manifest_version: 3,
name,
description,
version,
action: {
default_title: name,
default_icon: buildIconsMap([16, 32]),
},
permissions: [
"storage",
"notifications",
"tabs",
"declarativeNetRequestWithHostAccess",
],
host_permissions: hostPermissions,
background: {
service_worker: "background.js",
type: "module",
},
icons: buildIconsMap(EXTENSION_ICON_SIZES),
content_scripts: contentScripts,
};
}
function getFirefoxAddonId(): string {
return (
process.env.FIREFOX_ADDON_ID ||
process.env.GECKO_ID ||
"vot-extension@firefox"
);
}
function getFirefoxStrictMinVersion(): string {
return process.env.FIREFOX_STRICT_MIN_VERSION || "140.0";
}
function compareFirefoxVersions(left: string, right: string): number {
const leftParts = left.split(".").map((part) => Number.parseInt(part, 10) || 0);
const rightParts = right
.split(".")
.map((part) => Number.parseInt(part, 10) || 0);
const length = Math.max(leftParts.length, rightParts.length);
for (let index = 0; index < length; index += 1) {
const diff = (leftParts[index] || 0) - (rightParts[index] || 0);
if (diff !== 0) {
return diff;
}
}
return 0;
}
function getFirefoxAndroidSettings(): Record<string, string> {
const strictMinVersion = process.env.FIREFOX_ANDROID_STRICT_MIN_VERSION?.trim();
const strictMaxVersion = process.env.FIREFOX_ANDROID_STRICT_MAX_VERSION?.trim();
return {
...(strictMinVersion ? { strict_min_version: strictMinVersion } : {}),
...(strictMaxVersion ? { strict_max_version: strictMaxVersion } : {}),
};
}
function getFirefoxDataCollectionPermissions(): {
required: string[];
optional?: string[];
} {
const requiredRaw = process.env.FIREFOX_DATA_COLLECTION_REQUIRED;
const optionalRaw = process.env.FIREFOX_DATA_COLLECTION_OPTIONAL;
const parseList = (raw: string): string[] =>
String(raw)
.split(",")
.map((entry) => entry.trim())
.filter(Boolean);
const required = requiredRaw ? parseList(requiredRaw) : ["none"];
const optional = optionalRaw ? parseList(optionalRaw) : null;
return {
required,
...(optional && optional.length ? { optional } : {}),
};
}
function getFirefoxAndroidSettingsForDataCollectionPermissions(): Record<
string,
string
> {
const androidSettings = getFirefoxAndroidSettings();
const androidMinVersion = androidSettings.strict_min_version?.trim();
const requiredAndroidMinVersion = "142.0";
if (
!androidMinVersion ||
compareFirefoxVersions(androidMinVersion, requiredAndroidMinVersion) < 0
) {
return {
...androidSettings,
strict_min_version: requiredAndroidMinVersion,
};
}
return androidSettings;
}
function getFirefoxXpiRawUrl(version: string): string {
return `${GITHUB_DIST_EXT_RAW_BASE}/vot-extension-firefox-${version}.xpi`;
}
function buildManifestFirefox({
headers,
includeWorld,
}: {
headers: ExtensionHeaders;
includeWorld: boolean;
}): Record<string, unknown> {
const manifest = buildManifestChrome({ headers, includeWorld });
const dataCollectionPermissions = getFirefoxDataCollectionPermissions();
const action = manifest.action as Record<string, unknown> | undefined;
const defaultIcon = action?.default_icon as Record<string, unknown> | undefined;
if (defaultIcon) {
defaultIcon[64] = "icons/icon-64.png";
}
delete manifest.update_url;
delete manifest.background;
manifest.background = {
scripts: ["background.js"],
};
manifest.browser_specific_settings = {
gecko: {
id: getFirefoxAddonId(),
update_url: FIREFOX_UPDATES_MANIFEST_URL,
strict_min_version: getFirefoxStrictMinVersion(),
data_collection_permissions: dataCollectionPermissions,
},
gecko_android:
dataCollectionPermissions.required.length ||
dataCollectionPermissions.optional?.length
? getFirefoxAndroidSettingsForDataCollectionPermissions()
: getFirefoxAndroidSettings(),
};
return manifest;
}
async function writeManifest(
targetDir: string,
manifest: Record<string, unknown>,
): Promise<void> {
await fs.writeFile(
path.join(targetDir, "manifest.json"),
JSON.stringify(manifest, null, 3),
"utf8",
);
}
async function writeFirefoxUpdatesManifest({
version,
addonId,
}: {
version: string;
addonId: string;
}): Promise<string> {
const updatesManifestPath = path.join(outBase, FIREFOX_UPDATES_MANIFEST_FILE);
const updatesManifest = {
addons: {
[addonId]: {
updates: [
{
version,
update_link: getFirefoxXpiRawUrl(version),
},
],
},
},
};
await fs.writeFile(
updatesManifestPath,
JSON.stringify(updatesManifest, null, 3),
"utf8",
);
return updatesManifestPath;
}
async function zipDir(sourceDirPath: string, outZipPath: string): Promise<void> {
await fs.rm(outZipPath, { force: true });
await fs.mkdir(path.dirname(outZipPath), { recursive: true });
await zip(sourceDirPath, outZipPath, {
compression: COMPRESSION_LEVEL.high,
zlib: { level: 9 },
});
}
function getCrx3Bin(): string {
return path.join(
rootDir,
"node_modules",
".bin",
process.platform === "win32" ? "crx3.cmd" : "crx3",
);
}
async function runCmd(cmd: string, args: string[], cwd?: string): Promise<void> {
await new Promise<void>((resolve, reject) => {
const child = spawn(cmd, args, {
cwd,
stdio: "inherit",
shell: process.platform === "win32",
env: process.env,
});
child.on("error", reject);
child.on("close", (code) => {
if (code === 0) resolve();
else reject(new Error(`${cmd} exited with code ${code}`));
});
});
}
async function maybeBuildCrx({
sourceDir,
version,
}: {
sourceDir: string;
version: string;
}): Promise<{ crxPath: string }> {
const customKeyPath = process.env.CHROME_CRX_KEY_PATH?.trim() || null;
const useTemporaryKey = !customKeyPath;
const keyPath = path.resolve(
rootDir,
customKeyPath || path.join(outTmp, "vot-extension-chrome.pem"),
);
await fs.mkdir(path.dirname(keyPath), { recursive: true });
const crx3Bin = getCrx3Bin();
if (!(await exists(crx3Bin))) {
throw new Error(`CRX builder not found: ${crx3Bin}. Install dependencies first.`);
}
const outCrx = path.join(outBase, `vot-extension-chrome-${version}.zip`);
try {
await runCmd(
crx3Bin,
[
"-p",
keyPath,
"-o",
outCrx,
"--appVersion",
version,
sourceDir,
],
rootDir,
);
} finally {
if (useTemporaryKey) {
await fs.rm(keyPath, { force: true });
await fs.rm(path.join(outBase, "vot-extension-chrome.pem"), { force: true });
}
}
return {
crxPath: outCrx,
};
}
function isValidMatchPattern(pattern: string): boolean {
if (pattern === "<all_urls>") return true;
return /^(\*|http|https|file|ftp|ws|wss):\/\/(\*|\*\.[^/*]+|[^/*]+)\/.*$/.test(
pattern,
);
}
function assertValidPatterns(
label: string,
browserName: string,
patterns: string[] = [],
): void {
for (const pattern of patterns) {
if (!isValidMatchPattern(pattern)) {
throw new Error(
`${browserName}: ${label} contains malformed URL pattern: ${pattern}`,
);
}
}
}
function assertOriginFallbackPathIsWildcard(
browserName: string,
patterns: string[] = [],
): void {
for (const pattern of patterns) {
const match = /^[^:]+:\/\/[^/]+(\/.*)$/.exec(pattern);
const pathPart = match ? match[1] : null;
if (pathPart !== "/*") {
throw new Error(
`${browserName}: match_origin_as_fallback requires path /*. Got: ${pattern}`,
);
}
}
}
async function verifyOne(browserName: "chrome" | "firefox"): Promise<void> {
const dir = path.join(outBase, browserName);
const manifestPath = path.join(dir, "manifest.json");
if (!(await exists(manifestPath))) {
throw new Error(`${browserName}: missing manifest.json at ${manifestPath}`);
}
const manifest = await readJson<Record<string, any>>(manifestPath);
if (manifest.manifest_version !== 3) {
throw new Error(
`${browserName}: expected manifest_version 3, got ${manifest.manifest_version}`,
);
}
const permissions = new Set(manifest.permissions || []);
if (
!permissions.has("declarativeNetRequestWithHostAccess") &&
!permissions.has("declarativeNetRequest")
) {
throw new Error(
`${browserName}: expected declarativeNetRequestWithHostAccess or declarativeNetRequest permission`,
);
}
if (browserName === "chrome" && !manifest.background?.service_worker) {
throw new Error(`${browserName}: expected background.service_worker`);
}
if (browserName === "chrome" && manifest.update_url) {
throw new Error(`${browserName}: update_url must not be set`);
}
if (
browserName === "firefox" &&
(!Array.isArray(manifest.background?.scripts) ||
!manifest.background.scripts.length)
) {
throw new Error(`${browserName}: expected background.scripts[]`);
}
if (
browserName === "firefox" &&
manifest.browser_specific_settings?.gecko?.update_url !==
FIREFOX_UPDATES_MANIFEST_URL
) {
throw new Error(
`${browserName}: expected browser_specific_settings.gecko.update_url to be ${FIREFOX_UPDATES_MANIFEST_URL}, got ${manifest.browser_specific_settings?.gecko?.update_url}`,
);
}
if (
browserName === "firefox" &&
(!manifest.browser_specific_settings?.gecko_android ||
typeof manifest.browser_specific_settings.gecko_android !== "object" ||
Array.isArray(manifest.browser_specific_settings.gecko_android))
) {
throw new Error(
`${browserName}: expected browser_specific_settings.gecko_android to be an object`,
);
}
assertValidPatterns(
"host_permissions",
browserName,
manifest.host_permissions,
);
let sawOriginFallback = false;
for (const contentScript of manifest.content_scripts || []) {
assertValidPatterns(
"content_scripts.matches",
browserName,
contentScript.matches,
);
if (contentScript.all_frames !== true) {
throw new Error(
`${browserName}: content_scripts entry must set all_frames: true`,
);
}
if (contentScript.match_about_blank !== true) {
throw new Error(
`${browserName}: content_scripts entry must set match_about_blank: true`,
);
}
if (contentScript.match_origin_as_fallback === true) {
sawOriginFallback = true;
assertOriginFallbackPathIsWildcard(browserName, contentScript.matches);
}
}
if (!sawOriginFallback) {
throw new Error(
`${browserName}: expected at least one content_scripts entry with match_origin_as_fallback: true`,
);
}
const requiredFiles = [
...EXTENSION_ASSET_FILES,
"background.js",
...EXTENSION_ICON_SIZES.map((size) => `icons/icon-${size}.png`),
];
for (const relPath of requiredFiles) {
const fullPath = path.join(dir, relPath);
if (!(await exists(fullPath))) {
throw new Error(`${browserName}: missing required file: ${relPath}`);
}
}
const bridge = await fs.readFile(path.join(dir, "bridge.js"), "utf8");
const prelude = await fs.readFile(path.join(dir, "prelude.js"), "utf8");
const content = await fs.readFile(path.join(dir, "content.js"), "utf8");
const background = await fs.readFile(path.join(dir, "background.js"), "utf8");
const combined = `${bridge}\n${prelude}\n${content}\n${background}`;
const forbiddenSnippets = ["cdnjs.cloudflare.com/ajax/libs/hls.js", "@require"];
for (const snippet of forbiddenSnippets) {
if (combined.includes(snippet)) {
throw new Error(
`${browserName}: bundle contains forbidden snippet (${snippet})`,
);
}
}
const bridgeSrcPath = path.join(rootDir, "src/extension/bridge.ts");
const serializationSrcPath = path.join(rootDir, "src/extension/bodySerialization.ts");
let sourceToCheck: string | null = null;
if (await exists(serializationSrcPath)) {
sourceToCheck = await fs.readFile(serializationSrcPath, "utf8");
} else if (await exists(bridgeSrcPath)) {
sourceToCheck = await fs.readFile(bridgeSrcPath, "utf8");
}
if (sourceToCheck && !/(Blob|FileReader)/.test(sourceToCheck)) {
throw new Error(
`${browserName}: regression guard failed: expected Blob/FileReader handling in body serialization`,
);
}
if (await exists(bridgeSrcPath)) {
const bridgeSource = await fs.readFile(bridgeSrcPath, "utf8");
if (!/await\s+serializeBodyForPort\(/.test(bridgeSource)) {
throw new Error(
`${browserName}: regression guard failed: src/extension/bridge.ts must await serializeBodyForPort(...)`,
);
}
}
console.log(`OK ${browserName}: basic structure checks passed`);
}
export async function verifyExtensionOutputs(
target: ExtensionBuildTarget = "all",
): Promise<void> {
if (target === "all" || target === "chrome") {
await verifyOne("chrome");
}
if (target === "all" || target === "firefox") {
await verifyOne("firefox");
const firefoxUpdatesManifestPath = path.join(
outBase,
FIREFOX_UPDATES_MANIFEST_FILE,
);
if (!(await exists(firefoxUpdatesManifestPath))) {
throw new Error(
`firefox: missing updates manifest at ${firefoxUpdatesManifestPath}`,
);
}
}
console.log("\nExtension verification complete.");
}
interface BrowserBuildResult {
outDir: string;
packagePath: string;
removedPackages: string[];
updatesPath?: string;
}
async function buildBrowserArtifacts({
browserDir,
artifactPrefix,
fileExtension,
backgroundSrc,
headers,
version,
includeWorld,
buildManifest,
afterPackage,
}: {
browserDir: "chrome" | "firefox";
artifactPrefix: string;
fileExtension: ".zip" | ".xpi";
backgroundSrc: string;
headers: ExtensionHeaders;
version: string;
includeWorld: boolean;
buildManifest: (args: {
headers: ExtensionHeaders;
includeWorld: boolean;
}) => Record<string, unknown>;
afterPackage?: () => Promise<string>;
}): Promise<BrowserBuildResult> {
const removedPackages = await cleanupOlderVersionedArtifacts({
artifactPrefix,
fileExtension,
currentVersion: version,
});
const outDir = path.join(outBase, browserDir);
await ensureCleanDir(outDir);
await copyExtensionFiles(outDir, {
backgroundSrc,
backgroundDst: "background.js",
});
await writeManifest(outDir, buildManifest({ headers, includeWorld }));
const packagePath = path.join(outBase, `${artifactPrefix}-${version}${fileExtension}`);
await zipDir(outDir, packagePath);
const updatesPath = afterPackage ? await afterPackage() : undefined;
return { outDir, packagePath, removedPackages, updatesPath };
}
export async function finalizeExtensionBuildArtifacts(
target: ExtensionBuildTarget = "all",
): Promise<void> {
const headers = await getExtensionHeaders();
const includeWorld = true;
const shouldBuildChrome = target === "all" || target === "chrome";
const shouldBuildFirefox = target === "all" || target === "firefox";
await renameContentCss();
if (shouldBuildChrome) {
await fs.rm(path.join(outBase, CHROME_UPDATES_MANIFEST_FILE), { force: true });
}
const version = headers.version || DEFAULT_EXTENSION_VERSION;
let chromeBuild: BrowserBuildResult | null = null;
let firefoxBuild: BrowserBuildResult | null = null;
if (shouldBuildChrome) {
chromeBuild = await buildBrowserArtifacts({
browserDir: "chrome",
artifactPrefix: "vot-extension-chrome",
fileExtension: ".zip",
backgroundSrc: "background.js",
headers,
version,
includeWorld,
buildManifest: buildManifestChrome,
});
}