Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
siloContext,
serviceName,
{ injectorContext, autoloading },
) {
let initializer = this._initializers.get(serviceName);
if (initializer) {
return initializer;
}
// The auto loader must only have static dependencies
// and we have to do this check here to avoid caching
// non-autoloading request and then be blocked by an
// autoloader dep that waits for that cached load
if (autoloading) {
throw new YError(E_AUTOLOADER_DYNAMIC_DEPENDENCY, serviceName);
}
debug('No service provider:', serviceName);
let initializerPromise = this._initializerResolvers.get(serviceName);
if (initializerPromise) {
return await initializerPromise;
}
initializerPromise = (async () => {
if (!this._initializers.get(AUTOLOAD)) {
throw new YError(E_UNMATCHED_DEPENDENCY, serviceName);
}
debug(`Loading the $autoload service to lookup for: ${serviceName}.`);
try {
) {
const matches = source.match(
/^\s*(?:async\s+function(?:\s+\w+)?|async)\s*\(\s*\{\s*([^{}}]+)\s*\}/,
);
if (!matches) {
if (!source.match(/^\s*async/)) {
throw new YError('E_NON_ASYNC_INITIALIZER', source);
}
if (
allowEmpty &&
source.match(/^\s*(?:async\s+function(?:\s+\w+)?|async)\s*\(\s*\)/)
) {
return [];
}
throw new YError('E_AUTO_INJECTION_FAILURE', source);
}
return matches[1]
.trim()
.replace(/,$/, '')
.split(/\s*,\s*/)
.map(
injection =>
(injection.includes('=') ? '?' : '') +
injection
.split(/\s*=\s*/)
.shift()
.split(/\s*:\s*/)
.shift(),
)
.filter(injection => !/[)(\][]/.test(injection));
export function parseInjections(
source,
{ allowEmpty = false } = { allowEmpty: false },
) {
const matches = source.match(
/^\s*(?:async\s+function(?:\s+\w+)?|async)\s*\(\s*\{\s*([^{}}]+)\s*\}/,
);
if (!matches) {
if (!source.match(/^\s*async/)) {
throw new YError('E_NON_ASYNC_INITIALIZER', source);
}
if (
allowEmpty &&
source.match(/^\s*(?:async\s+function(?:\s+\w+)?|async)\s*\(\s*\)/)
) {
return [];
}
throw new YError('E_AUTO_INJECTION_FAILURE', source);
}
return matches[1]
.trim()
.replace(/,$/, '')
.split(/\s*,\s*/)
.map(
injection =>
it('should disallow new runs', async () => {
$.register(constant('ENV', ENV));
$.register(constant('time', time));
$.register(provider(hashProvider, 'hash', ['ENV']));
$.register(provider(hashProvider, 'hash1', ['ENV']));
const dependencies = await $.run(['$instance']);
assert.equal(typeof dependencies.$instance.destroy, 'function');
await dependencies.$instance.destroy();
try {
await $.run(['ENV', 'hash', 'hash1']);
throw new YError('E_UNEXPECTED_SUCCES');
} catch (err) {
assert.equal(err.code, 'E_INSTANCE_DESTROYED');
}
});
});
return async function $autoload(name) {
return mockedDepsHash[name]
? Promise.resolve({
path: `./services/${name}`,
initializer: mockedDepsHash[name],
})
: Promise.reject(new YError('E_UNMATCHED_DEPENDENCY', name));
};
},
initializer[SPECIAL_PROPS.INJECT].reduce(
(finalHash, dependencyDeclaration) => {
const { serviceName, mappedName } = parseDependencyDeclaration(
dependencyDeclaration,
);
finalHash[serviceName] = servicesHash[mappedName];
return finalHash;
},
{},
),
);
if (!serviceDescriptor) {
debug('Provider did not return a descriptor:', serviceName);
return Promise.reject(new YError(E_BAD_SERVICE_PROVIDER, serviceName));
}
debug('Successfully initialized a service descriptor:', serviceName);
if (serviceDescriptor.fatalErrorPromise) {
debug('Registering service descriptor error promise:', serviceName);
siloContext.errorsPromises.push(serviceDescriptor.fatalErrorPromise);
}
siloContext.servicesDescriptors.set(serviceName, serviceDescriptor);
} catch (err) {
debug('Error initializing a service descriptor:', serviceName, err.stack);
if (E_UNMATCHED_DEPENDENCY === err.code) {
throw YError.wrap(
...[err, E_UNMATCHED_DEPENDENCY, serviceName].concat(err.params),
);
}
throw err;
}
register(initializer) {
if (this.shutdownPromise) {
throw new YError(E_INSTANCE_DESTROYED);
}
if (typeof initializer !== 'function') {
throw new YError(E_BAD_INITIALIZER, initializer);
}
initializer[SPECIAL_PROPS.INJECT] = initializer[SPECIAL_PROPS.INJECT] || [];
initializer[SPECIAL_PROPS.OPTIONS] =
initializer[SPECIAL_PROPS.OPTIONS] || {};
initializer[SPECIAL_PROPS.TYPE] =
initializer[SPECIAL_PROPS.TYPE] || ALLOWED_INITIALIZER_TYPES[0];
if (!initializer[SPECIAL_PROPS.NAME]) {
throw new YError(E_ANONYMOUS_ANALYZER, initializer[SPECIAL_PROPS.NAME]);
}
if (
initializer[SPECIAL_PROPS.NAME] === AUTOLOAD &&
!initializer[SPECIAL_PROPS.OPTIONS].singleton
) {
throw new YError(E_BAD_AUTOLOADER, initializer[SPECIAL_PROPS.OPTIONS]);
}
if (!ALLOWED_INITIALIZER_TYPES.includes(initializer[SPECIAL_PROPS.TYPE])) {
function readFunctionName(aFunction) {
if (typeof aFunction !== 'function') {
throw new YError('E_AUTO_NAMING_FAILURE', typeof aFunction);
}
const functionName = parseName(aFunction.name || '');
if (!functionName) {
throw new YError('E_AUTO_NAMING_FAILURE', aFunction.name);
}
return functionName;
}