From 939bd70637c83487c3b102932b31fdf637932347 Mon Sep 17 00:00:00 2001 From: xv2 <29576033+xv2@users.noreply.github.com> Date: Wed, 13 Mar 2019 00:07:06 +0300 Subject: [PATCH] Add `createConnection` option for http or https requests --- lib/eventsource.js | 4 ++++ test/eventsource_test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/eventsource.js b/lib/eventsource.js index a7bb478..a36b172 100644 --- a/lib/eventsource.js +++ b/lib/eventsource.js @@ -96,6 +96,10 @@ function EventSource (url, eventSourceInitDict) { // but for now exists as a backwards-compatibility layer options.rejectUnauthorized = !(eventSourceInitDict && !eventSourceInitDict.rejectUnauthorized) + if (eventSourceInitDict && eventSourceInitDict.createConnection !== undefined) { + options.createConnection = eventSourceInitDict.createConnection + } + // If specify http proxy, make the request to sent to the proxy server, // and include the original url in path and Host headers var useProxy = eventSourceInitDict && eventSourceInitDict.proxy diff --git a/test/eventsource_test.js b/test/eventsource_test.js index a547b32..52f232e 100644 --- a/test/eventsource_test.js +++ b/test/eventsource_test.js @@ -8,6 +8,7 @@ var fs = require('fs') var mocha = require('mocha') var assert = require('assert') var u = require('url') +var net = require('net') var it = mocha.it var describe = mocha.describe @@ -599,6 +600,31 @@ describe('HTTP Request', function () { }) }) }) + + it('checks createConnection option', function (done) { + createServer(function (err, server) { + if (err) return done(err) + + var testResult = false + + server.on('request', function () { + assert.ok(testResult) + server.close(done) + }) + + var urlObj = u.parse(server.url) + + new EventSource(server.url, { + createConnection: function () { + var connection = net.createConnection({ port: urlObj.port, host: urlObj.hostname }) + connection.on('connect', function () { + testResult = true + }) + return connection + } + }) + }) + }) }) describe('HTTPS Support', function () {