@noita-ts/pollnet 0.4.0 → 0.5.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/dist/api.d.ts +221 -0
- package/dist/api.lua +490 -0
- package/dist/index.d.ts +51 -215
- package/dist/index.lua +171 -490
- package/dist/lualib_bundle.lua +7 -211
- package/package.json +4 -4
- package/dist/async.d.ts +0 -92
- package/dist/async.lua +0 -249
package/dist/index.d.ts
CHANGED
|
@@ -1,221 +1,57 @@
|
|
|
1
|
+
/** @noSelfInFile */
|
|
2
|
+
import pollnet from "./api";
|
|
3
|
+
export declare class HttpResponse {
|
|
4
|
+
status: number;
|
|
5
|
+
headers: string;
|
|
6
|
+
body: string;
|
|
7
|
+
constructor(status: number, headers: string, body: string);
|
|
8
|
+
static read(socket: pollnet.Socket): HttpResponse;
|
|
9
|
+
/**
|
|
10
|
+
* Parse the raw header block into a map.
|
|
11
|
+
* Header names seen more than once collapse into an array of their values, in
|
|
12
|
+
* order.
|
|
13
|
+
*/
|
|
14
|
+
parseHeaders(): Record<string, string | string[]>;
|
|
15
|
+
}
|
|
1
16
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Similar to nxml, this typedef was semi-vibecoded, this will likely be tested
|
|
4
|
-
* more thoroughly though.
|
|
17
|
+
* Import and set up a `Scheduler` from `@noita-ts/base/async` for this.
|
|
5
18
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
/** Initializes the pollnet context */
|
|
22
|
-
function init(): void;
|
|
23
|
-
|
|
24
|
-
/** Initializes the pollnet context using the static hack */
|
|
25
|
-
function init_hack_static(): void;
|
|
26
|
-
|
|
27
|
-
/** Shuts down the pollnet context */
|
|
28
|
-
function shutdown(): void;
|
|
29
|
-
|
|
30
|
-
/** Sleeps (blocks) for the given milliseconds */
|
|
31
|
-
function sleep_ms(ms: number): void;
|
|
32
|
-
|
|
33
|
-
/** Generates a nanoid string */
|
|
34
|
-
function nanoid(): string;
|
|
35
|
-
|
|
36
|
-
/** Formats headers as a string */
|
|
37
|
-
function format_headers(
|
|
38
|
-
headers: Record<string, string | string[]> | string,
|
|
39
|
-
): string;
|
|
40
|
-
|
|
41
|
-
/** Parses headers from a string */
|
|
42
|
-
function parse_headers(headers: string): Record<string, string>;
|
|
43
|
-
|
|
44
|
-
/** Parses HTTP method line */
|
|
45
|
-
function parse_method(
|
|
46
|
-
line: string,
|
|
47
|
-
): LuaMultiReturn<
|
|
48
|
-
[method: string, path: string, query: Record<string, string>]
|
|
49
|
-
>;
|
|
50
|
-
|
|
51
|
-
/** Wraps a request handler for HTTP server */
|
|
52
|
-
function wrap_req_handler(
|
|
53
|
-
handler: (req: {
|
|
54
|
-
addr: string;
|
|
55
|
-
method: string;
|
|
56
|
-
path: string;
|
|
57
|
-
query: Record<string, string>;
|
|
58
|
-
headers: Record<string, string>;
|
|
59
|
-
body: string;
|
|
60
|
-
raw: string[];
|
|
61
|
-
}) => {
|
|
62
|
-
status?: string;
|
|
63
|
-
headers?: Record<string, string | string[]>;
|
|
64
|
-
body?: string;
|
|
65
|
-
},
|
|
66
|
-
expose_errors?: boolean,
|
|
67
|
-
): (req_sock: Socket, addr: string) => void;
|
|
68
|
-
|
|
69
|
-
/** Opens a websocket client */
|
|
70
|
-
function open_ws(url: string): Socket;
|
|
71
|
-
|
|
72
|
-
/** Listens for websocket connections */
|
|
73
|
-
function listen_ws(
|
|
74
|
-
addr: string,
|
|
75
|
-
callback?: (sock: Socket, addr: string) => void,
|
|
76
|
-
): Socket;
|
|
77
|
-
|
|
78
|
-
/** Opens a TCP client */
|
|
79
|
-
function open_tcp(addr: string): Socket;
|
|
80
|
-
|
|
81
|
-
/** Listens for TCP connections */
|
|
82
|
-
function listen_tcp(
|
|
83
|
-
addr: string,
|
|
84
|
-
callback?: (sock: Socket, addr: string) => void,
|
|
85
|
-
): Socket;
|
|
86
|
-
|
|
87
|
-
/** Serves HTTP (static or dynamic) */
|
|
88
|
-
function serve_http(addr: string, dir?: string): Socket;
|
|
89
|
-
|
|
90
|
-
/** Serves dynamic HTTP */
|
|
91
|
-
function serve_dynamic_http(
|
|
92
|
-
addr: string,
|
|
93
|
-
keep_alive?: boolean,
|
|
94
|
-
callback?: (sock: Socket, addr: string) => void,
|
|
95
|
-
): Socket;
|
|
96
|
-
|
|
97
|
-
/** Performs an HTTP GET */
|
|
98
|
-
function http_get(
|
|
99
|
-
url: string,
|
|
100
|
-
headers?: Record<string, string | string[]> | string,
|
|
101
|
-
ret_body_only?: boolean,
|
|
102
|
-
): Socket;
|
|
103
|
-
|
|
104
|
-
/** Performs an HTTP POST */
|
|
105
|
-
function http_post(
|
|
106
|
-
url: string,
|
|
107
|
-
headers?: Record<string, string | string[]> | string,
|
|
108
|
-
body?: string,
|
|
109
|
-
ret_body_only?: boolean,
|
|
110
|
-
): Socket;
|
|
111
|
-
|
|
112
|
-
function Socket(): Socket;
|
|
113
|
-
|
|
114
|
-
interface Socket {
|
|
115
|
-
/** Opens a websocket client */
|
|
116
|
-
open_ws(url: string): this;
|
|
117
|
-
|
|
118
|
-
/** Opens a TCP client */
|
|
119
|
-
open_tcp(addr: string): this;
|
|
120
|
-
|
|
121
|
-
/** Serves HTTP (static or dynamic) */
|
|
122
|
-
serve_http(addr: string, dir?: string): this;
|
|
123
|
-
|
|
124
|
-
/** Adds a virtual file to HTTP server */
|
|
125
|
-
add_virtual_file(filename: string, filedata: string): void;
|
|
126
|
-
|
|
127
|
-
/** Removes a virtual file from HTTP server */
|
|
128
|
-
remove_virtual_file(filename: string): void;
|
|
129
|
-
|
|
130
|
-
/** Listens for websocket connections */
|
|
131
|
-
listen_ws(
|
|
132
|
-
addr: string,
|
|
133
|
-
callback?: (sock: Socket, addr: string) => void,
|
|
134
|
-
): this;
|
|
135
|
-
|
|
136
|
-
/** Listens for TCP connections */
|
|
137
|
-
listen_tcp(
|
|
138
|
-
addr: string,
|
|
139
|
-
callback?: (sock: Socket, addr: string) => void,
|
|
140
|
-
): this;
|
|
141
|
-
|
|
142
|
-
/** Serves dynamic HTTP */
|
|
143
|
-
serve_dynamic_http(
|
|
144
|
-
addr: string,
|
|
145
|
-
keep_alive?: boolean,
|
|
146
|
-
callback?: (sock: Socket, addr: string) => void,
|
|
147
|
-
): this;
|
|
148
|
-
|
|
149
|
-
/** Sets a connection handler for server sockets */
|
|
150
|
-
on_connection(callback: (sock: Socket, addr: string) => void): this;
|
|
151
|
-
|
|
19
|
+
interface Scheduler {
|
|
20
|
+
spawn<T>(body: () => T): Promise<T>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Tiny async HTTP client built on top of a {@link Scheduler}.
|
|
24
|
+
*
|
|
25
|
+
* Requests run in coroutines and resolve to an {@link HttpResponse}.
|
|
26
|
+
*/
|
|
27
|
+
export declare class HttpClient {
|
|
28
|
+
private scheduler;
|
|
29
|
+
constructor(scheduler: Scheduler);
|
|
30
|
+
/**
|
|
31
|
+
* Perform an HTTP GET.
|
|
32
|
+
*/
|
|
33
|
+
get(url: string, headers?: Record<string, string | string[]> | string): Promise<HttpResponse>;
|
|
152
34
|
/**
|
|
153
|
-
*
|
|
154
|
-
* Returns [ok, message], where on success the message may or may not be
|
|
155
|
-
* available (non-blocking), and on failure the message is an error string.
|
|
35
|
+
* Perform an HTTP POST with an optional request `body`.
|
|
156
36
|
*/
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
status(): SocketStatus | undefined;
|
|
175
|
-
|
|
176
|
-
/** Sends a string message */
|
|
177
|
-
send(msg: string): void;
|
|
178
|
-
|
|
179
|
-
/** Sends a binary message */
|
|
180
|
-
send_binary(msg: string): void;
|
|
181
|
-
|
|
182
|
-
/** Closes the socket */
|
|
37
|
+
post(url: string, headers?: Record<string, string | string[]> | string, body?: string): Promise<HttpResponse>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Async WebSocket client built on top of an {@link AsyncReactor}.
|
|
41
|
+
* Messages are delivered via the `onmessage` callback, and errors via
|
|
42
|
+
* `onerror`.
|
|
43
|
+
* Call {@link send} to send a message, and {@link close} to close the
|
|
44
|
+
* connection and cleanup.
|
|
45
|
+
*/
|
|
46
|
+
export declare class WebSocket {
|
|
47
|
+
private socket;
|
|
48
|
+
timeout: number;
|
|
49
|
+
onmessage: ((msg: string) => void) | null;
|
|
50
|
+
onerror: ((msg: string) => void) | null;
|
|
51
|
+
constructor(scheduler: Scheduler, url: string);
|
|
52
|
+
send(message: string): void;
|
|
53
|
+
sendBinary(data: string): void;
|
|
183
54
|
close(): void;
|
|
184
|
-
|
|
185
|
-
/** Is HTTP server */
|
|
186
|
-
is_http_server?: boolean;
|
|
187
|
-
|
|
188
|
-
/** Parent socket (for server connections) */
|
|
189
|
-
parent?: Socket;
|
|
190
|
-
|
|
191
|
-
/** Remote address (for server connections) */
|
|
192
|
-
remote_addr?: string;
|
|
193
|
-
|
|
194
|
-
/** Timeout for await */
|
|
195
|
-
timeout?: number;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function Reactor(): Reactor;
|
|
199
|
-
|
|
200
|
-
interface Reactor {
|
|
201
|
-
/** Initializes a reactor */
|
|
202
|
-
init(): void;
|
|
203
|
-
|
|
204
|
-
/** Runs a coroutine thread */
|
|
205
|
-
run(thread_body: (this: Reactor) => void): void;
|
|
206
|
-
|
|
207
|
-
/** Runs a server with client handler */
|
|
208
|
-
run_server(
|
|
209
|
-
server_sock: Socket,
|
|
210
|
-
client_body: (sock: Socket, addr: string) => void,
|
|
211
|
-
): void;
|
|
212
|
-
|
|
213
|
-
/** Logs messages */
|
|
214
|
-
log(...args: any[]): void;
|
|
215
|
-
|
|
216
|
-
/** Updates reactor (runs threads) */
|
|
217
|
-
update(): number;
|
|
218
|
-
}
|
|
219
55
|
}
|
|
220
|
-
|
|
221
|
-
export
|
|
56
|
+
export declare const runServer: (scheduler: Scheduler, serverSocket: pollnet.Socket, clientBody: (clientSocket: pollnet.Socket, addr: string) => void) => void;
|
|
57
|
+
export {};
|