-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor to a provider/framework type absraction
- Loading branch information
1 parent
e614c64
commit e586231
Showing
28 changed files
with
310 additions
and
298 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
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
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
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
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,54 @@ | ||
'use strict'; | ||
|
||
const Response = require('../response'); | ||
|
||
function common(cb) { | ||
return request => { | ||
const response = new Response(request); | ||
|
||
cb(request, response); | ||
|
||
return response; | ||
}; | ||
} | ||
|
||
module.exports = function getFramework(app) { | ||
if (typeof app.callback === 'function') { | ||
return common(app.callback()); | ||
} | ||
|
||
if (typeof app.handle === 'function') { | ||
return common((request, response) => { | ||
app.handle(request, response); | ||
}); | ||
} | ||
|
||
if (typeof app === 'function') { | ||
return common(app); | ||
} | ||
|
||
if (app.router && typeof app.router.route == 'function') { | ||
return common((req, res) => { | ||
const { url, method, headers, body } = req; | ||
app.router.route({ url, method, headers, body }, res); | ||
}); | ||
} | ||
|
||
if (app._core && typeof app._core._dispatch === 'function') { | ||
return common(app._core._dispatch({ | ||
app | ||
})); | ||
} | ||
|
||
if (typeof app.inject === 'function') { | ||
return async request => { | ||
const { method, url, headers, body } = request; | ||
|
||
const res = await app.inject({ method, url, headers, payload: body }) | ||
|
||
return Response.from(res); | ||
}; | ||
} | ||
|
||
throw new Error('Unsupported framework'); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,45 @@ | ||
'use strict'; | ||
|
||
const Request = require('../../request'); | ||
|
||
function requestHeaders(event) { | ||
return Object.keys(event.headers).reduce((headers, key) => { | ||
headers[key.toLowerCase()] = event.headers[key]; | ||
return headers; | ||
}, {}); | ||
} | ||
|
||
function requestBody(event, headers) { | ||
if (typeof event.body === 'string') { | ||
return Buffer.from(event.body, event.isBase64Encoded ? 'base64' : 'utf8'); | ||
} else if (Buffer.isBuffer(event.body)) { | ||
return event.body; | ||
} else if (typeof event.body === 'object') { | ||
const contentType = headers['content-type']; | ||
|
||
if (contentType && contentType.indexOf('application/json') === 0) { | ||
return Buffer.from(JSON.stringify(event.body)); | ||
} | ||
|
||
throw new Error('event.body was an object but content-type is not json'); | ||
} | ||
|
||
throw new Error(`Unexpected event.body type: ${typeof event.body}`); | ||
} | ||
|
||
module.exports = (event, options) => { | ||
const path = event.requestContext.path; | ||
const method = event.httpMethod; | ||
const query = event.multiValueQueryStringParameters || event.queryStringParameters; | ||
const baseUrl = event.requestContext.path.slice(0, -event.path.length); | ||
const remoteAddress = event.requestContext.identity.sourceIp; | ||
const headers = requestHeaders(event); | ||
const body = requestBody(event, headers); | ||
|
||
if (typeof options.requestId === 'string' && options.requestId.length > 0) { | ||
const header = options.requestId.toLowerCase(); | ||
headers[header] = headers[header] || event.requestContext.requestId; | ||
} | ||
|
||
return new Request({ method, path, baseUrl, query, headers, body, remoteAddress }); | ||
}; |
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,20 @@ | ||
'use strict'; | ||
|
||
const isBinary = require('./is-binary'); | ||
const Response = require('../../response'); | ||
const sanitizeHeaders = require('./sanitize-headers'); | ||
|
||
module.exports = (response, options) => { | ||
const { statusCode } = response; | ||
const headers = sanitizeHeaders(Response.headers(response)); | ||
|
||
if (headers['transfer-encoding'] === 'chunked' || response.chunkedEncoding) { | ||
throw new Error('chunked encoding not supported'); | ||
} | ||
|
||
const isBase64Encoded = isBinary(headers, options); | ||
const encoding = isBase64Encoded ? 'base64' : 'utf8'; | ||
const body = Response.body(response).toString(encoding); | ||
|
||
return { statusCode, headers, isBase64Encoded, body }; | ||
}; |
Oops, something went wrong.