@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.
- package/LICENSE +373 -0
- package/README.ja.md +346 -0
- package/README.md +874 -0
- package/dist/auth/digest-file.d.ts +17 -0
- package/dist/auth/digest-file.js +90 -0
- package/dist/auth/digest-file.js.map +1 -0
- package/dist/auth/digest.d.ts +23 -0
- package/dist/auth/digest.js +139 -0
- package/dist/auth/digest.js.map +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +156 -0
- package/dist/cli.js.map +1 -0
- package/dist/community.d.ts +81 -0
- package/dist/community.js +689 -0
- package/dist/community.js.map +1 -0
- package/dist/digest-cli.d.ts +1 -0
- package/dist/digest-cli.js +159 -0
- package/dist/digest-cli.js.map +1 -0
- package/dist/extension-manifest.json +441 -0
- package/dist/protocol.d.ts +59 -0
- package/dist/protocol.js +71 -0
- package/dist/protocol.js.map +1 -0
- package/dist/server.d.ts +89 -0
- package/dist/server.js +650 -0
- package/dist/server.js.map +1 -0
- package/dist/turbowarp-http-server.js +1110 -0
- package/docs/deployment-targets.ja.md +274 -0
- package/docs/turbowarp-http-server.architecture.html +15149 -0
- package/docs/turbowarp-http-server.architecture.json +1014 -0
- 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 +32 -0
- package/docs/turbowarp-http-server.architecture.visual-check.json +547 -0
- package/examples/01-hello-form/README.ja.md +46 -0
- package/examples/01-hello-form/server.mjs +59 -0
- package/examples/02-visitor-counter/README.ja.md +39 -0
- package/examples/02-visitor-counter/server.mjs +44 -0
- package/examples/03-message-board/README.ja.md +47 -0
- package/examples/03-message-board/server.mjs +72 -0
- package/examples/04-image-upload/README.ja.md +46 -0
- package/examples/04-image-upload/server.mjs +149 -0
- package/examples/05-live-camera/README.ja.md +47 -0
- package/examples/05-live-camera/server.mjs +147 -0
- package/examples/06-scratch-like-community/README.ja.md +82 -0
- package/examples/06-scratch-like-community/server.mjs +17 -0
- package/examples/README.ja.md +20 -0
- package/package.json +75 -0
- package/schemas/extension-manifest.schema.json +49 -0
- package/src/auth/digest-file.ts +115 -0
- package/src/auth/digest.ts +170 -0
- package/src/block-definitions.json +417 -0
- package/src/cli.ts +172 -0
- package/src/community.ts +815 -0
- package/src/config.ts +12 -0
- package/src/digest-cli.ts +178 -0
- package/src/extension-manifest.ts +149 -0
- package/src/extension.ts +713 -0
- package/src/globals.d.ts +30 -0
- package/src/index.ts +8 -0
- package/src/protocol.ts +132 -0
- package/src/server.ts +880 -0
package/src/config.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const extensionConfig = {
|
|
2
|
+
id: 'kubohiroyaturbowarphttpserver',
|
|
3
|
+
slug: 'turbowarp-http-server',
|
|
4
|
+
name: 'TurboWarp-HTTP-Server',
|
|
5
|
+
description: 'Connect TurboWarp blocks to an HTTP bridge server over WebSocket.',
|
|
6
|
+
author: 'Hiroya Kubo',
|
|
7
|
+
license: 'MPL-2.0',
|
|
8
|
+
unsandboxed: true,
|
|
9
|
+
docsURI: 'https://kubohiroya.github.io/turbowarp-http-server/',
|
|
10
|
+
blockIconURI:
|
|
11
|
+
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMDAvc3ZnIiB2aWV3Qm94PSIwIDAgNDggNDgiPjxyZWN0IHg9IjYiIHk9IjEwIiB3aWR0aD0iMzYiIGhlaWdodD0iMjgiIHJ4PSI0IiBmaWxsPSIjMjU2M0VCIi8+PHBhdGggZD0iTTEyIDE4aDI0TTExIDI0aDE0TTExIDMwaDIwIiBzdHJva2U9IiNGRkYiIHN0cm9rZS13aWR0aD0iMyIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIi8+PGNpcmNsZSBjeD0iMzQiIGN5PSIzMCIgcj0iMyIgZmlsbD0iIzIyQzU1RSIvPjwvc3ZnPg=='
|
|
12
|
+
} as const;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import {stdin as input, stdout as output} from 'node:process';
|
|
2
|
+
import {
|
|
3
|
+
deleteHtdigestUser,
|
|
4
|
+
readHtdigestFile,
|
|
5
|
+
setHtdigestPassword,
|
|
6
|
+
updateHtdigestPassword,
|
|
7
|
+
validateField,
|
|
8
|
+
writeHtdigestFile
|
|
9
|
+
} from './auth/digest-file.js';
|
|
10
|
+
|
|
11
|
+
interface DigestCliOptions {
|
|
12
|
+
command: 'init' | 'add' | 'passwd' | 'delete' | 'help';
|
|
13
|
+
file?: string;
|
|
14
|
+
username?: string;
|
|
15
|
+
realm?: string;
|
|
16
|
+
password?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
await main(process.argv.slice(2));
|
|
21
|
+
} catch (error) {
|
|
22
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
23
|
+
process.exitCode = 1;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function main(args: readonly string[]): Promise<void> {
|
|
27
|
+
const options = parseArgs(args);
|
|
28
|
+
if (options.command === 'help') {
|
|
29
|
+
printHelp();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const file = requireOption(options.file, 'file');
|
|
34
|
+
const realm = requireOption(options.realm ?? process.env.TURBOWARP_HTTP_DIGEST_REALM, '--realm');
|
|
35
|
+
validateField('realm', realm);
|
|
36
|
+
|
|
37
|
+
if (options.command === 'init') {
|
|
38
|
+
const entries = await readHtdigestFile(file);
|
|
39
|
+
await writeHtdigestFile(file, entries);
|
|
40
|
+
console.log(`Initialized ${file}.`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const username = requireOption(options.username, 'username');
|
|
45
|
+
validateField('username', username);
|
|
46
|
+
|
|
47
|
+
if (options.command === 'delete') {
|
|
48
|
+
const deleted = await deleteHtdigestUser(file, username, realm);
|
|
49
|
+
console.log(deleted ? `Deleted ${username}.` : `User ${username} was not found.`);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const password =
|
|
54
|
+
options.password ??
|
|
55
|
+
process.env.TURBOWARP_HTTP_DIGEST_PASSWORD ??
|
|
56
|
+
(await promptPassword(`Password for ${username}: `));
|
|
57
|
+
if (options.command === 'passwd') {
|
|
58
|
+
const updated = await updateHtdigestPassword(file, username, realm, password);
|
|
59
|
+
if (!updated) throw new Error(`User ${username} was not found.`);
|
|
60
|
+
console.log(`Updated ${username}.`);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const result = await setHtdigestPassword(file, username, realm, password);
|
|
64
|
+
console.log(`${result === 'created' ? 'Added' : 'Updated'} ${username}.`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function parseArgs(args: readonly string[]): DigestCliOptions {
|
|
68
|
+
const command = args[0];
|
|
69
|
+
if (command === undefined || command === '--help' || command === '-h') return {command: 'help'};
|
|
70
|
+
if (command !== 'init' && command !== 'add' && command !== 'passwd' && command !== 'delete') {
|
|
71
|
+
throw new Error(`Unknown command: ${command}`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const options: DigestCliOptions = {command};
|
|
75
|
+
let positionalIndex = 0;
|
|
76
|
+
for (let index = 1; index < args.length; index += 1) {
|
|
77
|
+
const arg = args[index];
|
|
78
|
+
if (arg === undefined) break;
|
|
79
|
+
if (arg === '--realm') {
|
|
80
|
+
options.realm = requireValue(args, index, '--realm');
|
|
81
|
+
index += 1;
|
|
82
|
+
} else if (arg === '--password') {
|
|
83
|
+
options.password = requireValue(args, index, '--password');
|
|
84
|
+
index += 1;
|
|
85
|
+
} else if (arg === '--help' || arg === '-h') {
|
|
86
|
+
return {command: 'help'};
|
|
87
|
+
} else if (arg.startsWith('--')) {
|
|
88
|
+
throw new Error(`Unknown option: ${arg}`);
|
|
89
|
+
} else if (positionalIndex === 0) {
|
|
90
|
+
options.file = arg;
|
|
91
|
+
positionalIndex += 1;
|
|
92
|
+
} else if (positionalIndex === 1) {
|
|
93
|
+
options.username = arg;
|
|
94
|
+
positionalIndex += 1;
|
|
95
|
+
} else {
|
|
96
|
+
throw new Error(`Unexpected argument: ${arg}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (command === 'init' && options.username) {
|
|
101
|
+
throw new Error('init does not accept a username.');
|
|
102
|
+
}
|
|
103
|
+
return options;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function requireValue(args: readonly string[], index: number, name: string): string {
|
|
107
|
+
const value = args[index + 1];
|
|
108
|
+
if (value === undefined || value.startsWith('--')) {
|
|
109
|
+
throw new Error(`${name} requires a value.`);
|
|
110
|
+
}
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function requireOption(value: string | undefined, name: string): string {
|
|
115
|
+
if (!value) throw new Error(`${name} is required.`);
|
|
116
|
+
return value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function promptPassword(prompt: string): Promise<string> {
|
|
120
|
+
if (!input.isTTY || !output.isTTY) {
|
|
121
|
+
throw new Error('Password is required. Use --password or TURBOWARP_HTTP_DIGEST_PASSWORD.');
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
output.write(prompt);
|
|
125
|
+
input.setRawMode(true);
|
|
126
|
+
input.resume();
|
|
127
|
+
input.setEncoding('utf8');
|
|
128
|
+
|
|
129
|
+
return new Promise((resolve, reject) => {
|
|
130
|
+
let value = '';
|
|
131
|
+
const cleanup = () => {
|
|
132
|
+
input.setRawMode(false);
|
|
133
|
+
input.pause();
|
|
134
|
+
input.off('data', onData);
|
|
135
|
+
};
|
|
136
|
+
const onData = (chunk: string) => {
|
|
137
|
+
for (const char of chunk) {
|
|
138
|
+
if (char === '\u0003') {
|
|
139
|
+
cleanup();
|
|
140
|
+
reject(new Error('Canceled.'));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (char === '\r' || char === '\n') {
|
|
144
|
+
cleanup();
|
|
145
|
+
output.write('\n');
|
|
146
|
+
resolve(value);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (char === '\b' || char === '\u007f') {
|
|
150
|
+
value = value.slice(0, -1);
|
|
151
|
+
} else {
|
|
152
|
+
value += char;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
input.on('data', onData);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function printHelp(): void {
|
|
161
|
+
console.log(`Usage: turbowarp-http-digest <command> <file> [username] --realm <realm>
|
|
162
|
+
|
|
163
|
+
Commands:
|
|
164
|
+
init <file> Create or normalize an htdigest file.
|
|
165
|
+
add <file> <username> Add a user or update an existing user.
|
|
166
|
+
passwd <file> <username> Update a user's password.
|
|
167
|
+
delete <file> <username> Delete a user.
|
|
168
|
+
|
|
169
|
+
Options:
|
|
170
|
+
--realm <realm> Digest authentication realm.
|
|
171
|
+
--password <password> Password value. Prefer interactive input when possible.
|
|
172
|
+
-h, --help Show this help.
|
|
173
|
+
|
|
174
|
+
Environment:
|
|
175
|
+
TURBOWARP_HTTP_DIGEST_REALM
|
|
176
|
+
TURBOWARP_HTTP_DIGEST_PASSWORD
|
|
177
|
+
`);
|
|
178
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type {Plugin} from 'vite';
|
|
2
|
+
|
|
3
|
+
export const EXTENSION_MANIFEST_FORMAT_VERSION = 1 as const;
|
|
4
|
+
|
|
5
|
+
export interface ExtensionManifestArgument {
|
|
6
|
+
id: string;
|
|
7
|
+
type: string;
|
|
8
|
+
menu?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ExtensionManifestBlock {
|
|
12
|
+
opcode: string;
|
|
13
|
+
blockType: string;
|
|
14
|
+
arguments: ExtensionManifestArgument[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ExtensionManifestMenu {
|
|
18
|
+
id: string;
|
|
19
|
+
acceptReporters: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ExtensionManifest {
|
|
23
|
+
formatVersion: typeof EXTENSION_MANIFEST_FORMAT_VERSION;
|
|
24
|
+
id: string;
|
|
25
|
+
blocks: ExtensionManifestBlock[];
|
|
26
|
+
menus: ExtensionManifestMenu[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ExtensionManifestPluginOptions {
|
|
30
|
+
id: string;
|
|
31
|
+
definitions: unknown;
|
|
32
|
+
fileName?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createExtensionManifest(id: string, definitions: unknown): ExtensionManifest {
|
|
36
|
+
if (!/^[a-z0-9]+$/.test(id)) {
|
|
37
|
+
throw new TypeError('Extension manifest ID must contain only lowercase letters and numbers.');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const source = requireRecord(definitions, 'Block definitions');
|
|
41
|
+
const sourceBlocks = source.blocks;
|
|
42
|
+
if (!Array.isArray(sourceBlocks)) {
|
|
43
|
+
throw new TypeError('Block definitions must contain a blocks array.');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const menus = normalizeMenus(source.menus);
|
|
47
|
+
const menuIds = new Set(menus.map((menu) => menu.id));
|
|
48
|
+
const seenOpcodes = new Set<string>();
|
|
49
|
+
const blocks = sourceBlocks.map((block, index) => {
|
|
50
|
+
const normalized = normalizeBlock(block, index, menuIds);
|
|
51
|
+
if (seenOpcodes.has(normalized.opcode)) {
|
|
52
|
+
throw new TypeError(`Duplicate block opcode: ${normalized.opcode}`);
|
|
53
|
+
}
|
|
54
|
+
seenOpcodes.add(normalized.opcode);
|
|
55
|
+
return normalized;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
formatVersion: EXTENSION_MANIFEST_FORMAT_VERSION,
|
|
60
|
+
id,
|
|
61
|
+
blocks: blocks.sort((left, right) => compareIds(left.opcode, right.opcode)),
|
|
62
|
+
menus
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function serializeExtensionManifest(id: string, definitions: unknown): string {
|
|
67
|
+
return `${JSON.stringify(createExtensionManifest(id, definitions), null, 2)}\n`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function extensionManifestPlugin(options: ExtensionManifestPluginOptions): Plugin {
|
|
71
|
+
return {
|
|
72
|
+
name: 'extension-api-manifest',
|
|
73
|
+
apply: 'build',
|
|
74
|
+
enforce: 'post',
|
|
75
|
+
generateBundle() {
|
|
76
|
+
this.emitFile({
|
|
77
|
+
type: 'asset',
|
|
78
|
+
fileName: options.fileName ?? 'extension-manifest.json',
|
|
79
|
+
source: serializeExtensionManifest(options.id, options.definitions)
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function normalizeBlock(
|
|
86
|
+
value: unknown,
|
|
87
|
+
index: number,
|
|
88
|
+
menuIds: ReadonlySet<string>
|
|
89
|
+
): ExtensionManifestBlock {
|
|
90
|
+
const block = requireRecord(value, `Block at index ${index}`);
|
|
91
|
+
const opcode = requireNonEmptyString(block.opcode, `Block at index ${index} opcode`);
|
|
92
|
+
const blockType = requireNonEmptyString(block.blockType, `Block ${opcode} blockType`);
|
|
93
|
+
const sourceArguments = block.arguments ?? {};
|
|
94
|
+
const argumentRecord = requireRecord(sourceArguments, `Block ${opcode} arguments`);
|
|
95
|
+
const argumentsList = Object.entries(argumentRecord).map(([argumentId, argument]) => {
|
|
96
|
+
requireNonEmptyString(argumentId, `Block ${opcode} argument ID`);
|
|
97
|
+
const definition = requireRecord(argument, `Block ${opcode} argument ${argumentId}`);
|
|
98
|
+
const type = requireNonEmptyString(
|
|
99
|
+
definition.type,
|
|
100
|
+
`Block ${opcode} argument ${argumentId} type`
|
|
101
|
+
);
|
|
102
|
+
const menu = definition.menu;
|
|
103
|
+
if (menu !== undefined && (typeof menu !== 'string' || !menuIds.has(menu))) {
|
|
104
|
+
throw new TypeError(`Block ${opcode} argument ${argumentId} references unknown menu: ${menu}`);
|
|
105
|
+
}
|
|
106
|
+
return menu === undefined ? {id: argumentId, type} : {id: argumentId, type, menu};
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
opcode,
|
|
111
|
+
blockType,
|
|
112
|
+
arguments: argumentsList.sort((left, right) => compareIds(left.id, right.id))
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function normalizeMenus(value: unknown): ExtensionManifestMenu[] {
|
|
117
|
+
const menuRecord = requireRecord(value ?? {}, 'Block definition menus');
|
|
118
|
+
return Object.entries(menuRecord)
|
|
119
|
+
.map(([id, menu]) => {
|
|
120
|
+
requireNonEmptyString(id, 'Menu ID');
|
|
121
|
+
const definition = requireRecord(menu, `Menu ${id}`);
|
|
122
|
+
const acceptReporters = definition.acceptReporters ?? false;
|
|
123
|
+
if (typeof acceptReporters !== 'boolean') {
|
|
124
|
+
throw new TypeError(`Menu ${id} acceptReporters must be a boolean.`);
|
|
125
|
+
}
|
|
126
|
+
return {id, acceptReporters};
|
|
127
|
+
})
|
|
128
|
+
.sort((left, right) => compareIds(left.id, right.id));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
|
132
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
133
|
+
throw new TypeError(`${label} must be an object.`);
|
|
134
|
+
}
|
|
135
|
+
return value as Record<string, unknown>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function requireNonEmptyString(value: unknown, label: string): string {
|
|
139
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
140
|
+
throw new TypeError(`${label} must be a non-empty string.`);
|
|
141
|
+
}
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function compareIds(left: string, right: string): number {
|
|
146
|
+
if (left < right) return -1;
|
|
147
|
+
if (left > right) return 1;
|
|
148
|
+
return 0;
|
|
149
|
+
}
|