manner.js 0.0.42 → 0.0.43
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/client/index.js +32 -1
- package/client/lib/clearCookie.js +20 -1
- package/client/lib/filterNamespace.js +15 -1
- package/client/lib/readCookie.js +38 -1
- package/client/lib/setCookie.js +25 -1
- package/package.json +1 -1
- package/server/class/CommonHttp.js +143 -1
- package/server/index.js +27 -1
- package/server/lib/formateHttpDate.js +11 -1
- package/server/lib/formateHttpKey.js +11 -1
- package/server/lib/getHtml.js +20 -2
- package/server/lib/parseDateString.js +9 -1
- package/server/lib/parseOption.js +70 -1
- package/server/lib/readCookie.js +27 -1
package/client/index.js
CHANGED
|
@@ -1 +1,32 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "clearCookie", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _setCookie.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "filterNamespace", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _filterNamespace.default;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "readCookie", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _setCookie.default;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "setCookie", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () {
|
|
27
|
+
return _setCookie.default;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
var _filterNamespace = _interopRequireDefault(require("./lib/filterNamespace"));
|
|
31
|
+
var _setCookie = _interopRequireDefault(require("./lib/setCookie"));
|
|
32
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -1 +1,20 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = clearCookie;
|
|
7
|
+
function clearCookie(namespaces) {
|
|
8
|
+
Object.keys(namespaces).forEach(n => {
|
|
9
|
+
if (n.expires !== undefined) {
|
|
10
|
+
Object.keys(n).forEach(k => {
|
|
11
|
+
if (n === 'user') {
|
|
12
|
+
document.cookie[n + '_' + k] = '';
|
|
13
|
+
}
|
|
14
|
+
window.localStorage.removeItem(n + '_' + k);
|
|
15
|
+
});
|
|
16
|
+
delete namespaces[n];
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return namespaces;
|
|
20
|
+
}
|
|
@@ -1 +1,15 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = filterNamespace;
|
|
7
|
+
function filterNamespace(namespace) {
|
|
8
|
+
const ans = {};
|
|
9
|
+
Object.keys(namespace).forEach(k => {
|
|
10
|
+
if (k !== 'expires') {
|
|
11
|
+
ans[k] = namespace[k];
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
return ans;
|
|
15
|
+
}
|
package/client/lib/readCookie.js
CHANGED
|
@@ -1 +1,38 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = readCookie;
|
|
7
|
+
var _clearCookie = _interopRequireDefault(require("./clearCookie"));
|
|
8
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
|
+
function readCookie() {
|
|
10
|
+
const cookies = {};
|
|
11
|
+
document.cookie.split(';').forEach(i => {
|
|
12
|
+
const [key, value] = i.split('=');
|
|
13
|
+
cookies[key.trim()] = value;
|
|
14
|
+
});
|
|
15
|
+
const namespaces = {};
|
|
16
|
+
Object.keys(cookies).forEach(k => {
|
|
17
|
+
const result = k.split('_');
|
|
18
|
+
if (result.length === 2) {
|
|
19
|
+
const [namespace, key] = result;
|
|
20
|
+
if (namespaces[namespace] === undefined) {
|
|
21
|
+
namespaces[namespace] = {};
|
|
22
|
+
}
|
|
23
|
+
namespaces[namespace][key] = cookies[k];
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
for (let i = 0; i < window.localStorage.length; i += 1) {
|
|
27
|
+
const k = window.localStorage.key(i);
|
|
28
|
+
const result = k.split('_');
|
|
29
|
+
if (result.length === 2) {
|
|
30
|
+
const [namespace, key] = result;
|
|
31
|
+
if (namespaces[namespace] === undefined) {
|
|
32
|
+
namespaces[namespace] = {};
|
|
33
|
+
}
|
|
34
|
+
namespaces[namespace][key] = window.localStorage.getItem(k);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return (0, _clearCookie.default)(namespaces);
|
|
38
|
+
}
|
package/client/lib/setCookie.js
CHANGED
|
@@ -1 +1,25 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = setCookie;
|
|
7
|
+
function setCookie(response) {
|
|
8
|
+
const cookie = response.headers.get('cookie');
|
|
9
|
+
if (cookie !== null) {
|
|
10
|
+
if (cookie === '') {
|
|
11
|
+
document.cookie.split(';').forEach(c => {
|
|
12
|
+
const [k, v] = c.split('=');
|
|
13
|
+
const [namespace] = k.split('_');
|
|
14
|
+
if (namespace === 'user') {
|
|
15
|
+
document.cookie = k + '=' + v;
|
|
16
|
+
}
|
|
17
|
+
window.localStorage.set(k, v);
|
|
18
|
+
});
|
|
19
|
+
} else {
|
|
20
|
+
cookie.split(';').forEach(c => {
|
|
21
|
+
document.cookie = c;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
package/package.json
CHANGED
|
@@ -1 +1,143 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _path = _interopRequireDefault(require("path"));
|
|
8
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
9
|
+
var _zlib = _interopRequireDefault(require("zlib"));
|
|
10
|
+
var _getHtml = _interopRequireDefault(require("../lib/getHtml"));
|
|
11
|
+
var _parseDateString = _interopRequireDefault(require("../lib/parseDateString"));
|
|
12
|
+
var _formateHttpDate = _interopRequireDefault(require("../lib/formateHttpDate"));
|
|
13
|
+
var _formateHttpKey = _interopRequireDefault(require("../lib/formateHttpKey"));
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
function getLists(list) {
|
|
16
|
+
return list.join('|');
|
|
17
|
+
}
|
|
18
|
+
class CommonHttp {
|
|
19
|
+
constructor(options) {
|
|
20
|
+
const {
|
|
21
|
+
fonts
|
|
22
|
+
} = options;
|
|
23
|
+
if (options.fonts === undefined) {
|
|
24
|
+
options.fonts = [];
|
|
25
|
+
}
|
|
26
|
+
this.time = new Date().getTime();
|
|
27
|
+
this.modify = {};
|
|
28
|
+
this.file = {};
|
|
29
|
+
this.raw = {};
|
|
30
|
+
this.compress = {};
|
|
31
|
+
this.options = options;
|
|
32
|
+
this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['ico', 'js', 'png', 'jpg']))})$`);
|
|
33
|
+
}
|
|
34
|
+
compressOutput(req, res, buffer, path) {
|
|
35
|
+
res.setHeader('Vary', 'Accept-Encoding');
|
|
36
|
+
let acceptEncoding = req.headers['accept-encoding'];
|
|
37
|
+
if (/gzip/.test(acceptEncoding)) {
|
|
38
|
+
res.writeHead(200, {
|
|
39
|
+
'Content-Encoding': 'gzip'
|
|
40
|
+
});
|
|
41
|
+
this.dealCompress(_zlib.default.gzipSync(buffer), path, res);
|
|
42
|
+
} else {
|
|
43
|
+
res.writeHead(200, {});
|
|
44
|
+
this.dealDirect(buffer, path, res);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
cacheOutput(req, res, path, file, ms) {
|
|
48
|
+
let ifModifiedSince = req.headers['If-Modified-Since'];
|
|
49
|
+
if (ifModifiedSince === undefined) {
|
|
50
|
+
if (this.modify[path] === undefined) {
|
|
51
|
+
this.modify[path] = new Date(ms).toString();
|
|
52
|
+
}
|
|
53
|
+
if (this.file[path] === undefined) {
|
|
54
|
+
this.file[path] = file;
|
|
55
|
+
}
|
|
56
|
+
res.setHeader('Last-Modified', (0, _formateHttpDate.default)(this.modify[path]));
|
|
57
|
+
this.compressOutput(req, res, this.file[path], path);
|
|
58
|
+
} else {
|
|
59
|
+
if (this.modify[path] === undefined) {
|
|
60
|
+
this.modify[path] = new Date(ms).toString();
|
|
61
|
+
}
|
|
62
|
+
if ((0, _parseDateString.default)(new Date().toString()) < (0, _parseDateString.default)(this.modify[path])) {
|
|
63
|
+
if (this.file[path] === undefined) {
|
|
64
|
+
this.file[path] = file;
|
|
65
|
+
}
|
|
66
|
+
res.setHeader('Last-Modified', (0, _formateHttpDate.default)(this.modify[path]));
|
|
67
|
+
this.compressOutput(req, res, this.file[path], path);
|
|
68
|
+
} else {
|
|
69
|
+
res.writeHead(304);
|
|
70
|
+
res.end();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
dealCompress(data, path, res) {
|
|
75
|
+
if (this.compress[path] === undefined) {
|
|
76
|
+
this.compress[path] = data;
|
|
77
|
+
}
|
|
78
|
+
res.end(this.compress[path]);
|
|
79
|
+
}
|
|
80
|
+
directDeal(data, path, res) {
|
|
81
|
+
if (this.raw[path] === undefined) {
|
|
82
|
+
this.compress[path] = data;
|
|
83
|
+
}
|
|
84
|
+
res.end(this.raw[path]);
|
|
85
|
+
}
|
|
86
|
+
async process(req, res) {
|
|
87
|
+
try {
|
|
88
|
+
const {
|
|
89
|
+
url
|
|
90
|
+
} = req;
|
|
91
|
+
if (url === '/update/time') {
|
|
92
|
+
const {
|
|
93
|
+
time
|
|
94
|
+
} = this;
|
|
95
|
+
res.end(String(time));
|
|
96
|
+
} else if (this.regexp.test(url)) {
|
|
97
|
+
const restPath = url.substring(1, url.length);
|
|
98
|
+
this.cacheOutput(req, res, restPath, _fs.default.readFileSync(_path.default.resolve('static', restPath)), parseInt(_fs.default.statSync(_path.default.resolve('static', restPath)).mtimeMs));
|
|
99
|
+
} else if (url.substring(0, 4) === '/api') {
|
|
100
|
+
const body = await new Promise((resolve, reject) => {
|
|
101
|
+
req.on('data', data => {
|
|
102
|
+
resolve(data.toString());
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
const {
|
|
106
|
+
location
|
|
107
|
+
} = this.options;
|
|
108
|
+
if (location !== undefined) {
|
|
109
|
+
const response = await fetch(location + url, {
|
|
110
|
+
method: 'POST',
|
|
111
|
+
body
|
|
112
|
+
});
|
|
113
|
+
for (const k of response.headers.keys()) {
|
|
114
|
+
res.setHeader((0, _formateHttpKey.default)(k), response.headers.get(k));
|
|
115
|
+
}
|
|
116
|
+
const data = await response.text();
|
|
117
|
+
res.end(JSON.stringify(data));
|
|
118
|
+
}
|
|
119
|
+
} else {
|
|
120
|
+
if (this.html === undefined) {
|
|
121
|
+
const {
|
|
122
|
+
title,
|
|
123
|
+
content,
|
|
124
|
+
icon
|
|
125
|
+
} = this.options;
|
|
126
|
+
this.html = (0, _getHtml.default)(title, content, icon);
|
|
127
|
+
}
|
|
128
|
+
this.cacheOutput(req, res, '*html', this.html, this.time);
|
|
129
|
+
}
|
|
130
|
+
} catch (e) {
|
|
131
|
+
const {
|
|
132
|
+
develope
|
|
133
|
+
} = this.options;
|
|
134
|
+
if (develope === true) {
|
|
135
|
+
throw e;
|
|
136
|
+
} else {
|
|
137
|
+
res.writeHead(500);
|
|
138
|
+
res.end();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
exports.default = CommonHttp;
|
package/server/index.js
CHANGED
|
@@ -1 +1,27 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "CommonHttp", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _CommonHttp.default;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "parseOption", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _parseOption.default;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "readCookie", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _readCookie.default;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
var _CommonHttp = _interopRequireDefault(require("./class/CommonHttp"));
|
|
25
|
+
var _parseOption = _interopRequireDefault(require("./lib/parseOption"));
|
|
26
|
+
var _readCookie = _interopRequireDefault(require("./lib/readCookie"));
|
|
27
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -1 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = formateHttpDate;
|
|
7
|
+
function formateHttpDate(date) {
|
|
8
|
+
let [week, month, day, year, time, zone] = date.toString().split(' ');
|
|
9
|
+
zone = zone.split('+')[0];
|
|
10
|
+
return month + ', ' + day + ' ' + month + ' ' + year + ' ' + time + ' ' + zone;
|
|
11
|
+
}
|
|
@@ -1 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = formateHttpKey;
|
|
7
|
+
function formateHttpKey(key) {
|
|
8
|
+
return key.split('-').map(v => {
|
|
9
|
+
return v.substring(0, 1).toUpperCase() + v.substring(1, v.length);
|
|
10
|
+
}).join('-');
|
|
11
|
+
}
|
package/server/lib/getHtml.js
CHANGED
|
@@ -1,4 +1,14 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = getHtml;
|
|
7
|
+
var _handlebars = _interopRequireDefault(require("handlebars"));
|
|
8
|
+
var _htmlMinifier = require("html-minifier");
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
function getHtml(title, content, icon) {
|
|
11
|
+
const template = _handlebars.default.compile(`
|
|
2
12
|
<!doctype html>
|
|
3
13
|
<html lang="en">
|
|
4
14
|
<head>
|
|
@@ -17,4 +27,12 @@
|
|
|
17
27
|
<script src="main.bundle.js"></script>
|
|
18
28
|
</body>
|
|
19
29
|
</html>
|
|
20
|
-
`);
|
|
30
|
+
`);
|
|
31
|
+
return (0, _htmlMinifier.minify)(template({
|
|
32
|
+
title,
|
|
33
|
+
content,
|
|
34
|
+
icon
|
|
35
|
+
}), {
|
|
36
|
+
collapseWhitespace: true
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -1 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = parseDateString;
|
|
7
|
+
function parseDateString(dateString) {
|
|
8
|
+
return new Date(httpDate).getTime();
|
|
9
|
+
}
|
|
@@ -1 +1,70 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = parseOption;
|
|
7
|
+
function isOption(string) {
|
|
8
|
+
let ans = true;
|
|
9
|
+
if (typeof string === 'string') {
|
|
10
|
+
let i = 0;
|
|
11
|
+
for (i = 0; i < 1; i += 1) {
|
|
12
|
+
if (string.charAt(i) === '-') {
|
|
13
|
+
break;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (i === 0) {
|
|
17
|
+
ans = false;
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
ans = false;
|
|
21
|
+
}
|
|
22
|
+
return ans;
|
|
23
|
+
}
|
|
24
|
+
function transformOption(value) {
|
|
25
|
+
let ans;
|
|
26
|
+
const s = value.split('-');
|
|
27
|
+
if (s.length >= 1) {
|
|
28
|
+
ans = s.map((e, i) => {
|
|
29
|
+
if (i === 0) {
|
|
30
|
+
return e;
|
|
31
|
+
} else {
|
|
32
|
+
return e.charAt(0).toUpperCase() + e.substring(1, e.length);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
} else {
|
|
36
|
+
ans = value;
|
|
37
|
+
}
|
|
38
|
+
return ans.join('');
|
|
39
|
+
}
|
|
40
|
+
function parseOption(...params) {
|
|
41
|
+
const ans = {};
|
|
42
|
+
for (let i = 0; i < params.length; i += 1) {
|
|
43
|
+
const param = params[i];
|
|
44
|
+
if (param.charAt(0) === '-') {
|
|
45
|
+
const regexp = /^\-([a-z])$/;
|
|
46
|
+
if (regexp.test(param)) {
|
|
47
|
+
const [_, k] = param.match(regexp);
|
|
48
|
+
if (isOption(params[i + 1])) {
|
|
49
|
+
ans[k] = params[i + 1];
|
|
50
|
+
i += 1;
|
|
51
|
+
} else {
|
|
52
|
+
ans[k] = true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (param.charAt(1) === '-') {
|
|
56
|
+
const regexp = /^\-\-([a-z\-]+)$/;
|
|
57
|
+
if (regexp.test(param)) {
|
|
58
|
+
const [_, k] = param.match(regexp);
|
|
59
|
+
if (isOption(params[i + 1])) {
|
|
60
|
+
ans[transformOption(k)] = params[i + 1];
|
|
61
|
+
i += 1;
|
|
62
|
+
} else {
|
|
63
|
+
ans[k] = true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return ans;
|
|
70
|
+
}
|
package/server/lib/readCookie.js
CHANGED
|
@@ -1 +1,27 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.readCookie = readCookie;
|
|
7
|
+
function readCookie(cookie) {
|
|
8
|
+
const cookies = {};
|
|
9
|
+
if (typeof cookie === 'string') {
|
|
10
|
+
cookie.split(';').forEach(i => {
|
|
11
|
+
const [key, value] = i.split('=');
|
|
12
|
+
cookies[key.trim()] = value;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
const namespaces = {};
|
|
16
|
+
Object.keys(cookies).forEach(k => {
|
|
17
|
+
const result = k.split('_');
|
|
18
|
+
if (result.length === 2) {
|
|
19
|
+
const [namespace, key] = result;
|
|
20
|
+
if (namespaces[namespace] === undefined) {
|
|
21
|
+
namespaces[namespace] = {};
|
|
22
|
+
}
|
|
23
|
+
namespaces[namespace][key] = cookies[k];
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return namespaces;
|
|
27
|
+
}
|