pax_global_header00006660000000000000000000000064141735437070014525gustar00rootroot0000000000000052 comment=b45070ec1341223670e5ed1a106a8686664ea235 ncjsm-4.3.0/000077500000000000000000000000001417354370700126435ustar00rootroot00000000000000ncjsm-4.3.0/.editorconfig000066400000000000000000000004461417354370700153240ustar00rootroot00000000000000# EditorConfig is awesome: http://EditorConfig.org # top-most EditorConfig file root = true [*] charset = utf-8 end_of_line = lf insert_final_newline = true indent_style = tab trim_trailing_whitespace = true [*.{md,yml}] indent_size = 2 indent_style = space trim_trailing_whitespace = false ncjsm-4.3.0/.github/000077500000000000000000000000001417354370700142035ustar00rootroot00000000000000ncjsm-4.3.0/.github/FUNDING.yml000066400000000000000000000000201417354370700160100ustar00rootroot00000000000000github: medikoo ncjsm-4.3.0/.github/workflows/000077500000000000000000000000001417354370700162405ustar00rootroot00000000000000ncjsm-4.3.0/.github/workflows/integrate.yml000066400000000000000000000135241417354370700207520ustar00rootroot00000000000000# main only name: Integrate on: push: branches: [main] env: FORCE_COLOR: 1 jobs: linuxNode16: name: "[Linux] Node.js v16: Unit tests" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v16-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: npm-v16-${{ runner.os }}-${{ github.ref }}- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 16.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm test windowsNode16: name: "[Windows] Node.js v16: Unit tests" runs-on: windows-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v16-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: npm-v16-${{ runner.os }}-${{ github.ref }}- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 16.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm test linuxNode17: name: "[Linux] Node.js 17: Unit tests on npm package" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v17-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: npm-v17-${{ runner.os }}-${{ github.ref }}- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 17.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Build local package run: npm pack - name: Unit tests on npm package run: | NAME=`cat package.json | grep '"name": "' | grep -oE ": .*" | grep -oE '[^: ",]+'` VERSION=`cat package.json | grep '"version": "' | grep -oE ": .*" | grep -oE '[^: ",]+'` tar zxf "${NAME}-${VERSION}.tgz" cp -R test "package" ln -s "$(pwd)"/node_modules "package/node_modules" cd "package" npm test linuxNode14: name: "[Linux] Node.js v14: Unit tests with coverage" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v14-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: npm-v14-${{ runner.os }}-${{ github.ref }}- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 14.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm run coverage - name: Push coverage run: npx codecov linuxNode12: name: "[Linux] Node.js v12: Unit tests" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v12-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: npm-v12-${{ runner.os }}-${{ github.ref }}- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 12.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm test tagIfNewVersion: name: Tag if new version runs-on: ubuntu-latest needs: [linuxNode16, windowsNode16, linuxNode17, linuxNode14, linuxNode12] steps: - name: Checkout repository uses: actions/checkout@v2 with: # Ensure to have complete history of commits pushed with given push operation # It's loose and imperfect assumption that no more than 30 commits will be pushed at once fetch-depth: 30 # Tag needs to be pushed with real user token, otherwise pushed tag won't trigger the actions workflow # Hence we're passing 'serverless-ci' user authentication token token: ${{ secrets.USER_GITHUB_TOKEN }} - name: Tag if new version if: github.event.before != '0000000000000000000000000000000000000000' # Skip on first commit run: | NEW_VERSION=`git diff -U0 ${{ github.event.before }} package.json | grep '"version": "' | tail -n 1 | grep -oE "[0-9]+\.[0-9]+\.[0-9]+"` || : if [ -n "$NEW_VERSION" ]; then git tag v$NEW_VERSION git push --tags fi ncjsm-4.3.0/.github/workflows/publish.yml000066400000000000000000000033271417354370700204360ustar00rootroot00000000000000# Version tags only name: Publish on: push: tags: - v[0-9]+.[0-9]+.[0-9]+ jobs: publish: name: Publish runs-on: ubuntu-latest env: # It'll work with secrets.GITHUB_TOKEN (which is provided by GitHub unconditionally) # Still then release author would be "github-actions" GITHUB_TOKEN: ${{ secrets.USER_GITHUB_TOKEN }} steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v16-${{ runner.os }}-refs/heads/main-${{ hashFiles('package.json') }} - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 16.x registry-url: https://registry.npmjs.org - name: Publish new version # Note: Setting NODE_AUTH_TOKEN as job|workspace wide env var won't work # as it appears actions/setup-node sets own value env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} run: npm publish # Normally we have a guarantee that deps are already there, still it may not be the case when: # - `main` build for same commit failed (and we still pushed tag manually) # - We've pushed tag manually before `master` build finalized - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Publish release notes run: | TEMP_ARRAY=($(echo $GITHUB_REF | tr "/" "\n")) TAG=${TEMP_ARRAY[@]: -1} npx github-release-from-cc-changelog $TAG ncjsm-4.3.0/.github/workflows/validate.yml000066400000000000000000000123701417354370700205570ustar00rootroot00000000000000# PR's only name: Validate on: pull_request: branches: [main] env: FORCE_COLOR: 1 jobs: linuxNode16: name: "[Linux] Node.js v16: Lint, Formatting, Unit tests" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v16-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: | npm-v16-${{ runner.os }}-${{ github.ref }}- npm-v16-${{ runner.os }}-refs/heads/main- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 16.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Validate Prettier formatting run: npm run prettier-check:updated - name: Validate ESLint rules run: npm run lint:updated - name: Unit tests run: npm test windowsNode16: name: "[Windows] Node.js v16: Unit tests" runs-on: windows-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v16-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: | npm-v16-${{ runner.os }}-${{ github.ref }}- npm-v16-${{ runner.os }}-refs/heads/main- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 16.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm test linuxNode17: name: "[Linux] Node.js 17: Unit tests on npm package" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v17-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: | npm-v17-${{ runner.os }}-${{ github.ref }}- npm-v17-${{ runner.os }}-refs/heads/main- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 17.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Build local package run: npm pack - name: Unit tests on npm package run: | NAME=`cat package.json | grep '"name": "' | grep -oE ": .*" | grep -oE '[^: ",]+'` VERSION=`cat package.json | grep '"version": "' | grep -oE ": .*" | grep -oE '[^: ",]+'` tar zxf "${NAME}-${VERSION}.tgz" cp -R test "package" ln -s "$(pwd)"/node_modules "package/node_modules" cd "package" npm test linuxNode14: name: "[Linux] Node.js v14: Unit tests with coverage" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v14-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: | npm-v14-${{ runner.os }}-${{ github.ref }}- npm-v14-${{ runner.os }}-refs/heads/main- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 14.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm run coverage - name: Push coverage run: npx codecov linuxNode12: name: "[Linux] Node.js v12: Unit tests" runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Retrieve dependencies from cache id: cacheNpm uses: actions/cache@v2 with: path: | ~/.npm node_modules key: npm-v12-${{ runner.os }}-${{ github.ref }}-${{ hashFiles('package.json') }} restore-keys: | npm-v12-${{ runner.os }}-${{ github.ref }}- npm-v12-${{ runner.os }}-refs/heads/main- - name: Install Node.js and npm uses: actions/setup-node@v1 with: node-version: 12.x - name: Install dependencies if: steps.cacheNpm.outputs.cache-hit != 'true' run: | npm update --no-save npm update --save-dev --no-save - name: Unit tests run: npm test ncjsm-4.3.0/.gitignore000066400000000000000000000001061417354370700146300ustar00rootroot00000000000000/.nyc_output /coverage /node_modules npm-debug.log /package-lock.json ncjsm-4.3.0/CHANGELOG.md000066400000000000000000000147541417354370700144670ustar00rootroot00000000000000# Changelog All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. ## [4.3.0](https://github.com/medikoo/ncjsm/compare/v4.2.0...v4.3.0) (2022-01-24) ### Features - Support async callback in `requireUncached` ([91fdb82](https://github.com/medikoo/ncjsm/commit/91fdb82b171651ff5740a3d6e6a12f632415e0e9)) ## [4.2.0](https://github.com/medikoo/ncjsm/compare/v4.1.0...v4.2.0) (2021-04-30) ### Features - **Require Uncached:** Support clearing all require cache for callback ([75c2229](https://github.com/medikoo/ncjsm/commit/75c2229854885e257fdd061699600208d438b561)) ## [4.1.0](https://github.com/medikoo/ncjsm/compare/v4.0.1...v4.1.0) (2020-07-22) ### Features - `ignoreExternal` option for `getDependencies` ([158075b](https://github.com/medikoo/ncjsm/commit/158075bea3f66ac77bae5341fd474930172047a2)) ### [4.0.1](https://github.com/medikoo/ncjsm/compare/v4.0.0...v4.0.1) (2019-10-25) ### Bug Fixes - Fix main module resolution in case pkg.main leads to broken path ([331dd1c](https://github.com/medikoo/ncjsm/commit/331dd1c)) ### Tests - Add missing test ([2469fe9](https://github.com/medikoo/ncjsm/commit/2469fe9)) ## [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) ### Features - Change resolve return value format ([747493e](https://github.com/medikoo/ncjsm/commit/747493e)) - Expose realPath on resolve return object ([5ad6b23](https://github.com/medikoo/ncjsm/commit/5ad6b23)) ### Tests - Configure directory links tests ([988c92a](https://github.com/medikoo/ncjsm/commit/988c92a)) - Cover symlink cases in resolve tests ([c97d4a0](https://github.com/medikoo/ncjsm/commit/c97d4a0)) - Fix symlink type ([a371ed0](https://github.com/medikoo/ncjsm/commit/a371ed0)) - Improve naming ([81785d6](https://github.com/medikoo/ncjsm/commit/81785d6)) - Improve var name ([523498a](https://github.com/medikoo/ncjsm/commit/523498a)) - Improve var naming ([e48b5f0](https://github.com/medikoo/ncjsm/commit/e48b5f0)) - Maintain support for Node.js v6 ([3341664](https://github.com/medikoo/ncjsm/commit/3341664)) - Make symlink tests portable to windows ([0b57b2a](https://github.com/medikoo/ncjsm/commit/0b57b2a)) - Refactor to async/await ([6f20188](https://github.com/medikoo/ncjsm/commit/6f20188)) - Rename util ([55a6d6f](https://github.com/medikoo/ncjsm/commit/55a6d6f)) - Skip symlink tests if not capable to create them ([53cc946](https://github.com/medikoo/ncjsm/commit/53cc946)) ### BREAKING CHANGES - 'resolve', and 'resolve/sync' utils now resolve with objects that expose path on `targetPath` property (so far path was returned directly) # [2.3.0](https://github.com/medikoo/ncjsm/compare/v2.2.0...v2.3.0) (2019-04-30) ### Features - improve validation in isModuleNotFoundError ([8b2e641](https://github.com/medikoo/ncjsm/commit/8b2e641)) # [2.2.0](https://github.com/medikoo/ncjsm/compare/v2.1.0...v2.2.0) (2019-04-30) ### Bug Fixes - Fix isModuleNotFound crash in Node.js v12 ([c9b9cc8](https://github.com/medikoo/ncjsm/commit/c9b9cc8)) ### Features - Bring ES3 support to isModuleNotFoundError ([e6d0132](https://github.com/medikoo/ncjsm/commit/e6d0132)) # [2.1.0](https://github.com/medikoo/ncjsm/compare/v2.0.1...v2.1.0) (2019-04-18) ### Features - support ignoreMissing option in getDependencies ([20baf14](https://github.com/medikoo/ncjsm/commit/20baf14)) ## [2.0.1](https://github.com/medikoo/ncjsm/compare/v2.0.0...v2.0.1) (2019-03-18) ### Bug Fixes - ensure ES5 support in requireUncached ([bab908d](https://github.com/medikoo/ncjsm/commit/bab908d)) # [2.0.0](https://github.com/medikoo/ncjsm/compare/v1.6.0...v2.0.0) (2019-03-12) ### Features - improve resolution of dynamic requires ([fa1457b](https://github.com/medikoo/ncjsm/commit/fa1457b)) ### reafactor - convert to ES2015 ([ed2aa37](https://github.com/medikoo/ncjsm/commit/ed2aa37)) ### BREAKING CHANGES - Drop support for Node.js v4 and below # [1.6.0](https://github.com/medikoo/ncjsm/compare/v1.5.0...v1.6.0) (2019-02-07) ### Features - rename cjs-module to ncjsm ([743b95c](https://github.com/medikoo/ncjsm/commit/743b95c)) # [1.5.0](https://github.com/medikoo/cjs-module/compare/v1.4.1...v1.5.0) (2018-12-28) ### Features - improve error messaging ([ca04d59](https://github.com/medikoo/cjs-module/commit/ca04d59)) ## [1.4.1](https://github.com/medikoo/cjs-module/compare/v1.4.0...v1.4.1) (2018-12-28) # [1.4.0](https://github.com/medikoo/cjs-module/compare/v1.3.1...v1.4.0) (2018-09-14) ### Features - isModuleNotFoundError util ([130b066](https://github.com/medikoo/cjs-module/commit/130b066)) ## [1.3.1](https://github.com/medikoo/cjs-module/compare/v1.3.0...v1.3.1) (2018-07-26) ### Bug Fixes - ensure full isolation in require-uncached ([090eede](https://github.com/medikoo/cjs-module/commit/090eede)) # [1.3.0](https://github.com/medikoo/cjs-module/compare/v1.2.2...v1.3.0) (2018-05-14) ### Features - requireUncached util ([1f79012](https://github.com/medikoo/cjs-module/commit/1f79012)) ## [1.2.2](https://github.com/medikoo/cjs-module/compare/v1.2.1...v1.2.2) (2017-09-18) ### Bug Fixes - resolution of dirs referenced in main of package.json ([455ce5a](https://github.com/medikoo/cjs-module/commit/455ce5a)) ## [1.2.1](https://github.com/medikoo/cjs-module/compare/v1.2.0...v1.2.1) (2017-06-20) ### Bug Fixes - ensure to not resolve builtin modules ([1107e40](https://github.com/medikoo/cjs-module/commit/1107e40)) # [1.2.0](https://github.com/medikoo/cjs-module/compare/v1.1.0...v1.2.0) (2017-06-16) ### Features - getDependencies utility ([af1c3d0](https://github.com/medikoo/cjs-module/commit/af1c3d0)) ### Bug Fixes - ensure proper tests resolution on windows ([4e4960b](https://github.com/medikoo/cjs-module/commit/4e4960b)) ## Old Changelog See `CHANGES` ncjsm-4.3.0/CHANGES000066400000000000000000000003131417354370700136330ustar00rootroot00000000000000For new changelog see CHANGELOG.md v1.1.0 -- 2015.09.10 * Added modules: * isPackageRoot * resolvePackageRoot * resolveProjectRoot * isPathExternal (in utils) v1.0.0 -- 2015.06.29 * Initial ncjsm-4.3.0/LICENSE000066400000000000000000000014051417354370700136500ustar00rootroot00000000000000ISC License Copyright (c) 2015-2022, Mariusz Nowak, @medikoo, medikoo.com Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 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. ncjsm-4.3.0/README.md000066400000000000000000000156731417354370700141360ustar00rootroot00000000000000[![Build status][build-image]][build-url] [![Tests coverage][cov-image]][cov-url] [![npm version][npm-image]][npm-url] # ncjsm ## (Node.js) CJS modules resolver Environment agnostic (Node.js) CJS modules resolver. It implements a _strict_ version of Node.js modules resolution logic, differences are as follows: - [Loading from global folders](https://nodejs.org/api/all.html#all_loading_from_the_global_folders) is not supported - Only Unix path separators (`/`) are supported in require's _path_ arguments (_Background: even though Node.js internally seems to follow Windows path separator in Windows environment, it won't work in \*nix environments, and even in Window env it's [not reliable](https://github.com/nodejs/node/issues/6049) so by all means should be avoided_) - There's no awareness of node.js [core modules](https://nodejs.org/api/all.html#all_core_modules) e.g. `resolve(dir, 'fs')` will naturally result with _null_ ### Installation $ npm install ncjsm ### API #### getResolver(extensions, confirmFile, resolvePackageMain) For provided configuration, returns a CJS modules resolver: - **extensions** - List of supported file extensions in order of singificance, e.g. for Node.js it would be `['.js', '.json', '.node']` - **confirmFile** - a `confirmFile(filepath)` function. Confirms whether there's a module at provided (not normalized, absolute) file path. Returns promise-like object which resolves with either normalized full path of a module or null (if there's no module for given path). Although result is expected to be a promise-like object, resolution can be synchronous. - **resolvePackageMain** - a `resolvePackageMain(dirpath)` function. Returns value of _package.json_'s `main` property for given path. Returns promise-like object which resolves with either resolved value, or null, when either `package.json` file was not found, or it didn't have _main_ property. Same as with `confirmFile` resolution can be synchronous. #### resolve(dir, path) _Node.js resolver_ Asynchronously resolves module path against provided directory path. Returns promise. If module is found, then promise resolves with an object, containing two properties: - `targetPath` - A path at which module was resolved - `realPath` - Real path of resolved module (if targetPath involves symlinks then realPath will be different) 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 const resolve = require("ncjsm/resolve"); // Asynchronously resolve path for 'foo' module against current path 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 } }); ``` #### resolveSync(dir, path) _Node.js resolver_ Synchronously resolves module path against provided directory path. Otherwise works same as `resolve` ```javascript const resolveSync = require("ncjsm/resolve/sync"); // Synchronously resolve path for 'foo' module against current path 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 } ``` #### requireUnached([moduleIds, ]callback) Create temporary environment where `require` of specific modules will not resolved the eventually cached verions ```javascript var requireUncached = require("ncjsm/require-uncached"); const firstCopyOfModule1 = require("./module1"); var secondCopyOfModule2 = requireUnached([require.resolve("./module1")], function () { return require("./module1"); }); console.log(firstCopyOfModule1 === secondCopyOfModule2); // false ``` Alternatively we may resolve callback in completely cleared require cache, for that `moduleIds` argument should be skipped ```javascript var requireUncached = require("ncjsm/require-uncached"); const firstCopyOfModule1 = require("./module1"); var secondCopyOfModule2 = requireUnached(function () { return require("./module1"); }); console.log(firstCopyOfModule1 === secondCopyOfModule2); // false ``` #### isPackageRoot(dirPath) Whether provided path is a root of a package ```javascript var isPackageRoot = require("ncjsm/is-package-root"); isPackageRoot(dirPath).done(function (isRoot) { if (isRoot) { // Provided path is package root } }); ``` #### resolvePackageRoot(dirPath) Resolve package root path for provided path. It is about resolution of first upper package root ```javascript var resolvePackageRoot = require("ncjsm/resolve-package-root"); resolvePackageRoot(dirPath).done(function (root) { if (!root) { // Provided path is not located in any package } }); ``` #### resolveProjectRoot(dirPath) Resolve project root path for provided path. It is about resolution of topmost package root for given path ```javascript var resolveProjectRoot = require("ncjsm/resolve-project-root"); resolveProjectRoot(dirPath).done(function (root) { if (!root) { // Provided path is not located in any project } }); ``` #### getDependecies(modulePath, options = { ... }) Resolve all module dependencies. Returns promise that resolves with an array of paths, that includes path to input module and paths to all its dependencies (it includes deep dependencies, so also dependencies of the dependencies). Paths to native Node.js modules are ignored. ```javascript var getDependencies = require("ncjsm/get-dependencies"); getDependencies(modulePath).done(function (deps) { console.log(deps); // e.g. [pathToModulePath, pathToDep1, pathToDep2, ...pathToDepn] }); ``` ##### Supported `options` ###### `ignoreMissing: false` If file for given module cannot be found then error is thrown. Set this to `true` to simply ignore not found modules ###### `ignoreExternal: false` By default all paths to all required modules are resolved. Resolution scope may be narrowed only to modules from same package (referenced via relative path), by settung this option to `true` ## Tests [![Build Status](https://travis-ci.org/medikoo/ncjsm.svg)](https://travis-ci.org/medikoo/ncjsm) $ npm test [build-image]: https://github.com/medikoo/ncjsm/workflows/Integrate/badge.svg [build-url]: https://github.com/medikoo/ncjsm/actions?query=workflow%3AIntegrate [cov-image]: https://img.shields.io/codecov/c/github/medikoo/ncjsm.svg [cov-url]: https://codecov.io/gh/medikoo/ncjsm [npm-image]: https://img.shields.io/npm/v/ncjsm.svg [npm-url]: https://www.npmjs.com/package/ncjsm ncjsm-4.3.0/commitlint.config.js000066400000000000000000000012361417354370700166260ustar00rootroot00000000000000"use strict"; module.exports = { rules: { "body-leading-blank": [2, "always"], "body-max-line-length": [2, "always", 72], "footer-leading-blank": [2, "always"], "footer-max-line-length": [2, "always", 72], "header-max-length": [2, "always", 72], "scope-case": [2, "always", "start-case"], "scope-enum": [2, "always", [""]], "subject-case": [2, "always", "sentence-case"], "subject-empty": [2, "never"], "subject-full-stop": [2, "never", "."], "type-case": [2, "always", "lower-case"], "type-empty": [2, "never"], "type-enum": [ 2, "always", ["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test"] ] } }; ncjsm-4.3.0/get-dependencies.js000066400000000000000000000034371417354370700164130ustar00rootroot00000000000000"use strict"; const uniq = require("es5-ext/array/#/uniq") , ensureString = require("type/string/ensure") , isObject = require("type/object/is") , deferred = require("deferred") , { dirname, resolve } = require("path") , readFile = require("fs2/read-file") , findRequires = require("find-requires") , builtinModules = new Set(require("builtin-modules")) , cjsResolve = require("./resolve"); const nonLocalChar = new Set([".", "/"]); const getDirectDependencies = function (modulePath, options) { return readFile(modulePath)(content => { const dir = dirname(modulePath); return deferred.map( uniq.call( findRequires(content, { setupCode: `var __filename = ${ JSON.stringify(modulePath) }, __dirname = ${ JSON.stringify(dir) };` }) ), depPath => { if (!nonLocalChar.has(depPath[0])) { if (options.ignoreExternal) return null; if (builtinModules.has(depPath.split("/")[0])) return null; } return cjsResolve( dir, depPath )(pathData => { if (pathData) return pathData.targetPath; if (options.ignoreMissing) return null; throw new Error( `Could not resolve ${ JSON.stringify(depPath) } module, required in ${ JSON.stringify(modulePath) }` ); }); } )(paths => uniq.call(paths).filter(Boolean)); }); }; module.exports = function (programPath, options = {}) { if (!isObject(options)) options = {}; programPath = resolve(ensureString(programPath)); const paths = Object.create(null); return (function self(modulePath) { if (paths[modulePath]) return null; return (paths[modulePath] = getDirectDependencies(modulePath, options).map(self)); })(programPath)(() => Object.keys(paths)); }; ncjsm-4.3.0/get-resolver/000077500000000000000000000000001417354370700152615ustar00rootroot00000000000000ncjsm-4.3.0/get-resolver/.eslintrc.json000066400000000000000000000002551417354370700200570ustar00rootroot00000000000000{ "extends": "medikoo/es3", "root": true, "rules": { "eqeqeq": ["error", "always", { "null": "ignore" }], "max-lines-per-function": "off", "no-eq-null": "off" } } ncjsm-4.3.0/get-resolver/index.js000066400000000000000000000045111417354370700167270ustar00rootroot00000000000000// Returns CJS module (sync or async) path resolver "use strict"; var dirname = function (path) { return path.slice(0, path.lastIndexOf("/")) || "/"; }; var join = function (path1, path2) { return path1 === "/" ? path1 + path2 : path1 + "/" + path2; }; module.exports = function (extensions, confirmFile, resolvePackageMain) { var resolveFile = function (path, extIndex) { var targetPath = path + (extIndex == null ? "" : extensions[extIndex]); return confirmFile(targetPath).then(function (result) { if (result) return result; if (extIndex == null) extIndex = 0; else ++extIndex; if (!extensions[extIndex]) return null; return resolveFile(path, extIndex); }); }; var resolveDirectory = function (path) { return resolvePackageMain(path).then(function (result) { if (result) { var mainPath = join(path, result); return resolveFile(mainPath).then(function (mainResult) { if (mainResult) return mainResult; return resolveFile(join(mainPath, "index"), 0).then(function (mainDirResult) { if (mainDirResult) return mainDirResult; return resolveFile(join(path, "index"), 0); }); }); } return resolveFile(join(path, "index"), 0); }); }; var resolveLocal = function (path) { var pathChar = path.charAt(path.length - 1); if (pathChar === "/") return resolveDirectory(path); if (pathChar === ".") { pathChar = path.charAt(path.length - 2); if (pathChar === "/") return resolveDirectory(path); if (pathChar === ".") { if (path.charAt(path.length - 3) === "/") return resolveDirectory(path); } } return resolveFile(path).then(function (result) { return result || resolveDirectory(path); }); }; var resolveExternal = function (dir, path) { return resolveLocal(join(dir, "node_modules") + "/" + path).then(function (result) { if (result) return result; if (dir === "/") return null; return resolveExternal(dirname(dir), path); }); }; return function (dir, path) { var pathChar; if (path.charAt(0) === "/") return resolveLocal(path); if (path.charAt(0) === ".") { pathChar = path.charAt(1); if (!pathChar || pathChar === "/") return resolveLocal(join(dir, path)); if (pathChar === ".") { pathChar = path.charAt(2); if (!pathChar || pathChar === "/") return resolveLocal(join(dir, path)); } } return resolveExternal(dir, path); }; }; ncjsm-4.3.0/is-module-not-found-error/000077500000000000000000000000001417354370700175775ustar00rootroot00000000000000ncjsm-4.3.0/is-module-not-found-error/.eslintrc.json000066400000000000000000000000531417354370700223710ustar00rootroot00000000000000{ "extends": "medikoo/es3", "root": true } ncjsm-4.3.0/is-module-not-found-error/index.js000066400000000000000000000013261417354370700212460ustar00rootroot00000000000000// Whether given error is an error thrown by require internals in case module // (at given path) was not found "use strict"; var ensureString = require("type/string/ensure"); var pathToken = ":path", pattern; var resolveMessage = function (error) { var newLineIndex = error.message.indexOf("\n"); return newLineIndex > 0 ? error.message.slice(0, newLineIndex) : error.message; }; try { require(pathToken); } catch (error) { pattern = resolveMessage(error); } module.exports = function (error, path) { path = ensureString(path); if (!error || typeof error.message !== "string") return false; if (error.code !== "MODULE_NOT_FOUND") return false; return resolveMessage(error) === pattern.replace(pathToken, path); }; ncjsm-4.3.0/is-package-root.js000066400000000000000000000010741417354370700161700ustar00rootroot00000000000000"use strict"; const ensureString = require("type/string/ensure") , deferred = require("deferred") , { resolve } = require("path") , stat = require("fs2/stat"); module.exports = function (path) { path = ensureString(path); return deferred.some([ stat(resolve(path, "package.json"))( stats => stats.isFile(), e => { if (e.code === "ENOENT") return false; throw e; } ), stat(resolve(path, "node_modules"))( stats => stats.isDirectory(), e => { if (e.code === "ENOENT") return false; throw e; } ) ]); }; ncjsm-4.3.0/lib/000077500000000000000000000000001417354370700134115ustar00rootroot00000000000000ncjsm-4.3.0/lib/get-node-resolver.js000066400000000000000000000016351417354370700173150ustar00rootroot00000000000000// Generates module path resolver for Node.js // Used to generate both sync and async version "use strict"; const isObject = require("type/object/is") , ensureString = require("type/string/ensure") , getResolver = require("../get-resolver") , { resolve } = require("path"); module.exports = function (confirmFile, resolvePackageMain) { const resolveModule = getResolver([".js", ".json", ".node"], confirmFile, resolvePackageMain); 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).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; }); }; }; ncjsm-4.3.0/lib/resolve-root.js000066400000000000000000000015331417354370700164110ustar00rootroot00000000000000// Find root, either first upper or topmost "use strict"; const ensureValue = require("type/value/ensure") , deferred = require("deferred") , stat = require("fs2/stat") , { resolve, sep } = require("path") , isPackageRoot = require("../is-package-root"); module.exports = function (path, searchTopMost) { path = resolve(String(ensureValue(path))); return stat(path)(stats => { if (!stats.isDirectory()) throw new Error("Provided path is not a directory path"); const tokens = path.split(sep); tokens.forEach((token, index) => { if (!index) return; tokens[index] = tokens[index - 1] + sep + token; }); tokens[0] += sep; if (!searchTopMost) tokens.reverse(); return deferred.find(tokens, dirPath => isPackageRoot(dirPath))(rootPath => rootPath === undefined ? null : rootPath ); }); }; ncjsm-4.3.0/package.json000066400000000000000000000040121417354370700151260ustar00rootroot00000000000000{ "name": "ncjsm", "version": "4.3.0", "description": "CJS (Node.js) style modules resolver", "author": "Mariusz Nowak (http://www.medikoo.com/)", "keywords": [ "cjs", "modules", "bundle", "browserify", "webpack" ], "repository": "medikoo/ncjsm", "dependencies": { "builtin-modules": "^3.2.0", "deferred": "^0.7.11", "es5-ext": "^0.10.53", "es6-set": "^0.1.5", "ext": "^1.6.0", "find-requires": "^1.0.0", "fs2": "^0.3.9", "type": "^2.5.0" }, "devDependencies": { "eslint": "^8.7.0", "eslint-config-medikoo": "^4.1.1", "git-list-updated": "^1.2.1", "husky": "^4.3.8", "lint-staged": "^12.3.1", "nyc": "^15.1.0", "prettier-elastic": "^2.2.1", "tad": "^3.1.0" }, "husky": { "hooks": { "pre-commit": "lint-staged" } }, "lint-staged": { "*.js": [ "eslint" ], "*.{css,html,js,json,md,yaml,yml}": [ "prettier -c" ] }, "eslintConfig": { "extends": "medikoo/node/6", "root": true }, "eslintIgnore": [ "test/__playground/**/*.js" ], "prettier": { "printWidth": 100, "tabWidth": 4, "overrides": [ { "files": [ "*.md", "*.yml" ], "options": { "tabWidth": 2 } } ] }, "nyc": { "all": true, "exclude": [ ".github", "coverage/**", "test/**", "*.config.js" ], "reporter": [ "lcov", "html", "text-summary" ] }, "scripts": { "coverage": "nyc npm test", "lint": "eslint .", "lint:updated": "pipe-git-updated --ext=js -- eslint --ignore-pattern '!*'", "prettier-check": "prettier -c --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"", "prettier-check:updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c", "prettify": "prettier --write --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"", "prettify:updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier --write", "test": "node node_modules/tad/bin/tad" }, "license": "ISC" } ncjsm-4.3.0/require-uncached/000077500000000000000000000000001417354370700160675ustar00rootroot00000000000000ncjsm-4.3.0/require-uncached/.eslintrc.json000066400000000000000000000000531417354370700206610ustar00rootroot00000000000000{ "extends": "medikoo/es5", "root": true } ncjsm-4.3.0/require-uncached/index.js000066400000000000000000000030511417354370700175330ustar00rootroot00000000000000"use strict"; var objForEach = require("es5-ext/object/for-each") , clear = require("es5-ext/object/clear") , isObject = require("type/object/is") , isPlainFunction = require("type/plain-function/is") , ensureIterable = require("type/iterable/ensure") , ensureString = require("type/string/ensure") , ensurePlainFunction = require("type/plain-function/ensure") , isThenable = require("type/thenable/is") , finallyMethod = require("ext/thenable_/finally"); module.exports = function (moduleIds, callback) { if (isPlainFunction(moduleIds)) { callback = moduleIds; moduleIds = null; } else { if (isObject(moduleIds)) { moduleIds = ensureIterable(moduleIds, { ensureItem: ensureString, name: "moduleIds", denyEmpty: true }); } else { moduleIds = [ensureString(moduleIds)]; } callback = ensurePlainFunction(callback, { name: "callback" }); } var cache = {}; if (moduleIds) { moduleIds.forEach(function (moduleId) { cache[moduleId] = require.cache[moduleId]; delete require.cache[moduleId]; }); } else { Object.assign(cache, require.cache); clear(require.cache); } var restore = function () { objForEach(cache, function (value, moduleId) { if (value) require.cache[moduleId] = value; else delete require.cache[moduleId]; }); }; var result; try { result = callback(); } catch (error) { restore(); throw error; } if (!isThenable(result)) { restore(); return result; } return finallyMethod.call(result, restore); }; ncjsm-4.3.0/resolve-package-root.js000066400000000000000000000002421417354370700172300ustar00rootroot00000000000000// Find top most package root "use strict"; const resolveRoot = require("./lib/resolve-root"); module.exports = function (path) { return resolveRoot(path); }; ncjsm-4.3.0/resolve-project-root.js000066400000000000000000000002501417354370700173020ustar00rootroot00000000000000// Find top most package root "use strict"; const resolveRoot = require("./lib/resolve-root"); module.exports = function (path) { return resolveRoot(path, true); }; ncjsm-4.3.0/resolve/000077500000000000000000000000001417354370700143225ustar00rootroot00000000000000ncjsm-4.3.0/resolve/index.js000066400000000000000000000015071417354370700157720ustar00rootroot00000000000000// Async module resolver "use strict"; const getResolver = require("../lib/get-node-resolver") , { resolve } = require("path") , stat = require("fs2/stat") , readFile = require("fs2/read-file") , realpath = require("fs2/realpath"); const { parse } = JSON; module.exports = getResolver( targetPath => { targetPath = resolve(targetPath); return stat(targetPath) .then(stats => { if (!stats.isFile()) return null; return realpath(targetPath).then(realPath => ({ targetPath, realPath })); }) .catch(e => { if (e.code === "ENOENT") return null; throw e; }); }, path => readFile(resolve(path, "package.json"))( data => { try { return parse(data).main; } catch (e) { return null; } }, e => { if (e.code === "ENOENT") return null; throw e; } ) ); ncjsm-4.3.0/resolve/sync.js000066400000000000000000000020031417354370700156270ustar00rootroot00000000000000// Sync module resolver "use strict"; const { resolve } = require("path") , PassThru = require("../utils/pass-thru") , getResolver = require("../lib/get-node-resolver"); const { statSync: stat, readFileSync: readFile, realpathSync: realpath } = require("fs"); const { parse } = JSON; const resolver = getResolver( targetPath => { let stats; targetPath = resolve(targetPath); try { stats = stat(targetPath); } catch (e) { if (e.code === "ENOENT") return new PassThru(null); throw e; } if (stats.isFile()) return new PassThru({ targetPath, realPath: realpath(targetPath) }); return new PassThru(null); }, path => { let data, result; try { data = readFile(resolve(path, "package.json")); } catch (e) { if (e.code === "ENOENT") return new PassThru(null); throw e; } try { result = parse(data).main; } catch (e) { result = null; } return new PassThru(result); } ); module.exports = function (dir, path, options = {}) { return resolver(dir, path, options).value; }; ncjsm-4.3.0/test/000077500000000000000000000000001417354370700136225ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/000077500000000000000000000000001417354370700163045ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/deep-dir-link-target/000077500000000000000000000000001417354370700222145ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/deep-dir-link-target/index.js000066400000000000000000000000001417354370700236470ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/deep-file-link-target.js000066400000000000000000000000001417354370700227010ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/dir-link-target/000077500000000000000000000000001417354370700213015ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/dir-link-target/index.js000066400000000000000000000000001417354370700227340ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/dir/000077500000000000000000000000001417354370700170625ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/dir/lorem.js000066400000000000000000000000411417354370700205310ustar00rootroot00000000000000"use strict"; require("outer"); ncjsm-4.3.0/test/__playground/dir/package.json000066400000000000000000000000241417354370700213440ustar00rootroot00000000000000{ "main": "lorem" } ncjsm-4.3.0/test/__playground/dir/subdir/000077500000000000000000000000001417354370700203525ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/dir/subdir/bar.js000066400000000000000000000000371417354370700214540ustar00rootroot00000000000000"use strict"; require("../"); ncjsm-4.3.0/test/__playground/file-link-target.js000066400000000000000000000000161417354370700217750ustar00rootroot00000000000000"use strict"; ncjsm-4.3.0/test/__playground/foo.js000066400000000000000000000000751417354370700174270ustar00rootroot00000000000000"use strict"; require("./dir/subdir/bar"); require("url"); ncjsm-4.3.0/test/__playground/invalid-file-link-with-a-fallback.json000066400000000000000000000000031417354370700254120ustar00rootroot00000000000000{} ncjsm-4.3.0/test/__playground/is-module-not-found-error/000077500000000000000000000000001417354370700232405ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/is-module-not-found-error/sample-error.js000066400000000000000000000000571417354370700262100ustar00rootroot00000000000000"use strict"; global.should.generate.error(); ncjsm-4.3.0/test/__playground/node_modules/000077500000000000000000000000001417354370700207615ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/broken-main/000077500000000000000000000000001417354370700231635ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/broken-main/index.js000066400000000000000000000000151417354370700246240ustar00rootroot00000000000000"use strict";ncjsm-4.3.0/test/__playground/node_modules/broken-main/package.json000066400000000000000000000000231417354370700254440ustar00rootroot00000000000000{ "main": "foo" } ncjsm-4.3.0/test/__playground/node_modules/outer/000077500000000000000000000000001417354370700221175ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/outer/boo.js000066400000000000000000000000001417354370700232220ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/outer/node_modules/000077500000000000000000000000001417354370700245745ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/outer/node_modules/nested/000077500000000000000000000000001417354370700260565ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/outer/node_modules/nested/elo.js000066400000000000000000000000001417354370700271610ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/outer/package.json000066400000000000000000000000221417354370700243770ustar00rootroot00000000000000{ "main": "raz" } ncjsm-4.3.0/test/__playground/node_modules/outer/raz.js000066400000000000000000000000421417354370700232450ustar00rootroot00000000000000"use strict"; require("outer3"); ncjsm-4.3.0/test/__playground/node_modules/outer3/000077500000000000000000000000001417354370700222025ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/outer3/index.js000066400000000000000000000000001417354370700236350ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/pkg-main-dir/000077500000000000000000000000001417354370700232405ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/pkg-main-dir/lib/000077500000000000000000000000001417354370700240065ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/node_modules/pkg-main-dir/lib/index.js000066400000000000000000000000161417354370700254500ustar00rootroot00000000000000"use strict"; ncjsm-4.3.0/test/__playground/node_modules/pkg-main-dir/package.json000066400000000000000000000000261417354370700255240ustar00rootroot00000000000000{ "main": "./lib" } ncjsm-4.3.0/test/__playground/other.js000066400000000000000000000000001417354370700177510ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/other/000077500000000000000000000000001417354370700174255ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/other/index.js000066400000000000000000000000421417354370700210660ustar00rootroot00000000000000"use strict"; require("../foo"); ncjsm-4.3.0/test/__playground/otherdir/000077500000000000000000000000001417354370700201245ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/otherdir/esli.js000066400000000000000000000000001417354370700214040ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/samename000066400000000000000000000000001417354370700200030ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/samename.js000066400000000000000000000000001417354370700204160ustar00rootroot00000000000000ncjsm-4.3.0/test/__playground/samename.json000066400000000000000000000000001417354370700207530ustar00rootroot00000000000000ncjsm-4.3.0/test/_lib/000077500000000000000000000000001417354370700145275ustar00rootroot00000000000000ncjsm-4.3.0/test/_lib/setup-playground-dir-symlinks.js000066400000000000000000000015311417354370700230320ustar00rootroot00000000000000"use strict"; const { resolve } = require("path") , symlink = require("fs2/symlink") , unlink = require("fs2/unlink"); const playgroundDir = resolve(__dirname, "../__playground"), symlinkOptions = { type: "junction" }; const links = [ { destination: resolve(playgroundDir, "another-deep-dir-link"), source: "deep-dir-link-target" }, { destination: resolve(playgroundDir, "deep-dir-link"), source: "another-deep-dir-link" }, { destination: resolve(playgroundDir, "invalid-dir-link"), source: "non-existing-dir" }, { destination: resolve(playgroundDir, "valid-dir-link"), source: "dir-link-target" } ]; module.exports = { setup: () => Promise.all( links.map(({ source, destination }) => symlink(source, destination, symlinkOptions)) ), teardown: () => Promise.all(links.map(({ destination }) => unlink(destination))) }; ncjsm-4.3.0/test/_lib/setup-playground-file-symlinks.js000066400000000000000000000017611417354370700232000ustar00rootroot00000000000000"use strict"; const { resolve } = require("path") , symlink = require("fs2/symlink") , unlink = require("fs2/unlink"); const playgroundDir = resolve(__dirname, "../__playground"), symlinkOptions = { type: "file" }; const links = [ { destination: resolve(playgroundDir, "another-deep-file-link.js"), source: "deep-file-link-target.js" }, { destination: resolve(playgroundDir, "deep-file-link.js"), source: "another-deep-file-link.js" }, { destination: resolve(playgroundDir, "invalid-file-link-with-a-fallback.js"), source: "non-existing-file.js" }, { destination: resolve(playgroundDir, "invalid-file-link.js"), source: "non-existing-file.js" }, { destination: resolve(playgroundDir, "valid-file-link.js"), source: "file-link-target.js" } ]; module.exports = { setup: () => Promise.all( links.map(({ source, destination }) => symlink(source, destination, symlinkOptions)) ), teardown: () => Promise.all(links.map(({ destination }) => unlink(destination))) }; ncjsm-4.3.0/test/get-dependencies.js000066400000000000000000000015171417354370700173670ustar00rootroot00000000000000"use strict"; const { resolve } = require("path"); const pgDir = resolve(__dirname, "__playground"); module.exports.regular = function (t, a, d) { t(resolve(pgDir, "other/index.js")).done(result => { a.deep( result.sort(), [ resolve(pgDir, "other/index.js"), resolve(pgDir, "foo.js"), resolve(pgDir, "dir/subdir/bar.js"), resolve(pgDir, "dir/lorem.js"), resolve(pgDir, "node_modules/outer/raz.js"), resolve(pgDir, "node_modules/outer3/index.js") ].sort() ); d(); }, d); }; module.exports.ignoreExternal = function (t, a, d) { t(resolve(pgDir, "other/index.js"), { ignoreExternal: true }).done(result => { a.deep( result.sort(), [ resolve(pgDir, "other/index.js"), resolve(pgDir, "foo.js"), resolve(pgDir, "dir/subdir/bar.js"), resolve(pgDir, "dir/lorem.js") ].sort() ); d(); }, d); }; ncjsm-4.3.0/test/get-resolver.js000066400000000000000000000071561417354370700166070ustar00rootroot00000000000000/* eslint max-statements: off */ "use strict"; const { resolve } = require("path") , PassThru = require("../utils/pass-thru"); const existingFiles = [ "/project/foo.js", "/project/other.js", "/project/other/index.js", "/project/samename", "/project/samename.js", "/project/samename.json", "/project/dir/subdir/bar.js", "/project/dir/lorem.js", "/project/otherdir/esli.js", "/project/node_modules/outer/boo.js", "/project/node_modules/outer/raz.js", "/project/node_modules/outer/node_modules/nested/elo.js", "/project/node_modules/outer3/index.js" ]; existingFiles.includes = require("es5-ext/array/#/contains"); const existingMains = { "/project/dir": "lorem", "/project/node_modules/outer": "raz" }; const isFile = function (path) { path = resolve(path); return new PassThru(existingFiles.includes(path) ? path : null); }; const resolvePackageMain = function (path) { return new PassThru(existingMains[resolve(path)] || null); }; module.exports = function (t, a) { const resolver = t([".js", ".json"], isFile, resolvePackageMain); a(resolver("/", "elo").value, null); a(resolver("/", "foo").value, null); a(resolver("/", "outer/boo").value, null); a(resolver("/", "./project/foo").value, "/project/foo.js"); a(resolver("/project", "./foo").value, "/project/foo.js"); a(resolver("/project", "./foo.js").value, "/project/foo.js"); a(resolver("/project", "./foo.json").value, null); a(resolver("/project", "./other").value, "/project/other.js"); a(resolver("/project", "./other/").value, "/project/other/index.js"); a(resolver("/project", "./samename").value, "/project/samename"); a(resolver("/project", "./samename.js").value, "/project/samename.js"); a(resolver("/project", "./samename.json").value, "/project/samename.json"); a(resolver("/project", "./samename").value, "/project/samename"); a(resolver("/project", "./dir").value, "/project/dir/lorem.js"); a(resolver("/project", "./dir/lorem").value, "/project/dir/lorem.js"); a(resolver("/project", "./dir/subdir/bar").value, "/project/dir/subdir/bar.js"); a(resolver("/project/dir", ".").value, "/project/dir/lorem.js"); a(resolver("/project/dir", "./").value, "/project/dir/lorem.js"); a(resolver("/project/dir", "./lorem").value, "/project/dir/lorem.js"); a(resolver("/project/dir", "../other").value, "/project/other.js"); a(resolver("/project/dir", "../other/").value, "/project/other/index.js"); a(resolver("/project/dir/subdir", "../").value, "/project/dir/lorem.js"); a(resolver("/project/dir/subdir", "../../foo").value, "/project/foo.js"); a(resolver("/project", "outer").value, "/project/node_modules/outer/raz.js"); a(resolver("/project", "outer/boo").value, "/project/node_modules/outer/boo.js"); a(resolver("/project", "outer/boo.json").value, null); a(resolver("/project", "outer3").value, "/project/node_modules/outer3/index.js"); a(resolver("/project", "nested/elo").value, null); a( resolver("/project/node_modules/outer", "outer3").value, "/project/node_modules/outer3/index.js" ); a(resolver("/project/node_modules/outer", "project/foo").value, null); a( resolver("/project/node_modules/outer", "nested/elo").value, "/project/node_modules/outer/node_modules/nested/elo.js" ); a(resolver("/project/node_modules/outer/node_modules/nested", "project/foo").value, null); a( resolver("/project/node_modules/outer/node_modules/nested", "outer").value, "/project/node_modules/outer/raz.js" ); a( resolver("/project/node_modules/outer/node_modules/nested", "outer/boo").value, "/project/node_modules/outer/boo.js" ); a( resolver("/project/node_modules/outer/node_modules/nested", "outer3").value, "/project/node_modules/outer3/index.js" ); }; ncjsm-4.3.0/test/is-module-not-found-error/000077500000000000000000000000001417354370700205565ustar00rootroot00000000000000ncjsm-4.3.0/test/is-module-not-found-error/index.js000066400000000000000000000006741417354370700222320ustar00rootroot00000000000000"use strict"; const { resolve } = require("path"); const pg = resolve(__dirname, "../__playground/is-module-not-found-error"); module.exports = function (t, a) { let path; try { require((path = "./wrong/path")); a.never("Wrong path"); } catch (e) { a(t(e, path), true, "Wrong path"); } try { require((path = `${ pg }/sample-error`)); a.never("Evaluation error"); } catch (e2) { a(t(e2, path), false, "Syntax error"); } }; ncjsm-4.3.0/test/is-package-root.js000066400000000000000000000012561417354370700171510ustar00rootroot00000000000000"use strict"; const deferred = require("deferred") , { resolve } = require("path"); const playgroundDir = resolve(__dirname, "__playground"); module.exports = function (t, a, d) { deferred( t(playgroundDir)(value => { a(value, true, "node_modules"); }), t(resolve(playgroundDir, "otherdir"))(value => { a(value, false, "Empty"); }), t(resolve(playgroundDir, "dir"))(value => { a(value, true, "package.json"); }), t(resolve(playgroundDir, "node_modules/outer"))(value => { a(value, true, "package.json and node_modules"); }), t(resolve(playgroundDir, "node_modules/outer3"))(value => { a(value, false, "In node_modules"); }) ).done(() => { d(); }, d); }; ncjsm-4.3.0/test/lib/000077500000000000000000000000001417354370700143705ustar00rootroot00000000000000ncjsm-4.3.0/test/lib/get-node-resolver.js000066400000000000000000000011041417354370700202630ustar00rootroot00000000000000"use strict"; const PassThru = require("../../utils/pass-thru") , isModuleNotFoundError = require("../../is-module-not-found-error"); const resolver = function () { return new PassThru(null); }; module.exports = function (t, a) { const resolve = t(resolver, resolver); a.throws(() => { resolve(); }, TypeError); a.throws(() => { resolve("asdfa"); }, TypeError); a.throws(() => { resolve("asdfa", ""); }, TypeError); try { resolve("asdfa", "elo"); throw new Error("Unexpected"); } catch (error) { a(isModuleNotFoundError(error, "elo"), true); } }; ncjsm-4.3.0/test/lib/resolve-root.js000066400000000000000000000004171417354370700173700ustar00rootroot00000000000000"use strict"; // TOOD: Configure tests bulletproof against environment side-effects // In this case test is vulnerable to location of tested package // (any upmost package.json or node_modules folder will affect its result) module.exports = function (t, a, d) { d(); }; ncjsm-4.3.0/test/require-uncached.js000066400000000000000000000016061417354370700174070ustar00rootroot00000000000000"use strict"; module.exports = function (t, a) { const pathA = "./__playground/other" , pathB = "./__playground/samename" , moduleA = require(pathA); let moduleA2; const innerModuleB = t([require.resolve(pathA), require.resolve(pathB)], () => { moduleA2 = require(pathA); a.not(moduleA2, moduleA); return require(pathB); }); const moduleB = require(pathB); a(require(pathA), moduleA); a.not(moduleB, innerModuleB); t([require.resolve(pathA), require.resolve(pathB)], () => { const moduleA3 = require(pathA); a.not(moduleA3, moduleA); a.not(moduleA3, moduleA2); const moduleB3 = require(pathB); a.not(moduleB3, moduleB); }); a(require(pathA), moduleA); a(require(pathB), moduleB); t(() => { const moduleA3 = require(pathA); a.not(moduleA3, moduleA); a.not(moduleA3, moduleA2); const moduleB3 = require(pathB); a.not(moduleB3, moduleB); }); }; ncjsm-4.3.0/test/resolve-package-root.js000066400000000000000000000014151417354370700202120ustar00rootroot00000000000000"use strict"; const deferred = require("deferred") , { resolve } = require("path"); const playgroundDir = resolve(__dirname, "__playground"); module.exports = function (t, a, d) { deferred( t(playgroundDir)(value => { a(value, playgroundDir, "node_modules"); }), t(resolve(playgroundDir, "otherdir"))(value => { a(value, playgroundDir, "Empty"); }), t(resolve(playgroundDir, "dir"))(value => { a(value, resolve(playgroundDir, "dir"), "package.json"); }), t(resolve(playgroundDir, "node_modules/outer"))(value => { a(value, resolve(playgroundDir, "node_modules/outer"), "package.json and node_modules"); }), t(resolve(playgroundDir, "node_modules/outer3"))(value => { a(value, playgroundDir, "In node_modules"); }) ).done(() => { d(); }, d); }; ncjsm-4.3.0/test/resolve-project-root.js000066400000000000000000000004171417354370700202660ustar00rootroot00000000000000"use strict"; // TOOD: Configure tests bulletproof against environment side-effects // In this case test is vulnerable to location of tested package // (any upmost package.json or node_modules folder will affect its result) module.exports = function (t, a, d) { d(); }; ncjsm-4.3.0/test/resolve/000077500000000000000000000000001417354370700153015ustar00rootroot00000000000000ncjsm-4.3.0/test/resolve/index.js000066400000000000000000000226361417354370700167570ustar00rootroot00000000000000/* eslint max-lines: "off" */ "use strict"; const noop = require("es5-ext/function/noop") , { resolve } = require("path") , isModuleNotFoundError = require("../../is-module-not-found-error"); const { setup: setupFileLinks, teardown: teardownFileLinks } = require("../_lib/setup-playground-file-symlinks"); const { setup: setupDirLinks, teardown: teardownDirLinks } = require("../_lib/setup-playground-dir-symlinks"); const playgroundDir = resolve(__dirname, "../__playground"); const unexpected = () => { throw new Error("Unexpected"); }; module.exports = (t, a) => Promise.all([ t(playgroundDir, "./foo").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/foo.js`), realPath: resolve(`${ playgroundDir }/foo.js`) }); }), t(playgroundDir, "./foo.js").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/foo.js`), realPath: resolve(`${ playgroundDir }/foo.js`) }); }), 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 => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/other.js`), realPath: resolve(`${ playgroundDir }/other.js`) }); }), t(playgroundDir, "./other/").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/other/index.js`), realPath: resolve(`${ playgroundDir }/other/index.js`) }); }), t(playgroundDir, "./samename").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/samename`), realPath: resolve(`${ playgroundDir }/samename`) }); }), t(playgroundDir, "./samename.js").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/samename.js`), realPath: resolve(`${ playgroundDir }/samename.js`) }); }), t(playgroundDir, "./samename.json").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/samename.json`), realPath: resolve(`${ playgroundDir }/samename.json`) }); }), t(playgroundDir, "./samename").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/samename`), realPath: resolve(`${ playgroundDir }/samename`) }); }), t(playgroundDir, "./dir").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); }), t(playgroundDir, "./dir/lorem").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); }), t(playgroundDir, "./dir/subdir/bar").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/dir/subdir/bar.js`), realPath: resolve(`${ playgroundDir }/dir/subdir/bar.js`) }); }), t(`${ playgroundDir }/dir`, ".").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); }), t(`${ playgroundDir }/dir`, "./").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); }), t(`${ playgroundDir }/dir`, "./lorem").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); }), t(`${ playgroundDir }/dir`, "../other").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/other.js`), realPath: resolve(`${ playgroundDir }/other.js`) }); }), t(`${ playgroundDir }/dir`, "../other/").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/other/index.js`), realPath: resolve(`${ playgroundDir }/other/index.js`) }); }), t(`${ playgroundDir }/dir/subdir`, "../").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); }), t(`${ playgroundDir }/dir/subdir`, "../../foo").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/foo.js`), realPath: resolve(`${ playgroundDir }/foo.js`) }); }), t(playgroundDir, "outer").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`) }); }), t(playgroundDir, "outer/boo").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`) }); }), t(playgroundDir, "outer/boo.json").then(unexpected, error => { a(isModuleNotFoundError(error, "outer/boo.json"), true); }), t(playgroundDir, "outer3").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`) }); }), t(playgroundDir, "pkg-main-dir").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/node_modules/pkg-main-dir/lib/index.js`), realPath: resolve(`${ playgroundDir }/node_modules/pkg-main-dir/lib/index.js`) }); }), t(playgroundDir, "nested/elo").then(unexpected, error => { a(isModuleNotFoundError(error, "nested/elo"), true); }), t(playgroundDir, "broken-main").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/node_modules/broken-main/index.js`), realPath: resolve(`${ playgroundDir }/node_modules/broken-main/index.js`) }); }), t(`${ playgroundDir }/node_modules/outer`, "outer3").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`) }); }), t(`${ playgroundDir }/node_modules/outer`, "project/foo").then(unexpected, error => { a(isModuleNotFoundError(error, "project/foo"), true); }), t(`${ playgroundDir }/node_modules/outer`, "nested/elo").then(value => { a.deep(value, { targetPath: resolve( `${ playgroundDir }/node_modules/outer/node_modules/nested/elo.js` ), realPath: resolve( `${ playgroundDir }/node_modules/outer/node_modules/nested/elo.js` ) }); }), t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "project/foo").then( unexpected, error => { a(isModuleNotFoundError(error, "project/foo"), true); } ), t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "outer").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`) }); }), t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "outer/boo").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`) }); }), t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "outer3").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`) }); }), setupFileLinks().then( () => Promise.all([ t(playgroundDir, "./valid-file-link").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/valid-file-link.js`), realPath: resolve(`${ playgroundDir }/file-link-target.js`) }); }), t(playgroundDir, "./deep-file-link").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/deep-file-link.js`), realPath: resolve(`${ playgroundDir }/deep-file-link-target.js`) }); }), 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 => { a.deep(value, { targetPath: resolve( `${ playgroundDir }/invalid-file-link-with-a-fallback.json` ), realPath: resolve( `${ playgroundDir }/invalid-file-link-with-a-fallback.json` ) }); }) ]).then(teardownFileLinks, error => teardownFileLinks.then(() => { throw error; })), error => { if (error.code === "EPERM") { process.stdout.write( "Warning: Could not test file symlinks due to not suffient " + "process permissions to create them\n" ); return; } throw error; } ), setupDirLinks().then(() => Promise.all([ t(playgroundDir, "./valid-dir-link").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/valid-dir-link/index.js`), realPath: resolve(`${ playgroundDir }/dir-link-target/index.js`) }); }), t(playgroundDir, "./deep-dir-link").then(value => { a.deep(value, { targetPath: resolve(`${ playgroundDir }/deep-dir-link/index.js`), realPath: resolve(`${ playgroundDir }/deep-dir-link-target/index.js`) }); }), t(playgroundDir, "./invalid-dir-link").then(unexpected, error => { a(isModuleNotFoundError(error, "./invalid-dir-link"), true); }) ]).then(teardownDirLinks, error => teardownDirLinks.then(() => { throw error; })) ) ]).then(noop); ncjsm-4.3.0/test/resolve/sync.js000066400000000000000000000211031417354370700166100ustar00rootroot00000000000000/* eslint max-lines: "off" */ "use strict"; const noop = require("es5-ext/function/noop") , { resolve } = require("path") , isModuleNotFoundError = require("../../is-module-not-found-error"); const { setup: setupFileLinks, teardown: teardownFileLinks } = require("../_lib/setup-playground-file-symlinks"); const { setup: setupDirLinks, teardown: teardownDirLinks } = require("../_lib/setup-playground-dir-symlinks"); const playgroundDir = resolve(__dirname, "../__playground"); module.exports = (t, a) => { a.deep(t(playgroundDir, "./foo"), { targetPath: resolve(`${ playgroundDir }/foo.js`), realPath: resolve(`${ playgroundDir }/foo.js`) }); a.deep(t(playgroundDir, "./foo.js"), { targetPath: resolve(`${ playgroundDir }/foo.js`), realPath: resolve(`${ playgroundDir }/foo.js`) }); 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"), { targetPath: resolve(`${ playgroundDir }/other.js`), realPath: resolve(`${ playgroundDir }/other.js`) }); a.deep(t(playgroundDir, "./other/"), { targetPath: resolve(`${ playgroundDir }/other/index.js`), realPath: resolve(`${ playgroundDir }/other/index.js`) }); a.deep(t(playgroundDir, "./samename"), { targetPath: resolve(`${ playgroundDir }/samename`), realPath: resolve(`${ playgroundDir }/samename`) }); a.deep(t(playgroundDir, "./samename.js"), { targetPath: resolve(`${ playgroundDir }/samename.js`), realPath: resolve(`${ playgroundDir }/samename.js`) }); a.deep(t(playgroundDir, "./samename.json"), { targetPath: resolve(`${ playgroundDir }/samename.json`), realPath: resolve(`${ playgroundDir }/samename.json`) }); a.deep(t(playgroundDir, "./samename"), { targetPath: resolve(`${ playgroundDir }/samename`), realPath: resolve(`${ playgroundDir }/samename`) }); a.deep(t(playgroundDir, "./dir"), { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); a.deep(t(playgroundDir, "./dir/lorem"), { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); a.deep(t(playgroundDir, "./dir/subdir/bar"), { targetPath: resolve(`${ playgroundDir }/dir/subdir/bar.js`), realPath: resolve(`${ playgroundDir }/dir/subdir/bar.js`) }); a.deep(t(`${ playgroundDir }/dir`, "."), { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); a.deep(t(`${ playgroundDir }/dir`, "./"), { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); a.deep(t(`${ playgroundDir }/dir`, "./lorem"), { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); a.deep(t(`${ playgroundDir }/dir`, "../other"), { targetPath: resolve(`${ playgroundDir }/other.js`), realPath: resolve(`${ playgroundDir }/other.js`) }); a.deep(t(`${ playgroundDir }/dir`, "../other/"), { targetPath: resolve(`${ playgroundDir }/other/index.js`), realPath: resolve(`${ playgroundDir }/other/index.js`) }); a.deep(t(`${ playgroundDir }/dir/subdir`, "../"), { targetPath: resolve(`${ playgroundDir }/dir/lorem.js`), realPath: resolve(`${ playgroundDir }/dir/lorem.js`) }); a.deep(t(`${ playgroundDir }/dir/subdir`, "../../foo"), { targetPath: resolve(`${ playgroundDir }/foo.js`), realPath: resolve(`${ playgroundDir }/foo.js`) }); a.deep(t(playgroundDir, "outer"), { targetPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`) }); a.deep(t(playgroundDir, "outer/boo"), { targetPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`) }); try { t(playgroundDir, "outer/boo.json"); throw new Error("Unexpected"); } catch (error) { a(isModuleNotFoundError(error, "outer/boo.json"), true); } a.deep(t(playgroundDir, "outer3"), { targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`) }); a.deep(t(playgroundDir, "pkg-main-dir"), { targetPath: resolve(`${ playgroundDir }/node_modules/pkg-main-dir/lib/index.js`), realPath: resolve(`${ playgroundDir }/node_modules/pkg-main-dir/lib/index.js`) }); a.deep(t(playgroundDir, "broken-main"), { targetPath: resolve(`${ playgroundDir }/node_modules/broken-main/index.js`), realPath: resolve(`${ playgroundDir }/node_modules/broken-main/index.js`) }); 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"), { targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`) }); 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"), { targetPath: resolve(`${ playgroundDir }/node_modules/outer/node_modules/nested/elo.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer/node_modules/nested/elo.js`) }); 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"), { targetPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer/raz.js`) }); a.deep(t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "outer/boo"), { targetPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer/boo.js`) }); a.deep(t(`${ playgroundDir }/node_modules/outer/node_modules/nested`, "outer3"), { targetPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`), realPath: resolve(`${ playgroundDir }/node_modules/outer3/index.js`) }); // Symlink tests return Promise.all([ setupFileLinks().then( () => { let testError, teardownPromise; try { a.deep(t(playgroundDir, "./valid-file-link"), { targetPath: resolve(`${ playgroundDir }/valid-file-link.js`), realPath: resolve(`${ playgroundDir }/file-link-target.js`) }); a.deep(t(playgroundDir, "./deep-file-link"), { targetPath: resolve(`${ playgroundDir }/deep-file-link.js`), realPath: resolve(`${ playgroundDir }/deep-file-link-target.js`) }); 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"), { targetPath: resolve( `${ playgroundDir }/invalid-file-link-with-a-fallback.json` ), realPath: resolve( `${ playgroundDir }/invalid-file-link-with-a-fallback.json` ) }); } catch (error) { testError = error; } finally { teardownPromise = teardownFileLinks(); } return teardownPromise.then(() => { if (testError) throw testError; }); }, error => { if (error.code === "EPERM") { process.stdout.write( "Warning: Could not test file symlinks due to not suffient " + "process permissions to create them\n" ); return; } throw error; } ), setupDirLinks().then(() => { let testError, teardownPromise; try { a.deep(t(playgroundDir, "./valid-dir-link"), { targetPath: resolve(`${ playgroundDir }/valid-dir-link/index.js`), realPath: resolve(`${ playgroundDir }/dir-link-target/index.js`) }); a.deep(t(playgroundDir, "./deep-dir-link"), { targetPath: resolve(`${ playgroundDir }/deep-dir-link/index.js`), realPath: resolve(`${ playgroundDir }/deep-dir-link-target/index.js`) }); try { t(playgroundDir, "./invalid-dir-link"); throw new Error("Unexpected"); } catch (error) { a(isModuleNotFoundError(error, "./invalid-dir-link"), true); } } catch (error) { testError = error; } finally { teardownPromise = teardownDirLinks(); } return teardownPromise.then(() => { if (testError) throw testError; }); }) ]).then(noop); }; ncjsm-4.3.0/test/utils/000077500000000000000000000000001417354370700147625ustar00rootroot00000000000000ncjsm-4.3.0/test/utils/is-path-external.js000066400000000000000000000004311417354370700205030ustar00rootroot00000000000000"use strict"; module.exports = function (t, a) { a(t("foo"), true); a(t("foo/bar.js"), true); a(t("/foo/bar.js"), false); a(t("./foo/bar.js"), false); a(t("../foo/bar.js"), false); a(t(".."), false); a(t("."), false); a(t("../../"), false); a(t(".../sdfsdf/"), true); }; ncjsm-4.3.0/test/utils/pass-thru.js000066400000000000000000000002551417354370700172500ustar00rootroot00000000000000"use strict"; module.exports = function (T, a) { const passThru = new T("foo"); a(passThru.value, "foo"); a(passThru.then(value => `${ value }bar`).value, "foobar"); }; ncjsm-4.3.0/utils/000077500000000000000000000000001417354370700140035ustar00rootroot00000000000000ncjsm-4.3.0/utils/is-path-external.js000066400000000000000000000007001417354370700175230ustar00rootroot00000000000000"use strict"; const ensureString = require("type/string/ensure"); module.exports = function (path) { let pathChar; path = ensureString(path); pathChar = path.charAt(0); if (pathChar === "/") return false; if (pathChar === ".") { pathChar = path.charAt(1); if (!pathChar || pathChar === "/") return false; if (pathChar === ".") { pathChar = path.charAt(2); if (!pathChar || pathChar === "/") return false; } } return true; }; ncjsm-4.3.0/utils/pass-thru.js000066400000000000000000000004721417354370700162720ustar00rootroot00000000000000// Promise-like mock, to be used for sync resolution of modules "use strict"; const PassThru = (module.exports = function (value) { this.value = value; }); PassThru.prototype.then = function (fn) { const result = fn(this.value); if (result instanceof PassThru) return result; return new PassThru(result); };