@noita-ts/pollnet 0.3.6 → 0.3.8
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/async.d.ts +2 -0
- package/dist/async.lua +10 -10
- package/package.json +1 -1
package/dist/async.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** @noSelfInFile */
|
|
1
2
|
import pollnet from ".";
|
|
2
3
|
export declare namespace compat {
|
|
3
4
|
/**
|
|
@@ -48,6 +49,7 @@ export declare class HttpResponse {
|
|
|
48
49
|
headers: string;
|
|
49
50
|
body: string;
|
|
50
51
|
constructor(status: number, headers: string, body: string);
|
|
52
|
+
/** @noSelf */
|
|
51
53
|
static read(socket: pollnet.Socket): HttpResponse;
|
|
52
54
|
/**
|
|
53
55
|
* Parse the raw header block into a map.
|
package/dist/async.lua
CHANGED
|
@@ -20,7 +20,7 @@ do
|
|
|
20
20
|
--- Run `body` inside a reactor coroutine and get its completion as a Promise.
|
|
21
21
|
-- `body` may freely use socket.await() / other yield-based pollnet APIs.
|
|
22
22
|
-- Errors inside body reject the promise.
|
|
23
|
-
compat.asyncify = function(
|
|
23
|
+
compat.asyncify = function(reactor, body)
|
|
24
24
|
return __TS__New(
|
|
25
25
|
__TS__Promise,
|
|
26
26
|
function(____, resolve, reject)
|
|
@@ -32,7 +32,7 @@ do
|
|
|
32
32
|
local ____try, ____hasReturned = pcall(function()
|
|
33
33
|
resolve(
|
|
34
34
|
nil,
|
|
35
|
-
body(
|
|
35
|
+
body()
|
|
36
36
|
)
|
|
37
37
|
end)
|
|
38
38
|
if not ____try then
|
|
@@ -46,7 +46,7 @@ do
|
|
|
46
46
|
--- Parks the *current coroutine* until the promise settles, then returns its
|
|
47
47
|
-- value or rethrows its rejection.
|
|
48
48
|
-- Only call from inside a reactor coroutine (`reactor.run(() => { .. HERE .. })`).
|
|
49
|
-
compat.yieldPromise = function(
|
|
49
|
+
compat.yieldPromise = function(p)
|
|
50
50
|
local done = false
|
|
51
51
|
local ok = false
|
|
52
52
|
local result
|
|
@@ -87,14 +87,14 @@ function AsyncReactor.prototype.poll(self)
|
|
|
87
87
|
return self.reactor:update()
|
|
88
88
|
end
|
|
89
89
|
function AsyncReactor.prototype.spawn(self, body)
|
|
90
|
-
return ____exports.compat
|
|
90
|
+
return ____exports.compat.asyncify(self.reactor, body)
|
|
91
91
|
end
|
|
92
92
|
function AsyncReactor.prototype.runServer(self, server_sock, client_body)
|
|
93
93
|
server_sock:on_connection(function(client_sock, addr)
|
|
94
94
|
self.reactor:run(function()
|
|
95
|
-
local r = client_body(
|
|
95
|
+
local r = client_body(client_sock, addr)
|
|
96
96
|
if __TS__InstanceOf(r, __TS__Promise) then
|
|
97
|
-
____exports.compat
|
|
97
|
+
____exports.compat.yieldPromise(r)
|
|
98
98
|
end
|
|
99
99
|
end)
|
|
100
100
|
end)
|
|
@@ -111,7 +111,7 @@ function HttpResponse.prototype.____constructor(self, status, headers, body)
|
|
|
111
111
|
self.headers = headers
|
|
112
112
|
self.body = body
|
|
113
113
|
end
|
|
114
|
-
function HttpResponse.read(
|
|
114
|
+
function HttpResponse.read(socket)
|
|
115
115
|
local msgs, ____error = socket:await_n(3)
|
|
116
116
|
if not msgs then
|
|
117
117
|
error(
|
|
@@ -120,7 +120,7 @@ function HttpResponse.read(self, socket)
|
|
|
120
120
|
)
|
|
121
121
|
end
|
|
122
122
|
local statusStr, headersStr, respBody = unpack(msgs, 1, 3)
|
|
123
|
-
local status = tonumber((string.match("^%d+"
|
|
123
|
+
local status = tonumber((string.match(statusStr, "^%d+"))) or 500
|
|
124
124
|
return __TS__New(____exports.HttpResponse, status, headersStr, respBody)
|
|
125
125
|
end
|
|
126
126
|
function HttpResponse.prototype.parseHeaders(self)
|
|
@@ -159,9 +159,9 @@ function HttpClient.prototype.____constructor(self, reactor)
|
|
|
159
159
|
self.reactor = reactor
|
|
160
160
|
end
|
|
161
161
|
function HttpClient.prototype.get(self, url, headers)
|
|
162
|
-
return self.reactor:spawn(function() return ____exports.HttpResponse
|
|
162
|
+
return self.reactor:spawn(function() return ____exports.HttpResponse.read(pollnet.http_get(url, headers, false)) end)
|
|
163
163
|
end
|
|
164
164
|
function HttpClient.prototype.post(self, url, headers, body)
|
|
165
|
-
return self.reactor:spawn(function() return ____exports.HttpResponse
|
|
165
|
+
return self.reactor:spawn(function() return ____exports.HttpResponse.read(pollnet.http_post(url, headers, body, false)) end)
|
|
166
166
|
end
|
|
167
167
|
return ____exports
|