manner.js 0.0.19 → 0.0.21
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 +1 -81
- package/package.json +2 -1
- package/server.js +1 -177
- package/.nvmrc +0 -1
- package/babel.config.json +0 -26
- package/dist/client/index.js +0 -81
- package/dist/scripts/index.js +0 -9
- package/dist/server/index.js +0 -177
- package/gulpfile.babel.js +0 -10
- package/src/client/index.js +0 -75
- package/src/scripts/index.js +0 -8
- package/src/server/index.js +0 -170
- package/test/CommonHttp.test.js +0 -18
- package/test/parseOption.test.js +0 -9
- package/yarn.lock +0 -7293
package/client.js
CHANGED
|
@@ -1,81 +1 @@
|
|
|
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
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.clearCookie=clearCookie,exports.filterNamespace=filterNamespace,exports.readCookie=readCookie,exports.setCookie=setCookie;function clearCookie(a){return Object.keys(a).forEach(b=>{void 0!==b.expires&&(Object.keys(b).forEach(a=>{"user"===b&&(document.cookie[b+"_"+a]=""),window.localStorage.removeItem(b+"_"+a)}),delete a[b])}),a}function filterNamespace(a){const b={};return Object.keys(a).forEach(c=>{"expires"!==c&&(b[c]=a[c])}),b}function readCookie(){const a={};document.cookie.split(";").forEach(b=>{const[c,d]=b.split("=");a[c.trim()]=d});const b={};Object.keys(a).forEach(c=>{const d=c.split("_");if(2===d.length){const[e,f]=d;b[e]===void 0&&(b[e]={}),b[e][f]=a[c]}});for(let a=0;a<window.localStorage.length;a+=1){const c=window.localStorage.key(a),d=c.split("_");if(2===d.length){const[a,e]=d;b[a]===void 0&&(b[a]={}),b[a][e]=window.localStorage.getItem(c)}}return clearCookie(b)}function setCookie(a){const b=a.headers.get("cookie");null!==b&&(""===b?document.cookie.split(";").forEach(a=>{const[b,c]=a.split("="),[d]=b.split("_");"user"===d&&(document.cookie=b+"="+c),window.localStorage.set(b,c)}):b.split(";").forEach(a=>{document.cookie=a}))}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "manner.js",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.21",
|
|
4
4
|
"description": "Frontend mode project.",
|
|
5
5
|
"repository": "git@github.com:leobrad/mode.git",
|
|
6
6
|
"author": "Leo Ely",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"autoprefixer": "^10.4.13",
|
|
18
18
|
"babel-loader": "^9.1.2",
|
|
19
19
|
"babel-plugin-root-import": "^6.6.0",
|
|
20
|
+
"babel-preset-minify": "^0.5.2",
|
|
20
21
|
"css-loader": "^6.7.3",
|
|
21
22
|
"file-loader": "^6.2.0",
|
|
22
23
|
"gulp-babel": "^8.0.0",
|
package/server.js
CHANGED
|
@@ -1,177 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.CommonHttp = void 0;
|
|
7
|
-
exports.default = readCookie;
|
|
8
|
-
exports.filterNamespace = filterNamespace;
|
|
9
|
-
exports.formateHttpDate = formateHttpDate;
|
|
10
|
-
exports.formateHttpKey = formateHttpKey;
|
|
11
|
-
exports.parseOption = parseOption;
|
|
12
|
-
function isOption(string) {
|
|
13
|
-
let ans = true;
|
|
14
|
-
if (typeof string === 'string') {
|
|
15
|
-
let i = 0;
|
|
16
|
-
for (i = 0; i < 1; i += 1) {
|
|
17
|
-
if (string.charAt(i) === '-') {
|
|
18
|
-
break;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
if (i === 0) {
|
|
22
|
-
ans = false;
|
|
23
|
-
}
|
|
24
|
-
} else {
|
|
25
|
-
ans = false;
|
|
26
|
-
}
|
|
27
|
-
return ans;
|
|
28
|
-
}
|
|
29
|
-
function transformOption(value) {
|
|
30
|
-
let ans;
|
|
31
|
-
const s = value.split('-');
|
|
32
|
-
if (s.length >= 1) {
|
|
33
|
-
ans = s.map((e, i) => {
|
|
34
|
-
if (i === 0) {
|
|
35
|
-
return e;
|
|
36
|
-
} else {
|
|
37
|
-
return e.charAt(0).toUpperCase() + e.substring(1, e.length);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
} else {
|
|
41
|
-
ans = value;
|
|
42
|
-
}
|
|
43
|
-
return ans.join('');
|
|
44
|
-
}
|
|
45
|
-
function parseOption(...params) {
|
|
46
|
-
const ans = {};
|
|
47
|
-
for (let i = 0; i < params.length; i += 1) {
|
|
48
|
-
const param = params[i];
|
|
49
|
-
if (param.charAt(0) === '-') {
|
|
50
|
-
const regexp = /^\-([a-z])$/;
|
|
51
|
-
if (regexp.test(param)) {
|
|
52
|
-
const [_, k] = param.match(regexp);
|
|
53
|
-
if (isOption(params[i + 1])) {
|
|
54
|
-
ans[k] = params[i + 1];
|
|
55
|
-
i += 1;
|
|
56
|
-
} else {
|
|
57
|
-
ans[k] = true;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (param.charAt(1) === '-') {
|
|
61
|
-
const regexp = /^\-\-([a-z\-]+)$/;
|
|
62
|
-
if (regexp.test(param)) {
|
|
63
|
-
const [_, k] = param.match(regexp);
|
|
64
|
-
if (isOption(params[i + 1])) {
|
|
65
|
-
ans[transformOption(k)] = params[i + 1];
|
|
66
|
-
i += 1;
|
|
67
|
-
} else {
|
|
68
|
-
ans[k] = true;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return ans;
|
|
75
|
-
}
|
|
76
|
-
function filterNamespace(namespace) {
|
|
77
|
-
const ans = {};
|
|
78
|
-
if (typeof namespace === 'object') {
|
|
79
|
-
Object.keys(namespace).forEach(k => {
|
|
80
|
-
if (k !== 'expires') {
|
|
81
|
-
ans[k] = namespace[k];
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
return ans;
|
|
86
|
-
}
|
|
87
|
-
function readCookie(cookie) {
|
|
88
|
-
const cookies = {};
|
|
89
|
-
if (typeof cookie === 'string') {
|
|
90
|
-
cookie.split(';').forEach(i => {
|
|
91
|
-
const [key, value] = i.split('=');
|
|
92
|
-
cookies[key.trim()] = value;
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
const namespaces = {};
|
|
96
|
-
Object.keys(cookies).forEach(k => {
|
|
97
|
-
const result = k.split('_');
|
|
98
|
-
if (result.length === 2) {
|
|
99
|
-
const [namespace, key] = result;
|
|
100
|
-
if (namespaces[namespace] === undefined) {
|
|
101
|
-
namespaces[namespace] = {};
|
|
102
|
-
}
|
|
103
|
-
namespaces[namespace][key] = cookies[k];
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
return namespaces;
|
|
107
|
-
}
|
|
108
|
-
function formateHttpDate(date) {
|
|
109
|
-
let [week, month, day, year, time, zone] = date.toString().split(' ');
|
|
110
|
-
zone = zone.split('+')[0];
|
|
111
|
-
return month + ', ' + day + ' ' + month + ' ' + year + ' ' + time + ' ' + zone;
|
|
112
|
-
}
|
|
113
|
-
function formateHttpKey(key) {
|
|
114
|
-
return key.split('-').map(v => {
|
|
115
|
-
return v.substring(0, 1).toUpperCase() + v.substring(1, v.length);
|
|
116
|
-
}).join('-');
|
|
117
|
-
}
|
|
118
|
-
function getLists(list) {
|
|
119
|
-
return list.join('|');
|
|
120
|
-
}
|
|
121
|
-
class CommonHttp {
|
|
122
|
-
constructor(options) {
|
|
123
|
-
this.time = new Date().getTime();
|
|
124
|
-
const {
|
|
125
|
-
fonts
|
|
126
|
-
} = options;
|
|
127
|
-
if (options.fonts === undefined) {
|
|
128
|
-
options.fonts = [];
|
|
129
|
-
}
|
|
130
|
-
this.options = options;
|
|
131
|
-
this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['html, ico', 'js']))})$`);
|
|
132
|
-
}
|
|
133
|
-
async process(req, res) {
|
|
134
|
-
try {
|
|
135
|
-
const {
|
|
136
|
-
url
|
|
137
|
-
} = req;
|
|
138
|
-
if (url === '/update/time') {
|
|
139
|
-
const {
|
|
140
|
-
time
|
|
141
|
-
} = this;
|
|
142
|
-
res.end(time);
|
|
143
|
-
} else if (this.regexp.test(url)) {
|
|
144
|
-
cacheOutput(req, res, restPath, fs.readFileSync(path.resolve('static', restPath)), parseInt(fs.statSync(path.resolve('static', restPath)).mtimeMs));
|
|
145
|
-
} else if (url.substring(0, 4) === '/api') {
|
|
146
|
-
const body = await new Promise((resolve, reject) => {
|
|
147
|
-
req.on('data', data => {
|
|
148
|
-
resolve(data.toString());
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
const {
|
|
152
|
-
location
|
|
153
|
-
} = this.options;
|
|
154
|
-
const response = await fetch(location + url, {
|
|
155
|
-
method: 'POST',
|
|
156
|
-
body
|
|
157
|
-
});
|
|
158
|
-
for (const k of response.headers.keys()) {
|
|
159
|
-
res.setHeader(formateHttpKey(k), response.headers.get(k));
|
|
160
|
-
}
|
|
161
|
-
const data = await response.text();
|
|
162
|
-
res.end(JSON.stringify(data));
|
|
163
|
-
}
|
|
164
|
-
} catch (e) {
|
|
165
|
-
const {
|
|
166
|
-
develope
|
|
167
|
-
} = this.options;
|
|
168
|
-
if (develope === true) {
|
|
169
|
-
throw e;
|
|
170
|
-
} else {
|
|
171
|
-
res.writeHead(500);
|
|
172
|
-
res.end();
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
exports.CommonHttp = CommonHttp;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CommonHttp=void 0,exports.default=readCookie,exports.parseOption=parseOption;function isOption(a){let b=!0;if("string"==typeof a){let c=0;for(c=0;1>c&&!("-"===a.charAt(c));c+=1);0==c&&(b=!1)}else b=!1;return b}function transformOption(a){let b;const c=a.split("-");return b=1<=c.length?c.map((a,b)=>0===b?a:a.charAt(0).toUpperCase()+a.substring(1,a.length)):a,b.join("")}function parseOption(...a){const b={};for(let c=0;c<a.length;c+=1){const d=a[c];if("-"===d.charAt(0)){const e=/^\-([a-z])$/;if(e.test(d)){const[f,g]=d.match(e);isOption(a[c+1])?(b[g]=a[c+1],c+=1):b[g]=!0}if("-"===d.charAt(1)){const e=/^\-\-([a-z\-]+)$/;if(e.test(d)){const[f,g]=d.match(e);isOption(a[c+1])?(b[transformOption(g)]=a[c+1],c+=1):b[g]=!0}}}}return b}function readCookie(a){const b={};"string"==typeof a&&a.split(";").forEach(a=>{const[c,d]=a.split("=");b[c.trim()]=d});const c={};return Object.keys(b).forEach(a=>{const d=a.split("_");if(2===d.length){const[e,f]=d;void 0===c[e]&&(c[e]={}),c[e][f]=b[a]}}),c}function formateHttpKey(a){return a.split("-").map(a=>a.substring(0,1).toUpperCase()+a.substring(1,a.length)).join("-")}function getLists(a){return a.join("|")}class CommonHttp{constructor(a){this.time=new Date().getTime();const{fonts:b}=a;a.fonts===void 0&&(a.fonts=[]),this.options=a,this.regexp=new RegExp(`\.(${getLists(a.fonts.concat(["html, ico","js"]))})$`)}async process(a,b){try{const{url:c}=a;if("/update/time"===c){const{time:a}=this;b.end(a)}else if(this.regexp.test(c))cacheOutput(a,b,restPath,fs.readFileSync(path.resolve("static",restPath)),parseInt(fs.statSync(path.resolve("static",restPath)).mtimeMs));else if("/api"===c.substring(0,4)){const d=await new Promise(b=>{a.on("data",a=>{b(a.toString())})}),{location:e}=this.options;if(e!==void 0){const a=await fetch(e+c,{method:"POST",body:d});for(const c of a.headers.keys())b.setHeader(formateHttpKey(c),a.headers.get(c));const f=await a.text();b.end(JSON.stringify(f))}}}catch(a){const{develope:c}=this.options;if(!0===c)throw a;else b.writeHead(500),b.end()}}}exports.CommonHttp=CommonHttp;
|
package/.nvmrc
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
v21.6.2
|
package/babel.config.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"presets": [
|
|
3
|
-
[
|
|
4
|
-
"@babel/preset-env",
|
|
5
|
-
{
|
|
6
|
-
"targets": {
|
|
7
|
-
"node": "current"
|
|
8
|
-
}
|
|
9
|
-
},
|
|
10
|
-
"@babel/preset-react"
|
|
11
|
-
],
|
|
12
|
-
],
|
|
13
|
-
"plugins": [
|
|
14
|
-
[
|
|
15
|
-
"babel-plugin-root-import",
|
|
16
|
-
{
|
|
17
|
-
"paths": [
|
|
18
|
-
{
|
|
19
|
-
"rootPathSuffix": "./src",
|
|
20
|
-
"rootPathPrefix": "~/"
|
|
21
|
-
},
|
|
22
|
-
]
|
|
23
|
-
}
|
|
24
|
-
]
|
|
25
|
-
],
|
|
26
|
-
}
|
package/dist/client/index.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
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/dist/scripts/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _fs = require("fs");
|
|
4
|
-
var _path = _interopRequireDefault(require("path"));
|
|
5
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
6
|
-
const currentPath = _path.default.resolve('.');
|
|
7
|
-
const distPath = _path.default.join(currentPath, 'dist');
|
|
8
|
-
(0, _fs.copyFileSync)(_path.default.join(distPath, 'client', 'index.js'), _path.default.join(currentPath, 'client.js'));
|
|
9
|
-
(0, _fs.copyFileSync)(_path.default.join(distPath, 'server', 'index.js'), _path.default.join(currentPath, 'server.js'));
|
package/dist/server/index.js
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.CommonHttp = void 0;
|
|
7
|
-
exports.default = readCookie;
|
|
8
|
-
exports.filterNamespace = filterNamespace;
|
|
9
|
-
exports.formateHttpDate = formateHttpDate;
|
|
10
|
-
exports.formateHttpKey = formateHttpKey;
|
|
11
|
-
exports.parseOption = parseOption;
|
|
12
|
-
function isOption(string) {
|
|
13
|
-
let ans = true;
|
|
14
|
-
if (typeof string === 'string') {
|
|
15
|
-
let i = 0;
|
|
16
|
-
for (i = 0; i < 1; i += 1) {
|
|
17
|
-
if (string.charAt(i) === '-') {
|
|
18
|
-
break;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
if (i === 0) {
|
|
22
|
-
ans = false;
|
|
23
|
-
}
|
|
24
|
-
} else {
|
|
25
|
-
ans = false;
|
|
26
|
-
}
|
|
27
|
-
return ans;
|
|
28
|
-
}
|
|
29
|
-
function transformOption(value) {
|
|
30
|
-
let ans;
|
|
31
|
-
const s = value.split('-');
|
|
32
|
-
if (s.length >= 1) {
|
|
33
|
-
ans = s.map((e, i) => {
|
|
34
|
-
if (i === 0) {
|
|
35
|
-
return e;
|
|
36
|
-
} else {
|
|
37
|
-
return e.charAt(0).toUpperCase() + e.substring(1, e.length);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
} else {
|
|
41
|
-
ans = value;
|
|
42
|
-
}
|
|
43
|
-
return ans.join('');
|
|
44
|
-
}
|
|
45
|
-
function parseOption(...params) {
|
|
46
|
-
const ans = {};
|
|
47
|
-
for (let i = 0; i < params.length; i += 1) {
|
|
48
|
-
const param = params[i];
|
|
49
|
-
if (param.charAt(0) === '-') {
|
|
50
|
-
const regexp = /^\-([a-z])$/;
|
|
51
|
-
if (regexp.test(param)) {
|
|
52
|
-
const [_, k] = param.match(regexp);
|
|
53
|
-
if (isOption(params[i + 1])) {
|
|
54
|
-
ans[k] = params[i + 1];
|
|
55
|
-
i += 1;
|
|
56
|
-
} else {
|
|
57
|
-
ans[k] = true;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (param.charAt(1) === '-') {
|
|
61
|
-
const regexp = /^\-\-([a-z\-]+)$/;
|
|
62
|
-
if (regexp.test(param)) {
|
|
63
|
-
const [_, k] = param.match(regexp);
|
|
64
|
-
if (isOption(params[i + 1])) {
|
|
65
|
-
ans[transformOption(k)] = params[i + 1];
|
|
66
|
-
i += 1;
|
|
67
|
-
} else {
|
|
68
|
-
ans[k] = true;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return ans;
|
|
75
|
-
}
|
|
76
|
-
function filterNamespace(namespace) {
|
|
77
|
-
const ans = {};
|
|
78
|
-
if (typeof namespace === 'object') {
|
|
79
|
-
Object.keys(namespace).forEach(k => {
|
|
80
|
-
if (k !== 'expires') {
|
|
81
|
-
ans[k] = namespace[k];
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
return ans;
|
|
86
|
-
}
|
|
87
|
-
function readCookie(cookie) {
|
|
88
|
-
const cookies = {};
|
|
89
|
-
if (typeof cookie === 'string') {
|
|
90
|
-
cookie.split(';').forEach(i => {
|
|
91
|
-
const [key, value] = i.split('=');
|
|
92
|
-
cookies[key.trim()] = value;
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
const namespaces = {};
|
|
96
|
-
Object.keys(cookies).forEach(k => {
|
|
97
|
-
const result = k.split('_');
|
|
98
|
-
if (result.length === 2) {
|
|
99
|
-
const [namespace, key] = result;
|
|
100
|
-
if (namespaces[namespace] === undefined) {
|
|
101
|
-
namespaces[namespace] = {};
|
|
102
|
-
}
|
|
103
|
-
namespaces[namespace][key] = cookies[k];
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
return namespaces;
|
|
107
|
-
}
|
|
108
|
-
function formateHttpDate(date) {
|
|
109
|
-
let [week, month, day, year, time, zone] = date.toString().split(' ');
|
|
110
|
-
zone = zone.split('+')[0];
|
|
111
|
-
return month + ', ' + day + ' ' + month + ' ' + year + ' ' + time + ' ' + zone;
|
|
112
|
-
}
|
|
113
|
-
function formateHttpKey(key) {
|
|
114
|
-
return key.split('-').map(v => {
|
|
115
|
-
return v.substring(0, 1).toUpperCase() + v.substring(1, v.length);
|
|
116
|
-
}).join('-');
|
|
117
|
-
}
|
|
118
|
-
function getLists(list) {
|
|
119
|
-
return list.join('|');
|
|
120
|
-
}
|
|
121
|
-
class CommonHttp {
|
|
122
|
-
constructor(options) {
|
|
123
|
-
this.time = new Date().getTime();
|
|
124
|
-
const {
|
|
125
|
-
fonts
|
|
126
|
-
} = options;
|
|
127
|
-
if (options.fonts === undefined) {
|
|
128
|
-
options.fonts = [];
|
|
129
|
-
}
|
|
130
|
-
this.options = options;
|
|
131
|
-
this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['html, ico', 'js']))})$`);
|
|
132
|
-
}
|
|
133
|
-
async process(req, res) {
|
|
134
|
-
try {
|
|
135
|
-
const {
|
|
136
|
-
url
|
|
137
|
-
} = req;
|
|
138
|
-
if (url === '/update/time') {
|
|
139
|
-
const {
|
|
140
|
-
time
|
|
141
|
-
} = this;
|
|
142
|
-
res.end(time);
|
|
143
|
-
} else if (this.regexp.test(url)) {
|
|
144
|
-
cacheOutput(req, res, restPath, fs.readFileSync(path.resolve('static', restPath)), parseInt(fs.statSync(path.resolve('static', restPath)).mtimeMs));
|
|
145
|
-
} else if (url.substring(0, 4) === '/api') {
|
|
146
|
-
const body = await new Promise((resolve, reject) => {
|
|
147
|
-
req.on('data', data => {
|
|
148
|
-
resolve(data.toString());
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
const {
|
|
152
|
-
location
|
|
153
|
-
} = this.options;
|
|
154
|
-
const response = await fetch(location + url, {
|
|
155
|
-
method: 'POST',
|
|
156
|
-
body
|
|
157
|
-
});
|
|
158
|
-
for (const k of response.headers.keys()) {
|
|
159
|
-
res.setHeader(formateHttpKey(k), response.headers.get(k));
|
|
160
|
-
}
|
|
161
|
-
const data = await response.text();
|
|
162
|
-
res.end(JSON.stringify(data));
|
|
163
|
-
}
|
|
164
|
-
} catch (e) {
|
|
165
|
-
const {
|
|
166
|
-
develope
|
|
167
|
-
} = this.options;
|
|
168
|
-
if (develope === true) {
|
|
169
|
-
throw e;
|
|
170
|
-
} else {
|
|
171
|
-
res.writeHead(500);
|
|
172
|
-
res.end();
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
exports.CommonHttp = CommonHttp;
|
package/gulpfile.babel.js
DELETED
package/src/client/index.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
export function clearCookie(namespaces) {
|
|
2
|
-
Object.keys(namespaces).forEach((n) => {
|
|
3
|
-
if (n.expires !== undefined) {
|
|
4
|
-
Object.keys(n).forEach((k) => {
|
|
5
|
-
if (n === 'user') {
|
|
6
|
-
document.cookie[n + '_' + k] = '';
|
|
7
|
-
}
|
|
8
|
-
window.localStorage.removeItem(n + '_' + k);
|
|
9
|
-
});
|
|
10
|
-
delete namespaces[n];
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
return namespaces;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function filterNamespace(namespace) {
|
|
17
|
-
const ans = {};
|
|
18
|
-
Object.keys(namespace).forEach((k) => {
|
|
19
|
-
if (k !== 'expires') {
|
|
20
|
-
ans[k] = namespace[k];
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
return ans;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function readCookie() {
|
|
27
|
-
const cookies = {};
|
|
28
|
-
document.cookie.split(';').forEach((i) => {
|
|
29
|
-
const [key, value] = i.split('=');
|
|
30
|
-
cookies[key.trim()] = value;
|
|
31
|
-
});
|
|
32
|
-
const namespaces = {};
|
|
33
|
-
Object.keys(cookies).forEach((k) => {
|
|
34
|
-
const result = k.split('_');
|
|
35
|
-
if (result.length === 2) {
|
|
36
|
-
const [namespace, key] = result;
|
|
37
|
-
if (namespaces[namespace] === undefined) {
|
|
38
|
-
namespaces[namespace] = {};
|
|
39
|
-
}
|
|
40
|
-
namespaces[namespace][key] = cookies[k];
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
for (let i = 0; i < window.localStorage.length; i += 1) {
|
|
44
|
-
const k = window.localStorage.key(i);
|
|
45
|
-
const result = k.split('_');
|
|
46
|
-
if (result.length === 2) {
|
|
47
|
-
const [namespace, key] = result;
|
|
48
|
-
if (namespaces[namespace] === undefined) {
|
|
49
|
-
namespaces[namespace] = {};
|
|
50
|
-
}
|
|
51
|
-
namespaces[namespace][key] = window.localStorage.getItem(k);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return clearCookie(namespaces);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function setCookie(response) {
|
|
58
|
-
const cookie = response.headers.get('cookie');
|
|
59
|
-
if (cookie !== null) {
|
|
60
|
-
if (cookie === '') {
|
|
61
|
-
document.cookie.split(';').forEach((c) => {
|
|
62
|
-
const [k, v] = c.split('=');
|
|
63
|
-
const [namespace] = k.split('_');
|
|
64
|
-
if (namespace === 'user') {
|
|
65
|
-
document.cookie = k + '=' + v;
|
|
66
|
-
}
|
|
67
|
-
window.localStorage.set(k, v);
|
|
68
|
-
});
|
|
69
|
-
} else {
|
|
70
|
-
cookie.split(';').forEach((c) => {
|
|
71
|
-
document.cookie=c;
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
package/src/scripts/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { copyFileSync, } from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
|
|
4
|
-
const currentPath = path.resolve('.');
|
|
5
|
-
const distPath = path.join(currentPath, 'dist');
|
|
6
|
-
|
|
7
|
-
copyFileSync(path.join(distPath, 'client', 'index.js'), path.join(currentPath, 'client.js'));
|
|
8
|
-
copyFileSync(path.join(distPath, 'server', 'index.js'), path.join(currentPath, 'server.js'));
|