@lango-project/lango-server 0.2.4 → 0.2.5
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 +8 -0
- package/dist/body_reader.mjs.map +1 -1
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/server.mjs +3 -838
- package/dist/cli/server.mjs.map +1 -1
- package/dist/client_identity.mjs.map +1 -1
- package/dist/database.mjs.map +1 -1
- package/dist/database_action.mjs.map +1 -1
- package/dist/database_options.mjs.map +1 -1
- package/dist/database_scope.mjs.map +1 -1
- package/dist/endpoint.mjs.map +1 -1
- package/dist/error_details.mjs.map +1 -1
- package/dist/execution_failure.mjs.map +1 -1
- package/dist/execution_identity.mjs.map +1 -1
- package/dist/execution_log.mjs.map +1 -1
- package/dist/execution_runtime.mjs.map +1 -1
- package/dist/execution_worker.mjs.map +1 -1
- package/dist/executor.mjs.map +1 -1
- package/dist/group.mjs.map +1 -1
- package/dist/http_failure.mjs.map +1 -1
- package/dist/job_configuration.mjs.map +1 -1
- package/dist/job_context.mjs.map +1 -1
- package/dist/library.mjs.map +1 -1
- package/dist/longpoll_broker.mjs.map +1 -1
- package/dist/longpoll_options.mjs.map +1 -1
- package/dist/longpoll_session.mjs.map +1 -1
- package/dist/middleware.mjs.map +1 -1
- package/dist/module_loader.mjs.map +1 -1
- package/dist/options.mjs.map +1 -1
- package/dist/origin.mjs.map +1 -1
- package/dist/policy.mjs.map +1 -1
- package/dist/postgres_adapter.mjs.map +1 -1
- package/dist/postgres_worker.mjs.map +1 -1
- package/dist/queue_provider.mjs.map +1 -1
- package/dist/request_identity.mjs.map +1 -1
- package/dist/response.mjs.map +1 -1
- package/dist/route_info.mjs.map +1 -1
- package/dist/route_pattern.mjs.map +1 -1
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs.map +1 -1
- package/dist/server_context.mjs.map +1 -1
- package/dist/sqlite_adapter.mjs.map +1 -1
- package/dist/static_files.mjs.map +1 -1
- package/dist/token.mjs.map +1 -1
- package/dist/tokens.mjs.map +1 -1
- package/dist/trace.mjs.map +1 -1
- package/dist/worker.mjs.map +1 -1
- package/examples/security/package.json +1 -1
- package/package.json +6 -4
- package/dist/cli/.lango/bignumber.mjs +0 -3119
- package/dist/cli/.lango/localization.mjs +0 -108
- package/dist/cli/.lango/runtime.mjs +0 -725
- package/dist/cli/.lango/vendor/i18next.LICENSE +0 -21
- package/dist/cli/.lango/vendor/i18next.mjs +0 -2434
- package/dist/cli/.lango/xmldom/conventions.js +0 -429
- package/dist/cli/.lango/xmldom/dom-parser.js +0 -591
- package/dist/cli/.lango/xmldom/dom.js +0 -3759
- package/dist/cli/.lango/xmldom/entities.js +0 -2171
- package/dist/cli/.lango/xmldom/errors.js +0 -205
- package/dist/cli/.lango/xmldom/grammar.js +0 -559
- package/dist/cli/.lango/xmldom/index.js +0 -41
- package/dist/cli/.lango/xmldom/package.json +0 -1
- package/dist/cli/.lango/xmldom/sax.js +0 -983
- package/dist/cli/.lango/xmldom.LICENSE.md +0 -8
- package/dist/cli/body_reader.mjs +0 -180
- package/dist/cli/body_reader.mjs.map +0 -1
- package/dist/cli/client_identity.mjs +0 -250
- package/dist/cli/client_identity.mjs.map +0 -1
- package/dist/cli/database.mjs +0 -116
- package/dist/cli/database.mjs.map +0 -1
- package/dist/cli/database_options.mjs +0 -83
- package/dist/cli/database_options.mjs.map +0 -1
- package/dist/cli/database_scope.mjs +0 -93
- package/dist/cli/database_scope.mjs.map +0 -1
- package/dist/cli/endpoint.mjs +0 -50
- package/dist/cli/endpoint.mjs.map +0 -1
- package/dist/cli/error_details.mjs +0 -45
- package/dist/cli/error_details.mjs.map +0 -1
- package/dist/cli/longpoll_broker.mjs +0 -215
- package/dist/cli/longpoll_broker.mjs.map +0 -1
- package/dist/cli/longpoll_options.mjs +0 -103
- package/dist/cli/longpoll_options.mjs.map +0 -1
- package/dist/cli/longpoll_session.mjs +0 -136
- package/dist/cli/longpoll_session.mjs.map +0 -1
- package/dist/cli/middleware.mjs +0 -25
- package/dist/cli/middleware.mjs.map +0 -1
- package/dist/cli/options.mjs +0 -214
- package/dist/cli/options.mjs.map +0 -1
- package/dist/cli/origin.mjs +0 -87
- package/dist/cli/origin.mjs.map +0 -1
- package/dist/cli/policy.mjs +0 -121
- package/dist/cli/policy.mjs.map +0 -1
- package/dist/cli/postgres_adapter.mjs +0 -178
- package/dist/cli/postgres_adapter.mjs.map +0 -1
- package/dist/cli/route_pattern.mjs +0 -129
- package/dist/cli/route_pattern.mjs.map +0 -1
- package/dist/cli/routes.mjs +0 -360
- package/dist/cli/routes.mjs.map +0 -1
- package/dist/cli/sqlite_adapter.mjs +0 -163
- package/dist/cli/sqlite_adapter.mjs.map +0 -1
- package/dist/cli/static_files.mjs +0 -224
- package/dist/cli/static_files.mjs.map +0 -1
- package/dist/cli/trace.mjs +0 -53
- package/dist/cli/trace.mjs.map +0 -1
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
Copyright 2019 - present Christopher J. Brody and other contributors, as listed in: https://github.com/xmldom/xmldom/graphs/contributors
|
|
2
|
-
Copyright 2012 - 2017 @jindw <jindw@xidea.org> and other contributors, as listed in: https://github.com/jindw/xmldom/graphs/contributors
|
|
3
|
-
|
|
4
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
-
|
|
6
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
-
|
|
8
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/cli/body_reader.mjs
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
hostGlobal as __hostGlobal,
|
|
3
|
-
hostGlobalCall as __hostGlobalCall,
|
|
4
|
-
bindContexts as __bindContexts,
|
|
5
|
-
isNumber as __isNumber,
|
|
6
|
-
isString as __isString,
|
|
7
|
-
isBool as __isBool,
|
|
8
|
-
isObject as __isObject,
|
|
9
|
-
isArray as __isArray,
|
|
10
|
-
add as __add,
|
|
11
|
-
requireBoolean as __requireBoolean,
|
|
12
|
-
normalize as __normalize,
|
|
13
|
-
numericAdd as __numericAdd,
|
|
14
|
-
call as __call,
|
|
15
|
-
boundary as __boundary,
|
|
16
|
-
requireArity as __requireArity,
|
|
17
|
-
withArity as __withArity,
|
|
18
|
-
} from "./.lango/runtime.mjs";
|
|
19
|
-
|
|
20
|
-
export class BodyReader {
|
|
21
|
-
#req;
|
|
22
|
-
#res;
|
|
23
|
-
#options;
|
|
24
|
-
#complete;
|
|
25
|
-
#failure;
|
|
26
|
-
#chunks;
|
|
27
|
-
#size;
|
|
28
|
-
#done;
|
|
29
|
-
#timer;
|
|
30
|
-
#data;
|
|
31
|
-
#ended;
|
|
32
|
-
#failed;
|
|
33
|
-
#aborted;
|
|
34
|
-
#closed;
|
|
35
|
-
constructor(req, res, options, complete, failure) {
|
|
36
|
-
__requireArity(arguments.length, [0, 1, 2, 3, 4], "constructor");
|
|
37
|
-
req = __boundary(req);
|
|
38
|
-
res = __boundary(res);
|
|
39
|
-
options = __boundary(options);
|
|
40
|
-
complete = __boundary(complete);
|
|
41
|
-
failure = __boundary(failure);
|
|
42
|
-
this.#req = req;
|
|
43
|
-
this.#res = res;
|
|
44
|
-
this.#options = options;
|
|
45
|
-
this.#complete = complete;
|
|
46
|
-
this.#failure = failure;
|
|
47
|
-
this.#chunks = [];
|
|
48
|
-
this.#size = 0;
|
|
49
|
-
this.#done = false;
|
|
50
|
-
this.#timer = null;
|
|
51
|
-
let data = __withArity(__bindContexts((chunk) => {
|
|
52
|
-
chunk = __boundary(chunk);
|
|
53
|
-
this.#accept(chunk);
|
|
54
|
-
return null;
|
|
55
|
-
}), [0], "closure");
|
|
56
|
-
let ended = __bindContexts(() => {
|
|
57
|
-
this.#finish();
|
|
58
|
-
return null;
|
|
59
|
-
});
|
|
60
|
-
let failed = __withArity(__bindContexts((error) => {
|
|
61
|
-
error = __boundary(error);
|
|
62
|
-
this.#reject(400, "BadRequest");
|
|
63
|
-
return null;
|
|
64
|
-
}), [0], "closure");
|
|
65
|
-
let aborted = __bindContexts(() => {
|
|
66
|
-
this.#abandon();
|
|
67
|
-
return null;
|
|
68
|
-
});
|
|
69
|
-
let closed = __bindContexts(() => {
|
|
70
|
-
this.#abandon();
|
|
71
|
-
return null;
|
|
72
|
-
});
|
|
73
|
-
this.#data = data;
|
|
74
|
-
this.#ended = ended;
|
|
75
|
-
this.#failed = failed;
|
|
76
|
-
this.#aborted = aborted;
|
|
77
|
-
this.#closed = closed;
|
|
78
|
-
}
|
|
79
|
-
start() {
|
|
80
|
-
let req = __normalize(this.#req);
|
|
81
|
-
let res = __normalize(this.#res);
|
|
82
|
-
if (__requireBoolean(__requireBoolean(__normalize(req.aborted)) || __requireBoolean(__normalize(res.destroyed)))) {
|
|
83
|
-
this.#abandon();
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
__normalize(req.on("error", __normalize(this.#failed)));
|
|
87
|
-
__normalize(req.on("aborted", __normalize(this.#aborted)));
|
|
88
|
-
__normalize(res.on("close", __normalize(this.#closed)));
|
|
89
|
-
let length = req.headers["content-length"];
|
|
90
|
-
let options = __normalize(this.#options);
|
|
91
|
-
if (__requireBoolean(__requireBoolean(__isString(length)) && __requireBoolean(__normalize(__hostGlobal(["Number"]).parseInt(length, 10)) > __normalize(options.bodyLimit)))) {
|
|
92
|
-
this.#reject(413, "RequestTooLarge");
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
let expired = __bindContexts(() => {
|
|
96
|
-
this.#reject(408, "RequestTimeout");
|
|
97
|
-
return null;
|
|
98
|
-
});
|
|
99
|
-
this.#timer = __hostGlobalCall(["setTimeout"], expired, __normalize(options.bodyTimeoutMs));
|
|
100
|
-
__normalize(req.on("data", __normalize(this.#data)));
|
|
101
|
-
__normalize(req.on("end", __normalize(this.#ended)));
|
|
102
|
-
return null;
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
#accept(chunk) {
|
|
106
|
-
__requireArity(arguments.length, [0], "accept");
|
|
107
|
-
chunk = __boundary(chunk);
|
|
108
|
-
if (__requireBoolean(__normalize(this.#done))) {
|
|
109
|
-
return null;
|
|
110
|
-
}
|
|
111
|
-
this.#size = __numericAdd(__normalize(this.#size), __normalize(chunk.length));
|
|
112
|
-
let options = __normalize(this.#options);
|
|
113
|
-
if (__requireBoolean(__normalize(this.#size) > __normalize(options.bodyLimit))) {
|
|
114
|
-
this.#reject(413, "RequestTooLarge");
|
|
115
|
-
} else {
|
|
116
|
-
__add(this.#chunks, chunk);
|
|
117
|
-
}
|
|
118
|
-
return null;
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
121
|
-
#finish() {
|
|
122
|
-
if (__requireBoolean(__normalize(this.#done))) {
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
this.#done = true;
|
|
126
|
-
let body = __normalize(__normalize(__hostGlobal(["Buffer"]).concat(__normalize(this.#chunks))).toString("utf8"));
|
|
127
|
-
this.#cleanup();
|
|
128
|
-
this.#chunks = [];
|
|
129
|
-
let callback = __normalize(this.#complete);
|
|
130
|
-
__call(callback, body);
|
|
131
|
-
return null;
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
#abandon() {
|
|
135
|
-
if (__requireBoolean(__normalize(this.#done))) {
|
|
136
|
-
return null;
|
|
137
|
-
}
|
|
138
|
-
this.#done = true;
|
|
139
|
-
this.#cleanup();
|
|
140
|
-
this.#chunks = [];
|
|
141
|
-
return null;
|
|
142
|
-
return null;
|
|
143
|
-
}
|
|
144
|
-
#reject(status, error) {
|
|
145
|
-
__requireArity(arguments.length, [0, 1], "reject");
|
|
146
|
-
status = __boundary(status);
|
|
147
|
-
error = __boundary(error);
|
|
148
|
-
if (__requireBoolean(__normalize(this.#done))) {
|
|
149
|
-
return null;
|
|
150
|
-
}
|
|
151
|
-
this.#done = true;
|
|
152
|
-
this.#cleanup();
|
|
153
|
-
this.#chunks = [];
|
|
154
|
-
let req = __normalize(this.#req);
|
|
155
|
-
let res = __normalize(this.#res);
|
|
156
|
-
__normalize(req.pause());
|
|
157
|
-
if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(res.writableEnded))) && __requireBoolean(!__requireBoolean(__normalize(res.destroyed))))) {
|
|
158
|
-
__normalize(res.setHeader("Connection", "close"));
|
|
159
|
-
__normalize(res.once("finish", __bindContexts(() => {
|
|
160
|
-
__normalize(req.destroy());
|
|
161
|
-
return null;
|
|
162
|
-
})));
|
|
163
|
-
let callback = __normalize(this.#failure);
|
|
164
|
-
__call(callback, status, error);
|
|
165
|
-
}
|
|
166
|
-
return null;
|
|
167
|
-
return null;
|
|
168
|
-
}
|
|
169
|
-
#cleanup() {
|
|
170
|
-
__hostGlobalCall(["clearTimeout"], __normalize(this.#timer));
|
|
171
|
-
let req = __normalize(this.#req);
|
|
172
|
-
__normalize(req.removeListener("data", __normalize(this.#data)));
|
|
173
|
-
__normalize(req.removeListener("end", __normalize(this.#ended)));
|
|
174
|
-
__normalize(req.removeListener("aborted", __normalize(this.#aborted)));
|
|
175
|
-
__normalize(this.#res.removeListener("close", __normalize(this.#closed)));
|
|
176
|
-
return null;
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
//# sourceMappingURL=body_reader.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"body_reader.mjs","sources":["body_reader.lango"],"sourcesContent":["import js global Buffer\nimport js global Number\nimport js global setTimeout\nimport js global clearTimeout\n\nexport class BodyReader\n methods\n start()\n\nBodyReader(req, res, options, complete, failure)\n @req = req\n @res = res\n @options = options\n @complete = complete\n @failure = failure\n @chunks = []\n @size = 0\n @done = false\n @timer = null\n data = (chunk)\n accept(chunk)\n ended = ()\n finish()\n failed = (error)\n reject(400, \"BadRequest\")\n aborted = ()\n abandon()\n closed = ()\n abandon()\n @data = data\n @ended = ended\n @failed = failed\n @aborted = aborted\n @closed = closed\n\nBodyReader:start()\n req = @req\n res = @res\n if req.aborted or res.destroyed\n abandon()\n return null\n req.on(\"error\", @failed)\n req.on(\"aborted\", @aborted)\n res.on(\"close\", @closed)\n length = req.headers[\"content-length\"]\n options = @options\n if is_string(length) and Number/parseInt(length, 10) > options.bodyLimit\n reject(413, \"RequestTooLarge\")\n return null\n expired = ()\n reject(408, \"RequestTimeout\")\n @timer = setTimeout(expired, options.bodyTimeoutMs)\n req.on(\"data\", @data)\n req.on(\"end\", @ended)\n return null\n\nBodyReader:accept(chunk)\n if @done\n return null\n @size = @size + chunk.length\n options = @options\n if @size > options.bodyLimit\n reject(413, \"RequestTooLarge\")\n else\n @chunks.add(chunk)\n return null\n\nBodyReader:finish()\n if @done\n return null\n @done = true\n body = Buffer/concat(@chunks).toString(\"utf8\")\n cleanup()\n @chunks = []\n callback = @complete\n callback(body)\n return null\n\nBodyReader:abandon()\n if @done\n return null\n @done = true\n cleanup()\n @chunks = []\n return null\n\nBodyReader:reject(status, error)\n if @done\n return null\n @done = true\n cleanup()\n @chunks = []\n req = @req\n res = @res\n req.pause()\n if !res.writableEnded and !res.destroyed\n res.setHeader(\"Connection\", \"close\")\n res.once(\"finish\", ()\n capture req\n req.destroy()\n )\n callback = @failure\n callback(status, error)\n return null\n\nBodyReader:cleanup()\n clearTimeout(@timer)\n req = @req\n req.removeListener(\"data\", @data)\n req.removeListener(\"end\", @ended)\n req.removeListener(\"aborted\", @aborted)\n @res.removeListener(\"close\", @closed)\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
import * as crypto from "node:crypto";
|
|
2
|
-
import {
|
|
3
|
-
hostGlobal as __hostGlobal,
|
|
4
|
-
hostGlobalCall as __hostGlobalCall,
|
|
5
|
-
contextGet as __contextGet,
|
|
6
|
-
contextSet as __contextSet,
|
|
7
|
-
contextRestore as __contextRestore,
|
|
8
|
-
isNumber as __isNumber,
|
|
9
|
-
isString as __isString,
|
|
10
|
-
isBool as __isBool,
|
|
11
|
-
isObject as __isObject,
|
|
12
|
-
isArray as __isArray,
|
|
13
|
-
requireBoolean as __requireBoolean,
|
|
14
|
-
normalize as __normalize,
|
|
15
|
-
finite as __finite,
|
|
16
|
-
numericAdd as __numericAdd,
|
|
17
|
-
boundary as __boundary,
|
|
18
|
-
startTrans as __startTrans,
|
|
19
|
-
commitTrans as __commitTrans,
|
|
20
|
-
rollbackTrans as __rollbackTrans,
|
|
21
|
-
transState as __transState,
|
|
22
|
-
query as __query,
|
|
23
|
-
requireArity as __requireArity,
|
|
24
|
-
} from "./.lango/runtime.mjs";
|
|
25
|
-
|
|
26
|
-
export class ClientIdentity {
|
|
27
|
-
#settings;
|
|
28
|
-
#database;
|
|
29
|
-
constructor(settings, database) {
|
|
30
|
-
__requireArity(arguments.length, [0, 1], "constructor");
|
|
31
|
-
settings = __boundary(settings);
|
|
32
|
-
database = __boundary(database);
|
|
33
|
-
this.#settings = settings;
|
|
34
|
-
this.#database = database;
|
|
35
|
-
}
|
|
36
|
-
initialize() {
|
|
37
|
-
const __contextScope = [];
|
|
38
|
-
try {
|
|
39
|
-
__contextScope.push(__contextSet("db", __normalize(this.#database.scope())));
|
|
40
|
-
try {
|
|
41
|
-
__startTrans();
|
|
42
|
-
if (__requireBoolean(__normalize(__contextGet("db").parameterStyle) === "postgresql")) {
|
|
43
|
-
let lock = __query("select pg_advisory_xact_lock(1818324591, 1)", []);
|
|
44
|
-
__normalize(lock.execute());
|
|
45
|
-
}
|
|
46
|
-
let row = __query("create table if not exists _lango_clients (\n client_id text primary key,\n secret_hash text not null unique,\n expires_at bigint not null\n)", []);
|
|
47
|
-
__normalize(row.execute());
|
|
48
|
-
__commitTrans();
|
|
49
|
-
} finally {
|
|
50
|
-
__normalize(__contextGet("db").finish());
|
|
51
|
-
}
|
|
52
|
-
return null;
|
|
53
|
-
return null;
|
|
54
|
-
} finally {
|
|
55
|
-
__contextRestore(__contextScope);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
begin(claims, tokens) {
|
|
59
|
-
const __contextScope = [];
|
|
60
|
-
try {
|
|
61
|
-
__requireArity(arguments.length, [0, 1], "begin");
|
|
62
|
-
claims = __boundary(claims);
|
|
63
|
-
tokens = __boundary(tokens);
|
|
64
|
-
let value = readCookie();
|
|
65
|
-
if (__requireBoolean(__requireBoolean(value === null) && __requireBoolean(claims !== null))) {
|
|
66
|
-
__normalize(__contextGet("response").reject(401, "ClientProofRequired"));
|
|
67
|
-
}
|
|
68
|
-
let hash = null;
|
|
69
|
-
let row = null;
|
|
70
|
-
if (__requireBoolean(value === null)) {
|
|
71
|
-
value = clientSecret();
|
|
72
|
-
claims = __normalize(tokens.create());
|
|
73
|
-
hash = clientDigest(value);
|
|
74
|
-
claims.clientProof = hash;
|
|
75
|
-
return { claims: claims, value: value, hash: hash, fresh: true, restored: false, rotate: false, closed: false };
|
|
76
|
-
}
|
|
77
|
-
hash = clientDigest(value);
|
|
78
|
-
row = __query("select client_id from _lango_clients where secret_hash = :hash and expires_at > :now", ["hash","now"]);
|
|
79
|
-
row.hash = hash;
|
|
80
|
-
row.now = __normalize(__hostGlobal(["Date"]).now());
|
|
81
|
-
__normalize(row.execute());
|
|
82
|
-
if (__requireBoolean(!__requireBoolean(__normalize(row.next())))) {
|
|
83
|
-
__normalize(__contextGet("response").reject(401, "ClientCookieRejected"));
|
|
84
|
-
}
|
|
85
|
-
let restored = claims === null;
|
|
86
|
-
if (__requireBoolean(restored)) {
|
|
87
|
-
claims = __normalize(tokens.create(__normalize(row.client_id)));
|
|
88
|
-
claims.clientProof = hash;
|
|
89
|
-
}
|
|
90
|
-
if (__requireBoolean(__requireBoolean(__normalize(claims.clientId) !== __normalize(row.client_id)) || __requireBoolean(!__requireBoolean(sameProof(__normalize(claims.clientProof), hash))))) {
|
|
91
|
-
__normalize(__contextGet("response").reject(401, "ClientProofRequired"));
|
|
92
|
-
}
|
|
93
|
-
return { claims: claims, value: value, hash: hash, fresh: false, restored: restored, rotate: false, closed: false };
|
|
94
|
-
return null;
|
|
95
|
-
} finally {
|
|
96
|
-
__contextRestore(__contextScope);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
publish(identity, claims, response, tokens) {
|
|
100
|
-
const __contextScope = [];
|
|
101
|
-
try {
|
|
102
|
-
__requireArity(arguments.length, [0, 1, 2, 3], "publish");
|
|
103
|
-
identity = __boundary(identity);
|
|
104
|
-
claims = __boundary(claims);
|
|
105
|
-
response = __boundary(response);
|
|
106
|
-
tokens = __boundary(tokens);
|
|
107
|
-
let value = __normalize(identity.value);
|
|
108
|
-
if (__requireBoolean(__normalize(identity.rotate))) {
|
|
109
|
-
value = clientSecret();
|
|
110
|
-
}
|
|
111
|
-
let hash = clientDigest(value);
|
|
112
|
-
claims.clientProof = hash;
|
|
113
|
-
let encoded = __normalize(tokens.sign(claims));
|
|
114
|
-
__contextScope.push(__contextSet("db", __normalize(this.#database.scope())));
|
|
115
|
-
let row = null;
|
|
116
|
-
try {
|
|
117
|
-
if (__requireBoolean(__normalize(identity.fresh))) {
|
|
118
|
-
row = __query("insert into _lango_clients (client_id, secret_hash, expires_at)\nvalues (:id, :hash, :expires)", ["id","hash","expires"]);
|
|
119
|
-
row.id = __normalize(claims.clientId);
|
|
120
|
-
row.hash = hash;
|
|
121
|
-
row.expires = __numericAdd(__normalize(__hostGlobal(["Date"]).now()), __finite(__normalize(this.#settings.maxAgeSeconds) * 1000));
|
|
122
|
-
__normalize(row.execute());
|
|
123
|
-
} else {
|
|
124
|
-
row = __query("update _lango_clients set secret_hash = :replacement, expires_at = :expires\nwhere client_id = :id and secret_hash = :previous and expires_at > :now\nreturning client_id", ["replacement","expires","id","previous","now"]);
|
|
125
|
-
row.id = __normalize(claims.clientId);
|
|
126
|
-
row.previous = __normalize(identity.hash);
|
|
127
|
-
row.replacement = hash;
|
|
128
|
-
row.now = __normalize(__hostGlobal(["Date"]).now());
|
|
129
|
-
row.expires = __numericAdd(__normalize(__hostGlobal(["Date"]).now()), __finite(__normalize(this.#settings.maxAgeSeconds) * 1000));
|
|
130
|
-
__normalize(row.execute());
|
|
131
|
-
if (__requireBoolean(!__requireBoolean(__normalize(row.next())))) {
|
|
132
|
-
__normalize(response.reject(401, "ClientProofRequired"));
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
__normalize(response.header("Set-Cookie", `__Host-lango-client=${value}; Path=/; Secure; HttpOnly; SameSite=Strict; Max-Age=${__normalize(this.#settings.maxAgeSeconds)}`));
|
|
136
|
-
} finally {
|
|
137
|
-
__normalize(__contextGet("db").finish());
|
|
138
|
-
}
|
|
139
|
-
identity.closed = true;
|
|
140
|
-
return encoded;
|
|
141
|
-
return null;
|
|
142
|
-
} finally {
|
|
143
|
-
__contextRestore(__contextScope);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
export function clientOptions(spec, database, origin) {
|
|
148
|
-
__requireArity(arguments.length, [0, 1, 2], "clientOptions");
|
|
149
|
-
spec = __boundary(spec);
|
|
150
|
-
database = __boundary(database);
|
|
151
|
-
origin = __boundary(origin);
|
|
152
|
-
if (__requireBoolean(spec === null)) {
|
|
153
|
-
return null;
|
|
154
|
-
}
|
|
155
|
-
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
156
|
-
throw "Client configuration must be an object";
|
|
157
|
-
}
|
|
158
|
-
for (const name of __normalize(__hostGlobal(["Object"]).keys(spec))) {
|
|
159
|
-
if (__requireBoolean(name !== "maxAgeSeconds")) {
|
|
160
|
-
throw `Unknown client option: ${name}`;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
if (__requireBoolean(database === null)) {
|
|
164
|
-
throw "Managed client identity requires a database";
|
|
165
|
-
}
|
|
166
|
-
if (__requireBoolean(__requireBoolean(__normalize(database.type) === "sqlite") && __requireBoolean(__normalize(database.file) === ":memory:"))) {
|
|
167
|
-
throw "Managed client identity requires persistent database storage";
|
|
168
|
-
}
|
|
169
|
-
if (__requireBoolean(__requireBoolean(__requireBoolean(origin === null) || __requireBoolean(!__requireBoolean(__normalize(origin.required)))) || __requireBoolean(!__requireBoolean(__normalize(origin.url.startsWith("https:")))))) {
|
|
170
|
-
throw "Managed client identity requires a required HTTPS origin";
|
|
171
|
-
}
|
|
172
|
-
let lifetime = __normalize(spec.maxAgeSeconds);
|
|
173
|
-
if (__requireBoolean(lifetime === null)) {
|
|
174
|
-
lifetime = 2592000;
|
|
175
|
-
}
|
|
176
|
-
if (__requireBoolean(__isString(lifetime))) {
|
|
177
|
-
lifetime = __hostGlobalCall(["Number"], lifetime);
|
|
178
|
-
}
|
|
179
|
-
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(lifetime)))) || __requireBoolean(lifetime < 1)) || __requireBoolean(lifetime > 31536000))) {
|
|
180
|
-
throw "Client maxAgeSeconds must be between 1 and 31536000";
|
|
181
|
-
}
|
|
182
|
-
return { maxAgeSeconds: lifetime };
|
|
183
|
-
return null;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function readCookie() {
|
|
187
|
-
const __contextScope = [];
|
|
188
|
-
try {
|
|
189
|
-
let pattern;
|
|
190
|
-
let headers = __normalize(__contextGet("request").headers);
|
|
191
|
-
let header = __normalize(headers.cookie);
|
|
192
|
-
if (__requireBoolean(header === null)) {
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(header))) || __requireBoolean(__normalize(header.length) > 8192))) {
|
|
196
|
-
__normalize(__contextGet("response").reject(400, "InvalidCookie"));
|
|
197
|
-
}
|
|
198
|
-
let found = null;
|
|
199
|
-
for (const part of __normalize(header.split(";"))) {
|
|
200
|
-
let pair = __normalize(part.trim());
|
|
201
|
-
let index = __normalize(pair.indexOf("="));
|
|
202
|
-
if (__requireBoolean(__requireBoolean(index >= 0) && __requireBoolean(__normalize(pair.slice(0, index)) === "__Host-lango-client"))) {
|
|
203
|
-
if (__requireBoolean(found !== null)) {
|
|
204
|
-
__normalize(__contextGet("response").reject(400, "InvalidCookie"));
|
|
205
|
-
}
|
|
206
|
-
found = __normalize(pair.slice(__numericAdd(index, 1)));
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
if (__requireBoolean(found !== null)) {
|
|
210
|
-
pattern = new (__hostGlobal(["RegExp"]))("^[a-f0-9]{64}$");
|
|
211
|
-
if (__requireBoolean(!__requireBoolean(__normalize(pattern.test(found))))) {
|
|
212
|
-
__normalize(__contextGet("response").reject(400, "InvalidCookie"));
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return found;
|
|
216
|
-
return null;
|
|
217
|
-
} finally {
|
|
218
|
-
__contextRestore(__contextScope);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function clientSecret() {
|
|
223
|
-
let bytes = __normalize(crypto.randomBytes(32));
|
|
224
|
-
return __normalize(bytes.toString("hex"));
|
|
225
|
-
return null;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function clientDigest(value) {
|
|
229
|
-
__requireArity(arguments.length, [0], "clientDigest");
|
|
230
|
-
value = __boundary(value);
|
|
231
|
-
let hash = __normalize(crypto.createHash("sha256"));
|
|
232
|
-
__normalize(hash.update(value));
|
|
233
|
-
return __normalize(hash.digest("hex"));
|
|
234
|
-
return null;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function sameProof(left, right) {
|
|
238
|
-
__requireArity(arguments.length, [0, 1], "sameProof");
|
|
239
|
-
left = __boundary(left);
|
|
240
|
-
right = __boundary(right);
|
|
241
|
-
let pattern = new (__hostGlobal(["RegExp"]))("^[a-f0-9]{64}$");
|
|
242
|
-
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(left))) || __requireBoolean(!__requireBoolean(__normalize(pattern.test(left)))))) {
|
|
243
|
-
return false;
|
|
244
|
-
}
|
|
245
|
-
let first = __normalize(__hostGlobal(["Buffer"]).from(left, "hex"));
|
|
246
|
-
let second = __normalize(__hostGlobal(["Buffer"]).from(right, "hex"));
|
|
247
|
-
return __normalize(crypto.timingSafeEqual(first, second));
|
|
248
|
-
return null;
|
|
249
|
-
}
|
|
250
|
-
//# sourceMappingURL=client_identity.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client_identity.mjs","sources":["client_identity.lango"],"sourcesContent":["import js \"node:crypto\" as crypto\nimport js global Date\nimport js global Object\nimport js global Number\nimport js global RegExp\nimport js global Buffer\n\nexport clientOptions(spec, database, origin)\nexport class ClientIdentity(settings, database)\n methods\n initialize()\n begin(claims, tokens)\n publish(identity, claims, response, tokens)\n\nclientOptions(spec, database, origin)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"Client configuration must be an object\"\n for Object/keys(spec) as name\n if name != \"maxAgeSeconds\"\n throw \"Unknown client option: ${name}\"\n if database == null\n throw \"Managed client identity requires a database\"\n if database.type == \"sqlite\" and database.file == \":memory:\"\n throw \"Managed client identity requires persistent database storage\"\n if origin == null or !origin.required or !origin.url.startsWith(\"https:\")\n throw \"Managed client identity requires a required HTTPS origin\"\n lifetime = spec.maxAgeSeconds\n if lifetime == null\n lifetime = 2592000\n if is_string(lifetime)\n lifetime = Number(lifetime)\n if !Number/isSafeInteger(lifetime) or lifetime < 1 or lifetime > 31536000\n throw \"Client maxAgeSeconds must be between 1 and 31536000\"\n return {\n maxAgeSeconds: lifetime\n }\n\nClientIdentity(settings, database)\n @settings = settings\n @database = database\n\nClientIdentity:initialize()\n context db = @database.scope()\n try\n start_trans()\n if db.parameterStyle == \"postgresql\"\n lock = SQL\n select pg_advisory_xact_lock(1818324591, 1)\n lock.execute()\n row = SQL\n create table if not exists _lango_clients (\n client_id text primary key,\n secret_hash text not null unique,\n expires_at bigint not null\n )\n row.execute()\n commit_trans()\n finally\n db.finish()\n return null\n\nClientIdentity:begin(claims, tokens)\n context response\n value = readCookie()\n if value == null and claims != null\n response.reject(401, \"ClientProofRequired\")\n hash = null\n row = null\n if value == null\n value = clientSecret()\n claims = tokens.create()\n hash = clientDigest(value)\n claims.clientProof = hash\n return {\n claims: claims\n value: value\n hash: hash\n fresh: true\n restored: false\n rotate: false\n closed: false\n }\n hash = clientDigest(value)\n row = SQL\n select client_id from _lango_clients where secret_hash = :hash and expires_at > :now\n row.hash = hash\n row.now = Date/now()\n row.execute()\n if !row.next()\n response.reject(401, \"ClientCookieRejected\")\n restored = claims == null\n if restored\n claims = tokens.create(row.client_id)\n claims.clientProof = hash\n if claims.clientId != row.client_id or !sameProof(claims.clientProof, hash)\n response.reject(401, \"ClientProofRequired\")\n return {\n claims: claims\n value: value\n hash: hash\n fresh: false\n restored: restored\n rotate: false\n closed: false\n }\n\nClientIdentity:publish(identity, claims, response, tokens)\n value = identity.value\n if identity.rotate\n value = clientSecret()\n hash = clientDigest(value)\n claims.clientProof = hash\n encoded = tokens.sign(claims)\n context db = @database.scope()\n row = null\n try\n if identity.fresh\n row = SQL\n insert into _lango_clients (client_id, secret_hash, expires_at)\n values (:id, :hash, :expires)\n row.id = claims.clientId\n row.hash = hash\n row.expires = Date/now() + @settings.maxAgeSeconds * 1000\n row.execute()\n else\n row = SQL\n update _lango_clients set secret_hash = :replacement, expires_at = :expires\n where client_id = :id and secret_hash = :previous and expires_at > :now\n returning client_id\n row.id = claims.clientId\n row.previous = identity.hash\n row.replacement = hash\n row.now = Date/now()\n row.expires = Date/now() + @settings.maxAgeSeconds * 1000\n row.execute()\n if !row.next()\n response.reject(401, \"ClientProofRequired\")\n response.header(\"Set-Cookie\", \"__Host-lango-client=${value}; Path=/; Secure; HttpOnly; SameSite=Strict; Max-Age=${@settings.maxAgeSeconds}\")\n finally\n db.finish()\n identity.closed = true\n return encoded\n\nreadCookie()\n context request\n context response\n headers = request.headers\n header = headers.cookie\n if header == null\n return null\n if !is_string(header) or header.length > 8192\n response.reject(400, \"InvalidCookie\")\n found = null\n for header.split(\";\") as part\n pair = part.trim()\n index = pair.indexOf(\"=\")\n if index >= 0 and pair.slice(0, index) == \"__Host-lango-client\"\n if found != null\n response.reject(400, \"InvalidCookie\")\n found = pair.slice(index + 1)\n if found != null\n pattern = new RegExp(\"^[a-f0-9]{64}$\")\n if !pattern.test(found)\n response.reject(400, \"InvalidCookie\")\n return found\n\nclientSecret()\n bytes = crypto/randomBytes(32)\n return bytes.toString(\"hex\")\n\nclientDigest(value)\n hash = crypto/createHash(\"sha256\")\n hash.update(value)\n return hash.digest(\"hex\")\n\nsameProof(left, right)\n pattern = new RegExp(\"^[a-f0-9]{64}$\")\n if !is_string(left) or !pattern.test(left)\n return false\n first = Buffer/from(left, \"hex\")\n second = Buffer/from(right, \"hex\")\n return crypto/timingSafeEqual(first, second)\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuBA;;;;;;AAIA;;;;;;;;;AAKA;;;;;;;;;;;;;;"}
|
package/dist/cli/database.mjs
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { ClientIdentity } from "./client_identity.mjs";
|
|
2
|
-
import { DatabaseScope } from "./database_scope.mjs";
|
|
3
|
-
import { SqliteAdapter } from "./sqlite_adapter.mjs";
|
|
4
|
-
import { PostgresAdapter } from "./postgres_adapter.mjs";
|
|
5
|
-
import {
|
|
6
|
-
hostGlobal as __hostGlobal,
|
|
7
|
-
contextGet as __contextGet,
|
|
8
|
-
contextSet as __contextSet,
|
|
9
|
-
contextRestore as __contextRestore,
|
|
10
|
-
bindContexts as __bindContexts,
|
|
11
|
-
requireBoolean as __requireBoolean,
|
|
12
|
-
normalize as __normalize,
|
|
13
|
-
boundary as __boundary,
|
|
14
|
-
query as __query,
|
|
15
|
-
requireArity as __requireArity,
|
|
16
|
-
withArity as __withArity,
|
|
17
|
-
} from "./.lango/runtime.mjs";
|
|
18
|
-
|
|
19
|
-
export class Database {
|
|
20
|
-
#root;
|
|
21
|
-
#settings;
|
|
22
|
-
#adapter;
|
|
23
|
-
constructor(settings, root = null) {
|
|
24
|
-
__requireArity(arguments.length, [0], "constructor");
|
|
25
|
-
settings = __boundary(settings);
|
|
26
|
-
root = __boundary(root);
|
|
27
|
-
this.#root = root;
|
|
28
|
-
this.#settings = settings;
|
|
29
|
-
this.#adapter = null;
|
|
30
|
-
}
|
|
31
|
-
open() {
|
|
32
|
-
if (__requireBoolean(__normalize(this.#settings) === null)) {
|
|
33
|
-
return __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
34
|
-
}
|
|
35
|
-
let name = "node:sqlite";
|
|
36
|
-
if (__requireBoolean(__normalize(this.#settings.type) === "postgresql")) {
|
|
37
|
-
name = "node:worker_threads";
|
|
38
|
-
}
|
|
39
|
-
return import(name).then(__withArity(__bindContexts((mod) => {
|
|
40
|
-
const __contextScope = [];
|
|
41
|
-
try {
|
|
42
|
-
mod = __boundary(mod);
|
|
43
|
-
if (__requireBoolean(__normalize(this.#settings.type) === "sqlite")) {
|
|
44
|
-
this.#adapter = new SqliteAdapter(mod, __normalize(this.#settings));
|
|
45
|
-
} else {
|
|
46
|
-
this.#adapter = new PostgresAdapter(mod, __normalize(this.#settings), __normalize(this.#root));
|
|
47
|
-
}
|
|
48
|
-
__contextScope.push(__contextSet("db", this.scope()));
|
|
49
|
-
try {
|
|
50
|
-
let query = __query("select 1", []);
|
|
51
|
-
__normalize(query.execute());
|
|
52
|
-
} finally {
|
|
53
|
-
__normalize(__contextGet("db").finish());
|
|
54
|
-
}
|
|
55
|
-
return null;
|
|
56
|
-
return null;
|
|
57
|
-
} finally {
|
|
58
|
-
__contextRestore(__contextScope);
|
|
59
|
-
}
|
|
60
|
-
}), [0], "closure")).catch(__withArity(__bindContexts((error) => {
|
|
61
|
-
const __contextScope = [];
|
|
62
|
-
try {
|
|
63
|
-
error = __boundary(error);
|
|
64
|
-
__normalize(__contextGet("log").err(`Database startup failed: ${error}`));
|
|
65
|
-
this.close();
|
|
66
|
-
throw "Database startup failed; check configuration, compiler version and driver installation";
|
|
67
|
-
return null;
|
|
68
|
-
} finally {
|
|
69
|
-
__contextRestore(__contextScope);
|
|
70
|
-
}
|
|
71
|
-
}), [0], "closure"));
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
scope() {
|
|
75
|
-
if (__requireBoolean(__normalize(this.#adapter) === null)) {
|
|
76
|
-
return null;
|
|
77
|
-
}
|
|
78
|
-
return new DatabaseScope(__normalize(this.#adapter));
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
close() {
|
|
82
|
-
if (__requireBoolean(__normalize(this.#adapter) !== null)) {
|
|
83
|
-
__normalize(this.#adapter.close());
|
|
84
|
-
this.#adapter = null;
|
|
85
|
-
}
|
|
86
|
-
return null;
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
export function prepareDatabase(settings, root = null, client = null) {
|
|
91
|
-
__requireArity(arguments.length, [0], "prepareDatabase");
|
|
92
|
-
settings = __boundary(settings);
|
|
93
|
-
root = __boundary(root);
|
|
94
|
-
client = __boundary(client);
|
|
95
|
-
if (__requireBoolean(settings === null)) {
|
|
96
|
-
return __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
97
|
-
}
|
|
98
|
-
if (__requireBoolean(__requireBoolean(client === null) && __requireBoolean(__requireBoolean(__normalize(settings.type) !== "sqlite") || __requireBoolean(__normalize(settings.file) === ":memory:")))) {
|
|
99
|
-
return __normalize(__hostGlobal(["Promise"]).resolve(null));
|
|
100
|
-
}
|
|
101
|
-
let database = new Database(settings, root);
|
|
102
|
-
return __normalize(database.open()).then(__withArity(__bindContexts((ignored) => {
|
|
103
|
-
ignored = __boundary(ignored);
|
|
104
|
-
if (__requireBoolean(client !== null)) {
|
|
105
|
-
let identity = new ClientIdentity(client, database);
|
|
106
|
-
__normalize(identity.initialize());
|
|
107
|
-
}
|
|
108
|
-
return null;
|
|
109
|
-
return null;
|
|
110
|
-
}), [0], "closure")).finally(__bindContexts(() => {
|
|
111
|
-
__normalize(database.close());
|
|
112
|
-
return null;
|
|
113
|
-
}));
|
|
114
|
-
return null;
|
|
115
|
-
}
|
|
116
|
-
//# sourceMappingURL=database.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"database.mjs","sources":["database.lango"],"sourcesContent":["import \"./client_identity\"\n class ClientIdentity\nimport js global Promise\nimport \"./database_scope\"\n class DatabaseScope\nimport \"./sqlite_adapter\"\n class SqliteAdapter\nimport \"./postgres_adapter\"\n class PostgresAdapter\n\nexport prepareDatabase(settings, root = null, client = null)\n\nexport class Database(settings, root = null)\n methods\n open()\n scope()\n close()\n\nDatabase(settings, root = null)\n @root = root\n @settings = settings\n @adapter = null\n\nDatabase:open()\n if @settings == null\n return Promise/resolve(null)\n name = \"node:sqlite\"\n if @settings.type == \"postgresql\"\n name = \"node:worker_threads\"\n return chain import_module(name)\n then(mod)\n if @settings.type == \"sqlite\"\n @adapter = new SqliteAdapter(mod, @settings)\n else\n @adapter = new PostgresAdapter(mod, @settings, @root)\n context db = scope()\n try\n query = SQL\n select 1\n query.execute()\n finally\n db.finish()\n return null\n catch(error)\n context log\n log.err(\"Database startup failed: ${error}\")\n close()\n throw \"Database startup failed; check configuration, compiler version and driver installation\"\n\nDatabase:scope()\n if @adapter == null\n return null\n return new DatabaseScope(@adapter)\n\nDatabase:close()\n if @adapter != null\n @adapter.close()\n @adapter = null\n return null\n\nprepareDatabase(settings, root = null, client = null)\n if settings == null\n return Promise/resolve(null)\n if client == null and (settings.type != \"sqlite\" or settings.file == \":memory:\")\n return Promise/resolve(null)\n database = new Database(settings, root)\n return chain database.open()\n then(ignored)\n capture database\n capture client\n if client != null\n identity = new ClientIdentity(client, database)\n identity.initialize()\n return null\n finally\n capture database\n database.close()\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
import * as path from "node:path";
|
|
2
|
-
import {
|
|
3
|
-
hostGlobal as __hostGlobal,
|
|
4
|
-
hostGlobalCall as __hostGlobalCall,
|
|
5
|
-
isNumber as __isNumber,
|
|
6
|
-
isString as __isString,
|
|
7
|
-
isBool as __isBool,
|
|
8
|
-
isObject as __isObject,
|
|
9
|
-
isArray as __isArray,
|
|
10
|
-
entries as __entries,
|
|
11
|
-
requireBoolean as __requireBoolean,
|
|
12
|
-
normalize as __normalize,
|
|
13
|
-
boundary as __boundary,
|
|
14
|
-
requireArity as __requireArity,
|
|
15
|
-
} from "./.lango/runtime.mjs";
|
|
16
|
-
|
|
17
|
-
export function databaseOptions(root, spec, workers) {
|
|
18
|
-
__requireArity(arguments.length, [0, 1, 2], "databaseOptions");
|
|
19
|
-
root = __boundary(root);
|
|
20
|
-
spec = __boundary(spec);
|
|
21
|
-
workers = __boundary(workers);
|
|
22
|
-
let file;
|
|
23
|
-
if (__requireBoolean(spec === null)) {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
27
|
-
throw "Database configuration must be a map";
|
|
28
|
-
}
|
|
29
|
-
let allowed = [];
|
|
30
|
-
if (__normalize(spec.type) === "sqlite") {
|
|
31
|
-
allowed = ["type", "file", "busyTimeoutMs"];
|
|
32
|
-
} else if (__normalize(spec.type) === "postgresql") {
|
|
33
|
-
allowed = ["type", "connectionEnv", "queryTimeoutMs", "connectTimeoutSeconds"];
|
|
34
|
-
} else {
|
|
35
|
-
throw "Database type must be sqlite or postgresql";
|
|
36
|
-
}
|
|
37
|
-
for (const [key, value] of __entries(spec)) {
|
|
38
|
-
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
39
|
-
throw `Unknown database option: ${key}`;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (__requireBoolean(__normalize(spec.type) === "sqlite")) {
|
|
43
|
-
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.file)))) || __requireBoolean(__normalize(spec.file) === ""))) {
|
|
44
|
-
throw "SQLite database file is required";
|
|
45
|
-
}
|
|
46
|
-
file = __normalize(spec.file);
|
|
47
|
-
if (__requireBoolean(__requireBoolean(file === ":memory:") && __requireBoolean(workers !== 1))) {
|
|
48
|
-
throw "An in-memory SQLite database requires exactly one worker";
|
|
49
|
-
}
|
|
50
|
-
if (__requireBoolean(file !== ":memory:")) {
|
|
51
|
-
file = __normalize(path.resolve(root, file));
|
|
52
|
-
}
|
|
53
|
-
return { type: "sqlite", file: file, busyTimeoutMs: positive(__normalize(spec.busyTimeoutMs), 5000) };
|
|
54
|
-
}
|
|
55
|
-
let name = __normalize(spec.connectionEnv);
|
|
56
|
-
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(name))) || __requireBoolean(name === "")) || __requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(__normalize(__hostGlobal(["process"]).env), name)))))) {
|
|
57
|
-
throw "PostgreSQL connectionEnv must name an existing environment variable";
|
|
58
|
-
}
|
|
59
|
-
let connection = __normalize(__hostGlobal(["process"]).env[name]);
|
|
60
|
-
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(connection))) || __requireBoolean(__requireBoolean(!__requireBoolean(__normalize(connection.startsWith("postgresql://")))) && __requireBoolean(!__requireBoolean(__normalize(connection.startsWith("postgres://"))))))) {
|
|
61
|
-
throw "PostgreSQL connectionEnv must contain a PostgreSQL connection URL";
|
|
62
|
-
}
|
|
63
|
-
return { type: "postgresql", connection: connection, queryTimeoutMs: positive(__normalize(spec.queryTimeoutMs), 5000), connectTimeoutSeconds: positive(__normalize(spec.connectTimeoutSeconds), 5) };
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function positive(value, fallback) {
|
|
68
|
-
__requireArity(arguments.length, [0, 1], "positive");
|
|
69
|
-
value = __boundary(value);
|
|
70
|
-
fallback = __boundary(fallback);
|
|
71
|
-
if (__requireBoolean(value === null)) {
|
|
72
|
-
return fallback;
|
|
73
|
-
}
|
|
74
|
-
if (__requireBoolean(__isString(value))) {
|
|
75
|
-
value = __hostGlobalCall(["Number"], value);
|
|
76
|
-
}
|
|
77
|
-
if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(value)))) || __requireBoolean(value < 1)) || __requireBoolean(value > 2147483647))) {
|
|
78
|
-
throw "Database timeout must be a positive integer within the timer limit";
|
|
79
|
-
}
|
|
80
|
-
return value;
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
//# sourceMappingURL=database_options.mjs.map
|