@lango-project/lango-server 0.1.0 → 0.1.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.
- package/README.md +368 -10
- package/dist/builder.mjs +16 -1
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/endpoint.mjs +8 -6
- package/dist/cli/endpoint.mjs.map +1 -1
- package/dist/cli/longpoll_broker.mjs +214 -0
- package/dist/cli/longpoll_broker.mjs.map +1 -0
- package/dist/cli/longpoll_options.mjs +102 -0
- package/dist/cli/longpoll_options.mjs.map +1 -0
- package/dist/cli/longpoll_session.mjs +135 -0
- package/dist/cli/longpoll_session.mjs.map +1 -0
- package/dist/cli/middleware.mjs +25 -0
- package/dist/cli/middleware.mjs.map +1 -0
- package/dist/cli/options.mjs +14 -2
- package/dist/cli/options.mjs.map +1 -1
- package/dist/cli/route_pattern.mjs +129 -0
- package/dist/cli/route_pattern.mjs.map +1 -0
- package/dist/cli/routes.mjs +80 -4
- package/dist/cli/routes.mjs.map +1 -1
- package/dist/cli/server.mjs +243 -23
- package/dist/cli/server.mjs.map +1 -1
- package/dist/cli/static_files.mjs +125 -0
- package/dist/cli/static_files.mjs.map +1 -0
- package/dist/endpoint.mjs +8 -6
- package/dist/endpoint.mjs.map +1 -1
- package/dist/group.mjs +17 -1
- package/dist/group.mjs.map +1 -1
- package/dist/http_failure.mjs +45 -0
- package/dist/http_failure.mjs.map +1 -0
- package/dist/longpoll_broker.mjs +214 -0
- package/dist/longpoll_broker.mjs.map +1 -0
- package/dist/longpoll_options.mjs +102 -0
- package/dist/longpoll_options.mjs.map +1 -0
- package/dist/longpoll_session.mjs +135 -0
- package/dist/longpoll_session.mjs.map +1 -0
- package/dist/middleware.mjs +25 -0
- package/dist/middleware.mjs.map +1 -0
- package/dist/options.mjs +14 -2
- package/dist/options.mjs.map +1 -1
- package/dist/response.mjs +8 -0
- package/dist/response.mjs.map +1 -1
- package/dist/route_info.mjs +22 -0
- package/dist/route_info.mjs.map +1 -0
- package/dist/route_pattern.mjs +129 -0
- package/dist/route_pattern.mjs.map +1 -0
- package/dist/routes.mjs +80 -4
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +170 -59
- package/dist/runtime.mjs.map +1 -1
- package/dist/server.mjs +243 -23
- package/dist/server.mjs.map +1 -1
- package/dist/server_context.mjs +88 -0
- package/dist/server_context.mjs.map +1 -0
- package/dist/static_files.mjs +125 -0
- package/dist/static_files.mjs.map +1 -0
- package/dist/token.mjs +12 -0
- package/dist/token.mjs.map +1 -1
- package/dist/tokens.mjs +16 -1
- package/dist/tokens.mjs.map +1 -1
- package/package.json +9 -4
- package/src/builder.lango +11 -0
- package/src/endpoint.lango +10 -4
- package/src/group.lango +14 -0
- package/src/http_failure.lango +26 -0
- package/src/longpoll.lua +61 -0
- package/src/longpoll_broker.lango +161 -0
- package/src/longpoll_options.lango +79 -0
- package/src/longpoll_session.lango +90 -0
- package/src/middleware.lango +10 -0
- package/src/options.lango +16 -0
- package/src/response.lango +6 -0
- package/src/route_info.lango +15 -0
- package/src/route_pattern.lango +86 -0
- package/src/routes.lango +80 -4
- package/src/runtime.lango +113 -13
- package/src/server.lango +182 -17
- package/src/server_context.lango +66 -0
- package/src/static_files.lango +90 -0
- package/src/token.lango +11 -0
- package/src/tokens.lango +11 -1
|
@@ -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
|
package/src/token.lango
CHANGED
|
@@ -14,10 +14,13 @@ export class Token(claims, readonly = false)
|
|
|
14
14
|
get(name)
|
|
15
15
|
set(name, value)
|
|
16
16
|
snapshot()
|
|
17
|
+
renew()
|
|
18
|
+
renewalRequested()
|
|
17
19
|
|
|
18
20
|
Token(claims, readonly = false)
|
|
19
21
|
@claims = from_json(to_json(claims))
|
|
20
22
|
@readonly = readonly
|
|
23
|
+
@renew = false
|
|
21
24
|
@clientId = claims.clientId
|
|
22
25
|
@auth = claims.auth
|
|
23
26
|
@expiresAt = claims.exp
|
|
@@ -64,6 +67,14 @@ Token:snapshot()
|
|
|
64
67
|
claims.auth = @auth
|
|
65
68
|
return claims
|
|
66
69
|
|
|
70
|
+
Token:renew()
|
|
71
|
+
writable()
|
|
72
|
+
@renew = true
|
|
73
|
+
return @this
|
|
74
|
+
|
|
75
|
+
Token:renewalRequested()
|
|
76
|
+
return @renew
|
|
77
|
+
|
|
67
78
|
Token:writable()
|
|
68
79
|
if @readonly
|
|
69
80
|
throw "Token is read-only during this event"
|
package/src/tokens.lango
CHANGED
|
@@ -10,6 +10,7 @@ export class Tokens(settings)
|
|
|
10
10
|
read(header)
|
|
11
11
|
sign(claims)
|
|
12
12
|
create()
|
|
13
|
+
renew(claims)
|
|
13
14
|
|
|
14
15
|
Tokens(settings)
|
|
15
16
|
@settings = settings
|
|
@@ -52,10 +53,19 @@ Tokens:read(header)
|
|
|
52
53
|
throw "InvalidToken"
|
|
53
54
|
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
55
|
throw "InvalidToken"
|
|
55
|
-
if !Number/isSafeInteger(claims.validatedAt) or claims.validatedAt <
|
|
56
|
+
if !Number/isSafeInteger(claims.validatedAt) or claims.validatedAt < 0 or claims.validatedAt > Date/now()
|
|
56
57
|
throw "InvalidToken"
|
|
57
58
|
return claims
|
|
58
59
|
|
|
60
|
+
Tokens:renew(claims)
|
|
61
|
+
if claims.exp * 1000 <= Date/now()
|
|
62
|
+
throw "TokenExpired"
|
|
63
|
+
updated = from_json(to_json(claims))
|
|
64
|
+
now = Math/floor(Date/now() / 1000)
|
|
65
|
+
updated.iat = now
|
|
66
|
+
updated.exp = now + @settings.ttlSeconds
|
|
67
|
+
return updated
|
|
68
|
+
|
|
59
69
|
Tokens:sign(claims)
|
|
60
70
|
settings = @settings
|
|
61
71
|
if claims.exp * 1000 <= Date/now()
|