-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit baaf17f
Showing
13 changed files
with
6,788 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*.log | ||
.DS_Store | ||
node_modules | ||
.rts2_cache_cjs | ||
.rts2_cache_esm | ||
.rts2_cache_umd | ||
.rts2_cache_system | ||
dist | ||
test/temp.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.tabSize": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Sean Matheson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# zeit-node-server | ||
|
||
An unofficial package allowing you to create Node Server instances of your Zeit `@now/node` lambdas. | ||
|
||
Doing so allows you to write unit/integration tests for your routes. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install zeit-node-server | ||
``` | ||
|
||
### Example Jest Test | ||
|
||
```javascript | ||
import createServer from 'zeit-now-node-server'; | ||
import listen from 'test-listen'; | ||
import axios from 'axios'; | ||
import routeUnderTest from './api/hello-world'; | ||
|
||
it('should allow me to test my node lambdas' async () => { | ||
const server = createServer(routeUnderTest); | ||
const url = await listen(server); | ||
const response = await axios.get(url); | ||
expect(response.data).toBe('Hello world'); | ||
server.close(); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
|
||
const path = require('path'); | ||
|
||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testMatch: ['<rootDir>/test/**/*.test.ts'], | ||
}; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"name": "zeit-now-node-server", | ||
"version": "0.1.0", | ||
"description": "Create a server for your Zeit @now/node lambdas in order to test them", | ||
"license": "MIT", | ||
"author": "Sean Matheson", | ||
"main": "dist/index.js", | ||
"module": "dist/zeit-now-node-server.esm.js", | ||
"typings": "dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ctrlplusb/zeit-now-node-server.git" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"start": "tsdx watch", | ||
"build": "tsdx build", | ||
"test": "tsdx test", | ||
"lint": "tsdx lint" | ||
}, | ||
"peerDependencies": {}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "tsdx lint" | ||
} | ||
}, | ||
"prettier": { | ||
"printWidth": 80, | ||
"semi": true, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
}, | ||
"devDependencies": { | ||
"@now/node": "^1.0.2", | ||
"@types/content-type": "^1.1.3", | ||
"@types/cookie": "^0.3.3", | ||
"@types/jest": "^24.0.21", | ||
"@types/micro": "^7.3.3", | ||
"@types/test-listen": "^1.1.0", | ||
"axios": "^0.19.0", | ||
"form-data": "^2.5.1", | ||
"husky": "^3.0.9", | ||
"jest": "^24.9.0", | ||
"test-listen": "^1.1.0", | ||
"ts-jest": "^24.1.0", | ||
"tsdx": "^0.11.0", | ||
"tslib": "^1.10.0", | ||
"typescript": "^3.7.2" | ||
}, | ||
"dependencies": { | ||
"content-type": "^1.0.4", | ||
"cookie": "^0.4.0", | ||
"micro": "^9.3.4", | ||
"querystring": "^0.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { | ||
NowRequestCookies, | ||
NowRequestQuery, | ||
NowRequestBody, | ||
NowRequest, | ||
NowResponse, | ||
} from '@now/node'; | ||
import { IncomingMessage, ServerResponse } from 'http'; | ||
import { parse } from 'cookie'; | ||
import { parse as parseContentType } from 'content-type'; | ||
import { parse as parseQS } from 'querystring'; | ||
import { URL } from 'url'; | ||
import micro, { buffer, send } from 'micro'; | ||
|
||
export class ApiError extends Error { | ||
readonly statusCode: number; | ||
|
||
constructor(statusCode: number, message: string) { | ||
super(message); | ||
this.statusCode = statusCode; | ||
} | ||
} | ||
|
||
function getBodyParser(req: IncomingMessage, body: Buffer) { | ||
return function parseBody(): NowRequestBody { | ||
if (!req.headers['content-type']) { | ||
return undefined; | ||
} | ||
|
||
const { type } = parseContentType(req.headers['content-type']); | ||
|
||
if (type === 'application/json') { | ||
try { | ||
return JSON.parse(body.toString()); | ||
} catch (error) { | ||
throw new ApiError(400, 'Invalid JSON'); | ||
} | ||
} | ||
|
||
if (type === 'application/octet-stream') { | ||
return body; | ||
} | ||
|
||
if (type === 'application/x-www-form-urlencoded') { | ||
// note: querystring.parse does not produce an iterable object | ||
// https://nodejs.org/api/querystring.html#querystring_querystring_parse_str_sep_eq_options | ||
return parseQS(body.toString()); | ||
} | ||
|
||
if (type === 'text/plain') { | ||
return body.toString(); | ||
} | ||
|
||
return undefined; | ||
}; | ||
} | ||
|
||
function getQueryParser({ url = '/' }: IncomingMessage) { | ||
return function parseQuery(): NowRequestQuery { | ||
// we provide a placeholder base url because we only want searchParams | ||
const params = new URL(url, 'https://n').searchParams; | ||
|
||
const query: { [key: string]: string | string[] } = {}; | ||
params.forEach((value, name) => { | ||
query[name] = value; | ||
}); | ||
return query; | ||
}; | ||
} | ||
|
||
function getCookieParser(req: IncomingMessage) { | ||
return function parseCookie(): NowRequestCookies { | ||
const header: undefined | string | string[] = req.headers.cookie; | ||
if (!header) { | ||
return {}; | ||
} | ||
return parse(Array.isArray(header) ? header.join(';') : header); | ||
}; | ||
} | ||
|
||
const nowNodeServer = ( | ||
route: (req: NowRequest, res: NowResponse) => any | Promise<any> | ||
) => | ||
micro(async (req: IncomingMessage, res: ServerResponse) => { | ||
const bufferOrString = await buffer(req); | ||
const nowReq = Object.assign(req, { | ||
body: | ||
typeof bufferOrString === 'string' | ||
? bufferOrString | ||
: getBodyParser(req, bufferOrString)(), | ||
cookies: getCookieParser(req)(), | ||
query: getQueryParser(req)(), | ||
}); | ||
let _status: number; | ||
const nowRes = Object.assign(res, { | ||
status: (status: number) => { | ||
_status = status; | ||
return nowRes; | ||
}, | ||
json: (jsonBody: any) => { | ||
send(nowRes, _status || 200, jsonBody); | ||
return nowRes; | ||
}, | ||
send: (body: string | object | Buffer) => { | ||
send(nowRes, _status || 200, body); | ||
return nowRes; | ||
}, | ||
text: (body: string) => { | ||
send(nowRes, _status || 200, body); | ||
return nowRes; | ||
}, | ||
}); | ||
return await route(nowReq, nowRes); | ||
}); | ||
|
||
export default nowNodeServer; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.