Skip to content

Commit

Permalink
Remove the Underscore.js dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
moll committed Aug 22, 2024
1 parent f8d5659 commit 4d831f3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
26 changes: 22 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var _ = require("underscore")
var Net = require("net")
var Tls = require("tls")
var Http = require("http")
Expand Down Expand Up @@ -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)
))
Expand All @@ -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.
Expand Down Expand Up @@ -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 }
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down

0 comments on commit 4d831f3

Please sign in to comment.