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

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 (105) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +431 -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 +69 -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 +44 -0
  41. package/dist/cli/endpoint.mjs.map +1 -0
  42. package/dist/cli/options.mjs +165 -0
  43. package/dist/cli/options.mjs.map +1 -0
  44. package/dist/cli/postgres_adapter.mjs +166 -0
  45. package/dist/cli/postgres_adapter.mjs.map +1 -0
  46. package/dist/cli/routes.mjs +233 -0
  47. package/dist/cli/routes.mjs.map +1 -0
  48. package/dist/cli/server.mjs +572 -0
  49. package/dist/cli/server.mjs.map +1 -0
  50. package/dist/cli/sqlite_adapter.mjs +163 -0
  51. package/dist/cli/sqlite_adapter.mjs.map +1 -0
  52. package/dist/database.mjs +101 -0
  53. package/dist/database.mjs.map +1 -0
  54. package/dist/database_options.mjs +83 -0
  55. package/dist/database_options.mjs.map +1 -0
  56. package/dist/database_scope.mjs +82 -0
  57. package/dist/database_scope.mjs.map +1 -0
  58. package/dist/endpoint.mjs +44 -0
  59. package/dist/endpoint.mjs.map +1 -0
  60. package/dist/group.mjs +75 -0
  61. package/dist/group.mjs.map +1 -0
  62. package/dist/library.mjs +6 -0
  63. package/dist/library.mjs.map +1 -0
  64. package/dist/options.mjs +165 -0
  65. package/dist/options.mjs.map +1 -0
  66. package/dist/postgres_adapter.mjs +166 -0
  67. package/dist/postgres_adapter.mjs.map +1 -0
  68. package/dist/postgres_worker.mjs +119 -0
  69. package/dist/postgres_worker.mjs.map +1 -0
  70. package/dist/response.mjs +62 -0
  71. package/dist/response.mjs.map +1 -0
  72. package/dist/routes.mjs +233 -0
  73. package/dist/routes.mjs.map +1 -0
  74. package/dist/runtime.mjs +475 -0
  75. package/dist/runtime.mjs.map +1 -0
  76. package/dist/server.mjs +568 -0
  77. package/dist/server.mjs.map +1 -0
  78. package/dist/sqlite_adapter.mjs +163 -0
  79. package/dist/sqlite_adapter.mjs.map +1 -0
  80. package/dist/token.mjs +106 -0
  81. package/dist/token.mjs.map +1 -0
  82. package/dist/tokens.mjs +76 -0
  83. package/dist/tokens.mjs.map +1 -0
  84. package/dist/worker.mjs +57 -0
  85. package/dist/worker.mjs.map +1 -0
  86. package/package.json +39 -4
  87. package/src/body_reader.lango +113 -0
  88. package/src/builder.lango +53 -0
  89. package/src/database.lango +66 -0
  90. package/src/database_options.lango +57 -0
  91. package/src/database_scope.lango +59 -0
  92. package/src/endpoint.lango +23 -0
  93. package/src/group.lango +47 -0
  94. package/src/library.lango +10 -0
  95. package/src/options.lango +128 -0
  96. package/src/postgres_adapter.lango +138 -0
  97. package/src/postgres_worker.lango +114 -0
  98. package/src/response.lango +42 -0
  99. package/src/routes.lango +160 -0
  100. package/src/runtime.lango +320 -0
  101. package/src/server.lango +432 -0
  102. package/src/sqlite_adapter.lango +127 -0
  103. package/src/token.lango +70 -0
  104. package/src/tokens.lango +70 -0
  105. package/src/worker.lango +43 -0
@@ -0,0 +1,163 @@
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
@@ -0,0 +1 @@
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;;;;;;;;;;;;;;"}
package/dist/token.mjs ADDED
@@ -0,0 +1,106 @@
1
+ import { contains } from "./options.mjs";
2
+ import {
3
+ hostGlobal as __hostGlobal,
4
+ fromJson as __fromJson,
5
+ toJson as __toJson,
6
+ isNumber as __isNumber,
7
+ isString as __isString,
8
+ isBool as __isBool,
9
+ isObject as __isObject,
10
+ isArray as __isArray,
11
+ requireBoolean as __requireBoolean,
12
+ normalize as __normalize,
13
+ boundary as __boundary,
14
+ requireArity as __requireArity,
15
+ } from "./.lango/runtime.mjs";
16
+
17
+ export class Token {
18
+ clientId;
19
+ auth;
20
+ expiresAt;
21
+ #claims;
22
+ #readonly;
23
+ constructor(claims, readonly = false) {
24
+ __requireArity(arguments.length, [0], "constructor");
25
+ claims = __boundary(claims);
26
+ readonly = __boundary(readonly);
27
+ this.#claims = __fromJson(__toJson(claims));
28
+ this.#readonly = readonly;
29
+ this.clientId = __normalize(claims.clientId);
30
+ this.auth = __normalize(claims.auth);
31
+ this.expiresAt = __normalize(claims.exp);
32
+ }
33
+ AddAuth(permission) {
34
+ __requireArity(arguments.length, [0], "AddAuth");
35
+ permission = __boundary(permission);
36
+ this.#writable();
37
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(permission))) || __requireBoolean(__normalize(permission.trim()) === "")) || __requireBoolean(__normalize(permission.includes(","))))) {
38
+ throw "Permission must be a nonempty name without commas";
39
+ }
40
+ permission = __normalize(permission.trim());
41
+ if (__requireBoolean(!__requireBoolean(this.hasAuth(permission)))) {
42
+ if (__requireBoolean(__normalize(this.auth) === "")) {
43
+ this.auth = permission;
44
+ } else {
45
+ this.auth = `${__normalize(this.auth)}, ${permission}`;
46
+ }
47
+ }
48
+ return this;
49
+ return null;
50
+ }
51
+ clearAuth() {
52
+ this.#writable();
53
+ this.auth = "";
54
+ return this;
55
+ return null;
56
+ }
57
+ hasAuth(permission) {
58
+ __requireArity(arguments.length, [0], "hasAuth");
59
+ permission = __boundary(permission);
60
+ for (const name of __normalize(this.auth.split(","))) {
61
+ if (__requireBoolean(__normalize(name.trim()) === permission)) {
62
+ return true;
63
+ }
64
+ }
65
+ return false;
66
+ return null;
67
+ }
68
+ get(name) {
69
+ __requireArity(arguments.length, [0], "get");
70
+ name = __boundary(name);
71
+ if (__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(__normalize(this.#claims), name))))) {
72
+ return null;
73
+ }
74
+ return __fromJson(__toJson(__normalize(this.#claims[name])));
75
+ return null;
76
+ }
77
+ set(name, value) {
78
+ __requireArity(arguments.length, [0, 1], "set");
79
+ name = __boundary(name);
80
+ value = __boundary(value);
81
+ this.#writable();
82
+ if (__requireBoolean(contains(["clientId", "auth", "iat", "exp", "iss", "aud", "validatedAt", "nbf", "__proto__", "constructor", "prototype"], name))) {
83
+ throw `Reserved token field: ${name}`;
84
+ }
85
+ this.#claims[name] = __fromJson(__toJson(value));
86
+ return this;
87
+ return null;
88
+ }
89
+ snapshot() {
90
+ let claims = __fromJson(__toJson(__normalize(this.#claims)));
91
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(this.clientId) !== __normalize(claims.clientId)) || __requireBoolean(__normalize(this.expiresAt) !== __normalize(claims.exp))) || __requireBoolean(!__requireBoolean(__isString(__normalize(this.auth)))))) {
92
+ throw "Token identity and expiry cannot be changed";
93
+ }
94
+ claims.auth = __normalize(this.auth);
95
+ return claims;
96
+ return null;
97
+ }
98
+ #writable() {
99
+ if (__requireBoolean(__normalize(this.#readonly))) {
100
+ throw "Token is read-only during this event";
101
+ }
102
+ return null;
103
+ return null;
104
+ }
105
+ }
106
+ //# sourceMappingURL=token.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token.mjs","sources":["token.lango"],"sourcesContent":["import \"./options\"\n contains(items, value)\nimport js global Object\n\nexport class Token(claims, readonly = false)\n properties\n clientId\n auth\n expiresAt\n methods\n AddAuth(permission)\n clearAuth()\n hasAuth(permission)\n get(name)\n set(name, value)\n snapshot()\n\nToken(claims, readonly = false)\n @claims = from_json(to_json(claims))\n @readonly = readonly\n @clientId = claims.clientId\n @auth = claims.auth\n @expiresAt = claims.exp\n\nToken:AddAuth(permission)\n writable()\n if !is_string(permission) or permission.trim() == \"\" or permission.includes(\",\")\n throw \"Permission must be a nonempty name without commas\"\n permission = permission.trim()\n if !hasAuth(permission)\n if @auth == \"\"\n @auth = permission\n else\n @auth = \"${@auth}, ${permission}\"\n return @this\n\nToken:clearAuth()\n writable()\n @auth = \"\"\n return @this\n\nToken:hasAuth(permission)\n for @auth.split(\",\") as name\n if name.trim() == permission\n return true\n return false\n\nToken:get(name)\n if !Object/hasOwn(@claims, name)\n return null\n return from_json(to_json(@claims[name]))\n\nToken:set(name, value)\n writable()\n if contains([\"clientId\", \"auth\", \"iat\", \"exp\", \"iss\", \"aud\", \"validatedAt\", \"nbf\", \"__proto__\", \"constructor\", \"prototype\"], name)\n throw \"Reserved token field: ${name}\"\n @claims[name] = from_json(to_json(value))\n return @this\n\nToken:snapshot()\n claims = from_json(to_json(@claims))\n if @clientId != claims.clientId or @expiresAt != claims.exp or !is_string(@auth)\n throw \"Token identity and expiry cannot be changed\"\n claims.auth = @auth\n return claims\n\nToken:writable()\n if @readonly\n throw \"Token is read-only during this event\"\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,76 @@
1
+ import * as jwt from "jsonwebtoken";
2
+ import * as crypto from "node:crypto";
3
+ import {
4
+ hostGlobal as __hostGlobal,
5
+ isNumber as __isNumber,
6
+ isString as __isString,
7
+ isBool as __isBool,
8
+ isObject as __isObject,
9
+ isArray as __isArray,
10
+ requireBoolean as __requireBoolean,
11
+ normalize as __normalize,
12
+ finite as __finite,
13
+ numericAdd as __numericAdd,
14
+ boundary as __boundary,
15
+ requireArity as __requireArity,
16
+ } from "./.lango/runtime.mjs";
17
+
18
+ export class Tokens {
19
+ #settings;
20
+ constructor(settings) {
21
+ __requireArity(arguments.length, [0], "constructor");
22
+ settings = __boundary(settings);
23
+ this.#settings = settings;
24
+ }
25
+ create() {
26
+ let settings = __normalize(this.#settings);
27
+ let now = __normalize(__hostGlobal(["Math"]).floor(__finite(__normalize(__hostGlobal(["Date"]).now()) / 1000)));
28
+ return { clientId: __normalize(crypto.randomUUID()), auth: "", iat: now, exp: __numericAdd(now, __normalize(settings.ttlSeconds)), iss: __normalize(settings.issuer), aud: __normalize(settings.audience), validatedAt: __normalize(__hostGlobal(["Date"]).now()) };
29
+ return null;
30
+ }
31
+ read(header) {
32
+ __requireArity(arguments.length, [0], "read");
33
+ header = __boundary(header);
34
+ let settings = __normalize(this.#settings);
35
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(header))) || __requireBoolean(__normalize(header.length) > 16384)) || __requireBoolean(__normalize(__normalize(header.slice(0, 7)).toLowerCase()) !== "bearer ")) || __requireBoolean(__normalize(header.length) === 7))) {
36
+ throw "InvalidToken";
37
+ }
38
+ let encoded = __normalize(header.slice(7));
39
+ let decoded = __normalize(jwt.default.decode(encoded, { complete: true }));
40
+ if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(decoded))) || __requireBoolean(!__requireBoolean(__isObject(__normalize(decoded.header)))))) {
41
+ throw "InvalidToken";
42
+ }
43
+ let head = __normalize(decoded.header);
44
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__normalize(head.alg) !== "HS256") || __requireBoolean(__normalize(head.typ) !== "JWT")) || __requireBoolean(!__requireBoolean(__isString(__normalize(head.kid))))) || __requireBoolean(__normalize(head.crit) !== null)) || __requireBoolean(__normalize(head.b64) !== null))) {
45
+ throw "InvalidToken";
46
+ }
47
+ let keys = __normalize(settings.keys);
48
+ if (__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Object"]).hasOwn(keys, __normalize(head.kid)))))) {
49
+ throw "InvalidToken";
50
+ }
51
+ let claims = __normalize(jwt.default.verify(encoded, __normalize(keys[__normalize(head.kid)]), { algorithms: ["HS256"], issuer: __normalize(settings.issuer), audience: __normalize(settings.audience) }));
52
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(claims))) || __requireBoolean(!__requireBoolean(__isString(__normalize(claims.clientId))))) || __requireBoolean(__normalize(claims.clientId) === "")) || __requireBoolean(!__requireBoolean(__isString(__normalize(claims.auth)))))) {
53
+ throw "InvalidToken";
54
+ }
55
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(__normalize(claims.iat))))) || __requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(__normalize(claims.exp)))))) || __requireBoolean(__normalize(claims.exp) <= __normalize(claims.iat))) || __requireBoolean(__finite(__normalize(claims.exp) - __normalize(claims.iat)) > __normalize(settings.ttlSeconds))) || __requireBoolean(__finite(__normalize(claims.iat) * 1000) > __normalize(__hostGlobal(["Date"]).now())))) {
56
+ throw "InvalidToken";
57
+ }
58
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(__hostGlobal(["Number"]).isSafeInteger(__normalize(claims.validatedAt))))) || __requireBoolean(__normalize(claims.validatedAt) < __finite(__normalize(claims.iat) * 1000))) || __requireBoolean(__normalize(claims.validatedAt) > __normalize(__hostGlobal(["Date"]).now())))) {
59
+ throw "InvalidToken";
60
+ }
61
+ return claims;
62
+ return null;
63
+ }
64
+ sign(claims) {
65
+ __requireArity(arguments.length, [0], "sign");
66
+ claims = __boundary(claims);
67
+ let settings = __normalize(this.#settings);
68
+ if (__requireBoolean(__finite(__normalize(claims.exp) * 1000) <= __normalize(__hostGlobal(["Date"]).now()))) {
69
+ throw "TokenExpired";
70
+ }
71
+ let keys = __normalize(settings.keys);
72
+ return __normalize(jwt.default.sign(claims, __normalize(keys[__normalize(settings.activeKeyId)]), { algorithm: "HS256", header: { typ: "JWT", kid: __normalize(settings.activeKeyId) } }));
73
+ return null;
74
+ }
75
+ }
76
+ //# sourceMappingURL=tokens.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tokens.mjs","sources":["tokens.lango"],"sourcesContent":["import js \"jsonwebtoken\" as jwt\nimport js \"node:crypto\" as crypto\nimport js global Object\nimport js global Date\nimport js global Math\nimport js global Number\n\nexport class Tokens(settings)\n methods\n read(header)\n sign(claims)\n create()\n\nTokens(settings)\n @settings = settings\n\nTokens:create()\n settings = @settings\n now = Math/floor(Date/now() / 1000)\n return {\n clientId: crypto/randomUUID()\n auth: \"\"\n iat: now\n exp: now + settings.ttlSeconds\n iss: settings.issuer\n aud: settings.audience\n validatedAt: Date/now()\n }\n\nTokens:read(header)\n settings = @settings\n if !is_string(header) or header.length > 16384 or header.slice(0, 7).toLowerCase() != \"bearer \" or header.length == 7\n throw \"InvalidToken\"\n encoded = header.slice(7)\n decoded = jwt/default/decode(encoded, {\n complete: true\n })\n if !is_object(decoded) or !is_object(decoded.header)\n throw \"InvalidToken\"\n head = decoded.header\n if head.alg != \"HS256\" or head.typ != \"JWT\" or !is_string(head.kid) or head.crit != null or head.b64 != null\n throw \"InvalidToken\"\n keys = settings.keys\n if !Object/hasOwn(keys, head.kid)\n throw \"InvalidToken\"\n claims = jwt/default/verify(encoded, keys[head.kid], {\n algorithms: [\"HS256\"]\n issuer: settings.issuer\n audience: settings.audience\n })\n if !is_object(claims) or !is_string(claims.clientId) or claims.clientId == \"\" or !is_string(claims.auth)\n throw \"InvalidToken\"\n if !Number/isSafeInteger(claims.iat) or !Number/isSafeInteger(claims.exp) or claims.exp <= claims.iat or claims.exp - claims.iat > settings.ttlSeconds or claims.iat * 1000 > Date/now()\n throw \"InvalidToken\"\n if !Number/isSafeInteger(claims.validatedAt) or claims.validatedAt < claims.iat * 1000 or claims.validatedAt > Date/now()\n throw \"InvalidToken\"\n return claims\n\nTokens:sign(claims)\n settings = @settings\n if claims.exp * 1000 <= Date/now()\n throw \"TokenExpired\"\n keys = settings.keys\n return jwt/default/sign(claims, keys[settings.activeKeyId], {\n algorithm: \"HS256\"\n header: {\n typ: \"JWT\"\n kid: settings.activeKeyId\n }\n })\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,57 @@
1
+ import { Runtime } from "./runtime.mjs";
2
+ import * as threads from "node:worker_threads";
3
+ import {
4
+ hostGlobal as __hostGlobal,
5
+ hostGlobalCall as __hostGlobalCall,
6
+ requireBoolean as __requireBoolean,
7
+ normalize as __normalize,
8
+ boundary as __boundary,
9
+ requireArity as __requireArity,
10
+ withArity as __withArity,
11
+ } from "./.lango/runtime.mjs";
12
+
13
+ let runtime = new Runtime(__normalize(threads.workerData));
14
+
15
+ export let execute = initialize();
16
+ function initialize() {
17
+ return __normalize(runtime.preload()).then(__withArity((ignored) => {
18
+ ignored = __boundary(ignored);
19
+ __normalize(threads.parentPort.postMessage({ kind: "ready", threadId: __normalize(threads.threadId) }));
20
+ registerCleanup();
21
+ return workerHandler();
22
+ return null;
23
+ }, [0], "closure"));
24
+ return null;
25
+ }
26
+
27
+ function run(task) {
28
+ __requireArity(arguments.length, [0], "run");
29
+ task = __boundary(task);
30
+ let pending;
31
+ if (__requireBoolean(__normalize(task.close) === true)) {
32
+ __normalize(runtime.close());
33
+ pending = new (__hostGlobal(["Promise"]))(__withArity((resolve) => {
34
+ resolve = __boundary(resolve);
35
+ __hostGlobalCall(["setTimeout"], resolve, 10);
36
+ return null;
37
+ }, [0], "closure"));
38
+ return pending;
39
+ }
40
+ return __normalize(runtime.handle(task));
41
+ return null;
42
+ }
43
+
44
+ function workerHandler() {
45
+ return run;
46
+ return null;
47
+ }
48
+
49
+ function registerCleanup() {
50
+ __normalize(__hostGlobal(["process"]).once("exit", () => {
51
+ __normalize(runtime.close());
52
+ return null;
53
+ }));
54
+ return null;
55
+ return null;
56
+ }
57
+ //# sourceMappingURL=worker.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker.mjs","sources":["worker.lango"],"sourcesContent":["import js \"node:worker_threads\" as threads\nimport js global Promise\nimport js global process\nimport js global setTimeout\nimport \"./runtime\"\n class Runtime\n\nexport global execute\n\nglobal runtime = new Runtime(threads.workerData)\nglobal execute = initialize()\n\ninitialize()\n global runtime\n return chain runtime.preload()\n then(ignored)\n capture threads\n threads/parentPort/postMessage({\n kind: \"ready\"\n threadId: threads.threadId\n })\n registerCleanup()\n return workerHandler()\n\nrun(task)\n global runtime\n if task.close == true\n runtime.close()\n pending = new Promise((resolve)\n setTimeout(resolve, 10)\n )\n return pending\n return runtime.handle(task)\n\nworkerHandler()\n return run\n\nregisterCleanup()\n process.once(\"exit\", ()\n global runtime\n runtime.close()\n )\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;AASA;;AACA;AAEA;;;;;;;;;;;AAYA;;;;;;;;;;;;;;;;;AAUA;;;;;AAGA;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
2
  "name": "@lango-project/lango-server",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "A small JSON HTTP server for Lango applications.",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "engines": {
11
+ "node": ">=24"
12
+ },
13
+ "exports": {
14
+ ".": "./dist/library.mjs"
15
+ },
16
+ "bin": {
17
+ "lango-server": "dist/cli/server.mjs"
18
+ },
19
+ "files": [
20
+ "dist/**",
21
+ "src/*.lango",
22
+ "README.md",
23
+ "LICENSE",
24
+ "NOTICE"
25
+ ],
26
+ "scripts": {
27
+ "build": "lango run tools/clean.lango && lango build src/library.lango --out dist/library.mjs && lango build src/server.lango --out dist/cli/server.mjs --executable && lango build src/worker.lango --out dist/worker.mjs && lango build examples/application/handlers.lango --out examples/application/dist/handlers.mjs && lango build src/postgres_worker.lango --out dist/postgres_worker.mjs && lango build examples/database/handlers.lango --out examples/database/dist/handlers.mjs",
28
+ "test": "npm run build && lango build src/test/handlers.lango --out .test-output/handlers.mjs && lango build src/test/sql_handlers.lango --out .test-output/sql_handlers.mjs && lango test src/test/database.lango --build-dir .test-output/database && lango test src/test/unit.lango --build-dir .test-output/unit && lango test src/test/http.lango --build-dir .test-output/http && lango test src/test/standalone.lango --build-dir .test-output/standalone",
29
+ "start": "node dist/cli/server.mjs examples/application",
30
+ "prepack": "npm run build",
31
+ "test:postgres": "lango build src/test/sql_handlers.lango --out .test-output/sql_handlers.mjs && lango test src/test/database.lango --build-dir .test-output/database-postgres"
32
+ },
33
+ "dependencies": {
34
+ "jsonwebtoken": "^9.0.3",
35
+ "pg": "^8.23.1",
36
+ "piscina": "^5.3.2"
37
+ },
38
+ "devDependencies": {
39
+ "@lango-project/lango-js": "^0.1.13"
40
+ }
41
+ }
@@ -0,0 +1,113 @@
1
+ import js global Buffer
2
+ import js global Number
3
+ import js global setTimeout
4
+ import js global clearTimeout
5
+
6
+ export class BodyReader
7
+ methods
8
+ start()
9
+
10
+ BodyReader(req, res, options, complete, failure)
11
+ @req = req
12
+ @res = res
13
+ @options = options
14
+ @complete = complete
15
+ @failure = failure
16
+ @chunks = []
17
+ @size = 0
18
+ @done = false
19
+ @timer = null
20
+ data = (chunk)
21
+ accept(chunk)
22
+ ended = ()
23
+ finish()
24
+ failed = (error)
25
+ reject(400, "BadRequest")
26
+ aborted = ()
27
+ abandon()
28
+ closed = ()
29
+ abandon()
30
+ @data = data
31
+ @ended = ended
32
+ @failed = failed
33
+ @aborted = aborted
34
+ @closed = closed
35
+
36
+ BodyReader:start()
37
+ req = @req
38
+ res = @res
39
+ if req.aborted or res.destroyed
40
+ abandon()
41
+ return null
42
+ req.on("error", @failed)
43
+ req.on("aborted", @aborted)
44
+ res.on("close", @closed)
45
+ length = req.headers["content-length"]
46
+ options = @options
47
+ if is_string(length) and Number/parseInt(length, 10) > options.bodyLimit
48
+ reject(413, "RequestTooLarge")
49
+ return null
50
+ expired = ()
51
+ reject(408, "RequestTimeout")
52
+ @timer = setTimeout(expired, options.bodyTimeoutMs)
53
+ req.on("data", @data)
54
+ req.on("end", @ended)
55
+ return null
56
+
57
+ BodyReader:accept(chunk)
58
+ if @done
59
+ return null
60
+ @size = @size + chunk.length
61
+ options = @options
62
+ if @size > options.bodyLimit
63
+ reject(413, "RequestTooLarge")
64
+ else
65
+ @chunks.add(chunk)
66
+ return null
67
+
68
+ BodyReader:finish()
69
+ if @done
70
+ return null
71
+ @done = true
72
+ body = Buffer/concat(@chunks).toString("utf8")
73
+ cleanup()
74
+ @chunks = []
75
+ callback = @complete
76
+ callback(body)
77
+ return null
78
+
79
+ BodyReader:abandon()
80
+ if @done
81
+ return null
82
+ @done = true
83
+ cleanup()
84
+ @chunks = []
85
+ return null
86
+
87
+ BodyReader:reject(status, error)
88
+ if @done
89
+ return null
90
+ @done = true
91
+ cleanup()
92
+ @chunks = []
93
+ req = @req
94
+ res = @res
95
+ req.pause()
96
+ if !res.writableEnded and !res.destroyed
97
+ res.setHeader("Connection", "close")
98
+ res.once("finish", ()
99
+ capture req
100
+ req.destroy()
101
+ )
102
+ callback = @failure
103
+ callback(status, error)
104
+ return null
105
+
106
+ BodyReader:cleanup()
107
+ clearTimeout(@timer)
108
+ req = @req
109
+ req.removeListener("data", @data)
110
+ req.removeListener("end", @ended)
111
+ req.removeListener("aborted", @aborted)
112
+ @res.removeListener("close", @closed)
113
+ return null
@@ -0,0 +1,53 @@
1
+ import js "node:path" as path
2
+ import js global process
3
+ import "./routes"
4
+ class Routes
5
+ import "./group"
6
+ class Group
7
+ import "./options"
8
+ options(root, supplied = null)
9
+ import "./server"
10
+ class Server
11
+
12
+ export class Builder(root = null)
13
+ methods
14
+ configure(settings)
15
+ endpoint(spec)
16
+ group(spec)
17
+ include(spec)
18
+ event(spec)
19
+ build()
20
+
21
+ Builder(root = null)
22
+ if root == null
23
+ root = process.cwd()
24
+ @root = path/resolve(root)
25
+ @settings = {}
26
+ @routes = new Routes(@root)
27
+
28
+ Builder:configure(settings)
29
+ for settings as key: value
30
+ @settings[key] = value
31
+ return @this
32
+
33
+ Builder:endpoint(spec)
34
+ @routes.endpoint(spec)
35
+ return @this
36
+
37
+ Builder:group(spec)
38
+ return new Group(@routes, spec)
39
+
40
+ Builder:include(spec)
41
+ @routes.include(spec)
42
+ return @this
43
+
44
+ Builder:event(spec)
45
+ @routes.event(spec)
46
+ return @this
47
+
48
+ Builder:build()
49
+ return new Server(null, {
50
+ settings: options(@root, @settings)
51
+ routes: @routes.definitions()
52
+ events: @routes.events()
53
+ })