Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ncjsm

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ncjsm - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0

15

CHANGELOG.md

@@ -5,2 +5,17 @@ # Changelog

## [4.0.0](https://github.com/medikoo/ncjsm/compare/v3.0.0...v4.0.0) (2019-10-08)
### Bug Fixes
- Fix requireUncached moduleIds validation ([eb2e026](https://github.com/medikoo/ncjsm/commit/eb2e026))
### Features
- By default crash resolve with MODULE_NOT_FOUND if module not found ([0e446c4](https://github.com/medikoo/ncjsm/commit/0e446c4))
### BREAKING CHANGES
- `resolve` and `resolve/sync` now by default crash with MODULE_NOT_FOUND error, if module is not found. Old behavior was that `null` was returned.
To maintain old behavior { silent: true } option needs to be passed
## [3.0.0](https://github.com/medikoo/ncjsm/compare/v2.3.0...v3.0.0) (2019-09-02)

@@ -7,0 +22,0 @@

1

is-module-not-found-error/index.js

@@ -21,3 +21,4 @@ // Whether given error is an error thrown by require internals in case module

if (!error || typeof error.message !== "string") return false;
if (error.code !== "MODULE_NOT_FOUND") return false;
return resolveMessage(error) === pattern.replace(pathToken, path);
};

14

lib/get-node-resolver.js

@@ -6,3 +6,4 @@ // Generates module path resolver for Node.js

const ensureString = require("type/string/ensure")
const isObject = require("type/object/is")
, ensureString = require("type/string/ensure")
, getResolver = require("../get-resolver")

@@ -13,8 +14,15 @@ , { resolve } = require("path");

const resolveModule = getResolver([".js", ".json", ".node"], confirmFile, resolvePackageMain);
return function (dir, path) {
return function (dir, path, options = {}) {
if (!isObject(options)) options = {};
dir = resolve(ensureString(dir));
path = ensureString(path);
if (!path) throw new TypeError("Empty string is not a valid require path");
return resolveModule(dir, path);
return resolveModule(dir, path).then(result => {
if (result) return result;
if (options.silent) return null;
const error = new Error(`Cannot find module '${ path }'`);
error.code = "MODULE_NOT_FOUND";
throw error;
});
};
};
{
"name": "ncjsm",
"version": "3.0.0",
"version": "4.0.0",
"description": "CJS (Node.js) style modules resolver",

@@ -5,0 +5,0 @@ "author": "Mariusz Nowak <medyk@medikoo.com> (http://www.medikoo.com/)",

@@ -39,3 +39,3 @@ [![Build status][nix-build-image]][nix-build-url]

Asynchronously resolves module path against provided directory path.
Returns promise. If no matching module was found, promise resolves with `null` .
Returns promise.
If module is found, then promise resolves with an object, containing two properties:

@@ -46,12 +46,26 @@

If no matching module was found, promise is rejected with `MODULE_NOT_FOUND` error (unless `silent: true` is passed with options (passed as third argument), then it resolves with `null`)
```javascript
var resolve = require("ncjsm/resolve");
const resolve = require("ncjsm/resolve");
// Asynchronously resolve path for 'foo' module against current path
resolve(__dirname, "foo").done(function (pathData) {
if (!pathData) {
// 'foo' module doesn't exist
} else {
resolve(__dirname, "foo").then(
function (pathData) {
// 'foo' module found at fooModulePath
},
function (error) {
if (error.code === "MODULE_NOT_FOUND") {
// 'foo' module doesn't exist
}
}
);
// `silent` option, prevents module not found rejections:
resolve(__dirname, "foo", { silent: true }).then(function (pathData) {
if (pathData) {
// 'foo' module found at fooModulePath
} else {
// 'foo' module doesn't exist
}
});

@@ -64,14 +78,23 @@ ```

Synchronously resolves module path against provided directory path.
If matching module was found then object with `targetPath` and `realPath` properties is returned.
Synchronously resolves module path against provided directory path. Otherwise works same as `resolve`
```javascript
var resolveSync = require("ncjsm/resolve/sync");
const resolveSync = require("ncjsm/resolve/sync");
// Synchronously resolve path for 'foo' module against current path
var fooModulePath = resolveSync(__dirname, "foo");
if (!fooModulePath) {
let fooModulePathData;
try {
fooModulePathData = resolveSync(__dirname, "foo");
// 'foo' module found at fooModulePath
} catch (error) {
if (error.code === "MODULE_NOT_FOUND") {
// 'foo' module doesn't exist
}
}
fooModulePathData = resolveSync(__dirname, "foo", { silent: true });
if (fooModulePathData) {
// 'foo' module found
} else {
// 'foo' module doesn't exist
} else {
// 'foo' module found
}

@@ -78,0 +101,0 @@ ```

@@ -13,3 +13,3 @@ "use strict";

moduleIds = aFrom(moduleIds);
if (!moduleIds) throw new TypeError("Minimum one moduleId is required");
if (!moduleIds.length) throw new TypeError("Minimum one moduleId is required");
} else {

@@ -16,0 +16,0 @@ moduleIds = [ensureString(moduleIds)];

@@ -40,2 +40,2 @@ // Sync module resolver

module.exports = function (dir, path) { return resolver(dir, path).value; };
module.exports = function (dir, path, options = {}) { return resolver(dir, path, options).value; };
"use strict";
const PassThru = require("../../utils/pass-thru");
const PassThru = require("../../utils/pass-thru")
, isModuleNotFoundError = require("../../is-module-not-found-error");

@@ -12,3 +13,8 @@ const resolver = function () { return new PassThru(null); };

a.throws(() => { resolve("asdfa", ""); }, TypeError);
a(resolve("asdfa", "elo").value, null);
try {
resolve("asdfa", "elo");
throw new Error("Unexpected");
} catch (error) {
a(isModuleNotFoundError(error, "elo"), true);
}
};

@@ -5,4 +5,5 @@ /* eslint max-lines: "off" */

const noop = require("es5-ext/function/noop")
, { resolve } = require("path");
const noop = require("es5-ext/function/noop")
, { resolve } = require("path")
, isModuleNotFoundError = require("../../is-module-not-found-error");

@@ -21,2 +22,4 @@ const {

const unexpected = () => { throw new Error("Unexpected"); };
module.exports = (t, a) =>

@@ -36,3 +39,6 @@ Promise.all([

}),
t(playgroundDir, "./foo.json").then(value => { a(value, null); }),
t(playgroundDir, "./foo.json").then(unexpected, error => {
a(isModuleNotFoundError(error, "./foo.json"), true);
}),
t(playgroundDir, "./foo.json", { silent: true }).then(value => a(value, null)),
t(playgroundDir, "./other").then(value => {

@@ -146,3 +152,5 @@ a.deep(value, {

}),
t(playgroundDir, "outer/boo.json").then(value => { a(value, null); }),
t(playgroundDir, "outer/boo.json").then(unexpected, error => {
a(isModuleNotFoundError(error, "outer/boo.json"), true);
}),
t(playgroundDir, "outer3").then(value => {

@@ -160,3 +168,5 @@ a.deep(value, {

}),
t(playgroundDir, "nested/elo").then(value => { a(value, null); }),
t(playgroundDir, "nested/elo").then(unexpected, error => {
a(isModuleNotFoundError(error, "nested/elo"), true);
}),
t(`${ playgroundDir }/node_modules/outer`, "outer3").then(value => {

@@ -168,4 +178,4 @@ a.deep(value, {

}),
t(`${ playgroundDir }/node_modules/outer`, "project/foo").then(value => {
a(value, null);
t(`${ playgroundDir }/node_modules/outer`, "project/foo").then(unexpected, error => {
a(isModuleNotFoundError(error, "project/foo"), true);
}),

@@ -183,3 +193,4 @@ t(`${ playgroundDir }/node_modules/outer`, "nested/elo").then(value => {

t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "project/foo").then(
value => { a(value, null); }
unexpected,
error => { a(isModuleNotFoundError(error, "project/foo"), true); }
),

@@ -220,3 +231,5 @@ t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "outer").then(value => {

}),
t(playgroundDir, "./invalid-file-link").then(value => a(value, null)),
t(playgroundDir, "./invalid-file-link").then(unexpected, error => {
a(isModuleNotFoundError(error, "./invalid-file-link"), true);
}),
t(playgroundDir, "./invalid-file-link-with-a-fallback").then(value => {

@@ -259,5 +272,7 @@ a.deep(value, {

}),
t(playgroundDir, "./invalid-dir-link").then(value => a(value, null))
t(playgroundDir, "./invalid-dir-link").then(unexpected, error => {
a(isModuleNotFoundError(error, "./invalid-dir-link"), true);
})
]).then(teardownDirLinks, error => teardownDirLinks.then(() => { throw error; }))
)
]).then(noop);

@@ -5,4 +5,5 @@ /* eslint max-lines: "off" */

const noop = require("es5-ext/function/noop")
, { resolve } = require("path");
const noop = require("es5-ext/function/noop")
, { resolve } = require("path")
, isModuleNotFoundError = require("../../is-module-not-found-error");

@@ -30,3 +31,9 @@ const {

});
a(t(playgroundDir, "./foo.json"), null);
try {
t(playgroundDir, "./foo.json");
throw new Error("Unexpected");
} catch (error) {
a(isModuleNotFoundError(error, "./foo.json"), true);
}
a(t(playgroundDir, "./foo.json", { silent: true }), null);
a.deep(t(playgroundDir, "./other"), {

@@ -105,3 +112,8 @@ targetPath: resolve(`${ playgroundDir }/other.js`),

});
a(t(playgroundDir, "outer/boo.json"), null);
try {
t(playgroundDir, "outer/boo.json");
throw new Error("Unexpected");
} catch (error) {
a(isModuleNotFoundError(error, "outer/boo.json"), true);
}
a.deep(t(playgroundDir, "outer3"), {

@@ -111,3 +123,8 @@ targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`),

});
a(t(playgroundDir, "nested/elo"), null);
try {
t(playgroundDir, "nested/elo");
throw new Error("Unexpected");
} catch (error) {
a(isModuleNotFoundError(error, "nested/elo"), true);
}
a.deep(t(`${ playgroundDir }/node_modules/outer`, "outer3"), {

@@ -117,3 +134,8 @@ targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`),

});
a(t(`${ playgroundDir }/node_modules/outer`, "project/foo"), null);
try {
t(`${ playgroundDir }/node_modules/outer`, "project/foo");
throw new Error("Unexpected");
} catch (error) {
a(isModuleNotFoundError(error, "project/foo"), true);
}
a.deep(t(`${ playgroundDir }/node_modules/outer`, "nested/elo"), {

@@ -123,3 +145,8 @@ targetPath: resolve(`${ playgroundDir }/node_modules/outer/node_modules/nested/elo.js`),

});
a(t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "project/foo"), null);
try {
t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "project/foo");
throw new Error("Unexpected");
} catch (error) {
a(isModuleNotFoundError(error, "project/foo"), true);
}
a.deep(t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "outer"), {

@@ -152,3 +179,8 @@ targetPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`),

});
a(t(playgroundDir, "./invalid-file-link"), null);
try {
t(playgroundDir, "./invalid-file-link");
throw new Error("Unexpected");
} catch (error) {
a(isModuleNotFoundError(error, "./invalid-file-link"), true);
}
a.deep(t(playgroundDir, "./invalid-file-link-with-a-fallback"), {

@@ -191,3 +223,8 @@ targetPath: resolve(

});
a(t(playgroundDir, "./invalid-dir-link"), null);
try {
t(playgroundDir, "./invalid-dir-link");
throw new Error("Unexpected");
} catch (error) {
a(isModuleNotFoundError(error, "./invalid-dir-link"), true);
}
} catch (error) {

@@ -194,0 +231,0 @@ testError = error;

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