expediate 0.0.3 → 1.0.1
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/.npmignore +7 -5
- package/LICENSE +21 -0
- package/README.md +662 -51
- package/dist/apis.d.ts +166 -0
- package/dist/apis.d.ts.map +1 -0
- package/dist/apis.js.map +1 -0
- package/dist/git.d.ts +74 -0
- package/dist/git.d.ts.map +1 -0
- package/dist/git.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/jwt-auth.d.ts +280 -0
- package/dist/jwt-auth.d.ts.map +1 -0
- package/dist/jwt-auth.js.map +1 -0
- package/dist/misc.d.ts +203 -0
- package/dist/misc.d.ts.map +1 -0
- package/dist/misc.js.map +1 -0
- package/dist/router.d.ts +224 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js.map +1 -0
- package/dist/static.d.ts +164 -0
- package/dist/static.d.ts.map +1 -0
- package/dist/static.js.map +1 -0
- package/package.json +31 -7
- package/src/apis.ts +428 -0
- package/src/git.ts +326 -0
- package/src/index.ts +85 -0
- package/src/jwt-auth.ts +861 -0
- package/src/mimetypes.json +1 -0
- package/src/misc.ts +734 -0
- package/src/router.ts +736 -0
- package/src/static.ts +904 -0
- package/.gitignore +0 -14
- package/index.js +0 -305
- package/sample.js +0 -9
- package/static.js +0 -416
package/.gitignore
DELETED
package/index.js
DELETED
|
@@ -1,305 +0,0 @@
|
|
|
1
|
-
/* Copyright 2021 Fabien Bavent
|
|
2
|
-
*
|
|
3
|
-
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
4
|
-
* copy of this software and associated documentation files (the "Software"),
|
|
5
|
-
* to deal in the Software without restriction, including without limitation
|
|
6
|
-
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
7
|
-
* and/or sell copies of the Software, and to permit persons to whom the
|
|
8
|
-
* Software is furnished to do so, subject to the following conditions:
|
|
9
|
-
*
|
|
10
|
-
* The above copyright notice and this permission notice shall be included
|
|
11
|
-
* in all copies or substantial portions of the Software.
|
|
12
|
-
*
|
|
13
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
14
|
-
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
16
|
-
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
18
|
-
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
19
|
-
* DEALINGS IN THE SOFTWARE.
|
|
20
|
-
*/
|
|
21
|
-
'use strict';
|
|
22
|
-
|
|
23
|
-
const http = require('http'),
|
|
24
|
-
https = require('https'),
|
|
25
|
-
serv = require('./static.js');
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Prepare an layer object to define a new route
|
|
29
|
-
*
|
|
30
|
-
* @param {String} method
|
|
31
|
-
* @param {String} [path]
|
|
32
|
-
* @param {Listener} listener
|
|
33
|
-
* @return {Layer}
|
|
34
|
-
*/
|
|
35
|
-
function buildRouteLayer(method, path, listener) {
|
|
36
|
-
if (method)
|
|
37
|
-
method = method.toUpperCase();
|
|
38
|
-
if (typeof path !== 'string' && !(path instanceof RegExp)) {
|
|
39
|
-
listener = path;
|
|
40
|
-
path = '/';
|
|
41
|
-
}
|
|
42
|
-
let parts = path instanceof RegExp ? null : path.split('/').filter(x => x.length > 0);
|
|
43
|
-
if (listener && typeof listener !== 'function')
|
|
44
|
-
listener = listener.listener // Route object
|
|
45
|
-
if (typeof listener !== 'function')
|
|
46
|
-
throw new TypeError('Incorrect listener type');
|
|
47
|
-
return { method, path, parts, listener };
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Check if a request match the current route(layer)
|
|
52
|
-
*
|
|
53
|
-
* In case the request match the route, we update the request
|
|
54
|
-
* path field and the queries.route map depending on route parameters
|
|
55
|
-
*
|
|
56
|
-
* @param {Layer} layer
|
|
57
|
-
* @param {http.ClientRequest} req
|
|
58
|
-
* @param {String[]} parts
|
|
59
|
-
* @return {Bool}
|
|
60
|
-
*/
|
|
61
|
-
function matchRouteLayer(layer, req, parts, path) {
|
|
62
|
-
if (layer.method && layer.method != req.method)
|
|
63
|
-
return false;
|
|
64
|
-
let params = {}
|
|
65
|
-
for (let i = 0; ; ++i) {
|
|
66
|
-
if (layer.parts == null) {
|
|
67
|
-
var m = layer.path.exec(path);
|
|
68
|
-
if (m == null)
|
|
69
|
-
return false;
|
|
70
|
-
for (let i = 1; i < m.length; ++i)
|
|
71
|
-
params[i] = m[i];
|
|
72
|
-
req.path = m.index == 0 ? path.replace(m[0], '') : path;
|
|
73
|
-
req.queries.route = params;
|
|
74
|
-
for (var k in params)
|
|
75
|
-
req.params[k] = params[k];
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
if (layer.parts.length <= i) {
|
|
79
|
-
req.path = '/' + parts.slice(i).join('/');
|
|
80
|
-
req.queries.route = params;
|
|
81
|
-
for (var k in params)
|
|
82
|
-
req.params[k] = params[k];
|
|
83
|
-
return true;
|
|
84
|
-
} else if (parts.length <= i)
|
|
85
|
-
return false;
|
|
86
|
-
else if (layer.parts[i][0] == ':')
|
|
87
|
-
params[layer.parts[i].substring(1)] = parts[i];
|
|
88
|
-
else if (layer.parts[i] != parts[i])
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Extends the request and response object of a http request
|
|
95
|
-
* with parameters and helper.
|
|
96
|
-
*
|
|
97
|
-
* @param {http.ClientRequest} req
|
|
98
|
-
* @param {http.ServerResponse} res
|
|
99
|
-
*/
|
|
100
|
-
function updateHttpObject(req, res) {
|
|
101
|
-
if (req.queries)
|
|
102
|
-
return
|
|
103
|
-
req.queries = {};
|
|
104
|
-
|
|
105
|
-
let qry = new URL(`http://${req.headers.host}${req.url}`)
|
|
106
|
-
req.originalUrl = req.url;
|
|
107
|
-
req.path = qry.pathname
|
|
108
|
-
|
|
109
|
-
// Parse URL-encoded data
|
|
110
|
-
let params = {};
|
|
111
|
-
for(var pair of qry.searchParams.entries())
|
|
112
|
-
params[pair[0]] = pair[1];
|
|
113
|
-
req.queries.url = params;
|
|
114
|
-
req.params = params;
|
|
115
|
-
|
|
116
|
-
// Parse cookies
|
|
117
|
-
if (req.cookies == null) {
|
|
118
|
-
req.cookies = {};
|
|
119
|
-
if (req.headers.cookie) {
|
|
120
|
-
const dico = req.headers.cookie.split(';')
|
|
121
|
-
.map(x => x.replace(/^\s+|\s+$/g, '').split('='));
|
|
122
|
-
for (var k in dico) {
|
|
123
|
-
let key = dico[k][0]
|
|
124
|
-
let val = dico[k][1]
|
|
125
|
-
req.cookies[key] = val;
|
|
126
|
-
// TODO s: Cookie is signed, j: Cookie is a JSON
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
res.setHeader('X-Powered-By', 'Expediate');
|
|
133
|
-
|
|
134
|
-
res.send = (data) => {
|
|
135
|
-
if (data)
|
|
136
|
-
res.write(data);
|
|
137
|
-
res.end();
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
res.status = (code, headers) => {
|
|
141
|
-
res.statusCode = code;
|
|
142
|
-
if (headers)
|
|
143
|
-
for (var k in headers)
|
|
144
|
-
res.setHeader(k, headers[k])
|
|
145
|
-
return res;
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
res.redirect = (url) => {
|
|
149
|
-
let status = 302;
|
|
150
|
-
res.setHeader('location', url);
|
|
151
|
-
res.writeHead(status)
|
|
152
|
-
res.write(`Found. Redirecting to ${url}`);
|
|
153
|
-
res.end();
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
res.cookie = function (name, value, options) {
|
|
157
|
-
var opts = options || {};
|
|
158
|
-
|
|
159
|
-
if (opts.signed && !res.req.secret)
|
|
160
|
-
throw new Error('cookieParser("secret") required for signed cookies');
|
|
161
|
-
|
|
162
|
-
var val = typeof value === 'object'
|
|
163
|
-
? 'j:' + JSON.stringify(value)
|
|
164
|
-
: String(value);
|
|
165
|
-
|
|
166
|
-
if (opts.signed)
|
|
167
|
-
val = 's:' + sign(val, res.req.secret);
|
|
168
|
-
|
|
169
|
-
let txt = `${name}=${String(val)}`
|
|
170
|
-
|
|
171
|
-
if ('maxAge' in opts) {
|
|
172
|
-
opts.expires = new Date(Date.now() + opts.maxAge);
|
|
173
|
-
opts.maxAge /= 1000;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (opts.path == null)
|
|
177
|
-
opts.path = '/';
|
|
178
|
-
txt += `; Path=${opts.path}`
|
|
179
|
-
|
|
180
|
-
res.setHeader('Set-Cookie', txt);
|
|
181
|
-
return res;
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
187
|
-
|
|
188
|
-
module.exports = Router;
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* This function create a new router function as a web listener.
|
|
192
|
-
*/
|
|
193
|
-
function Router() {
|
|
194
|
-
|
|
195
|
-
const routes = [];
|
|
196
|
-
|
|
197
|
-
const listener = function(req, res, done) {
|
|
198
|
-
const method = req.method;
|
|
199
|
-
const url = req.url;
|
|
200
|
-
let idx = 0;
|
|
201
|
-
updateHttpObject(req, res);
|
|
202
|
-
const parts = req.path.split('/').filter(x => x.length > 0);
|
|
203
|
-
|
|
204
|
-
const next = () => {
|
|
205
|
-
while (idx < routes.length) {
|
|
206
|
-
let layer = routes[idx++];
|
|
207
|
-
if (matchRouteLayer(layer, req, parts, req.path))
|
|
208
|
-
return layer.listener(req, res, next);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (done)
|
|
212
|
-
return done();
|
|
213
|
-
return res.status(404).end(`Cannot ${method} ${url}`);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
try {
|
|
217
|
-
next();
|
|
218
|
-
} catch (e) {
|
|
219
|
-
console.warn(e)
|
|
220
|
-
res.status(500).end(`Error ${method} ${url}`)
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
listener.use = (p, l) => routes.push(buildRouteLayer(null, p, l));
|
|
225
|
-
listener.all = (p, l) => routes.push(buildRouteLayer(null, p, l));
|
|
226
|
-
listener.get = (p, l) => routes.push(buildRouteLayer('GET', p, l));
|
|
227
|
-
listener.put = (p, l) => routes.push(buildRouteLayer('PUT', p, l));
|
|
228
|
-
listener.post = (p, l) => routes.push(buildRouteLayer('POST', p, l));
|
|
229
|
-
listener.delete = (p, l) => routes.push(buildRouteLayer('DELETE', p, l));
|
|
230
|
-
listener.patch = (p, l) => routes.push(buildRouteLayer('PATCH', p, l));
|
|
231
|
-
|
|
232
|
-
listener.listen = (port, opts, cb) => {
|
|
233
|
-
if (typeof opts === 'function') {
|
|
234
|
-
cb = opts;
|
|
235
|
-
opts = null;
|
|
236
|
-
}
|
|
237
|
-
if (opts && opts.key && opts.cert)
|
|
238
|
-
https.createServer(opts, listener).listen(port, cb);
|
|
239
|
-
else
|
|
240
|
-
http.createServer(listener).listen(port, cb);
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
return listener;
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
|
247
|
-
|
|
248
|
-
Router.logger = () => {
|
|
249
|
-
return (req, res, next) => {
|
|
250
|
-
const nclr = '\x1b[0m';
|
|
251
|
-
const colors = [
|
|
252
|
-
'\x1b[31m', '\x1b[33m', '\x1b[32m', '\x1b[33m', '\x1b[31m', '\x1b[91m'];
|
|
253
|
-
const rec = new Intl.DateTimeFormat('en-GB', {
|
|
254
|
-
month: 'short',
|
|
255
|
-
day:'2-digit',
|
|
256
|
-
hour:'2-digit',
|
|
257
|
-
minute:'2-digit'}).format();
|
|
258
|
-
const path = req.path;
|
|
259
|
-
req.received = new Date().getTime();
|
|
260
|
-
res.on('finish', ev => {
|
|
261
|
-
req.elapsed = new Date().getTime() - req.received;
|
|
262
|
-
const clr = colors[parseInt(res.statusCode / 100)];
|
|
263
|
-
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
|
264
|
-
const user = req.session ? `${req.session.username}/${req.session.ssid}` : '-'
|
|
265
|
-
const status = `${clr}${res.statusCode}${nclr}`
|
|
266
|
-
const elp = `${req.elapsed} ms`
|
|
267
|
-
const len = res.getHeader('content-length') ? res.getHeader('content-length') : '-';
|
|
268
|
-
console.log(`${rec} ${status} ${req.method} ${path} ${ip} <${user}> ${elp} (${len})`);
|
|
269
|
-
})
|
|
270
|
-
next();
|
|
271
|
-
};
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
Router.parseBody = () => {
|
|
275
|
-
return (req, res, next) => {
|
|
276
|
-
let data = '';
|
|
277
|
-
req.on('data', chunk => {
|
|
278
|
-
data += chunk;
|
|
279
|
-
});
|
|
280
|
-
req.on('end', () => {
|
|
281
|
-
try {
|
|
282
|
-
req.body = JSON.parse(data);
|
|
283
|
-
} catch {
|
|
284
|
-
req.body = data;
|
|
285
|
-
}
|
|
286
|
-
next();
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
Router.static = serv.serveStatic;
|
|
292
|
-
Router.file = serv.serveFile;
|
|
293
|
-
Router.sendFile = serv.sendFile;
|
|
294
|
-
Router.sendIndex = serv.sendIndex;
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
Router.session = (opts) => {
|
|
298
|
-
return (req, res, next) => {
|
|
299
|
-
let ssid = req.cookies.ssid;
|
|
300
|
-
req.session = opts.openSession(ssid);
|
|
301
|
-
if (ssid != req.session.ssid)
|
|
302
|
-
res.cookie('ssid', req.session.ssid);
|
|
303
|
-
next();
|
|
304
|
-
};
|
|
305
|
-
};
|