@lango-project/lango-server 0.1.0 → 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/README.md +283 -4
- package/dist/builder.mjs +16 -1
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/endpoint.mjs +6 -0
- package/dist/cli/endpoint.mjs.map +1 -1
- 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 +14 -2
- package/dist/cli/options.mjs.map +1 -1
- package/dist/cli/routes.mjs +66 -2
- package/dist/cli/routes.mjs.map +1 -1
- package/dist/cli/server.mjs +243 -23
- package/dist/cli/server.mjs.map +1 -1
- package/dist/cli/static_files.mjs +125 -0
- package/dist/cli/static_files.mjs.map +1 -0
- package/dist/endpoint.mjs +6 -0
- package/dist/endpoint.mjs.map +1 -1
- package/dist/group.mjs +17 -1
- package/dist/group.mjs.map +1 -1
- 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 +14 -2
- package/dist/options.mjs.map +1 -1
- package/dist/routes.mjs +66 -2
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +118 -44
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +243 -23
- package/dist/server.mjs.map +1 -1
- package/dist/server_context.mjs +78 -0
- package/dist/server_context.mjs.map +1 -0
- package/dist/static_files.mjs +125 -0
- package/dist/static_files.mjs.map +1 -0
- package/package.json +9 -4
- package/src/builder.lango +11 -0
- package/src/endpoint.lango +7 -0
- package/src/group.lango +14 -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 +16 -0
- package/src/routes.lango +65 -1
- package/src/runtime.lango +62 -0
- package/src/server.lango +182 -17
- package/src/server_context.lango +58 -0
- package/src/static_files.lango +90 -0
package/src/options.lango
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import "./longpoll_options"
|
|
2
|
+
redisOptions(spec)
|
|
1
3
|
import "./database_options"
|
|
2
4
|
databaseOptions(root, spec, workers)
|
|
5
|
+
import "./static_files"
|
|
6
|
+
staticOptions(root, spec)
|
|
3
7
|
import js "node:fs" as fs
|
|
4
8
|
import js "node:path" as path
|
|
5
9
|
import js global Object
|
|
@@ -38,6 +42,8 @@ options(root, supplied = null)
|
|
|
38
42
|
activeKeyId: "current"
|
|
39
43
|
requestEvent: false
|
|
40
44
|
database: null
|
|
45
|
+
redis: null
|
|
46
|
+
static: null
|
|
41
47
|
}
|
|
42
48
|
if supplied != null
|
|
43
49
|
for supplied as key: value
|
|
@@ -74,6 +80,8 @@ options(root, supplied = null)
|
|
|
74
80
|
result.keys = keys
|
|
75
81
|
result.key = null
|
|
76
82
|
result.database = databaseOptions(result.root, result.database, result.workers)
|
|
83
|
+
result.redis = redisOptions(result.redis)
|
|
84
|
+
result.static = staticOptions(result.root, result.static)
|
|
77
85
|
return result
|
|
78
86
|
|
|
79
87
|
deployment(directory)
|
|
@@ -90,12 +98,20 @@ deployment(directory)
|
|
|
90
98
|
throw "Duplicate or nonempty deployment section: ${name}"
|
|
91
99
|
seen.add(name)
|
|
92
100
|
switch name
|
|
101
|
+
case "static"
|
|
102
|
+
attributes(tag, ["path", "directory", "index"])
|
|
103
|
+
settings.static = tag.attributes()
|
|
104
|
+
continue
|
|
93
105
|
case "http"
|
|
94
106
|
attributes(tag, ["host", "port", "bodyLimit", "bodyTimeoutMs"])
|
|
95
107
|
case "workers"
|
|
96
108
|
attributes(tag, ["workers", "maxQueue", "requestTimeoutMs", "startupTimeoutMs", "shutdownTimeoutMs"])
|
|
97
109
|
case "events"
|
|
98
110
|
attributes(tag, ["requestEvent", "eventTimeoutMs"])
|
|
111
|
+
case "redis"
|
|
112
|
+
attributes(tag, ["connectionEnv", "namespace", "maxPendingPolls"])
|
|
113
|
+
settings.redis = tag.attributes()
|
|
114
|
+
continue
|
|
99
115
|
case "database"
|
|
100
116
|
attributes(tag, ["type", "file", "connectionEnv", "busyTimeoutMs", "queryTimeoutMs", "connectTimeoutSeconds"])
|
|
101
117
|
settings.database = tag.attributes()
|
package/src/routes.lango
CHANGED
|
@@ -5,12 +5,21 @@ import js "node:path" as path
|
|
|
5
5
|
import js "node:module" as modules
|
|
6
6
|
import "./options"
|
|
7
7
|
attributes(tag, allowed)
|
|
8
|
+
import js global Object
|
|
9
|
+
import "./middleware"
|
|
10
|
+
middlewareName(name)
|
|
8
11
|
import "./endpoint"
|
|
9
12
|
class Endpoint
|
|
10
13
|
|
|
14
|
+
import "./longpoll_options"
|
|
15
|
+
longpollOptions(spec)
|
|
16
|
+
|
|
11
17
|
export class Routes(root)
|
|
12
18
|
methods
|
|
13
19
|
endpoint(spec)
|
|
20
|
+
addMiddleware(name, spec)
|
|
21
|
+
middlewares()
|
|
22
|
+
longpoll(spec)
|
|
14
23
|
include(spec, auth = null)
|
|
15
24
|
xml(file)
|
|
16
25
|
definitions()
|
|
@@ -21,6 +30,7 @@ Routes(root)
|
|
|
21
30
|
@root = path/resolve(root)
|
|
22
31
|
@items = []
|
|
23
32
|
@events = {}
|
|
33
|
+
@middlewares = Object/create(null)
|
|
24
34
|
|
|
25
35
|
Routes:endpoint(spec)
|
|
26
36
|
item = new Endpoint(spec)
|
|
@@ -32,9 +42,38 @@ Routes:endpoint(spec)
|
|
|
32
42
|
module: resolveModule(item.module, @root)
|
|
33
43
|
handler: item.handler
|
|
34
44
|
auth: item.auth
|
|
45
|
+
middleware: item.middleware
|
|
35
46
|
})
|
|
36
47
|
return @this
|
|
37
48
|
|
|
49
|
+
Routes:addMiddleware(name, spec)
|
|
50
|
+
middlewareName(name)
|
|
51
|
+
if Object/hasOwn(@middlewares, name)
|
|
52
|
+
throw "Duplicate middleware: ${name}"
|
|
53
|
+
if !is_object(spec) or is_array(spec)
|
|
54
|
+
throw "Middleware requires a module and exported handler"
|
|
55
|
+
for spec as key: value
|
|
56
|
+
if key != "module" and key != "handler"
|
|
57
|
+
throw "Unknown middleware option: ${key}"
|
|
58
|
+
if !is_string(spec.handler) or spec.handler == ""
|
|
59
|
+
throw "Middleware handler is required"
|
|
60
|
+
@middlewares[name] = {
|
|
61
|
+
module: resolveModule(spec.module, @root)
|
|
62
|
+
handler: spec.handler
|
|
63
|
+
}
|
|
64
|
+
return @this
|
|
65
|
+
|
|
66
|
+
Routes:middlewares()
|
|
67
|
+
return from_json(to_json(@middlewares))
|
|
68
|
+
|
|
69
|
+
Routes:longpoll(spec)
|
|
70
|
+
item = longpollOptions(spec)
|
|
71
|
+
for @items as other
|
|
72
|
+
if other.path == item.path
|
|
73
|
+
throw "Duplicate endpoint: ${item.path}"
|
|
74
|
+
@items.add(item)
|
|
75
|
+
return @this
|
|
76
|
+
|
|
38
77
|
Routes:event(spec)
|
|
39
78
|
if !contains(["register", "validate", "request"], spec.name)
|
|
40
79
|
throw "Unknown event: ${spec.name}"
|
|
@@ -133,6 +172,14 @@ Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack,
|
|
|
133
172
|
handler: spec.handler
|
|
134
173
|
auth: auth
|
|
135
174
|
})
|
|
175
|
+
case "longpoll"
|
|
176
|
+
attributes(tag, ["path", "auth", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"])
|
|
177
|
+
leaf(tag)
|
|
178
|
+
spec.path = joinPath(prefix, spec.path)
|
|
179
|
+
spec.auth = auth
|
|
180
|
+
if spec.fallback != null
|
|
181
|
+
spec.fallback = joinPath(prefix, spec.fallback)
|
|
182
|
+
longpoll(spec)
|
|
136
183
|
case "group"
|
|
137
184
|
attributes(tag, ["path", "module", "auth"])
|
|
138
185
|
collect(tag, joinPath(prefix, spec.path), mod, auth, directory, stack, false)
|
|
@@ -140,6 +187,15 @@ Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack,
|
|
|
140
187
|
attributes(tag, ["module", "path"])
|
|
141
188
|
leaf(tag)
|
|
142
189
|
includeAt(spec, directory, prefix, stack, auth)
|
|
190
|
+
case "middleware"
|
|
191
|
+
attributes(tag, ["name", "module", "handler"])
|
|
192
|
+
leaf(tag)
|
|
193
|
+
if !allowEvents
|
|
194
|
+
throw "Middleware declarations are only allowed in the application's root routes.xml"
|
|
195
|
+
addMiddleware(spec.name, {
|
|
196
|
+
module: resolveModule(spec.module, directory)
|
|
197
|
+
handler: spec.handler
|
|
198
|
+
})
|
|
143
199
|
case "event"
|
|
144
200
|
attributes(tag, ["name", "module", "handler"])
|
|
145
201
|
leaf(tag)
|
|
@@ -151,7 +207,15 @@ Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack,
|
|
|
151
207
|
handler: spec.handler
|
|
152
208
|
})
|
|
153
209
|
else
|
|
154
|
-
|
|
210
|
+
attributes(tag, ["path", "module", "handler", "auth"])
|
|
211
|
+
leaf(tag)
|
|
212
|
+
endpoint({
|
|
213
|
+
path: joinPath(prefix, spec.path)
|
|
214
|
+
module: resolveModule(mod, directory)
|
|
215
|
+
handler: spec.handler
|
|
216
|
+
auth: auth
|
|
217
|
+
middleware: tag.tag()
|
|
218
|
+
})
|
|
155
219
|
return null
|
|
156
220
|
|
|
157
221
|
leaf(tag)
|
package/src/runtime.lango
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import "./server_context"
|
|
2
|
+
class ServerContext
|
|
1
3
|
import "./database"
|
|
2
4
|
class Database
|
|
3
5
|
import js global Promise
|
|
@@ -19,16 +21,21 @@ export class Runtime(definition)
|
|
|
19
21
|
close()
|
|
20
22
|
|
|
21
23
|
Runtime(definition)
|
|
24
|
+
@serverContext = new ServerContext()
|
|
22
25
|
@definition = definition
|
|
23
26
|
@routes = []
|
|
24
27
|
@events = {}
|
|
25
28
|
@tokens = new Tokens(definition.settings)
|
|
26
29
|
@modules = {}
|
|
30
|
+
@middlewares = Object/create(null)
|
|
27
31
|
@database = new Database(definition.settings.database, definition.settings.root)
|
|
28
32
|
|
|
29
33
|
Runtime:preload()
|
|
30
34
|
definitions = @definition.routes
|
|
31
35
|
pending = @database.open()
|
|
36
|
+
if @definition.middlewares != null
|
|
37
|
+
for @definition.middlewares as name: spec
|
|
38
|
+
pending = prepareMiddleware(pending, name, spec)
|
|
32
39
|
for definitions as route
|
|
33
40
|
pending = prepareRoute(pending, route)
|
|
34
41
|
events = @definition.events
|
|
@@ -43,17 +50,37 @@ Runtime:close()
|
|
|
43
50
|
@database.close()
|
|
44
51
|
return null
|
|
45
52
|
|
|
53
|
+
Runtime:prepareMiddleware(pending, name, spec)
|
|
54
|
+
return chain pending
|
|
55
|
+
then(ignored)
|
|
56
|
+
capture spec
|
|
57
|
+
return target(spec)
|
|
58
|
+
then(handler)
|
|
59
|
+
capture name
|
|
60
|
+
@middlewares[name] = handler
|
|
61
|
+
return null
|
|
62
|
+
|
|
46
63
|
Runtime:prepareRoute(pending, route)
|
|
64
|
+
if route.kind == "longpoll"
|
|
65
|
+
@routes.add(route)
|
|
66
|
+
return pending
|
|
47
67
|
return chain pending
|
|
48
68
|
then(ignored)
|
|
49
69
|
capture route
|
|
50
70
|
return target(route)
|
|
51
71
|
then(handler)
|
|
52
72
|
capture route
|
|
73
|
+
middleware = null
|
|
74
|
+
name = route.middleware
|
|
75
|
+
if name != null
|
|
76
|
+
if !Object/hasOwn(@middlewares, name)
|
|
77
|
+
throw "Middleware is not registered: ${name}"
|
|
78
|
+
middleware = @middlewares[name]
|
|
53
79
|
@routes.add({
|
|
54
80
|
path: route.path
|
|
55
81
|
auth: route.auth
|
|
56
82
|
handler: handler
|
|
83
|
+
middleware: middleware
|
|
57
84
|
})
|
|
58
85
|
return null
|
|
59
86
|
|
|
@@ -98,6 +125,7 @@ Runtime:match(path)
|
|
|
98
125
|
|
|
99
126
|
Runtime:handle(task)
|
|
100
127
|
Object/freeze(task.request.headers)
|
|
128
|
+
context server = @serverContext
|
|
101
129
|
context request = Object/freeze(task.request)
|
|
102
130
|
context response = new Response()
|
|
103
131
|
context log
|
|
@@ -124,6 +152,7 @@ Runtime:handle(task)
|
|
|
124
152
|
context db = @database.scope()
|
|
125
153
|
return chain Promise/resolve(null)
|
|
126
154
|
then(ignored)
|
|
155
|
+
capture server
|
|
127
156
|
capture request
|
|
128
157
|
capture response
|
|
129
158
|
capture token
|
|
@@ -135,6 +164,7 @@ Runtime:handle(task)
|
|
|
135
164
|
return event("register", input)
|
|
136
165
|
return validate()
|
|
137
166
|
then(validatedAt)
|
|
167
|
+
capture server
|
|
138
168
|
capture request
|
|
139
169
|
capture response
|
|
140
170
|
capture token
|
|
@@ -151,9 +181,15 @@ Runtime:handle(task)
|
|
|
151
181
|
status: 401
|
|
152
182
|
error: "Unauthorized"
|
|
153
183
|
}
|
|
184
|
+
if route.kind == "longpoll"
|
|
185
|
+
return null
|
|
154
186
|
handler = route.handler
|
|
187
|
+
middleware = route.middleware
|
|
188
|
+
if middleware != null
|
|
189
|
+
return middleware(handler, request.path, input)
|
|
155
190
|
return handler(input)
|
|
156
191
|
then(output)
|
|
192
|
+
capture server
|
|
157
193
|
capture request
|
|
158
194
|
capture response
|
|
159
195
|
capture token
|
|
@@ -161,6 +197,24 @@ Runtime:handle(task)
|
|
|
161
197
|
capture db
|
|
162
198
|
capture task
|
|
163
199
|
capture claims
|
|
200
|
+
if task.authorize == true
|
|
201
|
+
closeScope(db, true)
|
|
202
|
+
authorizedClaims = token.snapshot()
|
|
203
|
+
authorizedClaims.validatedAt = claims.validatedAt
|
|
204
|
+
encoded = @tokens.sign(authorizedClaims)
|
|
205
|
+
if task.pollResult == null
|
|
206
|
+
return {
|
|
207
|
+
status: 200
|
|
208
|
+
authorization: encoded
|
|
209
|
+
}
|
|
210
|
+
if task.pollResult.resyncRequired
|
|
211
|
+
response.status(409)
|
|
212
|
+
pollBody = JSON/stringify(task.pollResult)
|
|
213
|
+
pollResponse = response.result(pollBody)
|
|
214
|
+
pollResponse.headers["x-lango-token"] = encoded
|
|
215
|
+
if @definition.settings.requestEvent and "request" in @events
|
|
216
|
+
return audit(request.id, pollResponse, task.body, pollBody, authorizedClaims)
|
|
217
|
+
return pollResponse
|
|
164
218
|
body = JSON/stringify(output)
|
|
165
219
|
if !is_string(body)
|
|
166
220
|
throw "Handler result is not JSON serializable"
|
|
@@ -173,6 +227,7 @@ Runtime:handle(task)
|
|
|
173
227
|
return audit(request.id, result, task.body, body, updated)
|
|
174
228
|
return result
|
|
175
229
|
catch(error)
|
|
230
|
+
capture server
|
|
176
231
|
capture request
|
|
177
232
|
capture log
|
|
178
233
|
capture db
|
|
@@ -188,6 +243,7 @@ Runtime:handle(task)
|
|
|
188
243
|
Runtime:event(name, input)
|
|
189
244
|
if !(name in @events)
|
|
190
245
|
return null
|
|
246
|
+
context server
|
|
191
247
|
context request
|
|
192
248
|
context response
|
|
193
249
|
context token
|
|
@@ -200,6 +256,7 @@ Runtime:event(name, input)
|
|
|
200
256
|
})
|
|
201
257
|
return chain Promise/resolve(null)
|
|
202
258
|
then(ignored)
|
|
259
|
+
capture server
|
|
203
260
|
capture request
|
|
204
261
|
capture response
|
|
205
262
|
capture token
|
|
@@ -209,6 +266,7 @@ Runtime:event(name, input)
|
|
|
209
266
|
capture input
|
|
210
267
|
return handler(input)
|
|
211
268
|
finally
|
|
269
|
+
capture server
|
|
212
270
|
capture request
|
|
213
271
|
capture threads
|
|
214
272
|
threads/parentPort/postMessage({
|
|
@@ -220,6 +278,7 @@ Runtime:validate()
|
|
|
220
278
|
context db
|
|
221
279
|
context token
|
|
222
280
|
context response
|
|
281
|
+
context server
|
|
223
282
|
context request
|
|
224
283
|
context log
|
|
225
284
|
claims = token.snapshot()
|
|
@@ -231,6 +290,7 @@ Runtime:validate()
|
|
|
231
290
|
then(ignored)
|
|
232
291
|
capture token
|
|
233
292
|
capture response
|
|
293
|
+
capture server
|
|
234
294
|
capture request
|
|
235
295
|
capture log
|
|
236
296
|
capture db
|
|
@@ -253,6 +313,7 @@ Runtime:validate()
|
|
|
253
313
|
|
|
254
314
|
Runtime:audit(id, result, input, output, claims)
|
|
255
315
|
context db = @database.scope()
|
|
316
|
+
context server
|
|
256
317
|
context request
|
|
257
318
|
context log
|
|
258
319
|
context token = new Token(claims, true)
|
|
@@ -270,6 +331,7 @@ Runtime:audit(id, result, input, output, claims)
|
|
|
270
331
|
return chain Promise/resolve(null)
|
|
271
332
|
then(ignored)
|
|
272
333
|
capture token
|
|
334
|
+
capture server
|
|
273
335
|
capture request
|
|
274
336
|
capture response
|
|
275
337
|
capture log
|
package/src/server.lango
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import "./longpoll_broker"
|
|
2
|
+
class LongpollBroker
|
|
3
|
+
import "./longpoll_session"
|
|
4
|
+
class LongpollSession
|
|
1
5
|
import "./database"
|
|
2
6
|
prepareDatabase(settings)
|
|
3
7
|
import js "node:http" as http
|
|
@@ -20,6 +24,8 @@ import "./routes"
|
|
|
20
24
|
class Routes
|
|
21
25
|
import "./body_reader"
|
|
22
26
|
class BodyReader
|
|
27
|
+
import "./static_files"
|
|
28
|
+
class StaticFiles
|
|
23
29
|
|
|
24
30
|
export main(args)
|
|
25
31
|
|
|
@@ -27,6 +33,8 @@ export class Server(path, definition = null)
|
|
|
27
33
|
methods
|
|
28
34
|
start()
|
|
29
35
|
close()
|
|
36
|
+
publish(path, key, data)
|
|
37
|
+
cursor(path, key)
|
|
30
38
|
|
|
31
39
|
Server(path, definition = null)
|
|
32
40
|
if path != null
|
|
@@ -37,11 +45,15 @@ Server(path, definition = null)
|
|
|
37
45
|
settings: deployment(directory)
|
|
38
46
|
routes: routes.definitions()
|
|
39
47
|
events: routes.events()
|
|
48
|
+
middlewares: routes.middlewares()
|
|
40
49
|
}
|
|
41
50
|
if definition == null
|
|
42
51
|
throw "Use Server(directory) or Builder.build()"
|
|
43
52
|
@definition = from_json(to_json(definition))
|
|
53
|
+
@broker = new LongpollBroker(@definition.settings, @definition.routes)
|
|
54
|
+
@polls = Object/create(null)
|
|
44
55
|
@state = "new"
|
|
56
|
+
@connections = []
|
|
45
57
|
@server = null
|
|
46
58
|
@pool = null
|
|
47
59
|
@starting = null
|
|
@@ -52,10 +64,21 @@ Server(path, definition = null)
|
|
|
52
64
|
@ready = Promise/withResolvers()
|
|
53
65
|
@readyIds = []
|
|
54
66
|
@destroying = false
|
|
67
|
+
@static = null
|
|
55
68
|
|
|
56
69
|
pathJoin(directory, file)
|
|
57
70
|
return path/join(directory, file)
|
|
58
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
|
+
|
|
59
82
|
Server:start()
|
|
60
83
|
if @state != "new"
|
|
61
84
|
return Promise/reject("Server can only be started once")
|
|
@@ -65,7 +88,16 @@ Server:start()
|
|
|
65
88
|
|
|
66
89
|
Server:launch()
|
|
67
90
|
settings = @definition.settings
|
|
68
|
-
|
|
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)
|
|
69
101
|
then(ignored)
|
|
70
102
|
capture path
|
|
71
103
|
capture modules
|
|
@@ -100,6 +132,9 @@ Server:launch()
|
|
|
100
132
|
receive(req, res)
|
|
101
133
|
)
|
|
102
134
|
@server = server
|
|
135
|
+
server.on("connection", (socket)
|
|
136
|
+
connected(socket)
|
|
137
|
+
)
|
|
103
138
|
server.headersTimeout = settings.bodyTimeoutMs
|
|
104
139
|
server.requestTimeout = settings.bodyTimeoutMs
|
|
105
140
|
listening = new Promise((resolve, reject)
|
|
@@ -124,6 +159,7 @@ Server:launch()
|
|
|
124
159
|
)
|
|
125
160
|
return listening
|
|
126
161
|
catch(error)
|
|
162
|
+
@broker.close()
|
|
127
163
|
@state = "closed"
|
|
128
164
|
return chain destroyPool()
|
|
129
165
|
then(ignored)
|
|
@@ -142,6 +178,9 @@ startupDeadline(pending, timeout)
|
|
|
142
178
|
clearTimeout(timer)
|
|
143
179
|
|
|
144
180
|
Server:messageReceived(message)
|
|
181
|
+
if message.kind == "longpoll"
|
|
182
|
+
brokerMessage(message)
|
|
183
|
+
return null
|
|
145
184
|
if message.kind == "ready"
|
|
146
185
|
if !@readyIds.includes(message.threadId)
|
|
147
186
|
@readyIds.add(message.threadId)
|
|
@@ -170,14 +209,59 @@ Server:resetDeadline(record, duration)
|
|
|
170
209
|
record.timer = setTimeout(expired, Math/max(1, duration))
|
|
171
210
|
return null
|
|
172
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
|
+
|
|
173
248
|
Server:receive(req, res)
|
|
249
|
+
trackResponse(req, res)
|
|
174
250
|
req.on("error", (error)
|
|
175
251
|
return null
|
|
176
252
|
)
|
|
177
253
|
if @state != "running"
|
|
178
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)
|
|
179
260
|
if req.method != "POST"
|
|
180
|
-
|
|
261
|
+
allowed = "POST"
|
|
262
|
+
if @static != null
|
|
263
|
+
allowed = "GET, HEAD, POST"
|
|
264
|
+
res.setHeader("Allow", allowed)
|
|
181
265
|
return rejectHttp(req, res, 405, "MethodNotAllowed")
|
|
182
266
|
if req.url.includes("?") or req.url.includes("#") or req.url.includes("\\")
|
|
183
267
|
return rejectHttp(req, res, 400, "InvalidPath")
|
|
@@ -203,19 +287,90 @@ Server:receive(req, res)
|
|
|
203
287
|
return null
|
|
204
288
|
|
|
205
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)
|
|
206
359
|
settings = @definition.settings
|
|
207
360
|
if @state != "running" or (@active >= settings.workers and @waiting.length >= settings.maxQueue)
|
|
208
|
-
return
|
|
361
|
+
return Promise/resolve(failure(503, "Unavailable"))
|
|
209
362
|
id = crypto/randomUUID()
|
|
210
363
|
record = {
|
|
211
364
|
id: id
|
|
212
365
|
task: {
|
|
366
|
+
authorize: authorize
|
|
367
|
+
pollResult: pollResult
|
|
213
368
|
body: body
|
|
214
369
|
request: {
|
|
215
370
|
id: id
|
|
216
371
|
method: req.method
|
|
217
372
|
path: req.url
|
|
218
|
-
headers: req.headers
|
|
373
|
+
headers: Object/assign({}, req.headers)
|
|
219
374
|
remoteAddress: req.socket.remoteAddress
|
|
220
375
|
}
|
|
221
376
|
}
|
|
@@ -227,31 +382,28 @@ Server:submit(req, res, body)
|
|
|
227
382
|
deadline: Date/now() + settings.requestTimeoutMs
|
|
228
383
|
timer: null
|
|
229
384
|
}
|
|
385
|
+
if token != null
|
|
386
|
+
record.task.request.headers.authorization = "Bearer ${token}"
|
|
230
387
|
@tasks[id] = record
|
|
231
388
|
resetDeadline(record, settings.requestTimeoutMs)
|
|
232
389
|
@waiting.add(record)
|
|
233
|
-
|
|
390
|
+
disconnected = ()
|
|
234
391
|
capture res
|
|
235
392
|
capture record
|
|
236
393
|
if !res.writableEnded
|
|
237
394
|
cancel(record)
|
|
238
|
-
)
|
|
395
|
+
res.once("close", disconnected)
|
|
239
396
|
pump()
|
|
240
397
|
return chain record.completed.promise
|
|
241
|
-
then(result)
|
|
242
|
-
capture res
|
|
243
|
-
send(res, result)
|
|
244
|
-
return null
|
|
245
398
|
catch(error)
|
|
246
|
-
capture res
|
|
247
399
|
capture record
|
|
248
|
-
code = 500
|
|
249
|
-
name = "InternalServerError"
|
|
250
400
|
if record.expired
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
401
|
+
return failure(504, "RequestTimeout")
|
|
402
|
+
return failure(500, "InternalServerError")
|
|
403
|
+
finally
|
|
404
|
+
capture res
|
|
405
|
+
capture disconnected
|
|
406
|
+
res.removeListener("close", disconnected)
|
|
255
407
|
|
|
256
408
|
Server:expire(record)
|
|
257
409
|
record.expired = true
|
|
@@ -322,6 +474,11 @@ Server:close()
|
|
|
322
474
|
return @closing
|
|
323
475
|
|
|
324
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()
|
|
325
482
|
drained = new Promise((resolve)
|
|
326
483
|
server = @server
|
|
327
484
|
if server == null or !server.listening
|
|
@@ -349,6 +506,7 @@ Server:stop()
|
|
|
349
506
|
clearTimeout(timer)
|
|
350
507
|
return destroyPool()
|
|
351
508
|
finally
|
|
509
|
+
@broker.close()
|
|
352
510
|
@state = "closed"
|
|
353
511
|
|
|
354
512
|
Server:destroyPool()
|
|
@@ -430,3 +588,10 @@ main(args)
|
|
|
430
588
|
process.removeListener("SIGTERM", stop)
|
|
431
589
|
process.removeListener("SIGINT", stop)
|
|
432
590
|
throw error
|
|
591
|
+
|
|
592
|
+
brokerReply(port, message)
|
|
593
|
+
try
|
|
594
|
+
port.postMessage(message)
|
|
595
|
+
catch error
|
|
596
|
+
return null
|
|
597
|
+
return null
|