Skip to content

Commit

Permalink
Merge upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAnthony committed Nov 1, 2021
2 parents cacd0ad + a405ef4 commit c1046db
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 36 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"env": {
"es6": true,
"browser": false,
"commonjs": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"semi": ["error", "always"]
}
}
67 changes: 53 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
node-diskusage
==============

<p align="center">
<a href="https://www.npmjs.com/package/diskusage?activeTab=dependencies"><img src="https://img.shields.io/david/jduncanator/node-diskusage.svg" alt="npm Dependencies" title="npm Dependencies" /></a>
<a href="https://www.npmjs.com/package/diskusage"><img src="https://img.shields.io/npm/v/diskusage.svg" alt="npm Version" title="npm Version" /></a>
<a href="https://npm-stat.com/charts.html?package=diskusage"><img src="https://img.shields.io/npm/dw/diskusage.svg" alt="npm Downloads" title="npm Downloads" /></a>
</p>

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
Expand All @@ -17,6 +23,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<DiskUsage>` 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)
Expand All @@ -32,24 +40,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);
}
```

Expand All @@ -58,13 +85,25 @@ TypeScript

The module has an embedded .d.ts file. You can use `import * as diskusage from 'diskusage'`.

```
```ts
type DiskUsage = {
available: number;
free: number;
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<DiskUsage>
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/
```
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
}],
['OS=="mac"', {
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++'
}
}]
]
Expand Down
44 changes: 44 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -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}
Total: ${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()
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +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<DiskUsage>
export function checkSync(path: string): DiskUsage;
34 changes: 24 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"name": "diskusage",
"version": "0.2.4-hola.0",
"version": "1.1.3-hola.0",
"description": "Get total diskspace and free diskspace using bindings around platform specific calls.",
"main": "index.js",
"typings": "index.d.ts",
"gypfile": true,
"dependencies": {
"nan": "^2.5.0"
"es6-promise": "^4.2.5",
"nan": "^2.14.0"
},
"devDependencies": {
"eslint": "^6.0.1"
},
"repository": {
"type": "git",
Expand Down
16 changes: 8 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
static v8::Local<v8::Object> ConvertDiskUsage(const DiskUsage& usage)
{
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
obj->Set(Nan::New<v8::String>("available").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.available)));
obj->Set(Nan::New<v8::String>("free").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.free)));
obj->Set(Nan::New<v8::String>("total").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.total)));
obj->Set(Nan::New<v8::String>("inodes_available").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.inodes_available)));
obj->Set(Nan::New<v8::String>("inodes_free").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.inodes_free)));
obj->Set(Nan::New<v8::String>("inodes_total").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.inodes_total)));
Nan::Set(obj, Nan::New<v8::String>("available").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.available)));
Nan::Set(obj, Nan::New<v8::String>("free").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.free)));
Nan::Set(obj, Nan::New<v8::String>("total").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.total)));
Nan::Set(obj, Nan::New<v8::String>("inodes_available").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.inodes_available)));
Nan::Set(obj, Nan::New<v8::String>("inodes_free").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.inodes_free)));
Nan::Set(pbj, Nan::New<v8::String>("inodes_total").ToLocalChecked(), Nan::New<v8::Number>(static_cast<double>(usage.inodes_total)));

return obj;
}
Expand All @@ -26,7 +26,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) {
Expand All @@ -37,7 +37,7 @@ static NAN_METHOD(GetDiskUsage)
}
}

void Init(v8::Handle<v8::Object> exports)
void Init(v8::Local<v8::Object> exports)
{
Nan::SetMethod(exports, "getDiskUsage", GetDiskUsage);
}
Expand Down

0 comments on commit c1046db

Please sign in to comment.