Skip to content
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
22 changes: 8 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
}
29 changes: 29 additions & 0 deletions mac.js
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
{
"name": "node-prevent-sleep",
"version": "0.0.4",
"version": "0.1.0",
"main": "index.js",
"author": "Sebastian Alex <sebapotok@gmail.com>",
"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": {
"type": "git",
"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"
}
}
}
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions win.js
Original file line number Diff line number Diff line change
@@ -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;