stage-browser 0.0.0-stage → 0.1.1
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/README.md +136 -2
- package/build/icon.icns +0 -0
- package/build/icon.png +0 -0
- package/dist/bin/stage.js +111 -0
- package/dist/main/cli-args.js +28 -0
- package/dist/main/context-menu.js +68 -0
- package/dist/main/context.js +2 -0
- package/dist/main/extensions.js +78 -0
- package/dist/main/ipc.js +90 -0
- package/dist/main/main.js +279 -0
- package/dist/main/menu.js +140 -0
- package/dist/main/reveal.js +155 -0
- package/dist/main/selftest.js +490 -0
- package/dist/main/store.js +85 -0
- package/dist/main/suggest.js +43 -0
- package/dist/main/tabs.js +324 -0
- package/dist/main/url.js +38 -0
- package/dist/main/window.js +68 -0
- package/dist/preload/extension-compat.js +163 -0
- package/dist/preload/ui-preload.js +33 -0
- package/dist/shared/contract.js +6 -0
- package/dist/ui/app.js +653 -0
- package/dist/ui/index.html +41 -0
- package/dist/ui/mock.js +479 -0
- package/dist/ui/newtab.html +69 -0
- package/dist/ui/styles.css +410 -0
- package/node_modules/@types/chrome/LICENSE +21 -0
- package/node_modules/@types/chrome/README.md +15 -0
- package/node_modules/@types/chrome/chrome-cast/index.d.ts +1156 -0
- package/node_modules/@types/chrome/har-format/index.d.ts +6 -0
- package/node_modules/@types/chrome/index.d.ts +13786 -0
- package/node_modules/@types/chrome/package.json +99 -0
- package/node_modules/@types/filesystem/LICENSE +21 -0
- package/node_modules/@types/filesystem/README.md +15 -0
- package/node_modules/@types/filesystem/index.d.ts +564 -0
- package/node_modules/@types/filesystem/package.json +26 -0
- package/node_modules/@types/filewriter/LICENSE +21 -0
- package/node_modules/@types/filewriter/README.md +15 -0
- package/node_modules/@types/filewriter/index.d.ts +166 -0
- package/node_modules/@types/filewriter/package.json +25 -0
- package/node_modules/@types/har-format/LICENSE +21 -0
- package/node_modules/@types/har-format/README.md +15 -0
- package/node_modules/@types/har-format/index.d.ts +938 -0
- package/node_modules/@types/har-format/package.json +31 -0
- package/node_modules/adm-zip/LICENSE +21 -0
- package/node_modules/adm-zip/README.md +90 -0
- package/node_modules/adm-zip/adm-zip.js +1020 -0
- package/node_modules/adm-zip/headers/entryHeader.js +396 -0
- package/node_modules/adm-zip/headers/index.js +2 -0
- package/node_modules/adm-zip/headers/mainHeader.js +177 -0
- package/node_modules/adm-zip/methods/deflater.js +33 -0
- package/node_modules/adm-zip/methods/index.js +3 -0
- package/node_modules/adm-zip/methods/inflater.js +64 -0
- package/node_modules/adm-zip/methods/zipcrypto.js +175 -0
- package/node_modules/adm-zip/package.json +56 -0
- package/node_modules/adm-zip/types.d.ts +223 -0
- package/node_modules/adm-zip/util/constants.js +142 -0
- package/node_modules/adm-zip/util/decoder.js +5 -0
- package/node_modules/adm-zip/util/errors.js +66 -0
- package/node_modules/adm-zip/util/fattr.js +76 -0
- package/node_modules/adm-zip/util/index.js +5 -0
- package/node_modules/adm-zip/util/utils.js +481 -0
- package/node_modules/adm-zip/zipEntry.js +415 -0
- package/node_modules/adm-zip/zipFile.js +466 -0
- package/node_modules/debug/LICENSE +20 -0
- package/node_modules/debug/README.md +481 -0
- package/node_modules/debug/package.json +64 -0
- package/node_modules/debug/src/browser.js +272 -0
- package/node_modules/debug/src/common.js +292 -0
- package/node_modules/debug/src/index.js +10 -0
- package/node_modules/debug/src/node.js +263 -0
- package/node_modules/electron-chrome-web-store/README.md +143 -0
- package/node_modules/electron-chrome-web-store/dist/chrome-web-store.preload.js +299 -0
- package/node_modules/electron-chrome-web-store/dist/cjs/browser/index.js +1674 -0
- package/node_modules/electron-chrome-web-store/dist/esm/browser/index.mjs +950 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/api.d.ts +2 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/crx3.d.ts +20 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/id.d.ts +9 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/index.d.ts +62 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/installer.d.ts +29 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/loader.d.ts +18 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/types.d.ts +21 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/updater.d.ts +6 -0
- package/node_modules/electron-chrome-web-store/dist/types/browser/utils.d.ts +4 -0
- package/node_modules/electron-chrome-web-store/dist/types/common/constants.d.ts +44 -0
- package/node_modules/electron-chrome-web-store/dist/types/renderer/chrome-web-store.preload.d.ts +1 -0
- package/node_modules/electron-chrome-web-store/package.json +43 -0
- package/node_modules/ms/index.js +162 -0
- package/node_modules/ms/license.md +21 -0
- package/node_modules/ms/package.json +38 -0
- package/node_modules/ms/readme.md +59 -0
- package/node_modules/pbf/LICENSE +27 -0
- package/node_modules/pbf/README.md +293 -0
- package/node_modules/pbf/bin/pbf +17 -0
- package/node_modules/pbf/compile.js +441 -0
- package/node_modules/pbf/dist/pbf-dev.js +838 -0
- package/node_modules/pbf/dist/pbf.js +1 -0
- package/node_modules/pbf/index.d.ts +206 -0
- package/node_modules/pbf/index.js +830 -0
- package/node_modules/pbf/package.json +63 -0
- package/node_modules/protocol-buffers-schema/.travis.yml +7 -0
- package/node_modules/protocol-buffers-schema/LICENSE +21 -0
- package/node_modules/protocol-buffers-schema/README.md +122 -0
- package/node_modules/protocol-buffers-schema/example.js +11 -0
- package/node_modules/protocol-buffers-schema/example.proto +13 -0
- package/node_modules/protocol-buffers-schema/index.js +6 -0
- package/node_modules/protocol-buffers-schema/package.json +31 -0
- package/node_modules/protocol-buffers-schema/parse.js +769 -0
- package/node_modules/protocol-buffers-schema/stringify.js +206 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/basic.json +90 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/basic.proto +11 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/comments.json +99 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/comments.proto +19 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/complex.json +125 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/complex.proto +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/enum.json +42 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/enum.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/escaped-quotes.json +32 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/escaped-quotes.proto +3 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/extend.json +167 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/extend.proto +19 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/import.json +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/import.proto +6 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/map.json +33 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/map.proto +3 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/no-tags.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/oneof.json +41 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/oneof.proto +6 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/option.json +364 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/option.proto +72 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/options.json +47 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/options.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/pheromon-trajectories.proto +8 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/reserved.json +55 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/reserved.proto +17 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/search.json +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/search.proto +5 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/service.json +108 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/service.proto +30 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/valid-packed.proto +35 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/version.json +90 -0
- package/node_modules/protocol-buffers-schema/test/fixtures/version.proto +13 -0
- package/node_modules/protocol-buffers-schema/test/index.js +157 -0
- package/node_modules/protocol-buffers-schema/tokenize.js +56 -0
- package/node_modules/resolve-protobuf-schema/.travis.yml +3 -0
- package/node_modules/resolve-protobuf-schema/LICENSE +21 -0
- package/node_modules/resolve-protobuf-schema/README.md +49 -0
- package/node_modules/resolve-protobuf-schema/index.js +50 -0
- package/node_modules/resolve-protobuf-schema/package.json +25 -0
- package/node_modules/resolve-protobuf-schema/test/a.proto +5 -0
- package/node_modules/resolve-protobuf-schema/test/b.proto +5 -0
- package/node_modules/resolve-protobuf-schema/test/c.proto +3 -0
- package/node_modules/resolve-protobuf-schema/test/index.js +49 -0
- package/package.json +64 -4
|
@@ -0,0 +1,1674 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/browser/index.ts
|
|
31
|
+
var browser_exports = {};
|
|
32
|
+
__export(browser_exports, {
|
|
33
|
+
downloadExtension: () => downloadExtension,
|
|
34
|
+
installChromeWebStore: () => installChromeWebStore,
|
|
35
|
+
installExtension: () => installExtension,
|
|
36
|
+
loadAllExtensions: () => loadAllExtensions,
|
|
37
|
+
uninstallExtension: () => uninstallExtension,
|
|
38
|
+
updateExtensions: () => updateExtensions
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(browser_exports);
|
|
41
|
+
var import_electron5 = require("electron");
|
|
42
|
+
var path5 = __toESM(require("node:path"));
|
|
43
|
+
var import_node_fs = require("node:fs");
|
|
44
|
+
var import_node_module = require("node:module");
|
|
45
|
+
|
|
46
|
+
// src/browser/api.ts
|
|
47
|
+
var import_debug3 = __toESM(require("debug"));
|
|
48
|
+
var import_electron3 = require("electron");
|
|
49
|
+
|
|
50
|
+
// src/browser/utils.ts
|
|
51
|
+
var path = __toESM(require("node:path"));
|
|
52
|
+
var import_electron = require("electron");
|
|
53
|
+
var fetch = (
|
|
54
|
+
// Prefer Node's fetch until net.fetch crash is fixed
|
|
55
|
+
// https://github.com/electron/electron/pull/45050
|
|
56
|
+
globalThis.fetch || import_electron.net?.fetch || (() => {
|
|
57
|
+
throw new Error(
|
|
58
|
+
"electron-chrome-web-store: Missing fetch API. Please upgrade Electron or Node."
|
|
59
|
+
);
|
|
60
|
+
})
|
|
61
|
+
);
|
|
62
|
+
var getChromeVersion = () => process.versions.chrome || "131.0.6778.109";
|
|
63
|
+
function compareVersions(version1, version2) {
|
|
64
|
+
const v1 = version1.split(".").map(Number);
|
|
65
|
+
const v2 = version2.split(".").map(Number);
|
|
66
|
+
for (let i = 0; i < 3; i++) {
|
|
67
|
+
if (v1[i] > v2[i]) return 1;
|
|
68
|
+
if (v1[i] < v2[i]) return -1;
|
|
69
|
+
}
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
var getDefaultExtensionsPath = () => path.join(import_electron.app.getPath("userData"), "Extensions");
|
|
73
|
+
|
|
74
|
+
// src/common/constants.ts
|
|
75
|
+
var ExtensionInstallStatus = {
|
|
76
|
+
BLACKLISTED: "blacklisted",
|
|
77
|
+
BLOCKED_BY_POLICY: "blocked_by_policy",
|
|
78
|
+
CAN_REQUEST: "can_request",
|
|
79
|
+
CORRUPTED: "corrupted",
|
|
80
|
+
CUSTODIAN_APPROVAL_REQUIRED: "custodian_approval_required",
|
|
81
|
+
CUSTODIAN_APPROVAL_REQUIRED_FOR_INSTALLATION: "custodian_approval_required_for_installation",
|
|
82
|
+
DEPRECATED_MANIFEST_VERSION: "deprecated_manifest_version",
|
|
83
|
+
DISABLED: "disabled",
|
|
84
|
+
ENABLED: "enabled",
|
|
85
|
+
FORCE_INSTALLED: "force_installed",
|
|
86
|
+
INSTALLABLE: "installable",
|
|
87
|
+
REQUEST_PENDING: "request_pending",
|
|
88
|
+
TERMINATED: "terminated"
|
|
89
|
+
};
|
|
90
|
+
var MV2DeprecationStatus = {
|
|
91
|
+
INACTIVE: "inactive",
|
|
92
|
+
SOFT_DISABLE: "soft_disable",
|
|
93
|
+
WARNING: "warning"
|
|
94
|
+
};
|
|
95
|
+
var Result = {
|
|
96
|
+
ALREADY_INSTALLED: "already_installed",
|
|
97
|
+
BLACKLISTED: "blacklisted",
|
|
98
|
+
BLOCKED_BY_POLICY: "blocked_by_policy",
|
|
99
|
+
BLOCKED_FOR_CHILD_ACCOUNT: "blocked_for_child_account",
|
|
100
|
+
FEATURE_DISABLED: "feature_disabled",
|
|
101
|
+
ICON_ERROR: "icon_error",
|
|
102
|
+
INSTALL_ERROR: "install_error",
|
|
103
|
+
INSTALL_IN_PROGRESS: "install_in_progress",
|
|
104
|
+
INVALID_ICON_URL: "invalid_icon_url",
|
|
105
|
+
INVALID_ID: "invalid_id",
|
|
106
|
+
LAUNCH_IN_PROGRESS: "launch_in_progress",
|
|
107
|
+
MANIFEST_ERROR: "manifest_error",
|
|
108
|
+
MISSING_DEPENDENCIES: "missing_dependencies",
|
|
109
|
+
SUCCESS: "success",
|
|
110
|
+
UNKNOWN_ERROR: "unknown_error",
|
|
111
|
+
UNSUPPORTED_EXTENSION_TYPE: "unsupported_extension_type",
|
|
112
|
+
USER_CANCELLED: "user_cancelled",
|
|
113
|
+
USER_GESTURE_REQUIRED: "user_gesture_required"
|
|
114
|
+
};
|
|
115
|
+
var WebGlStatus = {
|
|
116
|
+
WEBGL_ALLOWED: "webgl_allowed",
|
|
117
|
+
WEBGL_BLOCKED: "webgl_blocked"
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// src/browser/installer.ts
|
|
121
|
+
var fs2 = __toESM(require("node:fs"));
|
|
122
|
+
var os = __toESM(require("node:os"));
|
|
123
|
+
var path3 = __toESM(require("node:path"));
|
|
124
|
+
var import_node_stream = require("node:stream");
|
|
125
|
+
var import_promises = require("node:stream/promises");
|
|
126
|
+
var import_electron2 = require("electron");
|
|
127
|
+
var import_adm_zip = __toESM(require("adm-zip"));
|
|
128
|
+
var import_debug2 = __toESM(require("debug"));
|
|
129
|
+
|
|
130
|
+
// ../../node_modules/pbf/index.js
|
|
131
|
+
var SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
|
|
132
|
+
var SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
|
|
133
|
+
var TEXT_DECODER_MIN_LENGTH = 12;
|
|
134
|
+
var utf8TextDecoder = typeof TextDecoder === "undefined" ? null : new TextDecoder("utf-8");
|
|
135
|
+
var PBF_VARINT = 0;
|
|
136
|
+
var PBF_FIXED64 = 1;
|
|
137
|
+
var PBF_BYTES = 2;
|
|
138
|
+
var PBF_FIXED32 = 5;
|
|
139
|
+
var Pbf = class {
|
|
140
|
+
/**
|
|
141
|
+
* @param {Uint8Array | ArrayBuffer} [buf]
|
|
142
|
+
*/
|
|
143
|
+
constructor(buf = new Uint8Array(16)) {
|
|
144
|
+
this.buf = ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
|
|
145
|
+
this.dataView = new DataView(this.buf.buffer);
|
|
146
|
+
this.pos = 0;
|
|
147
|
+
this.type = 0;
|
|
148
|
+
this.length = this.buf.length;
|
|
149
|
+
}
|
|
150
|
+
// === READING =================================================================
|
|
151
|
+
/**
|
|
152
|
+
* @template T
|
|
153
|
+
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
|
|
154
|
+
* @param {T} result
|
|
155
|
+
* @param {number} [end]
|
|
156
|
+
*/
|
|
157
|
+
readFields(readField, result, end = this.length) {
|
|
158
|
+
while (this.pos < end) {
|
|
159
|
+
const val = this.readVarint(), tag = val >> 3, startPos = this.pos;
|
|
160
|
+
this.type = val & 7;
|
|
161
|
+
readField(tag, result, this);
|
|
162
|
+
if (this.pos === startPos) this.skip(val);
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @template T
|
|
168
|
+
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
|
|
169
|
+
* @param {T} result
|
|
170
|
+
*/
|
|
171
|
+
readMessage(readField, result) {
|
|
172
|
+
return this.readFields(readField, result, this.readVarint() + this.pos);
|
|
173
|
+
}
|
|
174
|
+
readFixed32() {
|
|
175
|
+
const val = this.dataView.getUint32(this.pos, true);
|
|
176
|
+
this.pos += 4;
|
|
177
|
+
return val;
|
|
178
|
+
}
|
|
179
|
+
readSFixed32() {
|
|
180
|
+
const val = this.dataView.getInt32(this.pos, true);
|
|
181
|
+
this.pos += 4;
|
|
182
|
+
return val;
|
|
183
|
+
}
|
|
184
|
+
// 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed)
|
|
185
|
+
readFixed64() {
|
|
186
|
+
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getUint32(this.pos + 4, true) * SHIFT_LEFT_32;
|
|
187
|
+
this.pos += 8;
|
|
188
|
+
return val;
|
|
189
|
+
}
|
|
190
|
+
readSFixed64() {
|
|
191
|
+
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getInt32(this.pos + 4, true) * SHIFT_LEFT_32;
|
|
192
|
+
this.pos += 8;
|
|
193
|
+
return val;
|
|
194
|
+
}
|
|
195
|
+
readFloat() {
|
|
196
|
+
const val = this.dataView.getFloat32(this.pos, true);
|
|
197
|
+
this.pos += 4;
|
|
198
|
+
return val;
|
|
199
|
+
}
|
|
200
|
+
readDouble() {
|
|
201
|
+
const val = this.dataView.getFloat64(this.pos, true);
|
|
202
|
+
this.pos += 8;
|
|
203
|
+
return val;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* @param {boolean} [isSigned]
|
|
207
|
+
*/
|
|
208
|
+
readVarint(isSigned) {
|
|
209
|
+
const buf = this.buf;
|
|
210
|
+
let val, b;
|
|
211
|
+
b = buf[this.pos++];
|
|
212
|
+
val = b & 127;
|
|
213
|
+
if (b < 128) return val;
|
|
214
|
+
b = buf[this.pos++];
|
|
215
|
+
val |= (b & 127) << 7;
|
|
216
|
+
if (b < 128) return val;
|
|
217
|
+
b = buf[this.pos++];
|
|
218
|
+
val |= (b & 127) << 14;
|
|
219
|
+
if (b < 128) return val;
|
|
220
|
+
b = buf[this.pos++];
|
|
221
|
+
val |= (b & 127) << 21;
|
|
222
|
+
if (b < 128) return val;
|
|
223
|
+
b = buf[this.pos];
|
|
224
|
+
val |= (b & 15) << 28;
|
|
225
|
+
return readVarintRemainder(val, isSigned, this);
|
|
226
|
+
}
|
|
227
|
+
readVarint64() {
|
|
228
|
+
return this.readVarint(true);
|
|
229
|
+
}
|
|
230
|
+
readSVarint() {
|
|
231
|
+
const num = this.readVarint();
|
|
232
|
+
return num % 2 === 1 ? (num + 1) / -2 : num / 2;
|
|
233
|
+
}
|
|
234
|
+
readBoolean() {
|
|
235
|
+
return Boolean(this.readVarint());
|
|
236
|
+
}
|
|
237
|
+
readString() {
|
|
238
|
+
const end = this.readVarint() + this.pos;
|
|
239
|
+
const pos = this.pos;
|
|
240
|
+
this.pos = end;
|
|
241
|
+
if (end - pos >= TEXT_DECODER_MIN_LENGTH && utf8TextDecoder) {
|
|
242
|
+
return utf8TextDecoder.decode(this.buf.subarray(pos, end));
|
|
243
|
+
}
|
|
244
|
+
return readUtf8(this.buf, pos, end);
|
|
245
|
+
}
|
|
246
|
+
readBytes() {
|
|
247
|
+
const end = this.readVarint() + this.pos, buffer = this.buf.subarray(this.pos, end);
|
|
248
|
+
this.pos = end;
|
|
249
|
+
return buffer;
|
|
250
|
+
}
|
|
251
|
+
// verbose for performance reasons; doesn't affect gzipped size
|
|
252
|
+
/**
|
|
253
|
+
* @param {number[]} [arr]
|
|
254
|
+
* @param {boolean} [isSigned]
|
|
255
|
+
*/
|
|
256
|
+
readPackedVarint(arr = [], isSigned) {
|
|
257
|
+
const end = this.readPackedEnd();
|
|
258
|
+
while (this.pos < end) arr.push(this.readVarint(isSigned));
|
|
259
|
+
return arr;
|
|
260
|
+
}
|
|
261
|
+
/** @param {number[]} [arr] */
|
|
262
|
+
readPackedSVarint(arr = []) {
|
|
263
|
+
const end = this.readPackedEnd();
|
|
264
|
+
while (this.pos < end) arr.push(this.readSVarint());
|
|
265
|
+
return arr;
|
|
266
|
+
}
|
|
267
|
+
/** @param {boolean[]} [arr] */
|
|
268
|
+
readPackedBoolean(arr = []) {
|
|
269
|
+
const end = this.readPackedEnd();
|
|
270
|
+
while (this.pos < end) arr.push(this.readBoolean());
|
|
271
|
+
return arr;
|
|
272
|
+
}
|
|
273
|
+
/** @param {number[]} [arr] */
|
|
274
|
+
readPackedFloat(arr = []) {
|
|
275
|
+
const end = this.readPackedEnd();
|
|
276
|
+
while (this.pos < end) arr.push(this.readFloat());
|
|
277
|
+
return arr;
|
|
278
|
+
}
|
|
279
|
+
/** @param {number[]} [arr] */
|
|
280
|
+
readPackedDouble(arr = []) {
|
|
281
|
+
const end = this.readPackedEnd();
|
|
282
|
+
while (this.pos < end) arr.push(this.readDouble());
|
|
283
|
+
return arr;
|
|
284
|
+
}
|
|
285
|
+
/** @param {number[]} [arr] */
|
|
286
|
+
readPackedFixed32(arr = []) {
|
|
287
|
+
const end = this.readPackedEnd();
|
|
288
|
+
while (this.pos < end) arr.push(this.readFixed32());
|
|
289
|
+
return arr;
|
|
290
|
+
}
|
|
291
|
+
/** @param {number[]} [arr] */
|
|
292
|
+
readPackedSFixed32(arr = []) {
|
|
293
|
+
const end = this.readPackedEnd();
|
|
294
|
+
while (this.pos < end) arr.push(this.readSFixed32());
|
|
295
|
+
return arr;
|
|
296
|
+
}
|
|
297
|
+
/** @param {number[]} [arr] */
|
|
298
|
+
readPackedFixed64(arr = []) {
|
|
299
|
+
const end = this.readPackedEnd();
|
|
300
|
+
while (this.pos < end) arr.push(this.readFixed64());
|
|
301
|
+
return arr;
|
|
302
|
+
}
|
|
303
|
+
/** @param {number[]} [arr] */
|
|
304
|
+
readPackedSFixed64(arr = []) {
|
|
305
|
+
const end = this.readPackedEnd();
|
|
306
|
+
while (this.pos < end) arr.push(this.readSFixed64());
|
|
307
|
+
return arr;
|
|
308
|
+
}
|
|
309
|
+
readPackedEnd() {
|
|
310
|
+
return this.type === PBF_BYTES ? this.readVarint() + this.pos : this.pos + 1;
|
|
311
|
+
}
|
|
312
|
+
/** @param {number} val */
|
|
313
|
+
skip(val) {
|
|
314
|
+
const type = val & 7;
|
|
315
|
+
if (type === PBF_VARINT) while (this.buf[this.pos++] > 127) {
|
|
316
|
+
}
|
|
317
|
+
else if (type === PBF_BYTES) this.pos = this.readVarint() + this.pos;
|
|
318
|
+
else if (type === PBF_FIXED32) this.pos += 4;
|
|
319
|
+
else if (type === PBF_FIXED64) this.pos += 8;
|
|
320
|
+
else throw new Error(`Unimplemented type: ${type}`);
|
|
321
|
+
}
|
|
322
|
+
// === WRITING =================================================================
|
|
323
|
+
/**
|
|
324
|
+
* @param {number} tag
|
|
325
|
+
* @param {number} type
|
|
326
|
+
*/
|
|
327
|
+
writeTag(tag, type) {
|
|
328
|
+
this.writeVarint(tag << 3 | type);
|
|
329
|
+
}
|
|
330
|
+
/** @param {number} min */
|
|
331
|
+
realloc(min) {
|
|
332
|
+
let length = this.length || 16;
|
|
333
|
+
while (length < this.pos + min) length *= 2;
|
|
334
|
+
if (length !== this.length) {
|
|
335
|
+
const buf = new Uint8Array(length);
|
|
336
|
+
buf.set(this.buf);
|
|
337
|
+
this.buf = buf;
|
|
338
|
+
this.dataView = new DataView(buf.buffer);
|
|
339
|
+
this.length = length;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
finish() {
|
|
343
|
+
this.length = this.pos;
|
|
344
|
+
this.pos = 0;
|
|
345
|
+
return this.buf.subarray(0, this.length);
|
|
346
|
+
}
|
|
347
|
+
/** @param {number} val */
|
|
348
|
+
writeFixed32(val) {
|
|
349
|
+
this.realloc(4);
|
|
350
|
+
this.dataView.setInt32(this.pos, val, true);
|
|
351
|
+
this.pos += 4;
|
|
352
|
+
}
|
|
353
|
+
/** @param {number} val */
|
|
354
|
+
writeSFixed32(val) {
|
|
355
|
+
this.realloc(4);
|
|
356
|
+
this.dataView.setInt32(this.pos, val, true);
|
|
357
|
+
this.pos += 4;
|
|
358
|
+
}
|
|
359
|
+
/** @param {number} val */
|
|
360
|
+
writeFixed64(val) {
|
|
361
|
+
this.realloc(8);
|
|
362
|
+
this.dataView.setInt32(this.pos, val & -1, true);
|
|
363
|
+
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
|
|
364
|
+
this.pos += 8;
|
|
365
|
+
}
|
|
366
|
+
/** @param {number} val */
|
|
367
|
+
writeSFixed64(val) {
|
|
368
|
+
this.realloc(8);
|
|
369
|
+
this.dataView.setInt32(this.pos, val & -1, true);
|
|
370
|
+
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
|
|
371
|
+
this.pos += 8;
|
|
372
|
+
}
|
|
373
|
+
/** @param {number} val */
|
|
374
|
+
writeVarint(val) {
|
|
375
|
+
val = +val || 0;
|
|
376
|
+
if (val > 268435455 || val < 0) {
|
|
377
|
+
writeBigVarint(val, this);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
this.realloc(4);
|
|
381
|
+
this.buf[this.pos++] = val & 127 | (val > 127 ? 128 : 0);
|
|
382
|
+
if (val <= 127) return;
|
|
383
|
+
this.buf[this.pos++] = (val >>>= 7) & 127 | (val > 127 ? 128 : 0);
|
|
384
|
+
if (val <= 127) return;
|
|
385
|
+
this.buf[this.pos++] = (val >>>= 7) & 127 | (val > 127 ? 128 : 0);
|
|
386
|
+
if (val <= 127) return;
|
|
387
|
+
this.buf[this.pos++] = val >>> 7 & 127;
|
|
388
|
+
}
|
|
389
|
+
/** @param {number} val */
|
|
390
|
+
writeSVarint(val) {
|
|
391
|
+
this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2);
|
|
392
|
+
}
|
|
393
|
+
/** @param {boolean} val */
|
|
394
|
+
writeBoolean(val) {
|
|
395
|
+
this.writeVarint(+val);
|
|
396
|
+
}
|
|
397
|
+
/** @param {string} str */
|
|
398
|
+
writeString(str) {
|
|
399
|
+
str = String(str);
|
|
400
|
+
this.realloc(str.length * 4);
|
|
401
|
+
this.pos++;
|
|
402
|
+
const startPos = this.pos;
|
|
403
|
+
this.pos = writeUtf8(this.buf, str, this.pos);
|
|
404
|
+
const len = this.pos - startPos;
|
|
405
|
+
if (len >= 128) makeRoomForExtraLength(startPos, len, this);
|
|
406
|
+
this.pos = startPos - 1;
|
|
407
|
+
this.writeVarint(len);
|
|
408
|
+
this.pos += len;
|
|
409
|
+
}
|
|
410
|
+
/** @param {number} val */
|
|
411
|
+
writeFloat(val) {
|
|
412
|
+
this.realloc(4);
|
|
413
|
+
this.dataView.setFloat32(this.pos, val, true);
|
|
414
|
+
this.pos += 4;
|
|
415
|
+
}
|
|
416
|
+
/** @param {number} val */
|
|
417
|
+
writeDouble(val) {
|
|
418
|
+
this.realloc(8);
|
|
419
|
+
this.dataView.setFloat64(this.pos, val, true);
|
|
420
|
+
this.pos += 8;
|
|
421
|
+
}
|
|
422
|
+
/** @param {Uint8Array} buffer */
|
|
423
|
+
writeBytes(buffer) {
|
|
424
|
+
const len = buffer.length;
|
|
425
|
+
this.writeVarint(len);
|
|
426
|
+
this.realloc(len);
|
|
427
|
+
for (let i = 0; i < len; i++) this.buf[this.pos++] = buffer[i];
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* @template T
|
|
431
|
+
* @param {(obj: T, pbf: Pbf) => void} fn
|
|
432
|
+
* @param {T} obj
|
|
433
|
+
*/
|
|
434
|
+
writeRawMessage(fn, obj) {
|
|
435
|
+
this.pos++;
|
|
436
|
+
const startPos = this.pos;
|
|
437
|
+
fn(obj, this);
|
|
438
|
+
const len = this.pos - startPos;
|
|
439
|
+
if (len >= 128) makeRoomForExtraLength(startPos, len, this);
|
|
440
|
+
this.pos = startPos - 1;
|
|
441
|
+
this.writeVarint(len);
|
|
442
|
+
this.pos += len;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* @template T
|
|
446
|
+
* @param {number} tag
|
|
447
|
+
* @param {(obj: T, pbf: Pbf) => void} fn
|
|
448
|
+
* @param {T} obj
|
|
449
|
+
*/
|
|
450
|
+
writeMessage(tag, fn, obj) {
|
|
451
|
+
this.writeTag(tag, PBF_BYTES);
|
|
452
|
+
this.writeRawMessage(fn, obj);
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* @param {number} tag
|
|
456
|
+
* @param {number[]} arr
|
|
457
|
+
*/
|
|
458
|
+
writePackedVarint(tag, arr) {
|
|
459
|
+
if (arr.length) this.writeMessage(tag, writePackedVarint, arr);
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* @param {number} tag
|
|
463
|
+
* @param {number[]} arr
|
|
464
|
+
*/
|
|
465
|
+
writePackedSVarint(tag, arr) {
|
|
466
|
+
if (arr.length) this.writeMessage(tag, writePackedSVarint, arr);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* @param {number} tag
|
|
470
|
+
* @param {boolean[]} arr
|
|
471
|
+
*/
|
|
472
|
+
writePackedBoolean(tag, arr) {
|
|
473
|
+
if (arr.length) this.writeMessage(tag, writePackedBoolean, arr);
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* @param {number} tag
|
|
477
|
+
* @param {number[]} arr
|
|
478
|
+
*/
|
|
479
|
+
writePackedFloat(tag, arr) {
|
|
480
|
+
if (arr.length) this.writeMessage(tag, writePackedFloat, arr);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* @param {number} tag
|
|
484
|
+
* @param {number[]} arr
|
|
485
|
+
*/
|
|
486
|
+
writePackedDouble(tag, arr) {
|
|
487
|
+
if (arr.length) this.writeMessage(tag, writePackedDouble, arr);
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* @param {number} tag
|
|
491
|
+
* @param {number[]} arr
|
|
492
|
+
*/
|
|
493
|
+
writePackedFixed32(tag, arr) {
|
|
494
|
+
if (arr.length) this.writeMessage(tag, writePackedFixed32, arr);
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* @param {number} tag
|
|
498
|
+
* @param {number[]} arr
|
|
499
|
+
*/
|
|
500
|
+
writePackedSFixed32(tag, arr) {
|
|
501
|
+
if (arr.length) this.writeMessage(tag, writePackedSFixed32, arr);
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* @param {number} tag
|
|
505
|
+
* @param {number[]} arr
|
|
506
|
+
*/
|
|
507
|
+
writePackedFixed64(tag, arr) {
|
|
508
|
+
if (arr.length) this.writeMessage(tag, writePackedFixed64, arr);
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* @param {number} tag
|
|
512
|
+
* @param {number[]} arr
|
|
513
|
+
*/
|
|
514
|
+
writePackedSFixed64(tag, arr) {
|
|
515
|
+
if (arr.length) this.writeMessage(tag, writePackedSFixed64, arr);
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* @param {number} tag
|
|
519
|
+
* @param {Uint8Array} buffer
|
|
520
|
+
*/
|
|
521
|
+
writeBytesField(tag, buffer) {
|
|
522
|
+
this.writeTag(tag, PBF_BYTES);
|
|
523
|
+
this.writeBytes(buffer);
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* @param {number} tag
|
|
527
|
+
* @param {number} val
|
|
528
|
+
*/
|
|
529
|
+
writeFixed32Field(tag, val) {
|
|
530
|
+
this.writeTag(tag, PBF_FIXED32);
|
|
531
|
+
this.writeFixed32(val);
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* @param {number} tag
|
|
535
|
+
* @param {number} val
|
|
536
|
+
*/
|
|
537
|
+
writeSFixed32Field(tag, val) {
|
|
538
|
+
this.writeTag(tag, PBF_FIXED32);
|
|
539
|
+
this.writeSFixed32(val);
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* @param {number} tag
|
|
543
|
+
* @param {number} val
|
|
544
|
+
*/
|
|
545
|
+
writeFixed64Field(tag, val) {
|
|
546
|
+
this.writeTag(tag, PBF_FIXED64);
|
|
547
|
+
this.writeFixed64(val);
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* @param {number} tag
|
|
551
|
+
* @param {number} val
|
|
552
|
+
*/
|
|
553
|
+
writeSFixed64Field(tag, val) {
|
|
554
|
+
this.writeTag(tag, PBF_FIXED64);
|
|
555
|
+
this.writeSFixed64(val);
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* @param {number} tag
|
|
559
|
+
* @param {number} val
|
|
560
|
+
*/
|
|
561
|
+
writeVarintField(tag, val) {
|
|
562
|
+
this.writeTag(tag, PBF_VARINT);
|
|
563
|
+
this.writeVarint(val);
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* @param {number} tag
|
|
567
|
+
* @param {number} val
|
|
568
|
+
*/
|
|
569
|
+
writeSVarintField(tag, val) {
|
|
570
|
+
this.writeTag(tag, PBF_VARINT);
|
|
571
|
+
this.writeSVarint(val);
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* @param {number} tag
|
|
575
|
+
* @param {string} str
|
|
576
|
+
*/
|
|
577
|
+
writeStringField(tag, str) {
|
|
578
|
+
this.writeTag(tag, PBF_BYTES);
|
|
579
|
+
this.writeString(str);
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* @param {number} tag
|
|
583
|
+
* @param {number} val
|
|
584
|
+
*/
|
|
585
|
+
writeFloatField(tag, val) {
|
|
586
|
+
this.writeTag(tag, PBF_FIXED32);
|
|
587
|
+
this.writeFloat(val);
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* @param {number} tag
|
|
591
|
+
* @param {number} val
|
|
592
|
+
*/
|
|
593
|
+
writeDoubleField(tag, val) {
|
|
594
|
+
this.writeTag(tag, PBF_FIXED64);
|
|
595
|
+
this.writeDouble(val);
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* @param {number} tag
|
|
599
|
+
* @param {boolean} val
|
|
600
|
+
*/
|
|
601
|
+
writeBooleanField(tag, val) {
|
|
602
|
+
this.writeVarintField(tag, +val);
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
function readVarintRemainder(l, s, p) {
|
|
606
|
+
const buf = p.buf;
|
|
607
|
+
let h, b;
|
|
608
|
+
b = buf[p.pos++];
|
|
609
|
+
h = (b & 112) >> 4;
|
|
610
|
+
if (b < 128) return toNum(l, h, s);
|
|
611
|
+
b = buf[p.pos++];
|
|
612
|
+
h |= (b & 127) << 3;
|
|
613
|
+
if (b < 128) return toNum(l, h, s);
|
|
614
|
+
b = buf[p.pos++];
|
|
615
|
+
h |= (b & 127) << 10;
|
|
616
|
+
if (b < 128) return toNum(l, h, s);
|
|
617
|
+
b = buf[p.pos++];
|
|
618
|
+
h |= (b & 127) << 17;
|
|
619
|
+
if (b < 128) return toNum(l, h, s);
|
|
620
|
+
b = buf[p.pos++];
|
|
621
|
+
h |= (b & 127) << 24;
|
|
622
|
+
if (b < 128) return toNum(l, h, s);
|
|
623
|
+
b = buf[p.pos++];
|
|
624
|
+
h |= (b & 1) << 31;
|
|
625
|
+
if (b < 128) return toNum(l, h, s);
|
|
626
|
+
throw new Error("Expected varint not more than 10 bytes");
|
|
627
|
+
}
|
|
628
|
+
function toNum(low, high, isSigned) {
|
|
629
|
+
return isSigned ? high * 4294967296 + (low >>> 0) : (high >>> 0) * 4294967296 + (low >>> 0);
|
|
630
|
+
}
|
|
631
|
+
function writeBigVarint(val, pbf) {
|
|
632
|
+
let low, high;
|
|
633
|
+
if (val >= 0) {
|
|
634
|
+
low = val % 4294967296 | 0;
|
|
635
|
+
high = val / 4294967296 | 0;
|
|
636
|
+
} else {
|
|
637
|
+
low = ~(-val % 4294967296);
|
|
638
|
+
high = ~(-val / 4294967296);
|
|
639
|
+
if (low ^ 4294967295) {
|
|
640
|
+
low = low + 1 | 0;
|
|
641
|
+
} else {
|
|
642
|
+
low = 0;
|
|
643
|
+
high = high + 1 | 0;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
if (val >= 18446744073709552e3 || val < -18446744073709552e3) {
|
|
647
|
+
throw new Error("Given varint doesn't fit into 10 bytes");
|
|
648
|
+
}
|
|
649
|
+
pbf.realloc(10);
|
|
650
|
+
writeBigVarintLow(low, high, pbf);
|
|
651
|
+
writeBigVarintHigh(high, pbf);
|
|
652
|
+
}
|
|
653
|
+
function writeBigVarintLow(low, high, pbf) {
|
|
654
|
+
pbf.buf[pbf.pos++] = low & 127 | 128;
|
|
655
|
+
low >>>= 7;
|
|
656
|
+
pbf.buf[pbf.pos++] = low & 127 | 128;
|
|
657
|
+
low >>>= 7;
|
|
658
|
+
pbf.buf[pbf.pos++] = low & 127 | 128;
|
|
659
|
+
low >>>= 7;
|
|
660
|
+
pbf.buf[pbf.pos++] = low & 127 | 128;
|
|
661
|
+
low >>>= 7;
|
|
662
|
+
pbf.buf[pbf.pos] = low & 127;
|
|
663
|
+
}
|
|
664
|
+
function writeBigVarintHigh(high, pbf) {
|
|
665
|
+
const lsb = (high & 7) << 4;
|
|
666
|
+
pbf.buf[pbf.pos++] |= lsb | ((high >>>= 3) ? 128 : 0);
|
|
667
|
+
if (!high) return;
|
|
668
|
+
pbf.buf[pbf.pos++] = high & 127 | ((high >>>= 7) ? 128 : 0);
|
|
669
|
+
if (!high) return;
|
|
670
|
+
pbf.buf[pbf.pos++] = high & 127 | ((high >>>= 7) ? 128 : 0);
|
|
671
|
+
if (!high) return;
|
|
672
|
+
pbf.buf[pbf.pos++] = high & 127 | ((high >>>= 7) ? 128 : 0);
|
|
673
|
+
if (!high) return;
|
|
674
|
+
pbf.buf[pbf.pos++] = high & 127 | ((high >>>= 7) ? 128 : 0);
|
|
675
|
+
if (!high) return;
|
|
676
|
+
pbf.buf[pbf.pos++] = high & 127;
|
|
677
|
+
}
|
|
678
|
+
function makeRoomForExtraLength(startPos, len, pbf) {
|
|
679
|
+
const extraLen = len <= 16383 ? 1 : len <= 2097151 ? 2 : len <= 268435455 ? 3 : Math.floor(Math.log(len) / (Math.LN2 * 7));
|
|
680
|
+
pbf.realloc(extraLen);
|
|
681
|
+
for (let i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i];
|
|
682
|
+
}
|
|
683
|
+
function writePackedVarint(arr, pbf) {
|
|
684
|
+
for (let i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]);
|
|
685
|
+
}
|
|
686
|
+
function writePackedSVarint(arr, pbf) {
|
|
687
|
+
for (let i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]);
|
|
688
|
+
}
|
|
689
|
+
function writePackedFloat(arr, pbf) {
|
|
690
|
+
for (let i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]);
|
|
691
|
+
}
|
|
692
|
+
function writePackedDouble(arr, pbf) {
|
|
693
|
+
for (let i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]);
|
|
694
|
+
}
|
|
695
|
+
function writePackedBoolean(arr, pbf) {
|
|
696
|
+
for (let i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]);
|
|
697
|
+
}
|
|
698
|
+
function writePackedFixed32(arr, pbf) {
|
|
699
|
+
for (let i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]);
|
|
700
|
+
}
|
|
701
|
+
function writePackedSFixed32(arr, pbf) {
|
|
702
|
+
for (let i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]);
|
|
703
|
+
}
|
|
704
|
+
function writePackedFixed64(arr, pbf) {
|
|
705
|
+
for (let i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]);
|
|
706
|
+
}
|
|
707
|
+
function writePackedSFixed64(arr, pbf) {
|
|
708
|
+
for (let i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]);
|
|
709
|
+
}
|
|
710
|
+
function readUtf8(buf, pos, end) {
|
|
711
|
+
let str = "";
|
|
712
|
+
let i = pos;
|
|
713
|
+
while (i < end) {
|
|
714
|
+
const b0 = buf[i];
|
|
715
|
+
let c = null;
|
|
716
|
+
let bytesPerSequence = b0 > 239 ? 4 : b0 > 223 ? 3 : b0 > 191 ? 2 : 1;
|
|
717
|
+
if (i + bytesPerSequence > end) break;
|
|
718
|
+
let b1, b2, b3;
|
|
719
|
+
if (bytesPerSequence === 1) {
|
|
720
|
+
if (b0 < 128) {
|
|
721
|
+
c = b0;
|
|
722
|
+
}
|
|
723
|
+
} else if (bytesPerSequence === 2) {
|
|
724
|
+
b1 = buf[i + 1];
|
|
725
|
+
if ((b1 & 192) === 128) {
|
|
726
|
+
c = (b0 & 31) << 6 | b1 & 63;
|
|
727
|
+
if (c <= 127) {
|
|
728
|
+
c = null;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
} else if (bytesPerSequence === 3) {
|
|
732
|
+
b1 = buf[i + 1];
|
|
733
|
+
b2 = buf[i + 2];
|
|
734
|
+
if ((b1 & 192) === 128 && (b2 & 192) === 128) {
|
|
735
|
+
c = (b0 & 15) << 12 | (b1 & 63) << 6 | b2 & 63;
|
|
736
|
+
if (c <= 2047 || c >= 55296 && c <= 57343) {
|
|
737
|
+
c = null;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
} else if (bytesPerSequence === 4) {
|
|
741
|
+
b1 = buf[i + 1];
|
|
742
|
+
b2 = buf[i + 2];
|
|
743
|
+
b3 = buf[i + 3];
|
|
744
|
+
if ((b1 & 192) === 128 && (b2 & 192) === 128 && (b3 & 192) === 128) {
|
|
745
|
+
c = (b0 & 15) << 18 | (b1 & 63) << 12 | (b2 & 63) << 6 | b3 & 63;
|
|
746
|
+
if (c <= 65535 || c >= 1114112) {
|
|
747
|
+
c = null;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
if (c === null) {
|
|
752
|
+
c = 65533;
|
|
753
|
+
bytesPerSequence = 1;
|
|
754
|
+
} else if (c > 65535) {
|
|
755
|
+
c -= 65536;
|
|
756
|
+
str += String.fromCharCode(c >>> 10 & 1023 | 55296);
|
|
757
|
+
c = 56320 | c & 1023;
|
|
758
|
+
}
|
|
759
|
+
str += String.fromCharCode(c);
|
|
760
|
+
i += bytesPerSequence;
|
|
761
|
+
}
|
|
762
|
+
return str;
|
|
763
|
+
}
|
|
764
|
+
function writeUtf8(buf, str, pos) {
|
|
765
|
+
for (let i = 0, c, lead; i < str.length; i++) {
|
|
766
|
+
c = str.charCodeAt(i);
|
|
767
|
+
if (c > 55295 && c < 57344) {
|
|
768
|
+
if (lead) {
|
|
769
|
+
if (c < 56320) {
|
|
770
|
+
buf[pos++] = 239;
|
|
771
|
+
buf[pos++] = 191;
|
|
772
|
+
buf[pos++] = 189;
|
|
773
|
+
lead = c;
|
|
774
|
+
continue;
|
|
775
|
+
} else {
|
|
776
|
+
c = lead - 55296 << 10 | c - 56320 | 65536;
|
|
777
|
+
lead = null;
|
|
778
|
+
}
|
|
779
|
+
} else {
|
|
780
|
+
if (c > 56319 || i + 1 === str.length) {
|
|
781
|
+
buf[pos++] = 239;
|
|
782
|
+
buf[pos++] = 191;
|
|
783
|
+
buf[pos++] = 189;
|
|
784
|
+
} else {
|
|
785
|
+
lead = c;
|
|
786
|
+
}
|
|
787
|
+
continue;
|
|
788
|
+
}
|
|
789
|
+
} else if (lead) {
|
|
790
|
+
buf[pos++] = 239;
|
|
791
|
+
buf[pos++] = 191;
|
|
792
|
+
buf[pos++] = 189;
|
|
793
|
+
lead = null;
|
|
794
|
+
}
|
|
795
|
+
if (c < 128) {
|
|
796
|
+
buf[pos++] = c;
|
|
797
|
+
} else {
|
|
798
|
+
if (c < 2048) {
|
|
799
|
+
buf[pos++] = c >> 6 | 192;
|
|
800
|
+
} else {
|
|
801
|
+
if (c < 65536) {
|
|
802
|
+
buf[pos++] = c >> 12 | 224;
|
|
803
|
+
} else {
|
|
804
|
+
buf[pos++] = c >> 18 | 240;
|
|
805
|
+
buf[pos++] = c >> 12 & 63 | 128;
|
|
806
|
+
}
|
|
807
|
+
buf[pos++] = c >> 6 & 63 | 128;
|
|
808
|
+
}
|
|
809
|
+
buf[pos++] = c & 63 | 128;
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
return pos;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
// src/browser/crx3.ts
|
|
816
|
+
function readCrxFileHeader(pbf, end) {
|
|
817
|
+
return pbf.readFields(
|
|
818
|
+
readCrxFileHeaderField,
|
|
819
|
+
{
|
|
820
|
+
sha256_with_rsa: [],
|
|
821
|
+
sha256_with_ecdsa: [],
|
|
822
|
+
verified_contents: void 0,
|
|
823
|
+
signed_header_data: void 0
|
|
824
|
+
},
|
|
825
|
+
end
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
function readCrxFileHeaderField(tag, obj, pbf) {
|
|
829
|
+
if (tag === 2) obj.sha256_with_rsa.push(readAsymmetricKeyProof(pbf, pbf.readVarint() + pbf.pos));
|
|
830
|
+
else if (tag === 3)
|
|
831
|
+
obj.sha256_with_ecdsa.push(readAsymmetricKeyProof(pbf, pbf.readVarint() + pbf.pos));
|
|
832
|
+
else if (tag === 4) obj.verified_contents = pbf.readBytes();
|
|
833
|
+
else if (tag === 1e4) obj.signed_header_data = pbf.readBytes();
|
|
834
|
+
}
|
|
835
|
+
function readAsymmetricKeyProof(pbf, end) {
|
|
836
|
+
return pbf.readFields(
|
|
837
|
+
readAsymmetricKeyProofField,
|
|
838
|
+
{ public_key: void 0, signature: void 0 },
|
|
839
|
+
end
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
function readAsymmetricKeyProofField(tag, obj, pbf) {
|
|
843
|
+
if (tag === 1) obj.public_key = pbf.readBytes();
|
|
844
|
+
else if (tag === 2) obj.signature = pbf.readBytes();
|
|
845
|
+
}
|
|
846
|
+
function readSignedData(pbf, end) {
|
|
847
|
+
return pbf.readFields(readSignedDataField, { crx_id: void 0 }, end);
|
|
848
|
+
}
|
|
849
|
+
function readSignedDataField(tag, obj, pbf) {
|
|
850
|
+
if (tag === 1) obj.crx_id = pbf.readBytes();
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// src/browser/id.ts
|
|
854
|
+
var import_node_crypto = require("node:crypto");
|
|
855
|
+
function convertHexadecimalToIDAlphabet(id) {
|
|
856
|
+
let result = "";
|
|
857
|
+
for (const ch of id) {
|
|
858
|
+
const val = parseInt(ch, 16);
|
|
859
|
+
if (!isNaN(val)) {
|
|
860
|
+
result += String.fromCharCode("a".charCodeAt(0) + val);
|
|
861
|
+
} else {
|
|
862
|
+
result += "a";
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
return result;
|
|
866
|
+
}
|
|
867
|
+
function generateIdFromHash(hash) {
|
|
868
|
+
const hashedId = hash.subarray(0, 16).toString("hex");
|
|
869
|
+
return convertHexadecimalToIDAlphabet(hashedId);
|
|
870
|
+
}
|
|
871
|
+
function generateId(input) {
|
|
872
|
+
const hash = (0, import_node_crypto.createHash)("sha256").update(input, "base64").digest();
|
|
873
|
+
return generateIdFromHash(hash);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
// src/browser/loader.ts
|
|
877
|
+
var fs = __toESM(require("node:fs"));
|
|
878
|
+
var path2 = __toESM(require("node:path"));
|
|
879
|
+
var import_debug = __toESM(require("debug"));
|
|
880
|
+
var d = (0, import_debug.default)("electron-chrome-web-store:loader");
|
|
881
|
+
var manifestExists = async (dirPath) => {
|
|
882
|
+
if (!dirPath) return false;
|
|
883
|
+
const manifestPath = path2.join(dirPath, "manifest.json");
|
|
884
|
+
try {
|
|
885
|
+
return (await fs.promises.stat(manifestPath)).isFile();
|
|
886
|
+
} catch {
|
|
887
|
+
return false;
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
async function extensionSearch(dirPath, depth = 0) {
|
|
891
|
+
if (depth >= 2) return [];
|
|
892
|
+
const results = [];
|
|
893
|
+
const dirEntries = await fs.promises.readdir(dirPath, { withFileTypes: true });
|
|
894
|
+
for (const entry of dirEntries) {
|
|
895
|
+
if (entry.isDirectory()) {
|
|
896
|
+
if (await manifestExists(path2.join(dirPath, entry.name))) {
|
|
897
|
+
results.push(path2.join(dirPath, entry.name));
|
|
898
|
+
} else {
|
|
899
|
+
results.push(...await extensionSearch(path2.join(dirPath, entry.name), depth + 1));
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
return results;
|
|
904
|
+
}
|
|
905
|
+
async function discoverExtensions(extensionsPath) {
|
|
906
|
+
try {
|
|
907
|
+
const stat = await fs.promises.stat(extensionsPath);
|
|
908
|
+
if (!stat.isDirectory()) {
|
|
909
|
+
d("%s is not a directory", extensionsPath);
|
|
910
|
+
return [];
|
|
911
|
+
}
|
|
912
|
+
} catch {
|
|
913
|
+
d("%s does not exist", extensionsPath);
|
|
914
|
+
return [];
|
|
915
|
+
}
|
|
916
|
+
const extensionDirectories = await extensionSearch(extensionsPath);
|
|
917
|
+
const results = [];
|
|
918
|
+
for (const extPath of extensionDirectories.filter(Boolean)) {
|
|
919
|
+
try {
|
|
920
|
+
const manifestPath = path2.join(extPath, "manifest.json");
|
|
921
|
+
const manifestJson = (await fs.promises.readFile(manifestPath)).toString();
|
|
922
|
+
const manifest = JSON.parse(manifestJson);
|
|
923
|
+
const result = manifest.key ? {
|
|
924
|
+
type: "store",
|
|
925
|
+
path: extPath,
|
|
926
|
+
manifest,
|
|
927
|
+
id: generateId(manifest.key)
|
|
928
|
+
} : {
|
|
929
|
+
type: "unpacked",
|
|
930
|
+
path: extPath,
|
|
931
|
+
manifest
|
|
932
|
+
};
|
|
933
|
+
results.push(result);
|
|
934
|
+
} catch (e) {
|
|
935
|
+
console.error(e);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
return results;
|
|
939
|
+
}
|
|
940
|
+
function filterOutdatedExtensions(extensions) {
|
|
941
|
+
const uniqueExtensions = [];
|
|
942
|
+
const storeExtMap = /* @__PURE__ */ new Map();
|
|
943
|
+
for (const ext of extensions) {
|
|
944
|
+
if (ext.type === "unpacked") {
|
|
945
|
+
uniqueExtensions.push(ext);
|
|
946
|
+
} else if (!storeExtMap.has(ext.id)) {
|
|
947
|
+
storeExtMap.set(ext.id, ext);
|
|
948
|
+
} else {
|
|
949
|
+
const latestExt = storeExtMap.get(ext.id);
|
|
950
|
+
if (compareVersions(latestExt.manifest.version, ext.manifest.version) < 0) {
|
|
951
|
+
storeExtMap.set(ext.id, ext);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
storeExtMap.forEach((ext) => uniqueExtensions.push(ext));
|
|
956
|
+
return uniqueExtensions;
|
|
957
|
+
}
|
|
958
|
+
async function loadAllExtensions(session, extensionsPath, options = {}) {
|
|
959
|
+
const sessionExtensions = session.extensions || session;
|
|
960
|
+
let extensions = await discoverExtensions(extensionsPath);
|
|
961
|
+
extensions = filterOutdatedExtensions(extensions);
|
|
962
|
+
d("discovered %d extension(s) in %s", extensions.length, extensionsPath);
|
|
963
|
+
for (const ext of extensions) {
|
|
964
|
+
try {
|
|
965
|
+
let extension;
|
|
966
|
+
if (ext.type === "store") {
|
|
967
|
+
const existingExt = sessionExtensions.getExtension(ext.id);
|
|
968
|
+
if (existingExt) {
|
|
969
|
+
d("skipping loading existing extension %s", ext.id);
|
|
970
|
+
continue;
|
|
971
|
+
}
|
|
972
|
+
d("loading extension %s", `${ext.id}@${ext.manifest.version}`);
|
|
973
|
+
extension = await sessionExtensions.loadExtension(ext.path);
|
|
974
|
+
} else if (options.allowUnpacked) {
|
|
975
|
+
d("loading unpacked extension %s", ext.path);
|
|
976
|
+
extension = await sessionExtensions.loadExtension(ext.path);
|
|
977
|
+
}
|
|
978
|
+
if (extension && extension.manifest.manifest_version === 3 && extension.manifest.background?.service_worker) {
|
|
979
|
+
const scope = `chrome-extension://${extension.id}`;
|
|
980
|
+
await session.serviceWorkers.startWorkerForScope(scope).catch(() => {
|
|
981
|
+
console.error(`Failed to start worker for extension ${extension.id}`);
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
} catch (error) {
|
|
985
|
+
console.error(`Failed to load extension from ${ext.path}`);
|
|
986
|
+
console.error(error);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
async function findExtensionInstall(extensionId, extensionsPath) {
|
|
991
|
+
const extensionPath = path2.join(extensionsPath, extensionId);
|
|
992
|
+
let extensions = await discoverExtensions(extensionPath);
|
|
993
|
+
extensions = filterOutdatedExtensions(extensions);
|
|
994
|
+
return extensions.length > 0 ? extensions[0] : null;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
// src/browser/installer.ts
|
|
998
|
+
var d2 = (0, import_debug2.default)("electron-chrome-web-store:installer");
|
|
999
|
+
function getExtensionCrxURL(extensionId) {
|
|
1000
|
+
const url = new URL("https://clients2.google.com/service/update2/crx");
|
|
1001
|
+
url.searchParams.append("response", "redirect");
|
|
1002
|
+
url.searchParams.append("acceptformat", ["crx2", "crx3"].join(","));
|
|
1003
|
+
const x = new URLSearchParams();
|
|
1004
|
+
x.append("id", extensionId);
|
|
1005
|
+
x.append("uc", "");
|
|
1006
|
+
url.searchParams.append("x", x.toString());
|
|
1007
|
+
url.searchParams.append("prodversion", getChromeVersion());
|
|
1008
|
+
return url.toString();
|
|
1009
|
+
}
|
|
1010
|
+
function parseCrx(buffer) {
|
|
1011
|
+
const magicNumber = buffer.toString("utf8", 0, 4);
|
|
1012
|
+
if (magicNumber !== "Cr24") {
|
|
1013
|
+
throw new Error("Invalid CRX format");
|
|
1014
|
+
}
|
|
1015
|
+
const version = buffer.readUInt32LE(4);
|
|
1016
|
+
const headerSize = buffer.readUInt32LE(8);
|
|
1017
|
+
const header = buffer.subarray(12, 12 + headerSize);
|
|
1018
|
+
const contents = buffer.subarray(12 + headerSize);
|
|
1019
|
+
let extensionId;
|
|
1020
|
+
let publicKey;
|
|
1021
|
+
if (version === 2) {
|
|
1022
|
+
const pubKeyLength = buffer.readUInt32LE(8);
|
|
1023
|
+
const sigLength = buffer.readUInt32LE(12);
|
|
1024
|
+
publicKey = buffer.subarray(16, 16 + pubKeyLength);
|
|
1025
|
+
extensionId = generateId(publicKey.toString("base64"));
|
|
1026
|
+
} else {
|
|
1027
|
+
const crxFileHeader = readCrxFileHeader(new Pbf(header));
|
|
1028
|
+
const crxSignedData = readSignedData(new Pbf(crxFileHeader.signed_header_data));
|
|
1029
|
+
const declaredCrxId = crxSignedData.crx_id ? convertHexadecimalToIDAlphabet(crxSignedData.crx_id.toString("hex")) : null;
|
|
1030
|
+
if (!declaredCrxId) {
|
|
1031
|
+
throw new Error("Invalid CRX signed data");
|
|
1032
|
+
}
|
|
1033
|
+
const keyProof = crxFileHeader.sha256_with_rsa.find((proof) => {
|
|
1034
|
+
const crxId = proof.public_key ? generateId(proof.public_key.toString("base64")) : null;
|
|
1035
|
+
return crxId === declaredCrxId;
|
|
1036
|
+
});
|
|
1037
|
+
if (!keyProof) {
|
|
1038
|
+
throw new Error("Invalid CRX key");
|
|
1039
|
+
}
|
|
1040
|
+
extensionId = declaredCrxId;
|
|
1041
|
+
publicKey = keyProof.public_key;
|
|
1042
|
+
}
|
|
1043
|
+
return {
|
|
1044
|
+
extensionId,
|
|
1045
|
+
version,
|
|
1046
|
+
header,
|
|
1047
|
+
contents,
|
|
1048
|
+
publicKey
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
async function unpackCrx(crx, destPath) {
|
|
1052
|
+
const zip = new import_adm_zip.default(crx.contents);
|
|
1053
|
+
zip.extractAllTo(destPath, true);
|
|
1054
|
+
const manifestPath = path3.join(destPath, "manifest.json");
|
|
1055
|
+
const manifestContent = await fs2.promises.readFile(manifestPath, "utf8");
|
|
1056
|
+
const manifest = JSON.parse(manifestContent);
|
|
1057
|
+
manifest.key = crx.publicKey.toString("base64");
|
|
1058
|
+
await fs2.promises.writeFile(manifestPath, JSON.stringify(manifest, null, 2));
|
|
1059
|
+
return manifest;
|
|
1060
|
+
}
|
|
1061
|
+
async function readCrx(crxPath) {
|
|
1062
|
+
const crxBuffer = await fs2.promises.readFile(crxPath);
|
|
1063
|
+
return parseCrx(crxBuffer);
|
|
1064
|
+
}
|
|
1065
|
+
async function downloadCrx(url, dest) {
|
|
1066
|
+
const response = await fetch(url);
|
|
1067
|
+
if (!response.ok) {
|
|
1068
|
+
throw new Error("Failed to download extension");
|
|
1069
|
+
}
|
|
1070
|
+
const fileStream = fs2.createWriteStream(dest);
|
|
1071
|
+
const downloadStream = import_node_stream.Readable.fromWeb(response.body);
|
|
1072
|
+
await (0, import_promises.pipeline)(downloadStream, fileStream);
|
|
1073
|
+
}
|
|
1074
|
+
async function downloadExtensionFromURL(url, extensionsDir, expectedExtensionId) {
|
|
1075
|
+
d2("downloading %s", url);
|
|
1076
|
+
const installUuid = crypto.randomUUID();
|
|
1077
|
+
const crxPath = path3.join(os.tmpdir(), `electron-cws-download_${installUuid}.crx`);
|
|
1078
|
+
try {
|
|
1079
|
+
await downloadCrx(url, crxPath);
|
|
1080
|
+
const crx = await readCrx(crxPath);
|
|
1081
|
+
if (expectedExtensionId && expectedExtensionId !== crx.extensionId) {
|
|
1082
|
+
throw new Error(
|
|
1083
|
+
`CRX mismatches expected extension ID: ${expectedExtensionId} !== ${crx.extensionId}`
|
|
1084
|
+
);
|
|
1085
|
+
}
|
|
1086
|
+
const unpackedPath = path3.join(extensionsDir, crx.extensionId, installUuid);
|
|
1087
|
+
await fs2.promises.mkdir(unpackedPath, { recursive: true });
|
|
1088
|
+
const manifest = await unpackCrx(crx, unpackedPath);
|
|
1089
|
+
if (!manifest.version) {
|
|
1090
|
+
throw new Error("Installed extension is missing manifest version");
|
|
1091
|
+
}
|
|
1092
|
+
const versionedPath = path3.join(extensionsDir, crx.extensionId, `${manifest.version}_0`);
|
|
1093
|
+
await fs2.promises.rename(unpackedPath, versionedPath);
|
|
1094
|
+
return versionedPath;
|
|
1095
|
+
} finally {
|
|
1096
|
+
await fs2.promises.rm(crxPath, { force: true });
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
async function downloadExtension(extensionId, extensionsDir) {
|
|
1100
|
+
const url = getExtensionCrxURL(extensionId);
|
|
1101
|
+
return await downloadExtensionFromURL(url, extensionsDir, extensionId);
|
|
1102
|
+
}
|
|
1103
|
+
async function installExtension(extensionId, opts = {}) {
|
|
1104
|
+
d2("installing %s", extensionId);
|
|
1105
|
+
const session = opts.session || import_electron2.session.defaultSession;
|
|
1106
|
+
const sessionExtensions = session.extensions || session;
|
|
1107
|
+
const extensionsPath = opts.extensionsPath || getDefaultExtensionsPath();
|
|
1108
|
+
const existingExtension = sessionExtensions.getExtension(extensionId);
|
|
1109
|
+
if (existingExtension) {
|
|
1110
|
+
d2("%s already loaded", extensionId);
|
|
1111
|
+
return existingExtension;
|
|
1112
|
+
}
|
|
1113
|
+
const existingExtensionInfo = await findExtensionInstall(extensionId, extensionsPath);
|
|
1114
|
+
if (existingExtensionInfo && existingExtensionInfo.type === "store") {
|
|
1115
|
+
d2("%s already installed", extensionId);
|
|
1116
|
+
return await sessionExtensions.loadExtension(
|
|
1117
|
+
existingExtensionInfo.path,
|
|
1118
|
+
opts.loadExtensionOptions
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
const extensionPath = await downloadExtension(extensionId, extensionsPath);
|
|
1122
|
+
const extension = await sessionExtensions.loadExtension(extensionPath, opts.loadExtensionOptions);
|
|
1123
|
+
d2("installed %s", extensionId);
|
|
1124
|
+
return extension;
|
|
1125
|
+
}
|
|
1126
|
+
async function uninstallExtension(extensionId, opts = {}) {
|
|
1127
|
+
d2("uninstalling %s", extensionId);
|
|
1128
|
+
const session = opts.session || import_electron2.session.defaultSession;
|
|
1129
|
+
const sessionExtensions = session.extensions || session;
|
|
1130
|
+
const extensionsPath = opts.extensionsPath || getDefaultExtensionsPath();
|
|
1131
|
+
const extensions = sessionExtensions.getAllExtensions();
|
|
1132
|
+
const existingExt = extensions.find((ext) => ext.id === extensionId);
|
|
1133
|
+
if (existingExt) {
|
|
1134
|
+
sessionExtensions.removeExtension(extensionId);
|
|
1135
|
+
}
|
|
1136
|
+
const extensionDir = path3.join(extensionsPath, extensionId);
|
|
1137
|
+
try {
|
|
1138
|
+
const stat = await fs2.promises.stat(extensionDir);
|
|
1139
|
+
if (stat.isDirectory()) {
|
|
1140
|
+
await fs2.promises.rm(extensionDir, { recursive: true, force: true });
|
|
1141
|
+
}
|
|
1142
|
+
} catch (error) {
|
|
1143
|
+
if (error?.code !== "ENOENT") {
|
|
1144
|
+
throw error;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
// src/browser/api.ts
|
|
1150
|
+
var d3 = (0, import_debug3.default)("electron-chrome-web-store:api");
|
|
1151
|
+
var WEBSTORE_URL = "https://chromewebstore.google.com";
|
|
1152
|
+
function getExtensionInfo(ext) {
|
|
1153
|
+
const manifest = ext.manifest;
|
|
1154
|
+
return {
|
|
1155
|
+
description: manifest.description || "",
|
|
1156
|
+
enabled: !manifest.disabled,
|
|
1157
|
+
homepageUrl: manifest.homepage_url || "",
|
|
1158
|
+
hostPermissions: manifest.host_permissions || [],
|
|
1159
|
+
icons: Object.entries(manifest?.icons || {}).map(([size, url]) => ({
|
|
1160
|
+
size: parseInt(size),
|
|
1161
|
+
url: `chrome://extension-icon/${ext.id}/${size}/0`
|
|
1162
|
+
})),
|
|
1163
|
+
id: ext.id,
|
|
1164
|
+
installType: "normal",
|
|
1165
|
+
isApp: !!manifest.app,
|
|
1166
|
+
mayDisable: true,
|
|
1167
|
+
name: manifest.name,
|
|
1168
|
+
offlineEnabled: !!manifest.offline_enabled,
|
|
1169
|
+
optionsUrl: manifest.options_page ? `chrome-extension://${ext.id}/${manifest.options_page}` : "",
|
|
1170
|
+
permissions: manifest.permissions || [],
|
|
1171
|
+
shortName: manifest.short_name || manifest.name,
|
|
1172
|
+
type: manifest.app ? "app" : "extension",
|
|
1173
|
+
updateUrl: manifest.update_url || "",
|
|
1174
|
+
version: manifest.version
|
|
1175
|
+
};
|
|
1176
|
+
}
|
|
1177
|
+
function getExtensionInstallStatus(state, extensionId, manifest) {
|
|
1178
|
+
if (manifest && manifest.manifest_version < state.minimumManifestVersion) {
|
|
1179
|
+
return ExtensionInstallStatus.DEPRECATED_MANIFEST_VERSION;
|
|
1180
|
+
}
|
|
1181
|
+
if (state.denylist?.has(extensionId)) {
|
|
1182
|
+
return ExtensionInstallStatus.BLOCKED_BY_POLICY;
|
|
1183
|
+
}
|
|
1184
|
+
if (state.allowlist && !state.allowlist.has(extensionId)) {
|
|
1185
|
+
return ExtensionInstallStatus.BLOCKED_BY_POLICY;
|
|
1186
|
+
}
|
|
1187
|
+
const sessionExtensions = state.session.extensions || state.session;
|
|
1188
|
+
const extensions = sessionExtensions.getAllExtensions();
|
|
1189
|
+
const extension = extensions.find((ext) => ext.id === extensionId);
|
|
1190
|
+
if (!extension) {
|
|
1191
|
+
return ExtensionInstallStatus.INSTALLABLE;
|
|
1192
|
+
}
|
|
1193
|
+
if (extension.manifest.disabled) {
|
|
1194
|
+
return ExtensionInstallStatus.DISABLED;
|
|
1195
|
+
}
|
|
1196
|
+
return ExtensionInstallStatus.ENABLED;
|
|
1197
|
+
}
|
|
1198
|
+
async function beginInstall({ sender, senderFrame }, state, details) {
|
|
1199
|
+
const extensionId = details.id;
|
|
1200
|
+
try {
|
|
1201
|
+
if (state.installing.has(extensionId)) {
|
|
1202
|
+
return { result: Result.INSTALL_IN_PROGRESS };
|
|
1203
|
+
}
|
|
1204
|
+
let manifest;
|
|
1205
|
+
try {
|
|
1206
|
+
manifest = JSON.parse(details.manifest);
|
|
1207
|
+
} catch {
|
|
1208
|
+
return { result: Result.MANIFEST_ERROR };
|
|
1209
|
+
}
|
|
1210
|
+
const installStatus = getExtensionInstallStatus(state, extensionId, manifest);
|
|
1211
|
+
switch (installStatus) {
|
|
1212
|
+
case ExtensionInstallStatus.INSTALLABLE:
|
|
1213
|
+
break;
|
|
1214
|
+
// good to go
|
|
1215
|
+
case ExtensionInstallStatus.BLOCKED_BY_POLICY:
|
|
1216
|
+
return { result: Result.BLOCKED_BY_POLICY };
|
|
1217
|
+
default: {
|
|
1218
|
+
d3('unable to install extension %s with status "%s"', extensionId, installStatus);
|
|
1219
|
+
return { result: Result.UNKNOWN_ERROR };
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
let iconUrl;
|
|
1223
|
+
try {
|
|
1224
|
+
iconUrl = new URL(details.iconUrl);
|
|
1225
|
+
} catch {
|
|
1226
|
+
return { result: Result.INVALID_ICON_URL };
|
|
1227
|
+
}
|
|
1228
|
+
let icon;
|
|
1229
|
+
try {
|
|
1230
|
+
const response = await fetch(iconUrl.href);
|
|
1231
|
+
const imageBuffer = Buffer.from(await response.arrayBuffer());
|
|
1232
|
+
icon = import_electron3.nativeImage.createFromBuffer(imageBuffer);
|
|
1233
|
+
} catch {
|
|
1234
|
+
return { result: Result.ICON_ERROR };
|
|
1235
|
+
}
|
|
1236
|
+
const browserWindow = import_electron3.BrowserWindow.fromWebContents(sender);
|
|
1237
|
+
if (!senderFrame || senderFrame.isDestroyed()) {
|
|
1238
|
+
return { result: Result.UNKNOWN_ERROR };
|
|
1239
|
+
}
|
|
1240
|
+
if (state.beforeInstall) {
|
|
1241
|
+
const result = await state.beforeInstall({
|
|
1242
|
+
id: extensionId,
|
|
1243
|
+
localizedName: details.localizedName,
|
|
1244
|
+
manifest,
|
|
1245
|
+
icon,
|
|
1246
|
+
frame: senderFrame,
|
|
1247
|
+
browserWindow: browserWindow || void 0
|
|
1248
|
+
});
|
|
1249
|
+
if (typeof result !== "object" || typeof result.action !== "string") {
|
|
1250
|
+
return { result: Result.UNKNOWN_ERROR };
|
|
1251
|
+
} else if (result.action !== "allow") {
|
|
1252
|
+
return { result: Result.USER_CANCELLED };
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
state.installing.add(extensionId);
|
|
1256
|
+
await installExtension(extensionId, state);
|
|
1257
|
+
return { result: Result.SUCCESS };
|
|
1258
|
+
} catch (error) {
|
|
1259
|
+
console.error("Extension installation failed:", error);
|
|
1260
|
+
return {
|
|
1261
|
+
result: Result.INSTALL_ERROR,
|
|
1262
|
+
message: error instanceof Error ? error.message : String(error)
|
|
1263
|
+
};
|
|
1264
|
+
} finally {
|
|
1265
|
+
state.installing.delete(extensionId);
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
var handledIpcChannels = /* @__PURE__ */ new Map();
|
|
1269
|
+
function registerWebStoreApi(webStoreState) {
|
|
1270
|
+
const handle = (channel, handle2) => {
|
|
1271
|
+
let handlersMap = handledIpcChannels.get(channel);
|
|
1272
|
+
if (!handlersMap) {
|
|
1273
|
+
handlersMap = /* @__PURE__ */ new Map();
|
|
1274
|
+
handledIpcChannels.set(channel, handlersMap);
|
|
1275
|
+
import_electron3.ipcMain.handle(channel, async function handleWebStoreIpc(event, ...args) {
|
|
1276
|
+
d3("received %s", channel);
|
|
1277
|
+
const senderOrigin = event.senderFrame?.origin;
|
|
1278
|
+
if (!senderOrigin || !senderOrigin.startsWith(WEBSTORE_URL)) {
|
|
1279
|
+
d3("ignoring webstore request from %s", senderOrigin);
|
|
1280
|
+
return;
|
|
1281
|
+
}
|
|
1282
|
+
const session = event.sender.session;
|
|
1283
|
+
const handler = handlersMap?.get(session);
|
|
1284
|
+
if (!handler) {
|
|
1285
|
+
d3("no handler for session %s", session.storagePath);
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
const result = await handler(event, ...args);
|
|
1289
|
+
d3("%s result", channel, result);
|
|
1290
|
+
return result;
|
|
1291
|
+
});
|
|
1292
|
+
}
|
|
1293
|
+
handlersMap.set(webStoreState.session, handle2);
|
|
1294
|
+
};
|
|
1295
|
+
handle("chromeWebstore.beginInstall", async (event, details) => {
|
|
1296
|
+
const { senderFrame } = event;
|
|
1297
|
+
d3("beginInstall", details);
|
|
1298
|
+
const result = await beginInstall(event, webStoreState, details);
|
|
1299
|
+
if (result.result === Result.SUCCESS) {
|
|
1300
|
+
queueMicrotask(() => {
|
|
1301
|
+
const sessionExtensions = webStoreState.session.extensions || webStoreState.session;
|
|
1302
|
+
const ext = sessionExtensions.getExtension(details.id);
|
|
1303
|
+
if (ext && senderFrame && !senderFrame.isDestroyed()) {
|
|
1304
|
+
try {
|
|
1305
|
+
senderFrame.send("chrome.management.onInstalled", getExtensionInfo(ext));
|
|
1306
|
+
} catch (error) {
|
|
1307
|
+
console.error(error);
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1312
|
+
return result;
|
|
1313
|
+
});
|
|
1314
|
+
handle("chromeWebstore.completeInstall", async (event, id) => {
|
|
1315
|
+
return Result.SUCCESS;
|
|
1316
|
+
});
|
|
1317
|
+
handle("chromeWebstore.enableAppLauncher", async (event, enable) => {
|
|
1318
|
+
return true;
|
|
1319
|
+
});
|
|
1320
|
+
handle("chromeWebstore.getBrowserLogin", async () => {
|
|
1321
|
+
return "";
|
|
1322
|
+
});
|
|
1323
|
+
handle("chromeWebstore.getExtensionStatus", async (_event, id, manifestJson) => {
|
|
1324
|
+
const manifest = JSON.parse(manifestJson);
|
|
1325
|
+
return getExtensionInstallStatus(webStoreState, id, manifest);
|
|
1326
|
+
});
|
|
1327
|
+
handle("chromeWebstore.getFullChromeVersion", async () => {
|
|
1328
|
+
return {
|
|
1329
|
+
version_number: process.versions.chrome,
|
|
1330
|
+
app_name: import_electron3.app.getName()
|
|
1331
|
+
};
|
|
1332
|
+
});
|
|
1333
|
+
handle("chromeWebstore.getIsLauncherEnabled", async () => {
|
|
1334
|
+
return true;
|
|
1335
|
+
});
|
|
1336
|
+
handle("chromeWebstore.getMV2DeprecationStatus", async () => {
|
|
1337
|
+
return webStoreState.minimumManifestVersion > 2 ? MV2DeprecationStatus.SOFT_DISABLE : MV2DeprecationStatus.INACTIVE;
|
|
1338
|
+
});
|
|
1339
|
+
handle("chromeWebstore.getReferrerChain", async () => {
|
|
1340
|
+
return "EgIIAA==";
|
|
1341
|
+
});
|
|
1342
|
+
handle("chromeWebstore.getStoreLogin", async () => {
|
|
1343
|
+
return "";
|
|
1344
|
+
});
|
|
1345
|
+
handle("chromeWebstore.getWebGLStatus", async () => {
|
|
1346
|
+
await import_electron3.app.getGPUInfo("basic");
|
|
1347
|
+
const features = import_electron3.app.getGPUFeatureStatus();
|
|
1348
|
+
return features.webgl.startsWith("enabled") ? WebGlStatus.WEBGL_ALLOWED : WebGlStatus.WEBGL_BLOCKED;
|
|
1349
|
+
});
|
|
1350
|
+
handle("chromeWebstore.install", async (event, id, silentInstall) => {
|
|
1351
|
+
return Result.SUCCESS;
|
|
1352
|
+
});
|
|
1353
|
+
handle("chromeWebstore.isInIncognitoMode", async () => {
|
|
1354
|
+
return false;
|
|
1355
|
+
});
|
|
1356
|
+
handle("chromeWebstore.isPendingCustodianApproval", async (event, id) => {
|
|
1357
|
+
return false;
|
|
1358
|
+
});
|
|
1359
|
+
handle("chromeWebstore.setStoreLogin", async (event, login) => {
|
|
1360
|
+
return true;
|
|
1361
|
+
});
|
|
1362
|
+
handle("chrome.runtime.getManifest", async () => {
|
|
1363
|
+
return {};
|
|
1364
|
+
});
|
|
1365
|
+
handle("chrome.management.getAll", async (event) => {
|
|
1366
|
+
const sessionExtensions = webStoreState.session.extensions || webStoreState.session;
|
|
1367
|
+
const extensions = sessionExtensions.getAllExtensions();
|
|
1368
|
+
return extensions.map(getExtensionInfo);
|
|
1369
|
+
});
|
|
1370
|
+
handle("chrome.management.setEnabled", async (event, id, enabled) => {
|
|
1371
|
+
return true;
|
|
1372
|
+
});
|
|
1373
|
+
handle(
|
|
1374
|
+
"chrome.management.uninstall",
|
|
1375
|
+
async (event, id, options) => {
|
|
1376
|
+
if (options?.showConfirmDialog) {
|
|
1377
|
+
}
|
|
1378
|
+
try {
|
|
1379
|
+
await uninstallExtension(id, webStoreState);
|
|
1380
|
+
queueMicrotask(() => {
|
|
1381
|
+
event.sender.send("chrome.management.onUninstalled", id);
|
|
1382
|
+
});
|
|
1383
|
+
return Result.SUCCESS;
|
|
1384
|
+
} catch (error) {
|
|
1385
|
+
console.error(error);
|
|
1386
|
+
return Result.UNKNOWN_ERROR;
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
);
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
// src/browser/updater.ts
|
|
1393
|
+
var fs3 = __toESM(require("node:fs"));
|
|
1394
|
+
var path4 = __toESM(require("node:path"));
|
|
1395
|
+
var import_debug4 = __toESM(require("debug"));
|
|
1396
|
+
var import_electron4 = require("electron");
|
|
1397
|
+
var d4 = (0, import_debug4.default)("electron-chrome-web-store:updater");
|
|
1398
|
+
var SYSTEM_IDLE_DURATION = 1 * 60 * 60 * 1e3;
|
|
1399
|
+
var UPDATE_CHECK_INTERVAL = 5 * 60 * 60 * 1e3;
|
|
1400
|
+
var MIN_UPDATE_INTERVAL = 3 * 60 * 60 * 1e3;
|
|
1401
|
+
var lastUpdateCheck;
|
|
1402
|
+
var ALLOWED_UPDATE_URLS = /* @__PURE__ */ new Set(["https://clients2.google.com/service/update2/crx"]);
|
|
1403
|
+
var getSessionId = /* @__PURE__ */ (() => {
|
|
1404
|
+
let sessionId;
|
|
1405
|
+
return () => sessionId || (sessionId = crypto.randomUUID());
|
|
1406
|
+
})();
|
|
1407
|
+
var getOmahaPlatform = () => {
|
|
1408
|
+
switch (process.platform) {
|
|
1409
|
+
case "win32":
|
|
1410
|
+
return "win";
|
|
1411
|
+
case "darwin":
|
|
1412
|
+
return "mac";
|
|
1413
|
+
default:
|
|
1414
|
+
return process.platform;
|
|
1415
|
+
}
|
|
1416
|
+
};
|
|
1417
|
+
var getOmahaArch = () => {
|
|
1418
|
+
switch (process.arch) {
|
|
1419
|
+
case "ia32":
|
|
1420
|
+
return "x86";
|
|
1421
|
+
case "x64":
|
|
1422
|
+
return "x64";
|
|
1423
|
+
default:
|
|
1424
|
+
return process.arch;
|
|
1425
|
+
}
|
|
1426
|
+
};
|
|
1427
|
+
function filterWebStoreExtension(extension) {
|
|
1428
|
+
const manifest = extension.manifest;
|
|
1429
|
+
if (!manifest) return false;
|
|
1430
|
+
return manifest.key && manifest.update_url && ALLOWED_UPDATE_URLS.has(manifest.update_url);
|
|
1431
|
+
}
|
|
1432
|
+
async function fetchAvailableUpdates(extensions) {
|
|
1433
|
+
if (extensions.length === 0) return [];
|
|
1434
|
+
const extensionIds = extensions.map((extension) => extension.id);
|
|
1435
|
+
const extensionMap = extensions.reduce(
|
|
1436
|
+
(map, ext) => ({
|
|
1437
|
+
...map,
|
|
1438
|
+
[ext.id]: ext
|
|
1439
|
+
}),
|
|
1440
|
+
{}
|
|
1441
|
+
);
|
|
1442
|
+
const chromeVersion = getChromeVersion();
|
|
1443
|
+
const url = "https://update.googleapis.com/service/update2/json";
|
|
1444
|
+
const body = {
|
|
1445
|
+
request: {
|
|
1446
|
+
"@updater": "electron-chrome-web-store",
|
|
1447
|
+
acceptformat: "crx3",
|
|
1448
|
+
app: [
|
|
1449
|
+
...extensions.map((extension) => ({
|
|
1450
|
+
appid: extension.id,
|
|
1451
|
+
updatecheck: {}
|
|
1452
|
+
// API always reports 'noupdate' when version is set :thinking:
|
|
1453
|
+
// version: extension.version,
|
|
1454
|
+
}))
|
|
1455
|
+
],
|
|
1456
|
+
os: {
|
|
1457
|
+
platform: getOmahaPlatform(),
|
|
1458
|
+
arch: getOmahaArch()
|
|
1459
|
+
},
|
|
1460
|
+
prodversion: chromeVersion,
|
|
1461
|
+
protocol: "3.1",
|
|
1462
|
+
requestid: crypto.randomUUID(),
|
|
1463
|
+
sessionid: getSessionId(),
|
|
1464
|
+
testsource: process.env.NODE_ENV === "production" ? "" : "electron_dev"
|
|
1465
|
+
}
|
|
1466
|
+
};
|
|
1467
|
+
const response = await fetch(url, {
|
|
1468
|
+
method: "POST",
|
|
1469
|
+
headers: {
|
|
1470
|
+
"content-type": "application/json",
|
|
1471
|
+
"X-Goog-Update-Interactivity": "bg",
|
|
1472
|
+
"X-Goog-Update-AppId": extensionIds.join(","),
|
|
1473
|
+
"X-Goog-Update-Updater": `chromiumcrx-${chromeVersion}`
|
|
1474
|
+
},
|
|
1475
|
+
body: JSON.stringify(body)
|
|
1476
|
+
});
|
|
1477
|
+
if (!response.ok) {
|
|
1478
|
+
d4("update response not ok");
|
|
1479
|
+
return [];
|
|
1480
|
+
}
|
|
1481
|
+
const text = await response.text();
|
|
1482
|
+
const prefix = `)]}'
|
|
1483
|
+
`;
|
|
1484
|
+
if (!text.startsWith(prefix)) {
|
|
1485
|
+
d4("unexpected update response: %s", text);
|
|
1486
|
+
return [];
|
|
1487
|
+
}
|
|
1488
|
+
const json = text.substring(prefix.length);
|
|
1489
|
+
const result = JSON.parse(json);
|
|
1490
|
+
let updates;
|
|
1491
|
+
try {
|
|
1492
|
+
const apps = result?.response?.app || [];
|
|
1493
|
+
updates = apps.filter((app5) => app5.updatecheck.status === "ok").map((app5) => {
|
|
1494
|
+
const extensionId = app5.appid;
|
|
1495
|
+
const extension = extensionMap[extensionId];
|
|
1496
|
+
const manifest = app5.updatecheck.manifest;
|
|
1497
|
+
const pkg = manifest.packages.package[0];
|
|
1498
|
+
return {
|
|
1499
|
+
extension,
|
|
1500
|
+
id: extensionId,
|
|
1501
|
+
version: manifest.version,
|
|
1502
|
+
name: pkg.name,
|
|
1503
|
+
url: app5.updatecheck.urls.url[0].codebase
|
|
1504
|
+
};
|
|
1505
|
+
}).filter((update) => {
|
|
1506
|
+
const extension = extensionMap[update.id];
|
|
1507
|
+
return compareVersions(extension.version, update.version) < 0;
|
|
1508
|
+
});
|
|
1509
|
+
} catch (error) {
|
|
1510
|
+
console.error("Unable to read extension updates response", error);
|
|
1511
|
+
return [];
|
|
1512
|
+
}
|
|
1513
|
+
return updates;
|
|
1514
|
+
}
|
|
1515
|
+
async function updateExtension(session, update) {
|
|
1516
|
+
const sessionExtensions = session.extensions || session;
|
|
1517
|
+
const extensionId = update.id;
|
|
1518
|
+
const oldExtension = update.extension;
|
|
1519
|
+
d4("updating %s %s -> %s", extensionId, oldExtension.version, update.version);
|
|
1520
|
+
const oldVersionDirectoryName = path4.basename(oldExtension.path);
|
|
1521
|
+
if (!oldVersionDirectoryName.startsWith(oldExtension.version)) {
|
|
1522
|
+
console.error(
|
|
1523
|
+
`updateExtension: extension ${extensionId} must conform to versioned directory names`,
|
|
1524
|
+
{
|
|
1525
|
+
oldPath: oldExtension.path
|
|
1526
|
+
}
|
|
1527
|
+
);
|
|
1528
|
+
d4("skipping %s update due to invalid install path %s", extensionId, oldExtension.path);
|
|
1529
|
+
return;
|
|
1530
|
+
}
|
|
1531
|
+
const extensionsPath = path4.join(oldExtension.path, "..", "..");
|
|
1532
|
+
const updatePath = await downloadExtensionFromURL(update.url, extensionsPath, extensionId);
|
|
1533
|
+
d4("downloaded update %s@%s", extensionId, update.version);
|
|
1534
|
+
if (sessionExtensions.getExtension(extensionId)) {
|
|
1535
|
+
sessionExtensions.removeExtension(extensionId);
|
|
1536
|
+
await sessionExtensions.loadExtension(updatePath);
|
|
1537
|
+
d4("loaded update %s@%s", extensionId, update.version);
|
|
1538
|
+
}
|
|
1539
|
+
await fs3.promises.rm(oldExtension.path, { recursive: true, force: true });
|
|
1540
|
+
}
|
|
1541
|
+
async function checkForUpdates(session) {
|
|
1542
|
+
const sessionExtensions = session.extensions || session;
|
|
1543
|
+
const extensions = sessionExtensions.getAllExtensions().filter(filterWebStoreExtension);
|
|
1544
|
+
d4("checking for updates: %s", extensions.map((ext) => `${ext.id}@${ext.version}`).join(","));
|
|
1545
|
+
const updates = await fetchAvailableUpdates(extensions);
|
|
1546
|
+
if (!updates || updates.length === 0) {
|
|
1547
|
+
d4("no updates found");
|
|
1548
|
+
return [];
|
|
1549
|
+
}
|
|
1550
|
+
return updates;
|
|
1551
|
+
}
|
|
1552
|
+
async function installUpdates(session, updates) {
|
|
1553
|
+
d4("updating %d extension(s)", updates.length);
|
|
1554
|
+
for (const update of updates) {
|
|
1555
|
+
try {
|
|
1556
|
+
await updateExtension(session, update);
|
|
1557
|
+
} catch (error) {
|
|
1558
|
+
console.error(`checkForUpdates: Error updating extension ${update.id}`);
|
|
1559
|
+
console.error(error);
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
async function updateExtensions(session = import_electron4.session.defaultSession) {
|
|
1564
|
+
const updates = await checkForUpdates(session);
|
|
1565
|
+
if (updates.length > 0) {
|
|
1566
|
+
await installUpdates(session, updates);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
async function maybeCheckForUpdates(session) {
|
|
1570
|
+
const idleState = import_electron4.powerMonitor.getSystemIdleState(SYSTEM_IDLE_DURATION);
|
|
1571
|
+
if (idleState !== "active") {
|
|
1572
|
+
d4('skipping update check while system is in "%s" idle state', idleState);
|
|
1573
|
+
return;
|
|
1574
|
+
}
|
|
1575
|
+
if (lastUpdateCheck && Date.now() - lastUpdateCheck < MIN_UPDATE_INTERVAL) {
|
|
1576
|
+
return;
|
|
1577
|
+
}
|
|
1578
|
+
lastUpdateCheck = Date.now();
|
|
1579
|
+
void updateExtensions(session);
|
|
1580
|
+
}
|
|
1581
|
+
async function initUpdater(state) {
|
|
1582
|
+
const check = () => maybeCheckForUpdates(state.session);
|
|
1583
|
+
switch (process.platform) {
|
|
1584
|
+
case "darwin":
|
|
1585
|
+
import_electron4.app.on("did-become-active", check);
|
|
1586
|
+
break;
|
|
1587
|
+
case "win32":
|
|
1588
|
+
case "linux":
|
|
1589
|
+
import_electron4.app.on("browser-window-focus", check);
|
|
1590
|
+
break;
|
|
1591
|
+
}
|
|
1592
|
+
const updateIntervalId = setInterval(check, UPDATE_CHECK_INTERVAL);
|
|
1593
|
+
check();
|
|
1594
|
+
import_electron4.app.on("before-quit", (event) => {
|
|
1595
|
+
queueMicrotask(() => {
|
|
1596
|
+
if (!event.defaultPrevented) {
|
|
1597
|
+
d4("stopping update checks");
|
|
1598
|
+
clearInterval(updateIntervalId);
|
|
1599
|
+
}
|
|
1600
|
+
});
|
|
1601
|
+
});
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
// src/browser/index.ts
|
|
1605
|
+
function resolvePreloadPath(modulePath) {
|
|
1606
|
+
try {
|
|
1607
|
+
return (0, import_node_module.createRequire)(__dirname).resolve("electron-chrome-web-store/preload");
|
|
1608
|
+
} catch (error) {
|
|
1609
|
+
if (process.env.NODE_ENV !== "production") {
|
|
1610
|
+
console.error(error);
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
const preloadFilename = "chrome-web-store.preload.js";
|
|
1614
|
+
if (modulePath) {
|
|
1615
|
+
process.emitWarning(
|
|
1616
|
+
'electron-chrome-web-store: "modulePath" is deprecated and will be removed in future versions.',
|
|
1617
|
+
{ type: "DeprecationWarning" }
|
|
1618
|
+
);
|
|
1619
|
+
return path5.join(modulePath, "dist", preloadFilename);
|
|
1620
|
+
}
|
|
1621
|
+
return path5.join(__dirname, preloadFilename);
|
|
1622
|
+
}
|
|
1623
|
+
async function installChromeWebStore(opts = {}) {
|
|
1624
|
+
const session = opts.session || import_electron5.session.defaultSession;
|
|
1625
|
+
const extensionsPath = opts.extensionsPath || getDefaultExtensionsPath();
|
|
1626
|
+
const loadExtensions = typeof opts.loadExtensions === "boolean" ? opts.loadExtensions : true;
|
|
1627
|
+
const allowUnpackedExtensions = typeof opts.allowUnpackedExtensions === "boolean" ? opts.allowUnpackedExtensions : false;
|
|
1628
|
+
const autoUpdate = typeof opts.autoUpdate === "boolean" ? opts.autoUpdate : true;
|
|
1629
|
+
const minimumManifestVersion = typeof opts.minimumManifestVersion === "number" ? opts.minimumManifestVersion : 3;
|
|
1630
|
+
const beforeInstall = typeof opts.beforeInstall === "function" ? opts.beforeInstall : void 0;
|
|
1631
|
+
const webStoreState = {
|
|
1632
|
+
session,
|
|
1633
|
+
extensionsPath,
|
|
1634
|
+
installing: /* @__PURE__ */ new Set(),
|
|
1635
|
+
allowlist: opts.allowlist ? new Set(opts.allowlist) : void 0,
|
|
1636
|
+
denylist: opts.denylist ? new Set(opts.denylist) : void 0,
|
|
1637
|
+
minimumManifestVersion,
|
|
1638
|
+
beforeInstall
|
|
1639
|
+
};
|
|
1640
|
+
const preloadPath = resolvePreloadPath(opts.modulePath);
|
|
1641
|
+
if ("registerPreloadScript" in session) {
|
|
1642
|
+
session.registerPreloadScript({
|
|
1643
|
+
id: "electron-chrome-web-store",
|
|
1644
|
+
type: "frame",
|
|
1645
|
+
filePath: preloadPath
|
|
1646
|
+
});
|
|
1647
|
+
} else {
|
|
1648
|
+
session.setPreloads([...session.getPreloads(), preloadPath]);
|
|
1649
|
+
}
|
|
1650
|
+
if (!(0, import_node_fs.existsSync)(preloadPath)) {
|
|
1651
|
+
console.error(
|
|
1652
|
+
new Error(
|
|
1653
|
+
`electron-chrome-web-store: Preload file not found at "${preloadPath}". See "Packaging the preload script" in the readme.`
|
|
1654
|
+
)
|
|
1655
|
+
);
|
|
1656
|
+
}
|
|
1657
|
+
registerWebStoreApi(webStoreState);
|
|
1658
|
+
await import_electron5.app.whenReady();
|
|
1659
|
+
if (loadExtensions) {
|
|
1660
|
+
await loadAllExtensions(session, extensionsPath, { allowUnpacked: allowUnpackedExtensions });
|
|
1661
|
+
}
|
|
1662
|
+
if (autoUpdate) {
|
|
1663
|
+
void initUpdater(webStoreState);
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1667
|
+
0 && (module.exports = {
|
|
1668
|
+
downloadExtension,
|
|
1669
|
+
installChromeWebStore,
|
|
1670
|
+
installExtension,
|
|
1671
|
+
loadAllExtensions,
|
|
1672
|
+
uninstallExtension,
|
|
1673
|
+
updateExtensions
|
|
1674
|
+
});
|