manner.js 0.0.17 → 0.0.19

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.
@@ -0,0 +1,170 @@
1
+ function isOption(string) {
2
+ let ans = true;
3
+ if (typeof string === 'string') {
4
+ let i = 0;
5
+ for (i = 0; i < 1; i += 1) {
6
+ if (string.charAt(i) === '-') {
7
+ break;
8
+ }
9
+ }
10
+ if (i === 0) {
11
+ ans = false;
12
+ }
13
+ } else {
14
+ ans = false;
15
+ }
16
+ return ans;
17
+ }
18
+
19
+ function transformOption(value) {
20
+ let ans;
21
+ const s = value.split('-');
22
+ if (s.length >= 1) {
23
+ ans = s.map((e, i) => {
24
+ if (i === 0) {
25
+ return e;
26
+ } else {
27
+ return e.charAt(0).toUpperCase() + e.substring(1, e.length);
28
+ }
29
+ });
30
+ } else {
31
+ ans = value;
32
+ }
33
+ return ans.join('');
34
+ }
35
+
36
+ export function parseOption(...params) {
37
+ const ans = {};
38
+ for (let i = 0; i < params.length; i += 1) {
39
+ const param = params[i];
40
+ if (param.charAt(0) === '-') {
41
+ const regexp = /^\-([a-z])$/;
42
+ if (regexp.test(param)) {
43
+ const [_, k] = param.match(regexp);
44
+ if (isOption(params[i+1])) {
45
+ ans[k] = params[i+1];
46
+ i += 1;
47
+ } else {
48
+ ans[k] = true;
49
+ }
50
+ }
51
+ if (param.charAt(1) === '-') {
52
+ const regexp = /^\-\-([a-z\-]+)$/;
53
+ if (regexp.test(param)) {
54
+ const [_, k] = param.match(regexp);
55
+ if (isOption(params[i+1])) {
56
+ ans[transformOption(k)] = params[i+1];
57
+ i += 1;
58
+ } else {
59
+ ans[k] = true;
60
+ }
61
+ }
62
+ }
63
+ }
64
+ }
65
+ return ans;
66
+ }
67
+ export function filterNamespace(namespace) {
68
+ const ans = {};
69
+ if (typeof namespace === 'object') {
70
+ Object.keys(namespace).forEach((k) => {
71
+ if (k !== 'expires') {
72
+ ans[k] = namespace[k];
73
+ }
74
+ });
75
+ }
76
+ return ans;
77
+ }
78
+
79
+ export default function readCookie(cookie) {
80
+ const cookies = {};
81
+ if (typeof cookie === 'string') {
82
+ cookie.split(';').forEach((i) => {
83
+ const [key, value] = i.split('=');
84
+ cookies[key.trim()] = value;
85
+ });
86
+ }
87
+ const namespaces = {};
88
+ Object.keys(cookies).forEach((k) => {
89
+ const result = k.split('_');
90
+ if (result.length === 2) {
91
+ const [namespace, key] = result;
92
+ if (namespaces[namespace] === undefined) {
93
+ namespaces[namespace] = {};
94
+ }
95
+ namespaces[namespace][key] = cookies[k];
96
+ }
97
+ });
98
+ return namespaces;
99
+ }
100
+
101
+ export function formateHttpDate(date) {
102
+ let [week, month, day, year, time, zone] = date.toString().split(' ');
103
+ zone = zone.split('+')[0];
104
+ return month + ', ' + day + ' ' + month + ' ' + year + ' ' + time + ' ' + zone;
105
+ }
106
+
107
+ export function formateHttpKey(key) {
108
+ return key.split('-').map((v) => {
109
+ return v.substring(0, 1).toUpperCase() + v.substring(1, v.length);
110
+ }).join('-');
111
+ }
112
+
113
+ function getLists(list) {
114
+ return list.join('|');
115
+ }
116
+
117
+ export class CommonHttp {
118
+ constructor(options) {
119
+ this.time = new Date().getTime();
120
+ const { fonts, } = options;
121
+ if (options.fonts === undefined) {
122
+ options.fonts = [];
123
+ }
124
+ this.options = options;
125
+ this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['html, ico', 'js']))})$`);
126
+ }
127
+
128
+ async process(req, res) {
129
+ try {
130
+ const { url, } = req;
131
+ if (url === '/update/time') {
132
+ const { time, } = this;
133
+ res.end(time);
134
+ } else if (this.regexp.test(url)) {
135
+ cacheOutput(req, res, restPath,
136
+ fs.readFileSync(path.resolve('static', restPath)),
137
+ parseInt(fs.statSync(path.resolve('static', restPath)).mtimeMs),
138
+ );
139
+ } else if (url.substring(0, 4) === '/api') {
140
+ const body = await new Promise((resolve, reject) => {
141
+ req.on('data', (data) => {
142
+ resolve(data.toString());
143
+ });
144
+ });
145
+ const {
146
+ location,
147
+ } = this.options;
148
+ const response = await fetch(location + url, {
149
+ method: 'POST',
150
+ body,
151
+ });
152
+ for (const k of response.headers.keys()) {
153
+ res.setHeader(formateHttpKey(k), response.headers.get(k));
154
+ }
155
+ const data = await response.text();
156
+ res.end(JSON.stringify(data));
157
+ }
158
+ } catch (e) {
159
+ const {
160
+ develope,
161
+ } = this.options;
162
+ if (develope === true) {
163
+ throw e;
164
+ } else {
165
+ res.writeHead(500);
166
+ res.end();
167
+ }
168
+ }
169
+ }
170
+ }
@@ -0,0 +1,18 @@
1
+ import { describe, expect, test, } from '@jest/globals';
2
+ import http from 'http';
3
+ import fetch from 'node-fetch';
4
+ import CommonHttp from '~/server/class/CommonHttp';
5
+
6
+ beforeAll(() => {
7
+ http.createServer(async (req, res) => {
8
+ const commonHttp = new CommonHttp({});
9
+ await commonHttp.process(req, res);
10
+ res.end('result')
11
+ }).listen(80);
12
+ });
13
+
14
+ test('[class] CommonHttp', async () => {
15
+ const response = await fetch('http://localhost');
16
+ const body = await response.text();
17
+ expect(JSON.stringify(body)).toMatch('\"result\"');
18
+ });
@@ -0,0 +1,9 @@
1
+ import { describe, expect, test, } from '@jest/globals';
2
+ import parseOption from '~/server/lib/parseOption';
3
+
4
+ describe('[lib] parseOption', () => {
5
+ test('command line option should parse correct.', () => {
6
+ expect(parseOption('-t', 'type', '--type', 'type', '--detail-type', 'type')
7
+ ).toEqual({ 't': 'type', 'type': 'type', 'detailType': 'type' });
8
+ });
9
+ });