UNPKG

3.67 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const router = require("./router");
4const debug = require("debug");
5const stream = require("stream");
6const send = require("send");
7var log = debug('akala:master');
8var httpRouter = router.HttpRouter;
9exports.Router = httpRouter;
10function serveStatic(path, options) {
11 if (!options)
12 options = {};
13 if (typeof (options.fallthrough) == 'undefined')
14 options.fallthrough = true;
15 return function (req, res, ...next) {
16 var sendstr = send(req, req.url, options);
17 sendstr.on('error', function (error) {
18 if (error && error.code == "ENOENT")
19 if (options.fallthrough)
20 next[next.length - 1]();
21 else
22 res.status(404).end();
23 else
24 next[next.length - 1](error);
25 });
26 sendstr.pipe(res);
27 };
28}
29exports.serveStatic = serveStatic;
30function expressWrap(handler) {
31 return function (req, response, ...rest) {
32 handler(req, response, rest[rest.length - 1]);
33 };
34}
35exports.expressWrap = expressWrap;
36function expressWrapError(handler) {
37 return function (error, req, response, ...rest) {
38 handler(error, req, response, rest[rest.length - 1]);
39 };
40}
41exports.expressWrapError = expressWrapError;
42function translateRequest(req) {
43 return {
44 url: req.url,
45 headers: req.headers,
46 httpVersion: req.httpVersion,
47 httpVersionMajor: req.httpVersionMajor,
48 httpVersionMinor: req.httpVersionMinor,
49 ip: req.ip,
50 method: req.method,
51 params: req.params,
52 path: req.path,
53 protocol: req.protocol,
54 query: req.query,
55 rawHeaders: req.rawHeaders,
56 rawTrailers: req.rawTrailers,
57 statusCode: req.statusCode,
58 statusMessage: req.statusMessage,
59 trailers: req.trailers,
60 body: req['body'],
61 user: req['user']
62 };
63}
64exports.translateRequest = translateRequest;
65function handleResponse(res, locationReplacer, defaultStatus) {
66 return function (response) {
67 var status = response.statusCode || defaultStatus;
68 if (response.headers)
69 Object.keys(response.headers).forEach(function (header) {
70 if (header.toLowerCase() == 'location' && locationReplacer != null)
71 response.headers[header] = locationReplacer(response.headers[header]);
72 res.setHeader(header, response.headers[header]);
73 });
74 res.writeHead(status, response.statusMessage, response.headers);
75 if (response instanceof stream.Readable)
76 response.pipe(res);
77 else {
78 if (Buffer.isBuffer(response.data)) {
79 log('sending buffer');
80 res.write(response.data);
81 }
82 else if (Array.isArray(response.data)) {
83 log('sending array');
84 response.data.forEach(function (chunk) {
85 res.write(chunk);
86 });
87 }
88 else {
89 log('sending object');
90 if (typeof (response.data) !== 'string' && typeof response.data != 'number' && typeof (response.data) !== 'undefined')
91 res.write(JSON.stringify(response.data));
92 else if (typeof (response.data) != 'undefined')
93 res.write(response.data);
94 }
95 res.end();
96 }
97 };
98}
99exports.handleResponse = handleResponse;
100//# sourceMappingURL=master-meta.js.map
\No newline at end of file