Socket
Socket
Sign inDemoInstall

awilix

Package Overview
Dependencies
Maintainers
1
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

awilix - npm Package Compare versions

Comparing version 4.2.7 to 4.3.0

lib/load-module-native.js

4

CHANGELOG.md

@@ -0,1 +1,5 @@

# v4.3.0
- Add support for [Native Node ES modules](https://nodejs.org/api/esm.html) on Node v14+ ([#211](https://github.com/jeffijoe/awilix/pull/211), [Richard Simko](https://github.com/richardsimko))
# v4.2.7

@@ -2,0 +6,0 @@

161

lib/awilix.module.js

@@ -5,2 +5,3 @@ import { sync } from 'glob';

import { camelCase } from 'camel-case';
import { importModule } from './load-module-native.js';

@@ -533,2 +534,27 @@ /**

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
/**

@@ -1026,2 +1052,5 @@ * Lifetime types.

*
* @param {boolean} opts.esModules
* Set to `true` to use Node's native ECMAScriptModules modules
*
* @return {Object}

@@ -1031,50 +1060,96 @@ * Returns an object describing the result.

function loadModules(dependencies, globPatterns, opts) {
opts !== null && opts !== void 0 ? opts : (opts = {});
const container = dependencies.container;
opts = optsWithDefaults(opts);
const modules = dependencies.listModules(globPatterns, opts);
const result = modules.map((m) => {
const items = [];
const loaded = dependencies.require(m.path);
// Meh, it happens.
if (!loaded) {
return items;
if (opts.esModules) {
return loadEsModules(dependencies, container, modules, opts);
}
else {
const result = modules.map((m) => {
const loaded = dependencies.require(m.path);
return parseLoadedModule(loaded, m);
});
return registerModules(result, container, modules, opts);
}
}
/**
* Loads the modules using native ES6 modules and the async import()
* @param {AwilixContainer} container
* @param {ModuleDescriptor[]} modules
* @param {LoadModulesOptions} opts
*/
function loadEsModules(dependencies, container, modules, opts) {
return __awaiter(this, void 0, void 0, function* () {
const importPromises = [];
for (const m of modules) {
importPromises.push(dependencies.require(m.path));
}
if (isFunction(loaded)) {
// for module.exports = ...
items.push({
name: m.name,
path: m.path,
value: loaded,
opts: m.opts,
});
return items;
const imports = yield Promise.all(importPromises);
const result = [];
for (let i = 0; i < modules.length; i++) {
result.push(parseLoadedModule(imports[i], modules[i]));
}
if (loaded.default && isFunction(loaded.default)) {
// ES6 default export
return registerModules(result, container, modules, opts);
});
}
/**
* Parses the module which has been required
*
* @param {any} loaded
* @param {ModuleDescriptor} m
*/
function parseLoadedModule(loaded, m) {
const items = [];
// Meh, it happens.
if (!loaded) {
return items;
}
if (isFunction(loaded)) {
// for module.exports = ...
items.push({
name: m.name,
path: m.path,
value: loaded,
opts: m.opts,
});
return items;
}
if (loaded.default && isFunction(loaded.default)) {
// ES6 default export
items.push({
name: m.name,
path: m.path,
value: loaded.default,
opts: m.opts,
});
}
// loop through non-default exports, but require the RESOLVER property set for
// it to be a valid service module export.
for (const key of Object.keys(loaded)) {
if (key === 'default') {
// default case handled separately due to its different name (file name)
continue;
}
if (isFunction(loaded[key]) && RESOLVER in loaded[key]) {
items.push({
name: m.name,
name: key,
path: m.path,
value: loaded.default,
value: loaded[key],
opts: m.opts,
});
}
// loop through non-default exports, but require the RESOLVER property set for
// it to be a valid service module export.
for (const key of Object.keys(loaded)) {
if (key === 'default') {
// default case handled separately due to its different name (file name)
continue;
}
if (isFunction(loaded[key]) && RESOLVER in loaded[key]) {
items.push({
name: key,
path: m.path,
value: loaded[key],
opts: m.opts,
});
}
}
return items;
});
result
}
return items;
}
/**
* Registers the modules
*
* @param {ModuleDescriptorVal[][]} modulesToRegister
* @param {AwilixContainer} container
* @param {ModuleDescriptor[]} modules
* @param {LoadModulesOptions} opts
*/
function registerModules(modulesToRegister, container, modules, opts) {
modulesToRegister
.reduce((acc, cur) => acc.concat(cur), [])

@@ -1459,4 +1534,10 @@ .filter((x) => x)

};
loadModules(_loadModulesDeps, globPatterns, opts);
return container;
if (opts === null || opts === void 0 ? void 0 : opts.esModules) {
_loadModulesDeps.require = importModule;
return loadModules(_loadModulesDeps, globPatterns, opts).then(() => container);
}
else {
loadModules(_loadModulesDeps, globPatterns, opts);
return container;
}
}

@@ -1463,0 +1544,0 @@ /**

@@ -44,3 +44,3 @@ import { GlobWithOptions } from './list-modules';

*/
loadModules(globPatterns: Array<string | GlobWithOptions>, options?: LoadModulesOptions): this;
loadModules<ESM extends boolean = false>(globPatterns: Array<string | GlobWithOptions>, options?: LoadModulesOptions<ESM>): ESM extends false ? this : Promise<this>;
/**

@@ -47,0 +47,0 @@ * Adds a single registration that using a pre-constructed resolver.

@@ -12,2 +12,3 @@ "use strict";

const errors_1 = require("./errors");
const load_module_native_js_1 = require("./load-module-native.js");
/**

@@ -342,4 +343,10 @@ * Family tree symbol.

};
load_modules_1.loadModules(_loadModulesDeps, globPatterns, opts);
return container;
if (opts === null || opts === void 0 ? void 0 : opts.esModules) {
_loadModulesDeps.require = load_module_native_js_1.importModule;
return load_modules_1.loadModules(_loadModulesDeps, globPatterns, opts).then(() => container);
}
else {
load_modules_1.loadModules(_loadModulesDeps, globPatterns, opts);
return container;
}
}

@@ -346,0 +353,0 @@ /**

@@ -23,2 +23,8 @@ import * as glob from 'glob';

/**
* Metadata of the module as well as the loaded module itself.
*/
export interface LoadedModuleDescriptor extends ModuleDescriptor {
value: unknown;
}
/**
* A glob pattern with associated registration options.

@@ -25,0 +31,0 @@ */

@@ -8,6 +8,7 @@ import { ModuleDescriptor, GlobWithOptions, listModules } from './list-modules';

*/
export interface LoadModulesOptions {
export interface LoadModulesOptions<ESM extends boolean = false> {
cwd?: string;
formatName?: NameFormatter | BuiltInNameFormatters;
resolverOptions?: BuildResolverOptions<any>;
esModules?: ESM;
}

@@ -34,32 +35,10 @@ /**

container: AwilixContainer;
require(path: string): any;
require(path: string): any | Promise<any>;
}
/**
* Given an array of glob strings, will call `require`
* on them, and call their default exported function with the
* container as the first parameter.
*
* @param {AwilixContainer} dependencies.container
* The container to install loaded modules in.
*
* @param {Function} dependencies.listModules
* The listModules function to use for listing modules.
*
* @param {Function} dependencies.require
* The require function - it's a dependency because it makes testing easier.
*
* @param {String[]} globPatterns
* The array of globs to use when loading modules.
*
* @param {Object} opts
* Passed to `listModules`, e.g. `{ cwd: '...' }`.
*
* @param {(string, ModuleDescriptor) => string} opts.formatName
* Used to format the name the module is registered with in the container.
*
* @return {Object}
* Returns an object describing the result.
* The list of loaded modules
*/
export declare function loadModules(dependencies: LoadModulesDeps, globPatterns: string | Array<string | GlobWithOptions>, opts?: LoadModulesOptions): {
loadedModules: ModuleDescriptor[];
};
export interface LoadModulesResult {
loadedModules: Array<ModuleDescriptor>;
}
export declare function loadModules<ESM extends boolean = false>(dependencies: LoadModulesDeps, globPatterns: string | Array<string | GlobWithOptions>, opts?: LoadModulesOptions<ESM>): ESM extends true ? Promise<LoadModulesResult> : LoadModulesResult;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -34,2 +43,5 @@ exports.loadModules = void 0;

*
* @param {boolean} opts.esModules
* Set to `true` to use Node's native ECMAScriptModules modules
*
* @return {Object}

@@ -39,50 +51,97 @@ * Returns an object describing the result.

function loadModules(dependencies, globPatterns, opts) {
opts !== null && opts !== void 0 ? opts : (opts = {});
const container = dependencies.container;
opts = optsWithDefaults(opts, container);
const modules = dependencies.listModules(globPatterns, opts);
const result = modules.map((m) => {
const items = [];
const loaded = dependencies.require(m.path);
// Meh, it happens.
if (!loaded) {
return items;
if (opts.esModules) {
return loadEsModules(dependencies, container, modules, opts);
}
else {
const result = modules.map((m) => {
const loaded = dependencies.require(m.path);
return parseLoadedModule(loaded, m);
});
return registerModules(result, container, modules, opts);
}
}
exports.loadModules = loadModules;
/**
* Loads the modules using native ES6 modules and the async import()
* @param {AwilixContainer} container
* @param {ModuleDescriptor[]} modules
* @param {LoadModulesOptions} opts
*/
function loadEsModules(dependencies, container, modules, opts) {
return __awaiter(this, void 0, void 0, function* () {
const importPromises = [];
for (const m of modules) {
importPromises.push(dependencies.require(m.path));
}
if (utils_1.isFunction(loaded)) {
// for module.exports = ...
items.push({
name: m.name,
path: m.path,
value: loaded,
opts: m.opts,
});
return items;
const imports = yield Promise.all(importPromises);
const result = [];
for (let i = 0; i < modules.length; i++) {
result.push(parseLoadedModule(imports[i], modules[i]));
}
if (loaded.default && utils_1.isFunction(loaded.default)) {
// ES6 default export
return registerModules(result, container, modules, opts);
});
}
/**
* Parses the module which has been required
*
* @param {any} loaded
* @param {ModuleDescriptor} m
*/
function parseLoadedModule(loaded, m) {
const items = [];
// Meh, it happens.
if (!loaded) {
return items;
}
if (utils_1.isFunction(loaded)) {
// for module.exports = ...
items.push({
name: m.name,
path: m.path,
value: loaded,
opts: m.opts,
});
return items;
}
if (loaded.default && utils_1.isFunction(loaded.default)) {
// ES6 default export
items.push({
name: m.name,
path: m.path,
value: loaded.default,
opts: m.opts,
});
}
// loop through non-default exports, but require the RESOLVER property set for
// it to be a valid service module export.
for (const key of Object.keys(loaded)) {
if (key === 'default') {
// default case handled separately due to its different name (file name)
continue;
}
if (utils_1.isFunction(loaded[key]) && resolvers_1.RESOLVER in loaded[key]) {
items.push({
name: m.name,
name: key,
path: m.path,
value: loaded.default,
value: loaded[key],
opts: m.opts,
});
}
// loop through non-default exports, but require the RESOLVER property set for
// it to be a valid service module export.
for (const key of Object.keys(loaded)) {
if (key === 'default') {
// default case handled separately due to its different name (file name)
continue;
}
if (utils_1.isFunction(loaded[key]) && resolvers_1.RESOLVER in loaded[key]) {
items.push({
name: key,
path: m.path,
value: loaded[key],
opts: m.opts,
});
}
}
return items;
});
result
}
return items;
}
/**
* Registers the modules
*
* @param {ModuleDescriptorVal[][]} modulesToRegister
* @param {AwilixContainer} container
* @param {ModuleDescriptor[]} modules
* @param {LoadModulesOptions} opts
*/
function registerModules(modulesToRegister, container, modules, opts) {
modulesToRegister
.reduce((acc, cur) => acc.concat(cur), [])

@@ -95,3 +154,2 @@ .filter((x) => x)

}
exports.loadModules = loadModules;
/**

@@ -98,0 +156,0 @@ * Returns a new options object with defaults applied.

{
"name": "awilix",
"version": "4.2.7",
"version": "4.3.0",
"description": "Extremely powerful dependency injection container.",

@@ -53,25 +53,25 @@ "main": "lib/awilix.js",

"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/core": "^7.12.8",
"@babel/plugin-transform-runtime": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"@babel/runtime": "^7.12.1",
"@babel/preset-env": "^7.12.7",
"@babel/runtime": "^7.12.5",
"@types/glob": "^7.1.3",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.2",
"@types/node": "^14.14.9",
"@types/prettier": "^2.1.5",
"babel-jest": "^26.6.1",
"babel-jest": "^26.6.3",
"coveralls": "^3.1.0",
"husky": "^4.3.0",
"istanbul": "^0.4.5",
"jest": "^26.6.1",
"lint-staged": "^10.4.2",
"prettier": "^2.1.2",
"jest": "^26.6.3",
"lint-staged": "^10.5.2",
"prettier": "^2.2.0",
"rimraf": "^3.0.2",
"rollup": "^2.32.1",
"rollup": "^2.33.3",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-typescript2": "^0.28.0",
"rollup-plugin-typescript2": "^0.29.0",
"smid": "^0.1.1",
"ts-jest": "^26.4.1",
"ts-jest": "^26.4.4",
"tslib": "^2.0.3",

@@ -81,7 +81,8 @@ "tslint": "^6.1.3",

"tslint-config-standard": "^9.0.0",
"typescript": "^4.0.3"
"typescript": "^4.1.2"
},
"dependencies": {
"camel-case": "^4.1.1",
"glob": "^7.1.6"
"glob": "^7.1.6",
"rollup-plugin-copy": "^3.3.0"
},

@@ -121,3 +122,4 @@ "lint-staged": {

"__tests__",
"lib"
"lib",
"src/load-module-native.js"
],

@@ -124,0 +126,0 @@ "moduleFileExtensions": [

@@ -554,3 +554,3 @@ # Awilix

**Important**: Auto-loading relies on `glob` and therefore does not with
**Important**: Auto-loading relies on `glob` and therefore does not work with
bundlers like Webpack, Rollup and Browserify.

@@ -1068,2 +1068,3 @@

the lifetime, injection mode and more of the loaded modules.
- `opts.esModules`: Loads modules using Node's native ES modules. This is only supported on Node 14.0+ and should only be used if you're using the [Native Node ES modules](https://nodejs.org/api/esm.html)

@@ -1070,0 +1071,0 @@ Example:

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