diff --git a/index.js b/index.js index 6e857ff..ff1a1e5 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,9 @@ -const internalSleep = require('./build/Release/node_prevent_sleep.node'); +var os = require("os"); -const preventSleep = { - _timerId: 0, - _intervalTime: 5000, - enable: () => { - preventSleep._timerId = setInterval(internalSleep.enable, preventSleep._intervalTime); - }, - disable: () => { - clearInterval(preventSleep._timerId); - internalSleep.disable(); - } -}; - -module.exports = preventSleep; +if (os.platform == "win32") { + module.exports = require("./win"); +} else if (os.platform == "darwin") { + module.exports = require("./mac"); +} else { + throw new Error("Unsupported platform: " + os.platform); +} diff --git a/mac.js b/mac.js new file mode 100644 index 0000000..8fd0e50 --- /dev/null +++ b/mac.js @@ -0,0 +1,29 @@ +var child_process = require("child_process"); +var process = require("process"); + +const preventSleep = { + _caffeinateProcess: undefined, + enable: () => { + if (preventSleep._caffeinateProcess) { + // We are already preventing sleep + return; + } + preventSleep._caffeinateProcess = child_process.spawn("caffeinate", [ + "-w " + process.pid, + ]); + preventSleep._caffeinateProcess.on("error", () => { + preventSleep._caffeinateProcess = undefined; + }); + preventSleep._caffeinateProcess.on("exit", () => { + preventSleep._caffeinateProcess = undefined; + }); + }, + disable: () => { + if (preventSleep._caffeinateProcess) { + preventSleep._caffeinateProcess.kill(); + preventSleep._caffeinateProcess = undefined; + } + }, +}; + +module.exports = preventSleep; diff --git a/package.json b/package.json index bdbd4e2..83c30e8 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,19 @@ { "name": "node-prevent-sleep", - "version": "0.0.4", + "version": "0.1.0", "main": "index.js", - "author": "Sebastian Alex ", + "contributors": [ + { + "name": "Sebastian Alex", + "email": "sebapotok@gmail.com", + "url": "https://github.com/perf2711" + }, + { + "name": "George W. Walker", + "email": "george@georgewwalker.com", + "url": "https://github.com/pkmnct" + } + ], "license": "MIT", "gypfile": true, "repository": { @@ -10,13 +21,13 @@ "url": "https://github.com/perf2711/node-prevent-sleep" }, "devDependencies": { - "node-gyp": "^6.1.0" + "node-gyp": "^9.4.0" }, "dependencies": { - "node-addon-api": "^2.0.0" + "node-addon-api": "^7.0.0" }, "scripts": { "build": "node-gyp rebuild", "clean": "node-gyp clean" } -} +} \ No newline at end of file diff --git a/readme.md b/readme.md index 348c1ab..16cde90 100644 --- a/readme.md +++ b/readme.md @@ -29,7 +29,10 @@ Not supported yet. You are welcome to submit a PR with the functionality. ### Mac -Not supported yet. You are welcome to submit a PR with the functionality. +When `enable()` is used, a child `caffeinate` process is spawned with the `-w` flag and the process id of Node. This ensures the child process is terminated if Node quits unexpectedly. + +When `disable()` is used, the `caffeinate` process is terminated. ## License + MIT diff --git a/win.js b/win.js new file mode 100644 index 0000000..7ef7506 --- /dev/null +++ b/win.js @@ -0,0 +1,18 @@ +const internalSleep = require("./build/Release/node_prevent_sleep.node"); + +const preventSleep = { + _timerId: 0, + _intervalTime: 5000, + enable: () => { + preventSleep._timerId = setInterval( + internalSleep.enable, + preventSleep._intervalTime + ); + }, + disable: () => { + clearInterval(preventSleep._timerId); + internalSleep.disable(); + }, +}; + +module.exports = preventSleep;