manner.js 0.0.27 → 0.0.29

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/client.js +81 -1
  2. package/package.json +1 -1
  3. package/server.js +258 -2
package/client.js CHANGED
@@ -1 +1,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}))}
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "manner.js",
3
- "version": "0.0.27",
3
+ "version": "0.0.29",
4
4
  "description": "Frontend mode project.",
5
5
  "repository": "git@github.com:leobrad/mode.git",
6
6
  "author": "Leo Ely",
package/server.js CHANGED
@@ -1,4 +1,125 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.CommonHttp=void 0,exports.default=getHtml,exports.parseOption=parseOption,exports.readCookie=readCookie;var _handlebars=_interopRequireDefault(require("handlebars")),_htmlMinifier=require("html-minifier");function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}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("-")}let html;function getHtml(a,b){const c=_handlebars.default.compile(`
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(`
2
123
  <!doctype html>
3
124
  <html lang="en">
4
125
  <head>
@@ -12,4 +133,139 @@
12
133
  <script src="main.bundle.js"></script>
13
134
  </body>
14
135
  </html>
15
- `);return void 0===html&&(html=(0,_htmlMinifier.minify)(c({title:a,content:b}),{collapseWhitespace:!0})),html}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))}}else{if(this.html.val===void 0){const{title:a,content:b}=this.options;this.html.val=getHtml(a,b)}cacheOutput(a,b,"*html",this.html.val,time)}}catch(a){const{develope:c}=this.options;if(!0===c)throw a;else b.writeHead(500),b.end()}}}exports.CommonHttp=CommonHttp;
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
+ this.cacheOutput(req, res, restPath, _fs.default.readFileSync(_path.default.resolve('static', restPath)), parseInt(_fs.default.statSync(_path.default.resolve('static', restPath)).mtimeMs));
228
+ } else if (url.substring(0, 4) === '/api') {
229
+ const body = await new Promise((resolve, reject) => {
230
+ req.on('data', data => {
231
+ resolve(data.toString());
232
+ });
233
+ });
234
+ const {
235
+ location
236
+ } = this.options;
237
+ if (location !== undefined) {
238
+ const response = await fetch(location + url, {
239
+ method: 'POST',
240
+ body
241
+ });
242
+ for (const k of response.headers.keys()) {
243
+ res.setHeader(formateHttpKey(k), response.headers.get(k));
244
+ }
245
+ const data = await response.text();
246
+ res.end(JSON.stringify(data));
247
+ }
248
+ } else {
249
+ if (this.html === undefined) {
250
+ const {
251
+ title,
252
+ content
253
+ } = this.options;
254
+ this.html = getHtml(title, content);
255
+ }
256
+ this.cacheOutput(req, res, '*html', this.html, time);
257
+ }
258
+ } catch (e) {
259
+ const {
260
+ develope
261
+ } = this.options;
262
+ if (develope === true) {
263
+ throw e;
264
+ } else {
265
+ res.writeHead(500);
266
+ res.end();
267
+ }
268
+ }
269
+ }
270
+ }
271
+ exports.CommonHttp = CommonHttp;