@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,127 @@
1
+ import js global Error
2
+ import js "node:fs" as fs
3
+ import js "node:path" as path
4
+ import js global Reflect
5
+ import js global Object
6
+ import js global Number
7
+
8
+ export class SqliteAdapter(mod, settings)
9
+ properties
10
+ parameterStyle
11
+ methods
12
+ execute(sql, parameters)
13
+ begin()
14
+ commit()
15
+ rollback()
16
+ transactionState()
17
+ close()
18
+
19
+ SqliteAdapter(mod, settings)
20
+ if settings.file != ":memory:"
21
+ fs/mkdirSync(path/dirname(settings.file), {
22
+ recursive: true
23
+ })
24
+ @connection = Reflect/construct(mod.DatabaseSync, [settings.file])
25
+ @depth = 0
26
+ @state = null
27
+ @parameterStyle = "named"
28
+ try
29
+ @connection.exec("PRAGMA busy_timeout=${settings.busyTimeoutMs}")
30
+ @connection.exec("PRAGMA foreign_keys=ON")
31
+ modeQuery = @connection.prepare("PRAGMA journal_mode")
32
+ mode = modeQuery.get()
33
+ if settings.file != ":memory:" and mode.journal_mode != "wal"
34
+ @connection.exec("PRAGMA journal_mode=WAL")
35
+ catch error
36
+ @connection.close()
37
+ throw "SQLite initialization failed"
38
+
39
+ SqliteAdapter:execute(sql, parameters)
40
+ bound = {}
41
+ for parameters as name: value
42
+ if is_bool(value)
43
+ bound[name] = Number(value)
44
+ else
45
+ bound[name] = value
46
+ statement = @connection.prepare(sql)
47
+ statement.setReadBigInts(true)
48
+ columns = statement.columns()
49
+ if columns.length > 0
50
+ records = statement.all(bound)
51
+ for records as row
52
+ for row as name: value
53
+ row[name] = normalizeInteger(value)
54
+ return {
55
+ rows: records
56
+ changes: null
57
+ lastInsertRowid: null
58
+ }
59
+ result = statement.run(bound)
60
+ changes = null
61
+ generated = null
62
+ operation = sql.trim()
63
+ operation = operation.toLowerCase()
64
+ if operation.startsWith("insert") or operation.startsWith("update") or operation.startsWith("delete") or operation.startsWith("with")
65
+ changes = normalizeInteger(result.changes)
66
+ if operation.startsWith("insert") and changes == 1
67
+ generated = normalizeInteger(result.lastInsertRowid)
68
+ return {
69
+ rows: []
70
+ changes: changes
71
+ lastInsertRowid: generated
72
+ }
73
+
74
+ SqliteAdapter:begin()
75
+ if @depth == 0 and !@connection.isTransaction
76
+ @connection.exec("BEGIN")
77
+ @depth = @depth + 1
78
+ if @state == null
79
+ @state = "active"
80
+ return null
81
+
82
+ SqliteAdapter:commit()
83
+ if @depth == 0
84
+ throw new Error("NoActiveTransaction")
85
+ if @depth > 1
86
+ @depth = @depth - 1
87
+ return null
88
+ if @state == "rollback_only"
89
+ rollback()
90
+ throw new Error("TransactionRolledBack")
91
+ @connection.exec("COMMIT")
92
+ @depth = 0
93
+ @state = null
94
+ return null
95
+
96
+ SqliteAdapter:rollback()
97
+ if @depth > 1
98
+ @depth = @depth - 1
99
+ @state = "rollback_only"
100
+ return null
101
+ if @connection.isTransaction
102
+ @connection.exec("ROLLBACK")
103
+ @depth = 0
104
+ @state = null
105
+ return null
106
+
107
+ SqliteAdapter:transactionState()
108
+ if !@connection.isTransaction
109
+ @depth = 0
110
+ @state = null
111
+ if @connection.isTransaction and @state == null
112
+ return "active"
113
+ return @state
114
+
115
+ SqliteAdapter:close()
116
+ loop while transactionState() != null
117
+ rollback()
118
+ @connection.close()
119
+ return null
120
+
121
+ normalizeInteger(value)
122
+ if Object/prototype/toString/call(value) != "[object BigInt]"
123
+ return value
124
+ number = Number(value)
125
+ if Number/isSafeInteger(number)
126
+ return number
127
+ return "${value}"
@@ -0,0 +1,90 @@
1
+ import js "node:fs" as fs
2
+ import js "node:path" as path
3
+ import js "serve-static" as files
4
+ import js global RegExp
5
+
6
+ export staticOptions(root, spec)
7
+ export class StaticFiles(settings)
8
+ methods
9
+ receive(req, res, failed)
10
+
11
+ staticOptions(root, spec)
12
+ if spec == null
13
+ return null
14
+ if !is_object(spec) or is_array(spec)
15
+ throw "static must be a map"
16
+ allowed = ["path", "directory", "index"]
17
+ for spec as key: value
18
+ if !allowed.includes(key)
19
+ throw "Unknown static option: ${key}"
20
+ mount = spec.path
21
+ if mount == null
22
+ mount = "/"
23
+ pattern = new RegExp("^/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]*$")
24
+ if !is_string(mount) or !pattern.test(mount)
25
+ throw "Static path must be an absolute URL prefix"
26
+ if mount != "/" and mount.endsWith("/")
27
+ mount = mount.slice(0, -1)
28
+ if !is_string(spec.directory) or spec.directory.trim() == ""
29
+ throw "Static directory is required"
30
+ index = spec.index
31
+ if index == null
32
+ index = "index.html"
33
+ if !is_string(index) or index == "" or index.startsWith(".") or index.includes("/") or index.includes("\\")
34
+ throw "Static index must be a file name"
35
+ return {
36
+ path: mount
37
+ directory: path/resolve(root, spec.directory)
38
+ index: index
39
+ }
40
+
41
+ StaticFiles(settings)
42
+ @settings = settings
43
+ if !fs/statSync(settings.directory).isDirectory()
44
+ throw "Static directory must be a directory"
45
+ @serve = files/default(settings.directory, {
46
+ index: settings.index
47
+ dotfiles: "deny"
48
+ fallthrough: false
49
+ redirect: false
50
+ })
51
+
52
+ StaticFiles:receive(req, res, failed)
53
+ mount = @settings.path
54
+ url = req.url
55
+ pathname = url.split("?")[0]
56
+ if mount != "/" and pathname != mount and !pathname.startsWith("${mount}/")
57
+ failed(404, "NotFound")
58
+ return null
59
+ if mount != "/"
60
+ req.url = url.slice(mount.length)
61
+ if req.url == "" or req.url.startsWith("?")
62
+ req.url = "/${req.url}"
63
+ done = (error)
64
+ capture res
65
+ capture failed
66
+ if res.headersSent
67
+ res.destroy()
68
+ return null
69
+ status = 404
70
+ code = "NotFound"
71
+ if error != null
72
+ switch error.status
73
+ case 400
74
+ status = 400
75
+ code = "InvalidPath"
76
+ case 403
77
+ status = 403
78
+ code = "Forbidden"
79
+ case 416
80
+ status = 416
81
+ code = "RangeNotSatisfiable"
82
+ case 404
83
+ status = 404
84
+ else
85
+ status = 500
86
+ code = "InternalServerError"
87
+ failed(status, code)
88
+ serve = @serve
89
+ serve(req, res, done)
90
+ return null
@@ -0,0 +1,70 @@
1
+ import "./options"
2
+ contains(items, value)
3
+ import js global Object
4
+
5
+ export class Token(claims, readonly = false)
6
+ properties
7
+ clientId
8
+ auth
9
+ expiresAt
10
+ methods
11
+ AddAuth(permission)
12
+ clearAuth()
13
+ hasAuth(permission)
14
+ get(name)
15
+ set(name, value)
16
+ snapshot()
17
+
18
+ Token(claims, readonly = false)
19
+ @claims = from_json(to_json(claims))
20
+ @readonly = readonly
21
+ @clientId = claims.clientId
22
+ @auth = claims.auth
23
+ @expiresAt = claims.exp
24
+
25
+ Token:AddAuth(permission)
26
+ writable()
27
+ if !is_string(permission) or permission.trim() == "" or permission.includes(",")
28
+ throw "Permission must be a nonempty name without commas"
29
+ permission = permission.trim()
30
+ if !hasAuth(permission)
31
+ if @auth == ""
32
+ @auth = permission
33
+ else
34
+ @auth = "${@auth}, ${permission}"
35
+ return @this
36
+
37
+ Token:clearAuth()
38
+ writable()
39
+ @auth = ""
40
+ return @this
41
+
42
+ Token:hasAuth(permission)
43
+ for @auth.split(",") as name
44
+ if name.trim() == permission
45
+ return true
46
+ return false
47
+
48
+ Token:get(name)
49
+ if !Object/hasOwn(@claims, name)
50
+ return null
51
+ return from_json(to_json(@claims[name]))
52
+
53
+ Token:set(name, value)
54
+ writable()
55
+ if contains(["clientId", "auth", "iat", "exp", "iss", "aud", "validatedAt", "nbf", "__proto__", "constructor", "prototype"], name)
56
+ throw "Reserved token field: ${name}"
57
+ @claims[name] = from_json(to_json(value))
58
+ return @this
59
+
60
+ Token:snapshot()
61
+ claims = from_json(to_json(@claims))
62
+ if @clientId != claims.clientId or @expiresAt != claims.exp or !is_string(@auth)
63
+ throw "Token identity and expiry cannot be changed"
64
+ claims.auth = @auth
65
+ return claims
66
+
67
+ Token:writable()
68
+ if @readonly
69
+ throw "Token is read-only during this event"
70
+ return null
@@ -0,0 +1,70 @@
1
+ import js "jsonwebtoken" as jwt
2
+ import js "node:crypto" as crypto
3
+ import js global Object
4
+ import js global Date
5
+ import js global Math
6
+ import js global Number
7
+
8
+ export class Tokens(settings)
9
+ methods
10
+ read(header)
11
+ sign(claims)
12
+ create()
13
+
14
+ Tokens(settings)
15
+ @settings = settings
16
+
17
+ Tokens:create()
18
+ settings = @settings
19
+ now = Math/floor(Date/now() / 1000)
20
+ return {
21
+ clientId: crypto/randomUUID()
22
+ auth: ""
23
+ iat: now
24
+ exp: now + settings.ttlSeconds
25
+ iss: settings.issuer
26
+ aud: settings.audience
27
+ validatedAt: Date/now()
28
+ }
29
+
30
+ Tokens:read(header)
31
+ settings = @settings
32
+ if !is_string(header) or header.length > 16384 or header.slice(0, 7).toLowerCase() != "bearer " or header.length == 7
33
+ throw "InvalidToken"
34
+ encoded = header.slice(7)
35
+ decoded = jwt/default/decode(encoded, {
36
+ complete: true
37
+ })
38
+ if !is_object(decoded) or !is_object(decoded.header)
39
+ throw "InvalidToken"
40
+ head = decoded.header
41
+ if head.alg != "HS256" or head.typ != "JWT" or !is_string(head.kid) or head.crit != null or head.b64 != null
42
+ throw "InvalidToken"
43
+ keys = settings.keys
44
+ if !Object/hasOwn(keys, head.kid)
45
+ throw "InvalidToken"
46
+ claims = jwt/default/verify(encoded, keys[head.kid], {
47
+ algorithms: ["HS256"]
48
+ issuer: settings.issuer
49
+ audience: settings.audience
50
+ })
51
+ if !is_object(claims) or !is_string(claims.clientId) or claims.clientId == "" or !is_string(claims.auth)
52
+ throw "InvalidToken"
53
+ 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()
54
+ throw "InvalidToken"
55
+ if !Number/isSafeInteger(claims.validatedAt) or claims.validatedAt < claims.iat * 1000 or claims.validatedAt > Date/now()
56
+ throw "InvalidToken"
57
+ return claims
58
+
59
+ Tokens:sign(claims)
60
+ settings = @settings
61
+ if claims.exp * 1000 <= Date/now()
62
+ throw "TokenExpired"
63
+ keys = settings.keys
64
+ return jwt/default/sign(claims, keys[settings.activeKeyId], {
65
+ algorithm: "HS256"
66
+ header: {
67
+ typ: "JWT"
68
+ kid: settings.activeKeyId
69
+ }
70
+ })
@@ -0,0 +1,43 @@
1
+ import js "node:worker_threads" as threads
2
+ import js global Promise
3
+ import js global process
4
+ import js global setTimeout
5
+ import "./runtime"
6
+ class Runtime
7
+
8
+ export global execute
9
+
10
+ global runtime = new Runtime(threads.workerData)
11
+ global execute = initialize()
12
+
13
+ initialize()
14
+ global runtime
15
+ return chain runtime.preload()
16
+ then(ignored)
17
+ capture threads
18
+ threads/parentPort/postMessage({
19
+ kind: "ready"
20
+ threadId: threads.threadId
21
+ })
22
+ registerCleanup()
23
+ return workerHandler()
24
+
25
+ run(task)
26
+ global runtime
27
+ if task.close == true
28
+ runtime.close()
29
+ pending = new Promise((resolve)
30
+ setTimeout(resolve, 10)
31
+ )
32
+ return pending
33
+ return runtime.handle(task)
34
+
35
+ workerHandler()
36
+ return run
37
+
38
+ registerCleanup()
39
+ process.once("exit", ()
40
+ global runtime
41
+ runtime.close()
42
+ )
43
+ return null