lune-std-net 0.3.1

Lune standard library - Net
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH"

type HttpQueryOrHeaderMap = { [string]: string | { string } }
export type HttpQueryMap = HttpQueryOrHeaderMap
export type HttpHeaderMap = HttpQueryOrHeaderMap

--[=[
	@interface FetchParamsOptions
	@within Net

	Extra options for `FetchParams`.

	This is a dictionary that may contain one or more of the following values:

	* `decompress` - If the request body should be automatically decompressed when possible. Defaults to `true`
]=]
export type FetchParamsOptions = {
	decompress: boolean?,
}

--[=[
	@interface FetchParams
	@within Net

	Parameters for sending network requests with `net.request`.

	This is a dictionary that may contain one or more of the following values:

	* `url` - The URL to send a request to. This is always required
	* `method` - The HTTP method verb, such as `"GET"`, `"POST"`, `"PATCH"`, `"PUT"`, or `"DELETE"`. Defaults to `"GET"`
	* `body` - The request body
	* `query` - A table of key-value pairs representing query parameters in the request path
	* `headers` - A table of key-value pairs representing headers
	* `options` - Extra options for things such as automatic decompression of response bodies
]=]
export type FetchParams = {
	url: string,
	method: HttpMethod?,
	body: (string | buffer)?,
	query: HttpQueryMap?,
	headers: HttpHeaderMap?,
	options: FetchParamsOptions?,
}

--[=[
	@interface FetchResponse
	@within Net

	Response type for sending network requests with `net.request`.

	This is a dictionary containing the following values:

	* `ok` - If the status code is a canonical success status code, meaning within the range 200 -> 299
	* `statusCode` - The status code returned for the request
	* `statusMessage` - The canonical status message for the returned status code, such as `"Not Found"` for status code 404
	* `headers` - A table of key-value pairs representing headers
	* `body` - The request body, or an empty string if one was not given
]=]
export type FetchResponse = {
	ok: boolean,
	statusCode: number,
	statusMessage: string,
	headers: HttpHeaderMap,
	body: string,
}

--[=[
	@interface ServeRequest
	@within Net

	Data type for requests in `net.serve`.

	This is a dictionary containing the following values:

	* `path` - The path being requested, relative to the root. Will be `/` if not specified
	* `query` - A table of key-value pairs representing query parameters in the request path
	* `method` - The HTTP method verb, such as `"GET"`, `"POST"`, `"PATCH"`, `"PUT"`, or `"DELETE"`. Will always be uppercase
	* `headers` - A table of key-value pairs representing headers
	* `body` - The request body, or an empty string if one was not given
]=]
export type ServeRequest = {
	path: string,
	query: { [string]: string? },
	method: HttpMethod,
	headers: { [string]: string },
	body: string,
}

--[=[
	@interface ServeResponse
	@within Net

	Response type for requests in `net.serve`.

	This is a dictionary that may contain one or more of the following values:

	* `status` - The status code for the request, in the range `100` -> `599`
	* `headers` - A table of key-value pairs representing headers
	* `body` - The response body
]=]
export type ServeResponse = {
	status: number?,
	headers: { [string]: string }?,
	body: (string | buffer)?,
}

type ServeHttpHandler = (request: ServeRequest) -> string | ServeResponse
type ServeWebSocketHandler = (socket: WebSocket) -> ()

--[=[
	@interface ServeConfig
	@within Net

	Configuration for `net.serve`.

	This may contain one of or more of the following values:

	* `address` for setting the IP address to serve from. Defaults to the loopback interface (`http://localhost`).
	* `handleRequest` for handling normal http requests, equivalent to just passing a function to `net.serve`
	* `handleWebSocket` for handling web socket requests, which will receive a `WebSocket` object as its first and only parameter

	When setting `address`, the `handleRequest` callback must also be defined.

	### Example Usage

	```luau
	net.serve(8080, {
		address = "http://0.0.0.0",
		handleRequest = function(request)
			return {
				status = 200,
				body = "Echo:\n" .. request.body,
			}
		end
	})
	```
]=]
export type ServeConfig = {
	address: string?,
	handleRequest: ServeHttpHandler?,
	handleWebSocket: ServeWebSocketHandler?,
}

--[=[
	@interface ServeHandle
	@within Net

	A handle to a currently running web server, containing a single `stop` function to gracefully shut down the web server.
]=]
export type ServeHandle = {
	stop: () -> (),
}

--[=[
	@interface WebSocket
	@within Net

	A reference to a web socket connection.

	The web socket may be in either an "open" or a "closed" state, changing its current behavior.

	When open:

	* Any function on the socket such as `send`, `next` or `close` can be called without erroring
	* `next` can be called to yield until the next message is received or the socket becomes closed

	When closed:

	* `next` will no longer return any message(s) and instead instantly return nil
	* `send` will throw an error stating that the socket has been closed

	Once the websocket has been closed, `closeCode` will no longer be nil, and will be populated with a close
	code according to the [WebSocket specification](https://www.iana.org/assignments/websocket/websocket.xhtml).
	This will be an integer between 1000 and 4999, where 1000 is the canonical code for normal, error-free closure.
]=]
export type WebSocket = {
	closeCode: number?,
	close: (self: WebSocket, code: number?) -> (),
	send: (self: WebSocket, message: (string | buffer)?, asBinaryMessage: boolean?) -> (),
	next: (self: WebSocket) -> string?,
}

--[=[
	@interface TcpConfig
	@within Net

	Configuration options for a TCP stream.

	### Example Usage

	```luau
	-- Plain TCP connection
	local stream = net.tcp.connect("example.com", 80)

	-- TLS connection (shorthand)
	local stream = net.tcp.connect("example.com", 443, true)

	-- TLS connection (explicit config)
	local stream = net.tcp.connect("example.com", 443, { tls = true })

	-- Connection with custom TTL
	local stream = net.tcp.connect("192.168.1.100", 8080, {
		tls = false,
		ttl = 128
	})
	```
]=]
export type TcpConfig = {
	--[=[
		Whether or not to use TLS encryption.

		Defaults to `false`.
	]=]
	tls: boolean?,
	--[=[
		The TTL to use for packets sent over the socket.
	]=]
	ttl: number?,
}

--[=[
	@interface TcpStream
	@within Net

	A plain TCP stream, which may also be backed by a TLS connection.

	### Example Usage

	```luau
	local net = require("@lune/net")

	local conn = net.tcp.connect("example.com", 80)

	conn:write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")

	local response = conn:read()
	print(response)

	conn:close()
	```
]=]
export type TcpStream = {
	--[=[
		The local IP address of the socket, if any.
	]=]
	localIp: string?,
	--[=[
		The local port of the socket, if any.
	]=]
	localPort: number?,
	--[=[
		The remote IP address of the socket, if any.
	]=]
	remoteIp: string?,
	--[=[
		The remote port of the socket, if any.
	]=]
	remotePort: number?,
	--[=[
		Closes the underlying I/O for the stream.

		Any writes will throw an error after this method is called.
	]=]
	close: (self: TcpStream) -> (),
	--[=[
		Writes the given data to the stream.

		- If the stream is closed, this will throw an error.
	]=]
	write: (self: TcpStream, data: string | buffer) -> (),
	--[=[
		Reads data from the stream, returning a string up to the given `size`.

		- If there is no data to read, this will yield until data is available.
		- If the stream is closed, this will return `nil`.
	]=]
	read: (self: TcpStream, size: number?) -> string?,
}

--[=[
	TCP primitives for the `net` library

	Provides low-level TCP socket functionality for creating custom network
	protocols or communicating with services that don't use HTTP - for all
	HTTP usage, please use the `request` and `serve` HTTP functions instead.
]=]
local tcp = {}

--[=[
	Connects to the given host and port, returning a `TcpStream`.

	For additional details, see the documentation for the `TcpConfig` and `TcpStream` types.

	Will throw an error if the connection fails.

	@param host The host to connect to, either a DNS name or IP address
	@param port The port to connect to
	@param config The optional configuration to use for the stream
	@return A connected TcpStream ready for reading and writing
]=]
function tcp.connect(host: string, port: number, config: (true | TcpConfig)?): TcpStream
	return nil :: any
end

--[=[
	@class Net

	Built-in library for network access

	### Example usage

	```luau
	local net = require("@lune/net")
	local serde = require("@lune/serde")

	-- Sending a web request
	local response = net.request("https://www.google.com")
	print(response.ok)
	print(response.statusCode, response.statusMessage)
	print(response.headers)

	-- Using a JSON web API
	local response = net.request({
		url = "https://dummyjson.com/products/add",
		method = "POST",
		headers = { ["Content-Type"] = "application/json" },
		body = serde.encode("json", {
			title = "Cool Pencil",
		})
	})
	local product = serde.decode("json", response.body)
	print(product.id, "-", product.title)

	-- Starting up an http server
	net.serve(8080, function(request)
		return {
			status = 200,
			body = "Echo:\n" .. request.body,
		}
	end)

	-- Writing to a plain TCP stream
	local conn = net.tcp.connect("example.com", 80)

	conn:write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
	```
]=]
local net = {}

net.tcp = tcp

--[=[
	@within Net

	Sends an HTTP request using the given url and / or parameters, and returns a dictionary that describes the response received.

	Only throws an error if a miscellaneous network or I/O error occurs, never for unsuccessful status codes.

	@param config The URL or request config to use
	@return A dictionary representing the response for the request
]=]
function net.request(config: string | FetchParams): FetchResponse
	return nil :: any
end

--[=[
	@within Net
	@tag must_use

	Connects to a web socket at the given URL.

	Throws an error if the server at the given URL does not support
	web sockets, or if a miscellaneous network or I/O error occurs.

	@param url The URL to connect to
	@return A web socket handle
]=]
function net.socket(url: string): WebSocket
	return nil :: any
end

--[=[
	@within Net

	Creates an HTTP server that listens on the given `port`.

	This will ***not*** block and will keep listening for requests on the given `port`
	until the `stop` function on the returned `ServeHandle` has been called.

	@param port The port to use for the server
	@param handlerOrConfig The handler function or config to use for the server
]=]
function net.serve(port: number, handlerOrConfig: ServeHttpHandler | ServeConfig): ServeHandle
	return nil :: any
end

--[=[
	@within Net
	@tag must_use

	Encodes the given string using URL encoding.

	@param s The string to encode
	@param binary If the string should be treated as binary data and/or is not valid utf-8. Defaults to false
	@return The encoded string
]=]
function net.urlEncode(s: string, binary: boolean?): string
	return nil :: any
end

--[=[
	@within Net
	@tag must_use

	Decodes the given string using URL decoding.

	@param s The string to decode
	@param binary If the string should be treated as binary data and/or is not valid utf-8. Defaults to false
	@return The decoded string
]=]
function net.urlDecode(s: string, binary: boolean?): string
	return nil :: any
end

return net