@xlinklight/browser-extension-tools 1.0.0-rc.0 → 1.0.0
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/LICENSE +21 -21
- package/README.md +33 -33
- package/assets/shared/css/side-panel.css +94 -94
- package/assets/shared/js/chrome-runtime.js +87 -87
- package/assets/shared/js/extension-account-menu.js +239 -239
- package/assets/shared/js/extension-i18n.js +42 -42
- package/assets/shared/js/extension-session-store.js +42 -42
- package/assets/shared/js/extension-tabs.js +23 -23
- package/assets/shared/js/link-light-api-client.js +31 -31
- package/assets/shared/js/side-panel-account.js +14 -14
- package/assets/shared/js/side-panel-api.js +13 -13
- package/assets/shared/js/side-panel-bootstrap.js +127 -127
- package/assets/shared/js/side-panel-dom.js +15 -15
- package/assets/shared/js/side-panel-i18n.js +37 -37
- package/assets/shared/js/side-panel-import-preview.js +7 -7
- package/assets/shared/js/side-panel-status.js +13 -13
- package/assets/shared/js/side-panel-tabs.js +35 -35
- package/assets/shared/js/side-panel-translations.js +41 -41
- package/package.json +1 -1
- package/src/chrome-discovery.mjs +231 -231
- package/src/chrome-for-testing.mjs +203 -203
- package/src/chrome-install-worker.mjs +8 -8
- package/src/cli.mjs +171 -171
- package/src/dev-cli.mjs +9 -9
- package/src/dev-extension.mjs +350 -350
- package/src/package-cli.mjs +7 -7
- package/src/package.mjs +223 -223
- package/src/profile.mjs +144 -144
- package/src/reload-server.mjs +108 -108
- package/src/run.mjs +7 -7
- package/src/watcher.mjs +45 -45
package/src/profile.mjs
CHANGED
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { mkdir, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { assertProjectOutput, getProfileDirForExtensions, getRelevantExtensionFiles, getRelevantExtensionSourcePath, profileStateFileName } from "./cli.mjs";
|
|
6
|
-
import { getChromeExtensionIdForPath, samePath } from "./chrome-discovery.mjs";
|
|
7
|
-
|
|
8
|
-
async function readFileIfExists(filePath) {
|
|
9
|
-
try {
|
|
10
|
-
const stats = await stat(filePath);
|
|
11
|
-
if (stats.isDirectory()) {
|
|
12
|
-
const names = (await readdir(filePath)).toSorted();
|
|
13
|
-
return Buffer.concat(await Promise.all(names.map(async name => Buffer.concat([Buffer.from(name + "\0"), Buffer.from(await readFileIfExists(path.join(filePath, name)))]))));
|
|
14
|
-
}
|
|
15
|
-
return await readFile(filePath);
|
|
16
|
-
} catch (error) {
|
|
17
|
-
if (error?.code === "ENOENT") return "";
|
|
18
|
-
throw error;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export async function readJsonFileIfExists(filePath) {
|
|
23
|
-
try {
|
|
24
|
-
return JSON.parse(await readFile(filePath, "utf8"));
|
|
25
|
-
} catch (error) {
|
|
26
|
-
if (error?.code === "ENOENT") {
|
|
27
|
-
return {};
|
|
28
|
-
}
|
|
29
|
-
throw error;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export async function pinChromeExtensionsInProfile({
|
|
34
|
-
extensionDirs,
|
|
35
|
-
platform = process.platform,
|
|
36
|
-
profileDir,
|
|
37
|
-
}) {
|
|
38
|
-
const defaultProfileDir = path.join(profileDir, "Default");
|
|
39
|
-
const preferencesPath = path.join(defaultProfileDir, "Preferences");
|
|
40
|
-
const preferences = await readJsonFileIfExists(preferencesPath);
|
|
41
|
-
const pinnedExtensionIds = extensionDirs.map((extensionDir) =>
|
|
42
|
-
getChromeExtensionIdForPath(extensionDir, { platform }),
|
|
43
|
-
);
|
|
44
|
-
const existingPinnedExtensionIds = Array.isArray(preferences.extensions?.pinned_extensions)
|
|
45
|
-
? preferences.extensions.pinned_extensions
|
|
46
|
-
: [];
|
|
47
|
-
|
|
48
|
-
preferences.extensions = {
|
|
49
|
-
...(preferences.extensions ?? {}),
|
|
50
|
-
pinned_extensions: [...new Set([...existingPinnedExtensionIds, ...pinnedExtensionIds])],
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
await mkdir(defaultProfileDir, { recursive: true });
|
|
54
|
-
await writeFile(preferencesPath, `${JSON.stringify(preferences)}\n`, "utf8");
|
|
55
|
-
|
|
56
|
-
return [...new Set(pinnedExtensionIds)];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export async function getExtensionLaunchSignature(rootDir, extensionNames, extensionRoot) {
|
|
60
|
-
const hash = createHash("sha256");
|
|
61
|
-
|
|
62
|
-
for (const extensionName of extensionNames) {
|
|
63
|
-
hash.update(extensionName);
|
|
64
|
-
hash.update("\0");
|
|
65
|
-
for (const fileName of getRelevantExtensionFiles(rootDir, extensionName, extensionRoot)) {
|
|
66
|
-
hash.update(fileName);
|
|
67
|
-
hash.update("\0");
|
|
68
|
-
hash.update(await readFileIfExists(getRelevantExtensionSourcePath(rootDir, extensionName, fileName, extensionRoot)));
|
|
69
|
-
hash.update("\0");
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return hash.digest("hex");
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function getProfileStateFile(rootDir, extensionNames, extensionRoot) {
|
|
77
|
-
return path.join(getProfileDirForExtensions(rootDir, extensionNames, extensionRoot), profileStateFileName);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export async function readProfileSignature(stateFile) {
|
|
81
|
-
try {
|
|
82
|
-
return JSON.parse(await readFile(stateFile, "utf8"))?.extensionLaunchSignature ?? null;
|
|
83
|
-
} catch (error) {
|
|
84
|
-
if (error?.code === "ENOENT") {
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
throw error;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export async function writeProfileSignature(profileDir, extensionLaunchSignature) {
|
|
92
|
-
await mkdir(profileDir, { recursive: true });
|
|
93
|
-
await writeFile(
|
|
94
|
-
path.join(profileDir, profileStateFileName),
|
|
95
|
-
`${JSON.stringify({ extensionLaunchSignature }, null, 2)}\n`,
|
|
96
|
-
"utf8",
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export async function resetChromeProfile({
|
|
101
|
-
profileDir,
|
|
102
|
-
remover = rm,
|
|
103
|
-
rootDir = process.cwd(),
|
|
104
|
-
extensionName,
|
|
105
|
-
extensionRoot,
|
|
106
|
-
extensionNames = [extensionName],
|
|
107
|
-
} = {}) {
|
|
108
|
-
const expectedProfileDir = getProfileDirForExtensions(rootDir, extensionNames, extensionRoot);
|
|
109
|
-
assertProjectOutput(rootDir, expectedProfileDir);
|
|
110
|
-
const targetProfileDir = profileDir ?? expectedProfileDir;
|
|
111
|
-
|
|
112
|
-
if (!samePath(targetProfileDir, expectedProfileDir)) {
|
|
113
|
-
throw new Error(`Refusing to reset Chrome profile outside ${expectedProfileDir}`);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
await remover(expectedProfileDir, { recursive: true, force: true });
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export async function ensureFreshChromeProfileForExtension({
|
|
120
|
-
extensionName,
|
|
121
|
-
extensionRoot,
|
|
122
|
-
extensionNames = [extensionName],
|
|
123
|
-
logger = console.log,
|
|
124
|
-
rootDir = process.cwd(),
|
|
125
|
-
} = {}) {
|
|
126
|
-
const profileDir = getProfileDirForExtensions(rootDir, extensionNames, extensionRoot);
|
|
127
|
-
assertProjectOutput(rootDir, profileDir);
|
|
128
|
-
const stateFile = getProfileStateFile(rootDir, extensionNames, extensionRoot);
|
|
129
|
-
const nextSignature = await getExtensionLaunchSignature(rootDir, extensionNames, extensionRoot);
|
|
130
|
-
const previousSignature = await readProfileSignature(stateFile);
|
|
131
|
-
|
|
132
|
-
if (previousSignature && previousSignature !== nextSignature) {
|
|
133
|
-
logger(
|
|
134
|
-
`Extension launch signature changed; kept existing Chrome extension debug profile: ${profileDir}. Run with --reset-profile if manifest or permission state looks stale.`,
|
|
135
|
-
);
|
|
136
|
-
} else if (!previousSignature && existsSync(profileDir)) {
|
|
137
|
-
logger(
|
|
138
|
-
`Legacy Chrome extension debug profile detected; kept existing Chrome extension debug profile: ${profileDir}. Run with --reset-profile if manifest or permission state looks stale.`,
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
await writeProfileSignature(profileDir, nextSignature);
|
|
143
|
-
return { profileDir };
|
|
144
|
-
}
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { mkdir, readFile, readdir, rm, stat, writeFile } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { assertProjectOutput, getProfileDirForExtensions, getRelevantExtensionFiles, getRelevantExtensionSourcePath, profileStateFileName } from "./cli.mjs";
|
|
6
|
+
import { getChromeExtensionIdForPath, samePath } from "./chrome-discovery.mjs";
|
|
7
|
+
|
|
8
|
+
async function readFileIfExists(filePath) {
|
|
9
|
+
try {
|
|
10
|
+
const stats = await stat(filePath);
|
|
11
|
+
if (stats.isDirectory()) {
|
|
12
|
+
const names = (await readdir(filePath)).toSorted();
|
|
13
|
+
return Buffer.concat(await Promise.all(names.map(async name => Buffer.concat([Buffer.from(name + "\0"), Buffer.from(await readFileIfExists(path.join(filePath, name)))]))));
|
|
14
|
+
}
|
|
15
|
+
return await readFile(filePath);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
if (error?.code === "ENOENT") return "";
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function readJsonFileIfExists(filePath) {
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(await readFile(filePath, "utf8"));
|
|
25
|
+
} catch (error) {
|
|
26
|
+
if (error?.code === "ENOENT") {
|
|
27
|
+
return {};
|
|
28
|
+
}
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function pinChromeExtensionsInProfile({
|
|
34
|
+
extensionDirs,
|
|
35
|
+
platform = process.platform,
|
|
36
|
+
profileDir,
|
|
37
|
+
}) {
|
|
38
|
+
const defaultProfileDir = path.join(profileDir, "Default");
|
|
39
|
+
const preferencesPath = path.join(defaultProfileDir, "Preferences");
|
|
40
|
+
const preferences = await readJsonFileIfExists(preferencesPath);
|
|
41
|
+
const pinnedExtensionIds = extensionDirs.map((extensionDir) =>
|
|
42
|
+
getChromeExtensionIdForPath(extensionDir, { platform }),
|
|
43
|
+
);
|
|
44
|
+
const existingPinnedExtensionIds = Array.isArray(preferences.extensions?.pinned_extensions)
|
|
45
|
+
? preferences.extensions.pinned_extensions
|
|
46
|
+
: [];
|
|
47
|
+
|
|
48
|
+
preferences.extensions = {
|
|
49
|
+
...(preferences.extensions ?? {}),
|
|
50
|
+
pinned_extensions: [...new Set([...existingPinnedExtensionIds, ...pinnedExtensionIds])],
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
await mkdir(defaultProfileDir, { recursive: true });
|
|
54
|
+
await writeFile(preferencesPath, `${JSON.stringify(preferences)}\n`, "utf8");
|
|
55
|
+
|
|
56
|
+
return [...new Set(pinnedExtensionIds)];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function getExtensionLaunchSignature(rootDir, extensionNames, extensionRoot) {
|
|
60
|
+
const hash = createHash("sha256");
|
|
61
|
+
|
|
62
|
+
for (const extensionName of extensionNames) {
|
|
63
|
+
hash.update(extensionName);
|
|
64
|
+
hash.update("\0");
|
|
65
|
+
for (const fileName of getRelevantExtensionFiles(rootDir, extensionName, extensionRoot)) {
|
|
66
|
+
hash.update(fileName);
|
|
67
|
+
hash.update("\0");
|
|
68
|
+
hash.update(await readFileIfExists(getRelevantExtensionSourcePath(rootDir, extensionName, fileName, extensionRoot)));
|
|
69
|
+
hash.update("\0");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return hash.digest("hex");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function getProfileStateFile(rootDir, extensionNames, extensionRoot) {
|
|
77
|
+
return path.join(getProfileDirForExtensions(rootDir, extensionNames, extensionRoot), profileStateFileName);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function readProfileSignature(stateFile) {
|
|
81
|
+
try {
|
|
82
|
+
return JSON.parse(await readFile(stateFile, "utf8"))?.extensionLaunchSignature ?? null;
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error?.code === "ENOENT") {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function writeProfileSignature(profileDir, extensionLaunchSignature) {
|
|
92
|
+
await mkdir(profileDir, { recursive: true });
|
|
93
|
+
await writeFile(
|
|
94
|
+
path.join(profileDir, profileStateFileName),
|
|
95
|
+
`${JSON.stringify({ extensionLaunchSignature }, null, 2)}\n`,
|
|
96
|
+
"utf8",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export async function resetChromeProfile({
|
|
101
|
+
profileDir,
|
|
102
|
+
remover = rm,
|
|
103
|
+
rootDir = process.cwd(),
|
|
104
|
+
extensionName,
|
|
105
|
+
extensionRoot,
|
|
106
|
+
extensionNames = [extensionName],
|
|
107
|
+
} = {}) {
|
|
108
|
+
const expectedProfileDir = getProfileDirForExtensions(rootDir, extensionNames, extensionRoot);
|
|
109
|
+
assertProjectOutput(rootDir, expectedProfileDir);
|
|
110
|
+
const targetProfileDir = profileDir ?? expectedProfileDir;
|
|
111
|
+
|
|
112
|
+
if (!samePath(targetProfileDir, expectedProfileDir)) {
|
|
113
|
+
throw new Error(`Refusing to reset Chrome profile outside ${expectedProfileDir}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
await remover(expectedProfileDir, { recursive: true, force: true });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export async function ensureFreshChromeProfileForExtension({
|
|
120
|
+
extensionName,
|
|
121
|
+
extensionRoot,
|
|
122
|
+
extensionNames = [extensionName],
|
|
123
|
+
logger = console.log,
|
|
124
|
+
rootDir = process.cwd(),
|
|
125
|
+
} = {}) {
|
|
126
|
+
const profileDir = getProfileDirForExtensions(rootDir, extensionNames, extensionRoot);
|
|
127
|
+
assertProjectOutput(rootDir, profileDir);
|
|
128
|
+
const stateFile = getProfileStateFile(rootDir, extensionNames, extensionRoot);
|
|
129
|
+
const nextSignature = await getExtensionLaunchSignature(rootDir, extensionNames, extensionRoot);
|
|
130
|
+
const previousSignature = await readProfileSignature(stateFile);
|
|
131
|
+
|
|
132
|
+
if (previousSignature && previousSignature !== nextSignature) {
|
|
133
|
+
logger(
|
|
134
|
+
`Extension launch signature changed; kept existing Chrome extension debug profile: ${profileDir}. Run with --reset-profile if manifest or permission state looks stale.`,
|
|
135
|
+
);
|
|
136
|
+
} else if (!previousSignature && existsSync(profileDir)) {
|
|
137
|
+
logger(
|
|
138
|
+
`Legacy Chrome extension debug profile detected; kept existing Chrome extension debug profile: ${profileDir}. Run with --reset-profile if manifest or permission state looks stale.`,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
await writeProfileSignature(profileDir, nextSignature);
|
|
143
|
+
return { profileDir };
|
|
144
|
+
}
|
package/src/reload-server.mjs
CHANGED
|
@@ -1,108 +1,108 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { createServer } from "node:http";
|
|
3
|
-
export function addUniqueItems(items, additions) {
|
|
4
|
-
return [...new Set([...(items ?? []), ...additions])];
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function buildDevReloadClient({ reloadPort }) {
|
|
8
|
-
return `(() => {
|
|
9
|
-
const reloadSocketUrl = "ws://127.0.0.1:${reloadPort}";
|
|
10
|
-
|
|
11
|
-
function connect() {
|
|
12
|
-
const socket = new WebSocket(reloadSocketUrl);
|
|
13
|
-
socket.addEventListener("message", (event) => {
|
|
14
|
-
try {
|
|
15
|
-
const message = JSON.parse(event.data);
|
|
16
|
-
if (message?.type !== "reload") return;
|
|
17
|
-
chrome.runtime.reload();
|
|
18
|
-
} catch {
|
|
19
|
-
// Ignore malformed dev-server messages.
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
socket.addEventListener("close", () => setTimeout(connect, 1000));
|
|
23
|
-
socket.addEventListener("error", () => socket.close());
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
connect();
|
|
27
|
-
})();
|
|
28
|
-
`;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function encodeWebSocketTextFrame(text) {
|
|
32
|
-
const payload = Buffer.from(text);
|
|
33
|
-
if (payload.length < 126) {
|
|
34
|
-
return Buffer.concat([Buffer.from([0x81, payload.length]), payload]);
|
|
35
|
-
}
|
|
36
|
-
if (payload.length <= 0xffff) {
|
|
37
|
-
const header = Buffer.alloc(4);
|
|
38
|
-
header[0] = 0x81;
|
|
39
|
-
header[1] = 126;
|
|
40
|
-
header.writeUInt16BE(payload.length, 2);
|
|
41
|
-
return Buffer.concat([header, payload]);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const header = Buffer.alloc(10);
|
|
45
|
-
header[0] = 0x81;
|
|
46
|
-
header[1] = 127;
|
|
47
|
-
header.writeBigUInt64BE(BigInt(payload.length), 2);
|
|
48
|
-
return Buffer.concat([header, payload]);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export async function createDevReloadServer({ host = "127.0.0.1", logger = console.log, port = 0 } = {}) {
|
|
52
|
-
const clients = new Set();
|
|
53
|
-
const server = createServer();
|
|
54
|
-
|
|
55
|
-
server.on("upgrade", (request, socket) => {
|
|
56
|
-
const key = request.headers["sec-websocket-key"];
|
|
57
|
-
if (!key) {
|
|
58
|
-
socket.destroy();
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const accept = createHash("sha1")
|
|
63
|
-
.update(`${key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11`)
|
|
64
|
-
.digest("base64");
|
|
65
|
-
socket.write(
|
|
66
|
-
[
|
|
67
|
-
"HTTP/1.1 101 Switching Protocols",
|
|
68
|
-
"Upgrade: websocket",
|
|
69
|
-
"Connection: Upgrade",
|
|
70
|
-
`Sec-WebSocket-Accept: ${accept}`,
|
|
71
|
-
"",
|
|
72
|
-
"",
|
|
73
|
-
].join("\r\n"),
|
|
74
|
-
);
|
|
75
|
-
clients.add(socket);
|
|
76
|
-
socket.on("close", () => clients.delete(socket));
|
|
77
|
-
socket.on("error", () => clients.delete(socket));
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
await new Promise((resolve, reject) => {
|
|
81
|
-
server.once("error", reject);
|
|
82
|
-
server.listen(port, host, () => {
|
|
83
|
-
server.off("error", reject);
|
|
84
|
-
resolve();
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
const address = server.address();
|
|
89
|
-
const selectedPort = typeof address === "object" && address ? address.port : port;
|
|
90
|
-
logger(`Extension dev reload server listening on ws://${host}:${selectedPort}`);
|
|
91
|
-
|
|
92
|
-
return {
|
|
93
|
-
port: selectedPort,
|
|
94
|
-
broadcastReload: () => {
|
|
95
|
-
const frame = encodeWebSocketTextFrame(JSON.stringify({ type: "reload" }));
|
|
96
|
-
for (const client of clients) {
|
|
97
|
-
client.write(frame);
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
close: () =>
|
|
101
|
-
new Promise((resolve, reject) => {
|
|
102
|
-
for (const client of clients) {
|
|
103
|
-
client.destroy();
|
|
104
|
-
}
|
|
105
|
-
server.close((error) => (error ? reject(error) : resolve()));
|
|
106
|
-
}),
|
|
107
|
-
};
|
|
108
|
-
}
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
export function addUniqueItems(items, additions) {
|
|
4
|
+
return [...new Set([...(items ?? []), ...additions])];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function buildDevReloadClient({ reloadPort }) {
|
|
8
|
+
return `(() => {
|
|
9
|
+
const reloadSocketUrl = "ws://127.0.0.1:${reloadPort}";
|
|
10
|
+
|
|
11
|
+
function connect() {
|
|
12
|
+
const socket = new WebSocket(reloadSocketUrl);
|
|
13
|
+
socket.addEventListener("message", (event) => {
|
|
14
|
+
try {
|
|
15
|
+
const message = JSON.parse(event.data);
|
|
16
|
+
if (message?.type !== "reload") return;
|
|
17
|
+
chrome.runtime.reload();
|
|
18
|
+
} catch {
|
|
19
|
+
// Ignore malformed dev-server messages.
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
socket.addEventListener("close", () => setTimeout(connect, 1000));
|
|
23
|
+
socket.addEventListener("error", () => socket.close());
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
connect();
|
|
27
|
+
})();
|
|
28
|
+
`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function encodeWebSocketTextFrame(text) {
|
|
32
|
+
const payload = Buffer.from(text);
|
|
33
|
+
if (payload.length < 126) {
|
|
34
|
+
return Buffer.concat([Buffer.from([0x81, payload.length]), payload]);
|
|
35
|
+
}
|
|
36
|
+
if (payload.length <= 0xffff) {
|
|
37
|
+
const header = Buffer.alloc(4);
|
|
38
|
+
header[0] = 0x81;
|
|
39
|
+
header[1] = 126;
|
|
40
|
+
header.writeUInt16BE(payload.length, 2);
|
|
41
|
+
return Buffer.concat([header, payload]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const header = Buffer.alloc(10);
|
|
45
|
+
header[0] = 0x81;
|
|
46
|
+
header[1] = 127;
|
|
47
|
+
header.writeBigUInt64BE(BigInt(payload.length), 2);
|
|
48
|
+
return Buffer.concat([header, payload]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function createDevReloadServer({ host = "127.0.0.1", logger = console.log, port = 0 } = {}) {
|
|
52
|
+
const clients = new Set();
|
|
53
|
+
const server = createServer();
|
|
54
|
+
|
|
55
|
+
server.on("upgrade", (request, socket) => {
|
|
56
|
+
const key = request.headers["sec-websocket-key"];
|
|
57
|
+
if (!key) {
|
|
58
|
+
socket.destroy();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const accept = createHash("sha1")
|
|
63
|
+
.update(`${key}258EAFA5-E914-47DA-95CA-C5AB0DC85B11`)
|
|
64
|
+
.digest("base64");
|
|
65
|
+
socket.write(
|
|
66
|
+
[
|
|
67
|
+
"HTTP/1.1 101 Switching Protocols",
|
|
68
|
+
"Upgrade: websocket",
|
|
69
|
+
"Connection: Upgrade",
|
|
70
|
+
`Sec-WebSocket-Accept: ${accept}`,
|
|
71
|
+
"",
|
|
72
|
+
"",
|
|
73
|
+
].join("\r\n"),
|
|
74
|
+
);
|
|
75
|
+
clients.add(socket);
|
|
76
|
+
socket.on("close", () => clients.delete(socket));
|
|
77
|
+
socket.on("error", () => clients.delete(socket));
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
await new Promise((resolve, reject) => {
|
|
81
|
+
server.once("error", reject);
|
|
82
|
+
server.listen(port, host, () => {
|
|
83
|
+
server.off("error", reject);
|
|
84
|
+
resolve();
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const address = server.address();
|
|
89
|
+
const selectedPort = typeof address === "object" && address ? address.port : port;
|
|
90
|
+
logger(`Extension dev reload server listening on ws://${host}:${selectedPort}`);
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
port: selectedPort,
|
|
94
|
+
broadcastReload: () => {
|
|
95
|
+
const frame = encodeWebSocketTextFrame(JSON.stringify({ type: "reload" }));
|
|
96
|
+
for (const client of clients) {
|
|
97
|
+
client.write(frame);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
close: () =>
|
|
101
|
+
new Promise((resolve, reject) => {
|
|
102
|
+
for (const client of clients) {
|
|
103
|
+
client.destroy();
|
|
104
|
+
}
|
|
105
|
+
server.close((error) => (error ? reject(error) : resolve()));
|
|
106
|
+
}),
|
|
107
|
+
};
|
|
108
|
+
}
|
package/src/run.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from "./cli.mjs";
|
|
2
|
-
export { buildChromeLaunchConfig, findChromePath, findLocalChromeForTesting, getChromeExtensionIdForPath } from "./chrome-discovery.mjs";
|
|
3
|
-
export { getChromeForTestingInstallStep, installChromeForTesting } from "./chrome-for-testing.mjs";
|
|
4
|
-
export { prepareDevExtension, runChromeExtensionDev } from "./dev-extension.mjs";
|
|
5
|
-
export { ensureFreshChromeProfileForExtension, pinChromeExtensionsInProfile, resetChromeProfile } from "./profile.mjs";
|
|
6
|
-
export { createDevReloadServer } from "./reload-server.mjs";
|
|
7
|
-
export { createExtensionFileWatcher } from "./watcher.mjs";
|
|
1
|
+
export * from "./cli.mjs";
|
|
2
|
+
export { buildChromeLaunchConfig, findChromePath, findLocalChromeForTesting, getChromeExtensionIdForPath } from "./chrome-discovery.mjs";
|
|
3
|
+
export { getChromeForTestingInstallStep, installChromeForTesting } from "./chrome-for-testing.mjs";
|
|
4
|
+
export { prepareDevExtension, runChromeExtensionDev } from "./dev-extension.mjs";
|
|
5
|
+
export { ensureFreshChromeProfileForExtension, pinChromeExtensionsInProfile, resetChromeProfile } from "./profile.mjs";
|
|
6
|
+
export { createDevReloadServer } from "./reload-server.mjs";
|
|
7
|
+
export { createExtensionFileWatcher } from "./watcher.mjs";
|
package/src/watcher.mjs
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import { watch } from "node:fs";
|
|
2
|
-
import { getExistingRelevantExtensionSourcePaths } from "./cli.mjs";
|
|
3
|
-
|
|
4
|
-
export function createExtensionFileWatcher({
|
|
5
|
-
clearTimeoutFn = clearTimeout,
|
|
6
|
-
debounceMs = 100,
|
|
7
|
-
logger = console.log,
|
|
8
|
-
onChange,
|
|
9
|
-
rootDir = process.cwd(),
|
|
10
|
-
extensionName,
|
|
11
|
-
extensionRoot,
|
|
12
|
-
setTimeoutFn = setTimeout,
|
|
13
|
-
watcherFactory = watch,
|
|
14
|
-
}) {
|
|
15
|
-
let pendingTimer = null;
|
|
16
|
-
|
|
17
|
-
function scheduleReload() {
|
|
18
|
-
if (pendingTimer) {
|
|
19
|
-
clearTimeoutFn(pendingTimer);
|
|
20
|
-
}
|
|
21
|
-
pendingTimer = setTimeoutFn(async () => {
|
|
22
|
-
pendingTimer = null;
|
|
23
|
-
try {
|
|
24
|
-
await onChange();
|
|
25
|
-
} catch (error) {
|
|
26
|
-
logger(error instanceof Error ? error.message : String(error));
|
|
27
|
-
}
|
|
28
|
-
}, debounceMs);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const watchers = getExistingRelevantExtensionSourcePaths(rootDir, extensionName, extensionRoot).map((sourcePath) =>
|
|
32
|
-
watcherFactory(sourcePath, scheduleReload),
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
close: () => {
|
|
37
|
-
if (pendingTimer) {
|
|
38
|
-
clearTimeoutFn(pendingTimer);
|
|
39
|
-
}
|
|
40
|
-
for (const fileWatcher of watchers) {
|
|
41
|
-
fileWatcher.close();
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
}
|
|
1
|
+
import { watch } from "node:fs";
|
|
2
|
+
import { getExistingRelevantExtensionSourcePaths } from "./cli.mjs";
|
|
3
|
+
|
|
4
|
+
export function createExtensionFileWatcher({
|
|
5
|
+
clearTimeoutFn = clearTimeout,
|
|
6
|
+
debounceMs = 100,
|
|
7
|
+
logger = console.log,
|
|
8
|
+
onChange,
|
|
9
|
+
rootDir = process.cwd(),
|
|
10
|
+
extensionName,
|
|
11
|
+
extensionRoot,
|
|
12
|
+
setTimeoutFn = setTimeout,
|
|
13
|
+
watcherFactory = watch,
|
|
14
|
+
}) {
|
|
15
|
+
let pendingTimer = null;
|
|
16
|
+
|
|
17
|
+
function scheduleReload() {
|
|
18
|
+
if (pendingTimer) {
|
|
19
|
+
clearTimeoutFn(pendingTimer);
|
|
20
|
+
}
|
|
21
|
+
pendingTimer = setTimeoutFn(async () => {
|
|
22
|
+
pendingTimer = null;
|
|
23
|
+
try {
|
|
24
|
+
await onChange();
|
|
25
|
+
} catch (error) {
|
|
26
|
+
logger(error instanceof Error ? error.message : String(error));
|
|
27
|
+
}
|
|
28
|
+
}, debounceMs);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const watchers = getExistingRelevantExtensionSourcePaths(rootDir, extensionName, extensionRoot).map((sourcePath) =>
|
|
32
|
+
watcherFactory(sourcePath, scheduleReload),
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
close: () => {
|
|
37
|
+
if (pendingTimer) {
|
|
38
|
+
clearTimeoutFn(pendingTimer);
|
|
39
|
+
}
|
|
40
|
+
for (const fileWatcher of watchers) {
|
|
41
|
+
fileWatcher.close();
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|