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

puff

v1.1.0

Published

Promisify with proxies

Downloads

24

Readme

puff

Promisify with proxies

npm Build Status Coverage Status devDependency Status node

Intro

It's one of those modules that promisify (aka denodeify) things. Promisify means making a node-style function (...args, callback: (err, ...returnValues)) return a Promise instead (...args): Promise<returnValues>.

Using Javascript proxies has the advantage of not modifying the object you pass into it. At least, the operations that you can do with it. All the other operations we're not concerned about will still stay the same as if operating on the original object.

Usage

WARNING: Requires runtime Proxy support! (in case the description didn't give it away)

fn(fnArg, opts)

require('puff/fn')

Promisifies a node-style function.

  • fnArg The node-style function.
  • opts
    • multiArgs Whether to catch multiple arguments in the node-style callback. The arguments will be passed as an array when the returned Promise resolves. Otherwise, a single argument is assumed and that is passed as the resolution value of the returned Promise.

Returns Proxy<Function>: Promise.

obj(objArg, opts)

require('puff/obj')

Promisifies the enumerable properties of an object.

  • objArg The object.
  • opts → fn
    • filter A function to filter the keys with. Default = defaultFilter
    • bind | Pass true to bind the returned proxy to the promisified functions. Pass the string original to bind the original object instead.

Caches all the promisified functions, no hit on property access.

Returns Proxy<Object>.

dynamic(objArg, opts)

require('puff/dynamic')

Promisifies all of the properties of an object.

  • objArg The object.
  • opts → fn
    • filter A function to filter which keys should be promisified when called. Default = defaultFilter
    • bind | Pass true to bind the returned proxy to the promisified functions. Pass the string original to bind the original object instead.

Simplified proxy creation but takes a small hit on property access.

Functions defined on Object.prototype as well as some reserved words such as constructor and prototype are always excluded.

Returns Proxy<Object>.

class(constructorFn, opts)

require('puff/class')

Promisifies the properties of an instance of a class after the instance is created, given the class.

  • constructorFn The class (constructor function).
  • opts → dynamic
    • bind has no effect here, so it won't be passed on.

Returns Proxy<Function>.

Why use dynamic instead of obj?

This is to support Javascript's multiple inheritance model. Using obj is fine if we will be able to know for sure what keys are defined on an object. It's definitely possible if we enumerate all the keys on the object's prototype chain. But if an object inherits from multiple prototypes, there's no way we can find that out. (If there is, please do let me know.)

auto(objArg, opts)

require('puff') (main module)

Runs the object through obj and possibly fn if it's a function.

  • objArg |
  • opts → obj, → fn

defaultFilter(key)

(Internal) The default filter. It filters out strings ending with "Sync".

Chaining modules together

It is recommended that you know the type of the thing you want to promisify.

For plain objects, you can just use obj.

x = require('puff/obj')(x)

For hybrid function / function containers, you can use auto.

x = require('puff')(x)

But you can also chain calls to fn and obj to achieve the same result:

x = require('puff/fn')(require('puff/obj')(x))

The point is that you can mix and match the modules provided to match your needs.

Notes

means "also passed to (the right side)"

Special thanks

  • Portions of code are derived from pify.
  • Thanks to the npm team for support with this package!

License

MIT