vize 0.728.0 → 0.730.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/dist/cli.mjs CHANGED
@@ -1,9 +1,126 @@
1
1
  import { createRequire } from "node:module";
2
- import fs, { readFileSync } from "node:fs";
2
+ import fs, { readFileSync, readSync, writeSync } from "node:fs";
3
3
  import path from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
5
  import { execFileSync, spawnSync } from "node:child_process";
6
6
  import readline from "node:readline";
7
+ //#region src/content-mapper-launch.ts
8
+ const MAX_HEADER_BYTES = 8 * 1024;
9
+ const MAX_MESSAGE_BYTES = 64 * 1024 * 1024;
10
+ const idle = new Int32Array(new SharedArrayBuffer(4));
11
+ /** Error raised when the launcher receives invalid or incomplete content-mapper framing. */
12
+ var ContentMapperFramingError = class extends Error {};
13
+ function readSome(buffer, offset, length) {
14
+ for (;;) try {
15
+ return readSync(0, buffer, offset, length, null);
16
+ } catch (error) {
17
+ const code = error.code;
18
+ if (code === "EOF") return 0;
19
+ if (code !== "EAGAIN") throw error;
20
+ Atomics.wait(idle, 0, 0, 5);
21
+ }
22
+ }
23
+ /** One frame body, or `null` at EOF before any header byte. */
24
+ function readFrame() {
25
+ const byte = Buffer.alloc(1);
26
+ let header = "";
27
+ let line = "";
28
+ let contentLength;
29
+ for (;;) {
30
+ if (header.length >= MAX_HEADER_BYTES) throw new ContentMapperFramingError("content-mapper headers exceed 8 KiB");
31
+ if (readSome(byte, 0, 1) === 0) {
32
+ if (header.length === 0) return null;
33
+ throw new ContentMapperFramingError("EOF inside content-mapper headers");
34
+ }
35
+ const char = byte.toString("latin1");
36
+ header += char;
37
+ line += char;
38
+ if (char !== "\n") continue;
39
+ if (line === "\r\n" || line === "\n") break;
40
+ const separator = line.indexOf(":");
41
+ if (separator >= 0 && line.slice(0, separator).toLowerCase() === "content-length") {
42
+ if (contentLength !== void 0) throw new ContentMapperFramingError("duplicate Content-Length header");
43
+ const value = line.slice(separator + 1).trim();
44
+ if (!/^\d+$/.test(value)) throw new ContentMapperFramingError("invalid Content-Length");
45
+ contentLength = Number(value);
46
+ }
47
+ line = "";
48
+ }
49
+ if (contentLength === void 0) throw new ContentMapperFramingError("missing Content-Length header");
50
+ if (contentLength > MAX_MESSAGE_BYTES) throw new ContentMapperFramingError("content-mapper message exceeds 64 MiB");
51
+ const body = Buffer.alloc(contentLength);
52
+ for (let offset = 0; offset < contentLength;) {
53
+ const read = readSome(body, offset, contentLength - offset);
54
+ if (read === 0) throw new ContentMapperFramingError("EOF inside a content-mapper message");
55
+ offset += read;
56
+ }
57
+ return body;
58
+ }
59
+ function writeFrame(value) {
60
+ const body = Buffer.from(JSON.stringify(value));
61
+ const frame = Buffer.concat([Buffer.from(`Content-Length: ${body.length}\r\n\r\n`), body]);
62
+ for (let offset = 0; offset < frame.length;) offset += writeSync(1, frame, offset);
63
+ }
64
+ const fail$1 = (id, code, message) => writeFrame({
65
+ error: {
66
+ code,
67
+ message
68
+ },
69
+ id,
70
+ jsonrpc: "2.0"
71
+ });
72
+ function initializeProblem(params) {
73
+ if (typeof params !== "object" || params === null || Array.isArray(params)) return "Invalid initialize params: expected an object";
74
+ const { locale, positionEncodings } = params;
75
+ if (locale !== void 0 && locale !== null && typeof locale !== "string") return "Invalid initialize params: `locale` must be a string";
76
+ if (positionEncodings === void 0) return "Invalid initialize params: missing field `positionEncodings`";
77
+ if (!Array.isArray(positionEncodings) || positionEncodings.some((e) => typeof e !== "string")) return "Invalid initialize params: `positionEncodings` must be a string array";
78
+ if (!positionEncodings.includes("utf-8")) return "Vize requires the utf-8 position encoding";
79
+ return null;
80
+ }
81
+ /**
82
+ * Answer the stream until `initialize` succeeds. `true` means the native
83
+ * server takes over initialized; `false` means the host closed the stream
84
+ * first and there is nothing to serve.
85
+ */
86
+ function answerContentMapperInitialize() {
87
+ for (;;) {
88
+ const body = readFrame();
89
+ if (body === null) return false;
90
+ let request;
91
+ try {
92
+ const parsed = JSON.parse(body.toString("utf8"));
93
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error("expected a request object");
94
+ request = parsed;
95
+ if (typeof request.method !== "string") throw new Error("missing field `method`");
96
+ } catch (error) {
97
+ fail$1(null, -32700, `Parse error: ${error.message}`);
98
+ continue;
99
+ }
100
+ const id = request.id ?? null;
101
+ if (request.method === "initialize") {
102
+ const problem = initializeProblem(request.params ?? null);
103
+ if (problem === null) {
104
+ writeFrame({
105
+ id,
106
+ jsonrpc: "2.0",
107
+ result: {
108
+ diagnosticSource: "vize",
109
+ positionEncoding: "utf-8"
110
+ }
111
+ });
112
+ return true;
113
+ }
114
+ fail$1(id, -32602, problem);
115
+ } else if ([
116
+ "openProject",
117
+ "closeProject",
118
+ "transform"
119
+ ].includes(request.method)) fail$1(id, -32002, "Content mapper is not initialized");
120
+ else fail$1(id, -32601, `Method not found: ${request.method}`);
121
+ }
122
+ }
123
+ //#endregion
7
124
  //#region src/setup/config.ts
8
125
  const VIZE_CONFIG_FILES = [
9
126
  "vize.config.pkl",
@@ -2084,12 +2201,18 @@ try {
2084
2201
  const args = process.argv.slice(2);
2085
2202
  if (args[0] === "setup") runSetupCli(args.slice(1));
2086
2203
  else if (args[0] === "init") runInitCli(args.slice(1)).catch(fail);
2087
- else {
2204
+ else if (args[0] === "content-mapper") {
2205
+ assertMatchingNativeVersion();
2206
+ if (answerContentMapperInitialize()) require("@vizejs/native").runCli([...args, "--initialized"]);
2207
+ } else {
2088
2208
  assertMatchingNativeVersion();
2089
2209
  require("@vizejs/native").runCli(args);
2090
2210
  }
2091
2211
  } catch (error) {
2092
- fail(error);
2212
+ if (error instanceof ContentMapperFramingError) {
2213
+ process.stderr.write(`vize content-mapper: ${error.message}\n`);
2214
+ process.exitCode = 1;
2215
+ } else fail(error);
2093
2216
  }
2094
2217
  //#endregion
2095
2218
  export {};