@sdk-it/content-mapper 0.47.2 → 0.47.4
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/README.md +14 -3
- package/dist/register.d.ts +2 -0
- package/dist/register.d.ts.map +1 -0
- package/dist/register.js +54 -0
- package/dist/register.js.map +7 -0
- package/dist/render-worker.d.ts +2 -0
- package/dist/render-worker.d.ts.map +1 -0
- package/dist/render-worker.js +19 -0
- package/dist/render-worker.js.map +7 -0
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -8,7 +8,13 @@ import { type StoreEndpoints, createStore, store } from './store.openapi.yaml';
|
|
|
8
8
|
const pet = await store.request('GET /pets/{petId}', { petId: 'p1' });
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
It gives types only: TypeScript never emits JavaScript for a mapped file. To run the import, the host that runs the code loads the document too: the Vite plugin `sdkIt.imports()` from `@sdk-it/vite`, and
|
|
11
|
+
It gives types only: TypeScript never emits JavaScript for a mapped file. To run the import, the host that runs the code loads the document too: the Vite plugin `sdkIt.imports()` from `@sdk-it/vite`, and in Node this package's loader:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
node --import @sdk-it/content-mapper/register main.ts
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The loader renders a `file:` import that ends with `.openapi.yaml` or `.openapi` and gives Node the module as TypeScript, which Node strips. A document that does not render fails the import with `<file>:<line>:<column>: <message>`. Node's load hook is synchronous, so a worker thread renders while the hook waits (zukhruf #4966 will own that bridge). Each host renders the document with this package's `renderDocument`, so the types and the running code come from one render.
|
|
12
18
|
|
|
13
19
|
## Render a document in another host
|
|
14
20
|
|
|
@@ -31,11 +37,16 @@ if (rendered.kind === 'failed') {
|
|
|
31
37
|
{
|
|
32
38
|
"compilerOptions": { "module": "nodenext" },
|
|
33
39
|
"contentMappers": [
|
|
34
|
-
{
|
|
40
|
+
{
|
|
41
|
+
"package": "@sdk-it/content-mapper",
|
|
42
|
+
"extensions": [".openapi", ".openapi.yaml"],
|
|
43
|
+
},
|
|
35
44
|
],
|
|
36
45
|
}
|
|
37
46
|
```
|
|
38
47
|
|
|
48
|
+
[Import an OpenAPI document as a module](../../docs/recipes/import-openapi-document.md) sets up all three hosts: types, Vite and Node.
|
|
49
|
+
|
|
39
50
|
```bash
|
|
40
51
|
npx tsc --runExternalCode
|
|
41
52
|
```
|
|
@@ -87,5 +98,5 @@ No published content mapper reads OpenAPI (npm search, 2026-11-20). The protocol
|
|
|
87
98
|
|
|
88
99
|
- A change to a file that the document references through `$ref` does not re-type the import until the document itself changes: the protocol has no per-transform dependencies, and `watchedFiles` is read only when a project opens.
|
|
89
100
|
- The module holds every operation of the document, so a bundle ships the whole endpoint table.
|
|
90
|
-
- On
|
|
101
|
+
- A large document is slow to re-type. On App Store Connect (1,300 operations, a 7.6 MB JSON document that renders a 3.5 MB module), an importer opened in the editor gets its diagnostics after about 3 to 5 s, an edit to the document reaches them in about 2.5 to 5.5 s, and `tsc` checks the project in about 9 s. On Box (1.8 MB) an edit takes about 0.6 to 2.7 s (measured 2026-11-20).
|
|
91
102
|
- A string argument of `request('GET /pets/{petId}', …)` has no definition (TypeScript gives none for a string literal argument); the same key in a type position, `StoreEndpoints['GET /pets/{petId}']`, lands on the method key.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register.d.ts","sources":["../src/register.ts"],"names":[],"mappings":""}
|
package/dist/register.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { registerHooks } from "node:module";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import {
|
|
5
|
+
MessageChannel,
|
|
6
|
+
Worker,
|
|
7
|
+
receiveMessageOnPort
|
|
8
|
+
} from "node:worker_threads";
|
|
9
|
+
const signal = new Int32Array(new SharedArrayBuffer(4));
|
|
10
|
+
const { port1, port2 } = new MessageChannel();
|
|
11
|
+
const worker = new Worker(new URL("./render-worker.js", import.meta.url), {
|
|
12
|
+
// Without this the worker inherits --import and registers itself again.
|
|
13
|
+
execArgv: [],
|
|
14
|
+
workerData: { signal, port: port2 },
|
|
15
|
+
transferList: [port2]
|
|
16
|
+
});
|
|
17
|
+
worker.unref();
|
|
18
|
+
port1.unref();
|
|
19
|
+
const isDocument = (path) => path.endsWith(".openapi.yaml") || path.endsWith(".openapi");
|
|
20
|
+
function render(fileName) {
|
|
21
|
+
const content = readFileSync(fileName, "utf8");
|
|
22
|
+
Atomics.store(signal, 0, 0);
|
|
23
|
+
port1.postMessage({ fileName, content });
|
|
24
|
+
Atomics.wait(signal, 0, 0);
|
|
25
|
+
const reply = receiveMessageOnPort(port1)?.message;
|
|
26
|
+
if ("error" in reply) {
|
|
27
|
+
throw new Error(`${fileName}: ${reply.error}`);
|
|
28
|
+
}
|
|
29
|
+
const { rendered } = reply;
|
|
30
|
+
const [problem] = rendered.diagnostics;
|
|
31
|
+
if (problem) {
|
|
32
|
+
const before = content.slice(0, problem.range.start);
|
|
33
|
+
const line = before.split("\n").length;
|
|
34
|
+
const column = problem.range.start - (before.lastIndexOf("\n") + 1) + 1;
|
|
35
|
+
throw new SyntaxError(`${fileName}:${line}:${column}: ${problem.message}`);
|
|
36
|
+
}
|
|
37
|
+
if (rendered.kind === "failed") {
|
|
38
|
+
throw new Error(`${fileName}: the document did not render`);
|
|
39
|
+
}
|
|
40
|
+
return rendered.text;
|
|
41
|
+
}
|
|
42
|
+
registerHooks({
|
|
43
|
+
load(url, context, nextLoad) {
|
|
44
|
+
if (!url.startsWith("file:") || !isDocument(fileURLToPath(url))) {
|
|
45
|
+
return nextLoad(url, context);
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
format: "module-typescript",
|
|
49
|
+
source: render(fileURLToPath(url)),
|
|
50
|
+
shortCircuit: true
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
//# sourceMappingURL=register.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/register.ts"],
|
|
4
|
+
"sourcesContent": ["import { readFileSync } from 'node:fs';\nimport { registerHooks } from 'node:module';\nimport { fileURLToPath } from 'node:url';\nimport {\n MessageChannel,\n Worker,\n receiveMessageOnPort,\n} from 'node:worker_threads';\n\nimport type { RenderedDocument } from './render-document.js';\n\n// Node's load hook is synchronous and renderDocument is async, so a worker\n// renders while this thread waits (zukhruf #4966 will own this bridge, sdk-it #4967).\nconst signal = new Int32Array(new SharedArrayBuffer(4));\nconst { port1, port2 } = new MessageChannel();\nconst worker = new Worker(new URL('./render-worker.js', import.meta.url), {\n // Without this the worker inherits --import and registers itself again.\n execArgv: [],\n workerData: { signal, port: port2 },\n transferList: [port2],\n});\nworker.unref();\nport1.unref();\n\nconst isDocument = (path: string) =>\n path.endsWith('.openapi.yaml') || path.endsWith('.openapi');\n\nfunction render(fileName: string) {\n const content = readFileSync(fileName, 'utf8');\n Atomics.store(signal, 0, 0);\n port1.postMessage({ fileName, content });\n Atomics.wait(signal, 0, 0);\n const reply: { rendered: RenderedDocument } | { error: string } =\n receiveMessageOnPort(port1)?.message;\n if ('error' in reply) {\n throw new Error(`${fileName}: ${reply.error}`);\n }\n const { rendered } = reply;\n const [problem] = rendered.diagnostics;\n if (problem) {\n const before = content.slice(0, problem.range.start);\n const line = before.split('\\n').length;\n const column = problem.range.start - (before.lastIndexOf('\\n') + 1) + 1;\n throw new SyntaxError(`${fileName}:${line}:${column}: ${problem.message}`);\n }\n if (rendered.kind === 'failed') {\n throw new Error(`${fileName}: the document did not render`);\n }\n return rendered.text;\n}\n\nregisterHooks({\n load(url, context, nextLoad) {\n if (!url.startsWith('file:') || !isDocument(fileURLToPath(url))) {\n return nextLoad(url, context);\n }\n return {\n format: 'module-typescript',\n source: render(fileURLToPath(url)),\n shortCircuit: true,\n };\n },\n});\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,oBAAoB;AAC7B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AAAA,EAC5D;AACA,SAAO,SAAS;AAClB;AAEA,cAAc;AAAA,EACZ,KAAK,KAAK,SAAS,UAAU;AAC3B,QAAI,CAAC,IAAI,WAAW,OAAO,KAAK,CAAC,WAAW,cAAc,GAAG,CAAC,GAAG;AAC/D,aAAO,SAAS,KAAK,OAAO;AAAA,IAC9B;AACA,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,QAAQ,OAAO,cAAc,GAAG,CAAC;AAAA,MACjC,cAAc;AAAA,IAChB;AAAA,EACF;AACF,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render-worker.d.ts","sources":["../src/render-worker.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { workerData } from "node:worker_threads";
|
|
2
|
+
import { renderDocument } from "./render-document.js";
|
|
3
|
+
const { signal, port } = workerData;
|
|
4
|
+
async function answer({
|
|
5
|
+
fileName,
|
|
6
|
+
content
|
|
7
|
+
}) {
|
|
8
|
+
try {
|
|
9
|
+
port.postMessage({ rendered: await renderDocument(fileName, content) });
|
|
10
|
+
} catch (error) {
|
|
11
|
+
port.postMessage({
|
|
12
|
+
error: Error.isError(error) ? error.message : String(error)
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
Atomics.store(signal, 0, 1);
|
|
16
|
+
Atomics.notify(signal, 0);
|
|
17
|
+
}
|
|
18
|
+
port.on("message", (request) => void answer(request));
|
|
19
|
+
//# sourceMappingURL=render-worker.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/render-worker.ts"],
|
|
4
|
+
"sourcesContent": ["import { type MessagePort, workerData } from 'node:worker_threads';\n\nimport { renderDocument } from './render-document.js';\n\nconst { signal, port }: { signal: Int32Array; port: MessagePort } = workerData;\n\nasync function answer({\n fileName,\n content,\n}: {\n fileName: string;\n content: string;\n}) {\n try {\n port.postMessage({ rendered: await renderDocument(fileName, content) });\n } catch (error) {\n port.postMessage({\n error: Error.isError(error) ? error.message : String(error),\n });\n }\n Atomics.store(signal, 0, 1);\n Atomics.notify(signal, 0);\n}\n\nport.on('message', (request) => void answer(request));\n"],
|
|
5
|
+
"mappings": "AAAA,SAA2B,kBAAkB;AAE7C,SAAS,sBAAsB;AAE/B,MAAM,EAAE,QAAQ,KAAK,IAA+C;AAEpE,eAAe,OAAO;AAAA,EACpB;AAAA,EACA;AACF,GAGG;AACD,MAAI;AACF,SAAK,YAAY,EAAE,UAAU,MAAM,eAAe,UAAU,OAAO,EAAE,CAAC;AAAA,EACxE,SAAS,OAAO;AACd,SAAK,YAAY;AAAA,MACf,OAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,UAAU,OAAO,KAAK;AAAA,IAC5D,CAAC;AAAA,EACH;AACA,UAAQ,MAAM,QAAQ,GAAG,CAAC;AAC1B,UAAQ,OAAO,QAAQ,CAAC;AAC1B;AAEA,KAAK,GAAG,WAAW,CAAC,YAAY,KAAK,OAAO,OAAO,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdk-it/content-mapper",
|
|
3
|
-
"version": "0.47.
|
|
3
|
+
"version": "0.47.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
"types": "./dist/index.d.ts",
|
|
18
18
|
"import": "./dist/index.js",
|
|
19
19
|
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./register": {
|
|
22
|
+
"types": "./dist/register.d.ts",
|
|
23
|
+
"import": "./dist/register.js",
|
|
24
|
+
"default": "./dist/register.js"
|
|
20
25
|
}
|
|
21
26
|
},
|
|
22
27
|
"files": [
|
|
@@ -35,8 +40,8 @@
|
|
|
35
40
|
}
|
|
36
41
|
},
|
|
37
42
|
"dependencies": {
|
|
38
|
-
"@sdk-it/spec": "0.47.
|
|
39
|
-
"@sdk-it/typescript": "0.47.
|
|
43
|
+
"@sdk-it/spec": "0.47.4",
|
|
44
|
+
"@sdk-it/typescript": "0.47.4",
|
|
40
45
|
"ts-content-mapper": "^0.1.1",
|
|
41
46
|
"yaml": "^2.9.1"
|
|
42
47
|
},
|