Skip to content

Commit 1feec11

Browse files
raman325claude
andcommitted
fix: use readFile instead of import attributes for JSON import
Avoids Node version compatibility issues with `with { type: "json" }` (Node 22+) vs `assert { type: "json" }` (deprecated, errors on Node 22+). Uses readFile with caching for the 189KB schema payload. Adds a copy step to the build so the generated JSON lands in dist-esm/. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d5eb561 commit 1feec11

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"test": "npm run generate:schema && prettier --check src && tsc --noEmit && npm run lint && tsx src/test/integration.ts",
3939
"generate:schema": "ts-json-schema-generator --path src/lib/incoming_message.ts --type IncomingMessage --tsconfig tsconfig.json --no-type-check --out src/lib/generated/incoming_message_schema.json",
4040
"prebuild": "npm run generate:schema",
41-
"build": "tsc -p .",
41+
"build": "tsc -p . && mkdir -p dist-esm/lib/generated && cp src/lib/generated/incoming_message_schema.json dist-esm/lib/generated/",
4242
"postbuild": "esm2cjs --in dist-esm --out dist-cjs -l error -t node20",
4343
"prepare": "npm run build",
4444
"prepublishOnly": "rm -rf dist-* && npm run build"

src/lib/introspect/message_handler.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import { readFile } from "node:fs/promises";
2+
import { dirname, join } from "node:path";
3+
import { fileURLToPath } from "node:url";
14
import { UnknownCommandError } from "../error.js";
2-
import incomingMessageSchema from "../generated/incoming_message_schema.json" with { type: "json" };
35
import { MessageHandler } from "../message_handler.js";
46
import { IntrospectCommand } from "./command.js";
57
import { IncomingMessageIntrospect } from "./incoming_message.js";
68
import { IntrospectResultTypes } from "./outgoing_message.js";
79

10+
const schemaPath = join(
11+
dirname(fileURLToPath(import.meta.url)),
12+
"../generated/incoming_message_schema.json",
13+
);
14+
15+
let cachedSchema: Record<string, unknown> | undefined;
16+
817
export class IntrospectMessageHandler implements MessageHandler {
918
async handle(
1019
message: IncomingMessageIntrospect,
@@ -13,7 +22,10 @@ export class IntrospectMessageHandler implements MessageHandler {
1322

1423
switch (message.command) {
1524
case IntrospectCommand.commands:
16-
return incomingMessageSchema;
25+
if (!cachedSchema) {
26+
cachedSchema = JSON.parse(await readFile(schemaPath, "utf-8"));
27+
}
28+
return cachedSchema!;
1729
default:
1830
throw new UnknownCommandError(command);
1931
}

0 commit comments

Comments
 (0)