diff --git a/@packages/ip-filter/.verb.md b/@packages/ip-filter/.verb.md new file mode 100644 index 00000000..f6b95c02 --- /dev/null +++ b/@packages/ip-filter/.verb.md @@ -0,0 +1,7 @@ +## API + + + +{%= include(process.cwd() + "/docs/src/index.md") %} + + diff --git a/@packages/ip-filter/CHANGELOG.md b/@packages/ip-filter/CHANGELOG.md new file mode 100644 index 00000000..d0adcf6b --- /dev/null +++ b/@packages/ip-filter/CHANGELOG.md @@ -0,0 +1,38 @@ + + +## 2.0.0 - 2016-10-06 + +- update repo style and dotfiles +- use standard-version +- follow semver and standards +- use npm scripts + +**BREAKING CHANGES** + +- make 3rd argument to be `options` object instead of boolean +- fixes #3 and add more tests + +## 1.0.2 - 2015-03-26 +- Release v1.0.2 / npm@v1.0.2 +- use verb to generate docs +- update boilerplate and license year +- use pre-commit to guarantee workflow + +## 1.0.1 - 2015-03-26 +- Release v1.0.1 / npm@v1.0.1 +- add more tests +- little refactor to use `to-file-path` in strict mode + + forced to be done, because of `micromatch@2.2` that works only for filepaths +- update deps + +## 1.0.0 - 2015-05-24 +- Release v1.0.0 / npm@v1.0.0 +- add support for no strict mode +- run npm-related(1) +- run keywords(1) +- Commits on May 23, 2015 +- add test for when invalid ip +- implement + +## 0.0.0 - 2015-05-23 +- first commits diff --git a/@packages/ip-filter/LICENSE.md b/@packages/ip-filter/LICENSE.md new file mode 100644 index 00000000..e712be40 --- /dev/null +++ b/@packages/ip-filter/LICENSE.md @@ -0,0 +1,348 @@ +Mozilla Public License, version 2.0 + +1. Definitions + +1.1. “Contributor” + +means each individual or legal entity that creates, contributes to the +creation of, or owns Covered Software. + +1.2. “Contributor Version” + + means the combination of the Contributions of others (if any) used by a + Contributor and that particular Contributor’s Contribution. + +1.3. “Contribution” + + means Covered Software of a particular Contributor. + +1.4. “Covered Software” + + means Source Code Form to which the initial Contributor has attached the + notice in Exhibit A, the Executable Form of such Source Code Form, and + Modifications of such Source Code Form, in each case including portions + thereof. + +1.5. “Incompatible With Secondary Licenses” means + + a. that the initial Contributor has attached the notice described in + Exhibit B to the Covered Software; or + + b. that the Covered Software was made available under the terms of version + 1.1 or earlier of the License, but not also under the terms of a + Secondary License. + +1.6. “Executable Form” + + means any form of the work other than Source Code Form. + +1.7. “Larger Work” + + means a work that combines Covered Software with other material, in a separate + file or files, that is not Covered Software. + +1.8. “License” + + means this document. + +1.9. “Licensable” + + means having the right to grant, to the maximum extent possible, whether at the + time of the initial grant or subsequently, any and all of the rights conveyed by + this License. + +1.10. “Modifications” + + means any of the following: + + a. any file in Source Code Form that results from an addition to, deletion + from, or modification of the contents of Covered Software; or + + b. any new file in Source Code Form that contains any Covered Software. + +1.11. “Patent Claims” of a Contributor + + means any patent claim(s), including without limitation, method, process, + and apparatus claims, in any patent Licensable by such Contributor that + would be infringed, but for the grant of the License, by the making, + using, selling, offering for sale, having made, import, or transfer of + either its Contributions or its Contributor Version. + +1.12. “Secondary License” + + means either the GNU General Public License, Version 2.0, the GNU Lesser + General Public License, Version 2.1, the GNU Affero General Public + License, Version 3.0, or any later versions of those licenses. + +1.13. “Source Code Form” + + means the form of the work preferred for making modifications. + +1.14. “You” (or “Your”) + + means an individual or a legal entity exercising rights under this + License. For legal entities, “You” includes any entity that controls, is + controlled by, or is under common control with You. For purposes of this + definition, “control” means (a) the power, direct or indirect, to cause + the direction or management of such entity, whether by contract or + otherwise, or (b) ownership of more than fifty percent (50%) of the + outstanding shares or beneficial ownership of such entity. + +2. License Grants and Conditions + +2.1. Grants + + Each Contributor hereby grants You a world-wide, royalty-free, + non-exclusive license: + + a. under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or as + part of a Larger Work; and + + b. under Patent Claims of such Contributor to make, use, sell, offer for + sale, have made, import, and otherwise transfer either its Contributions + or its Contributor Version. + +2.2. Effective Date + + The licenses granted in Section 2.1 with respect to any Contribution become + effective for each Contribution on the date the Contributor first distributes + such Contribution. + +2.3. Limitations on Grant Scope + + The licenses granted in this Section 2 are the only rights granted under this + License. No additional rights or licenses will be implied from the distribution + or licensing of Covered Software under this License. Notwithstanding Section + 2.1(b) above, no patent license is granted by a Contributor: + + a. for any code that a Contributor has removed from Covered Software; or + + b. for infringements caused by: (i) Your and any other third party’s + modifications of Covered Software, or (ii) the combination of its + Contributions with other software (except as part of its Contributor + Version); or + + c. under Patent Claims infringed by Covered Software in the absence of its + Contributions. + + This License does not grant any rights in the trademarks, service marks, or + logos of any Contributor (except as may be necessary to comply with the + notice requirements in Section 3.4). + +2.4. Subsequent Licenses + + No Contributor makes additional grants as a result of Your choice to + distribute the Covered Software under a subsequent version of this License + (see Section 10.2) or under the terms of a Secondary License (if permitted + under the terms of Section 3.3). + +2.5. Representation + + Each Contributor represents that the Contributor believes its Contributions + are its original creation(s) or it has sufficient rights to grant the + rights to its Contributions conveyed by this License. + +2.6. Fair Use + + This License is not intended to limit any rights You have under applicable + copyright doctrines of fair use, fair dealing, or other equivalents. + +2.7. Conditions + + Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in + Section 2.1. + +3. Responsibilities + +3.1. Distribution of Source Form + + All distribution of Covered Software in Source Code Form, including any + Modifications that You create or to which You contribute, must be under the + terms of this License. You must inform recipients that the Source Code Form + of the Covered Software is governed by the terms of this License, and how + they can obtain a copy of this License. You may not attempt to alter or + restrict the recipients’ rights in the Source Code Form. + +3.2. Distribution of Executable Form + + If You distribute Covered Software in Executable Form then: + + a. such Covered Software must also be made available in Source Code Form, + as described in Section 3.1, and You must inform recipients of the + Executable Form how they can obtain a copy of such Source Code Form by + reasonable means in a timely manner, at a charge no more than the cost + of distribution to the recipient; and + + b. You may distribute such Executable Form under the terms of this License, + or sublicense it under different terms, provided that the license for + the Executable Form does not attempt to limit or alter the recipients’ + rights in the Source Code Form under this License. + +3.3. Distribution of a Larger Work + + You may create and distribute a Larger Work under terms of Your choice, + provided that You also comply with the requirements of this License for the + Covered Software. If the Larger Work is a combination of Covered Software + with a work governed by one or more Secondary Licenses, and the Covered + Software is not Incompatible With Secondary Licenses, this License permits + You to additionally distribute such Covered Software under the terms of + such Secondary License(s), so that the recipient of the Larger Work may, at + their option, further distribute the Covered Software under the terms of + either this License or such Secondary License(s). + +3.4. Notices + + You may not remove or alter the substance of any license notices (including + copyright notices, patent notices, disclaimers of warranty, or limitations + of liability) contained within the Source Code Form of the Covered + Software, except that You may alter any license notices to the extent + required to remedy known factual inaccuracies. + +3.5. Application of Additional Terms + + You may choose to offer, and to charge a fee for, warranty, support, + indemnity or liability obligations to one or more recipients of Covered + Software. However, You may do so only on Your own behalf, and not on behalf + of any Contributor. You must make it absolutely clear that any such + warranty, support, indemnity, or liability obligation is offered by You + alone, and You hereby agree to indemnify every Contributor for any + liability incurred by such Contributor as a result of warranty, support, + indemnity or liability terms You offer. You may include additional + disclaimers of warranty and limitations of liability specific to any + jurisdiction. + +4. Inability to Comply Due to Statute or Regulation + + If it is impossible for You to comply with any of the terms of this License + with respect to some or all of the Covered Software due to statute, judicial + order, or regulation then You must: (a) comply with the terms of this License + to the maximum extent possible; and (b) describe the limitations and the code + they affect. Such description must be placed in a text file included with all + distributions of the Covered Software under this License. Except to the + extent prohibited by statute or regulation, such description must be + sufficiently detailed for a recipient of ordinary skill to be able to + understand it. + +5. Termination + +5.1. The rights granted under this License will terminate automatically if You +fail to comply with any of its terms. However, if You become compliant, then the +rights granted under this License from a particular Contributor are reinstated +(a) provisionally, unless and until such Contributor explicitly and finally +terminates Your grants, and (b) on an ongoing basis, if such Contributor fails +to notify You of the non-compliance by some reasonable means prior to 60 days +after You have come back into compliance. Moreover, Your grants from a +particular Contributor are reinstated on an ongoing basis if such Contributor +notifies You of the non-compliance by some reasonable means, this is the first +time You have received notice of non-compliance with this License from such +Contributor, and You become compliant prior to 30 days after Your receipt of the +notice. + +5.2. If You initiate litigation against any entity by asserting a patent +infringement claim (excluding declaratory judgment actions, counter-claims, and +cross-claims) alleging that a Contributor Version directly or indirectly +infringes any patent, then the rights granted to You by any and all Contributors +for the Covered Software under Section 2.1 of this License shall terminate. + +5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user +license agreements (excluding distributors and resellers) which have been +validly granted by You or Your distributors under this License prior to +termination shall survive termination. + +6. Disclaimer of Warranty + + Covered Software is provided under this License on an “as is” basis, without + warranty of any kind, either expressed, implied, or statutory, including, + without limitation, warranties that the Covered Software is free of defects, + merchantable, fit for a particular purpose or non-infringing. The entire risk + as to the quality and performance of the Covered Software is with You. Should + any Covered Software prove defective in any respect, You (not any + Contributor) assume the cost of any necessary servicing, repair, or + correction. This disclaimer of warranty constitutes an essential part of this + License. No use of any Covered Software is authorized under this License + except under this disclaimer. + +7. Limitation of Liability + + Under no circumstances and under no legal theory, whether tort (including + negligence), contract, or otherwise, shall any Contributor, or anyone who + distributes Covered Software as permitted above, be liable to You for any + direct, indirect, special, incidental, or consequential damages of any + character including, without limitation, damages for lost profits, loss of + goodwill, work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses, even if such party shall have been + informed of the possibility of such damages. This limitation of liability + shall not apply to liability for death or personal injury resulting from such + party’s negligence to the extent applicable law prohibits such limitation. + Some jurisdictions do not allow the exclusion or limitation of incidental or + consequential damages, so this exclusion and limitation may not apply to You. + +8. Litigation + + Any litigation relating to this License may be brought only in the courts of + a jurisdiction where the defendant maintains its principal place of business + and such litigation shall be governed by laws of that jurisdiction, without + reference to its conflict-of-law provisions. Nothing in this Section shall + prevent a party’s ability to bring cross-claims or counter-claims. + +9. Miscellaneous + + This License represents the complete agreement concerning the subject matter + hereof. If any provision of this License is held to be unenforceable, such + provision shall be reformed only to the extent necessary to make it + enforceable. Any law or regulation which provides that the language of a + contract shall be construed against the drafter shall not be used to construe + this License against a Contributor. + +10) Versions of the License + +10.1. New Versions + + Mozilla Foundation is the license steward. Except as provided in Section + 10.3, no one other than the license steward has the right to modify or + publish new versions of this License. Each version will be given a + distinguishing version number. + +10.2. Effect of New Versions + + You may distribute the Covered Software under the terms of the version of + the License under which You originally received the Covered Software, or + under the terms of any subsequent version published by the license + steward. + +10.3. Modified Versions + + If you create software not governed by this License, and you want to + create a new license for such software, you may create and use a modified + version of this License if you rename the license and remove any + references to the name of the license steward (except to note that such + modified license differs from this License). + +10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses +If You choose to distribute Source Code Form that is Incompatible With Secondary +Licenses under the terms of this version of the License, the notice described in +Exhibit B of this License must be attached. + +Exhibit A - Source Code Form License Notice + + This Source Code Form is subject to the + terms of the Mozilla Public License, v. + 2.0. If a copy of the MPL was not + distributed with this file, You can + obtain one at + http://mozilla.org/MPL/2.0/. + +If it is not possible or desirable to put the notice in a particular file, then +You may include the notice in a location (such as a LICENSE file in a relevant +directory) where a recipient would be likely to look for such a notice. + +You may add additional accurate notices of copyright ownership. + +Exhibit B - “Incompatible With Secondary Licenses” Notice + + This Source Code Form is “Incompatible + With Secondary Licenses”, as defined by + the Mozilla Public License, v. 2.0. diff --git a/@packages/ip-filter/README.md b/@packages/ip-filter/README.md new file mode 100644 index 00000000..7301b595 --- /dev/null +++ b/@packages/ip-filter/README.md @@ -0,0 +1,324 @@ + + +# ip-filter [![npm version][npmv-img]][npmv-url] [![License][license-img]][license-url] [![Libera Manifesto][libera-manifesto-img]][libera-manifesto-url] + +> Validates valid IPs (IPv4 and IPv6) using [micromatch][] - glob patterns, RegExp, string or array of globs. If match returns the IP, otherwise null. + +Please consider following this project's author, +[Charlike Mike Reagent](https://github.com/tunnckoCore), and :star: the project +to show your :heart: and support. + +
+ +[![Code style][codestyle-img]][codestyle-url] +[![CircleCI linux build][linuxbuild-img]][linuxbuild-url] +[![CodeCov coverage status][codecoverage-img]][codecoverage-url] +[![Renovate App Status][renovateapp-img]][renovateapp-url] +[![Make A Pull Request][prs-welcome-img]][prs-welcome-url] +[![Time Since Last Commit][last-commit-img]][last-commit-url] + + + +If you have any _how-to_ kind of questions, please read the [Contributing +Guide][contributing-url] and [Code of Conduct][code_of_conduct-url] documents. +For bugs reports and feature requests, [please create an issue][open-issue-url] +or ping [@tunnckoCore](https://twitter.com/tunnckoCore) at Twitter. + +[![Conventional Commits][ccommits-img]][ccommits-url] +[![Minimum Required Nodejs][nodejs-img]][npmv-url] +[![NPM Downloads Monthly][downloads-monthly-img]][npmv-url] +[![NPM Downloads Total][downloads-total-img]][npmv-url] +[![Share Love Tweet][twitter-share-img]][twitter-share-url] +[![Twitter][twitter-img]][twitter-url] + +Project is [semantically](https://semver.org) versioned & automatically released +from [GitHub Actions](https://github.com/features/actions) with +[Lerna](https://github.com/lerna/lerna). + +[![Become a Patron][patreon-img]][patreon-url] +[![Buy me a Kofi][kofi-img]][kofi-url] +[![PayPal Donation][paypal-img]][paypal-url] +[![Bitcoin Coinbase][bitcoin-img]][bitcoin-url] +[![Keybase PGP][keybase-img]][keybase-url] + +| Topic | Contact | +| :--------------------------------------------------------------- | ------------------------------------------------: | +| Any legal or licensing questions, like private or commerical use | ![tunnckocore_legal][tunnckocore_legal] | +| For any critical problems and security reports | ![tunnckocore_security][tunnckocore_security] | +| Consulting, professional support, personal or team training | ![tunnckocore_consulting][tunnckocore_consulting] | +| For any questions about Open Source, partnerships and sponsoring | ![tunnckocore_opensource][tunnckocore_opensource] | + + + +## Table of Contents + +- [Install](#install) +- [API](#api) + * [ipFilter](#ipfilter) +- [See Also](#see-also) +- [Contributing](#contributing) + * [Guides and Community](#guides-and-community) + * [Support the project](#support-the-project) +- [Contributors](#contributors) +- [License](#license) + +_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_ + +## Install + +This project requires [**Node.js**](https://nodejs.org) **>=10.13** +_(see +[Support & Release Policy](https://github.com/tunnckoCoreLabs/support-release-policy))_. +Install it using [**yarn**](https://yarnpkg.com) or +[**npm**](https://npmjs.com).
_We highly recommend to use Yarn when you +think to contribute to this project._ + +```bash +$ yarn add ip-filter +``` + +## API + + + +_Generated using [jest-runner-docs](https://ghub.now.sh/jest-runner-docs)._ + +### [ipFilter](./src/index.js#L44) + +Filter `ip` against glob `patterns`, using [micromatch][] under the hood, +so `options` are passed to it. + + + +#### Signature + +```ts +function(ip, patterns, options) +``` + + + +#### Params + +- `ip` **{string}** - Accepts only valid IPs by default +- `patterns` **{string|array}** - Basically everything that [micromatch][]'s second argument can accept. +- `options` **{object}** - Pass `strict: false` if want to validate non-ip values, + options are also passed to [micromatch][]. +- `returns` **{string}** - a `string` or `null` If not match returns `null`, otherwise the passed `ip` as string. + + + +#### Examples + +```js +const ipFilter = require('ip-filter'); + +console.log(ipFilter('123.77.34.89', '123.??.34.8*')); // => '123.77.34.89' +console.log(ipFilter('123.222.34.88', '123.??.34.8*')); // => null +console.log(ipFilter('123.222.33.1', ['123.*.34.*', '*.222.33.*'])); // => '123.222.33.1' + +// should notice the difference +console.log(ipFilter('123.222.34.88', ['123.*.34.*', '!123.222.**'])); +// => null +console.log(ipFilter('123.222.34.88', ['123.*.34.*', '!123.222.*'])); +// => '123.222.34.88' +``` + + + +#### Examples + +```js +const ipFilter = require('ip-filter'); +// +// NON-STRICT mode +// + +const res = ipFilter('x-koaip', ['*-koaip', '!foo-koa*'], { strict: false }); +console.log(res); // => 'x-koaip' + +const res = ipFilter('x-koa.foo', ['*-koa.*', '!foo-koa.*'], { strict: false }); +console.log(res); // => 'x-koa.foo' +``` + + + +**[back to top](#readme)** + +## See Also + +Some of these projects are used here or were inspiration for this one, others +are just related. So, thanks for your existance! + +- [ip-regex](https://www.npmjs.com/package/ip-regex): Regular expression for matching IP addresses (IPv4 & IPv6) | [homepage](https://github.com/sindresorhus/ip-regex#readme "Regular expression for matching IP addresses (IPv4 & IPv6)") +- [is-match-ip](https://www.npmjs.com/package/is-match-ip): Matching IPs using [micromatch][] and [ip-filter][] - glob patterns, RegExp… [more](https://github.com/tunnckocore/is-match-ip#readme) | [homepage](https://github.com/tunnckocore/is-match-ip#readme "Matching IPs using [micromatch][] and [ip-filter][] - glob patterns, RegExp, string or array of globs. Returns matcher function.") +- [is-match](https://www.npmjs.com/package/is-match): Create a matching function from a glob pattern, regex, string… [more](https://github.com/jonschlinkert/is-match) | [homepage](https://github.com/jonschlinkert/is-match "Create a matching function from a glob pattern, regex, string, array, object or function.") +- [koa-ip-filter](https://www.npmjs.com/package/koa-ip-filter): Middleware for [koa][] that filters IPs against glob patterns, RegExp… [more](https://github.com/tunnckocore/koa-ip-filter#readme) | [homepage](https://github.com/tunnckocore/koa-ip-filter#readme "Middleware for [koa][] that filters IPs against glob patterns, RegExp, string or array of globs. Support custom `403 Forbidden` message and custom ID.") +- [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A replacement and faster alternative to… [more](https://github.com/micromatch/micromatch) | [homepage](https://github.com/micromatch/micromatch "Glob matching for javascript/node.js. A replacement and faster alternative to minimatch and multimatch.") +- [to-file-path](https://www.npmjs.com/package/to-file-path): Create a filepath from an object path (dot notation), list… [more](https://github.com/tunnckocore/to-file-path#readme) | [homepage](https://github.com/tunnckocore/to-file-path#readme "Create a filepath from an object path (dot notation), list of arguments, array, number or Arguments object.") + +**[back to top](#readme)** + +## Contributing + +### Guides and Community + +Please read the [Contributing Guide][contributing-url] and [Code of +Conduct][code_of_conduct-url] documents for advices. + +For bug reports and feature requests, please join our [community][community-url] +forum and open a thread there with prefixing the title of the thread with the +name of the project if there's no separate channel for it. + +Consider reading the +[Support and Release Policy](https://github.com/tunnckoCoreLabs/support-release-policy) +guide if you are interested in what are the supported Node.js versions and how +we proceed. In short, we support latest two even-numbered Node.js release lines. + +### Support the project + +[Become a Partner or Sponsor?][kofi-url] :dollar: Check the **OpenSource** +Commision (tier). :tada: You can get your company logo, link & name on this +file. It's also rendered on package's page in [npmjs.com][npmv-url] and +[yarnpkg.com](https://yarnpkg.com/en/package/ip-filter) sites too! :rocket: + +Not financial support? Okey! +[Pull requests](https://github.com/tunnckoCoreLabs/contributing#opening-a-pull-request), +stars and all kind of +[contributions](https://opensource.guide/how-to-contribute/#what-it-means-to-contribute) +are always welcome. :sparkles: + +## Contributors + +This project follows the +[all-contributors](https://github.com/all-contributors/all-contributors) +specification. Contributions of any kind are welcome! + +Thanks goes to these wonderful people +([emoji key](https://allcontributors.org/docs/en/emoji-key)), consider showing +your [support](#support-the-project) to them: + + + + + + + + +

Charlike Mike Reagent

🚇 💻 📖 🤔 🚧 ⚠️
+ + + + + + +**[back to top](#readme)** + +## License + +Copyright (c) 2015-present, +[Charlike Mike Reagent](https://tunnckocore.com) `` +& [contributors](#wonderful-contributors).
Released under the +[MPL-2.0 License][license-url]. + + + + + +[contributing-url]: https://github.com/tunnckoCore/opensource/blob/master/CONTRIBUTING.md +[code_of_conduct-url]: https://github.com/tunnckoCore/opensource/blob/master/CODE_OF_CONDUCT.md + + + +[npmv-url]: https://www.npmjs.com/package/ip-filter +[npmv-img]: https://badgen.net/npm/v/ip-filter?icon=npm&cache=300 + +[license-url]: https://github.com/tunnckoCore/opensource/blob/master/packages/ip-filter/LICENSE +[license-img]: https://badgen.net/npm/license/ip-filter?cache=300 + +[libera-manifesto-url]: https://liberamanifesto.com +[libera-manifesto-img]: https://badgen.net/badge/libera/manifesto/grey + + + +[codecoverage-img]: https://badgen.net/badge/coverage/unknown/grey?icon=codecov&cache=300 + +[codecoverage-url]: https://codecov.io/gh/tunnckoCore/opensource + +[codestyle-url]: https://github.com/airbnb/javascript +[codestyle-img]: https://badgen.net/badge/code%20style/airbnb/ff5a5f?icon=airbnb&cache=300 + +[linuxbuild-url]: https://github.com/tunnckocore/opensource/actions +[linuxbuild-img]: https://badgen.net/github/checks/tunnckoCore/opensource/master?cache=300&label=build&icon=github + +[ccommits-url]: https://conventionalcommits.org/ +[ccommits-img]: https://badgen.net/badge/conventional%20commits/v1.0.0/green?cache=300 + +[standard-release-url]: https://github.com/standard-release/standard-release +[standard-release-img]: https://badgen.net/badge/semantically/released/05c5ff?cache=300 + +[community-img]: https://badgen.net/badge/join/community/7b16ff?cache=300 +[community-url]: https://github.com/tunnckocorehq/community + +[last-commit-img]: https://badgen.net/github/last-commit/tunnckoCore/opensource/master?cache=300 +[last-commit-url]: https://github.com/tunnckoCore/opensource/commits/master + +[nodejs-img]: https://badgen.net/badge/node/>=10.13/green?cache=300 + +[downloads-weekly-img]: https://badgen.net/npm/dw/ip-filter?icon=npm&cache=300 +[downloads-monthly-img]: https://badgen.net/npm/dm/ip-filter?icon=npm&cache=300 +[downloads-total-img]: https://badgen.net/npm/dt/ip-filter?icon=npm&cache=300 + +[renovateapp-url]: https://renovatebot.com +[renovateapp-img]: https://badgen.net/badge/renovate/enabled/green?cache=300 + +[prs-welcome-img]: https://badgen.net/badge/PRs/welcome/green?cache=300 +[prs-welcome-url]: http://makeapullrequest.com + + + +[paypal-url]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=HYJJEZNSGAPGC&source=url +[paypal-img]: https://badgen.net/badge/PayPal/donate/003087?cache=300&icon=https://simpleicons.now.sh/paypal/fff + + + +[kofi-url]: https://ko-fi.com/tunnckoCore +[kofi-img]: https://badgen.net/badge/Buy%20me/a%20coffee/29abe0c2?cache=300&icon=https://rawcdn.githack.com/tunnckoCore/badgen-icons/f8264c6414e0bec449dd86f2241d50a9b89a1203/icons/kofi.svg + + + +[bitcoin-url]: https://www.blockchain.com/btc/payment_request?address=3QNHKun1K1SUui1b4Z3KEGPPsWC1TgtnqA&message=Open+Source+Software&amount_local=10¤cy=USD +[bitcoin-img]: https://badgen.net/badge/Bitcoin%20tip/3QNHKun...b4Z3KEGPPsWC1TgtnqA/yellow?cache=300&icon=https://simpleicons.now.sh/bitcoin/fff +[keybase-url]: https://keybase.io/tunnckoCore +[keybase-img]: https://badgen.net/keybase/pgp/tunnckoCore?cache=300 +[twitter-url]: https://twitter.com/tunnckoCore +[twitter-img]: https://badgen.net/twitter/follow/tunnckoCore?icon=twitter&color=1da1f2&cache=300 +[patreon-url]: https://www.patreon.com/bePatron?u=5579781 +[patreon-img]: https://badgen.net/badge/Become/a%20patron/F96854?icon=patreon + + + +[patreon-sponsor-img]: https://badgen.net/badge/become/a%20sponsor/F96854?icon=patreon +[twitter-share-url]: https://twitter.com/intent/tweet?text=https://ghub.now.sh/ip-filter&via=tunnckoCore +[twitter-share-img]: https://badgen.net/badge/twitter/share/1da1f2?icon=twitter +[open-issue-url]: https://github.com/tunnckoCore/opensource/issues/new +[tunnckocore_legal]: https://badgen.net/https/liam-badge-daknys6gadky.runkit.sh/com/legal/tunnckocore?label&color=A56016&icon=https://svgshare.com/i/Dt6.svg +[tunnckocore_consulting]: https://badgen.net/https/liam-badge-daknys6gadky.runkit.sh/com/consulting/tunnckocore?label&color=07ba96&icon=https://svgshare.com/i/Dt6.svg +[tunnckocore_security]: https://badgen.net/https/liam-badge-daknys6gadky.runkit.sh/com/security/tunnckocore?label&color=ed1848&icon=https://svgshare.com/i/Dt6.svg +[tunnckocore_opensource]: https://badgen.net/https/liam-badge-daknys6gadky.runkit.sh/com/opensource/tunnckocore?label&color=ff7a2f&icon=https://svgshare.com/i/Dt6.svg +[tunnckocore_newsletter]: https://badgen.net/https/liam-badge-daknys6gadky.runkit.sh/com/newsletter/tunnckocore?label&color=5199FF&icon=https://svgshare.com/i/Dt6.svg + + + +[ip-filter]: https://github.com/tunnckocore/ip-filter +[is-match]: https://github.com/jonschlinkert/is-match +[koa]: https://github.com/koajs/koa +[micromatch]: https://github.com/micromatch/micromatch \ No newline at end of file diff --git a/@packages/ip-filter/docs/src/index.md b/@packages/ip-filter/docs/src/index.md new file mode 100644 index 00000000..912e4591 --- /dev/null +++ b/@packages/ip-filter/docs/src/index.md @@ -0,0 +1,66 @@ + + +_Generated using [jest-runner-docs](https://ghub.now.sh/jest-runner-docs)._ + +### [ipFilter](./src/index.js#L44) + +Filter `ip` against glob `patterns`, using [micromatch][] under the hood, +so `options` are passed to it. + + + +#### Signature + +```ts +function(ip, patterns, options) +``` + + + +#### Params + +- `ip` **{string}** - Accepts only valid IPs by default +- `patterns` **{string|array}** - Basically everything that [micromatch][]'s second argument can accept. +- `options` **{object}** - Pass `strict: false` if want to validate non-ip values, + options are also passed to [micromatch][]. +- `returns` **{string}** - a `string` or `null` If not match returns `null`, otherwise the passed `ip` as string. + + + + + +#### Examples + +```js +const ipFilter = require('ip-filter'); + +console.log(ipFilter('123.77.34.89', '123.??.34.8*')); // => '123.77.34.89' +console.log(ipFilter('123.222.34.88', '123.??.34.8*')); // => null +console.log(ipFilter('123.222.33.1', ['123.*.34.*', '*.222.33.*'])); // => '123.222.33.1' + +// should notice the difference +console.log(ipFilter('123.222.34.88', ['123.*.34.*', '!123.222.**'])); +// => null +console.log(ipFilter('123.222.34.88', ['123.*.34.*', '!123.222.*'])); +// => '123.222.34.88' +``` + + + + + +#### Examples + +```js +const ipFilter = require('ip-filter'); +// +// NON-STRICT mode +// + +const res = ipFilter('x-koaip', ['*-koaip', '!foo-koa*'], { strict: false }); +console.log(res); // => 'x-koaip' + +const res = ipFilter('x-koa.foo', ['*-koa.*', '!foo-koa.*'], { strict: false }); +console.log(res); // => 'x-koa.foo' +``` + diff --git a/@packages/ip-filter/package.json b/@packages/ip-filter/package.json new file mode 100644 index 00000000..1ed048af --- /dev/null +++ b/@packages/ip-filter/package.json @@ -0,0 +1,108 @@ +{ + "name": "ip-filter", + "version": "2.0.0", + "licenseStart": 2015, + "license": "MPL-2.0", + "description": "Validates valid IPs (IPv4 and IPv6) using [micromatch][] - glob patterns, RegExp, string or array of globs. If match returns the IP, otherwise null.", + "author": "Charlike Mike Reagent (https://tunnckocore.com)", + "homepage": "https://tunnckocore.com/opensource", + "funding": [ + "https://github.com/sponsors/tunnckoCore", + "https://ko-fi.com/tunnckoCore/commissions", + "https://patreon.com/tunnckoCore" + ], + "repository": { + "type": "git", + "url": "https://github.com/tunnckoCore/opensource.git", + "directory": "@packages/ip-filter" + }, + "main": "src/index.js", + "files": [ + "src" + ], + "engines": { + "node": ">=10.13" + }, + "publishConfig": { + "access": "public", + "tag": "latest" + }, + "scripts": {}, + "dependencies": { + "ip-regex": "^4.1.0", + "micromatch": "^4.0.2", + "to-file-path": "^1.0.0" + }, + "devDependencies": {}, + "jest": { + "coverageThreshold": { + "src/**/*.js": { + "statements": 100, + "branches": 100, + "functions": 100, + "lines": 100 + } + } + }, + "cov": { + "color": "grey" + }, + "keywords": [ + "array", + "check", + "development", + "dx", + "filter", + "glob", + "hela", + "ip", + "ip-filter", + "ips", + "ipv", + "ismatch", + "match", + "matcher", + "micro", + "micromatch", + "modern", + "patterns", + "regexp", + "string", + "tunnckocore-oss", + "tunnckocorehq", + "valid", + "validate" + ], + "verb": { + "readme": "../../readme-template.md", + "run": true, + "toc": { + "render": true, + "method": "preWrite", + "maxdepth": 3 + }, + "layout": "empty", + "tasks": [ + "readme" + ], + "lint": { + "reflinks": true + }, + "related": { + "list": [ + "to-file-path", + "ip-regex", + "is-match", + "micromatch", + "koa-ip-filter", + "is-match-ip" + ] + }, + "reflinks": [ + "ip-filter", + "is-match", + "koa", + "micromatch" + ] + } +} diff --git a/@packages/ip-filter/src/index.js b/@packages/ip-filter/src/index.js new file mode 100644 index 00000000..bb1889d9 --- /dev/null +++ b/@packages/ip-filter/src/index.js @@ -0,0 +1,66 @@ +'use strict'; + +const ipRegex = require('ip-regex'); +const micromatch = require('micromatch'); +const toPath = require('to-file-path'); + +/** + * Filter `ip` against glob `patterns`, using [micromatch][] under the hood, + * so `options` are passed to it. + * + * @example + * const ipFilter = require('ip-filter'); + * + * console.log(ipFilter('123.77.34.89', '123.??.34.8*')); // => '123.77.34.89' + * console.log(ipFilter('123.222.34.88', '123.??.34.8*')); // => null + * console.log(ipFilter('123.222.33.1', ['123.*.34.*', '*.222.33.*'])); // => '123.222.33.1' + * + * // should notice the difference + * console.log(ipFilter('123.222.34.88', ['123.*.34.*', '!123.222.**'])); + * // => null + * console.log(ipFilter('123.222.34.88', ['123.*.34.*', '!123.222.*'])); + * // => '123.222.34.88' + * + * @example + * const ipFilter = require('ip-filter'); + * // + * // NON-STRICT mode + * // + * + * const res = ipFilter('x-koaip', ['*-koaip', '!foo-koa*'], { strict: false }); + * console.log(res); // => 'x-koaip' + * + * const res = ipFilter('x-koa.foo', ['*-koa.*', '!foo-koa.*'], { strict: false }); + * console.log(res); // => 'x-koa.foo' + * + * @name ipFilter + * @param {string} `ip` Accepts only valid IPs by default + * @param {string|array} `patterns` Basically everything that [micromatch][]'s second argument can accept. + * @param {object} `options` Pass `strict: false` if want to validate non-ip values, + * options are also passed to [micromatch][]. + * @return {string} a `string` or `null` - If not match returns `null`, otherwise the passed `ip` as string. + * @api public + */ +module.exports = function ipFilter(ip, patterns, options) { + if (typeof ip !== 'string') { + throw new TypeError('ip-filter: expect `ip` to be a string'); + } + + const opts = { ...options }; + opts.strict = typeof opts.strict === 'boolean' ? opts.strict : true; + + if (opts.strict && !ipRegex().test(ip)) { + throw new Error('ip-filter: expect only valid IPs when `opts.strict` mode'); + } + + const id = opts.strict ? tofp(ip) : ip; + const globs = opts.strict ? tofp(patterns) : patterns; + + const matches = micromatch(id, globs, opts); + return matches.length > 0 ? ip : null; +}; + +function tofp(val) { + const value = typeof val === 'string' ? toPath(val) : val; + return Array.isArray(value) ? value.map((p) => toPath(p)) : value; +} diff --git a/@packages/ip-filter/test/index.js b/@packages/ip-filter/test/index.js new file mode 100644 index 00000000..c1cd7e36 --- /dev/null +++ b/@packages/ip-filter/test/index.js @@ -0,0 +1,67 @@ +/*! + * ip-filter + * + * Copyright (c) 2015-2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk) + * Released under the MIT license. + */ + +'use strict'; + +const ipFilter = require('../src/index'); + +test('should throw TypeError if `ip` not a string', () => { + expect(() => ipFilter(123)).toThrow(TypeError); + expect(() => ipFilter(123)).toThrow(/expect `ip` to be a string/); +}); + +test('should throw Error if not valid IPv4 or IPv6 ip is given', () => { + // eslint-disable-next-line unicorn/consistent-function-scoping + function fixture() { + ipFilter('foo.bar.baz.123', './**/.glob*'); + } + expect(fixture).toThrow(Error); + expect(fixture).toThrow(/expect only valid IPs when `opts.strict` mode/); +}); + +test('should return IP if match string glob pattern', () => { + const actual1 = ipFilter('123.222.34.88', '123.??.34.8*'); // => null + const actual2 = ipFilter('123.77.34.89', '123.??.34.8*'); // => '123.77.34.89' + + expect(actual1).toBeNull(); + expect(actual2).toStrictEqual('123.77.34.89'); +}); + +test('should return IP if match one of array glob patterns', () => { + const actual1 = ipFilter('123.222.34.88', ['123.*.34.*', '!123.222.**']); // => null + const actual2 = ipFilter('123.222.34.88', ['123.*.34.*', '!123.222.*']); // => '123.222.34.88' + const actual3 = ipFilter('123.222.33.1', ['123.*.34.*', '*.222.33.*']); // => '123.222.33.1' + + expect(actual1).toBeNull(); + expect(actual2).toStrictEqual('123.222.34.88'); + expect(actual3).toStrictEqual('123.222.33.1'); +}); + +test('should return null if not match', () => { + const actual = ipFilter('123.222.34.89', ['123.???.34.8*', '!123.222.34.89']); + const expected = null; + + expect(actual).toStrictEqual(expected); +}); + +test('should support no strict mode to compare other than IPs', () => { + const actual1 = ipFilter('x-koaip', ['*-koaip', '!foo-koa'], { + strict: false, + }); + const actual2 = ipFilter('foo.bar.baz.123', 'foo.bar.**', { strict: false }); + const expected = 'x-koaip'; + + expect(actual1).toStrictEqual(expected); + expect(actual2).toStrictEqual('foo.bar.baz.123'); +}); + +test('should work in non strict mode', () => { + const actual = ipFilter('x-koaip.foo', ['*-koaip.*', '!foo-koa.*'], { + strict: false, + }); + expect(actual).toStrictEqual('x-koaip.foo'); +}); diff --git a/readme-template.md b/readme-template.md index 983964ce..50a73571 100644 --- a/readme-template.md +++ b/readme-template.md @@ -214,7 +214,7 @@ Copyright (c) {%= licenseStart %}-present, [patreon-sponsor-img]: https://badgen.net/badge/become/a%20sponsor/F96854?icon=patreon -[twitter-share-url]: https://twitter.com/intent/tweet?text=https://github.com/tunnckoCore/opensource/tree/master{%=repository.directory %}&via=tunnckoCore +[twitter-share-url]: https://twitter.com/intent/tweet?text=https://ghub.now.sh/{%= name %}&via=tunnckoCore [twitter-share-img]: https://badgen.net/badge/twitter/share/1da1f2?icon=twitter [open-issue-url]: https://github.com/tunnckoCore/opensource/issues/new [tunnckocore_legal]: https://badgen.net/https/liam-badge-daknys6gadky.runkit.sh/com/legal/tunnckocore?label&color=A56016&icon=https://svgshare.com/i/Dt6.svg diff --git a/yarn.lock b/yarn.lock index 892b85f2..955b42ce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8363,11 +8363,6 @@ globals@^12.1.0: dependencies: type-fest "^0.8.1" -globalyzer@^0.1.0: - version "0.1.4" - resolved "https://registry.yarnpkg.com/globalyzer/-/globalyzer-0.1.4.tgz#bc8e273afe1ac7c24eea8def5b802340c5cc534f" - integrity sha512-LeguVWaxgHN0MNbWC6YljNMzHkrCny9fzjmEUdnF1kQ7wATFD1RHFRqA1qxaX2tgxGENlcxjOflopBwj3YZiXA== - globby@10.0.1: version "10.0.1" resolved "https://registry.yarnpkg.com/globby/-/globby-10.0.1.tgz#4782c34cb75dd683351335c5829cc3420e606b22" @@ -8382,6 +8377,18 @@ globby@10.0.1: merge2 "^1.2.3" slash "^3.0.0" +globby@^11.0.0: + version "11.0.0" + resolved "https://registry.yarnpkg.com/globby/-/globby-11.0.0.tgz#56fd0e9f0d4f8fb0c456f1ab0dee96e1380bc154" + integrity sha512-iuehFnR3xu5wBBtm4xi0dMe92Ob87ufyu/dHwpDYfbcpYpIbrO5OnS8M1vWvrBhSGEJ3/Ecj7gnX76P8YxpPEg== + dependencies: + array-union "^2.1.0" + dir-glob "^3.0.1" + fast-glob "^3.1.1" + ignore "^5.1.4" + merge2 "^1.3.0" + slash "^3.0.0" + globby@^9.2.0: version "9.2.0" resolved "https://registry.yarnpkg.com/globby/-/globby-9.2.0.tgz#fd029a706c703d29bdd170f4b6db3a3f7a7cb63d" @@ -8396,11 +8403,6 @@ globby@^9.2.0: pify "^4.0.1" slash "^2.0.0" -globrex@^0.1.1: - version "0.1.2" - resolved "https://registry.yarnpkg.com/globrex/-/globrex-0.1.2.tgz#dd5d9ec826232730cd6793a5e33a9302985e6098" - integrity sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg== - got@^6.7.1: version "6.7.1" resolved "https://registry.yarnpkg.com/got/-/got-6.7.1.tgz#240cd05785a9a18e561dc1b44b41c763ef1e8db0" @@ -8893,7 +8895,7 @@ ignore@^4.0.3, ignore@^4.0.6: resolved "https://registry.yarnpkg.com/ignore/-/ignore-4.0.6.tgz#750e3db5862087b4737ebac8207ffd1ef27b25fc" integrity sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg== -ignore@^5.1.1: +ignore@^5.1.1, ignore@^5.1.4: version "5.1.4" resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.4.tgz#84b7b3dbe64552b6ef0eca99f6743dbec6d97adf" integrity sha512-MzbUSahkTW1u7JpKKjY7LCARd1fU5W2rLdxlM4kdkayuCwZImjkpluF9CM1aLewYJguPDqewLam18Y6AU69A8A== @@ -9103,6 +9105,11 @@ ip-regex@^2.1.0: resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-2.1.0.tgz#fa78bf5d2e6913c911ce9f819ee5146bb6d844e9" integrity sha1-+ni/XS5pE8kRzp+BnuUUa7bYROk= +ip-regex@^4.1.0: + version "4.1.0" + resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-4.1.0.tgz#5ad62f685a14edb421abebc2fff8db94df67b455" + integrity sha512-pKnZpbgCTfH/1NLIlOduP/V+WRXzC2MOz3Qo8xmxk8C5GudJLgK5QyLVXOSWy3ParAH7Eemurl3xjv/WXYFvMA== + ip@1.1.5, ip@^1.1.5: version "1.1.5" resolved "https://registry.yarnpkg.com/ip/-/ip-1.1.5.tgz#bdded70114290828c0a039e72ef25f5aaec4354a" @@ -11501,7 +11508,7 @@ media-typer@0.3.0: resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g= -memoize-fs@^2.0.0: +memoize-fs@^2.0.0, memoize-fs@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/memoize-fs/-/memoize-fs-2.1.0.tgz#a819ba8688cb9cb6b3223c068109dd3de6835a72" integrity sha512-vn8tNMeUsu1IopbrjEZ5RFzKFLvRu5d7DI+qsrVQNE2Gv3szQgMB1N3tT8Mzv1wPrAjbN/Dg+pnZTyKLCLem5g== @@ -15953,14 +15960,6 @@ timed-out@^4.0.0: resolved "https://registry.yarnpkg.com/timed-out/-/timed-out-4.0.1.tgz#f32eacac5a175bea25d7fab565ab3ed8741ef56f" integrity sha1-8y6srFoXW+ol1/q1Zas+2HQe9W8= -tiny-glob@^0.2.6: - version "0.2.6" - resolved "https://registry.yarnpkg.com/tiny-glob/-/tiny-glob-0.2.6.tgz#9e056e169d9788fe8a734dfa1ff02e9b92ed7eda" - integrity sha512-A7ewMqPu1B5PWwC3m7KVgAu96Ch5LA0w4SnEN/LbDREj/gAD0nPWboRbn8YoP9ISZXqeNAlMvKSKoEuhcfK3Pw== - dependencies: - globalyzer "^0.1.0" - globrex "^0.1.1" - titlecase@^1.1.2: version "1.1.3" resolved "https://registry.yarnpkg.com/titlecase/-/titlecase-1.1.3.tgz#fc6d65ff582b0602410768ef1a09b70506313dc3" @@ -16005,6 +16004,16 @@ to-fast-properties@^2.0.0: resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e" integrity sha1-3F5pjL0HkmW8c+A3doGk5Og/YW4= +to-file-path@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/to-file-path/-/to-file-path-1.0.0.tgz#4c55a9f2d2755d455d2258314a3411e19106d1d4" + integrity sha1-TFWp8tJ1XUVdIlgxSjQR4ZEG0dQ= + dependencies: + arr-map "^2.0.0" + is-arguments "^1.0.2" + isarray "^1.0.0" + lazy-cache "^1.0.3" + to-file@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/to-file/-/to-file-0.2.0.tgz#236c6c088065e570defbd15cf4b4e565be46ea93"