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.
@trademe/ensure
Advanced tools
@Singleton
:This decorator can be applied to
@Injectable
classes to ensure that they are instantiated only once. This is useful for stateful services that belong at the root of the application.
import { Singleton } from '@trademe/ensure';
@Singleton
@Injectable
export class MyStatefulService { }
let instance = new MyStatefulService(); // works:
let another = new MyStatefulService(); // throws!
@Value
:This decorator can be applied to properties on a class to ensure that they conform to rules when they are set. The rules also act as a mechanism for casting values from HTML attributes, which makes for nicer component APIs.
import { Value } from '@trademe/ensure';
export class MyClass {
@Value(isOne) mustBeOne: number;
@Value([isString, isLengthFive]) verySpecificString: string;
}
export function isOne (value: any) {
if (value === 1) {
return value;
}
throw new Error();
}
export function isString (value: string) {
if (typeof value === 'string') {
return value;
}
throw new Error();
}
export function isLengthFive (value: string) {
if (value.length === 5) {
return value;
}
throw new Error();
}
isNotNull
Ensure that a value is not
null
.isNotNull
is special in that it acts when getting the value, not setting it.
import { isNotNull } from '@trademe/ensure';
export class MyClass {
@Value(isNotNull) mustNotBeNull: boolean;
}
let instance = new MyClass();
instance.mustNotBeNull = true; // works
instance.mustNotBeNull = null; // works
console.log(instance.mustNotBeNull): // throws!
isBool
Ensure that a value is boolean. Also casts from
'true'
or''
totrue
and'false'
tofalse
.
import { isBool } from '@trademe/ensure';
export class MyClass {
@Value(isBool) mustBeBoolean: boolean;
}
let instance = new MyClass();
instance.mustBeBoolean = true; // works
instance.mustBeBoolean = 'hello'; // throws!
instance.mustBeBoolean = 'false'; // works
console.log(instance.mustBeBoolean) // false
isBool
is particularly useful for simplifying component APIs when combined with@Input
.
import { isBool } from '@trademe/ensure';
@Component({ ... })
export class MyComponent {
@Input() @Value(isBool) mustBeBoolean: boolean;
}
<my-component
mustBeBoolean><!-- No more pesky ="true"! -->
</my-component>
isEnum
Ensure that a value is a valid enum value.
import { isEnum } from '@trademe/ensure';
export enum MyEnum {
foo,
bar
}
export class MyClass {
@Value(isEnum<typeof MyEnum>(MyEnum)) mustBeEnum: MyEnum;
}
let instance = new MyClass();
instance.mustBeEnum = 'foo'; // works
console.log(instance.mustBeEnum); // 0
instance.mustBeEnum = 'bar'; // works!
console.log(instance.mustBeEnum); // 1
instance.mustBeEnum = 'baz'; // throws!
isNumber
Ensure that a value is a valid number.
import { isNumber } from '@trademe/ensure';
export class MyClass {
@Value(isNumber) mustBeNumber: number;
}
let instance = new MyClass();
instance.mustBeNumber = 5; // works
console.log(instance.mustBeNumber); // 5
instance.mustBeNumber = '1.33'; // works!
console.log(instance.mustBeNumber); // 1.33
instance.mustBeNumber = 'baz'; // throws!
FAQs
Unknown package
The npm package @trademe/ensure receives a total of 61 weekly downloads. As such, @trademe/ensure popularity was classified as not popular.
We found that @trademe/ensure demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 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
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.