Skip to content
Merged
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
},
"scripts": {
"lint": "eslint --plugin markdown --ext js,md .",
"test": "mocha --reporter spec --bail --check-leaks test/test.js",
"test": "mocha --reporter spec --check-leaks test/test.js",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deliberate change

"test-ci": "nyc --reporter=lcov --reporter=text npm test",
"test-cov": "nyc --reporter=html --reporter=text npm test",
"update-upstream-hashes": "node scripts/update-upstream-hashes.js",
"upstream": "mocha --reporter spec --check-leaks test/upstream.js",
"version": "node scripts/version-history.js && git add HISTORY.md"
}
Expand Down
17 changes: 17 additions & 0 deletions scripts/update-upstream-hashes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs')
const path = require('path')
const { getFunctionHash, httpServerResponsePrototype: res } = require('../scripts/upstream-common')

const updatedHashes = {
knownAppendHeaderHash: getFunctionHash(res.appendHeader),
knownRemoveHeaderHash: getFunctionHash(res.removeHeader),
knownSetHeaderHash: getFunctionHash(res.setHeader),
knownWriteHeadHash: getFunctionHash(res.writeHead)
}

const filename = 'known-upstream-hashes.json'

const filePath = path.join(__dirname, `../test/${filename}`)
fs.writeFileSync(filePath, JSON.stringify(updatedHashes, null, 2) + '\n', 'utf8')

console.log(`✅ updated '${filename}' with current method hashes.`)
12 changes: 12 additions & 0 deletions scripts/upstream-common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const crypto = require('crypto')
const http = require('http')

function getFunctionHash (fn) {
const src = fn.toString().replace(/\s+/g, '') // normalize whitespace
return crypto.createHash('sha256').update(src).digest('hex')
}

module.exports = {
getFunctionHash,
httpServerResponsePrototype: http.ServerResponse.prototype
}
6 changes: 6 additions & 0 deletions test/known-upstream-hashes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"knownAppendHeaderHash": "0deb9f70c3bba63993321cca9281fb4607e2567bed1436b8574c5b86698125a8",
"knownRemoveHeaderHash": "3ad5ccb0a858beb6268f281492bd8d42c9815f5316cc3c4f7f735e142fcd29d9",
"knownSetHeaderHash": "2d4f95e92586d28bfd4d3137a8eaacb82b255967d8c26413015c6b56daf0afe7",
"knownWriteHeadHash": "281e0d02084a69893b8c3b8692e3c7c4de2ce22a626217fcf597fa6ddf6955a9"
}
18 changes: 3 additions & 15 deletions test/upstream.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
// NOTE: this is a temporary solution to tell us if future changes to the monkey-patched methods
// could impact this package. Recognizing this is not an ideal solution, we plan to address this when
// we can drop the monkey-patching entirely.
const crypto = require('crypto')
const assert = require('assert')
const http = require('http')
const Socket = require('net').Socket
const knownHashes = require('./known-upstream-hashes.json')
const { getFunctionHash, httpServerResponsePrototype: res } = require('../scripts/upstream-common')

const req = new http.IncomingMessage(new Socket())
const res = new http.ServerResponse(req)

function getFunctionHash (fn) {
const src = fn.toString().replace(/\s+/g, '') // normalize whitespace
return crypto.createHash('sha256').update(src).digest('hex')
}

const knownWriteHeadHash = '281e0d02084a69893b8c3b8692e3c7c4de2ce22a626217fcf597fa6ddf6955a9'
const knownSetHeaderHash = '2d4f95e92586d28bfd4d3137a8eaacb82b255967d8c26413015c6b56daf0afe7'
const knownAppendHeaderHash = '0deb9f70c3bba63993321cca9281fb4607e2567bed1436b8574c5b86698125a8'
const knownRemoveHeaderHash = '3ad5ccb0a858beb6268f281492bd8d42c9815f5316cc3c4f7f735e142fcd29d9'
const { knownAppendHeaderHash, knownRemoveHeaderHash, knownSetHeaderHash, knownWriteHeadHash } = knownHashes

describe('function verification', function () {
it('should match the known function hash of writeHead', function () {
Expand Down