@3-/srv 0.0.1 → 0.1.9
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/bun.lock +30 -0
- package/install.js +9 -0
- package/package.json +22 -1
- package/uninstall.js +9 -0
- package/README.md +0 -41
- package/br.js +0 -37
- package/index.js +0 -116
package/bun.lock
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"lockfileVersion": 1,
|
|
3
|
+
"configVersion": 1,
|
|
4
|
+
"workspaces": {
|
|
5
|
+
"": {
|
|
6
|
+
"name": "@3-/srv",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"zx": "^8.8.5",
|
|
9
|
+
},
|
|
10
|
+
"optionalDependencies": {
|
|
11
|
+
"@3-/srv-darwin": "file:../darwin",
|
|
12
|
+
"@3-/srv-linux": "file:../linux",
|
|
13
|
+
"@3-/srv-win32": "file:../win32",
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
"packages": {
|
|
18
|
+
"@3-/srv-darwin": ["@3-/srv-darwin@file:../darwin", { "dependencies": { "@3-/srv-obj-replace": "file:../obj_replace", "zx": "^8.8.5" } }],
|
|
19
|
+
|
|
20
|
+
"@3-/srv-linux": ["@3-/srv-linux@file:../linux", { "dependencies": { "@3-/srv-obj-replace": "file:../obj_replace", "zx": "^8.8.5" } }],
|
|
21
|
+
|
|
22
|
+
"@3-/srv-win32": ["@3-/srv-win32@file:../win32", { "dependencies": { "zx": "^8.8.5" } }],
|
|
23
|
+
|
|
24
|
+
"zx": ["zx@8.8.5", "", { "bin": { "zx": "build/cli.js" } }, "sha512-SNgDF5L0gfN7FwVOdEFguY3orU5AkfFZm9B5YSHog/UDHv+lvmd82ZAsOenOkQixigwH2+yyH198AwNdKhj+RA=="],
|
|
25
|
+
|
|
26
|
+
"@3-/srv-darwin/@3-/srv-obj-replace": ["@3-/srv-obj-replace@file:../obj_replace", {}],
|
|
27
|
+
|
|
28
|
+
"@3-/srv-linux/@3-/srv-obj-replace": ["@3-/srv-obj-replace@file:../obj_replace", {}],
|
|
29
|
+
}
|
|
30
|
+
}
|
package/install.js
ADDED
package/package.json
CHANGED
|
@@ -1 +1,22 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"name": "@3-/srv",
|
|
3
|
+
"version": "0.1.9",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
"./install.js": "./install.js",
|
|
7
|
+
"./uninstall.js": "./uninstall.js"
|
|
8
|
+
},
|
|
9
|
+
"optionalDependencies": {
|
|
10
|
+
"@3-/srv-darwin": "file:../darwin",
|
|
11
|
+
"@3-/srv-linux": "file:../linux",
|
|
12
|
+
"@3-/srv-win32": "file:../win32"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"zx": "^8.8.5"
|
|
16
|
+
},
|
|
17
|
+
"repository": "https://github.com/i18n-site/lib/tree/dev",
|
|
18
|
+
"homepage": "https://github.com/i18n-site/lib/tree/dev",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/i18n-site/lib/issues"
|
|
21
|
+
}
|
|
22
|
+
}
|
package/uninstall.js
ADDED
package/README.md
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
[‼️]: ✏️README.mdt
|
|
2
|
-
|
|
3
|
-
# @3-/srv
|
|
4
|
-
|
|
5
|
-
返回值
|
|
6
|
-
|
|
7
|
-
如果函数,会被传入 res ,可以 res.writeStatus(status).end(r)
|
|
8
|
-
|
|
9
|
-
如果不是 undefined ,会被 msgpack pack
|
|
10
|
-
|
|
11
|
-
post,put
|
|
12
|
-
|
|
13
|
-
如果 content_type.endsWith '/json'
|
|
14
|
-
|
|
15
|
-
body 如果 length > 1
|
|
16
|
-
|
|
17
|
-
会被 json parse
|
|
18
|
-
|
|
19
|
-
如果是一个数组 会 f.apply opt, body
|
|
20
|
-
|
|
21
|
-
否则会 f.call opt, body
|
|
22
|
-
|
|
23
|
-
否则如果 body 有的长度 f.call opt, body
|
|
24
|
-
|
|
25
|
-
[test/main.coffee](./test/main.coffee) :
|
|
26
|
-
|
|
27
|
-
```coffee
|
|
28
|
-
#!/usr/bin/env coffee
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
output :
|
|
32
|
-
|
|
33
|
-
```
|
|
34
|
-
/test
|
|
35
|
-
→ http://127.0.0.1:3223
|
|
36
|
-
uWS.App {}
|
|
37
|
-
post test
|
|
38
|
-
post test
|
|
39
|
-
post test
|
|
40
|
-
post test
|
|
41
|
-
```
|
package/br.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
var brCompress;
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
brotliCompress
|
|
5
|
-
} from 'zlib';
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
promisify
|
|
9
|
-
} from 'util';
|
|
10
|
-
|
|
11
|
-
brCompress = promisify(brotliCompress);
|
|
12
|
-
|
|
13
|
-
export default async(res, status, accept_encoding, content_type, bin) => {
|
|
14
|
-
var br, content_encoding, length;
|
|
15
|
-
({length} = bin);
|
|
16
|
-
if (bin.length > 512) {
|
|
17
|
-
if (accept_encoding != null ? accept_encoding.includes('br') : void 0) {
|
|
18
|
-
br = (await brCompress(bin));
|
|
19
|
-
if ((20 + br.length) < length) { // 因为 Content-Encoding: br 是 20 字节
|
|
20
|
-
content_encoding = 'br';
|
|
21
|
-
bin = br;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
if (!res.aborted) {
|
|
26
|
-
res.cork(() => {
|
|
27
|
-
res.writeStatus(status);
|
|
28
|
-
if (content_type) {
|
|
29
|
-
res.writeHeader('Content-Type', content_type);
|
|
30
|
-
}
|
|
31
|
-
if (content_encoding) {
|
|
32
|
-
res.writeHeader('Content-Encoding', content_encoding);
|
|
33
|
-
}
|
|
34
|
-
res.end(br);
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
};
|
package/index.js
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
var bind;
|
|
2
|
-
|
|
3
|
-
import uWebSockets from 'uWebSockets';
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
pack
|
|
7
|
-
} from '@3-/msgpack';
|
|
8
|
-
|
|
9
|
-
import br from './br.js';
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
Cookie
|
|
13
|
-
} from 'tough-cookie';
|
|
14
|
-
|
|
15
|
-
({PORT} = process.env);
|
|
16
|
-
|
|
17
|
-
export var OK = '200';
|
|
18
|
-
|
|
19
|
-
export var NOT_FOUND = '404';
|
|
20
|
-
|
|
21
|
-
export var PORT = +PORT;
|
|
22
|
-
|
|
23
|
-
bind = (ws, name, func) => {
|
|
24
|
-
console.log('/' + name);
|
|
25
|
-
ws.any('/' + name, async(res, req) => {
|
|
26
|
-
var accept_encoding, accept_language, body, content_type, cookie, err, method, opt, r, res_content_type, status, url;
|
|
27
|
-
method = req.getMethod();
|
|
28
|
-
url = req.getUrl();
|
|
29
|
-
content_type = req.getHeader('content-type');
|
|
30
|
-
accept_language = req.getHeader('accept-language');
|
|
31
|
-
cookie = req.getHeader('cookie');
|
|
32
|
-
if (cookie) {
|
|
33
|
-
cookie = Cookie.parse(cookie);
|
|
34
|
-
}
|
|
35
|
-
accept_encoding = req.getHeader('accept-encoding');
|
|
36
|
-
opt = {content_type, accept_language, method, url, cookie};
|
|
37
|
-
if (accept_encoding) {
|
|
38
|
-
opt.accept_encoding = accept_encoding.split(',').map((i) => {
|
|
39
|
-
return i.trim();
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
try {
|
|
43
|
-
switch (method) {
|
|
44
|
-
case 'post':
|
|
45
|
-
case 'put':
|
|
46
|
-
res.onAborted(() => {
|
|
47
|
-
res.aborted = true;
|
|
48
|
-
});
|
|
49
|
-
body = (await new Promise((resolve) => {
|
|
50
|
-
var li;
|
|
51
|
-
li = [];
|
|
52
|
-
res.onData((buf, isLast) => {
|
|
53
|
-
if (buf.byteLength > 0) {
|
|
54
|
-
li.push(Buffer.from(buf));
|
|
55
|
-
}
|
|
56
|
-
if (isLast) {
|
|
57
|
-
if (li.length > 0) {
|
|
58
|
-
li = Buffer.concat(li);
|
|
59
|
-
}
|
|
60
|
-
resolve(li);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
}));
|
|
64
|
-
}
|
|
65
|
-
if (body && body.length > 0) {
|
|
66
|
-
if (content_type.endsWith('/json')) {
|
|
67
|
-
body = JSON.parse(body);
|
|
68
|
-
if (Array.isArray(body)) {
|
|
69
|
-
r = (await func.apply(opt, body));
|
|
70
|
-
} else {
|
|
71
|
-
r = (await func.call(opt, body));
|
|
72
|
-
}
|
|
73
|
-
} else {
|
|
74
|
-
r = (await func.call(opt, body));
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
r = (await func.call(opt));
|
|
78
|
-
}
|
|
79
|
-
if (r instanceof Function) {
|
|
80
|
-
await r(res);
|
|
81
|
-
} else {
|
|
82
|
-
if (r === void 0) {
|
|
83
|
-
r = '';
|
|
84
|
-
} else {
|
|
85
|
-
r = pack(r);
|
|
86
|
-
res_content_type = 'm';
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
status = OK;
|
|
90
|
-
} catch (error) {
|
|
91
|
-
err = error;
|
|
92
|
-
status = '500';
|
|
93
|
-
r = '' + err;
|
|
94
|
-
console.error(method, url, err);
|
|
95
|
-
}
|
|
96
|
-
console.log(status, method, name);
|
|
97
|
-
br(res, status, accept_encoding, res_content_type, r);
|
|
98
|
-
});
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
export default (route) => {
|
|
102
|
-
var f, name, ref, ws, x;
|
|
103
|
-
ws = uWebSockets.App({});
|
|
104
|
-
ref = Object.entries(route);
|
|
105
|
-
for (x of ref) {
|
|
106
|
-
[name, f] = x;
|
|
107
|
-
bind(ws, name, f);
|
|
108
|
-
}
|
|
109
|
-
return ws.any('/*', (res, req) => {
|
|
110
|
-
console.log(NOT_FOUND, req.getMethod(), req.getUrl());
|
|
111
|
-
// https://unetworking.github.io/uWebSockets.js/generated/interfaces/HttpRequest.html#getMethod
|
|
112
|
-
res.writeStatus(NOT_FOUND).end('');
|
|
113
|
-
}).listen(PORT, () => {
|
|
114
|
-
console.log('→ http://127.0.0.1:' + PORT);
|
|
115
|
-
});
|
|
116
|
-
};
|