This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): patch outgoing http requests to capture the zone (#430)
* feat(node): patch outgoing http requests to capture the zone * feat(node): patch all EventEmitters
- Loading branch information
Showing
6 changed files
with
216 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {makeZoneAwareAddListener, makeZoneAwareListeners, makeZoneAwareRemoveListener, patchMethod} from '../common/utils'; | ||
|
||
|
||
// For EventEmitter | ||
const EE_ADD_LISTENER = 'addListener'; | ||
const EE_PREPEND_LISTENER = 'prependListener'; | ||
const EE_REMOVE_LISTENER = 'removeListener'; | ||
const EE_LISTENERS = 'listeners'; | ||
const EE_ON = 'on'; | ||
|
||
|
||
const zoneAwareAddListener = makeZoneAwareAddListener(EE_ADD_LISTENER, EE_REMOVE_LISTENER, false, true); | ||
const zoneAwarePrependListener = makeZoneAwareAddListener(EE_PREPEND_LISTENER, EE_REMOVE_LISTENER, false, true); | ||
const zoneAwareRemoveListener = makeZoneAwareRemoveListener(EE_REMOVE_LISTENER, false); | ||
const zoneAwareListeners = makeZoneAwareListeners(EE_LISTENERS); | ||
|
||
export function patchEventEmitterMethods(obj: any): boolean { | ||
if (obj && obj.addListener) { | ||
patchMethod(obj, EE_ADD_LISTENER, () => zoneAwareAddListener); | ||
patchMethod(obj, EE_PREPEND_LISTENER, () => zoneAwarePrependListener); | ||
patchMethod(obj, EE_REMOVE_LISTENER, () => zoneAwareRemoveListener); | ||
patchMethod(obj, EE_LISTENERS, () => zoneAwareListeners); | ||
obj[EE_ON] = obj[EE_ADD_LISTENER]; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
// EventEmitter | ||
let events; | ||
try { | ||
events = require('events'); | ||
} catch (err) {} | ||
|
||
if (events && events.EventEmitter) { | ||
patchEventEmitterMethods(events.EventEmitter.prototype); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {EventEmitter} from 'events'; | ||
|
||
describe('nodejs EventEmitter', () => { | ||
let zone, zoneA, zoneB, emitter, expectZoneACount; | ||
beforeEach(() => { | ||
zone = Zone.current; | ||
zoneA = zone.fork({ name: 'A' }); | ||
zoneB = zone.fork({ name: 'B' }); | ||
|
||
emitter = new EventEmitter(); | ||
expectZoneACount = 0; | ||
}); | ||
|
||
function expectZoneA(value) { | ||
expectZoneACount++; | ||
expect(Zone.current).toBe(zoneA); | ||
expect(value).toBe('test value'); | ||
} | ||
|
||
function shouldNotRun() { | ||
fail('this listener should not run'); | ||
} | ||
|
||
it('should register listeners in the current zone', () => { | ||
zoneA.run(() => { | ||
emitter.on('test', expectZoneA); | ||
emitter.addListener('test', expectZoneA); | ||
}); | ||
zoneB.run(() => emitter.emit('test', 'test value')); | ||
expect(expectZoneACount).toBe(2); | ||
}); | ||
it('should remove listeners properly', () => { | ||
zoneA.run(() => { | ||
emitter.on('test', shouldNotRun); | ||
emitter.on('test2', shouldNotRun); | ||
emitter.removeListener('test', shouldNotRun); | ||
}); | ||
zoneB.run(() => { | ||
emitter.removeListener('test2', shouldNotRun); | ||
emitter.emit('test', 'test value'); | ||
emitter.emit('test2', 'test value'); | ||
}); | ||
}); | ||
it('should return all listeners for an event', () => { | ||
zoneA.run(() => { | ||
emitter.on('test', expectZoneA); | ||
}); | ||
zoneB.run(() => { | ||
emitter.on('test', shouldNotRun); | ||
}); | ||
expect(emitter.listeners('test')).toEqual([expectZoneA, shouldNotRun]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ import './test-env-setup'; | |
|
||
// List all tests here: | ||
import './common_tests'; | ||
|
||
import './node_tests'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './node/events.spec'; |