From 310e04db98625a19211885d4b5f8cb9723e0866e Mon Sep 17 00:00:00 2001 From: George Walker Date: Tue, 12 Sep 2023 22:55:47 -0400 Subject: [PATCH 1/4] Add mac support --- index.js | 22 ++++++++-------------- mac.js | 29 +++++++++++++++++++++++++++++ package.json | 21 ++++++++++++++++----- win.js | 18 ++++++++++++++++++ 4 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 mac.js create mode 100644 win.js diff --git a/index.js b/index.js index 6e857ff..cae6bcd 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,9 @@ -const internalSleep = require('./build/Release/node_prevent_sleep.node'); +var process = require("process"); -const preventSleep = { - _timerId: 0, - _intervalTime: 5000, - enable: () => { - preventSleep._timerId = setInterval(internalSleep.enable, preventSleep._intervalTime); - }, - disable: () => { - clearInterval(preventSleep._timerId); - internalSleep.disable(); - } -}; - -module.exports = preventSleep; +if (/^win/.test(process.platform)) { + module.exports = require("./win"); +} else if (/^darwin/.test(process.platform)) { + module.exports = require("./mac"); +} else { + throw new Error("Unsupported platform: " + process.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/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; From 2c06cfaba6f2202184edd122874ab0f959565a15 Mon Sep 17 00:00:00 2001 From: George Walker Date: Tue, 12 Sep 2023 23:06:24 -0400 Subject: [PATCH 2/4] Update readme --- readme.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 From 27c7c8755c195ec512412fb8e2dba5f60c5d082a Mon Sep 17 00:00:00 2001 From: George Walker Date: Tue, 12 Sep 2023 23:11:56 -0400 Subject: [PATCH 3/4] Remove regex --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index cae6bcd..812fe6d 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ -var process = require("process"); +var os = require("os"); -if (/^win/.test(process.platform)) { +if (os.platform === "win32") { module.exports = require("./win"); -} else if (/^darwin/.test(process.platform)) { +} else if (os.platform === "darwin") { module.exports = require("./mac"); } else { - throw new Error("Unsupported platform: " + process.platform); + throw new Error("Unsupported platform: " + os.platform); } From 620ad1cc1c81b3b099ac004ac15d7ee11785e20b Mon Sep 17 00:00:00 2001 From: George Walker Date: Wed, 13 Sep 2023 00:01:13 -0400 Subject: [PATCH 4/4] Fix compare --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 812fe6d..ff1a1e5 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,8 @@ var os = require("os"); -if (os.platform === "win32") { +if (os.platform == "win32") { module.exports = require("./win"); -} else if (os.platform === "darwin") { +} else if (os.platform == "darwin") { module.exports = require("./mac"); } else { throw new Error("Unsupported platform: " + os.platform);