@lango-project/lango-server 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/README.md +22 -3
  2. package/dist/body_reader.mjs.map +1 -1
  3. package/dist/builder.mjs +26 -2
  4. package/dist/builder.mjs.map +1 -1
  5. package/dist/cli/server.mjs +3 -835
  6. package/dist/cli/server.mjs.map +1 -1
  7. package/dist/client_identity.mjs.map +1 -1
  8. package/dist/database.mjs.map +1 -1
  9. package/dist/database_action.mjs +71 -0
  10. package/dist/database_action.mjs.map +1 -0
  11. package/dist/database_options.mjs.map +1 -1
  12. package/dist/database_scope.mjs +11 -0
  13. package/dist/database_scope.mjs.map +1 -1
  14. package/dist/endpoint.mjs.map +1 -1
  15. package/dist/error_details.mjs.map +1 -1
  16. package/dist/execution_failure.mjs +30 -0
  17. package/dist/execution_failure.mjs.map +1 -0
  18. package/dist/execution_identity.mjs +103 -0
  19. package/dist/execution_identity.mjs.map +1 -0
  20. package/dist/execution_log.mjs +47 -0
  21. package/dist/execution_log.mjs.map +1 -0
  22. package/dist/execution_runtime.mjs +201 -0
  23. package/dist/execution_runtime.mjs.map +1 -0
  24. package/dist/execution_worker.mjs +41 -0
  25. package/dist/execution_worker.mjs.map +1 -0
  26. package/dist/executor.mjs +384 -0
  27. package/dist/executor.mjs.map +1 -0
  28. package/dist/group.mjs +15 -0
  29. package/dist/group.mjs.map +1 -1
  30. package/dist/http_failure.mjs.map +1 -1
  31. package/dist/job_configuration.mjs +37 -0
  32. package/dist/job_configuration.mjs.map +1 -0
  33. package/dist/job_context.mjs +61 -0
  34. package/dist/job_context.mjs.map +1 -0
  35. package/dist/library.mjs +31 -0
  36. package/dist/library.mjs.map +1 -1
  37. package/dist/longpoll_broker.mjs.map +1 -1
  38. package/dist/longpoll_options.mjs.map +1 -1
  39. package/dist/longpoll_session.mjs.map +1 -1
  40. package/dist/middleware.mjs +1 -1
  41. package/dist/middleware.mjs.map +1 -1
  42. package/dist/module_loader.mjs +44 -0
  43. package/dist/module_loader.mjs.map +1 -0
  44. package/dist/options.mjs +12 -1
  45. package/dist/options.mjs.map +1 -1
  46. package/dist/origin.mjs.map +1 -1
  47. package/dist/policy.mjs.map +1 -1
  48. package/dist/postgres_adapter.mjs +12 -1
  49. package/dist/postgres_adapter.mjs.map +1 -1
  50. package/dist/postgres_worker.mjs +67 -5
  51. package/dist/postgres_worker.mjs.map +1 -1
  52. package/dist/queue_provider.mjs +52 -0
  53. package/dist/queue_provider.mjs.map +1 -0
  54. package/dist/request_identity.mjs.map +1 -1
  55. package/dist/response.mjs.map +1 -1
  56. package/dist/route_info.mjs.map +1 -1
  57. package/dist/route_pattern.mjs.map +1 -1
  58. package/dist/routes.mjs +36 -1
  59. package/dist/routes.mjs.map +1 -1
  60. package/dist/runtime.mjs +65 -25
  61. package/dist/runtime.mjs.map +1 -1
  62. package/dist/server.mjs +4 -1
  63. package/dist/server.mjs.map +1 -1
  64. package/dist/server_context.mjs.map +1 -1
  65. package/dist/sqlite_adapter.mjs.map +1 -1
  66. package/dist/static_files.mjs.map +1 -1
  67. package/dist/token.mjs.map +1 -1
  68. package/dist/tokens.mjs.map +1 -1
  69. package/dist/trace.mjs.map +1 -1
  70. package/dist/worker.mjs.map +1 -1
  71. package/docs/jobs.md +123 -0
  72. package/examples/security/package.json +1 -1
  73. package/package.json +10 -6
  74. package/src/builder.lango +22 -0
  75. package/src/database_action.lango +30 -0
  76. package/src/database_scope.lango +7 -0
  77. package/src/execution_failure.lango +14 -0
  78. package/src/execution_identity.lango +71 -0
  79. package/src/execution_log.lango +28 -0
  80. package/src/execution_runtime.lango +135 -0
  81. package/src/execution_worker.lango +32 -0
  82. package/src/executor.lango +284 -0
  83. package/src/group.lango +11 -0
  84. package/src/job_configuration.lango +29 -0
  85. package/src/job_context.lango +44 -0
  86. package/src/library.lango +21 -0
  87. package/src/middleware.lango +1 -1
  88. package/src/module_loader.lango +25 -0
  89. package/src/options.lango +7 -1
  90. package/src/postgres_adapter.lango +12 -0
  91. package/src/postgres_worker.lango +54 -3
  92. package/src/queue_provider.lango +30 -0
  93. package/src/routes.lango +44 -0
  94. package/src/runtime.lango +45 -17
  95. package/src/server.lango +3 -0
  96. package/dist/cli/.lango/bignumber.mjs +0 -3119
  97. package/dist/cli/.lango/localization.mjs +0 -108
  98. package/dist/cli/.lango/runtime.mjs +0 -725
  99. package/dist/cli/.lango/vendor/i18next.LICENSE +0 -21
  100. package/dist/cli/.lango/vendor/i18next.mjs +0 -2434
  101. package/dist/cli/.lango/xmldom/conventions.js +0 -429
  102. package/dist/cli/.lango/xmldom/dom-parser.js +0 -591
  103. package/dist/cli/.lango/xmldom/dom.js +0 -3759
  104. package/dist/cli/.lango/xmldom/entities.js +0 -2171
  105. package/dist/cli/.lango/xmldom/errors.js +0 -205
  106. package/dist/cli/.lango/xmldom/grammar.js +0 -559
  107. package/dist/cli/.lango/xmldom/index.js +0 -41
  108. package/dist/cli/.lango/xmldom/package.json +0 -1
  109. package/dist/cli/.lango/xmldom/sax.js +0 -983
  110. package/dist/cli/.lango/xmldom.LICENSE.md +0 -8
  111. package/dist/cli/body_reader.mjs +0 -180
  112. package/dist/cli/body_reader.mjs.map +0 -1
  113. package/dist/cli/client_identity.mjs +0 -250
  114. package/dist/cli/client_identity.mjs.map +0 -1
  115. package/dist/cli/database.mjs +0 -116
  116. package/dist/cli/database.mjs.map +0 -1
  117. package/dist/cli/database_options.mjs +0 -83
  118. package/dist/cli/database_options.mjs.map +0 -1
  119. package/dist/cli/database_scope.mjs +0 -82
  120. package/dist/cli/database_scope.mjs.map +0 -1
  121. package/dist/cli/endpoint.mjs +0 -50
  122. package/dist/cli/endpoint.mjs.map +0 -1
  123. package/dist/cli/error_details.mjs +0 -45
  124. package/dist/cli/error_details.mjs.map +0 -1
  125. package/dist/cli/longpoll_broker.mjs +0 -215
  126. package/dist/cli/longpoll_broker.mjs.map +0 -1
  127. package/dist/cli/longpoll_options.mjs +0 -103
  128. package/dist/cli/longpoll_options.mjs.map +0 -1
  129. package/dist/cli/longpoll_session.mjs +0 -136
  130. package/dist/cli/longpoll_session.mjs.map +0 -1
  131. package/dist/cli/middleware.mjs +0 -25
  132. package/dist/cli/middleware.mjs.map +0 -1
  133. package/dist/cli/options.mjs +0 -203
  134. package/dist/cli/options.mjs.map +0 -1
  135. package/dist/cli/origin.mjs +0 -87
  136. package/dist/cli/origin.mjs.map +0 -1
  137. package/dist/cli/policy.mjs +0 -121
  138. package/dist/cli/policy.mjs.map +0 -1
  139. package/dist/cli/postgres_adapter.mjs +0 -167
  140. package/dist/cli/postgres_adapter.mjs.map +0 -1
  141. package/dist/cli/route_pattern.mjs +0 -129
  142. package/dist/cli/route_pattern.mjs.map +0 -1
  143. package/dist/cli/routes.mjs +0 -325
  144. package/dist/cli/routes.mjs.map +0 -1
  145. package/dist/cli/sqlite_adapter.mjs +0 -163
  146. package/dist/cli/sqlite_adapter.mjs.map +0 -1
  147. package/dist/cli/static_files.mjs +0 -224
  148. package/dist/cli/static_files.mjs.map +0 -1
  149. package/dist/cli/trace.mjs +0 -53
  150. package/dist/cli/trace.mjs.map +0 -1
@@ -1,163 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import {
4
- hostGlobal as __hostGlobal,
5
- hostGlobalCall as __hostGlobalCall,
6
- isNumber as __isNumber,
7
- isString as __isString,
8
- isBool as __isBool,
9
- isObject as __isObject,
10
- isArray as __isArray,
11
- entries as __entries,
12
- requireBoolean as __requireBoolean,
13
- normalize as __normalize,
14
- finite as __finite,
15
- numericAdd as __numericAdd,
16
- boundary as __boundary,
17
- requireArity as __requireArity,
18
- } from "./.lango/runtime.mjs";
19
-
20
- export class SqliteAdapter {
21
- parameterStyle;
22
- #connection;
23
- #depth;
24
- #state;
25
- constructor(mod, settings) {
26
- __requireArity(arguments.length, [0, 1], "constructor");
27
- mod = __boundary(mod);
28
- settings = __boundary(settings);
29
- if (__requireBoolean(__normalize(settings.file) !== ":memory:")) {
30
- __normalize(fs.mkdirSync(__normalize(path.dirname(__normalize(settings.file))), { recursive: true }));
31
- }
32
- this.#connection = __normalize(__hostGlobal(["Reflect"]).construct(__normalize(mod.DatabaseSync), [__normalize(settings.file)]));
33
- this.#depth = 0;
34
- this.#state = null;
35
- this.parameterStyle = "named";
36
- try {
37
- __normalize(this.#connection.exec(`PRAGMA busy_timeout=${__normalize(settings.busyTimeoutMs)}`));
38
- __normalize(this.#connection.exec("PRAGMA foreign_keys=ON"));
39
- let modeQuery = __normalize(this.#connection.prepare("PRAGMA journal_mode"));
40
- let mode = __normalize(modeQuery.get());
41
- if (__requireBoolean(__requireBoolean(__normalize(settings.file) !== ":memory:") && __requireBoolean(__normalize(mode.journal_mode) !== "wal"))) {
42
- __normalize(this.#connection.exec("PRAGMA journal_mode=WAL"));
43
- }
44
- } catch (error) {
45
- __normalize(this.#connection.close());
46
- throw "SQLite initialization failed";
47
- }
48
- }
49
- execute(sql, parameters) {
50
- __requireArity(arguments.length, [0, 1], "execute");
51
- sql = __boundary(sql);
52
- parameters = __boundary(parameters);
53
- let bound = { };
54
- for (const [name, value] of __entries(parameters)) {
55
- if (__requireBoolean(__isBool(value))) {
56
- bound[name] = __hostGlobalCall(["Number"], value);
57
- } else {
58
- bound[name] = value;
59
- }
60
- }
61
- let statement = __normalize(this.#connection.prepare(sql));
62
- __normalize(statement.setReadBigInts(true));
63
- let columns = __normalize(statement.columns());
64
- if (__requireBoolean(__normalize(columns.length) > 0)) {
65
- let records = __normalize(statement.all(bound));
66
- for (const row of records) {
67
- for (const [name, value] of __entries(row)) {
68
- row[name] = normalizeInteger(value);
69
- }
70
- }
71
- return { rows: records, changes: null, lastInsertRowid: null };
72
- }
73
- let result = __normalize(statement.run(bound));
74
- let changes = null;
75
- let generated = null;
76
- let operation = __normalize(sql.trim());
77
- operation = __normalize(operation.toLowerCase());
78
- if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(operation.startsWith("insert"))) || __requireBoolean(__normalize(operation.startsWith("update")))) || __requireBoolean(__normalize(operation.startsWith("delete")))) || __requireBoolean(__normalize(operation.startsWith("with"))))) {
79
- changes = normalizeInteger(__normalize(result.changes));
80
- }
81
- if (__requireBoolean(__requireBoolean(__normalize(operation.startsWith("insert"))) && __requireBoolean(changes === 1))) {
82
- generated = normalizeInteger(__normalize(result.lastInsertRowid));
83
- }
84
- return { rows: [], changes: changes, lastInsertRowid: generated };
85
- return null;
86
- }
87
- begin() {
88
- if (__requireBoolean(__requireBoolean(__normalize(this.#depth) === 0) && __requireBoolean(!__requireBoolean(__normalize(this.#connection.isTransaction))))) {
89
- __normalize(this.#connection.exec("BEGIN"));
90
- }
91
- this.#depth = __numericAdd(__normalize(this.#depth), 1);
92
- if (__requireBoolean(__normalize(this.#state) === null)) {
93
- this.#state = "active";
94
- }
95
- return null;
96
- return null;
97
- }
98
- commit() {
99
- if (__requireBoolean(__normalize(this.#depth) === 0)) {
100
- throw new (__hostGlobal(["Error"]))("NoActiveTransaction");
101
- }
102
- if (__requireBoolean(__normalize(this.#depth) > 1)) {
103
- this.#depth = __finite(__normalize(this.#depth) - 1);
104
- return null;
105
- }
106
- if (__requireBoolean(__normalize(this.#state) === "rollback_only")) {
107
- this.rollback();
108
- throw new (__hostGlobal(["Error"]))("TransactionRolledBack");
109
- }
110
- __normalize(this.#connection.exec("COMMIT"));
111
- this.#depth = 0;
112
- this.#state = null;
113
- return null;
114
- return null;
115
- }
116
- rollback() {
117
- if (__requireBoolean(__normalize(this.#depth) > 1)) {
118
- this.#depth = __finite(__normalize(this.#depth) - 1);
119
- this.#state = "rollback_only";
120
- return null;
121
- }
122
- if (__requireBoolean(__normalize(this.#connection.isTransaction))) {
123
- __normalize(this.#connection.exec("ROLLBACK"));
124
- }
125
- this.#depth = 0;
126
- this.#state = null;
127
- return null;
128
- return null;
129
- }
130
- transactionState() {
131
- if (__requireBoolean(!__requireBoolean(__normalize(this.#connection.isTransaction)))) {
132
- this.#depth = 0;
133
- this.#state = null;
134
- }
135
- if (__requireBoolean(__requireBoolean(__normalize(this.#connection.isTransaction)) && __requireBoolean(__normalize(this.#state) === null))) {
136
- return "active";
137
- }
138
- return __normalize(this.#state);
139
- return null;
140
- }
141
- close() {
142
- __loop1: while (__requireBoolean(this.transactionState() !== null)) {
143
- this.rollback();
144
- }
145
- __normalize(this.#connection.close());
146
- return null;
147
- return null;
148
- }
149
- }
150
- function normalizeInteger(value) {
151
- __requireArity(arguments.length, [0], "normalizeInteger");
152
- value = __boundary(value);
153
- if (__requireBoolean(__normalize(__hostGlobal(["Object"]).prototype.toString.call(value)) !== "[object BigInt]")) {
154
- return value;
155
- }
156
- let number = __hostGlobalCall(["Number"], value);
157
- if (__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(number)))) {
158
- return number;
159
- }
160
- return `${value}`;
161
- return null;
162
- }
163
- //# sourceMappingURL=sqlite_adapter.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sqlite_adapter.mjs","sources":["sqlite_adapter.lango"],"sourcesContent":["import js global Error\nimport js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js global Reflect\nimport js global Object\nimport js global Number\n\nexport class SqliteAdapter(mod, settings)\n properties\n parameterStyle\n methods\n execute(sql, parameters)\n begin()\n commit()\n rollback()\n transactionState()\n close()\n\nSqliteAdapter(mod, settings)\n if settings.file != \":memory:\"\n fs/mkdirSync(path/dirname(settings.file), {\n recursive: true\n })\n @connection = Reflect/construct(mod.DatabaseSync, [settings.file])\n @depth = 0\n @state = null\n @parameterStyle = \"named\"\n try\n @connection.exec(\"PRAGMA busy_timeout=${settings.busyTimeoutMs}\")\n @connection.exec(\"PRAGMA foreign_keys=ON\")\n modeQuery = @connection.prepare(\"PRAGMA journal_mode\")\n mode = modeQuery.get()\n if settings.file != \":memory:\" and mode.journal_mode != \"wal\"\n @connection.exec(\"PRAGMA journal_mode=WAL\")\n catch error\n @connection.close()\n throw \"SQLite initialization failed\"\n\nSqliteAdapter:execute(sql, parameters)\n bound = {}\n for parameters as name: value\n if is_bool(value)\n bound[name] = Number(value)\n else\n bound[name] = value\n statement = @connection.prepare(sql)\n statement.setReadBigInts(true)\n columns = statement.columns()\n if columns.length > 0\n records = statement.all(bound)\n for records as row\n for row as name: value\n row[name] = normalizeInteger(value)\n return {\n rows: records\n changes: null\n lastInsertRowid: null\n }\n result = statement.run(bound)\n changes = null\n generated = null\n operation = sql.trim()\n operation = operation.toLowerCase()\n if operation.startsWith(\"insert\") or operation.startsWith(\"update\") or operation.startsWith(\"delete\") or operation.startsWith(\"with\")\n changes = normalizeInteger(result.changes)\n if operation.startsWith(\"insert\") and changes == 1\n generated = normalizeInteger(result.lastInsertRowid)\n return {\n rows: []\n changes: changes\n lastInsertRowid: generated\n }\n\nSqliteAdapter:begin()\n if @depth == 0 and !@connection.isTransaction\n @connection.exec(\"BEGIN\")\n @depth = @depth + 1\n if @state == null\n @state = \"active\"\n return null\n\nSqliteAdapter:commit()\n if @depth == 0\n throw new Error(\"NoActiveTransaction\")\n if @depth > 1\n @depth = @depth - 1\n return null\n if @state == \"rollback_only\"\n rollback()\n throw new Error(\"TransactionRolledBack\")\n @connection.exec(\"COMMIT\")\n @depth = 0\n @state = null\n return null\n\nSqliteAdapter:rollback()\n if @depth > 1\n @depth = @depth - 1\n @state = \"rollback_only\"\n return null\n if @connection.isTransaction\n @connection.exec(\"ROLLBACK\")\n @depth = 0\n @state = null\n return null\n\nSqliteAdapter:transactionState()\n if !@connection.isTransaction\n @depth = 0\n @state = null\n if @connection.isTransaction and @state == null\n return \"active\"\n return @state\n\nSqliteAdapter:close()\n loop while transactionState() != null\n rollback()\n @connection.close()\n return null\n\nnormalizeInteger(value)\n if Object/prototype/toString/call(value) != \"[object BigInt]\"\n return value\n number = Number(value)\n if Number/isSafeInteger(number)\n return number\n return \"${value}\"\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiHA;;;;;;;;;;;;;;"}
@@ -1,224 +0,0 @@
1
- import { trace } from "./trace.mjs";
2
- import * as fs from "node:fs";
3
- import * as path from "node:path";
4
- import * as files from "serve-static";
5
- import {
6
- hostGlobal as __hostGlobal,
7
- hostGlobalCall as __hostGlobalCall,
8
- bindContexts as __bindContexts,
9
- isNumber as __isNumber,
10
- isString as __isString,
11
- isBool as __isBool,
12
- isObject as __isObject,
13
- isArray as __isArray,
14
- entries as __entries,
15
- requireBoolean as __requireBoolean,
16
- normalize as __normalize,
17
- finite as __finite,
18
- call as __call,
19
- boundary as __boundary,
20
- requireArity as __requireArity,
21
- withArity as __withArity,
22
- } from "./.lango/runtime.mjs";
23
-
24
- export class StaticFiles {
25
- #settings;
26
- #serve;
27
- #runtime;
28
- constructor(settings) {
29
- __requireArity(arguments.length, [0], "constructor");
30
- settings = __boundary(settings);
31
- this.#settings = settings;
32
- if (__requireBoolean(!__requireBoolean(__normalize(__normalize(fs.statSync(__normalize(settings.directory))).isDirectory())))) {
33
- throw "Static directory must be a directory";
34
- }
35
- if (__requireBoolean(__requireBoolean(__normalize(settings.fallback) !== null) && __requireBoolean(!__requireBoolean(__normalize(__normalize(fs.statSync(__normalize(path.join(__normalize(settings.directory), __normalize(settings.fallback))))).isFile()))))) {
36
- throw "Static fallback must be a file";
37
- }
38
- this.#serve = __normalize(files.default(__normalize(settings.directory), { index: __normalize(settings.index), dotfiles: "deny", fallthrough: false, redirect: false }));
39
- this.#runtime = __normalize(files.default(__normalize(settings.directory), { index: false, dotfiles: "allow", fallthrough: false, redirect: false }));
40
- }
41
- receive(req, res, failed) {
42
- __requireArity(arguments.length, [0, 1, 2], "receive");
43
- req = __boundary(req);
44
- res = __boundary(res);
45
- failed = __boundary(failed);
46
- let mount = __normalize(this.#settings.path);
47
- let url = __normalize(req.url);
48
- let pathname = __normalize(__normalize(url.split("?"))[0]);
49
- if (__requireBoolean(__requireBoolean(__requireBoolean(mount !== "/") && __requireBoolean(pathname !== mount)) && __requireBoolean(!__requireBoolean(__normalize(pathname.startsWith(`${mount}/`)))))) {
50
- __call(failed, 404, "NotFound");
51
- return null;
52
- }
53
- if (__requireBoolean(mount !== "/")) {
54
- req.url = __normalize(url.slice(__normalize(mount.length)));
55
- if (__requireBoolean(__requireBoolean(__normalize(req.url) === "") || __requireBoolean(__normalize(req.url.startsWith("?"))))) {
56
- req.url = `/${__normalize(req.url)}`;
57
- }
58
- }
59
- let local = __normalize(__normalize(req.url.split("?"))[0]);
60
- let decoded = null;
61
- try {
62
- decoded = __hostGlobalCall(["decodeURIComponent"], local);
63
- } catch (error) {
64
- __call(failed, 400, "InvalidPath");
65
- return null;
66
- }
67
- if (__requireBoolean(__requireBoolean(__normalize(decoded.includes("\\"))) || __requireBoolean(__normalize(decoded.includes(__normalize(__hostGlobal(["String"]).fromCharCode(0))))))) {
68
- __call(failed, 400, "InvalidPath");
69
- return null;
70
- }
71
- let segments = __normalize(decoded.split("/"));
72
- let runtime = false;
73
- for (const [index, segment] of __entries(segments)) {
74
- if (__requireBoolean(__normalize(segment.startsWith(".")))) {
75
- if (__requireBoolean(__requireBoolean(__requireBoolean(index === 1) && __requireBoolean(segment === ".lango")) && __requireBoolean(__normalize(this.#settings.runtime)))) {
76
- runtime = true;
77
- } else {
78
- __call(failed, 403, "Forbidden");
79
- return null;
80
- }
81
- }
82
- }
83
- let done = __withArity(__bindContexts((error) => {
84
- error = __boundary(error);
85
- this.#failure(req, res, failed, error, decoded);
86
- return null;
87
- }), [0], "closure");
88
- let serve = __normalize(this.#serve);
89
- if (__requireBoolean(runtime)) {
90
- serve = __normalize(this.#runtime);
91
- }
92
- trace(`Static ${__normalize(req.method)} ${pathname}: directory=${__normalize(this.#settings.directory)}; index=${__normalize(this.#settings.index)}; runtime=${runtime}`);
93
- __call(serve, req, res, done);
94
- return null;
95
- return null;
96
- }
97
- #failure(req, res, failed, error, pathname = null) {
98
- __requireArity(arguments.length, [0, 1, 2, 3], "failure");
99
- req = __boundary(req);
100
- res = __boundary(res);
101
- failed = __boundary(failed);
102
- error = __boundary(error);
103
- pathname = __boundary(pathname);
104
- if (__requireBoolean(__normalize(res.headersSent))) {
105
- __normalize(res.destroy());
106
- return null;
107
- }
108
- let status = 404;
109
- let code = "NotFound";
110
- if (__requireBoolean(error !== null)) {
111
- if (__normalize(error.status) === 400) {
112
- status = 400;
113
- code = "InvalidPath";
114
- } else if (__normalize(error.status) === 403) {
115
- status = 403;
116
- code = "Forbidden";
117
- } else if (__normalize(error.status) === 416) {
118
- status = 416;
119
- code = "RangeNotSatisfiable";
120
- } else if (__normalize(error.status) === 404) {
121
- status = 404;
122
- } else {
123
- status = 500;
124
- code = "InternalServerError";
125
- }
126
- }
127
- if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(status === 404) && __requireBoolean(pathname !== null)) && __requireBoolean(__normalize(this.#settings.fallback) !== null)) && __requireBoolean(!__requireBoolean(__normalize(pathname.startsWith("/.lango"))))) && __requireBoolean(__normalize(path.posix.extname(pathname)) === "")) && __requireBoolean(acceptsHTML(req.headers.accept)))) {
128
- req.url = `/${__normalize(this.#settings.fallback)}`;
129
- let done = __withArity(__bindContexts((error) => {
130
- error = __boundary(error);
131
- this.#failure(req, res, failed, error);
132
- return null;
133
- }), [0], "closure");
134
- let serve = __normalize(this.#serve);
135
- __call(serve, req, res, done);
136
- return null;
137
- }
138
- __call(failed, status, code);
139
- return null;
140
- return null;
141
- }
142
- }
143
- export function staticOptions(root, spec) {
144
- __requireArity(arguments.length, [0, 1], "staticOptions");
145
- root = __boundary(root);
146
- spec = __boundary(spec);
147
- if (__requireBoolean(spec === null)) {
148
- return null;
149
- }
150
- if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
151
- throw "static must be a map";
152
- }
153
- let allowed = ["path", "directory", "index", "fallback", "runtime"];
154
- for (const [key, value] of __entries(spec)) {
155
- if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
156
- throw `Unknown static option: ${key}`;
157
- }
158
- }
159
- let mount = __normalize(spec.path);
160
- if (__requireBoolean(mount === null)) {
161
- mount = "/";
162
- }
163
- let pattern = new (__hostGlobal(["RegExp"]))("^/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]*$");
164
- if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(mount))) || __requireBoolean(!__requireBoolean(__normalize(pattern.test(mount)))))) {
165
- throw "Static path must be an absolute URL prefix";
166
- }
167
- if (__requireBoolean(__requireBoolean(mount !== "/") && __requireBoolean(__normalize(mount.endsWith("/"))))) {
168
- mount = __normalize(mount.slice(0, __finite(-1)));
169
- }
170
- if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.directory)))) || __requireBoolean(__normalize(spec.directory.trim()) === ""))) {
171
- throw "Static directory is required";
172
- }
173
- let index = __normalize(spec.index);
174
- if (__requireBoolean(index === null)) {
175
- index = "index.html";
176
- }
177
- if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(index))) || __requireBoolean(index === "")) || __requireBoolean(__normalize(index.startsWith(".")))) || __requireBoolean(__normalize(index.includes("/")))) || __requireBoolean(__normalize(index.includes("\\"))))) {
178
- throw "Static index must be a file name";
179
- }
180
- let fallback = __normalize(spec.fallback);
181
- if (__requireBoolean(__requireBoolean(fallback !== null) && __requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(fallback))) || __requireBoolean(fallback === "")) || __requireBoolean(__normalize(fallback.startsWith(".")))) || __requireBoolean(__normalize(fallback.includes("/")))) || __requireBoolean(__normalize(fallback.includes("\\")))))) {
182
- throw "Static fallback must be a file name";
183
- }
184
- let runtime = __normalize(spec.runtime);
185
- if (__requireBoolean(__requireBoolean(runtime === null) || __requireBoolean(runtime === "false"))) {
186
- runtime = false;
187
- }
188
- if (__requireBoolean(runtime === "true")) {
189
- runtime = true;
190
- }
191
- if (__requireBoolean(!__requireBoolean(__isBool(runtime)))) {
192
- throw "Static runtime must be boolean";
193
- }
194
- return { path: mount, directory: __normalize(path.resolve(root, __normalize(spec.directory))), index: index, fallback: fallback, runtime: runtime };
195
- return null;
196
- }
197
-
198
- function acceptsHTML(value) {
199
- __requireArity(arguments.length, [0], "acceptsHTML");
200
- value = __boundary(value);
201
- if (__requireBoolean(!__requireBoolean(__isString(value)))) {
202
- return false;
203
- }
204
- let zero = new (__hostGlobal(["RegExp"]))("^q\\s*=\\s*0(?:\\.0*)?$", "i");
205
- for (const range of __normalize(value.split(","))) {
206
- let parts = __normalize(range.split(";"));
207
- let media = __normalize(parts[0]);
208
- let type = __normalize(__normalize(media.trim()).toLowerCase());
209
- if (__requireBoolean(__requireBoolean(type === "text/html") || __requireBoolean(type === "application/xhtml+xml"))) {
210
- let allowed = true;
211
- for (const parameter of __normalize(parts.slice(1))) {
212
- if (__requireBoolean(__normalize(zero.test(__normalize(parameter.trim()))))) {
213
- allowed = false;
214
- }
215
- }
216
- if (__requireBoolean(allowed)) {
217
- return true;
218
- }
219
- }
220
- }
221
- return false;
222
- return null;
223
- }
224
- //# sourceMappingURL=static_files.mjs.map
@@ -1 +0,0 @@
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,53 +0,0 @@
1
- import {
2
- contextGet as __contextGet,
3
- contextSet as __contextSet,
4
- contextRestore as __contextRestore,
5
- requireBoolean as __requireBoolean,
6
- normalize as __normalize,
7
- boundary as __boundary,
8
- requireArity as __requireArity,
9
- } from "./.lango/runtime.mjs";
10
-
11
- export function trace(message) {
12
- const __contextScope = [];
13
- try {
14
- __requireArity(arguments.length, [0], "trace");
15
- message = __boundary(message);
16
- if (__requireBoolean(__normalize(__contextGet("log").verbose) === true)) {
17
- __normalize(__contextGet("log").debug(message));
18
- }
19
- return null;
20
- return null;
21
- } finally {
22
- __contextRestore(__contextScope);
23
- }
24
- }
25
-
26
- export function responseLog(id, method, path, status) {
27
- const __contextScope = [];
28
- try {
29
- __requireArity(arguments.length, [0, 1, 2, 3], "responseLog");
30
- id = __boundary(id);
31
- method = __boundary(method);
32
- path = __boundary(path);
33
- status = __boundary(status);
34
- let message = `HTTP ${method} ${path} completed status=${status}`;
35
- if (__requireBoolean(id !== null)) {
36
- message = `[${id}] ${message}`;
37
- }
38
- if (__requireBoolean(status >= 500)) {
39
- __normalize(__contextGet("log").err(message));
40
- } else {
41
- if (__requireBoolean(status >= 400)) {
42
- __normalize(__contextGet("log").warn(message));
43
- } else {
44
- trace(message);
45
- }
46
- }
47
- return null;
48
- return null;
49
- } finally {
50
- __contextRestore(__contextScope);
51
- }
52
- }
53
- //# sourceMappingURL=trace.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"trace.mjs","sources":["trace.lango"],"sourcesContent":["export trace(message)\nexport responseLog(id, method, path, status)\n\ntrace(message)\n context log\n if log.verbose? == true\n log.debug(message)\n return null\n\n\nresponseLog(id, method, path, status)\n context log\n message = \"HTTP ${method} ${path} completed status=${status}\"\n if id != null\n message = \"[${id}] ${message}\"\n if status >= 500\n log.err(message)\n else\n if status >= 400\n log.warn(message)\n else\n trace(message)\n return null\n"],"names":[],"mappings":";;;;;;;;;;AAGA;;;;;;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}