pax_global_header00006660000000000000000000000064142032136120014504gustar00rootroot0000000000000052 comment=c926d3f3b735f368276cf6a818a2cc50bb28ff7a gar-promisify-1.1.3/000077500000000000000000000000001420321361200142765ustar00rootroot00000000000000gar-promisify-1.1.3/.github/000077500000000000000000000000001420321361200156365ustar00rootroot00000000000000gar-promisify-1.1.3/.github/dependabot.yml000066400000000000000000000004221420321361200204640ustar00rootroot00000000000000version: 2 updates: - package-ecosystem: npm directory: "/" schedule: interval: daily allow: - dependency-type: direct versioning-strategy: increase-if-necessary commit-message: prefix: deps prefix-development: chore labels: - "dependencies" gar-promisify-1.1.3/.github/workflows/000077500000000000000000000000001420321361200176735ustar00rootroot00000000000000gar-promisify-1.1.3/.github/workflows/ci.yml000066400000000000000000000013751420321361200210170ustar00rootroot00000000000000name: CI on: schedule: # every sunday at noon - cron: 0 12 * * 0 push: branches: [ main ] pull_request: branches: [ main ] jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: '16' - run: npm i --prefer-online -g npm@latest - run: npm i --prefer-online - run: npm run lint test: runs-on: ubuntu-latest strategy: matrix: node-version: [12.x, 14.x, 16.x] steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm i --prefer-online -g npm@latest - run: npm i --prefer-online - run: npm test gar-promisify-1.1.3/.gitignore000066400000000000000000000000371420321361200162660ustar00rootroot00000000000000node_modules package-lock.json gar-promisify-1.1.3/LICENSE.md000066400000000000000000000021061420321361200157010ustar00rootroot00000000000000The MIT License (MIT) Copyright © 2020-2022 Michael Garvin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. gar-promisify-1.1.3/README.md000066400000000000000000000026771420321361200155710ustar00rootroot00000000000000# @gar/promisify ### Promisify an entire object or class instance This module leverages es6 Proxy and Reflect to promisify every function in an object or class instance. It assumes the callback that the function is expecting is the last parameter, and that it is an error-first callback with only one value, i.e. `(err, value) => ...`. This mirrors node's `util.promisify` method. In order that you can use it as a one-stop-shop for all your promisify needs, you can also pass it a function. That function will be promisified as normal using node's built-in `util.promisify` method. [node's custom promisified functions](https://nodejs.org/api/util.html#util_custom_promisified_functions) will also be mirrored, further allowing this to be a drop-in replacement for the built-in `util.promisify`. ### Examples Promisify an entire object ```javascript const promisify = require('@gar/promisify') class Foo { constructor (attr) { this.attr = attr } double (input, cb) { cb(null, input * 2) } const foo = new Foo('baz') const promisified = promisify(foo) console.log(promisified.attr) console.log(await promisified.double(1024)) ``` Promisify a function ```javascript const promisify = require('@gar/promisify') function foo (a, cb) { if (a !== 'bad') { return cb(null, 'ok') } return cb('not ok') } const promisified = promisify(foo) // This will resolve to 'ok' promisified('good') // this will reject promisified('bad') ``` gar-promisify-1.1.3/index.js000066400000000000000000000017071420321361200157500ustar00rootroot00000000000000'use strict' const { promisify } = require('util') const handler = { get: function (target, prop, receiver) { if (typeof target[prop] !== 'function') { return target[prop] } if (target[prop][promisify.custom]) { return function () { return Reflect.get(target, prop, receiver)[promisify.custom].apply(target, arguments) } } return function () { return new Promise((resolve, reject) => { Reflect.get(target, prop, receiver).apply(target, [...arguments, function (err, result) { if (err) { return reject(err) } resolve(result) }]) }) } } } module.exports = function (thingToPromisify) { if (typeof thingToPromisify === 'function') { return promisify(thingToPromisify) } if (typeof thingToPromisify === 'object') { return new Proxy(thingToPromisify, handler) } throw new TypeError('Can only promisify functions or objects') } gar-promisify-1.1.3/package.json000066400000000000000000000012311420321361200165610ustar00rootroot00000000000000{ "name": "@gar/promisify", "version": "1.1.3", "description": "Promisify an entire class or object", "main": "index.js", "repository": { "type": "git", "url": "https://github.com/wraithgar/gar-promisify.git" }, "scripts": { "lint": "standard", "lint:fix": "standard --fix", "test": "lab -a @hapi/code -t 100", "posttest": "npm run lint" }, "files": [ "index.js" ], "keywords": [ "promisify", "all", "class", "object" ], "author": "Gar ", "license": "MIT", "devDependencies": { "@hapi/code": "^8.0.1", "@hapi/lab": "^24.1.0", "standard": "^16.0.3" } } gar-promisify-1.1.3/test/000077500000000000000000000000001420321361200152555ustar00rootroot00000000000000gar-promisify-1.1.3/test/index.js000066400000000000000000000042331420321361200167240ustar00rootroot00000000000000'use strict' const lab = (exports.lab = require('@hapi/lab').script()) const { describe, it } = lab const { expect } = require('@hapi/code') const util = require('util') const promisify = require('../') class Fixture { constructor (attr) { this.attr = attr this.custom[util.promisify.custom] = function (input1, input2) { return Promise.resolve([this.attr, input1, input2]) } } single (input, cb) { cb(null, [this.attr, input]) } custom (input1, input2, cb) { cb(null, this.attr, input1, input2) } error (input, cb) { cb(new Error(input)) } } it('requires a function or object', () => { const throws = () => { promisify('string') } expect(throws).to.throw(TypeError) }) describe('promisify object', () => { it('non function attribute', () => { const instance = new Fixture('test') const promisified = promisify(instance) expect(promisified.attr).to.equal('test') }) it('custom promisify', async () => { const instance = new Fixture('test') const promisified = promisify(instance) const custom = await promisified.custom('test one', 'test two') expect(custom).to.equal(['test', 'test one', 'test two']) }) it('callback success', async () => { const instance = new Fixture('test') const promisified = promisify(instance) const single = await promisified.single('test single') expect(single).to.equal(['test', 'test single']) }) it('callback success', async () => { const instance = new Fixture('test') const promisified = promisify(instance) const rejects = function () { return promisified.error('test error') } expect(rejects()).to.reject(Error, 'test error') }) }) describe('promisify function', () => { it('promisifies a function', async () => { const fn = (a, cb) => cb(null, a) const promisified = promisify(fn) const result = await promisified('test') expect(result).to.equal('test') }) it('assumes error first callback', async () => { const fn = (a, cb) => cb(new Error('test error'), a) const promisified = promisify(fn) const result = promisified('test') expect(result).to.reject('test error') }) })