@acryldev/plugin-share 0.1.3 → 0.1.5
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 +6 -0
- package/index.ts +313 -16
- package/lib/index.js +343 -11
- package/lib/index.js.map +1 -1
- package/lib/package.js +3 -3
- package/lib/package.js.map +1 -1
- package/lib/types/index.d.ts +26 -1
- package/lib/types/merge-edits.d.ts +16 -0
- package/lib/types/npm-login.d.ts +9 -0
- package/lib/types/package.d.ts +5 -2
- package/lib/types/publish.d.ts +16 -0
- package/lib/types/registry-source.d.ts +23 -0
- package/merge-edits.ts +85 -0
- package/npm-login.ts +48 -0
- package/package.json +1 -1
- package/package.ts +15 -7
- package/publish.ts +21 -0
- package/registry-source.ts +62 -0
- package/test/plugin-share.test.ts +375 -2
package/lib/index.js
CHANGED
|
@@ -2,16 +2,96 @@ import { packagePlugin } from "./package.js";
|
|
|
2
2
|
import { createReadStream, createWriteStream, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, renameSync, rmSync, statSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { dirname, join, resolve } from "node:path";
|
|
4
4
|
import { findSecrets } from "@acryldev/app-persistence";
|
|
5
|
-
import { digestFiles, digestFolder, portabilityIssues } from "@acryldev/blends-core";
|
|
5
|
+
import { digestFiles, digestFolder, pluginFiles, portabilityIssues } from "@acryldev/blends-core";
|
|
6
6
|
import { NAME } from "@acryldev/extensions";
|
|
7
7
|
import { LOCK_FILE, writeLock } from "@acryldev/manifest/lock";
|
|
8
|
-
import { InvalidPluginSourceError, PluginStore, PullError, bumpVersion, gitCli, githubCli, githubReleasesOverHttps, hostInjectFromSource, httpsDownload, packFolder, parsePluginSource, pullPlugin, readTarball, sharePlugin, storeRootOf } from "@acryldev/plugin-hub-core";
|
|
8
|
+
import { InvalidPluginSourceError, NPM_REGISTRY, PluginStore, PullError, bumpVersion, firstRegistryPort, gitCli, githubCli, githubReleasesOverHttps, hostInjectFromSource, httpsDownload, npmCli, npmRegistryOverHttps, npmRegistryViaCli, packFolder, parsePluginSource, pullPlugin, readTarball, sharePlugin, storeRootOf } from "@acryldev/plugin-hub-core";
|
|
9
9
|
import { Service } from "@deepseek-ai/cordis";
|
|
10
|
-
import { spawn } from "node:child_process";
|
|
10
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
11
12
|
import { createHash } from "node:crypto";
|
|
12
13
|
import { Readable } from "node:stream";
|
|
13
14
|
import { pipeline } from "node:stream/promises";
|
|
14
15
|
import { isMap, isSeq, parseDocument } from "yaml";
|
|
16
|
+
//#region merge-edits.ts
|
|
17
|
+
/**
|
|
18
|
+
* A pulled plugin pulled again at a newer release, with the person's own edits in its folder. Three trees meet: `base` (the release they pulled), `mine` (the
|
|
19
|
+
* folder now) and `theirs` (the new release). A file only one side changed takes that side's change; a file both changed is merged line by line, and the
|
|
20
|
+
* merge is refused when the changes overlap. Pure over bytes, except the line merge, which is git's (`git merge-file`) and is passed in so a test needs no git.
|
|
21
|
+
*/
|
|
22
|
+
const same = (a, b) => a === void 0 || b === void 0 ? a === b : Buffer.from(a).equals(b);
|
|
23
|
+
/** git's own three-way line merge. A binary file, a conflict, or no git on this computer is "cannot merge". */
|
|
24
|
+
const gitMergeFile = (base, mine, theirs) => {
|
|
25
|
+
if ([
|
|
26
|
+
base,
|
|
27
|
+
mine,
|
|
28
|
+
theirs
|
|
29
|
+
].some((bytes) => bytes.includes(0))) return void 0;
|
|
30
|
+
const dir = mkdtempSync(join(tmpdir(), "plugin-merge-"));
|
|
31
|
+
try {
|
|
32
|
+
const [b, m, t] = [
|
|
33
|
+
"base",
|
|
34
|
+
"mine",
|
|
35
|
+
"theirs"
|
|
36
|
+
].map((name) => join(dir, name));
|
|
37
|
+
writeFileSync(b, base);
|
|
38
|
+
writeFileSync(m, mine);
|
|
39
|
+
writeFileSync(t, theirs);
|
|
40
|
+
const result = spawnSync("git", [
|
|
41
|
+
"merge-file",
|
|
42
|
+
"-p",
|
|
43
|
+
m,
|
|
44
|
+
b,
|
|
45
|
+
t
|
|
46
|
+
], { maxBuffer: 64 * 1024 * 1024 });
|
|
47
|
+
return result.error === void 0 && result.status === 0 ? result.stdout : void 0;
|
|
48
|
+
} finally {
|
|
49
|
+
rmSync(dir, {
|
|
50
|
+
recursive: true,
|
|
51
|
+
force: true
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
/** shortcut: a symlink is read as the file it points at and written back as a file; a plugin that is pulled holds none (portability refuses them). */
|
|
56
|
+
function mergeTrees(base, mine, theirs, mergeFile = gitMergeFile) {
|
|
57
|
+
const files = /* @__PURE__ */ new Map();
|
|
58
|
+
const kept = [];
|
|
59
|
+
const conflicts = [];
|
|
60
|
+
const paths = [.../* @__PURE__ */ new Set([
|
|
61
|
+
...base.keys(),
|
|
62
|
+
...mine.keys(),
|
|
63
|
+
...theirs.keys()
|
|
64
|
+
])].sort();
|
|
65
|
+
for (const path of paths) {
|
|
66
|
+
const [b, m, t] = [
|
|
67
|
+
base.get(path),
|
|
68
|
+
mine.get(path),
|
|
69
|
+
theirs.get(path)
|
|
70
|
+
];
|
|
71
|
+
const put = (bytes) => {
|
|
72
|
+
if (bytes !== void 0) files.set(path, bytes);
|
|
73
|
+
};
|
|
74
|
+
if (same(m, t)) put(m);
|
|
75
|
+
else if (same(m, b)) put(t);
|
|
76
|
+
else if (same(t, b)) {
|
|
77
|
+
put(m);
|
|
78
|
+
kept.push(path);
|
|
79
|
+
} else if (b !== void 0 && m !== void 0 && t !== void 0) {
|
|
80
|
+
const merged = mergeFile(b, m, t);
|
|
81
|
+
if (merged === void 0) conflicts.push(path);
|
|
82
|
+
else {
|
|
83
|
+
files.set(path, merged);
|
|
84
|
+
kept.push(path);
|
|
85
|
+
}
|
|
86
|
+
} else conflicts.push(path);
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
files,
|
|
90
|
+
kept,
|
|
91
|
+
conflicts
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
15
95
|
//#region native.ts
|
|
16
96
|
/**
|
|
17
97
|
* A plugin's native packages: large, per-OS npm packages a plugin needs and declares instead of depending on (a pull installs no dependencies). The
|
|
@@ -204,15 +284,51 @@ function releaseNative(native, places, keepDownload = false) {
|
|
|
204
284
|
});
|
|
205
285
|
}
|
|
206
286
|
//#endregion
|
|
287
|
+
//#region npm-login.ts
|
|
288
|
+
/**
|
|
289
|
+
* The person's own npm, for a publish: who is signed in (`npm whoami`) and the publish itself, both through the `npm` on this computer and its configuration
|
|
290
|
+
* (`.npmrc`). Nothing here reads or stores a token; a missing npm or login is reported with what to do.
|
|
291
|
+
*/
|
|
292
|
+
/** Why `npm whoami` failed, in words: not signed in (with the command), or what npm said without its pointer to a log file. */
|
|
293
|
+
function whoamiProblem(stderr, registry) {
|
|
294
|
+
const said = stderr.split("\n").map((line) => line.replace(/^npm error\s*/u, "").trim()).filter((line) => line !== "" && !/^npm (notice|warn)|A complete log of this run/iu.test(line));
|
|
295
|
+
return /ENEEDAUTH|E401|E403|need auth|Unauthorized|not logged in/iu.test(stderr) || said.length === 0 ? `npm whoami failed: you are not signed in to npm on ${registry} (sign in with: npm login --registry ${registry})` : `npm whoami failed: ${said.slice(0, 2).join(" ")} (sign in with: npm login --registry ${registry})`;
|
|
296
|
+
}
|
|
297
|
+
function npmWithLogin() {
|
|
298
|
+
return {
|
|
299
|
+
...npmCli(),
|
|
300
|
+
login(registry) {
|
|
301
|
+
const result = spawnSync("npm", [
|
|
302
|
+
"whoami",
|
|
303
|
+
"--registry",
|
|
304
|
+
registry
|
|
305
|
+
], {
|
|
306
|
+
encoding: "utf8",
|
|
307
|
+
shell: process.platform === "win32",
|
|
308
|
+
env: {
|
|
309
|
+
...process.env,
|
|
310
|
+
npm_config_update_notifier: "false"
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
if (result.error !== void 0) throw new Error("npm is not available here: install Node.js (https://nodejs.org), which includes npm, and sign in with: npm login");
|
|
314
|
+
const account = (result.stdout ?? "").trim();
|
|
315
|
+
if (result.status !== 0 || account === "") throw new Error(whoamiProblem(result.stderr ?? "", registry));
|
|
316
|
+
return account;
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
//#endregion
|
|
207
321
|
//#region publish.ts
|
|
208
322
|
/** The line a friend copies: it names the bytes, so the check comes for free. */
|
|
209
323
|
const pullLineOf = (owner, repo, version, digest) => `npm run acryl -- plugin pull github:${owner}/${repo}@${version}@sha256:${digest}`;
|
|
324
|
+
/** The line a friend copies for an npm package. */
|
|
325
|
+
const npmPullLineOf = (name, version, digest) => `npm run acryl -- plugin pull npm:${name}@${version}@sha256:${digest}`;
|
|
210
326
|
/**
|
|
211
327
|
* The same plan: what the person confirmed is what would be published now. `released` only explains the version, and is gone once the next one is packaged.
|
|
212
328
|
* Field by field, so a plan that came back through JSON from another surface (the frame's confirm) is the same plan whatever order its fields arrived in.
|
|
213
329
|
*/
|
|
214
330
|
function samePlan(a, b) {
|
|
215
|
-
return a.owner === b.owner && a.repo === b.repo && a.version === b.version && a.visibility === b.visibility && a.license === b.license && a.createsRepository === b.createsRepository && a.integrity === b.integrity && a.digest === b.digest && a.pullLine === b.pullLine && a.files.length === b.files.length && a.files.every((file, index) => file === b.files[index]);
|
|
331
|
+
return a.target === b.target && a.package === b.package && a.registry === b.registry && a.owner === b.owner && a.repo === b.repo && a.version === b.version && a.visibility === b.visibility && a.license === b.license && a.createsRepository === b.createsRepository && a.integrity === b.integrity && a.digest === b.digest && a.pullLine === b.pullLine && a.files.length === b.files.length && a.files.every((file, index) => file === b.files[index]);
|
|
216
332
|
}
|
|
217
333
|
//#endregion
|
|
218
334
|
//#region pull-check.ts
|
|
@@ -375,6 +491,41 @@ function withPulledRecord(text, name, record) {
|
|
|
375
491
|
}
|
|
376
492
|
return String(document);
|
|
377
493
|
}
|
|
494
|
+
const GITHUB_REPO = /^https:\/\/github\.com\/([A-Za-z0-9][A-Za-z0-9-]*)\/([A-Za-z0-9][A-Za-z0-9._-]*?)(?:\.git)?\/?$/u;
|
|
495
|
+
/** Find `id` (or the package name, `@scope/name`) in the index text; its release as a source, or why there is none. */
|
|
496
|
+
function lookUpRegistry(indexText, id) {
|
|
497
|
+
let entries;
|
|
498
|
+
try {
|
|
499
|
+
entries = JSON.parse(indexText).entries;
|
|
500
|
+
} catch {
|
|
501
|
+
entries = void 0;
|
|
502
|
+
}
|
|
503
|
+
if (!Array.isArray(entries)) return {
|
|
504
|
+
ok: false,
|
|
505
|
+
reason: "the registry answered in a way this app does not understand",
|
|
506
|
+
next: "try again later, or pull github:<owner>/<repo> from the plugin page"
|
|
507
|
+
};
|
|
508
|
+
const wanted = id.trim().toLowerCase();
|
|
509
|
+
const entry = entries.find((item) => typeof item?.id === "string" && (item.id.toLowerCase() === wanted || typeof item.name === "string" && item.name.toLowerCase() === wanted));
|
|
510
|
+
if (entry === void 0) return {
|
|
511
|
+
ok: false,
|
|
512
|
+
reason: `the registry lists no plugin ${id}`,
|
|
513
|
+
next: "copy the id from https://cordisplugins.github.io, or pull github:<owner>/<repo>"
|
|
514
|
+
};
|
|
515
|
+
const repo = typeof entry.repo === "string" ? GITHUB_REPO.exec(entry.repo) : null;
|
|
516
|
+
if (repo === null || typeof entry.version !== "string" || typeof entry.integrity !== "string") return {
|
|
517
|
+
ok: false,
|
|
518
|
+
reason: `the registry's entry for ${id} does not name a GitHub release this app can pull`,
|
|
519
|
+
next: "open its page on https://cordisplugins.github.io for how to install it"
|
|
520
|
+
};
|
|
521
|
+
return {
|
|
522
|
+
ok: true,
|
|
523
|
+
hit: {
|
|
524
|
+
source: `github:${repo[1]}/${repo[2]}@${entry.version}`,
|
|
525
|
+
integrity: entry.integrity
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
}
|
|
378
529
|
//#endregion
|
|
379
530
|
//#region index.ts
|
|
380
531
|
/**
|
|
@@ -396,17 +547,19 @@ function releaseOf(folder) {
|
|
|
396
547
|
/** The license a new public plugin is offered under; the plan shows it before the person says yes. */
|
|
397
548
|
const DEFAULT_LICENSE = "MIT";
|
|
398
549
|
const LICENSE_ID = /^[A-Za-z0-9][A-Za-z0-9.+-]*$/u;
|
|
550
|
+
/** An npm scope: what follows `@`. */
|
|
551
|
+
const NPM_SCOPE = /^[a-z0-9][a-z0-9._~-]*$/u;
|
|
399
552
|
const publishRefused = (reason, next) => ({
|
|
400
553
|
ok: false,
|
|
401
554
|
reason,
|
|
402
555
|
next
|
|
403
556
|
});
|
|
404
|
-
const SOURCE_FORMS = "github:<owner>/<repo>[@<version>] or a .tgz path or address";
|
|
557
|
+
const SOURCE_FORMS = "github:<owner>/<repo>[@<version>], npm:<package>[@<version>], registry:<id> or a .tgz path or address";
|
|
405
558
|
/** Read what a person wrote. A `.tgz` path may be relative to the app folder; the hub only takes absolute ones. */
|
|
406
559
|
function readSource(input, root) {
|
|
407
560
|
const { source, digest } = splitDigest(input.trim());
|
|
408
561
|
const parsed = parsePluginSource(/\.(?:tgz|tar\.gz)$/iu.test(source) && !/^[a-z][a-z0-9+]+:/iu.test(source) ? resolve(root, source) : source);
|
|
409
|
-
if (parsed.kind !== "github" && parsed.kind !== "tarball-file" && parsed.kind !== "tarball-url") throw new InvalidPluginSourceError(input, `${parsed.kind === "name" ? "a bare name" : parsed.kind} sources are not offered here; pull ${SOURCE_FORMS}`);
|
|
562
|
+
if (parsed.kind !== "github" && parsed.kind !== "npm" && parsed.kind !== "tarball-file" && parsed.kind !== "tarball-url") throw new InvalidPluginSourceError(input, `${parsed.kind === "name" ? "a bare name" : parsed.kind} sources are not offered here; pull ${SOURCE_FORMS}`);
|
|
410
563
|
return {
|
|
411
564
|
source: parsed,
|
|
412
565
|
...digest === void 0 ? {} : { digest }
|
|
@@ -415,8 +568,27 @@ function readSource(input, root) {
|
|
|
415
568
|
/** The source as another machine can fetch it: the version pinned; a file on this machine names nothing. */
|
|
416
569
|
function recordedSource(source, version) {
|
|
417
570
|
if (source.kind === "github") return `github:${source.owner}/${source.repo}@${version}`;
|
|
571
|
+
if (source.kind === "npm") return `npm:${source.name.full}@${version}`;
|
|
418
572
|
return source.kind === "tarball-url" ? source.url : void 0;
|
|
419
573
|
}
|
|
574
|
+
/** What two releases of one plugin share in a recorded source: `github:<owner>/<repo>` or `npm:<package>`, lower case. */
|
|
575
|
+
const repoOf = (source) => {
|
|
576
|
+
const github = /^(github:[^/@]+\/[^/@]+)@/u.exec(source)?.[1];
|
|
577
|
+
const npm = /^(npm:(?:@[^/@]+\/)?[^/@]+)@/u.exec(source)?.[1];
|
|
578
|
+
return (github ?? npm)?.toLowerCase();
|
|
579
|
+
};
|
|
580
|
+
/** A package's folder in `plugins/`: the name after its scope (`@ana/clock` is `clock`). */
|
|
581
|
+
const folderOf = (packageName) => packageName.includes("/") ? packageName.slice(packageName.lastIndexOf("/") + 1) : packageName;
|
|
582
|
+
const treeOf = (files) => new Map(files.map((file) => [file.path, file.bytes]));
|
|
583
|
+
/** Make a plugin folder hold exactly `to`, having held `from`: files only `from` had go, every file of `to` is written. */
|
|
584
|
+
function applyTree(folder, from, to) {
|
|
585
|
+
for (const path of from.keys()) if (!to.has(path)) rmSync(join(folder, ...path.split("/")), { force: true });
|
|
586
|
+
for (const [path, bytes] of to) {
|
|
587
|
+
const file = join(folder, ...path.split("/"));
|
|
588
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
589
|
+
writeFileSync(file, bytes);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
420
592
|
const reasonOf = (cause) => {
|
|
421
593
|
if (cause instanceof PullError || cause instanceof InvalidPluginSourceError) return cause.reason;
|
|
422
594
|
return cause instanceof Error ? cause.message : String(cause);
|
|
@@ -427,6 +599,8 @@ var PluginShare = class extends Service {
|
|
|
427
599
|
dir;
|
|
428
600
|
releases;
|
|
429
601
|
download;
|
|
602
|
+
registryIndex;
|
|
603
|
+
npmRegistry;
|
|
430
604
|
ports;
|
|
431
605
|
/** One at a time: two pulls of one name must not race for its folder, nor two publishes for its package. */
|
|
432
606
|
queue = Promise.resolve();
|
|
@@ -446,6 +620,8 @@ var PluginShare = class extends Service {
|
|
|
446
620
|
this.dir = resolve(config.dir ?? join(ctx.appInstance.root, "plugins"));
|
|
447
621
|
this.releases = config.releases ?? githubReleasesOverHttps();
|
|
448
622
|
this.download = config.download ?? httpsDownload();
|
|
623
|
+
this.registryIndex = config.registryIndex ?? "https://cordisplugins.github.io/registry/index.json";
|
|
624
|
+
this.npmRegistry = config.npm ?? firstRegistryPort(npmRegistryOverHttps(), npmRegistryViaCli());
|
|
449
625
|
this.nativeWork = config.native ?? nativePorts();
|
|
450
626
|
this.ports = config.publish ?? {
|
|
451
627
|
host: githubCli(),
|
|
@@ -554,6 +730,7 @@ var PluginShare = class extends Service {
|
|
|
554
730
|
const plan = await this.planNow(name, options);
|
|
555
731
|
if ("ok" in plan) return plan;
|
|
556
732
|
if (!samePlan(plan, confirmed)) return publishRefused(`this plugin's publish changed since you confirmed it (now ${plan.owner}/${plan.repo} v${plan.version}, digest ${plan.digest}); nothing was sent`, "look at the new plan and say yes again");
|
|
733
|
+
if (plan.target === "npm") return this.publishNpm(name, plan);
|
|
557
734
|
const { home } = this.own.appInstance;
|
|
558
735
|
try {
|
|
559
736
|
const shared = await sharePlugin(join(this.packages, name), {
|
|
@@ -700,6 +877,7 @@ var PluginShare = class extends Service {
|
|
|
700
877
|
async planNow(name, options) {
|
|
701
878
|
const source = join(this.dir, name);
|
|
702
879
|
if (!NAME.test(name) || !existsSync(source)) return publishRefused(`this app has no plugin "${name}" in plugins/`, "name a folder of plugins/");
|
|
880
|
+
if (options.to === "npm") return this.planNpm(name, source, options);
|
|
703
881
|
try {
|
|
704
882
|
const { host } = this.ports;
|
|
705
883
|
const owner = options.owner ?? host.login();
|
|
@@ -741,8 +919,111 @@ var PluginShare = class extends Service {
|
|
|
741
919
|
return publishRefused(reasonOf(cause), "fix it, or sign in with gh; nothing was sent");
|
|
742
920
|
}
|
|
743
921
|
}
|
|
922
|
+
/**
|
|
923
|
+
* What publishing to npm would do, read without sending anything: the account is the person's own npm login (`npm whoami`), the package is
|
|
924
|
+
* `@<scope>/<name>` (the scope defaults to that account), and a version npm already has makes the plan name the next patch, as a GitHub release does.
|
|
925
|
+
*/
|
|
926
|
+
async planNpm(name, source, options) {
|
|
927
|
+
const npm = this.ports.npm ?? npmWithLogin();
|
|
928
|
+
const registry = options.registry ?? NPM_REGISTRY;
|
|
929
|
+
try {
|
|
930
|
+
const account = npm.login(registry);
|
|
931
|
+
const scope = (options.scope ?? account).replace(/^@/u, "").toLowerCase();
|
|
932
|
+
if (!NPM_SCOPE.test(scope)) return publishRefused(`"${scope}" is not an npm scope`, "write letters, digits and dashes, for example --scope acme");
|
|
933
|
+
const license = options.license ?? DEFAULT_LICENSE;
|
|
934
|
+
if (!LICENSE_ID.test(license)) return publishRefused(`"${license}" is not a license id`, "write one such as MIT or Apache-2.0");
|
|
935
|
+
const packageName = `@${scope}/${name}`;
|
|
936
|
+
const pack = (version) => this.packNpm(source, name, packageName, license, version);
|
|
937
|
+
let packaged = await pack();
|
|
938
|
+
const released = npm.hasVersion(packageName, packaged.version, registry) ? packaged.version : void 0;
|
|
939
|
+
if (released !== void 0) packaged = await pack(bumpVersion(released, "patch"));
|
|
940
|
+
const secrets = findSecrets(readTarball(packFolder(packaged.dir).tarball).map((file) => ({
|
|
941
|
+
path: file.path,
|
|
942
|
+
text: Buffer.from(file.content).toString("utf8")
|
|
943
|
+
})));
|
|
944
|
+
if (secrets.length > 0) return publishRefused(`the secret check found ${String(secrets.length)} possible secret(s) in ${secrets.map((secret) => secret.path).join(", ")}; nothing was sent`, "take them out of the plugin and read them from the app's settings");
|
|
945
|
+
return {
|
|
946
|
+
target: "npm",
|
|
947
|
+
package: packageName,
|
|
948
|
+
registry,
|
|
949
|
+
owner: scope,
|
|
950
|
+
repo: name,
|
|
951
|
+
version: packaged.version,
|
|
952
|
+
visibility: "public",
|
|
953
|
+
license,
|
|
954
|
+
createsRepository: false,
|
|
955
|
+
...released === void 0 ? {} : { released },
|
|
956
|
+
files: packaged.files,
|
|
957
|
+
integrity: packaged.integrity,
|
|
958
|
+
digest: packaged.digest,
|
|
959
|
+
pullLine: npmPullLineOf(packageName, packaged.version, packaged.digest)
|
|
960
|
+
};
|
|
961
|
+
} catch (cause) {
|
|
962
|
+
return publishRefused(reasonOf(cause), "fix it, or sign in with: npm login (--registry <address> for another registry); nothing was sent");
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
/** The plugin as the npm package `packageName`: a package of its own folder, the same bytes whenever it is made again for the same version. */
|
|
966
|
+
packNpm(source, name, packageName, license, version) {
|
|
967
|
+
return packagePlugin(source, this.packages, name, `The ${name} Cordis plugin.`, {
|
|
968
|
+
release: { license },
|
|
969
|
+
packageName,
|
|
970
|
+
...version === void 0 ? {} : { version }
|
|
971
|
+
});
|
|
972
|
+
}
|
|
973
|
+
/** The confirmed npm plan, published with the person's own npm: the package is made again, and only the bytes that were planned are sent. */
|
|
974
|
+
async publishNpm(name, plan) {
|
|
975
|
+
const registry = plan.registry ?? NPM_REGISTRY;
|
|
976
|
+
const packageName = plan.package ?? `@${plan.owner}/${plan.repo}`;
|
|
977
|
+
try {
|
|
978
|
+
const packed = await this.packNpm(join(this.dir, name), name, packageName, plan.license, plan.version);
|
|
979
|
+
if (packed.integrity !== plan.integrity) return publishRefused(`the package made now is not the one that was planned (${packageName} v${plan.version}); nothing was sent`, "look at the new plan and say yes again");
|
|
980
|
+
(this.ports.npm ?? npmWithLogin()).publish(packed.tarball, registry, "public");
|
|
981
|
+
const url = registry === NPM_REGISTRY ? `https://www.npmjs.com/package/${packageName}` : `${registry.replace(/\/$/u, "")}/${packageName}`;
|
|
982
|
+
return {
|
|
983
|
+
ok: true,
|
|
984
|
+
url,
|
|
985
|
+
releaseUrl: url,
|
|
986
|
+
version: packed.version,
|
|
987
|
+
integrity: packed.integrity,
|
|
988
|
+
digest: plan.digest,
|
|
989
|
+
pullLine: plan.pullLine
|
|
990
|
+
};
|
|
991
|
+
} catch (cause) {
|
|
992
|
+
return publishRefused(reasonOf(cause), "look at the registry; part of this publish may have been sent");
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
/** A `registry:<id>` source turned into the release it lists, with the integrity the registry lists for it; any other source as it is. */
|
|
996
|
+
async resolveRegistry(input, expectIntegrity) {
|
|
997
|
+
const text = input.trim();
|
|
998
|
+
if (!/^registry:/iu.test(text)) return {
|
|
999
|
+
input,
|
|
1000
|
+
...expectIntegrity === void 0 ? {} : { expectIntegrity }
|
|
1001
|
+
};
|
|
1002
|
+
let named;
|
|
1003
|
+
try {
|
|
1004
|
+
named = splitDigest(text);
|
|
1005
|
+
} catch (cause) {
|
|
1006
|
+
return refuse("source", reasonOf(cause), "write the 64 hex digits the plugin page shows after @sha256:");
|
|
1007
|
+
}
|
|
1008
|
+
let index;
|
|
1009
|
+
try {
|
|
1010
|
+
index = Buffer.from(await this.download(this.registryIndex)).toString("utf8");
|
|
1011
|
+
} catch (cause) {
|
|
1012
|
+
return refuse("fetch", `the registry could not be read: ${reasonOf(cause)}`, "check the internet connection and pull again, or pull github:<owner>/<repo>");
|
|
1013
|
+
}
|
|
1014
|
+
const found = lookUpRegistry(index, named.source.slice(9));
|
|
1015
|
+
if (!found.ok) return refuse("source", found.reason, found.next);
|
|
1016
|
+
return {
|
|
1017
|
+
input: named.digest === void 0 ? found.hit.source : `${found.hit.source}@sha256:${named.digest}`,
|
|
1018
|
+
expectIntegrity: expectIntegrity ?? found.hit.integrity
|
|
1019
|
+
};
|
|
1020
|
+
}
|
|
744
1021
|
async pullNow(input, expectIntegrity) {
|
|
745
1022
|
const { home, root } = this.own.appInstance;
|
|
1023
|
+
const resolved = await this.resolveRegistry(input, expectIntegrity);
|
|
1024
|
+
if ("refusal" in resolved) return resolved;
|
|
1025
|
+
input = resolved.input;
|
|
1026
|
+
expectIntegrity = resolved.expectIntegrity;
|
|
746
1027
|
let read;
|
|
747
1028
|
try {
|
|
748
1029
|
read = readSource(input, root);
|
|
@@ -757,11 +1038,13 @@ var PluginShare = class extends Service {
|
|
|
757
1038
|
pulled = await pullPlugin(read.source, {
|
|
758
1039
|
store: new PluginStore(scratch),
|
|
759
1040
|
releases: this.releases,
|
|
1041
|
+
registry: this.npmRegistry,
|
|
760
1042
|
download: this.download
|
|
761
1043
|
}, expectIntegrity === void 0 ? {} : { expectIntegrity });
|
|
762
1044
|
} catch (cause) {
|
|
763
1045
|
return refuse("fetch", reasonOf(cause), "check the source with the person who released it; nothing was installed");
|
|
764
1046
|
}
|
|
1047
|
+
const folderIn = (packageName) => read.source.kind === "npm" ? folderOf(packageName) : packageName;
|
|
765
1048
|
const files = readTarball(readFileSync(pulled.stored.path)).map(({ path, content }) => ({
|
|
766
1049
|
path,
|
|
767
1050
|
bytes: content,
|
|
@@ -769,12 +1052,12 @@ var PluginShare = class extends Service {
|
|
|
769
1052
|
}));
|
|
770
1053
|
const refused = pullRefusal(pulled.manifest, files, {
|
|
771
1054
|
digest: read.digest,
|
|
772
|
-
nameFits: (name) => NAME.test(name),
|
|
1055
|
+
nameFits: (name) => NAME.test(folderIn(name)),
|
|
773
1056
|
hasService: (service) => this.own.get(service) !== void 0
|
|
774
1057
|
});
|
|
775
1058
|
if (refused !== void 0) return refused;
|
|
776
1059
|
const declared = nativesOf(pulled.manifest);
|
|
777
|
-
return await this.install(pulled.stored.name, pulled.stored.version, pulled.stored.integrity, files, recordedSource(read.source, pulled.stored.version), "natives" in declared ? nativeFor(declared.natives) : void 0);
|
|
1060
|
+
return await this.install(folderIn(pulled.stored.name), pulled.stored.version, pulled.stored.integrity, files, recordedSource(read.source, pulled.stored.version), "natives" in declared ? nativeFor(declared.natives) : void 0);
|
|
778
1061
|
} finally {
|
|
779
1062
|
rmSync(scratch, {
|
|
780
1063
|
recursive: true,
|
|
@@ -782,6 +1065,44 @@ var PluginShare = class extends Service {
|
|
|
782
1065
|
});
|
|
783
1066
|
}
|
|
784
1067
|
}
|
|
1068
|
+
/**
|
|
1069
|
+
* What the folder holds once a newer release of its pulled plugin comes in, the person's own changes kept: the release they pulled is the base of a
|
|
1070
|
+
* three-way merge (fetched again from where it came from, checked against its recorded integrity, unless the folder is untouched and is the base).
|
|
1071
|
+
* Refused, and nothing written, when the release and the person changed the same lines.
|
|
1072
|
+
*/
|
|
1073
|
+
async planUpdate(name, before, held, release, target) {
|
|
1074
|
+
const mine = treeOf(pluginFiles(target));
|
|
1075
|
+
let base = mine;
|
|
1076
|
+
if (held !== before.digest) {
|
|
1077
|
+
const scratch = mkdtempSync(join(this.own.appInstance.home, "plugin-base-"));
|
|
1078
|
+
try {
|
|
1079
|
+
const pulled = await pullPlugin(readSource(before.source, this.own.appInstance.root).source, {
|
|
1080
|
+
store: new PluginStore(scratch),
|
|
1081
|
+
releases: this.releases,
|
|
1082
|
+
registry: this.npmRegistry,
|
|
1083
|
+
download: this.download
|
|
1084
|
+
}, { expectIntegrity: before.integrity });
|
|
1085
|
+
base = treeOf(readTarball(readFileSync(pulled.stored.path)).map(({ path, content }) => ({
|
|
1086
|
+
path,
|
|
1087
|
+
bytes: content,
|
|
1088
|
+
kind: "file"
|
|
1089
|
+
})));
|
|
1090
|
+
} catch (cause) {
|
|
1091
|
+
return refuse("folder-exists", `plugins/${name} has changes of your own, and the release it was pulled from could not be fetched to keep them: ${reasonOf(cause)}`, `check the internet connection and pull again, or move plugins/${name} out of plugins/ and pull again`);
|
|
1092
|
+
} finally {
|
|
1093
|
+
rmSync(scratch, {
|
|
1094
|
+
recursive: true,
|
|
1095
|
+
force: true
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
const merged = mergeTrees(base, mine, treeOf(release));
|
|
1100
|
+
if (merged.conflicts.length > 0) return refuse("folder-exists", `plugins/${name} has changes of your own to ${merged.conflicts.join(", ")}, and the new release changes ${merged.conflicts.length === 1 ? "it" : "them"} too`, `plugins/${name} is untouched: move it out of plugins/ and pull again, then bring your changes back by hand`);
|
|
1101
|
+
return {
|
|
1102
|
+
mine,
|
|
1103
|
+
merged
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
785
1106
|
/** Put the files in `plugins/<name>/` and install them; whatever does not end running takes its folder with it. */
|
|
786
1107
|
async install(name, version, integrity, files, source, native) {
|
|
787
1108
|
const target = join(this.dir, name);
|
|
@@ -800,14 +1121,23 @@ var PluginShare = class extends Service {
|
|
|
800
1121
|
const wrote = !existsSync(target);
|
|
801
1122
|
const before = this.recorded(name);
|
|
802
1123
|
const held = wrote ? void 0 : digestFolder(target);
|
|
1124
|
+
let update;
|
|
803
1125
|
if (held !== void 0 && held !== digest) {
|
|
804
|
-
const
|
|
805
|
-
|
|
806
|
-
|
|
1126
|
+
const sameRepo = before !== void 0 && source !== void 0 && repoOf(before.source) !== void 0 && repoOf(before.source) === repoOf(source);
|
|
1127
|
+
if (before !== void 0 && sameRepo && before.digest !== digest) {
|
|
1128
|
+
const planned = await this.planUpdate(name, before, held, files, target);
|
|
1129
|
+
if ("refusal" in planned) return planned;
|
|
1130
|
+
update = planned;
|
|
1131
|
+
} else {
|
|
1132
|
+
const holder = before === void 0 ? "made in this app" : `pulled from ${before.source}`;
|
|
1133
|
+
const next = before !== void 0 && held === before.digest ? `keep it, or remove it (npm run acryl -- plugin remove ${name}) and pull again` : `keep it, or move plugins/${name} out of plugins/ and pull again`;
|
|
1134
|
+
return refuse("folder-exists", `plugins/${name} holds a different copy of this plugin, ${holder}`, next);
|
|
1135
|
+
}
|
|
807
1136
|
}
|
|
808
1137
|
const liveBefore = this.live.get(name);
|
|
809
1138
|
this.live.set(name, digest);
|
|
810
1139
|
if (wrote) renameSync(incoming, target);
|
|
1140
|
+
else if (update !== void 0) applyTree(target, update.mine, update.merged.files);
|
|
811
1141
|
const remembered = source !== void 0 && this.remember(name, {
|
|
812
1142
|
source,
|
|
813
1143
|
integrity,
|
|
@@ -821,6 +1151,7 @@ var PluginShare = class extends Service {
|
|
|
821
1151
|
recursive: true,
|
|
822
1152
|
force: true
|
|
823
1153
|
});
|
|
1154
|
+
else if (update !== void 0) applyTree(target, update.merged.files, update.mine);
|
|
824
1155
|
if (remembered) this.remember(name, before);
|
|
825
1156
|
if (liveBefore === void 0) this.live.delete(name);
|
|
826
1157
|
else this.live.set(name, liveBefore);
|
|
@@ -856,6 +1187,7 @@ var PluginShare = class extends Service {
|
|
|
856
1187
|
action: result.action,
|
|
857
1188
|
state: result.state,
|
|
858
1189
|
remembered,
|
|
1190
|
+
...update === void 0 || update.merged.kept.length === 0 ? {} : { kept: update.merged.kept },
|
|
859
1191
|
...this.keepNative(name, native, nativeBefore, nativeAction)
|
|
860
1192
|
};
|
|
861
1193
|
} finally {
|