Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Http2 #99

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coverage
node_modules
/test/support/supertest/http2wrapper.js
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ script:
- "test -z $(npm -ps ls eslint) || npm run-script lint"
after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
matrix:
include:
- node_js: "10"
env: HTTP2_TEST=1
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ var deprecate = require('depd')('cookies')
var Keygrip = require('keygrip')
var http = require('http')
var cache = {}
var http2 = null;
try{
http2 = require('http2');
}catch(_){
}

/**
* RegExp to match field-content in RFC 7230 sec 3.2
Expand Down Expand Up @@ -111,7 +116,9 @@ Cookies.prototype.set = function(name, value, opts) {
pushCookie(headers, cookie)
}

var setHeader = res.set ? http.OutgoingMessage.prototype.setHeader : res.setHeader
var protoSetHeader = http2 && res instanceof http2.Http2ServerResponse ?
http2.Http2ServerResponse.prototype.setHeader : http.OutgoingMessage.prototype.setHeader;
var setHeader = res.set ? protoSetHeader : res.setHeader
setHeader.call(res, 'Set-Cookie', headers)
return this
};
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
},
"devDependencies": {
"eslint": "3.19.0",
"express": "4.9.8",
"express": "4.16.3",
"istanbul": "0.4.5",
"mocha": "2.5.3",
"restify": "2.8.1",
"supertest": "1.1.0"
"supertest": "2.0"
},
"files": [
"HISTORY.md",
Expand All @@ -32,6 +32,7 @@
"scripts": {
"lint": "eslint .",
"test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/",
"test-http2": "HTTP2_TEST=1 mocha --require test/support/env --reporter spec --bail --check-leaks test/",
"test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/"
}
Expand Down
10 changes: 7 additions & 3 deletions test/express.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@

var assert = require( "assert" )
, express = require( "express" )
, express = require( "./support/express" )
, http = require( "http" )
, keys = require( "keygrip" )(['a', 'b'])
, cookies = require( "../" ).express
, request = require('supertest')
, request = require('./support/supertest')

if(process.env.HTTP2_TEST){
http = require( "http2" )
}

describe('Express', function () {
var server
Expand Down Expand Up @@ -65,7 +69,7 @@ describe('Express', function () {
)
})

server = require('http').createServer(app).listen()
server = http.createServer(app).listen()
})

it('should set cookies', function (done) {
Expand Down
6 changes: 5 additions & 1 deletion test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ var assert = require( "assert" )
, http = require( "http" )
, keys = require( "keygrip" )(['a', 'b'])
, Cookies = require( "../" )
, request = require('supertest')
, request = require('./support/supertest')

if(process.env.HTTP2_TEST){
http = require( "http2" )
}

describe('HTTP', function () {
var server
Expand Down
7 changes: 5 additions & 2 deletions test/restify.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
if(Number(process.version.split('.')[0].slice(1)) >= 8){
process.EventEmitter = require('events')
}
var assert = require('assert')
, restify = require('restify')
, keys = require('keygrip')(['a', 'b'])
, http = require('http')
, Cookies = require('../')
, request = require('supertest')
, request = require('./support/supertest')

describe('Restify', function () {
if(!process.env.HTTP2_TEST) describe('Restify', function () {
var header
var server

Expand Down
25 changes: 25 additions & 0 deletions test/support/express/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var express = require('express');
var http2Req;
var http2Res;
if(process.env.HTTP2_TEST){
var http2 = require('http2');
var reqDecorator = require('./requestDecorator');
var resDecorator = require('./responseDecorator');
http2Req = reqDecorator(Object.create(http2.Http2ServerRequest.prototype));
http2Res = resDecorator(Object.create(http2.Http2ServerResponse.prototype));
}

function createApp(){
var app = express();
if(process.env.HTTP2_TEST){
app.request = Object.create(http2Req, {
app: { configurable: true, enumerable: true, writable: true, value: app }
});
app.response = Object.create(http2Res, {
app: { configurable: true, enumerable: true, writable: true, value: app }
});
}
return app;
}

module.exports = createApp;
Loading