Skip to content

Allow to pass env variables and provide way to access to child process #292

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The `opts` argument is an object, with the following properties:

Pass the arguments that the spawned Node process should receive.

#### `env` (object)

Pass the environment variables that the spawned Node process should receive.

#### `workingDir` (string)

The base directory where profile folders will be placed.
Expand Down Expand Up @@ -83,6 +87,10 @@ Called with the exit code when the observed process exits.

Default: ()=>{} (noop)

#### `onProcessStart` (function)

Called after the node child process is spwaned.

#### `status` (function)

Is called with status messages from 0x internals - useful for logging
Expand Down
27 changes: 27 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {ChildProcess} from 'child_process'

export = zeroEks;

declare type Args = {
argv: string[],
workingDir?: string,
pathToNodeBinary?: string,
name?: string,
onPort?: string,
title?: string,
visualizeOnly?: string,
collectOnly?: boolean,
collectDelay?: number,
mapFrames?: (frames: any, profiler: any) => Array | false,
onProcessExit?: () => void,
status?: (msg: any) => void,
kernelTracing?: boolean,
outputDir?: string,
outputHtml?: string,
treeDebug?: boolean,
kernelTracingDebug?: boolean,
env?: Record<string, string>,
onProcessStart?: (process: ChildProcess) => void
}

declare function zeroEks(args: Args): Promise<string>;
5 changes: 4 additions & 1 deletion platform/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function linux (args, sudo, cb) {
'--perf-basic-prof',
'-r', path.join(__dirname, '..', 'lib', 'preload', 'soft-exit.js')
].filter(Boolean).concat(args.argv), {
stdio: ['ignore', 'inherit', 'inherit', 'ignore', 'ignore', 'pipe']
stdio: ['ignore', 'inherit', 'inherit', 'ignore', 'ignore', 'pipe'],
env: args.env
}).on('exit', function (code) {
args.onProcessExit(code)
if (code !== null && code !== 0 && code !== 143 && code !== 130) {
Expand All @@ -63,6 +64,8 @@ function linux (args, sudo, cb) {
filterInternalFunctions(perfdat)
})

if (args.onProcessStart) args.onProcessStart(proc)

const folder = getTargetFolder({ outputDir, workingDir, name, pid: proc.pid })

status('Profiling')
Expand Down
5 changes: 4 additions & 1 deletion platform/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ async function v8 (args) {
'-r', path.join(__dirname, '..', 'lib', 'preload', 'soft-exit'),
...(onPort ? ['-r', path.join(__dirname, '..', 'lib', 'preload', 'detect-port.js')] : [])
].concat(args.argv), {
stdio: ['inherit', 'pipe', 'inherit', 'pipe', 'ignore', 'pipe']
stdio: ['inherit', 'pipe', 'inherit', 'pipe', 'ignore', 'pipe'],
env: args.env
})

if (args.onProcessStart) args.onProcessStart(proc)

// Isolate log is created before command is executed
// Add pid to original args object so if command errors, external handlers can clean up
args.pid = proc.pid
Expand Down
11 changes: 8 additions & 3 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"collectDelay": {
"type": "number"
},
"kernelTracing": {
"kernelTracing": {
"type": "boolean"
},
"kernel-tracing": {
Expand All @@ -136,6 +136,11 @@
"type": "array",
"items": {}
},
"onProcessExit": {}
"env": {
"type": "object",
"properties": {}
},
"onProcessExit": {},
"onProcessStart": {}
}
}
}