@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.
@@ -1,46 +1,3 @@
1
- local function __TS__New(target, ...)
2
- local instance = setmetatable({}, target.prototype)
3
- instance:____constructor(...)
4
- return instance
5
- end
6
-
7
- local __TS__Symbol, Symbol
8
- do
9
- local symbolMetatable = {__tostring = function(self)
10
- return ("Symbol(" .. (self.description or "")) .. ")"
11
- end}
12
- function __TS__Symbol(description)
13
- return setmetatable({description = description}, symbolMetatable)
14
- end
15
- Symbol = {
16
- asyncDispose = __TS__Symbol("Symbol.asyncDispose"),
17
- dispose = __TS__Symbol("Symbol.dispose"),
18
- iterator = __TS__Symbol("Symbol.iterator"),
19
- hasInstance = __TS__Symbol("Symbol.hasInstance"),
20
- species = __TS__Symbol("Symbol.species"),
21
- toStringTag = __TS__Symbol("Symbol.toStringTag")
22
- }
23
- end
24
-
25
- local function __TS__InstanceOf(obj, classTbl)
26
- if type(classTbl) ~= "table" then
27
- error("Right-hand side of 'instanceof' is not an object", 0)
28
- end
29
- if classTbl[Symbol.hasInstance] ~= nil then
30
- return not not classTbl[Symbol.hasInstance](classTbl, obj)
31
- end
32
- if type(obj) == "table" then
33
- local luaClass = obj.constructor
34
- while luaClass ~= nil do
35
- if luaClass == classTbl then
36
- return true
37
- end
38
- luaClass = luaClass.____super
39
- end
40
- end
41
- return false
42
- end
43
-
44
1
  local function __TS__Class(self)
45
2
  local c = {prototype = {}}
46
3
  c.prototype.__index = c.prototype
@@ -48,171 +5,6 @@ local function __TS__Class(self)
48
5
  return c
49
6
  end
50
7
 
51
- local __TS__Promise
52
- do
53
- local function makeDeferredPromiseFactory()
54
- local resolve
55
- local reject
56
- local function executor(____, res, rej)
57
- resolve = res
58
- reject = rej
59
- end
60
- return function()
61
- local promise = __TS__New(__TS__Promise, executor)
62
- return promise, resolve, reject
63
- end
64
- end
65
- local makeDeferredPromise = makeDeferredPromiseFactory()
66
- local function isPromiseLike(value)
67
- return __TS__InstanceOf(value, __TS__Promise)
68
- end
69
- local function doNothing(self)
70
- end
71
- local ____pcall = _G.pcall
72
- __TS__Promise = __TS__Class()
73
- __TS__Promise.name = "__TS__Promise"
74
- function __TS__Promise.prototype.____constructor(self, executor)
75
- self.state = 0
76
- self.fulfilledCallbacks = {}
77
- self.rejectedCallbacks = {}
78
- local success, ____error = ____pcall(
79
- executor,
80
- nil,
81
- function(____, v) return self:resolve(v) end,
82
- function(____, err) return self:reject(err) end
83
- )
84
- if not success then
85
- self:reject(____error)
86
- end
87
- end
88
- function __TS__Promise.resolve(value)
89
- if __TS__InstanceOf(value, __TS__Promise) then
90
- return value
91
- end
92
- local promise = __TS__New(__TS__Promise, doNothing)
93
- promise.state = 1
94
- promise.value = value
95
- return promise
96
- end
97
- function __TS__Promise.reject(reason)
98
- local promise = __TS__New(__TS__Promise, doNothing)
99
- promise.state = 2
100
- promise.rejectionReason = reason
101
- return promise
102
- end
103
- __TS__Promise.prototype["then"] = function(self, onFulfilled, onRejected)
104
- local promise, resolve, reject = makeDeferredPromise()
105
- self:addCallbacks(
106
- onFulfilled and self:createPromiseResolvingCallback(onFulfilled, resolve, reject) or resolve,
107
- onRejected and self:createPromiseResolvingCallback(onRejected, resolve, reject) or reject
108
- )
109
- return promise
110
- end
111
- function __TS__Promise.prototype.addCallbacks(self, fulfilledCallback, rejectedCallback)
112
- if self.state == 1 then
113
- return fulfilledCallback(nil, self.value)
114
- end
115
- if self.state == 2 then
116
- return rejectedCallback(nil, self.rejectionReason)
117
- end
118
- local ____self_fulfilledCallbacks_0 = self.fulfilledCallbacks
119
- ____self_fulfilledCallbacks_0[#____self_fulfilledCallbacks_0 + 1] = fulfilledCallback
120
- local ____self_rejectedCallbacks_1 = self.rejectedCallbacks
121
- ____self_rejectedCallbacks_1[#____self_rejectedCallbacks_1 + 1] = rejectedCallback
122
- end
123
- function __TS__Promise.prototype.catch(self, onRejected)
124
- return self["then"](self, nil, onRejected)
125
- end
126
- function __TS__Promise.prototype.finally(self, onFinally)
127
- if type(onFinally) ~= "function" then
128
- return self["then"](self, onFinally, onFinally)
129
- end
130
- return self["then"](
131
- self,
132
- function(____, x)
133
- local ____self_2 = __TS__New(
134
- __TS__Promise,
135
- function(____, resolve) return resolve(
136
- nil,
137
- onFinally(nil)
138
- ) end
139
- )
140
- return ____self_2["then"](
141
- ____self_2,
142
- function() return x end
143
- )
144
- end,
145
- function(____, e)
146
- local ____self_3 = __TS__New(
147
- __TS__Promise,
148
- function(____, resolve) return resolve(
149
- nil,
150
- onFinally(nil)
151
- ) end
152
- )
153
- return ____self_3["then"](
154
- ____self_3,
155
- function()
156
- error(e, 0)
157
- end
158
- )
159
- end
160
- )
161
- end
162
- function __TS__Promise.prototype.resolve(self, value)
163
- if isPromiseLike(value) then
164
- return value:addCallbacks(
165
- function(____, v) return self:resolve(v) end,
166
- function(____, err) return self:reject(err) end
167
- )
168
- end
169
- if self.state == 0 then
170
- self.state = 1
171
- self.value = value
172
- return self:invokeCallbacks(self.fulfilledCallbacks, value)
173
- end
174
- end
175
- function __TS__Promise.prototype.reject(self, reason)
176
- if self.state == 0 then
177
- self.state = 2
178
- self.rejectionReason = reason
179
- return self:invokeCallbacks(self.rejectedCallbacks, reason)
180
- end
181
- end
182
- function __TS__Promise.prototype.invokeCallbacks(self, callbacks, value)
183
- local callbacksLength = #callbacks
184
- if callbacksLength ~= 0 then
185
- for i = 1, callbacksLength - 1 do
186
- callbacks[i](callbacks, value)
187
- end
188
- return callbacks[callbacksLength](callbacks, value)
189
- end
190
- end
191
- function __TS__Promise.prototype.createPromiseResolvingCallback(self, f, resolve, reject)
192
- return function(____, value)
193
- local success, resultOrError = ____pcall(f, nil, value)
194
- if not success then
195
- return reject(nil, resultOrError)
196
- end
197
- return self:handleCallbackValue(resultOrError, resolve, reject)
198
- end
199
- end
200
- function __TS__Promise.prototype.handleCallbackValue(self, value, resolve, reject)
201
- if isPromiseLike(value) then
202
- local nextpromise = value
203
- if nextpromise.state == 1 then
204
- return resolve(nil, nextpromise.value)
205
- elseif nextpromise.state == 2 then
206
- return reject(nil, nextpromise.rejectionReason)
207
- else
208
- return nextpromise:addCallbacks(resolve, reject)
209
- end
210
- else
211
- return resolve(nil, value)
212
- end
213
- end
214
- end
215
-
216
8
  local function __TS__StringIncludes(self, searchString, position)
217
9
  if not position then
218
10
  position = 1
@@ -223,6 +15,12 @@ local function __TS__StringIncludes(self, searchString, position)
223
15
  return index ~= nil
224
16
  end
225
17
 
18
+ local function __TS__New(target, ...)
19
+ local instance = setmetatable({}, target.prototype)
20
+ instance:____constructor(...)
21
+ return instance
22
+ end
23
+
226
24
  local function __TS__ClassExtends(target, base)
227
25
  target.____super = base
228
26
  local staticMetatable = setmetatable({__index = base}, base)
@@ -375,16 +173,14 @@ local function __TS__ArrayIsArray(value)
375
173
  end
376
174
 
377
175
  return {
378
- __TS__Promise = __TS__Promise,
379
- __TS__New = __TS__New,
380
176
  __TS__Class = __TS__Class,
381
- __TS__InstanceOf = __TS__InstanceOf,
382
177
  Error = Error,
383
178
  RangeError = RangeError,
384
179
  ReferenceError = ReferenceError,
385
180
  SyntaxError = SyntaxError,
386
181
  TypeError = TypeError,
387
182
  URIError = URIError,
183
+ __TS__New = __TS__New,
388
184
  __TS__StringSplit = __TS__StringSplit,
389
185
  __TS__ArrayIsArray = __TS__ArrayIsArray
390
186
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noita-ts/pollnet",
3
- "version": "0.3.8",
3
+ "version": "0.5.0",
4
4
  "files": [
5
5
  "dist/**/*.{lua,dll,d.ts}",
6
6
  "LICENSE-pollnet"
@@ -13,9 +13,9 @@
13
13
  "types": "./dist/index.d.ts",
14
14
  "default": "./dist/index"
15
15
  },
16
- "./async": {
17
- "types": "./dist/async.d.ts",
18
- "default": "./dist/async"
16
+ "./api": {
17
+ "types": "./dist/api.d.ts",
18
+ "default": "./dist/api"
19
19
  }
20
20
  },
21
21
  "scripts": {
package/dist/async.d.ts DELETED
@@ -1,76 +0,0 @@
1
- /** @noSelfInFile */
2
- import pollnet from ".";
3
- export declare namespace compat {
4
- /**
5
- * Run `body` inside a reactor coroutine and get its completion as a Promise.
6
- * `body` may freely use socket.await() / other yield-based pollnet APIs.
7
- * Errors inside body reject the promise.
8
- */
9
- const asyncify: <T>(reactor: pollnet.Reactor, body: () => T) => Promise<T>;
10
- /**
11
- * Parks the *current coroutine* until the promise settles, then returns its
12
- * value or rethrows its rejection.
13
- * Only call from inside a reactor coroutine (`reactor.run(() => { .. HERE .. })`).
14
- */
15
- const yieldPromise: <T>(p: Promise<T>) => T;
16
- }
17
- /**
18
- * Promise-friendly wrapper around a {@link pollnet.Reactor}.
19
- *
20
- * Lets you drive yield-based pollnet coroutines while exposing their results
21
- * as Promises. Call {@link poll} regularly (e.g. once per frame) to advance
22
- * all running coroutines.
23
- */
24
- export declare class AsyncReactor {
25
- private reactor;
26
- constructor();
27
- /**
28
- * Advance every coroutine currently running on the reactor by one step.
29
- * Must be called periodically for spawned work to make progress.
30
- */
31
- poll(): number;
32
- /**
33
- * Run `body` as a reactor coroutine and return its result as a Promise.
34
- * `body` may use yield-based pollnet APIs (e.g. `socket.await()`).
35
- */
36
- spawn<T>(body: () => T): Promise<T>;
37
- /**
38
- * Async version of `pollnet.Reactor.run_server`.
39
- *
40
- * Accept connections on `server_sock`, spawning a coroutine that runs
41
- * `client_body` for each incoming client. `client_body` receives the client
42
- * socket and its remote address, and may itself return a Promise which the
43
- * coroutine parks on until it settles.
44
- */
45
- runServer(server_sock: pollnet.Socket, client_body: (sock: pollnet.Socket, addr: string) => Promise<void> | void): void;
46
- }
47
- export declare class HttpResponse {
48
- status: number;
49
- headers: string;
50
- body: string;
51
- constructor(status: number, headers: string, body: string);
52
- /** @noSelf */
53
- static read(socket: pollnet.Socket): HttpResponse;
54
- /**
55
- * Parse the raw header block into a map.
56
- * Header names seen more than once collapse into an array of their values, in
57
- * order.
58
- */
59
- parseHeaders(): Record<string, string | string[]>;
60
- }
61
- /**
62
- * Tiny async HTTP client built on top of an {@link AsyncReactor}.
63
- * Requests run as reactor coroutines and resolve to an {@link HttpResponse}.
64
- */
65
- export declare class HttpClient {
66
- private reactor;
67
- constructor(reactor: AsyncReactor);
68
- /**
69
- * Perform an HTTP GET.
70
- */
71
- get(url: string, headers?: Record<string, string | string[]> | string): Promise<HttpResponse>;
72
- /**
73
- * Perform an HTTP POST with an optional request `body`.
74
- */
75
- post(url: string, headers?: Record<string, string | string[]> | string, body?: string): Promise<HttpResponse>;
76
- }
package/dist/async.lua DELETED
@@ -1,167 +0,0 @@
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__InstanceOf = ____lualib.__TS__InstanceOf
6
- local Error = ____lualib.Error
7
- local RangeError = ____lualib.RangeError
8
- local ReferenceError = ____lualib.ReferenceError
9
- local SyntaxError = ____lualib.SyntaxError
10
- local TypeError = ____lualib.TypeError
11
- local URIError = ____lualib.URIError
12
- local __TS__StringSplit = ____lualib.__TS__StringSplit
13
- local __TS__ArrayIsArray = ____lualib.__TS__ArrayIsArray
14
- local ____exports = {}
15
- local _____2E = require("index")
16
- local pollnet = _____2E.default
17
- ____exports.compat = {}
18
- local compat = ____exports.compat
19
- do
20
- --- Run `body` inside a reactor coroutine and get its completion as a Promise.
21
- -- `body` may freely use socket.await() / other yield-based pollnet APIs.
22
- -- Errors inside body reject the promise.
23
- compat.asyncify = function(reactor, body)
24
- return __TS__New(
25
- __TS__Promise,
26
- function(____, resolve, reject)
27
- reactor:run(function()
28
- do
29
- local function ____catch(e)
30
- reject(nil, e)
31
- end
32
- local ____try, ____hasReturned = pcall(function()
33
- resolve(
34
- nil,
35
- body()
36
- )
37
- end)
38
- if not ____try then
39
- ____catch(____hasReturned)
40
- end
41
- end
42
- end)
43
- end
44
- )
45
- end
46
- --- Parks the *current coroutine* until the promise settles, then returns its
47
- -- value or rethrows its rejection.
48
- -- Only call from inside a reactor coroutine (`reactor.run(() => { .. HERE .. })`).
49
- compat.yieldPromise = function(p)
50
- local done = false
51
- local ok = false
52
- local result
53
- p["then"](
54
- p,
55
- function(____, v)
56
- done = true
57
- ok = true
58
- result = v
59
- end,
60
- function(____, e)
61
- done = true
62
- ok = false
63
- result = e
64
- end
65
- )
66
- while not done do
67
- coroutine.yield()
68
- end
69
- if not ok then
70
- error(result, 0)
71
- end
72
- return result
73
- end
74
- end
75
- --- Promise-friendly wrapper around a {@link pollnet.Reactor}.
76
- --
77
- -- Lets you drive yield-based pollnet coroutines while exposing their results
78
- -- as Promises. Call {@link poll} regularly (e.g. once per frame) to advance
79
- -- all running coroutines.
80
- ____exports.AsyncReactor = __TS__Class()
81
- local AsyncReactor = ____exports.AsyncReactor
82
- AsyncReactor.name = "AsyncReactor"
83
- function AsyncReactor.prototype.____constructor(self)
84
- self.reactor = pollnet.Reactor()
85
- end
86
- function AsyncReactor.prototype.poll(self)
87
- return self.reactor:update()
88
- end
89
- function AsyncReactor.prototype.spawn(self, body)
90
- return ____exports.compat.asyncify(self.reactor, body)
91
- end
92
- function AsyncReactor.prototype.runServer(self, server_sock, client_body)
93
- server_sock:on_connection(function(client_sock, addr)
94
- self.reactor:run(function()
95
- local r = client_body(client_sock, addr)
96
- if __TS__InstanceOf(r, __TS__Promise) then
97
- ____exports.compat.yieldPromise(r)
98
- end
99
- end)
100
- end)
101
- self.reactor:run(function()
102
- while (server_sock:await()) do
103
- end
104
- end)
105
- end
106
- ____exports.HttpResponse = __TS__Class()
107
- local HttpResponse = ____exports.HttpResponse
108
- HttpResponse.name = "HttpResponse"
109
- function HttpResponse.prototype.____constructor(self, status, headers, body)
110
- self.status = status
111
- self.headers = headers
112
- self.body = body
113
- end
114
- function HttpResponse.read(socket)
115
- local msgs, ____error = socket:await_n(3)
116
- if not msgs then
117
- error(
118
- __TS__New(Error, "socket error: " .. ____error),
119
- 0
120
- )
121
- end
122
- local statusStr, headersStr, respBody = unpack(msgs, 1, 3)
123
- local status = tonumber((string.match(statusStr, "^%d+"))) or 500
124
- return __TS__New(____exports.HttpResponse, status, headersStr, respBody)
125
- end
126
- function HttpResponse.prototype.parseHeaders(self)
127
- local headers = {}
128
- for ____, line in ipairs(__TS__StringSplit(self.headers, "\n")) do
129
- do
130
- local key, value = unpack(
131
- __TS__StringSplit(line, ": ", 2),
132
- 1,
133
- 2
134
- )
135
- if not key or not value then
136
- goto __continue26
137
- end
138
- local prev = headers[key]
139
- if not prev then
140
- headers[key] = value
141
- goto __continue26
142
- end
143
- if __TS__ArrayIsArray(prev) then
144
- prev[#prev + 1] = value
145
- else
146
- headers[key] = {prev, value}
147
- end
148
- end
149
- ::__continue26::
150
- end
151
- return headers
152
- end
153
- --- Tiny async HTTP client built on top of an {@link AsyncReactor}.
154
- -- Requests run as reactor coroutines and resolve to an {@link HttpResponse}.
155
- ____exports.HttpClient = __TS__Class()
156
- local HttpClient = ____exports.HttpClient
157
- HttpClient.name = "HttpClient"
158
- function HttpClient.prototype.____constructor(self, reactor)
159
- self.reactor = reactor
160
- end
161
- function HttpClient.prototype.get(self, url, headers)
162
- return self.reactor:spawn(function() return ____exports.HttpResponse.read(pollnet.http_get(url, headers, false)) end)
163
- end
164
- function HttpClient.prototype.post(self, url, headers, body)
165
- return self.reactor:spawn(function() return ____exports.HttpResponse.read(pollnet.http_post(url, headers, body, false)) end)
166
- end
167
- return ____exports