Skip to content

Commit

Permalink
Don't override fs module, export constants as object (#90)
Browse files Browse the repository at this point in the history
* Don't override fs module, export constants as object

Fixes #89
Fixes #81

* Desctibe breaking changes in README.md

* Bump major version in package.json
  • Loading branch information
ikokostya authored and baudehlo committed Nov 7, 2019
1 parent 42b8713 commit 3125353
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 175 deletions.
61 changes: 40 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,30 @@ fs-ext

Extras not included in Node's fs module.

**Note**: From v1.0.0 onwards, fs.utime and fs.utimeSync have been removed.
Use fs.utimes and fs.utimesSync instead.
**Note**:

* From `v2.0.0` onwards, module doesn't override `fs` and `constants` Node.js core modules. Instead
import functions and constants directly:

```js
const {flock, constants} = require('fs-ext');
// or
const fsExt = require('fs-ext');
// fsExt.flock
// fsExt.constants
```

* From `v1.0.0` onwards, fs.utime and fs.utimeSync have been removed.
Use fs.utimes and fs.utimesSync instead.

Installation
------------

Install via npm:

npm install fs-ext
```sh
npm install fs-ext
```

Usage
-----
Expand All @@ -24,22 +39,24 @@ fs-ext imports all of the methods from the core 'fs' module, so you don't
need two objects.

```js
var fs = require('fs-ext');
var fd = fs.openSync('foo.txt', 'r');
fs.flock(fd, 'ex', function (err) {
const fs = require('fs');
const {flock} = require('fs-ext');

const fd = fs.openSync('foo.txt', 'r');
flock(fd, 'ex', (err) => {
if (err) {
return console.log("Couldn't lock file");
return console.error("Couldn't lock file");
}
// file is locked
})
});
```

For an advanced example checkout `example.js`.

API
---

### fs.flock(fd, flags, [callback])
### flock(fd, flags, [callback])

Asynchronous flock(2). No arguments other than a possible error are passed to
the callback. Flags can be 'sh', 'ex', 'shnb', 'exnb', 'un' and correspond
Expand All @@ -50,11 +67,11 @@ instead: that does work over NFS, given a sufficiently recent version of Linux
and a server which supports locking.


### fs.flockSync(fd, flags)
### flockSync(fd, flags)

Synchronous flock(2). Throws an exception on error.

### fs.fcntl(fd, cmd, [arg], [callback])
### fcntl(fd, cmd, [arg], [callback])

Asynchronous fcntl(2).

Expand All @@ -74,29 +91,31 @@ and also F_RDLCK, F_WRLCK and F_UNLCK for use with F_SETLK (etc).
File locking can be used like so:

```js
fs.fcntl(fd, 'setlkw', constants.F_WRLCK, function (err, result) {
if (result != null) {
//Lock succeeded
const {fnctl, constants} = require('fs-ext');

fcntl(fd, 'setlkw', constants.F_WRLCK, (err) => {
if (!err) {
// Lock succeeded
}
});
```

### fs.fcntlSync(fd, flags)
### fcntlSync(fd, flags)

Synchronous fcntl(2). Throws an exception on error.

### fs.seek(fd, offset, whence, [callback])
### seek(fd, offset, whence, [callback])

Asynchronous lseek(2).
Asynchronous lseek(2).

callback will be given two arguments (err, currFilePos).

whence can be 0 (SEEK_SET) to set the new position in bytes to offset,
1 (SEEK_CUR) to set the new position to the current position plus offset
bytes (can be negative), or 2 (SEEK_END) to set to the end of the file
whence can be 0 (SEEK_SET) to set the new position in bytes to offset,
1 (SEEK_CUR) to set the new position to the current position plus offset
bytes (can be negative), or 2 (SEEK_END) to set to the end of the file
plus offset bytes (usually negative or zero to seek to the end of the file).

### fs.seekSync(fd, offset, whence)
### seekSync(fd, offset, whence)

Synchronous lseek(2). Throws an exception on error. Returns current
file position.
Expand Down
7 changes: 4 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var fs = require('./');
var fs = require('fs');
var {flock} = require('./');

/**
* Function that locks the current file
Expand All @@ -17,7 +18,7 @@ function getCurrentFileSize(counter, timeout, cb) {

console.log("Trying to aquire lock for the %s time", counter);

fs.flock(fd, 'exnb', function(err) {
flock(fd, 'exnb', function(err) {
if (err) {
return console.log("Couldn't lock file", counter);
}
Expand All @@ -26,7 +27,7 @@ function getCurrentFileSize(counter, timeout, cb) {

// unlock after `timeout`
setTimeout(function() {
fs.flock(fd, 'un', function(err) {
flock(fd, 'un', function(err) {
if (err) {
return console.log("Couldn't unlock file", counter);
}
Expand Down
45 changes: 25 additions & 20 deletions fs-ext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,21 @@ static void EIO_Seek(uv_work_t *req) {
#ifndef _WIN32
static void EIO_Fcntl(uv_work_t *req) {
store_data_t* data = static_cast<store_data_t *>(req->data);

struct flock lk;
lk.l_start = 0;
lk.l_len = 0;
lk.l_type = 0;
lk.l_whence = 0;
lk.l_pid = 0;

int result = -1;
if (data->oper == F_GETLK || data->oper == F_SETLK || data->oper == F_SETLKW) {
if (data->oper == F_SETLK || data->oper == F_SETLKW) {
lk.l_whence = SEEK_SET;
lk.l_type = data->arg;
}
data->result = result = fcntl(data->fd, data->oper, &lk);
data->result = result = fcntl(data->fd, data->oper, &lk);
} else {
data->result = result = fcntl(data->fd, data->oper, data->arg);
}
Expand Down Expand Up @@ -316,7 +316,7 @@ static int _win32_flock(int fd, int oper) {
return -1;
}
if (i == -1) {
if (GetLastError() == ERROR_LOCK_VIOLATION)
if (GetLastError() == ERROR_LOCK_VIOLATION)
errno = EWOULDBLOCK;
else
errno = EINVAL;
Expand Down Expand Up @@ -525,69 +525,74 @@ NAN_MODULE_INIT(init)
_set_invalid_parameter_handler(uv__crt_invalid_parameter_handler);
#endif

v8::Local<v8::Object> constants = Nan::New<v8::Object>();

#ifdef SEEK_SET
NODE_DEFINE_CONSTANT(target, SEEK_SET);
NODE_DEFINE_CONSTANT(constants, SEEK_SET);
#endif

#ifdef SEEK_CUR
NODE_DEFINE_CONSTANT(target, SEEK_CUR);
NODE_DEFINE_CONSTANT(constants, SEEK_CUR);
#endif

#ifdef SEEK_END
NODE_DEFINE_CONSTANT(target, SEEK_END);
NODE_DEFINE_CONSTANT(constants, SEEK_END);
#endif

#ifdef LOCK_SH
NODE_DEFINE_CONSTANT(target, LOCK_SH);
NODE_DEFINE_CONSTANT(constants, LOCK_SH);
#endif

#ifdef LOCK_EX
NODE_DEFINE_CONSTANT(target, LOCK_EX);
NODE_DEFINE_CONSTANT(constants, LOCK_EX);
#endif

#ifdef LOCK_NB
NODE_DEFINE_CONSTANT(target, LOCK_NB);
NODE_DEFINE_CONSTANT(constants, LOCK_NB);
#endif

#ifdef LOCK_UN
NODE_DEFINE_CONSTANT(target, LOCK_UN);
NODE_DEFINE_CONSTANT(constants, LOCK_UN);
#endif

#ifdef F_GETFD
NODE_DEFINE_CONSTANT(target, F_GETFD);
NODE_DEFINE_CONSTANT(constants, F_GETFD);
#endif

#ifdef F_SETFD
NODE_DEFINE_CONSTANT(target, F_SETFD);
NODE_DEFINE_CONSTANT(constants, F_SETFD);
#endif

#ifdef FD_CLOEXEC
NODE_DEFINE_CONSTANT(target, FD_CLOEXEC);
NODE_DEFINE_CONSTANT(constants, FD_CLOEXEC);
#endif

#ifdef F_RDLCK
NODE_DEFINE_CONSTANT(target, F_RDLCK);
NODE_DEFINE_CONSTANT(constants, F_RDLCK);
#endif

#ifdef F_WRLCK
NODE_DEFINE_CONSTANT(target, F_WRLCK);
NODE_DEFINE_CONSTANT(constants, F_WRLCK);
#endif

#ifdef F_UNLCK
NODE_DEFINE_CONSTANT(target, F_UNLCK);
NODE_DEFINE_CONSTANT(constants, F_UNLCK);
#endif

#ifdef F_SETLK
NODE_DEFINE_CONSTANT(target, F_SETLK);
NODE_DEFINE_CONSTANT(constants, F_SETLK);
#endif

#ifdef F_GETLK
NODE_DEFINE_CONSTANT(target, F_GETLK);
NODE_DEFINE_CONSTANT(constants, F_GETLK);
#endif

#ifdef F_SETLKW
NODE_DEFINE_CONSTANT(target, F_SETLKW);
NODE_DEFINE_CONSTANT(constants, F_SETLKW);
#endif

Nan::Set(target, Nan::New("constants").ToLocalChecked(), constants);

Export(target, "seek", Seek);
#ifndef _WIN32
Export(target, "fcntl", Fcntl);
Expand Down
38 changes: 11 additions & 27 deletions fs-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
"use strict";

var binding = require('./build/Release/fs-ext');
var fs = require('fs');
var constants = require('constants');

// Used by flock
function stringToFlockFlags(flag) {
Expand All @@ -31,19 +29,19 @@ function stringToFlockFlags(flag) {
}
switch (flag) {
case 'sh':
return binding.LOCK_SH;
return binding.constants.LOCK_SH;

case 'ex':
return binding.LOCK_EX;
return binding.constants.LOCK_EX;

case 'shnb':
return binding.LOCK_SH | binding.LOCK_NB;
return binding.constants.LOCK_SH | binding.constants.LOCK_NB;

case 'exnb':
return binding.LOCK_EX | binding.LOCK_NB;
return binding.constants.LOCK_EX | binding.constants.LOCK_NB;

case 'un':
return binding.LOCK_UN;
return binding.constants.LOCK_UN;

default:
throw new Error('Unknown flock flag: ' + flag);
Expand All @@ -58,19 +56,19 @@ function stringToFcntlFlags(flag) {

switch (flag) {
case 'getfd':
return binding.F_GETFD;
return binding.constants.F_GETFD;

case 'setfd':
return binding.F_SETFD;
return binding.constants.F_SETFD;

case 'setlk':
return binding.F_SETLK;
return binding.constants.F_SETLK;

case 'setlkw':
return binding.F_SETLKW;
return binding.constants.F_SETLKW;

case 'getlk':
return binding.F_GETLK;
return binding.constants.F_GETLK;

default:
throw new Error('Unknown fcntl flag: ' + flag);
Expand Down Expand Up @@ -130,18 +128,4 @@ exports.statVFS = function(path, callback) {
return binding.statVFS(path, callback);
};

// populate with fs functions from there
var key;
for (key in fs) {
exports[key] = fs[key];
}

// put constants into constants module (don't like doing this but...)
if (!Object.isExtensible || Object.isExtensible(constants)) {
for (key in binding) {
if (/^[A-Z_]+$/.test(key) && !constants.hasOwnProperty(key)) {
constants[key] = binding[key];
}
}
}

exports.constants = binding.constants;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"flock",
"seek"
],
"version": "1.3.0",
"version": "2.0.0",
"homepage": "https://github.com/baudehlo/node-fs-ext/",
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 3125353

Please sign in to comment.