@elliots/typical-compiler 0.2.4 → 0.3.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/client.d.ts +1 -1
- package/dist/client.js +31 -30
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/protocol.js +8 -8
- package/dist/types.d.ts +2 -2
- package/package.json +7 -7
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ProjectHandle, TransformResult, AnalyseResult } from
|
|
1
|
+
import type { ProjectHandle, TransformResult, AnalyseResult } from "./types.js";
|
|
2
2
|
export interface TypicalCompilerOptions {
|
|
3
3
|
/** Path to the typical binary. If not provided, uses the bundled binary. */
|
|
4
4
|
binaryPath?: string;
|
package/dist/client.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { spawn } from
|
|
2
|
-
import { join, dirname } from
|
|
3
|
-
import { fileURLToPath } from
|
|
4
|
-
import { createRequire } from
|
|
5
|
-
import { encodeRequest, decodeResponse } from
|
|
6
|
-
import { existsSync } from
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { join, dirname } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import { encodeRequest, decodeResponse } from "./protocol.js";
|
|
6
|
+
import { existsSync } from "node:fs";
|
|
7
7
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
8
8
|
const require = createRequire(import.meta.url);
|
|
9
|
-
const debug = process.env.DEBUG ===
|
|
9
|
+
const debug = process.env.DEBUG === "1";
|
|
10
10
|
function debugLog(...args) {
|
|
11
11
|
if (debug) {
|
|
12
12
|
console.error(...args);
|
|
@@ -14,7 +14,7 @@ function debugLog(...args) {
|
|
|
14
14
|
}
|
|
15
15
|
function getBinaryPath() {
|
|
16
16
|
// use bin/typical in development
|
|
17
|
-
const devPath = join(__dirname,
|
|
17
|
+
const devPath = join(__dirname, "../bin/typical");
|
|
18
18
|
if (existsSync(devPath)) {
|
|
19
19
|
debugLog(`[CLIENT] Using development binary at ${devPath}`);
|
|
20
20
|
return devPath;
|
|
@@ -40,20 +40,20 @@ export class TypicalCompiler {
|
|
|
40
40
|
}
|
|
41
41
|
async start() {
|
|
42
42
|
if (this.process) {
|
|
43
|
-
throw new Error(
|
|
43
|
+
throw new Error("Compiler already started");
|
|
44
44
|
}
|
|
45
|
-
this.process = spawn(this.binaryPath, [
|
|
46
|
-
stdio: [
|
|
45
|
+
this.process = spawn(this.binaryPath, ["--cwd", this.cwd], {
|
|
46
|
+
stdio: ["pipe", "pipe", "inherit"],
|
|
47
47
|
});
|
|
48
48
|
// Don't let the child process keep the Node process alive
|
|
49
49
|
this.process.unref();
|
|
50
|
-
this.process.stdout.on(
|
|
50
|
+
this.process.stdout.on("data", (data) => {
|
|
51
51
|
this.handleData(data);
|
|
52
52
|
});
|
|
53
|
-
this.process.on(
|
|
54
|
-
console.error(
|
|
53
|
+
this.process.on("error", (err) => {
|
|
54
|
+
console.error("Compiler process error:", err);
|
|
55
55
|
});
|
|
56
|
-
this.process.on(
|
|
56
|
+
this.process.on("exit", (code) => {
|
|
57
57
|
this.process = null;
|
|
58
58
|
// Reject any pending requests
|
|
59
59
|
for (const [, { reject }] of this.pendingRequests) {
|
|
@@ -62,8 +62,8 @@ export class TypicalCompiler {
|
|
|
62
62
|
this.pendingRequests.clear();
|
|
63
63
|
});
|
|
64
64
|
// Test the connection with echo
|
|
65
|
-
const result = await this.request(
|
|
66
|
-
if (result !==
|
|
65
|
+
const result = await this.request("echo", "ping");
|
|
66
|
+
if (result !== "ping") {
|
|
67
67
|
throw new Error(`Echo test failed: expected "ping", got "${result}"`);
|
|
68
68
|
}
|
|
69
69
|
}
|
|
@@ -78,11 +78,11 @@ export class TypicalCompiler {
|
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
async loadProject(configFileName) {
|
|
81
|
-
return this.request(
|
|
81
|
+
return this.request("loadProject", { configFileName });
|
|
82
82
|
}
|
|
83
83
|
async transformFile(project, fileName, ignoreTypes, maxGeneratedFunctions) {
|
|
84
|
-
const projectId = typeof project ===
|
|
85
|
-
return this.request(
|
|
84
|
+
const projectId = typeof project === "string" ? project : project.id;
|
|
85
|
+
return this.request("transformFile", {
|
|
86
86
|
project: projectId,
|
|
87
87
|
fileName,
|
|
88
88
|
ignoreTypes,
|
|
@@ -90,8 +90,8 @@ export class TypicalCompiler {
|
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
92
|
async release(handle) {
|
|
93
|
-
const id = typeof handle ===
|
|
94
|
-
await this.request(
|
|
93
|
+
const id = typeof handle === "string" ? handle : handle.id;
|
|
94
|
+
await this.request("release", id);
|
|
95
95
|
}
|
|
96
96
|
/**
|
|
97
97
|
* Analyse a file for validation points without transforming it.
|
|
@@ -105,8 +105,8 @@ export class TypicalCompiler {
|
|
|
105
105
|
* @returns Analysis result with validation items
|
|
106
106
|
*/
|
|
107
107
|
async analyseFile(project, fileName, content, ignoreTypes) {
|
|
108
|
-
const projectId = typeof project ===
|
|
109
|
-
return this.request(
|
|
108
|
+
const projectId = typeof project === "string" ? project : project.id;
|
|
109
|
+
return this.request("analyseFile", {
|
|
110
110
|
project: projectId,
|
|
111
111
|
fileName,
|
|
112
112
|
content,
|
|
@@ -123,7 +123,7 @@ export class TypicalCompiler {
|
|
|
123
123
|
* @returns Transformed code with validation
|
|
124
124
|
*/
|
|
125
125
|
async transformSource(fileName, source, options) {
|
|
126
|
-
return this.request(
|
|
126
|
+
return this.request("transformSource", {
|
|
127
127
|
fileName,
|
|
128
128
|
source,
|
|
129
129
|
ignoreTypes: options?.ignoreTypes,
|
|
@@ -132,7 +132,7 @@ export class TypicalCompiler {
|
|
|
132
132
|
}
|
|
133
133
|
async request(method, payload) {
|
|
134
134
|
if (!this.process) {
|
|
135
|
-
throw new Error(
|
|
135
|
+
throw new Error("Compiler not started");
|
|
136
136
|
}
|
|
137
137
|
// Use unique request ID to correlate request/response
|
|
138
138
|
const requestId = `${method}:${this.nextRequestId++}`;
|
|
@@ -158,17 +158,18 @@ export class TypicalCompiler {
|
|
|
158
158
|
// Find the pending request
|
|
159
159
|
const pending = this.pendingRequests.get(method);
|
|
160
160
|
if (!pending) {
|
|
161
|
-
const pendingKeys = [...this.pendingRequests.keys()].join(
|
|
162
|
-
throw new Error(`No pending request for method: ${method}. Pending requests: ${pendingKeys}. ` +
|
|
161
|
+
const pendingKeys = [...this.pendingRequests.keys()].join(", ") || "(none)";
|
|
162
|
+
throw new Error(`No pending request for method: ${method}. Pending requests: ${pendingKeys}. ` +
|
|
163
|
+
`This indicates a protocol bug - received response for a request that wasn't made or was already resolved.`);
|
|
163
164
|
}
|
|
164
165
|
this.pendingRequests.delete(method);
|
|
165
166
|
if (messageType === 4 /* MessageType.Response */) {
|
|
166
167
|
// Parse JSON payload
|
|
167
|
-
const result = payload.length > 0 ? JSON.parse(payload.toString(
|
|
168
|
+
const result = payload.length > 0 ? JSON.parse(payload.toString("utf8")) : null;
|
|
168
169
|
pending.resolve(result);
|
|
169
170
|
}
|
|
170
171
|
else if (messageType === 5 /* MessageType.Error */) {
|
|
171
|
-
pending.reject(new Error(payload.toString(
|
|
172
|
+
pending.reject(new Error(payload.toString("utf8")));
|
|
172
173
|
}
|
|
173
174
|
else {
|
|
174
175
|
pending.reject(new Error(`Unexpected message type: ${messageType}`));
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { TypicalCompiler, type TypicalCompilerOptions } from
|
|
2
|
-
export type { ProjectHandle, TransformResult, RawSourceMap } from
|
|
1
|
+
export { TypicalCompiler, type TypicalCompilerOptions } from "./client.js";
|
|
2
|
+
export type { ProjectHandle, TransformResult, RawSourceMap } from "./types.js";
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { TypicalCompiler } from
|
|
1
|
+
export { TypicalCompiler } from "./client.js";
|
package/dist/protocol.js
CHANGED
|
@@ -8,9 +8,9 @@ const MessagePackTypeBin16 = 0xc5;
|
|
|
8
8
|
const MessagePackTypeBin32 = 0xc6;
|
|
9
9
|
const MessagePackTypeU8 = 0xcc;
|
|
10
10
|
export function encodeRequest(method, payload) {
|
|
11
|
-
const methodBuf = Buffer.from(method,
|
|
11
|
+
const methodBuf = Buffer.from(method, "utf8");
|
|
12
12
|
const payloadStr = JSON.stringify(payload);
|
|
13
|
-
const payloadBuf = Buffer.from(payloadStr,
|
|
13
|
+
const payloadBuf = Buffer.from(payloadStr, "utf8");
|
|
14
14
|
// Calculate total size
|
|
15
15
|
// 1 byte for array marker (0x93)
|
|
16
16
|
// 2 bytes for message type (0xCC + type)
|
|
@@ -36,7 +36,7 @@ export function decodeResponse(data) {
|
|
|
36
36
|
let offset = 0;
|
|
37
37
|
// Check array marker
|
|
38
38
|
if (data[offset++] !== MessagePackTypeFixedArray3) {
|
|
39
|
-
throw new Error(`Expected 0x93, got 0x${data[0].toString(16)}: first 200 of data: ${data.subarray(0, 200).toString(
|
|
39
|
+
throw new Error(`Expected 0x93, got 0x${data[0].toString(16)}: first 200 of data: ${data.subarray(0, 200).toString("utf8")}`);
|
|
40
40
|
}
|
|
41
41
|
// Read message type
|
|
42
42
|
if (data[offset++] !== MessagePackTypeU8) {
|
|
@@ -46,7 +46,7 @@ export function decodeResponse(data) {
|
|
|
46
46
|
// Read method
|
|
47
47
|
const { value: methodBuf, newOffset: offset2 } = readBin(data, offset);
|
|
48
48
|
offset = offset2;
|
|
49
|
-
const method = methodBuf.toString(
|
|
49
|
+
const method = methodBuf.toString("utf8");
|
|
50
50
|
// Read payload
|
|
51
51
|
const { value: payload, newOffset: offset3 } = readBin(data, offset);
|
|
52
52
|
return { messageType, method, payload, bytesConsumed: offset3 };
|
|
@@ -79,25 +79,25 @@ function writeBin(buf, offset, data) {
|
|
|
79
79
|
}
|
|
80
80
|
function readBin(buf, offset) {
|
|
81
81
|
if (offset >= buf.length) {
|
|
82
|
-
throw new Error(
|
|
82
|
+
throw new Error("Not enough data: need type byte");
|
|
83
83
|
}
|
|
84
84
|
const type = buf[offset++];
|
|
85
85
|
let length;
|
|
86
86
|
switch (type) {
|
|
87
87
|
case MessagePackTypeBin8:
|
|
88
88
|
if (offset >= buf.length)
|
|
89
|
-
throw new Error(
|
|
89
|
+
throw new Error("Not enough data: need length byte");
|
|
90
90
|
length = buf[offset++];
|
|
91
91
|
break;
|
|
92
92
|
case MessagePackTypeBin16:
|
|
93
93
|
if (offset + 2 > buf.length)
|
|
94
|
-
throw new Error(
|
|
94
|
+
throw new Error("Not enough data: need 2 length bytes");
|
|
95
95
|
length = buf.readUInt16BE(offset);
|
|
96
96
|
offset += 2;
|
|
97
97
|
break;
|
|
98
98
|
case MessagePackTypeBin32:
|
|
99
99
|
if (offset + 4 > buf.length)
|
|
100
|
-
throw new Error(
|
|
100
|
+
throw new Error("Not enough data: need 4 length bytes");
|
|
101
101
|
length = buf.readUInt32BE(offset);
|
|
102
102
|
offset += 4;
|
|
103
103
|
break;
|
package/dist/types.d.ts
CHANGED
|
@@ -27,11 +27,11 @@ export interface ValidationItem {
|
|
|
27
27
|
/** 0-based column */
|
|
28
28
|
endColumn: number;
|
|
29
29
|
/** Type of validation: "parameter", "return", "cast", "json-parse", "json-stringify" */
|
|
30
|
-
kind:
|
|
30
|
+
kind: "parameter" | "return" | "cast" | "json-parse" | "json-stringify";
|
|
31
31
|
/** Name of the item being validated (param name, "return value", or expression text) */
|
|
32
32
|
name: string;
|
|
33
33
|
/** Whether the item will be validated or skipped */
|
|
34
|
-
status:
|
|
34
|
+
status: "validated" | "skipped";
|
|
35
35
|
/** Human-readable type string */
|
|
36
36
|
typeString: string;
|
|
37
37
|
/** Reason for skipping (when status is "skipped") */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elliots/typical-compiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "TypeScript compiler powered by tsgo for typical validation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"compiler",
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"typescript": "^5.7.0"
|
|
27
27
|
},
|
|
28
28
|
"optionalDependencies": {
|
|
29
|
-
"@elliots/typical-compiler-darwin-arm64": "0.
|
|
30
|
-
"@elliots/typical-compiler-darwin-x64": "0.
|
|
31
|
-
"@elliots/typical-compiler-linux-arm64": "0.
|
|
32
|
-
"@elliots/typical-compiler-linux-x64": "0.
|
|
33
|
-
"@elliots/typical-compiler-win32-arm64": "0.
|
|
34
|
-
"@elliots/typical-compiler-win32-x64": "0.
|
|
29
|
+
"@elliots/typical-compiler-darwin-arm64": "0.3.0",
|
|
30
|
+
"@elliots/typical-compiler-darwin-x64": "0.3.0",
|
|
31
|
+
"@elliots/typical-compiler-linux-arm64": "0.3.0",
|
|
32
|
+
"@elliots/typical-compiler-linux-x64": "0.3.0",
|
|
33
|
+
"@elliots/typical-compiler-win32-arm64": "0.3.0",
|
|
34
|
+
"@elliots/typical-compiler-win32-x64": "0.3.0"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "npm run build:ts && npm run build:go",
|