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