Add atomic, versioned notebook mutations, a searchable folder tree, and editor insertion forms. Validate notebook paths and symlink roots and retain drafts after external changes. Add yaml as a direct web dependency, reusing the existing workspace library for comment-preserving frontmatter edits.
137 lines
3.8 KiB
TypeScript
137 lines
3.8 KiB
TypeScript
import type { ServerEvent, StartRunRequest } from "@shellbook/core";
|
|
import type { Api } from "../api";
|
|
import { HttpError } from "../api";
|
|
import { parseNotebook, renderStep } from "@shellbook/core";
|
|
import initial from "./ssh-key.md?raw";
|
|
let source = initial;
|
|
let version = 1;
|
|
let counter = 0;
|
|
const listeners = new Set<(event: ServerEvent) => void>();
|
|
const emit = (event: ServerEvent) =>
|
|
listeners.forEach((listener) => listener(event));
|
|
const hosts = [
|
|
{
|
|
name: "staging",
|
|
user: "admin",
|
|
address: "192.0.2.10",
|
|
port: 22,
|
|
tags: ["demo"],
|
|
},
|
|
{
|
|
name: "production",
|
|
user: "ops",
|
|
address: "192.0.2.20",
|
|
port: 2222,
|
|
tags: ["prod"],
|
|
},
|
|
];
|
|
const artifactUrl = URL.createObjectURL(
|
|
new Blob(["Demo artifact only. No real key material.\n"], {
|
|
type: "text/plain",
|
|
}),
|
|
);
|
|
export const mockApi: Api = {
|
|
create: async () => {
|
|
throw new Error("Notebook management requires a live workspace");
|
|
},
|
|
move: async () => {
|
|
throw new Error("Notebook management requires a live workspace");
|
|
},
|
|
deleteNotebook: async () => {
|
|
throw new Error("Notebook management requires a live workspace");
|
|
},
|
|
me: async () => ({ authenticated: true }),
|
|
login: async () => undefined,
|
|
list: async () => ({
|
|
notebooks: [{ path: "ssh-key.md", title: "New server SSH key" }],
|
|
}),
|
|
hosts: async () => ({ hosts }),
|
|
notebook: async (path) => ({
|
|
path,
|
|
source,
|
|
notebook: parseNotebook(source),
|
|
version: String(version),
|
|
}),
|
|
save: async (_path, next, base) => {
|
|
if (base !== String(version)) throw new HttpError(409);
|
|
source = next;
|
|
version++;
|
|
return { version: String(version) };
|
|
},
|
|
events: (receive, connected) => {
|
|
listeners.add(receive);
|
|
connected(true);
|
|
return () => {
|
|
listeners.delete(receive);
|
|
};
|
|
},
|
|
artifact: () => artifactUrl,
|
|
run: async (request: StartRunRequest) => {
|
|
const notebook = parseNotebook(source);
|
|
const block = notebook.blocks.find(
|
|
(b) => b.type === "step" && b.step.attrs.id === request.stepId,
|
|
);
|
|
if (!block || block.type !== "step") throw new Error("Unknown demo step");
|
|
const rendered = renderStep(
|
|
notebook,
|
|
block.step,
|
|
request.params,
|
|
request.runtime,
|
|
hosts.map((h) => h.name),
|
|
);
|
|
const runId = `demo-${++counter}`;
|
|
setTimeout(() => {
|
|
emit({
|
|
type: "run.started",
|
|
runId,
|
|
notebookPath: request.notebookPath,
|
|
stepId: request.stepId,
|
|
script: rendered.script,
|
|
host: rendered.host,
|
|
});
|
|
if (rendered.host === "production" && !request.acceptHostKey) {
|
|
emit({
|
|
type: "run.hostKeyUnknown",
|
|
runId,
|
|
host: "production",
|
|
fingerprint: "SHA256:DEMO-VERIFY-OUT-OF-BAND",
|
|
changed: true,
|
|
});
|
|
return;
|
|
}
|
|
emit({
|
|
type: "run.output",
|
|
runId,
|
|
stream: "stdout",
|
|
data: "Demo stream — no shell commands execute.\n",
|
|
});
|
|
setTimeout(() => {
|
|
const failed = request.stepId === "password-login-disabled";
|
|
emit({
|
|
type: "run.output",
|
|
runId,
|
|
stream: failed ? "stderr" : "stdout",
|
|
data: failed
|
|
? "Demo: password authentication is enabled.\n"
|
|
: "Demo: completed successfully.\n",
|
|
});
|
|
emit({
|
|
type: "run.finished",
|
|
runId,
|
|
exitCode: failed ? 1 : 0,
|
|
durationMs: 850,
|
|
artifacts: rendered.artifacts,
|
|
sensitiveArtifacts:
|
|
request.stepId === "keygen"
|
|
? rendered.artifacts.filter((name) => !name.endsWith(".pub"))
|
|
: [],
|
|
captured: block.step.attrs.capture
|
|
? { name: block.step.attrs.capture, value: "demo-value" }
|
|
: undefined,
|
|
});
|
|
}, 850);
|
|
}, 50);
|
|
return { runId };
|
|
},
|
|
};
|