@adonisjs/fold
Advanced tools
Comparing version 6.1.5 to 6.1.6
@@ -1,2 +0,2 @@ | ||
/// <reference types="@types/node" /> | ||
/// <reference types="node" /> | ||
import { EventEmitter } from 'events'; | ||
@@ -14,21 +14,22 @@ export interface TracerContract extends EventEmitter { | ||
useProxies(): this; | ||
bind(name: string, callback: BindCallback): void; | ||
singleton(name: string, callback: BindCallback): void; | ||
bind(namespace: string, callback: BindCallback): void; | ||
singleton(namespace: string, callback: BindCallback): void; | ||
alias(namespace: string, alias: string): void; | ||
autoload(directoryPath: string, namespace: string): void; | ||
clearAutoloadCache(namespace?: string, clearRequireCache?: boolean): void; | ||
fake(name: string, callback: BindCallback): void; | ||
use<T extends any = any>(name: string): T; | ||
useEsm<T extends any = any>(name: string): T; | ||
make<T extends any = any>(name: string, args?: string[]): T; | ||
useFake<T extends any = any>(name: string): T; | ||
hasFake(name: string): boolean; | ||
hasAlias(name: string): boolean; | ||
fake(namespace: string, callback: BindCallback): void; | ||
use<T extends any = any>(namespace: string | LookupNode): T; | ||
useEsm<T extends any = any>(namespace: string | LookupNode): T; | ||
make<T extends any = any>(namespace: string | LookupNode, args?: string[]): T; | ||
useFake<T extends any = any>(namespace: string): T; | ||
hasFake(namespace: string): boolean; | ||
hasAlias(namespace: string): boolean; | ||
hasBinding(namespace: string, checkAliases?: boolean): boolean; | ||
getAliasNamespace(name: string): string | undefined; | ||
getAliasNamespace(namespace: string): string | undefined; | ||
isAutoloadNamespace(namespace: string): boolean; | ||
getAutoloadBaseNamespace(namespace: string): string | undefined; | ||
restore(name: string): void; | ||
restore(namespace: string): void; | ||
with(namespaces: string[], cb: (...args: any[]) => void): void; | ||
call<T extends object, K extends keyof T = any>(target: T, method: K, args: any[]): any; | ||
lookup(namespace: string, prefixNamespace?: string): LookupNode | null; | ||
} | ||
@@ -40,2 +41,6 @@ export declare type Binding = { | ||
}; | ||
export declare type LookupNode = { | ||
namespace: string; | ||
type: 'binding' | 'autoload'; | ||
}; | ||
export declare type AutoloadCacheItem = { | ||
@@ -42,0 +47,0 @@ diskPath: string; |
@@ -1,2 +0,2 @@ | ||
import { IocContract, BindCallback } from '../Contracts'; | ||
import { IocContract, BindCallback, LookupNode } from '../Contracts'; | ||
export declare class Ioc implements IocContract { | ||
@@ -26,2 +26,3 @@ private _emitEvents; | ||
private _resolve; | ||
private _isLookUpNode; | ||
private _resolveAndMake; | ||
@@ -39,10 +40,10 @@ private _clearRequireCache; | ||
fake(namespace: string, callback: BindCallback): void; | ||
use<T extends any = any>(namespace: string): T; | ||
useEsm<T extends any = any>(namespace: string): T; | ||
make<T extends any = any>(namespace: any, args?: string[]): T; | ||
use<T extends any = any>(node: string | LookupNode): T; | ||
useEsm<T extends any = any>(node: string | LookupNode): T; | ||
make<T extends any = any>(node: any, args?: string[]): T; | ||
useFake<T extends any = any>(namespace: string): T; | ||
hasFake(name: string): boolean; | ||
hasAlias(name: string): boolean; | ||
hasFake(namespace: string): boolean; | ||
hasAlias(namespace: string): boolean; | ||
hasBinding(namespace: string, checkAliases?: boolean): boolean; | ||
getAliasNamespace(name: string): string | undefined; | ||
getAliasNamespace(namespace: string): string | undefined; | ||
isAutoloadNamespace(namespace: string): boolean; | ||
@@ -53,2 +54,3 @@ getAutoloadBaseNamespace(namespace: string): string | undefined; | ||
call<T extends object, K extends keyof T = any>(target: T, method: K, args?: any[]): any; | ||
lookup(namespace: string, prefixNamespace?: string): null | LookupNode; | ||
} |
@@ -8,2 +8,3 @@ "use strict"; | ||
const path_1 = require("path"); | ||
const esmResolver_1 = require("@poppinss/utils/build/src/esmResolver"); | ||
const Tracer_1 = __importDefault(require("./Tracer")); | ||
@@ -24,19 +25,24 @@ const IoCProxy_1 = require("./IoCProxy"); | ||
} | ||
_resolveBinding(name) { | ||
const binding = this._bindings[name]; | ||
this.tracer.in(name, !!binding.cachedValue); | ||
if (binding.singleton && !binding.cachedValue) { | ||
binding.cachedValue = binding.callback(this); | ||
_resolveBinding(namespace, asEsm) { | ||
const binding = this._bindings[namespace]; | ||
if (!binding) { | ||
throw new Error(`Cannot resolve ${namespace} binding from IoC container`); | ||
} | ||
if (binding.singleton) { | ||
this.tracer.out(); | ||
return binding.cachedValue; | ||
this.tracer.in(namespace, !!binding.cachedValue); | ||
let value; | ||
if (binding.singleton && binding.cachedValue !== undefined) { | ||
value = binding.cachedValue; | ||
} | ||
const value = binding.callback(this); | ||
else if (binding.singleton) { | ||
value = binding.cachedValue = binding.callback(this); | ||
} | ||
else { | ||
value = binding.callback(this); | ||
} | ||
this.tracer.out(); | ||
return value; | ||
return asEsm ? this._toEsm(value) : value; | ||
} | ||
_ensureCallback(callback, message) { | ||
if (typeof (callback) !== 'function') { | ||
throw new Error(message); | ||
throw new utils_1.Exception(message, 500, 'E_RUNTIME_EXCEPTION'); | ||
} | ||
@@ -49,12 +55,12 @@ } | ||
_autoload(namespace, normalizeEsm) { | ||
const baseNamespace = this.getAutoloadBaseNamespace(namespace); | ||
const cacheEntry = this._autoloadsCache.get(namespace); | ||
this.tracer.in(namespace, !!cacheEntry); | ||
if (!cacheEntry) { | ||
const baseNamespace = this.getAutoloadBaseNamespace(namespace); | ||
const absPath = this._makeRequirePath(baseNamespace, namespace); | ||
const importValue = normalizeEsm ? utils_1.esmRequire(absPath) : require(absPath); | ||
this._autoloadsCache.set(namespace, { diskPath: absPath, cachedValue: importValue }); | ||
this._autoloadsCache.set(namespace, { diskPath: absPath, cachedValue: require(absPath) }); | ||
} | ||
this.tracer.out(); | ||
return this._autoloadsCache.get(namespace).cachedValue; | ||
const importValue = this._autoloadsCache.get(namespace).cachedValue; | ||
return normalizeEsm ? esmResolver_1.esmResolver(importValue) : importValue; | ||
} | ||
@@ -102,29 +108,32 @@ _isConstructorObject(value) { | ||
} | ||
_resolve(name, asEsm) { | ||
if (this.hasBinding(name)) { | ||
return asEsm ? this._toEsm(this._resolveBinding(name)) : this._resolveBinding(name); | ||
_resolve(node, asEsm) { | ||
if (typeof (node) !== 'string') { | ||
switch (node.type) { | ||
case 'binding': | ||
return this._resolveBinding(node.namespace, asEsm); | ||
case 'autoload': | ||
return this._autoload(node.namespace, !asEsm); | ||
} | ||
return; | ||
} | ||
if (this.hasAlias(name)) { | ||
return this._resolve(this.getAliasNamespace(name), asEsm); | ||
} | ||
if (this.isAutoloadNamespace(name)) { | ||
return this._autoload(name, !asEsm); | ||
} | ||
return this._requireModule(name); | ||
return this._requireModule(node); | ||
} | ||
_resolveAndMake(name, args) { | ||
if (typeof (name) !== 'string') { | ||
return this._makeInstanceOf(name, args); | ||
_isLookUpNode(node) { | ||
return node && node.type && node.namespace; | ||
} | ||
_resolveAndMake(node, args) { | ||
if (this._isLookUpNode(node)) { | ||
switch (node.type) { | ||
case 'binding': | ||
return this._resolveBinding(node.namespace, false); | ||
case 'autoload': | ||
const value = this._autoload(node.namespace, true); | ||
return this._makeInstanceOf(value, args); | ||
} | ||
return; | ||
} | ||
if (this.hasBinding(name)) { | ||
return this._resolveBinding(name); | ||
if (typeof (node) !== 'string') { | ||
return this._makeInstanceOf(node, args); | ||
} | ||
if (this.hasAlias(name)) { | ||
return this._resolve(this.getAliasNamespace(name), false); | ||
} | ||
if (this.isAutoloadNamespace(name)) { | ||
const value = this._autoload(name, true); | ||
return this._makeInstanceOf(value, args); | ||
} | ||
return this._requireModule(name); | ||
return this._requireModule(node); | ||
} | ||
@@ -200,21 +209,23 @@ _clearRequireCache(modulePath) { | ||
} | ||
use(namespace) { | ||
const value = this._resolve(namespace, false); | ||
if (!this._useProxies) { | ||
use(node) { | ||
const lookedupNode = typeof (node) === 'string' ? this.lookup(node) : node; | ||
const value = this._resolve(lookedupNode || node, false); | ||
if (!this._useProxies || !lookedupNode) { | ||
return value; | ||
} | ||
if (this._isObject(value)) { | ||
return new IoCProxy_1.IoCProxyObject(namespace, value, this); | ||
return new IoCProxy_1.IoCProxyObject(lookedupNode.namespace, value, this); | ||
} | ||
if (this._isClass(value)) { | ||
return IoCProxy_1.IocProxyClass(namespace, value, this); | ||
return IoCProxy_1.IocProxyClass(lookedupNode.namespace, value, this); | ||
} | ||
return value; | ||
} | ||
useEsm(namespace) { | ||
const value = this._resolve(namespace, true); | ||
useEsm(node) { | ||
const lookedupNode = typeof (node) === 'string' ? this.lookup(node) : node; | ||
const value = this._resolve(lookedupNode || node, true); | ||
if (!value.__esModule) { | ||
throw new Error(`${namespace} must be an ES module`); | ||
throw new Error(`${node} must be an ES module`); | ||
} | ||
if (!this._useProxies || !value.default) { | ||
if (!this._useProxies || !value.default || !lookedupNode) { | ||
return value; | ||
@@ -224,3 +235,3 @@ } | ||
return { | ||
default: new IoCProxy_1.IoCProxyObject(namespace, value.default, this), | ||
default: new IoCProxy_1.IoCProxyObject(lookedupNode.namespace, value.default, this), | ||
}; | ||
@@ -230,3 +241,3 @@ } | ||
return { | ||
default: IoCProxy_1.IocProxyClass(namespace, value.default, this), | ||
default: IoCProxy_1.IocProxyClass(lookedupNode.namespace, value.default, this), | ||
}; | ||
@@ -236,12 +247,13 @@ } | ||
} | ||
make(namespace, args) { | ||
const value = this._resolveAndMake(namespace, args); | ||
if (!this._useProxies) { | ||
make(node, args) { | ||
const lookedupNode = typeof (node) === 'string' ? this.lookup(node) : node; | ||
const value = this._resolveAndMake(lookedupNode || node, args); | ||
if (!this._useProxies || !this._isLookUpNode(lookedupNode)) { | ||
return value; | ||
} | ||
if (this._isObject(value)) { | ||
return new IoCProxy_1.IoCProxyObject(namespace, value, this); | ||
return new IoCProxy_1.IoCProxyObject(lookedupNode.namespace, value, this); | ||
} | ||
if (this._isClass(value)) { | ||
return IoCProxy_1.IocProxyClass(namespace, value, this); | ||
return IoCProxy_1.IocProxyClass(lookedupNode.namespace, value, this); | ||
} | ||
@@ -260,7 +272,7 @@ return value; | ||
} | ||
hasFake(name) { | ||
return this._fakes.has(name); | ||
hasFake(namespace) { | ||
return this._fakes.has(namespace); | ||
} | ||
hasAlias(name) { | ||
return !!this._aliases[name]; | ||
hasAlias(namespace) { | ||
return !!this._aliases[namespace]; | ||
} | ||
@@ -274,4 +286,4 @@ hasBinding(namespace, checkAliases = false) { | ||
} | ||
getAliasNamespace(name) { | ||
return this._aliases[name]; | ||
getAliasNamespace(namespace) { | ||
return this._aliases[namespace]; | ||
} | ||
@@ -304,3 +316,33 @@ isAutoloadNamespace(namespace) { | ||
} | ||
lookup(namespace, prefixNamespace) { | ||
if (!namespace) { | ||
throw new utils_1.Exception('Empty string cannot be used as IoC container reference', 500, 'E_INVALID_IOC_NAMESPACE'); | ||
} | ||
if (namespace.startsWith('/')) { | ||
namespace = namespace.substr(1); | ||
} | ||
else if (prefixNamespace) { | ||
namespace = `${prefixNamespace.replace(/\/$/, '')}/${namespace}`; | ||
} | ||
if (this.hasBinding(namespace)) { | ||
return { | ||
type: 'binding', | ||
namespace, | ||
}; | ||
} | ||
if (this.hasAlias(namespace)) { | ||
return { | ||
type: 'binding', | ||
namespace: this.getAliasNamespace(namespace), | ||
}; | ||
} | ||
if (this.isAutoloadNamespace(namespace)) { | ||
return { | ||
type: 'autoload', | ||
namespace: namespace, | ||
}; | ||
} | ||
return null; | ||
} | ||
} | ||
exports.Ioc = Ioc; |
@@ -1,2 +0,2 @@ | ||
/// <reference types="@types/node" /> | ||
/// <reference types="node" /> | ||
import Emitter from 'events'; | ||
@@ -3,0 +3,0 @@ import { TracerContract } from '../Contracts'; |
{ | ||
"name": "@adonisjs/fold", | ||
"version": "6.1.5", | ||
"version": "6.1.6", | ||
"description": "Dependency manager and ioc container for your next NodeJs application", | ||
@@ -37,4 +37,4 @@ "scripts": { | ||
"@adonisjs/mrm-preset": "^2.1.0", | ||
"@poppinss/dev-utils": "^1.0.0", | ||
"@types/node": "^12.7.2", | ||
"@poppinss/dev-utils": "^1.0.1", | ||
"@types/node": "^12.7.4", | ||
"commitizen": "^4.0.3", | ||
@@ -44,4 +44,4 @@ "cz-conventional-changelog": "^3.0.2", | ||
"doctoc": "^1.4.0", | ||
"husky": "^3.0.4", | ||
"japa": "^3.0.0", | ||
"husky": "^3.0.5", | ||
"japa": "^3.0.1", | ||
"mrm": "^1.2.2", | ||
@@ -55,3 +55,3 @@ "np": "^5.0.3", | ||
"typedoc-plugin-external-module-name": "^2.1.0", | ||
"typedoc-plugin-markdown": "^2.1.4", | ||
"typedoc-plugin-markdown": "^2.1.9", | ||
"typescript": "^3.6.2" | ||
@@ -89,3 +89,3 @@ }, | ||
"dependencies": { | ||
"@poppinss/utils": "^1.0.4" | ||
"@poppinss/utils": "^1.0.5" | ||
}, | ||
@@ -92,0 +92,0 @@ "publishConfig": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43938
666
Updated@poppinss/utils@^1.0.5