@lango-project/lango-server 0.0.0-stage → 0.1.1
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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +710 -2
- package/dist/.lango/bignumber.mjs +3119 -0
- package/dist/.lango/runtime.mjs +683 -0
- package/dist/.lango/xmldom/conventions.js +429 -0
- package/dist/.lango/xmldom/dom-parser.js +591 -0
- package/dist/.lango/xmldom/dom.js +3759 -0
- package/dist/.lango/xmldom/entities.js +2171 -0
- package/dist/.lango/xmldom/errors.js +205 -0
- package/dist/.lango/xmldom/grammar.js +559 -0
- package/dist/.lango/xmldom/index.js +41 -0
- package/dist/.lango/xmldom/package.json +1 -0
- package/dist/.lango/xmldom/sax.js +983 -0
- package/dist/.lango/xmldom.LICENSE.md +8 -0
- package/dist/body_reader.mjs +179 -0
- package/dist/body_reader.mjs.map +1 -0
- package/dist/builder.mjs +84 -0
- package/dist/builder.mjs.map +1 -0
- package/dist/cli/.lango/bignumber.mjs +3119 -0
- package/dist/cli/.lango/runtime.mjs +683 -0
- package/dist/cli/.lango/xmldom/conventions.js +429 -0
- package/dist/cli/.lango/xmldom/dom-parser.js +591 -0
- package/dist/cli/.lango/xmldom/dom.js +3759 -0
- package/dist/cli/.lango/xmldom/entities.js +2171 -0
- package/dist/cli/.lango/xmldom/errors.js +205 -0
- package/dist/cli/.lango/xmldom/grammar.js +559 -0
- package/dist/cli/.lango/xmldom/index.js +41 -0
- package/dist/cli/.lango/xmldom/package.json +1 -0
- package/dist/cli/.lango/xmldom/sax.js +983 -0
- package/dist/cli/.lango/xmldom.LICENSE.md +8 -0
- package/dist/cli/body_reader.mjs +179 -0
- package/dist/cli/body_reader.mjs.map +1 -0
- package/dist/cli/database.mjs +101 -0
- package/dist/cli/database.mjs.map +1 -0
- package/dist/cli/database_options.mjs +83 -0
- package/dist/cli/database_options.mjs.map +1 -0
- package/dist/cli/database_scope.mjs +82 -0
- package/dist/cli/database_scope.mjs.map +1 -0
- package/dist/cli/endpoint.mjs +50 -0
- package/dist/cli/endpoint.mjs.map +1 -0
- package/dist/cli/longpoll_broker.mjs +214 -0
- package/dist/cli/longpoll_broker.mjs.map +1 -0
- package/dist/cli/longpoll_options.mjs +102 -0
- package/dist/cli/longpoll_options.mjs.map +1 -0
- package/dist/cli/longpoll_session.mjs +135 -0
- package/dist/cli/longpoll_session.mjs.map +1 -0
- package/dist/cli/middleware.mjs +25 -0
- package/dist/cli/middleware.mjs.map +1 -0
- package/dist/cli/options.mjs +177 -0
- package/dist/cli/options.mjs.map +1 -0
- package/dist/cli/postgres_adapter.mjs +166 -0
- package/dist/cli/postgres_adapter.mjs.map +1 -0
- package/dist/cli/routes.mjs +297 -0
- package/dist/cli/routes.mjs.map +1 -0
- package/dist/cli/server.mjs +792 -0
- package/dist/cli/server.mjs.map +1 -0
- package/dist/cli/sqlite_adapter.mjs +163 -0
- package/dist/cli/sqlite_adapter.mjs.map +1 -0
- package/dist/cli/static_files.mjs +125 -0
- package/dist/cli/static_files.mjs.map +1 -0
- package/dist/database.mjs +101 -0
- package/dist/database.mjs.map +1 -0
- package/dist/database_options.mjs +83 -0
- package/dist/database_options.mjs.map +1 -0
- package/dist/database_scope.mjs +82 -0
- package/dist/database_scope.mjs.map +1 -0
- package/dist/endpoint.mjs +50 -0
- package/dist/endpoint.mjs.map +1 -0
- package/dist/group.mjs +91 -0
- package/dist/group.mjs.map +1 -0
- package/dist/library.mjs +6 -0
- package/dist/library.mjs.map +1 -0
- package/dist/longpoll_broker.mjs +214 -0
- package/dist/longpoll_broker.mjs.map +1 -0
- package/dist/longpoll_options.mjs +102 -0
- package/dist/longpoll_options.mjs.map +1 -0
- package/dist/longpoll_session.mjs +135 -0
- package/dist/longpoll_session.mjs.map +1 -0
- package/dist/middleware.mjs +25 -0
- package/dist/middleware.mjs.map +1 -0
- package/dist/options.mjs +177 -0
- package/dist/options.mjs.map +1 -0
- package/dist/postgres_adapter.mjs +166 -0
- package/dist/postgres_adapter.mjs.map +1 -0
- package/dist/postgres_worker.mjs +119 -0
- package/dist/postgres_worker.mjs.map +1 -0
- package/dist/response.mjs +62 -0
- package/dist/response.mjs.map +1 -0
- package/dist/routes.mjs +297 -0
- package/dist/routes.mjs.map +1 -0
- package/dist/runtime.mjs +549 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/server.mjs +788 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server_context.mjs +78 -0
- package/dist/server_context.mjs.map +1 -0
- package/dist/sqlite_adapter.mjs +163 -0
- package/dist/sqlite_adapter.mjs.map +1 -0
- package/dist/static_files.mjs +125 -0
- package/dist/static_files.mjs.map +1 -0
- package/dist/token.mjs +106 -0
- package/dist/token.mjs.map +1 -0
- package/dist/tokens.mjs +76 -0
- package/dist/tokens.mjs.map +1 -0
- package/dist/worker.mjs +57 -0
- package/dist/worker.mjs.map +1 -0
- package/package.json +44 -4
- package/src/body_reader.lango +113 -0
- package/src/builder.lango +64 -0
- package/src/database.lango +66 -0
- package/src/database_options.lango +57 -0
- package/src/database_scope.lango +59 -0
- package/src/endpoint.lango +30 -0
- package/src/group.lango +61 -0
- package/src/library.lango +10 -0
- package/src/longpoll.lua +61 -0
- package/src/longpoll_broker.lango +161 -0
- package/src/longpoll_options.lango +79 -0
- package/src/longpoll_session.lango +90 -0
- package/src/middleware.lango +10 -0
- package/src/options.lango +144 -0
- package/src/postgres_adapter.lango +138 -0
- package/src/postgres_worker.lango +114 -0
- package/src/response.lango +42 -0
- package/src/routes.lango +224 -0
- package/src/runtime.lango +382 -0
- package/src/server.lango +597 -0
- package/src/server_context.lango +58 -0
- package/src/sqlite_adapter.lango +127 -0
- package/src/static_files.lango +90 -0
- package/src/token.lango +70 -0
- package/src/tokens.lango +70 -0
- package/src/worker.lango +43 -0
package/src/server.lango
ADDED
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
import "./longpoll_broker"
|
|
2
|
+
class LongpollBroker
|
|
3
|
+
import "./longpoll_session"
|
|
4
|
+
class LongpollSession
|
|
5
|
+
import "./database"
|
|
6
|
+
prepareDatabase(settings)
|
|
7
|
+
import js "node:http" as http
|
|
8
|
+
import js "node:path" as path
|
|
9
|
+
import js "node:module" as modules
|
|
10
|
+
import js "node:crypto" as crypto
|
|
11
|
+
import js "piscina" as piscina
|
|
12
|
+
import js global process
|
|
13
|
+
import js global Promise
|
|
14
|
+
import js global Date
|
|
15
|
+
import js global Math
|
|
16
|
+
import js global Object
|
|
17
|
+
import js global Reflect
|
|
18
|
+
import js global AbortController
|
|
19
|
+
import js global setTimeout
|
|
20
|
+
import js global clearTimeout
|
|
21
|
+
import "./options"
|
|
22
|
+
deployment(directory)
|
|
23
|
+
import "./routes"
|
|
24
|
+
class Routes
|
|
25
|
+
import "./body_reader"
|
|
26
|
+
class BodyReader
|
|
27
|
+
import "./static_files"
|
|
28
|
+
class StaticFiles
|
|
29
|
+
|
|
30
|
+
export main(args)
|
|
31
|
+
|
|
32
|
+
export class Server(path, definition = null)
|
|
33
|
+
methods
|
|
34
|
+
start()
|
|
35
|
+
close()
|
|
36
|
+
publish(path, key, data)
|
|
37
|
+
cursor(path, key)
|
|
38
|
+
|
|
39
|
+
Server(path, definition = null)
|
|
40
|
+
if path != null
|
|
41
|
+
directory = path
|
|
42
|
+
routes = new Routes(directory)
|
|
43
|
+
routes.xml(pathJoin(directory, "routes.xml"))
|
|
44
|
+
definition = {
|
|
45
|
+
settings: deployment(directory)
|
|
46
|
+
routes: routes.definitions()
|
|
47
|
+
events: routes.events()
|
|
48
|
+
middlewares: routes.middlewares()
|
|
49
|
+
}
|
|
50
|
+
if definition == null
|
|
51
|
+
throw "Use Server(directory) or Builder.build()"
|
|
52
|
+
@definition = from_json(to_json(definition))
|
|
53
|
+
@broker = new LongpollBroker(@definition.settings, @definition.routes)
|
|
54
|
+
@polls = Object/create(null)
|
|
55
|
+
@state = "new"
|
|
56
|
+
@connections = []
|
|
57
|
+
@server = null
|
|
58
|
+
@pool = null
|
|
59
|
+
@starting = null
|
|
60
|
+
@closing = null
|
|
61
|
+
@waiting = []
|
|
62
|
+
@active = 0
|
|
63
|
+
@tasks = Object/create(null)
|
|
64
|
+
@ready = Promise/withResolvers()
|
|
65
|
+
@readyIds = []
|
|
66
|
+
@destroying = false
|
|
67
|
+
@static = null
|
|
68
|
+
|
|
69
|
+
pathJoin(directory, file)
|
|
70
|
+
return path/join(directory, file)
|
|
71
|
+
|
|
72
|
+
Server:publish(path, key, data)
|
|
73
|
+
if @state != "running"
|
|
74
|
+
return Promise/reject("Server is not running")
|
|
75
|
+
return @broker.publish(path, key, data)
|
|
76
|
+
|
|
77
|
+
Server:cursor(path, key)
|
|
78
|
+
if @state != "running"
|
|
79
|
+
return Promise/reject("Server is not running")
|
|
80
|
+
return @broker.cursor(path, key)
|
|
81
|
+
|
|
82
|
+
Server:start()
|
|
83
|
+
if @state != "new"
|
|
84
|
+
return Promise/reject("Server can only be started once")
|
|
85
|
+
@state = "starting"
|
|
86
|
+
@starting = launch()
|
|
87
|
+
return @starting
|
|
88
|
+
|
|
89
|
+
Server:launch()
|
|
90
|
+
settings = @definition.settings
|
|
91
|
+
if settings.static != null
|
|
92
|
+
try
|
|
93
|
+
@static = new StaticFiles(settings.static)
|
|
94
|
+
catch error
|
|
95
|
+
@state = "closed"
|
|
96
|
+
return Promise/reject(error)
|
|
97
|
+
return chain @broker.open()
|
|
98
|
+
then(ignored)
|
|
99
|
+
capture settings
|
|
100
|
+
return prepareDatabase(settings.database)
|
|
101
|
+
then(ignored)
|
|
102
|
+
capture path
|
|
103
|
+
capture modules
|
|
104
|
+
capture settings
|
|
105
|
+
resolver = modules/createRequire(path/join(settings.root, "package.json"))
|
|
106
|
+
entry = resolver.resolve("@lango-project/lango-server")
|
|
107
|
+
@pool = new piscina/Piscina({
|
|
108
|
+
filename: path/join(path/dirname(entry), "worker.mjs")
|
|
109
|
+
name: "execute"
|
|
110
|
+
minThreads: settings.workers
|
|
111
|
+
maxThreads: settings.workers
|
|
112
|
+
maxQueue: settings.maxQueue
|
|
113
|
+
concurrentTasksPerWorker: 1
|
|
114
|
+
closeTimeout: settings.shutdownTimeoutMs
|
|
115
|
+
workerData: @definition
|
|
116
|
+
})
|
|
117
|
+
pool = @pool
|
|
118
|
+
pool.on("message", (message)
|
|
119
|
+
messageReceived(message)
|
|
120
|
+
)
|
|
121
|
+
pool.on("error", (error)
|
|
122
|
+
@ready.reject(error)
|
|
123
|
+
context log
|
|
124
|
+
log.err("Worker pool error")
|
|
125
|
+
)
|
|
126
|
+
return startupDeadline(@ready.promise, settings.startupTimeoutMs)
|
|
127
|
+
then(ignored)
|
|
128
|
+
capture settings
|
|
129
|
+
if @state != "starting"
|
|
130
|
+
throw "Server closed during startup"
|
|
131
|
+
server = new http/Server((req, res)
|
|
132
|
+
receive(req, res)
|
|
133
|
+
)
|
|
134
|
+
@server = server
|
|
135
|
+
server.on("connection", (socket)
|
|
136
|
+
connected(socket)
|
|
137
|
+
)
|
|
138
|
+
server.headersTimeout = settings.bodyTimeoutMs
|
|
139
|
+
server.requestTimeout = settings.bodyTimeoutMs
|
|
140
|
+
listening = new Promise((resolve, reject)
|
|
141
|
+
capture server
|
|
142
|
+
capture settings
|
|
143
|
+
server.once("error", reject)
|
|
144
|
+
server.listen(settings.port, settings.host, ()
|
|
145
|
+
capture server
|
|
146
|
+
capture resolve
|
|
147
|
+
capture reject
|
|
148
|
+
server.removeListener("error", reject)
|
|
149
|
+
if @state != "starting"
|
|
150
|
+
reject("Server closed during startup")
|
|
151
|
+
return null
|
|
152
|
+
@state = "running"
|
|
153
|
+
address = server.address()
|
|
154
|
+
resolve({
|
|
155
|
+
host: address.address
|
|
156
|
+
port: address.port
|
|
157
|
+
})
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
return listening
|
|
161
|
+
catch(error)
|
|
162
|
+
@broker.close()
|
|
163
|
+
@state = "closed"
|
|
164
|
+
return chain destroyPool()
|
|
165
|
+
then(ignored)
|
|
166
|
+
capture error
|
|
167
|
+
throw error
|
|
168
|
+
|
|
169
|
+
startupDeadline(pending, timeout)
|
|
170
|
+
expired = Promise/withResolvers()
|
|
171
|
+
expire = ()
|
|
172
|
+
capture expired
|
|
173
|
+
expired.reject("Worker startup timed out")
|
|
174
|
+
timer = setTimeout(expire, timeout)
|
|
175
|
+
return chain Promise/race([pending, expired.promise])
|
|
176
|
+
finally
|
|
177
|
+
capture timer
|
|
178
|
+
clearTimeout(timer)
|
|
179
|
+
|
|
180
|
+
Server:messageReceived(message)
|
|
181
|
+
if message.kind == "longpoll"
|
|
182
|
+
brokerMessage(message)
|
|
183
|
+
return null
|
|
184
|
+
if message.kind == "ready"
|
|
185
|
+
if !@readyIds.includes(message.threadId)
|
|
186
|
+
@readyIds.add(message.threadId)
|
|
187
|
+
if @readyIds.length >= @definition.settings.workers
|
|
188
|
+
@ready.resolve(null)
|
|
189
|
+
return null
|
|
190
|
+
if !(message.id in @tasks)
|
|
191
|
+
return null
|
|
192
|
+
record = @tasks[message.id]
|
|
193
|
+
if message.kind == "prepared"
|
|
194
|
+
record.result = message.result
|
|
195
|
+
resetDeadline(record, @definition.settings.eventTimeoutMs)
|
|
196
|
+
if message.kind == "event"
|
|
197
|
+
resetDeadline(record, @definition.settings.eventTimeoutMs)
|
|
198
|
+
if message.kind == "handler" and record.result == null
|
|
199
|
+
resetDeadline(record, record.deadline - Date/now())
|
|
200
|
+
return null
|
|
201
|
+
|
|
202
|
+
Server:resetDeadline(record, duration)
|
|
203
|
+
clearTimeout(record.timer)
|
|
204
|
+
if record.result == null
|
|
205
|
+
duration = Math/min(duration, record.deadline - Date/now())
|
|
206
|
+
expired = ()
|
|
207
|
+
capture record
|
|
208
|
+
expire(record)
|
|
209
|
+
record.timer = setTimeout(expired, Math/max(1, duration))
|
|
210
|
+
return null
|
|
211
|
+
|
|
212
|
+
Server:connected(socket)
|
|
213
|
+
record = {
|
|
214
|
+
socket: socket
|
|
215
|
+
active: 0
|
|
216
|
+
}
|
|
217
|
+
@connections.add(record)
|
|
218
|
+
socket.once("close", ()
|
|
219
|
+
capture record
|
|
220
|
+
index = @connections.indexOf(record)
|
|
221
|
+
if index >= 0
|
|
222
|
+
@connections.splice(index, 1)
|
|
223
|
+
)
|
|
224
|
+
return null
|
|
225
|
+
|
|
226
|
+
Server:trackResponse(req, res)
|
|
227
|
+
for @connections as record
|
|
228
|
+
if record.socket == req.socket
|
|
229
|
+
record.active = record.active + 1
|
|
230
|
+
state = {
|
|
231
|
+
done: false
|
|
232
|
+
}
|
|
233
|
+
complete = ()
|
|
234
|
+
capture record
|
|
235
|
+
capture state
|
|
236
|
+
if state.done
|
|
237
|
+
return null
|
|
238
|
+
state.done = true
|
|
239
|
+
record.active = record.active - 1
|
|
240
|
+
if @state == "closing" and record.active == 0
|
|
241
|
+
record.socket.end()
|
|
242
|
+
return null
|
|
243
|
+
res.once("finish", complete)
|
|
244
|
+
res.once("close", complete)
|
|
245
|
+
return null
|
|
246
|
+
return null
|
|
247
|
+
|
|
248
|
+
Server:receive(req, res)
|
|
249
|
+
trackResponse(req, res)
|
|
250
|
+
req.on("error", (error)
|
|
251
|
+
return null
|
|
252
|
+
)
|
|
253
|
+
if @state != "running"
|
|
254
|
+
return rejectHttp(req, res, 503, "Unavailable")
|
|
255
|
+
if @static != null and (req.method == "GET" or req.method == "HEAD")
|
|
256
|
+
failed = (status, code)
|
|
257
|
+
capture res
|
|
258
|
+
send(res, failure(status, code))
|
|
259
|
+
return @static.receive(req, res, failed)
|
|
260
|
+
if req.method != "POST"
|
|
261
|
+
allowed = "POST"
|
|
262
|
+
if @static != null
|
|
263
|
+
allowed = "GET, HEAD, POST"
|
|
264
|
+
res.setHeader("Allow", allowed)
|
|
265
|
+
return rejectHttp(req, res, 405, "MethodNotAllowed")
|
|
266
|
+
if req.url.includes("?") or req.url.includes("#") or req.url.includes("\\")
|
|
267
|
+
return rejectHttp(req, res, 400, "InvalidPath")
|
|
268
|
+
type = req.headers["content-type"]
|
|
269
|
+
media = ""
|
|
270
|
+
if is_string(type)
|
|
271
|
+
parts = type.split(";")
|
|
272
|
+
media = parts[0].trim()
|
|
273
|
+
if media.toLowerCase() != "application/json"
|
|
274
|
+
return rejectHttp(req, res, 415, "JsonRequired")
|
|
275
|
+
settings = @definition.settings
|
|
276
|
+
if @active >= settings.workers and @waiting.length >= settings.maxQueue
|
|
277
|
+
return rejectHttp(req, res, 503, "QueueFull")
|
|
278
|
+
complete = (body)
|
|
279
|
+
capture req
|
|
280
|
+
capture res
|
|
281
|
+
submit(req, res, body)
|
|
282
|
+
failed = (status, code)
|
|
283
|
+
capture res
|
|
284
|
+
send(res, failure(status, code))
|
|
285
|
+
reader = new BodyReader(req, res, settings, complete, failed)
|
|
286
|
+
reader.start()
|
|
287
|
+
return null
|
|
288
|
+
|
|
289
|
+
Server:submit(req, res, body)
|
|
290
|
+
pending = null
|
|
291
|
+
route = @broker.route(req.url)
|
|
292
|
+
if route == null
|
|
293
|
+
pending = dispatch(req, res, body)
|
|
294
|
+
else
|
|
295
|
+
pending = longpoll(req, res, body, route)
|
|
296
|
+
return chain pending
|
|
297
|
+
then(result)
|
|
298
|
+
capture res
|
|
299
|
+
send(res, result)
|
|
300
|
+
return null
|
|
301
|
+
catch(error)
|
|
302
|
+
capture res
|
|
303
|
+
send(res, failure(500, "InternalServerError"))
|
|
304
|
+
return null
|
|
305
|
+
|
|
306
|
+
Server:longpoll(req, res, body, route)
|
|
307
|
+
if Object/keys(@polls).length >= @definition.settings.redis.maxPendingPolls
|
|
308
|
+
return Promise/resolve(failure(503, "PollLimit"))
|
|
309
|
+
authorize = (token, result)
|
|
310
|
+
capture req
|
|
311
|
+
capture res
|
|
312
|
+
capture body
|
|
313
|
+
return dispatch(req, res, body, true, token, result)
|
|
314
|
+
session = new LongpollSession(@broker, route, authorize, body)
|
|
315
|
+
id = crypto/randomUUID()
|
|
316
|
+
@polls[id] = session
|
|
317
|
+
disconnected = ()
|
|
318
|
+
capture session
|
|
319
|
+
session.cancel()
|
|
320
|
+
res.once("close", disconnected)
|
|
321
|
+
return chain session.run()
|
|
322
|
+
finally
|
|
323
|
+
capture res
|
|
324
|
+
capture disconnected
|
|
325
|
+
capture id
|
|
326
|
+
res.removeListener("close", disconnected)
|
|
327
|
+
Reflect/deleteProperty(@polls, id)
|
|
328
|
+
|
|
329
|
+
Server:brokerMessage(message)
|
|
330
|
+
port = message.port
|
|
331
|
+
pending = null
|
|
332
|
+
try
|
|
333
|
+
switch message.operation
|
|
334
|
+
case "publish"
|
|
335
|
+
pending = publish(message.path, message.key, message.data)
|
|
336
|
+
case "cursor"
|
|
337
|
+
pending = cursor(message.path, message.key)
|
|
338
|
+
else
|
|
339
|
+
pending = Promise/reject("Unknown longpoll operation")
|
|
340
|
+
catch error
|
|
341
|
+
pending = Promise/reject(error)
|
|
342
|
+
return chain pending
|
|
343
|
+
then(value)
|
|
344
|
+
capture port
|
|
345
|
+
brokerReply(port, {
|
|
346
|
+
ok: true
|
|
347
|
+
value: value
|
|
348
|
+
})
|
|
349
|
+
catch(error)
|
|
350
|
+
capture port
|
|
351
|
+
brokerReply(port, {
|
|
352
|
+
ok: false
|
|
353
|
+
})
|
|
354
|
+
finally
|
|
355
|
+
capture port
|
|
356
|
+
port.close()
|
|
357
|
+
|
|
358
|
+
Server:dispatch(req, res, body, authorize = false, token = null, pollResult = null)
|
|
359
|
+
settings = @definition.settings
|
|
360
|
+
if @state != "running" or (@active >= settings.workers and @waiting.length >= settings.maxQueue)
|
|
361
|
+
return Promise/resolve(failure(503, "Unavailable"))
|
|
362
|
+
id = crypto/randomUUID()
|
|
363
|
+
record = {
|
|
364
|
+
id: id
|
|
365
|
+
task: {
|
|
366
|
+
authorize: authorize
|
|
367
|
+
pollResult: pollResult
|
|
368
|
+
body: body
|
|
369
|
+
request: {
|
|
370
|
+
id: id
|
|
371
|
+
method: req.method
|
|
372
|
+
path: req.url
|
|
373
|
+
headers: Object/assign({}, req.headers)
|
|
374
|
+
remoteAddress: req.socket.remoteAddress
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
completed: Promise/withResolvers()
|
|
378
|
+
controller: new AbortController()
|
|
379
|
+
started: false
|
|
380
|
+
result: null
|
|
381
|
+
expired: false
|
|
382
|
+
deadline: Date/now() + settings.requestTimeoutMs
|
|
383
|
+
timer: null
|
|
384
|
+
}
|
|
385
|
+
if token != null
|
|
386
|
+
record.task.request.headers.authorization = "Bearer ${token}"
|
|
387
|
+
@tasks[id] = record
|
|
388
|
+
resetDeadline(record, settings.requestTimeoutMs)
|
|
389
|
+
@waiting.add(record)
|
|
390
|
+
disconnected = ()
|
|
391
|
+
capture res
|
|
392
|
+
capture record
|
|
393
|
+
if !res.writableEnded
|
|
394
|
+
cancel(record)
|
|
395
|
+
res.once("close", disconnected)
|
|
396
|
+
pump()
|
|
397
|
+
return chain record.completed.promise
|
|
398
|
+
catch(error)
|
|
399
|
+
capture record
|
|
400
|
+
if record.expired
|
|
401
|
+
return failure(504, "RequestTimeout")
|
|
402
|
+
return failure(500, "InternalServerError")
|
|
403
|
+
finally
|
|
404
|
+
capture res
|
|
405
|
+
capture disconnected
|
|
406
|
+
res.removeListener("close", disconnected)
|
|
407
|
+
|
|
408
|
+
Server:expire(record)
|
|
409
|
+
record.expired = true
|
|
410
|
+
cancel(record)
|
|
411
|
+
return null
|
|
412
|
+
|
|
413
|
+
Server:cancel(record)
|
|
414
|
+
if !(record.id in @tasks)
|
|
415
|
+
return null
|
|
416
|
+
if record.started
|
|
417
|
+
record.controller.abort()
|
|
418
|
+
else
|
|
419
|
+
index = @waiting.indexOf(record)
|
|
420
|
+
if index >= 0
|
|
421
|
+
@waiting.splice(index, 1)
|
|
422
|
+
finish(record, null, "Request cancelled")
|
|
423
|
+
return null
|
|
424
|
+
|
|
425
|
+
Server:pump()
|
|
426
|
+
loop while !@destroying and @waiting.length > 0 and @active < @definition.settings.workers
|
|
427
|
+
record = @waiting.shift()
|
|
428
|
+
record.started = true
|
|
429
|
+
@active = @active + 1
|
|
430
|
+
pending = @pool.run(record.task, {
|
|
431
|
+
signal: record.controller.signal
|
|
432
|
+
})
|
|
433
|
+
settle(record, pending)
|
|
434
|
+
return null
|
|
435
|
+
|
|
436
|
+
Server:settle(record, pending)
|
|
437
|
+
return chain pending
|
|
438
|
+
then(result)
|
|
439
|
+
capture record
|
|
440
|
+
finish(record, result, null)
|
|
441
|
+
return null
|
|
442
|
+
catch(error)
|
|
443
|
+
capture record
|
|
444
|
+
if record.result != null
|
|
445
|
+
context log
|
|
446
|
+
log.err("[${record.id}] Request event interrupted; preserving prepared result")
|
|
447
|
+
finish(record, record.result, error)
|
|
448
|
+
return null
|
|
449
|
+
finally
|
|
450
|
+
@active = @active - 1
|
|
451
|
+
pump()
|
|
452
|
+
|
|
453
|
+
Server:finish(record, result, error)
|
|
454
|
+
clearTimeout(record.timer)
|
|
455
|
+
Reflect/deleteProperty(@tasks, record.id)
|
|
456
|
+
if result != null
|
|
457
|
+
record.completed.resolve(result)
|
|
458
|
+
else
|
|
459
|
+
record.completed.reject(error)
|
|
460
|
+
return null
|
|
461
|
+
|
|
462
|
+
Server:close()
|
|
463
|
+
if @closing != null
|
|
464
|
+
return @closing
|
|
465
|
+
previous = @state
|
|
466
|
+
@state = "closing"
|
|
467
|
+
@shutdownDeadline = Date/now() + @definition.settings.shutdownTimeoutMs
|
|
468
|
+
if previous == "new" or previous == "closed"
|
|
469
|
+
@state = "closed"
|
|
470
|
+
@closing = Promise/resolve(null)
|
|
471
|
+
return @closing
|
|
472
|
+
@ready.reject("Server closed during startup")
|
|
473
|
+
@closing = stop()
|
|
474
|
+
return @closing
|
|
475
|
+
|
|
476
|
+
Server:stop()
|
|
477
|
+
for @connections as record
|
|
478
|
+
if record.active == 0
|
|
479
|
+
record.socket.destroy()
|
|
480
|
+
for @polls as id: session
|
|
481
|
+
session.cancel()
|
|
482
|
+
drained = new Promise((resolve)
|
|
483
|
+
server = @server
|
|
484
|
+
if server == null or !server.listening
|
|
485
|
+
resolve(null)
|
|
486
|
+
else
|
|
487
|
+
server.close(()
|
|
488
|
+
capture resolve
|
|
489
|
+
resolve(null)
|
|
490
|
+
)
|
|
491
|
+
)
|
|
492
|
+
expired = Promise/withResolvers()
|
|
493
|
+
force = ()
|
|
494
|
+
capture expired
|
|
495
|
+
@destroying = true
|
|
496
|
+
for @waiting as record
|
|
497
|
+
finish(record, null, "Server closed")
|
|
498
|
+
@waiting = []
|
|
499
|
+
if @server != null
|
|
500
|
+
@server.closeAllConnections()
|
|
501
|
+
expired.reject("Server shutdown timed out")
|
|
502
|
+
timer = setTimeout(force, @definition.settings.shutdownTimeoutMs)
|
|
503
|
+
return chain Promise/race([drained, expired.promise])
|
|
504
|
+
finally
|
|
505
|
+
capture timer
|
|
506
|
+
clearTimeout(timer)
|
|
507
|
+
return destroyPool()
|
|
508
|
+
finally
|
|
509
|
+
@broker.close()
|
|
510
|
+
@state = "closed"
|
|
511
|
+
|
|
512
|
+
Server:destroyPool()
|
|
513
|
+
@destroying = true
|
|
514
|
+
if @pool == null
|
|
515
|
+
return Promise/resolve(null)
|
|
516
|
+
pool = @pool
|
|
517
|
+
pending = Promise/resolve(null)
|
|
518
|
+
if @definition.settings.database != null and @state == "closing" and @active == 0
|
|
519
|
+
remaining = @shutdownDeadline - Date/now()
|
|
520
|
+
if remaining > 0
|
|
521
|
+
tasks = []
|
|
522
|
+
for range(@definition.settings.workers) as index
|
|
523
|
+
tasks.add(pool.run({
|
|
524
|
+
close: true
|
|
525
|
+
}))
|
|
526
|
+
pending = startupDeadline(Promise/all(tasks), remaining)
|
|
527
|
+
return chain pending
|
|
528
|
+
finally
|
|
529
|
+
capture pool
|
|
530
|
+
return pool.destroy()
|
|
531
|
+
|
|
532
|
+
Server:send(res, result)
|
|
533
|
+
if @state == "closing"
|
|
534
|
+
result.headers.connection = "close"
|
|
535
|
+
return write(res, result)
|
|
536
|
+
|
|
537
|
+
failure(status, code)
|
|
538
|
+
return {
|
|
539
|
+
status: status
|
|
540
|
+
headers: {
|
|
541
|
+
"content-type": "application/json; charset=utf-8"
|
|
542
|
+
"cache-control": "no-store"
|
|
543
|
+
}
|
|
544
|
+
body: to_json({
|
|
545
|
+
error: code
|
|
546
|
+
})
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
write(res, result)
|
|
550
|
+
if res.destroyed or res.writableEnded
|
|
551
|
+
return null
|
|
552
|
+
try
|
|
553
|
+
res.writeHead(result.status, result.headers)
|
|
554
|
+
res.end(result.body)
|
|
555
|
+
catch error
|
|
556
|
+
res.destroy()
|
|
557
|
+
return null
|
|
558
|
+
|
|
559
|
+
rejectHttp(req, res, status, code)
|
|
560
|
+
res.setHeader("Connection", "close")
|
|
561
|
+
res.once("finish", ()
|
|
562
|
+
capture req
|
|
563
|
+
req.destroy()
|
|
564
|
+
)
|
|
565
|
+
req.resume()
|
|
566
|
+
return write(res, failure(status, code))
|
|
567
|
+
|
|
568
|
+
main(args)
|
|
569
|
+
if args.length != 1
|
|
570
|
+
throw "Usage: lango-server <application-directory>"
|
|
571
|
+
server = new Server(args[0])
|
|
572
|
+
stop = ()
|
|
573
|
+
capture server
|
|
574
|
+
return chain server.close()
|
|
575
|
+
catch(error)
|
|
576
|
+
context log
|
|
577
|
+
log.err("Server shutdown failed")
|
|
578
|
+
process.exitCode = 1
|
|
579
|
+
process.once("SIGTERM", stop)
|
|
580
|
+
process.once("SIGINT", stop)
|
|
581
|
+
return chain server.start()
|
|
582
|
+
then(address)
|
|
583
|
+
context log
|
|
584
|
+
log.info("Listening on ${address.host}:${address.port}")
|
|
585
|
+
return null
|
|
586
|
+
catch(error)
|
|
587
|
+
capture stop
|
|
588
|
+
process.removeListener("SIGTERM", stop)
|
|
589
|
+
process.removeListener("SIGINT", stop)
|
|
590
|
+
throw error
|
|
591
|
+
|
|
592
|
+
brokerReply(port, message)
|
|
593
|
+
try
|
|
594
|
+
port.postMessage(message)
|
|
595
|
+
catch error
|
|
596
|
+
return null
|
|
597
|
+
return null
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import js "node:worker_threads" as threads
|
|
2
|
+
import js global Promise
|
|
3
|
+
import js global setTimeout
|
|
4
|
+
import js global clearTimeout
|
|
5
|
+
|
|
6
|
+
export class ServerContext()
|
|
7
|
+
methods
|
|
8
|
+
publish(path, key, data)
|
|
9
|
+
cursor(path, key)
|
|
10
|
+
|
|
11
|
+
ServerContext()
|
|
12
|
+
return null
|
|
13
|
+
|
|
14
|
+
ServerContext:publish(path, key, data)
|
|
15
|
+
return invoke("publish", path, key, data)
|
|
16
|
+
|
|
17
|
+
ServerContext:cursor(path, key)
|
|
18
|
+
return invoke("cursor", path, key, null)
|
|
19
|
+
|
|
20
|
+
invoke(operation, path, key, data)
|
|
21
|
+
channel = new threads/MessageChannel()
|
|
22
|
+
port = channel.port1
|
|
23
|
+
remote = channel.port2
|
|
24
|
+
pending = new Promise((resolve, reject)
|
|
25
|
+
capture port
|
|
26
|
+
port.once("message", (message)
|
|
27
|
+
capture resolve
|
|
28
|
+
capture reject
|
|
29
|
+
if message.ok
|
|
30
|
+
resolve(message.value)
|
|
31
|
+
else
|
|
32
|
+
reject("Longpoll operation failed")
|
|
33
|
+
)
|
|
34
|
+
)
|
|
35
|
+
expired = Promise/withResolvers()
|
|
36
|
+
fail = ()
|
|
37
|
+
capture expired
|
|
38
|
+
expired.reject("Longpoll operation timed out")
|
|
39
|
+
timer = setTimeout(fail, 4000)
|
|
40
|
+
try
|
|
41
|
+
threads/parentPort/postMessage({
|
|
42
|
+
kind: "longpoll"
|
|
43
|
+
operation: operation
|
|
44
|
+
path: path
|
|
45
|
+
key: key
|
|
46
|
+
data: from_json(to_json(data))
|
|
47
|
+
port: remote
|
|
48
|
+
}, [remote])
|
|
49
|
+
catch error
|
|
50
|
+
port.close()
|
|
51
|
+
clearTimeout(timer)
|
|
52
|
+
throw error
|
|
53
|
+
return chain Promise/race([pending, expired.promise])
|
|
54
|
+
finally
|
|
55
|
+
capture port
|
|
56
|
+
capture timer
|
|
57
|
+
clearTimeout(timer)
|
|
58
|
+
port.close()
|