Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@badrap/result
Advanced tools
A TypeScript result type taking cues from Rust's Result and Haskell's Either types
A TypeScript result type taking cues from Rust's Result and Haskell's Either types. It's goals are:
$ npm i @badrap/result
import { Result } from "@badrap/result";
Result<T, E extends Error = Error> is a type that wraps either a value that is the result of a succesful computation and of type T, or an error of type E denoting a failed computation.
The type is actually an union of two types: Result.Ok<T, E> that wraps a success value and Result.Err<T, E> that wraps an error.
Result.ok returns a new Result.Ok wrapping the given value, while Result.err returns a new Result.Err wrapping the given error.
const res = Result.ok(1);
res.isOk; // true
const res = Result.err(new Error());
res.isOk; // false
const res = Result.err(); // functionally equal to Result.err(new Error())
res.isOk; // false
Result.Ok has an additional property value containing the wrapped value. Similarly, Result.Err has the property error containing the wrapped error. They can be accessed after asserting to TypeScript's type checker that it's safe to do so. The isErr and isOk properties (see below) are handy for this.
const res = Math.random() < 0.5 ? Result.ok(1) : Result.err(new Error("oh no"));
if (res.isErr) {
// TypeScript now knows that res is a Result.Err, and we can access res.error
res.error; // Error("oh no")
}
if (res.isOk) {
// TypeScript now knows that res is a Result.Ok, and we can access res.value
res.value; // 1
}
Result#isOk and Result#isErr are complementary readonly properties. isOk is true
for Result.Ok and false
for Result.Err.
const ok = Result.ok(1);
ok.isOk; // true
const err = Result.err(new Error());
err.isOk; // false
isErr is the inverse of isOk: false
for Result.Ok and true
for Result.Err.
const ok = Result.ok(1);
ok.isErr; // false
const err = Result.err(new Error());
err.isErr; // true
Return the wrapped value for Result.Ok and throw the wrapped error for Result.Err. This can be modified for providing functions to map the value and error to some value.
const ok = Result.ok(1);
const err = Result.err(new Error("oh no"));
ok.unwrap(); // 1
err.unwrap(); // throws Error("oh no")
ok.unwrap((value) => value + 1); // 2
err.unwrap((value) => value + 2); // throws Error("oh no")
ok.unwrap(
(value) => value + 1,
(error) => 0
); // 2
err.unwrap(
(value) => value + 2,
(error) => 0
); // 0
As a small extra convenience the result types from the callbacks don't have to be the same. Here's an example Koa.js handler demonstrating this, using an imaginary validate function that returns a Result:
app.use(async ctx =>
await validate(ctx.request.body).unwrap(
async (value: any) => {
...
},
error => {
ctx.status = 422;
ctx.body = {
message: "validation failed"
};
}
)
);
Return a new Result where the given function/functions have been applied to the wrapped value and error.
const ok = Result.ok(1);
const err = Result.err(new Error("oh no"));
ok.map((value) => value + 1).unwrap(); // 2
err.map((value) => value + 1).unwrap(); // throws Error("oh no")
ok.map(
(value) => value + 1,
(error) => new Error("mapped")
).unwrap(); // 2
err
.map(
(value) => value + 1,
(error) => new Error("mapped")
)
.unwrap(); // throws Error("mapped")
const ok = Result.ok(1);
const err = Result.err(new Error("oh no"));
ok.chain((value) => Result.ok(value + 1)).unwrap(); // 2
err.chain((value) => Result.ok(value + 1)).unwrap(); // throws Error("oh no")
ok.chain(
(value) => Result.ok(value + 1),
(error) => Result.ok(0)
).unwrap(); // 2
err
.chain(
(value) => Result.ok(value + 1),
(error) => Result.ok(0)
)
.unwrap(); // 0
Return a new Result where the wrapped value is the collection of the wrapped values of the input array.
Result.all([Result.ok(1), Result.ok("test")]).unwrap(); // [1, "test"]
If any of the input results wraps an error then that result is returned as-is.
Result.all([Result.ok(1), Result.err(new Error("oh no"))]).unwrap(); // throws Error("oh no")
Non-array objects can also be given as arguments. In that case the wrapped output value is also an object.
Result.all({
x: Result.ok(1),
y: Result.ok("test"),
}).unwrap(); // { x: 1, y: "test" }
This library is licensed under the MIT license. See LICENSE.
FAQs
A TypeScript result type taking cues from Rust's Result and Haskell's Either types
The npm package @badrap/result receives a total of 10,602 weekly downloads. As such, @badrap/result popularity was classified as popular.
We found that @badrap/result demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.