@vedivad/typst-web-service 0.8.0 → 0.8.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/dist/index.d.ts CHANGED
@@ -1,3 +1,57 @@
1
+ /** LSP Position (0-based line and character). */
2
+ interface LspPosition {
3
+ line: number;
4
+ character: number;
5
+ }
6
+ /** LSP Range. */
7
+ interface LspRange {
8
+ start: LspPosition;
9
+ end: LspPosition;
10
+ }
11
+ /** LSP Diagnostic as returned by tinymist. */
12
+ interface LspDiagnostic {
13
+ range: LspRange;
14
+ severity?: number;
15
+ message: string;
16
+ source?: string;
17
+ }
18
+ /** LSP MarkupContent (markdown or plaintext). */
19
+ interface LspMarkupContent {
20
+ kind: "markdown" | "plaintext";
21
+ value: string;
22
+ }
23
+ /** LSP CompletionItem (subset actually populated by tinymist). */
24
+ interface LspCompletionItem {
25
+ label: string;
26
+ kind?: number;
27
+ detail?: string;
28
+ documentation?: string | LspMarkupContent;
29
+ insertText?: string;
30
+ filterText?: string;
31
+ sortText?: string;
32
+ textEdit?: {
33
+ range: LspRange;
34
+ newText: string;
35
+ };
36
+ }
37
+ /** LSP CompletionList. */
38
+ interface LspCompletionList {
39
+ isIncomplete: boolean;
40
+ items: LspCompletionItem[];
41
+ }
42
+ /** Response shape for `textDocument/completion`. */
43
+ type LspCompletionResponse = LspCompletionList | LspCompletionItem[] | null;
44
+ /** LSP Hover contents — legacy and current forms tinymist may emit. */
45
+ type LspHoverContents = string | LspMarkupContent | (string | {
46
+ language: string;
47
+ value: string;
48
+ })[];
49
+ /** Response shape for `textDocument/hover`. */
50
+ interface LspHover {
51
+ contents: LspHoverContents;
52
+ range?: LspRange;
53
+ }
54
+
1
55
  interface TypstAnalyzerOptions {
2
56
  /**
3
57
  * Explicit Worker instance. When omitted, an inlined blob worker is created automatically.
@@ -40,31 +94,50 @@ declare class TypstAnalyzer {
40
94
  * in a single worker roundtrip.
41
95
  */
42
96
  didCloseMany(uris: string[]): Promise<void>;
43
- completion(uri: string, line: number, character: number): Promise<unknown>;
44
- hover(uri: string, line: number, character: number): Promise<unknown>;
97
+ completion(uri: string, line: number, character: number): Promise<LspCompletionResponse>;
98
+ hover(uri: string, line: number, character: number): Promise<LspHover | null>;
45
99
  destroy(): void;
46
100
  }
47
101
 
48
- /** LSP Diagnostic as returned by tinymist. */
49
- interface LspDiagnostic {
50
- range: {
51
- start: {
52
- line: number;
53
- character: number;
54
- };
55
- end: {
56
- line: number;
57
- character: number;
58
- };
59
- };
60
- severity?: number;
61
- message: string;
62
- source?: string;
63
- }
64
-
65
- declare function normalizeUntitledUri(uri: string): string;
66
- declare function normalizePath(path: string): string;
102
+ /**
103
+ * Two addressing schemes live in this codebase. This file is the only place
104
+ * that knows how they relate. Keeping the vocabulary and conversions colocated
105
+ * means a new contributor can read one file and understand the model.
106
+ *
107
+ * 1. Path — "/main.typ". Always leading-slash, always forward slashes.
108
+ * Used by the compiler VFS, the `TypstProject` public API,
109
+ * and the `typstFilePath` facet. Addresses a file within
110
+ * the project.
111
+ *
112
+ * 2. AnalyzerUri — "untitled:project/main.typ". What tinymist's LSP expects.
113
+ * Derived from a Path plus the analyzer URI root.
114
+ *
115
+ * The types below are type aliases, not branded/opaque types. That's
116
+ * deliberate: callers can pass string literals where a Path is expected
117
+ * without wrapping, and the aliases only serve as self-documenting
118
+ * signatures. Ingress points still need to call `normalizePath` to defend
119
+ * against un-normalized input — the alias does not imply the string has
120
+ * been normalized.
121
+ */
122
+ /** `/path/to/file.typ` — leading-slash, forward slashes only. */
123
+ type Path = string;
124
+ /** `untitled:project/path/to/file.typ` — what tinymist's LSP consumes. */
125
+ type AnalyzerUri = string;
126
+ /** Ensure a path starts with a leading slash. Idempotent. */
127
+ declare function normalizePath(path: string): Path;
128
+ /**
129
+ * Normalize an analyzer URI root. Ensures leading slash, strips trailing
130
+ * slashes. Special-cases "/" → "" so URIs don't end up with a leading
131
+ * `untitled://`.
132
+ */
67
133
  declare function normalizeRoot(rootPath: string): string;
134
+ /**
135
+ * Build the analyzer URI for a given project path. `root` is expected to
136
+ * already be normalized (see `normalizeRoot`).
137
+ *
138
+ * pathToAnalyzerUri("/main.typ", "/project") -> "untitled:project/main.typ"
139
+ */
140
+ declare function pathToAnalyzerUri(path: Path, root: string): AnalyzerUri;
68
141
 
69
142
  /** Source range for a diagnostic. All values are 0-indexed. */
70
143
  interface DiagnosticRange {
@@ -121,24 +194,20 @@ declare class TypstCompiler {
121
194
  private readonly proxy;
122
195
  private readonly worker;
123
196
  private readonly encoder;
124
- /** The most recent vector artifact from a compile, if any. */
125
- lastVector?: Uint8Array;
126
197
  private constructor();
127
198
  static create(options?: TypstCompilerOptions): Promise<TypstCompiler>;
128
199
  /**
129
- * Compile. Pass a source string (written to the entry path) or a map of
130
- * files to register before compiling; omit to compile whatever is currently
131
- * in the VFS (populated via setText/setBinary/setJson/setMany).
132
- * Defaults to compiling "/main.typ"; override with `entry`.
200
+ * Compile whatever is currently in the VFS (populated via
201
+ * setText/setBinary/setJson/setMany). Defaults to compiling "/main.typ";
202
+ * override with `entry`.
133
203
  */
134
- compile(source?: string | Record<string, string>, entry?: string): Promise<CompileResult>;
204
+ compile(entry?: string): Promise<CompileResult>;
135
205
  /**
136
- * Compile to PDF. Pass a source string (written to the entry path) or a map
137
- * of files to register before compiling; omit to compile whatever is
138
- * currently in the VFS (populated via setText/setBinary/setJson/setMany).
139
- * Defaults to compiling "/main.typ"; override with `entry`.
206
+ * Compile to PDF. Operates on whatever is currently in the VFS (populated
207
+ * via setText/setBinary/setJson/setMany). Defaults to compiling "/main.typ";
208
+ * override with `entry`.
140
209
  */
141
- compilePdf(source?: string | Record<string, string>, entry?: string): Promise<Uint8Array>;
210
+ compilePdf(entry?: string): Promise<Uint8Array>;
142
211
  /** Add or overwrite a text file in the virtual compiler filesystem. */
143
212
  setText(path: string, source: string): Promise<void>;
144
213
  /** Add or overwrite a JSON file in the virtual compiler filesystem. */
@@ -210,11 +279,12 @@ interface TypstProjectOptions {
210
279
  /** Default entry file path. Default: "/main.typ". */
211
280
  entry?: string;
212
281
  /**
213
- * Project root used to build stable analyzer URIs. Default: "/project".
214
- * URIs are formed as `untitled:<root-without-leading-slash><path>` — so
215
- * `/main.typ` becomes `untitled:project/main.typ`.
282
+ * Prefix used to build the `untitled:` URIs handed to the analyzer.
283
+ * Default: "/project". A path of `/main.typ` becomes
284
+ * `untitled:project/main.typ`. Only affects URI construction — the compiler
285
+ * and project VFS use the raw paths unchanged.
216
286
  */
217
- rootPath?: string;
287
+ analyzerUriRoot?: string;
218
288
  }
219
289
  /**
220
290
  * Coordinates a compiler + analyzer pair for multi-file Typst projects.
@@ -228,56 +298,102 @@ interface TypstProjectOptions {
228
298
  * await project.setMany({ "/main.typ": "...", "/utils.typ": "..." });
229
299
  * const result = await project.compile();
230
300
  */
301
+ type CompileListener = (result: CompileResult) => void;
231
302
  declare class TypstProject {
232
303
  private readonly compiler;
233
304
  private readonly analyzer?;
234
- private readonly rootPath;
305
+ private readonly analyzerUriRoot;
235
306
  private readonly trackedTextPaths;
236
307
  /** Last content written via setText/setMany, per path. Used to skip redundant writes to compiler + analyzer. */
237
308
  private readonly lastSyncedContent;
309
+ private readonly compileListeners;
310
+ private compileVersion;
311
+ private _lastResult;
238
312
  private _entry;
313
+ private destroyed;
239
314
  constructor(options: TypstProjectOptions);
240
- /** Current entry file path. */
241
- get entry(): string;
315
+ /** Current entry file path. Assign to change the sticky entry used by subsequent `compile()` calls. */
316
+ get entry(): Path;
317
+ set entry(path: Path);
242
318
  /** Whether an analyzer is attached. */
243
319
  get hasAnalyzer(): boolean;
244
- /** Change the sticky entry file used by subsequent compile() calls. */
245
- setEntry(path: string): void;
320
+ /**
321
+ * Most recent compile result, or `undefined` before the first compile has
322
+ * settled. Useful for lazy-mounted UI that subscribes after boot and needs
323
+ * an initial value.
324
+ */
325
+ get lastResult(): CompileResult | undefined;
326
+ /**
327
+ * Snapshot of tracked text file paths, in insertion order. Updated by
328
+ * `setText`, `setMany`, `remove`, and `clear`. Returns a fresh array — mutate
329
+ * freely without affecting project state.
330
+ */
331
+ get files(): Path[];
332
+ /**
333
+ * Current text content for a tracked file, or `undefined` if the path was
334
+ * never written via `setText`/`setMany` (or was removed). Read-through to the
335
+ * project's sync cache — lets consumers avoid shadowing the VFS themselves.
336
+ */
337
+ getText(path: Path): string | undefined;
246
338
  /**
247
339
  * Add or overwrite a text file. Goes to the compiler's VFS and, when an
248
340
  * analyzer is attached, to the analyzer as a document change. Redundant
249
341
  * calls with unchanged content are skipped.
250
342
  */
251
- setText(path: string, content: string): Promise<void>;
343
+ setText(path: Path, content: string): Promise<void>;
252
344
  /**
253
345
  * Add or overwrite a JSON file. Compiler-only — the analyzer does not track
254
346
  * data files.
255
347
  */
256
- setJson(path: string, value: unknown): Promise<void>;
348
+ setJson(path: Path, value: unknown): Promise<void>;
257
349
  /** Add or overwrite a binary file. Compiler-only. */
258
- setBinary(path: string, content: ArrayBuffer | ArrayBufferView): Promise<void>;
350
+ setBinary(path: Path, content: ArrayBuffer | ArrayBufferView): Promise<void>;
259
351
  /**
260
352
  * Batch set multiple files. Strings route to both compiler and analyzer;
261
353
  * Uint8Array entries go to the compiler only.
262
354
  */
263
- setMany(files: Record<string, string | Uint8Array>): Promise<void>;
355
+ setMany(files: Record<Path, string | Uint8Array>): Promise<void>;
264
356
  /**
265
357
  * Remove a file. Always removed from the compiler's VFS; also closed on the
266
358
  * analyzer when it was previously tracked as text.
267
359
  */
268
- remove(path: string): Promise<void>;
360
+ remove(path: Path): Promise<void>;
269
361
  /** Clear all files from both compiler VFS and analyzer document set. */
270
362
  clear(): Promise<void>;
271
- /** Compile the current VFS state using the sticky entry. */
363
+ /**
364
+ * Subscribe to compile results. Fires after every `compile()` whose result is
365
+ * still current (stale results from out-of-order concurrent compiles are
366
+ * dropped). If a compile has already settled, the most recent result is
367
+ * delivered synchronously so late-mounted listeners aren't stuck blank until
368
+ * the next compile. Returns an unsubscribe function.
369
+ */
370
+ onCompile(listener: CompileListener): () => void;
371
+ /**
372
+ * Compile the current VFS state using the sticky entry. Errors from the
373
+ * underlying compiler are converted into a synthetic error diagnostic so
374
+ * callers and listeners always receive a `CompileResult`. Listeners are
375
+ * notified only for the most recent compile — results from an earlier call
376
+ * that resolves after a later one are suppressed.
377
+ */
272
378
  compile(): Promise<CompileResult>;
273
379
  /** Compile the current VFS state to PDF using the sticky entry. */
274
380
  compilePdf(): Promise<Uint8Array>;
275
381
  private requireAnalyzer;
276
382
  /** Request completions at the given position. Throws when no analyzer is attached. */
277
- completion(path: string, line: number, character: number): Promise<unknown>;
383
+ completion(path: Path, line: number, character: number): Promise<LspCompletionResponse>;
278
384
  /** Request hover info at the given position. Throws when no analyzer is attached. */
279
- hover(path: string, line: number, character: number): Promise<unknown>;
280
- private toUri;
385
+ hover(path: Path, line: number, character: number): Promise<LspHover | null>;
386
+ /**
387
+ * Tear down the project and the services it owns. Destroys the attached
388
+ * compiler and analyzer, drops all listeners, and clears VFS tracking state.
389
+ * Idempotent — calling twice is a no-op. After destruction, further calls on
390
+ * the project are not supported; construct a new one.
391
+ *
392
+ * If you need to share a compiler or analyzer across projects, destroy them
393
+ * yourself and don't call this method — the project does not provide an
394
+ * ownership toggle.
395
+ */
396
+ destroy(): void;
281
397
  }
282
398
 
283
399
  interface TypstRendererOptions {
@@ -305,4 +421,4 @@ declare class TypstRenderer {
305
421
  renderSvg(vector: Uint8Array): Promise<string>;
306
422
  }
307
423
 
308
- export { type CompileResult, type DiagnosticMessage, type DiagnosticRange, type FormatConfig, type FormatRangeResult, type LspDiagnostic, TypstAnalyzer, type TypstAnalyzerOptions, TypstCompiler, type TypstCompilerOptions, TypstFormatter, TypstProject, type TypstProjectOptions, TypstRenderer, normalizePath, normalizeRoot, normalizeUntitledUri };
424
+ export { type AnalyzerUri, type CompileResult, type DiagnosticMessage, type DiagnosticRange, type FormatConfig, type FormatRangeResult, type LspCompletionItem, type LspCompletionList, type LspCompletionResponse, type LspDiagnostic, type LspHover, type LspHoverContents, type LspMarkupContent, type LspPosition, type LspRange, type Path, TypstAnalyzer, type TypstAnalyzerOptions, TypstCompiler, type TypstCompilerOptions, TypstFormatter, TypstProject, type TypstProjectOptions, TypstRenderer, normalizePath, normalizeRoot, pathToAnalyzerUri };