-
-
Notifications
You must be signed in to change notification settings - Fork 199
add restrictions option support #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| MIT License http://www.opensource.org/licenses/mit-license.php | ||
| Author Ivan Kopeykin @vankop | ||
| */ | ||
|
|
||
| "use strict"; | ||
|
|
||
| /** @typedef {import("./Resolver")} Resolver */ | ||
| /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */ | ||
|
|
||
| const slashCode = "/".charCodeAt(0); | ||
| const backslashCode = "\\".charCodeAt(0); | ||
|
|
||
| const isInside = (path, parent) => { | ||
| if (!path.startsWith(parent)) return false; | ||
| if (path.length === parent.length) return true; | ||
| const charCode = path.charCodeAt(parent.length); | ||
| return charCode === slashCode || charCode === backslashCode; | ||
| }; | ||
|
|
||
| module.exports = class RestrictionsPlugin { | ||
| /** | ||
| * @param {string | ResolveStepHook} source source | ||
| * @param {Set<string | RegExp>} restrictions restrictions | ||
| */ | ||
| constructor(source, restrictions) { | ||
| this.source = source; | ||
| this.restrictions = restrictions; | ||
| } | ||
|
|
||
| /** | ||
| * @param {Resolver} resolver the resolver | ||
| * @returns {void} | ||
| */ | ||
| apply(resolver) { | ||
| resolver | ||
| .getHook(this.source) | ||
| .tapAsync("RestrictionsPlugin", (request, resolveContext, callback) => { | ||
| if (typeof request.path === "string") { | ||
| const path = request.path; | ||
| for (const rule of this.restrictions) { | ||
| if (typeof rule === "string") { | ||
| if (!isInside(path, rule)) { | ||
| if (resolveContext.log) { | ||
| resolveContext.log( | ||
| `${path} is not inside of the restriction ${rule}` | ||
| ); | ||
| } | ||
| return callback(null, null); | ||
| } | ||
| } else if (!rule.test(path)) { | ||
| if (resolveContext.log) { | ||
| resolveContext.log( | ||
| `${path} doesn't match the restriction ${rule}` | ||
| ); | ||
| } | ||
| return callback(null, null); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| callback(); | ||
| }); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| require("should"); | ||
| const path = require("path"); | ||
| const fs = require("fs"); | ||
| const ResolverFactory = require("../lib/ResolverFactory"); | ||
| const CachedInputFileSystem = require("../lib/CachedInputFileSystem"); | ||
|
|
||
| const fixture = path.resolve(__dirname, "fixtures", "restrictions"); | ||
| const nodeFileSystem = new CachedInputFileSystem(fs, 4000); | ||
|
|
||
| describe("restrictions", () => { | ||
| it("should respect RegExp restriction", done => { | ||
| const resolver = ResolverFactory.createResolver({ | ||
| extensions: [".js"], | ||
| fileSystem: nodeFileSystem, | ||
| restrictions: [/\.(sass|scss|css)$/] | ||
| }); | ||
|
|
||
| resolver.resolve({}, fixture, "pck1", {}, (err, result) => { | ||
| if (!err) throw new Error(`expect error, got ${result}`); | ||
| err.should.be.instanceof(Error); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it("should try to find alternative #1", done => { | ||
| const resolver = ResolverFactory.createResolver({ | ||
| extensions: [".js", ".css"], | ||
| fileSystem: nodeFileSystem, | ||
| mainFiles: ["index"], | ||
| restrictions: [/\.(sass|scss|css)$/] | ||
| }); | ||
|
|
||
| resolver.resolve({}, fixture, "pck1", {}, (err, result) => { | ||
| if (err) return done(err); | ||
| if (!result) throw new Error("No result"); | ||
| result.should.equal(path.resolve(fixture, "node_modules/pck1/index.css")); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it("should respect string restriction", done => { | ||
| const resolver = ResolverFactory.createResolver({ | ||
| extensions: [".js"], | ||
| fileSystem: nodeFileSystem, | ||
| restrictions: [fixture] | ||
| }); | ||
|
|
||
| resolver.resolve({}, fixture, "pck2", {}, (err, result) => { | ||
| if (!err) throw new Error(`expect error, got ${result}`); | ||
| err.should.be.instanceof(Error); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it("should try to find alternative #2", done => { | ||
| const resolver = ResolverFactory.createResolver({ | ||
| extensions: [".js"], | ||
| fileSystem: nodeFileSystem, | ||
| mainFields: ["main", "style"], | ||
| restrictions: [fixture, /\.(sass|scss|css)$/] | ||
| }); | ||
|
|
||
| resolver.resolve({}, fixture, "pck2", {}, (err, result) => { | ||
| if (err) return done(err); | ||
| if (!result) throw new Error("No result"); | ||
| result.should.equal(path.resolve(fixture, "node_modules/pck2/index.css")); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it("should try to find alternative #3", done => { | ||
| const resolver = ResolverFactory.createResolver({ | ||
| extensions: [".js"], | ||
| fileSystem: nodeFileSystem, | ||
| mainFields: ["main", "module", "style"], | ||
| restrictions: [fixture, /\.(sass|scss|css)$/] | ||
| }); | ||
|
|
||
| const log = []; | ||
|
|
||
| resolver.resolve( | ||
| {}, | ||
| fixture, | ||
| "pck2", | ||
| { log: log.push.bind(log) }, | ||
| (err, result) => { | ||
| if (err) return done(err); | ||
| if (!result) throw new Error("No result"); | ||
| result.should.equal( | ||
| path.resolve(fixture, "node_modules/pck2/index.css") | ||
| ); | ||
| log | ||
| .map(line => | ||
| line | ||
| .replace(path.resolve(__dirname, ".."), "...") | ||
| .replace(path.resolve(__dirname, ".."), "...") | ||
| .replace(/\\/g, "/") | ||
| ) | ||
| .should.be.eql([ | ||
| "resolve 'pck2' in '.../test/fixtures/restrictions'", | ||
| " Parsed request is a module", | ||
| " using description file: .../package.json (relative path: ./test/fixtures/restrictions)", | ||
| " resolve as module", | ||
| " looking for modules in .../test/fixtures/restrictions/node_modules", | ||
| " single file module", | ||
| " using description file: .../package.json (relative path: ./test/fixtures/restrictions/node_modules/pck2)", | ||
| " no extension", | ||
| " .../test/fixtures/restrictions/node_modules/pck2 is not a file", | ||
| " .js", | ||
| " .../test/fixtures/restrictions/node_modules/pck2.js doesn't exist", | ||
| " existing directory .../test/fixtures/restrictions/node_modules/pck2", | ||
| " using description file: .../test/fixtures/restrictions/node_modules/pck2/package.json (relative path: .)", | ||
| " using description file: .../package.json (relative path: ./test/fixtures/restrictions/node_modules/pck2)", | ||
| " no extension", | ||
| " .../test/fixtures/restrictions/node_modules/pck2 is not a file", | ||
| " .js", | ||
| " .../test/fixtures/restrictions/node_modules/pck2.js doesn't exist", | ||
| " as directory", | ||
| " existing directory .../test/fixtures/restrictions/node_modules/pck2", | ||
| " using description file: .../test/fixtures/restrictions/node_modules/pck2/package.json (relative path: .)", | ||
| " use ../../../c.js from main in package.json", | ||
| " using description file: .../package.json (relative path: ./test/fixtures/c.js)", | ||
| " no extension", | ||
| " existing file: .../test/fixtures/c.js", | ||
| " .../test/fixtures/c.js is not inside of the restriction .../test/fixtures/restrictions", | ||
| " .js", | ||
| " .../test/fixtures/c.js.js doesn't exist", | ||
| " as directory", | ||
| " .../test/fixtures/c.js is not a directory", | ||
| " use ./module.js from module in package.json", | ||
| " using description file: .../test/fixtures/restrictions/node_modules/pck2/package.json (relative path: ./module.js)", | ||
| " no extension", | ||
| " existing file: .../test/fixtures/restrictions/node_modules/pck2/module.js", | ||
| " .../test/fixtures/restrictions/node_modules/pck2/module.js doesn't match the restriction //.(sass|scss|css)$/", | ||
| " .js", | ||
| " .../test/fixtures/restrictions/node_modules/pck2/module.js.js doesn't exist", | ||
| " as directory", | ||
| " .../test/fixtures/restrictions/node_modules/pck2/module.js is not a directory", | ||
| " use ./index.css from style in package.json", | ||
| " using description file: .../test/fixtures/restrictions/node_modules/pck2/package.json (relative path: ./index.css)", | ||
| " no extension", | ||
| " existing file: .../test/fixtures/restrictions/node_modules/pck2/index.css", | ||
| " reporting result .../test/fixtures/restrictions/node_modules/pck2/index.css" | ||
| ]); | ||
| done(); | ||
| } | ||
| ); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.