manner.js 0.0.30 → 0.0.32

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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/client.js +0 -81
  3. package/server.js +0 -272
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "manner.js",
3
- "version": "0.0.30",
3
+ "version": "0.0.32",
4
4
  "description": "Frontend mode project.",
5
5
  "repository": "git@github.com:leobrad/mode.git",
6
6
  "author": "Leo Ely",
package/client.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/server.js DELETED
@@ -1,272 +0,0 @@
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 _zlib = _interopRequireDefault(require("zlib"));
13
- var _stream = require("stream");
14
- var _handlebars = _interopRequireDefault(require("handlebars"));
15
- var _htmlMinifier = require("html-minifier");
16
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
- function onError(err) {
18
- if (err) {
19
- console.error('An error occurred:', err);
20
- process.exitCode = 1;
21
- }
22
- }
23
- function parseDateString(dateString) {
24
- return new Date(httpDate).getTime();
25
- }
26
- function formateHttpDate(date) {
27
- let [week, month, day, year, time, zone] = date.toString().split(' ');
28
- zone = zone.split('+')[0];
29
- return month + ', ' + day + ' ' + month + ' ' + year + ' ' + time + ' ' + zone;
30
- }
31
- function isOption(string) {
32
- let ans = true;
33
- if (typeof string === 'string') {
34
- let i = 0;
35
- for (i = 0; i < 1; i += 1) {
36
- if (string.charAt(i) === '-') {
37
- break;
38
- }
39
- }
40
- if (i === 0) {
41
- ans = false;
42
- }
43
- } else {
44
- ans = false;
45
- }
46
- return ans;
47
- }
48
- function transformOption(value) {
49
- let ans;
50
- const s = value.split('-');
51
- if (s.length >= 1) {
52
- ans = s.map((e, i) => {
53
- if (i === 0) {
54
- return e;
55
- } else {
56
- return e.charAt(0).toUpperCase() + e.substring(1, e.length);
57
- }
58
- });
59
- } else {
60
- ans = value;
61
- }
62
- return ans.join('');
63
- }
64
- function parseOption(...params) {
65
- const ans = {};
66
- for (let i = 0; i < params.length; i += 1) {
67
- const param = params[i];
68
- if (param.charAt(0) === '-') {
69
- const regexp = /^\-([a-z])$/;
70
- if (regexp.test(param)) {
71
- const [_, k] = param.match(regexp);
72
- if (isOption(params[i + 1])) {
73
- ans[k] = params[i + 1];
74
- i += 1;
75
- } else {
76
- ans[k] = true;
77
- }
78
- }
79
- if (param.charAt(1) === '-') {
80
- const regexp = /^\-\-([a-z\-]+)$/;
81
- if (regexp.test(param)) {
82
- const [_, k] = param.match(regexp);
83
- if (isOption(params[i + 1])) {
84
- ans[transformOption(k)] = params[i + 1];
85
- i += 1;
86
- } else {
87
- ans[k] = true;
88
- }
89
- }
90
- }
91
- }
92
- }
93
- return ans;
94
- }
95
- function readCookie(cookie) {
96
- const cookies = {};
97
- if (typeof cookie === 'string') {
98
- cookie.split(';').forEach(i => {
99
- const [key, value] = i.split('=');
100
- cookies[key.trim()] = value;
101
- });
102
- }
103
- const namespaces = {};
104
- Object.keys(cookies).forEach(k => {
105
- const result = k.split('_');
106
- if (result.length === 2) {
107
- const [namespace, key] = result;
108
- if (namespaces[namespace] === undefined) {
109
- namespaces[namespace] = {};
110
- }
111
- namespaces[namespace][key] = cookies[k];
112
- }
113
- });
114
- return namespaces;
115
- }
116
- function formateHttpKey(key) {
117
- return key.split('-').map(v => {
118
- return v.substring(0, 1).toUpperCase() + v.substring(1, v.length);
119
- }).join('-');
120
- }
121
- function getHtml(title, content) {
122
- const template = _handlebars.default.compile(`
123
- <!doctype html>
124
- <html lang="en">
125
- <head>
126
- <meta charset="utf-8" />
127
- <title>{{title}}</title>
128
- <meta name="description" content="{{content}}" />
129
- <meta name="viewport" content="width=device-width, initial-scale=1" />
130
- </head>
131
- <body>
132
- <main id="root"></main>
133
- <script src="main.bundle.js"></script>
134
- </body>
135
- </html>
136
- `);
137
- return (0, _htmlMinifier.minify)(template({
138
- title,
139
- content
140
- }), {
141
- collapseWhitespace: true
142
- });
143
- }
144
- function getLists(list) {
145
- return list.join('|');
146
- }
147
- const time = new Date().getTime();
148
- class CommonHttp {
149
- constructor(options) {
150
- this.time = new Date().getTime();
151
- const {
152
- fonts
153
- } = options;
154
- if (options.fonts === undefined) {
155
- options.fonts = [];
156
- }
157
- this.modify = {};
158
- this.file = {};
159
- this.raw = {};
160
- this.compress = {};
161
- this.options = options;
162
- this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['html, ico', 'js']))})$`);
163
- }
164
- compressOutput(req, res, buffer, path) {
165
- res.setHeader('Vary', 'Accept-Encoding');
166
- let acceptEncoding = req.headers['accept-encoding'];
167
- if (/gzip/.test(acceptEncoding)) {
168
- res.writeHead(200, {
169
- 'Content-Encoding': 'gzip'
170
- });
171
- this.dealCompress(_zlib.default.gzipSync(buffer), path, res);
172
- } else {
173
- res.writeHead(200, {});
174
- this.dealDirect(buffer, path, res);
175
- }
176
- }
177
- cacheOutput(req, res, path, file, ms) {
178
- let ifModifiedSince = req.headers['If-Modified-Since'];
179
- if (ifModifiedSince === undefined) {
180
- if (this.modify[path] === undefined) {
181
- this.modify[path] = new Date(ms).toString();
182
- }
183
- if (this.file[path] === undefined) {
184
- this.file[path] = file;
185
- }
186
- res.setHeader('Last-Modified', formateHttpDate(this.modify[path]));
187
- this.compressOutput(req, res, this.file[path], path);
188
- } else {
189
- if (this.modify[path] === undefined) {
190
- this.modify[path] = new Date(ms).toString();
191
- }
192
- if (parseDateString(new Date().toString()) < parseDateString(this.modify[path])) {
193
- if (this.file[path] === undefined) {
194
- this.file[path] = file;
195
- }
196
- res.setHeader('Last-Modified', formateHttpDate(this.modify[path]));
197
- this.compressOutput(req, res, this.file[path], path);
198
- } else {
199
- res.writeHead(304);
200
- res.end();
201
- }
202
- }
203
- }
204
- dealCompress(data, path, res) {
205
- if (this.compress[path] === undefined) {
206
- this.compress[path] = data;
207
- }
208
- res.end(this.compress[path]);
209
- }
210
- directDeal(data, path, res) {
211
- if (this.raw[path] === undefined) {
212
- this.compress[path] = data;
213
- }
214
- res.end(this.raw[path]);
215
- }
216
- async process(req, res) {
217
- try {
218
- const {
219
- url
220
- } = req;
221
- if (url === '/update/time') {
222
- const {
223
- time
224
- } = this;
225
- res.end(time);
226
- } else if (this.regexp.test(url)) {
227
- const restPath = url.substring(1, url.length);
228
- this.cacheOutput(req, res, restPath, _fs.default.readFileSync(_path.default.resolve('static', restPath)), parseInt(_fs.default.statSync(_path.default.resolve('static', restPath)).mtimeMs));
229
- } else if (url.substring(0, 4) === '/api') {
230
- const body = await new Promise((resolve, reject) => {
231
- req.on('data', data => {
232
- resolve(data.toString());
233
- });
234
- });
235
- const {
236
- location
237
- } = this.options;
238
- if (location !== undefined) {
239
- const response = await fetch(location + url, {
240
- method: 'POST',
241
- body
242
- });
243
- for (const k of response.headers.keys()) {
244
- res.setHeader(formateHttpKey(k), response.headers.get(k));
245
- }
246
- const data = await response.text();
247
- res.end(JSON.stringify(data));
248
- }
249
- } else {
250
- if (this.html === undefined) {
251
- const {
252
- title,
253
- content
254
- } = this.options;
255
- this.html = getHtml(title, content);
256
- }
257
- this.cacheOutput(req, res, '*html', this.html, time);
258
- }
259
- } catch (e) {
260
- const {
261
- develope
262
- } = this.options;
263
- if (develope === true) {
264
- throw e;
265
- } else {
266
- res.writeHead(500);
267
- res.end();
268
- }
269
- }
270
- }
271
- }
272
- exports.CommonHttp = CommonHttp;