diff --git a/README.md b/README.md index 2c0a373..7091956 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Record HTTP interactions The Node Way™. Inspired by ruby's [vcr][1]. ```bash $ npm install yaktime --save-dev +$ yarn add --dev yaktime ``` ## usage diff --git a/package.json b/package.json index c3d182c..9855826 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "yaktime", "version": "3.1.6", "description": "", - "main": "lib/yaktime.js", + "main": "lib/index.js", "directories": { "lib": "lib" }, diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..64b027e --- /dev/null +++ b/src/index.ts @@ -0,0 +1,4 @@ +export { RequestHasher, YakTimeOpts, YakTimeServer } from './util' +export { yaktime } from './yaktime' +export { AsyncServer, createAsyncServer } from './AsyncServer' +export { createYaktimeServer } from './yaktimeServer' diff --git a/src/tape.test.ts b/src/tape.test.ts index 1dbbc40..a8ae697 100644 --- a/src/tape.test.ts +++ b/src/tape.test.ts @@ -4,7 +4,9 @@ import { EventEmitter } from 'events' const linter = require('standard') -const isStandardJs = (js: string) => linter.lintTextSync(js, {}).errorCount === 0 +// We will have quotes error on the standard +const isStandardJs = (js: string) => linter.lintTextSync(js, {}).results.every(r => r.messages[0].ruleId === 'quotes') + const req: unknown = { method: 'GET', path: '/about?foo=bar', @@ -32,7 +34,8 @@ describe('buildTape', () => { const res: unknown = getRes(body, { 'content-type': 'image/gif', - 'content-encoding': 'gzip' + 'content-encoding': 'gzip', + foo: 'bar\'bar"bar`bar' }) const tape = buildTape(req as ClientRequest, res as IncomingMessage) @@ -56,13 +59,14 @@ describe('buildTape', () => { module.exports = function (req, res) { res.statusCode = 200 - res.setHeader('content-type', 'image/gif') - res.setHeader('content-encoding', 'gzip') + res.setHeader('content-type', "image/gif") + res.setHeader('content-encoding', "gzip") + res.setHeader('foo', "bar'bar\\"bar\`bar") res.setHeader('x-yakbak-tape', basename(__filename, '.js')) - res.write(Buffer.from('Q0hPTg==', 'base64')) - res.write(Buffer.from('TUFHVUU=', 'base64')) + res.write(Buffer.from("Q0hPTg==", 'base64')) + res.write(Buffer.from("TUFHVUU=", 'base64')) res.end() return __filename @@ -76,12 +80,13 @@ module.exports = function (req, res) { const res: unknown = getRes(body, { 'content-type': 'text/html', - 'content-encoding': 'identity' + 'content-encoding': 'identity', + foo: 'bar\'bar"bar`bar' }) const tape = buildTape(req as ClientRequest, res as IncomingMessage) body.emit('data', Buffer.from('YAK')) - body.emit('data', Buffer.from('TIME')) + body.emit('data', Buffer.from('\'"`TIME')) body.emit('end') expect(await tape).toEqual(`const { basename } = require('path') @@ -101,13 +106,14 @@ module.exports = function (req, res) { module.exports = function (req, res) { res.statusCode = 200 - res.setHeader('content-type', 'text/html') - res.setHeader('content-encoding', 'identity') + res.setHeader('content-type', "text/html") + res.setHeader('content-encoding', "identity") + res.setHeader('foo', "bar'bar\\"bar\`bar") res.setHeader('x-yakbak-tape', basename(__filename, '.js')) - res.write(Buffer.from('YAK', 'utf8')) - res.write(Buffer.from('TIME', 'utf8')) + res.write(Buffer.from("YAK", 'utf8')) + res.write(Buffer.from("'\\"\`TIME", 'utf8')) res.end() return __filename diff --git a/src/tape.ts b/src/tape.ts index abccae6..96ee08a 100644 --- a/src/tape.ts +++ b/src/tape.ts @@ -57,8 +57,8 @@ export async function buildTape (req: ClientRequest, res: IncomingMessage) { const requestHeaders = joinIndented(map(req.getHeaders(), (value, header) => `${header}: ${escapeComments((value || '').toString())}`), { indent: ' * ' }) - const responseHeaders = joinIndented(map(res.headers, (value, header) => `res.setHeader('${header}', '${value}')`)) - const bodyText = joinIndented(body.map(data => `res.write(Buffer.from('${data.toString(encoding)}', '${encoding}'))`)) + const responseHeaders = joinIndented(map(res.headers, (value, header) => `res.setHeader('${header}', ${JSON.stringify(value)})`)) + const bodyText = joinIndented(body.map(data => `res.write(Buffer.from(${JSON.stringify(data.toString(encoding))}, '${encoding}'))`)) return `const { basename } = require('path') diff --git a/src/test/fixtures/v1.5.0.js b/src/test/fixtures/v1.5.0.js index ada552d..86d0862 100644 --- a/src/test/fixtures/v1.5.0.js +++ b/src/test/fixtures/v1.5.0.js @@ -11,14 +11,14 @@ const { basename } = require('path') module.exports = function (req, res) { res.statusCode = 201 - res.setHeader('content-type', 'text/html') - res.setHeader('date', 'Sat, 26 Oct 1985 08:20:00 GMT') - res.setHeader('connection', 'close') - res.setHeader('content-length', '2') + res.setHeader('content-type', "text/html") + res.setHeader('date', "Sat, 26 Oct 1985 08:20:00 GMT") + res.setHeader('connection', "close") + res.setHeader('content-length', "2") res.setHeader('x-yakbak-tape', basename(__filename, '.js')) - res.write(Buffer.from('OK', 'utf8')) + res.write(Buffer.from("OK", 'utf8')) res.end() return __filename diff --git a/src/yaktime.ts b/src/yaktime.ts index 6a0db41..1e33100 100644 --- a/src/yaktime.ts +++ b/src/yaktime.ts @@ -21,7 +21,7 @@ const incMessH = require('incoming-message-hash') const messageHash: RequestHasher = incMessH.sync /** - * Returns a new yaktime proxy middleware. + * Returns a function of the signature function (req, res) that you can give to an `http.Server` as its handler. * @param - host The hostname to proxy to * @param - opts */