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.
Files changed (3) hide show
  1. package/client.js +81 -1
  2. package/package.json +1 -1
  3. package/server.js +206 -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.26",
3
+ "version": "0.0.28",
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,111 @@
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 _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
- `);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;
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;