@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,114 @@
1
+ import js "pg" as pg
2
+ import js "node:worker_threads" as threads
3
+ import js global Atomics
4
+ import js global Math
5
+ import js global Number
6
+ import js global Promise
7
+
8
+
9
+ global state = initialState()
10
+ global installed = install()
11
+
12
+ install()
13
+ port = threads.workerData.port
14
+ port.on("message", (task)
15
+ return chain Promise/resolve(null)
16
+ then(ignored)
17
+ capture task
18
+ return execute(task)
19
+ then(result)
20
+ reply(result)
21
+ return null
22
+ catch(error)
23
+ global state
24
+ if state.transaction != null
25
+ state.transaction = "failed"
26
+ if error.code == null or error.code.length != 5 or error.code.startsWith("08")
27
+ state.dead = true
28
+ position = null
29
+ if error.position != null
30
+ position = Number/parseInt(error.position, 10)
31
+ reply({
32
+ failed: true
33
+ dead: state.dead
34
+ code: error.code
35
+ position: position
36
+ state: state.transaction
37
+ })
38
+ return null
39
+ )
40
+ return null
41
+
42
+ execute(task)
43
+ global state
44
+ settings = threads.workerData.settings
45
+ switch task.action
46
+ case "connect"
47
+ state.client = new pg/Client({
48
+ connectionString: settings.connection
49
+ connectionTimeoutMillis: Math/min(2147483647, settings.connectTimeoutSeconds * 1000)
50
+ statement_timeout: settings.queryTimeoutMs
51
+ application_name: "lango-server"
52
+ })
53
+ client = state.client
54
+ client.on("error", (error)
55
+ global state
56
+ state.dead = true
57
+ )
58
+ return chain client.connect()
59
+ then(ignored)
60
+ return success([], null)
61
+ case "query"
62
+ return chain state.client.query({
63
+ text: task.sql
64
+ values: task.parameters
65
+ queryMode: "extended"
66
+ })
67
+ then(result)
68
+ global state
69
+ switch result.command
70
+ case "BEGIN"
71
+ state.transaction = "active"
72
+ case "COMMIT"
73
+ case "ROLLBACK"
74
+ state.transaction = null
75
+ changes = null
76
+ switch result.command
77
+ case "INSERT"
78
+ case "UPDATE"
79
+ case "DELETE"
80
+ case "MERGE"
81
+ changes = result.rowCount
82
+ return success(result.rows, changes)
83
+ case "close"
84
+ return chain state.client.end()
85
+ then(ignored)
86
+ return success([], null)
87
+ else
88
+ throw "Unknown PostgreSQL operation"
89
+
90
+ success(rows, changes)
91
+ global state
92
+ return {
93
+ failed: false
94
+ dead: false
95
+ state: state.transaction
96
+ rows: rows
97
+ changes: changes
98
+ lastInsertRowid: null
99
+ }
100
+
101
+ reply(result)
102
+ port = threads.workerData.port
103
+ signal = threads.workerData.signal
104
+ port.postMessage(result)
105
+ Atomics/store(signal, 0, 1)
106
+ Atomics/notify(signal, 0)
107
+ return null
108
+
109
+ initialState()
110
+ return {
111
+ client: null
112
+ transaction: null
113
+ dead: false
114
+ }
@@ -0,0 +1,42 @@
1
+ import js "node:http" as http
2
+
3
+ import "./options"
4
+ contains(items, value)
5
+ export class Response()
6
+ methods
7
+ status(code)
8
+ header(name, value)
9
+ result(body)
10
+
11
+ Response()
12
+ @status = 200
13
+ @headers = {
14
+ "content-type": "application/json; charset=utf-8"
15
+ "cache-control": "no-store"
16
+ }
17
+
18
+ Response:status(code)
19
+ if !is_number(code) or code % 1 != 0 or code < 200 or code > 599 or code == 204 or code == 205 or code == 304
20
+ throw "Response requires a JSON-compatible HTTP status"
21
+ @status = code
22
+ return @this
23
+
24
+ Response:header(name, value)
25
+ if !is_string(name) or !is_string(value)
26
+ throw "Header name and value must be strings"
27
+ name = name.toLowerCase()
28
+ if contains(["x-lango-token", "content-length", "transfer-encoding", "connection", "content-type"], name)
29
+ throw "Reserved response header: ${name}"
30
+ if name.includes("\r") or name.includes("\n") or value.includes("\r") or value.includes("\n")
31
+ throw "Invalid response header"
32
+ http/validateHeaderName(name)
33
+ http/validateHeaderValue(name, value)
34
+ @headers[name] = value
35
+ return @this
36
+
37
+ Response:result(body)
38
+ return {
39
+ status: @status
40
+ headers: from_json(to_json(@headers))
41
+ body: body
42
+ }
@@ -0,0 +1,224 @@
1
+ import "./options"
2
+ contains(items, value)
3
+ import js "node:fs" as fs
4
+ import js "node:path" as path
5
+ import js "node:module" as modules
6
+ import "./options"
7
+ attributes(tag, allowed)
8
+ import js global Object
9
+ import "./middleware"
10
+ middlewareName(name)
11
+ import "./endpoint"
12
+ class Endpoint
13
+
14
+ import "./longpoll_options"
15
+ longpollOptions(spec)
16
+
17
+ export class Routes(root)
18
+ methods
19
+ endpoint(spec)
20
+ addMiddleware(name, spec)
21
+ middlewares()
22
+ longpoll(spec)
23
+ include(spec, auth = null)
24
+ xml(file)
25
+ definitions()
26
+ events()
27
+ event(spec)
28
+
29
+ Routes(root)
30
+ @root = path/resolve(root)
31
+ @items = []
32
+ @events = {}
33
+ @middlewares = Object/create(null)
34
+
35
+ Routes:endpoint(spec)
36
+ item = new Endpoint(spec)
37
+ for @items as other
38
+ if other.path == item.path
39
+ throw "Duplicate endpoint: ${item.path}"
40
+ @items.add({
41
+ path: item.path
42
+ module: resolveModule(item.module, @root)
43
+ handler: item.handler
44
+ auth: item.auth
45
+ middleware: item.middleware
46
+ })
47
+ return @this
48
+
49
+ Routes:addMiddleware(name, spec)
50
+ middlewareName(name)
51
+ if Object/hasOwn(@middlewares, name)
52
+ throw "Duplicate middleware: ${name}"
53
+ if !is_object(spec) or is_array(spec)
54
+ throw "Middleware requires a module and exported handler"
55
+ for spec as key: value
56
+ if key != "module" and key != "handler"
57
+ throw "Unknown middleware option: ${key}"
58
+ if !is_string(spec.handler) or spec.handler == ""
59
+ throw "Middleware handler is required"
60
+ @middlewares[name] = {
61
+ module: resolveModule(spec.module, @root)
62
+ handler: spec.handler
63
+ }
64
+ return @this
65
+
66
+ Routes:middlewares()
67
+ return from_json(to_json(@middlewares))
68
+
69
+ Routes:longpoll(spec)
70
+ item = longpollOptions(spec)
71
+ for @items as other
72
+ if other.path == item.path
73
+ throw "Duplicate endpoint: ${item.path}"
74
+ @items.add(item)
75
+ return @this
76
+
77
+ Routes:event(spec)
78
+ if !contains(["register", "validate", "request"], spec.name)
79
+ throw "Unknown event: ${spec.name}"
80
+ if spec.name in @events
81
+ throw "Duplicate event: ${spec.name}"
82
+ if !is_string(spec.handler) or spec.handler == ""
83
+ throw "Event handler is required"
84
+ @events[spec.name] = {
85
+ module: resolveModule(spec.module, @root)
86
+ handler: spec.handler
87
+ }
88
+ return @this
89
+
90
+ Routes:include(spec, auth = null)
91
+ includeAt(spec, @root, "", [], auth)
92
+ return @this
93
+
94
+ Routes:xml(file)
95
+ readXml(path/resolve(file), "", @root, [], true)
96
+ return @this
97
+
98
+ Routes:definitions()
99
+ return from_json(to_json(@items))
100
+
101
+ Routes:events()
102
+ return from_json(to_json(@events))
103
+
104
+ resolveModule(name, directory)
105
+ if !is_string(name) or name == "" or name.startsWith("node:")
106
+ throw "Application module is required"
107
+ resolver = modules/createRequire(path/join(directory, "package.json"))
108
+ resolved = resolver.resolve(name)
109
+ if !path/isAbsolute(resolved)
110
+ throw "Application module must resolve to a file: ${name}"
111
+ return resolved
112
+
113
+ joinPath(prefix, suffix)
114
+ if !is_string(suffix) or !suffix.startsWith("/")
115
+ throw "Route path must begin with /"
116
+ if prefix == "" or prefix == "/"
117
+ return suffix
118
+ if suffix == "/"
119
+ return "${prefix}/"
120
+ return "${prefix}${suffix}"
121
+
122
+ Routes:includeAt(spec, directory, prefix, stack, inheritedAuth = null)
123
+ if !is_string(spec.module) or spec.module == "" or spec.module.startsWith(".") or spec.module.startsWith("/") or spec.module.startsWith("node:")
124
+ throw "Include requires an npm package name"
125
+ mount = spec.path
126
+ if mount == null
127
+ mount = "/"
128
+ if !is_string(mount)
129
+ throw "Include path must be a string"
130
+ if mount.includes("*") or mount.includes("//") or mount.includes("?") or mount.includes("#") or mount.includes("\\")
131
+ throw "Invalid include prefix"
132
+ if mount.endsWith("/") and mount != "/"
133
+ mount = mount.slice(0, -1)
134
+ combined = joinPath(prefix, mount)
135
+ if combined == "/"
136
+ combined = ""
137
+ file = resolveModule("${spec.module}/routes.xml", directory)
138
+ readXml(file, combined, path/dirname(file), stack, false, inheritedAuth)
139
+ return null
140
+
141
+ Routes:readXml(file, prefix, directory, stack, allowEvents, inheritedAuth = null)
142
+ real = fs/realpathSync(file)
143
+ if stack.includes(real)
144
+ throw "Route include cycle: ${real}"
145
+ root = xml2obj(fs/readFileSync(real, "utf8"))
146
+ if root.tag() != "routes"
147
+ throw "Expected routes root: ${real}"
148
+ attributes(root, [])
149
+ nextStack = stack.slice()
150
+ nextStack.add(real)
151
+ collect(root, prefix, null, inheritedAuth, directory, nextStack, allowEvents)
152
+ return null
153
+
154
+ Routes:collect(parent, prefix, inheritedModule, inheritedAuth, directory, stack, allowEvents)
155
+ if parent.innerText().trim() != ""
156
+ throw "Route XML cannot contain text"
157
+ for parent.children() as tag
158
+ spec = tag.attributes()
159
+ mod = spec.module
160
+ auth = spec.auth
161
+ if mod == null
162
+ mod = inheritedModule
163
+ if auth == null
164
+ auth = inheritedAuth
165
+ switch tag.tag()
166
+ case "endpoint"
167
+ attributes(tag, ["path", "module", "handler", "auth"])
168
+ leaf(tag)
169
+ endpoint({
170
+ path: joinPath(prefix, spec.path)
171
+ module: resolveModule(mod, directory)
172
+ handler: spec.handler
173
+ auth: auth
174
+ })
175
+ case "longpoll"
176
+ attributes(tag, ["path", "auth", "waitTimeoutMs", "retentionMs", "maxMessages", "fallback"])
177
+ leaf(tag)
178
+ spec.path = joinPath(prefix, spec.path)
179
+ spec.auth = auth
180
+ if spec.fallback != null
181
+ spec.fallback = joinPath(prefix, spec.fallback)
182
+ longpoll(spec)
183
+ case "group"
184
+ attributes(tag, ["path", "module", "auth"])
185
+ collect(tag, joinPath(prefix, spec.path), mod, auth, directory, stack, false)
186
+ case "include"
187
+ attributes(tag, ["module", "path"])
188
+ leaf(tag)
189
+ includeAt(spec, directory, prefix, stack, auth)
190
+ case "middleware"
191
+ attributes(tag, ["name", "module", "handler"])
192
+ leaf(tag)
193
+ if !allowEvents
194
+ throw "Middleware declarations are only allowed in the application's root routes.xml"
195
+ addMiddleware(spec.name, {
196
+ module: resolveModule(spec.module, directory)
197
+ handler: spec.handler
198
+ })
199
+ case "event"
200
+ attributes(tag, ["name", "module", "handler"])
201
+ leaf(tag)
202
+ if !allowEvents
203
+ throw "Events are only allowed in the application's root routes.xml"
204
+ event({
205
+ name: spec.name
206
+ module: resolveModule(spec.module, directory)
207
+ handler: spec.handler
208
+ })
209
+ else
210
+ attributes(tag, ["path", "module", "handler", "auth"])
211
+ leaf(tag)
212
+ endpoint({
213
+ path: joinPath(prefix, spec.path)
214
+ module: resolveModule(mod, directory)
215
+ handler: spec.handler
216
+ auth: auth
217
+ middleware: tag.tag()
218
+ })
219
+ return null
220
+
221
+ leaf(tag)
222
+ if tag.children().length != 0 or tag.innerText().trim() != ""
223
+ throw "Expected an empty ${tag.tag()} element"
224
+ return null