@lango-project/lango-server 0.1.2 → 0.2.2

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 (50) hide show
  1. package/README.md +115 -12
  2. package/dist/.lango/localization.mjs +108 -0
  3. package/dist/.lango/runtime.mjs +33 -7
  4. package/dist/.lango/vendor/i18next.LICENSE +21 -0
  5. package/dist/.lango/vendor/i18next.mjs +2434 -0
  6. package/dist/cli/.lango/localization.mjs +108 -0
  7. package/dist/cli/.lango/runtime.mjs +33 -7
  8. package/dist/cli/.lango/vendor/i18next.LICENSE +21 -0
  9. package/dist/cli/.lango/vendor/i18next.mjs +2434 -0
  10. package/dist/cli/error_details.mjs +45 -0
  11. package/dist/cli/error_details.mjs.map +1 -0
  12. package/dist/cli/options.mjs +2 -2
  13. package/dist/cli/options.mjs.map +1 -1
  14. package/dist/cli/server.mjs +64 -21
  15. package/dist/cli/server.mjs.map +1 -1
  16. package/dist/cli/static_files.mjs +123 -25
  17. package/dist/cli/static_files.mjs.map +1 -1
  18. package/dist/cli/trace.mjs +53 -0
  19. package/dist/cli/trace.mjs.map +1 -0
  20. package/dist/error_details.mjs +45 -0
  21. package/dist/error_details.mjs.map +1 -0
  22. package/dist/options.mjs +2 -2
  23. package/dist/options.mjs.map +1 -1
  24. package/dist/request_identity.mjs +22 -0
  25. package/dist/request_identity.mjs.map +1 -0
  26. package/dist/runtime.mjs +99 -10
  27. package/dist/runtime.mjs.map +1 -1
  28. package/dist/server.mjs +64 -21
  29. package/dist/server.mjs.map +1 -1
  30. package/dist/server_context.mjs +14 -1
  31. package/dist/server_context.mjs.map +1 -1
  32. package/dist/static_files.mjs +123 -25
  33. package/dist/static_files.mjs.map +1 -1
  34. package/dist/token.mjs +26 -13
  35. package/dist/token.mjs.map +1 -1
  36. package/dist/trace.mjs +53 -0
  37. package/dist/trace.mjs.map +1 -0
  38. package/dist/worker.mjs +1 -0
  39. package/dist/worker.mjs.map +1 -1
  40. package/package.json +2 -2
  41. package/src/error_details.lango +21 -0
  42. package/src/options.lango +2 -2
  43. package/src/request_identity.lango +9 -0
  44. package/src/runtime.lango +88 -5
  45. package/src/server.lango +42 -5
  46. package/src/server_context.lango +10 -2
  47. package/src/static_files.lango +102 -23
  48. package/src/token.lango +13 -3
  49. package/src/trace.lango +23 -0
  50. package/src/worker.lango +1 -0
@@ -1,7 +1,11 @@
1
+ import "./trace"
2
+ trace(message)
1
3
  import js "node:fs" as fs
2
4
  import js "node:path" as path
3
5
  import js "serve-static" as files
4
6
  import js global RegExp
7
+ import js global decodeURIComponent
8
+ import js global String
5
9
 
6
10
  export staticOptions(root, spec)
7
11
  export class StaticFiles(settings)
@@ -13,7 +17,7 @@ staticOptions(root, spec)
13
17
  return null
14
18
  if !is_object(spec) or is_array(spec)
15
19
  throw "static must be a map"
16
- allowed = ["path", "directory", "index"]
20
+ allowed = ["path", "directory", "index", "fallback", "runtime"]
17
21
  for spec as key: value
18
22
  if !allowed.includes(key)
19
23
  throw "Unknown static option: ${key}"
@@ -32,22 +36,42 @@ staticOptions(root, spec)
32
36
  index = "index.html"
33
37
  if !is_string(index) or index == "" or index.startsWith(".") or index.includes("/") or index.includes("\\")
34
38
  throw "Static index must be a file name"
39
+ fallback = spec.fallback
40
+ if fallback != null and (!is_string(fallback) or fallback == "" or fallback.startsWith(".") or fallback.includes("/") or fallback.includes("\\"))
41
+ throw "Static fallback must be a file name"
42
+ runtime = spec.runtime
43
+ if runtime == null or runtime == "false"
44
+ runtime = false
45
+ if runtime == "true"
46
+ runtime = true
47
+ if !is_bool(runtime)
48
+ throw "Static runtime must be boolean"
35
49
  return {
36
50
  path: mount
37
51
  directory: path/resolve(root, spec.directory)
38
52
  index: index
53
+ fallback: fallback
54
+ runtime: runtime
39
55
  }
40
56
 
41
57
  StaticFiles(settings)
42
58
  @settings = settings
43
59
  if !fs/statSync(settings.directory).isDirectory()
44
60
  throw "Static directory must be a directory"
61
+ if settings.fallback != null and !fs/statSync(path/join(settings.directory, settings.fallback)).isFile()
62
+ throw "Static fallback must be a file"
45
63
  @serve = files/default(settings.directory, {
46
64
  index: settings.index
47
65
  dotfiles: "deny"
48
66
  fallthrough: false
49
67
  redirect: false
50
68
  })
69
+ @runtime = files/default(settings.directory, {
70
+ index: false
71
+ dotfiles: "allow"
72
+ fallthrough: false
73
+ redirect: false
74
+ })
51
75
 
52
76
  StaticFiles:receive(req, res, failed)
53
77
  mount = @settings.path
@@ -60,31 +84,86 @@ StaticFiles:receive(req, res, failed)
60
84
  req.url = url.slice(mount.length)
61
85
  if req.url == "" or req.url.startsWith("?")
62
86
  req.url = "/${req.url}"
87
+ local = req.url.split("?")[0]
88
+ decoded = null
89
+ try
90
+ decoded = decodeURIComponent(local)
91
+ catch error
92
+ failed(400, "InvalidPath")
93
+ return null
94
+ if decoded.includes("\\") or decoded.includes(String/fromCharCode(0))
95
+ failed(400, "InvalidPath")
96
+ return null
97
+ segments = decoded.split("/")
98
+ runtime = false
99
+ for segments as index: segment
100
+ if segment.startsWith(".")
101
+ if index == 1 and segment == ".lango" and @settings.runtime
102
+ runtime = true
103
+ else
104
+ failed(403, "Forbidden")
105
+ return null
63
106
  done = (error)
107
+ capture req
64
108
  capture res
65
109
  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)
110
+ capture decoded
111
+ failure(req, res, failed, error, decoded)
88
112
  serve = @serve
113
+ if runtime
114
+ serve = @runtime
115
+ trace("Static ${req.method} ${pathname}: directory=${@settings.directory}; index=${@settings.index}; runtime=${runtime}")
89
116
  serve(req, res, done)
90
117
  return null
118
+
119
+ StaticFiles:failure(req, res, failed, error, pathname = null)
120
+ if res.headersSent
121
+ res.destroy()
122
+ return null
123
+ status = 404
124
+ code = "NotFound"
125
+ if error != null
126
+ switch error.status
127
+ case 400
128
+ status = 400
129
+ code = "InvalidPath"
130
+ case 403
131
+ status = 403
132
+ code = "Forbidden"
133
+ case 416
134
+ status = 416
135
+ code = "RangeNotSatisfiable"
136
+ case 404
137
+ status = 404
138
+ else
139
+ status = 500
140
+ code = "InternalServerError"
141
+ if status == 404 and pathname != null and @settings.fallback != null and !pathname.startsWith("/.lango") and path/posix/extname(pathname) == "" and acceptsHTML(req.headers.accept)
142
+ req.url = "/${@settings.fallback}"
143
+ done = (error)
144
+ capture req
145
+ capture res
146
+ capture failed
147
+ failure(req, res, failed, error)
148
+ serve = @serve
149
+ serve(req, res, done)
150
+ return null
151
+ failed(status, code)
152
+ return null
153
+
154
+ acceptsHTML(value)
155
+ if !is_string(value)
156
+ return false
157
+ zero = new RegExp("^q\\s*=\\s*0(?:\\.0*)?$", "i")
158
+ for value.split(",") as range
159
+ parts = range.split(";")
160
+ media = parts[0]
161
+ type = media.trim().toLowerCase()
162
+ if type == "text/html" or type == "application/xhtml+xml"
163
+ allowed = true
164
+ for parts.slice(1) as parameter
165
+ if zero.test(parameter.trim())
166
+ allowed = false
167
+ if allowed
168
+ return true
169
+ return false
package/src/token.lango CHANGED
@@ -4,9 +4,9 @@ import js global Object
4
4
 
5
5
  export class Token(claims, readonly = false)
6
6
  properties
7
- clientId
8
- auth
9
- expiresAt
7
+ clientId?
8
+ auth?
9
+ expiresAt?
10
10
  methods
11
11
  AddAuth(permission)
12
12
  clearAuth()
@@ -24,6 +24,16 @@ Token(claims, readonly = false)
24
24
  @clientId = claims.clientId
25
25
  @auth = claims.auth
26
26
  @expiresAt = claims.exp
27
+ Object/freeze(@this)
28
+
29
+ Token:clientId?
30
+ return @clientId
31
+
32
+ Token:auth?
33
+ return @auth
34
+
35
+ Token:expiresAt?
36
+ return @expiresAt
27
37
 
28
38
  Token:AddAuth(permission)
29
39
  writable()
@@ -0,0 +1,23 @@
1
+ export trace(message)
2
+ export responseLog(id, method, path, status)
3
+
4
+ trace(message)
5
+ context log
6
+ if log.verbose? == true
7
+ log.debug(message)
8
+ return null
9
+
10
+
11
+ responseLog(id, method, path, status)
12
+ context log
13
+ message = "HTTP ${method} ${path} completed status=${status}"
14
+ if id != null
15
+ message = "[${id}] ${message}"
16
+ if status >= 500
17
+ log.err(message)
18
+ else
19
+ if status >= 400
20
+ log.warn(message)
21
+ else
22
+ trace(message)
23
+ return null
package/src/worker.lango CHANGED
@@ -12,6 +12,7 @@ global execute = initialize()
12
12
 
13
13
  initialize()
14
14
  global runtime
15
+ process.setSourceMapsEnabled(true)
15
16
  return chain runtime.preload()
16
17
  then(ignored)
17
18
  capture threads