@kubohiroya/turbowarp-http-server 0.1.0 → 0.2.1
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.ja.md +29 -13
- package/README.md +101 -42
- package/dist/cli.js +131 -17
- package/dist/cli.js.map +1 -1
- package/dist/compiler/adapters/cloudflare.d.ts +2 -0
- package/dist/compiler/adapters/cloudflare.js +477 -0
- package/dist/compiler/adapters/cloudflare.js.map +1 -0
- package/dist/compiler/adapters/firebase.d.ts +2 -0
- package/dist/compiler/adapters/firebase.js +489 -0
- package/dist/compiler/adapters/firebase.js.map +1 -0
- package/dist/compiler/adapters/index.d.ts +2 -0
- package/dist/compiler/adapters/index.js +3 -0
- package/dist/compiler/adapters/index.js.map +1 -0
- package/dist/compiler/binary/codec.d.ts +4 -0
- package/dist/compiler/binary/codec.js +45 -0
- package/dist/compiler/binary/codec.js.map +1 -0
- package/dist/compiler/binary/error.d.ts +3 -0
- package/dist/compiler/binary/error.js +21 -0
- package/dist/compiler/binary/error.js.map +1 -0
- package/dist/compiler/binary/index.d.ts +4 -0
- package/dist/compiler/binary/index.js +5 -0
- package/dist/compiler/binary/index.js.map +1 -0
- package/dist/compiler/binary/runtime.d.ts +18 -0
- package/dist/compiler/binary/runtime.js +123 -0
- package/dist/compiler/binary/runtime.js.map +1 -0
- package/dist/compiler/binary/types.d.ts +17 -0
- package/dist/compiler/binary/types.js +2 -0
- package/dist/compiler/binary/types.js.map +1 -0
- package/dist/compiler/feature-flags.d.ts +4 -0
- package/dist/compiler/feature-flags.js +4 -0
- package/dist/compiler/feature-flags.js.map +1 -0
- package/dist/compiler/index.d.ts +35 -0
- package/dist/compiler/index.js +84 -0
- package/dist/compiler/index.js.map +1 -0
- package/dist/compiler/ir-v2/canonical.d.ts +3 -0
- package/dist/compiler/ir-v2/canonical.js +50 -0
- package/dist/compiler/ir-v2/canonical.js.map +1 -0
- package/dist/compiler/ir-v2/index.d.ts +4 -0
- package/dist/compiler/ir-v2/index.js +5 -0
- package/dist/compiler/ir-v2/index.js.map +1 -0
- package/dist/compiler/ir-v2/parse.d.ts +10 -0
- package/dist/compiler/ir-v2/parse.js +753 -0
- package/dist/compiler/ir-v2/parse.js.map +1 -0
- package/dist/compiler/ir-v2/strict-json.d.ts +4 -0
- package/dist/compiler/ir-v2/strict-json.js +133 -0
- package/dist/compiler/ir-v2/strict-json.js.map +1 -0
- package/dist/compiler/ir-v2/types.d.ts +412 -0
- package/dist/compiler/ir-v2/types.js +84 -0
- package/dist/compiler/ir-v2/types.js.map +1 -0
- package/dist/compiler/kvs/lowering.d.ts +13 -0
- package/dist/compiler/kvs/lowering.js +77 -0
- package/dist/compiler/kvs/lowering.js.map +1 -0
- package/dist/compiler/manifest/error.d.ts +7 -0
- package/dist/compiler/manifest/error.js +9 -0
- package/dist/compiler/manifest/error.js.map +1 -0
- package/dist/compiler/manifest/index.d.ts +6 -0
- package/dist/compiler/manifest/index.js +7 -0
- package/dist/compiler/manifest/index.js.map +1 -0
- package/dist/compiler/manifest/operations.d.ts +12 -0
- package/dist/compiler/manifest/operations.js +60 -0
- package/dist/compiler/manifest/operations.js.map +1 -0
- package/dist/compiler/manifest/parse.d.ts +9 -0
- package/dist/compiler/manifest/parse.js +280 -0
- package/dist/compiler/manifest/parse.js.map +1 -0
- package/dist/compiler/manifest/registry.d.ts +3 -0
- package/dist/compiler/manifest/registry.js +48 -0
- package/dist/compiler/manifest/registry.js.map +1 -0
- package/dist/compiler/manifest/resolve.d.ts +14 -0
- package/dist/compiler/manifest/resolve.js +116 -0
- package/dist/compiler/manifest/resolve.js.map +1 -0
- package/dist/compiler/manifest/types.d.ts +95 -0
- package/dist/compiler/manifest/types.js +3 -0
- package/dist/compiler/manifest/types.js.map +1 -0
- package/dist/compiler/named-body/index.d.ts +1 -0
- package/dist/compiler/named-body/index.js +2 -0
- package/dist/compiler/named-body/index.js.map +1 -0
- package/dist/compiler/named-body/lowering.d.ts +18 -0
- package/dist/compiler/named-body/lowering.js +83 -0
- package/dist/compiler/named-body/lowering.js.map +1 -0
- package/dist/compiler/pipeline/compiler.d.ts +18 -0
- package/dist/compiler/pipeline/compiler.js +105 -0
- package/dist/compiler/pipeline/compiler.js.map +1 -0
- package/dist/compiler/pipeline/core-generator.d.ts +3 -0
- package/dist/compiler/pipeline/core-generator.js +483 -0
- package/dist/compiler/pipeline/core-generator.js.map +1 -0
- package/dist/compiler/pipeline/index.d.ts +5 -0
- package/dist/compiler/pipeline/index.js +6 -0
- package/dist/compiler/pipeline/index.js.map +1 -0
- package/dist/compiler/pipeline/registry.d.ts +7 -0
- package/dist/compiler/pipeline/registry.js +17 -0
- package/dist/compiler/pipeline/registry.js.map +1 -0
- package/dist/compiler/pipeline/requirements.d.ts +9 -0
- package/dist/compiler/pipeline/requirements.js +135 -0
- package/dist/compiler/pipeline/requirements.js.map +1 -0
- package/dist/compiler/pipeline/types.d.ts +65 -0
- package/dist/compiler/pipeline/types.js +2 -0
- package/dist/compiler/pipeline/types.js.map +1 -0
- package/dist/compiler/runtime/error.d.ts +6 -0
- package/dist/compiler/runtime/error.js +9 -0
- package/dist/compiler/runtime/error.js.map +1 -0
- package/dist/compiler/runtime/index.d.ts +1 -0
- package/dist/compiler/runtime/index.js +2 -0
- package/dist/compiler/runtime/index.js.map +1 -0
- package/dist/compiler/structured-data/index.d.ts +3 -0
- package/dist/compiler/structured-data/index.js +4 -0
- package/dist/compiler/structured-data/index.js.map +1 -0
- package/dist/compiler/structured-data/lowering.d.ts +30 -0
- package/dist/compiler/structured-data/lowering.js +202 -0
- package/dist/compiler/structured-data/lowering.js.map +1 -0
- package/dist/compiler/structured-data/path.d.ts +2 -0
- package/dist/compiler/structured-data/path.js +60 -0
- package/dist/compiler/structured-data/path.js.map +1 -0
- package/dist/compiler/structured-data/runtime.d.ts +20 -0
- package/dist/compiler/structured-data/runtime.js +210 -0
- package/dist/compiler/structured-data/runtime.js.map +1 -0
- package/dist/compiler/turbowarp-v2.d.ts +8 -0
- package/dist/compiler/turbowarp-v2.js +479 -0
- package/dist/compiler/turbowarp-v2.js.map +1 -0
- package/dist/compiler/validator/index.d.ts +4 -0
- package/dist/compiler/validator/index.js +5 -0
- package/dist/compiler/validator/index.js.map +1 -0
- package/dist/compiler/validator/ir-validator.d.ts +10 -0
- package/dist/compiler/validator/ir-validator.js +463 -0
- package/dist/compiler/validator/ir-validator.js.map +1 -0
- package/dist/compiler/validator/resource-lifetime.d.ts +18 -0
- package/dist/compiler/validator/resource-lifetime.js +106 -0
- package/dist/compiler/validator/resource-lifetime.js.map +1 -0
- package/dist/compiler/validator/turbowarp-subset.d.ts +3 -0
- package/dist/compiler/validator/turbowarp-subset.js +241 -0
- package/dist/compiler/validator/turbowarp-subset.js.map +1 -0
- package/dist/compiler/validator/types.d.ts +30 -0
- package/dist/compiler/validator/types.js +7 -0
- package/dist/compiler/validator/types.js.map +1 -0
- package/dist/config.d.ts +11 -0
- package/dist/config.js +12 -0
- package/dist/config.js.map +1 -0
- package/dist/digest-cli.js +0 -0
- package/dist/extension-manifest.json +102 -0
- package/dist/named-body.d.ts +57 -0
- package/dist/named-body.js +396 -0
- package/dist/named-body.js.map +1 -0
- package/dist/named-data-namespace.d.ts +3 -0
- package/dist/named-data-namespace.js +6 -0
- package/dist/named-data-namespace.js.map +1 -0
- package/dist/protocol.d.ts +13 -1
- package/dist/protocol.js +34 -0
- package/dist/protocol.js.map +1 -1
- package/dist/resource-named-body-provider.d.ts +8 -0
- package/dist/resource-named-body-provider.js +88 -0
- package/dist/resource-named-body-provider.js.map +1 -0
- package/dist/server.d.ts +10 -1
- package/dist/server.js +37 -7
- package/dist/server.js.map +1 -1
- package/dist/turbowarp-http-server.js +516 -1
- package/docs/binary-resource-semantics.ja.md +84 -0
- package/docs/cloudflare-worker-compiler.ja.md +90 -0
- package/docs/compiler-manifest-registry.ja.md +79 -0
- package/docs/compiler-platform-pipeline.ja.md +94 -0
- package/docs/deploy-ir-v2.ja.md +123 -0
- package/docs/fixed-input-editors.ja.md +23 -0
- package/docs/index.html +80 -0
- package/docs/ir-v2-conformance.ja.md +65 -0
- package/docs/named-response-body.ja.md +120 -0
- package/docs/platform-storage-adapters.ja.md +99 -0
- package/docs/server-executable-subset.ja.md +88 -0
- package/docs/structured-data-lowering.ja.md +63 -0
- package/examples/compiler/message-app.v2.ir.json +41 -0
- package/package.json +44 -20
- package/schemas/block-editor-fixed-inputs.schema.json +85 -0
- package/schemas/compiler-extension-manifest.schema.json +126 -0
- package/schemas/compiler-manifest-lock-v1.schema.json +48 -0
- package/schemas/deploy-ir-v2.schema.json +835 -0
- package/schemas/ir-v2-conformance-fixture.schema.json +44 -0
- package/src/block-definitions.json +76 -0
- package/src/cli.ts +136 -23
- package/src/compiler/adapters/cloudflare.ts +510 -0
- package/src/compiler/adapters/firebase.ts +538 -0
- package/src/compiler/adapters/index.ts +2 -0
- package/src/compiler/binary/codec.ts +61 -0
- package/src/compiler/binary/error.ts +25 -0
- package/src/compiler/binary/index.ts +4 -0
- package/src/compiler/binary/runtime.ts +133 -0
- package/src/compiler/binary/types.ts +26 -0
- package/src/compiler/feature-flags.ts +7 -0
- package/src/compiler/index.ts +114 -0
- package/src/compiler/ir-v2/canonical.ts +46 -0
- package/src/compiler/ir-v2/index.ts +9 -0
- package/src/compiler/ir-v2/parse.ts +921 -0
- package/src/compiler/ir-v2/strict-json.ts +129 -0
- package/src/compiler/ir-v2/types.ts +462 -0
- package/src/compiler/kvs/lowering.ts +88 -0
- package/src/compiler/manifest/error.ts +29 -0
- package/src/compiler/manifest/index.ts +17 -0
- package/src/compiler/manifest/operations.ts +175 -0
- package/src/compiler/manifest/parse.ts +390 -0
- package/src/compiler/manifest/registry.ts +74 -0
- package/src/compiler/manifest/resolve.ts +159 -0
- package/src/compiler/manifest/types.ts +112 -0
- package/src/compiler/named-body/index.ts +1 -0
- package/src/compiler/named-body/lowering.ts +186 -0
- package/src/compiler/pipeline/compiler.ts +138 -0
- package/src/compiler/pipeline/core-generator.ts +486 -0
- package/src/compiler/pipeline/index.ts +5 -0
- package/src/compiler/pipeline/registry.ts +18 -0
- package/src/compiler/pipeline/requirements.ts +139 -0
- package/src/compiler/pipeline/types.ts +59 -0
- package/src/compiler/runtime/error.ts +11 -0
- package/src/compiler/runtime/index.ts +1 -0
- package/src/compiler/structured-data/index.ts +3 -0
- package/src/compiler/structured-data/lowering.ts +314 -0
- package/src/compiler/structured-data/path.ts +53 -0
- package/src/compiler/structured-data/runtime.ts +221 -0
- package/src/compiler/turbowarp-v2.ts +610 -0
- package/src/compiler/validator/index.ts +4 -0
- package/src/compiler/validator/ir-validator.ts +812 -0
- package/src/compiler/validator/resource-lifetime.ts +153 -0
- package/src/compiler/validator/turbowarp-subset.ts +387 -0
- package/src/compiler/validator/types.ts +68 -0
- package/src/extension.ts +87 -1
- package/src/fixed-input-editors.ts +181 -0
- package/src/fixed-input-schema.ts +15 -0
- package/src/named-body.ts +495 -0
- package/src/named-data-namespace.ts +6 -0
- package/src/protocol.ts +50 -1
- package/src/resource-named-body-provider.ts +106 -0
- package/src/server.ts +88 -8
- package/docs/turbowarp-http-server.architecture.visual-check.1440x900.dark.png +0 -0
- package/docs/turbowarp-http-server.architecture.visual-check.1440x900.light.png +0 -0
- package/docs/turbowarp-http-server.architecture.visual-check.2048x1320.dark.png +0 -0
- package/docs/turbowarp-http-server.architecture.visual-check.2048x1320.light.png +0 -0
- package/docs/turbowarp-http-server.architecture.visual-check.html +0 -32
- package/docs/turbowarp-http-server.architecture.visual-check.json +0 -547
- package/schemas/extension-manifest.schema.json +0 -49
- package/src/extension-manifest.ts +0 -149
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type {BinaryLocatorV2, BinaryMetadataV2, BinaryRefDescriptorV2} from '../ir-v2/types.js';
|
|
2
|
+
import {IrRuntimeError} from '../runtime/error.js';
|
|
3
|
+
import {DEFAULT_MAX_BINARY_BYTES, type BinaryBodySource, type BinaryObjectStore} from './types.js';
|
|
4
|
+
import {validateBinaryLocator, validateBinaryMetadata} from './codec.js';
|
|
5
|
+
import {binaryError} from './error.js';
|
|
6
|
+
|
|
7
|
+
export class BinaryBodyHandle {
|
|
8
|
+
private consumed = false;
|
|
9
|
+
|
|
10
|
+
public constructor(private readonly source: BinaryBodySource) {}
|
|
11
|
+
|
|
12
|
+
public take(): BinaryBodySource {
|
|
13
|
+
if (this.consumed) binaryError('BINARY_BODY_CONSUMED', 'Binary body has already been consumed.');
|
|
14
|
+
this.consumed = true;
|
|
15
|
+
return this.source;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function effectiveBinaryLimit(
|
|
20
|
+
operationLimit: number,
|
|
21
|
+
compilerLimit = DEFAULT_MAX_BINARY_BYTES,
|
|
22
|
+
targetLimit = Number.MAX_SAFE_INTEGER
|
|
23
|
+
): number {
|
|
24
|
+
for (const limit of [operationLimit, compilerLimit, targetLimit]) {
|
|
25
|
+
if (!Number.isSafeInteger(limit) || limit < 1) binaryError('BINARY_INVALID_REF', 'Binary limit is invalid.');
|
|
26
|
+
}
|
|
27
|
+
return Math.min(operationLimit, compilerLimit, targetLimit);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function collectBinaryBody(source: BinaryBodySource, maxBytes: number): Promise<Uint8Array> {
|
|
31
|
+
if (source.size !== undefined && (!Number.isSafeInteger(source.size) || source.size < 0)) {
|
|
32
|
+
binaryError('BINARY_INVALID_REF', 'Binary body size is invalid.');
|
|
33
|
+
}
|
|
34
|
+
if (source.size !== undefined && source.size > maxBytes) {
|
|
35
|
+
binaryError('BINARY_TOO_LARGE', 'Binary body exceeds the configured limit.');
|
|
36
|
+
}
|
|
37
|
+
const chunks: Uint8Array[] = [];
|
|
38
|
+
let total = 0;
|
|
39
|
+
const iterator = source.chunks[Symbol.asyncIterator]();
|
|
40
|
+
try {
|
|
41
|
+
while (true) {
|
|
42
|
+
const item = await iterator.next();
|
|
43
|
+
if (item.done) break;
|
|
44
|
+
total += item.value.byteLength;
|
|
45
|
+
if (total > maxBytes) {
|
|
46
|
+
await iterator.return?.();
|
|
47
|
+
binaryError('BINARY_TOO_LARGE', 'Binary body exceeds the configured limit.');
|
|
48
|
+
}
|
|
49
|
+
chunks.push(item.value.slice());
|
|
50
|
+
}
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (error instanceof IrRuntimeError) throw error;
|
|
53
|
+
binaryError('BINARY_STORAGE_FAILURE', 'Binary stream failed.');
|
|
54
|
+
}
|
|
55
|
+
const result = new Uint8Array(total);
|
|
56
|
+
let offset = 0;
|
|
57
|
+
for (const chunk of chunks) {
|
|
58
|
+
result.set(chunk, offset);
|
|
59
|
+
offset += chunk.byteLength;
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export class InMemoryBinaryObjectStore implements BinaryObjectStore {
|
|
65
|
+
private readonly objects = new Map<string, {bytes: Uint8Array; ref: BinaryRefDescriptorV2}>();
|
|
66
|
+
private revision = 0;
|
|
67
|
+
|
|
68
|
+
public async resolve(locator: BinaryLocatorV2): Promise<BinaryRefDescriptorV2 | null> {
|
|
69
|
+
return this.objects.get(locatorKey(validateBinaryLocator(locator)))?.ref ?? null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public async get(ref: BinaryRefDescriptorV2): Promise<BinaryBodySource | null> {
|
|
73
|
+
validateBinaryLocator(ref);
|
|
74
|
+
validateBinaryMetadata(ref);
|
|
75
|
+
const object = this.objects.get(locatorKey(ref));
|
|
76
|
+
if (object === undefined || (ref.revision !== undefined && object.ref.revision !== ref.revision)) return null;
|
|
77
|
+
const bytes = object.bytes.slice();
|
|
78
|
+
const source: BinaryBodySource = {chunks: chunksOf(bytes), size: bytes.byteLength};
|
|
79
|
+
return object.ref.contentType === undefined ? source : {...source, contentType: object.ref.contentType};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public async put(
|
|
83
|
+
locator: BinaryLocatorV2,
|
|
84
|
+
source: BinaryBodySource,
|
|
85
|
+
metadata: BinaryMetadataV2,
|
|
86
|
+
maxBytes: number
|
|
87
|
+
): Promise<BinaryRefDescriptorV2> {
|
|
88
|
+
validateBinaryLocator(locator);
|
|
89
|
+
validateBinaryMetadata(metadata);
|
|
90
|
+
const bytes = await collectBinaryBody(source, maxBytes);
|
|
91
|
+
if (metadata.size !== undefined && metadata.size !== bytes.byteLength) {
|
|
92
|
+
binaryError('BINARY_INTEGRITY_MISMATCH', 'Binary size does not match.');
|
|
93
|
+
}
|
|
94
|
+
const integrity = await sha256Integrity(bytes);
|
|
95
|
+
if (metadata.integrity !== undefined && metadata.integrity !== integrity) {
|
|
96
|
+
binaryError('BINARY_INTEGRITY_MISMATCH', 'Binary integrity does not match.');
|
|
97
|
+
}
|
|
98
|
+
this.revision += 1;
|
|
99
|
+
const ref: BinaryRefDescriptorV2 = {
|
|
100
|
+
...locator,
|
|
101
|
+
size: bytes.byteLength,
|
|
102
|
+
integrity,
|
|
103
|
+
revision: String(this.revision),
|
|
104
|
+
...(metadata.contentType === undefined ? {} : {contentType: metadata.contentType})
|
|
105
|
+
};
|
|
106
|
+
this.objects.set(locatorKey(locator), {bytes: bytes.slice(), ref});
|
|
107
|
+
return ref;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public async delete(target: BinaryLocatorV2 | BinaryRefDescriptorV2): Promise<boolean> {
|
|
111
|
+
validateBinaryLocator(target);
|
|
112
|
+
const key = locatorKey(target);
|
|
113
|
+
const current = this.objects.get(key);
|
|
114
|
+
if (current === undefined) return false;
|
|
115
|
+
if ('revision' in target && target.revision !== undefined && target.revision !== current.ref.revision) return false;
|
|
116
|
+
return this.objects.delete(key);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function sha256Integrity(bytes: Uint8Array): Promise<`sha256:${string}`> {
|
|
121
|
+
const copy = new Uint8Array(bytes.byteLength);
|
|
122
|
+
copy.set(bytes);
|
|
123
|
+
const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', copy.buffer));
|
|
124
|
+
return `sha256:${Array.from(digest, (byte) => byte.toString(16).padStart(2, '0')).join('')}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function locatorKey(locator: BinaryLocatorV2): string {
|
|
128
|
+
return `${locator.namespace}\u0000${locator.key}`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function* chunksOf(bytes: Uint8Array): AsyncIterable<Uint8Array> {
|
|
132
|
+
yield bytes;
|
|
133
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type {BinaryLocatorV2, BinaryMetadataV2, BinaryRefDescriptorV2} from '../ir-v2/types.js';
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_MAX_BINARY_BYTES = 16 * 1024 * 1024;
|
|
4
|
+
|
|
5
|
+
export interface BinaryBodySource {
|
|
6
|
+
readonly chunks: AsyncIterable<Uint8Array>;
|
|
7
|
+
readonly contentType?: string;
|
|
8
|
+
readonly size?: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface BinaryObjectStore {
|
|
12
|
+
resolve(locator: BinaryLocatorV2): Promise<BinaryRefDescriptorV2 | null>;
|
|
13
|
+
get(ref: BinaryRefDescriptorV2): Promise<BinaryBodySource | null>;
|
|
14
|
+
put(
|
|
15
|
+
locator: BinaryLocatorV2,
|
|
16
|
+
source: BinaryBodySource,
|
|
17
|
+
metadata: BinaryMetadataV2,
|
|
18
|
+
maxBytes: number
|
|
19
|
+
): Promise<BinaryRefDescriptorV2>;
|
|
20
|
+
delete(target: BinaryLocatorV2 | BinaryRefDescriptorV2): Promise<boolean>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface BinaryTargetLimits {
|
|
24
|
+
maxBinaryBytes: number;
|
|
25
|
+
supportsStreaming: boolean;
|
|
26
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {mkdir, readFile, readdir, writeFile} from 'node:fs/promises';
|
|
2
|
+
import {dirname, extname, join, resolve} from 'node:path';
|
|
3
|
+
import {compileTurboWarpProjectV2} from './turbowarp-v2.js';
|
|
4
|
+
import {resolveCompilerManifestLock} from './manifest/resolve.js';
|
|
5
|
+
import {parseDeployIrV2Json, type CompilerDiagnosticV2, type DeployIrV2} from './ir-v2/index.js';
|
|
6
|
+
import {compileDeployIrV2, type PipelineDiagnostic} from './pipeline/index.js';
|
|
7
|
+
|
|
8
|
+
export * from './feature-flags.js';
|
|
9
|
+
export * from './adapters/index.js';
|
|
10
|
+
export * from './binary/index.js';
|
|
11
|
+
export * from './ir-v2/index.js';
|
|
12
|
+
export * from './manifest/index.js';
|
|
13
|
+
export * from './named-body/index.js';
|
|
14
|
+
export * from './pipeline/index.js';
|
|
15
|
+
export * from './runtime/index.js';
|
|
16
|
+
export * from './structured-data/index.js';
|
|
17
|
+
export * from './turbowarp-v2.js';
|
|
18
|
+
export * from './validator/index.js';
|
|
19
|
+
|
|
20
|
+
export type CompilerInputFormat = 'turbowarp-json' | 'ir';
|
|
21
|
+
|
|
22
|
+
export interface CompileOptions {
|
|
23
|
+
input: string;
|
|
24
|
+
output: string;
|
|
25
|
+
format: CompilerInputFormat;
|
|
26
|
+
force?: boolean;
|
|
27
|
+
target: string;
|
|
28
|
+
targetConfig?: string;
|
|
29
|
+
manifestLock?: string;
|
|
30
|
+
namedResponseBody?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface CompilerOutput {
|
|
34
|
+
ir: DeployIrV2;
|
|
35
|
+
diagnostics: Array<CompilerDiagnosticV2 | PipelineDiagnostic>;
|
|
36
|
+
files: string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function compileToDirectory(options: CompileOptions): Promise<CompilerOutput> {
|
|
40
|
+
if (extname(options.input).toLowerCase() === '.sb3') {
|
|
41
|
+
throw new Error('Direct .sb3 input is not supported yet. Export or extract project.json first.');
|
|
42
|
+
}
|
|
43
|
+
if (options.manifestLock !== undefined && options.format !== 'turbowarp-json') {
|
|
44
|
+
throw new Error('manifestLock requires TurboWarp JSON input.');
|
|
45
|
+
}
|
|
46
|
+
const inputPath = resolve(options.input);
|
|
47
|
+
const outputPath = resolve(options.output);
|
|
48
|
+
const source = await readFile(inputPath, 'utf8');
|
|
49
|
+
const registry =
|
|
50
|
+
options.format === 'turbowarp-json' && options.manifestLock !== undefined
|
|
51
|
+
? (await resolveCompilerManifestLock(options.manifestLock)).registry
|
|
52
|
+
: undefined;
|
|
53
|
+
const frontend =
|
|
54
|
+
options.format === 'ir'
|
|
55
|
+
? {ir: parseDeployIrV2Json(source), diagnostics: []}
|
|
56
|
+
: compileTurboWarpProjectV2(JSON.parse(source) as unknown, registry);
|
|
57
|
+
const frontendErrors = frontend.diagnostics.filter(({severity}) => severity === 'error');
|
|
58
|
+
if (frontendErrors.length > 0) throw new CompilerDiagnosticsError(frontendErrors);
|
|
59
|
+
const ir = frontend.ir;
|
|
60
|
+
const targetConfig =
|
|
61
|
+
options.targetConfig === undefined
|
|
62
|
+
? undefined
|
|
63
|
+
: (JSON.parse(await readFile(resolve(options.targetConfig), 'utf8')) as unknown);
|
|
64
|
+
const result = compileDeployIrV2(ir, {
|
|
65
|
+
target: options.target,
|
|
66
|
+
targetConfig,
|
|
67
|
+
featureFlags: {namedResponseBody: options.namedResponseBody === true}
|
|
68
|
+
});
|
|
69
|
+
if (!result.ok) throw new CompilerDiagnosticsError(result.diagnostics);
|
|
70
|
+
await writeGeneratedFiles(outputPath, result.files, options.force === true);
|
|
71
|
+
return {ir, diagnostics: frontend.diagnostics, files: Object.keys(result.files).sort()};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function writeGeneratedFiles(
|
|
75
|
+
output: string,
|
|
76
|
+
files: Readonly<Record<string, string>>,
|
|
77
|
+
force: boolean
|
|
78
|
+
): Promise<void> {
|
|
79
|
+
await mkdir(output, {recursive: true});
|
|
80
|
+
const existing = await readdir(output);
|
|
81
|
+
if (existing.length > 0 && !force) {
|
|
82
|
+
throw new Error(`Output directory is not empty: ${output}. Use --force to replace generated files.`);
|
|
83
|
+
}
|
|
84
|
+
for (const [relativePath, contents] of Object.entries(files)) {
|
|
85
|
+
const destination = join(output, relativePath);
|
|
86
|
+
await mkdir(dirname(destination), {recursive: true});
|
|
87
|
+
await writeFile(destination, contents, 'utf8');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
type AnyCompilerDiagnostic = CompilerDiagnosticV2 | PipelineDiagnostic;
|
|
92
|
+
|
|
93
|
+
export class CompilerDiagnosticsError extends Error {
|
|
94
|
+
public constructor(public readonly diagnostics: AnyCompilerDiagnostic[]) {
|
|
95
|
+
super(diagnostics.map(formatDiagnostic).join('\n'));
|
|
96
|
+
this.name = 'CompilerDiagnosticsError';
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function formatDiagnostic(diagnostic: AnyCompilerDiagnostic): string {
|
|
101
|
+
const location = diagnosticLocation(diagnostic);
|
|
102
|
+
return `[${diagnostic.code}]${location ? ` ${location}` : ''} ${diagnostic.message}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function diagnosticLocation(diagnostic: AnyCompilerDiagnostic): string {
|
|
106
|
+
if ('reason' in diagnostic) {
|
|
107
|
+
return [diagnostic.targetId, diagnostic.routeId, diagnostic.sourceRef?.blockId].filter(Boolean).join(':');
|
|
108
|
+
}
|
|
109
|
+
if (diagnostic.code.startsWith('TW2_')) {
|
|
110
|
+
const sourceRef = (diagnostic as CompilerDiagnosticV2).sourceRef;
|
|
111
|
+
return [sourceRef?.targetName, sourceRef?.blockId].filter(Boolean).join(':');
|
|
112
|
+
}
|
|
113
|
+
return '';
|
|
114
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type {DeployIrV2, JsonValue} from './types.js';
|
|
2
|
+
|
|
3
|
+
export function canonicalizeDeployIrV2(ir: DeployIrV2): string {
|
|
4
|
+
return canonicalizeJson(ir as unknown as JsonValue);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function canonicalizeJson(value: JsonValue): string {
|
|
8
|
+
if (value === null) return 'null';
|
|
9
|
+
if (typeof value === 'boolean') return JSON.stringify(value);
|
|
10
|
+
if (typeof value === 'string') {
|
|
11
|
+
assertWellFormedUnicode(value);
|
|
12
|
+
return JSON.stringify(value);
|
|
13
|
+
}
|
|
14
|
+
if (typeof value === 'number') {
|
|
15
|
+
if (!Number.isFinite(value)) throw new Error('JCS numbers must be finite.');
|
|
16
|
+
return JSON.stringify(Object.is(value, -0) ? 0 : value);
|
|
17
|
+
}
|
|
18
|
+
if (Array.isArray(value)) return `[${value.map(canonicalizeJson).join(',')}]`;
|
|
19
|
+
const keys = Object.keys(value);
|
|
20
|
+
for (const key of keys) assertWellFormedUnicode(key);
|
|
21
|
+
return `{${keys
|
|
22
|
+
.sort(compareUtf16)
|
|
23
|
+
.map((key) => `${JSON.stringify(key)}:${canonicalizeJson(value[key]!)}`)
|
|
24
|
+
.join(',')}}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function assertWellFormedUnicode(value: string): void {
|
|
28
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
29
|
+
const code = value.charCodeAt(index);
|
|
30
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
31
|
+
const next = value.charCodeAt(index + 1);
|
|
32
|
+
if (!(next >= 0xdc00 && next <= 0xdfff)) {
|
|
33
|
+
throw new Error('JCS strings must not contain lone surrogates.');
|
|
34
|
+
}
|
|
35
|
+
index += 1;
|
|
36
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) {
|
|
37
|
+
throw new Error('JCS strings must not contain lone surrogates.');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function compareUtf16(left: string, right: string): number {
|
|
43
|
+
if (left < right) return -1;
|
|
44
|
+
if (left > right) return 1;
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export {canonicalizeDeployIrV2, canonicalizeJson} from './canonical.js';
|
|
2
|
+
export {
|
|
3
|
+
DeployIrV2ParseError,
|
|
4
|
+
parseDeployIrV2,
|
|
5
|
+
parseDeployIrV2Json,
|
|
6
|
+
type DeployIrV2ParseErrorCode
|
|
7
|
+
} from './parse.js';
|
|
8
|
+
export {parseJsonWithoutDuplicateKeys} from './strict-json.js';
|
|
9
|
+
export * from './types.js';
|