diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c64a39..27d1aa9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## Unreleased +- Removes the [Underscore.js](https://underscorejs.org) dependency in favor of just inlining two rewritten helper functions. + ## 1.7.2 (May 1, 2021) - Increases the upper-bound on [Underscore.js](https://underscorejs.org) dependency to v1.13 (inclusive). Thanks, [Martin Caruso](https://github.com/mcaruso85), for the heads-up. diff --git a/index.js b/index.js index 28abb66..50930c9 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -var _ = require("underscore") var Net = require("net") var Tls = require("tls") var Http = require("http") @@ -66,7 +65,7 @@ Mitm.prototype.enable = function() { // ClientRequest.prototype.onSocket is called synchronously from // ClientRequest's constructor and is a convenient place to hook into new // ClientRequests. - this.stubs.stub(ClientRequest.prototype, "onSocket", _.compose( + this.stubs.stub(ClientRequest.prototype, "onSocket", compose( ClientRequest.prototype.onSocket, this.request.bind(this) )) @@ -84,7 +83,7 @@ Mitm.prototype.connect = function connect(orig, Socket, opts, done) { // Don't set client.connecting to false because there's nothing setting it // back to false later. Originally that was done in Socket.prototype.connect // and its afterConnect handler, but we're not calling that. - var client = new Socket(_.defaults({ + var client = new Socket(defaults({ handle: sockets[0], // Node v10 expects readable and writable to be set at Socket creation time. @@ -153,11 +152,30 @@ Mitm.prototype.request = function request(socket) { var self = this if (NODE_0_10) { self = Object.create(this) - self.emit = _.compose(process.nextTick, Function.bind.bind(this.emit, this)) + self.emit = compose(process.nextTick, Function.bind.bind(this.emit, this)) } createRequestAndResponse.call(self, socket.serverSocket) return socket } +function compose() { + var fns = arguments + + return function() { + var args = arguments + for (var i = fns.length - 1; i >= 0; --i) args = [fns[i].apply(this, args)] + return args[0] + } +} + +function defaults(target) { + if (target != null) for (var i = 1; i < arguments.length; ++i) { + var source = arguments[i] + for (var key in source) if (!(key in target)) target[key] = source[key] + } + + return target +} + function addCrossReferences(req, res) { req.res = res; res.req = req } diff --git a/package.json b/package.json index ba316a3..cd29848 100644 --- a/package.json +++ b/package.json @@ -33,14 +33,14 @@ "scripts": {"test": "make test"}, "dependencies": { - "underscore": ">= 1.1.6 < 1.14", "semver": ">= 5 < 6" }, "devDependencies": { "mocha": ">= 1.12.0 < 3", "must": ">= 0.13 < 0.14", - "sinon": ">= 1.9 < 2" + "sinon": ">= 1.9 < 2", + "underscore": ">= 1.1.6 < 1.14" }, "engines": {"node": ">= 0.10.24"}