@morkaai/runtime 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +13 -2
- package/bin/morka.cjs +57 -0
- package/dist/agent-actions/access.js +44 -0
- package/dist/agent-actions/manifest.js +52 -0
- package/dist/agent-actions/runner.js +69 -0
- package/dist/agents/account.js +42 -0
- package/dist/agents/acp-sandbox.js +118 -0
- package/dist/agents/activity.js +26 -0
- package/dist/agents/attachments.js +129 -0
- package/dist/agents/context.js +76 -0
- package/dist/agents/contracts.js +2 -0
- package/dist/agents/process.js +61 -0
- package/dist/agents/providers/acp.js +411 -0
- package/dist/agents/providers/claude.js +180 -0
- package/dist/agents/providers/codex.js +416 -0
- package/dist/agents/registry.js +76 -0
- package/dist/agents/sessions.js +331 -0
- package/dist/app-runtime/bridge.js +40 -0
- package/dist/app-runtime/starter/agents.json +1 -0
- package/dist/app-runtime/starter/index.html +11 -0
- package/dist/app-runtime/starter/main.js +0 -0
- package/dist/app-runtime/starter/package-lock.json +13 -0
- package/dist/app-runtime/starter/package.json +9 -0
- package/dist/app-runtime/starter/server.mjs +20 -0
- package/dist/app-runtime/starter/styles.css +0 -0
- package/dist/app-runtime/text-file.js +18 -0
- package/dist/branding/Morka.icns +0 -0
- package/dist/branding/README.md +24 -0
- package/dist/branding/generated-app-icon.png +0 -0
- package/dist/branding/morka-icon.png +0 -0
- package/dist/branding/morka-logo.png +0 -0
- package/dist/electron/main.js +802 -0
- package/dist/electron/preload.js +51 -0
- package/dist/instances/desktop.js +223 -0
- package/dist/instances/distribution.js +240 -0
- package/dist/instances/macos.js +62 -0
- package/dist/instances/repository.js +183 -0
- package/dist/instances/shadow-repository.js +95 -0
- package/dist/project/app-proxy.js +325 -0
- package/dist/project/bundle-toolchain.js +35 -0
- package/dist/project/drafts.js +27 -0
- package/dist/project/files.js +176 -0
- package/dist/project/live-preview.js +198 -0
- package/dist/project/managed-runtime.js +324 -0
- package/dist/project/node-lifecycle.js +45 -0
- package/dist/project/packages.js +302 -0
- package/dist/project/page-state.js +198 -0
- package/dist/project/project.js +112 -0
- package/dist/project/shadow-effects.js +156 -0
- package/dist/project/shadows.js +122 -0
- package/dist/project/toolchain.js +216 -0
- package/dist/renderer/activity.js +91 -0
- package/dist/renderer/app-setup.js +126 -0
- package/dist/renderer/chat-tools.css +83 -0
- package/dist/renderer/highlight.css +118 -0
- package/dist/renderer/icons/attach.png +0 -0
- package/dist/renderer/icons/brain-weight.png +0 -0
- package/dist/renderer/icons/brain.png +0 -0
- package/dist/renderer/icons/chat.png +0 -0
- package/dist/renderer/icons/chevron.png +0 -0
- package/dist/renderer/icons/clone.png +0 -0
- package/dist/renderer/icons/close.png +0 -0
- package/dist/renderer/icons/document.png +0 -0
- package/dist/renderer/icons/edit.png +0 -0
- package/dist/renderer/icons/folder.png +0 -0
- package/dist/renderer/icons/import.png +0 -0
- package/dist/renderer/icons/open-folder.png +0 -0
- package/dist/renderer/icons/play.png +0 -0
- package/dist/renderer/icons/plus.png +0 -0
- package/dist/renderer/icons/remove.png +0 -0
- package/dist/renderer/icons/search.png +0 -0
- package/dist/renderer/icons/send.png +0 -0
- package/dist/renderer/icons/shadow.png +0 -0
- package/dist/renderer/icons/stop.png +0 -0
- package/dist/renderer/icons/terminal.png +0 -0
- package/dist/renderer/index.html +130 -0
- package/dist/renderer/launcher/OFL.txt +93 -0
- package/dist/renderer/launcher/cell.js +322 -0
- package/dist/renderer/launcher/launcher.css +79 -0
- package/dist/renderer/launcher/lexend-caption-bold.ttf +0 -0
- package/dist/renderer/launcher.html +23 -0
- package/dist/renderer/licenses/dompurify.txt +202 -0
- package/dist/renderer/licenses/github-slugger.txt +5 -0
- package/dist/renderer/licenses/highlight.js.txt +29 -0
- package/dist/renderer/licenses/marked-footnote.txt +21 -0
- package/dist/renderer/licenses/marked-gfm-heading-id.txt +21 -0
- package/dist/renderer/licenses/marked.txt +44 -0
- package/dist/renderer/markdown.css +43 -0
- package/dist/renderer/markdown.js +74 -0
- package/dist/renderer/renderer.js +18902 -0
- package/dist/renderer/styles.css +351 -0
- package/dist/skills/morka-agent-action/SKILL.md +39 -0
- package/dist/skills/morka-app/SKILL.md +36 -0
- package/dist/skills/morka-shadow-authoring/SKILL.md +32 -0
- package/package.json +30 -4
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.appIcon = exports.preparedRelease = exports.bundledKit = exports.releaseProject = exports.releaseFile = exports.repositoryKit = exports.repositoryConfig = exports.idPattern = void 0;
|
|
4
|
+
exports.appsDirectory = appsDirectory;
|
|
5
|
+
exports.repositoryFile = repositoryFile;
|
|
6
|
+
exports.checkoutKey = checkoutKey;
|
|
7
|
+
exports.repositoryProfileId = repositoryProfileId;
|
|
8
|
+
exports.appName = appName;
|
|
9
|
+
exports.portableAppName = portableAppName;
|
|
10
|
+
exports.readRelease = readRelease;
|
|
11
|
+
exports.repositoryProject = repositoryProject;
|
|
12
|
+
exports.materializeRepository = materializeRepository;
|
|
13
|
+
exports.writeRepositoryScaffold = writeRepositoryScaffold;
|
|
14
|
+
exports.repositorySlug = repositorySlug;
|
|
15
|
+
exports.writeRepository = writeRepository;
|
|
16
|
+
const node_crypto_1 = require("node:crypto");
|
|
17
|
+
const node_fs_1 = require("node:fs");
|
|
18
|
+
const promises_1 = require("node:fs/promises");
|
|
19
|
+
const node_path_1 = require("node:path");
|
|
20
|
+
const files_1 = require("../project/files");
|
|
21
|
+
const project_1 = require("../project/project");
|
|
22
|
+
exports.idPattern = /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/;
|
|
23
|
+
exports.repositoryConfig = [".morka", "release.json"];
|
|
24
|
+
exports.repositoryKit = [".morka", "npm"];
|
|
25
|
+
exports.releaseFile = "morka-release.json";
|
|
26
|
+
exports.releaseProject = "morka-project";
|
|
27
|
+
exports.bundledKit = "morka-npm";
|
|
28
|
+
exports.preparedRelease = "morka-app-release";
|
|
29
|
+
exports.appIcon = "AppIcon.icns";
|
|
30
|
+
function appsDirectory(appData) {
|
|
31
|
+
return (0, node_path_1.join)(appData, "Morka Apps");
|
|
32
|
+
}
|
|
33
|
+
function repositoryFile(repository, ...parts) {
|
|
34
|
+
return (0, node_path_1.join)(repository, ".morka", ...parts);
|
|
35
|
+
}
|
|
36
|
+
function checkoutKey(directory) {
|
|
37
|
+
return (0, node_crypto_1.createHash)("sha256").update(process.platform === "win32" ? directory.toLowerCase() : directory).digest("hex");
|
|
38
|
+
}
|
|
39
|
+
function repositoryProfileId(spec, repository) {
|
|
40
|
+
return (0, node_crypto_1.createHash)("sha256").update(spec.id + ":" + checkoutKey(repository)).digest("hex").slice(0, 32);
|
|
41
|
+
}
|
|
42
|
+
function appName(value) {
|
|
43
|
+
const name = typeof value === "string" ? value.trim().normalize("NFC") : "";
|
|
44
|
+
if (!name || name.length > 72 || name.startsWith(".") || /[/:\\\p{Cc}]/u.test(name) || /\.app$/i.test(name)) {
|
|
45
|
+
throw new Error("Enter an app name of up to 72 characters. Leave out slashes, colons, and the .app extension.");
|
|
46
|
+
}
|
|
47
|
+
return name;
|
|
48
|
+
}
|
|
49
|
+
function portableAppName(value) {
|
|
50
|
+
const name = appName(value);
|
|
51
|
+
if (/[<>"|?*]/.test(name) || name.endsWith(".") ||
|
|
52
|
+
/^(?:CON|PRN|AUX|NUL|COM[1-9¹²³]|LPT[1-9¹²³])(?:\.|$)/i.test(name)) {
|
|
53
|
+
throw new Error("Choose an app name that can be used as a folder on Windows, macOS, and Linux.");
|
|
54
|
+
}
|
|
55
|
+
return name;
|
|
56
|
+
}
|
|
57
|
+
function readRelease(path) {
|
|
58
|
+
const spec = JSON.parse((0, node_fs_1.readFileSync)(path, "utf8"));
|
|
59
|
+
if (spec?.version !== 1 || typeof spec.id !== "string" || !exports.idPattern.test(spec.id) ||
|
|
60
|
+
spec.name !== appName(spec.name) || !Number.isInteger(spec.iconColor) || spec.iconColor < 0 || spec.iconColor >= 8) {
|
|
61
|
+
throw new Error("Invalid Morka release configuration.");
|
|
62
|
+
}
|
|
63
|
+
const { id, name, iconColor, clonedFrom } = spec;
|
|
64
|
+
if (clonedFrom && (!exports.idPattern.test(clonedFrom.id) || !/^[a-f0-9]{64}$/.test(clonedFrom.revision))) {
|
|
65
|
+
throw new Error("Invalid Morka clone origin.");
|
|
66
|
+
}
|
|
67
|
+
portableAppName(name);
|
|
68
|
+
return { version: 1, id, name, iconColor, ...(clonedFrom ? {
|
|
69
|
+
clonedFrom: { id: clonedFrom.id, revision: clonedFrom.revision },
|
|
70
|
+
} : {}) };
|
|
71
|
+
}
|
|
72
|
+
async function repositoryProject(repository) {
|
|
73
|
+
const project = (0, node_path_1.join)(await (0, promises_1.realpath)(repository), "app");
|
|
74
|
+
if (!(await (0, files_1.info)(project))?.isDirectory())
|
|
75
|
+
throw new Error("The repository needs a regular app/ directory.");
|
|
76
|
+
return project;
|
|
77
|
+
}
|
|
78
|
+
async function materializeRepository(parent, spec, files, kit, icon) {
|
|
79
|
+
const directory = await (0, promises_1.realpath)(parent);
|
|
80
|
+
const destination = (0, node_path_1.join)(directory, spec.name);
|
|
81
|
+
if (await (0, files_1.info)(destination))
|
|
82
|
+
throw new Error(`A folder named “${spec.name}” already exists at that location.`);
|
|
83
|
+
const staged = await (0, promises_1.mkdtemp)((0, node_path_1.join)(directory, ".morka-project-"));
|
|
84
|
+
try {
|
|
85
|
+
await writeRepository(staged, spec, files, kit, icon);
|
|
86
|
+
if (process.platform === "win32") {
|
|
87
|
+
await (0, promises_1.rename)(staged, destination);
|
|
88
|
+
return destination;
|
|
89
|
+
}
|
|
90
|
+
await (0, promises_1.mkdir)(destination, { mode: 0o700 });
|
|
91
|
+
try {
|
|
92
|
+
await (0, promises_1.rename)(staged, destination);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
await (0, promises_1.rmdir)(destination).catch(console.error);
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
return destination;
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
await (0, promises_1.rm)(staged, { recursive: true, force: true }).catch(console.error);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async function writeRepositoryScaffold(directory, name, kit, scripts, readme, instructions) {
|
|
105
|
+
const lock = JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(kit, "project-lock.json"), "utf8"));
|
|
106
|
+
const config = (0, node_path_1.join)(directory, ".morka");
|
|
107
|
+
const git = (0, node_path_1.join)(directory, ".git");
|
|
108
|
+
const packageJson = {
|
|
109
|
+
name,
|
|
110
|
+
version: lock.version,
|
|
111
|
+
private: true,
|
|
112
|
+
scripts,
|
|
113
|
+
engines: { node: ">=24" },
|
|
114
|
+
dependencies: lock.packages[""].dependencies,
|
|
115
|
+
devDependencies: lock.packages[""].devDependencies,
|
|
116
|
+
};
|
|
117
|
+
lock.name = lock.packages[""].name = name;
|
|
118
|
+
lock.packages[""].engines = packageJson.engines;
|
|
119
|
+
await Promise.all([
|
|
120
|
+
(0, promises_1.mkdir)(config, { recursive: true }),
|
|
121
|
+
(0, promises_1.mkdir)((0, node_path_1.join)(git, "objects", "info"), { recursive: true }),
|
|
122
|
+
(0, promises_1.mkdir)((0, node_path_1.join)(git, "objects", "pack"), { recursive: true }),
|
|
123
|
+
(0, promises_1.mkdir)((0, node_path_1.join)(git, "refs", "heads"), { recursive: true }),
|
|
124
|
+
(0, promises_1.mkdir)((0, node_path_1.join)(git, "refs", "tags"), { recursive: true }),
|
|
125
|
+
]);
|
|
126
|
+
await Promise.all([
|
|
127
|
+
(0, promises_1.cp)(kit, (0, node_path_1.join)(directory, ...exports.repositoryKit), { recursive: true }),
|
|
128
|
+
(0, promises_1.writeFile)((0, node_path_1.join)(directory, "package.json"), (0, project_1.jsonFile)(packageJson)),
|
|
129
|
+
(0, promises_1.writeFile)((0, node_path_1.join)(directory, "package-lock.json"), (0, project_1.jsonFile)(lock)),
|
|
130
|
+
(0, promises_1.writeFile)((0, node_path_1.join)(directory, ".gitignore"), "node_modules/\n/dist/\n/.morka/install.lock\n.DS_Store\n.env\n.env.*\n!.env.example\n.morka-data/\n*.sqlite\n*.sqlite-*\n*.sqlite3\n*.sqlite3-*\n*.db\n*.db-*\n"),
|
|
131
|
+
(0, promises_1.writeFile)((0, node_path_1.join)(directory, ".gitattributes"), "* text=auto\n*.icns binary\n*.tgz binary\n"),
|
|
132
|
+
(0, promises_1.writeFile)((0, node_path_1.join)(directory, "AGENTS.md"), instructions),
|
|
133
|
+
(0, promises_1.writeFile)((0, node_path_1.join)(directory, "README.md"), readme),
|
|
134
|
+
(0, promises_1.writeFile)((0, node_path_1.join)(git, "HEAD"), "ref: refs/heads/main\n"),
|
|
135
|
+
(0, promises_1.writeFile)((0, node_path_1.join)(git, "config"), "[core]\n\trepositoryformatversion = 0\n\tbare = false\n"),
|
|
136
|
+
]);
|
|
137
|
+
}
|
|
138
|
+
function repositorySlug(name) {
|
|
139
|
+
return name.toLowerCase().normalize("NFKD").replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
140
|
+
}
|
|
141
|
+
async function writeRepository(directory, spec, files, kit, icon) {
|
|
142
|
+
const project = (0, node_path_1.join)(directory, "app");
|
|
143
|
+
const config = (0, node_path_1.join)(directory, ".morka");
|
|
144
|
+
const name = repositorySlug(spec.name) || `morka-app-${spec.id.slice(0, 8)}`;
|
|
145
|
+
const scripts = { start: "morka run", build: "morka-pack local-build",
|
|
146
|
+
"package:mac": "morka-pack package:mac", "install:app": "node .morka/npm/install-app.cjs" };
|
|
147
|
+
const localInstructions = `## Run locally
|
|
148
|
+
|
|
149
|
+
Install Node.js 24 or newer with npm on macOS, Windows, or Linux.
|
|
150
|
+
|
|
151
|
+
\`\`\`sh
|
|
152
|
+
npm ci
|
|
153
|
+
npm start
|
|
154
|
+
\`\`\`
|
|
155
|
+
|
|
156
|
+
Run \`npm run install:app\` to install the project in your Applications or app menu.
|
|
157
|
+
This command installs missing npm dependencies. The installed app opens this
|
|
158
|
+
checkout and uses the same local app data and chats as \`npm start\`.
|
|
159
|
+
|
|
160
|
+
You can also open this folder in Morka. To create a local launch script, run
|
|
161
|
+
\`npm run build\` after installing the dependencies. The generated script lives
|
|
162
|
+
in \`dist/\` and opens this checkout. On macOS, run \`npm run package:mac\`
|
|
163
|
+
for a self-contained build.
|
|
164
|
+
|
|
165
|
+
If the project uses AI, open **AI setup** and sign in with your own supported
|
|
166
|
+
local CLI. Your account, chats and app data are stored outside the repository.
|
|
167
|
+
|
|
168
|
+
Commit the app source, lockfiles and .morka/ to share this repo. The portable
|
|
169
|
+
Morka archives in .morka/npm/ pin the engine code for all three platforms.
|
|
170
|
+
`;
|
|
171
|
+
await writeRepositoryScaffold(directory, name, kit, scripts, `# ${spec.name}
|
|
172
|
+
|
|
173
|
+
A desktop app built with Morka. The app source and dependencies live in \`app/\`.
|
|
174
|
+
|
|
175
|
+
${localInstructions}`, "# Editing the app\n\nRead app/morka.project.json first. App changes belong under app/. Leave root repository tooling, README, Git configuration, root npm metadata and .morka/ alone during app edits. App dependencies belong in app/package.json and app/package-lock.json; those app files are editable.\n");
|
|
176
|
+
await (0, promises_1.mkdir)(project);
|
|
177
|
+
await (0, promises_1.mkdir)((0, node_path_1.join)(config, "icons"));
|
|
178
|
+
await Promise.all([
|
|
179
|
+
(0, files_1.applyFiles)(project, new Map(), files),
|
|
180
|
+
(0, promises_1.writeFile)((0, node_path_1.join)(config, "release.json"), (0, project_1.jsonFile)(spec)),
|
|
181
|
+
(0, promises_1.writeFile)((0, node_path_1.join)(config, "icons", exports.appIcon), icon),
|
|
182
|
+
]);
|
|
183
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.containsPath = containsPath;
|
|
4
|
+
exports.readShadowWorkspace = readShadowWorkspace;
|
|
5
|
+
exports.createShadowRepository = createShadowRepository;
|
|
6
|
+
exports.packShadowRepository = packShadowRepository;
|
|
7
|
+
const node_crypto_1 = require("node:crypto");
|
|
8
|
+
const promises_1 = require("node:fs/promises");
|
|
9
|
+
const node_path_1 = require("node:path");
|
|
10
|
+
const files_1 = require("../project/files");
|
|
11
|
+
const project_1 = require("../project/project");
|
|
12
|
+
const shadows_1 = require("../project/shadows");
|
|
13
|
+
const repository_1 = require("./repository");
|
|
14
|
+
const workspaceFile = [".morka", "shadow.json"];
|
|
15
|
+
function containsPath(directory, path) {
|
|
16
|
+
const rel = (0, node_path_1.relative)(directory, path);
|
|
17
|
+
return !rel || (!(0, node_path_1.isAbsolute)(rel) && rel !== ".." && !rel.startsWith(".." + node_path_1.sep));
|
|
18
|
+
}
|
|
19
|
+
async function readShadowWorkspace(directory) {
|
|
20
|
+
const repository = await (0, promises_1.realpath)(directory);
|
|
21
|
+
const root = (0, node_path_1.join)(repository, "shadow");
|
|
22
|
+
if (!(await (0, files_1.info)(root))?.isDirectory())
|
|
23
|
+
throw new Error("Choose a Morka Shadow repository with a shadow/ directory.");
|
|
24
|
+
const { id, name } = (0, project_1.object)(JSON.parse(await (0, promises_1.readFile)((0, repository_1.repositoryFile)(repository, "shadow.json"), "utf8")));
|
|
25
|
+
if (typeof id !== "string" || !repository_1.idPattern.test(id) || typeof name !== "string" || name !== (0, repository_1.appName)(name)) {
|
|
26
|
+
throw new Error("Invalid Shadow repository information.");
|
|
27
|
+
}
|
|
28
|
+
await (0, shadows_1.readShadow)(root);
|
|
29
|
+
return { id, name, repository, root };
|
|
30
|
+
}
|
|
31
|
+
async function createShadowRepository(parent, name, kit) {
|
|
32
|
+
const repository = (0, node_path_1.join)(await (0, promises_1.realpath)(parent), name);
|
|
33
|
+
if (await (0, files_1.info)(repository))
|
|
34
|
+
throw new Error(`A folder named “${name}” already exists there.`);
|
|
35
|
+
const workspaceId = (0, node_crypto_1.randomUUID)();
|
|
36
|
+
const id = (0, repository_1.repositorySlug)(name).slice(0, 63).replace(/-+$/, "") || `shadow-${workspaceId.slice(0, 8)}`;
|
|
37
|
+
const staged = await (0, promises_1.mkdtemp)((0, node_path_1.join)(parent, ".morka-shadow-"));
|
|
38
|
+
const root = (0, node_path_1.join)(staged, "shadow");
|
|
39
|
+
try {
|
|
40
|
+
await (0, repository_1.writeRepositoryScaffold)(staged, id, kit, {
|
|
41
|
+
validate: "morka-pack shadow-validate", pack: "morka-pack shadow-pack",
|
|
42
|
+
}, `# ${name}
|
|
43
|
+
|
|
44
|
+
This repository contains an independent Morka Plugin Shadow. Open the repository
|
|
45
|
+
from the Shadows panel to edit it. The installable files are inside \`shadow/\`.
|
|
46
|
+
|
|
47
|
+
Run \`npm ci\`, then \`npm run validate\` to check the package and \`npm run pack\`
|
|
48
|
+
to create an installable folder under \`dist/\`. Select that folder in any Morka
|
|
49
|
+
app's Shadows panel to install it.
|
|
50
|
+
`, "# Editing this Shadow\n\nStart with shadow/manifest.json. Edit only files inside shadow/. The root contains the Git, npm, and Morka project configuration.\n");
|
|
51
|
+
const files = new Map([
|
|
52
|
+
["manifest.json", (0, project_1.jsonFile)({ id, version: "1.0.0", description: name, skills: `skills/${id}/SKILL.md`, rules: "rules/shadow.md" })],
|
|
53
|
+
[`skills/${id}/SKILL.md`, Buffer.from(`---\nname: ${id}\ndescription: Apply the ${id} Shadow to the target app.\n---\n\nRead the package rule and adapt it to the target app's source layout.\n`)],
|
|
54
|
+
["rules/shadow.md", Buffer.from("# Project rule\n\nDefine the lasting behavior for this Shadow here.\n")],
|
|
55
|
+
]);
|
|
56
|
+
await (0, promises_1.mkdir)(root);
|
|
57
|
+
await (0, files_1.applyFiles)(root, new Map(), files);
|
|
58
|
+
await (0, promises_1.writeFile)((0, node_path_1.join)(staged, ...workspaceFile), (0, project_1.jsonFile)({ id: workspaceId, name }));
|
|
59
|
+
await readShadowWorkspace(staged);
|
|
60
|
+
await (0, promises_1.mkdir)(repository, { mode: 0o700 });
|
|
61
|
+
try {
|
|
62
|
+
await (0, promises_1.rename)(staged, repository);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
await (0, promises_1.rmdir)(repository).catch(console.error);
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
return { id: workspaceId, name, repository, root: (0, node_path_1.join)(repository, "shadow") };
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
await (0, promises_1.rm)(staged, { force: true, recursive: true }).catch(console.error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function packShadowRepository(directory) {
|
|
75
|
+
const workspace = await readShadowWorkspace(directory);
|
|
76
|
+
const shadow = await (0, shadows_1.readShadow)(workspace.root);
|
|
77
|
+
const output = (0, node_path_1.join)(workspace.repository, "dist");
|
|
78
|
+
await (0, promises_1.mkdir)(output, { recursive: true });
|
|
79
|
+
const staged = await (0, promises_1.mkdtemp)((0, node_path_1.join)(output, ".morka-pack-"));
|
|
80
|
+
const destination = (0, node_path_1.join)(output, `${shadow.id}-${shadow.version}-${shadow.digest.slice(0, 12)}`);
|
|
81
|
+
try {
|
|
82
|
+
await (0, files_1.applyFiles)(staged, new Map(), shadow.files);
|
|
83
|
+
try {
|
|
84
|
+
await (0, promises_1.rename)(staged, destination);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
if ((await (0, shadows_1.readShadow)(destination).catch(() => undefined))?.digest !== shadow.digest)
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
return destination;
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
await (0, promises_1.rm)(staged, { force: true, recursive: true }).catch(console.error);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AppProxy = void 0;
|
|
4
|
+
const node_http_1 = require("node:http");
|
|
5
|
+
const node_net_1 = require("node:net");
|
|
6
|
+
const promises_1 = require("node:fs/promises");
|
|
7
|
+
const node_crypto_1 = require("node:crypto");
|
|
8
|
+
const node_path_1 = require("node:path");
|
|
9
|
+
const text_file_1 = require("../app-runtime/text-file");
|
|
10
|
+
class AppProxy {
|
|
11
|
+
originFile;
|
|
12
|
+
originsDirectory;
|
|
13
|
+
bridge;
|
|
14
|
+
target;
|
|
15
|
+
held = false;
|
|
16
|
+
checkingPreview = false;
|
|
17
|
+
waiting = [];
|
|
18
|
+
requests = new Map();
|
|
19
|
+
assets = new Map();
|
|
20
|
+
styleRules = new Map();
|
|
21
|
+
stylesheetSources = new Map();
|
|
22
|
+
styleHits = 0;
|
|
23
|
+
sockets = new Set();
|
|
24
|
+
origin = "";
|
|
25
|
+
server = (0, node_http_1.createServer)((req, res) => this.receive(req, res));
|
|
26
|
+
constructor(originFile, originsDirectory, bridge) {
|
|
27
|
+
this.originFile = originFile;
|
|
28
|
+
this.originsDirectory = originsDirectory;
|
|
29
|
+
this.bridge = bridge;
|
|
30
|
+
this.server.on("connection", socket => { this.sockets.add(socket); socket.once("close", () => this.sockets.delete(socket)); });
|
|
31
|
+
this.server.on("upgrade", (req, client, head) => {
|
|
32
|
+
if (!this.target || this.held || this.checkingPreview || (req.headers.origin && req.headers.origin !== this.origin)) {
|
|
33
|
+
client.destroy();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const target = new URL(this.target);
|
|
37
|
+
const upstream = (0, node_net_1.connect)(Number(target.port), target.hostname, () => {
|
|
38
|
+
upstream.write(`${req.method} ${req.url} HTTP/${req.httpVersion}\r\n` + req.rawHeaders.reduce((text, value, i) => text + value + (i % 2 ? "\r\n" : ": "), "") + "\r\n");
|
|
39
|
+
if (head.length)
|
|
40
|
+
upstream.write(head);
|
|
41
|
+
client.pipe(upstream).pipe(client);
|
|
42
|
+
});
|
|
43
|
+
upstream.on("error", () => client.destroy());
|
|
44
|
+
client.on("error", () => upstream.destroy());
|
|
45
|
+
client.once("close", () => upstream.destroy());
|
|
46
|
+
upstream.once("close", () => client.destroy());
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
get url() { return this.origin + "/"; }
|
|
50
|
+
get stylesheetHits() { return this.styleHits; }
|
|
51
|
+
async start(requestedPort = 0) {
|
|
52
|
+
if (this.origin)
|
|
53
|
+
return this.url;
|
|
54
|
+
const previous = await (0, promises_1.readFile)(this.originFile, "utf8").catch(() => undefined);
|
|
55
|
+
const port = previous ? JSON.parse(previous).port : requestedPort;
|
|
56
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535)
|
|
57
|
+
throw new Error("Invalid saved app origin port.");
|
|
58
|
+
const bind = async (selected) => {
|
|
59
|
+
await new Promise((resolve, reject) => {
|
|
60
|
+
this.server.once("error", reject);
|
|
61
|
+
this.server.listen(selected, "127.0.0.1", () => { this.server.off("error", reject); resolve(); });
|
|
62
|
+
}).catch(error => { throw new Error(`The app's stable local address is unavailable; it was not changed to avoid losing browser storage. ${error}`); });
|
|
63
|
+
const address = this.server.address();
|
|
64
|
+
if (!address || typeof address === "string")
|
|
65
|
+
throw new Error("The app proxy did not open a local port.");
|
|
66
|
+
return address.port;
|
|
67
|
+
};
|
|
68
|
+
try {
|
|
69
|
+
const selected = this.originsDirectory ? await this.reserve(port, bind) : await bind(port);
|
|
70
|
+
this.origin = `http://127.0.0.1:${selected}`;
|
|
71
|
+
await (0, text_file_1.writeTextAtomically)(this.originFile, JSON.stringify({ version: 1, port: selected }) + "\n");
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
await this.close();
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
return this.url;
|
|
78
|
+
}
|
|
79
|
+
async reserve(port, bind) {
|
|
80
|
+
const directory = this.originsDirectory;
|
|
81
|
+
await (0, promises_1.mkdir)(directory, { recursive: true, mode: 0o700 });
|
|
82
|
+
const lock = (0, node_path_1.join)(directory, "ports.lock"), file = (0, node_path_1.join)(directory, "ports.json");
|
|
83
|
+
const deadline = Date.now() + 30_000;
|
|
84
|
+
let acquired = false;
|
|
85
|
+
while (!acquired) {
|
|
86
|
+
try {
|
|
87
|
+
const handle = await (0, promises_1.open)(lock, "wx", 0o600);
|
|
88
|
+
await handle.writeFile(String(process.pid));
|
|
89
|
+
await handle.close();
|
|
90
|
+
acquired = true;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
if (error.code !== "EEXIST")
|
|
94
|
+
throw error;
|
|
95
|
+
const owner = Number(await (0, promises_1.readFile)(lock, "utf8").catch(() => "0"));
|
|
96
|
+
let alive = true;
|
|
97
|
+
if (owner > 0) {
|
|
98
|
+
try {
|
|
99
|
+
process.kill(owner, 0);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
alive = error.code !== "ESRCH";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const age = await (0, promises_1.stat)(lock).then(info => Date.now() - info.mtimeMs, () => 0);
|
|
106
|
+
if ((!alive && owner > 0) || (!owner && age > 30_000)) {
|
|
107
|
+
await (0, promises_1.rm)(lock, { force: true });
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (Date.now() > deadline)
|
|
111
|
+
throw new Error("Another app is allocating its stable origin. Retry shortly.");
|
|
112
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
const ports = JSON.parse(await (0, promises_1.readFile)(file, "utf8").catch(() => "{}"));
|
|
117
|
+
const key = (0, node_crypto_1.createHash)("sha256").update(this.originFile).digest("hex");
|
|
118
|
+
const remembered = port || ports[key] || 0;
|
|
119
|
+
const occupied = new Set(Object.entries(ports).filter(([id]) => id !== key).map(([, value]) => value));
|
|
120
|
+
if (remembered && occupied.has(remembered))
|
|
121
|
+
throw new Error("This stable origin belongs to another app; browser storage was not reassigned.");
|
|
122
|
+
for (let attempt = 0; attempt < 100; attempt++) {
|
|
123
|
+
const selected = await bind(remembered);
|
|
124
|
+
if (occupied.has(selected)) {
|
|
125
|
+
await new Promise(resolve => this.server.close(() => resolve()));
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
ports[key] = selected;
|
|
129
|
+
await (0, text_file_1.writeTextAtomically)(file, JSON.stringify(ports) + "\n");
|
|
130
|
+
return selected;
|
|
131
|
+
}
|
|
132
|
+
throw new Error("No unused stable local origin could be allocated.");
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
await (0, promises_1.rm)(lock, { force: true });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
receive(req, res) {
|
|
139
|
+
if (req.headers.origin && req.headers.origin !== this.origin) {
|
|
140
|
+
res.writeHead(403);
|
|
141
|
+
res.end("This app accepts requests from its own origin.");
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (this.held) {
|
|
145
|
+
if (this.waiting.length >= 512) {
|
|
146
|
+
res.writeHead(503);
|
|
147
|
+
res.end("The app is updating. Retry shortly.");
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
this.waiting.push(() => { if (!req.destroyed && !res.destroyed)
|
|
151
|
+
this.receive(req, res); });
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const target = this.target;
|
|
155
|
+
if (!target) {
|
|
156
|
+
res.writeHead(503);
|
|
157
|
+
res.end("The app is preparing its first release.");
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const write = !["GET", "HEAD", "OPTIONS"].includes(req.method || "GET");
|
|
161
|
+
if (write && this.checkingPreview) {
|
|
162
|
+
res.writeHead(503);
|
|
163
|
+
res.end("The updated preview is still being checked. Retry shortly.");
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const path = new URL(req.url || "/", this.origin);
|
|
167
|
+
let decodedPath;
|
|
168
|
+
try {
|
|
169
|
+
decodedPath = decodeURIComponent(path.pathname);
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
res.writeHead(400);
|
|
173
|
+
res.end("Invalid app URL.");
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (path.pathname === "/__morka/bridge.js" && this.bridge && ["GET", "HEAD"].includes(req.method || "GET")) {
|
|
177
|
+
res.writeHead(200, { "content-type": "text/javascript; charset=utf-8", "cache-control": "no-store", "x-content-type-options": "nosniff" });
|
|
178
|
+
res.end(req.method === "HEAD" ? undefined : this.bridge);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const asset = (req.method === "GET" || req.method === "HEAD") && this.assets.get(decodedPath);
|
|
182
|
+
if (asset) {
|
|
183
|
+
this.styleHits++;
|
|
184
|
+
res.writeHead(200, { "content-type": "text/css; charset=utf-8", "cache-control": "no-store" });
|
|
185
|
+
res.end(req.method === "HEAD" ? undefined : asset);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const counts = this.requests.get(target) || { all: 0, writes: 0 };
|
|
189
|
+
counts.all++;
|
|
190
|
+
if (write)
|
|
191
|
+
counts.writes++;
|
|
192
|
+
this.requests.set(target, counts);
|
|
193
|
+
let finished = false;
|
|
194
|
+
const done = () => { if (finished)
|
|
195
|
+
return; finished = true; counts.all--; if (write)
|
|
196
|
+
counts.writes--; };
|
|
197
|
+
const upstream = (0, node_http_1.request)(new URL(path.pathname + path.search, target), {
|
|
198
|
+
method: req.method, headers: { ...req.headers, ...(this.styleRules.size ? { "accept-encoding": "identity" } : {}),
|
|
199
|
+
"x-forwarded-host": new URL(this.origin).host, "x-forwarded-proto": "http" },
|
|
200
|
+
}, response => {
|
|
201
|
+
const headers = { ...response.headers };
|
|
202
|
+
const internalOrigin = new URL(target).origin;
|
|
203
|
+
if (headers.location === internalOrigin || headers.location?.startsWith(internalOrigin + "/"))
|
|
204
|
+
headers.location = this.origin + headers.location.slice(internalOrigin.length);
|
|
205
|
+
if (this.styleRules.size && response.statusCode === 200 && String(headers["content-type"]).includes("text/css")) {
|
|
206
|
+
const chunks = [];
|
|
207
|
+
let length = 0, streaming = false;
|
|
208
|
+
response.on("data", (chunk) => {
|
|
209
|
+
if (streaming) {
|
|
210
|
+
res.write(chunk);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
chunks.push(chunk);
|
|
214
|
+
length += chunk.length;
|
|
215
|
+
if (length > 8 * 1024 * 1024) {
|
|
216
|
+
streaming = true;
|
|
217
|
+
res.writeHead(200, headers);
|
|
218
|
+
res.write(Buffer.concat(chunks));
|
|
219
|
+
chunks.length = 0;
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
response.once("end", () => {
|
|
223
|
+
if (streaming) {
|
|
224
|
+
res.end();
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
let body = Buffer.concat(chunks);
|
|
228
|
+
const hash = (0, node_crypto_1.createHash)("sha256").update(body).digest("hex");
|
|
229
|
+
const matchingPaths = [...this.stylesheetSources.get(hash) ?? []].filter(source => (0, node_path_1.basename)(source) === (0, node_path_1.basename)(path.pathname));
|
|
230
|
+
const rule = matchingPaths.length === 1 ? this.styleRules.get(matchingPaths[0]) : undefined;
|
|
231
|
+
if (rule) {
|
|
232
|
+
body = rule;
|
|
233
|
+
this.styleHits++;
|
|
234
|
+
delete headers["content-encoding"];
|
|
235
|
+
delete headers["content-length"];
|
|
236
|
+
delete headers.etag;
|
|
237
|
+
delete headers["last-modified"];
|
|
238
|
+
headers["cache-control"] = "no-store";
|
|
239
|
+
}
|
|
240
|
+
res.writeHead(200, headers);
|
|
241
|
+
res.end(body);
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
res.writeHead(response.statusCode || 502, headers);
|
|
246
|
+
response.pipe(res);
|
|
247
|
+
}
|
|
248
|
+
response.once("end", done);
|
|
249
|
+
response.once("error", () => { done(); res.destroy(); });
|
|
250
|
+
response.once("close", done);
|
|
251
|
+
});
|
|
252
|
+
upstream.once("error", () => { done(); if (!res.headersSent)
|
|
253
|
+
res.writeHead(502); res.end("The app service is unavailable."); });
|
|
254
|
+
req.once("aborted", () => upstream.destroy());
|
|
255
|
+
req.pipe(upstream);
|
|
256
|
+
}
|
|
257
|
+
hold() { this.held = true; }
|
|
258
|
+
restoreStyles() {
|
|
259
|
+
const { assets, styleRules, stylesheetSources } = this;
|
|
260
|
+
return () => {
|
|
261
|
+
this.assets = assets;
|
|
262
|
+
this.styleRules = styleRules;
|
|
263
|
+
this.stylesheetSources = stylesheetSources;
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
route(target, checkingPreview = false) {
|
|
267
|
+
const previous = this.target;
|
|
268
|
+
const restore = this.restoreStyles();
|
|
269
|
+
this.target = target;
|
|
270
|
+
this.checkingPreview = checkingPreview;
|
|
271
|
+
this.assets = new Map();
|
|
272
|
+
this.styleRules = new Map();
|
|
273
|
+
this.stylesheetSources = new Map();
|
|
274
|
+
this.release();
|
|
275
|
+
return () => {
|
|
276
|
+
this.target = previous;
|
|
277
|
+
this.checkingPreview = false;
|
|
278
|
+
restore();
|
|
279
|
+
this.release();
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
activate(target) { this.route(target); }
|
|
283
|
+
confirmPreview() { this.checkingPreview = false; }
|
|
284
|
+
styles(files, previous) {
|
|
285
|
+
const restore = this.restoreStyles();
|
|
286
|
+
this.assets = new Map(this.assets);
|
|
287
|
+
this.styleRules = new Map(this.styleRules);
|
|
288
|
+
this.stylesheetSources = new Map(this.stylesheetSources);
|
|
289
|
+
for (const [path, bytes] of previous)
|
|
290
|
+
if (path.endsWith(".css")) {
|
|
291
|
+
const hash = (0, node_crypto_1.createHash)("sha256").update(bytes).digest("hex");
|
|
292
|
+
const paths = new Set(this.stylesheetSources.get(hash));
|
|
293
|
+
paths.add(path);
|
|
294
|
+
this.stylesheetSources.set(hash, paths);
|
|
295
|
+
}
|
|
296
|
+
for (const [path, bytes] of files) {
|
|
297
|
+
this.assets.set("/" + path, bytes);
|
|
298
|
+
this.styleRules.set(path, bytes);
|
|
299
|
+
}
|
|
300
|
+
return restore;
|
|
301
|
+
}
|
|
302
|
+
release() { this.held = false; const waiting = this.waiting; this.waiting = []; for (const resume of waiting)
|
|
303
|
+
resume(); }
|
|
304
|
+
async drained(target, writesOnly = false, signal) {
|
|
305
|
+
if (!target)
|
|
306
|
+
return;
|
|
307
|
+
const deadline = Date.now() + 30_000;
|
|
308
|
+
while (this.requests.get(target)?.[writesOnly ? "writes" : "all"]) {
|
|
309
|
+
signal?.throwIfAborted();
|
|
310
|
+
if (Date.now() > deadline)
|
|
311
|
+
throw new Error("The app still has requests in progress. Its current release was kept running.");
|
|
312
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
async close() {
|
|
316
|
+
this.target = undefined;
|
|
317
|
+
this.release();
|
|
318
|
+
for (const socket of this.sockets)
|
|
319
|
+
socket.destroy();
|
|
320
|
+
if (this.server.listening)
|
|
321
|
+
await new Promise(resolve => this.server.close(() => resolve()));
|
|
322
|
+
this.origin = "";
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
exports.AppProxy = AppProxy;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bundleToolchain = bundleToolchain;
|
|
4
|
+
const promises_1 = require("node:fs/promises");
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
const toolchain_1 = require("./toolchain");
|
|
7
|
+
// Bundle the build's actual Node/npm pair, independently of Electron's Node ABI.
|
|
8
|
+
// Releases never resolve `node` or `npm` from the end user's PATH.
|
|
9
|
+
async function bundleToolchain(destination) {
|
|
10
|
+
const node = await (0, promises_1.realpath)(process.execPath);
|
|
11
|
+
const npm = process.env.MORKA_NPM_DIR
|
|
12
|
+
? await (0, promises_1.realpath)(process.env.MORKA_NPM_DIR)
|
|
13
|
+
: (0, node_path_1.dirname)((0, node_path_1.dirname)(await (0, toolchain_1.npmEntry)(node, process.env.npm_execpath)));
|
|
14
|
+
const metadata = JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(npm, "package.json"), "utf8"));
|
|
15
|
+
await (0, promises_1.mkdir)((0, node_path_1.join)(destination, "bin"), { recursive: true });
|
|
16
|
+
await (0, promises_1.cp)(node, (0, node_path_1.join)(destination, "bin", "node"));
|
|
17
|
+
const relativeDateTranslations = (0, node_path_1.join)(npm, "node_modules", "tiny-relative-date", "translations");
|
|
18
|
+
await (0, promises_1.cp)(npm, (0, node_path_1.join)(destination, "npm"), {
|
|
19
|
+
recursive: true,
|
|
20
|
+
dereference: true,
|
|
21
|
+
filter: path => (0, node_path_1.dirname)(path) !== relativeDateTranslations || /^en(?:-short)?\.js$/.test((0, node_path_1.basename)(path)),
|
|
22
|
+
});
|
|
23
|
+
await (0, promises_1.cp)((0, node_path_1.join)((0, node_path_1.dirname)((0, node_path_1.dirname)(node)), "include", "node"), (0, node_path_1.join)(destination, "include", "node"), { recursive: true, dereference: true });
|
|
24
|
+
for (const [name, entry] of [["npm", "npm-cli.js"], ["npx", "npx-cli.js"]]) {
|
|
25
|
+
const path = (0, node_path_1.join)(destination, "bin", name);
|
|
26
|
+
await (0, promises_1.writeFile)(path, '#!/bin/sh\nnode_dir="$(dirname "$0")"\nexec "$node_dir/node" "$node_dir/../npm/bin/' + entry + '" "$@"\n');
|
|
27
|
+
await (0, promises_1.chmod)(path, 0o755);
|
|
28
|
+
}
|
|
29
|
+
const license = await (0, promises_1.realpath)((0, node_path_1.join)((0, node_path_1.dirname)((0, node_path_1.dirname)(node)), "LICENSE")).catch(() => (0, promises_1.realpath)((0, node_path_1.join)((0, node_path_1.dirname)((0, node_path_1.dirname)(node)), "share", "doc", "node", "LICENSE")));
|
|
30
|
+
await (0, promises_1.cp)(license, (0, node_path_1.join)(destination, "NODE-LICENSE"));
|
|
31
|
+
await (0, promises_1.writeFile)((0, node_path_1.join)(destination, "toolchain.json"), JSON.stringify({
|
|
32
|
+
version: 1, node: process.version, npm: metadata.version, platform: process.platform, arch: process.arch,
|
|
33
|
+
abi: process.versions.modules,
|
|
34
|
+
}, null, 2) + "\n");
|
|
35
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.synchronizeDraft = synchronizeDraft;
|
|
4
|
+
const promises_1 = require("node:fs/promises");
|
|
5
|
+
const files_1 = require("./files");
|
|
6
|
+
async function synchronizeDraft(workspace, current, saved, signal) {
|
|
7
|
+
let draft = await (0, files_1.snapshot)(workspace);
|
|
8
|
+
const base = (0, files_1.digest)(current);
|
|
9
|
+
const draftHash = (0, files_1.digest)(draft);
|
|
10
|
+
if (draftHash === base)
|
|
11
|
+
return { draft, base };
|
|
12
|
+
if (!draft.size || draftHash === saved.base) {
|
|
13
|
+
await (0, promises_1.mkdir)(workspace, { recursive: true, mode: 0o700 });
|
|
14
|
+
await (0, files_1.applyFiles)(workspace, draft, current, signal);
|
|
15
|
+
draft = new Map(current);
|
|
16
|
+
}
|
|
17
|
+
else if (saved.base !== base) {
|
|
18
|
+
const changed = [...new Set([...current.keys(), ...draft.keys()])]
|
|
19
|
+
.filter(path => {
|
|
20
|
+
const live = current.get(path), pending = draft.get(path);
|
|
21
|
+
return !live || !pending || !live.equals(pending);
|
|
22
|
+
}).sort();
|
|
23
|
+
throw new Error("The app source changed outside this chat while this chat has unpublished edits. The draft is preserved. " +
|
|
24
|
+
"Files to reconcile: " + changed.slice(0, 12).join(", "));
|
|
25
|
+
}
|
|
26
|
+
return { draft, base };
|
|
27
|
+
}
|