@fungi.computer/oyster 0.11.0 → 0.12.0
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 +70 -10
- package/dist/app-builds.d.ts +2 -0
- package/dist/app-builds.js +17 -6
- package/dist/authorization.d.ts +1 -1
- package/dist/build/artifact-writer.d.ts +11 -2
- package/dist/build/artifact-writer.js +42 -24
- package/dist/build/candidate-cli.mjs +26 -18
- package/dist/build/content-objects.d.ts +29 -14
- package/dist/build/content-objects.js +57 -19
- package/dist/build/local-build.js +3 -0
- package/dist/build/submission-contract.d.ts +1 -1
- package/dist/build/submission-contract.js +5 -5
- package/dist/build-egress.d.ts +8 -7
- package/dist/build-egress.js +4 -3
- package/dist/build.d.ts +1 -1
- package/dist/install-release-cli.d.ts +2 -1
- package/dist/install-release-cli.js +18 -10
- package/dist/installation-client.d.ts +1 -0
- package/dist/installation-operations.d.ts +2 -0
- package/dist/installation-operations.js +1 -0
- package/dist/pack.d.ts +5 -2
- package/dist/pack.js +3 -1
- package/dist/standalone/agents.d.ts +6 -2
- package/dist/standalone/agents.js +21 -4
- package/dist/standalone/computer-egress.d.ts +5 -3
- package/dist/standalone/computer-egress.js +1 -1
- package/dist/standalone/hello-app.d.ts +9 -0
- package/dist/standalone/hello-app.js +78 -0
- package/dist/standalone/install-apps-cli.d.ts +2 -1
- package/dist/standalone/install-apps-cli.js +36 -14
- package/dist/standalone/instance.js +2 -2
- package/dist/standalone/local-desktop.d.ts +11 -0
- package/dist/standalone/local-desktop.js +75 -0
- package/dist/standalone/registry.d.ts +2 -0
- package/dist/standalone/sign-in-link.d.ts +9 -0
- package/dist/standalone/sign-in-link.js +49 -0
- package/dist/standalone/try-cli.d.ts +52 -0
- package/dist/standalone/try-cli.js +404 -0
- package/dist/standalone/worker.js +1 -1
- package/dist/webhook-abuse.d.ts +7 -1
- package/dist/webhook-abuse.js +8 -2
- package/dist/workers.d.ts +7 -2
- package/dist/workers.js +1 -1
- package/docs/api/authorization.md +56 -11
- package/docs/api/build-contract.md +52 -10
- package/docs/api/build.md +16 -2
- package/docs/api/host.md +1 -0
- package/docs/api/pack.md +179 -3
- package/docs/site/agent-quickstart.mdx +8 -0
- package/package.json +9 -8
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// The Tourtois desktop for a local Oyster. The standalone Oyster builds its
|
|
2
|
+
// starter Apps on its own Computer, which a laptop does not have, so `oyster
|
|
3
|
+
// try` builds Tourtois here from its public source with the ordinary local
|
|
4
|
+
// packer and installs it as the home installation, approving its shell the
|
|
5
|
+
// same way `install-apps` does.
|
|
6
|
+
import { execFileSync } from "node:child_process";
|
|
7
|
+
import { mkdtemp, rm } from "node:fs/promises";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
import * as Schema from "effect/Schema";
|
|
11
|
+
import * as z from "zod";
|
|
12
|
+
import { installRelease, packRelease } from "../pack.js";
|
|
13
|
+
import { appBuildRecipe, readBuildManifest } from "../build/workspace-build.js";
|
|
14
|
+
import { approveChosenDashboard } from "./dashboard-approval.js";
|
|
15
|
+
const tourtoisRepository = "https://github.com/fungi-computer/tourtois.git";
|
|
16
|
+
const installationRowsSchema = z.array(z.object({ installationId: z.string() }).loose());
|
|
17
|
+
const git = (cwd, args) => execFileSync("git", [...args], {
|
|
18
|
+
cwd,
|
|
19
|
+
encoding: "utf8",
|
|
20
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
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), {
|
|
25
|
+
headers: { Authorization: `Bearer ${input.key}` },
|
|
26
|
+
});
|
|
27
|
+
if (!response.ok)
|
|
28
|
+
return false;
|
|
29
|
+
const rows = installationRowsSchema.parse(await response.json());
|
|
30
|
+
return rows.some((row) => row.installationId === input.installationId);
|
|
31
|
+
}
|
|
32
|
+
/** Build Tourtois from source on this machine and install it as `tourtois`. */
|
|
33
|
+
export async function installLocalDesktop(input) {
|
|
34
|
+
const scratch = await mkdtemp(join(tmpdir(), "oyster-tourtois-"));
|
|
35
|
+
try {
|
|
36
|
+
const source = join(scratch, "tourtois");
|
|
37
|
+
git(scratch, ["clone", "--quiet", "--depth=1", tourtoisRepository, source]);
|
|
38
|
+
const commit = git(source, ["rev-parse", "HEAD"]);
|
|
39
|
+
const release = join(scratch, "release");
|
|
40
|
+
await packRelease({
|
|
41
|
+
appId: "app_tourtois",
|
|
42
|
+
sourceDirectory: source,
|
|
43
|
+
sourceRevision: `git:${commit}`,
|
|
44
|
+
recipe: appBuildRecipe(readBuildManifest(git(source, ["show", `${commit}:package.json`])), ""),
|
|
45
|
+
outDirectory: release,
|
|
46
|
+
});
|
|
47
|
+
const owner = (method, path, body) => {
|
|
48
|
+
const headers = new Headers({
|
|
49
|
+
Authorization: `Bearer ${input.key}`,
|
|
50
|
+
Accept: "application/json",
|
|
51
|
+
});
|
|
52
|
+
const init = { method, headers };
|
|
53
|
+
if (body !== undefined) {
|
|
54
|
+
headers.set("Content-Type", "application/json");
|
|
55
|
+
init.body = JSON.stringify(body);
|
|
56
|
+
}
|
|
57
|
+
return fetch(new URL(path, input.url), init);
|
|
58
|
+
};
|
|
59
|
+
const response = await installRelease({
|
|
60
|
+
directory: release,
|
|
61
|
+
url: input.url,
|
|
62
|
+
key: input.key,
|
|
63
|
+
installationId: "tourtois",
|
|
64
|
+
onCompleted: async (operation) => {
|
|
65
|
+
if (operation.state === "succeeded")
|
|
66
|
+
await approveChosenDashboard(operation, owner);
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
if (!response.ok)
|
|
70
|
+
throw new Error(`Tourtois install failed (${String(response.status)})`);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
await rm(scratch, { recursive: true, force: true });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -8,6 +8,7 @@ import { type CloudflareArtifactsOptions } from "@fungi.computer/burrow/cloudfla
|
|
|
8
8
|
import { type OysterBuildStartResult, type OysterBuildView } from "../app-builds.js";
|
|
9
9
|
import type { TeamAppCandidateManifest } from "../build-contract.js";
|
|
10
10
|
import type { BuildGitEvent } from "../build-egress.js";
|
|
11
|
+
import type { ContentObjectOwner } from "../build/content-objects.js";
|
|
11
12
|
import { type StandaloneTenancyEnv } from "./tenancy.js";
|
|
12
13
|
import type { ComputerDO } from "./computer.js";
|
|
13
14
|
import type { ComputerEnv } from "@fungi.computer/computer";
|
|
@@ -280,6 +281,7 @@ export declare abstract class StandaloneRegistry extends OysterRegistry implemen
|
|
|
280
281
|
/** The egress: may this Computer's live job upload one object, how large? */
|
|
281
282
|
admitBuildObject(jobId: string, computerObjectId: string, grant: string): Promise<Readonly<{
|
|
282
283
|
maxObjectBytes: number;
|
|
284
|
+
owner: ContentObjectOwner;
|
|
283
285
|
}> | null>;
|
|
284
286
|
/** The egress: a live job's candidate manifest; its objects are stored. */
|
|
285
287
|
submitBuildCandidate(jobId: string, manifest: TeamAppCandidateManifest, grant: string, credentialId: string): Promise<Response>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type SignInLink = Readonly<{
|
|
2
|
+
url: string;
|
|
3
|
+
close: () => void;
|
|
4
|
+
}>;
|
|
5
|
+
/** Serve one single-use page that signs the owner in to `oysterUrl`. */
|
|
6
|
+
export declare function startSignInLink(input: Readonly<{
|
|
7
|
+
oysterUrl: string;
|
|
8
|
+
key: string;
|
|
9
|
+
}>): Promise<SignInLink>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// A one-time sign-in link for `oyster try`. The owner key never goes in a URL:
|
|
2
|
+
// the link opens a page served by this process that posts the key to the local
|
|
3
|
+
// Oyster's own /login form, then the server forgets the link.
|
|
4
|
+
import { randomBytes } from "node:crypto";
|
|
5
|
+
import { createServer } from "node:http";
|
|
6
|
+
import * as z from "zod";
|
|
7
|
+
const addressSchema = z.object({ port: z.number() });
|
|
8
|
+
const lifetimeMs = 10 * 60_000;
|
|
9
|
+
const escapeAttribute = (text) => text.replace(/[&"<>]/gu, (character) => `&#${String(character.codePointAt(0))};`);
|
|
10
|
+
/** Serve one single-use page that signs the owner in to `oysterUrl`. */
|
|
11
|
+
export async function startSignInLink(input) {
|
|
12
|
+
const nonce = Array.from(randomBytes(18), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
13
|
+
let used = false;
|
|
14
|
+
const page = `<!doctype html><meta charset="utf-8"><meta name="referrer" content="no-referrer"><title>Signing in to Oyster</title>` +
|
|
15
|
+
`<form method="post" action="${escapeAttribute(new URL("/login", input.oysterUrl).href)}">` +
|
|
16
|
+
`<input type="hidden" name="key" value="${escapeAttribute(input.key)}"></form>` +
|
|
17
|
+
`<p>Signing in to Oyster...</p><script>document.forms[0].submit()</script>`;
|
|
18
|
+
const server = createServer((request, response) => {
|
|
19
|
+
if (used || request.method !== "GET" || request.url !== `/${nonce}`) {
|
|
20
|
+
response.writeHead(404, { "Cache-Control": "no-store" }).end("Not found");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
used = true;
|
|
24
|
+
response
|
|
25
|
+
.writeHead(200, {
|
|
26
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
27
|
+
"Cache-Control": "no-store",
|
|
28
|
+
"Referrer-Policy": "no-referrer",
|
|
29
|
+
})
|
|
30
|
+
.end(page);
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
server.close();
|
|
33
|
+
}, 1_000).unref();
|
|
34
|
+
});
|
|
35
|
+
await new Promise((done, fail) => {
|
|
36
|
+
server.once("error", fail);
|
|
37
|
+
server.listen(0, "127.0.0.1", done);
|
|
38
|
+
});
|
|
39
|
+
setTimeout(() => {
|
|
40
|
+
server.close();
|
|
41
|
+
}, lifetimeMs).unref();
|
|
42
|
+
const { port } = addressSchema.parse(server.address());
|
|
43
|
+
return {
|
|
44
|
+
url: `http://127.0.0.1:${String(port)}/${nonce}`,
|
|
45
|
+
close: () => {
|
|
46
|
+
server.close();
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Cli } from "@fungi.computer/lantern";
|
|
2
|
+
import { type SignInLink } from "./sign-in-link.js";
|
|
3
|
+
export declare const tryUsage = "Usage: oyster try [directory] [--port 8787] [--no-open] [--verbose] [--package <npm spec>]";
|
|
4
|
+
/**
|
|
5
|
+
* Streaming logs and a redrawing spinner cannot share a terminal, so
|
|
6
|
+
* --verbose prints plain lines instead of spinners.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createTryCli(args: readonly string[]): Cli;
|
|
9
|
+
/** Show a failed `oyster try` as an error with its next step. */
|
|
10
|
+
export declare function reportTryFailure(cause: unknown, ui: Cli): void;
|
|
11
|
+
/** The injectable effects of `oyster try`; tests replace them. */
|
|
12
|
+
export type TryEffects = Readonly<{
|
|
13
|
+
/** Install the scaffolded project's dependencies. */
|
|
14
|
+
installDependencies: (directory: string, manager: Manager, verbose: boolean) => Promise<void>;
|
|
15
|
+
/** Start Oyster and resolve once it answers; `stop` ends it. */
|
|
16
|
+
startServer: (input: {
|
|
17
|
+
directory: string;
|
|
18
|
+
port: number;
|
|
19
|
+
key: string;
|
|
20
|
+
verbose: boolean;
|
|
21
|
+
}) => Promise<{
|
|
22
|
+
stop: () => Promise<void>;
|
|
23
|
+
exited: Promise<number | null>;
|
|
24
|
+
}>;
|
|
25
|
+
/** Install the Hello App on the running Oyster. */
|
|
26
|
+
installHello: (input: {
|
|
27
|
+
directory: string;
|
|
28
|
+
url: string;
|
|
29
|
+
key: string;
|
|
30
|
+
}) => Promise<number>;
|
|
31
|
+
/** Build and install the Tourtois desktop unless it is already there. */
|
|
32
|
+
installDesktop: (input: {
|
|
33
|
+
directory: string;
|
|
34
|
+
url: string;
|
|
35
|
+
key: string;
|
|
36
|
+
}) => Promise<void>;
|
|
37
|
+
/** A one-time page that signs the owner in without the key in a URL. */
|
|
38
|
+
signInLink: (input: {
|
|
39
|
+
oysterUrl: string;
|
|
40
|
+
key: string;
|
|
41
|
+
}) => Promise<SignInLink>;
|
|
42
|
+
/** Open the browser. */
|
|
43
|
+
open: (url: string) => void;
|
|
44
|
+
/** Resolve when the user asks to stop. */
|
|
45
|
+
interrupted: () => Promise<void>;
|
|
46
|
+
/** Check that the port is free. */
|
|
47
|
+
portFree: (port: number) => Promise<boolean>;
|
|
48
|
+
}>;
|
|
49
|
+
export type Manager = "npm" | "pnpm";
|
|
50
|
+
/** pnpm when it ran this command (`pnpm dlx`), otherwise npm. */
|
|
51
|
+
export declare function detectManager(userAgent: string | undefined): "pnpm" | "npm";
|
|
52
|
+
export declare function tryOyster(args: readonly string[], effects?: TryEffects, ui?: Cli): Promise<void>;
|
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
// `oyster try`: one command from nothing to a running local Oyster with a
|
|
2
|
+
// Hello App. It scaffolds a small project (the standalone worker and wrangler
|
|
3
|
+
// config this package ships, plus a package.json), installs it with the
|
|
4
|
+
// caller's package manager, runs `wrangler dev` locally, installs the Hello
|
|
5
|
+
// App and opens the browser. Running it again in the same directory restarts
|
|
6
|
+
// the server without scaffolding again.
|
|
7
|
+
//
|
|
8
|
+
// npx @fungi.computer/oyster try [directory] [--port 8787] [--no-open] [--verbose]
|
|
9
|
+
//
|
|
10
|
+
// Output goes through Lantern, the shared Fungi terminal kit: a wordmark, one
|
|
11
|
+
// spinner per real step, a success box and a next step for every failure. It
|
|
12
|
+
// is quiet by default; install and server output go to files under .oyster/
|
|
13
|
+
// and --verbose streams them instead. Piped output or CI gets plain lines.
|
|
14
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
15
|
+
import { randomBytes } from "node:crypto";
|
|
16
|
+
import { createWriteStream } from "node:fs";
|
|
17
|
+
import { copyFile, mkdir, readdir, readFile, stat, writeFile, } from "node:fs/promises";
|
|
18
|
+
import { createServer } from "node:net";
|
|
19
|
+
import { basename, delimiter, join, resolve } from "node:path";
|
|
20
|
+
import process from "node:process";
|
|
21
|
+
import { parseArgs } from "node:util";
|
|
22
|
+
import { Cli } from "@fungi.computer/lantern";
|
|
23
|
+
import * as z from "zod";
|
|
24
|
+
import { installHelloApp } from "./hello-app.js";
|
|
25
|
+
import { installLocalDesktop, isInstalled } from "./local-desktop.js";
|
|
26
|
+
import { startSignInLink } from "./sign-in-link.js";
|
|
27
|
+
export const tryUsage = "Usage: oyster try [directory] [--port 8787] [--no-open] [--verbose] [--package <npm spec>]";
|
|
28
|
+
const tryHelp = `${tryUsage}
|
|
29
|
+
|
|
30
|
+
Creates ./my-oyster (or <directory>) with a local Oyster, installs it, starts it
|
|
31
|
+
on http://127.0.0.1:<port> and installs a Hello App from <directory>/hello-app,
|
|
32
|
+
a git repository you can edit. Commit an edit and run this again, from inside
|
|
33
|
+
that directory or by naming it, to restart and reinstall without scaffolding.
|
|
34
|
+
Press Ctrl-C to stop.
|
|
35
|
+
|
|
36
|
+
Options:
|
|
37
|
+
--port <number> port to listen on (default 8787)
|
|
38
|
+
--no-open do not open the browser
|
|
39
|
+
--verbose stream install and server output instead of saving it
|
|
40
|
+
under .oyster/
|
|
41
|
+
--package <spec> install Oyster from this npm spec instead of the version
|
|
42
|
+
that is running (a tarball path, for example)
|
|
43
|
+
-h, --help show this help
|
|
44
|
+
`;
|
|
45
|
+
// The local workspace builder (src/build/local-build.ts) refuses any other pnpm.
|
|
46
|
+
const localBuildPnpmVersion = "11.24.0";
|
|
47
|
+
const requiredNodeMajor = 24;
|
|
48
|
+
const ownerKeyFile = ".oyster-owner";
|
|
49
|
+
const logFile = join(".oyster", "wrangler.log");
|
|
50
|
+
const installLogFile = join(".oyster", "install.log");
|
|
51
|
+
const command = "npx @fungi.computer/oyster try";
|
|
52
|
+
/** A failure that knows the exact next step; the command shows both. */
|
|
53
|
+
class TryError extends Error {
|
|
54
|
+
title;
|
|
55
|
+
next;
|
|
56
|
+
constructor(title, next) {
|
|
57
|
+
super(`${title} ${next}`);
|
|
58
|
+
this.name = "TryError";
|
|
59
|
+
this.title = title;
|
|
60
|
+
this.next = next;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Streaming logs and a redrawing spinner cannot share a terminal, so
|
|
65
|
+
* --verbose prints plain lines instead of spinners.
|
|
66
|
+
*/
|
|
67
|
+
export function createTryCli(args) {
|
|
68
|
+
return args.includes("--verbose")
|
|
69
|
+
? new Cli({ brand: "fungi", interactive: false })
|
|
70
|
+
: new Cli({ brand: "fungi" });
|
|
71
|
+
}
|
|
72
|
+
/** Show a failed `oyster try` as an error with its next step. */
|
|
73
|
+
export function reportTryFailure(cause, ui) {
|
|
74
|
+
if (cause instanceof TryError)
|
|
75
|
+
ui.problem(cause.title, cause.next);
|
|
76
|
+
else
|
|
77
|
+
ui.problem(cause instanceof Error ? cause.message : String(cause), `Run \`${command} --help\` for the options, or add --verbose to see the full output.`);
|
|
78
|
+
}
|
|
79
|
+
const ownManifestSchema = z.object({
|
|
80
|
+
version: z.string(),
|
|
81
|
+
dependencies: z.record(z.string(), z.string()).default({}),
|
|
82
|
+
peerDependencies: z.record(z.string(), z.string()).default({}),
|
|
83
|
+
});
|
|
84
|
+
/** pnpm when it ran this command (`pnpm dlx`), otherwise npm. */
|
|
85
|
+
export function detectManager(userAgent) {
|
|
86
|
+
return userAgent?.startsWith("pnpm/") === true ? "pnpm" : "npm";
|
|
87
|
+
}
|
|
88
|
+
async function ownManifest() {
|
|
89
|
+
return ownManifestSchema.parse(JSON.parse(await readFile(join(import.meta.dirname, "../../package.json"), "utf8")));
|
|
90
|
+
}
|
|
91
|
+
function projectManifest(own, spec) {
|
|
92
|
+
const dependencies = {
|
|
93
|
+
"@fungi.computer/oyster": spec ?? own.version,
|
|
94
|
+
"@fungi.computer/computer": own.dependencies["@fungi.computer/computer"] ?? "latest",
|
|
95
|
+
react: "^19.2.0",
|
|
96
|
+
"react-dom": "^19.2.0",
|
|
97
|
+
// Building the desktop here needs this exact pnpm, whatever the caller uses.
|
|
98
|
+
pnpm: localBuildPnpmVersion,
|
|
99
|
+
wrangler: own.peerDependencies.wrangler ?? "^4.127.1",
|
|
100
|
+
};
|
|
101
|
+
return (JSON.stringify({
|
|
102
|
+
name: "my-oyster",
|
|
103
|
+
private: true,
|
|
104
|
+
type: "module",
|
|
105
|
+
// npm 11.19 and newer skip dependency install scripts unless approved.
|
|
106
|
+
allowScripts: {
|
|
107
|
+
"@google/genai": false,
|
|
108
|
+
"@mongodb-js/zstd": false,
|
|
109
|
+
esbuild: true,
|
|
110
|
+
exifreader: false,
|
|
111
|
+
"node-liblzma": false,
|
|
112
|
+
protobufjs: false,
|
|
113
|
+
workerd: true,
|
|
114
|
+
},
|
|
115
|
+
scripts: { start: "npx @fungi.computer/oyster try" },
|
|
116
|
+
dependencies,
|
|
117
|
+
}, null, 2) + "\n");
|
|
118
|
+
}
|
|
119
|
+
// pnpm 11 refuses dependency build scripts it was not told about.
|
|
120
|
+
const pnpmWorkspace = `allowBuilds:
|
|
121
|
+
"@google/genai": false
|
|
122
|
+
"@mongodb-js/zstd": false
|
|
123
|
+
esbuild: true
|
|
124
|
+
exifreader: false
|
|
125
|
+
node-liblzma: false
|
|
126
|
+
protobufjs: false
|
|
127
|
+
workerd: true
|
|
128
|
+
`;
|
|
129
|
+
async function exists(path) {
|
|
130
|
+
return stat(path).then(() => true, () => false);
|
|
131
|
+
}
|
|
132
|
+
async function scaffold(directory, own, spec, manager) {
|
|
133
|
+
await mkdir(directory, { recursive: true });
|
|
134
|
+
const examples = join(import.meta.dirname, "../../examples/standalone");
|
|
135
|
+
for (const name of ["worker.ts", "wrangler.jsonc"])
|
|
136
|
+
await copyFile(join(examples, name), join(directory, name));
|
|
137
|
+
await writeFile(join(directory, "package.json"), projectManifest(own, spec));
|
|
138
|
+
if (manager === "pnpm")
|
|
139
|
+
await writeFile(join(directory, "pnpm-workspace.yaml"), pnpmWorkspace);
|
|
140
|
+
await writeFile(join(directory, ".gitignore"), ".oyster/\n.wrangler/\nnode_modules/\n.oyster-owner\n");
|
|
141
|
+
}
|
|
142
|
+
/** Write a child's output to a log file, and also to stderr when verbose. */
|
|
143
|
+
function captureOutput(child, log, verbose) {
|
|
144
|
+
for (const stream of [child.stdout, child.stderr]) {
|
|
145
|
+
stream?.pipe(log, { end: false });
|
|
146
|
+
if (verbose)
|
|
147
|
+
stream?.pipe(process.stderr, { end: false });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async function runInstall(directory, manager, verbose) {
|
|
151
|
+
await mkdir(join(directory, ".oyster"), { recursive: true });
|
|
152
|
+
const log = createWriteStream(join(directory, installLogFile));
|
|
153
|
+
try {
|
|
154
|
+
await new Promise((done, fail) => {
|
|
155
|
+
const child = spawn(manager, ["install"], {
|
|
156
|
+
cwd: directory,
|
|
157
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
158
|
+
shell: process.platform === "win32",
|
|
159
|
+
});
|
|
160
|
+
captureOutput(child, log, verbose);
|
|
161
|
+
child.on("error", (cause) => {
|
|
162
|
+
fail(new Error(`Could not run ${manager} install: ${cause.message}`));
|
|
163
|
+
});
|
|
164
|
+
child.on("close", (code) => {
|
|
165
|
+
if (code === 0)
|
|
166
|
+
done();
|
|
167
|
+
else
|
|
168
|
+
fail(new Error(`${manager} install failed (exit ${String(code)}).`));
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
log.end();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function portFree(port) {
|
|
177
|
+
return new Promise((done) => {
|
|
178
|
+
const server = createServer();
|
|
179
|
+
server.once("error", () => {
|
|
180
|
+
done(false);
|
|
181
|
+
});
|
|
182
|
+
server.listen(port, "127.0.0.1", () => {
|
|
183
|
+
server.close(() => {
|
|
184
|
+
done(true);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async function waitUntilReady(url, child, timeoutMs) {
|
|
190
|
+
const deadline = Date.now() + timeoutMs;
|
|
191
|
+
while (Date.now() < deadline) {
|
|
192
|
+
if (child.exitCode !== null)
|
|
193
|
+
return false;
|
|
194
|
+
const answered = await fetch(`${url}/login`, {
|
|
195
|
+
signal: AbortSignal.timeout(2_000),
|
|
196
|
+
}).then((response) => response.status < 500, () => false);
|
|
197
|
+
if (answered)
|
|
198
|
+
return true;
|
|
199
|
+
await new Promise((wake) => setTimeout(wake, 500));
|
|
200
|
+
}
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
async function tail(path) {
|
|
204
|
+
try {
|
|
205
|
+
const text = await readFile(path, "utf8");
|
|
206
|
+
return text.trim().split("\n").slice(-15).join("\n");
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
return "(no log was written)";
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function startWrangler(input) {
|
|
213
|
+
return (async () => {
|
|
214
|
+
const wrangler = join(input.directory, "node_modules", "wrangler", "bin", "wrangler.js");
|
|
215
|
+
if (!(await exists(wrangler)))
|
|
216
|
+
throw new TryError(`wrangler is not installed in ${input.directory}.`, `Run \`npm install\` there, then run \`${command}\` again.`);
|
|
217
|
+
await mkdir(join(input.directory, ".oyster"), { recursive: true });
|
|
218
|
+
const log = join(input.directory, logFile);
|
|
219
|
+
const logStream = createWriteStream(log);
|
|
220
|
+
const url = `http://127.0.0.1:${String(input.port)}`;
|
|
221
|
+
const child = spawn(process.execPath, [
|
|
222
|
+
wrangler,
|
|
223
|
+
"dev",
|
|
224
|
+
"--local",
|
|
225
|
+
"--enable-containers=false",
|
|
226
|
+
"--ip",
|
|
227
|
+
"127.0.0.1",
|
|
228
|
+
"--port",
|
|
229
|
+
String(input.port),
|
|
230
|
+
"--local-upstream",
|
|
231
|
+
"oyster.test",
|
|
232
|
+
"--upstream-protocol",
|
|
233
|
+
"https",
|
|
234
|
+
"--var",
|
|
235
|
+
`OYSTER_OWNER_KEY:${input.key}`,
|
|
236
|
+
"--var",
|
|
237
|
+
"OYSTER_ORIGIN:https://oyster.test",
|
|
238
|
+
], {
|
|
239
|
+
cwd: input.directory,
|
|
240
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
241
|
+
env: { ...process.env, WRANGLER_SEND_METRICS: "false" },
|
|
242
|
+
});
|
|
243
|
+
captureOutput(child, logStream, input.verbose);
|
|
244
|
+
const exited = new Promise((done) => {
|
|
245
|
+
child.on("close", (code) => {
|
|
246
|
+
logStream.end();
|
|
247
|
+
done(code);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
const stop = async () => {
|
|
251
|
+
if (child.exitCode === null)
|
|
252
|
+
child.kill("SIGTERM");
|
|
253
|
+
const forced = setTimeout(() => {
|
|
254
|
+
child.kill("SIGKILL");
|
|
255
|
+
}, 10_000);
|
|
256
|
+
await exited;
|
|
257
|
+
clearTimeout(forced);
|
|
258
|
+
};
|
|
259
|
+
if (!(await waitUntilReady(url, child, 120_000))) {
|
|
260
|
+
await stop();
|
|
261
|
+
throw new TryError("Oyster did not start.", `Read ${log}, or run \`${command} --verbose\` to watch it start. The last lines:\n${await tail(log)}`);
|
|
262
|
+
}
|
|
263
|
+
return { stop, exited };
|
|
264
|
+
})();
|
|
265
|
+
}
|
|
266
|
+
function openBrowser(url) {
|
|
267
|
+
const [command, args] = process.platform === "darwin"
|
|
268
|
+
? ["open", [url]]
|
|
269
|
+
: process.platform === "win32"
|
|
270
|
+
? ["cmd", ["/c", "start", "", url]]
|
|
271
|
+
: ["xdg-open", [url]];
|
|
272
|
+
const child = spawn(command, args, { stdio: "ignore", detached: true });
|
|
273
|
+
child.on("error", () => undefined);
|
|
274
|
+
child.unref();
|
|
275
|
+
}
|
|
276
|
+
function untilInterrupted() {
|
|
277
|
+
return new Promise((done) => {
|
|
278
|
+
const once = () => {
|
|
279
|
+
process.off("SIGINT", once);
|
|
280
|
+
process.off("SIGTERM", once);
|
|
281
|
+
done();
|
|
282
|
+
};
|
|
283
|
+
process.on("SIGINT", once);
|
|
284
|
+
process.on("SIGTERM", once);
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
const realEffects = {
|
|
288
|
+
installDependencies: runInstall,
|
|
289
|
+
startServer: startWrangler,
|
|
290
|
+
installHello: installHelloApp,
|
|
291
|
+
installDesktop: async ({ directory, ...input }) => {
|
|
292
|
+
process.env.PATH = `${join(directory, "node_modules", ".bin")}${delimiter}${process.env.PATH ?? ""}`;
|
|
293
|
+
if (!(await isInstalled({ ...input, installationId: "tourtois" })))
|
|
294
|
+
await installLocalDesktop(input);
|
|
295
|
+
},
|
|
296
|
+
signInLink: startSignInLink,
|
|
297
|
+
open: openBrowser,
|
|
298
|
+
interrupted: untilInterrupted,
|
|
299
|
+
portFree,
|
|
300
|
+
};
|
|
301
|
+
export async function tryOyster(args, effects = realEffects, ui = createTryCli(args)) {
|
|
302
|
+
const { values, positionals } = parseArgs({
|
|
303
|
+
args: [...args],
|
|
304
|
+
allowPositionals: true,
|
|
305
|
+
strict: true,
|
|
306
|
+
options: {
|
|
307
|
+
port: { type: "string" },
|
|
308
|
+
"no-open": { type: "boolean" },
|
|
309
|
+
verbose: { type: "boolean" },
|
|
310
|
+
package: { type: "string" },
|
|
311
|
+
help: { type: "boolean", short: "h" },
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
const verbose = values.verbose === true;
|
|
315
|
+
if (values.help === true) {
|
|
316
|
+
process.stdout.write(tryHelp);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (positionals.length > 1)
|
|
320
|
+
throw new Error(tryUsage);
|
|
321
|
+
ui.welcome("Oyster: your own Apps, running on this machine");
|
|
322
|
+
const major = Number(process.versions.node.split(".")[0]);
|
|
323
|
+
if (major < requiredNodeMajor)
|
|
324
|
+
throw new TryError(`Oyster needs Node ${String(requiredNodeMajor)} or newer; this is Node ${process.versions.node}.`, `Install a current Node from https://nodejs.org, then run \`${command}\` again.`);
|
|
325
|
+
if (!gitAvailable())
|
|
326
|
+
throw new TryError("Oyster builds the Hello App from a git commit, so it needs Git.", `Install Git from https://git-scm.com, then run \`${command}\` again.`);
|
|
327
|
+
const port = Number(values.port ?? "8787");
|
|
328
|
+
if (!Number.isInteger(port) || port < 1 || port > 65_535)
|
|
329
|
+
throw new TryError("--port must be a number from 1 to 65535.", `For example: \`${command} --port 8788\`.`);
|
|
330
|
+
const directory = resolve(positionals[0] ??
|
|
331
|
+
((await exists(join(process.cwd(), ownerKeyFile))) ? "." : "my-oyster"));
|
|
332
|
+
const keyPath = join(directory, ownerKeyFile);
|
|
333
|
+
const fresh = !(await exists(keyPath));
|
|
334
|
+
if (fresh) {
|
|
335
|
+
const occupied = (await exists(directory))
|
|
336
|
+
? (await readdir(directory)).length > 0
|
|
337
|
+
: false;
|
|
338
|
+
if (occupied)
|
|
339
|
+
throw new TryError(`${directory} already has files and is not an Oyster made by \`oyster try\`.`, `Choose a new directory: \`${command} <directory>\`.`);
|
|
340
|
+
}
|
|
341
|
+
if (!(await effects.portFree(port)))
|
|
342
|
+
throw new TryError(`Port ${String(port)} on 127.0.0.1 is already in use.`, `Stop whatever is using it, or choose another port: \`${command} --port ${String(port + 1)}\`.`);
|
|
343
|
+
let key;
|
|
344
|
+
if (fresh) {
|
|
345
|
+
const manager = detectManager(process.env.npm_config_user_agent);
|
|
346
|
+
const own = await ownManifest();
|
|
347
|
+
await ui.task(`Creating ${basename(directory)}`, () => scaffold(directory, own, values.package, manager));
|
|
348
|
+
await ui
|
|
349
|
+
.task(`Installing dependencies with ${manager} (a minute or two)`, () => effects.installDependencies(directory, manager, verbose))
|
|
350
|
+
.catch((cause) => {
|
|
351
|
+
throw new TryError(cause instanceof Error ? cause.message : String(cause), `Read ${join(directory, installLogFile)} (or add --verbose), delete ${directory}, and run \`${command}\` again.`);
|
|
352
|
+
});
|
|
353
|
+
key = Array.from(randomBytes(24), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
354
|
+
await writeFile(keyPath, key + "\n", { mode: 0o600 });
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
key = (await readFile(keyPath, "utf8")).trim();
|
|
358
|
+
ui.info(`Restarting the Oyster in ${directory}`);
|
|
359
|
+
}
|
|
360
|
+
const url = `http://127.0.0.1:${String(port)}`;
|
|
361
|
+
const server = await ui.task("Starting Oyster", () => effects.startServer({ directory, port, key, verbose }));
|
|
362
|
+
let closeLink = () => undefined;
|
|
363
|
+
try {
|
|
364
|
+
await ui.task("Installing the Hello App", () => effects.installHello({ directory, url, key }));
|
|
365
|
+
await ui
|
|
366
|
+
.task(fresh
|
|
367
|
+
? "Building Tourtois (about 2 min the first time)"
|
|
368
|
+
: "Checking Tourtois", () => effects.installDesktop({ directory, url, key }))
|
|
369
|
+
.catch((cause) => {
|
|
370
|
+
ui.warn(`Tourtois, the desktop, could not be built (${cause instanceof Error ? cause.message : String(cause)}). Continuing with the Hello App.`, `Connect to the internet and run \`${command}\` again from ${directory} to try the desktop again.`);
|
|
371
|
+
});
|
|
372
|
+
const link = await ui.task("Opening your desktop", async () => {
|
|
373
|
+
const made = await effects.signInLink({ oysterUrl: url, key });
|
|
374
|
+
closeLink = made.close;
|
|
375
|
+
if (values["no-open"] !== true && process.stdout.isTTY)
|
|
376
|
+
effects.open(made.url);
|
|
377
|
+
return made;
|
|
378
|
+
});
|
|
379
|
+
ui.note("Oyster is running", [
|
|
380
|
+
`Open (works once): ${link.url}`,
|
|
381
|
+
`Oyster: ${url}`,
|
|
382
|
+
`Owner key saved in: ${keyPath}`,
|
|
383
|
+
`Hello App source: ${join(directory, "hello-app")}`,
|
|
384
|
+
`Sign in later by pasting that key at ${url}/login.`,
|
|
385
|
+
"Press Ctrl-C to stop.",
|
|
386
|
+
]);
|
|
387
|
+
const outcome = await Promise.race([
|
|
388
|
+
effects.interrupted().then(() => "interrupted"),
|
|
389
|
+
server.exited.then(() => "exited"),
|
|
390
|
+
]);
|
|
391
|
+
if (outcome === "exited")
|
|
392
|
+
throw new TryError("Oyster stopped unexpectedly.", `Read ${join(directory, logFile)}, then run \`${command}\` again.`);
|
|
393
|
+
}
|
|
394
|
+
finally {
|
|
395
|
+
closeLink();
|
|
396
|
+
ui.step("Stopping Oyster");
|
|
397
|
+
await server.stop();
|
|
398
|
+
}
|
|
399
|
+
ui.outro("Stopped. Run the same command again to start it again.");
|
|
400
|
+
}
|
|
401
|
+
/** Whether `git` is available; Hello App packing reads its commit. */
|
|
402
|
+
function gitAvailable() {
|
|
403
|
+
return spawnSync("git", ["--version"], { stdio: "ignore" }).status === 0;
|
|
404
|
+
}
|
|
@@ -151,7 +151,7 @@ export class OysterComputerEgress extends WorkerEntrypoint {
|
|
|
151
151
|
admitObject: (jobId, grant) => registry.admitBuildObject(jobId, computerObjectId, grant),
|
|
152
152
|
// The bytes stream from the Computer to R2, which checks the digest;
|
|
153
153
|
// the registry only admits the upload and later reads the facts.
|
|
154
|
-
storeObject: (object, body, mediaType) => putContentObject(this.env.RELEASES, object,
|
|
154
|
+
storeObject: (object, body, mediaType, owner) => putContentObject(this.env.RELEASES, owner, object,
|
|
155
155
|
// R2 takes a stream of known length; the stated length is exact.
|
|
156
156
|
body?.pipeThrough(new FixedLengthStream(object.byteLength)) ?? null, mediaType),
|
|
157
157
|
submitCandidate: (jobId, manifest, grant, credentialId) => registry.submitBuildCandidate(jobId, manifest, grant, credentialId),
|
package/dist/webhook-abuse.d.ts
CHANGED
|
@@ -18,7 +18,13 @@ export declare function workflowWebhookAbuse(options: {
|
|
|
18
18
|
hostConcurrency: number;
|
|
19
19
|
bindingConcurrency: number;
|
|
20
20
|
};
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* Per-instance streaming bound; durable run concurrency belongs to Braid.
|
|
23
|
+
* The rate key carries the tenant: a multi-tenant host (the Hub) serves
|
|
24
|
+
* every Team on one origin, and this lease is spent before any signature
|
|
25
|
+
* check, so a shared key would let one tenant's unauthenticated traffic
|
|
26
|
+
* refuse every other tenant's deliveries.
|
|
27
|
+
*/
|
|
22
28
|
hostLease(): Promise<null | (() => void)>;
|
|
23
29
|
bindingAdmission(bindingId: string): Promise<boolean>;
|
|
24
30
|
};
|
package/dist/webhook-abuse.js
CHANGED
|
@@ -21,12 +21,18 @@ export function workflowWebhookAbuse(options) {
|
|
|
21
21
|
let active = 0;
|
|
22
22
|
return {
|
|
23
23
|
concurrency,
|
|
24
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Per-instance streaming bound; durable run concurrency belongs to Braid.
|
|
26
|
+
* The rate key carries the tenant: a multi-tenant host (the Hub) serves
|
|
27
|
+
* every Team on one origin, and this lease is spent before any signature
|
|
28
|
+
* check, so a shared key would let one tenant's unauthenticated traffic
|
|
29
|
+
* refuse every other tenant's deliveries.
|
|
30
|
+
*/
|
|
25
31
|
async hostLease() {
|
|
26
32
|
if (active >= concurrency.hostConcurrency)
|
|
27
33
|
return null;
|
|
28
34
|
const { success } = await options.hostRate.limit({
|
|
29
|
-
key: rateKey([
|
|
35
|
+
key: rateKey([...prefix, "host"]),
|
|
30
36
|
});
|
|
31
37
|
// Native admission awaits I/O; simultaneous successful replies still
|
|
32
38
|
// cannot overbook the local streaming ceiling.
|
package/dist/workers.d.ts
CHANGED
|
@@ -260,8 +260,13 @@ export type OysterBindingContext<Env> = Readonly<{
|
|
|
260
260
|
*/
|
|
261
261
|
export type OysterBindingRow<Env> = Readonly<{
|
|
262
262
|
methods: Readonly<Record<string, FrameBindingMethod<OysterBindingResult>>>;
|
|
263
|
-
/**
|
|
264
|
-
|
|
263
|
+
/**
|
|
264
|
+
* Serve one decoded packet; the Instance has already admitted its frame and
|
|
265
|
+
* grant. `admitted` is the installation that packet's one Registry read
|
|
266
|
+
* re-admitted: its approved grants are the resource ceiling a row applies
|
|
267
|
+
* to results no single `requestedResource` selects.
|
|
268
|
+
*/
|
|
269
|
+
dispatch(context: OysterBindingContext<Env>, frame: InvocationFrame<OysterInvocationHost>, props: FrameBindingProps, method: string, args: readonly unknown[], admitted: Installation): Promise<OysterBindingResult>;
|
|
265
270
|
}>;
|
|
266
271
|
/**
|
|
267
272
|
* The bindings an Instance serves, keyed by the closed App binding vocabulary;
|