@kubohiroya/turbowarp-http-server 0.1.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.
Files changed (63) hide show
  1. package/LICENSE +373 -0
  2. package/README.ja.md +346 -0
  3. package/README.md +874 -0
  4. package/dist/auth/digest-file.d.ts +17 -0
  5. package/dist/auth/digest-file.js +90 -0
  6. package/dist/auth/digest-file.js.map +1 -0
  7. package/dist/auth/digest.d.ts +23 -0
  8. package/dist/auth/digest.js +139 -0
  9. package/dist/auth/digest.js.map +1 -0
  10. package/dist/cli.d.ts +1 -0
  11. package/dist/cli.js +156 -0
  12. package/dist/cli.js.map +1 -0
  13. package/dist/community.d.ts +81 -0
  14. package/dist/community.js +689 -0
  15. package/dist/community.js.map +1 -0
  16. package/dist/digest-cli.d.ts +1 -0
  17. package/dist/digest-cli.js +159 -0
  18. package/dist/digest-cli.js.map +1 -0
  19. package/dist/extension-manifest.json +441 -0
  20. package/dist/protocol.d.ts +59 -0
  21. package/dist/protocol.js +71 -0
  22. package/dist/protocol.js.map +1 -0
  23. package/dist/server.d.ts +89 -0
  24. package/dist/server.js +650 -0
  25. package/dist/server.js.map +1 -0
  26. package/dist/turbowarp-http-server.js +1110 -0
  27. package/docs/deployment-targets.ja.md +274 -0
  28. package/docs/turbowarp-http-server.architecture.html +15149 -0
  29. package/docs/turbowarp-http-server.architecture.json +1014 -0
  30. package/docs/turbowarp-http-server.architecture.visual-check.1440x900.dark.png +0 -0
  31. package/docs/turbowarp-http-server.architecture.visual-check.1440x900.light.png +0 -0
  32. package/docs/turbowarp-http-server.architecture.visual-check.2048x1320.dark.png +0 -0
  33. package/docs/turbowarp-http-server.architecture.visual-check.2048x1320.light.png +0 -0
  34. package/docs/turbowarp-http-server.architecture.visual-check.html +32 -0
  35. package/docs/turbowarp-http-server.architecture.visual-check.json +547 -0
  36. package/examples/01-hello-form/README.ja.md +46 -0
  37. package/examples/01-hello-form/server.mjs +59 -0
  38. package/examples/02-visitor-counter/README.ja.md +39 -0
  39. package/examples/02-visitor-counter/server.mjs +44 -0
  40. package/examples/03-message-board/README.ja.md +47 -0
  41. package/examples/03-message-board/server.mjs +72 -0
  42. package/examples/04-image-upload/README.ja.md +46 -0
  43. package/examples/04-image-upload/server.mjs +149 -0
  44. package/examples/05-live-camera/README.ja.md +47 -0
  45. package/examples/05-live-camera/server.mjs +147 -0
  46. package/examples/06-scratch-like-community/README.ja.md +82 -0
  47. package/examples/06-scratch-like-community/server.mjs +17 -0
  48. package/examples/README.ja.md +20 -0
  49. package/package.json +75 -0
  50. package/schemas/extension-manifest.schema.json +49 -0
  51. package/src/auth/digest-file.ts +115 -0
  52. package/src/auth/digest.ts +170 -0
  53. package/src/block-definitions.json +417 -0
  54. package/src/cli.ts +172 -0
  55. package/src/community.ts +815 -0
  56. package/src/config.ts +12 -0
  57. package/src/digest-cli.ts +178 -0
  58. package/src/extension-manifest.ts +149 -0
  59. package/src/extension.ts +713 -0
  60. package/src/globals.d.ts +30 -0
  61. package/src/index.ts +8 -0
  62. package/src/protocol.ts +132 -0
  63. package/src/server.ts +880 -0
@@ -0,0 +1,30 @@
1
+ interface TurboWarpExtension {
2
+ getInfo(): Record<string, unknown>;
3
+ }
4
+
5
+ interface ScratchTranslate {
6
+ (text: string): string;
7
+ (message: {default: string; description?: string}, placeholders?: Record<string, string | number>): string;
8
+ }
9
+
10
+ interface ScratchApi {
11
+ vm?: {
12
+ runtime?: {
13
+ startHats?(opcode: string, matchFields?: Record<string, string>): unknown[] | undefined;
14
+ };
15
+ };
16
+ extensions: {
17
+ unsandboxed: boolean;
18
+ register(extension: TurboWarpExtension): void;
19
+ };
20
+ BlockType: Record<'COMMAND' | 'REPORTER' | 'BOOLEAN' | 'HAT', string>;
21
+ ArgumentType: Record<'STRING' | 'NUMBER' | 'BOOLEAN', string>;
22
+ Cast: {
23
+ toString(value: unknown): string;
24
+ toNumber(value: unknown): number;
25
+ toBoolean(value: unknown): boolean;
26
+ };
27
+ translate: ScratchTranslate;
28
+ }
29
+
30
+ declare const Scratch: ScratchApi;
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ import {extensionConfig} from './config.js';
2
+ import {TurboWarpHttpServerExtension} from './extension.js';
3
+
4
+ if (extensionConfig.unsandboxed && !Scratch.extensions.unsandboxed) {
5
+ throw new Error(`${extensionConfig.name} must run unsandboxed.`);
6
+ }
7
+
8
+ Scratch.extensions.register(new TurboWarpHttpServerExtension());
@@ -0,0 +1,132 @@
1
+ export const HTTP_BRIDGE_PROTOCOL = 'turbowarp-http-server' as const;
2
+ export const HTTP_BRIDGE_PROTOCOL_VERSION = 1 as const;
3
+
4
+ export interface BridgeTextBody {
5
+ kind: 'text';
6
+ text: string;
7
+ }
8
+
9
+ export interface BridgeEmptyBody {
10
+ kind: 'empty';
11
+ }
12
+
13
+ export interface BridgeUnsupportedBody {
14
+ kind: 'unsupported';
15
+ reason: string;
16
+ }
17
+
18
+ export type BridgeBody = BridgeTextBody | BridgeEmptyBody | BridgeUnsupportedBody;
19
+
20
+ export interface BridgeAuthContext {
21
+ type: string;
22
+ username?: string;
23
+ provider?: string;
24
+ profile?: Record<string, unknown>;
25
+ }
26
+
27
+ export interface BridgeRequestMessage {
28
+ type: 'request';
29
+ protocol: typeof HTTP_BRIDGE_PROTOCOL;
30
+ version: typeof HTTP_BRIDGE_PROTOCOL_VERSION;
31
+ id: string;
32
+ method: string;
33
+ url: string;
34
+ path: string;
35
+ route: string;
36
+ pathParams: Record<string, string>;
37
+ query: Record<string, string[]>;
38
+ headers: Record<string, string[]>;
39
+ body: BridgeBody;
40
+ clientAddress: string;
41
+ auth?: BridgeAuthContext;
42
+ }
43
+
44
+ export interface BridgeResponseMessage {
45
+ type: 'response';
46
+ id: string;
47
+ status: number;
48
+ headers: Record<string, string[]>;
49
+ body: BridgeBody;
50
+ }
51
+
52
+ export interface BridgeErrorMessage {
53
+ type: 'error';
54
+ id?: string;
55
+ message: string;
56
+ }
57
+
58
+ export type BridgeClientMessage = BridgeResponseMessage | BridgeErrorMessage;
59
+
60
+ export function normalizeHeaderName(name: string): string {
61
+ return name.trim().toLowerCase();
62
+ }
63
+
64
+ export function isValidHttpStatus(status: number): boolean {
65
+ return Number.isInteger(status) && status >= 100 && status <= 599;
66
+ }
67
+
68
+ export function isBodyForbidden(method: string, status: number): boolean {
69
+ return method.toUpperCase() === 'HEAD' || status === 204 || status === 205 || status === 304;
70
+ }
71
+
72
+ export function isForbiddenResponseHeader(name: string): boolean {
73
+ return ['connection', 'content-length', 'transfer-encoding', 'upgrade'].includes(
74
+ normalizeHeaderName(name)
75
+ );
76
+ }
77
+
78
+ export function validateHeaderName(name: string): boolean {
79
+ return /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/.test(name);
80
+ }
81
+
82
+ export function validateHeaderValue(value: string): boolean {
83
+ return !/[\r\n]/.test(value);
84
+ }
85
+
86
+ export function firstValue(values: readonly string[] | undefined): string {
87
+ return values?.[0] ?? '';
88
+ }
89
+
90
+ export function parseBridgeClientMessage(value: string): BridgeClientMessage | null {
91
+ try {
92
+ const parsed = JSON.parse(value) as Partial<BridgeClientMessage>;
93
+ if (parsed.type === 'response' && typeof parsed.id === 'string') {
94
+ return {
95
+ type: 'response',
96
+ id: parsed.id,
97
+ status: typeof parsed.status === 'number' ? parsed.status : 200,
98
+ headers: requireHeaderRecord(parsed.headers),
99
+ body: requireBridgeBody(parsed.body)
100
+ };
101
+ }
102
+ if (parsed.type === 'error') {
103
+ const error: BridgeErrorMessage = {
104
+ type: 'error',
105
+ message: typeof parsed.message === 'string' ? parsed.message : 'Bridge error.'
106
+ };
107
+ if (typeof parsed.id === 'string') error.id = parsed.id;
108
+ return error;
109
+ }
110
+ } catch {
111
+ return null;
112
+ }
113
+ return null;
114
+ }
115
+
116
+ export function requireHeaderRecord(value: unknown): Record<string, string[]> {
117
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return {};
118
+ const result: Record<string, string[]> = {};
119
+ for (const [name, rawValues] of Object.entries(value)) {
120
+ const values = Array.isArray(rawValues) ? rawValues : [rawValues];
121
+ result[name] = values.map((item) => String(item));
122
+ }
123
+ return result;
124
+ }
125
+
126
+ export function requireBridgeBody(value: unknown): BridgeBody {
127
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) return {kind: 'empty'};
128
+ const record = value as Record<string, unknown>;
129
+ if (record.kind === 'text') return {kind: 'text', text: String(record.text ?? '')};
130
+ if (record.kind === 'unsupported') return {kind: 'unsupported', reason: String(record.reason ?? '')};
131
+ return {kind: 'empty'};
132
+ }