npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lockfd

v2.0.3

Published

thin wrapper around fcntl(F_SETLK) and flock()

Downloads

9

Readme

node-lockfd

This repository is part of the Joyent Triton project. See the contribution guidelines and general documentation at the main Triton project page.

A trivial wrapper around flock(3C) and fcntl(F_SETLKW). The provided interfaces presently allow a synchronous or asynchronous call to get a whole-file, exclusive, advisory write lock on a file, or to block until one is possible. The flock() wrappers additionally allow acquiring a shared lock.

This module has been crafted specifically to work on SmartOS, and may not work anywhere else. The flock functions manipulate Open File Description (OFD) style advisory locks, while the lockfd family of functions manipulate POSIX-style advisory locks. Please see the FILE LOCKING section of fcntl(2) for more details on the locking semantics of each style. In general, the lock will be released when either the file descriptor is closed, or the process exits. The manual page contains information on exceptions to this behaviour.

Note that flock() was added to libc in SmartOS in the 20150219 release, so building and running on older platforms will fail.

Usage

lockfd(fd, callback)

Will attempt to lock the open file descriptor fd as described above. Once the lock is acquired, or an error condition manifests, callback(err) will be called.

lockfdSync(fd)

Synchronous version of lockfd(fd).

flock(fd, op, callback)

This will call flock(3C) and perform the specified operation, which is a bitwise inclusive OR of LOCK_SH, LOCK_EX, LOCK_UN, and LOCK_NB. These constants are exported with this module for convenience. Once the operation completes, the callback will be invoked.

flockSync(fd, op, callback)

Synchronous version of flock(fd, op).

Examples

var mod_fs = require('fs');
var mod_lockfd = require('lockfd');

var fd = mod_fs.openSync('/tmp/.lockfile', 'r+');
console.error('open fd %d', fd);

console.error('locking file...');
mod_lockfd.lockfdSync(fd);
console.log('locked.');

/*
 * Do work...
 */

mod_fs.closeSync(fd);
process.exit(0);

Using OFD-style locking:

var mod_fs = require('fs');
var mod_lockfd = require('lockfd');

var lockfileA = mod_fs.openSync('/tmp/.lockfile', 'r+');
var lockfileB = mod_fs.openSync('/tmp/.lockfile', 'r+');
console.error('opened fds %d and %d', lockfileA, lockfileB);

mod_lockfd.flock(lockfileA, mod_lockfd.LOCK_EX, function (err) {
    if (err) {
        throw err;
    }

    doSomeWork(function () {
        mod_lockfd.flockSync(lockfileA, mod_lockfd.LOCK_UN);
        mod_fs.closeSync(lockfileA);
    });
});

mod_lockfd.flock(lockfileB, mod_lockfd.LOCK_EX, function (err) {
    if (err) {
        throw err;
    }

    doConflictingWork(function () {
        mod_lockfd.flockSync(lockfileB, mod_lockfd.LOCK_UN);
        mod_fs.closeSync(lockfileB);
    });
});

Testing

make clean all test CTFCONVERT=/bin/true CTFMERGE=/bin/true

License

MIT.