You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

mockttp

Package Overview
Dependencies
Maintainers
1
Versions
122
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.18.0 to 0.18.1

28

dist/rules/handlers.d.ts

@@ -87,4 +87,15 @@ /**

}
interface ForwardingOptions {
targetHost: string;
updateHostHeader?: true | false | string;
}
export interface PassThroughHandlerOptions {
forwarding?: ForwardingOptions;
ignoreHostCertificateErrors?: string[];
clientCertificateHostMap?: {
[host: string]: {
pfx: Buffer;
passphrase?: string;
};
};
beforeRequest?: (req: CompletedRequest) => MaybePromise<CallbackRequestResult>;

@@ -96,3 +107,10 @@ beforeResponse?: (res: PassThroughResponse) => MaybePromise<CallbackResponseResult>;

forwardToLocation?: string;
forwarding?: ForwardingOptions;
ignoreHostCertificateErrors?: string[];
clientCertificateHostMap?: {
[host: string]: {
pfx: string;
passphrase?: string;
};
};
hasBeforeRequestCallback?: boolean;

@@ -103,7 +121,13 @@ hasBeforeResponseCallback?: boolean;

readonly type = "passthrough";
readonly forwardToLocation?: string;
readonly forwarding?: ForwardingOptions;
readonly ignoreHostCertificateErrors: string[];
readonly clientCertificateHostMap: {
[host: string]: {
pfx: Buffer;
passphrase?: string;
};
};
readonly beforeRequest?: (req: CompletedRequest) => MaybePromise<CallbackRequestResult>;
readonly beforeResponse?: (res: PassThroughResponse) => MaybePromise<CallbackResponseResult>;
constructor(options?: PassThroughHandlerOptions, forwardToLocation?: string);
constructor(options?: PassThroughHandlerOptions);
explain(): string;

@@ -110,0 +134,0 @@ handle(clientReq: OngoingRequest, clientRes: express.Response): Promise<void>;

68

dist/rules/handlers.js

@@ -299,3 +299,3 @@ "use strict";

class PassThroughHandler extends serialization_1.Serializable {
constructor(options = {}, forwardToLocation) {
constructor(options = {}) {
super();

@@ -305,14 +305,16 @@ this.type = 'passthrough';

// If a location is provided, and it's not a bare hostname, it must be parseable
if (forwardToLocation && forwardToLocation.includes('/')) {
const { protocol, hostname, port, path } = url.parse(forwardToLocation);
const { forwarding } = options;
if (forwarding && forwarding.targetHost.includes('/')) {
const { protocol, hostname, port, path } = url.parse(forwarding.targetHost);
if (path && path.trim() !== "/") {
const suggestion = url.format({ protocol, hostname, port }) ||
forwardToLocation.slice(0, forwardToLocation.indexOf('/'));
forwarding.targetHost.slice(0, forwarding.targetHost.indexOf('/'));
throw new Error(common_tags_1.stripIndent `
URLs passed to thenForwardTo cannot include a path, but "${forwardToLocation}" does. ${''}Did you mean ${suggestion}?
URLs for forwarding cannot include a path, but "${forwarding.targetHost}" does. ${''}Did you mean ${suggestion}?
`);
}
}
this.forwardToLocation = forwardToLocation;
this.forwarding = forwarding;
this.ignoreHostCertificateErrors = options.ignoreHostCertificateErrors || [];
this.clientCertificateHostMap = options.clientCertificateHostMap || {};
this.beforeRequest = options.beforeRequest;

@@ -322,4 +324,4 @@ this.beforeResponse = options.beforeResponse;

explain() {
return this.forwardToLocation
? `forward the request to ${this.forwardToLocation}`
return this.forwarding
? `forward the request to ${this.forwarding.targetHost}`
: 'pass the request through to the target host';

@@ -332,12 +334,20 @@ }

let { protocol, hostname, port, path } = url.parse(reqUrl);
if (this.forwardToLocation) {
if (!this.forwardToLocation.includes('/')) {
if (this.forwarding) {
const { targetHost, updateHostHeader } = this.forwarding;
if (!targetHost.includes('/')) {
// We're forwarding to a bare hostname
[hostname, port] = this.forwardToLocation.split(':');
[hostname, port] = targetHost.split(':');
}
else {
// We're forwarding to a fully specified URL; override the host etc, but never the path.
({ protocol, hostname, port } = url.parse(this.forwardToLocation));
({ protocol, hostname, port } = url.parse(targetHost));
}
headers['host'] = hostname + (port ? `:${port}` : '');
if (updateHostHeader === undefined || updateHostHeader === true) {
// If updateHostHeader is true, or just not specified, match the new target
headers['host'] = hostname + (port ? `:${port}` : '');
}
else if (updateHostHeader) {
// If it's an explicit custom value, use that directly.
headers['host'] = updateHostHeader;
} // Otherwise: falsey means don't touch it.
}

@@ -395,2 +405,7 @@ // Check if this request is a request loop:

const checkServerCertificate = !_.includes(this.ignoreHostCertificateErrors, hostname);
const host = hostname + ((protocol === 'https:' && port === '443') || (protocol === 'http:' && port === '80')
? '' // Omit the port if it's the default
: `:${port}` // Otherwise use it
);
const clientCert = this.clientCertificateHostMap[host] || {};
let makeRequest = protocol === 'https:' ? https.request : http.request;

@@ -415,4 +430,3 @@ let family;

return new Promise((resolve, reject) => {
let serverReq = makeRequest({
protocol,
let serverReq = makeRequest(Object.assign({ protocol,
method,

@@ -424,5 +438,3 @@ hostname,

headers,
agent,
rejectUnauthorized: checkServerCertificate
}, (serverRes) => __awaiter(this, void 0, void 0, function* () {
agent, rejectUnauthorized: checkServerCertificate }, clientCert), (serverRes) => __awaiter(this, void 0, void 0, function* () {
serverRes.once('error', reject);

@@ -554,9 +566,6 @@ let serverStatusCode = serverRes.statusCode;

}
return {
type: this.type,
forwardToLocation: this.forwardToLocation,
ignoreHostCertificateErrors: this.ignoreHostCertificateErrors,
hasBeforeRequestCallback: !!this.beforeRequest,
hasBeforeResponseCallback: !!this.beforeResponse
};
return Object.assign({ type: this.type }, this.forwarding ? {
forwardToLocation: this.forwarding.targetHost,
forwarding: this.forwarding
} : {}, { ignoreHostCertificateErrors: this.ignoreHostCertificateErrors, clientCertificateHostMap: _.mapValues(this.clientCertificateHostMap, ({ pfx, passphrase }) => ({ pfx: serialization_1.serializeBuffer(pfx), passphrase })), hasBeforeRequestCallback: !!this.beforeRequest, hasBeforeResponseCallback: !!this.beforeResponse });
}

@@ -584,7 +593,6 @@ static deserialize(data, channel) {

}
return new PassThroughHandler({
beforeRequest,
beforeResponse,
ignoreHostCertificateErrors: data.ignoreHostCertificateErrors
}, data.forwardToLocation);
return new PassThroughHandler(Object.assign({ beforeRequest,
beforeResponse }, data.forwardToLocation ? {
forwarding: { targetHost: data.forwardToLocation }
} : {}, { forwarding: data.forwarding, ignoreHostCertificateErrors: data.ignoreHostCertificateErrors, clientCertificateHostMap: _.mapValues(data.clientCertificateHostMap, ({ pfx, passphrase }) => ({ pfx: serialization_1.deserializeBuffer(pfx), passphrase })) }));
}

@@ -591,0 +599,0 @@ }

@@ -211,2 +211,4 @@ /**

* certificate errors should be ignored (none, by default).
* * clientCertificateHostMap, a mapping of hosts to client certificates to use,
* in the form of { key, cert } objects (none, by default)
* * beforeRequest, a callback that will be passed the full request

@@ -258,5 +260,3 @@ * before it is passed through, and which returns an object that defines

* This method also takes options to configure how the request is passed
* through. The only option currently supported is ignoreHostCertificateErrors,
* a list of hostnames for which server certificate errors should
* be ignored (none, by default).
* through, see thenPassThrough for more details.
*

@@ -271,3 +271,5 @@ * Calling this method registers the rule with the server, so it

*/
thenForwardTo(forwardToLocation: string, options?: PassThroughHandlerOptions): Promise<MockedEndpoint>;
thenForwardTo(forwardToLocation: string, options?: Omit<PassThroughHandlerOptions, 'forwarding'> & {
forwarding?: Omit<PassThroughHandlerOptions['forwarding'], 'targetHost'>;
}): Promise<MockedEndpoint>;
/**

@@ -274,0 +276,0 @@ * Close connections that match this rule immediately, without

@@ -284,2 +284,4 @@ "use strict";

* certificate errors should be ignored (none, by default).
* * clientCertificateHostMap, a mapping of hosts to client certificates to use,
* in the form of { key, cert } objects (none, by default)
* * beforeRequest, a callback that will be passed the full request

@@ -338,5 +340,3 @@ * before it is passed through, and which returns an object that defines

* This method also takes options to configure how the request is passed
* through. The only option currently supported is ignoreHostCertificateErrors,
* a list of hostnames for which server certificate errors should
* be ignored (none, by default).
* through, see thenPassThrough for more details.
*

@@ -351,3 +351,3 @@ * Calling this method registers the rule with the server, so it

*/
thenForwardTo(forwardToLocation, options) {
thenForwardTo(forwardToLocation, options = {}) {
return __awaiter(this, void 0, void 0, function* () {

@@ -357,3 +357,3 @@ const rule = {

completionChecker: this.completionChecker,
handler: new handlers_1.PassThroughHandler(options, forwardToLocation)
handler: new handlers_1.PassThroughHandler(Object.assign({}, options, { forwarding: Object.assign({}, options.forwarding, { targetHost: forwardToLocation }) }))
};

@@ -360,0 +360,0 @@ return this.addRule(rule);

@@ -69,3 +69,3 @@ "use strict";

if (!options.https) {
return destroyable_server_1.default(http.createServer(requestListener));
return destroyable_server_1.destroyable(http.createServer(requestListener));
}

@@ -192,3 +192,3 @@ const ca = yield tls_1.getCA(options.https);

}
return destroyable_server_1.default(server);
return destroyable_server_1.destroyable(server);
});

@@ -195,0 +195,0 @@ }

@@ -58,3 +58,3 @@ "use strict";

});
this.app.use(bodyParser.json());
this.app.use(bodyParser.json({ limit: '50mb' }));
this.app.post('/start', (req, res) => __awaiter(this, void 0, void 0, function* () {

@@ -110,3 +110,3 @@ if (this.debug)

yield new Promise((resolve, reject) => {
this.server = destroyable_server_1.default(this.app.listen(listenOptions, resolve));
this.server = destroyable_server_1.destroyable(this.app.listen(listenOptions, resolve));
this.server.on('upgrade', (req, socket, head) => {

@@ -189,5 +189,3 @@ let isSubscriptionRequest = req.url.match(/^\/server\/(\d+)\/subscription$/);

});
mockServerRouter.use(bodyParser.json(), apollo_server_express_1.graphqlExpress({
schema
}));
mockServerRouter.use(apollo_server_express_1.graphqlExpress({ schema }));
return {

@@ -194,0 +192,0 @@ mockPort,

@@ -9,2 +9,2 @@ /**

}
export default function destroyable(server: net.Server): DestroyableServer;
export declare function destroyable(server: net.Server): DestroyableServer;

@@ -23,18 +23,19 @@ "use strict";

});
server.destroy = function () {
return new Promise((resolve, reject) => {
server.close((err) => {
if (err)
reject(err);
else
resolve();
return Object.assign(server, {
destroy: () => {
return new Promise((resolve, reject) => {
server.close((err) => {
if (err)
reject(err);
else
resolve();
});
for (let key in connections) {
connections[key].destroy();
}
});
for (let key in connections) {
connections[key].destroy();
}
});
};
return server;
}
});
}
exports.default = destroyable;
exports.destroyable = destroyable;
//# sourceMappingURL=destroyable-server.js.map

@@ -41,2 +41,4 @@ /// <reference types="node" />

}
export declare function serializeBuffer(buffer: Buffer): string;
export declare function deserializeBuffer(buffer: string): Buffer;
export declare function withSerializedBodyReader<T extends {

@@ -43,0 +45,0 @@ body: CompletedBody;

@@ -174,2 +174,10 @@ "use strict";

exports.ClientServerChannel = ClientServerChannel;
function serializeBuffer(buffer) {
return buffer.toString('base64');
}
exports.serializeBuffer = serializeBuffer;
function deserializeBuffer(buffer) {
return Buffer.from(buffer, 'base64');
}
exports.deserializeBuffer = deserializeBuffer;
function withSerializedBodyReader(input) {

@@ -181,3 +189,3 @@ return Object.assign({}, input, { body: input.body.buffer.toString('base64') });

return Object.assign({}, input, {
body: request_utils_1.buildBodyReader(Buffer.from(input.body, 'base64'), input.headers)
body: request_utils_1.buildBodyReader(deserializeBuffer(input.body), input.headers)
});

@@ -192,6 +200,6 @@ }

else if (_.isString(input.body)) {
serializedBody = Buffer.from(input.body).toString('base64');
serializedBody = serializeBuffer(Buffer.from(input.body));
}
else if (_.isBuffer(input.body)) {
serializedBody = input.body.toString('base64');
serializedBody = serializeBuffer(input.body);
}

@@ -202,3 +210,3 @@ else if (_.isArrayBuffer(input.body) || _.isTypedArray(input.body)) {

else if (input.body.hasOwnProperty('decodedBuffer')) {
serializedBody = input.body.buffer.toString('base64');
serializedBody = serializeBuffer(input.body.buffer);
}

@@ -205,0 +213,0 @@ return Object.assign({}, input, { body: serializedBody });

{
"name": "mockttp",
"version": "0.18.0",
"version": "0.18.1",
"description": "Mock HTTP server for testing HTTP clients and stubbing webservices",

@@ -73,2 +73,3 @@ "main": "dist/main.js",

"chai-fetch": "^0.3.1",
"fs-extra": "^8.1.0",
"http-proxy-agent": "^2.0.0",

@@ -83,2 +84,3 @@ "https-proxy-agent": "^2.2.1",

"mocha": "^3.0.2",
"null-loader": "^3.0.0",
"opn": "^5.1.0",

@@ -85,0 +87,0 @@ "request": "^2.75.0",

@@ -31,3 +31,5 @@ /**

withDeserializedBodyBuffer,
WithSerializedBodyBuffer
WithSerializedBodyBuffer,
serializeBuffer,
deserializeBuffer
} from "../util/serialization";

@@ -338,4 +340,13 @@ import { MaybePromise, Replace } from '../util/type-utils';

interface ForwardingOptions {
targetHost: string,
updateHostHeader?: true | false | string // Change automatically/ignore/change to custom value
}
export interface PassThroughHandlerOptions {
forwarding?: ForwardingOptions,
ignoreHostCertificateErrors?: string[];
clientCertificateHostMap?: {
[host: string]: { pfx: Buffer, passphrase?: string }
};
beforeRequest?: (req: CompletedRequest) => MaybePromise<CallbackRequestResult>;

@@ -348,3 +359,5 @@ beforeResponse?: (res: PassThroughResponse) => MaybePromise<CallbackResponseResult>;

forwardToLocation?: string;
forwarding?: ForwardingOptions;
ignoreHostCertificateErrors?: string[];
clientCertificateHostMap?: { [host: string]: { pfx: string, passphrase?: string } };

@@ -476,4 +489,8 @@ hasBeforeRequestCallback?: boolean;

public readonly forwardToLocation?: string;
public readonly forwarding?: ForwardingOptions;
public readonly ignoreHostCertificateErrors: string[] = [];
public readonly clientCertificateHostMap: {
[host: string]: { pfx: Buffer, passphrase?: string }
};

@@ -483,13 +500,14 @@ public readonly beforeRequest?: (req: CompletedRequest) => MaybePromise<CallbackRequestResult>;

constructor(options: PassThroughHandlerOptions = {}, forwardToLocation?: string) {
constructor(options: PassThroughHandlerOptions = {}) {
super();
// If a location is provided, and it's not a bare hostname, it must be parseable
if (forwardToLocation && forwardToLocation.includes('/')) {
const { protocol, hostname, port, path } = url.parse(forwardToLocation);
const { forwarding } = options;
if (forwarding && forwarding.targetHost.includes('/')) {
const { protocol, hostname, port, path } = url.parse(forwarding.targetHost);
if (path && path.trim() !== "/") {
const suggestion = url.format({ protocol, hostname, port }) ||
forwardToLocation.slice(0, forwardToLocation.indexOf('/'));
forwarding.targetHost.slice(0, forwarding.targetHost.indexOf('/'));
throw new Error(stripIndent`
URLs passed to thenForwardTo cannot include a path, but "${forwardToLocation}" does. ${''
URLs for forwarding cannot include a path, but "${forwarding.targetHost}" does. ${''
}Did you mean ${suggestion}?

@@ -499,4 +517,7 @@ `);

}
this.forwardToLocation = forwardToLocation;
this.forwarding = forwarding;
this.ignoreHostCertificateErrors = options.ignoreHostCertificateErrors || [];
this.clientCertificateHostMap = options.clientCertificateHostMap || {};

@@ -508,4 +529,4 @@ this.beforeRequest = options.beforeRequest;

explain() {
return this.forwardToLocation
? `forward the request to ${this.forwardToLocation}`
return this.forwarding
? `forward the request to ${this.forwarding.targetHost}`
: 'pass the request through to the target host';

@@ -519,11 +540,19 @@ }

if (this.forwardToLocation) {
if (!this.forwardToLocation.includes('/')) {
if (this.forwarding) {
const { targetHost, updateHostHeader } = this.forwarding;
if (!targetHost.includes('/')) {
// We're forwarding to a bare hostname
[hostname, port] = this.forwardToLocation.split(':');
[hostname, port] = targetHost.split(':');
} else {
// We're forwarding to a fully specified URL; override the host etc, but never the path.
({ protocol, hostname, port } = url.parse(this.forwardToLocation));
({ protocol, hostname, port } = url.parse(targetHost));
}
headers['host'] = hostname + (port ? `:${port}` : '');
if (updateHostHeader === undefined || updateHostHeader === true) {
// If updateHostHeader is true, or just not specified, match the new target
headers['host'] = hostname + (port ? `:${port}` : '');
} else if (updateHostHeader) {
// If it's an explicit custom value, use that directly.
headers['host'] = updateHostHeader;
} // Otherwise: falsey means don't touch it.
}

@@ -596,2 +625,8 @@

const checkServerCertificate = !_.includes(this.ignoreHostCertificateErrors, hostname);
const host = hostname + (
(protocol === 'https:' && port === '443') || (protocol === 'http:' && port === '80')
? '' // Omit the port if it's the default
: `:${port}` // Otherwise use it
);
const clientCert = this.clientCertificateHostMap[host] || {};

@@ -628,3 +663,4 @@ let makeRequest = protocol === 'https:' ? https.request : http.request;

agent,
rejectUnauthorized: checkServerCertificate
rejectUnauthorized: checkServerCertificate,
...clientCert
}, async (serverRes) => {

@@ -787,4 +823,10 @@ serverRes.once('error', reject);

type: this.type,
forwardToLocation: this.forwardToLocation,
...this.forwarding ? {
forwardToLocation: this.forwarding.targetHost,
forwarding: this.forwarding
} : {},
ignoreHostCertificateErrors: this.ignoreHostCertificateErrors,
clientCertificateHostMap: _.mapValues(this.clientCertificateHostMap,
({ pfx, passphrase }) => ({ pfx: serializeBuffer(pfx), passphrase })
),
hasBeforeRequestCallback: !!this.beforeRequest,

@@ -833,4 +875,12 @@ hasBeforeResponseCallback: !!this.beforeResponse

beforeResponse,
ignoreHostCertificateErrors: data.ignoreHostCertificateErrors
}, data.forwardToLocation);
// Backward compat for old clients:
...data.forwardToLocation ? {
forwarding: { targetHost: data.forwardToLocation }
} : {},
forwarding: data.forwarding,
ignoreHostCertificateErrors: data.ignoreHostCertificateErrors,
clientCertificateHostMap: _.mapValues(data.clientCertificateHostMap,
({ pfx, passphrase }) => ({ pfx: deserializeBuffer(pfx), passphrase })
),
});
}

@@ -837,0 +887,0 @@ }

@@ -392,2 +392,4 @@ /**

* certificate errors should be ignored (none, by default).
* * clientCertificateHostMap, a mapping of hosts to client certificates to use,
* in the form of { key, cert } objects (none, by default)
* * beforeRequest, a callback that will be passed the full request

@@ -448,5 +450,3 @@ * before it is passed through, and which returns an object that defines

* This method also takes options to configure how the request is passed
* through. The only option currently supported is ignoreHostCertificateErrors,
* a list of hostnames for which server certificate errors should
* be ignored (none, by default).
* through, see thenPassThrough for more details.
*

@@ -461,7 +461,18 @@ * Calling this method registers the rule with the server, so it

*/
async thenForwardTo(forwardToLocation: string, options?: PassThroughHandlerOptions): Promise<MockedEndpoint> {
async thenForwardTo(
forwardToLocation: string,
options: Omit<PassThroughHandlerOptions, 'forwarding'> & {
forwarding?: Omit<PassThroughHandlerOptions['forwarding'], 'targetHost'>
} = {}
): Promise<MockedEndpoint> {
const rule: MockRuleData = {
matchers: this.matchers,
completionChecker: this.completionChecker,
handler: new PassThroughHandler(options, forwardToLocation)
handler: new PassThroughHandler({
...options,
forwarding: {
...options.forwarding,
targetHost: forwardToLocation
}
})
};

@@ -468,0 +479,0 @@

@@ -8,3 +8,3 @@ import _ = require('lodash');

import { TlsRequest } from '../types';
import destroyable, { DestroyableServer } from '../util/destroyable-server';
import { destroyable, DestroyableServer } from '../util/destroyable-server';
import { getCA, CAOptions } from '../util/tls';

@@ -11,0 +11,0 @@ import { peekFirstByte, mightBeTLSHandshake } from '../util/socket-util';

@@ -25,3 +25,3 @@ /**

import destroyable, { DestroyableServer } from "../util/destroyable-server";
import { destroyable, DestroyableServer } from "../util/destroyable-server";
import MockttpServer from "../server/mockttp-server";

@@ -64,4 +64,5 @@ import { buildStandaloneModel } from "./standalone-model";

});
this.app.use(bodyParser.json());
this.app.use(bodyParser.json({ limit: '50mb' }));
this.app.post('/start', async (req, res) => {

@@ -230,5 +231,3 @@ if (this.debug) console.log('Standalone starting mock server on port', req.query.port);

mockServerRouter.use(bodyParser.json(), graphqlExpress({
schema
}));
mockServerRouter.use(graphqlExpress({ schema }));

@@ -235,0 +234,0 @@ return {

@@ -13,35 +13,38 @@ /**

// Mostly from https://github.com/isaacs/server-destroy (which seems to be unmaintained)
export default function destroyable(server: net.Server): DestroyableServer {
const connections: { [key: string]: net.Socket } = {};
export function destroyable(server: net.Server): DestroyableServer {
const connections: { [key: string]: net.Socket } = {};
server.on('connection', function(conn: net.Socket) {
const key = conn.remoteAddress + ':' + conn.remotePort;
connections[key] = conn;
conn.on('close', function() {
delete connections[key];
server.on('connection', function(conn: net.Socket) {
const key = conn.remoteAddress + ':' + conn.remotePort;
connections[key] = conn;
conn.on('close', function() {
delete connections[key];
});
});
});
server.on('secureConnection', function(conn: net.Socket) {
const key = conn.remoteAddress + ':' + conn.remotePort;
connections[key] = conn;
conn.on('close', function() {
delete connections[key];
server.on('secureConnection', function(conn: net.Socket) {
const key = conn.remoteAddress + ':' + conn.remotePort;
connections[key] = conn;
conn.on('close', function() {
delete connections[key];
});
});
});
(<DestroyableServer> server).destroy = function() {
return new Promise<void>((resolve, reject) => {
server.close((err: any) => {
if (err) reject(err);
else resolve();
});
return Object.assign(
server,
{
destroy: () => {
return new Promise<void>((resolve, reject) => {
server.close((err: any) => {
if (err) reject(err);
else resolve();
});
for (let key in connections) {
connections[key].destroy();
}
});
};
return (<DestroyableServer> server);
for (let key in connections) {
connections[key].destroy();
}
});
}
}
);
}

@@ -236,2 +236,10 @@ import * as _ from 'lodash';

export function serializeBuffer(buffer: Buffer): string {
return buffer.toString('base64');
}
export function deserializeBuffer(buffer: string): Buffer {
return Buffer.from(buffer, 'base64');
}
export function withSerializedBodyReader<T extends {

@@ -247,3 +255,3 @@ body: CompletedBody

return <T> Object.assign({}, input as Omit<T, 'body'>, {
body: buildBodyReader(Buffer.from(input.body, 'base64'), input.headers)
body: buildBodyReader(deserializeBuffer(input.body), input.headers)
})

@@ -260,9 +268,9 @@ }

} else if (_.isString(input.body)) {
serializedBody = Buffer.from(input.body).toString('base64');
serializedBody = serializeBuffer(Buffer.from(input.body));
} else if (_.isBuffer(input.body)) {
serializedBody = input.body.toString('base64');
serializedBody = serializeBuffer(input.body as Buffer);
} else if (_.isArrayBuffer(input.body) || _.isTypedArray(input.body)) {
serializedBody = encodeBase64(input.body as ArrayBuffer);
} else if (input.body.hasOwnProperty('decodedBuffer')) {
serializedBody = input.body.buffer.toString('base64');
serializedBody = serializeBuffer(input.body.buffer);
}

@@ -269,0 +277,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc