@ricsam/quickjs-fetch 0.2.11 → 0.2.12
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/dist/cjs/package.json +1 -1
- package/dist/mjs/package.json +1 -1
- package/dist/types/quickjs.d.ts +241 -0
- package/package.json +5 -2
package/dist/cjs/package.json
CHANGED
package/dist/mjs/package.json
CHANGED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QuickJS Global Type Definitions for @ricsam/quickjs-fetch
|
|
3
|
+
*
|
|
4
|
+
* These types define the globals injected by setupFetch() into a QuickJS context.
|
|
5
|
+
* Use these types to typecheck user code that will run inside QuickJS.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Typecheck QuickJS code with serve()
|
|
9
|
+
* serve({
|
|
10
|
+
* fetch(request, server) {
|
|
11
|
+
* if (request.url.includes("/ws")) {
|
|
12
|
+
* server.upgrade(request, { data: { id: 123 } });
|
|
13
|
+
* return new Response(null, { status: 101 });
|
|
14
|
+
* }
|
|
15
|
+
* return new Response("Hello!");
|
|
16
|
+
* },
|
|
17
|
+
* websocket: {
|
|
18
|
+
* message(ws, message) {
|
|
19
|
+
* ws.send("Echo: " + message);
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* });
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export {};
|
|
26
|
+
|
|
27
|
+
declare global {
|
|
28
|
+
// ============================================
|
|
29
|
+
// Standard Fetch API (from lib.dom)
|
|
30
|
+
// ============================================
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Headers class for HTTP headers manipulation.
|
|
34
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Headers
|
|
35
|
+
*/
|
|
36
|
+
const Headers: typeof globalThis.Headers;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Request class for HTTP requests.
|
|
40
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Request
|
|
41
|
+
*/
|
|
42
|
+
const Request: typeof globalThis.Request;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Response class for HTTP responses.
|
|
46
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response
|
|
47
|
+
*/
|
|
48
|
+
const Response: typeof globalThis.Response;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* AbortController for cancelling fetch requests.
|
|
52
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortController
|
|
53
|
+
*/
|
|
54
|
+
const AbortController: typeof globalThis.AbortController;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* AbortSignal for listening to abort events.
|
|
58
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
|
|
59
|
+
*/
|
|
60
|
+
const AbortSignal: typeof globalThis.AbortSignal;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* FormData for constructing form data.
|
|
64
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/FormData
|
|
65
|
+
*/
|
|
66
|
+
const FormData: typeof globalThis.FormData;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Fetch function for making HTTP requests.
|
|
70
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/fetch
|
|
71
|
+
*/
|
|
72
|
+
function fetch(
|
|
73
|
+
input: RequestInfo | URL,
|
|
74
|
+
init?: RequestInit
|
|
75
|
+
): Promise<Response>;
|
|
76
|
+
|
|
77
|
+
// ============================================
|
|
78
|
+
// QuickJS-specific: serve() API
|
|
79
|
+
// ============================================
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Server interface for handling WebSocket upgrades within serve() handlers.
|
|
83
|
+
*/
|
|
84
|
+
interface Server {
|
|
85
|
+
/**
|
|
86
|
+
* Upgrade an HTTP request to a WebSocket connection.
|
|
87
|
+
*
|
|
88
|
+
* @param request - The incoming HTTP request to upgrade
|
|
89
|
+
* @param options - Optional data to associate with the WebSocket connection
|
|
90
|
+
* @returns true if upgrade will proceed, false otherwise
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* serve({
|
|
94
|
+
* fetch(request, server) {
|
|
95
|
+
* if (server.upgrade(request, { data: { userId: 123 } })) {
|
|
96
|
+
* return new Response(null, { status: 101 });
|
|
97
|
+
* }
|
|
98
|
+
* return new Response("Upgrade failed", { status: 400 });
|
|
99
|
+
* }
|
|
100
|
+
* });
|
|
101
|
+
*/
|
|
102
|
+
upgrade(request: Request, options?: { data?: unknown }): boolean;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* ServerWebSocket interface for WebSocket connections within serve() handlers.
|
|
107
|
+
*
|
|
108
|
+
* @typeParam T - The type of data associated with this WebSocket connection
|
|
109
|
+
*/
|
|
110
|
+
interface ServerWebSocket<T = unknown> {
|
|
111
|
+
/**
|
|
112
|
+
* User data associated with this connection.
|
|
113
|
+
* Set via `server.upgrade(request, { data: ... })`.
|
|
114
|
+
*/
|
|
115
|
+
readonly data: T;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Send a message to the client.
|
|
119
|
+
*
|
|
120
|
+
* @param message - The message to send (string, ArrayBuffer, or Uint8Array)
|
|
121
|
+
*/
|
|
122
|
+
send(message: string | ArrayBuffer | Uint8Array): void;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Close the WebSocket connection.
|
|
126
|
+
*
|
|
127
|
+
* @param code - Optional close code (default: 1000)
|
|
128
|
+
* @param reason - Optional close reason
|
|
129
|
+
*/
|
|
130
|
+
close(code?: number, reason?: string): void;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* WebSocket ready state.
|
|
134
|
+
* - 0: CONNECTING
|
|
135
|
+
* - 1: OPEN
|
|
136
|
+
* - 2: CLOSING
|
|
137
|
+
* - 3: CLOSED
|
|
138
|
+
*/
|
|
139
|
+
readonly readyState: number;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Options for the serve() function.
|
|
144
|
+
*
|
|
145
|
+
* @typeParam T - The type of data associated with WebSocket connections
|
|
146
|
+
*/
|
|
147
|
+
interface ServeOptions<T = unknown> {
|
|
148
|
+
/**
|
|
149
|
+
* Handler for HTTP requests.
|
|
150
|
+
*
|
|
151
|
+
* @param request - The incoming HTTP request
|
|
152
|
+
* @param server - Server interface for WebSocket upgrades
|
|
153
|
+
* @returns Response or Promise resolving to Response
|
|
154
|
+
*/
|
|
155
|
+
fetch(request: Request, server: Server): Response | Promise<Response>;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* WebSocket event handlers.
|
|
159
|
+
*/
|
|
160
|
+
websocket?: {
|
|
161
|
+
/**
|
|
162
|
+
* Called when a WebSocket connection is opened.
|
|
163
|
+
*
|
|
164
|
+
* @param ws - The WebSocket connection
|
|
165
|
+
*/
|
|
166
|
+
open?(ws: ServerWebSocket<T>): void | Promise<void>;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Called when a message is received.
|
|
170
|
+
*
|
|
171
|
+
* @param ws - The WebSocket connection
|
|
172
|
+
* @param message - The received message (string or ArrayBuffer)
|
|
173
|
+
*/
|
|
174
|
+
message?(
|
|
175
|
+
ws: ServerWebSocket<T>,
|
|
176
|
+
message: string | ArrayBuffer
|
|
177
|
+
): void | Promise<void>;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Called when the connection is closed.
|
|
181
|
+
*
|
|
182
|
+
* @param ws - The WebSocket connection
|
|
183
|
+
* @param code - The close code
|
|
184
|
+
* @param reason - The close reason
|
|
185
|
+
*/
|
|
186
|
+
close?(
|
|
187
|
+
ws: ServerWebSocket<T>,
|
|
188
|
+
code: number,
|
|
189
|
+
reason: string
|
|
190
|
+
): void | Promise<void>;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Called when an error occurs.
|
|
194
|
+
*
|
|
195
|
+
* @param ws - The WebSocket connection
|
|
196
|
+
* @param error - The error that occurred
|
|
197
|
+
*/
|
|
198
|
+
error?(ws: ServerWebSocket<T>, error: Error): void | Promise<void>;
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Register an HTTP server handler in QuickJS.
|
|
204
|
+
*
|
|
205
|
+
* Only one serve() handler can be active at a time.
|
|
206
|
+
* Calling serve() again replaces the previous handler.
|
|
207
|
+
*
|
|
208
|
+
* @param options - Server configuration including fetch handler and optional WebSocket handlers
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* serve({
|
|
212
|
+
* fetch(request, server) {
|
|
213
|
+
* const url = new URL(request.url);
|
|
214
|
+
*
|
|
215
|
+
* if (url.pathname === "/ws") {
|
|
216
|
+
* if (server.upgrade(request, { data: { connectedAt: Date.now() } })) {
|
|
217
|
+
* return new Response(null, { status: 101 });
|
|
218
|
+
* }
|
|
219
|
+
* }
|
|
220
|
+
*
|
|
221
|
+
* if (url.pathname === "/api/hello") {
|
|
222
|
+
* return Response.json({ message: "Hello!" });
|
|
223
|
+
* }
|
|
224
|
+
*
|
|
225
|
+
* return new Response("Not Found", { status: 404 });
|
|
226
|
+
* },
|
|
227
|
+
* websocket: {
|
|
228
|
+
* open(ws) {
|
|
229
|
+
* console.log("Connected at:", ws.data.connectedAt);
|
|
230
|
+
* },
|
|
231
|
+
* message(ws, message) {
|
|
232
|
+
* ws.send("Echo: " + message);
|
|
233
|
+
* },
|
|
234
|
+
* close(ws, code, reason) {
|
|
235
|
+
* console.log("Closed:", code, reason);
|
|
236
|
+
* }
|
|
237
|
+
* }
|
|
238
|
+
* });
|
|
239
|
+
*/
|
|
240
|
+
function serve<T = unknown>(options: ServeOptions<T>): void;
|
|
241
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ricsam/quickjs-fetch",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
4
4
|
"main": "./dist/cjs/index.cjs",
|
|
5
5
|
"types": "./dist/types/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
"types": "./dist/types/index.d.ts",
|
|
9
9
|
"require": "./dist/cjs/index.cjs",
|
|
10
10
|
"import": "./dist/mjs/index.mjs"
|
|
11
|
+
},
|
|
12
|
+
"./quickjs": {
|
|
13
|
+
"types": "./dist/types/quickjs.d.ts"
|
|
11
14
|
}
|
|
12
15
|
},
|
|
13
16
|
"scripts": {
|
|
@@ -16,7 +19,7 @@
|
|
|
16
19
|
"typecheck": "tsc --noEmit"
|
|
17
20
|
},
|
|
18
21
|
"dependencies": {
|
|
19
|
-
"@ricsam/quickjs-core": "^0.2.
|
|
22
|
+
"@ricsam/quickjs-core": "^0.2.11",
|
|
20
23
|
"quickjs-emscripten": "^0.31.0"
|
|
21
24
|
},
|
|
22
25
|
"peerDependencies": {
|