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 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(async): fix #740, use async hooks to wrap nodejs async api
- Loading branch information
1 parent
b92b6e3
commit 8d64c10
Showing
7 changed files
with
251 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
Zone.__load_patch('node_async_check', (global: any, Zone: ZoneType, api: _ZonePrivate) => { | ||
try { | ||
require('async_hooks'); | ||
(process as any)._rawDebug('load async_hooks'); | ||
// nodejs 8.x with async_hooks support. | ||
// disable original Zone patch | ||
global['__Zone_disable_ZoneAwarePromise'] = true; | ||
global['__Zone_disable_node_timers'] = true; | ||
global['__Zone_disable_nextTick'] = true; | ||
global['__Zone_disable_handleUnhandledPromiseRejection'] = true; | ||
global['__Zone_disable_crypto'] = true; | ||
global['__Zone_disable_fs'] = true; | ||
} catch (err) { | ||
global['__Zone_disable_node_async_hooks'] = true; | ||
} | ||
}); |
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,91 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/** | ||
* patch nodejs async operations (timer, promise, net...) with | ||
* nodejs async_hooks | ||
*/ | ||
Zone.__load_patch('node_async_hooks', (global: any, Zone: ZoneType, api: _ZonePrivate) => { | ||
let async_hooks; | ||
const BEFORE_RUN_TASK_STATUS = 'BEFORE_RUN_TASK_STATUS'; | ||
|
||
async_hooks = require('async_hooks'); | ||
|
||
const idTaskMap: {[key: number]: Task} = (Zone as any)[Zone.__symbol__('nodeTasks')] = {}; | ||
|
||
const noop = function() {}; | ||
|
||
function init(id: number, provider: string, parentId: number, parentHandle: any) { | ||
// @JiaLiPassion, check which tasks are microTask or macroTask | ||
//(process as any)._rawDebug('init hook', id , provider); | ||
if (provider === 'TIMERWRAP') { | ||
return; | ||
} | ||
// create microTask if 'PROMISE' | ||
if (provider === 'PROMISE') { | ||
const task = idTaskMap[id] = Zone.current.scheduleMicroTask(provider, noop, null, noop); | ||
//(process as any)._rawDebug('after init', id, 'status', task.state); | ||
return; | ||
} | ||
// create macroTask in other cases | ||
if (provider === 'Timeout' || provider === 'Immediate' || provider === 'FSREQWRAP') { | ||
idTaskMap[id] = Zone.current.scheduleMacroTask(provider, noop, null, noop, noop); | ||
} | ||
} | ||
|
||
function before(id: number) { | ||
//(process as any)._rawDebug('before hook', id); | ||
// call Zone.beforeRunTask | ||
const task: Task = idTaskMap[id]; | ||
if (!task) { | ||
return; | ||
} | ||
(task as any)[Zone.__symbol__(BEFORE_RUN_TASK_STATUS)] = api.beforeRunTask(task.zone, task); | ||
} | ||
|
||
function after(id: number) { | ||
//(process as any)._rawDebug('after hook', id); | ||
const task: Task = idTaskMap[id]; | ||
if (!task) { | ||
return; | ||
} | ||
const beforeRunTask: BeforeRunTaskStatus = (task as any)[Zone.__symbol__(BEFORE_RUN_TASK_STATUS)]; | ||
if (beforeRunTask) { | ||
return; | ||
} | ||
(task as any)[Zone.__symbol__(BEFORE_RUN_TASK_STATUS)] = null; | ||
api.afterRunTask(task.zone, beforeRunTask, task); | ||
} | ||
|
||
function destroy(id: number) { | ||
// try to cancel the task if is not canceled | ||
const task: Task = idTaskMap[id]; | ||
if (task && task.state === 'scheduled') { | ||
task.zone.cancelTask(task); | ||
} | ||
idTaskMap[id] = null; | ||
} | ||
|
||
process.on('uncaughtException', (err: any) => { | ||
const task = Zone.currentTask; | ||
if (task) { | ||
const beforeRunTask: BeforeRunTaskStatus = (task as any)[Zone.__symbol__(BEFORE_RUN_TASK_STATUS)]; | ||
if (beforeRunTask) { | ||
if ((task.zone as any)._zoneDelegate.handleError(Zone.current, err)) { | ||
throw err; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
global[Zone.__symbol__('setTimeout')] = global.setTimeout; | ||
global[Zone.__symbol__('setInterval')] = global.setInterval; | ||
global[Zone.__symbol__('setImmediate')] = global.setImmediate; | ||
|
||
async_hooks.createHook({ init, before, after, destroy }).enable(); | ||
}); |
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
Oops, something went wrong.