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

Make it work with async hooks #36

Merged
merged 1 commit into from
Feb 20, 2022
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
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Prevent loss of async hooks context

2.3.0 / 2015-05-26
==================

Expand Down
33 changes: 32 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports.isFinished = isFinished
* @private
*/

var asyncHooks = tryRequireAsyncHooks()
var first = require('ee-first')

/**
Expand Down Expand Up @@ -49,7 +50,7 @@ function onFinished (msg, listener) {
}

// attach the listener to the message
attachListener(msg, listener)
attachListener(msg, wrap(listener))

return msg
}
Expand Down Expand Up @@ -195,3 +196,33 @@ function patchAssignSocket (res, callback) {
callback(socket)
}
}

/**
* Try to require async_hooks
* @private
*/

function tryRequireAsyncHooks () {
try {
return require('async_hooks')
} catch (e) {
/* istanbul ignore next */
return {}
}
}

/**
* Wrap function with async resource
* @private
*/

function wrap (fn) {
if (!asyncHooks.AsyncResource) {
/* istanbul ignore next */
return fn
}

// AsyncResource.bind static method backported
var res = new asyncHooks.AsyncResource(fn.name || 'bound-anonymous-fn')
return res.runInAsyncScope.bind(res, fn, null)
}
125 changes: 112 additions & 13 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@

var assert = require('assert')
var asyncHooks = tryRequire('async_hooks')
var http = require('http')
var net = require('net')
var onFinished = require('..')

var describeAsyncHooks = typeof asyncHooks.AsyncLocalStorage === 'function'
? describe
: describe.skip

describe('onFinished(res, listener)', function () {
it('should invoke listener given an unknown object', function (done) {
onFinished({}, done)
Expand Down Expand Up @@ -32,15 +37,57 @@ describe('onFinished(res, listener)', function () {
sendGet(server)
})

it('should fire when called after finish', function (done) {
var server = http.createServer(function (req, res) {
onFinished(res, function () {
onFinished(res, done)
describe('when called after finish', function () {
it('should fire when called after finish', function (done) {
var server = http.createServer(function (req, res) {
onFinished(res, function () {
onFinished(res, done)
})
setTimeout(res.end.bind(res), 0)
})
setTimeout(res.end.bind(res), 0)

sendGet(server)
})

sendGet(server)
describeAsyncHooks('when async local storage', function () {
it('should presist store in callback', function (done) {
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()
var store = { foo: 'bar' }

var server = http.createServer(function (req, res) {
onFinished(res, function () {
asyncLocalStorage.run(store, function () {
onFinished(res, function () {
assert.strictEqual(asyncLocalStorage.getStore().foo, 'bar')
done()
})
})
})
setTimeout(res.end.bind(res), 0)
})

sendGet(server)
})
})
})

describeAsyncHooks('when async local storage', function () {
it('should presist store in callback', function (done) {
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()
var store = { foo: 'bar' }

var server = http.createServer(function (req, res) {
asyncLocalStorage.run(store, function () {
onFinished(res, function () {
assert.strictEqual(asyncLocalStorage.getStore().foo, 'bar')
done()
})
})
setTimeout(res.end.bind(res), 0)
})

sendGet(server)
})
})
})

Expand Down Expand Up @@ -385,16 +432,60 @@ describe('onFinished(req, listener)', function () {
sendGet(server)
})

it('should fire when called after finish', function (done) {
var server = http.createServer(function (req, res) {
onFinished(req, function () {
onFinished(req, done)
describe('when called after finish', function () {
it('should fire when called after finish', function (done) {
var server = http.createServer(function (req, res) {
onFinished(req, function () {
onFinished(req, done)
})
req.resume()
setTimeout(res.end.bind(res), 0)
})
req.resume()
setTimeout(res.end.bind(res), 0)

sendGet(server)
})

sendGet(server)
describeAsyncHooks('when async local storage', function () {
it('should presist store in callback', function (done) {
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()
var store = { foo: 'bar' }

var server = http.createServer(function (req, res) {
onFinished(req, function () {
asyncLocalStorage.run(store, function () {
onFinished(req, function () {
assert.strictEqual(asyncLocalStorage.getStore().foo, 'bar')
done()
})
})
})
req.resume()
setTimeout(res.end.bind(res), 0)
})

sendGet(server)
})
})
})

describeAsyncHooks('when async local storage', function () {
it('should presist store in callback', function (done) {
var asyncLocalStorage = new asyncHooks.AsyncLocalStorage()
var store = { foo: 'bar' }

var server = http.createServer(function (req, res) {
asyncLocalStorage.run(store, function () {
onFinished(req, function () {
assert.strictEqual(asyncLocalStorage.getStore().foo, 'bar')
done()
})
})
req.resume()
setTimeout(res.end.bind(res), 0)
})

sendGet(server)
})
})
})

Expand Down Expand Up @@ -1068,6 +1159,14 @@ function sendGet (server) {
})
}

function tryRequire (name) {
try {
return require(name)
} catch (e) {
return {}
}
}

function writeRequest (socket, chunked) {
socket.write('GET / HTTP/1.1\r\n')
socket.write('Host: localhost\r\n')
Expand Down