Repository navigation
Expand file tree
/
Copy pathmessages.ts
More file actions
376 lines (315 loc) · 10.7 KB
/
Copy pathmessages.ts
File metadata and controls
376 lines (315 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* Wire protocol shared by the host page (where the Nodepod runtime lives) and
* the VS Code bridge extension (which runs inside the workbench's web
* extension host worker).
*
* Everything here must survive `structuredClone`: plain objects, primitives and
* `Uint8Array`. No class instances, no functions, no `vscode.Uri`.
*/
export const PROTOCOL_VERSION = 1;
/** Discriminator so unrelated BroadcastChannel traffic is ignored. */
export const ENVELOPE_TAG = "opends-code";
export type BridgeRole = "host" | "client";
/* -------------------------------------------------------------------------- */
/* Filesystem */
/* -------------------------------------------------------------------------- */
/** Mirrors `vscode.FileType` so the extension can cast without a lookup table. */
export const FileTypeDTO = {
Unknown: 0,
File: 1,
Directory: 2,
SymbolicLink: 64,
} as const;
export type FileTypeDTO = (typeof FileTypeDTO)[keyof typeof FileTypeDTO];
export interface FileStatDTO {
type: FileTypeDTO;
ctime: number;
mtime: number;
size: number;
/** Set when the entry is known to be read-only (e.g. pulled from storage in a read-only session). */
permissions?: 1;
}
export type DirEntryDTO = [name: string, type: FileTypeDTO];
/** Mirrors `vscode.FileChangeType`. */
export const FileChangeTypeDTO = {
Changed: 1,
Created: 2,
Deleted: 3,
} as const;
export type FileChangeTypeDTO =
(typeof FileChangeTypeDTO)[keyof typeof FileChangeTypeDTO];
export interface FileChangeDTO {
type: FileChangeTypeDTO;
path: string;
}
/* -------------------------------------------------------------------------- */
/* Search */
/* -------------------------------------------------------------------------- */
export interface FileSearchQueryDTO {
pattern: string;
includes?: string[];
excludes?: string[];
maxResults?: number;
}
export interface TextSearchQueryDTO {
pattern: string;
isRegExp?: boolean;
isCaseSensitive?: boolean;
isWordMatch?: boolean;
includes?: string[];
excludes?: string[];
maxResults?: number;
/** Files larger than this are skipped outright. Defaults to 8 MiB. */
maxFileSize?: number;
}
export interface TextSearchHitDTO {
path: string;
/** 0-based line index within the file. */
line: number;
/** The full text of the matching line, clipped to a sane width. */
preview: string;
/** Match ranges within `preview`, as `[startColumn, endColumn]` pairs. */
ranges: Array<[number, number]>;
}
/* -------------------------------------------------------------------------- */
/* Terminals */
/* -------------------------------------------------------------------------- */
/**
* `local` runs the shell in the browser through Nodepod — no container, no
* network round trip. `sandbox` proxies a real PTY in a Cloudflare Sandbox
* container, which is only booted when a sandbox terminal is actually opened.
*/
export type TerminalMode = "local" | "sandbox";
export interface TerminalOpenParams {
id: string;
mode: TerminalMode;
cols: number;
rows: number;
cwd?: string;
/** Run this command instead of an interactive shell, then keep the pty open. */
command?: string;
}
export interface TerminalOpenResult {
id: string;
mode: TerminalMode;
title: string;
/** Present for sandbox terminals once the container has accepted the session. */
sessionId?: string;
}
/* -------------------------------------------------------------------------- */
/* Processes and packages */
/* -------------------------------------------------------------------------- */
export interface ExecParams {
command: string;
cwd?: string;
env?: Record<string, string>;
mode?: TerminalMode;
}
export interface ExecResult {
exitCode: number;
stdout: string;
stderr: string;
}
export interface InstallParams {
packages?: string[];
cwd?: string;
dev?: boolean;
}
export interface InstallResult {
ok: boolean;
message: string;
}
/* -------------------------------------------------------------------------- */
/* Persistence */
/* -------------------------------------------------------------------------- */
export type SyncPhase = "idle" | "hydrating" | "pushing" | "pulling" | "error";
export interface SyncStatusDTO {
phase: SyncPhase;
/** Number of paths queued for upload. */
pending: number;
/** Epoch millis of the last successful push, or 0 if none yet. */
lastPushedAt: number;
/** Set when `phase === "error"`. */
error?: string;
/** False when the session was constructed without a storage adapter. */
enabled: boolean;
}
/* -------------------------------------------------------------------------- */
/* Previews */
/* -------------------------------------------------------------------------- */
export interface PreviewPortDTO {
port: number;
url: string;
}
/* -------------------------------------------------------------------------- */
/* Runtime */
/* -------------------------------------------------------------------------- */
export interface RuntimeInfoDTO {
protocolVersion: number;
/** Root path of the virtual workspace, e.g. `/workspace`. */
workspaceRoot: string;
/** URI scheme the extension must register a FileSystemProvider for. */
scheme: string;
ready: boolean;
/** True when a Cloudflare Sandbox endpoint is configured for this session. */
sandboxAvailable: boolean;
persistence: SyncStatusDTO;
}
/* -------------------------------------------------------------------------- */
/* Request map */
/* -------------------------------------------------------------------------- */
/**
* The single source of truth for the RPC surface. `BridgeRequests[M]` is the
* parameter type of method `M`, `BridgeResponses[M]` its result type; both the
* host dispatcher and the client proxy are typed off this map, so adding a
* method in one place is a compile error until it is handled in the other.
*/
export interface BridgeRequests {
"runtime.info": void;
"runtime.ready": void;
"fs.stat": { path: string };
"fs.readDirectory": { path: string };
"fs.readFile": { path: string };
"fs.writeFile": {
path: string;
data: Uint8Array;
create: boolean;
overwrite: boolean;
};
"fs.rename": { from: string; to: string; overwrite: boolean };
"fs.copy": { from: string; to: string; overwrite: boolean };
"fs.delete": { path: string; recursive: boolean };
"fs.createDirectory": { path: string };
"fs.watch": {
id: string;
path: string;
recursive: boolean;
excludes?: string[];
};
"fs.unwatch": { id: string };
"search.file": FileSearchQueryDTO;
"search.text": TextSearchQueryDTO;
"term.open": TerminalOpenParams;
"term.input": { id: string; data: string };
"term.resize": { id: string; cols: number; rows: number };
"term.close": { id: string };
"proc.exec": ExecParams;
"npm.install": InstallParams;
"sync.status": void;
"sync.flush": void;
"sync.pull": void;
"preview.list": void;
/** Boots (or resumes) the full `openvscode-server` inside the sandbox container. */
"sandbox.openEditor": void;
}
export interface BridgeResponses {
"runtime.info": RuntimeInfoDTO;
"runtime.ready": RuntimeInfoDTO;
"fs.stat": FileStatDTO;
"fs.readDirectory": DirEntryDTO[];
"fs.readFile": { data: Uint8Array };
"fs.writeFile": void;
"fs.rename": void;
"fs.copy": void;
"fs.delete": void;
"fs.createDirectory": void;
"fs.watch": void;
"fs.unwatch": void;
"search.file": string[];
"search.text": TextSearchHitDTO[];
"term.open": TerminalOpenResult;
"term.input": void;
"term.resize": void;
"term.close": void;
"proc.exec": ExecResult;
"npm.install": InstallResult;
"sync.status": SyncStatusDTO;
"sync.flush": SyncStatusDTO;
"sync.pull": SyncStatusDTO;
"preview.list": PreviewPortDTO[];
"sandbox.openEditor": { url: string };
}
export type BridgeMethod = keyof BridgeRequests;
/* -------------------------------------------------------------------------- */
/* Event map (host -> client, fire and forget) */
/* -------------------------------------------------------------------------- */
export interface BridgeEvents {
"fs.changed": { id: string; changes: FileChangeDTO[] };
"term.data": { id: string; data: string };
"term.exit": { id: string; code: number };
"preview.ready": PreviewPortDTO;
"sync.status": SyncStatusDTO;
"runtime.state": RuntimeInfoDTO;
/** Free-form log line surfaced in the extension's output channel. */
log: { level: "info" | "warn" | "error"; message: string };
}
export type BridgeEventName = keyof BridgeEvents;
/* -------------------------------------------------------------------------- */
/* Envelopes */
/* -------------------------------------------------------------------------- */
export interface RequestEnvelope {
tag: typeof ENVELOPE_TAG;
kind: "req";
from: BridgeRole;
session: string;
id: number;
method: BridgeMethod;
params: unknown;
}
export interface ResponseEnvelope {
tag: typeof ENVELOPE_TAG;
kind: "res";
from: BridgeRole;
session: string;
id: number;
ok: boolean;
result?: unknown;
error?: BridgeErrorDTO;
}
export interface EventEnvelope {
tag: typeof ENVELOPE_TAG;
kind: "evt";
from: BridgeRole;
session: string;
event: BridgeEventName;
payload: unknown;
}
/** Sent by a client on connect so a host that booted first replays its state. */
export interface HelloEnvelope {
tag: typeof ENVELOPE_TAG;
kind: "hello";
from: BridgeRole;
session: string;
protocolVersion: number;
}
export type BridgeEnvelope =
| RequestEnvelope
| ResponseEnvelope
| EventEnvelope
| HelloEnvelope;
/**
* Error codes the extension maps onto `vscode.FileSystemError` variants. Any
* other code surfaces as `Unavailable`, which is the honest answer for an
* unexpected runtime failure.
*/
export type BridgeErrorCode =
| "FileNotFound"
| "FileExists"
| "FileNotADirectory"
| "FileIsADirectory"
| "NoPermissions"
| "Unavailable"
| "Cancelled"
| "Unknown";
export interface BridgeErrorDTO {
code: BridgeErrorCode;
message: string;
stack?: string;
}
export function isBridgeEnvelope(value: unknown): value is BridgeEnvelope {
return (
typeof value === "object" &&
value !== null &&
(value as { tag?: unknown }).tag === ENVELOPE_TAG
);
}