export-runtime 0.0.4 → 0.0.5
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/bin/generate-types.mjs +223 -0
- package/entry.js +2 -1
- package/handler.js +2 -2
- package/package.json +8 -3
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { parseSync } from "oxc-parser";
|
|
4
|
+
import fs from "fs";
|
|
5
|
+
import path from "path";
|
|
6
|
+
|
|
7
|
+
const cwd = process.cwd();
|
|
8
|
+
|
|
9
|
+
// Read wrangler.toml to find user module path
|
|
10
|
+
const wranglerPath = path.join(cwd, "wrangler.toml");
|
|
11
|
+
if (!fs.existsSync(wranglerPath)) {
|
|
12
|
+
console.error("wrangler.toml not found in", cwd);
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
const wranglerContent = fs.readFileSync(wranglerPath, "utf8");
|
|
16
|
+
const aliasMatch = wranglerContent.match(/"__USER_MODULE__"\s*=\s*"([^"]+)"/);
|
|
17
|
+
if (!aliasMatch) {
|
|
18
|
+
console.error('Could not find __USER_MODULE__ alias in wrangler.toml');
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const userModulePath = path.resolve(cwd, aliasMatch[1]);
|
|
23
|
+
if (!fs.existsSync(userModulePath)) {
|
|
24
|
+
console.error("User module not found:", userModulePath);
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const source = fs.readFileSync(userModulePath, "utf8");
|
|
29
|
+
const isTS = userModulePath.endsWith(".ts") || userModulePath.endsWith(".tsx");
|
|
30
|
+
const fileName = path.basename(userModulePath);
|
|
31
|
+
const result = parseSync(fileName, source, { sourceType: "module" });
|
|
32
|
+
const program = result.program;
|
|
33
|
+
|
|
34
|
+
// --- Type extraction helpers ---
|
|
35
|
+
|
|
36
|
+
function extractType(node) {
|
|
37
|
+
if (!node) return "any";
|
|
38
|
+
const ta = node.type === "TSTypeAnnotation" ? node.typeAnnotation : node;
|
|
39
|
+
switch (ta.type) {
|
|
40
|
+
case "TSStringKeyword": return "string";
|
|
41
|
+
case "TSNumberKeyword": return "number";
|
|
42
|
+
case "TSBooleanKeyword": return "boolean";
|
|
43
|
+
case "TSVoidKeyword": return "void";
|
|
44
|
+
case "TSAnyKeyword": return "any";
|
|
45
|
+
case "TSNullKeyword": return "null";
|
|
46
|
+
case "TSUndefinedKeyword": return "undefined";
|
|
47
|
+
case "TSNeverKeyword": return "never";
|
|
48
|
+
case "TSUnknownKeyword": return "unknown";
|
|
49
|
+
case "TSBigIntKeyword": return "bigint";
|
|
50
|
+
case "TSSymbolKeyword": return "symbol";
|
|
51
|
+
case "TSObjectKeyword": return "object";
|
|
52
|
+
case "TSArrayType":
|
|
53
|
+
return `${extractType(ta.elementType)}[]`;
|
|
54
|
+
case "TSTupleType":
|
|
55
|
+
return `[${(ta.elementTypes || []).map(e => extractType(e)).join(", ")}]`;
|
|
56
|
+
case "TSUnionType":
|
|
57
|
+
return ta.types.map(t => extractType(t)).join(" | ");
|
|
58
|
+
case "TSIntersectionType":
|
|
59
|
+
return ta.types.map(t => extractType(t)).join(" & ");
|
|
60
|
+
case "TSLiteralType": {
|
|
61
|
+
const lit = ta.literal;
|
|
62
|
+
if (lit.type === "StringLiteral") return JSON.stringify(lit.value);
|
|
63
|
+
if (lit.type === "NumericLiteral") return String(lit.value);
|
|
64
|
+
if (lit.type === "BooleanLiteral") return String(lit.value);
|
|
65
|
+
if (lit.type === "UnaryExpression") return `-${lit.argument.value}`;
|
|
66
|
+
return "any";
|
|
67
|
+
}
|
|
68
|
+
case "TSTypeReference": {
|
|
69
|
+
const name = ta.typeName?.name || ta.typeName?.right?.name || "any";
|
|
70
|
+
const typeArgs = ta.typeArguments || ta.typeParameters;
|
|
71
|
+
if (typeArgs?.params?.length) {
|
|
72
|
+
const args = typeArgs.params.map(p => extractType(p)).join(", ");
|
|
73
|
+
return `${name}<${args}>`;
|
|
74
|
+
}
|
|
75
|
+
return name;
|
|
76
|
+
}
|
|
77
|
+
case "TSFunctionType": {
|
|
78
|
+
const params = extractParams(ta.params);
|
|
79
|
+
const ret = ta.returnType ? extractType(ta.returnType) : "any";
|
|
80
|
+
return `(${params.join(", ")}) => ${ret}`;
|
|
81
|
+
}
|
|
82
|
+
case "TSTypeLiteral": {
|
|
83
|
+
const members = (ta.members || []).map(m => {
|
|
84
|
+
if (m.type === "TSPropertySignature") {
|
|
85
|
+
const key = m.key?.name || m.key?.value;
|
|
86
|
+
const type = m.typeAnnotation ? extractType(m.typeAnnotation) : "any";
|
|
87
|
+
const opt = m.optional ? "?" : "";
|
|
88
|
+
return `${key}${opt}: ${type}`;
|
|
89
|
+
}
|
|
90
|
+
return "";
|
|
91
|
+
}).filter(Boolean);
|
|
92
|
+
return `{ ${members.join("; ")} }`;
|
|
93
|
+
}
|
|
94
|
+
case "TSTypeAnnotation":
|
|
95
|
+
return extractType(ta.typeAnnotation);
|
|
96
|
+
default:
|
|
97
|
+
return "any";
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function extractParams(params) {
|
|
102
|
+
return params.map(p => {
|
|
103
|
+
if (p.type === "AssignmentPattern") {
|
|
104
|
+
const name = p.left?.name || "arg";
|
|
105
|
+
const type = p.left?.typeAnnotation ? extractType(p.left.typeAnnotation) : "any";
|
|
106
|
+
return `${name}?: ${type}`;
|
|
107
|
+
}
|
|
108
|
+
const name = p.name || p.argument?.name || "arg";
|
|
109
|
+
const type = p.typeAnnotation ? extractType(p.typeAnnotation) : "any";
|
|
110
|
+
const opt = p.optional ? "?" : "";
|
|
111
|
+
const rest = p.type === "RestElement" ? "..." : "";
|
|
112
|
+
return `${rest}${name}${opt}: ${type}`;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Wrap return type: all functions become async over the network
|
|
117
|
+
function wrapReturnType(returnType, isAsync, isGenerator) {
|
|
118
|
+
if (isGenerator) {
|
|
119
|
+
// async generator → AsyncIterable<YieldType>
|
|
120
|
+
// extract inner type from AsyncGenerator<T> if present
|
|
121
|
+
if (returnType.startsWith("AsyncGenerator")) {
|
|
122
|
+
const inner = returnType.match(/^AsyncGenerator<(.+?)(?:,.*)?>/);
|
|
123
|
+
return `Promise<AsyncIterable<${inner ? inner[1] : "any"}>>`;
|
|
124
|
+
}
|
|
125
|
+
return `Promise<AsyncIterable<${returnType === "any" ? "any" : returnType}>>`;
|
|
126
|
+
}
|
|
127
|
+
// Already Promise<T> → keep as-is
|
|
128
|
+
if (returnType.startsWith("Promise<")) return returnType;
|
|
129
|
+
// ReadableStream<T> → Promise<ReadableStream<T>>
|
|
130
|
+
if (returnType.startsWith("ReadableStream")) return `Promise<${returnType}>`;
|
|
131
|
+
// Wrap in Promise
|
|
132
|
+
return `Promise<${returnType}>`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// --- Generate .d.ts ---
|
|
136
|
+
|
|
137
|
+
const lines = [
|
|
138
|
+
"// Auto-generated type definitions (oxc-parser)",
|
|
139
|
+
"// All functions are async over the network",
|
|
140
|
+
"",
|
|
141
|
+
];
|
|
142
|
+
|
|
143
|
+
for (const node of program.body) {
|
|
144
|
+
if (node.type !== "ExportNamedDeclaration" || !node.declaration) continue;
|
|
145
|
+
const decl = node.declaration;
|
|
146
|
+
|
|
147
|
+
if (decl.type === "FunctionDeclaration") {
|
|
148
|
+
const name = decl.id.name;
|
|
149
|
+
const params = extractParams(decl.params);
|
|
150
|
+
const rawRet = decl.returnType ? extractType(decl.returnType) : "any";
|
|
151
|
+
const ret = wrapReturnType(rawRet, decl.async, decl.generator);
|
|
152
|
+
lines.push(`export declare function ${name}(${params.join(", ")}): ${ret};`);
|
|
153
|
+
lines.push("");
|
|
154
|
+
|
|
155
|
+
} else if (decl.type === "ClassDeclaration") {
|
|
156
|
+
const name = decl.id.name;
|
|
157
|
+
lines.push(`export declare class ${name} {`);
|
|
158
|
+
for (const member of decl.body.body) {
|
|
159
|
+
if (member.type === "MethodDefinition") {
|
|
160
|
+
const mName = member.key.name || member.key.value;
|
|
161
|
+
if (member.kind === "constructor") {
|
|
162
|
+
const params = extractParams(member.value.params);
|
|
163
|
+
lines.push(` constructor(${params.join(", ")});`);
|
|
164
|
+
} else {
|
|
165
|
+
const params = extractParams(member.value.params);
|
|
166
|
+
const rawRet = member.value.returnType ? extractType(member.value.returnType) : "any";
|
|
167
|
+
const ret = wrapReturnType(rawRet, member.value.async, member.value.generator);
|
|
168
|
+
lines.push(` ${mName}(${params.join(", ")}): ${ret};`);
|
|
169
|
+
}
|
|
170
|
+
} else if (member.type === "PropertyDefinition") {
|
|
171
|
+
// Skip private members
|
|
172
|
+
if (member.accessibility === "private") continue;
|
|
173
|
+
const mName = member.key.name;
|
|
174
|
+
const type = member.typeAnnotation ? extractType(member.typeAnnotation) : "any";
|
|
175
|
+
lines.push(` ${mName}: ${type};`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
lines.push(` [Symbol.dispose](): Promise<void>;`);
|
|
179
|
+
lines.push(` "[release]"(): Promise<void>;`);
|
|
180
|
+
lines.push(`}`);
|
|
181
|
+
lines.push("");
|
|
182
|
+
|
|
183
|
+
} else if (decl.type === "VariableDeclaration") {
|
|
184
|
+
for (const d of decl.declarations) {
|
|
185
|
+
const name = d.id.name;
|
|
186
|
+
if (d.init?.type === "ObjectExpression") {
|
|
187
|
+
lines.push(`export declare const ${name}: {`);
|
|
188
|
+
for (const prop of d.init.properties) {
|
|
189
|
+
if (prop.type === "SpreadElement") continue;
|
|
190
|
+
const key = prop.key?.name || prop.key?.value;
|
|
191
|
+
if (prop.value?.type === "FunctionExpression" || prop.value?.type === "ArrowFunctionExpression") {
|
|
192
|
+
const params = extractParams(prop.value.params);
|
|
193
|
+
const rawRet = prop.value.returnType ? extractType(prop.value.returnType) : "any";
|
|
194
|
+
const ret = wrapReturnType(rawRet, prop.value.async, prop.value.generator);
|
|
195
|
+
lines.push(` ${key}(${params.join(", ")}): ${ret};`);
|
|
196
|
+
} else {
|
|
197
|
+
const type = d.id.typeAnnotation ? "any" : "any";
|
|
198
|
+
lines.push(` ${key}: any;`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
lines.push(`};`);
|
|
202
|
+
lines.push("");
|
|
203
|
+
} else {
|
|
204
|
+
const type = d.id.typeAnnotation ? extractType(d.id.typeAnnotation) : "any";
|
|
205
|
+
lines.push(`export declare const ${name}: ${type};`);
|
|
206
|
+
lines.push("");
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Add createUploadStream helper type
|
|
213
|
+
lines.push("export declare function createUploadStream(): Promise<{");
|
|
214
|
+
lines.push(" stream: WritableStream<any>;");
|
|
215
|
+
lines.push(" writableId: number;");
|
|
216
|
+
lines.push("}>;");
|
|
217
|
+
|
|
218
|
+
const typeDefinitions = lines.join("\n");
|
|
219
|
+
|
|
220
|
+
// Write as a JS module that exports the string
|
|
221
|
+
const outPath = path.join(cwd, ".export-types.js");
|
|
222
|
+
fs.writeFileSync(outPath, `export default ${JSON.stringify(typeDefinitions)};\n`);
|
|
223
|
+
console.log("Generated type definitions →", outPath);
|
package/entry.js
CHANGED
package/handler.js
CHANGED
|
@@ -91,7 +91,7 @@ const generateTypeDefinitions = (exports, exportKeys) => {
|
|
|
91
91
|
return lines.join("\n");
|
|
92
92
|
};
|
|
93
93
|
|
|
94
|
-
export const createHandler = (exports) => {
|
|
94
|
+
export const createHandler = (exports, generatedTypes) => {
|
|
95
95
|
const exportKeys = Object.keys(exports);
|
|
96
96
|
const iteratorStore = new Map();
|
|
97
97
|
const instanceStore = new Map();
|
|
@@ -354,7 +354,7 @@ export const createHandler = (exports) => {
|
|
|
354
354
|
|
|
355
355
|
// Serve TypeScript type definitions
|
|
356
356
|
if (url.searchParams.has("types") || url.pathname.endsWith(".d.ts")) {
|
|
357
|
-
const typeDefinitions = generateTypeDefinitions(exports, exportKeys);
|
|
357
|
+
const typeDefinitions = generatedTypes || generateTypeDefinitions(exports, exportKeys);
|
|
358
358
|
return new Response(typeDefinitions, {
|
|
359
359
|
headers: {
|
|
360
360
|
"Content-Type": "application/typescript; charset=utf-8",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "export-runtime",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Cloudflare Workers ESM Export Framework Runtime",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -22,12 +22,17 @@
|
|
|
22
22
|
"exports": {
|
|
23
23
|
".": "./entry.js"
|
|
24
24
|
},
|
|
25
|
+
"bin": {
|
|
26
|
+
"generate-export-types": "./bin/generate-types.mjs"
|
|
27
|
+
},
|
|
25
28
|
"files": [
|
|
26
29
|
"entry.js",
|
|
27
30
|
"handler.js",
|
|
28
|
-
"client.js"
|
|
31
|
+
"client.js",
|
|
32
|
+
"bin/generate-types.mjs"
|
|
29
33
|
],
|
|
30
34
|
"dependencies": {
|
|
31
|
-
"devalue": "^5.1.1"
|
|
35
|
+
"devalue": "^5.1.1",
|
|
36
|
+
"oxc-parser": "^0.121.0"
|
|
32
37
|
}
|
|
33
38
|
}
|