Socket
Socket
Sign inDemoInstall

@aurelia/kernel

Package Overview
Dependencies
Maintainers
1
Versions
1114
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aurelia/kernel - npm Package Compare versions

Comparing version 2.0.1-dev.202404041108 to 2.0.1-dev.202404170324

17

CHANGELOG.md

@@ -6,2 +6,19 @@ # Change Log

<a name="2.0.0-beta.15"></a>
# 2.0.0-beta.15 (2024-04-17)
### Features:
* **resources:** support static `$au` property for definition (#1939) ([877a385](https://github.com/aurelia/aurelia/commit/877a385))
### Bug Fixes:
* ***:** residual decorator work (#1942) ([7e8c12f](https://github.com/aurelia/aurelia/commit/7e8c12f))
### Refactorings:
* ***:** migration to TC39 decorators + metadata simplification (#1932) ([22f90ad](https://github.com/aurelia/aurelia/commit/22f90ad))
<a name="2.0.0-beta.14"></a>

@@ -8,0 +25,0 @@ # 2.0.0-beta.14 (2024-04-03)

8

dist/types/di.container.d.ts

@@ -1,3 +0,3 @@

import { IContainer, type Key, type IResolver, type Resolved, type IContainerConfiguration } from './di';
import type { IAllResolver, INewInstanceResolver, ILazyResolver, IResolvedLazy, IOptionalResolver, IFactoryResolver, IResolvedFactory } from './di.resolvers';
import { IContainer, InterfaceSymbol, type IContainerConfiguration, type IResolver, type Key, type Resolved } from './di';
import type { IAllResolver, IFactoryResolver, ILazyResolver, INewInstanceResolver, IOptionalResolver, IResolvedFactory, IResolvedLazy } from './di.resolvers';
export declare const Registrable: Readonly<{

@@ -24,5 +24,3 @@ /**

}
export type IResolvedInjection<K extends Key> = K extends IAllResolver<infer R> ? Resolved<R>[] : K extends INewInstanceResolver<infer R> ? Resolved<R> : K extends ILazyResolver<infer R> ? IResolvedLazy<R> : K extends IOptionalResolver<infer R> ? Resolved<R> | undefined : K extends IFactoryResolver<infer R> ? IResolvedFactory<R> : K extends IResolver<infer R> ? Resolved<R> : K extends [infer R1 extends Key, ...infer R2] ? [IResolvedInjection<R1>, ...IResolvedInjection<R2>] : K extends {
__resolved__: infer T;
} ? T : Resolved<K>;
export type IResolvedInjection<K extends Key> = K extends IAllResolver<infer R> ? Resolved<R>[] : K extends INewInstanceResolver<infer R> ? Resolved<R> : K extends ILazyResolver<infer R> ? IResolvedLazy<R> : K extends IOptionalResolver<infer R> ? Resolved<R> | undefined : K extends IFactoryResolver<infer R> ? IResolvedFactory<R> : K extends IResolver<infer R> ? Resolved<R> : K extends [infer R1 extends Key, ...infer R2] ? [IResolvedInjection<R1>, ...IResolvedInjection<R2>] : K extends InterfaceSymbol<infer T> ? T : Resolved<K>;
/**

@@ -29,0 +27,0 @@ * Retrieve the resolved value of a key, or values of a list of keys from the currently active container.

@@ -5,3 +5,8 @@ import { Constructable, IDisposable } from './interfaces';

export type ResolveCallback<T = any> = (handler: IContainer, requestor: IContainer, resolver: IResolver<T>) => T;
export type InterfaceSymbol<K = any> = (target: Injectable | AbstractInjectable, property: string | symbol | undefined, index?: number) => void;
export interface InterfaceSymbol<K = any> {
$isInterface: boolean;
friendlyName?: string;
register?(container: IContainer, key?: K): IResolver<K>;
toString?(): string;
}
interface IResolverLike<C, K = any> {

@@ -66,2 +71,3 @@ readonly $isResolver: true;

useResources(container: IContainer): void;
find<TResType extends ResourceType>(kind: string, name: string): TResType | null;
find<TResType extends ResourceType>(key: string): TResType | null;

@@ -103,8 +109,6 @@ }

}
export declare const inject: (...dependencies: Key[]) => (target: Injectable, key?: string | number, descriptor?: PropertyDescriptor | number) => void;
export declare const inject: (...dependencies: Key[]) => (decorated: unknown, context: DecoratorContext) => void;
export declare const DI: {
createContainer: (config?: Partial<IContainerConfiguration> | undefined) => IContainer;
getDesignParamtypes: (Type: Constructable | Injectable) => readonly Key[] | undefined;
getAnnotationParamtypes: (Type: Constructable | Injectable) => readonly Key[] | undefined;
getOrCreateAnnotationParamTypes: (Type: Constructable | Injectable) => Key[];
getDependencies: (Type: Constructable | Injectable) => Key[];

@@ -153,3 +157,3 @@ /**

createInterface: <K extends Key>(configureOrName?: string | ((builder: ResolverBuilder<K>) => IResolver<K>) | undefined, configuror?: ((builder: ResolverBuilder<K>) => IResolver<K>) | undefined) => InterfaceSymbol<K>;
inject: (...dependencies: Key[]) => (target: Injectable, key?: string | number, descriptor?: PropertyDescriptor | number) => void;
inject: (...dependencies: Key[]) => (decorated: unknown, context: DecoratorContext) => void;
/**

@@ -195,3 +199,3 @@ * Registers the `target` class as a transient dependency; each time the dependency is resolved

export declare const IServiceLocator: InterfaceSymbol<IServiceLocator>;
declare function transientDecorator<T extends Constructable>(target: T & Partial<RegisterSelf<T>>): T & RegisterSelf<T>;
declare function transientDecorator<T extends Constructable>(target: T & Partial<RegisterSelf<T>>, _context: ClassDecoratorContext): T & RegisterSelf<T>;
/**

@@ -218,7 +222,7 @@ * Registers the decorated class as a transient dependency; each time the dependency is resolved

*/
export declare function transient<T extends Constructable>(target: T & Partial<RegisterSelf<T>>): T & RegisterSelf<T>;
export declare function transient<T extends Constructable>(target: T & Partial<RegisterSelf<T>>, context: ClassDecoratorContext): T & RegisterSelf<T>;
type SingletonOptions = {
scoped: boolean;
};
declare const singletonDecorator: <T extends Constructable>(target: T & Partial<RegisterSelf<T>>) => T & RegisterSelf<T>;
type SingletonDecorator = <T extends Constructable>(target: T & Partial<RegisterSelf<T>>, _context: ClassDecoratorContext) => T & RegisterSelf<T>;
/**

@@ -233,4 +237,4 @@ * Registers the decorated class as a singleton dependency; the class will only be created once. Each

*/
export declare function singleton<T extends Constructable>(): typeof singletonDecorator;
export declare function singleton<T extends Constructable>(options?: SingletonOptions): typeof singletonDecorator;
export declare function singleton<T extends Constructable>(): SingletonDecorator;
export declare function singleton<T extends Constructable>(options?: SingletonOptions): SingletonDecorator;
/**

@@ -247,3 +251,3 @@ * Registers the `target` class as a singleton dependency; the class will only be created once. Each

*/
export declare function singleton<T extends Constructable>(target: T & Partial<RegisterSelf<T>>): T & RegisterSelf<T>;
export declare function singleton<T extends Constructable>(target: T & Partial<RegisterSelf<T>>, context: ClassDecoratorContext): T & RegisterSelf<T>;
export declare class InstanceProvider<K extends Key> implements IDisposableResolver<K | null> {

@@ -250,0 +254,0 @@ get friendlyName(): string | undefined;

@@ -13,6 +13,3 @@ import { IContainer, IResolver, Key, Resolved } from './di';

export declare const all: <T extends Key>(key: T, searchAncestors?: boolean) => IAllResolver<T>;
export type IAllResolver<T> = IResolver<Resolved<T>[]> & {
__isAll: undefined;
(...args: unknown[]): any;
};
export type IAllResolver<T> = IResolver<readonly Resolved<T>[]> & ((decorated: unknown, context: DecoratorContext) => any);
/**

@@ -19,0 +16,0 @@ * Lazily inject a dependency depending on whether the [[`Key`]] is present at the time of function call.

@@ -52,3 +52,3 @@ import { Constructable, Overwrite } from './interfaces';

*/
export declare const bound: <T extends Function>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>;
export declare const bound: <TThis extends object, TArgs extends unknown[], TReturn>(originalMethod: (this: TThis, ...args: TArgs) => TReturn, context: ClassMethodDecoratorContext<TThis, (this: TThis, ...args: TArgs) => TReturn>) => void;
export declare const mergeArrays: <T>(...arrays: (readonly T[] | undefined)[]) => T[];

@@ -55,0 +55,0 @@ export declare const firstDefined: <T>(...values: readonly (T | undefined)[]) => T;

@@ -10,3 +10,3 @@ export { IPlatform, } from './platform';

export { noop, emptyArray, emptyObject, } from './platform';
export { resourceBaseName, getResourceKeyFor, type IResourceKind, type PartialResourceDefinition, Protocol, type ResourceDefinition, type ResourceType, fromAnnotationOrDefinitionOrTypeOrDefault, fromAnnotationOrTypeOrDefault, fromDefinitionOrDefault, } from './resource';
export { resourceBaseName, getResourceKeyFor, type IResourceKind, type PartialResourceDefinition, Protocol, type ResourceDefinition, type StaticResourceType, type ResourceType, fromAnnotationOrDefinitionOrTypeOrDefault, fromAnnotationOrTypeOrDefault, fromDefinitionOrDefault, } from './resource';
export { EventAggregator, IEventAggregator, } from './eventaggregator';

@@ -13,0 +13,0 @@ export { isArrayIndex, camelCase, kebabCase, pascalCase, toArray, bound, mergeArrays, firstDefined, getPrototypeChain, isNativeFunction, onResolve, onResolveAll, } from './functions';

@@ -7,3 +7,3 @@ export interface IDisposable {

};
export type Class<T, C = {}> = C & {
export type Class<T, TStaticProps = {}> = TStaticProps & {
readonly prototype: T;

@@ -10,0 +10,0 @@ new (...args: any[]): T;

@@ -155,6 +155,6 @@ import { IContainer, IRegistry } from './di';

key: string;
define<TSink extends ISink>(target: Constructable<TSink>, definition: SinkDefinition): Constructable<TSink>;
getHandles<TSink_1 extends ISink>(target: TSink_1 | Constructable<TSink_1>): LogLevel[] | undefined;
define<TSink extends Constructable<ISink>>(target: TSink, definition: SinkDefinition): void;
getHandles<TSink_1 extends ISink>(target: TSink_1): Exclude<LogLevel, typeof none>[] | undefined;
}>;
export declare const sink: (definition: SinkDefinition) => <TSink extends ISink>(target: Constructable<TSink>) => Constructable<TSink>;
export declare const sink: (definition: SinkDefinition) => <TSink extends Constructable<ISink>>(_target: TSink, context: ClassDecoratorContext<TSink>) => void;
export interface IConsoleLike {

@@ -161,0 +161,0 @@ debug(message: string, ...optionalParams: unknown[]): void;

import { IContainer } from './di';
import { Constructable } from './interfaces';
export type ResourceType<TUserType extends Constructable = Constructable, TResInstance extends {} = {}, TResType extends {} = {}, TUserInstance extends InstanceType<TUserType> = InstanceType<TUserType>> = (new (...args: any[]) => TResInstance & TUserInstance) & {
readonly aliases?: readonly string[];
} & TResType & TUserType;
export type StaticResourceType<TDef extends object = object> = {
readonly aliases?: string[];
readonly $au?: PartialResourceDefinition<{
type: string;
} & TDef>;
};
export type ResourceType<TUserType extends Constructable = Constructable, TResInstance extends {} = {}, TResType extends {} = {}, TUserInstance extends InstanceType<TUserType> = InstanceType<TUserType>> = (new (...args: any[]) => TResInstance & TUserInstance) & StaticResourceType & TResType & TUserType;
export type ResourceDefinition<TUserType extends Constructable = Constructable, TResInstance extends {} = {}, TDef extends {} = {}, TResType extends {} = {}, TUserInstance extends InstanceType<TUserType> = InstanceType<TUserType>> = {

@@ -22,3 +26,3 @@ /**

} & TDef;
export type PartialResourceDefinition<TDef extends {} = {}> = {
export type PartialResourceDefinition<TDef extends object = object> = {
readonly name: string;

@@ -25,0 +29,0 @@ readonly aliases?: readonly string[];

{
"name": "@aurelia/kernel",
"version": "2.0.1-dev.202404041108",
"version": "2.0.1-dev.202404170324",
"main": "dist/cjs/index.cjs",

@@ -56,4 +56,4 @@ "module": "dist/esm/index.mjs",

"dependencies": {
"@aurelia/metadata": "2.0.1-dev.202404041108",
"@aurelia/platform": "2.0.1-dev.202404041108"
"@aurelia/metadata": "2.0.1-dev.202404170324",
"@aurelia/platform": "2.0.1-dev.202404170324"
},

@@ -60,0 +60,0 @@ "devDependencies": {

@@ -20,5 +20,6 @@ /* eslint-disable @typescript-eslint/no-unused-vars */

class Def {
public constructor(@all(I) private readonly i: I) {}
}
// Not yet supported by TS, as the method-parameter decorator proposal is not in Stage 4 yet
// class Def {
// public constructor(@all(I) private readonly i: I) {}
// }

@@ -35,8 +36,10 @@ @inject(all(I))

class Def {
public constructor(@lazy(I) private readonly i: I) {}
}
// Not yet supported by TS, as the method-parameter decorator proposal is not in Stage 4 yet
// class Def {
// public constructor(@lazy(I) private readonly i: I) {}
// }
@inject(lazy(I))
class G {
private readonly foo = lazy(I);
public i = resolve(lazy(I));

@@ -55,5 +58,6 @@ public b: I = this.i();

class Def {
public constructor(@optional(I) private readonly i: I) { }
}
// Not yet supported by TS, as the method-parameter decorator proposal is not in Stage 4 yet
// class Def {
// public constructor(@optional(I) private readonly i: I) { }
// }

@@ -79,8 +83,9 @@ @inject(optional(I))

class Def {
public constructor(
@newInstanceOf(I) private readonly i: I,
@newInstanceForScope(I) private readonly j: I,
) { }
}
// Not yet supported by TS, as the method-parameter decorator proposal is not in Stage 4 yet
// class Def {
// public constructor(
// @newInstanceOf(I) private readonly i: I,
// @newInstanceForScope(I) private readonly j: I,
// ) { }
// }

@@ -87,0 +92,0 @@ @inject(newInstanceOf(I))

/* eslint-disable @typescript-eslint/no-this-alias */
/* eslint-disable @typescript-eslint/strict-boolean-expressions, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any */
import { Metadata, isObject } from '@aurelia/metadata';
import { isNativeFunction } from './functions';
import { type Class, type Constructable, type IDisposable } from './interfaces';
import { emptyArray } from './platform';
import { resourceBaseName, ResourceDefinition, type ResourceType } from './resource';
import { isFunction, isString, objectFreeze } from './utilities';
import { isObject } from '@aurelia/metadata';
import {
IContainer,
type Key,
type IResolver,
InterfaceSymbol,
Resolver,
ResolverStrategy,
getDependencies,
type IContainerConfiguration,
type IDisposableResolver,
type IFactory,
type IRegistry,
Resolver,
ResolverStrategy,
type Transformer,
type IResolver,
type Key,
type RegisterSelf,
type Resolved,
getDependencies,
type IFactory,
type IContainerConfiguration,
type Transformer,
} from './di';
import { aliasToRegistration, singletonRegistration } from './di.registration';
import type {
IAllResolver,
IFactoryResolver,
ILazyResolver,
INewInstanceResolver,
IOptionalResolver,
IResolvedFactory,
IResolvedLazy,
} from './di.resolvers';
import { ErrorNames, createMappedError, logError, logWarn } from './errors';
import { singletonRegistration } from './di.registration';
import type { IAllResolver, INewInstanceResolver, ILazyResolver, IResolvedLazy, IOptionalResolver, IFactoryResolver, IResolvedFactory } from './di.resolvers';
import { isNativeFunction } from './functions';
import { type Class, type Constructable, type IDisposable } from './interfaces';
import { emptyArray } from './platform';
import { ResourceDefinition, StaticResourceType, resourceBaseName, type ResourceType } from './resource';
import { getMetadata, isFunction, isString, objectFreeze } from './utilities';

@@ -63,3 +72,3 @@ export const Registrable = /*@__PURE__*/(() => {

public readonly defaultResolver: (key: Key, handler: IContainer) => IResolver,
) {}
) { }

@@ -186,6 +195,28 @@ public static from(config?: IContainerConfiguration): ContainerConfiguration {

Registrable.get(current)!.call(current, this);
} else if ((def = Metadata.getOwn(resourceBaseName, current)) != null) {
} else if ((def = getMetadata(resourceBaseName, current)!) != null) {
def.register(this);
} else if (isClass(current)) {
singletonRegistration(current, current as Constructable).register(this);
} else if (isClass<StaticResourceType>(current)) {
if (isString((current).$au?.type)) {
const $au = current.$au;
const aliases = (current.aliases ?? emptyArray).concat($au.aliases ?? emptyArray);
let key = `${resourceBaseName}:${$au.type}:${$au.name}`;
if (!this.has(key, false)) {
aliasToRegistration(current, key).register(this);
if (!this.has(current, false)) {
singletonRegistration(current, current).register(this);
}
j = 0;
jj = aliases.length;
for (; j < jj; ++j) {
key = `${resourceBaseName}:${$au.type}:${aliases[j]}`;
if (!this.has(key, false)) {
aliasToRegistration(current, key).register(this);
}
}
} else {
// dev message for registering static resources that the key already registered
}
} else {
singletonRegistration(current, current as Constructable).register(this);
}
} else {

@@ -505,3 +536,6 @@ keys = Object.keys(current);

public find<TResType extends ResourceType>(key: string): TResType | null {
public find<TResType extends ResourceType>(kind: string, name: string): TResType | null;
public find<TResType extends ResourceType>(key: string): TResType | null;
public find<TResType extends ResourceType>(keyOrKind: string, name?: string): TResType | null {
const key = isString(name) ? `${resourceBaseName}:${keyOrKind}:${name}` : keyOrKind;
let container: Container = this;

@@ -532,3 +566,4 @@ let resolver = container.res[key];

private _jitRegister(keyAsValue: any, handler: Container): IResolver {
if (!isFunction(keyAsValue)) {
const $isRegistry = isRegistry(keyAsValue);
if (!isFunction(keyAsValue) && !$isRegistry) {
throw createMappedError(ErrorNames.unable_jit_non_constructor, keyAsValue);

@@ -541,3 +576,3 @@ }

if (isRegistry(keyAsValue)) {
if ($isRegistry) {
const registrationResolver = keyAsValue.register(handler, keyAsValue);

@@ -554,2 +589,3 @@ if (!(registrationResolver instanceof Object) || (registrationResolver as IResolver).resolve == null) {

// TODO(sayan): remove potential dead code
if (keyAsValue.$isInterface) {

@@ -571,3 +607,3 @@ throw createMappedError(ErrorNames.no_jit_interface, keyAsValue.friendlyName);

private readonly dependencies: Key[],
) {}
) { }

@@ -660,3 +696,3 @@ public construct(container: IContainer, dynamicDependencies?: unknown[]): Resolved<T> {

? [IResolvedInjection<R1>, ...IResolvedInjection<R2>]
: K extends { __resolved__: infer T }
: K extends InterfaceSymbol<infer T>
? T

@@ -733,6 +769,6 @@ : Resolved<K>;

const isClass = <T extends { prototype?: any }>(obj: T): obj is Class<any, T> =>
obj.prototype !== void 0;
const isClass = <T>(obj: unknown): obj is Class<any, T> =>
(obj as { prototype: object }).prototype !== void 0;
const isResourceKey = (key: Key): key is string =>
isString(key) && key.indexOf(':') > 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
import { IContainer, IFactory, IRegistry, IResolver, Injectable, InstanceProvider, InterfaceSymbol, Key, Resolved, inject } from './di';
import { IContainer, IFactory, IRegistry, IResolver, InstanceProvider, InterfaceSymbol, Key, Resolved, inject } from './di';
import { createContainer } from './di.container';

@@ -17,4 +17,4 @@ import { ErrorNames, createMappedError } from './errors';

return function (key: any) {
function Resolver(target: any, property?: string | number, descriptor?: PropertyDescriptor | number): void {
inject(Resolver)(target, property, descriptor);
function Resolver(target: any, context: DecoratorContext): void {
inject(Resolver)(target, context);
}

@@ -35,4 +35,4 @@

export const all = <T extends Key>(key: T, searchAncestors: boolean = false): IAllResolver<T> => {
function resolver(target: Injectable, property?: string | number, descriptor?: PropertyDescriptor | number): void {
inject(resolver)(target, property, descriptor);
function resolver(decorated: unknown, context: DecoratorContext): void {
inject(resolver)(decorated, context);
}

@@ -45,8 +45,5 @@

};
export type IAllResolver<T> = IResolver<Resolved<T>[]> & {
// type only hack
__isAll: undefined;
export type IAllResolver<T> = IResolver<readonly Resolved<T>[]> &
// any for decorator
(...args: unknown[]): any;
};
((decorated: unknown, context: DecoratorContext) => any);

@@ -129,5 +126,5 @@ /**

*/
export const ignore: IResolver<undefined> = /*@__PURE__*/objectAssign((target: Injectable, property?: string | number, descriptor?: PropertyDescriptor | number): void => {
inject(ignore)(target, property, descriptor);
}, { $isResolver: true, resolve: () => void 0 } as const);
export const ignore: IResolver<undefined> = /*@__PURE__*/objectAssign((decorated: unknown, context: DecoratorContext): void => {
inject(ignore)(decorated, context);
}, {$isResolver: true, resolve: () => void 0} as const);

@@ -276,5 +273,4 @@ /**

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const isInterface = <K>(key: any): key is InterfaceSymbol<K> => isFunction(key) && key.$isInterface === true;
const isInterface = <K>(key: any): key is InterfaceSymbol<K> => (key as {$isInterface?: boolean})?.$isInterface === true;
let newInstanceContainer: IContainer;

@@ -5,5 +5,5 @@ /* eslint-disable @typescript-eslint/no-explicit-any */

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { applyMetadataPolyfill } from '@aurelia/metadata';
import { initializeTC39Metadata } from '@aurelia/metadata';
applyMetadataPolyfill(Reflect, false, false);
initializeTC39Metadata();

@@ -13,4 +13,4 @@ import { isArrayIndex } from './functions';

import { Constructable, IDisposable } from './interfaces';
import { appendAnnotation, getAnnotationKeyFor, ResourceType } from './resource';
import { defineMetadata, getOwnMetadata, isFunction, isString } from './utilities';
import { getAnnotationKeyFor, ResourceType } from './resource';
import { defineMetadata, getMetadata, isFunction, isString } from './utilities';
import { singletonRegistration, cacheCallbackResult, transientRegistation } from './di.registration';

@@ -22,4 +22,10 @@ import { ErrorNames, createMappedError } from './errors';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type InterfaceSymbol<K = any> = (target: Injectable | AbstractInjectable, property: string | symbol | undefined, index?: number) => void;
export interface InterfaceSymbol<K = any> {
// We can activate decorator if the argument decorator proposal will be standardized by TC39 (https://github.com/tc39/proposal-class-method-parameter-decorators)
// (target: Injectable | AbstractInjectable, property: string | symbol | undefined, index?: number): void;
$isInterface: boolean;
friendlyName?: string;
register?(container: IContainer, key?: K): IResolver<K>;
toString?(): string;
}

@@ -95,2 +101,3 @@ // This interface exists only to break a circular type referencing issue in the IServiceLocator interface.

useResources(container: IContainer): void;
find<TResType extends ResourceType>(kind: string, name: string): TResType | null;
find<TResType extends ResourceType>(key: string): TResType | null;

@@ -185,18 +192,12 @@ }

const diParamTypesKeys = getAnnotationKeyFor('di:paramtypes');
const getAnnotationParamtypes = (Type: Constructable | Injectable): readonly Key[] | undefined => {
const key = getAnnotationKeyFor('di:paramtypes');
return getOwnMetadata(key, Type);
return getMetadata(diParamTypesKeys, Type);
};
const getDesignParamtypes = (Type: Constructable | Injectable): readonly Key[] | undefined =>
getOwnMetadata('design:paramtypes', Type);
getMetadata('design:paramtypes', Type);
const getOrCreateAnnotationParamTypes = (Type: Constructable | Injectable): Key[] => {
const key = getAnnotationKeyFor('di:paramtypes');
let annotationParamtypes = getOwnMetadata(key, Type);
if (annotationParamtypes === void 0) {
defineMetadata(key, annotationParamtypes = [], Type);
appendAnnotation(Type, key);
}
return annotationParamtypes;
const getOrCreateAnnotationParamTypes = (context: DecoratorContext): Key[] => {
return (context.metadata[diParamTypesKeys] ??= []) as Key[];
};

@@ -211,3 +212,3 @@

const key = getAnnotationKeyFor('di:dependencies');
let dependencies = getOwnMetadata(key, Type) as Key[] | undefined;
let dependencies = getMetadata<Key[] | undefined>(key, Type);
if (dependencies === void 0) {

@@ -272,4 +273,3 @@ // Type.length is the number of constructor parameters. If this is 0, it could mean the class has an empty constructor

defineMetadata(key, dependencies, Type);
appendAnnotation(Type, key);
defineMetadata(dependencies, Type, key);
}

@@ -286,60 +286,53 @@

export const createInterface = <K extends Key>(configureOrName?: string | ((builder: ResolverBuilder<K>) => IResolver<K>), configuror?: (builder: ResolverBuilder<K>) => IResolver<K>): InterfaceSymbol<K> => {
const configure = isFunction(configureOrName) ? configureOrName : configuror;
const friendlyName = (isString(configureOrName) ? configureOrName : undefined) ?? '(anonymous)';
const configure = isFunction(configureOrName) ? configureOrName : configuror;
const friendlyName = (isString(configureOrName) ? configureOrName : undefined) ?? '(anonymous)';
const Interface = function (target: Injectable | AbstractInjectable, property: string | symbol | undefined, index: number | undefined): void {
if (target == null || new.target !== undefined) {
throw createMappedError(ErrorNames.no_registration_for_interface, friendlyName);
}
const annotationParamtypes = getOrCreateAnnotationParamTypes(target as Injectable);
annotationParamtypes[index!] = Interface;
};
Interface.$isInterface = true;
Interface.friendlyName = friendlyName;
if (configure != null) {
Interface.register = (container: IContainer, key?: Key): IResolver<K> =>
configure(new ResolverBuilder(container, key ?? Interface));
}
Interface.toString = (): string => `InterfaceSymbol<${friendlyName}>`;
return Interface;
const Interface = {
// Old code kept with the hope that the argument decorator proposal will be standardized by TC39 (https://github.com/tc39/proposal-class-method-parameter-decorators)
// function(_target: Injectable | AbstractInjectable, _property: string | symbol | undefined, _index: number | undefined): void {
// if (target == null || new.target !== undefined) {
// throw createMappedError(ErrorNames.no_registration_for_interface, friendlyName);
// }
// const annotationParamtypes = getOrCreateAnnotationParamTypes(target as Injectable);
// annotationParamtypes[index!] = Interface;
// },
$isInterface: true,
friendlyName: friendlyName,
toString: (): string => `InterfaceSymbol<${friendlyName}>`,
register: configure != null
? (container: IContainer, key?: Key): IResolver<K> => configure(new ResolverBuilder(container, key ?? Interface))
: void 0,
};
return Interface;
};
export const inject = (...dependencies: Key[]): (target: Injectable, key?: string | number, descriptor?: PropertyDescriptor | number) => void => {
return (target: Injectable, key?: string | number, descriptor?: PropertyDescriptor | number): void => {
if (typeof descriptor === 'number') { // It's a parameter decorator.
const annotationParamtypes = getOrCreateAnnotationParamTypes(target);
const dep = dependencies[0];
if (dep !== void 0) {
annotationParamtypes[descriptor] = dep;
}
} else if (key) { // It's a property decorator. Not supported by the container without plugins.
const annotationParamtypes = getOrCreateAnnotationParamTypes((target as unknown as { constructor: Injectable }).constructor);
const dep = dependencies[0];
if (dep !== void 0) {
annotationParamtypes[key as number] = dep;
}
} else if (descriptor) { // It's a function decorator (not a Class constructor)
const fn = descriptor.value;
const annotationParamtypes = getOrCreateAnnotationParamTypes(fn);
let dep: Key;
let i = 0;
for (; i < dependencies.length; ++i) {
dep = dependencies[i];
if (dep !== void 0) {
annotationParamtypes[i] = dep;
export const inject = (...dependencies: Key[]): (decorated: unknown, context: DecoratorContext) => void => {
return (decorated: unknown, context: DecoratorContext): void => {
switch (context.kind) {
case 'class': {
const annotationParamtypes = getOrCreateAnnotationParamTypes(context);
let dep: Key;
let i = 0;
for (; i < dependencies.length; ++i) {
dep = dependencies[i];
if (dep !== void 0) {
annotationParamtypes[i] = dep;
}
}
break;
}
} else { // It's a class decorator.
const annotationParamtypes = getOrCreateAnnotationParamTypes(target);
let dep: Key;
let i = 0;
for (; i < dependencies.length; ++i) {
dep = dependencies[i];
case 'field': {
const annotationParamtypes: any = getOrCreateAnnotationParamTypes(context);
const dep = dependencies[0];
if (dep !== void 0) {
annotationParamtypes[i] = dep;
annotationParamtypes[context.name] = dep;
}
break;
}
// TODO(sayan): support getter injection - new feature
// TODO:
// support method parameter injection when the class-method-parameter-decorators proposal (https://github.com/tc39/proposal-class-method-parameter-decorators)
// reaches stage 4 and/or implemented by TS.
default:
throw createMappedError(ErrorNames.invalid_inject_decorator_usage, String(context.name), context.kind);
}

@@ -352,4 +345,4 @@ };

getDesignParamtypes,
getAnnotationParamtypes,
getOrCreateAnnotationParamTypes,
// getAnnotationParamtypes,
// getOrCreateAnnotationParamTypes,
getDependencies: getDependencies,

@@ -456,3 +449,3 @@ /**

function transientDecorator<T extends Constructable>(target: T & Partial<RegisterSelf<T>>):
function transientDecorator<T extends Constructable>(target: T & Partial<RegisterSelf<T>>, _context: ClassDecoratorContext):
T & RegisterSelf<T> {

@@ -483,5 +476,5 @@ return DI.transient(target);

*/
export function transient<T extends Constructable>(target: T & Partial<RegisterSelf<T>>): T & RegisterSelf<T>;
export function transient<T extends Constructable>(target?: T & Partial<RegisterSelf<T>>): T & RegisterSelf<T> | typeof transientDecorator {
return target == null ? transientDecorator : transientDecorator(target);
export function transient<T extends Constructable>(target: T & Partial<RegisterSelf<T>>, context: ClassDecoratorContext): T & RegisterSelf<T>;
export function transient<T extends Constructable>(target?: T & Partial<RegisterSelf<T>>, _context?: ClassDecoratorContext): T & RegisterSelf<T> | typeof transientDecorator {
return target == null ? transientDecorator : transientDecorator(target, _context!);
}

@@ -493,5 +486,3 @@

const singletonDecorator = <T extends Constructable>(target: T & Partial<RegisterSelf<T>>): T & RegisterSelf<T> => {
return decorateSingleton(target);
};
type SingletonDecorator = <T extends Constructable>(target: T & Partial<RegisterSelf<T>>, _context: ClassDecoratorContext) => T & RegisterSelf<T>;
/**

@@ -507,5 +498,5 @@ * Registers the decorated class as a singleton dependency; the class will only be created once. Each

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function singleton<T extends Constructable>(): typeof singletonDecorator;
export function singleton<T extends Constructable>(): SingletonDecorator;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function singleton<T extends Constructable>(options?: SingletonOptions): typeof singletonDecorator;
export function singleton<T extends Constructable>(options?: SingletonOptions): SingletonDecorator;
/**

@@ -522,10 +513,10 @@ * Registers the `target` class as a singleton dependency; the class will only be created once. Each

*/
export function singleton<T extends Constructable>(target: T & Partial<RegisterSelf<T>>): T & RegisterSelf<T>;
export function singleton<T extends Constructable>(targetOrOptions?: (T & Partial<RegisterSelf<T>>) | SingletonOptions): T & RegisterSelf<T> | typeof singletonDecorator {
if (isFunction(targetOrOptions)) {
return decorateSingleton(targetOrOptions);
}
return function <T extends Constructable>($target: T) {
return decorateSingleton($target, targetOrOptions);
};
export function singleton<T extends Constructable>(target: T & Partial<RegisterSelf<T>>, context: ClassDecoratorContext): T & RegisterSelf<T>;
export function singleton<T extends Constructable>(targetOrOptions?: (T & Partial<RegisterSelf<T>>) | SingletonOptions, _context?: ClassDecoratorContext): T & RegisterSelf<T> | SingletonDecorator {
return isFunction(targetOrOptions)
// The decorator is applied without options. Example: `@singleton()` or `@singleton`
? decorateSingleton(targetOrOptions)
: function <T extends Constructable>($target: T, _ctx: ClassDecoratorContext) {
return decorateSingleton($target, targetOrOptions);
};
}

@@ -532,0 +523,0 @@

@@ -34,2 +34,3 @@ /* eslint-disable prefer-template */

invalid_module_transform_input = 21,
invalid_inject_decorator_usage = 22,
}

@@ -63,2 +64,3 @@ _END_CONST_ENUM();

// [ErrorNames.module_loader_received_null]: `Module loader received null/undefined input. Expected Object.`,
[ErrorNames.invalid_inject_decorator_usage]: `The @inject decorator on the target ('{{0}}') type '{{1}}' is not supported.`,
};

@@ -65,0 +67,0 @@

@@ -242,18 +242,18 @@ import { ErrorNames, createMappedError } from './errors';

*/
// eslint-disable-next-line @typescript-eslint/ban-types
export const bound = <T extends Function>(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> => {
return {
configurable: true,
enumerable: descriptor.enumerable,
get(): T {
const boundFn = descriptor.value!.bind(this) as TypedPropertyDescriptor<T>;
Reflect.defineProperty(this, key, {
value: boundFn,
writable: true,
configurable: true,
enumerable: descriptor.enumerable,
});
return boundFn as T;
},
};
export const bound = <
TThis extends object,
TArgs extends unknown[],
TReturn>(
originalMethod: (this: TThis, ...args: TArgs) => TReturn,
context: ClassMethodDecoratorContext<TThis, (this: TThis, ...args: TArgs) => TReturn>,
): void => {
const methodName = context.name as string;
context.addInitializer(function (this: TThis) {
Reflect.defineProperty(this, methodName, {
value: originalMethod.bind(this),
writable: true,
configurable: true,
enumerable: false,
});
});
};

@@ -260,0 +260,0 @@

@@ -114,2 +114,3 @@ export {

type ResourceDefinition,
type StaticResourceType,
type ResourceType,

@@ -116,0 +117,0 @@ fromAnnotationOrDefinitionOrTypeOrDefault,

@@ -10,3 +10,3 @@ export interface IDisposable {

export type Class<T, C = {}> = C & {
export type Class<T, TStaticProps = {}> = TStaticProps & {
readonly prototype: T;

@@ -13,0 +13,0 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any

@@ -1,2 +0,1 @@

import { Metadata } from '@aurelia/metadata';
import { createInterface, IContainer, IRegistry } from './di';

@@ -8,3 +7,3 @@ import { instanceRegistration, singletonRegistration } from './di.registration';

import { getAnnotationKeyFor } from './resource';
import { createObject, defineMetadata, isFunction, objectFreeze } from './utilities';
import { createObject, defineMetadata, getMetadata, isFunction, objectFreeze } from './utilities';
import { resolve } from './di.container';

@@ -180,8 +179,7 @@ import { all, optional } from './di.resolvers';

key: getAnnotationKeyFor('logger-sink-handles'),
define<TSink extends ISink>(target: Constructable<TSink>, definition: SinkDefinition) {
defineMetadata(this.key, definition.handles, target.prototype);
return target;
define<TSink extends Constructable<ISink>>(target: TSink, definition: SinkDefinition) {
defineMetadata(definition.handles, target, this.key);
},
getHandles<TSink extends ISink>(target: Constructable<TSink> | TSink) {
return Metadata.get(this.key, target) as LogLevel[] | undefined;
getHandles<TSink extends ISink>(target: TSink): Exclude<LogLevel, typeof none>[] | undefined {
return getMetadata(this.key, target.constructor);
},

@@ -191,4 +189,6 @@ });

export const sink = (definition: SinkDefinition) => {
return <TSink extends ISink>(target: Constructable<TSink>): Constructable<TSink> =>
LoggerSink.define(target, definition);
return <TSink extends Constructable<ISink>>(_target: TSink, context: ClassDecoratorContext<TSink>): void =>
context.addInitializer(function (this) {
LoggerSink.define(this, definition);
});
};

@@ -195,0 +195,0 @@

import { createInterface } from './di';
import { resourceBaseName } from './resource';
import { getOwnMetadata, isFunction } from './utilities';
import { getMetadata, isFunction } from './utilities';
import { ErrorNames, createMappedError } from './errors';

@@ -100,3 +100,3 @@

isConstructable = (value as Constructable).prototype !== void 0;
definition = getOwnMetadata(resourceBaseName, value) ?? null;
definition = getMetadata(resourceBaseName, value) ?? null;
break;

@@ -103,0 +103,0 @@ default:

import { IContainer } from './di';
import { Constructable } from './interfaces';
import { defineMetadata, getOwnMetadata, objectFreeze } from './utilities';
import { defineMetadata, getMetadata, objectFreeze } from './utilities';
export type StaticResourceType<TDef extends object = object> = {
readonly aliases?: string[];
readonly $au?: PartialResourceDefinition<{
type: string;
} & TDef>;
};
export type ResourceType<

@@ -13,5 +20,3 @@ TUserType extends Constructable = Constructable,

new (...args: any[]) => TResInstance & TUserInstance
) & {
readonly aliases?: readonly string[];
} & TResType & TUserType;
) & StaticResourceType & TResType & TUserType;

@@ -42,3 +47,3 @@ export type ResourceDefinition<

export type PartialResourceDefinition<TDef extends {} = {}> = {
export type PartialResourceDefinition<TDef extends object = object> = {
readonly name: string;

@@ -65,5 +70,5 @@ readonly aliases?: readonly string[];

export const appendAnnotation = (target: Constructable, key: string): void => {
const keys = getOwnMetadata(annoBaseName, target) as string[];
const keys = getMetadata<string[]>(annoBaseName, target);
if (keys === void 0) {
defineMetadata(annoBaseName, [key], target);
defineMetadata([key], target, annoBaseName);
} else {

@@ -78,9 +83,9 @@ keys.push(key);

set(target: Constructable, prop: string, value: unknown): void {
defineMetadata(getAnnotationKeyFor(prop), value, target);
defineMetadata(value, target, getAnnotationKeyFor(prop));
},
get: (target: Constructable, prop: string): unknown => getOwnMetadata(getAnnotationKeyFor(prop), target),
get: (target: Constructable, prop: string): unknown => getMetadata(getAnnotationKeyFor(prop), target),
getKeys(target: Constructable): readonly string[] {
let keys = getOwnMetadata(annoBaseName, target) as string[];
let keys = getMetadata<string[]>(annoBaseName, target);
if (keys === void 0) {
defineMetadata(annoBaseName, keys = [], target);
defineMetadata(keys = [], target, annoBaseName);
}

@@ -130,3 +135,3 @@ return keys;

): Required<TDef>[K] {
let value = getOwnMetadata(getAnnotationKeyFor(name as string), Type) as TDef[K] | undefined;
let value = getMetadata<TDef[K] | undefined>(getAnnotationKeyFor(name as string), Type);
if (value === void 0) {

@@ -157,3 +162,3 @@ value = def[name];

): V {
let value = getOwnMetadata(getAnnotationKeyFor(name as string), Type) as V;
let value = getMetadata<V>(getAnnotationKeyFor(name as string), Type);
if (value === void 0) {

@@ -160,0 +165,0 @@ value = Type[name] as unknown as V;

@@ -6,4 +6,4 @@ import { Metadata } from '@aurelia/metadata';

/** @internal */ export const safeString = String;
/** @internal */ export const getOwnMetadata = Metadata.getOwn;
/** @internal */ export const hasOwnMetadata = Metadata.hasOwn;
/** @internal */ export const getMetadata = Metadata.get;
/** @internal */ export const hasMetadata = Metadata.has;
/** @internal */ export const defineMetadata = Metadata.define;

@@ -10,0 +10,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc