@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.
Files changed (158) hide show
  1. package/README.md +252 -29
  2. package/dist/.lango/runtime.mjs +16 -0
  3. package/dist/body_reader.mjs +15 -14
  4. package/dist/body_reader.mjs.map +1 -1
  5. package/dist/builder.mjs +26 -2
  6. package/dist/builder.mjs.map +1 -1
  7. package/dist/cli/.lango/runtime.mjs +16 -0
  8. package/dist/cli/body_reader.mjs +15 -14
  9. package/dist/cli/body_reader.mjs.map +1 -1
  10. package/dist/cli/client_identity.mjs +250 -0
  11. package/dist/cli/client_identity.mjs.map +1 -0
  12. package/dist/cli/database.mjs +23 -8
  13. package/dist/cli/database.mjs.map +1 -1
  14. package/dist/cli/database_scope.mjs +11 -0
  15. package/dist/cli/database_scope.mjs.map +1 -1
  16. package/dist/cli/endpoint.mjs +11 -7
  17. package/dist/cli/endpoint.mjs.map +1 -1
  18. package/dist/cli/longpoll_broker.mjs +9 -8
  19. package/dist/cli/longpoll_broker.mjs.map +1 -1
  20. package/dist/cli/longpoll_options.mjs +8 -7
  21. package/dist/cli/longpoll_options.mjs.map +1 -1
  22. package/dist/cli/longpoll_session.mjs +11 -10
  23. package/dist/cli/longpoll_session.mjs.map +1 -1
  24. package/dist/cli/middleware.mjs +1 -1
  25. package/dist/cli/middleware.mjs.map +1 -1
  26. package/dist/cli/options.mjs +41 -4
  27. package/dist/cli/options.mjs.map +1 -1
  28. package/dist/cli/origin.mjs +87 -0
  29. package/dist/cli/origin.mjs.map +1 -0
  30. package/dist/cli/policy.mjs +121 -0
  31. package/dist/cli/policy.mjs.map +1 -0
  32. package/dist/cli/postgres_adapter.mjs +15 -3
  33. package/dist/cli/postgres_adapter.mjs.map +1 -1
  34. package/dist/cli/routes.mjs +69 -18
  35. package/dist/cli/routes.mjs.map +1 -1
  36. package/dist/cli/server.mjs +88 -81
  37. package/dist/cli/server.mjs.map +1 -1
  38. package/dist/cli/static_files.mjs +5 -4
  39. package/dist/cli/static_files.mjs.map +1 -1
  40. package/dist/client_identity.mjs +250 -0
  41. package/dist/client_identity.mjs.map +1 -0
  42. package/dist/database.mjs +23 -8
  43. package/dist/database.mjs.map +1 -1
  44. package/dist/database_action.mjs +71 -0
  45. package/dist/database_action.mjs.map +1 -0
  46. package/dist/database_scope.mjs +11 -0
  47. package/dist/database_scope.mjs.map +1 -1
  48. package/dist/endpoint.mjs +11 -7
  49. package/dist/endpoint.mjs.map +1 -1
  50. package/dist/execution_failure.mjs +30 -0
  51. package/dist/execution_failure.mjs.map +1 -0
  52. package/dist/execution_identity.mjs +103 -0
  53. package/dist/execution_identity.mjs.map +1 -0
  54. package/dist/execution_log.mjs +47 -0
  55. package/dist/execution_log.mjs.map +1 -0
  56. package/dist/execution_runtime.mjs +201 -0
  57. package/dist/execution_runtime.mjs.map +1 -0
  58. package/dist/execution_worker.mjs +41 -0
  59. package/dist/execution_worker.mjs.map +1 -0
  60. package/dist/executor.mjs +384 -0
  61. package/dist/executor.mjs.map +1 -0
  62. package/dist/group.mjs +49 -16
  63. package/dist/group.mjs.map +1 -1
  64. package/dist/http_failure.mjs +9 -2
  65. package/dist/http_failure.mjs.map +1 -1
  66. package/dist/job_configuration.mjs +37 -0
  67. package/dist/job_configuration.mjs.map +1 -0
  68. package/dist/job_context.mjs +61 -0
  69. package/dist/job_context.mjs.map +1 -0
  70. package/dist/library.mjs +31 -0
  71. package/dist/library.mjs.map +1 -1
  72. package/dist/longpoll_broker.mjs +9 -8
  73. package/dist/longpoll_broker.mjs.map +1 -1
  74. package/dist/longpoll_options.mjs +8 -7
  75. package/dist/longpoll_options.mjs.map +1 -1
  76. package/dist/longpoll_session.mjs +11 -10
  77. package/dist/longpoll_session.mjs.map +1 -1
  78. package/dist/middleware.mjs +1 -1
  79. package/dist/middleware.mjs.map +1 -1
  80. package/dist/module_loader.mjs +44 -0
  81. package/dist/module_loader.mjs.map +1 -0
  82. package/dist/options.mjs +41 -4
  83. package/dist/options.mjs.map +1 -1
  84. package/dist/origin.mjs +87 -0
  85. package/dist/origin.mjs.map +1 -0
  86. package/dist/policy.mjs +121 -0
  87. package/dist/policy.mjs.map +1 -0
  88. package/dist/postgres_adapter.mjs +15 -3
  89. package/dist/postgres_adapter.mjs.map +1 -1
  90. package/dist/postgres_worker.mjs +82 -19
  91. package/dist/postgres_worker.mjs.map +1 -1
  92. package/dist/queue_provider.mjs +52 -0
  93. package/dist/queue_provider.mjs.map +1 -0
  94. package/dist/response.mjs +1 -1
  95. package/dist/response.mjs.map +1 -1
  96. package/dist/route_info.mjs +1 -1
  97. package/dist/route_info.mjs.map +1 -1
  98. package/dist/routes.mjs +69 -18
  99. package/dist/routes.mjs.map +1 -1
  100. package/dist/runtime.mjs +287 -132
  101. package/dist/runtime.mjs.map +1 -1
  102. package/dist/server.mjs +88 -81
  103. package/dist/server.mjs.map +1 -1
  104. package/dist/server_context.mjs +21 -9
  105. package/dist/server_context.mjs.map +1 -1
  106. package/dist/static_files.mjs +5 -4
  107. package/dist/static_files.mjs.map +1 -1
  108. package/dist/token.mjs +34 -7
  109. package/dist/token.mjs.map +1 -1
  110. package/dist/tokens.mjs +11 -2
  111. package/dist/tokens.mjs.map +1 -1
  112. package/dist/worker.mjs +7 -6
  113. package/dist/worker.mjs.map +1 -1
  114. package/docs/jobs.md +123 -0
  115. package/docs/security.md +420 -0
  116. package/examples/security/README.md +160 -0
  117. package/examples/security/deploy.xml +12 -0
  118. package/examples/security/handlers.lango +610 -0
  119. package/examples/security/package.json +19 -0
  120. package/examples/security/passwords.lango +61 -0
  121. package/examples/security/routes.xml +14 -0
  122. package/examples/security/schema.lango +91 -0
  123. package/examples/security/setup.lango +30 -0
  124. package/package.json +14 -6
  125. package/src/builder.lango +22 -0
  126. package/src/client_identity.lango +184 -0
  127. package/src/database.lango +15 -4
  128. package/src/database_action.lango +30 -0
  129. package/src/database_scope.lango +7 -0
  130. package/src/endpoint.lango +12 -5
  131. package/src/execution_failure.lango +14 -0
  132. package/src/execution_identity.lango +71 -0
  133. package/src/execution_log.lango +28 -0
  134. package/src/execution_runtime.lango +135 -0
  135. package/src/execution_worker.lango +32 -0
  136. package/src/executor.lango +284 -0
  137. package/src/group.lango +36 -21
  138. package/src/http_failure.lango +9 -5
  139. package/src/job_configuration.lango +29 -0
  140. package/src/job_context.lango +44 -0
  141. package/src/library.lango +21 -0
  142. package/src/longpoll_options.lango +11 -5
  143. package/src/middleware.lango +1 -1
  144. package/src/module_loader.lango +25 -0
  145. package/src/options.lango +41 -2
  146. package/src/origin.lango +56 -0
  147. package/src/policy.lango +84 -0
  148. package/src/postgres_adapter.lango +12 -0
  149. package/src/postgres_worker.lango +54 -3
  150. package/src/queue_provider.lango +30 -0
  151. package/src/response.lango +1 -1
  152. package/src/route_info.lango +2 -0
  153. package/src/routes.lango +74 -15
  154. package/src/runtime.lango +173 -94
  155. package/src/server.lango +9 -2
  156. package/src/server_context.lango +10 -2
  157. package/src/token.lango +26 -5
  158. package/src/tokens.lango +10 -3
@@ -1 +1 @@
1
- {"version":3,"file":"static_files.mjs","sources":["static_files.lango"],"sourcesContent":["import \"./trace\"\n trace(message)\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"serve-static\" as files\nimport js global RegExp\nimport js global decodeURIComponent\nimport js global String\n\nexport staticOptions(root, spec)\nexport class StaticFiles(settings)\n methods\n receive(req, res, failed)\n\nstaticOptions(root, spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"static must be a map\"\n allowed = [\"path\", \"directory\", \"index\", \"fallback\", \"runtime\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown static option: ${key}\"\n mount = spec.path\n if mount == null\n mount = \"/\"\n pattern = new RegExp(\"^/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]*$\")\n if !is_string(mount) or !pattern.test(mount)\n throw \"Static path must be an absolute URL prefix\"\n if mount != \"/\" and mount.endsWith(\"/\")\n mount = mount.slice(0, -1)\n if !is_string(spec.directory) or spec.directory.trim() == \"\"\n throw \"Static directory is required\"\n index = spec.index\n if index == null\n index = \"index.html\"\n if !is_string(index) or index == \"\" or index.startsWith(\".\") or index.includes(\"/\") or index.includes(\"\\\\\")\n throw \"Static index must be a file name\"\n fallback = spec.fallback\n if fallback != null and (!is_string(fallback) or fallback == \"\" or fallback.startsWith(\".\") or fallback.includes(\"/\") or fallback.includes(\"\\\\\"))\n throw \"Static fallback must be a file name\"\n runtime = spec.runtime\n if runtime == null or runtime == \"false\"\n runtime = false\n if runtime == \"true\"\n runtime = true\n if !is_bool(runtime)\n throw \"Static runtime must be boolean\"\n return {\n path: mount\n directory: path/resolve(root, spec.directory)\n index: index\n fallback: fallback\n runtime: runtime\n }\n\nStaticFiles(settings)\n @settings = settings\n if !fs/statSync(settings.directory).isDirectory()\n throw \"Static directory must be a directory\"\n if settings.fallback != null and !fs/statSync(path/join(settings.directory, settings.fallback)).isFile()\n throw \"Static fallback must be a file\"\n @serve = files/default(settings.directory, {\n index: settings.index\n dotfiles: \"deny\"\n fallthrough: false\n redirect: false\n })\n @runtime = files/default(settings.directory, {\n index: false\n dotfiles: \"allow\"\n fallthrough: false\n redirect: false\n })\n\nStaticFiles:receive(req, res, failed)\n mount = @settings.path\n url = req.url\n pathname = url.split(\"?\")[0]\n if mount != \"/\" and pathname != mount and !pathname.startsWith(\"${mount}/\")\n failed(404, \"NotFound\")\n return null\n if mount != \"/\"\n req.url = url.slice(mount.length)\n if req.url == \"\" or req.url.startsWith(\"?\")\n req.url = \"/${req.url}\"\n local = req.url.split(\"?\")[0]\n decoded = null\n try\n decoded = decodeURIComponent(local)\n catch error\n failed(400, \"InvalidPath\")\n return null\n if decoded.includes(\"\\\\\") or decoded.includes(String/fromCharCode(0))\n failed(400, \"InvalidPath\")\n return null\n segments = decoded.split(\"/\")\n runtime = false\n for segments as index: segment\n if segment.startsWith(\".\")\n if index == 1 and segment == \".lango\" and @settings.runtime\n runtime = true\n else\n failed(403, \"Forbidden\")\n return null\n done = (error)\n capture req\n capture res\n capture failed\n capture decoded\n failure(req, res, failed, error, decoded)\n serve = @serve\n if runtime\n serve = @runtime\n trace(\"Static ${req.method} ${pathname}: directory=${@settings.directory}; index=${@settings.index}; runtime=${runtime}\")\n serve(req, res, done)\n return null\n\nStaticFiles:failure(req, res, failed, error, pathname = null)\n if res.headersSent\n res.destroy()\n return null\n status = 404\n code = \"NotFound\"\n if error != null\n switch error.status\n case 400\n status = 400\n code = \"InvalidPath\"\n case 403\n status = 403\n code = \"Forbidden\"\n case 416\n status = 416\n code = \"RangeNotSatisfiable\"\n case 404\n status = 404\n else\n status = 500\n code = \"InternalServerError\"\n if status == 404 and pathname != null and @settings.fallback != null and !pathname.startsWith(\"/.lango\") and path/posix/extname(pathname) == \"\" and acceptsHTML(req.headers.accept)\n req.url = \"/${@settings.fallback}\"\n done = (error)\n capture req\n capture res\n capture failed\n failure(req, res, failed, error)\n serve = @serve\n serve(req, res, done)\n return null\n failed(status, code)\n return null\n\nacceptsHTML(value)\n if !is_string(value)\n return false\n zero = new RegExp(\"^q\\\\s*=\\\\s*0(?:\\\\.0*)?$\", \"i\")\n for value.split(\",\") as range\n parts = range.split(\";\")\n media = parts[0]\n type = media.trim().toLowerCase()\n if type == \"text/html\" or type == \"application/xhtml+xml\"\n allowed = true\n for parts.slice(1) as parameter\n if zero.test(parameter.trim())\n allowed = false\n if allowed\n return true\n return false\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2IA;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"static_files.mjs","sources":["static_files.lango"],"sourcesContent":["import \"./trace\"\n trace(message)\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"serve-static\" as files\nimport js global RegExp\nimport js global decodeURIComponent\nimport js global String\n\nexport staticOptions(root, spec)\nexport class StaticFiles(settings)\n methods\n receive(req, res, failed)\n\nstaticOptions(root, spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"static must be a map\"\n allowed = [\"path\", \"directory\", \"index\", \"fallback\", \"runtime\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown static option: ${key}\"\n mount = spec.path\n if mount == null\n mount = \"/\"\n pattern = new RegExp(\"^/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]*$\")\n if !is_string(mount) or !pattern.test(mount)\n throw \"Static path must be an absolute URL prefix\"\n if mount != \"/\" and mount.endsWith(\"/\")\n mount = mount.slice(0, -1)\n if !is_string(spec.directory) or spec.directory.trim() == \"\"\n throw \"Static directory is required\"\n index = spec.index\n if index == null\n index = \"index.html\"\n if !is_string(index) or index == \"\" or index.startsWith(\".\") or index.includes(\"/\") or index.includes(\"\\\\\")\n throw \"Static index must be a file name\"\n fallback = spec.fallback\n if fallback != null and (!is_string(fallback) or fallback == \"\" or fallback.startsWith(\".\") or fallback.includes(\"/\") or fallback.includes(\"\\\\\"))\n throw \"Static fallback must be a file name\"\n runtime = spec.runtime\n if runtime == null or runtime == \"false\"\n runtime = false\n if runtime == \"true\"\n runtime = true\n if !is_bool(runtime)\n throw \"Static runtime must be boolean\"\n return {\n path: mount\n directory: path/resolve(root, spec.directory)\n index: index\n fallback: fallback\n runtime: runtime\n }\n\nStaticFiles(settings)\n @settings = settings\n if !fs/statSync(settings.directory).isDirectory()\n throw \"Static directory must be a directory\"\n if settings.fallback != null and !fs/statSync(path/join(settings.directory, settings.fallback)).isFile()\n throw \"Static fallback must be a file\"\n @serve = files/default(settings.directory, {\n index: settings.index\n dotfiles: \"deny\"\n fallthrough: false\n redirect: false\n })\n @runtime = files/default(settings.directory, {\n index: false\n dotfiles: \"allow\"\n fallthrough: false\n redirect: false\n })\n\nStaticFiles:receive(req, res, failed)\n mount = @settings.path\n url = req.url\n pathname = url.split(\"?\")[0]\n if mount != \"/\" and pathname != mount and !pathname.startsWith(\"${mount}/\")\n failed(404, \"NotFound\")\n return null\n if mount != \"/\"\n req.url = url.slice(mount.length)\n if req.url == \"\" or req.url.startsWith(\"?\")\n req.url = \"/${req.url}\"\n local = req.url.split(\"?\")[0]\n decoded = null\n try\n decoded = decodeURIComponent(local)\n catch error\n failed(400, \"InvalidPath\")\n return null\n if decoded.includes(\"\\\\\") or decoded.includes(String/fromCharCode(0))\n failed(400, \"InvalidPath\")\n return null\n segments = decoded.split(\"/\")\n runtime = false\n for segments as index: segment\n if segment.startsWith(\".\")\n if index == 1 and segment == \".lango\" and @settings.runtime\n runtime = true\n else\n failed(403, \"Forbidden\")\n return null\n done = (error)\n capture req\n capture res\n capture failed\n capture decoded\n failure(req, res, failed, error, decoded)\n serve = @serve\n if runtime\n serve = @runtime\n trace(\"Static ${req.method} ${pathname}: directory=${@settings.directory}; index=${@settings.index}; runtime=${runtime}\")\n serve(req, res, done)\n return null\n\nStaticFiles:failure(req, res, failed, error, pathname = null)\n if res.headersSent\n res.destroy()\n return null\n status = 404\n code = \"NotFound\"\n if error != null\n switch error.status\n case 400\n status = 400\n code = \"InvalidPath\"\n case 403\n status = 403\n code = \"Forbidden\"\n case 416\n status = 416\n code = \"RangeNotSatisfiable\"\n case 404\n status = 404\n else\n status = 500\n code = \"InternalServerError\"\n if status == 404 and pathname != null and @settings.fallback != null and !pathname.startsWith(\"/.lango\") and path/posix/extname(pathname) == \"\" and acceptsHTML(req.headers.accept)\n req.url = \"/${@settings.fallback}\"\n done = (error)\n capture req\n capture res\n capture failed\n failure(req, res, failed, error)\n serve = @serve\n serve(req, res, done)\n return null\n failed(status, code)\n return null\n\nacceptsHTML(value)\n if !is_string(value)\n return false\n zero = new RegExp(\"^q\\\\s*=\\\\s*0(?:\\\\.0*)?$\", \"i\")\n for value.split(\",\") as range\n parts = range.split(\";\")\n media = parts[0]\n type = media.trim().toLowerCase()\n if type == \"text/html\" or type == \"application/xhtml+xml\"\n allowed = true\n for parts.slice(1) as parameter\n if zero.test(parameter.trim())\n allowed = false\n if allowed\n return true\n return false\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2IA;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,250 @@
1
+ import * as crypto from "node:crypto";
2
+ import {
3
+ hostGlobal as __hostGlobal,
4
+ hostGlobalCall as __hostGlobalCall,
5
+ contextGet as __contextGet,
6
+ contextSet as __contextSet,
7
+ contextRestore as __contextRestore,
8
+ isNumber as __isNumber,
9
+ isString as __isString,
10
+ isBool as __isBool,
11
+ isObject as __isObject,
12
+ isArray as __isArray,
13
+ requireBoolean as __requireBoolean,
14
+ normalize as __normalize,
15
+ finite as __finite,
16
+ numericAdd as __numericAdd,
17
+ boundary as __boundary,
18
+ startTrans as __startTrans,
19
+ commitTrans as __commitTrans,
20
+ rollbackTrans as __rollbackTrans,
21
+ transState as __transState,
22
+ query as __query,
23
+ requireArity as __requireArity,
24
+ } from "./.lango/runtime.mjs";
25
+
26
+ export class ClientIdentity {
27
+ #settings;
28
+ #database;
29
+ constructor(settings, database) {
30
+ __requireArity(arguments.length, [0, 1], "constructor");
31
+ settings = __boundary(settings);
32
+ database = __boundary(database);
33
+ this.#settings = settings;
34
+ this.#database = database;
35
+ }
36
+ initialize() {
37
+ const __contextScope = [];
38
+ try {
39
+ __contextScope.push(__contextSet("db", __normalize(this.#database.scope())));
40
+ try {
41
+ __startTrans();
42
+ if (__requireBoolean(__normalize(__contextGet("db").parameterStyle) === "postgresql")) {
43
+ let lock = __query("select pg_advisory_xact_lock(1818324591, 1)", []);
44
+ __normalize(lock.execute());
45
+ }
46
+ let row = __query("create table if not exists _lango_clients (\n client_id text primary key,\n secret_hash text not null unique,\n expires_at bigint not null\n)", []);
47
+ __normalize(row.execute());
48
+ __commitTrans();
49
+ } finally {
50
+ __normalize(__contextGet("db").finish());
51
+ }
52
+ return null;
53
+ return null;
54
+ } finally {
55
+ __contextRestore(__contextScope);
56
+ }
57
+ }
58
+ begin(claims, tokens) {
59
+ const __contextScope = [];
60
+ try {
61
+ __requireArity(arguments.length, [0, 1], "begin");
62
+ claims = __boundary(claims);
63
+ tokens = __boundary(tokens);
64
+ let value = readCookie();
65
+ if (__requireBoolean(__requireBoolean(value === null) && __requireBoolean(claims !== null))) {
66
+ __normalize(__contextGet("response").reject(401, "ClientProofRequired"));
67
+ }
68
+ let hash = null;
69
+ let row = null;
70
+ if (__requireBoolean(value === null)) {
71
+ value = clientSecret();
72
+ claims = __normalize(tokens.create());
73
+ hash = clientDigest(value);
74
+ claims.clientProof = hash;
75
+ return { claims: claims, value: value, hash: hash, fresh: true, restored: false, rotate: false, closed: false };
76
+ }
77
+ hash = clientDigest(value);
78
+ row = __query("select client_id from _lango_clients where secret_hash = :hash and expires_at > :now", ["hash","now"]);
79
+ row.hash = hash;
80
+ row.now = __normalize(__hostGlobal(["Date"]).now());
81
+ __normalize(row.execute());
82
+ if (__requireBoolean(!__requireBoolean(__normalize(row.next())))) {
83
+ __normalize(__contextGet("response").reject(401, "ClientCookieRejected"));
84
+ }
85
+ let restored = claims === null;
86
+ if (__requireBoolean(restored)) {
87
+ claims = __normalize(tokens.create(__normalize(row.client_id)));
88
+ claims.clientProof = hash;
89
+ }
90
+ if (__requireBoolean(__requireBoolean(__normalize(claims.clientId) !== __normalize(row.client_id)) || __requireBoolean(!__requireBoolean(sameProof(__normalize(claims.clientProof), hash))))) {
91
+ __normalize(__contextGet("response").reject(401, "ClientProofRequired"));
92
+ }
93
+ return { claims: claims, value: value, hash: hash, fresh: false, restored: restored, rotate: false, closed: false };
94
+ return null;
95
+ } finally {
96
+ __contextRestore(__contextScope);
97
+ }
98
+ }
99
+ publish(identity, claims, response, tokens) {
100
+ const __contextScope = [];
101
+ try {
102
+ __requireArity(arguments.length, [0, 1, 2, 3], "publish");
103
+ identity = __boundary(identity);
104
+ claims = __boundary(claims);
105
+ response = __boundary(response);
106
+ tokens = __boundary(tokens);
107
+ let value = __normalize(identity.value);
108
+ if (__requireBoolean(__normalize(identity.rotate))) {
109
+ value = clientSecret();
110
+ }
111
+ let hash = clientDigest(value);
112
+ claims.clientProof = hash;
113
+ let encoded = __normalize(tokens.sign(claims));
114
+ __contextScope.push(__contextSet("db", __normalize(this.#database.scope())));
115
+ let row = null;
116
+ try {
117
+ if (__requireBoolean(__normalize(identity.fresh))) {
118
+ row = __query("insert into _lango_clients (client_id, secret_hash, expires_at)\nvalues (:id, :hash, :expires)", ["id","hash","expires"]);
119
+ row.id = __normalize(claims.clientId);
120
+ row.hash = hash;
121
+ row.expires = __numericAdd(__normalize(__hostGlobal(["Date"]).now()), __finite(__normalize(this.#settings.maxAgeSeconds) * 1000));
122
+ __normalize(row.execute());
123
+ } else {
124
+ row = __query("update _lango_clients set secret_hash = :replacement, expires_at = :expires\nwhere client_id = :id and secret_hash = :previous and expires_at > :now\nreturning client_id", ["replacement","expires","id","previous","now"]);
125
+ row.id = __normalize(claims.clientId);
126
+ row.previous = __normalize(identity.hash);
127
+ row.replacement = hash;
128
+ row.now = __normalize(__hostGlobal(["Date"]).now());
129
+ row.expires = __numericAdd(__normalize(__hostGlobal(["Date"]).now()), __finite(__normalize(this.#settings.maxAgeSeconds) * 1000));
130
+ __normalize(row.execute());
131
+ if (__requireBoolean(!__requireBoolean(__normalize(row.next())))) {
132
+ __normalize(response.reject(401, "ClientProofRequired"));
133
+ }
134
+ }
135
+ __normalize(response.header("Set-Cookie", `__Host-lango-client=${value}; Path=/; Secure; HttpOnly; SameSite=Strict; Max-Age=${__normalize(this.#settings.maxAgeSeconds)}`));
136
+ } finally {
137
+ __normalize(__contextGet("db").finish());
138
+ }
139
+ identity.closed = true;
140
+ return encoded;
141
+ return null;
142
+ } finally {
143
+ __contextRestore(__contextScope);
144
+ }
145
+ }
146
+ }
147
+ export function clientOptions(spec, database, origin) {
148
+ __requireArity(arguments.length, [0, 1, 2], "clientOptions");
149
+ spec = __boundary(spec);
150
+ database = __boundary(database);
151
+ origin = __boundary(origin);
152
+ if (__requireBoolean(spec === null)) {
153
+ return null;
154
+ }
155
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
156
+ throw "Client configuration must be an object";
157
+ }
158
+ for (const name of __normalize(__hostGlobal(["Object"]).keys(spec))) {
159
+ if (__requireBoolean(name !== "maxAgeSeconds")) {
160
+ throw `Unknown client option: ${name}`;
161
+ }
162
+ }
163
+ if (__requireBoolean(database === null)) {
164
+ throw "Managed client identity requires a database";
165
+ }
166
+ if (__requireBoolean(__requireBoolean(__normalize(database.type) === "sqlite") && __requireBoolean(__normalize(database.file) === ":memory:"))) {
167
+ throw "Managed client identity requires persistent database storage";
168
+ }
169
+ if (__requireBoolean(__requireBoolean(__requireBoolean(origin === null) || __requireBoolean(!__requireBoolean(__normalize(origin.required)))) || __requireBoolean(!__requireBoolean(__normalize(origin.url.startsWith("https:")))))) {
170
+ throw "Managed client identity requires a required HTTPS origin";
171
+ }
172
+ let lifetime = __normalize(spec.maxAgeSeconds);
173
+ if (__requireBoolean(lifetime === null)) {
174
+ lifetime = 2592000;
175
+ }
176
+ if (__requireBoolean(__isString(lifetime))) {
177
+ lifetime = __hostGlobalCall(["Number"], lifetime);
178
+ }
179
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(lifetime)))) || __requireBoolean(lifetime < 1)) || __requireBoolean(lifetime > 31536000))) {
180
+ throw "Client maxAgeSeconds must be between 1 and 31536000";
181
+ }
182
+ return { maxAgeSeconds: lifetime };
183
+ return null;
184
+ }
185
+
186
+ function readCookie() {
187
+ const __contextScope = [];
188
+ try {
189
+ let pattern;
190
+ let headers = __normalize(__contextGet("request").headers);
191
+ let header = __normalize(headers.cookie);
192
+ if (__requireBoolean(header === null)) {
193
+ return null;
194
+ }
195
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(header))) || __requireBoolean(__normalize(header.length) > 8192))) {
196
+ __normalize(__contextGet("response").reject(400, "InvalidCookie"));
197
+ }
198
+ let found = null;
199
+ for (const part of __normalize(header.split(";"))) {
200
+ let pair = __normalize(part.trim());
201
+ let index = __normalize(pair.indexOf("="));
202
+ if (__requireBoolean(__requireBoolean(index >= 0) && __requireBoolean(__normalize(pair.slice(0, index)) === "__Host-lango-client"))) {
203
+ if (__requireBoolean(found !== null)) {
204
+ __normalize(__contextGet("response").reject(400, "InvalidCookie"));
205
+ }
206
+ found = __normalize(pair.slice(__numericAdd(index, 1)));
207
+ }
208
+ }
209
+ if (__requireBoolean(found !== null)) {
210
+ pattern = new (__hostGlobal(["RegExp"]))("^[a-f0-9]{64}$");
211
+ if (__requireBoolean(!__requireBoolean(__normalize(pattern.test(found))))) {
212
+ __normalize(__contextGet("response").reject(400, "InvalidCookie"));
213
+ }
214
+ }
215
+ return found;
216
+ return null;
217
+ } finally {
218
+ __contextRestore(__contextScope);
219
+ }
220
+ }
221
+
222
+ function clientSecret() {
223
+ let bytes = __normalize(crypto.randomBytes(32));
224
+ return __normalize(bytes.toString("hex"));
225
+ return null;
226
+ }
227
+
228
+ function clientDigest(value) {
229
+ __requireArity(arguments.length, [0], "clientDigest");
230
+ value = __boundary(value);
231
+ let hash = __normalize(crypto.createHash("sha256"));
232
+ __normalize(hash.update(value));
233
+ return __normalize(hash.digest("hex"));
234
+ return null;
235
+ }
236
+
237
+ function sameProof(left, right) {
238
+ __requireArity(arguments.length, [0, 1], "sameProof");
239
+ left = __boundary(left);
240
+ right = __boundary(right);
241
+ let pattern = new (__hostGlobal(["RegExp"]))("^[a-f0-9]{64}$");
242
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(left))) || __requireBoolean(!__requireBoolean(__normalize(pattern.test(left)))))) {
243
+ return false;
244
+ }
245
+ let first = __normalize(__hostGlobal(["Buffer"]).from(left, "hex"));
246
+ let second = __normalize(__hostGlobal(["Buffer"]).from(right, "hex"));
247
+ return __normalize(crypto.timingSafeEqual(first, second));
248
+ return null;
249
+ }
250
+ //# sourceMappingURL=client_identity.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client_identity.mjs","sources":["client_identity.lango"],"sourcesContent":["import js \"node:crypto\" as crypto\nimport js global Date\nimport js global Object\nimport js global Number\nimport js global RegExp\nimport js global Buffer\n\nexport clientOptions(spec, database, origin)\nexport class ClientIdentity(settings, database)\n methods\n initialize()\n begin(claims, tokens)\n publish(identity, claims, response, tokens)\n\nclientOptions(spec, database, origin)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"Client configuration must be an object\"\n for Object/keys(spec) as name\n if name != \"maxAgeSeconds\"\n throw \"Unknown client option: ${name}\"\n if database == null\n throw \"Managed client identity requires a database\"\n if database.type == \"sqlite\" and database.file == \":memory:\"\n throw \"Managed client identity requires persistent database storage\"\n if origin == null or !origin.required or !origin.url.startsWith(\"https:\")\n throw \"Managed client identity requires a required HTTPS origin\"\n lifetime = spec.maxAgeSeconds\n if lifetime == null\n lifetime = 2592000\n if is_string(lifetime)\n lifetime = Number(lifetime)\n if !Number/isSafeInteger(lifetime) or lifetime < 1 or lifetime > 31536000\n throw \"Client maxAgeSeconds must be between 1 and 31536000\"\n return {\n maxAgeSeconds: lifetime\n }\n\nClientIdentity(settings, database)\n @settings = settings\n @database = database\n\nClientIdentity:initialize()\n context db = @database.scope()\n try\n start_trans()\n if db.parameterStyle == \"postgresql\"\n lock = SQL\n select pg_advisory_xact_lock(1818324591, 1)\n lock.execute()\n row = SQL\n create table if not exists _lango_clients (\n client_id text primary key,\n secret_hash text not null unique,\n expires_at bigint not null\n )\n row.execute()\n commit_trans()\n finally\n db.finish()\n return null\n\nClientIdentity:begin(claims, tokens)\n context response\n value = readCookie()\n if value == null and claims != null\n response.reject(401, \"ClientProofRequired\")\n hash = null\n row = null\n if value == null\n value = clientSecret()\n claims = tokens.create()\n hash = clientDigest(value)\n claims.clientProof = hash\n return {\n claims: claims\n value: value\n hash: hash\n fresh: true\n restored: false\n rotate: false\n closed: false\n }\n hash = clientDigest(value)\n row = SQL\n select client_id from _lango_clients where secret_hash = :hash and expires_at > :now\n row.hash = hash\n row.now = Date/now()\n row.execute()\n if !row.next()\n response.reject(401, \"ClientCookieRejected\")\n restored = claims == null\n if restored\n claims = tokens.create(row.client_id)\n claims.clientProof = hash\n if claims.clientId != row.client_id or !sameProof(claims.clientProof, hash)\n response.reject(401, \"ClientProofRequired\")\n return {\n claims: claims\n value: value\n hash: hash\n fresh: false\n restored: restored\n rotate: false\n closed: false\n }\n\nClientIdentity:publish(identity, claims, response, tokens)\n value = identity.value\n if identity.rotate\n value = clientSecret()\n hash = clientDigest(value)\n claims.clientProof = hash\n encoded = tokens.sign(claims)\n context db = @database.scope()\n row = null\n try\n if identity.fresh\n row = SQL\n insert into _lango_clients (client_id, secret_hash, expires_at)\n values (:id, :hash, :expires)\n row.id = claims.clientId\n row.hash = hash\n row.expires = Date/now() + @settings.maxAgeSeconds * 1000\n row.execute()\n else\n row = SQL\n update _lango_clients set secret_hash = :replacement, expires_at = :expires\n where client_id = :id and secret_hash = :previous and expires_at > :now\n returning client_id\n row.id = claims.clientId\n row.previous = identity.hash\n row.replacement = hash\n row.now = Date/now()\n row.expires = Date/now() + @settings.maxAgeSeconds * 1000\n row.execute()\n if !row.next()\n response.reject(401, \"ClientProofRequired\")\n response.header(\"Set-Cookie\", \"__Host-lango-client=${value}; Path=/; Secure; HttpOnly; SameSite=Strict; Max-Age=${@settings.maxAgeSeconds}\")\n finally\n db.finish()\n identity.closed = true\n return encoded\n\nreadCookie()\n context request\n context response\n headers = request.headers\n header = headers.cookie\n if header == null\n return null\n if !is_string(header) or header.length > 8192\n response.reject(400, \"InvalidCookie\")\n found = null\n for header.split(\";\") as part\n pair = part.trim()\n index = pair.indexOf(\"=\")\n if index >= 0 and pair.slice(0, index) == \"__Host-lango-client\"\n if found != null\n response.reject(400, \"InvalidCookie\")\n found = pair.slice(index + 1)\n if found != null\n pattern = new RegExp(\"^[a-f0-9]{64}$\")\n if !pattern.test(found)\n response.reject(400, \"InvalidCookie\")\n return found\n\nclientSecret()\n bytes = crypto/randomBytes(32)\n return bytes.toString(\"hex\")\n\nclientDigest(value)\n hash = crypto/createHash(\"sha256\")\n hash.update(value)\n return hash.digest(\"hex\")\n\nsameProof(left, right)\n pattern = new RegExp(\"^[a-f0-9]{64}$\")\n if !is_string(left) or !pattern.test(left)\n return false\n first = Buffer/from(left, \"hex\")\n second = Buffer/from(right, \"hex\")\n return crypto/timingSafeEqual(first, second)\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuBA;;;;;;AAIA;;;;;;;;;AAKA;;;;;;;;;;;;;;"}
package/dist/database.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { ClientIdentity } from "./client_identity.mjs";
1
2
  import { DatabaseScope } from "./database_scope.mjs";
2
3
  import { SqliteAdapter } from "./sqlite_adapter.mjs";
3
4
  import { PostgresAdapter } from "./postgres_adapter.mjs";
@@ -6,6 +7,7 @@ import {
6
7
  contextGet as __contextGet,
7
8
  contextSet as __contextSet,
8
9
  contextRestore as __contextRestore,
10
+ bindContexts as __bindContexts,
9
11
  requireBoolean as __requireBoolean,
10
12
  normalize as __normalize,
11
13
  boundary as __boundary,
@@ -34,7 +36,7 @@ export class Database {
34
36
  if (__requireBoolean(__normalize(this.#settings.type) === "postgresql")) {
35
37
  name = "node:worker_threads";
36
38
  }
37
- return import(name).then(__withArity((mod) => {
39
+ return import(name).then(__withArity(__bindContexts((mod) => {
38
40
  const __contextScope = [];
39
41
  try {
40
42
  mod = __boundary(mod);
@@ -55,7 +57,7 @@ export class Database {
55
57
  } finally {
56
58
  __contextRestore(__contextScope);
57
59
  }
58
- }, [0], "closure")).catch(__withArity((error) => {
60
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
59
61
  const __contextScope = [];
60
62
  try {
61
63
  error = __boundary(error);
@@ -66,7 +68,7 @@ export class Database {
66
68
  } finally {
67
69
  __contextRestore(__contextScope);
68
70
  }
69
- }, [0], "closure"));
71
+ }), [0], "closure"));
70
72
  return null;
71
73
  }
72
74
  scope() {
@@ -85,17 +87,30 @@ export class Database {
85
87
  return null;
86
88
  }
87
89
  }
88
- export function prepareDatabase(settings) {
90
+ export function prepareDatabase(settings, root = null, client = null) {
89
91
  __requireArity(arguments.length, [0], "prepareDatabase");
90
92
  settings = __boundary(settings);
91
- if (__requireBoolean(__requireBoolean(__requireBoolean(settings === null) || __requireBoolean(__normalize(settings.type) !== "sqlite")) || __requireBoolean(__normalize(settings.file) === ":memory:"))) {
93
+ root = __boundary(root);
94
+ client = __boundary(client);
95
+ if (__requireBoolean(settings === null)) {
92
96
  return __normalize(__hostGlobal(["Promise"]).resolve(null));
93
97
  }
94
- let database = new Database(settings);
95
- return __normalize(database.open()).finally(() => {
98
+ if (__requireBoolean(__requireBoolean(client === null) && __requireBoolean(__requireBoolean(__normalize(settings.type) !== "sqlite") || __requireBoolean(__normalize(settings.file) === ":memory:")))) {
99
+ return __normalize(__hostGlobal(["Promise"]).resolve(null));
100
+ }
101
+ let database = new Database(settings, root);
102
+ return __normalize(database.open()).then(__withArity(__bindContexts((ignored) => {
103
+ ignored = __boundary(ignored);
104
+ if (__requireBoolean(client !== null)) {
105
+ let identity = new ClientIdentity(client, database);
106
+ __normalize(identity.initialize());
107
+ }
108
+ return null;
109
+ return null;
110
+ }), [0], "closure")).finally(__bindContexts(() => {
96
111
  __normalize(database.close());
97
112
  return null;
98
- });
113
+ }));
99
114
  return null;
100
115
  }
101
116
  //# sourceMappingURL=database.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"database.mjs","sources":["database.lango"],"sourcesContent":["import js global Promise\nimport \"./database_scope\"\n class DatabaseScope\nimport \"./sqlite_adapter\"\n class SqliteAdapter\nimport \"./postgres_adapter\"\n class PostgresAdapter\n\nexport prepareDatabase(settings)\n\nexport class Database(settings, root = null)\n methods\n open()\n scope()\n close()\n\nDatabase(settings, root = null)\n @root = root\n @settings = settings\n @adapter = null\n\nDatabase:open()\n if @settings == null\n return Promise/resolve(null)\n name = \"node:sqlite\"\n if @settings.type == \"postgresql\"\n name = \"node:worker_threads\"\n return chain import_module(name)\n then(mod)\n if @settings.type == \"sqlite\"\n @adapter = new SqliteAdapter(mod, @settings)\n else\n @adapter = new PostgresAdapter(mod, @settings, @root)\n context db = scope()\n try\n query = SQL\n select 1\n query.execute()\n finally\n db.finish()\n return null\n catch(error)\n context log\n log.err(\"Database startup failed: ${error}\")\n close()\n throw \"Database startup failed; check configuration, compiler version and driver installation\"\n\nDatabase:scope()\n if @adapter == null\n return null\n return new DatabaseScope(@adapter)\n\nDatabase:close()\n if @adapter != null\n @adapter.close()\n @adapter = null\n return null\n\nprepareDatabase(settings)\n if settings == null or settings.type != \"sqlite\" or settings.file == \":memory:\"\n return Promise/resolve(null)\n database = new Database(settings)\n return chain database.open()\n finally\n capture database\n database.close()\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"database.mjs","sources":["database.lango"],"sourcesContent":["import \"./client_identity\"\n class ClientIdentity\nimport js global Promise\nimport \"./database_scope\"\n class DatabaseScope\nimport \"./sqlite_adapter\"\n class SqliteAdapter\nimport \"./postgres_adapter\"\n class PostgresAdapter\n\nexport prepareDatabase(settings, root = null, client = null)\n\nexport class Database(settings, root = null)\n methods\n open()\n scope()\n close()\n\nDatabase(settings, root = null)\n @root = root\n @settings = settings\n @adapter = null\n\nDatabase:open()\n if @settings == null\n return Promise/resolve(null)\n name = \"node:sqlite\"\n if @settings.type == \"postgresql\"\n name = \"node:worker_threads\"\n return chain import_module(name)\n then(mod)\n if @settings.type == \"sqlite\"\n @adapter = new SqliteAdapter(mod, @settings)\n else\n @adapter = new PostgresAdapter(mod, @settings, @root)\n context db = scope()\n try\n query = SQL\n select 1\n query.execute()\n finally\n db.finish()\n return null\n catch(error)\n context log\n log.err(\"Database startup failed: ${error}\")\n close()\n throw \"Database startup failed; check configuration, compiler version and driver installation\"\n\nDatabase:scope()\n if @adapter == null\n return null\n return new DatabaseScope(@adapter)\n\nDatabase:close()\n if @adapter != null\n @adapter.close()\n @adapter = null\n return null\n\nprepareDatabase(settings, root = null, client = null)\n if settings == null\n return Promise/resolve(null)\n if client == null and (settings.type != \"sqlite\" or settings.file == \":memory:\")\n return Promise/resolve(null)\n database = new Database(settings, root)\n return chain database.open()\n then(ignored)\n capture database\n capture client\n if client != null\n identity = new ClientIdentity(client, database)\n identity.initialize()\n return null\n finally\n capture database\n database.close()\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,71 @@
1
+ import { Database } from "./database.mjs";
2
+ import { readDatabaseConfiguration } from "./job_configuration.mjs";
3
+ import * as path from "node:path";
4
+ import {
5
+ hostGlobal as __hostGlobal,
6
+ contextGet as __contextGet,
7
+ contextSet as __contextSet,
8
+ contextRestore as __contextRestore,
9
+ bindContexts as __bindContexts,
10
+ requireBoolean as __requireBoolean,
11
+ normalize as __normalize,
12
+ call as __call,
13
+ boundary as __boundary,
14
+ requireArity as __requireArity,
15
+ withArity as __withArity,
16
+ } from "./.lango/runtime.mjs";
17
+
18
+ export function withDatabase(directory, action) {
19
+ __requireArity(arguments.length, [0, 1], "withDatabase");
20
+ directory = __boundary(directory);
21
+ action = __boundary(action);
22
+ let root = __normalize(path.resolve(directory));
23
+ let database = new Database(readDatabaseConfiguration(root), root);
24
+ return __normalize(database.open()).then(__withArity(__bindContexts((ignored) => {
25
+ const __contextScope = [];
26
+ try {
27
+ ignored = __boundary(ignored);
28
+ __contextScope.push(__contextSet("db", __normalize(database.scope())));
29
+ return __normalize(__hostGlobal(["Promise"]).resolve(null)).then(__withArity(__bindContexts((ignored) => {
30
+ const __contextScope = [];
31
+ try {
32
+ ignored = __boundary(ignored);
33
+ return __call(action, );
34
+ return null;
35
+ } finally {
36
+ __contextRestore(__contextScope);
37
+ }
38
+ }), [0], "closure")).then(__withArity(__bindContexts((result) => {
39
+ const __contextScope = [];
40
+ try {
41
+ result = __boundary(result);
42
+ if (__requireBoolean(__requireBoolean(__contextGet("db") !== null) && __requireBoolean(__normalize(__contextGet("db").finish())))) {
43
+ throw "UnclosedTransaction";
44
+ }
45
+ return result;
46
+ return null;
47
+ } finally {
48
+ __contextRestore(__contextScope);
49
+ }
50
+ }), [0], "closure")).finally(__bindContexts(() => {
51
+ const __contextScope = [];
52
+ try {
53
+ if (__requireBoolean(__contextGet("db") !== null)) {
54
+ __normalize(__contextGet("db").finish());
55
+ }
56
+ return null;
57
+ } finally {
58
+ __contextRestore(__contextScope);
59
+ }
60
+ }));
61
+ return null;
62
+ } finally {
63
+ __contextRestore(__contextScope);
64
+ }
65
+ }), [0], "closure")).finally(__bindContexts(() => {
66
+ __normalize(database.close());
67
+ return null;
68
+ }));
69
+ return null;
70
+ }
71
+ //# sourceMappingURL=database_action.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database_action.mjs","sources":["database_action.lango"],"sourcesContent":["import \"./database\"\n class Database\nimport \"./job_configuration\"\n readDatabaseConfiguration(directory)\nimport js \"node:path\" as path\nimport js global Promise\n\nexport withDatabase(directory, action)\n\nwithDatabase(directory, action)\n root = path/resolve(directory)\n database = new Database(readDatabaseConfiguration(root), root)\n return chain database.open()\n then(ignored)\n capture database\n context db = database.scope()\n return chain Promise/resolve(null)\n then(ignored)\n capture action\n return action()\n then(result)\n if db != null and db.finish()\n throw \"UnclosedTransaction\"\n return result\n finally\n if db != null\n db.finish()\n finally\n capture database\n database.close()\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AASA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -28,6 +28,17 @@ export class DatabaseScope {
28
28
  return __normalize(this.#adapter.execute(sql, parameters));
29
29
  return null;
30
30
  }
31
+ invoke(spec, input) {
32
+ __requireArity(arguments.length, [0, 1], "invoke");
33
+ spec = __boundary(spec);
34
+ input = __boundary(input);
35
+ this.#requireOpen();
36
+ if (__requireBoolean(__normalize(this.parameterStyle) !== "postgresql")) {
37
+ throw "Database extensions require PostgreSQL";
38
+ }
39
+ return __normalize(this.#adapter.invoke(spec, input));
40
+ return null;
41
+ }
31
42
  begin() {
32
43
  this.#requireOpen();
33
44
  return __normalize(this.#adapter.begin());
@@ -1 +1 @@
1
- {"version":3,"file":"database_scope.mjs","sources":["database_scope.lango"],"sourcesContent":["import js global Error\nimport js global Object\nimport js global process\n\nexport class DatabaseScope(adapter)\n properties\n parameterStyle\n methods\n execute(sql, parameters)\n begin()\n commit()\n rollback()\n transactionState()\n finish()\n\nDatabaseScope(adapter)\n @adapter = adapter\n @closed = false\n @parameterStyle = adapter.parameterStyle\n\nDatabaseScope:execute(sql, parameters)\n requireOpen()\n return @adapter.execute(sql, parameters)\n\nDatabaseScope:begin()\n requireOpen()\n return @adapter.begin()\n\nDatabaseScope:commit()\n requireOpen()\n return @adapter.commit()\n\nDatabaseScope:rollback()\n requireOpen()\n return @adapter.rollback()\n\nDatabaseScope:transactionState()\n requireOpen()\n return @adapter.transactionState()\n\nDatabaseScope:requireOpen()\n if @closed\n throw new Error(\"DatabaseScopeClosed\")\n return null\n\nDatabaseScope:finish()\n if @closed\n return false\n @closed = true\n dirty = false\n try\n loop while @adapter.transactionState() != null\n dirty = true\n @adapter.rollback()\n catch error\n context log\n log.err(\"Database cleanup failed; retiring worker\")\n process.exit(1)\n return dirty\n"],"names":[],"mappings":";;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"database_scope.mjs","sources":["database_scope.lango"],"sourcesContent":["import js global Error\nimport js global Object\nimport js global process\n\nexport class DatabaseScope(adapter)\n properties\n parameterStyle\n methods\n execute(sql, parameters)\n invoke(spec, input)\n begin()\n commit()\n rollback()\n transactionState()\n finish()\n\nDatabaseScope(adapter)\n @adapter = adapter\n @closed = false\n @parameterStyle = adapter.parameterStyle\n\nDatabaseScope:execute(sql, parameters)\n requireOpen()\n return @adapter.execute(sql, parameters)\n\nDatabaseScope:invoke(spec, input)\n requireOpen()\n if @parameterStyle != \"postgresql\"\n throw \"Database extensions require PostgreSQL\"\n return @adapter.invoke(spec, input)\n\nDatabaseScope:begin()\n requireOpen()\n return @adapter.begin()\n\nDatabaseScope:commit()\n requireOpen()\n return @adapter.commit()\n\nDatabaseScope:rollback()\n requireOpen()\n return @adapter.rollback()\n\nDatabaseScope:transactionState()\n requireOpen()\n return @adapter.transactionState()\n\nDatabaseScope:requireOpen()\n if @closed\n throw new Error(\"DatabaseScopeClosed\")\n return null\n\nDatabaseScope:finish()\n if @closed\n return false\n @closed = true\n dirty = false\n try\n loop while @adapter.transactionState() != null\n dirty = true\n @adapter.rollback()\n catch error\n context log\n log.err(\"Database cleanup failed; retiring worker\")\n process.exit(1)\n return dirty\n"],"names":[],"mappings":";;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/endpoint.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { policyOptions } from "./policy.mjs";
1
2
  import { routePattern } from "./route_pattern.mjs";
2
3
  import { middlewareName } from "./middleware.mjs";
3
4
  import {
@@ -17,6 +18,10 @@ export class Endpoint {
17
18
  module;
18
19
  handler;
19
20
  auth;
21
+ policy;
22
+ reauthSeconds;
23
+ reauthExplicit;
24
+ legacyAuth;
20
25
  middleware;
21
26
  constructor(spec) {
22
27
  __requireArity(arguments.length, [0], "constructor");
@@ -34,13 +39,12 @@ export class Endpoint {
34
39
  if (__requireBoolean(__normalize(this.middleware) !== null)) {
35
40
  middlewareName(__normalize(this.middleware));
36
41
  }
37
- this.auth = __normalize(spec.auth);
38
- if (__requireBoolean(__normalize(this.auth) === null)) {
39
- this.auth = "login";
40
- }
41
- if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(this.auth)))) || __requireBoolean(__normalize(this.auth) === "")) || __requireBoolean(__normalize(this.auth.includes(","))))) {
42
- throw "Endpoint auth must name one permission";
43
- }
42
+ let selected = policyOptions(spec);
43
+ this.policy = __normalize(selected.name);
44
+ this.auth = __normalize(selected.name);
45
+ this.reauthSeconds = __normalize(selected.reauthSeconds);
46
+ this.reauthExplicit = __normalize(selected.explicit);
47
+ this.legacyAuth = __normalize(selected.legacy);
44
48
  }
45
49
  }
46
50
  //# sourceMappingURL=endpoint.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"endpoint.mjs","sources":["endpoint.lango"],"sourcesContent":["import \"./route_pattern\"\n routePattern(path)\nimport \"./middleware\"\n middlewareName(name)\n\nexport class Endpoint(spec)\n properties\n path\n module\n handler\n auth\n middleware\n\nEndpoint(spec)\n routePattern(spec.path)\n for [\"module\", \"handler\"] as key\n if !is_string(spec[key]) or spec[key] == \"\"\n throw \"Endpoint requires ${key}\"\n @path = spec.path\n @module = spec.module\n @handler = spec.handler\n @middleware = spec.middleware\n if @middleware != null\n middlewareName(@middleware)\n @auth = spec.auth\n if @auth == null\n @auth = \"login\"\n if !is_string(@auth) or @auth == \"\" or @auth.includes(\",\")\n throw \"Endpoint auth must name one permission\"\n"],"names":[],"mappings":";;;;;;;;;;;;;;AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"endpoint.mjs","sources":["endpoint.lango"],"sourcesContent":["import \"./policy\"\n policyOptions(spec)\nimport \"./route_pattern\"\n routePattern(path)\nimport \"./middleware\"\n middlewareName(name)\n\nexport class Endpoint(spec)\n properties\n path\n module\n handler\n auth\n policy\n reauthSeconds\n reauthExplicit\n legacyAuth\n middleware\n\nEndpoint(spec)\n routePattern(spec.path)\n for [\"module\", \"handler\"] as key\n if !is_string(spec[key]) or spec[key] == \"\"\n throw \"Endpoint requires ${key}\"\n @path = spec.path\n @module = spec.module\n @handler = spec.handler\n @middleware = spec.middleware\n if @middleware != null\n middlewareName(@middleware)\n selected = policyOptions(spec)\n @policy = selected.name\n @auth = selected.name\n @reauthSeconds = selected.reauthSeconds\n @reauthExplicit = selected.explicit\n @legacyAuth = selected.legacy\n"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,30 @@
1
+ import {
2
+ hostGlobal as __hostGlobal,
3
+ isNumber as __isNumber,
4
+ isString as __isString,
5
+ isBool as __isBool,
6
+ isObject as __isObject,
7
+ isArray as __isArray,
8
+ requireBoolean as __requireBoolean,
9
+ normalize as __normalize,
10
+ boundary as __boundary,
11
+ requireArity as __requireArity,
12
+ } from "./.lango/runtime.mjs";
13
+
14
+ export class ExecutionFailure {
15
+ #code;
16
+ constructor(code) {
17
+ __requireArity(arguments.length, [0], "constructor");
18
+ code = __boundary(code);
19
+ let pattern = new (__hostGlobal(["RegExp"]))("^[A-Za-z][A-Za-z0-9_]{0,63}$");
20
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(code))) || __requireBoolean(!__requireBoolean(__normalize(pattern.test(code)))))) {
21
+ throw "Execution rejection requires an error code";
22
+ }
23
+ this.#code = code;
24
+ }
25
+ get code() {
26
+ return __normalize(this.#code);
27
+ return null;
28
+ }
29
+ }
30
+ //# sourceMappingURL=execution_failure.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execution_failure.mjs","sources":["execution_failure.lango"],"sourcesContent":["import js global RegExp\n\nexport class ExecutionFailure(code)\n properties\n code?\n\nExecutionFailure(code)\n pattern = new RegExp(\"^[A-Za-z][A-Za-z0-9_]{0,63}$\")\n if !is_string(code) or !pattern.test(code)\n throw \"Execution rejection requires an error code\"\n @code = code\n\nExecutionFailure:code?\n return @code\n"],"names":[],"mappings":";;;;;;;;;;;;;AAEA;;;;;;;;;;;;;;;;;"}