manner.js 0.0.26 → 0.0.28
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.js +81 -1
- package/package.json +1 -1
- package/server.js +206 -2
package/client.js
CHANGED
|
@@ -1 +1,81 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.clearCookie = clearCookie;
|
|
7
|
+
exports.filterNamespace = filterNamespace;
|
|
8
|
+
exports.readCookie = readCookie;
|
|
9
|
+
exports.setCookie = setCookie;
|
|
10
|
+
function clearCookie(namespaces) {
|
|
11
|
+
Object.keys(namespaces).forEach(n => {
|
|
12
|
+
if (n.expires !== undefined) {
|
|
13
|
+
Object.keys(n).forEach(k => {
|
|
14
|
+
if (n === 'user') {
|
|
15
|
+
document.cookie[n + '_' + k] = '';
|
|
16
|
+
}
|
|
17
|
+
window.localStorage.removeItem(n + '_' + k);
|
|
18
|
+
});
|
|
19
|
+
delete namespaces[n];
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
return namespaces;
|
|
23
|
+
}
|
|
24
|
+
function filterNamespace(namespace) {
|
|
25
|
+
const ans = {};
|
|
26
|
+
Object.keys(namespace).forEach(k => {
|
|
27
|
+
if (k !== 'expires') {
|
|
28
|
+
ans[k] = namespace[k];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return ans;
|
|
32
|
+
}
|
|
33
|
+
function readCookie() {
|
|
34
|
+
const cookies = {};
|
|
35
|
+
document.cookie.split(';').forEach(i => {
|
|
36
|
+
const [key, value] = i.split('=');
|
|
37
|
+
cookies[key.trim()] = value;
|
|
38
|
+
});
|
|
39
|
+
const namespaces = {};
|
|
40
|
+
Object.keys(cookies).forEach(k => {
|
|
41
|
+
const result = k.split('_');
|
|
42
|
+
if (result.length === 2) {
|
|
43
|
+
const [namespace, key] = result;
|
|
44
|
+
if (namespaces[namespace] === undefined) {
|
|
45
|
+
namespaces[namespace] = {};
|
|
46
|
+
}
|
|
47
|
+
namespaces[namespace][key] = cookies[k];
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
for (let i = 0; i < window.localStorage.length; i += 1) {
|
|
51
|
+
const k = window.localStorage.key(i);
|
|
52
|
+
const result = k.split('_');
|
|
53
|
+
if (result.length === 2) {
|
|
54
|
+
const [namespace, key] = result;
|
|
55
|
+
if (namespaces[namespace] === undefined) {
|
|
56
|
+
namespaces[namespace] = {};
|
|
57
|
+
}
|
|
58
|
+
namespaces[namespace][key] = window.localStorage.getItem(k);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return clearCookie(namespaces);
|
|
62
|
+
}
|
|
63
|
+
function setCookie(response) {
|
|
64
|
+
const cookie = response.headers.get('cookie');
|
|
65
|
+
if (cookie !== null) {
|
|
66
|
+
if (cookie === '') {
|
|
67
|
+
document.cookie.split(';').forEach(c => {
|
|
68
|
+
const [k, v] = c.split('=');
|
|
69
|
+
const [namespace] = k.split('_');
|
|
70
|
+
if (namespace === 'user') {
|
|
71
|
+
document.cookie = k + '=' + v;
|
|
72
|
+
}
|
|
73
|
+
window.localStorage.set(k, v);
|
|
74
|
+
});
|
|
75
|
+
} else {
|
|
76
|
+
cookie.split(';').forEach(c => {
|
|
77
|
+
document.cookie = c;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -1,4 +1,111 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.CommonHttp = void 0;
|
|
7
|
+
exports.default = getHtml;
|
|
8
|
+
exports.parseOption = parseOption;
|
|
9
|
+
exports.readCookie = readCookie;
|
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
|
11
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
12
|
+
var _handlebars = _interopRequireDefault(require("handlebars"));
|
|
13
|
+
var _htmlMinifier = require("html-minifier");
|
|
14
|
+
var _cacheOutput = _interopRequireDefault(require("./lib/cacheOutput"));
|
|
15
|
+
var _compressOutput = _interopRequireDefault(require("./lib/compressOutput"));
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
function isOption(string) {
|
|
18
|
+
let ans = true;
|
|
19
|
+
if (typeof string === 'string') {
|
|
20
|
+
let i = 0;
|
|
21
|
+
for (i = 0; i < 1; i += 1) {
|
|
22
|
+
if (string.charAt(i) === '-') {
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (i === 0) {
|
|
27
|
+
ans = false;
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
ans = false;
|
|
31
|
+
}
|
|
32
|
+
return ans;
|
|
33
|
+
}
|
|
34
|
+
function transformOption(value) {
|
|
35
|
+
let ans;
|
|
36
|
+
const s = value.split('-');
|
|
37
|
+
if (s.length >= 1) {
|
|
38
|
+
ans = s.map((e, i) => {
|
|
39
|
+
if (i === 0) {
|
|
40
|
+
return e;
|
|
41
|
+
} else {
|
|
42
|
+
return e.charAt(0).toUpperCase() + e.substring(1, e.length);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
} else {
|
|
46
|
+
ans = value;
|
|
47
|
+
}
|
|
48
|
+
return ans.join('');
|
|
49
|
+
}
|
|
50
|
+
function parseOption(...params) {
|
|
51
|
+
const ans = {};
|
|
52
|
+
for (let i = 0; i < params.length; i += 1) {
|
|
53
|
+
const param = params[i];
|
|
54
|
+
if (param.charAt(0) === '-') {
|
|
55
|
+
const regexp = /^\-([a-z])$/;
|
|
56
|
+
if (regexp.test(param)) {
|
|
57
|
+
const [_, k] = param.match(regexp);
|
|
58
|
+
if (isOption(params[i + 1])) {
|
|
59
|
+
ans[k] = params[i + 1];
|
|
60
|
+
i += 1;
|
|
61
|
+
} else {
|
|
62
|
+
ans[k] = true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (param.charAt(1) === '-') {
|
|
66
|
+
const regexp = /^\-\-([a-z\-]+)$/;
|
|
67
|
+
if (regexp.test(param)) {
|
|
68
|
+
const [_, k] = param.match(regexp);
|
|
69
|
+
if (isOption(params[i + 1])) {
|
|
70
|
+
ans[transformOption(k)] = params[i + 1];
|
|
71
|
+
i += 1;
|
|
72
|
+
} else {
|
|
73
|
+
ans[k] = true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return ans;
|
|
80
|
+
}
|
|
81
|
+
function readCookie(cookie) {
|
|
82
|
+
const cookies = {};
|
|
83
|
+
if (typeof cookie === 'string') {
|
|
84
|
+
cookie.split(';').forEach(i => {
|
|
85
|
+
const [key, value] = i.split('=');
|
|
86
|
+
cookies[key.trim()] = value;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
const namespaces = {};
|
|
90
|
+
Object.keys(cookies).forEach(k => {
|
|
91
|
+
const result = k.split('_');
|
|
92
|
+
if (result.length === 2) {
|
|
93
|
+
const [namespace, key] = result;
|
|
94
|
+
if (namespaces[namespace] === undefined) {
|
|
95
|
+
namespaces[namespace] = {};
|
|
96
|
+
}
|
|
97
|
+
namespaces[namespace][key] = cookies[k];
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
return namespaces;
|
|
101
|
+
}
|
|
102
|
+
function formateHttpKey(key) {
|
|
103
|
+
return key.split('-').map(v => {
|
|
104
|
+
return v.substring(0, 1).toUpperCase() + v.substring(1, v.length);
|
|
105
|
+
}).join('-');
|
|
106
|
+
}
|
|
107
|
+
function getHtml(title, content) {
|
|
108
|
+
const template = _handlebars.default.compile(`
|
|
2
109
|
<!doctype html>
|
|
3
110
|
<html lang="en">
|
|
4
111
|
<head>
|
|
@@ -12,4 +119,101 @@
|
|
|
12
119
|
<script src="main.bundle.js"></script>
|
|
13
120
|
</body>
|
|
14
121
|
</html>
|
|
15
|
-
`);
|
|
122
|
+
`);
|
|
123
|
+
return (0, _htmlMinifier.minify)(template({
|
|
124
|
+
title,
|
|
125
|
+
content
|
|
126
|
+
}), {
|
|
127
|
+
collapseWhitespace: true
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function getLists(list) {
|
|
131
|
+
return list.join('|');
|
|
132
|
+
}
|
|
133
|
+
const time = new Date().getTime();
|
|
134
|
+
class CommonHttp {
|
|
135
|
+
constructor(options) {
|
|
136
|
+
this.time = new Date().getTime();
|
|
137
|
+
const {
|
|
138
|
+
fonts
|
|
139
|
+
} = options;
|
|
140
|
+
if (options.fonts === undefined) {
|
|
141
|
+
options.fonts = [];
|
|
142
|
+
}
|
|
143
|
+
this.modify = {};
|
|
144
|
+
this.file = {};
|
|
145
|
+
this.raw = {};
|
|
146
|
+
this.compress = {};
|
|
147
|
+
this.options = options;
|
|
148
|
+
this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['html, ico', 'js']))})$`);
|
|
149
|
+
this.cacheOutput = _cacheOutput.default.bind(this);
|
|
150
|
+
this.compressOutput = _compressOutput.default.bind(this);
|
|
151
|
+
}
|
|
152
|
+
dealCompress(data, path, res) {
|
|
153
|
+
if (this.compress[path] === undefined) {
|
|
154
|
+
this.compress[path] = data;
|
|
155
|
+
}
|
|
156
|
+
res.end(this.compress[path]);
|
|
157
|
+
}
|
|
158
|
+
directDeal(data, path, res) {
|
|
159
|
+
if (this.raw[path] === undefined) {
|
|
160
|
+
this.compress[path] = data;
|
|
161
|
+
}
|
|
162
|
+
res.end(this.raw[path]);
|
|
163
|
+
}
|
|
164
|
+
async process(req, res) {
|
|
165
|
+
try {
|
|
166
|
+
const {
|
|
167
|
+
url
|
|
168
|
+
} = req;
|
|
169
|
+
if (url === '/update/time') {
|
|
170
|
+
const {
|
|
171
|
+
time
|
|
172
|
+
} = this;
|
|
173
|
+
res.end(time);
|
|
174
|
+
} else if (this.regexp.test(url)) {
|
|
175
|
+
this.cacheOutput(req, res, restPath, _fs.default.readFileSync(_path.default.resolve('static', restPath)), parseInt(_fs.default.statSync(_path.default.resolve('static', restPath)).mtimeMs));
|
|
176
|
+
} else if (url.substring(0, 4) === '/api') {
|
|
177
|
+
const body = await new Promise((resolve, reject) => {
|
|
178
|
+
req.on('data', data => {
|
|
179
|
+
resolve(data.toString());
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
const {
|
|
183
|
+
location
|
|
184
|
+
} = this.options;
|
|
185
|
+
if (location !== undefined) {
|
|
186
|
+
const response = await fetch(location + url, {
|
|
187
|
+
method: 'POST',
|
|
188
|
+
body
|
|
189
|
+
});
|
|
190
|
+
for (const k of response.headers.keys()) {
|
|
191
|
+
res.setHeader(formateHttpKey(k), response.headers.get(k));
|
|
192
|
+
}
|
|
193
|
+
const data = await response.text();
|
|
194
|
+
res.end(JSON.stringify(data));
|
|
195
|
+
}
|
|
196
|
+
} else {
|
|
197
|
+
if (this.html === undefined) {
|
|
198
|
+
const {
|
|
199
|
+
title,
|
|
200
|
+
content
|
|
201
|
+
} = this.options;
|
|
202
|
+
this.html = getHtml(title, content);
|
|
203
|
+
}
|
|
204
|
+
this.cacheOutput(req, res, '*html', this.html, time);
|
|
205
|
+
}
|
|
206
|
+
} catch (e) {
|
|
207
|
+
const {
|
|
208
|
+
develope
|
|
209
|
+
} = this.options;
|
|
210
|
+
if (develope === true) {
|
|
211
|
+
throw e;
|
|
212
|
+
} else {
|
|
213
|
+
res.writeHead(500);
|
|
214
|
+
res.end();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
exports.CommonHttp = CommonHttp;
|