@fungi.computer/oyster 0.12.1 → 0.13.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/dist/build/candidate-cli.mjs +257 -142
- package/dist/build/definition-emission.js +17 -3
- package/dist/build/local-build.d.ts +6 -0
- package/dist/build/local-build.js +104 -91
- package/dist/build/process-group.d.ts +6 -0
- package/dist/build/process-group.js +92 -0
- package/dist/build/workspace-build.d.ts +1 -1
- package/dist/build.d.ts +1 -1
- package/dist/build.js +1 -1
- package/dist/embed-scripts.js +1 -1
- package/dist/host-page-assets.d.ts +12 -0
- package/dist/host-page-assets.js +1 -0
- package/dist/host-page.d.ts +31 -0
- package/dist/host-page.js +342 -0
- package/dist/host.js +116 -49
- package/dist/pack.d.ts +3 -1
- package/dist/pack.js +28 -7
- package/dist/page/app-permissions.js +3 -13
- package/dist/page-script.d.ts +1 -1
- package/dist/page-script.js +2 -2
- package/dist/standalone/hello-app.d.ts +4 -1
- package/dist/standalone/hello-app.js +67 -22
- package/dist/standalone/hello-lesson.d.ts +2 -0
- package/dist/standalone/hello-lesson.js +36 -0
- package/dist/standalone/local-desktop.d.ts +6 -4
- package/dist/standalone/local-desktop.js +39 -20
- package/dist/standalone/sign-in-link.d.ts +2 -1
- package/dist/standalone/sign-in-link.js +94 -32
- package/dist/standalone/try-cli.d.ts +12 -9
- package/dist/standalone/try-cli.js +402 -137
- package/docs/api/pack.md +3 -2
- package/package.json +12 -8
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { helloPage } from "./hello-lesson.js";
|
|
2
|
+
/** A first commit, never mere .git existence, makes the editable source ready. */
|
|
3
|
+
export declare function prepareHelloSource(directory: string, signal: AbortSignal): Promise<string>;
|
|
2
4
|
/** Pack the Hello App kept at `<directory>/hello-app` and install it as
|
|
3
5
|
* `hello`; answers the HTTP status (201 new, 200 when already installed).
|
|
4
6
|
* The first call writes and commits the source; later calls pack its current
|
|
@@ -7,4 +9,5 @@ export declare function installHelloApp(input: Readonly<{
|
|
|
7
9
|
directory: string;
|
|
8
10
|
url: string;
|
|
9
11
|
key: string;
|
|
12
|
+
signal?: AbortSignal;
|
|
10
13
|
}>): Promise<number>;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
import { CleanupError } from "../build/process-group.js";
|
|
1
2
|
// The starter Hello App for a local Oyster: a one-page bundled-html App,
|
|
2
3
|
// committed to git, packed into a Release and installed as `hello`. The
|
|
3
4
|
// quick start and `oyster try` install the same App through this module;
|
|
4
5
|
// `try` keeps its source at `<project>/hello-app` for the person to edit.
|
|
5
|
-
import {
|
|
6
|
-
import { access, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
6
|
+
import { GitCommandError, runGit } from "../build/local-build.js";
|
|
7
|
+
import { access, mkdir, mkdtemp, readdir, rename, rm, writeFile, } from "node:fs/promises";
|
|
7
8
|
import { tmpdir } from "node:os";
|
|
8
9
|
import { join } from "node:path";
|
|
10
|
+
import { helloPage, helloLayout } from "./hello-lesson.js";
|
|
9
11
|
import { helloAppScript } from "#hello-app-script";
|
|
10
12
|
import { installRelease, packRelease } from "../pack.js";
|
|
11
13
|
const manifest = {
|
|
@@ -13,39 +15,78 @@ const manifest = {
|
|
|
13
15
|
private: true,
|
|
14
16
|
fungi: { app: { displayName: "Hello", icon: "fungi", capabilities: [] } },
|
|
15
17
|
};
|
|
16
|
-
|
|
17
|
-
export const helloPage = '<!doctype html>\n<title>Hello</title>\n<h1>Hello from my first Oyster App</h1>\n<script type="module" src="./main.js"></script>\n';
|
|
18
|
+
export { helloPage } from "./hello-lesson.js";
|
|
18
19
|
const exists = (path) => access(path).then(() => true, () => false);
|
|
19
|
-
const git = (directory, args) =>
|
|
20
|
-
|
|
21
|
-
directory,
|
|
20
|
+
const git = async (directory, args, signal) => new TextDecoder()
|
|
21
|
+
.decode(await runGit([
|
|
22
22
|
"-c",
|
|
23
23
|
"user.name=Oyster",
|
|
24
24
|
"-c",
|
|
25
25
|
"user.email=oyster@example.invalid",
|
|
26
26
|
...args,
|
|
27
|
-
],
|
|
28
|
-
|
|
29
|
-
async function
|
|
30
|
-
|
|
27
|
+
], directory, signal, process.env))
|
|
28
|
+
.trim();
|
|
29
|
+
async function seedHello(source, signal) {
|
|
30
|
+
signal.throwIfAborted();
|
|
31
31
|
await mkdir(join(source, "bundle"), { recursive: true });
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
const { shellStyles, loginFontStyles } = await import("#page-script");
|
|
33
|
+
for (const [name, contents] of [
|
|
34
|
+
["package.json", JSON.stringify(manifest, null, 2) + "\n"],
|
|
35
|
+
["bundle/index.html", helloPage],
|
|
36
|
+
["bundle/main.js", helloAppScript],
|
|
37
|
+
["bundle/lesson.css", shellStyles + loginFontStyles + helloLayout],
|
|
38
|
+
]) {
|
|
39
|
+
signal.throwIfAborted();
|
|
40
|
+
await writeFile(join(source, name), contents, { flag: "wx" }).catch((cause) => {
|
|
41
|
+
if (!(cause instanceof Error &&
|
|
42
|
+
"code" in cause &&
|
|
43
|
+
cause.code === "EEXIST"))
|
|
44
|
+
throw cause;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
await git(source, ["init", "-q"], signal);
|
|
48
|
+
await git(source, ["add", "."], signal);
|
|
49
|
+
await git(source, ["commit", "-qm", "Hello App"], signal);
|
|
50
|
+
return git(source, ["rev-parse", "HEAD"], signal);
|
|
51
|
+
}
|
|
52
|
+
/** A first commit, never mere .git existence, makes the editable source ready. */
|
|
53
|
+
export async function prepareHelloSource(directory, signal) {
|
|
54
|
+
signal.throwIfAborted();
|
|
55
|
+
const source = join(directory, "hello-app");
|
|
56
|
+
if (await exists(join(source, ".git"))) {
|
|
57
|
+
try {
|
|
58
|
+
return await git(source, ["rev-parse", "--verify", "--quiet", "HEAD"], signal);
|
|
59
|
+
}
|
|
60
|
+
catch (cause) {
|
|
61
|
+
if (!(cause instanceof GitCommandError && cause.exitCode === 1))
|
|
62
|
+
throw cause;
|
|
63
|
+
return seedHello(source, signal);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if ((await exists(source)) && (await readdir(source)).length > 0)
|
|
67
|
+
throw new Error("hello-app already contains files without a Git repository; move that folder before creating the starter App.");
|
|
68
|
+
const stage = await mkdtemp(join(directory, ".hello-app-"));
|
|
69
|
+
try {
|
|
70
|
+
const commit = await seedHello(stage, signal);
|
|
71
|
+
signal.throwIfAborted();
|
|
72
|
+
await rename(stage, source);
|
|
73
|
+
return commit;
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
await rm(stage, { recursive: true, force: true }).catch((cause) => {
|
|
77
|
+
throw new CleanupError("Hello staging cleanup failed", { cause });
|
|
78
|
+
});
|
|
79
|
+
}
|
|
39
80
|
}
|
|
40
81
|
/** Pack the Hello App kept at `<directory>/hello-app` and install it as
|
|
41
82
|
* `hello`; answers the HTTP status (201 new, 200 when already installed).
|
|
42
83
|
* The first call writes and commits the source; later calls pack its current
|
|
43
84
|
* `HEAD`, so a committed edit followed by `oyster try` shows the change. */
|
|
44
85
|
export async function installHelloApp(input) {
|
|
86
|
+
const signal = input.signal ?? new AbortController().signal;
|
|
45
87
|
const source = join(input.directory, "hello-app");
|
|
46
|
-
const commit =
|
|
47
|
-
|
|
48
|
-
: await writeHelloApp(input.directory);
|
|
88
|
+
const commit = await prepareHelloSource(input.directory, signal);
|
|
89
|
+
signal.throwIfAborted();
|
|
49
90
|
const scratch = await mkdtemp(join(tmpdir(), "oyster-hello-"));
|
|
50
91
|
try {
|
|
51
92
|
await packRelease({
|
|
@@ -66,16 +107,20 @@ export async function installHelloApp(input) {
|
|
|
66
107
|
maxOutputBytes: 4_194_304,
|
|
67
108
|
},
|
|
68
109
|
outDirectory: join(scratch, "release"),
|
|
110
|
+
signal,
|
|
69
111
|
});
|
|
70
112
|
const response = await installRelease({
|
|
71
113
|
directory: join(scratch, "release"),
|
|
72
114
|
url: input.url,
|
|
73
115
|
key: input.key,
|
|
74
116
|
installationId: "hello",
|
|
117
|
+
signal,
|
|
75
118
|
});
|
|
76
119
|
return response.status;
|
|
77
120
|
}
|
|
78
121
|
finally {
|
|
79
|
-
await rm(scratch, { recursive: true, force: true })
|
|
122
|
+
await rm(scratch, { recursive: true, force: true }).catch((cause) => {
|
|
123
|
+
throw new CleanupError("App scratch cleanup failed", { cause });
|
|
124
|
+
});
|
|
80
125
|
}
|
|
81
126
|
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export declare const helloPage: string;
|
|
2
|
+
export declare const helloLayout = "\nhtml { --font-sans: \"Nunito\", sans-serif; --font-mono: \"Maple Mono\", monospace; --font-display: var(--font-sans); }\nbody { margin: 0; font-family: var(--font-sans); color: var(--color-base-content); background: var(--color-base-100); }\nmain { max-width: 760px; margin: auto; padding: clamp(20px, 5vw, 56px); }\nh1 { font-size: clamp(28px, 5vw, 44px); line-height: 1.15; margin: 12px 0; overflow-wrap: anywhere; }\nh2 { font-size: 18px; margin: 0 0 6px; }\np { line-height: 1.6; }\nol { list-style: decimal; padding-inline-start: 24px; display: grid; gap: 20px; }\nli p { margin: 0; }\npre { overflow: auto; padding: 12px 0; font-family: var(--font-mono); }\nnav { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 24px; }\nnav a { text-decoration: underline; padding-block: 10px; }\n.eyebrow { font-family: var(--font-mono); font-size: 12px; }\n.intro { font-size: 18px; margin-bottom: 28px; }\n.note { margin-top: 16px; font-size: 14px; }\n";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { createElement as h } from "react";
|
|
2
|
+
import { renderToStaticMarkup } from "react-dom/server";
|
|
3
|
+
import { Card, CardContent } from "@fungi.computer/caps/components/card";
|
|
4
|
+
const docs = "https://docs.fungi.computer";
|
|
5
|
+
const step = (title, ...content) => h("li", null, h("h2", null, title), ...content.map((text) => h("p", null, text)));
|
|
6
|
+
// This is an ordinary installed App. Keep the readiness script when editing it.
|
|
7
|
+
export const helloPage = "<!doctype html>" +
|
|
8
|
+
renderToStaticMarkup(h("html", { lang: "en", "data-theme": "latte" }, h("head", null, h("meta", { charSet: "utf-8" }), h("meta", {
|
|
9
|
+
name: "viewport",
|
|
10
|
+
content: "width=device-width, initial-scale=1",
|
|
11
|
+
}), h("title", null, "Hello · Your first Oyster App"), h("link", { rel: "stylesheet", href: "./lesson.css" })), h("body", null, h("main", null, h("p", { className: "eyebrow" }, "Oyster · Your first App"), h("h1", null, "Hello from my first Oyster App"), h("p", { className: "intro" }, "You have a working App on your own computer. Make one small change and see it here."), h(Card, null, h(CardContent, null, h("ol", null, step("Open the source", "In your Oyster project, open hello-app/bundle/index.html in your editor. This page is yours to change."), step("Make it yours", "Change the heading above to something you wrote. Keep the main.js script at the bottom: it tells Oyster this App is ready."), step("Commit your edit", "Save the file, then commit it in the hello-app Git repository. Oyster builds committed source; unsaved or uncommitted edits will not appear."), step("Restart and open Hello", "Press Ctrl-C in the terminal running your trial. From your Oyster project folder, run the command below. Open the new sign-in link and choose Hello again.")), h("pre", null, h("code", null, "npx @fungi.computer/oyster try .")))), h("nav", { "aria-label": "Next steps" }, h("a", {
|
|
12
|
+
href: docs + "/agents/build-an-app.md",
|
|
13
|
+
target: "_blank",
|
|
14
|
+
rel: "noreferrer",
|
|
15
|
+
}, "Get the exact edit commands"), h("a", {
|
|
16
|
+
href: docs + "/guides/run-your-own-oyster/#deploy-it",
|
|
17
|
+
target: "_blank",
|
|
18
|
+
rel: "noreferrer",
|
|
19
|
+
}, "Read the hosting guide")), h("p", { className: "note" }, "This local trial needs no account or payment. Cloud hosting needs your own account and paid resources; the guide explains those before you deploy.")), h("script", { type: "module", src: "./main.js" }))));
|
|
20
|
+
// Layout and typography only. Caps owns the Card styles.
|
|
21
|
+
export const helloLayout = `
|
|
22
|
+
html { --font-sans: "Nunito", sans-serif; --font-mono: "Maple Mono", monospace; --font-display: var(--font-sans); }
|
|
23
|
+
body { margin: 0; font-family: var(--font-sans); color: var(--color-base-content); background: var(--color-base-100); }
|
|
24
|
+
main { max-width: 760px; margin: auto; padding: clamp(20px, 5vw, 56px); }
|
|
25
|
+
h1 { font-size: clamp(28px, 5vw, 44px); line-height: 1.15; margin: 12px 0; overflow-wrap: anywhere; }
|
|
26
|
+
h2 { font-size: 18px; margin: 0 0 6px; }
|
|
27
|
+
p { line-height: 1.6; }
|
|
28
|
+
ol { list-style: decimal; padding-inline-start: 24px; display: grid; gap: 20px; }
|
|
29
|
+
li p { margin: 0; }
|
|
30
|
+
pre { overflow: auto; padding: 12px 0; font-family: var(--font-mono); }
|
|
31
|
+
nav { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 24px; }
|
|
32
|
+
nav a { text-decoration: underline; padding-block: 10px; }
|
|
33
|
+
.eyebrow { font-family: var(--font-mono); font-size: 12px; }
|
|
34
|
+
.intro { font-size: 18px; margin-bottom: 28px; }
|
|
35
|
+
.note { margin-top: 16px; font-size: 14px; }
|
|
36
|
+
`;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export type DesktopReadiness = "ready" | "approval-required" | "disabled" | "deleted" | "unavailable";
|
|
2
|
+
/** The host-owned entry decides whether the saved desktop may run. Read-only. */
|
|
3
|
+
export declare function localDesktopState(input: Readonly<{
|
|
3
4
|
url: string;
|
|
4
5
|
key: string;
|
|
5
|
-
|
|
6
|
-
}>): Promise<
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
}>): Promise<DesktopReadiness | "missing">;
|
|
7
8
|
/** Build Tourtois from source on this machine and install it as `tourtois`. */
|
|
8
9
|
export declare function installLocalDesktop(input: Readonly<{
|
|
9
10
|
url: string;
|
|
10
11
|
key: string;
|
|
12
|
+
signal?: AbortSignal;
|
|
11
13
|
}>): Promise<void>;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { CleanupError } from "../build/process-group.js";
|
|
1
2
|
// The Tourtois desktop for a local Oyster. The standalone Oyster builds its
|
|
2
3
|
// starter Apps on its own Computer, which a laptop does not have, so `oyster
|
|
3
4
|
// try` builds Tourtois here from its public source with the ordinary local
|
|
4
5
|
// packer and installs it as the home installation, approving its shell the
|
|
5
6
|
// same way `install-apps` does.
|
|
6
|
-
import {
|
|
7
|
+
import { runGit } from "../build/local-build.js";
|
|
7
8
|
import { mkdtemp, realpath, rm } from "node:fs/promises";
|
|
8
9
|
import { tmpdir } from "node:os";
|
|
9
10
|
import { join } from "node:path";
|
|
@@ -11,45 +12,60 @@ import * as Schema from "effect/Schema";
|
|
|
11
12
|
import * as z from "zod";
|
|
12
13
|
import { installRelease, packRelease } from "../pack.js";
|
|
13
14
|
import { appBuildRecipe, readBuildManifest } from "../build/workspace-build.js";
|
|
15
|
+
import { installationSchema } from "../apps/installation.js";
|
|
14
16
|
import { approveChosenDashboard } from "./dashboard-approval.js";
|
|
15
17
|
const tourtoisRepository = "https://github.com/fungi-computer/tourtois.git";
|
|
16
|
-
const installationRowsSchema = z.array(
|
|
17
|
-
const git = (cwd, args) =>
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}).trim();
|
|
22
|
-
/** Whether the Oyster already has an installation with this id. */
|
|
23
|
-
export async function isInstalled(input) {
|
|
24
|
-
const response = await fetch(new URL("/api/installations", input.url), {
|
|
18
|
+
const installationRowsSchema = z.array(installationSchema);
|
|
19
|
+
const git = async (cwd, args, signal) => new TextDecoder().decode(await runGit(args, cwd, signal, process.env)).trim();
|
|
20
|
+
/** The host-owned entry decides whether the saved desktop may run. Read-only. */
|
|
21
|
+
export async function localDesktopState(input) {
|
|
22
|
+
const request = {
|
|
25
23
|
headers: { Authorization: `Bearer ${input.key}` },
|
|
26
|
-
}
|
|
24
|
+
};
|
|
25
|
+
if (input.signal !== undefined)
|
|
26
|
+
request.signal = input.signal;
|
|
27
|
+
const response = await fetch(new URL("/api/installations", input.url), request);
|
|
27
28
|
if (!response.ok)
|
|
28
|
-
|
|
29
|
+
throw new Error(`Desktop installation read failed (HTTP ${String(response.status)}).`);
|
|
29
30
|
const rows = installationRowsSchema.parse(await response.json());
|
|
30
|
-
|
|
31
|
+
const installed = rows.find((row) => row.installationId === "tourtois");
|
|
32
|
+
if (installed === undefined)
|
|
33
|
+
return "missing";
|
|
34
|
+
if (installed.status === "disabled")
|
|
35
|
+
return "disabled";
|
|
36
|
+
if (installed.status === "deleted")
|
|
37
|
+
return "deleted";
|
|
38
|
+
const entry = await fetch(new URL(`/ui-entry/tourtois/${encodeURIComponent(installed.release.releaseId)}/shell.js`, input.url), input.signal === undefined ? undefined : { signal: input.signal });
|
|
39
|
+
if (entry.ok)
|
|
40
|
+
return "ready";
|
|
41
|
+
return entry.status === 403 ? "approval-required" : "unavailable";
|
|
31
42
|
}
|
|
32
43
|
/** Build Tourtois from source on this machine and install it as `tourtois`. */
|
|
33
44
|
export async function installLocalDesktop(input) {
|
|
34
|
-
const
|
|
45
|
+
const signal = input.signal ?? new AbortController().signal;
|
|
46
|
+
signal.throwIfAborted();
|
|
47
|
+
const scratch = await mkdtemp(join(tmpdir(), "oyster-tourtois-"));
|
|
35
48
|
try {
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
const
|
|
49
|
+
const canonical = await realpath(scratch);
|
|
50
|
+
signal.throwIfAborted();
|
|
51
|
+
const source = join(canonical, "tourtois");
|
|
52
|
+
await git(canonical, ["clone", "--quiet", "--depth=1", tourtoisRepository, source], signal);
|
|
53
|
+
const commit = await git(source, ["rev-parse", "HEAD"], signal);
|
|
39
54
|
const release = join(scratch, "release");
|
|
40
55
|
await packRelease({
|
|
41
56
|
appId: "app_tourtois",
|
|
42
57
|
sourceDirectory: source,
|
|
43
58
|
sourceRevision: `git:${commit}`,
|
|
44
|
-
recipe: appBuildRecipe(readBuildManifest(git(source, ["show", `${commit}:package.json`])), ""),
|
|
59
|
+
recipe: appBuildRecipe(readBuildManifest(await git(source, ["show", `${commit}:package.json`], signal)), ""),
|
|
45
60
|
outDirectory: release,
|
|
61
|
+
signal,
|
|
46
62
|
});
|
|
47
63
|
const owner = (method, path, body) => {
|
|
48
64
|
const headers = new Headers({
|
|
49
65
|
Authorization: `Bearer ${input.key}`,
|
|
50
66
|
Accept: "application/json",
|
|
51
67
|
});
|
|
52
|
-
const init = { method, headers };
|
|
68
|
+
const init = { method, headers, signal };
|
|
53
69
|
if (body !== undefined) {
|
|
54
70
|
headers.set("Content-Type", "application/json");
|
|
55
71
|
init.body = JSON.stringify(body);
|
|
@@ -61,6 +77,7 @@ export async function installLocalDesktop(input) {
|
|
|
61
77
|
url: input.url,
|
|
62
78
|
key: input.key,
|
|
63
79
|
installationId: "tourtois",
|
|
80
|
+
signal,
|
|
64
81
|
onCompleted: async (operation) => {
|
|
65
82
|
if (operation.state === "succeeded")
|
|
66
83
|
await approveChosenDashboard(operation, owner);
|
|
@@ -70,6 +87,8 @@ export async function installLocalDesktop(input) {
|
|
|
70
87
|
throw new Error(`Tourtois install failed (${String(response.status)})`);
|
|
71
88
|
}
|
|
72
89
|
finally {
|
|
73
|
-
await rm(scratch, { recursive: true, force: true })
|
|
90
|
+
await rm(scratch, { recursive: true, force: true }).catch((cause) => {
|
|
91
|
+
throw new CleanupError("App scratch cleanup failed", { cause });
|
|
92
|
+
});
|
|
74
93
|
}
|
|
75
94
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export type SignInLink = Readonly<{
|
|
2
2
|
url: string;
|
|
3
|
-
close: () => void
|
|
3
|
+
close: () => Promise<void>;
|
|
4
4
|
}>;
|
|
5
5
|
/** Serve one single-use page that signs the owner in to `oysterUrl`. */
|
|
6
6
|
export declare function startSignInLink(input: Readonly<{
|
|
7
7
|
oysterUrl: string;
|
|
8
8
|
key: string;
|
|
9
|
+
signal?: AbortSignal;
|
|
9
10
|
}>): Promise<SignInLink>;
|
|
@@ -1,49 +1,111 @@
|
|
|
1
1
|
// A one-time sign-in link for `oyster try`. The owner key never goes in a URL:
|
|
2
2
|
// the link opens a page served by this process that posts the key to the local
|
|
3
3
|
// Oyster's own /login form, then the server forgets the link.
|
|
4
|
-
import {
|
|
4
|
+
import { awaitCleanup } from "../build/process-group.js";
|
|
5
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
6
|
+
import { addAbortListener } from "node:events";
|
|
5
7
|
import { createServer } from "node:http";
|
|
6
8
|
import * as z from "zod";
|
|
9
|
+
import { hostPage } from "../host-page.js";
|
|
10
|
+
import { loadPageAssets } from "../page-assets.js";
|
|
7
11
|
const addressSchema = z.object({ port: z.number() });
|
|
8
12
|
const lifetimeMs = 10 * 60_000;
|
|
9
|
-
const escapeAttribute = (text) => text.replace(/[&"<>]/gu, (character) => `&#${String(character.codePointAt(0))};`);
|
|
10
13
|
/** Serve one single-use page that signs the owner in to `oysterUrl`. */
|
|
11
14
|
export async function startSignInLink(input) {
|
|
15
|
+
const signal = input.signal ?? new AbortController().signal;
|
|
16
|
+
signal.throwIfAborted();
|
|
12
17
|
const nonce = Array.from(randomBytes(18), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
13
18
|
let used = false;
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
const sources = await loadPageAssets();
|
|
20
|
+
const stylesheets = Object.entries({
|
|
21
|
+
"styles.css": sources.shellStyles,
|
|
22
|
+
"login-fonts.css": sources.loginFontStyles,
|
|
23
|
+
}).map(([name, source]) => `/_oyster/assets/${createHash("sha256")
|
|
24
|
+
.update(source)
|
|
25
|
+
.digest("hex")}/${name}`);
|
|
26
|
+
const pageOptions = { oysterUrl: input.oysterUrl, stylesheets };
|
|
27
|
+
let page = await (await hostPage({
|
|
28
|
+
...pageOptions,
|
|
29
|
+
state: "signing-in",
|
|
30
|
+
signIn: {
|
|
31
|
+
action: new URL("/login", input.oysterUrl).href,
|
|
32
|
+
key: input.key,
|
|
33
|
+
},
|
|
34
|
+
})).text();
|
|
35
|
+
const expired = await (await hostPage({ ...pageOptions, state: "link-expired", status: 404 })).text();
|
|
36
|
+
const notFound = await (await hostPage({ ...pageOptions, state: "not-found", status: 404 })).text();
|
|
37
|
+
signal.throwIfAborted();
|
|
38
|
+
const timers = new Set();
|
|
18
39
|
const server = createServer((request, response) => {
|
|
19
|
-
|
|
20
|
-
response.writeHead(404, { "Cache-Control": "no-store" }).end("Not found");
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
used = true;
|
|
24
|
-
response
|
|
25
|
-
.writeHead(200, {
|
|
40
|
+
const headers = {
|
|
26
41
|
"Content-Type": "text/html; charset=utf-8",
|
|
27
42
|
"Cache-Control": "no-store",
|
|
28
43
|
"Referrer-Policy": "no-referrer",
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
44
|
+
"X-Robots-Tag": "noindex, nofollow",
|
|
45
|
+
"X-Content-Type-Options": "nosniff",
|
|
46
|
+
};
|
|
47
|
+
if (request.method !== "GET" || request.url !== `/${nonce}`) {
|
|
48
|
+
response.writeHead(404, headers).end(notFound);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (used) {
|
|
52
|
+
response.writeHead(404, headers).end(expired);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
used = true;
|
|
56
|
+
response.writeHead(200, headers).end(page);
|
|
57
|
+
page = "";
|
|
58
|
+
scheduleClose(1_000);
|
|
38
59
|
});
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
60
|
+
let closing;
|
|
61
|
+
let abortListener;
|
|
62
|
+
const beginClose = () => (closing ??= Promise.allSettled([
|
|
63
|
+
(async () => {
|
|
64
|
+
for (const timer of timers)
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
timers.clear();
|
|
67
|
+
page = "";
|
|
68
|
+
abortListener?.[Symbol.dispose]();
|
|
69
|
+
await new Promise((resolve, reject) => {
|
|
70
|
+
server.close((cause) => {
|
|
71
|
+
if (cause &&
|
|
72
|
+
!("code" in cause && cause.code === "ERR_SERVER_NOT_RUNNING"))
|
|
73
|
+
reject(cause);
|
|
74
|
+
else
|
|
75
|
+
resolve();
|
|
76
|
+
});
|
|
77
|
+
server.closeAllConnections();
|
|
78
|
+
});
|
|
79
|
+
})(),
|
|
80
|
+
]));
|
|
81
|
+
const close = () => awaitCleanup(beginClose());
|
|
82
|
+
function scheduleClose(ms) {
|
|
83
|
+
const timer = setTimeout(() => {
|
|
84
|
+
void beginClose();
|
|
85
|
+
}, ms);
|
|
86
|
+
timer.unref();
|
|
87
|
+
timers.add(timer);
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
await new Promise((done, fail) => {
|
|
91
|
+
server.once("error", fail);
|
|
92
|
+
// listen is called synchronously before yielding ownership; cancellation
|
|
93
|
+
// joins the acquired listener even when it wins before the listen event.
|
|
94
|
+
server.listen(0, "127.0.0.1", () => {
|
|
95
|
+
server.off("error", fail);
|
|
96
|
+
done();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
signal.throwIfAborted();
|
|
100
|
+
abortListener = addAbortListener(signal, () => {
|
|
101
|
+
void beginClose();
|
|
102
|
+
});
|
|
103
|
+
scheduleClose(lifetimeMs);
|
|
104
|
+
const { port } = addressSchema.parse(server.address());
|
|
105
|
+
return { url: `http://127.0.0.1:${String(port)}/${nonce}`, close };
|
|
106
|
+
}
|
|
107
|
+
catch (cause) {
|
|
108
|
+
await close();
|
|
109
|
+
throw cause;
|
|
110
|
+
}
|
|
49
111
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Cli } from "@fungi.computer/lantern";
|
|
2
|
+
import { type DesktopReadiness } from "./local-desktop.js";
|
|
2
3
|
import { type SignInLink } from "./sign-in-link.js";
|
|
3
4
|
export declare const tryUsage = "Usage: oyster try [directory] [--port 8787] [--no-open] [--verbose] [--package <npm spec>]";
|
|
4
5
|
/**
|
|
5
|
-
* Streaming logs and
|
|
6
|
-
* --verbose prints plain lines
|
|
6
|
+
* Streaming logs and interactive progress cannot share a terminal, so
|
|
7
|
+
* --verbose prints plain lines.
|
|
7
8
|
*/
|
|
8
9
|
export declare function createTryCli(args: readonly string[]): Cli;
|
|
9
10
|
/** Show a failed `oyster try` as an error with its next step. */
|
|
@@ -11,12 +12,13 @@ export declare function reportTryFailure(cause: unknown, ui: Cli): void;
|
|
|
11
12
|
/** The injectable effects of `oyster try`; tests replace them. */
|
|
12
13
|
export type TryEffects = Readonly<{
|
|
13
14
|
/** Install the scaffolded project's dependencies. */
|
|
14
|
-
installDependencies: (directory: string, manager: Manager, verbose: boolean) => Promise<void>;
|
|
15
|
+
installDependencies: (directory: string, manager: Manager, verbose: boolean, signal: AbortSignal) => Promise<void>;
|
|
15
16
|
/** Start Oyster and resolve once it answers; `stop` ends it. */
|
|
16
17
|
startServer: (input: {
|
|
17
18
|
directory: string;
|
|
18
19
|
port: number;
|
|
19
20
|
key: string;
|
|
21
|
+
signal: AbortSignal;
|
|
20
22
|
verbose: boolean;
|
|
21
23
|
}) => Promise<{
|
|
22
24
|
stop: () => Promise<void>;
|
|
@@ -27,26 +29,27 @@ export type TryEffects = Readonly<{
|
|
|
27
29
|
directory: string;
|
|
28
30
|
url: string;
|
|
29
31
|
key: string;
|
|
32
|
+
signal: AbortSignal;
|
|
30
33
|
}) => Promise<number>;
|
|
31
|
-
/**
|
|
34
|
+
/** Prepare Tourtois without changing an existing installation or its approval. */
|
|
32
35
|
installDesktop: (input: {
|
|
33
36
|
directory: string;
|
|
34
37
|
url: string;
|
|
35
38
|
key: string;
|
|
36
|
-
|
|
39
|
+
signal: AbortSignal;
|
|
40
|
+
}) => Promise<DesktopReadiness>;
|
|
37
41
|
/** A one-time page that signs the owner in without the key in a URL. */
|
|
38
42
|
signInLink: (input: {
|
|
39
43
|
oysterUrl: string;
|
|
40
44
|
key: string;
|
|
45
|
+
signal: AbortSignal;
|
|
41
46
|
}) => Promise<SignInLink>;
|
|
42
47
|
/** Open the browser. */
|
|
43
|
-
open: (url: string) => void;
|
|
44
|
-
/** Resolve when the user asks to stop. */
|
|
45
|
-
interrupted: () => Promise<void>;
|
|
48
|
+
open: (url: string, failed: () => void) => void;
|
|
46
49
|
/** Check that the port is free. */
|
|
47
50
|
portFree: (port: number) => Promise<boolean>;
|
|
48
51
|
}>;
|
|
49
52
|
export type Manager = "npm" | "pnpm";
|
|
50
53
|
/** pnpm when it ran this command (`pnpm dlx`), otherwise npm. */
|
|
51
54
|
export declare function detectManager(userAgent: string | undefined): "pnpm" | "npm";
|
|
52
|
-
export declare function tryOyster(args: readonly string[], effects?: TryEffects, ui?: Cli): Promise<void>;
|
|
55
|
+
export declare function tryOyster(args: readonly string[], effects?: TryEffects, ui?: Cli, callerSignal?: AbortSignal): Promise<void>;
|