@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/dist/worker.mjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Runtime } from "./runtime.mjs";
|
|
2
|
+
import * as threads from "node:worker_threads";
|
|
3
|
+
import {
|
|
4
|
+
hostGlobal as __hostGlobal,
|
|
5
|
+
hostGlobalCall as __hostGlobalCall,
|
|
6
|
+
requireBoolean as __requireBoolean,
|
|
7
|
+
normalize as __normalize,
|
|
8
|
+
boundary as __boundary,
|
|
9
|
+
requireArity as __requireArity,
|
|
10
|
+
withArity as __withArity,
|
|
11
|
+
} from "./.lango/runtime.mjs";
|
|
12
|
+
|
|
13
|
+
let runtime = new Runtime(__normalize(threads.workerData));
|
|
14
|
+
|
|
15
|
+
export let execute = initialize();
|
|
16
|
+
function initialize() {
|
|
17
|
+
return __normalize(runtime.preload()).then(__withArity((ignored) => {
|
|
18
|
+
ignored = __boundary(ignored);
|
|
19
|
+
__normalize(threads.parentPort.postMessage({ kind: "ready", threadId: __normalize(threads.threadId) }));
|
|
20
|
+
registerCleanup();
|
|
21
|
+
return workerHandler();
|
|
22
|
+
return null;
|
|
23
|
+
}, [0], "closure"));
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function run(task) {
|
|
28
|
+
__requireArity(arguments.length, [0], "run");
|
|
29
|
+
task = __boundary(task);
|
|
30
|
+
let pending;
|
|
31
|
+
if (__requireBoolean(__normalize(task.close) === true)) {
|
|
32
|
+
__normalize(runtime.close());
|
|
33
|
+
pending = new (__hostGlobal(["Promise"]))(__withArity((resolve) => {
|
|
34
|
+
resolve = __boundary(resolve);
|
|
35
|
+
__hostGlobalCall(["setTimeout"], resolve, 10);
|
|
36
|
+
return null;
|
|
37
|
+
}, [0], "closure"));
|
|
38
|
+
return pending;
|
|
39
|
+
}
|
|
40
|
+
return __normalize(runtime.handle(task));
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function workerHandler() {
|
|
45
|
+
return run;
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function registerCleanup() {
|
|
50
|
+
__normalize(__hostGlobal(["process"]).once("exit", () => {
|
|
51
|
+
__normalize(runtime.close());
|
|
52
|
+
return null;
|
|
53
|
+
}));
|
|
54
|
+
return null;
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=worker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.mjs","sources":["worker.lango"],"sourcesContent":["import js \"node:worker_threads\" as threads\nimport js global Promise\nimport js global process\nimport js global setTimeout\nimport \"./runtime\"\n class Runtime\n\nexport global execute\n\nglobal runtime = new Runtime(threads.workerData)\nglobal execute = initialize()\n\ninitialize()\n global runtime\n return chain runtime.preload()\n then(ignored)\n capture threads\n threads/parentPort/postMessage({\n kind: \"ready\"\n threadId: threads.threadId\n })\n registerCleanup()\n return workerHandler()\n\nrun(task)\n global runtime\n if task.close == true\n runtime.close()\n pending = new Promise((resolve)\n setTimeout(resolve, 10)\n )\n return pending\n return runtime.handle(task)\n\nworkerHandler()\n return run\n\nregisterCleanup()\n process.once(\"exit\", ()\n global runtime\n runtime.close()\n )\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;AASA;;AACA;AAEA;;;;;;;;;;;AAYA;;;;;;;;;;;;;;;;;AAUA;;;;;AAGA;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lango-project/lango-server",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "A small JSON HTTP server for Lango applications.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=24"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./dist/library.mjs"
|
|
15
|
+
},
|
|
16
|
+
"bin": {
|
|
17
|
+
"lango-server": "dist/cli/server.mjs"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist/**",
|
|
21
|
+
"src/*.lango",
|
|
22
|
+
"src/*.lua",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"NOTICE"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "lango run tools/clean.lango && lango build src/library.lango --out dist/library.mjs && lango build src/server.lango --out dist/cli/server.mjs --executable && lango build src/worker.lango --out dist/worker.mjs && lango build examples/application/handlers.lango --out examples/application/dist/handlers.mjs && lango build src/postgres_worker.lango --out dist/postgres_worker.mjs && lango build examples/database/handlers.lango --out examples/database/dist/handlers.mjs",
|
|
29
|
+
"test": "npm run build && lango build src/test/handlers.lango --out .test-output/handlers.mjs && lango build src/test/sql_handlers.lango --out .test-output/sql_handlers.mjs && lango build src/test/middleware_handlers.lango --out .test-output/middleware_handlers.mjs && lango test src/test/database.lango --build-dir .test-output/database && lango test src/test/unit.lango --build-dir .test-output/unit && lango test src/test/http.lango --build-dir .test-output/http && lango test src/test/middleware.lango --build-dir .test-output/middleware && lango test src/test/static.lango --build-dir .test-output/static && lango test src/test/standalone.lango --build-dir .test-output/standalone",
|
|
30
|
+
"start": "node dist/cli/server.mjs examples/application",
|
|
31
|
+
"prepack": "npm run build",
|
|
32
|
+
"test:postgres": "lango build src/test/sql_handlers.lango --out .test-output/sql_handlers.mjs && lango test src/test/database.lango --build-dir .test-output/database-postgres",
|
|
33
|
+
"test:longpoll": "lango build src/test/longpoll_handlers.lango --out .test-output/longpoll_handlers.mjs && lango test src/test/longpoll.lango --build-dir .test-output/longpoll && lango build src/test/middleware_handlers.lango --out .test-output/middleware_handlers.mjs && lango test src/test/standalone.lango --build-dir .test-output/standalone-longpoll",
|
|
34
|
+
"test:middleware": "lango build src/test/handlers.lango --out .test-output/handlers.mjs && lango build src/test/middleware_handlers.lango --out .test-output/middleware_handlers.mjs && lango test src/test/middleware.lango --build-dir .test-output/middleware"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"jsonwebtoken": "^9.0.3",
|
|
38
|
+
"pg": "^8.23.1",
|
|
39
|
+
"piscina": "^5.3.2",
|
|
40
|
+
"redis": "^5.12.1",
|
|
41
|
+
"serve-static": "^2.2.1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@lango-project/lango-js": "^0.1.13"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import js global Buffer
|
|
2
|
+
import js global Number
|
|
3
|
+
import js global setTimeout
|
|
4
|
+
import js global clearTimeout
|
|
5
|
+
|
|
6
|
+
export class BodyReader
|
|
7
|
+
methods
|
|
8
|
+
start()
|
|
9
|
+
|
|
10
|
+
BodyReader(req, res, options, complete, failure)
|
|
11
|
+
@req = req
|
|
12
|
+
@res = res
|
|
13
|
+
@options = options
|
|
14
|
+
@complete = complete
|
|
15
|
+
@failure = failure
|
|
16
|
+
@chunks = []
|
|
17
|
+
@size = 0
|
|
18
|
+
@done = false
|
|
19
|
+
@timer = null
|
|
20
|
+
data = (chunk)
|
|
21
|
+
accept(chunk)
|
|
22
|
+
ended = ()
|
|
23
|
+
finish()
|
|
24
|
+
failed = (error)
|
|
25
|
+
reject(400, "BadRequest")
|
|
26
|
+
aborted = ()
|
|
27
|
+
abandon()
|
|
28
|
+
closed = ()
|
|
29
|
+
abandon()
|
|
30
|
+
@data = data
|
|
31
|
+
@ended = ended
|
|
32
|
+
@failed = failed
|
|
33
|
+
@aborted = aborted
|
|
34
|
+
@closed = closed
|
|
35
|
+
|
|
36
|
+
BodyReader:start()
|
|
37
|
+
req = @req
|
|
38
|
+
res = @res
|
|
39
|
+
if req.aborted or res.destroyed
|
|
40
|
+
abandon()
|
|
41
|
+
return null
|
|
42
|
+
req.on("error", @failed)
|
|
43
|
+
req.on("aborted", @aborted)
|
|
44
|
+
res.on("close", @closed)
|
|
45
|
+
length = req.headers["content-length"]
|
|
46
|
+
options = @options
|
|
47
|
+
if is_string(length) and Number/parseInt(length, 10) > options.bodyLimit
|
|
48
|
+
reject(413, "RequestTooLarge")
|
|
49
|
+
return null
|
|
50
|
+
expired = ()
|
|
51
|
+
reject(408, "RequestTimeout")
|
|
52
|
+
@timer = setTimeout(expired, options.bodyTimeoutMs)
|
|
53
|
+
req.on("data", @data)
|
|
54
|
+
req.on("end", @ended)
|
|
55
|
+
return null
|
|
56
|
+
|
|
57
|
+
BodyReader:accept(chunk)
|
|
58
|
+
if @done
|
|
59
|
+
return null
|
|
60
|
+
@size = @size + chunk.length
|
|
61
|
+
options = @options
|
|
62
|
+
if @size > options.bodyLimit
|
|
63
|
+
reject(413, "RequestTooLarge")
|
|
64
|
+
else
|
|
65
|
+
@chunks.add(chunk)
|
|
66
|
+
return null
|
|
67
|
+
|
|
68
|
+
BodyReader:finish()
|
|
69
|
+
if @done
|
|
70
|
+
return null
|
|
71
|
+
@done = true
|
|
72
|
+
body = Buffer/concat(@chunks).toString("utf8")
|
|
73
|
+
cleanup()
|
|
74
|
+
@chunks = []
|
|
75
|
+
callback = @complete
|
|
76
|
+
callback(body)
|
|
77
|
+
return null
|
|
78
|
+
|
|
79
|
+
BodyReader:abandon()
|
|
80
|
+
if @done
|
|
81
|
+
return null
|
|
82
|
+
@done = true
|
|
83
|
+
cleanup()
|
|
84
|
+
@chunks = []
|
|
85
|
+
return null
|
|
86
|
+
|
|
87
|
+
BodyReader:reject(status, error)
|
|
88
|
+
if @done
|
|
89
|
+
return null
|
|
90
|
+
@done = true
|
|
91
|
+
cleanup()
|
|
92
|
+
@chunks = []
|
|
93
|
+
req = @req
|
|
94
|
+
res = @res
|
|
95
|
+
req.pause()
|
|
96
|
+
if !res.writableEnded and !res.destroyed
|
|
97
|
+
res.setHeader("Connection", "close")
|
|
98
|
+
res.once("finish", ()
|
|
99
|
+
capture req
|
|
100
|
+
req.destroy()
|
|
101
|
+
)
|
|
102
|
+
callback = @failure
|
|
103
|
+
callback(status, error)
|
|
104
|
+
return null
|
|
105
|
+
|
|
106
|
+
BodyReader:cleanup()
|
|
107
|
+
clearTimeout(@timer)
|
|
108
|
+
req = @req
|
|
109
|
+
req.removeListener("data", @data)
|
|
110
|
+
req.removeListener("end", @ended)
|
|
111
|
+
req.removeListener("aborted", @aborted)
|
|
112
|
+
@res.removeListener("close", @closed)
|
|
113
|
+
return null
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import js "node:path" as path
|
|
2
|
+
import js global process
|
|
3
|
+
import "./routes"
|
|
4
|
+
class Routes
|
|
5
|
+
import "./group"
|
|
6
|
+
class Group
|
|
7
|
+
import "./options"
|
|
8
|
+
options(root, supplied = null)
|
|
9
|
+
import "./server"
|
|
10
|
+
class Server
|
|
11
|
+
|
|
12
|
+
export class Builder(root = null)
|
|
13
|
+
methods
|
|
14
|
+
configure(settings)
|
|
15
|
+
addMiddleware(name, spec)
|
|
16
|
+
endpoint(spec)
|
|
17
|
+
longpoll(spec)
|
|
18
|
+
group(spec)
|
|
19
|
+
include(spec)
|
|
20
|
+
event(spec)
|
|
21
|
+
build()
|
|
22
|
+
|
|
23
|
+
Builder(root = null)
|
|
24
|
+
if root == null
|
|
25
|
+
root = process.cwd()
|
|
26
|
+
@root = path/resolve(root)
|
|
27
|
+
@settings = {}
|
|
28
|
+
@routes = new Routes(@root)
|
|
29
|
+
|
|
30
|
+
Builder:configure(settings)
|
|
31
|
+
for settings as key: value
|
|
32
|
+
@settings[key] = value
|
|
33
|
+
return @this
|
|
34
|
+
|
|
35
|
+
Builder:addMiddleware(name, spec)
|
|
36
|
+
@routes.addMiddleware(name, spec)
|
|
37
|
+
return @this
|
|
38
|
+
|
|
39
|
+
Builder:endpoint(spec)
|
|
40
|
+
@routes.endpoint(spec)
|
|
41
|
+
return @this
|
|
42
|
+
|
|
43
|
+
Builder:longpoll(spec)
|
|
44
|
+
@routes.longpoll(spec)
|
|
45
|
+
return @this
|
|
46
|
+
|
|
47
|
+
Builder:group(spec)
|
|
48
|
+
return new Group(@routes, spec)
|
|
49
|
+
|
|
50
|
+
Builder:include(spec)
|
|
51
|
+
@routes.include(spec)
|
|
52
|
+
return @this
|
|
53
|
+
|
|
54
|
+
Builder:event(spec)
|
|
55
|
+
@routes.event(spec)
|
|
56
|
+
return @this
|
|
57
|
+
|
|
58
|
+
Builder:build()
|
|
59
|
+
return new Server(null, {
|
|
60
|
+
settings: options(@root, @settings)
|
|
61
|
+
routes: @routes.definitions()
|
|
62
|
+
events: @routes.events()
|
|
63
|
+
middlewares: @routes.middlewares()
|
|
64
|
+
})
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import js global Promise
|
|
2
|
+
import "./database_scope"
|
|
3
|
+
class DatabaseScope
|
|
4
|
+
import "./sqlite_adapter"
|
|
5
|
+
class SqliteAdapter
|
|
6
|
+
import "./postgres_adapter"
|
|
7
|
+
class PostgresAdapter
|
|
8
|
+
|
|
9
|
+
export prepareDatabase(settings)
|
|
10
|
+
|
|
11
|
+
export class Database(settings, root = null)
|
|
12
|
+
methods
|
|
13
|
+
open()
|
|
14
|
+
scope()
|
|
15
|
+
close()
|
|
16
|
+
|
|
17
|
+
Database(settings, root = null)
|
|
18
|
+
@root = root
|
|
19
|
+
@settings = settings
|
|
20
|
+
@adapter = null
|
|
21
|
+
|
|
22
|
+
Database:open()
|
|
23
|
+
if @settings == null
|
|
24
|
+
return Promise/resolve(null)
|
|
25
|
+
name = "node:sqlite"
|
|
26
|
+
if @settings.type == "postgresql"
|
|
27
|
+
name = "node:worker_threads"
|
|
28
|
+
return chain import_module(name)
|
|
29
|
+
then(mod)
|
|
30
|
+
if @settings.type == "sqlite"
|
|
31
|
+
@adapter = new SqliteAdapter(mod, @settings)
|
|
32
|
+
else
|
|
33
|
+
@adapter = new PostgresAdapter(mod, @settings, @root)
|
|
34
|
+
context db = scope()
|
|
35
|
+
try
|
|
36
|
+
query = SQL
|
|
37
|
+
select 1
|
|
38
|
+
query.execute()
|
|
39
|
+
finally
|
|
40
|
+
db.finish()
|
|
41
|
+
return null
|
|
42
|
+
catch(error)
|
|
43
|
+
context log
|
|
44
|
+
log.err("Database startup failed: ${error}")
|
|
45
|
+
close()
|
|
46
|
+
throw "Database startup failed; check configuration, compiler version and driver installation"
|
|
47
|
+
|
|
48
|
+
Database:scope()
|
|
49
|
+
if @adapter == null
|
|
50
|
+
return null
|
|
51
|
+
return new DatabaseScope(@adapter)
|
|
52
|
+
|
|
53
|
+
Database:close()
|
|
54
|
+
if @adapter != null
|
|
55
|
+
@adapter.close()
|
|
56
|
+
@adapter = null
|
|
57
|
+
return null
|
|
58
|
+
|
|
59
|
+
prepareDatabase(settings)
|
|
60
|
+
if settings == null or settings.type != "sqlite" or settings.file == ":memory:"
|
|
61
|
+
return Promise/resolve(null)
|
|
62
|
+
database = new Database(settings)
|
|
63
|
+
return chain database.open()
|
|
64
|
+
finally
|
|
65
|
+
capture database
|
|
66
|
+
database.close()
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import js "node:path" as path
|
|
2
|
+
import js global Object
|
|
3
|
+
import js global Number
|
|
4
|
+
import js global process
|
|
5
|
+
|
|
6
|
+
export databaseOptions(root, spec, workers)
|
|
7
|
+
|
|
8
|
+
databaseOptions(root, spec, workers)
|
|
9
|
+
if spec == null
|
|
10
|
+
return null
|
|
11
|
+
if !is_object(spec) or is_array(spec)
|
|
12
|
+
throw "Database configuration must be a map"
|
|
13
|
+
allowed = []
|
|
14
|
+
switch spec.type
|
|
15
|
+
case "sqlite"
|
|
16
|
+
allowed = ["type", "file", "busyTimeoutMs"]
|
|
17
|
+
case "postgresql"
|
|
18
|
+
allowed = ["type", "connectionEnv", "queryTimeoutMs", "connectTimeoutSeconds"]
|
|
19
|
+
else
|
|
20
|
+
throw "Database type must be sqlite or postgresql"
|
|
21
|
+
for spec as key: value
|
|
22
|
+
if !allowed.includes(key)
|
|
23
|
+
throw "Unknown database option: ${key}"
|
|
24
|
+
if spec.type == "sqlite"
|
|
25
|
+
if !is_string(spec.file) or spec.file == ""
|
|
26
|
+
throw "SQLite database file is required"
|
|
27
|
+
file = spec.file
|
|
28
|
+
if file == ":memory:" and workers != 1
|
|
29
|
+
throw "An in-memory SQLite database requires exactly one worker"
|
|
30
|
+
if file != ":memory:"
|
|
31
|
+
file = path/resolve(root, file)
|
|
32
|
+
return {
|
|
33
|
+
type: "sqlite"
|
|
34
|
+
file: file
|
|
35
|
+
busyTimeoutMs: positive(spec.busyTimeoutMs, 5000)
|
|
36
|
+
}
|
|
37
|
+
name = spec.connectionEnv
|
|
38
|
+
if !is_string(name) or name == "" or !Object/hasOwn(process.env, name)
|
|
39
|
+
throw "PostgreSQL connectionEnv must name an existing environment variable"
|
|
40
|
+
connection = process.env[name]
|
|
41
|
+
if !is_string(connection) or (!connection.startsWith("postgresql://") and !connection.startsWith("postgres://"))
|
|
42
|
+
throw "PostgreSQL connectionEnv must contain a PostgreSQL connection URL"
|
|
43
|
+
return {
|
|
44
|
+
type: "postgresql"
|
|
45
|
+
connection: connection
|
|
46
|
+
queryTimeoutMs: positive(spec.queryTimeoutMs, 5000)
|
|
47
|
+
connectTimeoutSeconds: positive(spec.connectTimeoutSeconds, 5)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
positive(value, fallback)
|
|
51
|
+
if value == null
|
|
52
|
+
return fallback
|
|
53
|
+
if is_string(value)
|
|
54
|
+
value = Number(value)
|
|
55
|
+
if !Number/isSafeInteger(value) or value < 1 or value > 2147483647
|
|
56
|
+
throw "Database timeout must be a positive integer within the timer limit"
|
|
57
|
+
return value
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import js global Error
|
|
2
|
+
import js global Object
|
|
3
|
+
import js global process
|
|
4
|
+
|
|
5
|
+
export class DatabaseScope(adapter)
|
|
6
|
+
properties
|
|
7
|
+
parameterStyle
|
|
8
|
+
methods
|
|
9
|
+
execute(sql, parameters)
|
|
10
|
+
begin()
|
|
11
|
+
commit()
|
|
12
|
+
rollback()
|
|
13
|
+
transactionState()
|
|
14
|
+
finish()
|
|
15
|
+
|
|
16
|
+
DatabaseScope(adapter)
|
|
17
|
+
@adapter = adapter
|
|
18
|
+
@closed = false
|
|
19
|
+
@parameterStyle = adapter.parameterStyle
|
|
20
|
+
|
|
21
|
+
DatabaseScope:execute(sql, parameters)
|
|
22
|
+
requireOpen()
|
|
23
|
+
return @adapter.execute(sql, parameters)
|
|
24
|
+
|
|
25
|
+
DatabaseScope:begin()
|
|
26
|
+
requireOpen()
|
|
27
|
+
return @adapter.begin()
|
|
28
|
+
|
|
29
|
+
DatabaseScope:commit()
|
|
30
|
+
requireOpen()
|
|
31
|
+
return @adapter.commit()
|
|
32
|
+
|
|
33
|
+
DatabaseScope:rollback()
|
|
34
|
+
requireOpen()
|
|
35
|
+
return @adapter.rollback()
|
|
36
|
+
|
|
37
|
+
DatabaseScope:transactionState()
|
|
38
|
+
requireOpen()
|
|
39
|
+
return @adapter.transactionState()
|
|
40
|
+
|
|
41
|
+
DatabaseScope:requireOpen()
|
|
42
|
+
if @closed
|
|
43
|
+
throw new Error("DatabaseScopeClosed")
|
|
44
|
+
return null
|
|
45
|
+
|
|
46
|
+
DatabaseScope:finish()
|
|
47
|
+
if @closed
|
|
48
|
+
return false
|
|
49
|
+
@closed = true
|
|
50
|
+
dirty = false
|
|
51
|
+
try
|
|
52
|
+
loop while @adapter.transactionState() != null
|
|
53
|
+
dirty = true
|
|
54
|
+
@adapter.rollback()
|
|
55
|
+
catch error
|
|
56
|
+
context log
|
|
57
|
+
log.err("Database cleanup failed; retiring worker")
|
|
58
|
+
process.exit(1)
|
|
59
|
+
return dirty
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import "./middleware"
|
|
2
|
+
middlewareName(name)
|
|
3
|
+
|
|
4
|
+
export class Endpoint(spec)
|
|
5
|
+
properties
|
|
6
|
+
path
|
|
7
|
+
module
|
|
8
|
+
handler
|
|
9
|
+
auth
|
|
10
|
+
middleware
|
|
11
|
+
|
|
12
|
+
Endpoint(spec)
|
|
13
|
+
if !is_string(spec.path) or !spec.path.startsWith("/") or spec.path.includes("?") or spec.path.includes("#") or spec.path.includes("\\") or spec.path.includes("//")
|
|
14
|
+
throw "Endpoint requires an absolute URL path"
|
|
15
|
+
if spec.path.includes("*") and (!spec.path.endsWith("/*") or spec.path.indexOf("*") != spec.path.lastIndexOf("*"))
|
|
16
|
+
throw "Only trailing /* wildcards are supported"
|
|
17
|
+
for ["module", "handler"] as key
|
|
18
|
+
if !is_string(spec[key]) or spec[key] == ""
|
|
19
|
+
throw "Endpoint requires ${key}"
|
|
20
|
+
@path = spec.path
|
|
21
|
+
@module = spec.module
|
|
22
|
+
@handler = spec.handler
|
|
23
|
+
@middleware = spec.middleware
|
|
24
|
+
if @middleware != null
|
|
25
|
+
middlewareName(@middleware)
|
|
26
|
+
@auth = spec.auth
|
|
27
|
+
if @auth == null
|
|
28
|
+
@auth = "login"
|
|
29
|
+
if !is_string(@auth) or @auth == "" or @auth.includes(",")
|
|
30
|
+
throw "Endpoint auth must name one permission"
|
package/src/group.lango
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import js global Object
|
|
2
|
+
|
|
3
|
+
export class Group(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null)
|
|
4
|
+
methods
|
|
5
|
+
endpoint(spec)
|
|
6
|
+
longpoll(spec)
|
|
7
|
+
group(spec)
|
|
8
|
+
include(spec)
|
|
9
|
+
|
|
10
|
+
Group(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null)
|
|
11
|
+
if !is_string(spec.path) or !spec.path.startsWith("/") or spec.path.endsWith("/") or spec.path.includes("*")
|
|
12
|
+
throw "Group requires a path without a trailing slash or wildcard"
|
|
13
|
+
@routes = routes
|
|
14
|
+
@prefix = "${prefix}${spec.path}"
|
|
15
|
+
@module = spec.module
|
|
16
|
+
@auth = spec.auth
|
|
17
|
+
if @module == null
|
|
18
|
+
@module = inheritedModule
|
|
19
|
+
if @auth == null
|
|
20
|
+
@auth = inheritedAuth
|
|
21
|
+
|
|
22
|
+
Group:endpoint(spec)
|
|
23
|
+
if !is_string(spec.path) or !spec.path.startsWith("/")
|
|
24
|
+
throw "Endpoint path must begin with /"
|
|
25
|
+
mod = spec.module
|
|
26
|
+
auth = spec.auth
|
|
27
|
+
if mod == null
|
|
28
|
+
mod = @module
|
|
29
|
+
if auth == null
|
|
30
|
+
auth = @auth
|
|
31
|
+
@routes.endpoint({
|
|
32
|
+
path: "${@prefix}${spec.path}"
|
|
33
|
+
module: mod
|
|
34
|
+
handler: spec.handler
|
|
35
|
+
auth: auth
|
|
36
|
+
middleware: spec.middleware
|
|
37
|
+
})
|
|
38
|
+
return @this
|
|
39
|
+
|
|
40
|
+
Group:longpoll(spec)
|
|
41
|
+
item = Object/assign({}, spec)
|
|
42
|
+
item.path = "${@prefix}${spec.path}"
|
|
43
|
+
if item.auth == null
|
|
44
|
+
item.auth = @auth
|
|
45
|
+
if item.fallback != null
|
|
46
|
+
item.fallback = "${@prefix}${item.fallback}"
|
|
47
|
+
@routes.longpoll(item)
|
|
48
|
+
return @this
|
|
49
|
+
|
|
50
|
+
Group:group(spec)
|
|
51
|
+
return new Group(@routes, spec, @prefix, @module, @auth)
|
|
52
|
+
|
|
53
|
+
Group:include(spec)
|
|
54
|
+
mount = spec.path
|
|
55
|
+
if mount == null
|
|
56
|
+
mount = "/"
|
|
57
|
+
@routes.include({
|
|
58
|
+
module: spec.module
|
|
59
|
+
path: "${@prefix}${mount}"
|
|
60
|
+
}, @auth)
|
|
61
|
+
return @this
|
package/src/longpoll.lua
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
-- Atomic channel generation, trimming, publication and cursor validation.
|
|
2
|
+
local stream, meta = KEYS[1], KEYS[2]
|
|
3
|
+
local operation, candidate = ARGV[1], ARGV[2]
|
|
4
|
+
local retention, maximum = tonumber(ARGV[3]), tonumber(ARGV[4])
|
|
5
|
+
local now = redis.call('TIME')
|
|
6
|
+
local milliseconds = tonumber(now[1]) * 1000 + math.floor(tonumber(now[2]) / 1000)
|
|
7
|
+
local generation = redis.call('HGET', meta, 'generation')
|
|
8
|
+
if not generation then
|
|
9
|
+
redis.call('DEL', stream)
|
|
10
|
+
generation = candidate
|
|
11
|
+
redis.call('HSET', meta, 'generation', generation, 'floor', '0-0', 'last', '0-0')
|
|
12
|
+
redis.call('PEXPIRE', meta, retention)
|
|
13
|
+
end
|
|
14
|
+
local function compare(a, b)
|
|
15
|
+
local am, as = string.match(a, '^(%d+)%-(%d+)$')
|
|
16
|
+
local bm, bs = string.match(b, '^(%d+)%-(%d+)$')
|
|
17
|
+
if #am ~= #bm then return #am < #bm end
|
|
18
|
+
if am ~= bm then return am < bm end
|
|
19
|
+
if #as ~= #bs then return #as < #bs end
|
|
20
|
+
return as < bs
|
|
21
|
+
end
|
|
22
|
+
local floor = redis.call('HGET', meta, 'floor')
|
|
23
|
+
local cutoff = tostring(math.max(0, milliseconds - retention)) .. '-0'
|
|
24
|
+
local removed = redis.call('XREVRANGE', stream, '(' .. cutoff, '-', 'COUNT', 1)
|
|
25
|
+
if #removed > 0 and compare(floor, removed[1][1]) then floor = removed[1][1] end
|
|
26
|
+
redis.call('XTRIM', stream, 'MINID', cutoff)
|
|
27
|
+
if operation == 'publish' then
|
|
28
|
+
local id = redis.call('XADD', stream, '*', 'data', ARGV[7])
|
|
29
|
+
redis.call('HSET', meta, 'last', id)
|
|
30
|
+
local excess = redis.call('XLEN', stream) - maximum
|
|
31
|
+
if excess > 0 then
|
|
32
|
+
local old = redis.call('XRANGE', stream, '-', '+', 'COUNT', excess)
|
|
33
|
+
floor = old[#old][1]
|
|
34
|
+
redis.call('XTRIM', stream, 'MAXLEN', maximum)
|
|
35
|
+
end
|
|
36
|
+
redis.call('HSET', meta, 'floor', floor)
|
|
37
|
+
redis.call('PEXPIRE', stream, retention)
|
|
38
|
+
redis.call('PEXPIRE', meta, retention)
|
|
39
|
+
return cjson.encode({generation=generation, id=id})
|
|
40
|
+
end
|
|
41
|
+
redis.call('HSET', meta, 'floor', floor)
|
|
42
|
+
local last = redis.call('HGET', meta, 'last')
|
|
43
|
+
if operation == 'cursor' then
|
|
44
|
+
return cjson.encode({generation=generation, id=last})
|
|
45
|
+
end
|
|
46
|
+
local epoch, position = ARGV[5], ARGV[6]
|
|
47
|
+
if epoch ~= '' and (epoch ~= generation or compare(position, floor) or compare(last, position)) then
|
|
48
|
+
return cjson.encode({resyncRequired=true})
|
|
49
|
+
end
|
|
50
|
+
local entries = redis.call('XRANGE', stream, '(' .. position, '+', 'COUNT', 16)
|
|
51
|
+
local messages = {}
|
|
52
|
+
local bytes = 0
|
|
53
|
+
local id = position
|
|
54
|
+
for _, entry in ipairs(entries) do
|
|
55
|
+
local data = entry[2][2]
|
|
56
|
+
if bytes + string.len(data) > 1048576 then break end
|
|
57
|
+
bytes = bytes + string.len(data)
|
|
58
|
+
id = entry[1]
|
|
59
|
+
table.insert(messages, {id=id, data=data})
|
|
60
|
+
end
|
|
61
|
+
return cjson.encode({generation=generation, id=id, messages=messages})
|