@noita-ts/pollnet 0.3.8 → 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/index.d.ts CHANGED
@@ -1,215 +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
- * DISCLAIMER:
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
- /** @noSelfInFile */
8
-
9
- declare namespace pollnet {
10
- const VERSION: string;
11
-
12
- type SocketStatus =
13
- | "unpolled"
14
- | "invalid"
15
- | "invalid_handle"
16
- | "open"
17
- | "opening"
18
- | "error"
19
- | "closed";
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 */
150
- on_connection(callback: (sock: Socket, addr: string) => void): this;
151
-
152
- /** Polls for socket events */
153
- poll(): LuaMultiReturn<[true, undefined]> | LuaMultiReturn<[false, string]>;
154
-
155
- /** Awaits a message (coroutine.yield) */
156
- await():
157
- LuaMultiReturn<[string, undefined]> | LuaMultiReturn<[false, string]>;
158
-
159
- /** Awaits N messages */
160
- await_n(
161
- count: number,
162
- ): LuaMultiReturn<[string[], undefined]> | LuaMultiReturn<[false, string]>;
163
-
164
- /** Gets the last message */
165
- last_message(): string | undefined;
166
-
167
- /** Gets the status */
168
- status(): SocketStatus | undefined;
169
-
170
- /** Sends a string message */
171
- send(msg: string): void;
172
-
173
- /** Sends a binary message */
174
- send_binary(msg: string): void;
175
-
176
- /** Closes the socket */
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>;
34
+ /**
35
+ * Perform an HTTP POST with an optional request `body`.
36
+ */
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;
177
54
  close(): void;
178
-
179
- /** Is HTTP server */
180
- is_http_server?: boolean;
181
-
182
- /** Parent socket (for server connections) */
183
- parent?: Socket;
184
-
185
- /** Remote address (for server connections) */
186
- remote_addr?: string;
187
-
188
- /** Timeout for await */
189
- timeout?: number;
190
- }
191
-
192
- function Reactor(): Reactor;
193
-
194
- interface Reactor {
195
- /** Initializes a reactor */
196
- init(): void;
197
-
198
- /** Runs a coroutine thread */
199
- run(thread_body: (this: Reactor) => void): void;
200
-
201
- /** Runs a server with client handler */
202
- run_server(
203
- server_sock: Socket,
204
- client_body: (sock: Socket, addr: string) => void,
205
- ): void;
206
-
207
- /** Logs messages */
208
- log(...args: any[]): void;
209
-
210
- /** Updates reactor (runs threads) */
211
- update(): number;
212
- }
213
55
  }
214
-
215
- export default pollnet;
56
+ export declare const runServer: (scheduler: Scheduler, serverSocket: pollnet.Socket, clientBody: (clientSocket: pollnet.Socket, addr: string) => void) => void;
57
+ export {};