@durableintel/nenya 0.0.0-stage → 1.2.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 +392 -2
- package/bin/nenya.cjs +25 -0
- package/bin/nenya.js +72 -0
- package/bin/start.cjs +9 -0
- package/lib/cli.mjs +305 -0
- package/lib/feedenv.mjs +124 -0
- package/lib/filefeed.mjs +40 -0
- package/lib/install.mjs +579 -0
- package/lib/linux.mjs +156 -0
- package/lib/message.mjs +752 -0
- package/lib/release.mjs +120 -0
- package/lib/tools.mjs +227 -0
- package/lib/verify.mjs +146 -0
- package/lib/windows.mjs +395 -0
- package/package.json +25 -4
- package/release-keys.pub +8 -0
package/lib/cli.mjs
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// The `nenya` command of @qwntllabs/nenya (DQ-DESK-10 (b), GO-4):
|
|
2
|
+
//
|
|
3
|
+
// npx -y @qwntllabs/nenya@latest setup --invite CODE
|
|
4
|
+
//
|
|
5
|
+
// installs Nenya for this account on macOS and Windows (fetched from the release bucket, verified
|
|
6
|
+
// against its signature bundle with one of the release public keys, no administrator prompt) and opens it
|
|
7
|
+
// with the invite code. On Linux ("one admin install", operator O2) it prints the package-manager
|
|
8
|
+
// lines and the invite code instead. `nenya repair` installs again over the copy that is there.
|
|
9
|
+
// `--feed URL` reads another feed than the release bucket: an https:// URL, or a local copy of a
|
|
10
|
+
// feed (file://), such as a qa build copied from the private qa feed bucket. On macOS and Windows a
|
|
11
|
+
// local copy is read from disk and verified as the bucket is, with the key its feed.json names
|
|
12
|
+
// for its builds when it names one (lib/feedenv.mjs); on every OS the environment its feed.json
|
|
13
|
+
// names is recorded for this account. `nenya message` sends an agent's message to its swarm through
|
|
14
|
+
// the Nenya that is running (lib/message.mjs); `nenya tools` lists, describes and runs the tools
|
|
15
|
+
// Nenya learned in this repo (lib/tools.mjs).
|
|
16
|
+
// Everything the person sees is plain language, one outcome line and what to do next; a refusal
|
|
17
|
+
// is one line.
|
|
18
|
+
import { readFileSync } from "node:fs";
|
|
19
|
+
import { createRequire } from "node:module";
|
|
20
|
+
import { parseArgs } from "node:util";
|
|
21
|
+
import {
|
|
22
|
+
DEFAULT_LIMITS,
|
|
23
|
+
SetupError,
|
|
24
|
+
installDarwin,
|
|
25
|
+
installWin32,
|
|
26
|
+
} from "./install.mjs";
|
|
27
|
+
import {
|
|
28
|
+
FeedEnvironmentError,
|
|
29
|
+
feedKey,
|
|
30
|
+
readFeedFile,
|
|
31
|
+
recordEnvironment,
|
|
32
|
+
userDataFolder,
|
|
33
|
+
} from "./feedenv.mjs";
|
|
34
|
+
import { feedFetch } from "./filefeed.mjs";
|
|
35
|
+
import { linuxFamily, linuxLines } from "./linux.mjs";
|
|
36
|
+
import { messageMain } from "./message.mjs";
|
|
37
|
+
import { toolsMain } from "./tools.mjs";
|
|
38
|
+
import { CHANNELS } from "./release.mjs";
|
|
39
|
+
import { pemBlocks } from "./verify.mjs";
|
|
40
|
+
|
|
41
|
+
const require = createRequire(import.meta.url);
|
|
42
|
+
const { version: PACKAGE_VERSION } = require("../package.json");
|
|
43
|
+
|
|
44
|
+
// The release download base URL (CONTRACTS.md §8: one bucket for every channel).
|
|
45
|
+
export const DEFAULT_BASE_URL = "https://downloads.harness.platform.qwntl.com";
|
|
46
|
+
// The codes the app takes (the old app's deepLinks.ts cleanInviteCode).
|
|
47
|
+
export const INVITE_CODE = /^[A-Za-z0-9][A-Za-z0-9_-]{2,63}$/;
|
|
48
|
+
|
|
49
|
+
export const USAGE = `Usage: nenya <command> [options]
|
|
50
|
+
|
|
51
|
+
Commands
|
|
52
|
+
setup Install Nenya for this account and open it.
|
|
53
|
+
repair Install Nenya again over the copy that is there.
|
|
54
|
+
message Message a teammate in your swarm through Nenya (nenya message --help).
|
|
55
|
+
tools The tools Nenya learned in this repo: list, describe, run (nenya tools --help).
|
|
56
|
+
|
|
57
|
+
Options
|
|
58
|
+
--invite CODE Open Nenya with this invite code filled in.
|
|
59
|
+
--channel NAME Release channel: alpha, beta or stable (default: this package's own).
|
|
60
|
+
--feed URL Install from another feed: an https:// URL, or a local copy (file://).
|
|
61
|
+
--help Show this help.
|
|
62
|
+
--version Show this command's version.
|
|
63
|
+
|
|
64
|
+
On Linux setup and repair print the package-manager lines for an administrator; nothing is
|
|
65
|
+
installed by this command there.`;
|
|
66
|
+
|
|
67
|
+
// The channel a package version belongs to: 0.12.0-alpha.1 is alpha, 0.12.0 is stable. An
|
|
68
|
+
// unstamped checkout (0.0.0) follows alpha, the first Nenya channel.
|
|
69
|
+
export function defaultChannel(version) {
|
|
70
|
+
const match = /-(alpha|beta)\./.exec(version);
|
|
71
|
+
if (match) return match[1];
|
|
72
|
+
return version === "0.0.0" ? "alpha" : "stable";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// A feed URL as the base to use, or null when it isn't one: https://host[:port]/path, a local copy
|
|
76
|
+
// file:///absolute/path, or http on this machine (the tests' loopback server). No credentials,
|
|
77
|
+
// query, fragment or IPv6 literal, and only characters that are safe unquoted in the printed shell
|
|
78
|
+
// lines (zsh would glob "[" and "]"). Trailing slashes are dropped.
|
|
79
|
+
function feedUrl(value) {
|
|
80
|
+
let url;
|
|
81
|
+
try {
|
|
82
|
+
url = new URL(value);
|
|
83
|
+
} catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
const loopback = ["127.0.0.1", "localhost"].includes(url.hostname);
|
|
87
|
+
const file = url.protocol === "file:";
|
|
88
|
+
if (!(
|
|
89
|
+
url.protocol === "https:" ||
|
|
90
|
+
file ||
|
|
91
|
+
(url.protocol === "http:" && loopback)
|
|
92
|
+
)) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
if (url.username || url.password || /[?#]/.test(url.href)) return null;
|
|
96
|
+
const host = file
|
|
97
|
+
? url.host === ""
|
|
98
|
+
: /^[a-z0-9.-]+(:[0-9]+)?$/.test(url.host);
|
|
99
|
+
const path = /^\/[A-Za-z0-9._~:/%-]*$/.test(url.pathname);
|
|
100
|
+
const base = url.href.replace(/\/+$/, "");
|
|
101
|
+
// A file feed names a folder, never the root.
|
|
102
|
+
return host && path && !(file && url.pathname.replace(/\/+$/, "") === "")
|
|
103
|
+
? base
|
|
104
|
+
: null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// The listed release keys, as PEM blocks: the file NENYA_RELEASE_PUBLIC_KEY names (one or more
|
|
108
|
+
// PEM blocks), else release-keys.pub, which this package carries (spec 15, signing rotation: the
|
|
109
|
+
// Cloud KMS key, then its Key Vault successor; README, "Where the public key comes from").
|
|
110
|
+
function readPublicKeys(env) {
|
|
111
|
+
const file = env.NENYA_RELEASE_PUBLIC_KEY;
|
|
112
|
+
return pemBlocks(
|
|
113
|
+
readFileSync(
|
|
114
|
+
file || new URL("../release-keys.pub", import.meta.url),
|
|
115
|
+
"utf8",
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// The one line for a feed.json or harness.json that can't be used or written.
|
|
121
|
+
function environmentError(error) {
|
|
122
|
+
return error instanceof FeedEnvironmentError
|
|
123
|
+
? error.message
|
|
124
|
+
: `Couldn't record the feed's environment for this account: ${error.message}.`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Sets the environment a local feed names in this account's harness.json; returns the line that
|
|
128
|
+
// says so.
|
|
129
|
+
async function recordFeedEnvironment(context, environment) {
|
|
130
|
+
const file = await recordEnvironment(
|
|
131
|
+
userDataFolder(context.platform, context.env, context.home),
|
|
132
|
+
environment,
|
|
133
|
+
);
|
|
134
|
+
return `This feed's builds are for ${environment}: Nenya on this account will sign in to ${environment} (${file}).`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Runs the command line; resolves to the exit status (0 done, 1 refused or failed, 2 usage).
|
|
138
|
+
export async function main(argv, context) {
|
|
139
|
+
// `nenya message …` and `nenya tools …` have options of their own (lib/message.mjs, lib/tools.mjs), parsed there
|
|
140
|
+
if (argv[0] === "message") return messageMain(argv.slice(1), context);
|
|
141
|
+
if (argv[0] === "tools") return toolsMain(argv.slice(1), context);
|
|
142
|
+
const { platform, arch, env, out, err } = context;
|
|
143
|
+
let values;
|
|
144
|
+
let positionals;
|
|
145
|
+
try {
|
|
146
|
+
({ values, positionals } = parseArgs({
|
|
147
|
+
args: argv,
|
|
148
|
+
allowPositionals: true,
|
|
149
|
+
options: {
|
|
150
|
+
invite: { type: "string" },
|
|
151
|
+
channel: { type: "string" },
|
|
152
|
+
feed: { type: "string" },
|
|
153
|
+
help: { type: "boolean" },
|
|
154
|
+
version: { type: "boolean" },
|
|
155
|
+
},
|
|
156
|
+
}));
|
|
157
|
+
} catch (error) {
|
|
158
|
+
err(`${error.message.split("\n")[0]}.`);
|
|
159
|
+
err(USAGE);
|
|
160
|
+
return 2;
|
|
161
|
+
}
|
|
162
|
+
if (values.version) {
|
|
163
|
+
out(PACKAGE_VERSION);
|
|
164
|
+
return 0;
|
|
165
|
+
}
|
|
166
|
+
const [command, ...extra] = positionals;
|
|
167
|
+
if (values.help || command === undefined) {
|
|
168
|
+
out(USAGE);
|
|
169
|
+
return command === undefined && !values.help ? 2 : 0;
|
|
170
|
+
}
|
|
171
|
+
if (!["setup", "repair"].includes(command) || extra.length > 0) {
|
|
172
|
+
err(`'${[command, ...extra].join(" ")}' isn't a nenya command.`);
|
|
173
|
+
err(USAGE);
|
|
174
|
+
return 2;
|
|
175
|
+
}
|
|
176
|
+
const invite = values.invite?.trim();
|
|
177
|
+
if (values.invite !== undefined && !INVITE_CODE.test(invite)) {
|
|
178
|
+
err(
|
|
179
|
+
`'${values.invite}' isn't an invite code (3 to 64 letters, digits, - or _, starting with a letter or digit).`,
|
|
180
|
+
);
|
|
181
|
+
return 2;
|
|
182
|
+
}
|
|
183
|
+
const channel =
|
|
184
|
+
values.channel ?? env.NENYA_CHANNEL ?? defaultChannel(PACKAGE_VERSION);
|
|
185
|
+
if (!CHANNELS.includes(channel)) {
|
|
186
|
+
err(
|
|
187
|
+
`'${channel}' isn't a release channel: the channels are alpha, beta and stable.`,
|
|
188
|
+
);
|
|
189
|
+
return 2;
|
|
190
|
+
}
|
|
191
|
+
// --feed, then NENYA_FEED, then NENYA_DOWNLOAD_BASE_URL, then the release bucket. There is no
|
|
192
|
+
// keyword and no built-in second feed: a feed is always a URL the person gives.
|
|
193
|
+
const feed = values.feed ?? (env.NENYA_FEED || undefined);
|
|
194
|
+
let baseUrl = (env.NENYA_DOWNLOAD_BASE_URL || DEFAULT_BASE_URL).replace(
|
|
195
|
+
/\/+$/,
|
|
196
|
+
"",
|
|
197
|
+
);
|
|
198
|
+
if (feed !== undefined) {
|
|
199
|
+
baseUrl = feedUrl(feed);
|
|
200
|
+
if (!baseUrl) {
|
|
201
|
+
err(`'${feed}' isn't a feed: a feed is an https:// or file:// URL.`);
|
|
202
|
+
return 2;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const repair = command === "repair";
|
|
206
|
+
|
|
207
|
+
if (platform === "linux") {
|
|
208
|
+
// A local feed copy that names its environment (feed.json) sets it for this account.
|
|
209
|
+
let recorded;
|
|
210
|
+
try {
|
|
211
|
+
const feedFile = await readFeedFile(baseUrl);
|
|
212
|
+
if (feedFile) {
|
|
213
|
+
recorded = await recordFeedEnvironment(context, feedFile.env);
|
|
214
|
+
}
|
|
215
|
+
} catch (error) {
|
|
216
|
+
err(environmentError(error));
|
|
217
|
+
return 1;
|
|
218
|
+
}
|
|
219
|
+
const family = linuxFamily(context.osRelease);
|
|
220
|
+
// The AUR package reads the release's own .deb, never a feed's.
|
|
221
|
+
if (baseUrl !== DEFAULT_BASE_URL && family !== "pacman") {
|
|
222
|
+
out(`Packages come from ${baseUrl}.`);
|
|
223
|
+
}
|
|
224
|
+
if (recorded) out(recorded);
|
|
225
|
+
for (const line of linuxLines({
|
|
226
|
+
family,
|
|
227
|
+
baseUrl,
|
|
228
|
+
channel,
|
|
229
|
+
repair,
|
|
230
|
+
invite,
|
|
231
|
+
})) {
|
|
232
|
+
out(line);
|
|
233
|
+
}
|
|
234
|
+
return 0;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const install = { darwin: installDarwin, win32: installWin32 }[platform];
|
|
238
|
+
if (!install) {
|
|
239
|
+
err(`Nenya isn't available for ${platform}.`);
|
|
240
|
+
return 1;
|
|
241
|
+
}
|
|
242
|
+
if (platform === "darwin" && arch !== "arm64") {
|
|
243
|
+
err("Nenya runs on Apple silicon Macs; this Mac isn't one.");
|
|
244
|
+
return 1;
|
|
245
|
+
}
|
|
246
|
+
if (platform === "win32" && !["x64", "arm64"].includes(arch)) {
|
|
247
|
+
err(`Nenya isn't available for ${arch} Windows PCs.`);
|
|
248
|
+
return 1;
|
|
249
|
+
}
|
|
250
|
+
// A local feed's feed.json: the key its builds are signed with and the environment they are for,
|
|
251
|
+
// recorded once the download has verified and before Nenya is installed or started.
|
|
252
|
+
let feedFile;
|
|
253
|
+
let feedPem;
|
|
254
|
+
try {
|
|
255
|
+
feedFile = await readFeedFile(baseUrl);
|
|
256
|
+
feedPem = feedKey(feedFile);
|
|
257
|
+
} catch (error) {
|
|
258
|
+
err(environmentError(error));
|
|
259
|
+
return 1;
|
|
260
|
+
}
|
|
261
|
+
const beforeInstall = feedFile
|
|
262
|
+
? async () => {
|
|
263
|
+
let line;
|
|
264
|
+
try {
|
|
265
|
+
line = await recordFeedEnvironment(context, feedFile.env);
|
|
266
|
+
} catch (error) {
|
|
267
|
+
throw new SetupError(environmentError(error));
|
|
268
|
+
}
|
|
269
|
+
out(line);
|
|
270
|
+
}
|
|
271
|
+
: undefined;
|
|
272
|
+
// Every download must verify with one of the listed keys: NENYA_RELEASE_PUBLIC_KEY's file (one
|
|
273
|
+
// or more PEM blocks) when it is set; else the one key a local feed's feed.json names for its
|
|
274
|
+
// builds; else the release keys this package carries (release-keys.pub). There is always at least
|
|
275
|
+
// one key, and lib/install.mjs installs nothing that doesn't verify.
|
|
276
|
+
const useFeedKey = feedPem !== null && !env.NENYA_RELEASE_PUBLIC_KEY;
|
|
277
|
+
try {
|
|
278
|
+
const publicKeyPems = useFeedKey ? [feedPem] : readPublicKeys(env);
|
|
279
|
+
if (useFeedKey) {
|
|
280
|
+
out(
|
|
281
|
+
"This feed's builds are checked with the key its feed.json names, not the Nenya release key.",
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
return await install({
|
|
285
|
+
limits: DEFAULT_LIMITS,
|
|
286
|
+
...context,
|
|
287
|
+
fetch: feedFetch(context.fetch),
|
|
288
|
+
baseUrl,
|
|
289
|
+
channel,
|
|
290
|
+
publicKeyPems,
|
|
291
|
+
invite,
|
|
292
|
+
repair,
|
|
293
|
+
beforeInstall,
|
|
294
|
+
});
|
|
295
|
+
} catch (error) {
|
|
296
|
+
if (error instanceof SetupError) {
|
|
297
|
+
err(error.message);
|
|
298
|
+
return 1;
|
|
299
|
+
}
|
|
300
|
+
err(
|
|
301
|
+
`Nenya couldn't be ${repair ? "repaired" : "installed"}: ${error.message}.`,
|
|
302
|
+
);
|
|
303
|
+
return 1;
|
|
304
|
+
}
|
|
305
|
+
}
|
package/lib/feedenv.mjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// A local copy of a feed may say which environment its builds are for, in `feed.json` at its root
|
|
2
|
+
// (ci/qa-feed.gitlab-ci.yml writes {"env": "qa", ...} into every qa build). The packages carry no
|
|
3
|
+
// environment (one signed build serves every environment, CONTRACTS.md §6a), and the app chooses
|
|
4
|
+
// its environment from HARNESS_ENV, else `env` in <userData>/harness.json, else production
|
|
5
|
+
// (apps/desktop/src/main/core/config.ts). So `setup --feed file:///…` records the feed's environment
|
|
6
|
+
// in this account's harness.json, keeping its other settings, on every OS: Nenya opened from the
|
|
7
|
+
// menu then signs in to the environment its build is for, with no environment variable.
|
|
8
|
+
//
|
|
9
|
+
// On macOS and Windows the command itself verifies what it installs, and a qa build is not signed
|
|
10
|
+
// with a release key: its feed.json may name the one key it is signed with, `releaseKey`, the PEM text
|
|
11
|
+
// of an EC P-256 public key (as cosign.pub is). Only a local feed whose feed.json names qa or
|
|
12
|
+
// staging can name one, and its downloads are verified with it exactly as the release keys' are.
|
|
13
|
+
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
import { loadPublicKey, pemBlocks } from "./verify.mjs";
|
|
17
|
+
|
|
18
|
+
// apps/desktop/src/main/userData.ts: <appData>/com.qwntl.nenya.desktop on every OS.
|
|
19
|
+
export const USER_DATA_FOLDER = "com.qwntl.nenya.desktop";
|
|
20
|
+
// Production is every build's default; a feed can only move an account to another environment.
|
|
21
|
+
const FEED_ENVIRONMENTS = ["qa", "staging"];
|
|
22
|
+
const FEED_FILE_LIMIT = 4096;
|
|
23
|
+
|
|
24
|
+
// Thrown for a feed.json or harness.json the command can't use; the message is the one line.
|
|
25
|
+
export class FeedEnvironmentError extends Error {}
|
|
26
|
+
|
|
27
|
+
// What a file feed's feed.json says, { env, releaseKey }, or null when the feed has none (an https
|
|
28
|
+
// feed, or a folder with no feed.json). `releaseKey` is as written (see feedKey).
|
|
29
|
+
export async function readFeedFile(baseUrl) {
|
|
30
|
+
if (!baseUrl.startsWith("file:")) return null;
|
|
31
|
+
let bytes;
|
|
32
|
+
try {
|
|
33
|
+
bytes = await readFile(fileURLToPath(`${baseUrl}/feed.json`));
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (error.code === "ENOENT") return null;
|
|
36
|
+
throw new FeedEnvironmentError("The feed's feed.json can't be read.");
|
|
37
|
+
}
|
|
38
|
+
let feed;
|
|
39
|
+
try {
|
|
40
|
+
if (bytes.length > FEED_FILE_LIMIT) throw new Error("too large");
|
|
41
|
+
feed = JSON.parse(bytes.toString("utf8"));
|
|
42
|
+
} catch {
|
|
43
|
+
throw new FeedEnvironmentError("The feed's feed.json isn't valid.");
|
|
44
|
+
}
|
|
45
|
+
if (
|
|
46
|
+
feed === null ||
|
|
47
|
+
typeof feed !== "object" ||
|
|
48
|
+
Array.isArray(feed) ||
|
|
49
|
+
!FEED_ENVIRONMENTS.includes(feed.env)
|
|
50
|
+
) {
|
|
51
|
+
throw new FeedEnvironmentError(
|
|
52
|
+
"The feed's feed.json names no environment Nenya knows (qa or staging).",
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
return { env: feed.env, releaseKey: feed.releaseKey };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// The public key (PEM) a feed's feed.json names for its builds, or null when it names none. A key
|
|
59
|
+
// that is there but isn't one EC P-256 public key is refused, never passed over: nothing from that
|
|
60
|
+
// feed is installed. A feed names exactly one key, the one its builds are signed with (CI writes
|
|
61
|
+
// the build's cosign.pub), so two PEM blocks are refused too: only release-keys.pub is a list.
|
|
62
|
+
export function feedKey(feed) {
|
|
63
|
+
if (feed?.releaseKey === undefined) return null;
|
|
64
|
+
try {
|
|
65
|
+
if (typeof feed.releaseKey !== "string") throw new Error("not text");
|
|
66
|
+
if (pemBlocks(feed.releaseKey).length !== 1) throw new Error("not one key");
|
|
67
|
+
loadPublicKey(feed.releaseKey);
|
|
68
|
+
} catch {
|
|
69
|
+
throw new FeedEnvironmentError(
|
|
70
|
+
"The feed's feed.json names a releaseKey that isn't an EC P-256 public key.",
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return feed.releaseKey;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// <userData> as the app computes it (userData.ts, testMode.ts): NENYA_TEST_USER_DATA under
|
|
77
|
+
// NENYA_TEST=1, else <appData>/com.qwntl.nenya.desktop, where <appData> is Electron's: on Linux
|
|
78
|
+
// $XDG_CONFIG_HOME (~/.config when unset), on macOS ~/Library/Application Support, on Windows
|
|
79
|
+
// %APPDATA% (~\AppData\Roaming when unset).
|
|
80
|
+
export function userDataFolder(platform, env, home) {
|
|
81
|
+
const testFolder = env.NENYA_TEST === "1" ? env.NENYA_TEST_USER_DATA : "";
|
|
82
|
+
if (testFolder && testFolder.trim() !== "") return testFolder;
|
|
83
|
+
return join(appData(platform, env, home), USER_DATA_FOLDER);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function appData(platform, env, home) {
|
|
87
|
+
if (platform === "darwin") {
|
|
88
|
+
return join(home, "Library", "Application Support");
|
|
89
|
+
}
|
|
90
|
+
if (platform === "win32") {
|
|
91
|
+
return env.APPDATA || join(home, "AppData", "Roaming");
|
|
92
|
+
}
|
|
93
|
+
return env.XDG_CONFIG_HOME || join(home, ".config");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Sets `env` in <userData>/harness.json, keeping every other setting; returns the file's path. The
|
|
97
|
+
// file is replaced in one rename, so the app never reads half of it.
|
|
98
|
+
export async function recordEnvironment(userData, environment) {
|
|
99
|
+
const file = join(userData, "harness.json");
|
|
100
|
+
const unreadable = new FeedEnvironmentError(
|
|
101
|
+
`${file} isn't a settings file; this command works once it's fixed or removed.`,
|
|
102
|
+
);
|
|
103
|
+
let settings = {};
|
|
104
|
+
try {
|
|
105
|
+
settings = JSON.parse(await readFile(file, "utf8"));
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (error.code !== "ENOENT") throw unreadable;
|
|
108
|
+
}
|
|
109
|
+
if (
|
|
110
|
+
settings === null ||
|
|
111
|
+
typeof settings !== "object" ||
|
|
112
|
+
Array.isArray(settings)
|
|
113
|
+
) {
|
|
114
|
+
throw unreadable;
|
|
115
|
+
}
|
|
116
|
+
await mkdir(userData, { recursive: true });
|
|
117
|
+
const staged = `${file}.${process.pid}.tmp`;
|
|
118
|
+
await writeFile(
|
|
119
|
+
staged,
|
|
120
|
+
`${JSON.stringify({ ...settings, env: environment }, null, 2)}\n`,
|
|
121
|
+
);
|
|
122
|
+
await rename(staged, file);
|
|
123
|
+
return file;
|
|
124
|
+
}
|
package/lib/filefeed.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// A local copy of a feed (`--feed file:///…`) is read from disk: Node's fetch reads no `file:` URL.
|
|
2
|
+
// Each read answers as the bucket would, so lib/install.mjs reads a folder exactly as it reads the
|
|
3
|
+
// bucket, with the same bounds and the same signature checks: a file that is there is a 200 with
|
|
4
|
+
// its size as Content-Length (the bound is checked before a byte is read), and a file that isn't
|
|
5
|
+
// (or a folder in its place) is a 404. Anything else (unreadable, or changed while it is read)
|
|
6
|
+
// fails the read as a broken connection would. So a copy is read v2 root first, as the bucket is
|
|
7
|
+
// (spec 15): a copy with no desktop/v2/<channel>/<platform>/<arch> release list answers 404 for it
|
|
8
|
+
// and its legacy desktop/<channel>/... folder is read; an unreadable v2 list is refused.
|
|
9
|
+
import { openAsBlob } from "node:fs";
|
|
10
|
+
import { stat } from "node:fs/promises";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
|
|
13
|
+
const MISSING = ["ENOENT", "ENOTDIR"];
|
|
14
|
+
|
|
15
|
+
// One `file:` URL as a fetch Response.
|
|
16
|
+
export async function fileFetch(url) {
|
|
17
|
+
const path = fileURLToPath(url);
|
|
18
|
+
let info;
|
|
19
|
+
try {
|
|
20
|
+
info = await stat(path);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
if (MISSING.includes(error.code)) {
|
|
23
|
+
return new Response(null, { status: 404 });
|
|
24
|
+
}
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
if (!info.isFile()) return new Response(null, { status: 404 });
|
|
28
|
+
// The blob reads the file lazily and fails if it changes after this point.
|
|
29
|
+
const blob = await openAsBlob(path);
|
|
30
|
+
return new Response(blob.stream(), {
|
|
31
|
+
status: 200,
|
|
32
|
+
headers: { "content-length": String(blob.size) },
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// `fetch` for every URL but a `file:` one, which is read from disk.
|
|
37
|
+
export function feedFetch(fetch) {
|
|
38
|
+
return (url) =>
|
|
39
|
+
String(url).startsWith("file:") ? fileFetch(url) : fetch(url);
|
|
40
|
+
}
|