Skip to content

Commit d1eda84

Browse files
authored
Merge pull request #867 from flrgh/chore/update-lua-resty-websocket-annotations
chore: update lua-resty-websocket library annotations
2 parents b645238 + 9941d7a commit d1eda84

File tree

4 files changed

+246
-29
lines changed

4 files changed

+246
-29
lines changed
Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,63 @@
11
---@meta
2-
resty_websocket_client={}
3-
function resty_websocket_client.send_text(self, data) end
4-
function resty_websocket_client.new(self, opts) end
5-
function resty_websocket_client.send_ping(self, data) end
6-
function resty_websocket_client.connect(self, uri, opts) end
7-
function resty_websocket_client.set_timeout(self, time) end
8-
function resty_websocket_client.set_keepalive(self, ...) end
9-
function resty_websocket_client.send_binary(self, data) end
10-
function resty_websocket_client.send_close() end
11-
function resty_websocket_client.send_frame() end
12-
function resty_websocket_client.recv_frame(self) end
13-
function resty_websocket_client.close(self) end
14-
resty_websocket_client._VERSION="0.07"
15-
function resty_websocket_client.send_pong(self, data) end
2+
3+
---@class resty.websocket.client : resty.websocket
4+
resty_websocket_client = {
5+
_VERSION = "0.09"
6+
}
7+
8+
---Instantiates a WebSocket client object.
9+
---
10+
---In case of error, it returns nil and a string describing the error.
11+
---
12+
---An optional options table can be specified.
13+
---
14+
---@param opts? resty.websocket.new.opts
15+
---@return resty.websocket.client? client
16+
---@return string? error
17+
function resty_websocket_client:new(opts) end
18+
19+
---Connects to the remote WebSocket service port and performs the websocket
20+
---handshake process on the client side.
21+
---
22+
---Before actually resolving the host name and connecting to the remote backend,
23+
---this method will always look up the connection pool for matched idle
24+
---connections created by previous calls of this method.
25+
---
26+
---@param url string
27+
---@param opts? resty.websocket.client.connect.opts
28+
---@return boolean ok
29+
---@return string? error
30+
function resty_websocket_client:connect(uri, opts) end
31+
32+
--- Puts the current WebSocket connection immediately into the ngx_lua cosocket connection pool.
33+
---
34+
--- You can specify the max idle timeout (in ms) when the connection is in the pool and the maximal size of the pool every nginx worker process.
35+
---
36+
--- In case of success, returns 1. In case of errors, returns nil with a string describing the error.
37+
---
38+
--- Only call this method in the place you would have called the close method instead. Calling this method will immediately turn the current WebSocket object into the closed state. Any subsequent operations other than connect() on the current object will return the closed error.
39+
----
40+
---@param max_idle_timeout number
41+
---@param pool_size integer
42+
---@return boolean ok
43+
---@return string? error
44+
function resty_websocket_client:set_keepalive(max_idle_timeout, pool_size) end
45+
46+
---Closes the current WebSocket connection.
47+
---
48+
---If no close frame is sent yet, then the close frame will be automatically sent.
49+
---
50+
---@return boolean ok
51+
---@return string? error
52+
function resty_websocket_client:close() end
53+
54+
---@class resty.websocket.client.connect.opts : table
55+
---
56+
---@field protocols string|string[] subprotocol(s) used for the current WebSocket session
57+
---@field origin string the value of the Origin request header
58+
---@field pool string custom name for the connection pool being used. If omitted, then the connection pool name will be generated from the string template <host>:<port>.
59+
---@field ssl_verify boolean whether to perform SSL certificate verification during the SSL handshake if the wss:// scheme is used.
60+
---@field headers string[] custom headers to be sent in the handshake request. The table is expected to contain strings in the format {"a-header: a header value", "another-header: another header value"}.
61+
62+
1663
return resty_websocket_client
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---@meta
2+
3+
--- websocket object
4+
--- https://github.com/openresty/lua-resty-websocket
5+
---
6+
---@class resty.websocket : table
7+
---@field sock tcpsock
8+
---@field fatal boolean
9+
---@field max_payload_len number
10+
---@field send_masked boolean
11+
resty_websocket = {}
12+
13+
---@param ms integer sets the timeout delay (in milliseconds) for the network-related operations
14+
function resty_websocket:set_timeout(ms) end
15+
16+
---Sends the text argument out as an unfragmented data frame of the text type.
17+
---
18+
---Returns the number of bytes that have actually been sent on the TCP level.
19+
---
20+
---In case of errors, returns nil and a string describing the error.
21+
---
22+
---@param text string
23+
---@return integer? bytes
24+
---@return string? error
25+
function resty_websocket:send_text(text) end
26+
27+
---Sends the data argument out as an unfragmented data frame of the binary type.
28+
---
29+
---Returns the number of bytes that have actually been sent on the TCP level.
30+
---
31+
---In case of errors, returns nil and a string describing the error.
32+
---
33+
---@param data string
34+
---@return integer? bytes
35+
---@return string? error
36+
function resty_websocket:send_binary(data) end
37+
38+
---Sends out a ping frame with an optional message specified by the msg argument.
39+
---Returns the number of bytes that have actually been sent on the TCP level.
40+
---
41+
---In case of errors, returns nil and a string describing the error.
42+
---
43+
---Note that this method does not wait for a pong frame from the remote end.
44+
---
45+
---@param msg? string
46+
---@return integer? bytes
47+
---@return string? error
48+
function resty_websocket:send_ping(msg) end
49+
50+
---Sends out a pong frame with an optional message specified by the msg argument.
51+
---Returns the number of bytes that have actually been sent on the TCP level.
52+
---
53+
---In case of errors, returns nil and a string describing the error.
54+
---@param msg? string
55+
---@return integer? bytes
56+
---@return string? error
57+
function resty_websocket:send_pong(msg) end
58+
59+
---Sends out a close frame with an optional status code and a message.
60+
---
61+
---In case of errors, returns nil and a string describing the error.
62+
---
63+
---For a list of valid status code, see the following document:
64+
---
65+
---http://tools.ietf.org/html/rfc6455#section-7.4.1
66+
---
67+
---Note that this method does not wait for a close frame from the remote end.
68+
---@param code? integer
69+
---@param msg? string
70+
---@return integer? bytes
71+
---@return string? error
72+
function resty_websocket:send_close(code, msg) end
73+
74+
---Sends out a raw websocket frame by specifying the fin field (boolean value), the opcode, and the payload.
75+
---
76+
---For a list of valid opcode, see
77+
---
78+
---http://tools.ietf.org/html/rfc6455#section-5.2
79+
---
80+
---In case of errors, returns nil and a string describing the error.
81+
---
82+
---To control the maximal payload length allowed, you can pass the max_payload_len option to the new constructor.
83+
---
84+
---To control whether to send masked frames, you can pass true to the send_masked option in the new constructor method. By default, unmasked frames are sent.
85+
---@param fin boolean
86+
---@param opcode resty.websocket.protocol.opcode
87+
---@param payload string
88+
---@return integer? bytes
89+
---@return string? error
90+
function resty_websocket:send_frame(fin, opcode, payload) end
91+
92+
---Receives a WebSocket frame from the wire.
93+
---
94+
---In case of an error, returns two nil values and a string describing the error.
95+
---
96+
---The second return value is always the frame type, which could be one of continuation, text, binary, close, ping, pong, or nil (for unknown types).
97+
---
98+
---For close frames, returns 3 values: the extra status message (which could be an empty string), the string "close", and a Lua number for the status code (if any). For possible closing status codes, see
99+
---
100+
---http://tools.ietf.org/html/rfc6455#section-7.4.1
101+
---
102+
---For other types of frames, just returns the payload and the type.
103+
---
104+
---For fragmented frames, the err return value is the Lua string "again".
105+
---
106+
---@return string? data
107+
---@return resty.websocket.protocol.type? typ
108+
---@return string|integer? error_or_status_code
109+
function resty_websocket:recv_frame() end
110+
111+
---@class resty.websocket.new.opts : table
112+
---@field max_payload_len integer maximal length of payload allowed when sending and receiving WebSocket frames
113+
---@field send_masked boolean whether to send out masked WebSocket frames
114+
---@field timeout integer network timeout threshold in milliseconds
115+
116+
return resty_websocket
Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,59 @@
11
---@meta
2-
resty_websocket_protocol={}
3-
function resty_websocket_protocol.build_frame() end
4-
function resty_websocket_protocol.new_tab() end
2+
3+
---@class resty.websocket.protocol
4+
resty_websocket_protocol = {
5+
_VERSION = "0.09",
6+
}
7+
8+
--- Websocket op code
9+
---
10+
--- Defines the interpretation of the payload data.
11+
---
12+
--- See RFC 6455 section 5.2
13+
---
14+
---@alias resty.websocket.protocol.opcode
15+
---| '0x0' # continuation
16+
---| '0x1' # text
17+
---| '0x2' # binary
18+
---| '0x8' # close
19+
---| '0x9' # ping
20+
---| '0xa' # pong
21+
22+
---@alias resty.websocket.protocol.type
23+
---| '"continuation"'
24+
---| '"text"'
25+
---| '"binary"'
26+
---| '"close"'
27+
---| '"ping"'
28+
---| '"pong"'
29+
30+
--- Builds a raw WebSocket frame.
31+
---@param fin boolean
32+
---@param opcode resty.websocket.protocol.opcode
33+
---@param payload_len integer
34+
---@param payload string
35+
---@param masking boolean
36+
---@return string
37+
function resty_websocket_protocol.build_frame(fin, opcode, payload_len, payload, masking) end
38+
39+
--- Sends a raw WebSocket frame.
40+
---@param sock tcpsock
41+
---@param fin boolean
42+
---@param opcode resty.websocket.protocol.opcode
43+
---@param payload string
44+
---@param max_payload_len interger
45+
---@param masking boolean
46+
---@return bytes? number
47+
---@return string? error
548
function resty_websocket_protocol.send_frame(sock, fin, opcode, payload, max_payload_len, masking) end
6-
resty_websocket_protocol._VERSION="0.07"
49+
50+
--- Receives a WebSocket frame from the wire.
51+
---@param sock tcpsock
52+
---@param max_payload_len interger
53+
---@param force_masking boolean
54+
---@return string? data
55+
---@return resty.websocket.protocol.type? typ
56+
---@return string? error
757
function resty_websocket_protocol.recv_frame(sock, max_payload_len, force_masking) end
58+
859
return resty_websocket_protocol
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
---@meta
2-
resty_websocket_server={}
3-
function resty_websocket_server.send_text(self, data) end
4-
function resty_websocket_server.new(self, opts) end
5-
function resty_websocket_server.send_ping(self, data) end
6-
function resty_websocket_server.set_timeout(self, time) end
7-
function resty_websocket_server.send_binary(self, data) end
8-
function resty_websocket_server.send_frame() end
9-
function resty_websocket_server.recv_frame(self) end
10-
function resty_websocket_server.send_close(self, code, msg) end
11-
resty_websocket_server._VERSION="0.07"
12-
function resty_websocket_server.send_pong(self, data) end
2+
3+
---@class resty.websocket.server : resty.websocket
4+
resty_websocket_server = {
5+
_VERSION = "0.09"
6+
}
7+
8+
---Performs the websocket handshake process on the server side and returns a WebSocket server object.
9+
---
10+
---In case of error, it returns nil and a string describing the error.
11+
---@param opts? resty.websocket.new.opts
12+
---@return resty.websocket.server? server
13+
---@return string? error
14+
function resty_websocket_server:new(opts) end
15+
1316
return resty_websocket_server

0 commit comments

Comments
 (0)