From 62e02403d01d92e9807bbe74d670888e21e3f66e Mon Sep 17 00:00:00 2001 From: Amila Welihinda Date: Mon, 24 Sep 2018 23:56:50 -0700 Subject: [PATCH 01/20] Update README.md (#30) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f6c5fb4..55166d7 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ TypeScript The module has an embedded .d.ts file. You can use `import * as diskusage from 'diskusage'`. -``` +```ts type DiskUsage = { available: number; free: number; From 1ac86d9c08d849af1ebfe11102c24027519432e5 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Tue, 25 Sep 2018 16:59:23 +1000 Subject: [PATCH 02/20] Version 0.2.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 94939a0..a8086a2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "diskusage", - "version": "0.2.4", + "version": "0.2.5", "description": "Get total diskspace and free diskspace using bindings around platform specific calls.", "main": "index.js", "typings": "index.d.ts", From 5e885d17d8dd699bd9678819e39c86a42bdc6107 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Thu, 1 Nov 2018 10:10:08 +1100 Subject: [PATCH 03/20] Bump nan dependency version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a8086a2..06b6b71 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "typings": "index.d.ts", "gypfile": true, "dependencies": { - "nan": "^2.5.0" + "nan": "^2.11.1" }, "repository": { "type": "git", From 1b7ef414b6eedfe8d52df52a95ba8e441fead906 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Thu, 1 Nov 2018 10:10:16 +1100 Subject: [PATCH 04/20] Version 0.2.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 06b6b71..10c8b85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "diskusage", - "version": "0.2.5", + "version": "0.2.6", "description": "Get total diskspace and free diskspace using bindings around platform specific calls.", "main": "index.js", "typings": "index.d.ts", From cde939930090926e846df24b96c1440530df0cbf Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Mon, 19 Nov 2018 01:40:23 +0100 Subject: [PATCH 05/20] Works with clang Apple LLVM version 10.0.0 (Xcode 10) (#34) --- binding.gyp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/binding.gyp b/binding.gyp index 10d09d5..9cd596d 100644 --- a/binding.gyp +++ b/binding.gyp @@ -36,7 +36,8 @@ }], ['OS=="mac"', { 'xcode_settings': { - 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES' + 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES', + 'CLANG_CXX_LIBRARY': 'libc++' } }] ] From a59460ab0a17f08a26ac15afb8123b5749d6c740 Mon Sep 17 00:00:00 2001 From: Jordon de Hoog Date: Mon, 26 Nov 2018 19:02:22 -0500 Subject: [PATCH 06/20] Feature - Add promise support (#33) Adds promise support to the library. Will use a polyfill for node versions that do not natively support promises. --- README.md | 57 ++++++++++++++++++++++++++++++++++++++++----------- demo/index.js | 44 +++++++++++++++++++++++++++++++++++++++ index.d.ts | 1 + index.js | 34 +++++++++++++++++++++--------- package.json | 1 + 5 files changed, 115 insertions(+), 22 deletions(-) create mode 100644 demo/index.js diff --git a/README.md b/README.md index 55166d7..2b538f6 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ Usage The module exposes two functions. `check` takes a path/mount point as the first argument and a callback as the second. The callback takes two arguments `err` and `info`. `err` will be an `Error` if something went wrong. `info` contains three members: `available`, `free` and `total` in bytes. +If no callback is supplied `check` will instead return a `Promise` that you can await. + - `available`: Disk space available to the current user (i.e. Linux reserves 5% for root) - `free`: Disk space physically free - `total`: Total disk space (free + used) @@ -32,24 +34,43 @@ const os = require('os'); let path = os.platform() === 'win32' ? 'c:' : '/'; +// Callbacks disk.check(path, function(err, info) { - if (err) { - console.log(err); - } else { - console.log(info.available); - console.log(info.free); - console.log(info.total); - } + if (err) { + console.log(err); + } else { + console.log(info.available); + console.log(info.free); + console.log(info.total); + } }); +// Promise +async function getFreeSpace(path) { + try { + const { free } = await disk.check(path); + console.log(`Free space: ${free}`); + return free + } catch (err) { + console.error(err) + return 0 + } +} + +// Or without using async/await +disk.check(path) + .then(info => console.log(`free: ${info.free}`)) + .catch(err => console.error(err)) + +// Synchronous try { - let info = disk.checkSync(path); - console.log(info.available); - console.log(info.free); - console.log(info.total); + let info = disk.checkSync(path); + console.log(info.available); + console.log(info.free); + console.log(info.total); } catch (err) { - console.log(err); + console.log(err); } ``` @@ -66,5 +87,17 @@ type DiskUsage = { } export function check(path: string, callback: (error: Error, result: DiskUsage) => void): void; +export function check(path: string): Promise export function checkSync(path: string): DiskUsage; ``` + +Demo +---- + +To see a demo of this library see the `demo/` folder. + +You can run it with node: (node 8+ required) + +```bash +node ./demo/ +``` diff --git a/demo/index.js b/demo/index.js new file mode 100644 index 0000000..f962e2c --- /dev/null +++ b/demo/index.js @@ -0,0 +1,44 @@ +const { check, checkSync } = require("../"); +const os = require("os"); + +const targetPath = os.platform() === "win32" ? "c:" : "/"; + +function printResults(type, { available, free, total }) { + console.log(`${type} + Available: ${available} + Free: ${free} + Tocal: ${total} + `); +} + +async function getFreeSpacePromise(path) { + try { + const info = await check(path); + printResults("PROMISE", info); + } catch (err) { + console.error(err); + } +} + +function getFreeSpaceCallback(path) { + check(path, (err, info) => { + if (err) { + console.error(err); + } else { + printResults("CALLBACK", info); + } + }); +} + +function getFreeSpaceSync(path) { + const info = checkSync(path); + printResults("SYNC", info); +} + +async function start() { + await getFreeSpacePromise(targetPath); + getFreeSpaceCallback(targetPath); + getFreeSpaceSync(targetPath); +} + +start() \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 138bcd0..0633bce 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,4 +5,5 @@ type DiskUsage = { } export function check(path: string, callback: (error: Error, result: DiskUsage) => void): void; +export function check(path: string): Promise export function checkSync(path: string): DiskUsage; diff --git a/index.js b/index.js index 03ac0b2..02808a7 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,29 @@ -var native = require('./build/Release/diskusage.node'); +var native = require("./build/Release/diskusage.node"); +var promise = typeof Promise !== "undefined" ? Promise : require("es6-promise").Promise exports.check = function(path, callback) { - var result = undefined; - var error = undefined; - try { - result = native.getDiskUsage(path); - } - catch (error_) { - error = error_ - } - callback(error, result); + if (callback) { + return check(path, callback); + } + + return new promise(function (resolve, reject) { + check(path, function (err, result) { + err ? reject(err) : resolve(result) + }) + }) }; exports.checkSync = native.getDiskUsage; + +function check(path, callback) { + var result = undefined; + var error = undefined; + + try { + result = native.getDiskUsage(path); + } catch (error_) { + error = error_; + } + + callback(error, result); +} diff --git a/package.json b/package.json index 10c8b85..6d31bba 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "typings": "index.d.ts", "gypfile": true, "dependencies": { + "es6-promise": "^4.2.5", "nan": "^2.11.1" }, "repository": { From 0c147fb7e722404ecbde773a5deec1824b5971da Mon Sep 17 00:00:00 2001 From: jduncanator Date: Mon, 3 Dec 2018 15:55:30 +1100 Subject: [PATCH 07/20] Version 1.0.0 Thought it was about time for version 1.0.0. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6d31bba..a20cbb4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "diskusage", - "version": "0.2.6", + "version": "1.0.0", "description": "Get total diskspace and free diskspace using bindings around platform specific calls.", "main": "index.js", "typings": "index.d.ts", From 587fcf4ca7ceb6515f68787363f5d7d7ff83dfa2 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Tue, 9 Apr 2019 10:45:31 +1000 Subject: [PATCH 08/20] Update callback TypeScript definition --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 0633bce..9f24300 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4,6 +4,6 @@ type DiskUsage = { total: number; } -export function check(path: string, callback: (error: Error, result: DiskUsage) => void): void; +export function check(path: string, callback: (error?: Error, result?: DiskUsage) => void): void; export function check(path: string): Promise export function checkSync(path: string): DiskUsage; From aedecd861a623e9d30cb896d3df63934cb45e6e4 Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 18 Apr 2019 00:33:33 -0500 Subject: [PATCH 09/20] fix: support Node 12 / Electron 5 (#36) Replace `v8::Handle` with `v8::Local` This should not affect behavior in any way since `Handle` was an alias for `Local`. From `https://v8docs.nodesource.com/node-10.6/d4/da0/v8_8h_source.html#l00342`: ``` 340 // Handle is an alias for Local for historical reasons. 341 template 342 using Handle = Local; 343 #endif ``` See also: * https://electronjs.org/blog/nodejs-native-addons-and-electron-5 * https://codereview.chromium.org/1224623004 * https://v8docs.nodesource.com/node-10.6/d2/dc3/namespacev8.html#a569580ff6260b59779fb214d5a1448fd --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index a05e579..ef7d7db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,7 +34,7 @@ static NAN_METHOD(GetDiskUsage) } } -void Init(v8::Handle exports) +void Init(v8::Local exports) { Nan::SetMethod(exports, "getDiskUsage", GetDiskUsage); } From 499b47ecb969c81ddf112b0fe0f7d3ef68904ca7 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Thu, 18 Apr 2019 15:37:36 +1000 Subject: [PATCH 10/20] Version 1.1.0 --- README.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2b538f6..ac924ab 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ type DiskUsage = { total: number; } -export function check(path: string, callback: (error: Error, result: DiskUsage) => void): void; +export function check(path: string, callback: (error?: Error, result?: DiskUsage) => void): void; export function check(path: string): Promise export function checkSync(path: string): DiskUsage; ``` diff --git a/package.json b/package.json index a20cbb4..9d22c83 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "diskusage", - "version": "1.0.0", + "version": "1.1.0", "description": "Get total diskspace and free diskspace using bindings around platform specific calls.", "main": "index.js", "typings": "index.d.ts", From a302c2112f2057a1d9bb9b9d12f25e865ae70951 Mon Sep 17 00:00:00 2001 From: Jacob Date: Mon, 29 Apr 2019 19:16:41 -0500 Subject: [PATCH 11/20] fix: use Nan::Utf8String instead of v8::String::Utf8Value (#38) This fixes another incompatibility problem with node v12.0.0: ``` node_modules\diskusage\src\main.cpp(26): error C2440: '': cannot convert from 'v8::Local' to 'v8::String::Utf8Value' [C:\Users\User\Documents\tmp\node_modules\diskusage\build\diskusage.vcxproj] c:\users\User\documents\tmp\node_modules\diskusage\src\main.cpp(26): note: No constructor could take the source type, or constructor overload resolution was ambiguous ``` --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index ef7d7db..47f7b1e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ static NAN_METHOD(GetDiskUsage) Nan::HandleScope scope; try { - DiskUsage result = GetDiskUsage(*v8::String::Utf8Value(info[0])); + DiskUsage result = GetDiskUsage(*Nan::Utf8String(info[0])); info.GetReturnValue().Set(ConvertDiskUsage(result)); } catch (const SystemError &error) { From 2173c9c2d0ca3e2ed23b74ff513c6982fed4ac94 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Tue, 30 Apr 2019 10:20:39 +1000 Subject: [PATCH 12/20] Version 1.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9d22c83..bcffe56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "diskusage", - "version": "1.1.0", + "version": "1.1.1", "description": "Get total diskspace and free diskspace using bindings around platform specific calls.", "main": "index.js", "typings": "index.d.ts", From fa7d48fa5ceaa7e75e4e9d25c1d5216695e31042 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Thu, 2 May 2019 07:18:46 +1000 Subject: [PATCH 13/20] Add README badges --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index ac924ab..421f6a6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ node-diskusage ============== +

+ npm Dependencies + npm Version + npm Downloads +

+ This module implements platform specific bindings to obtain disk usage information on Windows and POSIX platforms. Windows support is backed by [GetDiskFreeSpaceEx](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937/) and POSIX is implemented with [statvfs](http://www.freebsd.org/cgi/man.cgi?query=statvfs). Installation From 5778dc7c1cc81d7e42ee75ccfce9bf60dc0817a5 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Thu, 2 May 2019 07:24:05 +1000 Subject: [PATCH 14/20] Add badge links --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 421f6a6..db7fa5c 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ node-diskusage ==============

- npm Dependencies - npm Version - npm Downloads + npm Dependencies + npm Version + npm Downloads

This module implements platform specific bindings to obtain disk usage information on Windows and POSIX platforms. Windows support is backed by [GetDiskFreeSpaceEx](http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937/) and POSIX is implemented with [statvfs](http://www.freebsd.org/cgi/man.cgi?query=statvfs). From fdaaf8f23d61ad91501e40423c8342a64dab9114 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Mon, 17 Jun 2019 17:21:08 +1000 Subject: [PATCH 15/20] Bump nan version to 2.13.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bcffe56..e9c2e77 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "gypfile": true, "dependencies": { "es6-promise": "^4.2.5", - "nan": "^2.11.1" + "nan": "^2.13.2" }, "repository": { "type": "git", From 215da1fc23450b087bdf53f298b75a589b6e5e46 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Mon, 17 Jun 2019 17:21:33 +1000 Subject: [PATCH 16/20] Version 1.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e9c2e77..4ed1bd0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "diskusage", - "version": "1.1.1", + "version": "1.1.2", "description": "Get total diskspace and free diskspace using bindings around platform specific calls.", "main": "index.js", "typings": "index.d.ts", From e4a0fc5666e18756f28db466904249fe1754938e Mon Sep 17 00:00:00 2001 From: jduncanator Date: Fri, 28 Jun 2019 10:57:41 +1000 Subject: [PATCH 17/20] Add eslint to improve consistency --- .editorconfig | 11 +++++++++++ .eslintrc | 14 ++++++++++++++ index.js | 10 +++++----- package.json | 3 +++ 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 .editorconfig create mode 100644 .eslintrc diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4ccc916 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +charset = utf-8 +indent_size = 2 +indent_style = space +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false \ No newline at end of file diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..7ec2409 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,14 @@ +{ + "env": { + "es6": true, + "browser": false, + "commonjs": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "ecmaVersion": 2018 + }, + "rules": { + "semi": ["error", "always"] + } +} \ No newline at end of file diff --git a/index.js b/index.js index 02808a7..a0eb17e 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ var native = require("./build/Release/diskusage.node"); -var promise = typeof Promise !== "undefined" ? Promise : require("es6-promise").Promise +var promise = typeof Promise !== "undefined" ? Promise : require("es6-promise").Promise; exports.check = function(path, callback) { if (callback) { @@ -7,10 +7,10 @@ exports.check = function(path, callback) { } return new promise(function (resolve, reject) { - check(path, function (err, result) { - err ? reject(err) : resolve(result) - }) - }) + check(path, function (err, result) { + err ? reject(err) : resolve(result); + }); + }); }; exports.checkSync = native.getDiskUsage; diff --git a/package.json b/package.json index 4ed1bd0..36adce3 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "es6-promise": "^4.2.5", "nan": "^2.13.2" }, + "devDependencies": { + "eslint": "^6.0.1" + }, "repository": { "type": "git", "url": "https://github.com/jduncanator/node-diskusage.git" From f842a32c781c168274ff3ee2ffbf48286d1572b1 Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Mon, 8 Jul 2019 03:26:27 +0200 Subject: [PATCH 18/20] Avoid deprecated Set API (#45) Use `Nan::Set` instead --- package.json | 2 +- src/main.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 36adce3..6695b78 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "gypfile": true, "dependencies": { "es6-promise": "^4.2.5", - "nan": "^2.13.2" + "nan": "^2.14.0" }, "devDependencies": { "eslint": "^6.0.1" diff --git a/src/main.cpp b/src/main.cpp index 47f7b1e..b03230d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,9 +6,9 @@ static v8::Local ConvertDiskUsage(const DiskUsage& usage) { v8::Local obj = Nan::New(); - obj->Set(Nan::New("available").ToLocalChecked(), Nan::New(static_cast(usage.available))); - obj->Set(Nan::New("free").ToLocalChecked(), Nan::New(static_cast(usage.free))); - obj->Set(Nan::New("total").ToLocalChecked(), Nan::New(static_cast(usage.total))); + Nan::Set(obj, Nan::New("available").ToLocalChecked(), Nan::New(static_cast(usage.available))); + Nan::Set(obj, Nan::New("free").ToLocalChecked(), Nan::New(static_cast(usage.free))); + Nan::Set(obj, Nan::New("total").ToLocalChecked(), Nan::New(static_cast(usage.total))); return obj; } From 1382ff2e1abd621844315baefd1894dcc0de57c8 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Mon, 8 Jul 2019 11:28:07 +1000 Subject: [PATCH 19/20] Version 1.1.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6695b78..54b6020 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "diskusage", - "version": "1.1.2", + "version": "1.1.3", "description": "Get total diskspace and free diskspace using bindings around platform specific calls.", "main": "index.js", "typings": "index.d.ts", From a405ef41cac180eaaad2d387105d4d3b197fdb8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20O=2E=20Fern=C3=A1ndez=20Crisial?= Date: Sat, 10 Apr 2021 01:21:46 -0300 Subject: [PATCH 20/20] Fixing typo (#55) --- demo/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/demo/index.js b/demo/index.js index f962e2c..480a0d0 100644 --- a/demo/index.js +++ b/demo/index.js @@ -7,7 +7,7 @@ function printResults(type, { available, free, total }) { console.log(`${type} Available: ${available} Free: ${free} - Tocal: ${total} + Total: ${total} `); } @@ -41,4 +41,4 @@ async function start() { getFreeSpaceSync(targetPath); } -start() \ No newline at end of file +start()