Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
type-binder
Advanced tools
Given an ES6/TypeScript class
class Person {
name: string;
sayHello() {
return `Hello, my name is ${this.name}!`;
}
}
from a plain JavaScript object you can easily create an instance of Person
let object = { name: "Foo" }; // or JSON.parse('{ "name": "Foo" }');
let foo = new TypeBinder().bind(object, Person);
foo.sayHello(); // returns "Hello, my name is Foo!"
To bind also object properties, you can use a decorator
import { bind } from "type-binder";
class Person {
name: string;
@bind(Person) parent;
}
let foo = new TypeBinder().bind({ name: "Foo", parent: { name: "Bar" } }, Person);
foo.parent.sayHello(); // returns "Hello, my name is Bar!";
This works also with generic typing, using the @generics
decorator
import { generics } from "type-binder";
class Person {
name: string;
@generics(Person) children: Set<Person>;
}
let foo = new TypeBinder().bind({ name: "Foo", children: [ { name: "Bar" }, { name: "Baz" } ] }, Person);
for (let child of foo.children) {
child.sayHello();
}
And you can add binding callbacks for additional types
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
}
let binder = new TypeBinder();
binder.setBindingCallback(Person, object => new Person(object.name));
The @identifier
decorator takes a function which returns an identifier of that
object, which the binder will use to return same instances for all objects with
the same identifier (could be scoped, see code).
import { identifier } from "type-binder";
@identifier<Person>(person => person.name)
class Person {
name: string
}
let binder = new TypeBinder();
let foo1 = binder.bind({ "name": "Foo" }, Person);
let foo2 = binder.bind({ "name": "Foo" }, Person);
foo1 === foo2; // true
Decorating a property with @track
will instruct the binder to save the initial
property value in the object's metadata.
import { track } from "type-binder";
class Person {
@track() name: string
}
let person = new TypeBinder().bind({ name: "Foo" }, Person);
person.name = "Bar";
let changed = TypeBinder.propertyHasChanged(person, "name");
changed === true; // true
FAQs
A JavaScript type-binder.
The npm package type-binder receives a total of 1 weekly downloads. As such, type-binder popularity was classified as not popular.
We found that type-binder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.