Comparing version 0.2.4 to 1.1.0
156
index.js
@@ -0,66 +1,110 @@ | ||
'use strict' | ||
var ResType = require('result-type') | ||
, when = require('when/read') | ||
, Result = require('result') | ||
const promisifyCustom = require('util').promisify.custom | ||
class Result { | ||
constructor (err, data) { | ||
this.err = err | ||
this.data = data | ||
} | ||
} | ||
exports.resultifyP = resultifyPromise | ||
exports.resultifyPromise = resultifyPromise | ||
exports.resultify = resultify | ||
exports.resultifySync = resultifySync | ||
/** | ||
* Teach a node function all about the Result type | ||
* const { resultify } = require('resultify') | ||
* const fs = require('fs') | ||
* | ||
* var readFile = resultify(fs.readFile) | ||
* readFile('/path/to/file.js', 'utf8').then(function(src){ | ||
* process.stdout.write(src) | ||
* }) | ||
* | ||
* @param {Function} fn must take a callback as its last parameter | ||
* @return {Result} | ||
* const readFile = resultify(fs.readFile) | ||
* | ||
* async function main () { | ||
* const { err, data } = await readFile('my-file') | ||
* | ||
* const { err, data } = await resultify((cb) => { | ||
* fs.readFile('my-file', cb) | ||
* })() | ||
* } | ||
*/ | ||
function resultify (original) { | ||
if (typeof original !== 'function') { | ||
throw new Error('original must be a function.') | ||
} | ||
module.exports = function(fn){ | ||
return function () { | ||
var result = new Result | ||
var i = arguments.length | ||
if (original[promisifyCustom]) { | ||
const fn = original[promisifyCustom] | ||
if (typeof fn !== 'function') { | ||
throw new Error('util.promisify.custom must be a function.') | ||
} | ||
// scan for Result parameters | ||
while (i--) if (arguments[i] instanceof ResType) { | ||
var args = arguments | ||
var fail = function(e){ | ||
result.error(e) | ||
} | ||
var self = this | ||
var next = function(value){ | ||
args[i] = value | ||
if (i > 0) return when(args[--i], next, fail) | ||
// call `fn` (apply is slow) | ||
try { switch (args.length) { | ||
case 0: fn.call(self, cb); break; | ||
case 1: fn.call(self, args[0], cb); break; | ||
case 2: fn.call(self, args[0], args[1], cb); break; | ||
case 3: fn.call(self, args[0], args[1], args[2], cb); break; | ||
default: | ||
args[args.length++] = cb | ||
fn.apply(self, args) | ||
} } catch (e) { result.error(e) } | ||
} | ||
args[i].read(next, fail) | ||
return result | ||
} | ||
return resultifyPromise(fn) | ||
} | ||
// call `fn` (apply is slow) | ||
try { switch (arguments.length) { | ||
case 0: fn.call(this, cb); break; | ||
case 1: fn.call(this, arguments[0], cb); break; | ||
case 2: fn.call(this, arguments[0], arguments[1], cb); break; | ||
case 3: fn.call(this, arguments[0], arguments[1], arguments[2], cb); break; | ||
default: | ||
arguments[arguments.length++] = cb | ||
fn.apply(this, arguments) | ||
} } catch (e) { result.error (e) } | ||
function fn (...args) { | ||
return new Promise((resolve) => { | ||
original.call(this, ...args, (err, value) => { | ||
if (err) { | ||
return resolve(new Result(err, null)) | ||
} | ||
function cb(error, value){ | ||
if (error) result.error(error) | ||
else result.write(value) | ||
} | ||
resolve(new Result(null, value)) | ||
}) | ||
}) | ||
} | ||
return result | ||
} | ||
} | ||
return fn | ||
} | ||
/** | ||
* const { resultifyP } = require('resultify') | ||
* | ||
* const { err, data } = await resultifyP(() => { | ||
* return s3.listBuckets().promise() | ||
* })() | ||
*/ | ||
function resultifyPromise (original) { | ||
if (typeof original !== 'function') { | ||
throw new Error('original must be a function.') | ||
} | ||
function fn () { | ||
const p = original.apply(this, arguments) | ||
return p.then(createResolveResult, createRejectResult) | ||
} | ||
return fn | ||
} | ||
/** | ||
* const { resultifySync } = require('resultify') | ||
* | ||
* const { err, data } = await resultifySync(() => { | ||
* return JSON.parse(str) | ||
* })() | ||
*/ | ||
function resultifySync (original) { | ||
if (typeof original !== 'function') { | ||
throw new Error('original must be a function.') | ||
} | ||
function fn () { | ||
try { | ||
const ret = original.apply(this, arguments) | ||
return new Result(null, ret) | ||
} catch (err) { | ||
return new Result(err, null) | ||
} | ||
} | ||
return fn | ||
} | ||
function createResolveResult (data) { | ||
return new Result(null, data) | ||
} | ||
function createRejectResult (err) { | ||
return new Result(err, null) | ||
} |
{ | ||
"name": "resultify", | ||
"version": "0.2.4", | ||
"description": "convert node functions to Result returning ones", | ||
"dependencies": { | ||
"result": "0.3.1", | ||
"result-type": "1.0.0", | ||
"when": "http://github.com/jkroso/when/tarball/3.2.3" | ||
"version": "1.1.0", | ||
"description": "Handle errors with async/await without try/catch.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npr standard && node test/index.js" | ||
}, | ||
"devDependencies": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/jkroso/resultify.git" | ||
"dependencies": {}, | ||
"binDependencies": { | ||
"standard": "14.3.3" | ||
}, | ||
"devDependencies": { | ||
"@pre-bundled/tape": "4.11.0", | ||
"node-fetch": "2.6.0", | ||
"npm-bin-deps": "1.4.2" | ||
}, | ||
"author": "Raynos <raynos2@gmail.com>", | ||
"repository": "git://github.com/Raynos/resultify.git", | ||
"homepage": "https://github.com/Raynos/resultify", | ||
"bugs": { | ||
"url": "https://github.com/jkroso/resultify/issues" | ||
"url": "https://github.com/Raynos/resultify/issues", | ||
"email": "raynos2@gmail.com" | ||
}, | ||
"keywords": [ | ||
"cps", | ||
"result" | ||
"contributors": [ | ||
{ | ||
"name": "Raynos" | ||
} | ||
], | ||
"author": "Jake Rosoman", | ||
"license": "MIT" | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://github.com/Raynos/resultify/raw/master/LICENSE" | ||
} | ||
] | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
HTTP dependency
Supply chain riskContains a dependency which resolves to a remote HTTP URL which could be used to inject untrusted code and reduce overall package reliability.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
16096
0
6
305
0
0
196
0
3
2
4
- Removedresult@0.3.1
- Removedresult-type@1.0.0
- Removedresult@0.3.1(transitive)
- Removedresult-type@1.0.0(transitive)
- Removedruntime-context@0.1.1(transitive)
- Removedunhandled@0.1.1(transitive)