@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,135 @@
|
|
|
1
|
+
import "./module_loader"
|
|
2
|
+
class ModuleLoader
|
|
3
|
+
import "./database"
|
|
4
|
+
class Database
|
|
5
|
+
import "./execution_identity"
|
|
6
|
+
executionSource(source)
|
|
7
|
+
executionToken(identity, source, route)
|
|
8
|
+
import "./execution_failure"
|
|
9
|
+
class ExecutionFailure
|
|
10
|
+
import "./job_context"
|
|
11
|
+
class JobContext
|
|
12
|
+
import "./execution_log"
|
|
13
|
+
class ExecutionLog
|
|
14
|
+
import "./error_details"
|
|
15
|
+
errorDetails(error)
|
|
16
|
+
import js global Promise
|
|
17
|
+
import js global Object
|
|
18
|
+
|
|
19
|
+
export class ExecutionRuntime(definition)
|
|
20
|
+
methods
|
|
21
|
+
preload()
|
|
22
|
+
execute(task)
|
|
23
|
+
close()
|
|
24
|
+
|
|
25
|
+
ExecutionRuntime(definition)
|
|
26
|
+
@definition = definition
|
|
27
|
+
@loader = new ModuleLoader()
|
|
28
|
+
@database = new Database(definition.database, definition.root)
|
|
29
|
+
@handlers = Object/create(null)
|
|
30
|
+
@identity = null
|
|
31
|
+
@queueFactory = null
|
|
32
|
+
|
|
33
|
+
ExecutionRuntime:preload()
|
|
34
|
+
pending = @database.open()
|
|
35
|
+
for @definition.targets as route
|
|
36
|
+
pending = prepare(pending, route)
|
|
37
|
+
return chain pending
|
|
38
|
+
then(ignored)
|
|
39
|
+
return @loader.target(@definition.identity)
|
|
40
|
+
then(handler)
|
|
41
|
+
@identity = handler
|
|
42
|
+
if @definition.queueProvider != null
|
|
43
|
+
return @loader.target(@definition.queueProvider)
|
|
44
|
+
return null
|
|
45
|
+
then(factory)
|
|
46
|
+
@queueFactory = factory
|
|
47
|
+
return null
|
|
48
|
+
catch(error)
|
|
49
|
+
close()
|
|
50
|
+
throw error
|
|
51
|
+
|
|
52
|
+
ExecutionRuntime:prepare(pending, route)
|
|
53
|
+
return chain pending
|
|
54
|
+
then(ignored)
|
|
55
|
+
capture route
|
|
56
|
+
return @loader.target(route)
|
|
57
|
+
then(handler)
|
|
58
|
+
capture route
|
|
59
|
+
@handlers[route.path] = handler
|
|
60
|
+
return null
|
|
61
|
+
|
|
62
|
+
ExecutionRuntime:execute(task)
|
|
63
|
+
context db = @database.scope()
|
|
64
|
+
context log
|
|
65
|
+
route = null
|
|
66
|
+
for @definition.targets as target
|
|
67
|
+
if target.path == task.path
|
|
68
|
+
route = target
|
|
69
|
+
if route == null
|
|
70
|
+
if db != null
|
|
71
|
+
db.finish()
|
|
72
|
+
throw "Unregistered execution path"
|
|
73
|
+
context job = new JobContext(task, route)
|
|
74
|
+
sink = log
|
|
75
|
+
context log = new ExecutionLog(sink, job)
|
|
76
|
+
return chain Promise/resolve(null)
|
|
77
|
+
then(ignored)
|
|
78
|
+
capture task
|
|
79
|
+
capture route
|
|
80
|
+
source = executionSource(task.source)
|
|
81
|
+
Object/freeze(source)
|
|
82
|
+
target = Object/freeze({
|
|
83
|
+
path: route.path
|
|
84
|
+
queue: route.queue
|
|
85
|
+
policy: route.policy
|
|
86
|
+
reauthSeconds: route.reauthSeconds
|
|
87
|
+
})
|
|
88
|
+
handler = @identity
|
|
89
|
+
return handler({
|
|
90
|
+
source: source
|
|
91
|
+
route: target
|
|
92
|
+
})
|
|
93
|
+
then(identity)
|
|
94
|
+
capture task
|
|
95
|
+
capture route
|
|
96
|
+
context token = executionToken(identity, task.source, route)
|
|
97
|
+
if task.authorize == true
|
|
98
|
+
return null
|
|
99
|
+
sender = null
|
|
100
|
+
if @queueFactory != null
|
|
101
|
+
factory = @queueFactory
|
|
102
|
+
sender = factory(@definition.queueProvider.options)
|
|
103
|
+
context queue = sender
|
|
104
|
+
handler = @handlers[route.path]
|
|
105
|
+
return handler(task.input)
|
|
106
|
+
then(output)
|
|
107
|
+
body = to_json(output)
|
|
108
|
+
if !is_string(body)
|
|
109
|
+
throw "Execution output is not JSON serializable"
|
|
110
|
+
if db != null and db.finish()
|
|
111
|
+
throw "UnclosedTransaction"
|
|
112
|
+
return {
|
|
113
|
+
ok: true
|
|
114
|
+
output: from_json(body)
|
|
115
|
+
}
|
|
116
|
+
catch(error)
|
|
117
|
+
capture ExecutionFailure
|
|
118
|
+
permanent = is_kindof(error, ExecutionFailure)
|
|
119
|
+
code = "ExecutionFailed"
|
|
120
|
+
if permanent
|
|
121
|
+
code = error.code?
|
|
122
|
+
log.err("code=${code}: ${errorDetails(error)}")
|
|
123
|
+
return {
|
|
124
|
+
ok: false
|
|
125
|
+
code: code
|
|
126
|
+
permanent: permanent
|
|
127
|
+
message: errorDetails(error)
|
|
128
|
+
}
|
|
129
|
+
finally
|
|
130
|
+
if db != null
|
|
131
|
+
db.finish()
|
|
132
|
+
|
|
133
|
+
ExecutionRuntime:close()
|
|
134
|
+
@database.close()
|
|
135
|
+
return null
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import "./execution_runtime"
|
|
2
|
+
class ExecutionRuntime
|
|
3
|
+
import js "node:worker_threads" as threads
|
|
4
|
+
import js global process
|
|
5
|
+
|
|
6
|
+
export global execute
|
|
7
|
+
|
|
8
|
+
global runtime = new ExecutionRuntime(threads.workerData)
|
|
9
|
+
global execute = initialize()
|
|
10
|
+
|
|
11
|
+
initialize()
|
|
12
|
+
global runtime
|
|
13
|
+
process.setSourceMapsEnabled(true)
|
|
14
|
+
process.once("exit", ()
|
|
15
|
+
global runtime
|
|
16
|
+
runtime.close()
|
|
17
|
+
)
|
|
18
|
+
return chain runtime.preload()
|
|
19
|
+
then(ignored)
|
|
20
|
+
capture threads
|
|
21
|
+
threads/parentPort/postMessage({
|
|
22
|
+
kind: "ready"
|
|
23
|
+
threadId: threads.threadId
|
|
24
|
+
})
|
|
25
|
+
return workerHandler()
|
|
26
|
+
|
|
27
|
+
workerHandler()
|
|
28
|
+
return run
|
|
29
|
+
|
|
30
|
+
run(task)
|
|
31
|
+
global runtime
|
|
32
|
+
return runtime.execute(task)
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import "./job_configuration"
|
|
2
|
+
readDatabaseConfiguration(directory)
|
|
3
|
+
readJobRoutes(directory)
|
|
4
|
+
import "./execution_identity"
|
|
5
|
+
executionSource(source)
|
|
6
|
+
import "./queue_provider"
|
|
7
|
+
queueProvider(root, spec)
|
|
8
|
+
import "./error_details"
|
|
9
|
+
errorDetails(error)
|
|
10
|
+
import js "node:path" as path
|
|
11
|
+
import js "node:module" as modules
|
|
12
|
+
import js "piscina" as piscina
|
|
13
|
+
import js global Object
|
|
14
|
+
import js global Number
|
|
15
|
+
import js global Promise
|
|
16
|
+
import js global Error
|
|
17
|
+
import js global AbortController
|
|
18
|
+
import js global AbortSignal
|
|
19
|
+
import js global setTimeout
|
|
20
|
+
import js global clearTimeout
|
|
21
|
+
|
|
22
|
+
export class Executor(directory, settings)
|
|
23
|
+
methods
|
|
24
|
+
start()
|
|
25
|
+
execute(task, options = null)
|
|
26
|
+
authorize(task, options = null)
|
|
27
|
+
close()
|
|
28
|
+
|
|
29
|
+
Executor(directory, settings)
|
|
30
|
+
if !is_string(directory) or directory == ""
|
|
31
|
+
throw "Executor requires an application directory"
|
|
32
|
+
if settings == null or !is_object(settings) or is_array(settings)
|
|
33
|
+
throw "Executor requires settings and an identity provider"
|
|
34
|
+
allowed = ["identity", "workers", "maxQueue", "timeoutMs", "startupTimeoutMs", "shutdownTimeoutMs", "queueProvider"]
|
|
35
|
+
for settings as key: value
|
|
36
|
+
if !allowed.includes(key)
|
|
37
|
+
throw "Unknown executor option: ${key}"
|
|
38
|
+
root = path/resolve(directory)
|
|
39
|
+
@definition = {
|
|
40
|
+
root: root
|
|
41
|
+
database: readDatabaseConfiguration(root)
|
|
42
|
+
targets: readJobRoutes(root)
|
|
43
|
+
identity: provider(root, settings.identity)
|
|
44
|
+
queueProvider: queueProvider(root, settings.queueProvider)
|
|
45
|
+
}
|
|
46
|
+
@workers = limit(settings.workers, 2, 1, 128, "workers")
|
|
47
|
+
@maxQueue = limit(settings.maxQueue, 64, 0, 2147483647, "maxQueue")
|
|
48
|
+
@timeout = limit(settings.timeoutMs, 900000, 1, 2147483647, "timeoutMs")
|
|
49
|
+
@startupTimeout = limit(settings.startupTimeoutMs, 30000, 1, 2147483647, "startupTimeoutMs")
|
|
50
|
+
@shutdownTimeout = limit(settings.shutdownTimeoutMs, 30000, 1, 2147483647, "shutdownTimeoutMs")
|
|
51
|
+
@state = "new"
|
|
52
|
+
@pool = null
|
|
53
|
+
@closing = null
|
|
54
|
+
@closeError = null
|
|
55
|
+
@ready = Promise/withResolvers()
|
|
56
|
+
@readyIds = []
|
|
57
|
+
|
|
58
|
+
provider(root, spec)
|
|
59
|
+
if spec == null or !is_object(spec) or is_array(spec)
|
|
60
|
+
throw "Executor identity provider requires module and handler"
|
|
61
|
+
for spec as key: value
|
|
62
|
+
if key != "module" and key != "handler"
|
|
63
|
+
throw "Unknown identity provider option: ${key}"
|
|
64
|
+
if !is_string(spec.module) or spec.module == "" or spec.module.startsWith("node:") or !is_string(spec.handler) or spec.handler == ""
|
|
65
|
+
throw "Executor identity provider requires module and handler"
|
|
66
|
+
resolver = modules/createRequire(path/join(root, "package.json"))
|
|
67
|
+
file = resolver.resolve(spec.module)
|
|
68
|
+
if !path/isAbsolute(file)
|
|
69
|
+
throw "Identity provider must resolve to an application module"
|
|
70
|
+
return {
|
|
71
|
+
module: file
|
|
72
|
+
handler: spec.handler
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
limit(value, fallback, minimum, maximum, name)
|
|
76
|
+
if value == null
|
|
77
|
+
return fallback
|
|
78
|
+
if !Number/isSafeInteger(value) or value < minimum or value > maximum
|
|
79
|
+
throw "Invalid executor option: ${name}"
|
|
80
|
+
return value
|
|
81
|
+
|
|
82
|
+
Executor:start()
|
|
83
|
+
if @state != "new"
|
|
84
|
+
return Promise/reject("Executor can only be started once")
|
|
85
|
+
@state = "starting"
|
|
86
|
+
try
|
|
87
|
+
resolver = modules/createRequire(path/join(@definition.root, "package.json"))
|
|
88
|
+
entry = resolver.resolve("@lango-project/lango-server")
|
|
89
|
+
@pool = new piscina/Piscina({
|
|
90
|
+
filename: path/join(path/dirname(entry), "execution_worker.mjs")
|
|
91
|
+
name: "execute"
|
|
92
|
+
minThreads: @workers
|
|
93
|
+
maxThreads: @workers
|
|
94
|
+
maxQueue: @maxQueue
|
|
95
|
+
concurrentTasksPerWorker: 1
|
|
96
|
+
closeTimeout: @shutdownTimeout
|
|
97
|
+
workerData: @definition
|
|
98
|
+
})
|
|
99
|
+
pool = @pool
|
|
100
|
+
pool.on("message", (message)
|
|
101
|
+
if message.kind == "ready" and !@readyIds.includes(message.threadId)
|
|
102
|
+
@readyIds.add(message.threadId)
|
|
103
|
+
if @readyIds.length == @workers
|
|
104
|
+
@ready.resolve(null)
|
|
105
|
+
)
|
|
106
|
+
pool.on("error", (error)
|
|
107
|
+
@ready.reject(error)
|
|
108
|
+
if @state == "closing"
|
|
109
|
+
@closeError = error
|
|
110
|
+
context log
|
|
111
|
+
log.err("Execution worker error: ${errorDetails(error)}")
|
|
112
|
+
)
|
|
113
|
+
catch error
|
|
114
|
+
@state = "closed"
|
|
115
|
+
return Promise/reject(error)
|
|
116
|
+
expire = ()
|
|
117
|
+
@ready.reject("Executor startup timed out")
|
|
118
|
+
timer = setTimeout(expire, @startupTimeout)
|
|
119
|
+
return chain @ready.promise
|
|
120
|
+
then(ignored)
|
|
121
|
+
if @state != "starting"
|
|
122
|
+
throw "Executor closed during startup"
|
|
123
|
+
@state = "running"
|
|
124
|
+
return null
|
|
125
|
+
catch(error)
|
|
126
|
+
return chain destroy()
|
|
127
|
+
then(ignored)
|
|
128
|
+
capture error
|
|
129
|
+
@state = "closed"
|
|
130
|
+
throw error
|
|
131
|
+
finally
|
|
132
|
+
capture timer
|
|
133
|
+
clearTimeout(timer)
|
|
134
|
+
|
|
135
|
+
Executor:execute(task, options = null)
|
|
136
|
+
return run(task, options, false)
|
|
137
|
+
|
|
138
|
+
Executor:authorize(task, options = null)
|
|
139
|
+
return run(task, options, true)
|
|
140
|
+
|
|
141
|
+
Executor:run(task, options, authorize)
|
|
142
|
+
if @state != "running"
|
|
143
|
+
return Promise/reject("Executor is not running")
|
|
144
|
+
try
|
|
145
|
+
task = validateTask(task, @definition.targets)
|
|
146
|
+
task.authorize = authorize
|
|
147
|
+
options = executionOptions(options, @timeout)
|
|
148
|
+
catch error
|
|
149
|
+
return Promise/reject(error)
|
|
150
|
+
controller = new AbortController()
|
|
151
|
+
status = {
|
|
152
|
+
expired: false
|
|
153
|
+
}
|
|
154
|
+
signal = options.signal
|
|
155
|
+
abort = ()
|
|
156
|
+
capture controller
|
|
157
|
+
controller.abort()
|
|
158
|
+
if signal != null
|
|
159
|
+
signal.addEventListener("abort", abort, {
|
|
160
|
+
once: true
|
|
161
|
+
})
|
|
162
|
+
if signal.aborted
|
|
163
|
+
controller.abort()
|
|
164
|
+
expire = ()
|
|
165
|
+
capture controller
|
|
166
|
+
capture status
|
|
167
|
+
if status.expired
|
|
168
|
+
return null
|
|
169
|
+
status.expired = true
|
|
170
|
+
controller.abort()
|
|
171
|
+
timer = setTimeout(expire, options.timeoutMs)
|
|
172
|
+
return chain @pool.run(task, {
|
|
173
|
+
signal: controller.signal
|
|
174
|
+
})
|
|
175
|
+
then(result)
|
|
176
|
+
if !result.ok
|
|
177
|
+
error = new Error(result.message)
|
|
178
|
+
error.code = result.code
|
|
179
|
+
error.permanent = result.permanent
|
|
180
|
+
error.logged = true
|
|
181
|
+
throw error
|
|
182
|
+
return result.output
|
|
183
|
+
catch(error)
|
|
184
|
+
capture status
|
|
185
|
+
capture task
|
|
186
|
+
capture signal
|
|
187
|
+
if status.expired
|
|
188
|
+
error = new Error("Execution attempt timed out")
|
|
189
|
+
error.code = "ExecutionTimeout"
|
|
190
|
+
error.permanent = false
|
|
191
|
+
else
|
|
192
|
+
if signal != null and signal.aborted
|
|
193
|
+
error = new Error("Execution attempt was aborted")
|
|
194
|
+
error.code = "ExecutionAborted"
|
|
195
|
+
error.permanent = false
|
|
196
|
+
if !is_object(error)
|
|
197
|
+
error = new Error("${error}")
|
|
198
|
+
if error.permanent != true
|
|
199
|
+
error.permanent = false
|
|
200
|
+
context log
|
|
201
|
+
if error.logged != true
|
|
202
|
+
source = task.source
|
|
203
|
+
log.err("[jobId=${task.id}] path=${task.path} attempt=${task.attempt} clientId=${source.clientId} userId=${source.userId} identity=${source.identity}: ${errorDetails(error)}")
|
|
204
|
+
throw error
|
|
205
|
+
finally
|
|
206
|
+
capture timer
|
|
207
|
+
capture signal
|
|
208
|
+
capture abort
|
|
209
|
+
clearTimeout(timer)
|
|
210
|
+
if signal != null
|
|
211
|
+
signal.removeEventListener("abort", abort)
|
|
212
|
+
|
|
213
|
+
executionOptions(options, timeout)
|
|
214
|
+
if options == null
|
|
215
|
+
options = {}
|
|
216
|
+
if !is_object(options) or is_array(options)
|
|
217
|
+
throw "Execution options must be a map"
|
|
218
|
+
for options as key: value
|
|
219
|
+
if key != "signal" and key != "timeoutMs"
|
|
220
|
+
throw "Unknown execution option: ${key}"
|
|
221
|
+
if options.signal != null and !is_kindof(options.signal, AbortSignal)
|
|
222
|
+
throw "Execution signal must be an AbortSignal"
|
|
223
|
+
return {
|
|
224
|
+
signal: options.signal
|
|
225
|
+
timeoutMs: limit(options.timeoutMs, timeout, 1, 2147483647, "timeoutMs")
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
validateTask(task, targets)
|
|
229
|
+
if task == null or !is_object(task) or is_array(task)
|
|
230
|
+
throw "Execution task must be a map"
|
|
231
|
+
allowed = ["id", "path", "input", "source", "attempt"]
|
|
232
|
+
for task as key: value
|
|
233
|
+
if !allowed.includes(key)
|
|
234
|
+
throw "Unknown execution task option: ${key}"
|
|
235
|
+
if !is_string(task.id) or task.id == "" or task.id.length > 200
|
|
236
|
+
throw "Execution task requires a job id"
|
|
237
|
+
found = false
|
|
238
|
+
for targets as route
|
|
239
|
+
if route.path == task.path
|
|
240
|
+
found = true
|
|
241
|
+
if !found
|
|
242
|
+
throw "Unregistered execution path"
|
|
243
|
+
if task.input == null or !is_object(task.input) or is_array(task.input)
|
|
244
|
+
throw "Execution input must be a JSON map"
|
|
245
|
+
return {
|
|
246
|
+
id: task.id
|
|
247
|
+
path: task.path
|
|
248
|
+
input: from_json(to_json(task.input))
|
|
249
|
+
source: executionSource(task.source)
|
|
250
|
+
attempt: limit(task.attempt, 1, 1, 2147483647, "attempt")
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
Executor:close()
|
|
254
|
+
if @closing != null
|
|
255
|
+
return @closing
|
|
256
|
+
if @state == "closed" or @state == "new"
|
|
257
|
+
@state = "closed"
|
|
258
|
+
return Promise/resolve(null)
|
|
259
|
+
state = @state
|
|
260
|
+
@state = "closing"
|
|
261
|
+
if state == "starting"
|
|
262
|
+
@ready.reject("Executor closed during startup")
|
|
263
|
+
@closing = destroy()
|
|
264
|
+
else
|
|
265
|
+
@closing = drain()
|
|
266
|
+
return @closing
|
|
267
|
+
|
|
268
|
+
Executor:drain()
|
|
269
|
+
return chain @pool.close()
|
|
270
|
+
then(ignored)
|
|
271
|
+
if @closeError != null
|
|
272
|
+
throw @closeError
|
|
273
|
+
return null
|
|
274
|
+
finally
|
|
275
|
+
return destroy()
|
|
276
|
+
finally
|
|
277
|
+
@state = "closed"
|
|
278
|
+
|
|
279
|
+
Executor:destroy()
|
|
280
|
+
if @pool == null
|
|
281
|
+
return Promise/resolve(null)
|
|
282
|
+
pool = @pool
|
|
283
|
+
@pool = null
|
|
284
|
+
return pool.destroy()
|
package/src/group.lango
CHANGED
|
@@ -1,54 +1,60 @@
|
|
|
1
1
|
import js global Object
|
|
2
2
|
|
|
3
|
-
export class Group(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null)
|
|
3
|
+
export class Group(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)
|
|
4
4
|
methods
|
|
5
5
|
endpoint(spec)
|
|
6
|
+
job(spec)
|
|
6
7
|
longpoll(spec)
|
|
7
8
|
group(spec)
|
|
8
9
|
include(spec)
|
|
9
10
|
|
|
10
|
-
Group(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null)
|
|
11
|
+
Group(routes, spec, prefix = "", inheritedModule = null, inheritedAuth = null, inheritedPolicy = null, inheritedReauthSeconds = null)
|
|
11
12
|
if !is_string(spec.path) or !spec.path.startsWith("/") or spec.path.endsWith("/") or spec.path.includes("*")
|
|
12
13
|
throw "Group requires a path without a trailing slash or wildcard"
|
|
13
14
|
@routes = routes
|
|
14
15
|
@prefix = "${prefix}${spec.path}"
|
|
15
16
|
@module = spec.module
|
|
16
17
|
@auth = spec.auth
|
|
18
|
+
@policy = spec.policy
|
|
19
|
+
@reauthSeconds = spec.reauthSeconds
|
|
17
20
|
if @module == null
|
|
18
21
|
@module = inheritedModule
|
|
19
|
-
if @auth == null
|
|
22
|
+
if @auth == null and @policy == null
|
|
20
23
|
@auth = inheritedAuth
|
|
24
|
+
@policy = inheritedPolicy
|
|
25
|
+
if @reauthSeconds == null
|
|
26
|
+
@reauthSeconds = inheritedReauthSeconds
|
|
21
27
|
|
|
22
28
|
Group:endpoint(spec)
|
|
23
29
|
if !is_string(spec.path) or !spec.path.startsWith("/")
|
|
24
30
|
throw "Endpoint path must begin with /"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
})
|
|
31
|
+
item = merged(spec)
|
|
32
|
+
item.path = "${@prefix}${spec.path}"
|
|
33
|
+
if item.module == null
|
|
34
|
+
item.module = @module
|
|
35
|
+
@routes.endpoint(item)
|
|
38
36
|
return @this
|
|
39
37
|
|
|
40
38
|
Group:longpoll(spec)
|
|
41
|
-
item =
|
|
39
|
+
item = merged(spec)
|
|
42
40
|
item.path = "${@prefix}${spec.path}"
|
|
43
|
-
if item.auth == null
|
|
44
|
-
item.auth = @auth
|
|
45
41
|
if item.fallback != null
|
|
46
42
|
item.fallback = "${@prefix}${item.fallback}"
|
|
47
43
|
@routes.longpoll(item)
|
|
48
44
|
return @this
|
|
49
45
|
|
|
46
|
+
Group:job(spec)
|
|
47
|
+
item = merged(spec)
|
|
48
|
+
if !is_string(item.path) or !item.path.startsWith("/")
|
|
49
|
+
throw "Job path must begin with /"
|
|
50
|
+
item.path = "${@prefix}${item.path}"
|
|
51
|
+
if item.module == null
|
|
52
|
+
item.module = @module
|
|
53
|
+
@routes.job(item)
|
|
54
|
+
return @this
|
|
55
|
+
|
|
50
56
|
Group:group(spec)
|
|
51
|
-
return new Group(@routes, spec, @prefix, @module, @auth)
|
|
57
|
+
return new Group(@routes, spec, @prefix, @module, @auth, @policy, @reauthSeconds)
|
|
52
58
|
|
|
53
59
|
Group:include(spec)
|
|
54
60
|
mount = spec.path
|
|
@@ -57,5 +63,14 @@ Group:include(spec)
|
|
|
57
63
|
@routes.include({
|
|
58
64
|
module: spec.module
|
|
59
65
|
path: "${@prefix}${mount}"
|
|
60
|
-
}, @auth)
|
|
66
|
+
}, @auth, @policy, @reauthSeconds)
|
|
61
67
|
return @this
|
|
68
|
+
|
|
69
|
+
Group:merged(spec)
|
|
70
|
+
item = Object/assign({}, spec)
|
|
71
|
+
if item.auth == null and item.policy == null
|
|
72
|
+
item.auth = @auth
|
|
73
|
+
item.policy = @policy
|
|
74
|
+
if item.reauthSeconds == null
|
|
75
|
+
item.reauthSeconds = @reauthSeconds
|
|
76
|
+
return item
|
package/src/http_failure.lango
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import js global RegExp
|
|
2
2
|
|
|
3
|
-
export class HttpFailure(status, code, headers)
|
|
3
|
+
export class HttpFailure(status, code, headers, details = null)
|
|
4
4
|
methods
|
|
5
5
|
result()
|
|
6
6
|
|
|
7
|
-
HttpFailure(status, code, headers)
|
|
7
|
+
HttpFailure(status, code, headers, details = null)
|
|
8
8
|
if !is_number(status) or status % 1 != 0 or status < 400 or status > 599
|
|
9
9
|
throw "Rejected response requires an error status"
|
|
10
10
|
pattern = new RegExp("^[A-Za-z][A-Za-z0-9_]{0,63}$")
|
|
11
11
|
if !is_string(code) or !pattern.test(code)
|
|
12
12
|
throw "Rejected response requires an error code"
|
|
13
|
+
@details = details
|
|
13
14
|
@status = status
|
|
14
15
|
@code = code
|
|
15
16
|
@headers = from_json(to_json(headers))
|
|
@@ -17,10 +18,13 @@ HttpFailure(status, code, headers)
|
|
|
17
18
|
@headers["www-authenticate"] = "Bearer"
|
|
18
19
|
|
|
19
20
|
HttpFailure:result()
|
|
21
|
+
body = {
|
|
22
|
+
error: @code
|
|
23
|
+
}
|
|
24
|
+
if @details != null
|
|
25
|
+
body.needLogin = @details
|
|
20
26
|
return {
|
|
21
27
|
status: @status
|
|
22
28
|
headers: from_json(to_json(@headers))
|
|
23
|
-
body: to_json(
|
|
24
|
-
error: @code
|
|
25
|
-
})
|
|
29
|
+
body: to_json(body)
|
|
26
30
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import "./options"
|
|
2
|
+
readDeployment(directory, credentials = true)
|
|
3
|
+
import "./database_options"
|
|
4
|
+
databaseOptions(root, spec, workers)
|
|
5
|
+
import "./routes"
|
|
6
|
+
class Routes
|
|
7
|
+
import "./policy"
|
|
8
|
+
policyRegistry(spec)
|
|
9
|
+
resolvePolicies(routes, registry)
|
|
10
|
+
import js "node:path" as path
|
|
11
|
+
import js global Number
|
|
12
|
+
|
|
13
|
+
export readDatabaseConfiguration(directory)
|
|
14
|
+
export readJobRoutes(directory)
|
|
15
|
+
|
|
16
|
+
readDatabaseConfiguration(directory)
|
|
17
|
+
root = path/resolve(directory)
|
|
18
|
+
settings = readDeployment(root, false)
|
|
19
|
+
workers = 2
|
|
20
|
+
if settings.workers != null
|
|
21
|
+
workers = Number(settings.workers)
|
|
22
|
+
return databaseOptions(root, settings.database, workers)
|
|
23
|
+
|
|
24
|
+
readJobRoutes(directory)
|
|
25
|
+
root = path/resolve(directory)
|
|
26
|
+
settings = readDeployment(root, false)
|
|
27
|
+
routes = new Routes(root)
|
|
28
|
+
routes.xml(path/join(root, "routes.xml"))
|
|
29
|
+
return resolvePolicies(routes.jobs(), policyRegistry(settings.policies))
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import "./execution_failure"
|
|
2
|
+
class ExecutionFailure
|
|
3
|
+
import js global Object
|
|
4
|
+
|
|
5
|
+
export class JobContext(task, route)
|
|
6
|
+
properties
|
|
7
|
+
id?
|
|
8
|
+
path?
|
|
9
|
+
queue?
|
|
10
|
+
attempt?
|
|
11
|
+
source?
|
|
12
|
+
checking?
|
|
13
|
+
methods
|
|
14
|
+
reject(code)
|
|
15
|
+
|
|
16
|
+
JobContext(task, route)
|
|
17
|
+
@id = task.id
|
|
18
|
+
@path = route.path
|
|
19
|
+
@queue = route.queue
|
|
20
|
+
@attempt = task.attempt
|
|
21
|
+
@source = from_json(to_json(task.source))
|
|
22
|
+
@checking = task.authorize == true
|
|
23
|
+
Object/freeze(@this)
|
|
24
|
+
|
|
25
|
+
JobContext:id?
|
|
26
|
+
return @id
|
|
27
|
+
|
|
28
|
+
JobContext:path?
|
|
29
|
+
return @path
|
|
30
|
+
|
|
31
|
+
JobContext:queue?
|
|
32
|
+
return @queue
|
|
33
|
+
|
|
34
|
+
JobContext:attempt?
|
|
35
|
+
return @attempt
|
|
36
|
+
|
|
37
|
+
JobContext:source?
|
|
38
|
+
return from_json(to_json(@source))
|
|
39
|
+
|
|
40
|
+
JobContext:checking?
|
|
41
|
+
return @checking
|
|
42
|
+
|
|
43
|
+
JobContext:reject(code)
|
|
44
|
+
throw new ExecutionFailure(code)
|
package/src/library.lango
CHANGED
|
@@ -2,9 +2,30 @@ import "./server"
|
|
|
2
2
|
alias ServerClass = class Server
|
|
3
3
|
import "./builder"
|
|
4
4
|
alias BuilderClass = class Builder
|
|
5
|
+
import "./executor"
|
|
6
|
+
alias ExecutorClass = class Executor
|
|
7
|
+
import "./database_action"
|
|
8
|
+
alias databaseAction = withDatabase(directory, action)
|
|
9
|
+
import "./job_configuration"
|
|
10
|
+
alias databaseConfig = readDatabaseConfiguration(directory)
|
|
11
|
+
alias jobRoutes = readJobRoutes(directory)
|
|
5
12
|
|
|
6
13
|
export global Server
|
|
7
14
|
export global Builder
|
|
15
|
+
export global Executor
|
|
16
|
+
export readDatabaseConfiguration(directory)
|
|
17
|
+
export readJobRoutes(directory)
|
|
18
|
+
export withDatabase(directory, action)
|
|
8
19
|
|
|
9
20
|
global Server = ServerClass
|
|
10
21
|
global Builder = BuilderClass
|
|
22
|
+
global Executor = ExecutorClass
|
|
23
|
+
|
|
24
|
+
readDatabaseConfiguration(directory)
|
|
25
|
+
return databaseConfig(directory)
|
|
26
|
+
|
|
27
|
+
readJobRoutes(directory)
|
|
28
|
+
return jobRoutes(directory)
|
|
29
|
+
|
|
30
|
+
withDatabase(directory, action)
|
|
31
|
+
return databaseAction(directory, action)
|