From 12e25e281d69f11426d8d475e4128260467060da Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 24 Apr 2021 20:16:42 +0530 Subject: [PATCH 1/3] async_hooks,doc: replace process.stdout.fd with 1 This stops "RangeError: Maximum call stack size exceeded". --- doc/api/async_hooks.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 6463527b598815..20553a241de5dd 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -275,7 +275,7 @@ async_hooks.createHook({ init(asyncId, type, triggerAsyncId) { const eid = async_hooks.executionAsyncId(); fs.writeSync( - process.stdout.fd, + 1, `${type}(${asyncId}): trigger: ${triggerAsyncId} execution: ${eid}\n`); } }).enable(); @@ -328,7 +328,7 @@ async_hooks.createHook({ const eid = async_hooks.executionAsyncId(); const indentStr = ' '.repeat(indent); fs.writeSync( - process.stdout.fd, + 1, `${indentStr}${type}(${asyncId}):` + ` trigger: ${triggerAsyncId} execution: ${eid}\n`); }, From 6a940bbfc2735400407cc9d6661e317237fb9233 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 24 Apr 2021 21:17:24 +0530 Subject: [PATCH 2/3] cache process.stdout.fd outside init --- doc/api/async_hooks.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 20553a241de5dd..404c9691de248a 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -271,11 +271,16 @@ created, while `triggerAsyncId` shows *why* a resource was created. The following is a simple demonstration of `triggerAsyncId`: ```js +const async_hooks = require('async_hooks'); +const fs = require('fs'); + +const { fd } = process.stdout; + async_hooks.createHook({ init(asyncId, type, triggerAsyncId) { const eid = async_hooks.executionAsyncId(); fs.writeSync( - 1, + fd, `${type}(${asyncId}): trigger: ${triggerAsyncId} execution: ${eid}\n`); } }).enable(); @@ -322,29 +327,34 @@ callback to `listen()` will look like. The output formatting is slightly more elaborate to make calling context easier to see. ```js +const async_hooks = require('async_hooks'); +const fs = require('fs'); + +const { fd } = process.stdout; + let indent = 0; async_hooks.createHook({ init(asyncId, type, triggerAsyncId) { const eid = async_hooks.executionAsyncId(); const indentStr = ' '.repeat(indent); fs.writeSync( - 1, + fd, `${indentStr}${type}(${asyncId}):` + ` trigger: ${triggerAsyncId} execution: ${eid}\n`); }, before(asyncId) { const indentStr = ' '.repeat(indent); - fs.writeSync(process.stdout.fd, `${indentStr}before: ${asyncId}\n`); + fs.writeSync(fd, `${indentStr}before: ${asyncId}\n`); indent += 2; }, after(asyncId) { indent -= 2; const indentStr = ' '.repeat(indent); - fs.writeSync(process.stdout.fd, `${indentStr}after: ${asyncId}\n`); + fs.writeSync(fd, `${indentStr}after: ${asyncId}\n`); }, destroy(asyncId) { const indentStr = ' '.repeat(indent); - fs.writeSync(process.stdout.fd, `${indentStr}destroy: ${asyncId}\n`); + fs.writeSync(fd, `${indentStr}destroy: ${asyncId}\n`); }, }).enable(); From 4d4106cbe2007ef4a8e82df4eb8d703af0b41953 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 25 Apr 2021 19:16:56 +0530 Subject: [PATCH 3/3] directly use modules --- doc/api/async_hooks.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 404c9691de248a..55cc0542561334 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -271,9 +271,6 @@ created, while `triggerAsyncId` shows *why* a resource was created. The following is a simple demonstration of `triggerAsyncId`: ```js -const async_hooks = require('async_hooks'); -const fs = require('fs'); - const { fd } = process.stdout; async_hooks.createHook({ @@ -285,7 +282,7 @@ async_hooks.createHook({ } }).enable(); -require('net').createServer((conn) => {}).listen(8080); +net.createServer((conn) => {}).listen(8080); ``` Output when hitting the server with `nc localhost 8080`: @@ -327,9 +324,6 @@ callback to `listen()` will look like. The output formatting is slightly more elaborate to make calling context easier to see. ```js -const async_hooks = require('async_hooks'); -const fs = require('fs'); - const { fd } = process.stdout; let indent = 0; @@ -358,7 +352,7 @@ async_hooks.createHook({ }, }).enable(); -require('net').createServer(() => {}).listen(8080, () => { +net.createServer(() => {}).listen(8080, () => { // Let's wait 10ms before logging the server started. setTimeout(() => { console.log('>>>', async_hooks.executionAsyncId());