@noita-ts/pollnet 0.1.0 → 0.2.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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 probable-basilisk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,31 @@
1
+ import pollnet from "./index";
2
+ /**
3
+ * Run `body` inside a reactor coroutine and get its completion as a Promise.
4
+ * `body` may freely use socket.await() / other yield-based pollnet APIs.
5
+ * Errors inside body reject the promise.
6
+ */
7
+ export declare const spawn: <T>(reactor: pollnet.Reactor, body: () => T) => Promise<T>;
8
+ /**
9
+ * Parks the *current coroutine* until the promise settles, then returns its
10
+ * value or rethrows its rejection.
11
+ * Only call from inside a reactor coroutine (`reactor.run(() => { .. HERE .. })`).
12
+ */
13
+ export declare const blockOn: <T>(p: Promise<T>) => T;
14
+ export declare class HttpResponse {
15
+ status: number;
16
+ headers: string;
17
+ body: string;
18
+ constructor(status: number, headers: string, body: string);
19
+ parseHeaders(): Record<string, string | string[]>;
20
+ }
21
+ export default class AsyncReactor {
22
+ private reactor;
23
+ constructor();
24
+ update(): number;
25
+ /** Runs a coroutine thread */
26
+ run(thread_body: (this: pollnet.Reactor) => void): void;
27
+ /** Runs a server with client handler */
28
+ run_server(server_sock: pollnet.Socket, client_body: (sock: pollnet.Socket, addr: string) => void): void;
29
+ http_get(url: string, headers?: Record<string, string | string[]> | string): Promise<HttpResponse>;
30
+ http_post(url: string, headers?: Record<string, string | string[]> | string, body?: string): Promise<HttpResponse>;
31
+ }
package/dist/async.lua ADDED
@@ -0,0 +1,158 @@
1
+ local ____lualib = require("lualib_bundle")
2
+ local __TS__Promise = ____lualib.__TS__Promise
3
+ local __TS__New = ____lualib.__TS__New
4
+ local __TS__Class = ____lualib.__TS__Class
5
+ local __TS__StringSplit = ____lualib.__TS__StringSplit
6
+ local __TS__ArrayIsArray = ____lualib.__TS__ArrayIsArray
7
+ local Error = ____lualib.Error
8
+ local RangeError = ____lualib.RangeError
9
+ local ReferenceError = ____lualib.ReferenceError
10
+ local SyntaxError = ____lualib.SyntaxError
11
+ local TypeError = ____lualib.TypeError
12
+ local URIError = ____lualib.URIError
13
+ local ____exports = {}
14
+ local ____index = require("index")
15
+ local pollnet = ____index.default
16
+ --- Run `body` inside a reactor coroutine and get its completion as a Promise.
17
+ -- `body` may freely use socket.await() / other yield-based pollnet APIs.
18
+ -- Errors inside body reject the promise.
19
+ ____exports.spawn = function(____, reactor, body)
20
+ return __TS__New(
21
+ __TS__Promise,
22
+ function(____, resolve, reject)
23
+ reactor:run(function()
24
+ do
25
+ local function ____catch(e)
26
+ reject(nil, e)
27
+ end
28
+ local ____try, ____hasReturned = pcall(function()
29
+ resolve(
30
+ nil,
31
+ body(nil)
32
+ )
33
+ end)
34
+ if not ____try then
35
+ ____catch(____hasReturned)
36
+ end
37
+ end
38
+ end)
39
+ end
40
+ )
41
+ end
42
+ --- Parks the *current coroutine* until the promise settles, then returns its
43
+ -- value or rethrows its rejection.
44
+ -- Only call from inside a reactor coroutine (`reactor.run(() => { .. HERE .. })`).
45
+ ____exports.blockOn = function(____, p)
46
+ local done = false
47
+ local ok = false
48
+ local result
49
+ p["then"](
50
+ p,
51
+ function(____, v)
52
+ done = true
53
+ ok = true
54
+ result = v
55
+ end,
56
+ function(____, e)
57
+ done = true
58
+ ok = false
59
+ result = e
60
+ end
61
+ )
62
+ while not done do
63
+ coroutine.yield()
64
+ end
65
+ if not ok then
66
+ error(result, 0)
67
+ end
68
+ return result
69
+ end
70
+ ____exports.HttpResponse = __TS__Class()
71
+ local HttpResponse = ____exports.HttpResponse
72
+ HttpResponse.name = "HttpResponse"
73
+ function HttpResponse.prototype.____constructor(self, status, headers, body)
74
+ self.status = status
75
+ self.headers = headers
76
+ self.body = body
77
+ end
78
+ function HttpResponse.prototype.parseHeaders(self)
79
+ local headers = {}
80
+ for ____, line in ipairs(__TS__StringSplit(self.headers, "\n")) do
81
+ do
82
+ local key, value = unpack(
83
+ __TS__StringSplit(line, ": ", 2),
84
+ 1,
85
+ 2
86
+ )
87
+ if not key or not value then
88
+ goto __continue14
89
+ end
90
+ local prev = headers[key]
91
+ if not prev then
92
+ headers[key] = value
93
+ goto __continue14
94
+ end
95
+ if __TS__ArrayIsArray(prev) then
96
+ prev[#prev + 1] = value
97
+ else
98
+ headers[key] = {prev, value}
99
+ end
100
+ end
101
+ ::__continue14::
102
+ end
103
+ return headers
104
+ end
105
+ ____exports.default = __TS__Class()
106
+ local AsyncReactor = ____exports.default
107
+ AsyncReactor.name = "AsyncReactor"
108
+ function AsyncReactor.prototype.____constructor(self)
109
+ self.reactor = pollnet.Reactor()
110
+ end
111
+ function AsyncReactor.prototype.update(self)
112
+ return self.reactor:update()
113
+ end
114
+ function AsyncReactor.prototype.run(self, thread_body)
115
+ self.reactor:run(thread_body)
116
+ end
117
+ function AsyncReactor.prototype.run_server(self, server_sock, client_body)
118
+ self.reactor:run_server(server_sock, client_body)
119
+ end
120
+ function AsyncReactor.prototype.http_get(self, url, headers)
121
+ return ____exports.spawn(
122
+ nil,
123
+ self.reactor,
124
+ function()
125
+ local socket = pollnet.http_get(url, headers, false)
126
+ local msgs, ____error = socket:await_n(3)
127
+ if not msgs then
128
+ error(
129
+ __TS__New(Error, "socket error: " .. ____error),
130
+ 0
131
+ )
132
+ end
133
+ local statusStr, headersStr, respBody = unpack(msgs, 1, 3)
134
+ local status = tonumber({string.match("^%d+", statusStr)}) or 500
135
+ return __TS__New(____exports.HttpResponse, status, headersStr, respBody)
136
+ end
137
+ )
138
+ end
139
+ function AsyncReactor.prototype.http_post(self, url, headers, body)
140
+ return ____exports.spawn(
141
+ nil,
142
+ self.reactor,
143
+ function()
144
+ local socket = pollnet.http_post(url, headers, body, false)
145
+ local msgs, ____error = socket:await_n(3)
146
+ if not msgs then
147
+ error(
148
+ __TS__New(Error, "socket error: " .. ____error),
149
+ 0
150
+ )
151
+ end
152
+ local statusStr, headersStr, respBody = unpack(msgs, 1, 3)
153
+ local status = tonumber({string.match("^%d+", statusStr)}) or 500
154
+ return __TS__New(____exports.HttpResponse, status, headersStr, respBody)
155
+ end
156
+ )
157
+ end
158
+ return ____exports
@@ -154,8 +154,7 @@ declare namespace pollnet {
154
154
 
155
155
  /** Awaits a message (coroutine.yield) */
156
156
  await():
157
- | LuaMultiReturn<[string, undefined]>
158
- | LuaMultiReturn<[false, string]>;
157
+ LuaMultiReturn<[string, undefined]> | LuaMultiReturn<[false, string]>;
159
158
 
160
159
  /** Awaits N messages */
161
160
  await_n(
@@ -197,12 +196,12 @@ declare namespace pollnet {
197
196
  init(): void;
198
197
 
199
198
  /** Runs a coroutine thread */
200
- run(thread_body: (self: Reactor) => Promise<void> | void): void;
199
+ run(thread_body: (this: Reactor) => void): void;
201
200
 
202
201
  /** Runs a server with client handler */
203
202
  run_server(
204
203
  server_sock: Socket,
205
- client_body: (sock: Socket, addr: string) => Promise<void> | void,
204
+ client_body: (sock: Socket, addr: string) => void,
206
205
  ): void;
207
206
 
208
207
  /** Logs messages */