@lango-project/lango-server 0.0.0-stage → 0.1.1

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 (134) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +710 -2
  4. package/dist/.lango/bignumber.mjs +3119 -0
  5. package/dist/.lango/runtime.mjs +683 -0
  6. package/dist/.lango/xmldom/conventions.js +429 -0
  7. package/dist/.lango/xmldom/dom-parser.js +591 -0
  8. package/dist/.lango/xmldom/dom.js +3759 -0
  9. package/dist/.lango/xmldom/entities.js +2171 -0
  10. package/dist/.lango/xmldom/errors.js +205 -0
  11. package/dist/.lango/xmldom/grammar.js +559 -0
  12. package/dist/.lango/xmldom/index.js +41 -0
  13. package/dist/.lango/xmldom/package.json +1 -0
  14. package/dist/.lango/xmldom/sax.js +983 -0
  15. package/dist/.lango/xmldom.LICENSE.md +8 -0
  16. package/dist/body_reader.mjs +179 -0
  17. package/dist/body_reader.mjs.map +1 -0
  18. package/dist/builder.mjs +84 -0
  19. package/dist/builder.mjs.map +1 -0
  20. package/dist/cli/.lango/bignumber.mjs +3119 -0
  21. package/dist/cli/.lango/runtime.mjs +683 -0
  22. package/dist/cli/.lango/xmldom/conventions.js +429 -0
  23. package/dist/cli/.lango/xmldom/dom-parser.js +591 -0
  24. package/dist/cli/.lango/xmldom/dom.js +3759 -0
  25. package/dist/cli/.lango/xmldom/entities.js +2171 -0
  26. package/dist/cli/.lango/xmldom/errors.js +205 -0
  27. package/dist/cli/.lango/xmldom/grammar.js +559 -0
  28. package/dist/cli/.lango/xmldom/index.js +41 -0
  29. package/dist/cli/.lango/xmldom/package.json +1 -0
  30. package/dist/cli/.lango/xmldom/sax.js +983 -0
  31. package/dist/cli/.lango/xmldom.LICENSE.md +8 -0
  32. package/dist/cli/body_reader.mjs +179 -0
  33. package/dist/cli/body_reader.mjs.map +1 -0
  34. package/dist/cli/database.mjs +101 -0
  35. package/dist/cli/database.mjs.map +1 -0
  36. package/dist/cli/database_options.mjs +83 -0
  37. package/dist/cli/database_options.mjs.map +1 -0
  38. package/dist/cli/database_scope.mjs +82 -0
  39. package/dist/cli/database_scope.mjs.map +1 -0
  40. package/dist/cli/endpoint.mjs +50 -0
  41. package/dist/cli/endpoint.mjs.map +1 -0
  42. package/dist/cli/longpoll_broker.mjs +214 -0
  43. package/dist/cli/longpoll_broker.mjs.map +1 -0
  44. package/dist/cli/longpoll_options.mjs +102 -0
  45. package/dist/cli/longpoll_options.mjs.map +1 -0
  46. package/dist/cli/longpoll_session.mjs +135 -0
  47. package/dist/cli/longpoll_session.mjs.map +1 -0
  48. package/dist/cli/middleware.mjs +25 -0
  49. package/dist/cli/middleware.mjs.map +1 -0
  50. package/dist/cli/options.mjs +177 -0
  51. package/dist/cli/options.mjs.map +1 -0
  52. package/dist/cli/postgres_adapter.mjs +166 -0
  53. package/dist/cli/postgres_adapter.mjs.map +1 -0
  54. package/dist/cli/routes.mjs +297 -0
  55. package/dist/cli/routes.mjs.map +1 -0
  56. package/dist/cli/server.mjs +792 -0
  57. package/dist/cli/server.mjs.map +1 -0
  58. package/dist/cli/sqlite_adapter.mjs +163 -0
  59. package/dist/cli/sqlite_adapter.mjs.map +1 -0
  60. package/dist/cli/static_files.mjs +125 -0
  61. package/dist/cli/static_files.mjs.map +1 -0
  62. package/dist/database.mjs +101 -0
  63. package/dist/database.mjs.map +1 -0
  64. package/dist/database_options.mjs +83 -0
  65. package/dist/database_options.mjs.map +1 -0
  66. package/dist/database_scope.mjs +82 -0
  67. package/dist/database_scope.mjs.map +1 -0
  68. package/dist/endpoint.mjs +50 -0
  69. package/dist/endpoint.mjs.map +1 -0
  70. package/dist/group.mjs +91 -0
  71. package/dist/group.mjs.map +1 -0
  72. package/dist/library.mjs +6 -0
  73. package/dist/library.mjs.map +1 -0
  74. package/dist/longpoll_broker.mjs +214 -0
  75. package/dist/longpoll_broker.mjs.map +1 -0
  76. package/dist/longpoll_options.mjs +102 -0
  77. package/dist/longpoll_options.mjs.map +1 -0
  78. package/dist/longpoll_session.mjs +135 -0
  79. package/dist/longpoll_session.mjs.map +1 -0
  80. package/dist/middleware.mjs +25 -0
  81. package/dist/middleware.mjs.map +1 -0
  82. package/dist/options.mjs +177 -0
  83. package/dist/options.mjs.map +1 -0
  84. package/dist/postgres_adapter.mjs +166 -0
  85. package/dist/postgres_adapter.mjs.map +1 -0
  86. package/dist/postgres_worker.mjs +119 -0
  87. package/dist/postgres_worker.mjs.map +1 -0
  88. package/dist/response.mjs +62 -0
  89. package/dist/response.mjs.map +1 -0
  90. package/dist/routes.mjs +297 -0
  91. package/dist/routes.mjs.map +1 -0
  92. package/dist/runtime.mjs +549 -0
  93. package/dist/runtime.mjs.map +1 -0
  94. package/dist/server.mjs +788 -0
  95. package/dist/server.mjs.map +1 -0
  96. package/dist/server_context.mjs +78 -0
  97. package/dist/server_context.mjs.map +1 -0
  98. package/dist/sqlite_adapter.mjs +163 -0
  99. package/dist/sqlite_adapter.mjs.map +1 -0
  100. package/dist/static_files.mjs +125 -0
  101. package/dist/static_files.mjs.map +1 -0
  102. package/dist/token.mjs +106 -0
  103. package/dist/token.mjs.map +1 -0
  104. package/dist/tokens.mjs +76 -0
  105. package/dist/tokens.mjs.map +1 -0
  106. package/dist/worker.mjs +57 -0
  107. package/dist/worker.mjs.map +1 -0
  108. package/package.json +44 -4
  109. package/src/body_reader.lango +113 -0
  110. package/src/builder.lango +64 -0
  111. package/src/database.lango +66 -0
  112. package/src/database_options.lango +57 -0
  113. package/src/database_scope.lango +59 -0
  114. package/src/endpoint.lango +30 -0
  115. package/src/group.lango +61 -0
  116. package/src/library.lango +10 -0
  117. package/src/longpoll.lua +61 -0
  118. package/src/longpoll_broker.lango +161 -0
  119. package/src/longpoll_options.lango +79 -0
  120. package/src/longpoll_session.lango +90 -0
  121. package/src/middleware.lango +10 -0
  122. package/src/options.lango +144 -0
  123. package/src/postgres_adapter.lango +138 -0
  124. package/src/postgres_worker.lango +114 -0
  125. package/src/response.lango +42 -0
  126. package/src/routes.lango +224 -0
  127. package/src/runtime.lango +382 -0
  128. package/src/server.lango +597 -0
  129. package/src/server_context.lango +58 -0
  130. package/src/sqlite_adapter.lango +127 -0
  131. package/src/static_files.lango +90 -0
  132. package/src/token.lango +70 -0
  133. package/src/tokens.lango +70 -0
  134. package/src/worker.lango +43 -0
@@ -0,0 +1,8 @@
1
+ Copyright 2019 - present Christopher J. Brody and other contributors, as listed in: https://github.com/xmldom/xmldom/graphs/contributors
2
+ Copyright 2012 - 2017 @jindw <jindw@xidea.org> and other contributors, as listed in: https://github.com/jindw/xmldom/graphs/contributors
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,179 @@
1
+ import {
2
+ hostGlobal as __hostGlobal,
3
+ hostGlobalCall as __hostGlobalCall,
4
+ isNumber as __isNumber,
5
+ isString as __isString,
6
+ isBool as __isBool,
7
+ isObject as __isObject,
8
+ isArray as __isArray,
9
+ add as __add,
10
+ requireBoolean as __requireBoolean,
11
+ normalize as __normalize,
12
+ numericAdd as __numericAdd,
13
+ call as __call,
14
+ boundary as __boundary,
15
+ requireArity as __requireArity,
16
+ withArity as __withArity,
17
+ } from "./.lango/runtime.mjs";
18
+
19
+ export class BodyReader {
20
+ #req;
21
+ #res;
22
+ #options;
23
+ #complete;
24
+ #failure;
25
+ #chunks;
26
+ #size;
27
+ #done;
28
+ #timer;
29
+ #data;
30
+ #ended;
31
+ #failed;
32
+ #aborted;
33
+ #closed;
34
+ constructor(req, res, options, complete, failure) {
35
+ __requireArity(arguments.length, [0, 1, 2, 3, 4], "constructor");
36
+ req = __boundary(req);
37
+ res = __boundary(res);
38
+ options = __boundary(options);
39
+ complete = __boundary(complete);
40
+ failure = __boundary(failure);
41
+ this.#req = req;
42
+ this.#res = res;
43
+ this.#options = options;
44
+ this.#complete = complete;
45
+ this.#failure = failure;
46
+ this.#chunks = [];
47
+ this.#size = 0;
48
+ this.#done = false;
49
+ this.#timer = null;
50
+ let data = __withArity((chunk) => {
51
+ chunk = __boundary(chunk);
52
+ this.#accept(chunk);
53
+ return null;
54
+ }, [0], "closure");
55
+ let ended = () => {
56
+ this.#finish();
57
+ return null;
58
+ };
59
+ let failed = __withArity((error) => {
60
+ error = __boundary(error);
61
+ this.#reject(400, "BadRequest");
62
+ return null;
63
+ }, [0], "closure");
64
+ let aborted = () => {
65
+ this.#abandon();
66
+ return null;
67
+ };
68
+ let closed = () => {
69
+ this.#abandon();
70
+ return null;
71
+ };
72
+ this.#data = data;
73
+ this.#ended = ended;
74
+ this.#failed = failed;
75
+ this.#aborted = aborted;
76
+ this.#closed = closed;
77
+ }
78
+ start() {
79
+ let req = __normalize(this.#req);
80
+ let res = __normalize(this.#res);
81
+ if (__requireBoolean(__requireBoolean(__normalize(req.aborted)) || __requireBoolean(__normalize(res.destroyed)))) {
82
+ this.#abandon();
83
+ return null;
84
+ }
85
+ __normalize(req.on("error", __normalize(this.#failed)));
86
+ __normalize(req.on("aborted", __normalize(this.#aborted)));
87
+ __normalize(res.on("close", __normalize(this.#closed)));
88
+ let length = req.headers["content-length"];
89
+ let options = __normalize(this.#options);
90
+ if (__requireBoolean(__requireBoolean(__isString(length)) && __requireBoolean(__normalize(__hostGlobal(["Number"]).parseInt(length, 10)) > __normalize(options.bodyLimit)))) {
91
+ this.#reject(413, "RequestTooLarge");
92
+ return null;
93
+ }
94
+ let expired = () => {
95
+ this.#reject(408, "RequestTimeout");
96
+ return null;
97
+ };
98
+ this.#timer = __hostGlobalCall(["setTimeout"], expired, __normalize(options.bodyTimeoutMs));
99
+ __normalize(req.on("data", __normalize(this.#data)));
100
+ __normalize(req.on("end", __normalize(this.#ended)));
101
+ return null;
102
+ return null;
103
+ }
104
+ #accept(chunk) {
105
+ __requireArity(arguments.length, [0], "accept");
106
+ chunk = __boundary(chunk);
107
+ if (__requireBoolean(__normalize(this.#done))) {
108
+ return null;
109
+ }
110
+ this.#size = __numericAdd(__normalize(this.#size), __normalize(chunk.length));
111
+ let options = __normalize(this.#options);
112
+ if (__requireBoolean(__normalize(this.#size) > __normalize(options.bodyLimit))) {
113
+ this.#reject(413, "RequestTooLarge");
114
+ } else {
115
+ __add(this.#chunks, chunk);
116
+ }
117
+ return null;
118
+ return null;
119
+ }
120
+ #finish() {
121
+ if (__requireBoolean(__normalize(this.#done))) {
122
+ return null;
123
+ }
124
+ this.#done = true;
125
+ let body = __normalize(__normalize(__hostGlobal(["Buffer"]).concat(__normalize(this.#chunks))).toString("utf8"));
126
+ this.#cleanup();
127
+ this.#chunks = [];
128
+ let callback = __normalize(this.#complete);
129
+ __call(callback, body);
130
+ return null;
131
+ return null;
132
+ }
133
+ #abandon() {
134
+ if (__requireBoolean(__normalize(this.#done))) {
135
+ return null;
136
+ }
137
+ this.#done = true;
138
+ this.#cleanup();
139
+ this.#chunks = [];
140
+ return null;
141
+ return null;
142
+ }
143
+ #reject(status, error) {
144
+ __requireArity(arguments.length, [0, 1], "reject");
145
+ status = __boundary(status);
146
+ error = __boundary(error);
147
+ if (__requireBoolean(__normalize(this.#done))) {
148
+ return null;
149
+ }
150
+ this.#done = true;
151
+ this.#cleanup();
152
+ this.#chunks = [];
153
+ let req = __normalize(this.#req);
154
+ let res = __normalize(this.#res);
155
+ __normalize(req.pause());
156
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(res.writableEnded))) && __requireBoolean(!__requireBoolean(__normalize(res.destroyed))))) {
157
+ __normalize(res.setHeader("Connection", "close"));
158
+ __normalize(res.once("finish", () => {
159
+ __normalize(req.destroy());
160
+ return null;
161
+ }));
162
+ let callback = __normalize(this.#failure);
163
+ __call(callback, status, error);
164
+ }
165
+ return null;
166
+ return null;
167
+ }
168
+ #cleanup() {
169
+ __hostGlobalCall(["clearTimeout"], __normalize(this.#timer));
170
+ let req = __normalize(this.#req);
171
+ __normalize(req.removeListener("data", __normalize(this.#data)));
172
+ __normalize(req.removeListener("end", __normalize(this.#ended)));
173
+ __normalize(req.removeListener("aborted", __normalize(this.#aborted)));
174
+ __normalize(this.#res.removeListener("close", __normalize(this.#closed)));
175
+ return null;
176
+ return null;
177
+ }
178
+ }
179
+ //# sourceMappingURL=body_reader.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"body_reader.mjs","sources":["body_reader.lango"],"sourcesContent":["import js global Buffer\nimport js global Number\nimport js global setTimeout\nimport js global clearTimeout\n\nexport class BodyReader\n methods\n start()\n\nBodyReader(req, res, options, complete, failure)\n @req = req\n @res = res\n @options = options\n @complete = complete\n @failure = failure\n @chunks = []\n @size = 0\n @done = false\n @timer = null\n data = (chunk)\n accept(chunk)\n ended = ()\n finish()\n failed = (error)\n reject(400, \"BadRequest\")\n aborted = ()\n abandon()\n closed = ()\n abandon()\n @data = data\n @ended = ended\n @failed = failed\n @aborted = aborted\n @closed = closed\n\nBodyReader:start()\n req = @req\n res = @res\n if req.aborted or res.destroyed\n abandon()\n return null\n req.on(\"error\", @failed)\n req.on(\"aborted\", @aborted)\n res.on(\"close\", @closed)\n length = req.headers[\"content-length\"]\n options = @options\n if is_string(length) and Number/parseInt(length, 10) > options.bodyLimit\n reject(413, \"RequestTooLarge\")\n return null\n expired = ()\n reject(408, \"RequestTimeout\")\n @timer = setTimeout(expired, options.bodyTimeoutMs)\n req.on(\"data\", @data)\n req.on(\"end\", @ended)\n return null\n\nBodyReader:accept(chunk)\n if @done\n return null\n @size = @size + chunk.length\n options = @options\n if @size > options.bodyLimit\n reject(413, \"RequestTooLarge\")\n else\n @chunks.add(chunk)\n return null\n\nBodyReader:finish()\n if @done\n return null\n @done = true\n body = Buffer/concat(@chunks).toString(\"utf8\")\n cleanup()\n @chunks = []\n callback = @complete\n callback(body)\n return null\n\nBodyReader:abandon()\n if @done\n return null\n @done = true\n cleanup()\n @chunks = []\n return null\n\nBodyReader:reject(status, error)\n if @done\n return null\n @done = true\n cleanup()\n @chunks = []\n req = @req\n res = @res\n req.pause()\n if !res.writableEnded and !res.destroyed\n res.setHeader(\"Connection\", \"close\")\n res.once(\"finish\", ()\n capture req\n req.destroy()\n )\n callback = @failure\n callback(status, error)\n return null\n\nBodyReader:cleanup()\n clearTimeout(@timer)\n req = @req\n req.removeListener(\"data\", @data)\n req.removeListener(\"end\", @ended)\n req.removeListener(\"aborted\", @aborted)\n @res.removeListener(\"close\", @closed)\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,101 @@
1
+ import { DatabaseScope } from "./database_scope.mjs";
2
+ import { SqliteAdapter } from "./sqlite_adapter.mjs";
3
+ import { PostgresAdapter } from "./postgres_adapter.mjs";
4
+ import {
5
+ hostGlobal as __hostGlobal,
6
+ contextGet as __contextGet,
7
+ contextSet as __contextSet,
8
+ contextRestore as __contextRestore,
9
+ requireBoolean as __requireBoolean,
10
+ normalize as __normalize,
11
+ boundary as __boundary,
12
+ query as __query,
13
+ requireArity as __requireArity,
14
+ withArity as __withArity,
15
+ } from "./.lango/runtime.mjs";
16
+
17
+ export class Database {
18
+ #root;
19
+ #settings;
20
+ #adapter;
21
+ constructor(settings, root = null) {
22
+ __requireArity(arguments.length, [0], "constructor");
23
+ settings = __boundary(settings);
24
+ root = __boundary(root);
25
+ this.#root = root;
26
+ this.#settings = settings;
27
+ this.#adapter = null;
28
+ }
29
+ open() {
30
+ if (__requireBoolean(__normalize(this.#settings) === null)) {
31
+ return __normalize(__hostGlobal(["Promise"]).resolve(null));
32
+ }
33
+ let name = "node:sqlite";
34
+ if (__requireBoolean(__normalize(this.#settings.type) === "postgresql")) {
35
+ name = "node:worker_threads";
36
+ }
37
+ return import(name).then(__withArity((mod) => {
38
+ const __contextScope = [];
39
+ try {
40
+ mod = __boundary(mod);
41
+ if (__requireBoolean(__normalize(this.#settings.type) === "sqlite")) {
42
+ this.#adapter = new SqliteAdapter(mod, __normalize(this.#settings));
43
+ } else {
44
+ this.#adapter = new PostgresAdapter(mod, __normalize(this.#settings), __normalize(this.#root));
45
+ }
46
+ __contextScope.push(__contextSet("db", this.scope()));
47
+ try {
48
+ let query = __query("select 1", []);
49
+ __normalize(query.execute());
50
+ } finally {
51
+ __normalize(__contextGet("db").finish());
52
+ }
53
+ return null;
54
+ return null;
55
+ } finally {
56
+ __contextRestore(__contextScope);
57
+ }
58
+ }, [0], "closure")).catch(__withArity((error) => {
59
+ const __contextScope = [];
60
+ try {
61
+ error = __boundary(error);
62
+ __normalize(__contextGet("log").err(`Database startup failed: ${error}`));
63
+ this.close();
64
+ throw "Database startup failed; check configuration, compiler version and driver installation";
65
+ return null;
66
+ } finally {
67
+ __contextRestore(__contextScope);
68
+ }
69
+ }, [0], "closure"));
70
+ return null;
71
+ }
72
+ scope() {
73
+ if (__requireBoolean(__normalize(this.#adapter) === null)) {
74
+ return null;
75
+ }
76
+ return new DatabaseScope(__normalize(this.#adapter));
77
+ return null;
78
+ }
79
+ close() {
80
+ if (__requireBoolean(__normalize(this.#adapter) !== null)) {
81
+ __normalize(this.#adapter.close());
82
+ this.#adapter = null;
83
+ }
84
+ return null;
85
+ return null;
86
+ }
87
+ }
88
+ export function prepareDatabase(settings) {
89
+ __requireArity(arguments.length, [0], "prepareDatabase");
90
+ settings = __boundary(settings);
91
+ if (__requireBoolean(__requireBoolean(__requireBoolean(settings === null) || __requireBoolean(__normalize(settings.type) !== "sqlite")) || __requireBoolean(__normalize(settings.file) === ":memory:"))) {
92
+ return __normalize(__hostGlobal(["Promise"]).resolve(null));
93
+ }
94
+ let database = new Database(settings);
95
+ return __normalize(database.open()).finally(() => {
96
+ __normalize(database.close());
97
+ return null;
98
+ });
99
+ return null;
100
+ }
101
+ //# sourceMappingURL=database.mjs.map
@@ -0,0 +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;;;;;;;;;;;;;;"}
@@ -0,0 +1,83 @@
1
+ import * as path from "node:path";
2
+ import {
3
+ hostGlobal as __hostGlobal,
4
+ hostGlobalCall as __hostGlobalCall,
5
+ isNumber as __isNumber,
6
+ isString as __isString,
7
+ isBool as __isBool,
8
+ isObject as __isObject,
9
+ isArray as __isArray,
10
+ entries as __entries,
11
+ requireBoolean as __requireBoolean,
12
+ normalize as __normalize,
13
+ boundary as __boundary,
14
+ requireArity as __requireArity,
15
+ } from "./.lango/runtime.mjs";
16
+
17
+ export function databaseOptions(root, spec, workers) {
18
+ __requireArity(arguments.length, [0, 1, 2], "databaseOptions");
19
+ root = __boundary(root);
20
+ spec = __boundary(spec);
21
+ workers = __boundary(workers);
22
+ let file;
23
+ if (__requireBoolean(spec === null)) {
24
+ return null;
25
+ }
26
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
27
+ throw "Database configuration must be a map";
28
+ }
29
+ let allowed = [];
30
+ if (__normalize(spec.type) === "sqlite") {
31
+ allowed = ["type", "file", "busyTimeoutMs"];
32
+ } else if (__normalize(spec.type) === "postgresql") {
33
+ allowed = ["type", "connectionEnv", "queryTimeoutMs", "connectTimeoutSeconds"];
34
+ } else {
35
+ throw "Database type must be sqlite or postgresql";
36
+ }
37
+ for (const [key, value] of __entries(spec)) {
38
+ if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
39
+ throw `Unknown database option: ${key}`;
40
+ }
41
+ }
42
+ if (__requireBoolean(__normalize(spec.type) === "sqlite")) {
43
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.file)))) || __requireBoolean(__normalize(spec.file) === ""))) {
44
+ throw "SQLite database file is required";
45
+ }
46
+ file = __normalize(spec.file);
47
+ if (__requireBoolean(__requireBoolean(file === ":memory:") && __requireBoolean(workers !== 1))) {
48
+ throw "An in-memory SQLite database requires exactly one worker";
49
+ }
50
+ if (__requireBoolean(file !== ":memory:")) {
51
+ file = __normalize(path.resolve(root, file));
52
+ }
53
+ return { type: "sqlite", file: file, busyTimeoutMs: positive(__normalize(spec.busyTimeoutMs), 5000) };
54
+ }
55
+ let name = __normalize(spec.connectionEnv);
56
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(name))) || __requireBoolean(name === "")) || __requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(__normalize(__hostGlobal(["process"]).env), name)))))) {
57
+ throw "PostgreSQL connectionEnv must name an existing environment variable";
58
+ }
59
+ let connection = __normalize(__hostGlobal(["process"]).env[name]);
60
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(connection))) || __requireBoolean(__requireBoolean(!__requireBoolean(__normalize(connection.startsWith("postgresql://")))) && __requireBoolean(!__requireBoolean(__normalize(connection.startsWith("postgres://"))))))) {
61
+ throw "PostgreSQL connectionEnv must contain a PostgreSQL connection URL";
62
+ }
63
+ return { type: "postgresql", connection: connection, queryTimeoutMs: positive(__normalize(spec.queryTimeoutMs), 5000), connectTimeoutSeconds: positive(__normalize(spec.connectTimeoutSeconds), 5) };
64
+ return null;
65
+ }
66
+
67
+ function positive(value, fallback) {
68
+ __requireArity(arguments.length, [0, 1], "positive");
69
+ value = __boundary(value);
70
+ fallback = __boundary(fallback);
71
+ if (__requireBoolean(value === null)) {
72
+ return fallback;
73
+ }
74
+ if (__requireBoolean(__isString(value))) {
75
+ value = __hostGlobalCall(["Number"], value);
76
+ }
77
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(value)))) || __requireBoolean(value < 1)) || __requireBoolean(value > 2147483647))) {
78
+ throw "Database timeout must be a positive integer within the timer limit";
79
+ }
80
+ return value;
81
+ return null;
82
+ }
83
+ //# sourceMappingURL=database_options.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database_options.mjs","sources":["database_options.lango"],"sourcesContent":["import js \"node:path\" as path\nimport js global Object\nimport js global Number\nimport js global process\n\nexport databaseOptions(root, spec, workers)\n\ndatabaseOptions(root, spec, workers)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"Database configuration must be a map\"\n allowed = []\n switch spec.type\n case \"sqlite\"\n allowed = [\"type\", \"file\", \"busyTimeoutMs\"]\n case \"postgresql\"\n allowed = [\"type\", \"connectionEnv\", \"queryTimeoutMs\", \"connectTimeoutSeconds\"]\n else\n throw \"Database type must be sqlite or postgresql\"\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown database option: ${key}\"\n if spec.type == \"sqlite\"\n if !is_string(spec.file) or spec.file == \"\"\n throw \"SQLite database file is required\"\n file = spec.file\n if file == \":memory:\" and workers != 1\n throw \"An in-memory SQLite database requires exactly one worker\"\n if file != \":memory:\"\n file = path/resolve(root, file)\n return {\n type: \"sqlite\"\n file: file\n busyTimeoutMs: positive(spec.busyTimeoutMs, 5000)\n }\n name = spec.connectionEnv\n if !is_string(name) or name == \"\" or !Object/hasOwn(process.env, name)\n throw \"PostgreSQL connectionEnv must name an existing environment variable\"\n connection = process.env[name]\n if !is_string(connection) or (!connection.startsWith(\"postgresql://\") and !connection.startsWith(\"postgres://\"))\n throw \"PostgreSQL connectionEnv must contain a PostgreSQL connection URL\"\n return {\n type: \"postgresql\"\n connection: connection\n queryTimeoutMs: positive(spec.queryTimeoutMs, 5000)\n connectTimeoutSeconds: positive(spec.connectTimeoutSeconds, 5)\n }\n\npositive(value, fallback)\n if value == null\n return fallback\n if is_string(value)\n value = Number(value)\n if !Number/isSafeInteger(value) or value < 1 or value > 2147483647\n throw \"Database timeout must be a positive integer within the timer limit\"\n return value\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,82 @@
1
+ import {
2
+ hostGlobal as __hostGlobal,
3
+ contextGet as __contextGet,
4
+ contextSet as __contextSet,
5
+ contextRestore as __contextRestore,
6
+ requireBoolean as __requireBoolean,
7
+ normalize as __normalize,
8
+ boundary as __boundary,
9
+ requireArity as __requireArity,
10
+ } from "./.lango/runtime.mjs";
11
+
12
+ export class DatabaseScope {
13
+ parameterStyle;
14
+ #adapter;
15
+ #closed;
16
+ constructor(adapter) {
17
+ __requireArity(arguments.length, [0], "constructor");
18
+ adapter = __boundary(adapter);
19
+ this.#adapter = adapter;
20
+ this.#closed = false;
21
+ this.parameterStyle = __normalize(adapter.parameterStyle);
22
+ }
23
+ execute(sql, parameters) {
24
+ __requireArity(arguments.length, [0, 1], "execute");
25
+ sql = __boundary(sql);
26
+ parameters = __boundary(parameters);
27
+ this.#requireOpen();
28
+ return __normalize(this.#adapter.execute(sql, parameters));
29
+ return null;
30
+ }
31
+ begin() {
32
+ this.#requireOpen();
33
+ return __normalize(this.#adapter.begin());
34
+ return null;
35
+ }
36
+ commit() {
37
+ this.#requireOpen();
38
+ return __normalize(this.#adapter.commit());
39
+ return null;
40
+ }
41
+ rollback() {
42
+ this.#requireOpen();
43
+ return __normalize(this.#adapter.rollback());
44
+ return null;
45
+ }
46
+ transactionState() {
47
+ this.#requireOpen();
48
+ return __normalize(this.#adapter.transactionState());
49
+ return null;
50
+ }
51
+ #requireOpen() {
52
+ if (__requireBoolean(__normalize(this.#closed))) {
53
+ throw new (__hostGlobal(["Error"]))("DatabaseScopeClosed");
54
+ }
55
+ return null;
56
+ return null;
57
+ }
58
+ finish() {
59
+ const __contextScope = [];
60
+ try {
61
+ if (__requireBoolean(__normalize(this.#closed))) {
62
+ return false;
63
+ }
64
+ this.#closed = true;
65
+ let dirty = false;
66
+ try {
67
+ __loop1: while (__requireBoolean(__normalize(this.#adapter.transactionState()) !== null)) {
68
+ dirty = true;
69
+ __normalize(this.#adapter.rollback());
70
+ }
71
+ } catch (error) {
72
+ __normalize(__contextGet("log").err("Database cleanup failed; retiring worker"));
73
+ __normalize(__hostGlobal(["process"]).exit(1));
74
+ }
75
+ return dirty;
76
+ return null;
77
+ } finally {
78
+ __contextRestore(__contextScope);
79
+ }
80
+ }
81
+ }
82
+ //# sourceMappingURL=database_scope.mjs.map
@@ -0,0 +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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,50 @@
1
+ import { middlewareName } from "./middleware.mjs";
2
+ import {
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 Endpoint {
15
+ path;
16
+ module;
17
+ handler;
18
+ auth;
19
+ middleware;
20
+ constructor(spec) {
21
+ __requireArity(arguments.length, [0], "constructor");
22
+ spec = __boundary(spec);
23
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec.path)))) || __requireBoolean(!__requireBoolean(__normalize(spec.path.startsWith("/"))))) || __requireBoolean(__normalize(spec.path.includes("?")))) || __requireBoolean(__normalize(spec.path.includes("#")))) || __requireBoolean(__normalize(spec.path.includes("\\")))) || __requireBoolean(__normalize(spec.path.includes("//"))))) {
24
+ throw "Endpoint requires an absolute URL path";
25
+ }
26
+ if (__requireBoolean(__requireBoolean(__normalize(spec.path.includes("*"))) && __requireBoolean(__requireBoolean(!__requireBoolean(__normalize(spec.path.endsWith("/*")))) || __requireBoolean(__normalize(spec.path.indexOf("*")) !== __normalize(spec.path.lastIndexOf("*")))))) {
27
+ throw "Only trailing /* wildcards are supported";
28
+ }
29
+ for (const key of ["module", "handler"]) {
30
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(spec[key])))) || __requireBoolean(__normalize(spec[key]) === ""))) {
31
+ throw `Endpoint requires ${key}`;
32
+ }
33
+ }
34
+ this.path = __normalize(spec.path);
35
+ this.module = __normalize(spec.module);
36
+ this.handler = __normalize(spec.handler);
37
+ this.middleware = __normalize(spec.middleware);
38
+ if (__requireBoolean(__normalize(this.middleware) !== null)) {
39
+ middlewareName(__normalize(this.middleware));
40
+ }
41
+ this.auth = __normalize(spec.auth);
42
+ if (__requireBoolean(__normalize(this.auth) === null)) {
43
+ this.auth = "login";
44
+ }
45
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(this.auth)))) || __requireBoolean(__normalize(this.auth) === "")) || __requireBoolean(__normalize(this.auth.includes(","))))) {
46
+ throw "Endpoint auth must name one permission";
47
+ }
48
+ }
49
+ }
50
+ //# sourceMappingURL=endpoint.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"endpoint.mjs","sources":["endpoint.lango"],"sourcesContent":["import \"./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 if !is_string(spec.path) or !spec.path.startsWith(\"/\") or spec.path.includes(\"?\") or spec.path.includes(\"#\") or spec.path.includes(\"\\\\\") or spec.path.includes(\"//\")\n throw \"Endpoint requires an absolute URL path\"\n if spec.path.includes(\"*\") and (!spec.path.endsWith(\"/*\") or spec.path.indexOf(\"*\") != spec.path.lastIndexOf(\"*\"))\n throw \"Only trailing /* wildcards are supported\"\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":";;;;;;;;;;;;;AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}