@opencode/browse 0.0.18-dev.192 → 0.0.18-dev.198
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/dist/cli.mjs +62 -107
- package/package.json +13 -33
- package/dist/Runtime-tvxdkp60.mjs +0 -16
- package/dist/THIRD_PARTY_NOTICES.txt +0 -310
- package/dist/Undici-xgvbtsb1.mjs +0 -25295
- package/dist/cli-54nftqxr.mjs +0 -5939
- package/dist/coreBundle-mbshhes2.mjs +0 -8
- package/dist/helpers-yksnc17b.mjs +0 -70
- package/dist/index-kmxtpxxk.mjs +0 -13
- package/dist/legacy-runtime-e6y2jnc6.mjs +0 -134
- package/dist/main-4fw36c57.mjs +0 -160
- package/dist/main-4wycvga6.mjs +0 -797
- package/dist/main-c9qxrdzn.mjs +0 -73
- package/dist/main-d1th6z2h.mjs +0 -5395
- package/dist/main-de0xhbg0.mjs +0 -1269
- package/dist/main-dyya6jpy.mjs +0 -140
- package/dist/main-gexpjhk5.mjs +0 -32
- package/dist/main-h65pjrag.mjs +0 -11403
- package/dist/main-hh16893t.mjs +0 -5358
- package/dist/main-j98fzrvv.mjs +0 -68
- package/dist/main-jtgan144.mjs +0 -327
- package/dist/main-p44hff2a.mjs +0 -213
- package/dist/main-p8jk5985.mjs +0 -1750
- package/dist/main-p8q3wrpd.mjs +0 -342
- package/dist/main-pmfd055v.mjs +0 -63
- package/dist/main-q5w61fgh.mjs +0 -17
- package/dist/main-t8hspw29.mjs +0 -922
- package/dist/main-t8xtycpr.mjs +0 -227
- package/dist/main-tk4axre7.mjs +0 -39
- package/dist/main-w6180mxa.mjs +0 -161
- package/dist/main-wds0wcmt.mjs +0 -4
- package/dist/main-z4c819pg.mjs +0 -5512
- package/dist/main-zrjcdksk.mjs +0 -62
- package/dist/mcp-a2jkny55.mjs +0 -9466
- package/dist/migrate-p84kbr3y.mjs +0 -353
- package/dist/relay-e9q9hfc3.mjs +0 -18369
- package/dist/remove-stored-secrets-c90a5h72.mjs +0 -6
- package/dist/runtime-update-jj6pa7vd.mjs +0 -244
- package/dist/setup-p86ywb7k.mjs +0 -1051
- package/dist/update-rh24h3m1.mjs +0 -17
- package/dist/version-1p8wpyzk.mjs +0 -8
package/dist/main-4wycvga6.mjs
DELETED
|
@@ -1,797 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
legacyDataRoot,
|
|
3
|
-
insidePath
|
|
4
|
-
} from "./main-pmfd055v.mjs";
|
|
5
|
-
import {
|
|
6
|
-
opencodeBrowserVersion2
|
|
7
|
-
} from "./main-wds0wcmt.mjs";
|
|
8
|
-
import {
|
|
9
|
-
stateRoot,
|
|
10
|
-
fromCheckout,
|
|
11
|
-
compiledBinary,
|
|
12
|
-
entrySpawnCommand,
|
|
13
|
-
installedBinary,
|
|
14
|
-
cliPrefix,
|
|
15
|
-
RELEASE_SERVICE
|
|
16
|
-
} from "./main-dyya6jpy.mjs";
|
|
17
|
-
|
|
18
|
-
// src/protocol.ts
|
|
19
|
-
var extensionProtocolVersion = 3;
|
|
20
|
-
var minExtensionProtocolVersion = 3;
|
|
21
|
-
var unpackedExtensionId = "afeafocngkodbmaipcngoamamfmekgfo";
|
|
22
|
-
var browserUnresponsive = "The browser isn't answering (asleep, frozen or very busy?). Wake it or wait a moment, then try again.";
|
|
23
|
-
var browserReloading = "browse stopped answering in this browser for over a minute, so the relay asked it to reload. Try again in a few seconds.";
|
|
24
|
-
var unpackedReloadAdvice = "Reload browse on your browser's extensions page (↻).";
|
|
25
|
-
var checkoutReloadPrefix = "Extension protocol incompatible: extension-older; reload ";
|
|
26
|
-
var extensionReconnectAlarmPeriodMs = 30000;
|
|
27
|
-
var legacyExtensionProtocolVersion = 1;
|
|
28
|
-
function extensionProtocolCompatibility(value) {
|
|
29
|
-
const legacy = value === undefined;
|
|
30
|
-
const valid = typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
|
31
|
-
const version = valid ? value : legacy ? legacyExtensionProtocolVersion : null;
|
|
32
|
-
return { version, compatible: version !== null && version >= minExtensionProtocolVersion && version <= extensionProtocolVersion, legacy };
|
|
33
|
-
}
|
|
34
|
-
function extensionProtocolSkew(version) {
|
|
35
|
-
if (typeof version !== "number")
|
|
36
|
-
return;
|
|
37
|
-
if (version > extensionProtocolVersion)
|
|
38
|
-
return "extension-newer";
|
|
39
|
-
if (version < minExtensionProtocolVersion)
|
|
40
|
-
return "extension-older";
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
function extensionProtocolAdvice(version, commands, distribution) {
|
|
44
|
-
const skew = extensionProtocolSkew(version);
|
|
45
|
-
const versions = `extension protocol ${version ?? "unknown"}, this relay speaks ${minExtensionProtocolVersion === extensionProtocolVersion ? extensionProtocolVersion : `${minExtensionProtocolVersion} to ${extensionProtocolVersion}`}`;
|
|
46
|
-
if (skew === "extension-newer")
|
|
47
|
-
return `The browse extension is newer than this browse CLI (${versions}). Update the CLI: run \`${commands.install}\` (an npm install with automatic updates also catches up by itself within a day), then restart opencode.`;
|
|
48
|
-
if (skew === "extension-older") {
|
|
49
|
-
const store = "The Chrome Web Store updates it within a few hours (to update now, open chrome://extensions, turn on Developer mode and click Update).";
|
|
50
|
-
const next = distribution === "unpacked" ? unpackedReloadAdvice : distribution === "store" ? store : `For an unpacked extension: ${unpackedReloadAdvice} For a store extension: ${store}`;
|
|
51
|
-
return `The browse extension is older than this browse CLI (${versions}). ${next}`;
|
|
52
|
-
}
|
|
53
|
-
return `The browse extension and this browse CLI don't speak the same protocol (${versions}). Run \`${commands.install}\` again and update the extension.`;
|
|
54
|
-
}
|
|
55
|
-
function makePageStatus(options) {
|
|
56
|
-
return {
|
|
57
|
-
state: options.state,
|
|
58
|
-
owner: options.targetOwner === "user" ? "user" : "session",
|
|
59
|
-
...options.sessionId ? { sessionId: options.sessionId } : {},
|
|
60
|
-
...options.sessionId && options.readOnly ? { readOnly: true } : {},
|
|
61
|
-
...options.message ? { message: options.message } : {},
|
|
62
|
-
...options.handoffId ? { handoffId: options.handoffId } : {},
|
|
63
|
-
...options.recording ? { recording: true } : {}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
var extensionCommandMethodValues = [
|
|
67
|
-
"ping",
|
|
68
|
-
"debugger.attach",
|
|
69
|
-
"debugger.detach",
|
|
70
|
-
"debugger.sendCommand",
|
|
71
|
-
"tabs.create",
|
|
72
|
-
"tabs.remove",
|
|
73
|
-
"tabs.group",
|
|
74
|
-
"tabs.ungroup",
|
|
75
|
-
"action.setAttached",
|
|
76
|
-
"action.setBadge",
|
|
77
|
-
"pageStatus.set",
|
|
78
|
-
"pageStatus.clear",
|
|
79
|
-
"runtime.reload",
|
|
80
|
-
"tabs.cleanup",
|
|
81
|
-
"update.notice",
|
|
82
|
-
"relay.settings",
|
|
83
|
-
"settings.get",
|
|
84
|
-
"settings.set"
|
|
85
|
-
];
|
|
86
|
-
var extensionEventMethodValues = [
|
|
87
|
-
"protocol",
|
|
88
|
-
"hello",
|
|
89
|
-
"auth.code",
|
|
90
|
-
"auth.response",
|
|
91
|
-
"ready",
|
|
92
|
-
"toolbar.clicked",
|
|
93
|
-
"handoff.completed",
|
|
94
|
-
"debugger.event",
|
|
95
|
-
"debugger.attached",
|
|
96
|
-
"debugger.detached",
|
|
97
|
-
"tabs.removed",
|
|
98
|
-
"tabs.opened",
|
|
99
|
-
"pong",
|
|
100
|
-
"log",
|
|
101
|
-
"pageStatus.requested",
|
|
102
|
-
"settings"
|
|
103
|
-
];
|
|
104
|
-
function parseJsonObject(input) {
|
|
105
|
-
const parsed = JSON.parse(input);
|
|
106
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
107
|
-
throw new Error("Expected JSON object");
|
|
108
|
-
}
|
|
109
|
-
return parsed;
|
|
110
|
-
}
|
|
111
|
-
var extensionCommandMethods = new Set(extensionCommandMethodValues);
|
|
112
|
-
var extensionEventMethods = new Set(extensionEventMethodValues);
|
|
113
|
-
function isCdpRequest(input) {
|
|
114
|
-
return typeof input.id === "number" && typeof input.method === "string" && (input.params === undefined || isJsonObject(input.params)) && (input.sessionId === undefined || typeof input.sessionId === "string");
|
|
115
|
-
}
|
|
116
|
-
function isExtensionResponse(input) {
|
|
117
|
-
return typeof input.id === "number" && (input.result === undefined || isJsonObject(input.result)) && (input.error === undefined || typeof input.error === "string") && !(input.result !== undefined && input.error !== undefined);
|
|
118
|
-
}
|
|
119
|
-
function isExtensionEvent(input) {
|
|
120
|
-
return typeof input.method === "string" && extensionEventMethods.has(input.method) && (input.params === undefined || isJsonObject(input.params));
|
|
121
|
-
}
|
|
122
|
-
function isJsonObject(value) {
|
|
123
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// src/update/auto-update.ts
|
|
127
|
-
import { spawn, spawnSync } from "node:child_process";
|
|
128
|
-
import { chmodSync as chmodSync2, closeSync, existsSync, mkdirSync as mkdirSync2, mkdtempSync, openSync, readFileSync as readFileSync2, renameSync, rmSync, realpathSync, writeFileSync as writeFileSync2 } from "node:fs";
|
|
129
|
-
import os from "node:os";
|
|
130
|
-
import path2 from "node:path";
|
|
131
|
-
|
|
132
|
-
// src/update/release-service.ts
|
|
133
|
-
import crypto from "node:crypto";
|
|
134
|
-
import { chmodSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
135
|
-
import path from "node:path";
|
|
136
|
-
function serviceTarget(target) {
|
|
137
|
-
return target.replace(/^darwin-/, "macos-");
|
|
138
|
-
}
|
|
139
|
-
function runningServiceTarget() {
|
|
140
|
-
return globalThis.__OPENCODE_BROWSER_TARGET__ ? serviceTarget(globalThis.__OPENCODE_BROWSER_TARGET__) : undefined;
|
|
141
|
-
}
|
|
142
|
-
function releaseChannel(channel) {
|
|
143
|
-
if (channel === "dev" || channel === "beta" || channel === "stable")
|
|
144
|
-
return channel;
|
|
145
|
-
return "stable";
|
|
146
|
-
}
|
|
147
|
-
var releaseService = (env = process.env) => (env.OPENCODE_BROWSER_RELEASE_SERVICE?.trim() || RELEASE_SERVICE).replace(/\/+$/, "");
|
|
148
|
-
var NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
149
|
-
var SHA256 = /^[0-9a-f]{64}$/;
|
|
150
|
-
var VERSION = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
|
|
151
|
-
var record = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
152
|
-
var text = (value, pattern) => typeof value === "string" && (!pattern || pattern.test(value)) ? value : undefined;
|
|
153
|
-
var integer = (value, min, max = Number.MAX_SAFE_INTEGER) => typeof value === "number" && Number.isSafeInteger(value) && value >= min && value <= max ? value : undefined;
|
|
154
|
-
var https = (value) => {
|
|
155
|
-
const url = text(value);
|
|
156
|
-
if (!url)
|
|
157
|
-
return;
|
|
158
|
-
try {
|
|
159
|
-
return new URL(url).protocol === "https:" ? url : undefined;
|
|
160
|
-
} catch {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
function parseManifest(value) {
|
|
165
|
-
const raw = record(value);
|
|
166
|
-
const release = record(raw?.release);
|
|
167
|
-
if (!raw || !release || raw.schema !== 1)
|
|
168
|
-
return;
|
|
169
|
-
const app = text(raw.app, NAME);
|
|
170
|
-
const artifact = text(raw.artifact, NAME);
|
|
171
|
-
const channel = text(raw.channel, NAME);
|
|
172
|
-
const sequence = integer(raw.sequence, 1);
|
|
173
|
-
const version = text(release.version, VERSION);
|
|
174
|
-
const rollout = integer(release.rollout_percent, 0, 100);
|
|
175
|
-
if (!app || !artifact || !channel || sequence === undefined || !version || rollout === undefined)
|
|
176
|
-
return;
|
|
177
|
-
if (typeof raw.published_at !== "string")
|
|
178
|
-
return;
|
|
179
|
-
if (release.required !== undefined && release.required !== false && release.required !== true)
|
|
180
|
-
return;
|
|
181
|
-
const targets = {};
|
|
182
|
-
const rawTargets = record(release.targets);
|
|
183
|
-
if (!rawTargets || Object.keys(rawTargets).length > 64)
|
|
184
|
-
return;
|
|
185
|
-
for (const [name, item] of Object.entries(rawTargets)) {
|
|
186
|
-
const target = record(item);
|
|
187
|
-
const url = text(target?.url);
|
|
188
|
-
const size = integer(target?.size, 0);
|
|
189
|
-
const sha256 = text(target?.sha256, SHA256);
|
|
190
|
-
if (!url || size === undefined || !sha256)
|
|
191
|
-
return;
|
|
192
|
-
targets[name] = { url, size, sha256 };
|
|
193
|
-
}
|
|
194
|
-
if (!Array.isArray(release.distributions) || release.distributions.length > 32)
|
|
195
|
-
return;
|
|
196
|
-
const distributions = [];
|
|
197
|
-
for (const item of release.distributions) {
|
|
198
|
-
const distribution = record(item);
|
|
199
|
-
const kind = text(distribution?.kind, NAME);
|
|
200
|
-
if (!distribution || !kind)
|
|
201
|
-
return;
|
|
202
|
-
const distributionVersion = distribution.version === undefined ? undefined : text(distribution.version, VERSION);
|
|
203
|
-
const command = distribution.command === undefined ? undefined : text(distribution.command);
|
|
204
|
-
if (distribution.version !== undefined && !distributionVersion || distribution.command !== undefined && !command)
|
|
205
|
-
return;
|
|
206
|
-
distributions.push({ kind, ...distributionVersion ? { version: distributionVersion } : {}, ...command ? { command } : {} });
|
|
207
|
-
}
|
|
208
|
-
const notes_url = release.notes_url === undefined ? undefined : https(release.notes_url);
|
|
209
|
-
if (release.notes_url !== undefined && !notes_url)
|
|
210
|
-
return;
|
|
211
|
-
return {
|
|
212
|
-
schema: 1,
|
|
213
|
-
app,
|
|
214
|
-
artifact,
|
|
215
|
-
channel,
|
|
216
|
-
sequence,
|
|
217
|
-
release: {
|
|
218
|
-
version,
|
|
219
|
-
...notes_url ? { notes_url } : {},
|
|
220
|
-
rollout_percent: rollout,
|
|
221
|
-
required: release.required === true,
|
|
222
|
-
targets,
|
|
223
|
-
distributions
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
function decide(input) {
|
|
228
|
-
let manifest;
|
|
229
|
-
try {
|
|
230
|
-
manifest = parseManifest(JSON.parse(input.manifest));
|
|
231
|
-
} catch {
|
|
232
|
-
return { action: "reject", reason: "malformed" };
|
|
233
|
-
}
|
|
234
|
-
if (!manifest)
|
|
235
|
-
return { action: "reject", reason: "malformed" };
|
|
236
|
-
if (manifest.app !== input.app)
|
|
237
|
-
return { action: "reject", reason: "wrong_app" };
|
|
238
|
-
if (manifest.artifact !== input.artifact)
|
|
239
|
-
return { action: "reject", reason: "wrong_artifact" };
|
|
240
|
-
if (manifest.channel !== input.channel)
|
|
241
|
-
return { action: "reject", reason: "wrong_channel" };
|
|
242
|
-
const release = manifest.release;
|
|
243
|
-
let candidate = release.version;
|
|
244
|
-
let target;
|
|
245
|
-
let distribution;
|
|
246
|
-
if (input.install.distribution === "direct") {
|
|
247
|
-
target = release.targets[input.install.target];
|
|
248
|
-
if (!target)
|
|
249
|
-
return { action: "none", reason: "no_target" };
|
|
250
|
-
} else if (input.install.distribution !== "unknown") {
|
|
251
|
-
distribution = release.distributions.find((item) => item.kind === input.install.distribution);
|
|
252
|
-
if (!distribution)
|
|
253
|
-
return { action: "none", reason: "distribution_unlisted" };
|
|
254
|
-
if (!distribution.version)
|
|
255
|
-
return { action: "none", reason: "distribution_pending" };
|
|
256
|
-
candidate = distribution.version;
|
|
257
|
-
}
|
|
258
|
-
const running = target && input.install.sha256 !== undefined ? input.install.sha256 === target.sha256 : candidate === input.install.version;
|
|
259
|
-
if (running)
|
|
260
|
-
return { action: "none", reason: "up_to_date" };
|
|
261
|
-
const act = (reason) => target ? { action: "install", reason, version: candidate, target } : {
|
|
262
|
-
action: "notify",
|
|
263
|
-
reason,
|
|
264
|
-
version: candidate,
|
|
265
|
-
distribution: input.install.distribution,
|
|
266
|
-
...distribution?.command ? { command: distribution.command } : {},
|
|
267
|
-
...release.notes_url ? { notes_url: release.notes_url } : {}
|
|
268
|
-
};
|
|
269
|
-
if (input.install.channel !== undefined && input.install.channel !== input.channel)
|
|
270
|
-
return act("switch");
|
|
271
|
-
if (release.required)
|
|
272
|
-
return act("required");
|
|
273
|
-
if (input.install.rolloutBucket < release.rollout_percent)
|
|
274
|
-
return act("update");
|
|
275
|
-
return { action: "none", reason: "rollout_deferred" };
|
|
276
|
-
}
|
|
277
|
-
async function fetchManifest(options) {
|
|
278
|
-
const service = (options.service ?? releaseService()).replace(/\/+$/, "");
|
|
279
|
-
const response = await (options.fetch ?? fetch)(`${service}/browse/cli/${options.channel}.json`, {
|
|
280
|
-
headers: {
|
|
281
|
-
"user-agent": `browse/${options.channel}/${options.version}/cli`,
|
|
282
|
-
...options.etag ? { "if-none-match": options.etag } : {}
|
|
283
|
-
},
|
|
284
|
-
signal: AbortSignal.timeout(30000)
|
|
285
|
-
});
|
|
286
|
-
if (response.status === 304)
|
|
287
|
-
return { status: "unchanged" };
|
|
288
|
-
if (response.status === 404)
|
|
289
|
-
return { status: "missing" };
|
|
290
|
-
if (!response.ok)
|
|
291
|
-
throw new Error(`the release service answered ${response.status} for browse/cli/${options.channel}`);
|
|
292
|
-
const etag = response.headers.get("etag");
|
|
293
|
-
return { status: "manifest", text: await response.text(), ...etag ? { etag } : {} };
|
|
294
|
-
}
|
|
295
|
-
async function checkRelease(options) {
|
|
296
|
-
const manifest = await fetchManifest({
|
|
297
|
-
channel: options.channel,
|
|
298
|
-
version: options.version,
|
|
299
|
-
...options.etag ? { etag: options.etag } : {},
|
|
300
|
-
...options.service ? { service: options.service } : {},
|
|
301
|
-
...options.fetch ? { fetch: options.fetch } : {}
|
|
302
|
-
});
|
|
303
|
-
if (manifest.status !== "manifest")
|
|
304
|
-
return manifest;
|
|
305
|
-
const decision = decide({
|
|
306
|
-
app: "browse",
|
|
307
|
-
artifact: "cli",
|
|
308
|
-
channel: options.channel,
|
|
309
|
-
manifest: manifest.text,
|
|
310
|
-
install: {
|
|
311
|
-
version: options.version,
|
|
312
|
-
target: options.target,
|
|
313
|
-
distribution: "direct",
|
|
314
|
-
rolloutBucket: rolloutBucket(options.stateDir)
|
|
315
|
-
}
|
|
316
|
-
});
|
|
317
|
-
let releaseVersion = options.version;
|
|
318
|
-
try {
|
|
319
|
-
releaseVersion = parseManifest(JSON.parse(manifest.text))?.release.version ?? options.version;
|
|
320
|
-
} catch {}
|
|
321
|
-
return { status: "manifest", releaseVersion, decision, ...manifest.etag ? { etag: manifest.etag } : {} };
|
|
322
|
-
}
|
|
323
|
-
function rolloutBucket(root = stateRoot()) {
|
|
324
|
-
const file = path.join(root, "rollout-bucket");
|
|
325
|
-
try {
|
|
326
|
-
const saved = Number(readFileSync(file, "utf8").trim());
|
|
327
|
-
if (Number.isInteger(saved) && saved >= 0 && saved < 100)
|
|
328
|
-
return saved;
|
|
329
|
-
} catch {}
|
|
330
|
-
const bucket = crypto.randomInt(100);
|
|
331
|
-
try {
|
|
332
|
-
mkdirSync(root, { recursive: true, mode: 448 });
|
|
333
|
-
writeFileSync(file, `${bucket}
|
|
334
|
-
`, { mode: 384 });
|
|
335
|
-
chmodSync(file, 384);
|
|
336
|
-
} catch {}
|
|
337
|
-
return bucket;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// src/update/auto-update.ts
|
|
341
|
-
var PACKAGE_NAME = "@opencode/browse";
|
|
342
|
-
var HOUR_MS = 60 * 60 * 1000;
|
|
343
|
-
var DEFAULT_REGISTRY = "https://registry.npmjs.org";
|
|
344
|
-
var autoUpdateModes = ["on", "notify", "off"];
|
|
345
|
-
var UPDATE_VERSION = /^(\d+)\.(\d+)\.(\d+)(?:-(dev|beta)\.(\d+)(?:\.(\d+))?)?$/;
|
|
346
|
-
var channelOf = (version) => version.includes("-dev.") ? "dev" : version.includes("-beta.") ? "beta" : "latest";
|
|
347
|
-
function compareVersions(left, right) {
|
|
348
|
-
const parse = (value) => {
|
|
349
|
-
const match = UPDATE_VERSION.exec(value);
|
|
350
|
-
if (!match)
|
|
351
|
-
return;
|
|
352
|
-
const stage = match[4] === undefined ? 2 : match[4] === "beta" ? 1 : 0;
|
|
353
|
-
return [Number(match[1]), Number(match[2]), Number(match[3]), stage, Number(match[5] ?? 0), Number(match[6] ?? 0)];
|
|
354
|
-
};
|
|
355
|
-
const a = parse(left);
|
|
356
|
-
const b = parse(right);
|
|
357
|
-
if (!a || !b)
|
|
358
|
-
return 0;
|
|
359
|
-
for (let index = 0;index < a.length; index++)
|
|
360
|
-
if (a[index] !== b[index])
|
|
361
|
-
return a[index] < b[index] ? -1 : 1;
|
|
362
|
-
return 0;
|
|
363
|
-
}
|
|
364
|
-
function installKind(entry = process.argv[1] ?? "", checkout = fromCheckout()) {
|
|
365
|
-
if (checkout)
|
|
366
|
-
return "checkout";
|
|
367
|
-
if (compiledBinary())
|
|
368
|
-
return "binary";
|
|
369
|
-
const normalized = entry.replaceAll("\\", "/");
|
|
370
|
-
if (/\/_npx\/|\/npm-cache\/_npx\//.test(normalized))
|
|
371
|
-
return "npx";
|
|
372
|
-
return entry ? "npm" : "other";
|
|
373
|
-
}
|
|
374
|
-
var packageVersion = (entry) => {
|
|
375
|
-
try {
|
|
376
|
-
const manifest = path2.join(path2.dirname(path2.dirname(entry)), "package.json");
|
|
377
|
-
const version = JSON.parse(readFileSync2(manifest, "utf8")).version;
|
|
378
|
-
return typeof version === "string" ? version : undefined;
|
|
379
|
-
} catch {
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
};
|
|
383
|
-
var realEntry = (entry) => {
|
|
384
|
-
try {
|
|
385
|
-
return realpathSync(entry);
|
|
386
|
-
} catch {
|
|
387
|
-
return entry;
|
|
388
|
-
}
|
|
389
|
-
};
|
|
390
|
-
function installedRuntime2(entry = process.argv[1] ?? process.execPath) {
|
|
391
|
-
const kind = installKind(entry);
|
|
392
|
-
if (kind === "binary") {
|
|
393
|
-
const binary = installedBinary();
|
|
394
|
-
if (binary)
|
|
395
|
-
return { kind, entry: binary, version: opencodeBrowserVersion2 };
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
if (kind !== "npm" && kind !== "npx")
|
|
399
|
-
return;
|
|
400
|
-
const resolved = realEntry(entry);
|
|
401
|
-
return { kind, entry: resolved, version: packageVersion(resolved) ?? opencodeBrowserVersion2 };
|
|
402
|
-
}
|
|
403
|
-
async function updateTarget(runtime, run = runCommand, env = process.env) {
|
|
404
|
-
if (runtime.kind !== "npm" || !insidePath(runtime.entry, path2.join(legacyDataRoot(), "runtime", "versions")))
|
|
405
|
-
return runtime;
|
|
406
|
-
const root = await npmGlobalRoot(run, env);
|
|
407
|
-
return root ? { ...runtime, entry: path2.join(root, ...PACKAGE_NAME.split("/"), "dist", "cli.mjs") } : undefined;
|
|
408
|
-
}
|
|
409
|
-
async function npmGlobalRoot(run = runCommand, env = process.env) {
|
|
410
|
-
const result = await run("npm", ["root", "-g"], { timeoutMs: 20000, env });
|
|
411
|
-
const root = result.status === 0 ? result.stdout.trim() : "";
|
|
412
|
-
return root && path2.isAbsolute(root) ? root : undefined;
|
|
413
|
-
}
|
|
414
|
-
function installedVersion(runtime) {
|
|
415
|
-
if (runtime.kind === "binary") {
|
|
416
|
-
const result = spawnSync(runtime.entry, ["--version"], { encoding: "utf8", timeout: 15000, env: { ...process.env, OPENCODE_BROWSER_AUTOSTART: "0" }, windowsHide: true });
|
|
417
|
-
return result.status === 0 ? result.stdout.trim() || undefined : undefined;
|
|
418
|
-
}
|
|
419
|
-
return packageVersion(runtime.entry);
|
|
420
|
-
}
|
|
421
|
-
var autoUpdateStatePath = () => path2.join(stateRoot(), "auto-update.json");
|
|
422
|
-
function readJson(file) {
|
|
423
|
-
try {
|
|
424
|
-
return JSON.parse(readFileSync2(file, "utf8"));
|
|
425
|
-
} catch {
|
|
426
|
-
return;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
function writeJsonAtomic(file, value) {
|
|
430
|
-
mkdirSync2(path2.dirname(file), { recursive: true, mode: 448 });
|
|
431
|
-
const temporary = `${file}.${process.pid}.tmp`;
|
|
432
|
-
writeFileSync2(temporary, JSON.stringify(value, null, 2) + `
|
|
433
|
-
`, { mode: 384 });
|
|
434
|
-
chmodSync2(temporary, 384);
|
|
435
|
-
renameSync(temporary, file);
|
|
436
|
-
}
|
|
437
|
-
function readState() {
|
|
438
|
-
return readJson(autoUpdateStatePath()) ?? {};
|
|
439
|
-
}
|
|
440
|
-
function updateState(change) {
|
|
441
|
-
const next = change(readState());
|
|
442
|
-
writeJsonAtomic(autoUpdateStatePath(), next);
|
|
443
|
-
return next;
|
|
444
|
-
}
|
|
445
|
-
function autoUpdateOverride(env = process.env) {
|
|
446
|
-
const value = env.OPENCODE_BROWSER_AUTO_UPDATE?.trim().toLowerCase();
|
|
447
|
-
if (value === "on" || value === "1" || value === "true")
|
|
448
|
-
return "on";
|
|
449
|
-
if (value === "notify")
|
|
450
|
-
return "notify";
|
|
451
|
-
if (value === "off" || value === "0" || value === "false")
|
|
452
|
-
return "off";
|
|
453
|
-
if (env.OPENCODE_BROWSER_UPDATE_CHECK === "0")
|
|
454
|
-
return "off";
|
|
455
|
-
return;
|
|
456
|
-
}
|
|
457
|
-
function storedMode() {
|
|
458
|
-
const mode = readState().mode;
|
|
459
|
-
return mode && autoUpdateModes.includes(mode) ? mode : "on";
|
|
460
|
-
}
|
|
461
|
-
function effectiveMode(env = process.env) {
|
|
462
|
-
return autoUpdateOverride(env) ?? storedMode();
|
|
463
|
-
}
|
|
464
|
-
function setStoredMode(mode) {
|
|
465
|
-
updateState((state) => ({ ...state, mode }));
|
|
466
|
-
}
|
|
467
|
-
function autoUpdateStatus(options = {}) {
|
|
468
|
-
const env = options.env ?? process.env;
|
|
469
|
-
const runtime = options.runtime === undefined ? installedRuntime2() : options.runtime;
|
|
470
|
-
const state = readState();
|
|
471
|
-
const override = autoUpdateOverride(env);
|
|
472
|
-
const install = runtime?.kind ?? installKind();
|
|
473
|
-
return {
|
|
474
|
-
install,
|
|
475
|
-
cli: cliPrefix(),
|
|
476
|
-
mode: storedMode(),
|
|
477
|
-
effective: install === "binary" || install === "npm" ? override ?? storedMode() : "off",
|
|
478
|
-
...override ? { overriddenBy: env.OPENCODE_BROWSER_AUTO_UPDATE !== undefined ? "OPENCODE_BROWSER_AUTO_UPDATE" : "OPENCODE_BROWSER_UPDATE_CHECK" } : {},
|
|
479
|
-
...runtime ? { version: runtime.version, channel: channelOf(runtime.version) } : {},
|
|
480
|
-
...state.latest ? { latest: state.latest } : {},
|
|
481
|
-
...state.latestAt ? { latestAt: new Date(state.latestAt).toISOString() } : {},
|
|
482
|
-
...state.checkedAt ? { checkedAt: new Date(state.checkedAt).toISOString() } : {},
|
|
483
|
-
...state.lastUpdate ? { lastUpdate: state.lastUpdate } : {},
|
|
484
|
-
...state.lastError ? { lastError: state.lastError } : {}
|
|
485
|
-
};
|
|
486
|
-
}
|
|
487
|
-
function describeAutoUpdate(status) {
|
|
488
|
-
if (status.install === "checkout")
|
|
489
|
-
return "Updates: checkout install; `update` pulls new commits";
|
|
490
|
-
if (status.install === "npx")
|
|
491
|
-
return "Updates: npx resolves the package itself; `update` is a no-op";
|
|
492
|
-
if (status.install !== "binary" && status.install !== "npm")
|
|
493
|
-
return "Updates: not an installed runtime";
|
|
494
|
-
const mode = { on: "automatic", notify: "notify only", off: "off" }[status.effective];
|
|
495
|
-
const parts = [`${mode}${status.overriddenBy ? ` (set by ${status.overriddenBy})` : ""}`, `channel ${status.channel ?? "latest"}`];
|
|
496
|
-
if (updateAvailable(status))
|
|
497
|
-
parts.push(status.effective === "on" ? `${status.latest} available; the installer runs when no agent is using the browser` : `${status.latest} available; run \`${status.cli} update\` to install it`);
|
|
498
|
-
if (status.lastError)
|
|
499
|
-
parts.push(`last attempt failed: ${status.lastError.message}`);
|
|
500
|
-
return `Updates: ${parts.join("; ")}`;
|
|
501
|
-
}
|
|
502
|
-
function updateAvailable(status) {
|
|
503
|
-
if (!status.latest || !status.version)
|
|
504
|
-
return false;
|
|
505
|
-
return status.install === "binary" ? status.latest !== status.version : compareVersions(status.latest, status.version) > 0;
|
|
506
|
-
}
|
|
507
|
-
var runCommand = (command, args, options = {}) => new Promise((resolve) => {
|
|
508
|
-
const npmCli = process.platform === "win32" && command === "npm" ? path2.join(path2.dirname(process.execPath), "node_modules", "npm", "bin", "npm-cli.js") : undefined;
|
|
509
|
-
const [file, argv] = npmCli && existsSync(npmCli) ? [process.execPath, [npmCli, ...args]] : [command, args];
|
|
510
|
-
const child = spawn(file, argv, { env: options.env ?? process.env, stdio: ["ignore", "pipe", "pipe"], windowsHide: true });
|
|
511
|
-
let stdout = "";
|
|
512
|
-
let stderr = "";
|
|
513
|
-
child.stdout.setEncoding("utf8").on("data", (chunk) => void (stdout += chunk));
|
|
514
|
-
child.stderr.setEncoding("utf8").on("data", (chunk) => void (stderr += chunk));
|
|
515
|
-
const timer = setTimeout(() => child.kill(), options.timeoutMs ?? 120000);
|
|
516
|
-
child.on("error", (error) => {
|
|
517
|
-
clearTimeout(timer);
|
|
518
|
-
resolve({ status: null, stdout, stderr: stderr + error.message });
|
|
519
|
-
});
|
|
520
|
-
child.on("close", (status) => {
|
|
521
|
-
clearTimeout(timer);
|
|
522
|
-
resolve({ status, stdout, stderr });
|
|
523
|
-
});
|
|
524
|
-
});
|
|
525
|
-
async function registryUrl(run = runCommand, env = process.env) {
|
|
526
|
-
const override = env.OPENCODE_BROWSER_NPM_REGISTRY?.trim();
|
|
527
|
-
const configured = override || (compiledBinary() ? "" : (await run("npm", ["config", "get", "registry"], { timeoutMs: 15000 })).stdout.trim());
|
|
528
|
-
return /^https?:\/\//.test(configured) ? configured.replace(/\/+$/, "") : DEFAULT_REGISTRY;
|
|
529
|
-
}
|
|
530
|
-
async function fetchPublished(options) {
|
|
531
|
-
const request = options.fetch ?? fetch;
|
|
532
|
-
const name = options.packageName ?? PACKAGE_NAME;
|
|
533
|
-
const response = await request(`${options.registry}/${name.replace("/", "%2f")}`, {
|
|
534
|
-
headers: { accept: "application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8" },
|
|
535
|
-
signal: AbortSignal.timeout(20000)
|
|
536
|
-
});
|
|
537
|
-
if (!response.ok)
|
|
538
|
-
throw new Error(`the registry answered ${response.status} for ${name}`);
|
|
539
|
-
const body = await response.json();
|
|
540
|
-
const version = body["dist-tags"]?.[options.channel];
|
|
541
|
-
if (!version)
|
|
542
|
-
throw new Error(`${name} has no ${options.channel} dist-tag`);
|
|
543
|
-
return { version };
|
|
544
|
-
}
|
|
545
|
-
var INSTALL_SCRIPT_TIMEOUT_MS = 60 * 60000;
|
|
546
|
-
async function runInstaller(options) {
|
|
547
|
-
const run = options.run ?? runCommand;
|
|
548
|
-
const env = options.env ?? process.env;
|
|
549
|
-
const platform = options.platform ?? process.platform;
|
|
550
|
-
if (options.runtime.kind === "npx")
|
|
551
|
-
return options.runtime.version;
|
|
552
|
-
if (options.runtime.kind === "npm") {
|
|
553
|
-
const spec = `${PACKAGE_NAME}@${options.version ?? options.channel ?? "latest"}`;
|
|
554
|
-
const npm = await run("npm", ["install", "-g", spec], { timeoutMs: 10 * 60000, env });
|
|
555
|
-
if (npm.status !== 0)
|
|
556
|
-
throw new Error(`npm install -g ${spec} failed: ${(npm.stderr || npm.stdout).trim().split(`
|
|
557
|
-
`).slice(-3).join(" ")}`);
|
|
558
|
-
const target = await updateTarget(options.runtime, run, env);
|
|
559
|
-
if (!target)
|
|
560
|
-
throw new Error(`npm install -g ${spec} ran, but \`npm root -g\` didn't say where it installed`);
|
|
561
|
-
const found = existsSync(target.entry) ? installedVersion(target) : undefined;
|
|
562
|
-
if (!found && target !== options.runtime)
|
|
563
|
-
throw new Error(`npm install -g ${spec} ran, but ${target.entry} isn't there`);
|
|
564
|
-
const version = found ?? options.version ?? options.channel ?? "latest";
|
|
565
|
-
if (options.version && compareVersions(version, options.version) < 0)
|
|
566
|
-
throw new Error(`npm install -g ${spec} left ${version}, not the requested ${options.version}`);
|
|
567
|
-
return version;
|
|
568
|
-
}
|
|
569
|
-
const override = env.OPENCODE_BROWSER_INSTALL_SCRIPT;
|
|
570
|
-
const script = override ? existsSync(override) ? readFileSync2(override, "utf8") : await (options.fetch ?? fetch)(override, { signal: AbortSignal.timeout(60000) }).then(async (response) => {
|
|
571
|
-
if (!response.ok)
|
|
572
|
-
throw new Error(`couldn't download the installer (${response.status})`);
|
|
573
|
-
return response.text();
|
|
574
|
-
}) : options.installer ?? globalThis.__OPENCODE_BROWSER_INSTALLER__;
|
|
575
|
-
if (!script)
|
|
576
|
-
throw new Error("this build has no installer to run; install browse again with the install script");
|
|
577
|
-
const directory = mkdtempSync(path2.join(os.tmpdir(), "browse-install-"));
|
|
578
|
-
const file = path2.join(directory, platform === "win32" ? "install.ps1" : "install");
|
|
579
|
-
const args = [...options.version ? ["--version", options.version] : options.channel ? ["--channel", options.channel] : [], "--no-setup"];
|
|
580
|
-
try {
|
|
581
|
-
writeFileSync2(file, script, { mode: platform === "win32" ? 384 : 448 });
|
|
582
|
-
const command = platform === "win32" ? "powershell.exe" : "sh";
|
|
583
|
-
const argv = platform === "win32" ? ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", file, ...args] : [file, ...args];
|
|
584
|
-
const installed = await run(command, argv, { timeoutMs: INSTALL_SCRIPT_TIMEOUT_MS, env });
|
|
585
|
-
if (installed.status !== 0)
|
|
586
|
-
throw new Error(`the installer failed: ${(installed.stderr || installed.stdout).trim().split(`
|
|
587
|
-
`).slice(-3).join(" ")}`);
|
|
588
|
-
} finally {
|
|
589
|
-
rmSync(directory, { recursive: true, force: true });
|
|
590
|
-
}
|
|
591
|
-
const version = installedVersion(options.runtime);
|
|
592
|
-
if (!version)
|
|
593
|
-
throw new Error(`the installer ran, but ${options.runtime.entry} doesn't report a version`);
|
|
594
|
-
if (options.version && compareVersions(version, options.version) < 0)
|
|
595
|
-
throw new Error(`the installer left ${version}, not the requested ${options.version}`);
|
|
596
|
-
return version;
|
|
597
|
-
}
|
|
598
|
-
function processAlive(pid) {
|
|
599
|
-
if (!Number.isSafeInteger(pid) || pid <= 0)
|
|
600
|
-
return false;
|
|
601
|
-
try {
|
|
602
|
-
process.kill(pid, 0);
|
|
603
|
-
return true;
|
|
604
|
-
} catch (error) {
|
|
605
|
-
return error.code === "EPERM";
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
var INSTALL_TIMEOUT_MS = INSTALL_SCRIPT_TIMEOUT_MS + 5 * 60000;
|
|
609
|
-
var SWITCH_TIMEOUT_MS = 5 * 60000;
|
|
610
|
-
function createAutoUpdater(deps) {
|
|
611
|
-
const env = deps.env ?? process.env;
|
|
612
|
-
const now = deps.now ?? Date.now;
|
|
613
|
-
const { runtime } = deps;
|
|
614
|
-
const running = runtime.version;
|
|
615
|
-
const checkIntervalMs = Math.min(Number(env.OPENCODE_BROWSER_AUTO_UPDATE_INTERVAL_MS) || HOUR_MS, HOUR_MS);
|
|
616
|
-
const spawnChild = deps.spawnChild ?? ((args, onExit, from) => spawnRuntimeChild(from, args, onExit, env));
|
|
617
|
-
let updates;
|
|
618
|
-
let busy = false;
|
|
619
|
-
let switching;
|
|
620
|
-
let timer;
|
|
621
|
-
async function checkBinary(state, channel) {
|
|
622
|
-
const target = runningServiceTarget();
|
|
623
|
-
if (!target)
|
|
624
|
-
throw new Error("this install has no release service target");
|
|
625
|
-
const serviceChannel = releaseChannel(channel);
|
|
626
|
-
const result = await checkRelease({
|
|
627
|
-
version: running,
|
|
628
|
-
channel: serviceChannel,
|
|
629
|
-
target,
|
|
630
|
-
...state.etag ? { etag: state.etag } : {},
|
|
631
|
-
...deps.fetch ? { fetch: deps.fetch } : {}
|
|
632
|
-
});
|
|
633
|
-
if (result.status === "unchanged") {
|
|
634
|
-
deps.log("autoupdate.unchanged", { channel: serviceChannel, running });
|
|
635
|
-
return updateState(({ failed: _failed, ...rest }) => ({
|
|
636
|
-
...rest,
|
|
637
|
-
checkedAt: now(),
|
|
638
|
-
...rest.latest && !rest.latestAt ? { latestAt: now() } : {}
|
|
639
|
-
}));
|
|
640
|
-
}
|
|
641
|
-
if (result.status === "missing") {
|
|
642
|
-
deps.log("autoupdate.no_release", { channel: serviceChannel, running });
|
|
643
|
-
return updateState(({ failed: _failed, latest: _latest, latestAt: _latestAt, etag: _etag, ...rest }) => ({ ...rest, checkedAt: now() }));
|
|
644
|
-
}
|
|
645
|
-
const decision = result.decision;
|
|
646
|
-
if (decision.action === "reject")
|
|
647
|
-
throw new Error(`the ${serviceChannel} manifest was rejected: ${decision.reason}`);
|
|
648
|
-
deps.log("autoupdate.checked", { channel: serviceChannel, current: result.releaseVersion, running, decision: decision.reason });
|
|
649
|
-
return updateState(({ failed: _failed, latest: previous, latestAt: previousAt, etag: _etag, ...rest }) => ({
|
|
650
|
-
...rest,
|
|
651
|
-
checkedAt: now(),
|
|
652
|
-
...result.etag ? { etag: result.etag } : {},
|
|
653
|
-
...decision.action === "install" ? { latest: decision.version, latestAt: previous === decision.version && previousAt ? previousAt : now() } : {}
|
|
654
|
-
}));
|
|
655
|
-
}
|
|
656
|
-
async function tick() {
|
|
657
|
-
if (switching && (now() - switching.startedAt > SWITCH_TIMEOUT_MS || switching.pid !== undefined && !processAlive(switching.pid))) {
|
|
658
|
-
deps.log("autoupdate.switch_abandoned", { pid: switching.pid });
|
|
659
|
-
switching = undefined;
|
|
660
|
-
}
|
|
661
|
-
if (busy || switching)
|
|
662
|
-
return;
|
|
663
|
-
busy = true;
|
|
664
|
-
try {
|
|
665
|
-
const mode = effectiveMode(env);
|
|
666
|
-
if (mode === "off")
|
|
667
|
-
return;
|
|
668
|
-
let state = readState();
|
|
669
|
-
if (!state.checkedAt || now() - state.checkedAt >= checkIntervalMs || now() < state.checkedAt) {
|
|
670
|
-
const channel = channelOf(running);
|
|
671
|
-
try {
|
|
672
|
-
if (runtime.kind === "binary") {
|
|
673
|
-
state = await checkBinary(state, channel);
|
|
674
|
-
} else {
|
|
675
|
-
const published = await fetchPublished({ registry: await registryUrl(deps.run, env), channel, ...deps.fetch ? { fetch: deps.fetch } : {} });
|
|
676
|
-
state = updateState(({ failed: _failed, latest: previous, latestAt: previousAt, ...rest }) => ({
|
|
677
|
-
...rest,
|
|
678
|
-
checkedAt: now(),
|
|
679
|
-
latest: published.version,
|
|
680
|
-
latestAt: previous === published.version && previousAt ? previousAt : now()
|
|
681
|
-
}));
|
|
682
|
-
deps.log("autoupdate.checked", { channel, latest: published.version, running });
|
|
683
|
-
}
|
|
684
|
-
} catch (error) {
|
|
685
|
-
state = updateState(({ latest: _latest, etag: _etag, ...rest }) => ({ ...rest, checkedAt: now(), failed: error.message }));
|
|
686
|
-
deps.log("autoupdate.check_failed", { error: error.message });
|
|
687
|
-
}
|
|
688
|
-
deps.onChange?.();
|
|
689
|
-
}
|
|
690
|
-
const wanted = runtime.kind === "binary" ? state.latest !== running ? state.latest : undefined : state.latest && compareVersions(state.latest, running) > 0 ? state.latest : undefined;
|
|
691
|
-
if (state.lastError && (state.lastError.version === running || !state.failed && (!wanted || state.lastError.version !== wanted))) {
|
|
692
|
-
state = updateState(({ lastError: _lastError, ...rest }) => rest);
|
|
693
|
-
deps.onChange?.();
|
|
694
|
-
}
|
|
695
|
-
if (!wanted || mode !== "on")
|
|
696
|
-
return;
|
|
697
|
-
updates ??= await updateTarget(runtime, deps.run ?? runCommand, env);
|
|
698
|
-
if (!updates)
|
|
699
|
-
return void deps.log("autoupdate.no_target", { reason: "npm root -g failed" });
|
|
700
|
-
const installing = state.installing;
|
|
701
|
-
if (installing) {
|
|
702
|
-
const alive = processAlive(installing.pid) && now() - installing.startedAt < INSTALL_TIMEOUT_MS;
|
|
703
|
-
if (alive)
|
|
704
|
-
return;
|
|
705
|
-
const installed = installedVersion(updates);
|
|
706
|
-
state = updateState(({ installing: _installing, ...rest }) => ({
|
|
707
|
-
...rest,
|
|
708
|
-
...installed && installed !== running ? {} : { lastError: { message: "the installer stopped without finishing", at: new Date(now()).toISOString(), version: installing.version } }
|
|
709
|
-
}));
|
|
710
|
-
deps.log("autoupdate.installer_gone", { version: installing.version });
|
|
711
|
-
}
|
|
712
|
-
if (!deps.isIdle())
|
|
713
|
-
return;
|
|
714
|
-
const installed = installedVersion(updates);
|
|
715
|
-
if (installed === wanted) {
|
|
716
|
-
switching = { pid: undefined, startedAt: now() };
|
|
717
|
-
deps.log("autoupdate.switching", { from: running, to: installed });
|
|
718
|
-
switching.pid = spawnChild(["runtime-switch", installed], (code) => {
|
|
719
|
-
if (switching)
|
|
720
|
-
switching = undefined;
|
|
721
|
-
if (code !== 0)
|
|
722
|
-
deps.log("autoupdate.restart_refused", { version: installed, code });
|
|
723
|
-
deps.onChange?.();
|
|
724
|
-
}, updates);
|
|
725
|
-
return;
|
|
726
|
-
}
|
|
727
|
-
if (state.lastError?.version === wanted) {
|
|
728
|
-
const failure = state.lastError;
|
|
729
|
-
const attempts = failure.attempts ?? 1;
|
|
730
|
-
const backoff = (attempts <= 1 ? 15 : attempts === 2 ? 60 : 240) * 60000;
|
|
731
|
-
const age = now() - Date.parse(failure.at);
|
|
732
|
-
if (age >= 0 && age < Math.min(backoff, checkIntervalMs))
|
|
733
|
-
return;
|
|
734
|
-
}
|
|
735
|
-
const priorFailureAt = state.lastError?.at;
|
|
736
|
-
deps.log("autoupdate.installing", { version: wanted });
|
|
737
|
-
const startedAt = now();
|
|
738
|
-
const pid = spawnChild(["self-update", wanted, "--channel", channelOf(running), "--from", running], (code) => {
|
|
739
|
-
deps.log(code === 0 ? "autoupdate.installed" : "autoupdate.install_failed", { version: wanted, code });
|
|
740
|
-
updateState(({ installing: _installing, ...rest }) => {
|
|
741
|
-
if (code === 0) {
|
|
742
|
-
const { checkedAt: _checkedAt, etag: _etag, lastError: _lastError, ...kept } = rest;
|
|
743
|
-
return kept;
|
|
744
|
-
}
|
|
745
|
-
const failure = rest.lastError;
|
|
746
|
-
if (failure !== undefined && failure.version === wanted && failure.at !== priorFailureAt)
|
|
747
|
-
return rest;
|
|
748
|
-
return {
|
|
749
|
-
...rest,
|
|
750
|
-
lastError: { message: "the installer failed", at: new Date(now()).toISOString(), version: wanted, attempts: 1 + (failure?.version === wanted ? failure.attempts ?? 1 : 0) }
|
|
751
|
-
};
|
|
752
|
-
});
|
|
753
|
-
deps.onChange?.();
|
|
754
|
-
if (code === 0)
|
|
755
|
-
tick();
|
|
756
|
-
}, runtime);
|
|
757
|
-
if (pid)
|
|
758
|
-
updateState((rest) => ({ ...rest, installing: { version: wanted, pid, startedAt } }));
|
|
759
|
-
} catch (error) {
|
|
760
|
-
deps.log("autoupdate.error", { error: error.message });
|
|
761
|
-
} finally {
|
|
762
|
-
busy = false;
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
return {
|
|
766
|
-
tick,
|
|
767
|
-
start(intervalMs = Number(env.OPENCODE_BROWSER_AUTO_UPDATE_TICK_MS) || 10 * 60000) {
|
|
768
|
-
timer = setInterval(() => void tick(), intervalMs);
|
|
769
|
-
timer.unref();
|
|
770
|
-
setTimeout(() => void tick(), Math.min(intervalMs, 60000)).unref();
|
|
771
|
-
},
|
|
772
|
-
stop() {
|
|
773
|
-
clearInterval(timer);
|
|
774
|
-
}
|
|
775
|
-
};
|
|
776
|
-
}
|
|
777
|
-
function spawnRuntimeChild(runtime, args, onExit, env = process.env) {
|
|
778
|
-
const { OPENCODE_BROWSER_MANAGED_RELAY: _managed, OPENCODE_BROWSER_RESTART_REQUEST_ID: _request, ...rest } = env;
|
|
779
|
-
const directory = path2.join(stateRoot(), "logs");
|
|
780
|
-
mkdirSync2(directory, { recursive: true, mode: 448 });
|
|
781
|
-
const log = openSync(path2.join(directory, "auto-update.log"), "a", 384);
|
|
782
|
-
try {
|
|
783
|
-
const [file, ...prefix] = entrySpawnCommand(runtime.entry);
|
|
784
|
-
const child = spawn(file, [...prefix, ...args], { detached: true, stdio: ["ignore", log, log], env: rest, windowsHide: true });
|
|
785
|
-
child.on("error", () => onExit(null));
|
|
786
|
-
child.on("exit", (code) => onExit(code));
|
|
787
|
-
child.unref();
|
|
788
|
-
return child.pid;
|
|
789
|
-
} catch {
|
|
790
|
-
onExit(null);
|
|
791
|
-
return;
|
|
792
|
-
} finally {
|
|
793
|
-
closeSync(log);
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
export { extensionProtocolVersion, minExtensionProtocolVersion, unpackedExtensionId, browserUnresponsive, browserReloading, unpackedReloadAdvice, checkoutReloadPrefix, extensionReconnectAlarmPeriodMs, extensionProtocolCompatibility, extensionProtocolSkew, extensionProtocolAdvice, makePageStatus, parseJsonObject, isCdpRequest, isExtensionResponse, isExtensionEvent, isJsonObject, runningServiceTarget, releaseChannel, checkRelease, autoUpdateModes, UPDATE_VERSION, channelOf, compareVersions, installedRuntime2, updateTarget, npmGlobalRoot, installedVersion, updateState, autoUpdateOverride, storedMode, effectiveMode, setStoredMode, autoUpdateStatus, describeAutoUpdate, updateAvailable, runCommand, registryUrl, fetchPublished, runInstaller, createAutoUpdater };
|