@lango-project/lango-server 0.1.0 → 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.
- package/README.md +283 -4
- package/dist/builder.mjs +16 -1
- package/dist/builder.mjs.map +1 -1
- package/dist/cli/endpoint.mjs +6 -0
- 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/routes.mjs +66 -2
- 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 +6 -0
- package/dist/endpoint.mjs.map +1 -1
- package/dist/group.mjs +17 -1
- package/dist/group.mjs.map +1 -1
- 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/routes.mjs +66 -2
- package/dist/routes.mjs.map +1 -1
- package/dist/runtime.mjs +118 -44
- 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 +78 -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/package.json +9 -4
- package/src/builder.lango +11 -0
- package/src/endpoint.lango +7 -0
- package/src/group.lango +14 -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/routes.lango +65 -1
- package/src/runtime.lango +62 -0
- package/src/server.lango +182 -17
- package/src/server_context.lango +58 -0
- package/src/static_files.lango +90 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import js "node:worker_threads" as threads
|
|
2
|
+
import js global Promise
|
|
3
|
+
import js global setTimeout
|
|
4
|
+
import js global clearTimeout
|
|
5
|
+
|
|
6
|
+
export class ServerContext()
|
|
7
|
+
methods
|
|
8
|
+
publish(path, key, data)
|
|
9
|
+
cursor(path, key)
|
|
10
|
+
|
|
11
|
+
ServerContext()
|
|
12
|
+
return null
|
|
13
|
+
|
|
14
|
+
ServerContext:publish(path, key, data)
|
|
15
|
+
return invoke("publish", path, key, data)
|
|
16
|
+
|
|
17
|
+
ServerContext:cursor(path, key)
|
|
18
|
+
return invoke("cursor", path, key, null)
|
|
19
|
+
|
|
20
|
+
invoke(operation, path, key, data)
|
|
21
|
+
channel = new threads/MessageChannel()
|
|
22
|
+
port = channel.port1
|
|
23
|
+
remote = channel.port2
|
|
24
|
+
pending = new Promise((resolve, reject)
|
|
25
|
+
capture port
|
|
26
|
+
port.once("message", (message)
|
|
27
|
+
capture resolve
|
|
28
|
+
capture reject
|
|
29
|
+
if message.ok
|
|
30
|
+
resolve(message.value)
|
|
31
|
+
else
|
|
32
|
+
reject("Longpoll operation failed")
|
|
33
|
+
)
|
|
34
|
+
)
|
|
35
|
+
expired = Promise/withResolvers()
|
|
36
|
+
fail = ()
|
|
37
|
+
capture expired
|
|
38
|
+
expired.reject("Longpoll operation timed out")
|
|
39
|
+
timer = setTimeout(fail, 4000)
|
|
40
|
+
try
|
|
41
|
+
threads/parentPort/postMessage({
|
|
42
|
+
kind: "longpoll"
|
|
43
|
+
operation: operation
|
|
44
|
+
path: path
|
|
45
|
+
key: key
|
|
46
|
+
data: from_json(to_json(data))
|
|
47
|
+
port: remote
|
|
48
|
+
}, [remote])
|
|
49
|
+
catch error
|
|
50
|
+
port.close()
|
|
51
|
+
clearTimeout(timer)
|
|
52
|
+
throw error
|
|
53
|
+
return chain Promise/race([pending, expired.promise])
|
|
54
|
+
finally
|
|
55
|
+
capture port
|
|
56
|
+
capture timer
|
|
57
|
+
clearTimeout(timer)
|
|
58
|
+
port.close()
|
|
@@ -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
|