1 | import * as akala from '@akala/core'
|
2 | import * as router from './router';
|
3 | import * as worker from './worker-meta'
|
4 | export { CoreProperties as Package } from '../src/package';
|
5 | import * as stream from 'stream'
|
6 | import * as express from 'express';
|
7 | import send from 'send'
|
8 |
|
9 | var log = akala.log('akala:master');
|
10 |
|
11 | var httpRouter = router.HttpRouter;
|
12 | type request = router.Request & { body?: any };
|
13 | type response = router.Response;
|
14 | export { httpRouter as Router, request as Request, response as Response };
|
15 |
|
16 | export function serveStatic(path, options?: send.SendOptions & { fallthrough?: boolean })
|
17 | {
|
18 | if (!options)
|
19 | options = {};
|
20 | if (typeof (options.fallthrough) == 'undefined')
|
21 | options.fallthrough = true;
|
22 |
|
23 | return function (req: request, res: response, ...next: akala.NextFunction[])
|
24 | {
|
25 | var sendstr = send(req, path || req.url, options);
|
26 | sendstr.on('error', function (error)
|
27 | {
|
28 | if (error && error.code == "ENOENT")
|
29 | if (options.fallthrough)
|
30 | next[next.length - 1]();
|
31 | else
|
32 | res.status(404).end();
|
33 | else
|
34 | next[next.length - 1](error);
|
35 | });
|
36 | sendstr.pipe(res);
|
37 | }
|
38 | }
|
39 |
|
40 | export function expressWrap(handler: express.Handler)
|
41 | {
|
42 | return function (req: router.Request, response: router.Response, ...rest)
|
43 | {
|
44 | handler(req as any, response as any, rest[rest.length - 1]);
|
45 | }
|
46 | }
|
47 | export function expressWrapError(handler: express.ErrorRequestHandler)
|
48 | {
|
49 | return function (error, req: router.Request, response: router.Response, ...rest)
|
50 | {
|
51 | handler(error, req as any, response as any, rest[rest.length - 1]);
|
52 | }
|
53 | }
|
54 |
|
55 | export function translateRequest(req: router.Request): Partial<worker.Request>
|
56 | {
|
57 | return {
|
58 | url: req.url,
|
59 | headers: req.headers,
|
60 | httpVersion: req.httpVersion,
|
61 | httpVersionMajor: req.httpVersionMajor,
|
62 | httpVersionMinor: req.httpVersionMinor,
|
63 | ip: req.ip,
|
64 | method: req.method,
|
65 | params: req.params,
|
66 | path: req.path,
|
67 | protocol: req.protocol,
|
68 | query: req.query,
|
69 | rawHeaders: req.rawHeaders,
|
70 | rawTrailers: req.rawTrailers,
|
71 | statusCode: req.statusCode,
|
72 | statusMessage: req.statusMessage,
|
73 | trailers: req.trailers,
|
74 | body: req['body'],
|
75 | user: req['user']
|
76 | }
|
77 | }
|
78 |
|
79 | export function handleResponse(res: router.Response, locationReplacer: (key: string) => string, defaultStatus: number): (response: worker.CallbackResponse) => void
|
80 | {
|
81 | return function (response)
|
82 | {
|
83 | var status = response.statusCode || defaultStatus;
|
84 | if (response.headers)
|
85 | Object.keys(response.headers).forEach(function (header)
|
86 | {
|
87 | if (header.toLowerCase() == 'location' && locationReplacer != null)
|
88 | response.headers[header] = locationReplacer(response.headers[header]);
|
89 | res.setHeader(header, response.headers[header]);
|
90 | });
|
91 | res.writeHead(status, response.statusMessage, response.headers);
|
92 | if (response instanceof stream.Readable)
|
93 | response.pipe(res);
|
94 | else
|
95 | {
|
96 | if (Buffer.isBuffer(response.data))
|
97 | {
|
98 | log('sending buffer');
|
99 | res.write(response.data);
|
100 | }
|
101 | else if (Array.isArray(response.data))
|
102 | {
|
103 | log('sending array');
|
104 | response.data.forEach(function (chunk)
|
105 | {
|
106 | res.write(chunk);
|
107 | });
|
108 | }
|
109 | else
|
110 | {
|
111 | log('sending object');
|
112 | if (typeof (response.data) !== 'string' && typeof response.data != 'number' && typeof (response.data) !== 'undefined')
|
113 | res.write(JSON.stringify(response.data));
|
114 | else if (typeof (response.data) != 'undefined')
|
115 | res.write(response.data);
|
116 | }
|
117 | res.end();
|
118 | }
|
119 | }
|
120 | }
|