-
Notifications
You must be signed in to change notification settings - Fork 6
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
Ike Ku
committed
Sep 3, 2017
1 parent
0fad15d
commit 10a2579
Showing
5 changed files
with
85 additions
and
14 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,44 @@ | ||
type Headers = { [k: string]: string } | ||
|
||
const isBuffer = Buffer.isBuffer | ||
const isString = (a: any): a is string => typeof a === 'string' | ||
|
||
const isContentEncoded = (headers: Headers) => { | ||
const encoding = headers['content-encoding'] | ||
return isString(encoding) && encoding !== '' | ||
} | ||
|
||
const isBinaryBuffer = (buffer: Buffer) => { | ||
if (!isBuffer(buffer)) return false | ||
|
||
const reconstructedBuffer = new Buffer(buffer.toString('utf8'), 'utf8') | ||
if (buffer.length !== reconstructedBuffer.length) return true | ||
|
||
for (var i = 0; i < buffer.length; ++i) | ||
if (buffer[i] !== reconstructedBuffer[i]) | ||
return true | ||
|
||
return false | ||
} | ||
|
||
const mergeBuffers = (chunks: Buffer[]) => { | ||
if (chunks.length === 0) return new Buffer(0) | ||
if (!isBuffer(chunks[0])) return new Buffer(chunks.join(''), 'utf8') | ||
return Buffer.concat(chunks) | ||
} | ||
|
||
const processEncodedBuffers = (chunks: Buffer[]) => chunks.map(c => { | ||
if (!isBuffer(c) && isString(c)) c = new Buffer(c) | ||
return c.toString('hex') | ||
}) | ||
|
||
export default (chunks: Buffer[], headers: Headers = {}) => { | ||
if (isContentEncoded(headers)) return processEncodedBuffers(chunks) | ||
const mergedBuffers = mergeBuffers(chunks) | ||
if (isBinaryBuffer(mergedBuffers)) return mergedBuffers.toString('hex') | ||
|
||
const stringified = mergedBuffers.toString('utf8') | ||
if (stringified.length === 0) return {} | ||
try { return JSON.parse(stringified) } | ||
catch (errr) { return stringified } | ||
} |
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 |
---|---|---|
@@ -1,14 +1,39 @@ | ||
import * as I from '../interfaces' | ||
import * as Request from './request' | ||
import * as Response from './response' | ||
import chunksToBody from '../common/chunks-to-body' | ||
|
||
const getScope = ({ host, __https__, port }: I.Options) => { | ||
let scope = `${__https__ ? 'https' : 'http'}://${host}` | ||
const isDefaultPort = port.toString() === (__https__ ? '443' : '80') | ||
const needPort = !host.includes(':') && port && isDefaultPort | ||
return needPort ? `${scope}:${port}` : scope | ||
} | ||
|
||
const generatePlayback = ( | ||
req: I.Request, | ||
res: I.Response, | ||
options: I.Options, | ||
requestBody: Buffer[], | ||
responseBody: Buffer[] | ||
) => ({ | ||
scope: getScope(options), | ||
path: options.path, | ||
method: options.method || 'GET', | ||
status: res.statusCode, | ||
body: chunksToBody(requestBody), | ||
response: chunksToBody(responseBody, res.headers), | ||
rawHeaders: res.rawHeaders || res.headers | ||
}) | ||
|
||
export default async (protocol: string, options: I.Options, req: I.Request, res: I.Response) => { | ||
const requestBody: Buffer[] = [] | ||
const responseBody: Buffer[] = [] | ||
Request.onData(req, chunk => requestBody.push(chunk)) | ||
Response.onData(res, chunk => responseBody.push(chunk)) | ||
Request.onData(req, requestBody.push.bind(requestBody)) | ||
Response.onData(res, responseBody.push.bind(responseBody)) | ||
await Response.waitEnd(res) | ||
|
||
console.dir(Buffer.concat(requestBody).toString('utf8')) | ||
console.dir(Buffer.concat(responseBody).toString('utf8')) | ||
console.log(options.host) | ||
const playback = generatePlayback(req, res, options, requestBody, responseBody) | ||
console.dir(playback, {colors: true, depth: 5}) | ||
} |
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