@pedit/serve 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +107 -2
- package/dist/bridge.d.ts +13 -0
- package/dist/bridge.js +87 -0
- package/dist/bridge.js.map +1 -0
- package/dist/client.d.ts +33 -0
- package/dist/client.js +176 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +19 -0
- package/dist/errors.js +24 -0
- package/dist/errors.js.map +1 -0
- package/dist/hono.d.ts +43 -0
- package/dist/hono.js +292 -0
- package/dist/hono.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/resolve.d.ts +20 -0
- package/dist/resolve.js +88 -0
- package/dist/resolve.js.map +1 -0
- package/dist/served.d.ts +29 -0
- package/dist/served.js +251 -0
- package/dist/served.js.map +1 -0
- package/dist/types.d.ts +14 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +86 -4
package/dist/served.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { realpath, stat } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { basename, dirname, isAbsolute } from 'node:path';
|
|
5
|
+
import { dialSocket } from "./bridge.js";
|
|
6
|
+
import { HeldError, LimitError, NoPeditError } from "./errors.js";
|
|
7
|
+
import { peditEnv } from "./resolve.js";
|
|
8
|
+
const MAX_FILES = 32;
|
|
9
|
+
const MAX_CLIENTS = 8;
|
|
10
|
+
const MAX_LINE = 4096;
|
|
11
|
+
const STDERR_KEEP = 1024;
|
|
12
|
+
const TERM_AFTER_MS = 5000;
|
|
13
|
+
async function inodeOf(real) {
|
|
14
|
+
const st = await stat(real);
|
|
15
|
+
if (!st.isFile())
|
|
16
|
+
throw new Error('not a regular file');
|
|
17
|
+
return `${st.dev}:${st.ino}`;
|
|
18
|
+
}
|
|
19
|
+
export class ServedFiles {
|
|
20
|
+
#options;
|
|
21
|
+
#byPath = new Map();
|
|
22
|
+
#byIno = new Map();
|
|
23
|
+
#closed = false;
|
|
24
|
+
constructor(options) {
|
|
25
|
+
if (process.platform === 'win32')
|
|
26
|
+
throw new Error('@pedit/serve does not support Windows');
|
|
27
|
+
if (!isAbsolute(options.bin))
|
|
28
|
+
throw new TypeError('bin must be an absolute path');
|
|
29
|
+
if (!options.app)
|
|
30
|
+
throw new TypeError('app is required');
|
|
31
|
+
this.#options = options;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Start or reuse the child serving `path` (absolute). Keyed by realpath and dev:ino, so a hard
|
|
35
|
+
* link or a case variant of one file never gets two children.
|
|
36
|
+
*/
|
|
37
|
+
async acquire(path) {
|
|
38
|
+
if (!isAbsolute(path))
|
|
39
|
+
throw new TypeError('path must be absolute');
|
|
40
|
+
for (;;) {
|
|
41
|
+
if (this.#closed)
|
|
42
|
+
throw new Error('ServedFiles is closed');
|
|
43
|
+
const real = await realpath(path);
|
|
44
|
+
const ino = await inodeOf(real);
|
|
45
|
+
// An inode alias counts only while the other path still has that inode: an atomic save
|
|
46
|
+
// gives the served path a new one and splits a hard link into two files.
|
|
47
|
+
const alias = this.#byIno.get(ino);
|
|
48
|
+
const aliasHolds = alias !== undefined &&
|
|
49
|
+
alias.path !== real &&
|
|
50
|
+
(await inodeOf(alias.path).catch(() => '')) === ino;
|
|
51
|
+
if (this.#closed)
|
|
52
|
+
throw new Error('ServedFiles is closed');
|
|
53
|
+
let entry = this.#byPath.get(real) ?? (aliasHolds ? alias : undefined);
|
|
54
|
+
if (entry && entry.ino !== ino && entry.path === real)
|
|
55
|
+
this.#reindex(entry, ino);
|
|
56
|
+
if (entry?.stopping) {
|
|
57
|
+
await entry.exited;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (!entry) {
|
|
61
|
+
if (this.#byPath.size >= MAX_FILES)
|
|
62
|
+
throw new LimitError('too many served files');
|
|
63
|
+
entry = this.#start(real, ino);
|
|
64
|
+
}
|
|
65
|
+
await entry.ready;
|
|
66
|
+
return entry.handle;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
#reindex(entry, ino) {
|
|
70
|
+
if (this.#byIno.get(entry.ino) === entry)
|
|
71
|
+
this.#byIno.delete(entry.ino);
|
|
72
|
+
entry.ino = ino;
|
|
73
|
+
this.#byIno.set(ino, entry);
|
|
74
|
+
}
|
|
75
|
+
/** End every child: close stdin, wait, SIGTERM after 5 s. Never SIGKILL inside the write delay. */
|
|
76
|
+
async close() {
|
|
77
|
+
this.#closed = true;
|
|
78
|
+
const entries = [...this.#byPath.values()];
|
|
79
|
+
for (const e of entries)
|
|
80
|
+
this.#stop(e, 'shutdown');
|
|
81
|
+
await Promise.all(entries.map((e) => e.exited));
|
|
82
|
+
}
|
|
83
|
+
#emit(event) {
|
|
84
|
+
try {
|
|
85
|
+
this.#options.onEvent?.(event);
|
|
86
|
+
}
|
|
87
|
+
catch { }
|
|
88
|
+
}
|
|
89
|
+
#start(path, ino) {
|
|
90
|
+
const child = spawn(this.#options.bin, [
|
|
91
|
+
'serve',
|
|
92
|
+
'--root',
|
|
93
|
+
dirname(path),
|
|
94
|
+
'--json',
|
|
95
|
+
'--app',
|
|
96
|
+
this.#options.app,
|
|
97
|
+
'--',
|
|
98
|
+
basename(path),
|
|
99
|
+
], { env: peditEnv(), cwd: tmpdir(), stdio: ['pipe', 'pipe', 'pipe'], shell: false });
|
|
100
|
+
let onExit;
|
|
101
|
+
const exited = new Promise((r) => {
|
|
102
|
+
onExit = r;
|
|
103
|
+
});
|
|
104
|
+
let onReady;
|
|
105
|
+
let onFail;
|
|
106
|
+
const ready = new Promise((res, rej) => {
|
|
107
|
+
onReady = res;
|
|
108
|
+
onFail = rej;
|
|
109
|
+
});
|
|
110
|
+
ready.catch(() => { }); // a rejection nobody awaits is not an unhandled one
|
|
111
|
+
const entry = {
|
|
112
|
+
path,
|
|
113
|
+
ino,
|
|
114
|
+
child,
|
|
115
|
+
ready,
|
|
116
|
+
socket: undefined,
|
|
117
|
+
generation: '',
|
|
118
|
+
clients: 0,
|
|
119
|
+
idle: undefined,
|
|
120
|
+
stopping: undefined,
|
|
121
|
+
exited,
|
|
122
|
+
dead: false,
|
|
123
|
+
handle: {
|
|
124
|
+
path,
|
|
125
|
+
get generation() {
|
|
126
|
+
return entry.generation;
|
|
127
|
+
},
|
|
128
|
+
dial: (role) => this.#dial(entry, role),
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
this.#byPath.set(path, entry);
|
|
132
|
+
this.#byIno.set(ino, entry);
|
|
133
|
+
let held;
|
|
134
|
+
let errorMessage;
|
|
135
|
+
let missing = false;
|
|
136
|
+
let stderr = '';
|
|
137
|
+
let buf = '';
|
|
138
|
+
child.stdin.on('error', () => { });
|
|
139
|
+
child.stderr.setEncoding('utf8');
|
|
140
|
+
child.stderr.on('data', (d) => {
|
|
141
|
+
if (stderr.length < STDERR_KEEP)
|
|
142
|
+
stderr = (stderr + d).slice(0, STDERR_KEEP);
|
|
143
|
+
});
|
|
144
|
+
const onLine = (line) => {
|
|
145
|
+
let ev;
|
|
146
|
+
try {
|
|
147
|
+
ev = JSON.parse(line);
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (ev.event === 'listening' && typeof ev.socket === 'string' && !entry.socket) {
|
|
153
|
+
if (typeof ev.generation !== 'string' || ev.generation === '') {
|
|
154
|
+
errorMessage = 'pedit did not report a generation; it is too old';
|
|
155
|
+
return child.kill('SIGTERM');
|
|
156
|
+
}
|
|
157
|
+
entry.socket = ev.socket;
|
|
158
|
+
entry.generation = ev.generation;
|
|
159
|
+
this.#emit({ type: 'started', path });
|
|
160
|
+
this.#armIdle(entry);
|
|
161
|
+
onReady(ev.socket);
|
|
162
|
+
}
|
|
163
|
+
else if (ev.event === 'held') {
|
|
164
|
+
held = { pid: Number(ev.pid), ...(typeof ev.app === 'string' ? { app: ev.app } : {}) };
|
|
165
|
+
}
|
|
166
|
+
else if (ev.event === 'error' && typeof ev.message === 'string') {
|
|
167
|
+
errorMessage = ev.message.slice(0, STDERR_KEEP);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
child.stdout.setEncoding('utf8');
|
|
171
|
+
child.stdout.on('data', (d) => {
|
|
172
|
+
buf += d;
|
|
173
|
+
for (let i = buf.indexOf('\n'); i >= 0; i = buf.indexOf('\n')) {
|
|
174
|
+
const line = buf.slice(0, i);
|
|
175
|
+
buf = buf.slice(i + 1);
|
|
176
|
+
if (line.length > MAX_LINE)
|
|
177
|
+
return child.kill('SIGTERM');
|
|
178
|
+
onLine(line);
|
|
179
|
+
}
|
|
180
|
+
if (buf.length > MAX_LINE)
|
|
181
|
+
child.kill('SIGTERM');
|
|
182
|
+
});
|
|
183
|
+
child.on('error', (e) => {
|
|
184
|
+
if (e.code === 'ENOENT')
|
|
185
|
+
missing = true;
|
|
186
|
+
else
|
|
187
|
+
errorMessage = e.message;
|
|
188
|
+
});
|
|
189
|
+
child.on('close', (code) => {
|
|
190
|
+
entry.dead = true;
|
|
191
|
+
clearTimeout(entry.idle);
|
|
192
|
+
if (this.#byPath.get(path) === entry)
|
|
193
|
+
this.#byPath.delete(path);
|
|
194
|
+
if (this.#byIno.get(entry.ino) === entry)
|
|
195
|
+
this.#byIno.delete(entry.ino);
|
|
196
|
+
if (!entry.socket) {
|
|
197
|
+
if (missing)
|
|
198
|
+
onFail(new NoPeditError('the pedit binary cannot be run'));
|
|
199
|
+
else if (code === 3 && held)
|
|
200
|
+
onFail(new HeldError(held));
|
|
201
|
+
else
|
|
202
|
+
onFail(new Error(errorMessage ?? (stderr.trim() || `pedit exited with code ${code}`)));
|
|
203
|
+
if (!held)
|
|
204
|
+
this.#emit({ type: 'error', path, message: String(errorMessage ?? stderr) });
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
if (stderr.trim() && code !== 0)
|
|
208
|
+
this.#emit({ type: 'error', path, message: stderr });
|
|
209
|
+
this.#emit({ type: 'stopped', path, reason: entry.stopping ?? 'exited' });
|
|
210
|
+
}
|
|
211
|
+
onExit();
|
|
212
|
+
});
|
|
213
|
+
return entry;
|
|
214
|
+
}
|
|
215
|
+
#armIdle(entry) {
|
|
216
|
+
clearTimeout(entry.idle);
|
|
217
|
+
if (entry.clients > 0 || entry.dead || entry.stopping)
|
|
218
|
+
return;
|
|
219
|
+
entry.idle = setTimeout(() => this.#stop(entry, 'idle'), this.#options.idleMs ?? 60_000);
|
|
220
|
+
entry.idle.unref();
|
|
221
|
+
}
|
|
222
|
+
#stop(entry, reason) {
|
|
223
|
+
if (entry.stopping || entry.dead)
|
|
224
|
+
return;
|
|
225
|
+
entry.stopping = reason;
|
|
226
|
+
clearTimeout(entry.idle);
|
|
227
|
+
entry.child.stdin?.end();
|
|
228
|
+
// Saving takes a write delay: ask nicely first, and never SIGKILL.
|
|
229
|
+
setTimeout(() => entry.dead || entry.child.kill('SIGTERM'), TERM_AFTER_MS).unref();
|
|
230
|
+
}
|
|
231
|
+
async #dial(entry, role) {
|
|
232
|
+
if (entry.dead || entry.stopping || !entry.socket)
|
|
233
|
+
throw new Error('the file is no longer served');
|
|
234
|
+
if (entry.clients >= MAX_CLIENTS)
|
|
235
|
+
throw new LimitError('too many clients for this file');
|
|
236
|
+
entry.clients++;
|
|
237
|
+
clearTimeout(entry.idle);
|
|
238
|
+
const release = () => {
|
|
239
|
+
entry.clients--;
|
|
240
|
+
this.#armIdle(entry);
|
|
241
|
+
};
|
|
242
|
+
try {
|
|
243
|
+
return await dialSocket(entry.socket, role, release);
|
|
244
|
+
}
|
|
245
|
+
catch (e) {
|
|
246
|
+
release();
|
|
247
|
+
throw e;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=served.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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dist/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** `read` sends `X-Pedit-Role: read`, which pedit enforces. */
|
|
2
|
+
export type Role = 'edit' | 'read';
|
|
3
|
+
export type ServeEvent = {
|
|
4
|
+
type: 'started';
|
|
5
|
+
path: string;
|
|
6
|
+
} | {
|
|
7
|
+
type: 'stopped';
|
|
8
|
+
path: string;
|
|
9
|
+
reason: 'idle' | 'exited' | 'shutdown';
|
|
10
|
+
} | {
|
|
11
|
+
type: 'error';
|
|
12
|
+
path: string | null;
|
|
13
|
+
message: string;
|
|
14
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,88 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pedit/serve",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Run pedit serve for a local app: the file's Yjs document over a Unix socket, with Hono routes and a browser client",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pedit",
|
|
7
|
+
"yjs",
|
|
8
|
+
"crdt",
|
|
9
|
+
"collaborative-editing",
|
|
10
|
+
"hono",
|
|
11
|
+
"local-first"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"homepage": "https://github.com/piconic-ai/pedit/tree/main/packages/serve#readme",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/piconic-ai/pedit.git",
|
|
18
|
+
"directory": "packages/serve"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public",
|
|
32
|
+
"provenance": true
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
36
|
+
"prepack": "cp ../../LICENSE LICENSE",
|
|
37
|
+
"postpack": "rm -f LICENSE",
|
|
38
|
+
"typecheck": "tsc -p .",
|
|
39
|
+
"test": "vitest run --passWithNoTests"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"ws": "^8.18.3"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"hono": "^4.9.0",
|
|
46
|
+
"y-protocols": "^1.0.7",
|
|
47
|
+
"y-websocket": "^3.0.0",
|
|
48
|
+
"yjs": "^13.6.33"
|
|
49
|
+
},
|
|
50
|
+
"peerDependenciesMeta": {
|
|
51
|
+
"hono": {
|
|
52
|
+
"optional": true
|
|
53
|
+
},
|
|
54
|
+
"y-protocols": {
|
|
55
|
+
"optional": true
|
|
56
|
+
},
|
|
57
|
+
"y-websocket": {
|
|
58
|
+
"optional": true
|
|
59
|
+
},
|
|
60
|
+
"yjs": {
|
|
61
|
+
"optional": true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@hono/node-server": "^1.19.5",
|
|
66
|
+
"@hono/node-ws": "^1.2.0",
|
|
67
|
+
"@types/node": "^22.0.0",
|
|
68
|
+
"@types/ws": "^8.18.1",
|
|
69
|
+
"hono": "^4.9.0",
|
|
70
|
+
"y-protocols": "^1.0.7",
|
|
71
|
+
"y-websocket": "^3.0.0",
|
|
72
|
+
"yjs": "^13.6.33"
|
|
73
|
+
},
|
|
74
|
+
"exports": {
|
|
75
|
+
".": {
|
|
76
|
+
"types": "./dist/index.d.ts",
|
|
77
|
+
"import": "./dist/index.js"
|
|
78
|
+
},
|
|
79
|
+
"./hono": {
|
|
80
|
+
"types": "./dist/hono.d.ts",
|
|
81
|
+
"import": "./dist/hono.js"
|
|
82
|
+
},
|
|
83
|
+
"./client": {
|
|
84
|
+
"types": "./dist/client.d.ts",
|
|
85
|
+
"import": "./dist/client.js"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|