Giter Club home page Giter Club logo

promisify-node's Introduction

NodeGit

Node bindings to the libgit2 project.

Actions Status

Stable ([email protected]): 0.28.3

Have a problem? Come chat with us!

Visit slack.libgit2.org to sign up, then join us in #nodegit.

Maintained by

Tyler Ang-Wanek @twwanek with help from tons of awesome contributors!

Alumni Maintainers

Tim Branyen @tbranyen, John Haley @johnhaley81, Max Korp @maxkorp, Steve Smith @orderedlist, Michael Robinson @codeofinterest, and Nick Kallen @nk

API Documentation.

http://www.nodegit.org/

Getting started.

NodeGit will work on most systems out-of-the-box without any native dependencies.

npm install nodegit

If you receive errors about libstdc++, which are commonly experienced when building on Travis-CI, you can fix this by upgrading to the latest libstdc++-4.9.

In Ubuntu:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install libstdc++-4.9-dev

In Travis:

addons:
  apt:
    sources:
      - ubuntu-toolchain-r-test
    packages:
      - libstdc++-4.9-dev

In CircleCI:

  dependencies:
    pre:
      - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
      - sudo apt-get update
      - sudo apt-get install -y libstdc++-4.9-dev

If you receive errors about lifecycleScripts preinstall/install you probably miss libssl-dev In Ubuntu:

sudo apt-get install libssl-dev

You will need the following libraries installed on your linux machine:

  • libpcre
  • libpcreposix
  • libkrb5
  • libk5crypto
  • libcom_err

When building locally, you will also need development packages for kerberos and pcre, so both of these utilities must be present on your machine:

  • pcre-config
  • krb5-config

If you are still encountering problems while installing, you should try the Building from source instructions.

API examples.

Cloning a repository and reading a file:

var Git = require("nodegit");

// Clone a given repository into the `./tmp` folder.
Git.Clone("https://github.com/nodegit/nodegit", "./tmp")
  // Look up this known commit.
  .then(function(repo) {
    // Use a known commit sha from this repository.
    return repo.getCommit("59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5");
  })
  // Look up a specific file within that commit.
  .then(function(commit) {
    return commit.getEntry("README.md");
  })
  // Get the blob contents from the file.
  .then(function(entry) {
    // Patch the blob to contain a reference to the entry.
    return entry.getBlob().then(function(blob) {
      blob.entry = entry;
      return blob;
    });
  })
  // Display information about the blob.
  .then(function(blob) {
    // Show the path, sha, and filesize in bytes.
    console.log(blob.entry.path() + blob.entry.sha() + blob.rawsize() + "b");

    // Show a spacer.
    console.log(Array(72).join("=") + "\n\n");

    // Show the entire file.
    console.log(String(blob));
  })
  .catch(function(err) { console.log(err); });

Emulating git log:

var Git = require("nodegit");

// Open the repository directory.
Git.Repository.open("tmp")
  // Open the master branch.
  .then(function(repo) {
    return repo.getMasterCommit();
  })
  // Display information about commits on master.
  .then(function(firstCommitOnMaster) {
    // Create a new history event emitter.
    var history = firstCommitOnMaster.history();

    // Create a counter to only show up to 9 entries.
    var count = 0;

    // Listen for commit events from the history.
    history.on("commit", function(commit) {
      // Disregard commits past 9.
      if (++count >= 9) {
        return;
      }

      // Show the commit sha.
      console.log("commit " + commit.sha());

      // Store the author object.
      var author = commit.author();

      // Display author information.
      console.log("Author:\t" + author.name() + " <" + author.email() + ">");

      // Show the commit date.
      console.log("Date:\t" + commit.date());

      // Give some space and show the message.
      console.log("\n    " + commit.message());
    });

    // Start emitting events.
    history.start();
  });

For more examples, check the examples/ folder.

Unit tests.

You will need to build locally before running the tests. See above.

npm test

promisify-node's People

Contributors

johnhaley81 avatar jon-hall avatar jugglinmike avatar maxkorp avatar tbranyen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

promisify-node's Issues

fails with es6 class member functions

version 0.4.0
node v6.9.1

When I enter this code in the node REPL:

var promisify = require("promisify-node");
class C {
    constructor() {
        this.promisified_connect = promisify(this.connect);
    }
    connect(done) {
        if (done) {
            done();
        } else {
            return this.promisified_connect();
        }
    }
}
var c = new C()

promisify-node throws this error:

TypeError: Cannot read property '1' of null
    at module.exports (./node_modules/promisify-node/utils/args.js:9:63)
    at processExports (./node_modules/promisify-node/index.js:61:29)
    at module.exports (./node_modules/promisify-node/index.js:164:10)

so I updated the RegExp in https://github.com/nodegit/promisify-node/blob/master/utils/args.js#L9 from:

/function\s.*?\(([^)]*)\)/

to:

/\(([^)]*)\)/

Then re-running the above test code produced another error:

TypeError: Cannot convert undefined or null to object
    at processExports (./node_modules/promisify-node/index.js:86:16)
    at module.exports (./node_modules/promisify-node/index.js:164:10)
    at new C (repl:3:28)

I was able to get past this by modifying https://github.com/nodegit/promisify-node/blob/master/index.js#L86 to guard the checking of the keys of the prototype to:

    if (exports.prototype) {
      if (Object.keys(exports.prototype).length) {

I believe there are likely other changes required.

Promisify an object's prototype functions?

I wrote a library object using a CoffeeScript class and was not able to promisify it until I converted the code to just return a plain Object with the functions defined. No biggie but it would be cool if there were a way to promisify the object's prototype functions.

`fs.write()` has two signatures, so it's broken with promisify-node

From the docs:

fs.write(fd, buffer, offset, length[, position], callback)
fs.write(fd, data[, position[, encoding]], callback)

By default the method signature is this:

function (fd, buffer, offset, length, position, callback)

The parameters can change depending on the input:

if (buffer instanceof Buffer) {
    // if no position is passed then assume null
    if (typeof position === 'function') {
      callback = position;
      position = null;
    }

The tls.connect() method also has two different signatures.

Problem with fs.writeFile

This appears to be caused by the fact that fs module is mutated by promisify('fs'). Internally, fs.writeFile uses some other fs methods in a callback fashion, so it expects fs to be un-modified.

Repro:

const promisify = require('promisify-node');
const fs = promisify('fs');
fs.writeFile('./testfile.txt', 'Hello World')
  .then(() => {
    console.log('File saved!');
  })
  .catch((error) => {
    console.error('Error doing something:', error);
  });

Neither 'File saved!' or 'Error doing something' ever gets logged. The file gets created but no data is written to it. Tested on OS X 10.11 using Node v5.

Cannot read property 'name' of null

This is thrown from nodegit, which uses version 0.3.0 of promisify-node

TypeError: Cannot read property 'name' of null
    at processExports (C:\Users\username\.atom\packages\git-gui\node_modules\promisify-node\index.js:31:21)
    at processExports (C:\Users\username\.atom\packages\git-gui\node_modules\promisify-node\index.js:37:26)
    at processExports (C:\Users\username\.atom\packages\git-gui\node_modules\promisify-node\index.js:37:26)
    at processExports (C:\Users\username\.atom\packages\git-gui\node_modules\promisify-node\index.js:37:26)
    at processExports (C:\Users\username\.atom\packages\git-gui\node_modules\promisify-node\index.js:37:26)
    at processExports (C:\Users\username\.atom\packages\git-gui\node_modules\promisify-node\index.js:37:26)
    at module.exports (C:\Users\username\.atom\packages\git-gui\node_modules\promisify-node\index.js:112:10)
    at Object.<anonymous> (C:\Users\username\.atom\packages\git-gui\node_modules\nodegit\dist\nodegit.js:27:34)
    at Module._compile (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\native-compile-cache.js:103:30)
    at Object.defineProperty.value [as .js] (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\compile-cache.js:208:21)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\native-compile-cache.js:50:27)
    at Object.<anonymous> (file:///C:/Users/username/.atom/packages/git-gui/lib/git-gui-action-bar-view.coffee:1:7)
    at Object.<anonymous> (file:///C:/Users/username/.atom/packages/git-gui/lib/git-gui-action-bar-view.coffee:1:1)
    at Module._compile (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\native-compile-cache.js:103:30)
    at Object.defineProperty.value [as .coffee] (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\compile-cache.js:208:21)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\native-compile-cache.js:50:27)
    at Object.<anonymous> (file:///C:/Users/username/.atom/packages/git-gui/lib/git-gui-view.coffee:5:23)
    at Object.<anonymous> (file:///C:/Users/username/.atom/packages/git-gui/lib/git-gui-view.coffee:1:1)
    at Module._compile (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\native-compile-cache.js:103:30)
    at Object.defineProperty.value [as .coffee] (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\compile-cache.js:208:21)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\native-compile-cache.js:50:27)
    at Object.<anonymous> (file:///C:/Users/username/.atom/packages/git-gui/lib/git-gui.coffee:1:14)
    at Object.<anonymous> (file:///C:/Users/username/.atom/packages/git-gui/lib/git-gui.coffee:1:1)
    at Module._compile (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\native-compile-cache.js:103:30)
    at Object.defineProperty.value [as .coffee] (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\compile-cache.js:208:21)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\native-compile-cache.js:50:27)
    at Package.module.exports.Package.requireMainModule (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\package.js:718:27)
    at Package.module.exports.Package.activateNow (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\package.js:173:16)
    at C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\package.js:798:25
    at Function.module.exports.Emitter.simpleDispatch (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\node_modules\event-kit\lib\emitter.js:25:14)
    at Emitter.module.exports.Emitter.emit (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\node_modules\event-kit\lib\emitter.js:125:28)
    at CommandRegistry.module.exports.CommandRegistry.handleCommandEvent (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\command-registry.js:241:20)
    at C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\command-registry.js:3:61
    at KeymapManager.module.exports.KeymapManager.dispatchCommandEvent (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\node_modules\atom-keymap\lib\keymap-manager.js:580:16)
    at KeymapManager.module.exports.KeymapManager.handleKeyboardEvent (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\node_modules\atom-keymap\lib\keymap-manager.js:388:22)
    at WindowEventHandler.module.exports.WindowEventHandler.handleDocumentKeyEvent (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\window-event-handler.js:98:36)
    at HTMLDocument.<anonymous> (C:\Users\username\AppData\Local\atom\app-1.10.2\resources\app.asar\src\window-event-handler.js:3:61)

List the repository in the package.json

I'll get to this in a bit, just setting a reminder for myself. Need the repo in the package.json so NPM stops complaining when installing nodegit (or otherwise installing promisify-node)

Remove dependencies on polyfills?

In relatively recent versions of Node, including the current LTS, object-assign and nodegit-promise are unnecessary. It'd be great if promisify-node didn't directly depend on polyfill (or polyfill-like in the case of object-assign) libraries and instead required that the environment provide them if necessary.

Leaner version for current-ish node?

It would be nice to have either a newer version of this library, or a companion library, that leaves the future behind and works specifically with vaguely recent node versions. I believe it could then ditch both dependencies - the "stable" node has had built-in Promise and Object.assign for quite some time as I understand.

promisify("module_name") rewrite module functions

Example:

var promisify = require("promisify-node"),
  fs = promisify("fs"),
  native_fs = require("fs");

fs.readFile === native_fs.readFile // true

This behavior destructive, because promisify no should to rewrite common used modules.

fs.exists("") returns Promise<undefined>

var promisifyNode = require("promisify-node");
var fs = require("fs");
var util = require("util");

async function testOne(existsFn) {
    var prom = existsFn("test").catch(e => console.log("error " + e));
    console.log("result " + await prom);
}

async function testBoth() {
    console.log("---builtin");
    var builtin = util.promisify(fs.exists);
    await testOne(builtin);

    console.log("---library");
    var library = promisifyNode(fs).exists;
    await testOne(library);
}

testBoth();

result:

---builtin
result false

---library
result undefined

Broken with node tls module: Cannot read property '1' of null

> require('promisify-node')('tls');
TypeError: Cannot read property '1' of null
    at module.exports (/home/ohnobinki/public_html/ohnopub-status/node_modules/promisify-node/utils/args.js:9:63)
    at processExports (/home/ohnobinki/public_html/ohnopub-status/node_modules/promisify-node/index.js:61:29)
    at /home/ohnobinki/public_html/ohnopub-status/node_modules/promisify-node/index.js:137:23
    at Array.forEach (native)
    at processExports (/home/ohnobinki/public_html/ohnopub-status/node_modules/promisify-node/index.js:132:6)
    at module.exports (/home/ohnobinki/public_html/ohnopub-status/node_modules/promisify-node/index.js:164:10)
    at repl:1:26
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:96:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
ohnobinki@gibby ~/public_html/ohnopub-status $ grep -e version node_modules/promisify-node/package.json
  "version": "0.4.0"
ohnobinki@gibby ~/public_html/ohnopub-status $ node --version
v7.0.0

Webpack support

It looks like the following lines doing dynamic requires is causing problems for webpack because it cannot be statically analyzed:

https://github.com/nodegit/promisify-node/blob/master/index.js#L156

I realize this module is called promisify-node not promisify-js, etc, but just wondering if this dynamic lookup is necessary.

Cannot promisify node-serialport

Hello, I'm trying to use this module bu I'm having some weird time with node-serialport.

The problems seems to be the SerialPort constructor, which accepts a callback that is correctly promisified but not resolved ( at least this is what is seems to me ).

var promisify = require("promisify-node");
var serialport = promisify('serialport');
var SerialPort = serialport.SerialPort;


var port = "/dev/ttyACM0";
var serial_port = new SerialPort(port, {
    baudrate: 57600,
    parser: serialport.parsers.readline("\n")
});

console.log(serial_port)
// Promise {then: function, state: null, isFulfilled: function, isRejected: function, isPending: function…}
serial_port
  .then(function() {console.log(arguments, serial_port)})
  // [undefined] Promise {then: function, state: null, isFulfilled: function, isRejected: function, isPending: function…}

In this situation I'm not able to call methods on the serial_port object. Any hint on how to solve this? Thanks in advance.

promisified functions are cached in an 'unpromisified' state

The cache currently puts the original function in it, so when the function is seen again, instead of being promisified, the original is returned instead.

e.g.

var a = { a: function(cb) { /*...*/ cb(); } };
a.b = a.a;
promisify(a);
a.b().then(/*...*/); // ERROR: Cannot call 'then' on undefined.

Node 10.0.0 Function 's toString method behaves differently than in previous versions

Node 10.0.0 yields an unexpected result when functions are converted to strings through the toString method. Here's what happens:

  • 10.0.0: (function () { }).toString() results in: 'function () { }'.
  • 9.5.0: (function(){ }).toString() results in: 'function (){ }'.

The affected line is: https://github.com/nodegit/promisify-node/blob/master/utils/args.js#L9
A possible solution is to change the RegExp to: /function.*?\(([^)]*)\)/ (for example).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.