|
| 1 | +import { |
| 2 | + NowRequestCookies, |
| 3 | + NowRequestQuery, |
| 4 | + NowRequestBody, |
| 5 | + NowRequest, |
| 6 | + NowResponse, |
| 7 | +} from '@now/node'; |
| 8 | +import { IncomingMessage, ServerResponse } from 'http'; |
| 9 | +import { parse } from 'cookie'; |
| 10 | +import { parse as parseContentType } from 'content-type'; |
| 11 | +import { parse as parseQS } from 'querystring'; |
| 12 | +import { URL } from 'url'; |
| 13 | +import micro, { buffer, send } from 'micro'; |
| 14 | + |
| 15 | +export class ApiError extends Error { |
| 16 | + readonly statusCode: number; |
| 17 | + |
| 18 | + constructor(statusCode: number, message: string) { |
| 19 | + super(message); |
| 20 | + this.statusCode = statusCode; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function getBodyParser(req: IncomingMessage, body: Buffer) { |
| 25 | + return function parseBody(): NowRequestBody { |
| 26 | + if (!req.headers['content-type']) { |
| 27 | + return undefined; |
| 28 | + } |
| 29 | + |
| 30 | + const { type } = parseContentType(req.headers['content-type']); |
| 31 | + |
| 32 | + if (type === 'application/json') { |
| 33 | + try { |
| 34 | + return JSON.parse(body.toString()); |
| 35 | + } catch (error) { |
| 36 | + throw new ApiError(400, 'Invalid JSON'); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (type === 'application/octet-stream') { |
| 41 | + return body; |
| 42 | + } |
| 43 | + |
| 44 | + if (type === 'application/x-www-form-urlencoded') { |
| 45 | + // note: querystring.parse does not produce an iterable object |
| 46 | + // https://nodejs.org/api/querystring.html#querystring_querystring_parse_str_sep_eq_options |
| 47 | + return parseQS(body.toString()); |
| 48 | + } |
| 49 | + |
| 50 | + if (type === 'text/plain') { |
| 51 | + return body.toString(); |
| 52 | + } |
| 53 | + |
| 54 | + return undefined; |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +function getQueryParser({ url = '/' }: IncomingMessage) { |
| 59 | + return function parseQuery(): NowRequestQuery { |
| 60 | + // we provide a placeholder base url because we only want searchParams |
| 61 | + const params = new URL(url, 'https://n').searchParams; |
| 62 | + |
| 63 | + const query: { [key: string]: string | string[] } = {}; |
| 64 | + params.forEach((value, name) => { |
| 65 | + query[name] = value; |
| 66 | + }); |
| 67 | + return query; |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +function getCookieParser(req: IncomingMessage) { |
| 72 | + return function parseCookie(): NowRequestCookies { |
| 73 | + const header: undefined | string | string[] = req.headers.cookie; |
| 74 | + if (!header) { |
| 75 | + return {}; |
| 76 | + } |
| 77 | + return parse(Array.isArray(header) ? header.join(';') : header); |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +const nowNodeServer = ( |
| 82 | + route: (req: NowRequest, res: NowResponse) => any | Promise<any> |
| 83 | +) => |
| 84 | + micro(async (req: IncomingMessage, res: ServerResponse) => { |
| 85 | + const bufferOrString = await buffer(req); |
| 86 | + const nowReq = Object.assign(req, { |
| 87 | + body: |
| 88 | + typeof bufferOrString === 'string' |
| 89 | + ? bufferOrString |
| 90 | + : getBodyParser(req, bufferOrString)(), |
| 91 | + cookies: getCookieParser(req)(), |
| 92 | + query: getQueryParser(req)(), |
| 93 | + }); |
| 94 | + let _status: number; |
| 95 | + const nowRes = Object.assign(res, { |
| 96 | + status: (status: number) => { |
| 97 | + _status = status; |
| 98 | + return nowRes; |
| 99 | + }, |
| 100 | + json: (jsonBody: any) => { |
| 101 | + send(nowRes, _status || 200, jsonBody); |
| 102 | + return nowRes; |
| 103 | + }, |
| 104 | + send: (body: string | object | Buffer) => { |
| 105 | + send(nowRes, _status || 200, body); |
| 106 | + return nowRes; |
| 107 | + }, |
| 108 | + text: (body: string) => { |
| 109 | + send(nowRes, _status || 200, body); |
| 110 | + return nowRes; |
| 111 | + }, |
| 112 | + }); |
| 113 | + return await route(nowReq, nowRes); |
| 114 | + }); |
| 115 | + |
| 116 | +export default nowNodeServer; |
0 commit comments