📦 添加虚拟环境和启动脚本

新增:
- backend/venv/ - Python 虚拟环境
- backend/start.sh - 启动脚本(使用虚拟环境)
- backend/requirements.txt - 依赖列表
- .gitignore - 忽略虚拟环境和缓存文件

说明:
- 每个项目使用独立虚拟环境
- 避免依赖冲突
- 启动脚本自动创建和激活虚拟环境
This commit is contained in:
2026-04-04 18:28:31 +08:00
parent 9ab279e1fe
commit 96f6318101
32058 changed files with 3949495 additions and 22 deletions

View File

@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015 Ben Mosher
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,44 @@
# eslint-import-resolver-node
[![npm](https://img.shields.io/npm/v/eslint-import-resolver-node.svg)](https://www.npmjs.com/package/eslint-import-resolver-node)
Default Node-style module resolution plugin for [`eslint-plugin-import`](https://www.npmjs.com/package/eslint-plugin-import).
Published separately to allow pegging to a specific version in case of breaking
changes.
Config is passed directly through to [`resolve`](https://www.npmjs.com/package/resolve#resolve-sync-id-opts) as options:
```yaml
settings:
import/resolver:
node:
extensions:
# if unset, default is just '.js', but it must be re-added explicitly if set
- .js
- .jsx
- .es6
- .coffee
paths:
# an array of absolute paths which will also be searched
# think NODE_PATH
- /usr/local/share/global_modules
# this is technically for identifying `node_modules` alternate names
moduleDirectory:
- node_modules # defaults to 'node_modules', but...
- bower_components
- project/src # can add a path segment here that will act like
# a source root, for in-project aliasing (i.e.
# `import MyStore from 'stores/my-store'`)
```
or to use the default options:
```yaml
settings:
import/resolver: node
```

View File

@@ -0,0 +1,68 @@
'use strict';
const resolve = require('resolve/sync');
const isCoreModule = require('is-core-module');
const path = require('path');
const log = require('debug')('eslint-plugin-import:resolver:node');
exports.interfaceVersion = 2;
function opts(file, config, packageFilter) {
return Object.assign({ // more closely matches Node (#333)
// plus 'mjs' for native modules! (#939)
extensions: ['.mjs', '.js', '.json', '.node'],
// TODO: semver-major: remove this to match Node's default behavior
preserveSymlinks: true,
}, config, {
// path.resolve will handle paths relative to CWD
basedir: path.dirname(path.resolve(file)),
packageFilter,
});
}
function identity(x) { return x; }
function packageFilter(pkg, dir, config) {
let found = false;
const file = path.join(dir, 'dummy.js');
if (pkg.module) {
try {
resolve(String(pkg.module).replace(/^(?:\.\/)?/, './'), opts(file, config, identity));
pkg.main = pkg.module;
found = true;
} catch (err) {
log('resolve threw error trying to find pkg.module:', err);
}
}
if (!found && pkg['jsnext:main']) {
try {
resolve(String(pkg['jsnext:main']).replace(/^(?:\.\/)?/, './'), opts(file, config, identity));
pkg.main = pkg['jsnext:main'];
found = true;
} catch (err) {
log('resolve threw error trying to find pkg[\'jsnext:main\']:', err);
}
}
return pkg;
}
exports.resolve = function (source, file, config) {
log('Resolving:', source, 'from:', file);
let resolvedPath;
if (isCoreModule(source)) {
log('resolved to core');
return { found: true, path: null };
}
try {
const cachedFilter = function (pkg, pkgFileOrDir, maybeDir) { return packageFilter(pkg, maybeDir || pkgFileOrDir, config); };
resolvedPath = resolve(source, opts(file, config, cachedFilter));
log('Resolved to:', resolvedPath);
return { found: true, path: resolvedPath };
} catch (err) {
log('resolve threw error:', err);
return { found: false };
}
};

View File

@@ -0,0 +1 @@
../resolve/bin/resolve

View File

@@ -0,0 +1,395 @@
3.1.0 / 2017-09-26
==================
* Add `DEBUG_HIDE_DATE` env var (#486)
* Remove ReDoS regexp in %o formatter (#504)
* Remove "component" from package.json
* Remove `component.json`
* Ignore package-lock.json
* Examples: fix colors printout
* Fix: browser detection
* Fix: spelling mistake (#496, @EdwardBetts)
3.0.1 / 2017-08-24
==================
* Fix: Disable colors in Edge and Internet Explorer (#489)
3.0.0 / 2017-08-08
==================
* Breaking: Remove DEBUG_FD (#406)
* Breaking: Use `Date#toISOString()` instead to `Date#toUTCString()` when output is not a TTY (#418)
* Breaking: Make millisecond timer namespace specific and allow 'always enabled' output (#408)
* Addition: document `enabled` flag (#465)
* Addition: add 256 colors mode (#481)
* Addition: `enabled()` updates existing debug instances, add `destroy()` function (#440)
* Update: component: update "ms" to v2.0.0
* Update: separate the Node and Browser tests in Travis-CI
* Update: refactor Readme, fixed documentation, added "Namespace Colors" section, redid screenshots
* Update: separate Node.js and web browser examples for organization
* Update: update "browserify" to v14.4.0
* Fix: fix Readme typo (#473)
2.6.9 / 2017-09-22
==================
* remove ReDoS regexp in %o formatter (#504)
2.6.8 / 2017-05-18
==================
* Fix: Check for undefined on browser globals (#462, @marbemac)
2.6.7 / 2017-05-16
==================
* Fix: Update ms to 2.0.0 to fix regular expression denial of service vulnerability (#458, @hubdotcom)
* Fix: Inline extend function in node implementation (#452, @dougwilson)
* Docs: Fix typo (#455, @msasad)
2.6.5 / 2017-04-27
==================
* Fix: null reference check on window.documentElement.style.WebkitAppearance (#447, @thebigredgeek)
* Misc: clean up browser reference checks (#447, @thebigredgeek)
* Misc: add npm-debug.log to .gitignore (@thebigredgeek)
2.6.4 / 2017-04-20
==================
* Fix: bug that would occur if process.env.DEBUG is a non-string value. (#444, @LucianBuzzo)
* Chore: ignore bower.json in npm installations. (#437, @joaovieira)
* Misc: update "ms" to v0.7.3 (@tootallnate)
2.6.3 / 2017-03-13
==================
* Fix: Electron reference to `process.env.DEBUG` (#431, @paulcbetts)
* Docs: Changelog fix (@thebigredgeek)
2.6.2 / 2017-03-10
==================
* Fix: DEBUG_MAX_ARRAY_LENGTH (#420, @slavaGanzin)
* Docs: Add backers and sponsors from Open Collective (#422, @piamancini)
* Docs: Add Slackin invite badge (@tootallnate)
2.6.1 / 2017-02-10
==================
* Fix: Module's `export default` syntax fix for IE8 `Expected identifier` error
* Fix: Whitelist DEBUG_FD for values 1 and 2 only (#415, @pi0)
* Fix: IE8 "Expected identifier" error (#414, @vgoma)
* Fix: Namespaces would not disable once enabled (#409, @musikov)
2.6.0 / 2016-12-28
==================
* Fix: added better null pointer checks for browser useColors (@thebigredgeek)
* Improvement: removed explicit `window.debug` export (#404, @tootallnate)
* Improvement: deprecated `DEBUG_FD` environment variable (#405, @tootallnate)
2.5.2 / 2016-12-25
==================
* Fix: reference error on window within webworkers (#393, @KlausTrainer)
* Docs: fixed README typo (#391, @lurch)
* Docs: added notice about v3 api discussion (@thebigredgeek)
2.5.1 / 2016-12-20
==================
* Fix: babel-core compatibility
2.5.0 / 2016-12-20
==================
* Fix: wrong reference in bower file (@thebigredgeek)
* Fix: webworker compatibility (@thebigredgeek)
* Fix: output formatting issue (#388, @kribblo)
* Fix: babel-loader compatibility (#383, @escwald)
* Misc: removed built asset from repo and publications (@thebigredgeek)
* Misc: moved source files to /src (#378, @yamikuronue)
* Test: added karma integration and replaced babel with browserify for browser tests (#378, @yamikuronue)
* Test: coveralls integration (#378, @yamikuronue)
* Docs: simplified language in the opening paragraph (#373, @yamikuronue)
2.4.5 / 2016-12-17
==================
* Fix: `navigator` undefined in Rhino (#376, @jochenberger)
* Fix: custom log function (#379, @hsiliev)
* Improvement: bit of cleanup + linting fixes (@thebigredgeek)
* Improvement: rm non-maintainted `dist/` dir (#375, @freewil)
* Docs: simplified language in the opening paragraph. (#373, @yamikuronue)
2.4.4 / 2016-12-14
==================
* Fix: work around debug being loaded in preload scripts for electron (#368, @paulcbetts)
2.4.3 / 2016-12-14
==================
* Fix: navigation.userAgent error for react native (#364, @escwald)
2.4.2 / 2016-12-14
==================
* Fix: browser colors (#367, @tootallnate)
* Misc: travis ci integration (@thebigredgeek)
* Misc: added linting and testing boilerplate with sanity check (@thebigredgeek)
2.4.1 / 2016-12-13
==================
* Fix: typo that broke the package (#356)
2.4.0 / 2016-12-13
==================
* Fix: bower.json references unbuilt src entry point (#342, @justmatt)
* Fix: revert "handle regex special characters" (@tootallnate)
* Feature: configurable util.inspect()`options for NodeJS (#327, @tootallnate)
* Feature: %O`(big O) pretty-prints objects (#322, @tootallnate)
* Improvement: allow colors in workers (#335, @botverse)
* Improvement: use same color for same namespace. (#338, @lchenay)
2.3.3 / 2016-11-09
==================
* Fix: Catch `JSON.stringify()` errors (#195, Jovan Alleyne)
* Fix: Returning `localStorage` saved values (#331, Levi Thomason)
* Improvement: Don't create an empty object when no `process` (Nathan Rajlich)
2.3.2 / 2016-11-09
==================
* Fix: be super-safe in index.js as well (@TooTallNate)
* Fix: should check whether process exists (Tom Newby)
2.3.1 / 2016-11-09
==================
* Fix: Added electron compatibility (#324, @paulcbetts)
* Improvement: Added performance optimizations (@tootallnate)
* Readme: Corrected PowerShell environment variable example (#252, @gimre)
* Misc: Removed yarn lock file from source control (#321, @fengmk2)
2.3.0 / 2016-11-07
==================
* Fix: Consistent placement of ms diff at end of output (#215, @gorangajic)
* Fix: Escaping of regex special characters in namespace strings (#250, @zacronos)
* Fix: Fixed bug causing crash on react-native (#282, @vkarpov15)
* Feature: Enabled ES6+ compatible import via default export (#212 @bucaran)
* Feature: Added %O formatter to reflect Chrome's console.log capability (#279, @oncletom)
* Package: Update "ms" to 0.7.2 (#315, @DevSide)
* Package: removed superfluous version property from bower.json (#207 @kkirsche)
* Readme: fix USE_COLORS to DEBUG_COLORS
* Readme: Doc fixes for format string sugar (#269, @mlucool)
* Readme: Updated docs for DEBUG_FD and DEBUG_COLORS environment variables (#232, @mattlyons0)
* Readme: doc fixes for PowerShell (#271 #243, @exoticknight @unreadable)
* Readme: better docs for browser support (#224, @matthewmueller)
* Tooling: Added yarn integration for development (#317, @thebigredgeek)
* Misc: Renamed History.md to CHANGELOG.md (@thebigredgeek)
* Misc: Added license file (#226 #274, @CantemoInternal @sdaitzman)
* Misc: Updated contributors (@thebigredgeek)
2.2.0 / 2015-05-09
==================
* package: update "ms" to v0.7.1 (#202, @dougwilson)
* README: add logging to file example (#193, @DanielOchoa)
* README: fixed a typo (#191, @amir-s)
* browser: expose `storage` (#190, @stephenmathieson)
* Makefile: add a `distclean` target (#189, @stephenmathieson)
2.1.3 / 2015-03-13
==================
* Updated stdout/stderr example (#186)
* Updated example/stdout.js to match debug current behaviour
* Renamed example/stderr.js to stdout.js
* Update Readme.md (#184)
* replace high intensity foreground color for bold (#182, #183)
2.1.2 / 2015-03-01
==================
* dist: recompile
* update "ms" to v0.7.0
* package: update "browserify" to v9.0.3
* component: fix "ms.js" repo location
* changed bower package name
* updated documentation about using debug in a browser
* fix: security error on safari (#167, #168, @yields)
2.1.1 / 2014-12-29
==================
* browser: use `typeof` to check for `console` existence
* browser: check for `console.log` truthiness (fix IE 8/9)
* browser: add support for Chrome apps
* Readme: added Windows usage remarks
* Add `bower.json` to properly support bower install
2.1.0 / 2014-10-15
==================
* node: implement `DEBUG_FD` env variable support
* package: update "browserify" to v6.1.0
* package: add "license" field to package.json (#135, @panuhorsmalahti)
2.0.0 / 2014-09-01
==================
* package: update "browserify" to v5.11.0
* node: use stderr rather than stdout for logging (#29, @stephenmathieson)
1.0.4 / 2014-07-15
==================
* dist: recompile
* example: remove `console.info()` log usage
* example: add "Content-Type" UTF-8 header to browser example
* browser: place %c marker after the space character
* browser: reset the "content" color via `color: inherit`
* browser: add colors support for Firefox >= v31
* debug: prefer an instance `log()` function over the global one (#119)
* Readme: update documentation about styled console logs for FF v31 (#116, @wryk)
1.0.3 / 2014-07-09
==================
* Add support for multiple wildcards in namespaces (#122, @seegno)
* browser: fix lint
1.0.2 / 2014-06-10
==================
* browser: update color palette (#113, @gscottolson)
* common: make console logging function configurable (#108, @timoxley)
* node: fix %o colors on old node <= 0.8.x
* Makefile: find node path using shell/which (#109, @timoxley)
1.0.1 / 2014-06-06
==================
* browser: use `removeItem()` to clear localStorage
* browser, node: don't set DEBUG if namespaces is undefined (#107, @leedm777)
* package: add "contributors" section
* node: fix comment typo
* README: list authors
1.0.0 / 2014-06-04
==================
* make ms diff be global, not be scope
* debug: ignore empty strings in enable()
* node: make DEBUG_COLORS able to disable coloring
* *: export the `colors` array
* npmignore: don't publish the `dist` dir
* Makefile: refactor to use browserify
* package: add "browserify" as a dev dependency
* Readme: add Web Inspector Colors section
* node: reset terminal color for the debug content
* node: map "%o" to `util.inspect()`
* browser: map "%j" to `JSON.stringify()`
* debug: add custom "formatters"
* debug: use "ms" module for humanizing the diff
* Readme: add "bash" syntax highlighting
* browser: add Firebug color support
* browser: add colors for WebKit browsers
* node: apply log to `console`
* rewrite: abstract common logic for Node & browsers
* add .jshintrc file
0.8.1 / 2014-04-14
==================
* package: re-add the "component" section
0.8.0 / 2014-03-30
==================
* add `enable()` method for nodejs. Closes #27
* change from stderr to stdout
* remove unnecessary index.js file
0.7.4 / 2013-11-13
==================
* remove "browserify" key from package.json (fixes something in browserify)
0.7.3 / 2013-10-30
==================
* fix: catch localStorage security error when cookies are blocked (Chrome)
* add debug(err) support. Closes #46
* add .browser prop to package.json. Closes #42
0.7.2 / 2013-02-06
==================
* fix package.json
* fix: Mobile Safari (private mode) is broken with debug
* fix: Use unicode to send escape character to shell instead of octal to work with strict mode javascript
0.7.1 / 2013-02-05
==================
* add repository URL to package.json
* add DEBUG_COLORED to force colored output
* add browserify support
* fix component. Closes #24
0.7.0 / 2012-05-04
==================
* Added .component to package.json
* Added debug.component.js build
0.6.0 / 2012-03-16
==================
* Added support for "-" prefix in DEBUG [Vinay Pulim]
* Added `.enabled` flag to the node version [TooTallNate]
0.5.0 / 2012-02-02
==================
* Added: humanize diffs. Closes #8
* Added `debug.disable()` to the CS variant
* Removed padding. Closes #10
* Fixed: persist client-side variant again. Closes #9
0.4.0 / 2012-02-01
==================
* Added browser variant support for older browsers [TooTallNate]
* Added `debug.enable('project:*')` to browser variant [TooTallNate]
* Added padding to diff (moved it to the right)
0.3.0 / 2012-01-26
==================
* Added millisecond diff when isatty, otherwise UTC string
0.2.0 / 2012-01-22
==================
* Added wildcard support
0.1.0 / 2011-12-02
==================
* Added: remove colors unless stderr isatty [TooTallNate]
0.0.1 / 2010-01-03
==================
* Initial release

View File

@@ -0,0 +1,19 @@
(The MIT License)
Copyright (c) 2014 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the 'Software'), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,437 @@
# debug
[![Build Status](https://travis-ci.org/visionmedia/debug.svg?branch=master)](https://travis-ci.org/visionmedia/debug) [![Coverage Status](https://coveralls.io/repos/github/visionmedia/debug/badge.svg?branch=master)](https://coveralls.io/github/visionmedia/debug?branch=master) [![Slack](https://visionmedia-community-slackin.now.sh/badge.svg)](https://visionmedia-community-slackin.now.sh/) [![OpenCollective](https://opencollective.com/debug/backers/badge.svg)](#backers)
[![OpenCollective](https://opencollective.com/debug/sponsors/badge.svg)](#sponsors)
<img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">
A tiny JavaScript debugging utility modelled after Node.js core's debugging
technique. Works in Node.js and web browsers.
## Installation
```bash
$ npm install debug
```
## Usage
`debug` exposes a function; simply pass this function the name of your module, and it will return a decorated version of `console.error` for you to pass debug statements to. This will allow you to toggle the debug output for different parts of your module as well as the module as a whole.
Example [_app.js_](./examples/node/app.js):
```js
var debug = require('debug')('http')
, http = require('http')
, name = 'My App';
// fake app
debug('booting %o', name);
http.createServer(function(req, res){
debug(req.method + ' ' + req.url);
res.end('hello\n');
}).listen(3000, function(){
debug('listening');
});
// fake worker of some kind
require('./worker');
```
Example [_worker.js_](./examples/node/worker.js):
```js
var a = require('debug')('worker:a')
, b = require('debug')('worker:b');
function work() {
a('doing lots of uninteresting work');
setTimeout(work, Math.random() * 1000);
}
work();
function workb() {
b('doing some work');
setTimeout(workb, Math.random() * 2000);
}
workb();
```
The `DEBUG` environment variable is then used to enable these based on space or
comma-delimited names.
Here are some examples:
<img width="647" alt="screen shot 2017-08-08 at 12 53 04 pm" src="https://user-images.githubusercontent.com/71256/29091703-a6302cdc-7c38-11e7-8304-7c0b3bc600cd.png">
<img width="647" alt="screen shot 2017-08-08 at 12 53 38 pm" src="https://user-images.githubusercontent.com/71256/29091700-a62a6888-7c38-11e7-800b-db911291ca2b.png">
<img width="647" alt="screen shot 2017-08-08 at 12 53 25 pm" src="https://user-images.githubusercontent.com/71256/29091701-a62ea114-7c38-11e7-826a-2692bedca740.png">
#### Windows command prompt notes
##### CMD
On Windows the environment variable is set using the `set` command.
```cmd
set DEBUG=*,-not_this
```
Example:
```cmd
set DEBUG=* & node app.js
```
##### PowerShell (VS Code default)
PowerShell uses different syntax to set environment variables.
```cmd
$env:DEBUG = "*,-not_this"
```
Example:
```cmd
$env:DEBUG='app';node app.js
```
Then, run the program to be debugged as usual.
npm script example:
```js
"windowsDebug": "@powershell -Command $env:DEBUG='*';node app.js",
```
## Namespace Colors
Every debug instance has a color generated for it based on its namespace name.
This helps when visually parsing the debug output to identify which debug instance
a debug line belongs to.
#### Node.js
In Node.js, colors are enabled when stderr is a TTY. You also _should_ install
the [`supports-color`](https://npmjs.org/supports-color) module alongside debug,
otherwise debug will only use a small handful of basic colors.
<img width="521" src="https://user-images.githubusercontent.com/71256/29092181-47f6a9e6-7c3a-11e7-9a14-1928d8a711cd.png">
#### Web Browser
Colors are also enabled on "Web Inspectors" that understand the `%c` formatting
option. These are WebKit web inspectors, Firefox ([since version
31](https://hacks.mozilla.org/2014/05/editable-box-model-multiple-selection-sublime-text-keys-much-more-firefox-developer-tools-episode-31/))
and the Firebug plugin for Firefox (any version).
<img width="524" src="https://user-images.githubusercontent.com/71256/29092033-b65f9f2e-7c39-11e7-8e32-f6f0d8e865c1.png">
## Millisecond diff
When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the "+NNNms" will show you how much time was spent between calls.
<img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">
When stdout is not a TTY, `Date#toISOString()` is used, making it more useful for logging the debug information as shown below:
<img width="647" src="https://user-images.githubusercontent.com/71256/29091956-6bd78372-7c39-11e7-8c55-c948396d6edd.png">
## Conventions
If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser". If you append a "*" to the end of your name, it will always be enabled regardless of the setting of the DEBUG environment variable. You can then use it for normal output as well as debug output.
## Wildcards
The `*` character may be used as a wildcard. Suppose for example your library has
debuggers named "connect:bodyParser", "connect:compress", "connect:session",
instead of listing all three with
`DEBUG=connect:bodyParser,connect:compress,connect:session`, you may simply do
`DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`.
You can also exclude specific debuggers by prefixing them with a "-" character.
For example, `DEBUG=*,-connect:*` would include all debuggers except those
starting with "connect:".
## Environment Variables
When running through Node.js, you can set a few environment variables that will
change the behavior of the debug logging:
| Name | Purpose |
|-----------|-------------------------------------------------|
| `DEBUG` | Enables/disables specific debugging namespaces. |
| `DEBUG_HIDE_DATE` | Hide date from debug output (non-TTY). |
| `DEBUG_COLORS`| Whether or not to use colors in the debug output. |
| `DEBUG_DEPTH` | Object inspection depth. |
| `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |
__Note:__ The environment variables beginning with `DEBUG_` end up being
converted into an Options object that gets used with `%o`/`%O` formatters.
See the Node.js documentation for
[`util.inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options)
for the complete list.
## Formatters
Debug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting.
Below are the officially supported formatters:
| Formatter | Representation |
|-----------|----------------|
| `%O` | Pretty-print an Object on multiple lines. |
| `%o` | Pretty-print an Object all on a single line. |
| `%s` | String. |
| `%d` | Number (both integer and float). |
| `%j` | JSON. Replaced with the string '[Circular]' if the argument contains circular references. |
| `%%` | Single percent sign ('%'). This does not consume an argument. |
### Custom formatters
You can add custom formatters by extending the `debug.formatters` object.
For example, if you wanted to add support for rendering a Buffer as hex with
`%h`, you could do something like:
```js
const createDebug = require('debug')
createDebug.formatters.h = (v) => {
return v.toString('hex')
}
// …elsewhere
const debug = createDebug('foo')
debug('this is hex: %h', new Buffer('hello world'))
// foo this is hex: 68656c6c6f20776f726c6421 +0ms
```
## Browser Support
You can build a browser-ready script using [browserify](https://github.com/substack/node-browserify),
or just use the [browserify-as-a-service](https://wzrd.in/) [build](https://wzrd.in/standalone/debug@latest),
if you don't want to build it yourself.
Debug's enable state is currently persisted by `localStorage`.
Consider the situation shown below where you have `worker:a` and `worker:b`,
and wish to debug both. You can enable this using `localStorage.debug`:
```js
localStorage.debug = 'worker:*'
```
And then refresh the page.
```js
a = debug('worker:a');
b = debug('worker:b');
setInterval(function(){
a('doing some work');
}, 1000);
setInterval(function(){
b('doing some work');
}, 1200);
```
## Output streams
By default `debug` will log to stderr, however this can be configured per-namespace by overriding the `log` method:
Example [_stdout.js_](./examples/node/stdout.js):
```js
var debug = require('debug');
var error = debug('app:error');
// by default stderr is used
error('goes to stderr!');
var log = debug('app:log');
// set this namespace to log via console.log
log.log = console.log.bind(console); // don't forget to bind to console!
log('goes to stdout');
error('still goes to stderr!');
// set all output to go via console.info
// overrides all per-namespace log settings
debug.log = console.info.bind(console);
error('now goes to stdout via console.info');
log('still goes to stdout, but via console.info now');
```
## Extend
You can simply extend debugger
```js
const log = require('debug')('auth');
//creates new debug instance with extended namespace
const logSign = log.extend('sign');
const logLogin = log.extend('login');
log('hello'); // auth hello
logSign('hello'); //auth:sign hello
logLogin('hello'); //auth:login hello
```
## Set dynamically
You can also enable debug dynamically by calling the `enable()` method :
```js
let debug = require('debug');
console.log(1, debug.enabled('test'));
debug.enable('test');
console.log(2, debug.enabled('test'));
debug.disable();
console.log(3, debug.enabled('test'));
```
print :
```
1 false
2 true
3 false
```
Usage :
`enable(namespaces)`
`namespaces` can include modes separated by a colon and wildcards.
Note that calling `enable()` completely overrides previously set DEBUG variable :
```
$ DEBUG=foo node -e 'var dbg = require("debug"); dbg.enable("bar"); console.log(dbg.enabled("foo"))'
=> false
```
## Checking whether a debug target is enabled
After you've created a debug instance, you can determine whether or not it is
enabled by checking the `enabled` property:
```javascript
const debug = require('debug')('http');
if (debug.enabled) {
// do stuff...
}
```
You can also manually toggle this property to force the debug instance to be
enabled or disabled.
## Authors
- TJ Holowaychuk
- Nathan Rajlich
- Andrew Rhyne
## Backers
Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/debug#backer)]
<a href="https://opencollective.com/debug/backer/0/website" target="_blank"><img src="https://opencollective.com/debug/backer/0/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/1/website" target="_blank"><img src="https://opencollective.com/debug/backer/1/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/2/website" target="_blank"><img src="https://opencollective.com/debug/backer/2/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/3/website" target="_blank"><img src="https://opencollective.com/debug/backer/3/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/4/website" target="_blank"><img src="https://opencollective.com/debug/backer/4/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/5/website" target="_blank"><img src="https://opencollective.com/debug/backer/5/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/6/website" target="_blank"><img src="https://opencollective.com/debug/backer/6/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/7/website" target="_blank"><img src="https://opencollective.com/debug/backer/7/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/8/website" target="_blank"><img src="https://opencollective.com/debug/backer/8/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/9/website" target="_blank"><img src="https://opencollective.com/debug/backer/9/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/10/website" target="_blank"><img src="https://opencollective.com/debug/backer/10/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/11/website" target="_blank"><img src="https://opencollective.com/debug/backer/11/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/12/website" target="_blank"><img src="https://opencollective.com/debug/backer/12/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/13/website" target="_blank"><img src="https://opencollective.com/debug/backer/13/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/14/website" target="_blank"><img src="https://opencollective.com/debug/backer/14/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/15/website" target="_blank"><img src="https://opencollective.com/debug/backer/15/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/16/website" target="_blank"><img src="https://opencollective.com/debug/backer/16/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/17/website" target="_blank"><img src="https://opencollective.com/debug/backer/17/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/18/website" target="_blank"><img src="https://opencollective.com/debug/backer/18/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/19/website" target="_blank"><img src="https://opencollective.com/debug/backer/19/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/20/website" target="_blank"><img src="https://opencollective.com/debug/backer/20/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/21/website" target="_blank"><img src="https://opencollective.com/debug/backer/21/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/22/website" target="_blank"><img src="https://opencollective.com/debug/backer/22/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/23/website" target="_blank"><img src="https://opencollective.com/debug/backer/23/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/24/website" target="_blank"><img src="https://opencollective.com/debug/backer/24/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/25/website" target="_blank"><img src="https://opencollective.com/debug/backer/25/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/26/website" target="_blank"><img src="https://opencollective.com/debug/backer/26/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/27/website" target="_blank"><img src="https://opencollective.com/debug/backer/27/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/28/website" target="_blank"><img src="https://opencollective.com/debug/backer/28/avatar.svg"></a>
<a href="https://opencollective.com/debug/backer/29/website" target="_blank"><img src="https://opencollective.com/debug/backer/29/avatar.svg"></a>
## Sponsors
Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/debug#sponsor)]
<a href="https://opencollective.com/debug/sponsor/0/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/0/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/1/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/1/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/2/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/2/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/3/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/3/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/4/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/4/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/5/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/5/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/6/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/6/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/7/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/7/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/8/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/8/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/9/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/9/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/10/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/10/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/11/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/11/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/12/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/12/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/13/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/13/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/14/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/14/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/15/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/15/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/16/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/16/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/17/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/17/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/18/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/18/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/19/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/19/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/20/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/20/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/21/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/21/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/22/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/22/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/23/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/23/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/24/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/24/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/25/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/25/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/26/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/26/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/27/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/27/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/28/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/28/avatar.svg"></a>
<a href="https://opencollective.com/debug/sponsor/29/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/29/avatar.svg"></a>
## License
(The MIT License)
Copyright (c) 2014-2017 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1 @@
module.exports = require('./src/node');

View File

@@ -0,0 +1,51 @@
{
"name": "debug",
"version": "3.2.7",
"repository": {
"type": "git",
"url": "git://github.com/visionmedia/debug.git"
},
"description": "small debugging utility",
"keywords": [
"debug",
"log",
"debugger"
],
"files": [
"src",
"node.js",
"dist/debug.js",
"LICENSE",
"README.md"
],
"author": "TJ Holowaychuk <tj@vision-media.ca>",
"contributors": [
"Nathan Rajlich <nathan@tootallnate.net> (http://n8.io)",
"Andrew Rhyne <rhyneandrew@gmail.com>"
],
"license": "MIT",
"dependencies": {
"ms": "^2.1.1"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"browserify": "14.4.0",
"chai": "^3.5.0",
"concurrently": "^3.1.0",
"coveralls": "^3.0.2",
"istanbul": "^0.4.5",
"karma": "^3.0.0",
"karma-chai": "^0.1.0",
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.2",
"mocha": "^5.2.0",
"mocha-lcov-reporter": "^1.2.0",
"rimraf": "^2.5.4",
"xo": "^0.23.0"
},
"main": "./src/index.js",
"browser": "./src/browser.js",
"unpkg": "./dist/debug.js"
}

View File

@@ -0,0 +1,180 @@
"use strict";
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/* eslint-env browser */
/**
* This is the web browser implementation of `debug()`.
*/
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.storage = localstorage();
/**
* Colors.
*/
exports.colors = ['#0000CC', '#0000FF', '#0033CC', '#0033FF', '#0066CC', '#0066FF', '#0099CC', '#0099FF', '#00CC00', '#00CC33', '#00CC66', '#00CC99', '#00CCCC', '#00CCFF', '#3300CC', '#3300FF', '#3333CC', '#3333FF', '#3366CC', '#3366FF', '#3399CC', '#3399FF', '#33CC00', '#33CC33', '#33CC66', '#33CC99', '#33CCCC', '#33CCFF', '#6600CC', '#6600FF', '#6633CC', '#6633FF', '#66CC00', '#66CC33', '#9900CC', '#9900FF', '#9933CC', '#9933FF', '#99CC00', '#99CC33', '#CC0000', '#CC0033', '#CC0066', '#CC0099', '#CC00CC', '#CC00FF', '#CC3300', '#CC3333', '#CC3366', '#CC3399', '#CC33CC', '#CC33FF', '#CC6600', '#CC6633', '#CC9900', '#CC9933', '#CCCC00', '#CCCC33', '#FF0000', '#FF0033', '#FF0066', '#FF0099', '#FF00CC', '#FF00FF', '#FF3300', '#FF3333', '#FF3366', '#FF3399', '#FF33CC', '#FF33FF', '#FF6600', '#FF6633', '#FF9900', '#FF9933', '#FFCC00', '#FFCC33'];
/**
* Currently only WebKit-based Web Inspectors, Firefox >= v31,
* and the Firebug extension (any Firefox version) are known
* to support "%c" CSS customizations.
*
* TODO: add a `localStorage` variable to explicitly enable/disable colors
*/
// eslint-disable-next-line complexity
function useColors() {
// NB: In an Electron preload script, document will be defined but not fully
// initialized. Since we know we're in Chrome, we'll just detect this case
// explicitly
if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
return true;
} // Internet Explorer and Edge do not support colors.
if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
return false;
} // Is webkit? http://stackoverflow.com/a/16459606/376773
// document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
return typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance || // Is firebug? http://stackoverflow.com/a/398120/376773
typeof window !== 'undefined' && window.console && (window.console.firebug || window.console.exception && window.console.table) || // Is firefox >= v31?
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31 || // Double check webkit in userAgent just in case we are in a worker
typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/);
}
/**
* Colorize log arguments if enabled.
*
* @api public
*/
function formatArgs(args) {
args[0] = (this.useColors ? '%c' : '') + this.namespace + (this.useColors ? ' %c' : ' ') + args[0] + (this.useColors ? '%c ' : ' ') + '+' + module.exports.humanize(this.diff);
if (!this.useColors) {
return;
}
var c = 'color: ' + this.color;
args.splice(1, 0, c, 'color: inherit'); // The final "%c" is somewhat tricky, because there could be other
// arguments passed either before or after the %c, so we need to
// figure out the correct index to insert the CSS into
var index = 0;
var lastC = 0;
args[0].replace(/%[a-zA-Z%]/g, function (match) {
if (match === '%%') {
return;
}
index++;
if (match === '%c') {
// We only are interested in the *last* %c
// (the user may have provided their own)
lastC = index;
}
});
args.splice(lastC, 0, c);
}
/**
* Invokes `console.log()` when available.
* No-op when `console.log` is not a "function".
*
* @api public
*/
function log() {
var _console;
// This hackery is required for IE8/9, where
// the `console.log` function doesn't have 'apply'
return (typeof console === "undefined" ? "undefined" : _typeof(console)) === 'object' && console.log && (_console = console).log.apply(_console, arguments);
}
/**
* Save `namespaces`.
*
* @param {String} namespaces
* @api private
*/
function save(namespaces) {
try {
if (namespaces) {
exports.storage.setItem('debug', namespaces);
} else {
exports.storage.removeItem('debug');
}
} catch (error) {// Swallow
// XXX (@Qix-) should we be logging these?
}
}
/**
* Load `namespaces`.
*
* @return {String} returns the previously persisted debug modes
* @api private
*/
function load() {
var r;
try {
r = exports.storage.getItem('debug');
} catch (error) {} // Swallow
// XXX (@Qix-) should we be logging these?
// If debug isn't set in LS, and we're in Electron, try to load $DEBUG
if (!r && typeof process !== 'undefined' && 'env' in process) {
r = process.env.DEBUG;
}
return r;
}
/**
* Localstorage attempts to return the localstorage.
*
* This is necessary because safari throws
* when a user disables cookies/localstorage
* and you attempt to access it.
*
* @return {LocalStorage}
* @api private
*/
function localstorage() {
try {
// TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
// The Browser also has localStorage in the global context.
return localStorage;
} catch (error) {// Swallow
// XXX (@Qix-) should we be logging these?
}
}
module.exports = require('./common')(exports);
var formatters = module.exports.formatters;
/**
* Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
*/
formatters.j = function (v) {
try {
return JSON.stringify(v);
} catch (error) {
return '[UnexpectedJSONParseError]: ' + error.message;
}
};

View File

@@ -0,0 +1,249 @@
"use strict";
/**
* This is the common logic for both the Node.js and web browser
* implementations of `debug()`.
*/
function setup(env) {
createDebug.debug = createDebug;
createDebug.default = createDebug;
createDebug.coerce = coerce;
createDebug.disable = disable;
createDebug.enable = enable;
createDebug.enabled = enabled;
createDebug.humanize = require('ms');
Object.keys(env).forEach(function (key) {
createDebug[key] = env[key];
});
/**
* Active `debug` instances.
*/
createDebug.instances = [];
/**
* The currently active debug mode names, and names to skip.
*/
createDebug.names = [];
createDebug.skips = [];
/**
* Map of special "%n" handling functions, for the debug "format" argument.
*
* Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
*/
createDebug.formatters = {};
/**
* Selects a color for a debug namespace
* @param {String} namespace The namespace string for the for the debug instance to be colored
* @return {Number|String} An ANSI color code for the given namespace
* @api private
*/
function selectColor(namespace) {
var hash = 0;
for (var i = 0; i < namespace.length; i++) {
hash = (hash << 5) - hash + namespace.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
}
createDebug.selectColor = selectColor;
/**
* Create a debugger with the given `namespace`.
*
* @param {String} namespace
* @return {Function}
* @api public
*/
function createDebug(namespace) {
var prevTime;
function debug() {
// Disabled?
if (!debug.enabled) {
return;
}
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var self = debug; // Set `diff` timestamp
var curr = Number(new Date());
var ms = curr - (prevTime || curr);
self.diff = ms;
self.prev = prevTime;
self.curr = curr;
prevTime = curr;
args[0] = createDebug.coerce(args[0]);
if (typeof args[0] !== 'string') {
// Anything else let's inspect with %O
args.unshift('%O');
} // Apply any `formatters` transformations
var index = 0;
args[0] = args[0].replace(/%([a-zA-Z%])/g, function (match, format) {
// If we encounter an escaped % then don't increase the array index
if (match === '%%') {
return match;
}
index++;
var formatter = createDebug.formatters[format];
if (typeof formatter === 'function') {
var val = args[index];
match = formatter.call(self, val); // Now we need to remove `args[index]` since it's inlined in the `format`
args.splice(index, 1);
index--;
}
return match;
}); // Apply env-specific formatting (colors, etc.)
createDebug.formatArgs.call(self, args);
var logFn = self.log || createDebug.log;
logFn.apply(self, args);
}
debug.namespace = namespace;
debug.enabled = createDebug.enabled(namespace);
debug.useColors = createDebug.useColors();
debug.color = selectColor(namespace);
debug.destroy = destroy;
debug.extend = extend; // Debug.formatArgs = formatArgs;
// debug.rawLog = rawLog;
// env-specific initialization logic for debug instances
if (typeof createDebug.init === 'function') {
createDebug.init(debug);
}
createDebug.instances.push(debug);
return debug;
}
function destroy() {
var index = createDebug.instances.indexOf(this);
if (index !== -1) {
createDebug.instances.splice(index, 1);
return true;
}
return false;
}
function extend(namespace, delimiter) {
return createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
}
/**
* Enables a debug mode by namespaces. This can include modes
* separated by a colon and wildcards.
*
* @param {String} namespaces
* @api public
*/
function enable(namespaces) {
createDebug.save(namespaces);
createDebug.names = [];
createDebug.skips = [];
var i;
var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
var len = split.length;
for (i = 0; i < len; i++) {
if (!split[i]) {
// ignore empty strings
continue;
}
namespaces = split[i].replace(/\*/g, '.*?');
if (namespaces[0] === '-') {
createDebug.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
} else {
createDebug.names.push(new RegExp('^' + namespaces + '$'));
}
}
for (i = 0; i < createDebug.instances.length; i++) {
var instance = createDebug.instances[i];
instance.enabled = createDebug.enabled(instance.namespace);
}
}
/**
* Disable debug output.
*
* @api public
*/
function disable() {
createDebug.enable('');
}
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
function enabled(name) {
if (name[name.length - 1] === '*') {
return true;
}
var i;
var len;
for (i = 0, len = createDebug.skips.length; i < len; i++) {
if (createDebug.skips[i].test(name)) {
return false;
}
}
for (i = 0, len = createDebug.names.length; i < len; i++) {
if (createDebug.names[i].test(name)) {
return true;
}
}
return false;
}
/**
* Coerce `val`.
*
* @param {Mixed} val
* @return {Mixed}
* @api private
*/
function coerce(val) {
if (val instanceof Error) {
return val.stack || val.message;
}
return val;
}
createDebug.enable(createDebug.load());
return createDebug;
}
module.exports = setup;

View File

@@ -0,0 +1,12 @@
"use strict";
/**
* Detect Electron renderer / nwjs process, which is node, but we should
* treat as a browser.
*/
if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
module.exports = require('./browser.js');
} else {
module.exports = require('./node.js');
}

View File

@@ -0,0 +1,177 @@
"use strict";
/**
* Module dependencies.
*/
var tty = require('tty');
var util = require('util');
/**
* This is the Node.js implementation of `debug()`.
*/
exports.init = init;
exports.log = log;
exports.formatArgs = formatArgs;
exports.save = save;
exports.load = load;
exports.useColors = useColors;
/**
* Colors.
*/
exports.colors = [6, 2, 3, 4, 5, 1];
try {
// Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json)
// eslint-disable-next-line import/no-extraneous-dependencies
var supportsColor = require('supports-color');
if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
exports.colors = [20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62, 63, 68, 69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112, 113, 128, 129, 134, 135, 148, 149, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 178, 179, 184, 185, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 214, 215, 220, 221];
}
} catch (error) {} // Swallow - we only care if `supports-color` is available; it doesn't have to be.
/**
* Build up the default `inspectOpts` object from the environment variables.
*
* $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
*/
exports.inspectOpts = Object.keys(process.env).filter(function (key) {
return /^debug_/i.test(key);
}).reduce(function (obj, key) {
// Camel-case
var prop = key.substring(6).toLowerCase().replace(/_([a-z])/g, function (_, k) {
return k.toUpperCase();
}); // Coerce string value into JS value
var val = process.env[key];
if (/^(yes|on|true|enabled)$/i.test(val)) {
val = true;
} else if (/^(no|off|false|disabled)$/i.test(val)) {
val = false;
} else if (val === 'null') {
val = null;
} else {
val = Number(val);
}
obj[prop] = val;
return obj;
}, {});
/**
* Is stdout a TTY? Colored output is enabled when `true`.
*/
function useColors() {
return 'colors' in exports.inspectOpts ? Boolean(exports.inspectOpts.colors) : tty.isatty(process.stderr.fd);
}
/**
* Adds ANSI color escape codes if enabled.
*
* @api public
*/
function formatArgs(args) {
var name = this.namespace,
useColors = this.useColors;
if (useColors) {
var c = this.color;
var colorCode = "\x1B[3" + (c < 8 ? c : '8;5;' + c);
var prefix = " ".concat(colorCode, ";1m").concat(name, " \x1B[0m");
args[0] = prefix + args[0].split('\n').join('\n' + prefix);
args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + "\x1B[0m");
} else {
args[0] = getDate() + name + ' ' + args[0];
}
}
function getDate() {
if (exports.inspectOpts.hideDate) {
return '';
}
return new Date().toISOString() + ' ';
}
/**
* Invokes `util.format()` with the specified arguments and writes to stderr.
*/
function log() {
return process.stderr.write(util.format.apply(util, arguments) + '\n');
}
/**
* Save `namespaces`.
*
* @param {String} namespaces
* @api private
*/
function save(namespaces) {
if (namespaces) {
process.env.DEBUG = namespaces;
} else {
// If you set a process.env field to null or undefined, it gets cast to the
// string 'null' or 'undefined'. Just delete instead.
delete process.env.DEBUG;
}
}
/**
* Load `namespaces`.
*
* @return {String} returns the previously persisted debug modes
* @api private
*/
function load() {
return process.env.DEBUG;
}
/**
* Init logic for `debug` instances.
*
* Create a new `inspectOpts` object in case `useColors` is set
* differently for a particular `debug` instance.
*/
function init(debug) {
debug.inspectOpts = {};
var keys = Object.keys(exports.inspectOpts);
for (var i = 0; i < keys.length; i++) {
debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
}
}
module.exports = require('./common')(exports);
var formatters = module.exports.formatters;
/**
* Map %o to `util.inspect()`, all on a single line.
*/
formatters.o = function (v) {
this.inspectOpts.colors = this.useColors;
return util.inspect(v, this.inspectOpts)
.split('\n')
.map(function (str) { return str.trim(); })
.join(' ');
};
/**
* Map %O to `util.inspect()`, allowing multiple lines if needed.
*/
formatters.O = function (v) {
this.inspectOpts.colors = this.useColors;
return util.inspect(v, this.inspectOpts);
};

View File

@@ -0,0 +1,23 @@
{
"permissions": {
"allow": [
"WebFetch(domain:github.com)",
"WebSearch",
"WebFetch(domain:raw.githubusercontent.com)",
"WebFetch(domain:api.github.com)",
"WebFetch(domain:www.npmjs.com)",
"Bash(tree:*)",
"Bash(find:*)",
"Bash(npm view:*)",
"Bash(node test/resolver_sync.js:*)",
"Bash(xxd:*)",
"Bash(npm info:*)",
"Bash(grep:*)",
"Bash(npm run submodule:update:*)",
"Bash(git rev-parse:*)",
"Bash(git -C test/list-exports diff packages/tests/fixtures/resolve-2/project/test/resolver/nested_symlinks/mylib/async.js)",
"Bash(gh run list:*)",
"Bash(ls:*)"
]
}
}

View File

@@ -0,0 +1,35 @@
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 200
[*.{,m}js]
block_comment_start = /*
block_comment = *
block_comment_end = */
[*.yml]
indent_style = space
indent_size = 1
[{package.json,*.mjs}]
indent_style = tab
[CHANGELOG.md]
indent_style = space
indent_size = 2
[{*.json,Makefile,CONTRIBUTING.md,readme.markdown}]
max_line_length = unset
[test/{dotdot,resolver,module_dir,multirepo,node_path,pathfilter,precedence}/**/*]
indent_style = unset
indent_size = unset
max_line_length = unset
insert_final_newline = unset

View File

@@ -0,0 +1,12 @@
# These are supported funding model platforms
github: [ljharb]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: npm/resolve
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

View File

@@ -0,0 +1,119 @@
# Incident Response Process for **resolve**
## Reporting a Vulnerability
We take the security of **resolve** very seriously. If you believe youve found a security vulnerability, please inform us responsibly through coordinated disclosure.
### How to Report
> **Do not** report security vulnerabilities through public GitHub issues, discussions, or social media.
Instead, please use one of these secure channels:
1. **GitHub Security Advisories**
Use the **Report a vulnerability** button in the Security tab of the [browserify/resolve repository](https://github.com/browserify/resolve).
2. **Email**
Follow the posted [Security Policy](https://github.com/browserify/resolve/security/policy).
### What to Include
**Required Information:**
- Brief description of the vulnerability type
- Affected version(s) and components
- Steps to reproduce the issue
- Impact assessment (what an attacker could achieve)
- Confirm the issue is not present in test files (in other words, only via the official entry points in `exports`)
**Helpful Additional Details:**
- Full paths of affected source files
- Specific commit or branch where the issue exists
- Required configuration to reproduce
- Proof-of-concept code (if available)
- Suggested mitigation or fix
## Our Response Process
**Timeline Commitments:**
- **Initial acknowledgment**: Within 24 hours
- **Detailed response**: Within 3 business days
- **Status updates**: Every 7 days until resolved
- **Resolution target**: 90 days for most issues
**What Well Do:**
1. Acknowledge your report and assign a tracking ID
2. Assess the vulnerability and determine severity
3. Develop and test a fix
4. Coordinate disclosure timeline with you
5. Release a security update and publish an advisory and CVE
6. Credit you in our security advisory (if desired)
## Disclosure Policy
- **Coordinated disclosure**: Well work with you on timing
- **Typical timeline**: 90 days from report to public disclosure
- **Early disclosure**: If actively exploited
- **Delayed disclosure**: For complex issues
## Scope
**In Scope:**
- **resolve** package (all supported versions)
- Official examples and documentation
- Core resolution APIs
- Dependencies with direct security implications
**Out of Scope:**
- Third-party wrappers or extensions
- Bundler-specific integrations
- Social engineering or physical attacks
- Theoretical vulnerabilities without practical exploitation
- Issues in non-production files
## Security Measures
**Our Commitments:**
- Regular vulnerability scanning via `npm audit`
- Automated security checks in CI/CD (GitHub Actions)
- Secure coding practices and mandatory code review
- Prompt patch releases for critical issues
**User Responsibilities:**
- Keep **resolve** updated
- Monitor dependency vulnerabilities
- Follow secure configuration guidelines for module resolution
## Legal Safe Harbor
**We will NOT:**
- Initiate legal action
- Contact law enforcement
- Suspend or terminate your access
**You must:**
- Only test against your own installations
- Not access, modify, or delete user data
- Not degrade service availability
- Not publicly disclose before coordinated disclosure
- Act in good faith
## Recognition
- **Advisory Credits**: Credit in GitHub Security Advisories (unless anonymous)
## Security Updates
**Stay Informed:**
- Subscribe to npm updates for **resolve**
- Enable GitHub Security Advisory notifications
**Update Process:**
- Patch releases (e.g., 1.22.10 → 1.22.11)
- Out-of-band releases for critical issues
- Advisories via GitHub Security Advisories
## Contact Information
- **Security reports**: Security tab of [browserify/resolve](https://github.com/browserify/resolve/security)
- **General inquiries**: GitHub Discussions or Issues

View File

@@ -0,0 +1,74 @@
## Threat Model for resolve (module path resolution library)
### 1. Library Overview
- **Library Name:** resolve
- **Brief Description:** Implements Node.js `require.resolve()` algorithm for synchronous and asynchronous file path resolution. Used to locate modules and files in Node.js projects.
- **Key Public APIs/Functions:** `resolve.sync()` / `resolve/sync`, `resolve()` / `resolve/async`
### 2. Define Scope
This threat model focuses on the core path resolution algorithm, including filesystem interaction, option handling, and cache management.
### 3. Conceptual System Diagram
```
Caller Application → resolve(id, options) → Resolution Algorithm → File System
└→ Options Handling
└→ Cache System
```
**Trust Boundaries:**
- **Input module IDs:** May come from untrusted sources (user input, configuration)
- **Filesystem access:** The library interacts with the filesystem to resolve paths
- **Options:** Provided by the caller
- **Cache:** Used to improve performance, but could be a vector for tampering or information disclosure if not handled securely
### 4. Identify Assets
- **Integrity of resolution output:** Ensure correct and safe file path matching.
- **Confidentiality of configuration:** Prevent sensitive path information from being leaked.
- **Availability/performance for host application:** Prevent crashes or resource exhaustion.
- **Security of host application:** Prevent path traversal or unintended filesystem access.
- **Reputation of library:** Maintain trust by avoiding supply chain attacks and vulnerabilities[1][3][4].
### 5. Identify Threats
| Component / API / Interaction | S | T | R | I | D | E |
|-----------------------------------------------------|----|----|----|----|----|----|
| Public API Call (`resolve/async`, `resolve/sync`) | ✓ | ✓ | | ✓ | | |
| Filesystem Access | | ✓ | | ✓ | ✓ | |
| Options Handling | ✓ | ✓ | | ✓ | | |
| Cache System | | ✓ | | ✓ | | |
**Key Threats:**
- **Spoofing:** Malicious module IDs mimicking legitimate packages, or spoofing configuration options[1].
- **Tampering:** Caller-provided paths altering resolution order, or cache tampering leading to incorrect results[1][4].
- **Information Disclosure:** Error messages revealing filesystem structure or sensitive paths[1].
- **Denial of Service:** Recursive or excessive resolution exhausting filesystem handles or causing application crashes[1].
- **Path Traversal:** Malicious input allowing access to files outside the intended directory[4].
### 6. Mitigation/Countermeasures
| Threat Identified | Proposed Mitigation |
|--------------------------------------------|---------------------|
| Spoofing (malicious module IDs/config) | Sanitize input IDs; validate against known patterns; restrict `basedir` to app-controlled paths[1][4]. |
| Tampering (path traversal, cache) | Validate input IDs for directory escapes; secure cache reads/writes; restrict cache to trusted sources[1][4]. |
| Information Disclosure (error messages) | Generic "not found" errors without internal paths; avoid exposing sensitive configuration in errors[1]. |
| Denial of Service (resource exhaustion) | Limit recursive resolution depth; implement timeout; monitor for excessive filesystem operations[1]. |
### 7. Risk Ranking
- **High:** Path traversal via malicious IDs (if not properly mitigated)
- **Medium:** Cache tampering or spoofing (if cache is not secured)
- **Low:** Information disclosure in errors (if error handling is generic)
### 8. Next Steps & Review
1. **Implement input sanitization for module IDs and configuration.**
2. **Add resolution depth limiting and timeout.**
3. **Audit cache handling for race conditions and tampering.**
4. **Regularly review dependencies for vulnerabilities.**
5. **Keep documentation and threat model up to date.**
6. **Monitor for new threats as the ecosystem and library evolve[1][3].**

View File

@@ -0,0 +1,4 @@
[submodule "test/list-exports"]
path = test/list-exports
url = https://github.com/ljharb/list-exports.git
shallow = true

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2012 James Halliday
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,11 @@
# Security
Please file a private vulnerability via GitHub, email [@ljharb](https://github.com/ljharb), or see https://tidelift.com/security if you have a potential security vulnerability to report.
## Incident Response
See our [Incident Response Process](.github/INCIDENT_RESPONSE_PROCESS.md).
## Threat Model
See [THREAT_MODEL.md](./THREAT_MODEL.md).

View File

@@ -0,0 +1,3 @@
'use strict';
module.exports = require('./lib/async');

View File

@@ -0,0 +1,50 @@
#!/usr/bin/env node
'use strict';
var path = require('path');
var fs = require('fs');
if (
String(process.env.npm_lifecycle_script).slice(0, 8) !== 'resolve '
&& (
!process.argv
|| process.argv.length < 2
|| (process.argv[1] !== __filename && fs.statSync(process.argv[1]).ino !== fs.statSync(__filename).ino)
|| (process.env.npm_lifecycle_event !== 'npx' && process.env._ && fs.realpathSync(path.resolve(process.env._)) !== __filename)
)
) {
console.error('Error: `resolve` must be run directly as an executable');
process.exit(1);
}
var supportsPreserveSymlinkFlag = require('supports-preserve-symlinks-flag');
var preserveSymlinks = false;
for (var i = 2; i < process.argv.length; i += 1) {
if (process.argv[i].slice(0, 2) === '--') {
if (supportsPreserveSymlinkFlag && process.argv[i] === '--preserve-symlinks') {
preserveSymlinks = true;
} else if (process.argv[i].length > 2) {
console.error('Unknown argument ' + process.argv[i].replace(/[=].*$/, ''));
process.exit(2);
}
process.argv.splice(i, 1);
i -= 1;
if (process.argv[i] === '--') { break; } // eslint-disable-line no-restricted-syntax
}
}
if (process.argv.length < 3) {
console.error('Error: `resolve` expects a specifier');
process.exit(2);
}
var resolve = require('../');
var result = resolve.sync(process.argv[2], {
basedir: process.cwd(),
preserveSymlinks: preserveSymlinks
});
console.log(result);

View File

@@ -0,0 +1,82 @@
import ljharb from '@ljharb/eslint-config/flat';
export default [
...ljharb,
{
ignores: [
'test/resolver/malformed_package_json/package.json',
'test/list-exports/**',
],
},
{
rules: {
'array-bracket-newline': 'off',
complexity: 'off',
'consistent-return': 'off',
curly: 'off',
'dot-notation': ['error', { allowKeywords: true }],
eqeqeq: ['error', 'allow-null'],
'func-name-matching': 'off',
'func-style': 'off',
'global-require': 'warn',
'id-length': ['error', { min: 1, max: 40 }],
'max-depth': 'off',
'max-lines-per-function': 'off',
'max-lines': 'off',
'max-nested-callbacks': 'off',
'max-params': 'off',
'max-statements-per-line': ['error', { max: 2 }],
'max-statements': 'off',
'multiline-comment-style': 'off',
'no-extra-parens': 'off',
'no-magic-numbers': 'off',
'no-shadow': 'off',
'no-use-before-define': 'off',
'sort-keys': 'off',
strict: 'off',
},
},
{
files: ['**/*.js'],
rules: {
indent: ['error', 4],
},
},
{
files: ['bin/**'],
rules: {
'no-process-exit': 'off',
},
},
{
files: ['example/**'],
rules: {
'no-console': 'off',
},
},
{
files: ['test/resolver/nested_symlinks/mylib/*.js'],
rules: {
'no-throw-literal': 'off',
},
},
{
files: ['test/**'],
languageOptions: {
ecmaVersion: 5,
parserOptions: {
allowReserved: false,
},
},
rules: {
'dot-notation': ['error', { allowPattern: 'throws' }],
'max-lines': 'off',
'max-lines-per-function': 'off',
'no-unused-vars': ['error', {
vars: 'all',
args: 'none',
caughtErrors: 'none',
}],
},
},
];

View File

@@ -0,0 +1,5 @@
var resolve = require('../');
resolve('tap', { basedir: __dirname }, function (err, res) {
if (err) console.error(err);
else console.log(res);
});

View File

@@ -0,0 +1,3 @@
var resolve = require('../');
var res = resolve.sync('tap', { basedir: __dirname });
console.log(res);

View File

@@ -0,0 +1,4 @@
var async = require('./lib/async');
async.sync = require('./lib/sync');
module.exports = async;

View File

@@ -0,0 +1,4 @@
import async from 'resolve/async';
import sync from 'resolve/sync';
export { async, sync };

View File

@@ -0,0 +1,641 @@
var fs = require('fs');
var getHomedir = require('./homedir');
var path = require('path');
var caller = require('./caller');
var nodeModulesPaths = require('./node-modules-paths');
var normalizeOptions = require('./normalize-options');
var isCore = require('is-core-module');
var $Error = require('es-errors');
var $TypeError = require('es-errors/type');
var getCategoryInfo = require('node-exports-info/getCategoryInfo');
var getCategoriesForRange = require('node-exports-info/getCategoriesForRange');
var resolveExports = require('./exports-resolve');
var parsePackageSpecifier = require('./parse-package-specifier');
var getExportsCategory = require('./get-exports-category');
var selectMostRestrictive = require('./select-most-restrictive');
var realpathFS = process.platform !== 'win32' && fs.realpath && typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath;
var relativePathRegex = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/;
var windowsDriveRegex = /^\w:[/\\]*$/;
var nodeModulesRegex = /[/\\]node_modules[/\\]*$/;
var homedir = getHomedir();
function defaultPaths() {
return [
path.join(homedir, '.node_modules'),
path.join(homedir, '.node_libraries')
];
}
var defaultIsFile = function isFile(file, cb) {
fs.stat(file, function (err, stat) {
if (!err) {
return cb(null, stat.isFile() || stat.isFIFO());
}
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
return cb(err);
});
};
var defaultIsDir = function isDirectory(dir, cb) {
fs.stat(dir, function (err, stat) {
if (!err) {
return cb(null, stat.isDirectory());
}
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
return cb(err);
});
};
var defaultRealpath = function realpath(x, cb) {
realpathFS(x, function (realpathErr, realPath) {
if (realpathErr && realpathErr.code !== 'ENOENT') cb(realpathErr);
else cb(null, realpathErr ? x : realPath);
});
};
function maybeRealpath(realpath, x, opts, cb) {
if (!opts || !opts.preserveSymlinks) {
realpath(x, cb);
} else {
cb(null, x);
}
}
function defaultReadPackage(readFile, pkgfile, cb) {
readFile(pkgfile, function (readFileErr, body) {
if (readFileErr) cb(readFileErr);
else {
try {
var pkg = JSON.parse(body);
cb(null, pkg);
} catch (jsonErr) {
cb(jsonErr);
}
}
});
}
function getPackageCandidates(x, start, opts) {
var dirs = nodeModulesPaths(start, opts, x);
for (var i = 0; i < dirs.length; i++) {
dirs[i] = path.join(dirs[i], x);
}
return dirs;
}
function findUpConsumer(currentDir, isFile, readPackage, readFile, done) {
var pkgfile = path.join(currentDir, 'package.json');
isFile(pkgfile, function (err, exists) {
if (err) { return done(err); }
if (exists) {
readPackage(readFile, pkgfile, function (readErr, pkg) {
if (readErr && !(readErr instanceof SyntaxError)) { return done(readErr); }
done(null, pkg || null);
});
} else {
var parentDir = path.dirname(currentDir);
if (parentDir === currentDir) {
return done(null, null);
}
findUpConsumer(parentDir, isFile, readPackage, readFile, done);
}
});
}
function findConsumerPackage(startDir, isFile, readPackage, readFile, done) {
var dir = path.resolve(startDir);
findUpConsumer(dir, isFile, readPackage, readFile, done);
}
function findUpWithDir(currentDir, isFile, readPackage, readFile, done) {
// Stop at node_modules boundaries - can't self-reference across node_modules
if (nodeModulesRegex.test(currentDir)) {
return done(null, null);
}
var pkgfile = path.join(currentDir, 'package.json');
isFile(pkgfile, function (err, exists) {
if (err) { return done(err); }
if (exists) {
readPackage(readFile, pkgfile, function (readErr, pkg) {
if (readErr && !(readErr instanceof SyntaxError)) { return done(readErr); }
done(null, pkg ? {
__proto__: null, pkg: pkg, dir: currentDir
} : null);
});
} else {
var parentDir = path.dirname(currentDir);
if (parentDir === currentDir) {
return done(null, null);
}
findUpWithDir(parentDir, isFile, readPackage, readFile, done);
}
});
}
module.exports = function resolve(x, options, callback) {
var cb = callback;
var opts = options;
if (typeof options === 'function') {
cb = opts;
opts = {};
}
if (typeof x !== 'string') {
var err = new $TypeError('Path must be a string.');
return process.nextTick(function () {
cb(err);
});
}
opts = normalizeOptions(x, opts);
var isFile = opts.isFile || defaultIsFile;
var isDirectory = opts.isDirectory || defaultIsDir;
var readFile = opts.readFile || fs.readFile;
var realpath = opts.realpath || defaultRealpath;
var readPackage = opts.readPackage || defaultReadPackage;
if (opts.readFile && opts.readPackage) {
var conflictErr = new $TypeError('`readFile` and `readPackage` are mutually exclusive.');
return process.nextTick(function () {
cb(conflictErr);
});
}
var packageIterator = opts.packageIterator;
var extensions = opts.extensions || ['.js'];
var includeCoreModules = opts.includeCoreModules !== false;
var basedir = opts.basedir || path.dirname(caller());
var parent = opts.filename || basedir;
opts.paths = opts.paths || defaultPaths();
// Determine exports category
var exportsCategory;
try {
exportsCategory = getExportsCategory(opts);
} catch (catErr) {
return process.nextTick(function () {
cb(catErr);
});
}
// ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
var absoluteStart = path.resolve(basedir);
maybeRealpath(
realpath,
absoluteStart,
opts,
function (err, realStart) {
if (err) cb(err);
else if (exportsCategory === 'engines') {
// Need to read consumer's package.json for engines.node
findConsumerPackage(realStart, isFile, readPackage, readFile, function (findErr, consumerPkg) {
if (findErr) return cb(findErr);
if (consumerPkg && consumerPkg.engines && consumerPkg.engines.node) {
var categories = getCategoriesForRange(consumerPkg.engines.node);
exportsCategory = selectMostRestrictive(categories);
} else {
exportsCategory = null;
}
validateBasedir(realStart);
});
} else {
validateBasedir(realStart);
}
}
);
function findPackageWithDir(startDir, done) {
var dir = path.resolve(startDir);
findUpWithDir(dir, isFile, readPackage, readFile, done);
}
function resolveSelfReference(x, startDir, done) {
var parsed = parsePackageSpecifier(x);
findPackageWithDir(startDir, function (err, pkgInfo) {
if (err) return done(err);
if (!pkgInfo || !pkgInfo.pkg || pkgInfo.pkg.name !== parsed.name) {
return done(null, null); // Not a self-reference
}
var pkg = pkgInfo.pkg;
var pkgDir = pkgInfo.dir;
if (opts.packageFilter) {
pkg = opts.packageFilter(pkg, path.join(pkgDir, 'package.json'), pkgDir);
}
// If package has exports field, resolve via exports
if (typeof pkg.exports !== 'undefined') {
var categoryInfo = getCategoryInfo(exportsCategory, 'require');
var conditions = opts.conditions || categoryInfo.conditions;
var resolved;
try {
resolved = resolveExports(pkg.exports, parsed.subpath, conditions, categoryInfo.flags);
} catch (exportsErr) {
return done(exportsErr);
}
if (resolved) {
var resolvedPath = path.resolve(pkgDir, resolved);
isFile(resolvedPath, function (err, exists) {
if (err) return done(err);
if (exists) return done(null, resolvedPath, pkg);
// File doesn't exist
done(null, undefined);
});
return;
}
// exports field exists but didn't resolve
return done(null, undefined);
}
// No exports field - fall back to traditional resolution for self-reference
if (parsed.subpath === '.') {
loadAsDirectory(pkgDir, pkg, function (err, result, resultPkg) {
done(err, result, resultPkg);
});
} else {
var subPath = path.join(pkgDir, parsed.subpath.slice(1));
loadAsFile(subPath, pkg, function (err, m, mPkg) {
if (err) return done(err);
if (m) return done(null, m, mPkg);
loadAsDirectory(subPath, pkg, function (err, n, nPkg) {
done(err, n, nPkg);
});
});
}
});
}
function validateBasedir(basedir) {
if (opts.basedir) {
var dirError = new $TypeError('Provided basedir "' + basedir + '" is not a directory' + (opts.preserveSymlinks ? '' : ', or a symlink to a directory'));
dirError.code = 'INVALID_BASEDIR';
isDirectory(basedir, function (err, result) {
if (err) return cb(err);
if (!result) { return cb(dirError); }
validBasedir(basedir);
});
} else {
validBasedir(basedir);
}
}
var useExports = false;
var res;
function validBasedir(basedir) {
useExports = exportsCategory !== null && exportsCategory !== 'pre-exports';
if (relativePathRegex.test(x)) {
res = path.resolve(basedir, x);
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
if (x.slice(-1) === '/' && res === basedir) {
loadAsDirectory(res, opts.package, onfile);
} else loadAsFile(res, opts.package, onfile);
} else if (includeCoreModules && isCore(x)) {
return cb(null, x);
} else if (useExports) {
// Try self-reference resolution first
resolveSelfReference(x, basedir, function (selfErr, selfRef, selfPkg) {
if (selfErr) return cb(selfErr);
if (selfRef) {
return maybeRealpath(realpath, selfRef, opts, function (err, realSelf) {
if (err) cb(err);
else cb(null, realSelf, selfPkg);
});
}
if (selfRef === undefined) {
// exports field exists but didn't resolve - error per Node semantics
var moduleError = new $Error("Cannot find module '" + x + "' from '" + parent + "'");
moduleError.code = 'MODULE_NOT_FOUND';
return cb(moduleError);
}
loadNodeModulesWithExports(x, basedir, function (err, n, pkg) {
if (err) cb(err);
else if (n) {
return maybeRealpath(realpath, n, opts, function (err, realN) {
if (err) {
cb(err);
} else {
cb(null, realN, pkg);
}
});
} else {
var moduleError = new $Error("Cannot find module '" + x + "' from '" + parent + "'");
moduleError.code = 'MODULE_NOT_FOUND';
cb(moduleError);
}
});
});
} else {
loadNodeModules(x, basedir, function (err, n, pkg) {
if (err) cb(err);
else if (n) {
return maybeRealpath(realpath, n, opts, function (err, realN) {
if (err) {
cb(err);
} else {
cb(null, realN, pkg);
}
});
} else {
var moduleError = new $Error("Cannot find module '" + x + "' from '" + parent + "'");
moduleError.code = 'MODULE_NOT_FOUND';
cb(moduleError);
}
});
}
}
function onfile(err, m, pkg) {
if (err) cb(err);
else if (m) cb(null, m, pkg);
else loadAsDirectory(res, function (err, d, pkg) {
if (err) cb(err);
else if (d) {
maybeRealpath(realpath, d, opts, function (err, realD) {
if (err) {
cb(err);
} else {
cb(null, realD, pkg);
}
});
} else {
var moduleError = new $Error("Cannot find module '" + x + "' from '" + parent + "'");
moduleError.code = 'MODULE_NOT_FOUND';
cb(moduleError);
}
});
}
function loadAsFile(x, thePackage, callback) {
var loadAsFilePackage = thePackage;
var cb = callback;
if (typeof loadAsFilePackage === 'function') {
cb = loadAsFilePackage;
loadAsFilePackage = undefined;
}
var exts = [''].concat(extensions);
load(exts, x, loadAsFilePackage);
function load(exts, x, loadPackage) {
if (exts.length === 0) return cb(null, undefined, loadPackage);
var file = x + exts[0];
var pkg = loadPackage;
if (pkg) onpkg(null, pkg);
else loadpkg(path.dirname(file), onpkg);
function onpkg(err, pkg_, dir) {
pkg = pkg_;
if (err) return cb(err);
if (dir && pkg && opts.pathFilter) {
var rfile = path.relative(dir, file);
var rel = rfile.slice(0, rfile.length - exts[0].length);
var r = opts.pathFilter(pkg, x, rel);
if (r) return load(
[''].concat(extensions),
path.resolve(dir, r),
pkg
);
}
isFile(file, onex);
}
function onex(err, ex) {
if (err) return cb(err);
if (ex) return cb(null, file, pkg);
load(exts.slice(1), x, pkg);
}
}
}
function loadpkg(dir, cb) {
if (dir === '' || dir === '/') return cb(null);
if (process.platform === 'win32' && windowsDriveRegex.test(dir)) {
return cb(null);
}
if (nodeModulesRegex.test(dir)) return cb(null);
maybeRealpath(realpath, dir, opts, function (unwrapErr, pkgdir) {
if (unwrapErr) return loadpkg(path.dirname(dir), cb);
var pkgfile = path.join(pkgdir, 'package.json');
isFile(pkgfile, function (err, ex) {
// on err, ex is false
if (!ex) return loadpkg(path.dirname(dir), cb);
readPackage(readFile, pkgfile, function (err, pkgParam) {
if (err && !(err instanceof SyntaxError)) cb(err);
var pkg = pkgParam;
if (pkg && opts.packageFilter) {
pkg = opts.packageFilter(pkg, pkgfile, dir);
}
cb(null, pkg, dir);
});
});
});
}
function loadAsDirectory(x, loadAsDirectoryPackage, callback) {
var cb = callback;
var fpkg = loadAsDirectoryPackage;
if (typeof fpkg === 'function') {
cb = fpkg;
fpkg = opts.package;
}
maybeRealpath(realpath, x, opts, function (unwrapErr, pkgdir) {
if (unwrapErr) return loadAsDirectory(path.dirname(x), fpkg, cb);
var pkgfile = path.join(pkgdir, 'package.json');
isFile(pkgfile, function (err, ex) {
if (err) return cb(err);
if (!ex) return loadAsFile(path.join(x, 'index'), fpkg, cb);
readPackage(readFile, pkgfile, function (err, pkgParam) {
if (err) return cb(err);
var pkg = pkgParam;
if (pkg && opts.packageFilter) {
pkg = opts.packageFilter(pkg, pkgfile, pkgdir);
}
if (pkg && pkg.main) {
if (typeof pkg.main !== 'string') {
var mainError = new $TypeError('package “' + pkg.name + '” `main` must be a string');
mainError.code = 'INVALID_PACKAGE_MAIN';
return cb(mainError);
}
if (pkg.main === '.' || pkg.main === './') {
pkg.main = 'index';
}
loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {
if (err) return cb(err);
if (m) return cb(null, m, pkg);
if (!pkg) return loadAsFile(path.join(x, 'index'), pkg, cb);
var dir = path.resolve(x, pkg.main);
loadAsDirectory(dir, pkg, function (err, n, pkg) {
if (err) return cb(err);
if (n) return cb(null, n, pkg);
loadAsFile(path.join(x, 'index'), pkg, function (err, m, pkg) {
if (err) return cb(err);
if (m) return cb(null, m, pkg);
var incorrectMainError = new $Error("Cannot find module '" + path.resolve(x, pkg.main) + "'. Please verify that the package.json has a valid \"main\" entry");
incorrectMainError.code = 'INCORRECT_PACKAGE_MAIN';
return cb(incorrectMainError);
});
});
});
return;
}
loadAsFile(path.join(x, '/index'), pkg, cb);
});
});
});
}
function processDirs(cb, dirs) {
if (dirs.length === 0) return cb(null, undefined);
var dir = dirs[0];
isDirectory(path.dirname(dir), isdir);
function isdir(err, isdir) {
if (err) return cb(err);
if (!isdir) return processDirs(cb, dirs.slice(1));
loadAsFile(dir, opts.package, onfile);
}
function onfile(err, m, pkg) {
if (err) return cb(err);
if (m) return cb(null, m, pkg);
loadAsDirectory(dir, opts.package, ondir);
}
function ondir(err, n, pkg) {
if (err) return cb(err);
if (n) return cb(null, n, pkg);
processDirs(cb, dirs.slice(1));
}
}
function loadNodeModules(x, start, cb) {
var thunk = function () { return getPackageCandidates(x, start, opts); };
processDirs(
cb,
packageIterator ? packageIterator(x, start, thunk, opts) : thunk()
);
}
function loadNodeModulesWithExports(x, start, done) {
var parsed = parsePackageSpecifier(x);
var categoryInfo = getCategoryInfo(exportsCategory, 'require');
var conditions = opts.conditions || categoryInfo.conditions;
var thunk = function () { return getPackageCandidates(parsed.name, start, opts); };
var dirs = packageIterator ? packageIterator(parsed.name, start, thunk, opts) : thunk();
processExportsDirs(dirs, 0);
function processExportsDirs(dirs, idx) {
if (idx >= dirs.length) return done(null, undefined);
var pkgDir = dirs[idx];
isDirectory(pkgDir, function (err, isDir) {
if (err) return done(err);
if (!isDir) return processExportsDirs(dirs, idx + 1);
var pkgfile = path.join(pkgDir, 'package.json');
isFile(pkgfile, function (err, exists) {
if (err) return done(err);
if (!exists) {
// No package.json, fall back to file/directory resolution
if (parsed.subpath === '.') {
loadAsFile(pkgDir, opts.package, function (err, m, pkg) {
if (err) return done(err);
if (m) return done(null, m, pkg);
loadAsDirectory(pkgDir, opts.package, function (err, n, pkg) {
if (err) return done(err);
if (n) return done(null, n, pkg);
processExportsDirs(dirs, idx + 1);
});
});
} else {
var fullPath = path.join(pkgDir, parsed.subpath.slice(1));
loadAsFile(fullPath, opts.package, function (err, m, pkg) {
if (err) return done(err);
if (m) return done(null, m, pkg);
loadAsDirectory(fullPath, opts.package, function (err, n, pkg) {
if (err) return done(err);
if (n) return done(null, n, pkg);
processExportsDirs(dirs, idx + 1);
});
});
}
return;
}
readPackage(readFile, pkgfile, function (err, pkg) {
if (err) {
if (!(err instanceof SyntaxError)) return done(err);
return processExportsDirs(dirs, idx + 1);
}
if (pkg && opts.packageFilter) {
// eslint-disable-next-line no-param-reassign
pkg = opts.packageFilter(pkg, pkgfile, pkgDir);
}
// If package has exports field, use exports resolution
if (pkg && typeof pkg.exports !== 'undefined') {
var resolved;
try {
resolved = resolveExports(pkg.exports, parsed.subpath, conditions, categoryInfo.flags);
} catch (exportsErr) {
return done(exportsErr);
}
if (resolved) {
var resolvedPath = path.resolve(pkgDir, resolved);
isFile(resolvedPath, function (err, exists) {
if (err) return done(err);
if (exists) return done(null, resolvedPath, pkg);
// File doesn't exist
done(null, undefined);
});
return;
}
// exports field exists but didn't resolve
return done(null, undefined);
}
// No exports field, fall back to traditional resolution
if (parsed.subpath === '.') {
loadAsDirectory(pkgDir, pkg, function (err, result, resultPkg) {
if (err) return done(err);
if (result) return done(null, result, resultPkg);
processExportsDirs(dirs, idx + 1);
});
} else {
var subPath = path.join(pkgDir, parsed.subpath.slice(1));
loadAsFile(subPath, pkg, function (err, m, mPkg) {
if (err) return done(err);
if (m) return done(null, m, mPkg);
loadAsDirectory(subPath, pkg, function (err, n, nPkg) {
if (err) return done(err);
if (n) return done(null, n, nPkg);
processExportsDirs(dirs, idx + 1);
});
});
}
});
});
});
}
}
};

View File

@@ -0,0 +1,12 @@
'use strict';
var $Error = require('es-errors');
module.exports = function () {
// see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
var origPrepareStackTrace = $Error.prepareStackTrace;
$Error.prepareStackTrace = function (_, stack) { return stack; };
var stack = (new $Error()).stack;
$Error.prepareStackTrace = origPrepareStackTrace;
return stack[2].getFileName();
};

View File

@@ -0,0 +1,273 @@
'use strict';
var objectKeys = require('object-keys');
var $Error = require('es-errors');
// Check if an exports map key looks like a subpath (starts with '.')
function isSubpathKey(key) {
return key.length > 0 && key.charAt(0) === '.';
}
// Normalize the exports field into a map of subpath -> target
function normalizeExports(exportsField) {
if (typeof exportsField === 'string') {
return { __proto__: null, '.': exportsField };
}
if (Array.isArray(exportsField)) {
return { __proto__: null, '.': exportsField };
}
if (typeof exportsField === 'object' && exportsField !== null) {
var keys = objectKeys(exportsField);
if (keys.length === 0) {
return { __proto__: null };
}
// If any key starts with '.', it's a subpath map
// If no key starts with '.', it's a conditions object for '.'
var hasSubpath = false;
for (var i = 0; !hasSubpath && i < keys.length; i++) {
if (isSubpathKey(keys[i])) {
hasSubpath = true;
}
}
// Copy to new object with null prototype
var result = { __proto__: null };
for (var j = 0; j < keys.length; j++) {
result[keys[j]] = exportsField[keys[j]];
}
if (hasSubpath) {
return result;
}
return { __proto__: null, '.': result };
}
return null;
}
// Resolve a target value through conditions
// conditions: array of condition strings, or null (broken: string/array only)
function resolveTarget(target, conditions) {
if (typeof target === 'string') {
return target;
}
if (target === null) {
return null;
}
if (Array.isArray(target)) {
for (var i = 0; i < target.length; i++) {
var resolved = resolveTarget(target[i], conditions);
if (resolved !== null && typeof resolved !== 'undefined') {
return resolved;
}
}
return null;
}
if (typeof target === 'object') {
// If no conditions supported (broken category), can't resolve objects
if (conditions === null) {
return null;
}
var keys = objectKeys(target);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
for (var k = 0; k < conditions.length; k++) {
if (key === conditions[k]) {
var result = resolveTarget(target[key], conditions);
if (result != null) {
return result;
}
}
}
}
return null;
}
return null;
}
// Validate a resolved path
function validateTarget(target) {
if (typeof target !== 'string') {
return false;
}
if (target.slice(0, 2) !== './') {
return false;
}
if (target.indexOf('/node_modules/') !== -1) {
return false;
}
// Check for '..' path traversal
var parts = target.split('/');
for (var i = 0; i < parts.length; i++) {
if (parts[i] === '..') {
return false;
}
}
return true;
}
// Find the best pattern match for a subpath among keys with '*'
function findPatternMatch(subpath, exportsMap, allowPatternTrailers) {
var keys = objectKeys(exportsMap);
var bestKey = null;
var bestPrefixLen = -1;
var bestMatch = '';
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var starIndex = key.indexOf('*');
// Key must have exactly one '*'
if (starIndex !== -1 && key.indexOf('*', starIndex + 1) === -1) {
var prefix = key.slice(0, starIndex);
var suffix = key.slice(starIndex + 1);
// Pattern trailers: if suffix is non-empty after *, need allowPatternTrailers
if (suffix.length === 0 || allowPatternTrailers) {
if (
subpath.length >= prefix.length + suffix.length
&& subpath.slice(0, prefix.length) === prefix
&& (suffix.length === 0 || subpath.slice(subpath.length - suffix.length) === suffix)
) {
// Longest prefix wins
if (prefix.length > bestPrefixLen) {
bestPrefixLen = prefix.length;
bestKey = key;
bestMatch = subpath.slice(prefix.length, subpath.length - suffix.length);
}
}
}
}
}
if (bestKey !== null) {
return {
__proto__: null, key: bestKey, match: bestMatch
};
}
return null;
}
// Find directory slash match (for categories that support it)
function findDirSlashMatch(subpath, exportsMap) {
var keys = objectKeys(exportsMap);
var bestKey = null;
var bestPrefixLen = -1;
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (key.charAt(key.length - 1) === '/') {
if (subpath.slice(0, key.length) === key && key.length > bestPrefixLen) {
bestPrefixLen = key.length;
bestKey = key;
}
}
}
if (bestKey !== null) {
return {
__proto__: null, key: bestKey, remainder: subpath.slice(bestKey.length)
};
}
return null;
}
// Replace '*' in target string with match value
function substitutePattern(target, match) {
if (typeof target === 'string') {
return target.split('*').join(match);
}
if (Array.isArray(target)) {
var result = [];
for (var i = 0; i < target.length; i++) {
result.push(substitutePattern(target[i], match));
}
return result;
}
if (typeof target === 'object' && target !== null) {
var obj = { __proto__: null };
var keys = objectKeys(target);
for (var j = 0; j < keys.length; j++) {
obj[keys[j]] = substitutePattern(target[keys[j]], match);
}
return obj;
}
return target;
}
// Main exports resolution function
// exportsField: the value of package.json "exports"
// subpath: the subpath to resolve (e.g., "." or "./foo/bar")
// conditions: array of condition strings, or null for broken category
// options: { patterns: boolean, patternTrailers: boolean, dirSlash: boolean }
// Returns: resolved relative path string, or null if no exports field
// Throws: when exports field exists but subpath is not exported
module.exports = function resolveExports(exportsField, subpath, conditions, options) {
if (typeof exportsField === 'undefined') {
return null;
}
var exportsMap = normalizeExports(exportsField);
if (!exportsMap) {
return null;
}
var allowPatterns = options && options.patterns;
var allowPatternTrailers = options && options.patternTrailers;
var allowDirSlash = options && options.dirSlash;
// 1. Exact key match
if (typeof exportsMap[subpath] !== 'undefined') {
var resolved = resolveTarget(exportsMap[subpath], conditions);
if (resolved !== null && typeof resolved !== 'undefined') {
if (!validateTarget(resolved)) {
var invalidError = new $Error('Invalid "exports" target "' + resolved + '" for subpath "' + subpath + '"');
invalidError.code = 'ERR_INVALID_PACKAGE_CONFIG';
throw invalidError;
}
return resolved;
}
// Target exists but resolved to null (explicitly not exported)
var notExportedError = new $Error('Package subpath "' + subpath + '" is not defined by "exports"');
notExportedError.code = 'ERR_PACKAGE_PATH_NOT_EXPORTED';
throw notExportedError;
}
// 2. Pattern match (keys with '*')
if (allowPatterns) {
var patternResult = findPatternMatch(subpath, exportsMap, allowPatternTrailers);
if (patternResult) {
var substituted = substitutePattern(exportsMap[patternResult.key], patternResult.match);
var patternResolved = resolveTarget(substituted, conditions);
if (patternResolved !== null && typeof patternResolved !== 'undefined') {
if (!validateTarget(patternResolved)) {
var patternInvalidError = new $Error('Invalid "exports" target "' + patternResolved + '" for subpath "' + subpath + '"');
patternInvalidError.code = 'ERR_INVALID_PACKAGE_CONFIG';
throw patternInvalidError;
}
return patternResolved;
}
}
}
// 3. Directory slash match (for older categories)
if (allowDirSlash) {
var dirResult = findDirSlashMatch(subpath, exportsMap);
if (dirResult) {
var dirTarget = resolveTarget(exportsMap[dirResult.key], conditions);
if (dirTarget !== null && typeof dirTarget !== 'undefined' && typeof dirTarget === 'string') {
var dirResolved = dirTarget + dirResult.remainder;
if (!validateTarget(dirResolved)) {
var dirInvalidError = new $Error('Invalid "exports" target "' + dirResolved + '" for subpath "' + subpath + '"');
dirInvalidError.code = 'ERR_INVALID_PACKAGE_CONFIG';
throw dirInvalidError;
}
return dirResolved;
}
}
}
var err = new $Error('Package subpath "' + subpath + '" is not defined by "exports"');
err.code = 'ERR_PACKAGE_PATH_NOT_EXPORTED';
throw err;
};

View File

@@ -0,0 +1,52 @@
'use strict';
var isCategory = require('node-exports-info/isCategory');
var getCategoriesForRange = require('node-exports-info/getCategoriesForRange');
var $TypeError = require('es-errors/type');
var selectMostRestrictive = require('./select-most-restrictive');
// Determine the active exports category from resolve options
// Returns null if no exports resolution should be applied
// Returns 'engines' if engines: true (needs consumer package.json lookup)
// Throws TypeError if invalid options are provided
/** @type {(opts?: { exportsCategory?: import('node-exports-info/getCategory').Category, engines?: boolean | string }) => null | import('node-exports-info/getCategory').Category} */
module.exports = function getExportsCategory(opts) {
if (!opts) {
return null;
}
var hasCategory = typeof opts.exportsCategory !== 'undefined';
var engines = opts.engines;
var hasEngines = typeof engines !== 'undefined' && engines !== false;
if (hasCategory && hasEngines) {
throw new $TypeError('`exportsCategory` and `engines` are mutually exclusive.');
}
if (hasCategory) {
if (!isCategory(opts.exportsCategory)) {
var catError = new $TypeError('Invalid exports category: "' + opts.exportsCategory + '"');
catError.code = 'INVALID_EXPORTS_CATEGORY';
throw catError;
}
return opts.exportsCategory;
}
if (hasEngines) {
// engines: true means read from consumer's package.json
if (engines === true) {
return 'engines';
}
// engines must be a non-empty string (semver range)
if (typeof engines !== 'string' || engines === '') {
throw new $TypeError('`engines` must be `true`, `false`, or a non-empty string semver range.');
}
var categories = getCategoriesForRange(engines);
return selectMostRestrictive(categories);
}
return null;
};

View File

@@ -0,0 +1,24 @@
'use strict';
var os = require('os');
// adapted from https://github.com/sindresorhus/os-homedir/blob/11e089f4754db38bb535e5a8416320c4446e8cfd/index.js
module.exports = os.homedir || function homedir() {
var home = process.env.HOME;
var user = process.env.LOGNAME || process.env.USER || process.env.LNAME || process.env.USERNAME;
if (process.platform === 'win32') {
return process.env.USERPROFILE || process.env.HOMEDRIVE + process.env.HOMEPATH || home || null;
}
if (process.platform === 'darwin') {
return home || (user ? '/Users/' + user : null);
}
if (process.platform === 'linux') {
return home || (process.getuid() === 0 ? '/root' : (user ? '/home/' + user : null));
}
return home || null;
};

View File

@@ -0,0 +1,45 @@
var path = require('path');
var parse = path.parse || require('path-parse'); // eslint-disable-line global-require
var driveLetterRegex = /^([A-Za-z]:)/;
var uncPathRegex = /^\\\\/;
function getNodeModulesDirs(absoluteStart, modules) {
var prefix = '/';
if (driveLetterRegex.test(absoluteStart)) {
prefix = '';
} else if (uncPathRegex.test(absoluteStart)) {
prefix = '\\\\';
}
var paths = [absoluteStart];
var parsed = parse(absoluteStart);
while (parsed.dir !== paths[paths.length - 1]) {
paths.push(parsed.dir);
parsed = parse(parsed.dir);
}
return paths.reduce(function (dirs, aPath) {
return dirs.concat(modules.map(function (moduleDir) {
return path.resolve(prefix, aPath, moduleDir);
}));
}, []);
}
module.exports = function nodeModulesPaths(start, opts, request) {
var modules = opts && opts.moduleDirectory
? [].concat(opts.moduleDirectory)
: ['node_modules'];
if (opts && typeof opts.paths === 'function') {
return opts.paths(
request,
start,
function () { return getNodeModulesDirs(start, modules); },
opts
);
}
var dirs = getNodeModulesDirs(start, modules);
return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
};

View File

@@ -0,0 +1,10 @@
module.exports = function (x, opts) {
/**
* This file is purposefully a passthrough. It's expected that third-party
* environments will override it at runtime in order to inject special logic
* into `resolve` (by manipulating the options). One such example is the PnP
* code path in Yarn.
*/
return opts || {};
};

View File

@@ -0,0 +1,31 @@
'use strict';
/** @type {(x: string) => { __proto__: null, name: string, subpath: string }} */
module.exports = function parsePackageSpecifier(x) {
if (x.charAt(0) === '@') {
var slashIndex = x.indexOf('/');
if (slashIndex === -1) {
return {
__proto__: null, name: x, subpath: '.'
};
}
var secondSlash = x.indexOf('/', slashIndex + 1);
if (secondSlash === -1) {
return {
__proto__: null, name: x, subpath: '.'
};
}
return {
__proto__: null, name: x.slice(0, secondSlash), subpath: '.' + x.slice(secondSlash)
};
}
var firstSlash = x.indexOf('/');
if (firstSlash === -1) {
return {
__proto__: null, name: x, subpath: '.'
};
}
return {
__proto__: null, name: x.slice(0, firstSlash), subpath: '.' + x.slice(firstSlash)
};
};

View File

@@ -0,0 +1,42 @@
'use strict';
// Category ranking from most restrictive (lowest rank) to least restrictive (highest rank)
// Lower rank = more restrictive = fewer features supported
var categoryRank = /** @type {const} */ {
__proto__: null,
'pre-exports': /** @type {const} */ (0),
broken: /** @type {const} */ (1),
experimental: /** @type {const} */ (2),
conditions: /** @type {const} */ (3),
'broken-dir-slash-conditions': /** @type {const} */ (4),
patterns: /** @type {const} */ (5),
'pattern-trailers': /** @type {const} */ (6),
'pattern-trailers+json-imports': /** @type {const} */ (7),
'pattern-trailers-no-dir-slash': /** @type {const} */ (8),
'pattern-trailers-no-dir-slash+json-imports': /** @type {const} */ (9),
'require-esm': /** @type {const} */ (10),
'strips-types': /** @type {const} */ (11),
'subpath-imports-slash': /** @type {const} */ (12)
};
// Select the most restrictive category from an array of categories
/** @type {(categories?: ReturnType<import('node-exports-info/getCategory')>[]) => import('node-exports-info/getCategory').Category | null} */
module.exports = function selectMostRestrictive(categories) {
if (!categories || categories.length === 0) {
return null;
}
var mostRestrictive = null;
var lowestRank = Infinity;
for (var i = 0; i < categories.length; i++) {
var cat = categories[i];
var rank = categoryRank[cat];
if (typeof rank === 'number' && rank < lowestRank) {
lowestRank = rank;
mostRestrictive = cat;
}
}
return mostRestrictive;
};

View File

@@ -0,0 +1,418 @@
var isCore = require('is-core-module');
var fs = require('fs');
var path = require('path');
var $Error = require('es-errors');
var $TypeError = require('es-errors/type');
var getHomedir = require('./homedir');
var caller = require('./caller');
var nodeModulesPaths = require('./node-modules-paths');
var normalizeOptions = require('./normalize-options');
var resolveExports = require('./exports-resolve');
var parsePackageSpecifier = require('./parse-package-specifier');
var getExportsCategory = require('./get-exports-category');
var getCategoryInfo = require('node-exports-info/getCategoryInfo');
var getCategoriesForRange = require('node-exports-info/getCategoriesForRange');
var selectMostRestrictive = require('./select-most-restrictive');
var realpathFS = process.platform !== 'win32' && fs.realpathSync && typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;
var relativePathRegex = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/;
var windowsDriveRegex = /^\w:[/\\]*$/;
var nodeModulesRegex = /[/\\]node_modules[/\\]*$/;
var homedir = getHomedir();
function defaultPaths() {
return [
path.join(homedir, '.node_modules'),
path.join(homedir, '.node_libraries')
];
}
var defaultIsFile = function isFile(file) {
try {
var stat = fs.statSync(file, { throwIfNoEntry: false });
} catch (e) {
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
throw e;
}
return !!stat && (stat.isFile() || stat.isFIFO());
};
var defaultIsDir = function isDirectory(dir) {
try {
var stat = fs.statSync(dir, { throwIfNoEntry: false });
} catch (e) {
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
throw e;
}
return !!stat && stat.isDirectory();
};
var defaultRealpathSync = function realpathSync(x) {
try {
return realpathFS(x);
} catch (realpathErr) {
if (realpathErr.code !== 'ENOENT') {
throw realpathErr;
}
}
return x;
};
function maybeRealpathSync(realpathSync, x, opts) {
if (!opts || !opts.preserveSymlinks) {
return realpathSync(x);
}
return x;
}
function defaultReadPackageSync(readFileSync, pkgfile) {
return JSON.parse(readFileSync(pkgfile));
}
function getPackageCandidates(x, start, opts) {
var dirs = nodeModulesPaths(start, opts, x);
for (var i = 0; i < dirs.length; i++) {
dirs[i] = path.join(dirs[i], x);
}
return dirs;
}
function findConsumerPackageSync(startDir, isFile, readPackageSync, readFileSync) {
var dir = path.resolve(startDir);
while (true) {
var pkgfile = path.join(dir, 'package.json');
if (isFile(pkgfile)) {
try {
return readPackageSync(readFileSync, pkgfile);
} catch (e) {
if (!(e instanceof SyntaxError)) {
throw e;
}
}
}
var parentDir = path.dirname(dir);
if (parentDir === dir) {
return null;
}
dir = parentDir;
}
}
function findPackageWithDirSync(startDir, isFile, readPackageSync, readFileSync) {
var dir = path.resolve(startDir);
while (true) {
// Stop at node_modules boundaries - can't self-reference across node_modules
if (nodeModulesRegex.test(dir)) {
return null;
}
var pkgfile = path.join(dir, 'package.json');
if (isFile(pkgfile)) {
try {
var pkg = readPackageSync(readFileSync, pkgfile);
return {
__proto__: null, pkg: pkg, dir: dir
};
} catch (e) {
if (!(e instanceof SyntaxError)) {
throw e;
}
}
}
var parentDir = path.dirname(dir);
if (parentDir === dir) {
return null;
}
dir = parentDir;
}
}
module.exports = function resolveSync(x, options) {
if (typeof x !== 'string') {
throw new $TypeError('Path must be a string.');
}
var opts = normalizeOptions(x, options);
var isFile = opts.isFile || defaultIsFile;
var isDirectory = opts.isDirectory || defaultIsDir;
var readFileSync = opts.readFileSync || fs.readFileSync;
var realpathSync = opts.realpathSync || defaultRealpathSync;
var readPackageSync = opts.readPackageSync || defaultReadPackageSync;
if (opts.readFileSync && opts.readPackageSync) {
throw new $TypeError('`readFileSync` and `readPackageSync` are mutually exclusive.');
}
var packageIterator = opts.packageIterator;
var extensions = opts.extensions || ['.js'];
var includeCoreModules = opts.includeCoreModules !== false;
var basedir = opts.basedir || path.dirname(caller());
var parent = opts.filename || basedir;
opts.paths = opts.paths || defaultPaths();
// Determine exports category
var exportsCategory = getExportsCategory(opts);
if (exportsCategory === 'engines') {
// Read consumer's package.json to get engines.node
var consumerPkg = findConsumerPackageSync(basedir, isFile, readPackageSync, readFileSync);
if (consumerPkg && consumerPkg.engines && consumerPkg.engines.node) {
var categories = getCategoriesForRange(consumerPkg.engines.node);
exportsCategory = selectMostRestrictive(categories);
} else {
exportsCategory = null;
}
}
var useExports = exportsCategory !== null && exportsCategory !== 'pre-exports';
// ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
var absoluteStart = maybeRealpathSync(realpathSync, path.resolve(basedir), opts);
if (opts.basedir && !isDirectory(absoluteStart)) {
var dirError = new $TypeError('Provided basedir "' + opts.basedir + '" is not a directory' + (opts.preserveSymlinks ? '' : ', or a symlink to a directory'));
dirError.code = 'INVALID_BASEDIR';
throw dirError;
}
if (relativePathRegex.test(x)) {
var res = path.resolve(absoluteStart, x);
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return maybeRealpathSync(realpathSync, m, opts);
} else if (includeCoreModules && isCore(x)) {
return x;
} else if (useExports) {
// Try self-reference resolution first
var selfRef = resolveSelfReferenceSync(x, absoluteStart);
if (selfRef) return maybeRealpathSync(realpathSync, selfRef, opts);
var nE = loadNodeModulesWithExportsSync(x, absoluteStart);
if (nE) return maybeRealpathSync(realpathSync, nE, opts);
} else {
var n = loadNodeModulesSync(x, absoluteStart);
if (n) return maybeRealpathSync(realpathSync, n, opts);
}
var err = new $Error("Cannot find module '" + x + "' from '" + parent + "'");
err.code = 'MODULE_NOT_FOUND';
throw err;
function resolveSelfReferenceSync(x, startDir) {
var parsed = parsePackageSpecifier(x);
var pkgInfo = findPackageWithDirSync(startDir, isFile, readPackageSync, readFileSync);
if (!pkgInfo || !pkgInfo.pkg || pkgInfo.pkg.name !== parsed.name) {
return null; // Not a self-reference
}
var pkg = pkgInfo.pkg;
var pkgDir = pkgInfo.dir;
if (opts.packageFilter) {
pkg = opts.packageFilter(pkg, path.join(pkgDir, 'package.json'), pkgDir);
}
// If package has exports field, resolve via exports
if (typeof pkg.exports !== 'undefined') {
var categoryInfo = getCategoryInfo(exportsCategory, 'require');
var conditions = opts.conditions || categoryInfo.conditions;
var resolved = resolveExports(pkg.exports, parsed.subpath, conditions, categoryInfo.flags);
if (resolved) {
var resolvedPath = path.resolve(pkgDir, resolved);
if (isFile(resolvedPath)) {
return resolvedPath;
}
}
// exports field exists but didn't resolve - this is an error per Node semantics
return undefined;
}
// No exports field - fall back to traditional resolution for self-reference
// (Note: this matches Node's behavior where self-ref without exports uses main)
if (parsed.subpath === '.') {
return loadAsDirectorySync(pkgDir);
}
var subPath = path.join(pkgDir, parsed.subpath.slice(1));
var sm = loadAsFileSync(subPath);
if (sm) return sm;
return loadAsDirectorySync(subPath);
}
function loadAsFileSync(x) {
var pkg = loadpkg(path.dirname(x));
if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) {
var rfile = path.relative(pkg.dir, x);
var r = opts.pathFilter(pkg.pkg, x, rfile);
if (r) {
x = path.resolve(pkg.dir, r); // eslint-disable-line no-param-reassign
}
}
if (isFile(x)) {
return x;
}
for (var i = 0; i < extensions.length; i++) {
var file = x + extensions[i];
if (isFile(file)) {
return file;
}
}
}
function loadpkg(dir) {
if (dir === '' || dir === '/') return;
if (process.platform === 'win32' && windowsDriveRegex.test(dir)) {
return;
}
if (nodeModulesRegex.test(dir)) return;
var pkgfile = path.join(isDirectory(dir) ? maybeRealpathSync(realpathSync, dir, opts) : dir, 'package.json');
if (!isFile(pkgfile)) {
return loadpkg(path.dirname(dir));
}
var pkg;
try {
pkg = readPackageSync(readFileSync, pkgfile);
} catch (e) {
if (!(e instanceof SyntaxError)) {
throw e;
}
}
if (pkg && opts.packageFilter) {
pkg = opts.packageFilter(pkg, pkgfile, dir);
}
return { pkg: pkg, dir: dir };
}
function loadAsDirectorySync(x) {
var pkgfile = path.join(isDirectory(x) ? maybeRealpathSync(realpathSync, x, opts) : x, '/package.json');
if (isFile(pkgfile)) {
try {
var pkg = readPackageSync(readFileSync, pkgfile);
} catch (e) {}
if (pkg && opts.packageFilter) {
pkg = opts.packageFilter(pkg, pkgfile, x);
}
if (pkg && pkg.main) {
if (typeof pkg.main !== 'string') {
var mainError = new $TypeError('package “' + pkg.name + '” `main` must be a string');
mainError.code = 'INVALID_PACKAGE_MAIN';
throw mainError;
}
if (pkg.main === '.' || pkg.main === './') {
pkg.main = 'index';
}
try {
var mainPath = path.resolve(x, pkg.main);
var m = loadAsFileSync(mainPath);
if (m) return m;
var n = loadAsDirectorySync(mainPath);
if (n) return n;
var checkIndex = loadAsFileSync(path.resolve(x, 'index'));
if (checkIndex) return checkIndex;
} catch (e) { }
var incorrectMainError = new $Error("Cannot find module '" + path.resolve(x, pkg.main) + "'. Please verify that the package.json has a valid \"main\" entry");
incorrectMainError.code = 'INCORRECT_PACKAGE_MAIN';
throw incorrectMainError;
}
}
return loadAsFileSync(path.join(x, '/index'));
}
function loadNodeModulesSync(x, start) {
var thunk = function () { return getPackageCandidates(x, start, opts); };
var dirs = packageIterator ? packageIterator(x, start, thunk, opts) : thunk();
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
if (isDirectory(path.dirname(dir))) {
var m = loadAsFileSync(dir);
if (m) return m;
var n = loadAsDirectorySync(dir);
if (n) return n;
}
}
}
function loadNodeModulesWithExportsSync(x, start) {
var parsed = parsePackageSpecifier(x);
var categoryInfo = getCategoryInfo(exportsCategory, 'require');
var conditions = opts.conditions || categoryInfo.conditions;
// Get candidate directories for the package name
var thunk = function () { return getPackageCandidates(parsed.name, start, opts); };
var dirs = packageIterator ? packageIterator(parsed.name, start, thunk, opts) : thunk();
for (var i = 0; i < dirs.length; i++) {
var pkgDir = dirs[i];
if (isDirectory(pkgDir)) {
var pkgfile = path.join(pkgDir, 'package.json');
if (isFile(pkgfile)) {
var pkg;
try {
pkg = readPackageSync(readFileSync, pkgfile);
} catch (e) {
if (!(e instanceof SyntaxError)) {
throw e;
}
pkg = null;
}
if (pkg) {
if (opts.packageFilter) {
pkg = opts.packageFilter(pkg, pkgfile, pkgDir);
}
// If package has exports field, use exports resolution
if (typeof pkg.exports !== 'undefined') {
var resolved = resolveExports(pkg.exports, parsed.subpath, conditions, categoryInfo.flags);
if (resolved) {
var resolvedPath = path.resolve(pkgDir, resolved);
if (isFile(resolvedPath)) {
return resolvedPath;
}
}
// exports field exists but didn't resolve - this is an error per Node semantics
// (don't fall through to main/index)
return undefined;
}
// No exports field, fall back to traditional resolution
if (parsed.subpath === '.') {
var result = loadAsDirectorySync(pkgDir);
if (result) { return result; }
} else {
var subPath = path.join(pkgDir, parsed.subpath.slice(1));
var sm = loadAsFileSync(subPath);
if (sm) { return sm; }
var sn = loadAsDirectorySync(subPath);
if (sn) { return sn; }
}
}
} else if (parsed.subpath === '.') {
// No package.json, fall back to file/directory resolution
var m = loadAsFileSync(pkgDir);
if (m) { return m; }
var n = loadAsDirectorySync(pkgDir);
if (n) { return n; }
} else {
// No package.json, fall back to file/directory resolution for subpath
var fullPath = path.join(pkgDir, parsed.subpath.slice(1));
var m2 = loadAsFileSync(fullPath);
if (m2) { return m2; }
var n2 = loadAsDirectorySync(fullPath);
if (n2) { return n2; }
}
}
}
}
};

View File

@@ -0,0 +1,92 @@
{
"name": "resolve",
"description": "resolve like require.resolve() on behalf of files asynchronously and synchronously",
"version": "2.0.0-next.6",
"repository": {
"type": "git",
"url": "ssh://github.com/browserify/resolve.git"
},
"bin": {
"resolve": "./bin/resolve"
},
"main": "index.js",
"exports": {
".": [
{
"import": "./index.mjs",
"default": "./index.js"
},
"./index.js"
],
"./sync": "./lib/sync.js",
"./async": "./lib/async.js",
"./package.json": "./package.json"
},
"keywords": [
"resolve",
"require",
"node",
"module"
],
"scripts": {
"prepack": "npmignore --auto --commentLines=autogenerated",
"prepublishOnly": "safe-publish-latest",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prelint": "eclint check $(git ls-files | grep -Ev test\\/list-exports$ | xargs find 2> /dev/null | grep -vE 'node_modules|\\.git')",
"lint": "eslint .",
"pretests-only": "npm run submodule:update && cd ./test/resolver/nested_symlinks && node mylib/sync && node mylib/async",
"tests-only": "tape test/*.js",
"pretest": "npm run lint",
"test": "npm run --silent tests-only",
"posttest": "npm run test:multirepo && npx npm@\">= 10.2\" audit --production",
"test:multirepo": "cd ./test/resolver/multirepo && npm install && npm test",
"submodule:update": "git submodule update --init --depth 1 && cd test/list-exports && git sparse-checkout init --cone && git sparse-checkout set packages/tests"
},
"license": "MIT",
"author": {
"name": "James Halliday",
"email": "mail@substack.net",
"url": "http://substack.net"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
},
"dependencies": {
"es-errors": "^1.3.0",
"is-core-module": "^2.16.1",
"node-exports-info": "^1.6.0",
"object-keys": "^1.1.1",
"path-parse": "^1.0.7",
"supports-preserve-symlinks-flag": "^1.0.0"
},
"devDependencies": {
"@ljharb/eslint-config": "^22.1.3",
"array.prototype.map": "^1.0.8",
"copy-dir": "^1.3.0",
"eclint": "^2.8.1",
"eslint": "^9.39.1",
"in-publish": "^2.0.1",
"jiti": "^0.0.0",
"mkdirp": "^0.5.6",
"mv": "^2.1.1",
"npmignore": "^0.3.5",
"rimraf": "^2.7.1",
"safe-publish-latest": "^2.0.0",
"tap": "^0.4.13",
"tape": "^5.9.0",
"tmp": "^0.0.31"
},
"publishConfig": {
"ignore": [
".github/workflows",
".github/.well-known",
"appveyor.yml",
"CONTRIBUTING.md",
"test/resolver/malformed_package_json",
"test/list-exports"
]
},
"engines": {
"node": ">= 0.4"
}
}

View File

@@ -0,0 +1,322 @@
# resolve <sup>[![Version Badge][2]][1]</sup>
implements the [node `require.resolve()` algorithm](https://nodejs.org/api/modules.html#modules_all_together) such that you can `require.resolve()` on behalf of a file asynchronously and synchronously
[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]
[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/10759/badge)](https://bestpractices.coreinfrastructure.org/projects/10759)
[![npm badge][11]][1]
# example
asynchronously resolve:
```js
var resolve = require('resolve/async'); // or, require('resolve')
resolve('tap', { basedir: __dirname }, function (err, res) {
if (err) console.error(err);
else console.log(res);
});
```
```
$ node example/async.js
/home/substack/projects/node-resolve/node_modules/tap/lib/main.js
```
synchronously resolve:
```js
var resolve = require('resolve/sync'); // or, `require('resolve').sync
var res = resolve('tap', { basedir: __dirname });
console.log(res);
```
```
$ node example/sync.js
/home/substack/projects/node-resolve/node_modules/tap/lib/main.js
```
# methods
```js
var resolve = require('resolve');
var async = require('resolve/async');
var sync = require('resolve/sync');
```
For both the synchronous and asynchronous methods, errors may have any of the following `err.code` values:
- `MODULE_NOT_FOUND`: the given path string (`id`) could not be resolved to a module
- `INVALID_BASEDIR`: the specified `opts.basedir` doesn't exist, or is not a directory
- `INVALID_PACKAGE_MAIN`: a `package.json` was encountered with an invalid `main` property (eg. not a string)
- `ERR_PACKAGE_PATH_NOT_EXPORTED`: the requested subpath is not defined in the package's `exports` field
- `ERR_INVALID_PACKAGE_CONFIG`: the package's `exports` field contains an invalid target
- `INVALID_EXPORTS_CATEGORY`: an invalid `exportsCategory` was specified
## resolve(id, opts={}, cb)
Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`.
options are:
* opts.basedir - directory to begin resolving from
* opts.package - `package.json` data applicable to the module being loaded
* opts.extensions - array of file extensions to search in order
* opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search
* opts.readFile - how to read files asynchronously
* opts.isFile - function to asynchronously test whether a file exists
* opts.isDirectory - function to asynchronously test whether a file exists and is a directory
* opts.realpath - function to asynchronously resolve a potential symlink to its real path
* `opts.readPackage(readFile, pkgfile, cb)` - function to asynchronously read and parse a package.json file
* readFile - the passed `opts.readFile` or `fs.readFile` if not specified
* pkgfile - path to package.json
* cb - callback. a SyntaxError error argument will be ignored, all other error arguments will be treated as an error.
* `opts.packageFilter(pkg, pkgfile, dir)` - transform the parsed package.json contents before looking at the "main" field
* pkg - package data
* pkgfile - path to package.json
* dir - directory that contains package.json
* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
* pkg - package data
* path - the path being resolved
* relativePath - the path relative from the package.json location
* returns - a relative path that will be joined from the package.json location
* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
* request - the import specifier being resolved
* start - lookup path
* getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
* opts - the resolution options
* `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)
* request - the import specifier being resolved
* start - lookup path
* getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
* opts - the resolution options
* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
* opts.exportsCategory - a [node-exports-info](https://npmjs.com/package/node-exports-info) category string (e.g. `'conditions'`, `'patterns'`, `'pre-exports'`) that determines which `exports` field semantics to use.
When set, resolution will use the package's `exports` field according to that category's supported conditions and features.
This also enables **self-reference** support, allowing a package to import itself by name (e.g., `require('my-package')` from within `my-package`).
* opts.engines - determines `exports` field resolution based on Node.js version semantics:
* When a **string** (e.g. `'>= 14'`): treated as a semver range, mapped to the most restrictive [node-exports-info](https://npmjs.com/package/node-exports-info) category covering that range.
* When `true`: reads `engines.node` from the nearest `package.json` to `basedir` and uses the most restrictive category for that range.
* When `false` or omitted: no engine-based exports resolution.
* Throws if set to an empty string or non-boolean/non-string value.
**Note:** `exportsCategory` and `engines` are mutually exclusive - only one can be specified.
* opts.conditions - an array of condition strings (e.g. `['require', 'node']`) to use when resolving the `exports` field.
If specified, this overrides the conditions that would otherwise be derived from the category.
This option only has effect when `exportsCategory` or `engines` is also set.
default `opts` values:
```js
{
paths: [],
basedir: __dirname,
extensions: ['.js'],
includeCoreModules: true,
readFile: fs.readFile,
isFile: function isFile(file, cb) {
fs.stat(file, function (err, stat) {
if (!err) {
return cb(null, stat.isFile() || stat.isFIFO());
}
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
return cb(err);
});
},
isDirectory: function isDirectory(dir, cb) {
fs.stat(dir, function (err, stat) {
if (!err) {
return cb(null, stat.isDirectory());
}
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
return cb(err);
});
},
realpath: function realpath(file, cb) {
var realpath = typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath;
realpath(file, function (realPathErr, realPath) {
if (realPathErr && realPathErr.code !== 'ENOENT') cb(realPathErr);
else cb(null, realPathErr ? file : realPath);
});
},
readPackage: function defaultReadPackage(readFile, pkgfile, cb) {
readFile(pkgfile, function (readFileErr, body) {
if (readFileErr) cb(readFileErr);
else {
try {
var pkg = JSON.parse(body);
cb(null, pkg);
} catch (jsonErr) {
cb(jsonErr);
}
}
});
},
moduleDirectory: 'node_modules',
preserveSymlinks: false
}
```
## resolve.sync(id, opts)
Synchronously resolve the module path string `id`, returning the result and
throwing an error when `id` can't be resolved.
options are:
* opts.basedir - directory to begin resolving from
* opts.extensions - array of file extensions to search in order
* opts.includeCoreModules - set to `false` to exclude node core modules (e.g. `fs`) from the search
* opts.readFileSync - how to read files synchronously
* opts.isFile - function to synchronously test whether a file exists
* opts.isDirectory - function to synchronously test whether a file exists and is a directory
* opts.realpathSync - function to synchronously resolve a potential symlink to its real path
* `opts.readPackageSync(readFileSync, pkgfile)` - function to synchronously read and parse a package.json file. a thrown SyntaxError will be ignored, all other exceptions will propagate.
* readFileSync - the passed `opts.readFileSync` or `fs.readFileSync` if not specified
* pkgfile - path to package.json
* `opts.packageFilter(pkg, pkgfile, dir)` - transform the parsed package.json contents before looking at the "main" field
* pkg - package data
* pkgfile - path to package.json
* dir - directory that contains package.json
* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
* pkg - package data
* path - the path being resolved
* relativePath - the path relative from the package.json location
* returns - a relative path that will be joined from the package.json location
* opts.paths - require.paths array to use if nothing is found on the normal `node_modules` recursive walk (probably don't use this)
For advanced users, `paths` can also be a `opts.paths(request, start, opts)` function
* request - the import specifier being resolved
* start - lookup path
* getNodeModulesDirs - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
* opts - the resolution options
* `opts.packageIterator(request, start, opts)` - return the list of candidate paths where the packages sources may be found (probably don't use this)
* request - the import specifier being resolved
* start - lookup path
* getPackageCandidates - a thunk (no-argument function) that returns the paths using standard `node_modules` resolution
* opts - the resolution options
* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
* opts.preserveSymlinks - if true, doesn't resolve `basedir` to real path before resolving.
This is the way Node resolves dependencies when executed with the [--preserve-symlinks](https://nodejs.org/api/all.html#cli_preserve_symlinks) flag.
* opts.exportsCategory - a [node-exports-info](https://npmjs.com/package/node-exports-info) category string (e.g. `'conditions'`, `'patterns'`, `'pre-exports'`) that determines which `exports` field semantics to use. When set, resolution will use the package's `exports` field according to that category's supported conditions and features. This also enables **self-reference** support, allowing a package to import itself by name (e.g., `require('my-package')` from within `my-package`).
* opts.enginesRange - a semver range string (e.g. `'>= 14'`) that will be mapped to the most restrictive [node-exports-info](https://npmjs.com/package/node-exports-info) category covering that range.
* opts.engines - if `true`, reads `engines.node` from the nearest `package.json` to `basedir` and uses the most restrictive [node-exports-info](https://npmjs.com/package/node-exports-info) category for that range.
**Note:** `exportsCategory`, `enginesRange`, and `engines` are mutually exclusive - only one can be specified.
* opts.conditions - an array of condition strings (e.g. `['require', 'node']`) to use when resolving the `exports` field. If specified, this overrides the conditions that would otherwise be derived from the category. This option only has effect when one of `exportsCategory`, `enginesRange`, or `engines` is also set.
default `opts` values:
```js
{
paths: [],
basedir: __dirname,
extensions: ['.js'],
includeCoreModules: true,
readFileSync: fs.readFileSync,
isFile: function isFile(file) {
try {
var stat = fs.statSync(file);
} catch (e) {
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
throw e;
}
return stat.isFile() || stat.isFIFO();
},
isDirectory: function isDirectory(dir) {
try {
var stat = fs.statSync(dir);
} catch (e) {
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
throw e;
}
return stat.isDirectory();
},
realpathSync: function realpathSync(file) {
try {
var realpath = typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;
return realpath(file);
} catch (realPathErr) {
if (realPathErr.code !== 'ENOENT') {
throw realPathErr;
}
}
return file;
},
readPackageSync: function defaultReadPackageSync(readFileSync, pkgfile) {
return JSON.parse(readFileSync(pkgfile));
},
moduleDirectory: 'node_modules',
preserveSymlinks: false
}
```
# install
With [npm](https://npmjs.org) do:
```sh
npm install resolve
```
# license
MIT
[1]: https://npmjs.org/package/resolve
[2]: https://versionbadg.es/browserify/resolve.svg
[5]: https://david-dm.org/browserify/resolve.svg
[6]: https://david-dm.org/browserify/resolve
[7]: https://david-dm.org/browserify/resolve/dev-status.svg
[8]: https://david-dm.org/browserify/resolve#info=devDependencies
[11]: https://nodei.co/npm/resolve.png?downloads=true&stars=true
[license-image]: https://img.shields.io/npm/l/resolve.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/resolve.svg
[downloads-url]: https://npm-stat.com/charts.html?package=resolve
[codecov-image]: https://codecov.io/gh/browserify/resolve/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/browserify/resolve/
[actions-image]: https://img.shields.io/github/check-runs/browserify/resolve/main
[actions-url]: https://github.com/browserify/resolve/actions

View File

@@ -0,0 +1,3 @@
'use strict';
module.exports = require('./lib/sync');

View File

@@ -0,0 +1,29 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('dotdot', function (t) {
t.plan(4);
var dir = path.join(__dirname, '/dotdot/abc');
resolve('..', { basedir: dir }, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(__dirname, 'dotdot/index.js'));
});
resolve('.', { basedir: dir }, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, 'index.js'));
});
});
test('dotdot sync', function (t) {
t.plan(2);
var dir = path.join(__dirname, '/dotdot/abc');
var a = resolve.sync('..', { basedir: dir });
t.equal(a, path.join(__dirname, 'dotdot/index.js'));
var b = resolve.sync('.', { basedir: dir });
t.equal(b, path.join(dir, 'index.js'));
});

View File

@@ -0,0 +1,461 @@
'use strict';
var fs = require('fs');
var path = require('path');
var test = require('tape');
var resolve = require('../');
var fixturesDir = path.join(__dirname, 'list-exports', 'packages', 'tests', 'fixtures');
var categories = [
'broken',
'broken-dir-slash-conditions',
'conditions',
'experimental',
'pattern-trailers',
'pattern-trailers+json-imports',
'pattern-trailers-no-dir-slash',
'pattern-trailers-no-dir-slash+json-imports',
'patterns',
'require-esm',
'strips-types',
'subpath-imports-slash'
// 'pre-exports' is tested separately since it uses main/index resolution
];
// Fixtures that are symlinks pointing outside the fixture dir cause path confusion
// ex-private is a private package whose expected files don't include exports data
var skipFixtures = ['list-exports', 'ls-exports', 'ex-private'];
function getFixtures() {
return fs.readdirSync(fixturesDir).filter(function (name) {
if (skipFixtures.indexOf(name) !== -1) {
return false;
}
var stat = fs.statSync(path.join(fixturesDir, name));
return stat.isDirectory();
});
}
function loadExpected(fixtureName, category) {
var expectedPath = path.join(fixturesDir, fixtureName, 'expected', category + '.json');
if (!fs.existsSync(expectedPath)) {
return null;
}
try {
return JSON.parse(fs.readFileSync(expectedPath, 'utf8'));
} catch (e) {
return null;
}
}
function loadProjectPkg(fixtureName) {
var pkgPath = path.join(fixturesDir, fixtureName, 'project', 'package.json');
try {
return JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
} catch (e) {
return null;
}
}
test('async exports resolution - exportsCategory option', function (t) {
var fixtures = getFixtures();
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
return;
}
var projectDir = path.join(fixturesDir, fixtureName, 'project');
var pkgName = projectPkg.name;
categories.forEach(function (category) {
var expected = loadExpected(fixtureName, category);
if (!expected || !expected.exports || !expected.exports[category]) {
return;
}
var requireMap = expected.exports[category].require;
if (!requireMap || typeof requireMap !== 'object') {
return;
}
Object.keys(requireMap).forEach(function (subpath) {
var expectedFile = requireMap[subpath];
var specifier = subpath === '.' ? pkgName : pkgName + subpath.substring(1);
t.test(fixtureName + ' / ' + category + ' / ' + subpath, function (st) {
st.plan(1);
resolve(specifier, {
basedir: __dirname,
exportsCategory: category,
extensions: ['.js', '.json'],
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail('Unexpected error for ' + specifier + ': ' + err.message);
return;
}
var relativeResult = './' + path.relative(projectDir, result).split(path.sep).join('/');
st.equal(relativeResult, expectedFile, specifier + ' resolves to ' + expectedFile);
});
});
});
});
});
t.end();
});
test('async exports resolution - pre-exports category uses main/index', function (t) {
var fixtures = getFixtures();
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
return;
}
var projectDir = path.join(fixturesDir, fixtureName, 'project');
var pkgName = projectPkg.name;
var expected = loadExpected(fixtureName, 'pre-exports');
if (!expected || !expected.exports || !expected.exports['pre-exports']) {
return;
}
var requireMap = expected.exports['pre-exports'].require;
if (!requireMap || typeof requireMap !== 'object') {
return;
}
// For pre-exports, only test the main entry point (.)
var mainEntry = requireMap['.'];
if (!mainEntry) {
return;
}
t.test(fixtureName + ' / pre-exports / .', function (st) {
st.plan(1);
resolve(pkgName, {
basedir: __dirname,
exportsCategory: 'pre-exports',
extensions: ['.js', '.json'],
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail('Unexpected error for ' + pkgName + ': ' + err.message);
return;
}
var relativeResult = './' + path.relative(projectDir, result).split(path.sep).join('/');
st.equal(relativeResult, mainEntry, pkgName + ' resolves to ' + mainEntry);
});
});
});
t.end();
});
test('async exports resolution - mutual exclusivity of options', function (t) {
t.test('exportsCategory and engines (string) are mutually exclusive', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
exportsCategory: 'conditions',
engines: '>= 14'
}, function (err) {
st.ok(err && (/mutually exclusive/).test(err.message), 'throws with mutually exclusive message');
});
});
t.test('exportsCategory and engines (true) are mutually exclusive', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
exportsCategory: 'conditions',
engines: true
}, function (err) {
st.ok(err && (/mutually exclusive/).test(err.message), 'throws with mutually exclusive message');
});
});
t.end();
});
test('async exports resolution - invalid category', function (t) {
t.plan(2);
resolve('tape', {
basedir: __dirname,
exportsCategory: 'not-a-real-category'
}, function (err) {
t.equal(err && err.code, 'INVALID_EXPORTS_CATEGORY', 'has correct error code');
t.ok(err && (/Invalid exports category/).test(err.message), 'has correct error message');
});
});
test('async exports resolution - engines option', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.test('engines string maps to category', function (st) {
st.plan(1);
resolve('ex-exports-string', {
basedir: __dirname,
engines: '>= 14',
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('index.js') > -1, 'resolves to index.js');
});
});
t.test('engines: false is same as omitting', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
engines: false
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('tape') > -1, 'resolves without exports resolution');
});
});
t.test('engines: empty string throws', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
engines: ''
}, function (err) {
st.ok(err && (/must be.*true.*false.*non-empty string/i).test(err.message), 'throws with correct message');
});
});
t.test('engines: number throws', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
engines: 14
}, function (err) {
st.ok(err && (/must be.*true.*false.*non-empty string/i).test(err.message), 'throws with correct message');
});
});
t.test('engines: object throws', function (st) {
st.plan(1);
resolve('tape', {
basedir: __dirname,
engines: { node: '>= 14' }
}, function (err) {
st.ok(err && (/must be.*true.*false.*non-empty string/i).test(err.message), 'throws with correct message');
});
});
t.end();
});
test('async exports resolution - conditions override', function (t) {
var projectDir = path.join(fixturesDir, 'ex-conditions', 'project');
t.test('default category conditions resolve to require.js', function (st) {
st.plan(1);
resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('require.js') > -1, 'resolves to require.js with default conditions');
});
});
t.test('conditions override to [default] resolves to default.js', function (st) {
st.plan(1);
resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
conditions: ['default'],
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('default.js') > -1, 'resolves to default.js with conditions override');
});
});
t.test('conditions override to [node] resolves to node.js', function (st) {
st.plan(1);
resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
conditions: ['node'],
packageIterator: function () {
return [projectDir];
}
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('node.js') > -1, 'resolves to node.js with conditions override');
});
});
t.end();
});
test('async exports resolution - subpath not exported throws', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.plan(2);
resolve('ex-exports-string/not-exported', {
basedir: __dirname,
exportsCategory: 'conditions',
packageIterator: function () {
return [projectDir];
}
}, function (err) {
t.equal(err && err.code, 'ERR_PACKAGE_PATH_NOT_EXPORTED', 'has correct error code');
t.ok(err && (/not defined by "exports"/).test(err.message), 'has correct error message');
});
});
test('async existing resolution without exports options still works', function (t) {
t.plan(1);
resolve('tape', { basedir: __dirname }, function (err, result) {
if (err) {
t.fail(err.message);
return;
}
t.ok(result.indexOf('tape') > -1, 'resolves tape without exports options');
});
});
test('all fixtures are tested', function (t) {
var fixtures = getFixtures();
var testedFixtures = [];
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
t.fail('Fixture ' + fixtureName + ' has no loadable package.json');
return;
}
var hasAnyTests = false;
// Check if at least one category has expected results
categories.forEach(function (category) {
var expected = loadExpected(fixtureName, category);
if (expected && expected.exports && expected.exports[category]) {
var requireMap = expected.exports[category].require;
if (requireMap && typeof requireMap === 'object' && Object.keys(requireMap).length > 0) {
hasAnyTests = true;
}
}
});
// Also check pre-exports
var preExpected = loadExpected(fixtureName, 'pre-exports');
if (preExpected && preExpected.exports && preExpected.exports['pre-exports']) {
var preRequireMap = preExpected.exports['pre-exports'].require;
if (preRequireMap && preRequireMap['.']) {
hasAnyTests = true;
}
}
if (hasAnyTests) {
testedFixtures.push(fixtureName);
} else {
t.fail('Fixture ' + fixtureName + ' has no testable entrypoints');
}
});
t.ok(testedFixtures.length > 0, 'At least one fixture is tested');
t.equal(testedFixtures.length, fixtures.length, 'All ' + fixtures.length + ' fixtures have testable entrypoints');
t.end();
});
test('async exports resolution - self-reference', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.test('self-reference resolves via exports when inside package', function (st) {
st.plan(1);
// basedir is inside the package, specifier is the package name
resolve('ex-exports-string', {
basedir: projectDir,
exportsCategory: 'conditions'
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('index.js') > -1, 'self-reference resolves to index.js via exports');
});
});
t.test('self-reference with subpath resolves via exports', function (st) {
var conditionsDir = path.join(fixturesDir, 'ex-conditions', 'project');
st.plan(1);
resolve('ex-conditions/rdni', {
basedir: conditionsDir,
exportsCategory: 'conditions'
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
st.ok(result.indexOf('require.js') > -1, 'self-reference subpath resolves correctly');
});
});
t.test('self-reference without exports falls back to main', function (st) {
var mainDotlessDir = path.join(fixturesDir, 'ex-main-dotless', 'project');
st.plan(1);
resolve('ex-main-dotless', {
basedir: mainDotlessDir,
exportsCategory: 'conditions'
}, function (err, result) {
// If it throws, that's also acceptable behavior (no exports means not exported)
st.ok(!err || result, 'self-reference without exports throws or resolves');
});
});
t.test('self-reference does not cross node_modules boundary', function (st) {
// basedir is inside node_modules, should NOT self-reference parent package
var nodeModulesDir = path.join(__dirname, '..', 'node_modules', 'tape');
st.plan(1);
resolve('resolve', {
basedir: nodeModulesDir,
exportsCategory: 'conditions'
}, function (err, result) {
if (err) {
st.fail(err.message);
return;
}
// The result should be from node_modules, not from a self-reference
st.ok(result.indexOf('node_modules') > -1 || result === 'resolve', 'does not self-reference across node_modules');
});
});
t.end();
});

View File

@@ -0,0 +1,442 @@
'use strict';
var fs = require('fs');
var path = require('path');
var test = require('tape');
var resolve = require('../sync');
var fixturesDir = path.join(__dirname, 'list-exports', 'packages', 'tests', 'fixtures');
var categories = [
'broken',
'broken-dir-slash-conditions',
'conditions',
'experimental',
'pattern-trailers',
'pattern-trailers+json-imports',
'pattern-trailers-no-dir-slash',
'pattern-trailers-no-dir-slash+json-imports',
'patterns',
'require-esm',
'strips-types',
'subpath-imports-slash'
// 'pre-exports' is tested separately since it uses main/index resolution
];
// Fixtures that are symlinks pointing outside the fixture dir cause path confusion
// ex-private is a private package whose expected files don't include exports data
var skipFixtures = ['list-exports', 'ls-exports', 'ex-private'];
function getFixtures() {
return fs.readdirSync(fixturesDir).filter(function (name) {
if (skipFixtures.indexOf(name) !== -1) {
return false;
}
var stat = fs.statSync(path.join(fixturesDir, name));
return stat.isDirectory();
});
}
function loadExpected(fixtureName, category) {
var expectedPath = path.join(fixturesDir, fixtureName, 'expected', category + '.json');
if (!fs.existsSync(expectedPath)) {
return null;
}
try {
return JSON.parse(fs.readFileSync(expectedPath, 'utf8'));
} catch (e) {
return null;
}
}
function loadProjectPkg(fixtureName) {
var pkgPath = path.join(fixturesDir, fixtureName, 'project', 'package.json');
try {
return JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
} catch (e) {
return null;
}
}
test('exports resolution - exportsCategory option', function (t) {
var fixtures = getFixtures();
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
return;
}
var projectDir = path.join(fixturesDir, fixtureName, 'project');
var pkgName = projectPkg.name;
categories.forEach(function (category) {
var expected = loadExpected(fixtureName, category);
if (!expected || !expected.exports || !expected.exports[category]) {
return;
}
var requireMap = expected.exports[category].require;
if (!requireMap || typeof requireMap !== 'object') {
return;
}
Object.keys(requireMap).forEach(function (subpath) {
var expectedFile = requireMap[subpath];
var specifier = subpath === '.' ? pkgName : pkgName + subpath.substring(1);
t.test(fixtureName + ' / ' + category + ' / ' + subpath, function (st) {
st.plan(1);
try {
var result = resolve(specifier, {
basedir: __dirname,
exportsCategory: category,
extensions: ['.js', '.json'],
packageIterator: function () {
return [projectDir];
}
});
var relativeResult = './' + path.relative(projectDir, result).split(path.sep).join('/');
st.equal(relativeResult, expectedFile, specifier + ' resolves to ' + expectedFile);
} catch (e) {
st.fail('Unexpected error for ' + specifier + ': ' + e.message);
}
});
});
});
});
t.end();
});
test('exports resolution - pre-exports category uses main/index', function (t) {
var fixtures = getFixtures();
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
return;
}
var projectDir = path.join(fixturesDir, fixtureName, 'project');
var pkgName = projectPkg.name;
var expected = loadExpected(fixtureName, 'pre-exports');
if (!expected || !expected.exports || !expected.exports['pre-exports']) {
return;
}
var requireMap = expected.exports['pre-exports'].require;
if (!requireMap || typeof requireMap !== 'object') {
return;
}
// For pre-exports, only test the main entry point (.)
var mainEntry = requireMap['.'];
if (!mainEntry) {
return;
}
t.test(fixtureName + ' / pre-exports / .', function (st) {
st.plan(1);
try {
var result = resolve(pkgName, {
basedir: __dirname,
exportsCategory: 'pre-exports',
extensions: ['.js', '.json'],
packageIterator: function () {
return [projectDir];
}
});
var relativeResult = './' + path.relative(projectDir, result).split(path.sep).join('/');
st.equal(relativeResult, mainEntry, pkgName + ' resolves to ' + mainEntry);
} catch (e) {
st.fail('Unexpected error for ' + pkgName + ': ' + e.message);
}
});
});
t.end();
});
test('exports resolution - mutual exclusivity of options', function (t) {
t.test('exportsCategory and engines (string) are mutually exclusive', function (st) {
st.plan(1);
try {
resolve('tape', {
basedir: __dirname,
exportsCategory: 'conditions',
engines: '>= 14'
});
st.fail('should have thrown');
} catch (e) {
st.ok((/mutually exclusive/).test(e.message), 'throws with mutually exclusive message');
}
});
t.test('exportsCategory and engines (true) are mutually exclusive', function (st) {
st.plan(1);
try {
resolve('tape', {
basedir: __dirname,
exportsCategory: 'conditions',
engines: true
});
st.fail('should have thrown');
} catch (e) {
st.ok((/mutually exclusive/).test(e.message), 'throws with mutually exclusive message');
}
});
t.end();
});
test('exports resolution - invalid category', function (t) {
t.plan(2);
try {
resolve('tape', {
basedir: __dirname,
exportsCategory: 'not-a-real-category'
});
t.fail('should have thrown');
} catch (e) {
t.equal(e.code, 'INVALID_EXPORTS_CATEGORY', 'has correct error code');
t.ok((/Invalid exports category/).test(e.message), 'has correct error message');
}
});
test('exports resolution - engines option', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.test('engines string maps to category', function (st) {
st.plan(1);
var result = resolve('ex-exports-string', {
basedir: __dirname,
engines: '>= 14',
packageIterator: function () {
return [projectDir];
}
});
st.ok(result.indexOf('index.js') > -1, 'resolves to index.js');
});
t.test('engines: false is same as omitting', function (st) {
st.plan(1);
var result = resolve('tape', {
basedir: __dirname,
engines: false
});
st.ok(result.indexOf('tape') > -1, 'resolves without exports resolution');
});
t.test('engines: empty string throws', function (st) {
st.plan(1);
try {
resolve('tape', {
basedir: __dirname,
engines: ''
});
st.fail('should have thrown');
} catch (e) {
st.ok((/must be.*true.*false.*non-empty string/i).test(e.message), 'throws with correct message');
}
});
t.test('engines: number throws', function (st) {
st.plan(1);
try {
resolve('tape', {
basedir: __dirname,
engines: 14
});
st.fail('should have thrown');
} catch (e) {
st.ok((/must be.*true.*false.*non-empty string/i).test(e.message), 'throws with correct message');
}
});
t.test('engines: object throws', function (st) {
st.plan(1);
try {
resolve('tape', {
basedir: __dirname,
engines: { node: '>= 14' }
});
st.fail('should have thrown');
} catch (e) {
st.ok((/must be.*true.*false.*non-empty string/i).test(e.message), 'throws with correct message');
}
});
t.end();
});
test('exports resolution - conditions override', function (t) {
var projectDir = path.join(fixturesDir, 'ex-conditions', 'project');
t.test('default category conditions resolve to require.js', function (st) {
st.plan(1);
var result = resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
packageIterator: function () {
return [projectDir];
}
});
st.ok(result.indexOf('require.js') > -1, 'resolves to require.js with default conditions');
});
t.test('conditions override to [default] resolves to default.js', function (st) {
st.plan(1);
var result = resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
conditions: ['default'],
packageIterator: function () {
return [projectDir];
}
});
st.ok(result.indexOf('default.js') > -1, 'resolves to default.js with conditions override');
});
t.test('conditions override to [node] resolves to node.js', function (st) {
st.plan(1);
var result = resolve('ex-conditions/rdni', {
basedir: __dirname,
exportsCategory: 'conditions',
conditions: ['node'],
packageIterator: function () {
return [projectDir];
}
});
st.ok(result.indexOf('node.js') > -1, 'resolves to node.js with conditions override');
});
t.end();
});
test('exports resolution - subpath not exported throws', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.plan(2);
try {
resolve('ex-exports-string/not-exported', {
basedir: __dirname,
exportsCategory: 'conditions',
packageIterator: function () {
return [projectDir];
}
});
t.fail('should have thrown');
} catch (e) {
t.equal(e.code, 'ERR_PACKAGE_PATH_NOT_EXPORTED', 'has correct error code');
t.ok((/not defined by "exports"/).test(e.message), 'has correct error message');
}
});
test('existing resolution without exports options still works', function (t) {
t.plan(1);
var result = resolve('tape', { basedir: __dirname });
t.ok(result.indexOf('tape') > -1, 'resolves tape without exports options');
});
test('all fixtures are tested', function (t) {
var fixtures = getFixtures();
var testedFixtures = [];
fixtures.forEach(function (fixtureName) {
var projectPkg = loadProjectPkg(fixtureName);
if (!projectPkg) {
t.fail('Fixture ' + fixtureName + ' has no loadable package.json');
return;
}
var hasAnyTests = false;
// Check if at least one category has expected results
categories.forEach(function (category) {
var expected = loadExpected(fixtureName, category);
if (expected && expected.exports && expected.exports[category]) {
var requireMap = expected.exports[category].require;
if (requireMap && typeof requireMap === 'object' && Object.keys(requireMap).length > 0) {
hasAnyTests = true;
}
}
});
// Also check pre-exports
var preExpected = loadExpected(fixtureName, 'pre-exports');
if (preExpected && preExpected.exports && preExpected.exports['pre-exports']) {
var preRequireMap = preExpected.exports['pre-exports'].require;
if (preRequireMap && preRequireMap['.']) {
hasAnyTests = true;
}
}
if (hasAnyTests) {
testedFixtures.push(fixtureName);
} else {
t.fail('Fixture ' + fixtureName + ' has no testable entrypoints');
}
});
t.ok(testedFixtures.length > 0, 'At least one fixture is tested');
t.equal(testedFixtures.length, fixtures.length, 'All ' + fixtures.length + ' fixtures have testable entrypoints');
t.end();
});
test('exports resolution - self-reference', function (t) {
var projectDir = path.join(fixturesDir, 'ex-exports-string', 'project');
t.test('self-reference resolves via exports when inside package', function (st) {
st.plan(1);
// basedir is inside the package, specifier is the package name
var result = resolve('ex-exports-string', {
basedir: projectDir,
exportsCategory: 'conditions'
});
st.ok(result.indexOf('index.js') > -1, 'self-reference resolves to index.js via exports');
});
t.test('self-reference with subpath resolves via exports', function (st) {
var conditionsDir = path.join(fixturesDir, 'ex-conditions', 'project');
st.plan(1);
var result = resolve('ex-conditions/rdni', {
basedir: conditionsDir,
exportsCategory: 'conditions'
});
st.ok(result.indexOf('require.js') > -1, 'self-reference subpath resolves correctly');
});
t.test('self-reference without exports falls back to main', function (st) {
// Create a scenario where there's no exports field
var mainDotlessDir = path.join(fixturesDir, 'ex-main-dotless', 'project');
st.plan(1);
try {
var result = resolve('ex-main-dotless', {
basedir: mainDotlessDir,
exportsCategory: 'conditions'
});
st.ok(result.indexOf('main.js') > -1 || result.indexOf('index.js') > -1, 'self-reference without exports uses main/index');
} catch (e) {
// If it throws, that's also acceptable behavior (no exports means not exported)
st.ok(true, 'self-reference without exports throws or resolves');
}
});
t.test('self-reference does not cross node_modules boundary', function (st) {
// basedir is inside node_modules, should NOT self-reference parent package
var nodeModulesDir = path.join(__dirname, '..', 'node_modules', 'tape');
st.plan(1);
// Trying to resolve 'resolve' from inside node_modules/tape should NOT
// self-reference the root resolve package - it should use normal resolution
var result = resolve('resolve', {
basedir: nodeModulesDir,
exportsCategory: 'conditions'
});
// The result should be from node_modules, not from a self-reference
// (self-reference would give us the current working directory's resolve)
st.ok(result.indexOf('node_modules') > -1 || result === 'resolve', 'does not self-reference across node_modules');
});
t.end();
});

View File

@@ -0,0 +1,29 @@
var test = require('tape');
var path = require('path');
var resolve = require('../');
test('faulty basedir must produce error in windows', { skip: process.platform !== 'win32' }, function (t) {
t.plan(1);
var resolverDir = 'C:\\a\\b\\c\\d';
resolve('tape/lib/test.js', { basedir: resolverDir }, function (err, res, pkg) {
t.equal(!!err, true);
});
});
test('non-existent basedir should not throw when preserveSymlinks is false', function (t) {
t.plan(2);
var opts = {
basedir: path.join(path.sep, 'unreal', 'path', 'that', 'does', 'not', 'exist'),
preserveSymlinks: false
};
var module = './dotdot/abc';
resolve(module, opts, function (err, res) {
t.equal(err.code, 'INVALID_BASEDIR');
t.equal(res, undefined);
});
});

View File

@@ -0,0 +1,37 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('filter', function (t) {
t.plan(5);
var dir = path.join(__dirname, 'resolver');
var packageFilterArgs;
resolve('./baz', {
basedir: dir,
packageFilter: function (pkg, pkgfile, dir) {
pkg.main = 'doom'; // eslint-disable-line no-param-reassign
packageFilterArgs = [pkg, pkgfile, dir];
return pkg;
}
}, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'baz/doom.js'), 'changing the package "main" works');
var packageData = packageFilterArgs[0];
t.equal(pkg, packageData, 'first packageFilter argument is "pkg"');
t.equal(packageData.main, 'doom', 'package "main" was altered');
var packageFile = packageFilterArgs[1];
t.equal(
packageFile,
path.join(dir, 'baz/package.json'),
'second packageFilter argument is "pkgfile"'
);
var packageFileDir = packageFilterArgs[2];
t.equal(packageFileDir, path.join(dir, 'baz'), 'third packageFilter argument is "dir"');
t.end();
});
});

View File

@@ -0,0 +1,33 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('filter', function (t) {
var dir = path.join(__dirname, 'resolver');
var packageFilterArgs;
var res = resolve.sync('./baz', {
basedir: dir,
packageFilter: function (pkg, pkgfile, dir) {
pkg.main = 'doom'; // eslint-disable-line no-param-reassign
packageFilterArgs = [pkg, pkgfile, dir];
return pkg;
}
});
t.equal(res, path.join(dir, 'baz/doom.js'), 'changing the package "main" works');
var packageData = packageFilterArgs[0];
t.equal(packageData.main, 'doom', 'package "main" was altered');
var packageFile = packageFilterArgs[1];
t.equal(
packageFile,
path.join(dir, 'baz/package.json'),
'second packageFilter argument is "pkgfile"'
);
var packageDir = packageFilterArgs[2];
t.equal(packageDir, path.join(dir, 'baz'), 'third packageFilter argument is "dir"');
t.end();
});

View File

@@ -0,0 +1,127 @@
'use strict';
var fs = require('fs');
var homedir = require('../lib/homedir');
var path = require('path');
var test = require('tape');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var mv = require('mv');
var copyDir = require('copy-dir');
var tmp = require('tmp');
var HOME = homedir();
var hnm = path.join(HOME, '.node_modules');
var hnl = path.join(HOME, '.node_libraries');
var resolve = require('../async');
function makeDir(t, dir, cb) {
mkdirp(dir, function (err) {
if (err) {
cb(err);
} else {
t.teardown(function cleanup() {
rimraf.sync(dir);
});
cb();
}
});
}
function makeTempDir(t, dir, cb) {
if (fs.existsSync(dir)) {
var tmpResult = tmp.dirSync();
t.teardown(tmpResult.removeCallback);
var backup = path.join(tmpResult.name, path.basename(dir));
mv(dir, backup, function (err) {
if (err) {
cb(err);
} else {
t.teardown(function () {
mv(backup, dir, cb);
});
makeDir(t, dir, cb);
}
});
} else {
makeDir(t, dir, cb);
}
}
test('homedir module paths', function (t) {
t.plan(7);
makeTempDir(t, hnm, function (err) {
t.error(err, 'no error with HNM temp dir');
if (err) {
return t.end();
}
var bazHNMDir = path.join(hnm, 'baz');
var dotMainDir = path.join(hnm, 'dot_main');
copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNMDir);
copyDir.sync(path.join(__dirname, 'resolver/dot_main'), dotMainDir);
var bazPkg = { name: 'baz', main: 'quux.js' };
var dotMainPkg = { main: 'index' };
var bazHNMmain = path.join(bazHNMDir, 'quux.js');
t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
var dotMainMain = path.join(dotMainDir, 'index.js');
t.equal(require.resolve('dot_main'), dotMainMain, 'sanity check: require.resolve finds `dot_main`');
makeTempDir(t, hnl, function (err) {
t.error(err, 'no error with HNL temp dir');
if (err) {
return t.end();
}
var bazHNLDir = path.join(hnl, 'baz');
copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNLDir);
var dotSlashMainDir = path.join(hnl, 'dot_slash_main');
var dotSlashMainMain = path.join(dotSlashMainDir, 'index.js');
var dotSlashMainPkg = { main: 'index' };
copyDir.sync(path.join(__dirname, 'resolver/dot_slash_main'), dotSlashMainDir);
t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
t.equal(require.resolve('dot_slash_main'), dotSlashMainMain, 'sanity check: require.resolve finds HNL `dot_slash_main`');
t.test('with temp dirs', function (st) {
st.plan(3);
st.test('just in `$HOME/.node_modules`', function (s2t) {
s2t.plan(3);
resolve('dot_main', function (err, res, pkg) {
s2t.error(err, 'no error resolving `dot_main`');
s2t.equal(res, dotMainMain, '`dot_main` resolves in `$HOME/.node_modules`');
s2t.deepEqual(pkg, dotMainPkg);
});
});
st.test('just in `$HOME/.node_libraries`', function (s2t) {
s2t.plan(3);
resolve('dot_slash_main', function (err, res, pkg) {
s2t.error(err, 'no error resolving `dot_slash_main`');
s2t.equal(res, dotSlashMainMain, '`dot_slash_main` resolves in `$HOME/.node_libraries`');
s2t.deepEqual(pkg, dotSlashMainPkg);
});
});
st.test('in `$HOME/.node_libraries` and `$HOME/.node_modules`', function (s2t) {
s2t.plan(3);
resolve('baz', function (err, res, pkg) {
s2t.error(err, 'no error resolving `baz`');
s2t.equal(res, bazHNMmain, '`baz` resolves in `$HOME/.node_modules` when in both');
s2t.deepEqual(pkg, bazPkg);
});
});
});
});
});
});

View File

@@ -0,0 +1,114 @@
'use strict';
var fs = require('fs');
var homedir = require('../lib/homedir');
var path = require('path');
var test = require('tape');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var mv = require('mv');
var copyDir = require('copy-dir');
var tmp = require('tmp');
var HOME = homedir();
var hnm = path.join(HOME, '.node_modules');
var hnl = path.join(HOME, '.node_libraries');
var resolve = require('../sync');
function makeDir(t, dir, cb) {
mkdirp(dir, function (err) {
if (err) {
cb(err);
} else {
t.teardown(function cleanup() {
rimraf.sync(dir);
});
cb();
}
});
}
function makeTempDir(t, dir, cb) {
if (fs.existsSync(dir)) {
var tmpResult = tmp.dirSync();
t.teardown(tmpResult.removeCallback);
var backup = path.join(tmpResult.name, path.basename(dir));
mv(dir, backup, function (err) {
if (err) {
cb(err);
} else {
t.teardown(function () {
mv(backup, dir, cb);
});
makeDir(t, dir, cb);
}
});
} else {
makeDir(t, dir, cb);
}
}
test('homedir module paths', function (t) {
t.plan(7);
makeTempDir(t, hnm, function (err) {
t.error(err, 'no error with HNM temp dir');
if (err) {
return t.end();
}
var bazHNMDir = path.join(hnm, 'baz');
var dotMainDir = path.join(hnm, 'dot_main');
copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNMDir);
copyDir.sync(path.join(__dirname, 'resolver/dot_main'), dotMainDir);
var bazHNMmain = path.join(bazHNMDir, 'quux.js');
t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
var dotMainMain = path.join(dotMainDir, 'index.js');
t.equal(require.resolve('dot_main'), dotMainMain, 'sanity check: require.resolve finds `dot_main`');
makeTempDir(t, hnl, function (err) {
t.error(err, 'no error with HNL temp dir');
if (err) {
return t.end();
}
var bazHNLDir = path.join(hnl, 'baz');
copyDir.sync(path.join(__dirname, 'resolver/baz'), bazHNLDir);
var dotSlashMainDir = path.join(hnl, 'dot_slash_main');
var dotSlashMainMain = path.join(dotSlashMainDir, 'index.js');
copyDir.sync(path.join(__dirname, 'resolver/dot_slash_main'), dotSlashMainDir);
t.equal(require.resolve('baz'), bazHNMmain, 'sanity check: require.resolve finds HNM `baz`');
t.equal(require.resolve('dot_slash_main'), dotSlashMainMain, 'sanity check: require.resolve finds HNL `dot_slash_main`');
t.test('with temp dirs', function (st) {
st.plan(3);
st.test('just in `$HOME/.node_modules`', function (s2t) {
s2t.plan(1);
var res = resolve('dot_main');
s2t.equal(res, dotMainMain, '`dot_main` resolves in `$HOME/.node_modules`');
});
st.test('just in `$HOME/.node_libraries`', function (s2t) {
s2t.plan(1);
var res = resolve('dot_slash_main');
s2t.equal(res, dotSlashMainMain, '`dot_slash_main` resolves in `$HOME/.node_libraries`');
});
st.test('in `$HOME/.node_libraries` and `$HOME/.node_modules`', function (s2t) {
s2t.plan(1);
var res = resolve('baz');
s2t.equal(res, bazHNMmain, '`baz` resolves in `$HOME/.node_modules` when in both');
});
});
});
});
});

View File

@@ -0,0 +1,315 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('mock', function (t) {
t.plan(8);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
var dirs = {};
dirs[path.resolve('/foo/bar')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
},
realpath: function (file, cb) {
cb(null, file);
}
};
}
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
t.equal(pkg, undefined);
});
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
t.equal(pkg, undefined);
});
resolve('baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
resolve('../baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
});
test('mock from package', function (t) {
t.plan(8);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
var dirs = {};
dirs[path.resolve('/foo/bar')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, file));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
'package': { main: 'bar' },
readFile: function (file, cb) {
cb(null, files[file]);
},
realpath: function (file, cb) {
cb(null, file);
}
};
}
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
t.equal(pkg && pkg.main, 'bar');
});
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
t.equal(pkg && pkg.main, 'bar');
});
resolve('baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
resolve('../baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
});
test('mock package', function (t) {
t.plan(2);
var files = {};
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
var dirs = {};
dirs[path.resolve('/foo')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
},
realpath: function (file, cb) {
cb(null, file);
}
};
}
resolve('bar', opts('/foo'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
t.equal(pkg && pkg.main, './baz.js');
});
});
test('mock package from package', function (t) {
t.plan(2);
var files = {};
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
var dirs = {};
dirs[path.resolve('/foo')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
'package': { main: 'bar' },
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
},
realpath: function (file, cb) {
cb(null, file);
}
};
}
resolve('bar', opts('/foo'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
t.equal(pkg && pkg.main, './baz.js');
});
});
test('symlinked', function (t) {
t.plan(4);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
var dirs = {};
dirs[path.resolve('/foo/bar')] = true;
dirs[path.resolve('/foo/bar/symlinked')] = true;
function opts(basedir) {
return {
preserveSymlinks: false,
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
},
realpath: function (file, cb) {
var resolved = path.resolve(file);
if (resolved.indexOf('symlinked') >= 0) {
cb(null, resolved);
return;
}
var ext = path.extname(resolved);
if (ext) {
var dir = path.dirname(resolved);
var base = path.basename(resolved);
cb(null, path.join(dir, 'symlinked', base));
} else {
cb(null, path.join(resolved, 'symlinked'));
}
}
};
}
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/symlinked/baz.js'));
t.equal(pkg, undefined);
});
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/symlinked/baz.js'));
t.equal(pkg, undefined);
});
});
test('readPackage', function (t) {
t.plan(3);
var files = {};
files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
var dirs = {};
dirs[path.resolve('/foo')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
},
isDirectory: function (dir, cb) {
cb(null, !!dirs[path.resolve(dir)]);
},
'package': { main: 'bar' },
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
},
realpath: function (file, cb) {
cb(null, file);
}
};
}
t.test('with readFile', function (st) {
st.plan(3);
resolve('bar', opts('/foo'), function (err, res, pkg) {
st.error(err);
st.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
st.equal(pkg && pkg.main, './baz.js');
});
});
function readPackage(readFile, file, cb) {
var barPackage = path.join('bar', 'package.json');
if (file.slice(-barPackage.length) === barPackage) {
cb(null, { main: './something-else.js' });
} else {
cb(null, JSON.parse(files[path.resolve(file)]));
}
}
t.test('with readPackage', function (st) {
st.plan(3);
var options = opts('/foo');
delete options.readFile;
options.readPackage = readPackage;
resolve('bar', options, function (err, res, pkg) {
st.error(err);
st.equal(res, path.resolve('/foo/node_modules/bar/something-else.js'));
st.equal(pkg && pkg.main, './something-else.js');
});
});
t.test('with readFile and readPackage', function (st) {
st.plan(1);
var options = opts('/foo');
options.readPackage = readPackage;
resolve('bar', options, function (err) {
st.throws(function () { throw err; }, TypeError, 'errors when both readFile and readPackage are provided');
});
});
});

View File

@@ -0,0 +1,215 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('mock', function (t) {
t.plan(4);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
var dirs = {};
dirs[path.resolve('/foo/bar')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file) {
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
},
isDirectory: function (dir) {
return !!dirs[path.resolve(dir)];
},
readFileSync: function (file) {
return files[path.resolve(file)];
},
realpathSync: function (file) {
return file;
}
};
}
t.equal(
resolve.sync('./baz', opts('/foo/bar')),
path.resolve('/foo/bar/baz.js')
);
t.equal(
resolve.sync('./baz.js', opts('/foo/bar')),
path.resolve('/foo/bar/baz.js')
);
t.throws(function () {
resolve.sync('baz', opts('/foo/bar'));
});
t.throws(function () {
resolve.sync('../baz', opts('/foo/bar'));
});
});
test('mock package', function (t) {
t.plan(1);
var files = {};
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
var dirs = {};
dirs[path.resolve('/foo')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir) {
return {
basedir: path.resolve(basedir),
isFile: function (file) {
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
},
isDirectory: function (dir) {
return !!dirs[path.resolve(dir)];
},
readFileSync: function (file) {
return files[path.resolve(file)];
},
realpathSync: function (file) {
return file;
}
};
}
t.equal(
resolve.sync('bar', opts('/foo')),
path.resolve('/foo/node_modules/bar/baz.js')
);
});
test('symlinked', function (t) {
t.plan(2);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
var dirs = {};
dirs[path.resolve('/foo/bar')] = true;
dirs[path.resolve('/foo/bar/symlinked')] = true;
function opts(basedir) {
return {
preserveSymlinks: false,
basedir: path.resolve(basedir),
isFile: function (file) {
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
},
isDirectory: function (dir) {
return !!dirs[path.resolve(dir)];
},
readFileSync: function (file) {
return files[path.resolve(file)];
},
realpathSync: function (file) {
var resolved = path.resolve(file);
if (resolved.indexOf('symlinked') >= 0) {
return resolved;
}
var ext = path.extname(resolved);
if (ext) {
var dir = path.dirname(resolved);
var base = path.basename(resolved);
return path.join(dir, 'symlinked', base);
}
return path.join(resolved, 'symlinked');
}
};
}
t.equal(
resolve.sync('./baz', opts('/foo/bar')),
path.resolve('/foo/bar/symlinked/baz.js')
);
t.equal(
resolve.sync('./baz.js', opts('/foo/bar')),
path.resolve('/foo/bar/symlinked/baz.js')
);
});
test('readPackageSync', function (t) {
t.plan(3);
var files = {};
files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
var dirs = {};
dirs[path.resolve('/foo')] = true;
dirs[path.resolve('/foo/node_modules')] = true;
function opts(basedir, useReadPackage) {
return {
basedir: path.resolve(basedir),
isFile: function (file) {
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
},
isDirectory: function (dir) {
return !!dirs[path.resolve(dir)];
},
readFileSync: useReadPackage ? null : function (file) {
return files[path.resolve(file)];
},
realpathSync: function (file) {
return file;
}
};
}
t.test('with readFile', function (st) {
st.plan(1);
st.equal(
resolve.sync('bar', opts('/foo')),
path.resolve('/foo/node_modules/bar/baz.js')
);
});
function readPackageSync(readFileSync, file) {
if (file.indexOf(path.join('bar', 'package.json')) >= 0) {
return { main: './something-else.js' };
}
return JSON.parse(files[path.resolve(file)]);
}
t.test('with readPackage', function (st) {
st.plan(1);
var options = opts('/foo');
delete options.readFileSync;
options.readPackageSync = readPackageSync;
st.equal(
resolve.sync('bar', options),
path.resolve('/foo/node_modules/bar/something-else.js')
);
});
t.test('with readFile and readPackage', function (st) {
st.plan(1);
var options = opts('/foo');
options.readPackageSync = readPackageSync;
st.throws(
function () { resolve.sync('bar', options); },
TypeError,
'errors when both readFile and readPackage are provided'
);
});
});

View File

@@ -0,0 +1,56 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('moduleDirectory strings', function (t) {
t.plan(4);
var dir = path.join(__dirname, 'module_dir');
var xopts = {
basedir: dir,
moduleDirectory: 'xmodules'
};
resolve('aaa', xopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, '/xmodules/aaa/index.js'));
});
var yopts = {
basedir: dir,
moduleDirectory: 'ymodules'
};
resolve('aaa', yopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, '/ymodules/aaa/index.js'));
});
});
test('moduleDirectory array', function (t) {
t.plan(6);
var dir = path.join(__dirname, 'module_dir');
var aopts = {
basedir: dir,
moduleDirectory: ['xmodules', 'ymodules', 'zmodules']
};
resolve('aaa', aopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, '/xmodules/aaa/index.js'));
});
var bopts = {
basedir: dir,
moduleDirectory: ['zmodules', 'ymodules', 'xmodules']
};
resolve('aaa', bopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, '/ymodules/aaa/index.js'));
});
var copts = {
basedir: dir,
moduleDirectory: ['xmodules', 'ymodules', 'zmodules']
};
resolve('bbb', copts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, '/zmodules/bbb/main.js'));
});
});

View File

@@ -0,0 +1,3 @@
{
"main": "main.js"
}

View File

@@ -0,0 +1,143 @@
var test = require('tape');
var path = require('path');
var parse = path.parse || require('path-parse');
var keys = require('object-keys');
var nodeModulesPaths = require('../lib/node-modules-paths');
function verifyDirs(t, start, dirs, moduleDirectories, paths) {
var moduleDirs = [].concat(moduleDirectories || 'node_modules');
if (paths) {
for (var k = 0; k < paths.length; ++k) {
moduleDirs.push(path.basename(paths[k]));
}
}
var foundModuleDirs = {};
var uniqueDirs = {};
var parsedDirs = {};
for (var i = 0; i < dirs.length; ++i) {
var parsed = parse(dirs[i]);
if (!foundModuleDirs[parsed.base]) { foundModuleDirs[parsed.base] = 0; }
foundModuleDirs[parsed.base] += 1;
parsedDirs[parsed.dir] = true;
uniqueDirs[dirs[i]] = true;
}
t.equal(keys(parsedDirs).length >= start.split(path.sep).length, true, 'there are >= dirs than "start" has');
var foundModuleDirNames = keys(foundModuleDirs);
t.deepEqual(foundModuleDirNames, moduleDirs, 'all desired module dirs were found');
t.equal(keys(uniqueDirs).length, dirs.length, 'all dirs provided were unique');
var counts = {};
for (var j = 0; j < foundModuleDirNames.length; ++j) {
counts[foundModuleDirs[j]] = true;
}
t.equal(keys(counts).length, 1, 'all found module directories had the same count');
}
test('node-modules-paths', function (t) {
t.test('no options', function (t) {
var start = path.join(__dirname, 'resolver');
var dirs = nodeModulesPaths(start);
verifyDirs(t, start, dirs);
t.end();
});
t.test('empty options', function (t) {
var start = path.join(__dirname, 'resolver');
var dirs = nodeModulesPaths(start, {});
verifyDirs(t, start, dirs);
t.end();
});
t.test('with paths=array option', function (t) {
var start = path.join(__dirname, 'resolver');
var paths = ['a', 'b'];
var dirs = nodeModulesPaths(start, { paths: paths });
verifyDirs(t, start, dirs, null, paths);
t.end();
});
t.test('with paths=function option', function (t) {
function paths(request, absoluteStart, getNodeModulesDirs, opts) {
return getNodeModulesDirs().concat(path.join(absoluteStart, 'not node modules', request));
}
var start = path.join(__dirname, 'resolver');
var dirs = nodeModulesPaths(start, { paths: paths }, 'pkg');
verifyDirs(t, start, dirs, null, [path.join(start, 'not node modules', 'pkg')]);
t.end();
});
t.test('with paths=function skipping node modules resolution', function (t) {
function paths(request, absoluteStart, getNodeModulesDirs, opts) {
return [];
}
var start = path.join(__dirname, 'resolver');
var dirs = nodeModulesPaths(start, { paths: paths });
t.deepEqual(dirs, [], 'no node_modules was computed');
t.end();
});
t.test('with moduleDirectory option', function (t) {
var start = path.join(__dirname, 'resolver');
var moduleDirectory = 'not node modules';
var dirs = nodeModulesPaths(start, { moduleDirectory: moduleDirectory });
verifyDirs(t, start, dirs, moduleDirectory);
t.end();
});
t.test('with 1 moduleDirectory and paths options', function (t) {
var start = path.join(__dirname, 'resolver');
var paths = ['a', 'b'];
var moduleDirectory = 'not node modules';
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectory });
verifyDirs(t, start, dirs, moduleDirectory, paths);
t.end();
});
t.test('with 1+ moduleDirectory and paths options', function (t) {
var start = path.join(__dirname, 'resolver');
var paths = ['a', 'b'];
var moduleDirectories = ['not node modules', 'other modules'];
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
verifyDirs(t, start, dirs, moduleDirectories, paths);
t.end();
});
t.test('combine paths correctly on Windows', function (t) {
var start = 'C:\\Users\\username\\myProject\\src';
var paths = [];
var moduleDirectories = ['node_modules', start];
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
t.equal(dirs.indexOf(path.resolve(start)) > -1, true, 'should contain start dir');
t.end();
});
t.test('combine paths correctly on non-Windows', { skip: process.platform === 'win32' }, function (t) {
var start = '/Users/username/git/myProject/src';
var paths = [];
var moduleDirectories = ['node_modules', '/Users/username/git/myProject/src'];
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
t.equal(dirs.indexOf(path.resolve(start)) > -1, true, 'should contain start dir');
t.end();
});
});

View File

@@ -0,0 +1,70 @@
var fs = require('fs');
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('$NODE_PATH', function (t) {
t.plan(8);
function isDir(dir, cb) {
if (dir === '/node_path' || dir === 'node_path/x') {
return cb(null, true);
}
fs.stat(dir, function (err, stat) {
if (!err) {
return cb(null, stat.isDirectory());
}
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
return cb(err);
});
}
resolve('aaa', {
paths: [
path.join(__dirname, '/node_path/x'),
path.join(__dirname, '/node_path/y')
],
basedir: __dirname,
isDirectory: isDir
}, function (err, res) {
t.error(err);
t.equal(res, path.join(__dirname, '/node_path/x/aaa/index.js'), 'aaa resolves');
});
resolve('bbb', {
paths: [
path.join(__dirname, '/node_path/x'),
path.join(__dirname, '/node_path/y')
],
basedir: __dirname,
isDirectory: isDir
}, function (err, res) {
t.error(err);
t.equal(res, path.join(__dirname, '/node_path/y/bbb/index.js'), 'bbb resolves');
});
resolve('ccc', {
paths: [
path.join(__dirname, '/node_path/x'),
path.join(__dirname, '/node_path/y')
],
basedir: __dirname,
isDirectory: isDir
}, function (err, res) {
t.error(err);
t.equal(res, path.join(__dirname, '/node_path/x/ccc/index.js'), 'ccc resolves');
});
// ensure that relative paths still resolve against the regular `node_modules` correctly
resolve('tap', {
paths: [
'node_path'
],
basedir: path.join(__dirname, 'node_path/x'),
isDirectory: isDir
}, function (err, res) {
var root = require('tap/package.json').main; // eslint-disable-line global-require
t.error(err);
t.equal(res.replace('/node_modules/.vlt/··tap@0.4.13/', '/'), path.resolve(__dirname, '..', 'node_modules/tap', root), 'tap resolves');
});
});

View File

@@ -0,0 +1,9 @@
var test = require('tape');
var resolve = require('../');
test('nonstring', function (t) {
t.plan(1);
resolve(555, function (err, res, pkg) {
t.ok(err);
});
});

View File

@@ -0,0 +1,75 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
var resolverDir = path.join(__dirname, '/pathfilter/deep_ref');
function pathFilterFactory(t) {
return function (pkg, x, remainder) {
t.equal(pkg.version, '1.2.3');
t.equal(x, path.join(resolverDir, 'node_modules/deep/ref'));
t.equal(remainder, 'ref');
return 'alt';
};
}
test('#62: deep module references and the pathFilter', function (t) {
t.test('deep/ref.js', function (st) {
st.plan(3);
resolve('deep/ref', { basedir: resolverDir }, function (err, res, pkg) {
if (err) st.fail(err);
st.equal(pkg.version, '1.2.3');
st.equal(res, path.join(resolverDir, 'node_modules/deep/ref.js'));
});
var res = resolve.sync('deep/ref', { basedir: resolverDir });
st.equal(res, path.join(resolverDir, 'node_modules/deep/ref.js'));
});
t.test('deep/deeper/ref', function (st) {
st.plan(4);
resolve(
'deep/deeper/ref',
{ basedir: resolverDir },
function (err, res, pkg) {
if (err) t.fail(err);
st.notEqual(pkg, undefined);
st.equal(pkg.version, '1.2.3');
st.equal(res, path.join(resolverDir, 'node_modules/deep/deeper/ref.js'));
}
);
var res = resolve.sync(
'deep/deeper/ref',
{ basedir: resolverDir }
);
st.equal(res, path.join(resolverDir, 'node_modules/deep/deeper/ref.js'));
});
t.test('deep/ref alt', function (st) {
st.plan(8);
var pathFilter = pathFilterFactory(st);
var res = resolve.sync(
'deep/ref',
{ basedir: resolverDir, pathFilter: pathFilter }
);
st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));
resolve(
'deep/ref',
{ basedir: resolverDir, pathFilter: pathFilter },
function (err, res, pkg) {
if (err) st.fail(err);
st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));
st.end();
}
);
});
t.end();
});

View File

@@ -0,0 +1,24 @@
var test = require('tape');
var path = require('path');
var resolve = require('../');
test('synchronous pathfilter', function (t) {
var res;
var resolverDir = __dirname + '/pathfilter/deep_ref';
function pathFilter(pkg, x, remainder) {
t.equal(pkg.version, '1.2.3');
t.equal(x, path.join(resolverDir, 'node_modules', 'deep', 'ref'));
t.equal(remainder, 'ref');
return 'alt';
}
res = resolve.sync('deep/ref', { basedir: resolverDir });
t.equal(res, path.join(resolverDir, 'node_modules', 'deep', 'ref.js'));
res = resolve.sync('deep/deeper/ref', { basedir: resolverDir });
t.equal(res, path.join(resolverDir, 'node_modules', 'deep', 'deeper', 'ref.js'));
res = resolve.sync('deep/ref', { basedir: resolverDir, pathFilter: pathFilter });
t.equal(res, path.join(resolverDir, 'node_modules', 'deep', 'alt.js'));
t.end();
});

View File

@@ -0,0 +1,23 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('precedence', function (t) {
t.plan(3);
var dir = path.join(__dirname, 'precedence/aaa');
resolve('./', { basedir: dir }, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, 'index.js'));
t.equal(pkg.name, 'resolve');
});
});
test('./ should not load ${dir}.js', function (t) { // eslint-disable-line no-template-curly-in-string
t.plan(1);
var dir = path.join(__dirname, 'precedence/bbb');
resolve('./', { basedir: dir }, function (err, res, pkg) {
t.ok(err);
});
});

View File

@@ -0,0 +1,613 @@
var path = require('path');
var fs = require('fs');
var test = require('tape');
var resolve = require('../');
var async = require('../async');
test('`./async` entry point', function (t) {
t.equal(resolve, async, '`./async` entry point is the same as `main`');
t.end();
});
test('async foo', function (t) {
t.plan(12);
var dir = path.join(__dirname, 'resolver');
resolve('./foo', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg && pkg.name, 'resolve');
});
resolve('./foo.js', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg && pkg.name, 'resolve');
});
resolve('./foo', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg && pkg.main, 'resolver');
});
resolve('./foo.js', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg.main, 'resolver');
});
resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
});
resolve('foo', { basedir: dir }, function (err) {
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
// Test that filename is reported as the "from" value when passed.
resolve('foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err) {
t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
});
});
test('bar', function (t) {
t.plan(6);
var dir = path.join(__dirname, 'resolver');
resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
t.equal(pkg, undefined);
});
resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
t.equal(pkg, undefined);
});
resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
t.equal(pkg.main, 'bar');
});
});
test('baz', function (t) {
t.plan(4);
var dir = path.join(__dirname, 'resolver');
resolve('./baz', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'baz/quux.js'));
t.equal(pkg.main, 'quux.js');
});
resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'baz/quux.js'));
t.equal(pkg.main, 'quux.js');
});
});
test('biz', function (t) {
t.plan(24);
var dir = path.join(__dirname, 'resolver/biz/node_modules');
resolve('./grux', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'grux/index.js'));
t.equal(pkg, undefined);
});
resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'grux/index.js'));
t.equal(pkg.main, 'biz');
});
resolve('./garply', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'garply/lib/index.js'));
t.equal(pkg.main, './lib');
});
resolve('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'garply/lib/index.js'));
t.equal(pkg.main, './lib');
});
resolve('tiv', { basedir: dir + '/grux' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'tiv/index.js'));
t.equal(pkg, undefined);
});
resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'tiv/index.js'));
t.equal(pkg.main, 'grux');
});
resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'tiv/index.js'));
t.equal(pkg, undefined);
});
resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'tiv/index.js'));
t.equal(pkg.main, './lib');
});
resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'grux/index.js'));
t.equal(pkg, undefined);
});
resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'grux/index.js'));
t.equal(pkg.main, 'tiv');
});
resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'garply/lib/index.js'));
t.equal(pkg.main, './lib');
});
resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'garply/lib/index.js'));
t.equal(pkg.main, './lib');
});
});
test('quux', function (t) {
t.plan(2);
var dir = path.join(__dirname, 'resolver/quux');
resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo/index.js'));
t.equal(pkg.main, 'quux');
});
});
test('normalize', function (t) {
t.plan(2);
var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
resolve('../grux', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
t.equal(pkg, undefined);
});
});
test('cup', function (t) {
t.plan(5);
var dir = path.join(__dirname, 'resolver');
resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'cup.coffee'));
});
resolve('./cup.coffee', { basedir: dir }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'cup.coffee'));
});
resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) {
t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
// Test that filename is reported as the "from" value when passed.
resolve('./cup', { basedir: dir, extensions: ['.js'], filename: path.join(dir, 'cupboard.js') }, function (err, res) {
t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
});
});
test('mug', function (t) {
t.plan(3);
var dir = path.join(__dirname, 'resolver');
resolve('./mug', { basedir: dir }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'mug.js'));
});
resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, '/mug.coffee'));
});
resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
t.equal(res, path.join(dir, '/mug.js'));
});
});
test('other path', function (t) {
t.plan(6);
var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'bar');
var otherDir = path.join(resolverDir, 'other_path');
resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(resolverDir, 'other_path/root.js'));
});
resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js'));
});
resolve('root', { basedir: dir }, function (err, res) {
t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) {
t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
});
test('path iterator', function (t) {
t.plan(2);
var resolverDir = path.join(__dirname, 'resolver');
function exactIterator(x, start, getPackageCandidates, opts) {
return [path.join(resolverDir, x)];
}
resolve('baz', { packageIterator: exactIterator }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(resolverDir, 'baz/quux.js'));
t.equal(pkg && pkg.name, 'baz');
});
});
test('empty main', function (t) {
t.plan(1);
var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'empty_main');
resolve('./empty_main', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});
test('incorrect main', function (t) {
t.plan(1);
var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'incorrect_main');
resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});
test('missing index', function (t) {
t.plan(2);
var resolverDir = path.join(__dirname, 'resolver');
resolve('./missing_index', { basedir: resolverDir }, function (err, res, pkg) {
t.ok(err instanceof Error);
t.equal(err && err.code, 'INCORRECT_PACKAGE_MAIN', 'error has correct error code');
});
});
test('missing main', function (t) {
t.plan(1);
var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'missing_main');
resolve('./missing_main', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});
test('null main', function (t) {
t.plan(1);
var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'null_main');
resolve('./null_main', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});
test('main: false', function (t) {
t.plan(2);
var basedir = path.join(__dirname, 'resolver');
var dir = path.join(basedir, 'false_main');
resolve('./false_main', { basedir: basedir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(
res,
path.join(dir, 'index.js'),
'`"main": false`: resolves to `index.js`'
);
t.deepEqual(pkg, {
name: 'false_main',
main: false
});
});
});
test('without basedir', function (t) {
t.plan(1);
var dir = path.join(__dirname, 'resolver/without_basedir');
var tester = require(path.join(dir, 'main.js')); // eslint-disable-line global-require
tester(t, function (err, res, pkg) {
if (err) {
t.fail(err);
} else {
t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
}
});
});
test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
t.plan(2);
var dir = path.join(__dirname, 'resolver');
resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'same_names/foo.js'));
});
resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
});
});
test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
t.plan(2);
var dir = path.join(__dirname, 'resolver');
resolve('./', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
});
resolve('.', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
});
});
test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
var testFile = path.basename(__filename);
t.test('sanity check', function (st) {
st.plan(1);
resolve('./' + testFile, function (err, res, pkg) {
if (err) t.fail(err);
st.equal(res, __filename, 'sanity check');
});
});
t.test('with a fake directory', function (st) {
st.plan(4);
resolve('./' + testFile + '/blah', function (err, res, pkg) {
st.ok(err, 'there is an error');
st.notOk(res, 'no result');
st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
st.equal(
err && err.message,
'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
'can not find nonexistent module'
);
st.end();
});
});
t.end();
});
test('async dot main', function (t) {
var start = new Date();
t.plan(3);
resolve('./resolver/dot_main', function (err, ret) {
t.notOk(err);
t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
t.ok(new Date() - start < 50, 'resolve.sync timedout');
t.end();
});
});
test('async dot slash main', function (t) {
var start = new Date();
t.plan(3);
resolve('./resolver/dot_slash_main', function (err, ret) {
t.notOk(err);
t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
t.ok(new Date() - start < 50, 'resolve.sync timedout');
t.end();
});
});
test('not a directory', function (t) {
t.plan(6);
var path = './foo';
resolve(path, { basedir: __filename }, function (err, res, pkg) {
t.ok(err, 'a non-directory errors');
t.equal(arguments.length, 1);
t.equal(res, undefined);
t.equal(pkg, undefined);
t.equal(err && err.message, 'Provided basedir "' + __filename + '" is not a directory, or a symlink to a directory');
t.equal(err && err.code, 'INVALID_BASEDIR');
});
});
test('non-string "main" field in package.json', function (t) {
t.plan(5);
var dir = path.join(__dirname, 'resolver');
resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
t.ok(err, 'errors on non-string main');
t.equal(err.message, 'package “invalid_main” `main` must be a string');
t.equal(err.code, 'INVALID_PACKAGE_MAIN');
t.equal(res, undefined, 'res is undefined');
t.equal(pkg, undefined, 'pkg is undefined');
});
});
test('non-string "main" field in package.json', function (t) {
t.plan(5);
var dir = path.join(__dirname, 'resolver');
resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
t.ok(err, 'errors on non-string main');
t.equal(err.message, 'package “invalid_main” `main` must be a string');
t.equal(err.code, 'INVALID_PACKAGE_MAIN');
t.equal(res, undefined, 'res is undefined');
t.equal(pkg, undefined, 'pkg is undefined');
});
});
test('browser field in package.json', function (t) {
t.plan(3);
var dir = path.join(__dirname, 'resolver');
resolve(
'./browser_field',
{
basedir: dir,
packageFilter: function packageFilter(pkg) {
if (pkg.browser) {
pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
delete pkg.browser; // eslint-disable-line no-param-reassign
}
return pkg;
}
},
function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'browser_field', 'b.js'));
t.equal(pkg && pkg.main, 'b');
t.equal(pkg && pkg.browser, undefined);
}
);
});
test('absolute paths', function (t) {
t.plan(4);
var extensionless = __filename.slice(0, -path.extname(__filename).length);
resolve(__filename, function (err, res) {
t.equal(
res,
__filename,
'absolute path to this file resolves'
);
});
resolve(extensionless, function (err, res) {
t.equal(
res,
__filename,
'extensionless absolute path to this file resolves'
);
});
resolve(__filename, { basedir: process.cwd() }, function (err, res) {
t.equal(
res,
__filename,
'absolute path to this file with a basedir resolves'
);
});
resolve(extensionless, { basedir: process.cwd() }, function (err, res) {
t.equal(
res,
__filename,
'extensionless absolute path to this file with a basedir resolves'
);
});
});
var malformedDir = path.join(__dirname, 'resolver/malformed_package_json');
test('malformed package.json', { skip: !fs.existsSync(malformedDir) }, function (t) {
/* eslint operator-linebreak: ["error", "before"], function-paren-newline: "off" */
t.plan(
(3 * 3) // 3 sets of 3 assertions in the final callback
+ 2 // 1 readPackage call with malformed package.json
);
var basedir = malformedDir;
var expected = path.join(basedir, 'index.js');
resolve('./index.js', { basedir: basedir }, function (err, res, pkg) {
t.error(err, 'no error');
t.equal(res, expected, 'malformed package.json is silently ignored');
t.equal(pkg, undefined, 'malformed package.json gives an undefined `pkg` argument');
});
resolve(
'./index.js',
{
basedir: basedir,
packageFilter: function (pkg, pkgfile, dir) {
t.fail('should not reach here');
}
},
function (err, res, pkg) {
t.error(err, 'with packageFilter: no error');
t.equal(res, expected, 'with packageFilter: malformed package.json is silently ignored');
t.equal(pkg, undefined, 'with packageFilter: malformed package.json gives an undefined `pkg` argument');
}
);
resolve(
'./index.js',
{
basedir: basedir,
readPackage: function (readFile, pkgfile, cb) {
t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
readFile(pkgfile, function (err, result) {
try {
cb(null, JSON.parse(result));
} catch (e) {
t.ok(e instanceof SyntaxError, 'readPackage: malformed package.json parses as a syntax error');
cb(e);
}
});
}
},
function (err, res, pkg) {
t.error(err, 'with readPackage: no error');
t.equal(res, expected, 'with readPackage: malformed package.json is silently ignored');
t.equal(pkg, undefined, 'with readPackage: malformed package.json gives an undefined `pkg` argument');
}
);
});

View File

@@ -0,0 +1,4 @@
{
"name": "baz",
"main": "quux.js"
}

View File

@@ -0,0 +1,5 @@
{
"name": "browser_field",
"main": "a",
"browser": "b"
}

View File

@@ -0,0 +1,3 @@
{
"main": "."
}

View File

@@ -0,0 +1,3 @@
{
"main": "./"
}

View File

@@ -0,0 +1,3 @@
{
"main": ""
}

View File

@@ -0,0 +1,4 @@
{
"name": "false_main",
"main": false
}

View File

@@ -0,0 +1,3 @@
{
"main": "wrong.js"
}

View File

@@ -0,0 +1,7 @@
{
"name": "invalid_main",
"main": [
"why is this a thing",
"srsly omg wtf"
]
}

View File

@@ -0,0 +1,3 @@
{
"main": "index.js"
}

View File

@@ -0,0 +1,3 @@
{
"notmain": "index.js"
}

View File

@@ -0,0 +1,6 @@
{
"packages": [
"packages/*"
],
"version": "0.0.0"
}

Some files were not shown because too many files have changed in this diff Show More