@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
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import "./policy"
|
|
2
|
+
policyOptions(spec)
|
|
1
3
|
import js global Number
|
|
2
4
|
import js global Object
|
|
3
5
|
import js global process
|
|
@@ -10,7 +12,9 @@ longpollOptions(spec)
|
|
|
10
12
|
result = {
|
|
11
13
|
kind: "longpoll"
|
|
12
14
|
path: spec.path
|
|
13
|
-
auth:
|
|
15
|
+
auth: null
|
|
16
|
+
policy: null
|
|
17
|
+
reauthSeconds: 0
|
|
14
18
|
waitTimeoutMs: 20000
|
|
15
19
|
retentionMs: 300000
|
|
16
20
|
maxMessages: 1000
|
|
@@ -20,11 +24,13 @@ longpollOptions(spec)
|
|
|
20
24
|
if !Object/hasOwn(result, key) or key == "kind"
|
|
21
25
|
throw "Unknown longpoll option: ${key}"
|
|
22
26
|
result[key] = value
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
selected = policyOptions(result)
|
|
28
|
+
result.policy = selected.name
|
|
29
|
+
result.auth = selected.name
|
|
30
|
+
result.reauthSeconds = selected.reauthSeconds
|
|
31
|
+
result.reauthExplicit = spec.reauthSeconds != null
|
|
32
|
+
result.legacyAuth = selected.legacy
|
|
25
33
|
validPath(result.path)
|
|
26
|
-
if !is_string(result.auth) or result.auth == "" or result.auth.includes(",")
|
|
27
|
-
throw "Longpoll auth must name one permission"
|
|
28
34
|
if result.fallback != null
|
|
29
35
|
validPath(result.fallback)
|
|
30
36
|
limits = {
|
package/src/middleware.lango
CHANGED
|
@@ -3,7 +3,7 @@ import js global RegExp
|
|
|
3
3
|
export middlewareName(name)
|
|
4
4
|
|
|
5
5
|
middlewareName(name)
|
|
6
|
-
reserved = ["routes", "endpoint", "longpoll", "group", "include", "event", "middleware"]
|
|
6
|
+
reserved = ["routes", "endpoint", "longpoll", "job", "group", "include", "event", "middleware"]
|
|
7
7
|
pattern = new RegExp("^[A-Za-z_][A-Za-z0-9_-]*$")
|
|
8
8
|
if !is_string(name) or !pattern.test(name) or reserved.includes(name)
|
|
9
9
|
throw "Invalid or reserved middleware name: ${name}"
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import js global Object
|
|
2
|
+
import js global Promise
|
|
3
|
+
|
|
4
|
+
export class ModuleLoader()
|
|
5
|
+
methods
|
|
6
|
+
target(spec)
|
|
7
|
+
|
|
8
|
+
ModuleLoader()
|
|
9
|
+
@modules = Object/create(null)
|
|
10
|
+
|
|
11
|
+
ModuleLoader:target(spec)
|
|
12
|
+
file = spec.module
|
|
13
|
+
if Object/hasOwn(@modules, file)
|
|
14
|
+
return Promise/resolve(exported(@modules[file], spec.handler))
|
|
15
|
+
return chain import_module(file)
|
|
16
|
+
then(mod)
|
|
17
|
+
capture file
|
|
18
|
+
capture spec
|
|
19
|
+
@modules[file] = mod
|
|
20
|
+
return exported(mod, spec.handler)
|
|
21
|
+
|
|
22
|
+
exported(mod, name)
|
|
23
|
+
if !Object/hasOwn(mod, name) or !is_kindof(mod[name], Function)
|
|
24
|
+
throw "Handler is not exported: ${name}"
|
|
25
|
+
return mod[name]
|
package/src/options.lango
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import "./policy"
|
|
2
|
+
policyRegistry(spec)
|
|
3
|
+
import "./client_identity"
|
|
4
|
+
clientOptions(spec, database, origin)
|
|
5
|
+
import "./origin"
|
|
6
|
+
originOptions(spec)
|
|
1
7
|
import "./longpoll_options"
|
|
2
8
|
redisOptions(spec)
|
|
3
9
|
import "./database_options"
|
|
@@ -14,6 +20,7 @@ export contains(items, value)
|
|
|
14
20
|
export options(root, supplied = null)
|
|
15
21
|
export deployment(directory)
|
|
16
22
|
export attributes(tag, allowed)
|
|
23
|
+
export readDeployment(directory, credentials = true)
|
|
17
24
|
|
|
18
25
|
attributes(tag, allowed)
|
|
19
26
|
for tag.attributes() as key: value
|
|
@@ -44,6 +51,9 @@ options(root, supplied = null)
|
|
|
44
51
|
database: null
|
|
45
52
|
redis: null
|
|
46
53
|
static: null
|
|
54
|
+
origin: null
|
|
55
|
+
client: null
|
|
56
|
+
policies: null
|
|
47
57
|
}
|
|
48
58
|
if supplied != null
|
|
49
59
|
for supplied as key: value
|
|
@@ -79,12 +89,18 @@ options(root, supplied = null)
|
|
|
79
89
|
throw "JWT keys must contain at least 32 characters"
|
|
80
90
|
result.keys = keys
|
|
81
91
|
result.key = null
|
|
92
|
+
result.policies = policyRegistry(result.policies)
|
|
82
93
|
result.database = databaseOptions(result.root, result.database, result.workers)
|
|
83
94
|
result.redis = redisOptions(result.redis)
|
|
84
95
|
result.static = staticOptions(result.root, result.static)
|
|
96
|
+
result.origin = originOptions(result.origin)
|
|
97
|
+
result.client = clientOptions(result.client, result.database, result.origin)
|
|
85
98
|
return result
|
|
86
99
|
|
|
87
100
|
deployment(directory)
|
|
101
|
+
return options(directory, readDeployment(directory))
|
|
102
|
+
|
|
103
|
+
readDeployment(directory, credentials = true)
|
|
88
104
|
file = path/join(directory, "deploy.xml")
|
|
89
105
|
root = xml2obj(fs/readFileSync(file, "utf8"))
|
|
90
106
|
if root.tag() != "deploy"
|
|
@@ -94,10 +110,31 @@ deployment(directory)
|
|
|
94
110
|
seen = []
|
|
95
111
|
for root.children() as tag
|
|
96
112
|
name = tag.tag()
|
|
97
|
-
if seen.includes(name) or tag.children().length != 0 or tag.innerText().trim() != ""
|
|
113
|
+
if seen.includes(name) or (name != "policies" and tag.children().length != 0) or tag.innerText().trim() != ""
|
|
98
114
|
throw "Duplicate or nonempty deployment section: ${name}"
|
|
99
115
|
seen.add(name)
|
|
100
116
|
switch name
|
|
117
|
+
case "policies"
|
|
118
|
+
attributes(tag, ["reauthSeconds"])
|
|
119
|
+
items = []
|
|
120
|
+
for tag.children() as child
|
|
121
|
+
if child.tag() != "policy" or child.children().length != 0 or child.innerText().trim() != ""
|
|
122
|
+
throw "Policies may contain only empty policy elements"
|
|
123
|
+
attributes(child, ["name", "reauthSeconds"])
|
|
124
|
+
items.add(child.attributes())
|
|
125
|
+
settings.policies = {
|
|
126
|
+
reauthSeconds: tag.reauthSeconds
|
|
127
|
+
items: items
|
|
128
|
+
}
|
|
129
|
+
continue
|
|
130
|
+
case "client"
|
|
131
|
+
attributes(tag, ["maxAgeSeconds"])
|
|
132
|
+
settings.client = tag.attributes()
|
|
133
|
+
continue
|
|
134
|
+
case "origin"
|
|
135
|
+
attributes(tag, ["url", "urlEnv", "required"])
|
|
136
|
+
settings.origin = tag.attributes()
|
|
137
|
+
continue
|
|
101
138
|
case "static"
|
|
102
139
|
attributes(tag, ["path", "directory", "index", "fallback", "runtime"])
|
|
103
140
|
settings.static = tag.attributes()
|
|
@@ -118,6 +155,8 @@ deployment(directory)
|
|
|
118
155
|
continue
|
|
119
156
|
case "jwt"
|
|
120
157
|
attributes(tag, ["issuer", "audience", "ttlSeconds", "keyEnv", "keysFile", "activeKeyId"])
|
|
158
|
+
if !credentials
|
|
159
|
+
continue
|
|
121
160
|
else
|
|
122
161
|
throw "Unknown deployment section: ${name}"
|
|
123
162
|
for tag.attributes() as key: value
|
|
@@ -138,7 +177,7 @@ deployment(directory)
|
|
|
138
177
|
settings[key] = value == "true"
|
|
139
178
|
else
|
|
140
179
|
settings[key] = Number(value)
|
|
141
|
-
return
|
|
180
|
+
return settings
|
|
142
181
|
|
|
143
182
|
contains(items, value)
|
|
144
183
|
return items.includes(value)
|
package/src/origin.lango
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import js global Object
|
|
2
|
+
import js global URL
|
|
3
|
+
import js global process
|
|
4
|
+
|
|
5
|
+
export originOptions(spec)
|
|
6
|
+
export originAllowed(spec, request)
|
|
7
|
+
|
|
8
|
+
originOptions(spec)
|
|
9
|
+
if spec == null
|
|
10
|
+
return null
|
|
11
|
+
if !is_object(spec) or is_array(spec)
|
|
12
|
+
throw "Origin configuration must be an object"
|
|
13
|
+
for Object/keys(spec) as name
|
|
14
|
+
if name != "url" and name != "urlEnv" and name != "required"
|
|
15
|
+
throw "Unknown origin option: ${name}"
|
|
16
|
+
if spec.url != null and spec.urlEnv != null
|
|
17
|
+
throw "Origin requires either url or urlEnv, not both"
|
|
18
|
+
url = spec.url
|
|
19
|
+
if spec.urlEnv != null
|
|
20
|
+
if !is_string(spec.urlEnv) or spec.urlEnv == ""
|
|
21
|
+
throw "Origin urlEnv must name an environment variable"
|
|
22
|
+
envName = spec.urlEnv
|
|
23
|
+
url = process.env[envName]
|
|
24
|
+
if !is_string(url) or url == ""
|
|
25
|
+
throw "Origin URL is required"
|
|
26
|
+
parsed = null
|
|
27
|
+
try
|
|
28
|
+
parsed = new URL(url)
|
|
29
|
+
catch error
|
|
30
|
+
throw "Origin requires an exact HTTP or HTTPS origin"
|
|
31
|
+
if (parsed.protocol != "https:" and parsed.protocol != "http:") or parsed.origin != url
|
|
32
|
+
throw "Origin requires an exact HTTP or HTTPS origin without credentials, path, query or fragment"
|
|
33
|
+
required = spec.required
|
|
34
|
+
if required == null or required == "true"
|
|
35
|
+
required = true
|
|
36
|
+
if required == "false"
|
|
37
|
+
required = false
|
|
38
|
+
if !is_bool(required)
|
|
39
|
+
throw "Origin required must be true or false"
|
|
40
|
+
return {
|
|
41
|
+
url: url
|
|
42
|
+
required: required
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
originAllowed(spec, request)
|
|
46
|
+
if spec == null
|
|
47
|
+
return true
|
|
48
|
+
headers = request.headers
|
|
49
|
+
value = headers.origin
|
|
50
|
+
if value == null
|
|
51
|
+
if spec.required
|
|
52
|
+
return false
|
|
53
|
+
if value != null and value != spec.url
|
|
54
|
+
return false
|
|
55
|
+
site = headers["sec-fetch-site"]
|
|
56
|
+
return site == null or site == "same-origin" or site == "none"
|
package/src/policy.lango
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import js global Number
|
|
2
|
+
import js global Object
|
|
3
|
+
|
|
4
|
+
export policyOptions(spec)
|
|
5
|
+
export policyRegistry(spec)
|
|
6
|
+
export resolvePolicies(routes, registry)
|
|
7
|
+
|
|
8
|
+
policyOptions(spec)
|
|
9
|
+
name = spec.policy
|
|
10
|
+
if name != null and spec.auth != null and name != spec.auth
|
|
11
|
+
throw "Endpoint policy and legacy auth cannot disagree"
|
|
12
|
+
legacy = name == null
|
|
13
|
+
if name == null
|
|
14
|
+
name = spec.auth
|
|
15
|
+
if name == null
|
|
16
|
+
name = "login"
|
|
17
|
+
if !is_string(name) or name.trim() == "" or name.includes(",")
|
|
18
|
+
throw "Endpoint policy must name one permission"
|
|
19
|
+
seconds = spec.reauthSeconds
|
|
20
|
+
if seconds == null
|
|
21
|
+
seconds = 0
|
|
22
|
+
if is_string(seconds)
|
|
23
|
+
if seconds.trim() == ""
|
|
24
|
+
throw "reauthSeconds cannot be empty"
|
|
25
|
+
seconds = Number(seconds)
|
|
26
|
+
if !Number/isSafeInteger(seconds) or seconds < 0 or seconds > 31536000
|
|
27
|
+
throw "reauthSeconds must be an integer between 0 and 31536000"
|
|
28
|
+
if seconds > 0 and name == "anonymous"
|
|
29
|
+
throw "Anonymous policy cannot require password freshness"
|
|
30
|
+
return {
|
|
31
|
+
name: name
|
|
32
|
+
reauthSeconds: seconds
|
|
33
|
+
explicit: spec.reauthSeconds != null
|
|
34
|
+
legacy: legacy
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
policyRegistry(spec)
|
|
38
|
+
if spec == null
|
|
39
|
+
return null
|
|
40
|
+
if !is_object(spec) or is_array(spec)
|
|
41
|
+
throw "Policies must be a map"
|
|
42
|
+
for spec as key: value
|
|
43
|
+
if key != "reauthSeconds" and key != "items"
|
|
44
|
+
throw "Unknown policies option: ${key}"
|
|
45
|
+
base = policyOptions({
|
|
46
|
+
policy: "login"
|
|
47
|
+
reauthSeconds: spec.reauthSeconds
|
|
48
|
+
})
|
|
49
|
+
items = spec.items
|
|
50
|
+
if !is_array(items)
|
|
51
|
+
throw "Policies require an items array"
|
|
52
|
+
result = Object/create(null)
|
|
53
|
+
for items as item
|
|
54
|
+
if !is_object(item) or is_array(item)
|
|
55
|
+
throw "Policy must be a map"
|
|
56
|
+
for item as key: value
|
|
57
|
+
if key != "name" and key != "reauthSeconds"
|
|
58
|
+
throw "Unknown policy option: ${key}"
|
|
59
|
+
name = item.name
|
|
60
|
+
if !is_string(name) or name.trim() != name or name == "" or name == "anonymous" or Object/hasOwn(result, name)
|
|
61
|
+
throw "Invalid or duplicate policy name"
|
|
62
|
+
seconds = item.reauthSeconds
|
|
63
|
+
if seconds == null
|
|
64
|
+
seconds = base.reauthSeconds
|
|
65
|
+
selected = policyOptions({
|
|
66
|
+
policy: name
|
|
67
|
+
reauthSeconds: seconds
|
|
68
|
+
})
|
|
69
|
+
result[name] = selected.reauthSeconds
|
|
70
|
+
return result
|
|
71
|
+
|
|
72
|
+
resolvePolicies(routes, registry)
|
|
73
|
+
if registry == null
|
|
74
|
+
return routes
|
|
75
|
+
for routes as route
|
|
76
|
+
if route.policy != "anonymous"
|
|
77
|
+
if !Object/hasOwn(registry, route.policy)
|
|
78
|
+
throw "Undefined route policy: ${route.policy}"
|
|
79
|
+
maximum = registry[route.policy]
|
|
80
|
+
if route.reauthExplicit and maximum > 0 and (route.reauthSeconds == 0 or route.reauthSeconds > maximum)
|
|
81
|
+
throw "Route cannot weaken policy freshness: ${route.path}"
|
|
82
|
+
if !route.reauthExplicit
|
|
83
|
+
route.reauthSeconds = maximum
|
|
84
|
+
return routes
|
|
@@ -13,6 +13,7 @@ export class PostgresAdapter(mod, settings, root)
|
|
|
13
13
|
parameterStyle
|
|
14
14
|
methods
|
|
15
15
|
execute(sql, parameters)
|
|
16
|
+
invoke(spec, input)
|
|
16
17
|
begin()
|
|
17
18
|
commit()
|
|
18
19
|
rollback()
|
|
@@ -34,6 +35,7 @@ PostgresAdapter(mod, settings, root)
|
|
|
34
35
|
entry = resolver.resolve("@lango-project/lango-server")
|
|
35
36
|
@worker = Reflect/construct(mod.Worker, [path/join(path/dirname(entry), "postgres_worker.mjs"), {
|
|
36
37
|
workerData: {
|
|
38
|
+
root: root
|
|
37
39
|
settings: settings
|
|
38
40
|
port: channel.port2
|
|
39
41
|
signal: @signal
|
|
@@ -73,6 +75,8 @@ PostgresAdapter:exchange(task, timeout)
|
|
|
73
75
|
if result.failed
|
|
74
76
|
@broken = result.dead
|
|
75
77
|
error = new Error("PostgreSQL query failed")
|
|
78
|
+
if result.detail != null
|
|
79
|
+
error = new Error("PostgreSQL extension failed: ${result.detail}")
|
|
76
80
|
error.code = result.code
|
|
77
81
|
error.position = result.position
|
|
78
82
|
error.transactionUnusable = @state == "failed"
|
|
@@ -86,6 +90,14 @@ PostgresAdapter:execute(sql, parameters)
|
|
|
86
90
|
parameters: parameters
|
|
87
91
|
}, @timeout)
|
|
88
92
|
|
|
93
|
+
PostgresAdapter:invoke(spec, input)
|
|
94
|
+
result = exchange({
|
|
95
|
+
action: "invoke"
|
|
96
|
+
spec: spec
|
|
97
|
+
input: input
|
|
98
|
+
}, @timeout)
|
|
99
|
+
return result.value
|
|
100
|
+
|
|
89
101
|
PostgresAdapter:begin()
|
|
90
102
|
if @depth == 0 and @state == null
|
|
91
103
|
execute("BEGIN", [])
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import js "pg" as pg
|
|
2
2
|
import js "node:worker_threads" as threads
|
|
3
|
+
import js "node:module" as modules
|
|
4
|
+
import js "node:path" as path
|
|
5
|
+
import "./module_loader"
|
|
6
|
+
class ModuleLoader
|
|
7
|
+
import "./error_details"
|
|
8
|
+
errorDetails(error)
|
|
3
9
|
import js global Atomics
|
|
4
10
|
import js global Math
|
|
5
11
|
import js global Number
|
|
@@ -7,6 +13,7 @@ import js global Promise
|
|
|
7
13
|
|
|
8
14
|
|
|
9
15
|
global state = initialState()
|
|
16
|
+
global loader = new ModuleLoader()
|
|
10
17
|
global installed = install()
|
|
11
18
|
|
|
12
19
|
install()
|
|
@@ -20,12 +27,19 @@ install()
|
|
|
20
27
|
reply(result)
|
|
21
28
|
return null
|
|
22
29
|
catch(error)
|
|
30
|
+
capture task
|
|
23
31
|
global state
|
|
24
|
-
|
|
32
|
+
sqlError = is_string(error.code) and error.code.length == 5
|
|
33
|
+
if state.transaction != null and (task.action != "invoke" or sqlError or state.dead)
|
|
25
34
|
state.transaction = "failed"
|
|
26
|
-
if
|
|
35
|
+
if task.action != "invoke" and !sqlError
|
|
36
|
+
state.dead = true
|
|
37
|
+
if sqlError and error.code.startsWith("08")
|
|
27
38
|
state.dead = true
|
|
28
39
|
position = null
|
|
40
|
+
detail = null
|
|
41
|
+
if task.action == "invoke" and !sqlError
|
|
42
|
+
detail = errorDetails(error)
|
|
29
43
|
if error.position != null
|
|
30
44
|
position = Number/parseInt(error.position, 10)
|
|
31
45
|
reply({
|
|
@@ -33,6 +47,7 @@ install()
|
|
|
33
47
|
dead: state.dead
|
|
34
48
|
code: error.code
|
|
35
49
|
position: position
|
|
50
|
+
detail: detail
|
|
36
51
|
state: state.transaction
|
|
37
52
|
})
|
|
38
53
|
return null
|
|
@@ -80,6 +95,8 @@ execute(task)
|
|
|
80
95
|
case "MERGE"
|
|
81
96
|
changes = result.rowCount
|
|
82
97
|
return success(result.rows, changes)
|
|
98
|
+
case "invoke"
|
|
99
|
+
return invoke(task)
|
|
83
100
|
case "close"
|
|
84
101
|
return chain state.client.end()
|
|
85
102
|
then(ignored)
|
|
@@ -87,7 +104,40 @@ execute(task)
|
|
|
87
104
|
else
|
|
88
105
|
throw "Unknown PostgreSQL operation"
|
|
89
106
|
|
|
90
|
-
|
|
107
|
+
invoke(task)
|
|
108
|
+
global state
|
|
109
|
+
global loader
|
|
110
|
+
spec = task.spec
|
|
111
|
+
if spec == null or !is_string(spec.module) or spec.module.startsWith("node:") or !is_string(spec.handler)
|
|
112
|
+
throw "Database extension requires an application module and exported handler"
|
|
113
|
+
resolver = modules/createRequire(path/join(threads.workerData.root, "package.json"))
|
|
114
|
+
file = resolver.resolve(spec.module)
|
|
115
|
+
if !path/isAbsolute(file)
|
|
116
|
+
throw "Database extension must resolve to an application file"
|
|
117
|
+
return chain loader.target({
|
|
118
|
+
module: file
|
|
119
|
+
handler: spec.handler
|
|
120
|
+
})
|
|
121
|
+
then(handler)
|
|
122
|
+
capture task
|
|
123
|
+
global state
|
|
124
|
+
client = state.client
|
|
125
|
+
executeSql = (sql, parameters)
|
|
126
|
+
capture client
|
|
127
|
+
return chain client.query(sql, parameters)
|
|
128
|
+
catch(error)
|
|
129
|
+
global state
|
|
130
|
+
if state.transaction != null
|
|
131
|
+
state.transaction = "failed"
|
|
132
|
+
throw error
|
|
133
|
+
adapter = {
|
|
134
|
+
executeSql: executeSql
|
|
135
|
+
}
|
|
136
|
+
return handler(adapter, task.input)
|
|
137
|
+
then(value)
|
|
138
|
+
return success([], null, value)
|
|
139
|
+
|
|
140
|
+
success(rows, changes, value = null)
|
|
91
141
|
global state
|
|
92
142
|
return {
|
|
93
143
|
failed: false
|
|
@@ -96,6 +146,7 @@ success(rows, changes)
|
|
|
96
146
|
rows: rows
|
|
97
147
|
changes: changes
|
|
98
148
|
lastInsertRowid: null
|
|
149
|
+
value: value
|
|
99
150
|
}
|
|
100
151
|
|
|
101
152
|
reply(result)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import js "node:module" as modules
|
|
2
|
+
import js "node:path" as path
|
|
3
|
+
|
|
4
|
+
export queueProvider(root, spec)
|
|
5
|
+
|
|
6
|
+
queueProvider(root, spec)
|
|
7
|
+
if spec == null
|
|
8
|
+
return null
|
|
9
|
+
if !is_object(spec) or is_array(spec)
|
|
10
|
+
throw "Queue provider must be a map"
|
|
11
|
+
allowed = ["module", "handler", "options"]
|
|
12
|
+
for spec as key: value
|
|
13
|
+
if !allowed.includes(key)
|
|
14
|
+
throw "Unknown queue provider option: ${key}"
|
|
15
|
+
if !is_string(spec.module) or spec.module == "" or spec.module.startsWith("node:") or !is_string(spec.handler) or spec.handler == ""
|
|
16
|
+
throw "Queue provider requires a module and exported handler"
|
|
17
|
+
settings = spec.options
|
|
18
|
+
if settings == null
|
|
19
|
+
settings = {}
|
|
20
|
+
if !is_object(settings) or is_array(settings)
|
|
21
|
+
throw "Queue provider options must be a map"
|
|
22
|
+
resolver = modules/createRequire(path/join(root, "package.json"))
|
|
23
|
+
file = resolver.resolve(spec.module)
|
|
24
|
+
if !path/isAbsolute(file)
|
|
25
|
+
throw "Queue provider must resolve to an application file"
|
|
26
|
+
return {
|
|
27
|
+
module: file
|
|
28
|
+
handler: spec.handler
|
|
29
|
+
options: from_json(to_json(settings))
|
|
30
|
+
}
|
package/src/response.lango
CHANGED
|
@@ -28,7 +28,7 @@ Response:header(name, value)
|
|
|
28
28
|
if !is_string(name) or !is_string(value)
|
|
29
29
|
throw "Header name and value must be strings"
|
|
30
30
|
name = name.toLowerCase()
|
|
31
|
-
if contains(["x-lango-token", "content-length", "transfer-encoding", "connection", "content-type"], name)
|
|
31
|
+
if contains(["x-lango-auth-change", "x-lango-need-login", "x-lango-token", "content-length", "transfer-encoding", "connection", "content-type"], name)
|
|
32
32
|
throw "Reserved response header: ${name}"
|
|
33
33
|
if name.includes("\r") or name.includes("\n") or value.includes("\r") or value.includes("\n")
|
|
34
34
|
throw "Invalid response header"
|