@kubohiroya/turbowarp-http-server 0.2.0 → 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/README.ja.md +32 -14
- package/README.md +104 -43
- package/assets/block-icon.svg +1 -0
- 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 +1614 -1099
- 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 +86 -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/docs/turbowarp-http-server.architecture.html +4 -0
- package/examples/compiler/message-app.v2.ir.json +41 -0
- package/package.json +45 -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/config.ts +1 -1
- 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
package/src/extension.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {fixedInputRegistration} from './fixed-input-editors.js';
|
|
1
2
|
import {extensionConfig} from './config';
|
|
2
3
|
import definitions from './block-definitions.json';
|
|
3
4
|
import {
|
|
@@ -75,6 +76,9 @@ const HTTP_REQUEST_HAT_OPCODE = `${extensionConfig.id}_whenHttpRequestReceived`;
|
|
|
75
76
|
const URL_ATTRIBUTES = new Set(['action', 'cite', 'formaction', 'href', 'poster', 'src', 'xlink:href']);
|
|
76
77
|
const URL_LIST_ATTRIBUTES = new Set(['srcset']);
|
|
77
78
|
const SAFE_URL_SCHEMES = new Set(['http', 'https', 'mailto', 'tel']);
|
|
79
|
+
const NAMED_BODY_KINDS = new Set(['structured', 'document', 'binary', 'asset']);
|
|
80
|
+
const NAMED_BODY_SCOPES = new Set(['target', 'project']);
|
|
81
|
+
const NAMED_BODY_REPRESENTATIONS = new Set(['json', 'yaml', 'html', 'markdown', 'raw']);
|
|
78
82
|
|
|
79
83
|
type HtmlNode = HtmlElementNode | HtmlTextNode;
|
|
80
84
|
|
|
@@ -100,9 +104,12 @@ interface ResponseBuilder {
|
|
|
100
104
|
interface RequestContext {
|
|
101
105
|
request: BridgeRequestMessage;
|
|
102
106
|
response: ResponseBuilder;
|
|
107
|
+
handlerVariables: Map<string, string | number>;
|
|
103
108
|
}
|
|
104
109
|
|
|
105
110
|
export class TurboWarpHttpServerExtension implements TurboWarpExtension {
|
|
111
|
+
private readonly fixedInputs = fixedInputRegistration(typeof Scratch === 'undefined' ? undefined : Scratch);
|
|
112
|
+
|
|
106
113
|
private serverUrl = DEFAULT_SERVER_URL;
|
|
107
114
|
private socket: WebSocket | null = null;
|
|
108
115
|
private lastReceivedMessage = '';
|
|
@@ -113,7 +120,10 @@ export class TurboWarpHttpServerExtension implements TurboWarpExtension {
|
|
|
113
120
|
private readonly requestContexts = new Map<string, RequestContext>();
|
|
114
121
|
private currentRequestContextId = '';
|
|
115
122
|
|
|
123
|
+
public dispose(): void {this.fixedInputs.dispose();}
|
|
124
|
+
|
|
116
125
|
public getInfo(): Record<string, unknown> {
|
|
126
|
+
this.fixedInputs.ensure(blockDefinitions.map(block => `${extensionConfig.id}_${block.opcode}`));
|
|
117
127
|
return {
|
|
118
128
|
id: extensionConfig.id,
|
|
119
129
|
name: Scratch.translate(definitions.extensionName),
|
|
@@ -216,6 +226,40 @@ export class TurboWarpHttpServerExtension implements TurboWarpExtension {
|
|
|
216
226
|
return this.currentContext()?.request.clientAddress ?? '';
|
|
217
227
|
}
|
|
218
228
|
|
|
229
|
+
public setHandlerVariable(args: {NAME: unknown; VALUE: unknown}): void {
|
|
230
|
+
const context = this.mutableCurrentContext();
|
|
231
|
+
if (!context) return;
|
|
232
|
+
context.handlerVariables.set(Scratch.Cast.toString(args.NAME), Scratch.Cast.toString(args.VALUE));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
public changeHandlerVariable(args: {NAME: unknown; AMOUNT: unknown}): void {
|
|
236
|
+
const context = this.mutableCurrentContext();
|
|
237
|
+
if (!context) return;
|
|
238
|
+
const name = Scratch.Cast.toString(args.NAME);
|
|
239
|
+
const current = Scratch.Cast.toNumber(context.handlerVariables.get(name) ?? '');
|
|
240
|
+
context.handlerVariables.set(name, current + Scratch.Cast.toNumber(args.AMOUNT));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
public handlerVariable(args: {NAME: unknown}): string | number {
|
|
244
|
+
return this.currentContext()?.handlerVariables.get(Scratch.Cast.toString(args.NAME)) ?? '';
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
public handlerVariableExists(args: {NAME: unknown}): boolean {
|
|
248
|
+
return this.currentContext()?.handlerVariables.has(Scratch.Cast.toString(args.NAME)) ?? false;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
public deleteHandlerVariable(args: {NAME: unknown}): void {
|
|
252
|
+
this.mutableCurrentContext()?.handlerVariables.delete(Scratch.Cast.toString(args.NAME));
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
public clearHandlerVariables(): void {
|
|
256
|
+
this.mutableCurrentContext()?.handlerVariables.clear();
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
public listHandlerVariables(): string {
|
|
260
|
+
return Array.from(this.currentContext()?.handlerVariables.keys() ?? []).join(',');
|
|
261
|
+
}
|
|
262
|
+
|
|
219
263
|
public currentAuthType(): string {
|
|
220
264
|
return this.currentContext()?.request.auth?.type ?? '';
|
|
221
265
|
}
|
|
@@ -307,6 +351,47 @@ export class TurboWarpHttpServerExtension implements TurboWarpExtension {
|
|
|
307
351
|
this.sendResponse({BODY: args.BODY});
|
|
308
352
|
}
|
|
309
353
|
|
|
354
|
+
public respondWithNamedBody(args: {
|
|
355
|
+
NAMESPACE: unknown;
|
|
356
|
+
NAME: unknown;
|
|
357
|
+
KIND: unknown;
|
|
358
|
+
SCOPE: unknown;
|
|
359
|
+
TARGET_ID: unknown;
|
|
360
|
+
REPRESENTATION: unknown;
|
|
361
|
+
MAX_BYTES: unknown;
|
|
362
|
+
}): void {
|
|
363
|
+
const context = this.mutableCurrentContext();
|
|
364
|
+
if (!context) return;
|
|
365
|
+
const kind = Scratch.Cast.toString(args.KIND).toLowerCase();
|
|
366
|
+
const scope = Scratch.Cast.toString(args.SCOPE).toLowerCase();
|
|
367
|
+
const representation = Scratch.Cast.toString(args.REPRESENTATION).toLowerCase();
|
|
368
|
+
const maxBytes = Math.trunc(Scratch.Cast.toNumber(args.MAX_BYTES));
|
|
369
|
+
if (
|
|
370
|
+
!NAMED_BODY_KINDS.has(kind) ||
|
|
371
|
+
!NAMED_BODY_SCOPES.has(scope) ||
|
|
372
|
+
!NAMED_BODY_REPRESENTATIONS.has(representation) ||
|
|
373
|
+
!Number.isSafeInteger(maxBytes) ||
|
|
374
|
+
maxBytes < 1
|
|
375
|
+
) {
|
|
376
|
+
context.response.body = {kind: 'unsupported', reason: 'invalid_named_body'};
|
|
377
|
+
this.completeResponse(context);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
context.response.body = {
|
|
381
|
+
kind: 'named',
|
|
382
|
+
reference: {
|
|
383
|
+
namespace: Scratch.Cast.toString(args.NAMESPACE),
|
|
384
|
+
name: Scratch.Cast.toString(args.NAME),
|
|
385
|
+
kind: kind as 'structured' | 'document' | 'binary' | 'asset',
|
|
386
|
+
scope: scope as 'target' | 'project'
|
|
387
|
+
},
|
|
388
|
+
representation: representation as 'json' | 'yaml' | 'html' | 'markdown' | 'raw',
|
|
389
|
+
...(scope === 'target' ? {targetId: Scratch.Cast.toString(args.TARGET_ID)} : {}),
|
|
390
|
+
maxBytes
|
|
391
|
+
};
|
|
392
|
+
this.completeResponse(context);
|
|
393
|
+
}
|
|
394
|
+
|
|
310
395
|
public receiveBridgeRequestForTest(request: BridgeRequestMessage): void {
|
|
311
396
|
this.acceptBridgeRequest(request);
|
|
312
397
|
}
|
|
@@ -455,7 +540,8 @@ export class TurboWarpHttpServerExtension implements TurboWarpExtension {
|
|
|
455
540
|
headers: {},
|
|
456
541
|
body: {kind: 'empty'},
|
|
457
542
|
completed: false
|
|
458
|
-
}
|
|
543
|
+
},
|
|
544
|
+
handlerVariables: new Map()
|
|
459
545
|
});
|
|
460
546
|
this.currentRequestContextId = request.id;
|
|
461
547
|
this.startRequestHat();
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import {parseFixedNumberInput} from '@kubohiroya/turbowarp-extension-data-editor/schema';
|
|
2
|
+
import type {FixedInputBinding, FixedInputEditorOptions, JsonValue, LiteralValue} from '@kubohiroya/turbowarp-extension-data-editor';
|
|
3
|
+
import {fixedInputBundle} from './fixed-input-schema.js';
|
|
4
|
+
const extensionId = "kubohiroyaturbowarphttpserver";
|
|
5
|
+
const repository = "turbowarp-http-server";
|
|
6
|
+
interface Field {opcode: string; argument: string; control: 'number' | 'enum'; choices: readonly string[];
|
|
7
|
+
normalization: string; shape: string; maxBytes: boolean}
|
|
8
|
+
const fields: readonly Field[] = [
|
|
9
|
+
{
|
|
10
|
+
"opcode": "changeHandlerVariable",
|
|
11
|
+
"argument": "AMOUNT",
|
|
12
|
+
"control": "number",
|
|
13
|
+
"choices": [],
|
|
14
|
+
"normalization": "identity",
|
|
15
|
+
"shape": "math_number.NUM",
|
|
16
|
+
"maxBytes": false
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"opcode": "setHttpStatus",
|
|
20
|
+
"argument": "STATUS",
|
|
21
|
+
"control": "number",
|
|
22
|
+
"choices": [],
|
|
23
|
+
"normalization": "identity",
|
|
24
|
+
"shape": "math_number.NUM",
|
|
25
|
+
"maxBytes": false
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"opcode": "respondWithNamedBody",
|
|
29
|
+
"argument": "KIND",
|
|
30
|
+
"control": "enum",
|
|
31
|
+
"choices": [
|
|
32
|
+
"structured",
|
|
33
|
+
"document",
|
|
34
|
+
"binary",
|
|
35
|
+
"asset"
|
|
36
|
+
],
|
|
37
|
+
"normalization": "lowercase",
|
|
38
|
+
"shape": "text.TEXT",
|
|
39
|
+
"maxBytes": false
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"opcode": "respondWithNamedBody",
|
|
43
|
+
"argument": "SCOPE",
|
|
44
|
+
"control": "enum",
|
|
45
|
+
"choices": [
|
|
46
|
+
"target",
|
|
47
|
+
"project"
|
|
48
|
+
],
|
|
49
|
+
"normalization": "lowercase",
|
|
50
|
+
"shape": "text.TEXT",
|
|
51
|
+
"maxBytes": false
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"opcode": "respondWithNamedBody",
|
|
55
|
+
"argument": "REPRESENTATION",
|
|
56
|
+
"control": "enum",
|
|
57
|
+
"choices": [
|
|
58
|
+
"json",
|
|
59
|
+
"yaml",
|
|
60
|
+
"html",
|
|
61
|
+
"markdown",
|
|
62
|
+
"raw"
|
|
63
|
+
],
|
|
64
|
+
"normalization": "lowercase",
|
|
65
|
+
"shape": "text.TEXT",
|
|
66
|
+
"maxBytes": false
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"opcode": "respondWithNamedBody",
|
|
70
|
+
"argument": "MAX_BYTES",
|
|
71
|
+
"control": "number",
|
|
72
|
+
"choices": [],
|
|
73
|
+
"normalization": "identity",
|
|
74
|
+
"shape": "math_number.NUM",
|
|
75
|
+
"maxBytes": true
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
"opcode": "markdownHeading",
|
|
79
|
+
"argument": "LEVEL",
|
|
80
|
+
"control": "number",
|
|
81
|
+
"choices": [],
|
|
82
|
+
"normalization": "identity",
|
|
83
|
+
"shape": "math_number.NUM",
|
|
84
|
+
"maxBytes": false
|
|
85
|
+
}
|
|
86
|
+
];
|
|
87
|
+
function literalString(value: JsonValue | LiteralValue): string {
|
|
88
|
+
if (typeof value !== 'string') throw new TypeError('Expected a string literal.');
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
/** Legacy nonempty finite aliases are projections; opening never creates a write. */
|
|
92
|
+
export function existingFixedNumber(value: JsonValue | LiteralValue): number {
|
|
93
|
+
if ((typeof value !== 'string' && typeof value !== 'number') ||
|
|
94
|
+
(typeof value === 'string' && value.trim() === '')) throw new TypeError('Unavailable numeric projection.');
|
|
95
|
+
const number = Number(value);
|
|
96
|
+
if (!Number.isFinite(number)) throw new TypeError('Unavailable numeric projection.');
|
|
97
|
+
return number;
|
|
98
|
+
}
|
|
99
|
+
function normalizeChoice(field: Field, raw: string): string {
|
|
100
|
+
if (field.normalization === 'lowercase') return raw.toLowerCase();
|
|
101
|
+
if (field.normalization === 'method') return raw.trim().toUpperCase();
|
|
102
|
+
if (field.normalization === 'trim') return raw.trim();
|
|
103
|
+
if (field.normalization === 'parse-format') {
|
|
104
|
+
const lower = raw.trim().toLowerCase();
|
|
105
|
+
return lower === 'json' ? 'JSON' : lower === 'yaml' ? 'YAML' : lower === 'auto' ? 'auto' : raw;
|
|
106
|
+
}
|
|
107
|
+
return raw;
|
|
108
|
+
}
|
|
109
|
+
function binding(field: Field): FixedInputBinding {
|
|
110
|
+
return {argument: field.argument, schemaBundle: fixedInputBundle(field.opcode, field.argument),
|
|
111
|
+
control: {kind: field.control, ...(field.control === 'enum' ? {choices: field.choices.map(value => ({value}))} : {})},
|
|
112
|
+
read(value) {return field.control === 'number' ? existingFixedNumber(value) : normalizeChoice(field, literalString(value));},
|
|
113
|
+
encode(value) {
|
|
114
|
+
if (field.control === 'number') {
|
|
115
|
+
const parsed = parseFixedNumberInput(value);
|
|
116
|
+
if (parsed.kind !== 'valid') throw new TypeError('Invalid new numeric input.');
|
|
117
|
+
return parsed.token;
|
|
118
|
+
}
|
|
119
|
+
const raw = literalString(value), normalized = normalizeChoice(field, raw);
|
|
120
|
+
if (!field.choices.includes(normalized)) throw new TypeError('Unsupported fixed choice.');
|
|
121
|
+
return field.shape === 'menu-shadow' || field.shape === 'direct-field-dropdown' ? normalized : raw;
|
|
122
|
+
},
|
|
123
|
+
validateCandidate(value) {
|
|
124
|
+
try {
|
|
125
|
+
if (field.control === 'number') {
|
|
126
|
+
const number = existingFixedNumber(value);
|
|
127
|
+
if (field.maxBytes && (!Number.isSafeInteger(Math.trunc(number)) || Math.trunc(number) < 1)) return [{message: 'Invalid named body size.'}];
|
|
128
|
+
return [];
|
|
129
|
+
}
|
|
130
|
+
return field.choices.includes(normalizeChoice(field, literalString(value))) ? [] : [{message: 'Unsupported fixed choice.'}];
|
|
131
|
+
} catch {return [{message: 'Invalid fixed input.'}];}
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
export const fixedInputEditors: readonly FixedInputEditorOptions[] = [...new Set(fields.map(f => f.opcode))].map(opcode => ({
|
|
136
|
+
apiVersion: 1, extensionId, editorId: `${opcode}-fixed-inputs`, opcodes: [`${extensionId}_${opcode}`],
|
|
137
|
+
contractId: `${repository}/${opcode}`, contractRevision: 'fixed-inputs-v1',
|
|
138
|
+
bindings: fields.filter(field => field.opcode === opcode).map(binding),
|
|
139
|
+
validationContext: () => ({consumerId: repository, nowMs: 0, settings: null})
|
|
140
|
+
}));
|
|
141
|
+
|
|
142
|
+
interface OptionalFixedHost {
|
|
143
|
+
gui?: {blockEditors?: {apiVersion?: number; registerFixedInputEditor?: (definition: FixedInputEditorOptions) => () => void}};
|
|
144
|
+
vm?: {runtime?: {on?: (event: string, listener: () => void) => void; off?: (event: string, listener: () => void) => void}};
|
|
145
|
+
}
|
|
146
|
+
/** Registration follows the advertised opcode membership and the optional host capability. */
|
|
147
|
+
export function fixedInputRegistration(scratch: unknown) {
|
|
148
|
+
const host = (scratch ?? {}) as OptionalFixedHost;
|
|
149
|
+
let active: unknown;
|
|
150
|
+
let membership = '';
|
|
151
|
+
const removers: (() => void)[] = [];
|
|
152
|
+
let stopped = false;
|
|
153
|
+
const clear = () => {
|
|
154
|
+
host.vm?.runtime?.off?.('RUNTIME_DISPOSED', shutdown);
|
|
155
|
+
for (const remove of removers.splice(0).reverse()) remove();
|
|
156
|
+
active = undefined;
|
|
157
|
+
membership = '';
|
|
158
|
+
};
|
|
159
|
+
const shutdown = () => {stopped = true; clear();};
|
|
160
|
+
return {
|
|
161
|
+
ensure(visibleOpcodes: readonly string[]) {
|
|
162
|
+
const api = host.gui?.blockEditors;
|
|
163
|
+
const definitions = fixedInputEditors.filter(definition => definition.opcodes.every(opcode => visibleOpcodes.includes(opcode)));
|
|
164
|
+
const key = definitions.map(definition => definition.editorId).join(',');
|
|
165
|
+
if (stopped || (api === active && membership === key)) return;
|
|
166
|
+
clear();
|
|
167
|
+
if (api?.apiVersion !== 1 || typeof api.registerFixedInputEditor !== 'function' || !definitions.length) return;
|
|
168
|
+
try {
|
|
169
|
+
for (const definition of definitions) {
|
|
170
|
+
const remove = api.registerFixedInputEditor.call(api, definition);
|
|
171
|
+
if (typeof remove !== 'function') throw new TypeError('Invalid fixed input cleanup.');
|
|
172
|
+
removers.push(remove);
|
|
173
|
+
}
|
|
174
|
+
active = api;
|
|
175
|
+
membership = key;
|
|
176
|
+
host.vm?.runtime?.on?.('RUNTIME_DISPOSED', shutdown);
|
|
177
|
+
} catch (error) {clear(); throw error;}
|
|
178
|
+
},
|
|
179
|
+
dispose: shutdown
|
|
180
|
+
};
|
|
181
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type {SchemaBundle, SchemaDocument} from '@kubohiroya/turbowarp-extension-data-editor/schema';
|
|
2
|
+
import document from '../schemas/block-editor-fixed-inputs.schema.json' with {type: 'json'};
|
|
3
|
+
const repository = "turbowarp-http-server";
|
|
4
|
+
/** The owner JSON document is canonical; this selects a primitive field fragment. */
|
|
5
|
+
export function fixedInputBundle(opcode: string, argument: string): SchemaBundle {
|
|
6
|
+
const pointer = `#/$defs/${opcode}/properties/${argument}`;
|
|
7
|
+
const source = {repository, path: 'schemas/block-editor-fixed-inputs.schema.json', symbol: pointer};
|
|
8
|
+
const blocks = document.$defs as Record<string, {properties: Record<string, SchemaDocument>}>;
|
|
9
|
+
return {identity: {ownerId: repository, contractId: opcode, revision: 'fixed-inputs-v1', variantId: argument},
|
|
10
|
+
dialect: '2020-12', root: {retrievalUri: document.$id, document: blocks[opcode]!.properties[argument]!,
|
|
11
|
+
artifact: {role: 'canonical-schema', source}},
|
|
12
|
+
resources: [],
|
|
13
|
+
provenance: {canonicalSources: [source, {repository, path: 'src/extension.ts', symbol: opcode}],
|
|
14
|
+
generatedFiles: [], referenceSchemas: [], generationSources: []}};
|
|
15
|
+
}
|