daemonize-process 3.0.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/index.d.ts +14 -0
- package/dist/index.js +29 -0
- package/package.json +21 -26
- package/LICENSE +0 -22
- package/index.js +0 -27
package/README.md
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
# daemonize-process
|
|
2
|
-
[](https://www.npmjs.org/package/daemonize-process) [](https://www.npmjs.org/package/daemonize-process)
|
|
2
|
+
[](https://www.npmjs.org/package/daemonize-process) [](https://www.npmjs.org/package/daemonize-process) [](https://packagephobia.com/result?p=daemonize-process)
|
|
3
3
|
|
|
4
4
|
> Daemonize the current Node.js process
|
|
5
5
|
|
|
6
6
|
The module re-spawns the current process and then exits, which will lead to the new child process being adopted by `init` or similar mechanisms, effectively putting the current process into the background.
|
|
7
7
|
|
|
8
|
-
Differences to the
|
|
8
|
+
Differences to the `daemon` module include:
|
|
9
9
|
|
|
10
10
|
- Exposes all options of `child_process.spawn`.
|
|
11
11
|
- Cleans up `process.env` after itself.
|
|
12
|
-
- Works on Node.js >= 10.
|
|
13
12
|
|
|
14
13
|
## Install
|
|
15
14
|
|
|
@@ -20,8 +19,11 @@ $ npm i daemonize-process
|
|
|
20
19
|
## Examples
|
|
21
20
|
|
|
22
21
|
```js
|
|
23
|
-
|
|
22
|
+
import {daemonizeProcess} from "daemonize-process";
|
|
23
|
+
|
|
24
24
|
daemonizeProcess();
|
|
25
|
+
|
|
26
|
+
// below here the process is daemonized
|
|
25
27
|
```
|
|
26
28
|
|
|
27
29
|
## API
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SpawnOptions } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
type DaemonizeProcessOpts = {
|
|
4
|
+
/** The path to the script to be executed. Default: The current script. */
|
|
5
|
+
script?: string;
|
|
6
|
+
/** The command line arguments to be used. Default: The current arguments. */
|
|
7
|
+
arguments?: string[];
|
|
8
|
+
/** The path to the Node.js binary to be used. Default: The current Node.js binary. */
|
|
9
|
+
node?: string;
|
|
10
|
+
/** The exit code to be used when exiting the parent process. Default: `0`. */
|
|
11
|
+
exitCode?: number;
|
|
12
|
+
} & SpawnOptions;
|
|
13
|
+
export declare function daemonizeProcess(opts?: DaemonizeProcessOpts): void;
|
|
14
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { env, cwd, execPath, argv, exit } from 'node:process';
|
|
3
|
+
|
|
4
|
+
const id = "_DAEMONIZE_PROCESS";
|
|
5
|
+
function daemonizeProcess(opts = {}) {
|
|
6
|
+
if (id in env) {
|
|
7
|
+
delete env[id];
|
|
8
|
+
} else {
|
|
9
|
+
const o = {
|
|
10
|
+
// spawn options
|
|
11
|
+
env: Object.assign(env, opts.env, { [id]: "1" }),
|
|
12
|
+
cwd: cwd(),
|
|
13
|
+
stdio: "ignore",
|
|
14
|
+
detached: true,
|
|
15
|
+
// custom options
|
|
16
|
+
node: execPath,
|
|
17
|
+
script: argv[1],
|
|
18
|
+
arguments: argv.slice(2),
|
|
19
|
+
exitCode: 0,
|
|
20
|
+
...opts
|
|
21
|
+
};
|
|
22
|
+
const args = [o.script, ...o.arguments];
|
|
23
|
+
const proc = spawn(o.node, args, o);
|
|
24
|
+
proc?.unref?.();
|
|
25
|
+
exit(o.exitCode);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { daemonizeProcess };
|
package/package.json
CHANGED
|
@@ -1,37 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "daemonize-process",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "Daemonize the current Node.js process",
|
|
5
5
|
"author": "silverwind <me@silverwind.io>",
|
|
6
6
|
"repository": "silverwind/daemonize-process",
|
|
7
7
|
"license": "BSD-2-Clause",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
"node": ">=10"
|
|
14
|
-
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"exports": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
15
13
|
"files": [
|
|
16
|
-
"
|
|
17
|
-
],
|
|
18
|
-
"keywords": [
|
|
19
|
-
"daemonize",
|
|
20
|
-
"daemon",
|
|
21
|
-
"background",
|
|
22
|
-
"process"
|
|
14
|
+
"dist"
|
|
23
15
|
],
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"eslint": "6.8.0",
|
|
27
|
-
"eslint-config-silverwind": "10.0.1",
|
|
28
|
-
"jest": "25.1.0",
|
|
29
|
-
"tempy": "0.5.0",
|
|
30
|
-
"updates": "10.2.3",
|
|
31
|
-
"versions": "8.2.5"
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=18"
|
|
32
18
|
},
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "20.12.12",
|
|
21
|
+
"eslint": "8.57.0",
|
|
22
|
+
"eslint-config-silverwind": "85.1.4",
|
|
23
|
+
"eslint-config-silverwind-typescript": "3.2.7",
|
|
24
|
+
"typescript": "5.4.5",
|
|
25
|
+
"typescript-config-silverwind": "4.3.2",
|
|
26
|
+
"updates": "16.1.1",
|
|
27
|
+
"versions": "12.0.2",
|
|
28
|
+
"vite": "5.2.11",
|
|
29
|
+
"vite-config-silverwind": "1.1.2",
|
|
30
|
+
"vite-plugin-dts": "3.9.1"
|
|
36
31
|
}
|
|
37
32
|
}
|
package/LICENSE
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
Copyright (c) silverwind
|
|
2
|
-
All rights reserved.
|
|
3
|
-
|
|
4
|
-
Redistribution and use in source and binary forms, with or without
|
|
5
|
-
modification, are permitted provided that the following conditions are met:
|
|
6
|
-
|
|
7
|
-
1. Redistributions of source code must retain the above copyright notice, this
|
|
8
|
-
list of conditions and the following disclaimer.
|
|
9
|
-
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
10
|
-
this list of conditions and the following disclaimer in the documentation
|
|
11
|
-
and/or other materials provided with the distribution.
|
|
12
|
-
|
|
13
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
14
|
-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
15
|
-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
16
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
17
|
-
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
18
|
-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
19
|
-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
20
|
-
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
21
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
22
|
-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/index.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const id = "_DAEMONIZE_PROCESS";
|
|
4
|
-
|
|
5
|
-
module.exports = function(opts) {
|
|
6
|
-
if (id in process.env) {
|
|
7
|
-
// In the child, clean up the tracking environment variable
|
|
8
|
-
delete process.env[id];
|
|
9
|
-
} else {
|
|
10
|
-
// In the parent, set the tracking environment variable, fork the child and exit
|
|
11
|
-
opts = opts || {};
|
|
12
|
-
const o = Object.assign({
|
|
13
|
-
// spawn options
|
|
14
|
-
env: Object.assign(process.env, opts.env, {[id]: "1"}),
|
|
15
|
-
cwd: process.cwd(),
|
|
16
|
-
stdio: "ignore",
|
|
17
|
-
detached: true,
|
|
18
|
-
// custom options
|
|
19
|
-
node: process.execPath,
|
|
20
|
-
script: process.argv[1],
|
|
21
|
-
arguments: process.argv.slice(2),
|
|
22
|
-
exitCode: 0,
|
|
23
|
-
}, opts);
|
|
24
|
-
require("child_process").spawn(o.node, [o.script].concat(o.arguments), o).unref();
|
|
25
|
-
process.exit(o.exitCode);
|
|
26
|
-
}
|
|
27
|
-
};
|