@lango-project/lango-server 0.2.2 → 0.2.4
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 +252 -29
- package/dist/.lango/runtime.mjs +16 -0
- package/dist/body_reader.mjs +15 -14
- package/dist/body_reader.mjs.map +1 -1
- package/dist/builder.mjs +26 -2
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/.lango/runtime.mjs +16 -0
- package/dist/cli/body_reader.mjs +15 -14
- package/dist/cli/body_reader.mjs.map +1 -1
- package/dist/cli/client_identity.mjs +250 -0
- package/dist/cli/client_identity.mjs.map +1 -0
- package/dist/cli/database.mjs +23 -8
- package/dist/cli/database.mjs.map +1 -1
- package/dist/cli/database_scope.mjs +11 -0
- package/dist/cli/database_scope.mjs.map +1 -1
- package/dist/cli/endpoint.mjs +11 -7
- package/dist/cli/endpoint.mjs.map +1 -1
- package/dist/cli/longpoll_broker.mjs +9 -8
- package/dist/cli/longpoll_broker.mjs.map +1 -1
- package/dist/cli/longpoll_options.mjs +8 -7
- package/dist/cli/longpoll_options.mjs.map +1 -1
- package/dist/cli/longpoll_session.mjs +11 -10
- package/dist/cli/longpoll_session.mjs.map +1 -1
- package/dist/cli/middleware.mjs +1 -1
- package/dist/cli/middleware.mjs.map +1 -1
- package/dist/cli/options.mjs +41 -4
- package/dist/cli/options.mjs.map +1 -1
- package/dist/cli/origin.mjs +87 -0
- package/dist/cli/origin.mjs.map +1 -0
- package/dist/cli/policy.mjs +121 -0
- package/dist/cli/policy.mjs.map +1 -0
- package/dist/cli/postgres_adapter.mjs +15 -3
- package/dist/cli/postgres_adapter.mjs.map +1 -1
- package/dist/cli/routes.mjs +69 -18
- package/dist/cli/routes.mjs.map +1 -1
- package/dist/cli/server.mjs +88 -81
- package/dist/cli/server.mjs.map +1 -1
- package/dist/cli/static_files.mjs +5 -4
- package/dist/cli/static_files.mjs.map +1 -1
- package/dist/client_identity.mjs +250 -0
- package/dist/client_identity.mjs.map +1 -0
- package/dist/database.mjs +23 -8
- package/dist/database.mjs.map +1 -1
- package/dist/database_action.mjs +71 -0
- package/dist/database_action.mjs.map +1 -0
- package/dist/database_scope.mjs +11 -0
- package/dist/database_scope.mjs.map +1 -1
- package/dist/endpoint.mjs +11 -7
- package/dist/endpoint.mjs.map +1 -1
- package/dist/execution_failure.mjs +30 -0
- package/dist/execution_failure.mjs.map +1 -0
- package/dist/execution_identity.mjs +103 -0
- package/dist/execution_identity.mjs.map +1 -0
- package/dist/execution_log.mjs +47 -0
- package/dist/execution_log.mjs.map +1 -0
- package/dist/execution_runtime.mjs +201 -0
- package/dist/execution_runtime.mjs.map +1 -0
- package/dist/execution_worker.mjs +41 -0
- package/dist/execution_worker.mjs.map +1 -0
- package/dist/executor.mjs +384 -0
- package/dist/executor.mjs.map +1 -0
- package/dist/group.mjs +49 -16
- package/dist/group.mjs.map +1 -1
- package/dist/http_failure.mjs +9 -2
- package/dist/http_failure.mjs.map +1 -1
- package/dist/job_configuration.mjs +37 -0
- package/dist/job_configuration.mjs.map +1 -0
- package/dist/job_context.mjs +61 -0
- package/dist/job_context.mjs.map +1 -0
- package/dist/library.mjs +31 -0
- package/dist/library.mjs.map +1 -1
- package/dist/longpoll_broker.mjs +9 -8
- package/dist/longpoll_broker.mjs.map +1 -1
- package/dist/longpoll_options.mjs +8 -7
- package/dist/longpoll_options.mjs.map +1 -1
- package/dist/longpoll_session.mjs +11 -10
- package/dist/longpoll_session.mjs.map +1 -1
- package/dist/middleware.mjs +1 -1
- package/dist/middleware.mjs.map +1 -1
- package/dist/module_loader.mjs +44 -0
- package/dist/module_loader.mjs.map +1 -0
- package/dist/options.mjs +41 -4
- package/dist/options.mjs.map +1 -1
- package/dist/origin.mjs +87 -0
- package/dist/origin.mjs.map +1 -0
- package/dist/policy.mjs +121 -0
- package/dist/policy.mjs.map +1 -0
- package/dist/postgres_adapter.mjs +15 -3
- package/dist/postgres_adapter.mjs.map +1 -1
- package/dist/postgres_worker.mjs +82 -19
- package/dist/postgres_worker.mjs.map +1 -1
- package/dist/queue_provider.mjs +52 -0
- package/dist/queue_provider.mjs.map +1 -0
- package/dist/response.mjs +1 -1
- package/dist/response.mjs.map +1 -1
- package/dist/route_info.mjs +1 -1
- package/dist/route_info.mjs.map +1 -1
- package/dist/routes.mjs +69 -18
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +287 -132
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +88 -81
- package/dist/server.mjs.map +1 -1
- package/dist/server_context.mjs +21 -9
- package/dist/server_context.mjs.map +1 -1
- package/dist/static_files.mjs +5 -4
- package/dist/static_files.mjs.map +1 -1
- package/dist/token.mjs +34 -7
- package/dist/token.mjs.map +1 -1
- package/dist/tokens.mjs +11 -2
- package/dist/tokens.mjs.map +1 -1
- package/dist/worker.mjs +7 -6
- package/dist/worker.mjs.map +1 -1
- package/docs/jobs.md +123 -0
- package/docs/security.md +420 -0
- package/examples/security/README.md +160 -0
- package/examples/security/deploy.xml +12 -0
- package/examples/security/handlers.lango +610 -0
- package/examples/security/package.json +19 -0
- package/examples/security/passwords.lango +61 -0
- package/examples/security/routes.xml +14 -0
- package/examples/security/schema.lango +91 -0
- package/examples/security/setup.lango +30 -0
- package/package.json +14 -6
- package/src/builder.lango +22 -0
- package/src/client_identity.lango +184 -0
- package/src/database.lango +15 -4
- package/src/database_action.lango +30 -0
- package/src/database_scope.lango +7 -0
- package/src/endpoint.lango +12 -5
- package/src/execution_failure.lango +14 -0
- package/src/execution_identity.lango +71 -0
- package/src/execution_log.lango +28 -0
- package/src/execution_runtime.lango +135 -0
- package/src/execution_worker.lango +32 -0
- package/src/executor.lango +284 -0
- package/src/group.lango +36 -21
- package/src/http_failure.lango +9 -5
- package/src/job_configuration.lango +29 -0
- package/src/job_context.lango +44 -0
- package/src/library.lango +21 -0
- package/src/longpoll_options.lango +11 -5
- package/src/middleware.lango +1 -1
- package/src/module_loader.lango +25 -0
- package/src/options.lango +41 -2
- package/src/origin.lango +56 -0
- package/src/policy.lango +84 -0
- package/src/postgres_adapter.lango +12 -0
- package/src/postgres_worker.lango +54 -3
- package/src/queue_provider.lango +30 -0
- package/src/response.lango +1 -1
- package/src/route_info.lango +2 -0
- package/src/routes.lango +74 -15
- package/src/runtime.lango +173 -94
- package/src/server.lango +9 -2
- package/src/server_context.lango +10 -2
- package/src/token.lango +26 -5
- package/src/tokens.lango +10 -3
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ExecutionFailure } from "./execution_failure.mjs";
|
|
2
|
+
import { Token } from "./token.mjs";
|
|
3
|
+
import {
|
|
4
|
+
hostGlobal as __hostGlobal,
|
|
5
|
+
fromJson as __fromJson,
|
|
6
|
+
toJson as __toJson,
|
|
7
|
+
isNumber as __isNumber,
|
|
8
|
+
isString as __isString,
|
|
9
|
+
isBool as __isBool,
|
|
10
|
+
isObject as __isObject,
|
|
11
|
+
isArray as __isArray,
|
|
12
|
+
entries as __entries,
|
|
13
|
+
requireBoolean as __requireBoolean,
|
|
14
|
+
normalize as __normalize,
|
|
15
|
+
finite as __finite,
|
|
16
|
+
boundary as __boundary,
|
|
17
|
+
requireArity as __requireArity,
|
|
18
|
+
} from "./.lango/runtime.mjs";
|
|
19
|
+
|
|
20
|
+
export function executionSource(source) {
|
|
21
|
+
__requireArity(arguments.length, [0], "executionSource");
|
|
22
|
+
source = __boundary(source);
|
|
23
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(source === null) || __requireBoolean(!__requireBoolean(__isObject(source)))) || __requireBoolean(__isArray(source)))) {
|
|
24
|
+
throw "Execution source must be a map";
|
|
25
|
+
}
|
|
26
|
+
let allowed = ["kind", "clientId", "userId", "grantId", "identity"];
|
|
27
|
+
for (const [key, value] of __entries(source)) {
|
|
28
|
+
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
29
|
+
throw `Unknown execution source field: ${key}`;
|
|
30
|
+
}
|
|
31
|
+
if (__requireBoolean(__requireBoolean(key !== "kind") && __requireBoolean(__requireBoolean(!__requireBoolean(__isString(value))) || __requireBoolean(__normalize(value.trim()) === "")))) {
|
|
32
|
+
throw "Execution source references must be nonempty strings";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (__normalize(source.kind) === "user") {
|
|
36
|
+
if (__requireBoolean(__requireBoolean(__normalize(source.clientId) === null) || __requireBoolean(__normalize(source.identity) !== null))) {
|
|
37
|
+
throw "User execution source requires clientId and cannot select a system identity";
|
|
38
|
+
}
|
|
39
|
+
} else if (__normalize(source.kind) === "system") {
|
|
40
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(source.identity) === null) || __requireBoolean(__normalize(source.clientId) !== null)) || __requireBoolean(__normalize(source.userId) !== null)) || __requireBoolean(__normalize(source.grantId) !== null))) {
|
|
41
|
+
throw "System execution source requires only a configured identity";
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
throw "Execution source kind must be user or system";
|
|
45
|
+
}
|
|
46
|
+
return __fromJson(__toJson(source));
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function executionToken(identity, source, route) {
|
|
51
|
+
__requireArity(arguments.length, [0, 1, 2], "executionToken");
|
|
52
|
+
identity = __boundary(identity);
|
|
53
|
+
source = __boundary(source);
|
|
54
|
+
route = __boundary(route);
|
|
55
|
+
if (__requireBoolean(__requireBoolean(identity === null) || __requireBoolean(identity === false))) {
|
|
56
|
+
throw new ExecutionFailure("IdentityRejected");
|
|
57
|
+
}
|
|
58
|
+
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(identity))) || __requireBoolean(__isArray(identity)))) {
|
|
59
|
+
throw "Identity provider must return a map or null";
|
|
60
|
+
}
|
|
61
|
+
let allowed = ["clientId", "userId", "grantId", "identity", "policies", "authenticatedAt"];
|
|
62
|
+
for (const [key, value] of __entries(identity)) {
|
|
63
|
+
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
64
|
+
throw `Unknown execution identity field: ${key}`;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (__requireBoolean(!__requireBoolean(__isArray(__normalize(identity.policies))))) {
|
|
68
|
+
throw "Execution identity requires a policies array";
|
|
69
|
+
}
|
|
70
|
+
if (__requireBoolean(__normalize(source.kind) === "user")) {
|
|
71
|
+
for (const key of ["clientId", "userId", "grantId"]) {
|
|
72
|
+
if (__requireBoolean(__normalize(identity[key]) !== __normalize(source[key]))) {
|
|
73
|
+
throw new ExecutionFailure("IdentityChanged");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (__requireBoolean(__normalize(identity.identity) !== null)) {
|
|
77
|
+
throw new ExecutionFailure("IdentityChanged");
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(identity.identity) !== __normalize(source.identity)) || __requireBoolean(__normalize(identity.clientId) !== null)) || __requireBoolean(__normalize(identity.userId) !== null)) || __requireBoolean(__normalize(identity.grantId) !== null))) {
|
|
81
|
+
throw new ExecutionFailure("IdentityChanged");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
let now = __normalize(__hostGlobal(["Date"]).now());
|
|
85
|
+
let stamp = __normalize(identity.authenticatedAt);
|
|
86
|
+
if (__requireBoolean(__requireBoolean(stamp !== null) && __requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(stamp)))) || __requireBoolean(stamp < 0)) || __requireBoolean(stamp > now)))) {
|
|
87
|
+
throw "Execution identity authenticatedAt must be a real past timestamp";
|
|
88
|
+
}
|
|
89
|
+
let claims = { clientId: __normalize(identity.clientId), userId: __normalize(identity.userId), grantId: __normalize(identity.grantId), identity: __normalize(identity.identity), auth: "", authenticatedAt: stamp };
|
|
90
|
+
let token = new Token(claims);
|
|
91
|
+
for (const policy of __normalize(identity.policies)) {
|
|
92
|
+
__normalize(token.addPolicy(policy));
|
|
93
|
+
}
|
|
94
|
+
if (__requireBoolean(__requireBoolean(__normalize(route.policy) !== "anonymous") && __requireBoolean(!__requireBoolean(__normalize(token.hasPolicy(__normalize(route.policy))))))) {
|
|
95
|
+
throw new ExecutionFailure("Forbidden");
|
|
96
|
+
}
|
|
97
|
+
if (__requireBoolean(__requireBoolean(__normalize(route.reauthSeconds) > 0) && __requireBoolean(__requireBoolean(stamp === null) || __requireBoolean(__finite(now - stamp) >= __finite(__normalize(route.reauthSeconds) * 1000))))) {
|
|
98
|
+
throw new ExecutionFailure("ReauthenticationRequired");
|
|
99
|
+
}
|
|
100
|
+
return new Token(__normalize(token.snapshot()), true);
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=execution_identity.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execution_identity.mjs","sources":["execution_identity.lango"],"sourcesContent":["import \"./execution_failure\"\n class ExecutionFailure\nimport \"./token\"\n class Token\nimport js global Object\nimport js global Date\nimport js global Number\n\nexport executionSource(source)\nexport executionToken(identity, source, route)\n\nexecutionSource(source)\n if source == null or !is_object(source) or is_array(source)\n throw \"Execution source must be a map\"\n allowed = [\"kind\", \"clientId\", \"userId\", \"grantId\", \"identity\"]\n for source as key: value\n if !allowed.includes(key)\n throw \"Unknown execution source field: ${key}\"\n if key != \"kind\" and (!is_string(value) or value.trim() == \"\")\n throw \"Execution source references must be nonempty strings\"\n switch source.kind\n case \"user\"\n if source.clientId == null or source.identity != null\n throw \"User execution source requires clientId and cannot select a system identity\"\n case \"system\"\n if source.identity == null or source.clientId != null or source.userId != null or source.grantId != null\n throw \"System execution source requires only a configured identity\"\n else\n throw \"Execution source kind must be user or system\"\n return from_json(to_json(source))\n\nexecutionToken(identity, source, route)\n if identity == null or identity == false\n throw new ExecutionFailure(\"IdentityRejected\")\n if !is_object(identity) or is_array(identity)\n throw \"Identity provider must return a map or null\"\n allowed = [\"clientId\", \"userId\", \"grantId\", \"identity\", \"policies\", \"authenticatedAt\"]\n for identity as key: value\n if !allowed.includes(key)\n throw \"Unknown execution identity field: ${key}\"\n if !is_array(identity.policies)\n throw \"Execution identity requires a policies array\"\n if source.kind == \"user\"\n for [\"clientId\", \"userId\", \"grantId\"] as key\n if identity[key] != source[key]\n throw new ExecutionFailure(\"IdentityChanged\")\n if identity.identity != null\n throw new ExecutionFailure(\"IdentityChanged\")\n else\n if identity.identity != source.identity or identity.clientId != null or identity.userId != null or identity.grantId != null\n throw new ExecutionFailure(\"IdentityChanged\")\n now = Date/now()\n stamp = identity.authenticatedAt\n if stamp != null and (!Number/isSafeInteger(stamp) or stamp < 0 or stamp > now)\n throw \"Execution identity authenticatedAt must be a real past timestamp\"\n claims = {\n clientId: identity.clientId\n userId: identity.userId\n grantId: identity.grantId\n identity: identity.identity\n auth: \"\"\n authenticatedAt: stamp\n }\n token = new Token(claims)\n for identity.policies as policy\n token.addPolicy(policy)\n if route.policy != \"anonymous\" and !token.hasPolicy(route.policy)\n throw new ExecutionFailure(\"Forbidden\")\n if route.reauthSeconds > 0 and (stamp == null or now - stamp >= route.reauthSeconds * 1000)\n throw new ExecutionFailure(\"ReauthenticationRequired\")\n return new Token(token.snapshot(), true)\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAWA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalize as __normalize,
|
|
3
|
+
boundary as __boundary,
|
|
4
|
+
requireArity as __requireArity,
|
|
5
|
+
} from "./.lango/runtime.mjs";
|
|
6
|
+
|
|
7
|
+
export class ExecutionLog {
|
|
8
|
+
#sink;
|
|
9
|
+
#prefix;
|
|
10
|
+
constructor(sink, job) {
|
|
11
|
+
__requireArity(arguments.length, [0, 1], "constructor");
|
|
12
|
+
sink = __boundary(sink);
|
|
13
|
+
job = __boundary(job);
|
|
14
|
+
this.#sink = sink;
|
|
15
|
+
let source = __normalize(job.source);
|
|
16
|
+
this.#prefix = `[jobId=${__normalize(job.id)}] path=${__normalize(job.path)} queue=${__normalize(job.queue)} attempt=${__normalize(job.attempt)} clientId=${__normalize(source.clientId)} userId=${__normalize(source.userId)} grantId=${__normalize(source.grantId)} identity=${__normalize(source.identity)}`;
|
|
17
|
+
}
|
|
18
|
+
get verbose() {
|
|
19
|
+
return __normalize(this.#sink.verbose);
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
debug(message) {
|
|
23
|
+
__requireArity(arguments.length, [0], "debug");
|
|
24
|
+
message = __boundary(message);
|
|
25
|
+
return __normalize(this.#sink.debug(`${__normalize(this.#prefix)} ${message}`));
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
info(message) {
|
|
29
|
+
__requireArity(arguments.length, [0], "info");
|
|
30
|
+
message = __boundary(message);
|
|
31
|
+
return __normalize(this.#sink.info(`${__normalize(this.#prefix)} ${message}`));
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
warn(message) {
|
|
35
|
+
__requireArity(arguments.length, [0], "warn");
|
|
36
|
+
message = __boundary(message);
|
|
37
|
+
return __normalize(this.#sink.warn(`${__normalize(this.#prefix)} ${message}`));
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
err(message) {
|
|
41
|
+
__requireArity(arguments.length, [0], "err");
|
|
42
|
+
message = __boundary(message);
|
|
43
|
+
return __normalize(this.#sink.err(`${__normalize(this.#prefix)} ${message}`));
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=execution_log.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execution_log.mjs","sources":["execution_log.lango"],"sourcesContent":["export class ExecutionLog(sink, job)\n properties\n verbose?\n methods\n debug(message)\n info(message)\n warn(message)\n err(message)\n\nExecutionLog(sink, job)\n @sink = sink\n source = job.source?\n @prefix = \"[jobId=${job.id?}] path=${job.path?} queue=${job.queue?} attempt=${job.attempt?} clientId=${source.clientId} userId=${source.userId} grantId=${source.grantId} identity=${source.identity}\"\n\nExecutionLog:verbose?\n return @sink.verbose?\n\nExecutionLog:debug(message)\n return @sink.debug(\"${@prefix} ${message}\")\n\nExecutionLog:info(message)\n return @sink.info(\"${@prefix} ${message}\")\n\nExecutionLog:warn(message)\n return @sink.warn(\"${@prefix} ${message}\")\n\nExecutionLog:err(message)\n return @sink.err(\"${@prefix} ${message}\")\n"],"names":[],"mappings":";;;;;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { ModuleLoader } from "./module_loader.mjs";
|
|
2
|
+
import { Database } from "./database.mjs";
|
|
3
|
+
import { executionSource, executionToken } from "./execution_identity.mjs";
|
|
4
|
+
import { ExecutionFailure } from "./execution_failure.mjs";
|
|
5
|
+
import { JobContext } from "./job_context.mjs";
|
|
6
|
+
import { ExecutionLog } from "./execution_log.mjs";
|
|
7
|
+
import { errorDetails } from "./error_details.mjs";
|
|
8
|
+
import {
|
|
9
|
+
hostGlobal as __hostGlobal,
|
|
10
|
+
contextGet as __contextGet,
|
|
11
|
+
contextSet as __contextSet,
|
|
12
|
+
contextRestore as __contextRestore,
|
|
13
|
+
bindContexts as __bindContexts,
|
|
14
|
+
fromJson as __fromJson,
|
|
15
|
+
toJson as __toJson,
|
|
16
|
+
getClass as __getClass,
|
|
17
|
+
isKindof as __isKindof,
|
|
18
|
+
isNumber as __isNumber,
|
|
19
|
+
isString as __isString,
|
|
20
|
+
isBool as __isBool,
|
|
21
|
+
isObject as __isObject,
|
|
22
|
+
isArray as __isArray,
|
|
23
|
+
requireBoolean as __requireBoolean,
|
|
24
|
+
normalize as __normalize,
|
|
25
|
+
call as __call,
|
|
26
|
+
boundary as __boundary,
|
|
27
|
+
requireArity as __requireArity,
|
|
28
|
+
withArity as __withArity,
|
|
29
|
+
} from "./.lango/runtime.mjs";
|
|
30
|
+
|
|
31
|
+
export class ExecutionRuntime {
|
|
32
|
+
#definition;
|
|
33
|
+
#loader;
|
|
34
|
+
#database;
|
|
35
|
+
#handlers;
|
|
36
|
+
#identity;
|
|
37
|
+
#queueFactory;
|
|
38
|
+
constructor(definition) {
|
|
39
|
+
__requireArity(arguments.length, [0], "constructor");
|
|
40
|
+
definition = __boundary(definition);
|
|
41
|
+
this.#definition = definition;
|
|
42
|
+
this.#loader = new ModuleLoader();
|
|
43
|
+
this.#database = new Database(__normalize(definition.database), __normalize(definition.root));
|
|
44
|
+
this.#handlers = __normalize(__hostGlobal(["Object"]).create(null));
|
|
45
|
+
this.#identity = null;
|
|
46
|
+
this.#queueFactory = null;
|
|
47
|
+
}
|
|
48
|
+
preload() {
|
|
49
|
+
let pending = __normalize(this.#database.open());
|
|
50
|
+
for (const route of __normalize(this.#definition.targets)) {
|
|
51
|
+
pending = this.#prepare(pending, route);
|
|
52
|
+
}
|
|
53
|
+
return pending.then(__withArity(__bindContexts((ignored) => {
|
|
54
|
+
ignored = __boundary(ignored);
|
|
55
|
+
return __normalize(this.#loader.target(__normalize(this.#definition.identity)));
|
|
56
|
+
return null;
|
|
57
|
+
}), [0], "closure")).then(__withArity(__bindContexts((handler) => {
|
|
58
|
+
handler = __boundary(handler);
|
|
59
|
+
this.#identity = handler;
|
|
60
|
+
if (__requireBoolean(__normalize(this.#definition.queueProvider) !== null)) {
|
|
61
|
+
return __normalize(this.#loader.target(__normalize(this.#definition.queueProvider)));
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
return null;
|
|
65
|
+
}), [0], "closure")).then(__withArity(__bindContexts((factory) => {
|
|
66
|
+
factory = __boundary(factory);
|
|
67
|
+
this.#queueFactory = factory;
|
|
68
|
+
return null;
|
|
69
|
+
return null;
|
|
70
|
+
}), [0], "closure")).catch(__withArity(__bindContexts((error) => {
|
|
71
|
+
error = __boundary(error);
|
|
72
|
+
this.close();
|
|
73
|
+
throw error;
|
|
74
|
+
return null;
|
|
75
|
+
}), [0], "closure"));
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
#prepare(pending, route) {
|
|
79
|
+
__requireArity(arguments.length, [0, 1], "prepare");
|
|
80
|
+
pending = __boundary(pending);
|
|
81
|
+
route = __boundary(route);
|
|
82
|
+
return pending.then(__withArity(__bindContexts((ignored) => {
|
|
83
|
+
ignored = __boundary(ignored);
|
|
84
|
+
return __normalize(this.#loader.target(route));
|
|
85
|
+
return null;
|
|
86
|
+
}), [0], "closure")).then(__withArity(__bindContexts((handler) => {
|
|
87
|
+
handler = __boundary(handler);
|
|
88
|
+
this.#handlers[__normalize(route.path)] = handler;
|
|
89
|
+
return null;
|
|
90
|
+
return null;
|
|
91
|
+
}), [0], "closure"));
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
execute(task) {
|
|
95
|
+
const __contextScope = [];
|
|
96
|
+
try {
|
|
97
|
+
__requireArity(arguments.length, [0], "execute");
|
|
98
|
+
task = __boundary(task);
|
|
99
|
+
__contextScope.push(__contextSet("db", __normalize(this.#database.scope())));
|
|
100
|
+
let route = null;
|
|
101
|
+
for (const target of __normalize(this.#definition.targets)) {
|
|
102
|
+
if (__requireBoolean(__normalize(target.path) === __normalize(task.path))) {
|
|
103
|
+
route = target;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (__requireBoolean(route === null)) {
|
|
107
|
+
if (__requireBoolean(__contextGet("db") !== null)) {
|
|
108
|
+
__normalize(__contextGet("db").finish());
|
|
109
|
+
}
|
|
110
|
+
throw "Unregistered execution path";
|
|
111
|
+
}
|
|
112
|
+
__contextScope.push(__contextSet("job", new JobContext(task, route)));
|
|
113
|
+
let sink = __contextGet("log");
|
|
114
|
+
__contextScope.push(__contextSet("log", new ExecutionLog(sink, __contextGet("job"))));
|
|
115
|
+
return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
|
|
116
|
+
const __contextScope = [];
|
|
117
|
+
try {
|
|
118
|
+
ignored = __boundary(ignored);
|
|
119
|
+
let source = executionSource(__normalize(task.source));
|
|
120
|
+
__normalize(__hostGlobal(["Object"]).freeze(source));
|
|
121
|
+
let target = __normalize(__hostGlobal(["Object"]).freeze({ path: __normalize(route.path), queue: __normalize(route.queue), policy: __normalize(route.policy), reauthSeconds: __normalize(route.reauthSeconds) }));
|
|
122
|
+
let handler = __normalize(this.#identity);
|
|
123
|
+
return __call(handler, { source: source, route: target });
|
|
124
|
+
return null;
|
|
125
|
+
} finally {
|
|
126
|
+
__contextRestore(__contextScope);
|
|
127
|
+
}
|
|
128
|
+
}), [0], "closure")).then(__withArity(__bindContexts((identity) => {
|
|
129
|
+
const __contextScope = [];
|
|
130
|
+
try {
|
|
131
|
+
identity = __boundary(identity);
|
|
132
|
+
__contextScope.push(__contextSet("token", executionToken(identity, __normalize(task.source), route)));
|
|
133
|
+
if (__requireBoolean(__normalize(task.authorize) === true)) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
let sender = null;
|
|
137
|
+
if (__requireBoolean(__normalize(this.#queueFactory) !== null)) {
|
|
138
|
+
let factory = __normalize(this.#queueFactory);
|
|
139
|
+
sender = __call(factory, __normalize(this.#definition.queueProvider.options));
|
|
140
|
+
}
|
|
141
|
+
__contextScope.push(__contextSet("queue", sender));
|
|
142
|
+
let handler = __normalize(this.#handlers[__normalize(route.path)]);
|
|
143
|
+
return __call(handler, __normalize(task.input));
|
|
144
|
+
return null;
|
|
145
|
+
} finally {
|
|
146
|
+
__contextRestore(__contextScope);
|
|
147
|
+
}
|
|
148
|
+
}), [0], "closure")).then(__withArity(__bindContexts((output) => {
|
|
149
|
+
const __contextScope = [];
|
|
150
|
+
try {
|
|
151
|
+
output = __boundary(output);
|
|
152
|
+
let body = __toJson(output);
|
|
153
|
+
if (__requireBoolean(!__requireBoolean(__isString(body)))) {
|
|
154
|
+
throw "Execution output is not JSON serializable";
|
|
155
|
+
}
|
|
156
|
+
if (__requireBoolean(__requireBoolean(__contextGet("db") !== null) && __requireBoolean(__normalize(__contextGet("db").finish())))) {
|
|
157
|
+
throw "UnclosedTransaction";
|
|
158
|
+
}
|
|
159
|
+
return { ok: true, output: __fromJson(body) };
|
|
160
|
+
return null;
|
|
161
|
+
} finally {
|
|
162
|
+
__contextRestore(__contextScope);
|
|
163
|
+
}
|
|
164
|
+
}), [0], "closure")).catch(__withArity(__bindContexts((error) => {
|
|
165
|
+
const __contextScope = [];
|
|
166
|
+
try {
|
|
167
|
+
error = __boundary(error);
|
|
168
|
+
let permanent = __isKindof(error, ExecutionFailure);
|
|
169
|
+
let code = "ExecutionFailed";
|
|
170
|
+
if (__requireBoolean(permanent)) {
|
|
171
|
+
code = __normalize(error.code);
|
|
172
|
+
}
|
|
173
|
+
__normalize(__contextGet("log").err(`code=${code}: ${errorDetails(error)}`));
|
|
174
|
+
return { ok: false, code: code, permanent: permanent, message: errorDetails(error) };
|
|
175
|
+
return null;
|
|
176
|
+
} finally {
|
|
177
|
+
__contextRestore(__contextScope);
|
|
178
|
+
}
|
|
179
|
+
}), [0], "closure")).finally(__bindContexts(() => {
|
|
180
|
+
const __contextScope = [];
|
|
181
|
+
try {
|
|
182
|
+
if (__requireBoolean(__contextGet("db") !== null)) {
|
|
183
|
+
__normalize(__contextGet("db").finish());
|
|
184
|
+
}
|
|
185
|
+
return null;
|
|
186
|
+
} finally {
|
|
187
|
+
__contextRestore(__contextScope);
|
|
188
|
+
}
|
|
189
|
+
}));
|
|
190
|
+
return null;
|
|
191
|
+
} finally {
|
|
192
|
+
__contextRestore(__contextScope);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
close() {
|
|
196
|
+
__normalize(this.#database.close());
|
|
197
|
+
return null;
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=execution_runtime.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execution_runtime.mjs","sources":["execution_runtime.lango"],"sourcesContent":["import \"./module_loader\"\n class ModuleLoader\nimport \"./database\"\n class Database\nimport \"./execution_identity\"\n executionSource(source)\n executionToken(identity, source, route)\nimport \"./execution_failure\"\n class ExecutionFailure\nimport \"./job_context\"\n class JobContext\nimport \"./execution_log\"\n class ExecutionLog\nimport \"./error_details\"\n errorDetails(error)\nimport js global Promise\nimport js global Object\n\nexport class ExecutionRuntime(definition)\n methods\n preload()\n execute(task)\n close()\n\nExecutionRuntime(definition)\n @definition = definition\n @loader = new ModuleLoader()\n @database = new Database(definition.database, definition.root)\n @handlers = Object/create(null)\n @identity = null\n @queueFactory = null\n\nExecutionRuntime:preload()\n pending = @database.open()\n for @definition.targets as route\n pending = prepare(pending, route)\n return chain pending\n then(ignored)\n return @loader.target(@definition.identity)\n then(handler)\n @identity = handler\n if @definition.queueProvider != null\n return @loader.target(@definition.queueProvider)\n return null\n then(factory)\n @queueFactory = factory\n return null\n catch(error)\n close()\n throw error\n\nExecutionRuntime:prepare(pending, route)\n return chain pending\n then(ignored)\n capture route\n return @loader.target(route)\n then(handler)\n capture route\n @handlers[route.path] = handler\n return null\n\nExecutionRuntime:execute(task)\n context db = @database.scope()\n context log\n route = null\n for @definition.targets as target\n if target.path == task.path\n route = target\n if route == null\n if db != null\n db.finish()\n throw \"Unregistered execution path\"\n context job = new JobContext(task, route)\n sink = log\n context log = new ExecutionLog(sink, job)\n return chain Promise/resolve(null)\n then(ignored)\n capture task\n capture route\n source = executionSource(task.source)\n Object/freeze(source)\n target = Object/freeze({\n path: route.path\n queue: route.queue\n policy: route.policy\n reauthSeconds: route.reauthSeconds\n })\n handler = @identity\n return handler({\n source: source\n route: target\n })\n then(identity)\n capture task\n capture route\n context token = executionToken(identity, task.source, route)\n if task.authorize == true\n return null\n sender = null\n if @queueFactory != null\n factory = @queueFactory\n sender = factory(@definition.queueProvider.options)\n context queue = sender\n handler = @handlers[route.path]\n return handler(task.input)\n then(output)\n body = to_json(output)\n if !is_string(body)\n throw \"Execution output is not JSON serializable\"\n if db != null and db.finish()\n throw \"UnclosedTransaction\"\n return {\n ok: true\n output: from_json(body)\n }\n catch(error)\n capture ExecutionFailure\n permanent = is_kindof(error, ExecutionFailure)\n code = \"ExecutionFailed\"\n if permanent\n code = error.code?\n log.err(\"code=${code}: ${errorDetails(error)}\")\n return {\n ok: false\n code: code\n permanent: permanent\n message: errorDetails(error)\n }\n finally\n if db != null\n db.finish()\n\nExecutionRuntime:close()\n @database.close()\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ExecutionRuntime } from "./execution_runtime.mjs";
|
|
2
|
+
import * as threads from "node:worker_threads";
|
|
3
|
+
import {
|
|
4
|
+
hostGlobal as __hostGlobal,
|
|
5
|
+
bindContexts as __bindContexts,
|
|
6
|
+
normalize as __normalize,
|
|
7
|
+
boundary as __boundary,
|
|
8
|
+
requireArity as __requireArity,
|
|
9
|
+
withArity as __withArity,
|
|
10
|
+
} from "./.lango/runtime.mjs";
|
|
11
|
+
|
|
12
|
+
let runtime = new ExecutionRuntime(__normalize(threads.workerData));
|
|
13
|
+
|
|
14
|
+
export let execute = initialize();
|
|
15
|
+
function initialize() {
|
|
16
|
+
__normalize(__hostGlobal(["process"]).setSourceMapsEnabled(true));
|
|
17
|
+
__normalize(__hostGlobal(["process"]).once("exit", __bindContexts(() => {
|
|
18
|
+
__normalize(runtime.close());
|
|
19
|
+
return null;
|
|
20
|
+
})));
|
|
21
|
+
return __normalize(runtime.preload()).then(__withArity(__bindContexts((ignored) => {
|
|
22
|
+
ignored = __boundary(ignored);
|
|
23
|
+
__normalize(threads.parentPort.postMessage({ kind: "ready", threadId: __normalize(threads.threadId) }));
|
|
24
|
+
return workerHandler();
|
|
25
|
+
return null;
|
|
26
|
+
}), [0], "closure"));
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function workerHandler() {
|
|
31
|
+
return run;
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function run(task) {
|
|
36
|
+
__requireArity(arguments.length, [0], "run");
|
|
37
|
+
task = __boundary(task);
|
|
38
|
+
return __normalize(runtime.execute(task));
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=execution_worker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execution_worker.mjs","sources":["execution_worker.lango"],"sourcesContent":["import \"./execution_runtime\"\n class ExecutionRuntime\nimport js \"node:worker_threads\" as threads\nimport js global process\n\nexport global execute\n\nglobal runtime = new ExecutionRuntime(threads.workerData)\nglobal execute = initialize()\n\ninitialize()\n global runtime\n process.setSourceMapsEnabled(true)\n process.once(\"exit\", ()\n global runtime\n runtime.close()\n )\n return chain runtime.preload()\n then(ignored)\n capture threads\n threads/parentPort/postMessage({\n kind: \"ready\"\n threadId: threads.threadId\n })\n return workerHandler()\n\nworkerHandler()\n return run\n\nrun(task)\n global runtime\n return runtime.execute(task)\n"],"names":[],"mappings":";;;;;;;;;;;AAOA;;AACA;AAEA;;;;;;;;;;;;;;;AAgBA;;;;;AAGA;;;;;;;"}
|