@lango-project/lango-server 0.1.2 → 0.2.0
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 +64 -12
- package/dist/.lango/localization.mjs +108 -0
- package/dist/.lango/runtime.mjs +25 -6
- package/dist/.lango/vendor/i18next.LICENSE +21 -0
- package/dist/.lango/vendor/i18next.mjs +2434 -0
- package/dist/cli/.lango/localization.mjs +108 -0
- package/dist/cli/.lango/runtime.mjs +25 -6
- package/dist/cli/.lango/vendor/i18next.LICENSE +21 -0
- package/dist/cli/.lango/vendor/i18next.mjs +2434 -0
- package/dist/cli/options.mjs +2 -2
- package/dist/cli/options.mjs.map +1 -1
- package/dist/cli/static_files.mjs +121 -25
- package/dist/cli/static_files.mjs.map +1 -1
- package/dist/options.mjs +2 -2
- package/dist/options.mjs.map +1 -1
- package/dist/runtime.mjs +76 -3
- package/dist/runtime.mjs.map +1 -1
- package/dist/server_context.mjs +14 -1
- package/dist/server_context.mjs.map +1 -1
- package/dist/static_files.mjs +121 -25
- package/dist/static_files.mjs.map +1 -1
- package/dist/token.mjs +26 -13
- package/dist/token.mjs.map +1 -1
- package/package.json +2 -2
- package/src/options.lango +2 -2
- package/src/runtime.lango +65 -3
- package/src/server_context.lango +10 -2
- package/src/static_files.lango +99 -23
- package/src/token.lango +13 -3
package/dist/static_files.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import * as path from "node:path";
|
|
|
3
3
|
import * as files from "serve-static";
|
|
4
4
|
import {
|
|
5
5
|
hostGlobal as __hostGlobal,
|
|
6
|
+
hostGlobalCall as __hostGlobalCall,
|
|
6
7
|
isNumber as __isNumber,
|
|
7
8
|
isString as __isString,
|
|
8
9
|
isBool as __isBool,
|
|
@@ -21,6 +22,7 @@ import {
|
|
|
21
22
|
export class StaticFiles {
|
|
22
23
|
#settings;
|
|
23
24
|
#serve;
|
|
25
|
+
#runtime;
|
|
24
26
|
constructor(settings) {
|
|
25
27
|
__requireArity(arguments.length, [0], "constructor");
|
|
26
28
|
settings = __boundary(settings);
|
|
@@ -28,7 +30,11 @@ export class StaticFiles {
|
|
|
28
30
|
if (__requireBoolean(!__requireBoolean(__normalize(__normalize(fs.statSync(__normalize(settings.directory))).isDirectory())))) {
|
|
29
31
|
throw "Static directory must be a directory";
|
|
30
32
|
}
|
|
33
|
+
if (__requireBoolean(__requireBoolean(__normalize(settings.fallback) !== null) && __requireBoolean(!__requireBoolean(__normalize(__normalize(fs.statSync(__normalize(path.join(__normalize(settings.directory), __normalize(settings.fallback))))).isFile()))))) {
|
|
34
|
+
throw "Static fallback must be a file";
|
|
35
|
+
}
|
|
31
36
|
this.#serve = __normalize(files.default(__normalize(settings.directory), { index: __normalize(settings.index), dotfiles: "deny", fallthrough: false, redirect: false }));
|
|
37
|
+
this.#runtime = __normalize(files.default(__normalize(settings.directory), { index: false, dotfiles: "allow", fallthrough: false, redirect: false }));
|
|
32
38
|
}
|
|
33
39
|
receive(req, res, failed) {
|
|
34
40
|
__requireArity(arguments.length, [0, 1, 2], "receive");
|
|
@@ -48,39 +54,88 @@ export class StaticFiles {
|
|
|
48
54
|
req.url = `/${__normalize(req.url)}`;
|
|
49
55
|
}
|
|
50
56
|
}
|
|
51
|
-
let
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
} else if (__normalize(error.status) === 404) {
|
|
70
|
-
status = 404;
|
|
57
|
+
let local = __normalize(__normalize(req.url.split("?"))[0]);
|
|
58
|
+
let decoded = null;
|
|
59
|
+
try {
|
|
60
|
+
decoded = __hostGlobalCall(["decodeURIComponent"], local);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
__call(failed, 400, "InvalidPath");
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
if (__requireBoolean(__requireBoolean(__normalize(decoded.includes("\\"))) || __requireBoolean(__normalize(decoded.includes(__normalize(__hostGlobal(["String"]).fromCharCode(0))))))) {
|
|
66
|
+
__call(failed, 400, "InvalidPath");
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
let segments = __normalize(decoded.split("/"));
|
|
70
|
+
let runtime = false;
|
|
71
|
+
for (const [index, segment] of __entries(segments)) {
|
|
72
|
+
if (__requireBoolean(__normalize(segment.startsWith(".")))) {
|
|
73
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(index === 1) && __requireBoolean(segment === ".lango")) && __requireBoolean(__normalize(this.#settings.runtime)))) {
|
|
74
|
+
runtime = true;
|
|
71
75
|
} else {
|
|
72
|
-
|
|
73
|
-
|
|
76
|
+
__call(failed, 403, "Forbidden");
|
|
77
|
+
return null;
|
|
74
78
|
}
|
|
75
79
|
}
|
|
76
|
-
|
|
80
|
+
}
|
|
81
|
+
let done = __withArity((error) => {
|
|
82
|
+
error = __boundary(error);
|
|
83
|
+
this.#failure(req, res, failed, error, decoded);
|
|
77
84
|
return null;
|
|
78
85
|
}, [0], "closure");
|
|
79
86
|
let serve = __normalize(this.#serve);
|
|
87
|
+
if (__requireBoolean(runtime)) {
|
|
88
|
+
serve = __normalize(this.#runtime);
|
|
89
|
+
}
|
|
80
90
|
__call(serve, req, res, done);
|
|
81
91
|
return null;
|
|
82
92
|
return null;
|
|
83
93
|
}
|
|
94
|
+
#failure(req, res, failed, error, pathname = null) {
|
|
95
|
+
__requireArity(arguments.length, [0, 1, 2, 3], "failure");
|
|
96
|
+
req = __boundary(req);
|
|
97
|
+
res = __boundary(res);
|
|
98
|
+
failed = __boundary(failed);
|
|
99
|
+
error = __boundary(error);
|
|
100
|
+
pathname = __boundary(pathname);
|
|
101
|
+
if (__requireBoolean(__normalize(res.headersSent))) {
|
|
102
|
+
__normalize(res.destroy());
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
let status = 404;
|
|
106
|
+
let code = "NotFound";
|
|
107
|
+
if (__requireBoolean(error !== null)) {
|
|
108
|
+
if (__normalize(error.status) === 400) {
|
|
109
|
+
status = 400;
|
|
110
|
+
code = "InvalidPath";
|
|
111
|
+
} else if (__normalize(error.status) === 403) {
|
|
112
|
+
status = 403;
|
|
113
|
+
code = "Forbidden";
|
|
114
|
+
} else if (__normalize(error.status) === 416) {
|
|
115
|
+
status = 416;
|
|
116
|
+
code = "RangeNotSatisfiable";
|
|
117
|
+
} else if (__normalize(error.status) === 404) {
|
|
118
|
+
status = 404;
|
|
119
|
+
} else {
|
|
120
|
+
status = 500;
|
|
121
|
+
code = "InternalServerError";
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(status === 404) && __requireBoolean(pathname !== null)) && __requireBoolean(__normalize(this.#settings.fallback) !== null)) && __requireBoolean(!__requireBoolean(__normalize(pathname.startsWith("/.lango"))))) && __requireBoolean(__normalize(path.posix.extname(pathname)) === "")) && __requireBoolean(acceptsHTML(req.headers.accept)))) {
|
|
125
|
+
req.url = `/${__normalize(this.#settings.fallback)}`;
|
|
126
|
+
let done = __withArity((error) => {
|
|
127
|
+
error = __boundary(error);
|
|
128
|
+
this.#failure(req, res, failed, error);
|
|
129
|
+
return null;
|
|
130
|
+
}, [0], "closure");
|
|
131
|
+
let serve = __normalize(this.#serve);
|
|
132
|
+
__call(serve, req, res, done);
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
__call(failed, status, code);
|
|
136
|
+
return null;
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
84
139
|
}
|
|
85
140
|
export function staticOptions(root, spec) {
|
|
86
141
|
__requireArity(arguments.length, [0, 1], "staticOptions");
|
|
@@ -92,7 +147,7 @@ export function staticOptions(root, spec) {
|
|
|
92
147
|
if (__requireBoolean(__requireBoolean(!__requireBoolean(__isObject(spec))) || __requireBoolean(__isArray(spec)))) {
|
|
93
148
|
throw "static must be a map";
|
|
94
149
|
}
|
|
95
|
-
let allowed = ["path", "directory", "index"];
|
|
150
|
+
let allowed = ["path", "directory", "index", "fallback", "runtime"];
|
|
96
151
|
for (const [key, value] of __entries(spec)) {
|
|
97
152
|
if (__requireBoolean(!__requireBoolean(__normalize(allowed.includes(key))))) {
|
|
98
153
|
throw `Unknown static option: ${key}`;
|
|
@@ -119,7 +174,48 @@ export function staticOptions(root, spec) {
|
|
|
119
174
|
if (__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(index))) || __requireBoolean(index === "")) || __requireBoolean(__normalize(index.startsWith(".")))) || __requireBoolean(__normalize(index.includes("/")))) || __requireBoolean(__normalize(index.includes("\\"))))) {
|
|
120
175
|
throw "Static index must be a file name";
|
|
121
176
|
}
|
|
122
|
-
|
|
177
|
+
let fallback = __normalize(spec.fallback);
|
|
178
|
+
if (__requireBoolean(__requireBoolean(fallback !== null) && __requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(fallback))) || __requireBoolean(fallback === "")) || __requireBoolean(__normalize(fallback.startsWith(".")))) || __requireBoolean(__normalize(fallback.includes("/")))) || __requireBoolean(__normalize(fallback.includes("\\")))))) {
|
|
179
|
+
throw "Static fallback must be a file name";
|
|
180
|
+
}
|
|
181
|
+
let runtime = __normalize(spec.runtime);
|
|
182
|
+
if (__requireBoolean(__requireBoolean(runtime === null) || __requireBoolean(runtime === "false"))) {
|
|
183
|
+
runtime = false;
|
|
184
|
+
}
|
|
185
|
+
if (__requireBoolean(runtime === "true")) {
|
|
186
|
+
runtime = true;
|
|
187
|
+
}
|
|
188
|
+
if (__requireBoolean(!__requireBoolean(__isBool(runtime)))) {
|
|
189
|
+
throw "Static runtime must be boolean";
|
|
190
|
+
}
|
|
191
|
+
return { path: mount, directory: __normalize(path.resolve(root, __normalize(spec.directory))), index: index, fallback: fallback, runtime: runtime };
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function acceptsHTML(value) {
|
|
196
|
+
__requireArity(arguments.length, [0], "acceptsHTML");
|
|
197
|
+
value = __boundary(value);
|
|
198
|
+
if (__requireBoolean(!__requireBoolean(__isString(value)))) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
let zero = new (__hostGlobal(["RegExp"]))("^q\\s*=\\s*0(?:\\.0*)?$", "i");
|
|
202
|
+
for (const range of __normalize(value.split(","))) {
|
|
203
|
+
let parts = __normalize(range.split(";"));
|
|
204
|
+
let media = __normalize(parts[0]);
|
|
205
|
+
let type = __normalize(__normalize(media.trim()).toLowerCase());
|
|
206
|
+
if (__requireBoolean(__requireBoolean(type === "text/html") || __requireBoolean(type === "application/xhtml+xml"))) {
|
|
207
|
+
let allowed = true;
|
|
208
|
+
for (const parameter of __normalize(parts.slice(1))) {
|
|
209
|
+
if (__requireBoolean(__normalize(zero.test(__normalize(parameter.trim()))))) {
|
|
210
|
+
allowed = false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (__requireBoolean(allowed)) {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
123
219
|
return null;
|
|
124
220
|
}
|
|
125
221
|
//# sourceMappingURL=static_files.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"static_files.mjs","sources":["static_files.lango"],"sourcesContent":["import js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"serve-static\" as files\nimport js global RegExp\n\nexport staticOptions(root, spec)\nexport class StaticFiles(settings)\n methods\n receive(req, res, failed)\n\nstaticOptions(root, spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"static must be a map\"\n allowed = [\"path\", \"directory\", \"index\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown static option: ${key}\"\n mount = spec.path\n if mount == null\n mount = \"/\"\n pattern = new RegExp(\"^/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]*$\")\n if !is_string(mount) or !pattern.test(mount)\n throw \"Static path must be an absolute URL prefix\"\n if mount != \"/\" and mount.endsWith(\"/\")\n mount = mount.slice(0, -1)\n if !is_string(spec.directory) or spec.directory.trim() == \"\"\n throw \"Static directory is required\"\n index = spec.index\n if index == null\n index = \"index.html\"\n if !is_string(index) or index == \"\" or index.startsWith(\".\") or index.includes(\"/\") or index.includes(\"\\\\\")\n throw \"Static index must be a file name\"\n return {\n path: mount\n directory: path/resolve(root, spec.directory)\n index: index\n }\n\nStaticFiles(settings)\n @settings = settings\n if !fs/statSync(settings.directory).isDirectory()\n throw \"Static directory must be a directory\"\n @serve = files/default(settings.directory, {\n index: settings.index\n dotfiles: \"deny\"\n fallthrough: false\n redirect: false\n })\n\nStaticFiles:receive(req, res, failed)\n mount = @settings.path\n url = req.url\n pathname = url.split(\"?\")[0]\n if mount != \"/\" and pathname != mount and !pathname.startsWith(\"${mount}/\")\n failed(404, \"NotFound\")\n return null\n if mount != \"/\"\n req.url = url.slice(mount.length)\n if req.url == \"\" or req.url.startsWith(\"?\")\n req.url = \"/${req.url}\"\n done = (error)\n capture res\n capture failed\n if res.headersSent\n
|
|
1
|
+
{"version":3,"file":"static_files.mjs","sources":["static_files.lango"],"sourcesContent":["import js \"node:fs\" as fs\nimport js \"node:path\" as path\nimport js \"serve-static\" as files\nimport js global RegExp\nimport js global decodeURIComponent\nimport js global String\n\nexport staticOptions(root, spec)\nexport class StaticFiles(settings)\n methods\n receive(req, res, failed)\n\nstaticOptions(root, spec)\n if spec == null\n return null\n if !is_object(spec) or is_array(spec)\n throw \"static must be a map\"\n allowed = [\"path\", \"directory\", \"index\", \"fallback\", \"runtime\"]\n for spec as key: value\n if !allowed.includes(key)\n throw \"Unknown static option: ${key}\"\n mount = spec.path\n if mount == null\n mount = \"/\"\n pattern = new RegExp(\"^/(?:[A-Za-z0-9_-]+/)*[A-Za-z0-9_-]*$\")\n if !is_string(mount) or !pattern.test(mount)\n throw \"Static path must be an absolute URL prefix\"\n if mount != \"/\" and mount.endsWith(\"/\")\n mount = mount.slice(0, -1)\n if !is_string(spec.directory) or spec.directory.trim() == \"\"\n throw \"Static directory is required\"\n index = spec.index\n if index == null\n index = \"index.html\"\n if !is_string(index) or index == \"\" or index.startsWith(\".\") or index.includes(\"/\") or index.includes(\"\\\\\")\n throw \"Static index must be a file name\"\n fallback = spec.fallback\n if fallback != null and (!is_string(fallback) or fallback == \"\" or fallback.startsWith(\".\") or fallback.includes(\"/\") or fallback.includes(\"\\\\\"))\n throw \"Static fallback must be a file name\"\n runtime = spec.runtime\n if runtime == null or runtime == \"false\"\n runtime = false\n if runtime == \"true\"\n runtime = true\n if !is_bool(runtime)\n throw \"Static runtime must be boolean\"\n return {\n path: mount\n directory: path/resolve(root, spec.directory)\n index: index\n fallback: fallback\n runtime: runtime\n }\n\nStaticFiles(settings)\n @settings = settings\n if !fs/statSync(settings.directory).isDirectory()\n throw \"Static directory must be a directory\"\n if settings.fallback != null and !fs/statSync(path/join(settings.directory, settings.fallback)).isFile()\n throw \"Static fallback must be a file\"\n @serve = files/default(settings.directory, {\n index: settings.index\n dotfiles: \"deny\"\n fallthrough: false\n redirect: false\n })\n @runtime = files/default(settings.directory, {\n index: false\n dotfiles: \"allow\"\n fallthrough: false\n redirect: false\n })\n\nStaticFiles:receive(req, res, failed)\n mount = @settings.path\n url = req.url\n pathname = url.split(\"?\")[0]\n if mount != \"/\" and pathname != mount and !pathname.startsWith(\"${mount}/\")\n failed(404, \"NotFound\")\n return null\n if mount != \"/\"\n req.url = url.slice(mount.length)\n if req.url == \"\" or req.url.startsWith(\"?\")\n req.url = \"/${req.url}\"\n local = req.url.split(\"?\")[0]\n decoded = null\n try\n decoded = decodeURIComponent(local)\n catch error\n failed(400, \"InvalidPath\")\n return null\n if decoded.includes(\"\\\\\") or decoded.includes(String/fromCharCode(0))\n failed(400, \"InvalidPath\")\n return null\n segments = decoded.split(\"/\")\n runtime = false\n for segments as index: segment\n if segment.startsWith(\".\")\n if index == 1 and segment == \".lango\" and @settings.runtime\n runtime = true\n else\n failed(403, \"Forbidden\")\n return null\n done = (error)\n capture req\n capture res\n capture failed\n capture decoded\n failure(req, res, failed, error, decoded)\n serve = @serve\n if runtime\n serve = @runtime\n serve(req, res, done)\n return null\n\nStaticFiles:failure(req, res, failed, error, pathname = null)\n if res.headersSent\n res.destroy()\n return null\n status = 404\n code = \"NotFound\"\n if error != null\n switch error.status\n case 400\n status = 400\n code = \"InvalidPath\"\n case 403\n status = 403\n code = \"Forbidden\"\n case 416\n status = 416\n code = \"RangeNotSatisfiable\"\n case 404\n status = 404\n else\n status = 500\n code = \"InternalServerError\"\n if status == 404 and pathname != null and @settings.fallback != null and !pathname.startsWith(\"/.lango\") and path/posix/extname(pathname) == \"\" and acceptsHTML(req.headers.accept)\n req.url = \"/${@settings.fallback}\"\n done = (error)\n capture req\n capture res\n capture failed\n failure(req, res, failed, error)\n serve = @serve\n serve(req, res, done)\n return null\n failed(status, code)\n return null\n\nacceptsHTML(value)\n if !is_string(value)\n return false\n zero = new RegExp(\"^q\\\\s*=\\\\s*0(?:\\\\.0*)?$\", \"i\")\n for value.split(\",\") as range\n parts = range.split(\";\")\n media = parts[0]\n type = media.trim().toLowerCase()\n if type == \"text/html\" or type == \"application/xhtml+xml\"\n allowed = true\n for parts.slice(1) as parameter\n if zero.test(parameter.trim())\n allowed = false\n if allowed\n return true\n return false\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAQA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0IA;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/token.mjs
CHANGED
|
@@ -15,12 +15,12 @@ import {
|
|
|
15
15
|
} from "./.lango/runtime.mjs";
|
|
16
16
|
|
|
17
17
|
export class Token {
|
|
18
|
-
clientId;
|
|
19
|
-
auth;
|
|
20
|
-
expiresAt;
|
|
21
18
|
#claims;
|
|
22
19
|
#readonly;
|
|
23
20
|
#renew;
|
|
21
|
+
#clientId;
|
|
22
|
+
#auth;
|
|
23
|
+
#expiresAt;
|
|
24
24
|
constructor(claims, readonly = false) {
|
|
25
25
|
__requireArity(arguments.length, [0], "constructor");
|
|
26
26
|
claims = __boundary(claims);
|
|
@@ -28,9 +28,22 @@ export class Token {
|
|
|
28
28
|
this.#claims = __fromJson(__toJson(claims));
|
|
29
29
|
this.#readonly = readonly;
|
|
30
30
|
this.#renew = false;
|
|
31
|
-
this
|
|
32
|
-
this
|
|
33
|
-
this
|
|
31
|
+
this.#clientId = __normalize(claims.clientId);
|
|
32
|
+
this.#auth = __normalize(claims.auth);
|
|
33
|
+
this.#expiresAt = __normalize(claims.exp);
|
|
34
|
+
__normalize(__hostGlobal(["Object"]).freeze(this));
|
|
35
|
+
}
|
|
36
|
+
get clientId() {
|
|
37
|
+
return __normalize(this.#clientId);
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
get auth() {
|
|
41
|
+
return __normalize(this.#auth);
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
get expiresAt() {
|
|
45
|
+
return __normalize(this.#expiresAt);
|
|
46
|
+
return null;
|
|
34
47
|
}
|
|
35
48
|
AddAuth(permission) {
|
|
36
49
|
__requireArity(arguments.length, [0], "AddAuth");
|
|
@@ -41,10 +54,10 @@ export class Token {
|
|
|
41
54
|
}
|
|
42
55
|
permission = __normalize(permission.trim());
|
|
43
56
|
if (__requireBoolean(!__requireBoolean(this.hasAuth(permission)))) {
|
|
44
|
-
if (__requireBoolean(__normalize(this
|
|
45
|
-
this
|
|
57
|
+
if (__requireBoolean(__normalize(this.#auth) === "")) {
|
|
58
|
+
this.#auth = permission;
|
|
46
59
|
} else {
|
|
47
|
-
this
|
|
60
|
+
this.#auth = `${__normalize(this.#auth)}, ${permission}`;
|
|
48
61
|
}
|
|
49
62
|
}
|
|
50
63
|
return this;
|
|
@@ -52,14 +65,14 @@ export class Token {
|
|
|
52
65
|
}
|
|
53
66
|
clearAuth() {
|
|
54
67
|
this.#writable();
|
|
55
|
-
this
|
|
68
|
+
this.#auth = "";
|
|
56
69
|
return this;
|
|
57
70
|
return null;
|
|
58
71
|
}
|
|
59
72
|
hasAuth(permission) {
|
|
60
73
|
__requireArity(arguments.length, [0], "hasAuth");
|
|
61
74
|
permission = __boundary(permission);
|
|
62
|
-
for (const name of __normalize(this
|
|
75
|
+
for (const name of __normalize(this.#auth.split(","))) {
|
|
63
76
|
if (__requireBoolean(__normalize(name.trim()) === permission)) {
|
|
64
77
|
return true;
|
|
65
78
|
}
|
|
@@ -90,10 +103,10 @@ export class Token {
|
|
|
90
103
|
}
|
|
91
104
|
snapshot() {
|
|
92
105
|
let claims = __fromJson(__toJson(__normalize(this.#claims)));
|
|
93
|
-
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(this
|
|
106
|
+
if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(this.#clientId) !== __normalize(claims.clientId)) || __requireBoolean(__normalize(this.#expiresAt) !== __normalize(claims.exp))) || __requireBoolean(!__requireBoolean(__isString(__normalize(this.#auth)))))) {
|
|
94
107
|
throw "Token identity and expiry cannot be changed";
|
|
95
108
|
}
|
|
96
|
-
claims.auth = __normalize(this
|
|
109
|
+
claims.auth = __normalize(this.#auth);
|
|
97
110
|
return claims;
|
|
98
111
|
return null;
|
|
99
112
|
}
|
package/dist/token.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.mjs","sources":["token.lango"],"sourcesContent":["import \"./options\"\n contains(items, value)\nimport js global Object\n\nexport class Token(claims, readonly = false)\n properties\n clientId
|
|
1
|
+
{"version":3,"file":"token.mjs","sources":["token.lango"],"sourcesContent":["import \"./options\"\n contains(items, value)\nimport js global Object\n\nexport class Token(claims, readonly = false)\n properties\n clientId?\n auth?\n expiresAt?\n methods\n AddAuth(permission)\n clearAuth()\n hasAuth(permission)\n get(name)\n set(name, value)\n snapshot()\n renew()\n renewalRequested()\n\nToken(claims, readonly = false)\n @claims = from_json(to_json(claims))\n @readonly = readonly\n @renew = false\n @clientId = claims.clientId\n @auth = claims.auth\n @expiresAt = claims.exp\n Object/freeze(@this)\n\nToken:clientId?\n return @clientId\n\nToken:auth?\n return @auth\n\nToken:expiresAt?\n return @expiresAt\n\nToken:AddAuth(permission)\n writable()\n if !is_string(permission) or permission.trim() == \"\" or permission.includes(\",\")\n throw \"Permission must be a nonempty name without commas\"\n permission = permission.trim()\n if !hasAuth(permission)\n if @auth == \"\"\n @auth = permission\n else\n @auth = \"${@auth}, ${permission}\"\n return @this\n\nToken:clearAuth()\n writable()\n @auth = \"\"\n return @this\n\nToken:hasAuth(permission)\n for @auth.split(\",\") as name\n if name.trim() == permission\n return true\n return false\n\nToken:get(name)\n if !Object/hasOwn(@claims, name)\n return null\n return from_json(to_json(@claims[name]))\n\nToken:set(name, value)\n writable()\n if contains([\"clientId\", \"auth\", \"iat\", \"exp\", \"iss\", \"aud\", \"validatedAt\", \"nbf\", \"__proto__\", \"constructor\", \"prototype\"], name)\n throw \"Reserved token field: ${name}\"\n @claims[name] = from_json(to_json(value))\n return @this\n\nToken:snapshot()\n claims = from_json(to_json(@claims))\n if @clientId != claims.clientId or @expiresAt != claims.exp or !is_string(@auth)\n throw \"Token identity and expiry cannot be changed\"\n claims.auth = @auth\n return claims\n\nToken:renew()\n writable()\n @renew = true\n return @this\n\nToken:renewalRequested()\n return @renew\n\nToken:writable()\n if @readonly\n throw \"Token is read-only during this event\"\n return null\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lango-project/lango-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A small JSON HTTP server for Lango applications.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -41,6 +41,6 @@
|
|
|
41
41
|
"serve-static": "^2.2.1"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@lango-project/lango-js": "^0.1.
|
|
44
|
+
"@lango-project/lango-js": "^0.1.16"
|
|
45
45
|
}
|
|
46
46
|
}
|
package/src/options.lango
CHANGED
|
@@ -28,7 +28,7 @@ options(root, supplied = null)
|
|
|
28
28
|
port: 3000
|
|
29
29
|
workers: 2
|
|
30
30
|
maxQueue: 64
|
|
31
|
-
bodyLimit:
|
|
31
|
+
bodyLimit: 2097152
|
|
32
32
|
bodyTimeoutMs: 30000
|
|
33
33
|
requestTimeoutMs: 30000
|
|
34
34
|
eventTimeoutMs: 5000
|
|
@@ -99,7 +99,7 @@ deployment(directory)
|
|
|
99
99
|
seen.add(name)
|
|
100
100
|
switch name
|
|
101
101
|
case "static"
|
|
102
|
-
attributes(tag, ["path", "directory", "index"])
|
|
102
|
+
attributes(tag, ["path", "directory", "index", "fallback", "runtime"])
|
|
103
103
|
settings.static = tag.attributes()
|
|
104
104
|
continue
|
|
105
105
|
case "http"
|
package/src/runtime.lango
CHANGED
|
@@ -26,9 +26,12 @@ export class Runtime(definition)
|
|
|
26
26
|
preload()
|
|
27
27
|
handle(task)
|
|
28
28
|
close()
|
|
29
|
+
refreshTokens(tokens)
|
|
29
30
|
|
|
30
31
|
Runtime(definition)
|
|
31
|
-
|
|
32
|
+
refresh = (tokens)
|
|
33
|
+
return refreshTokens(tokens)
|
|
34
|
+
@serverContext = new ServerContext(definition.routes, refresh)
|
|
32
35
|
@definition = definition
|
|
33
36
|
@routes = []
|
|
34
37
|
@events = {}
|
|
@@ -206,6 +209,7 @@ Runtime:handle(task)
|
|
|
206
209
|
capture fresh
|
|
207
210
|
if !fresh and is_number(validatedAt)
|
|
208
211
|
stampValidation(claims, validatedAt)
|
|
212
|
+
token.renew()
|
|
209
213
|
if route.auth != "anonymous" and !token.hasAuth(route.auth)
|
|
210
214
|
throw {
|
|
211
215
|
status: 401
|
|
@@ -251,7 +255,7 @@ Runtime:handle(task)
|
|
|
251
255
|
result = response.result(body)
|
|
252
256
|
updated = tokenClaims(token, claims, result.status < 400)
|
|
253
257
|
result.headers["x-lango-token"] = @tokens.sign(updated)
|
|
254
|
-
if @definition.settings.requestEvent and "request" in @events
|
|
258
|
+
if task.internal != true and @definition.settings.requestEvent and "request" in @events
|
|
255
259
|
return audit(request.id, result, task.body, body, updated)
|
|
256
260
|
return result
|
|
257
261
|
catch(error)
|
|
@@ -278,6 +282,62 @@ Runtime:tokenClaims(token, claims, allowRenewal)
|
|
|
278
282
|
updated = @tokens.renew(updated)
|
|
279
283
|
return updated
|
|
280
284
|
|
|
285
|
+
Runtime:refreshTokens(tokens)
|
|
286
|
+
context request
|
|
287
|
+
context token
|
|
288
|
+
if !is_array(tokens) or tokens.length == 0 or tokens.length > 64
|
|
289
|
+
throw "Token refresh requires 1 to 64 tokens"
|
|
290
|
+
seen = []
|
|
291
|
+
for tokens as encoded
|
|
292
|
+
if !is_string(encoded) or encoded == "" or encoded.length > 16384 or seen.includes(encoded)
|
|
293
|
+
throw "Invalid refresh token"
|
|
294
|
+
seen.add(encoded)
|
|
295
|
+
owner = token.clientId?
|
|
296
|
+
results = []
|
|
297
|
+
pending = Promise/resolve(null)
|
|
298
|
+
for tokens as encoded
|
|
299
|
+
child = Object/assign({}, request, {
|
|
300
|
+
headers: Object/assign({}, request.headers, {
|
|
301
|
+
authorization: "Bearer ${encoded}"
|
|
302
|
+
})
|
|
303
|
+
})
|
|
304
|
+
pending = refreshOne(pending, results, encoded, owner, child)
|
|
305
|
+
return chain pending
|
|
306
|
+
then(ignored)
|
|
307
|
+
capture results
|
|
308
|
+
return results
|
|
309
|
+
|
|
310
|
+
Runtime:refreshOne(pending, results, encoded, owner, child)
|
|
311
|
+
return chain pending
|
|
312
|
+
then(ignored)
|
|
313
|
+
capture encoded
|
|
314
|
+
capture owner
|
|
315
|
+
capture child
|
|
316
|
+
checked = null
|
|
317
|
+
try
|
|
318
|
+
checked = @tokens.read("Bearer ${encoded}")
|
|
319
|
+
catch error
|
|
320
|
+
return failure(401, "Unauthorized")
|
|
321
|
+
if checked.clientId != owner
|
|
322
|
+
return failure(401, "Unauthorized")
|
|
323
|
+
return handle({
|
|
324
|
+
request: child
|
|
325
|
+
body: "{}"
|
|
326
|
+
internal: true
|
|
327
|
+
})
|
|
328
|
+
then(result)
|
|
329
|
+
capture results
|
|
330
|
+
capture encoded
|
|
331
|
+
updated = null
|
|
332
|
+
if result.status < 400
|
|
333
|
+
updated = result.headers["x-lango-token"]
|
|
334
|
+
results.add({
|
|
335
|
+
previous: encoded
|
|
336
|
+
token: updated
|
|
337
|
+
status: result.status
|
|
338
|
+
})
|
|
339
|
+
return null
|
|
340
|
+
|
|
281
341
|
Runtime:event(name, input)
|
|
282
342
|
if !(name in @events)
|
|
283
343
|
return null
|
|
@@ -320,8 +380,10 @@ Runtime:validate()
|
|
|
320
380
|
context request
|
|
321
381
|
context log
|
|
322
382
|
claims = token.snapshot()
|
|
323
|
-
if
|
|
383
|
+
if Date/now() - claims.validatedAt < 1000
|
|
324
384
|
return null
|
|
385
|
+
if !("validate" in @events)
|
|
386
|
+
return Date/now()
|
|
325
387
|
original = JSON/stringify(claims)
|
|
326
388
|
context token = new Token(claims, true)
|
|
327
389
|
return chain Promise/resolve(null)
|
package/src/server_context.lango
CHANGED
|
@@ -5,16 +5,24 @@ import js global clearTimeout
|
|
|
5
5
|
import "./route_info"
|
|
6
6
|
routeInfo(definitions)
|
|
7
7
|
|
|
8
|
-
export class ServerContext(definitions = null)
|
|
8
|
+
export class ServerContext(definitions = null, refresh = null)
|
|
9
9
|
methods
|
|
10
10
|
publish(path, key, data)
|
|
11
11
|
cursor(path, key)
|
|
12
12
|
routes()
|
|
13
|
+
refreshTokens(tokens)
|
|
13
14
|
|
|
14
|
-
ServerContext(definitions = null)
|
|
15
|
+
ServerContext(definitions = null, refresh = null)
|
|
15
16
|
if definitions == null
|
|
16
17
|
definitions = []
|
|
17
18
|
@routes = routeInfo(definitions)
|
|
19
|
+
@refresh = refresh
|
|
20
|
+
|
|
21
|
+
ServerContext:refreshTokens(tokens)
|
|
22
|
+
if @refresh == null
|
|
23
|
+
throw "Token refresh is unavailable"
|
|
24
|
+
refresh = @refresh
|
|
25
|
+
return refresh(tokens)
|
|
18
26
|
|
|
19
27
|
ServerContext:routes()
|
|
20
28
|
return from_json(to_json(@routes))
|
package/src/static_files.lango
CHANGED
|
@@ -2,6 +2,8 @@ import js "node:fs" as fs
|
|
|
2
2
|
import js "node:path" as path
|
|
3
3
|
import js "serve-static" as files
|
|
4
4
|
import js global RegExp
|
|
5
|
+
import js global decodeURIComponent
|
|
6
|
+
import js global String
|
|
5
7
|
|
|
6
8
|
export staticOptions(root, spec)
|
|
7
9
|
export class StaticFiles(settings)
|
|
@@ -13,7 +15,7 @@ staticOptions(root, spec)
|
|
|
13
15
|
return null
|
|
14
16
|
if !is_object(spec) or is_array(spec)
|
|
15
17
|
throw "static must be a map"
|
|
16
|
-
allowed = ["path", "directory", "index"]
|
|
18
|
+
allowed = ["path", "directory", "index", "fallback", "runtime"]
|
|
17
19
|
for spec as key: value
|
|
18
20
|
if !allowed.includes(key)
|
|
19
21
|
throw "Unknown static option: ${key}"
|
|
@@ -32,22 +34,42 @@ staticOptions(root, spec)
|
|
|
32
34
|
index = "index.html"
|
|
33
35
|
if !is_string(index) or index == "" or index.startsWith(".") or index.includes("/") or index.includes("\\")
|
|
34
36
|
throw "Static index must be a file name"
|
|
37
|
+
fallback = spec.fallback
|
|
38
|
+
if fallback != null and (!is_string(fallback) or fallback == "" or fallback.startsWith(".") or fallback.includes("/") or fallback.includes("\\"))
|
|
39
|
+
throw "Static fallback must be a file name"
|
|
40
|
+
runtime = spec.runtime
|
|
41
|
+
if runtime == null or runtime == "false"
|
|
42
|
+
runtime = false
|
|
43
|
+
if runtime == "true"
|
|
44
|
+
runtime = true
|
|
45
|
+
if !is_bool(runtime)
|
|
46
|
+
throw "Static runtime must be boolean"
|
|
35
47
|
return {
|
|
36
48
|
path: mount
|
|
37
49
|
directory: path/resolve(root, spec.directory)
|
|
38
50
|
index: index
|
|
51
|
+
fallback: fallback
|
|
52
|
+
runtime: runtime
|
|
39
53
|
}
|
|
40
54
|
|
|
41
55
|
StaticFiles(settings)
|
|
42
56
|
@settings = settings
|
|
43
57
|
if !fs/statSync(settings.directory).isDirectory()
|
|
44
58
|
throw "Static directory must be a directory"
|
|
59
|
+
if settings.fallback != null and !fs/statSync(path/join(settings.directory, settings.fallback)).isFile()
|
|
60
|
+
throw "Static fallback must be a file"
|
|
45
61
|
@serve = files/default(settings.directory, {
|
|
46
62
|
index: settings.index
|
|
47
63
|
dotfiles: "deny"
|
|
48
64
|
fallthrough: false
|
|
49
65
|
redirect: false
|
|
50
66
|
})
|
|
67
|
+
@runtime = files/default(settings.directory, {
|
|
68
|
+
index: false
|
|
69
|
+
dotfiles: "allow"
|
|
70
|
+
fallthrough: false
|
|
71
|
+
redirect: false
|
|
72
|
+
})
|
|
51
73
|
|
|
52
74
|
StaticFiles:receive(req, res, failed)
|
|
53
75
|
mount = @settings.path
|
|
@@ -60,31 +82,85 @@ StaticFiles:receive(req, res, failed)
|
|
|
60
82
|
req.url = url.slice(mount.length)
|
|
61
83
|
if req.url == "" or req.url.startsWith("?")
|
|
62
84
|
req.url = "/${req.url}"
|
|
85
|
+
local = req.url.split("?")[0]
|
|
86
|
+
decoded = null
|
|
87
|
+
try
|
|
88
|
+
decoded = decodeURIComponent(local)
|
|
89
|
+
catch error
|
|
90
|
+
failed(400, "InvalidPath")
|
|
91
|
+
return null
|
|
92
|
+
if decoded.includes("\\") or decoded.includes(String/fromCharCode(0))
|
|
93
|
+
failed(400, "InvalidPath")
|
|
94
|
+
return null
|
|
95
|
+
segments = decoded.split("/")
|
|
96
|
+
runtime = false
|
|
97
|
+
for segments as index: segment
|
|
98
|
+
if segment.startsWith(".")
|
|
99
|
+
if index == 1 and segment == ".lango" and @settings.runtime
|
|
100
|
+
runtime = true
|
|
101
|
+
else
|
|
102
|
+
failed(403, "Forbidden")
|
|
103
|
+
return null
|
|
63
104
|
done = (error)
|
|
105
|
+
capture req
|
|
64
106
|
capture res
|
|
65
107
|
capture failed
|
|
66
|
-
|
|
67
|
-
|
|
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)
|
|
108
|
+
capture decoded
|
|
109
|
+
failure(req, res, failed, error, decoded)
|
|
88
110
|
serve = @serve
|
|
111
|
+
if runtime
|
|
112
|
+
serve = @runtime
|
|
89
113
|
serve(req, res, done)
|
|
90
114
|
return null
|
|
115
|
+
|
|
116
|
+
StaticFiles:failure(req, res, failed, error, pathname = null)
|
|
117
|
+
if res.headersSent
|
|
118
|
+
res.destroy()
|
|
119
|
+
return null
|
|
120
|
+
status = 404
|
|
121
|
+
code = "NotFound"
|
|
122
|
+
if error != null
|
|
123
|
+
switch error.status
|
|
124
|
+
case 400
|
|
125
|
+
status = 400
|
|
126
|
+
code = "InvalidPath"
|
|
127
|
+
case 403
|
|
128
|
+
status = 403
|
|
129
|
+
code = "Forbidden"
|
|
130
|
+
case 416
|
|
131
|
+
status = 416
|
|
132
|
+
code = "RangeNotSatisfiable"
|
|
133
|
+
case 404
|
|
134
|
+
status = 404
|
|
135
|
+
else
|
|
136
|
+
status = 500
|
|
137
|
+
code = "InternalServerError"
|
|
138
|
+
if status == 404 and pathname != null and @settings.fallback != null and !pathname.startsWith("/.lango") and path/posix/extname(pathname) == "" and acceptsHTML(req.headers.accept)
|
|
139
|
+
req.url = "/${@settings.fallback}"
|
|
140
|
+
done = (error)
|
|
141
|
+
capture req
|
|
142
|
+
capture res
|
|
143
|
+
capture failed
|
|
144
|
+
failure(req, res, failed, error)
|
|
145
|
+
serve = @serve
|
|
146
|
+
serve(req, res, done)
|
|
147
|
+
return null
|
|
148
|
+
failed(status, code)
|
|
149
|
+
return null
|
|
150
|
+
|
|
151
|
+
acceptsHTML(value)
|
|
152
|
+
if !is_string(value)
|
|
153
|
+
return false
|
|
154
|
+
zero = new RegExp("^q\\s*=\\s*0(?:\\.0*)?$", "i")
|
|
155
|
+
for value.split(",") as range
|
|
156
|
+
parts = range.split(";")
|
|
157
|
+
media = parts[0]
|
|
158
|
+
type = media.trim().toLowerCase()
|
|
159
|
+
if type == "text/html" or type == "application/xhtml+xml"
|
|
160
|
+
allowed = true
|
|
161
|
+
for parts.slice(1) as parameter
|
|
162
|
+
if zero.test(parameter.trim())
|
|
163
|
+
allowed = false
|
|
164
|
+
if allowed
|
|
165
|
+
return true
|
|
166
|
+
return false
|