├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test ├── fixtures ├── 0 │ └── 012 ├── 1 │ └── 123 ├── 2 │ └── 234 ├── 3 │ └── .keep ├── 4 │ └── 456 └── 5 │ └── 567 └── unit.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | .travis.yml 4 | .nyc_output 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | .gitignore 4 | test 5 | .travis.yml 6 | .nyc_output 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | sudo: false 3 | language: node_js 4 | node_js: 5 | - "4" 6 | - "6" 7 | - "8" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nginx-cache 2 | 3 | A Node.js module that recursively scans 4 | the directories and files of an _Nginx_ cache 5 | as efficiently as possible 6 | looking for partial URL key matches 7 | based on a regular expression. 8 | 9 | ## Install 10 | 11 | ```sh 12 | npm install nginx-cache 13 | ``` 14 | 15 | ```sh 16 | yarn add nginx-cache 17 | ``` 18 | 19 | ## Usage example 20 | 21 | Purge all CSS files from an Nginx cache: 22 | 23 | ```javascript 24 | const NginxCache = require('nginx-cache'); 25 | 26 | NginxCache('/var/www/cache') 27 | .find(/.css$/) 28 | .on('match', function(file, url) { 29 | console.log('Attempting to remove cached version of ' + url); 30 | fs.unlink(file, function(err) { 31 | if (err) { 32 | console.log(err.message); 33 | } 34 | }); 35 | }) 36 | .on('error', function(err) { 37 | console.log(err.message); 38 | }) 39 | .on('finish', function() { 40 | console.log('Done'); 41 | }); 42 | ``` 43 | 44 | ## API 45 | 46 | ### NginxCache(directory) 47 | 48 | Constructor to which further methods are chained. 49 | 50 | * `directory` is the path to the _Nginx_ cache, as provided to its `proxy_cache_path` directive. 51 | 52 | ### find(pattern) 53 | 54 | * `pattern` is an instance of a standard JavaScript `RegExp` object. 55 | 56 | ### on(event, listener) 57 | 58 | Register a function to listen for the given event, where: 59 | 60 | * `event` is a String containing the name of the event (see below), and 61 | * `listener` is a function to call when the event is emitted. 62 | 63 | #### Event: 'match' 64 | 65 | The `match` event is emitted when a cache file matching the `pattern` passed to `find()` is found. 66 | 67 | The event attributes are `file, url` where: 68 | 69 | * `file` is the full path to the matching cache file, and 70 | * `url` is the full cache key that matched the pattern. 71 | 72 | #### Event: 'finish' 73 | 74 | The `finish` event is emitted when all cache directories and files have been scanned. 75 | No further events are emitted after this. 76 | 77 | #### Event: 'error' 78 | 79 | The `error` event is emitted when a non-ignorable problem occurs, 80 | e.g. lack of permission to read files in the cache directory. 81 | 82 | #### Event: 'warn' 83 | 84 | The `warn` event is emitted when an ignorable problem occurs, 85 | e.g. the Nginx cache manager deleted a file. 86 | 87 | ## Alternatives 88 | 89 | If you know the complete, original cache key 90 | then the commercial _Plus_ version of _Nginx_ provides a 91 | [proxy_cache_purge](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_purge) directive, 92 | which may be more suitable. 93 | 94 | ## Licence 95 | 96 | Copyright 2015, 2017 Dispatches LLP. 97 | 98 | Licensed under the Apache License, Version 2.0 (the "License"); 99 | you may not use this file except in compliance with the License. 100 | You may obtain a copy of the License at 101 | [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0.html) 102 | 103 | Unless required by applicable law or agreed to in writing, software 104 | distributed under the License is distributed on an "AS IS" BASIS, 105 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 106 | See the License for the specific language governing permissions and 107 | limitations under the License. 108 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const util = require('util'); 6 | const events = require('events'); 7 | 8 | const eachSeries = function (coll, iteratee, callback) { 9 | const len = coll.length; 10 | let index = 0; 11 | const next = function (err) { 12 | if (index === len || err) { 13 | return callback(err); 14 | } 15 | iteratee(coll[index++], next); 16 | }; 17 | next(); 18 | }; 19 | 20 | /* 21 | Constructor - requires path to cache directory 22 | */ 23 | const NginxCache = function (directory) { 24 | if (!(this instanceof NginxCache)) { 25 | return new NginxCache(directory); 26 | } 27 | events.EventEmitter.call(this); 28 | // Store cache directory 29 | if (typeof directory === 'string') { 30 | this.directory = directory; 31 | } else { 32 | throw new Error('Directory required'); 33 | } 34 | return this; 35 | }; 36 | util.inherits(NginxCache, events.EventEmitter); 37 | 38 | /* 39 | Find cache files where the key, usually the URL, matches the RegExp pattern 40 | */ 41 | NginxCache.prototype.find = function (pattern) { 42 | const that = this; 43 | process.nextTick(function () { 44 | if (typeof pattern === 'object' && pattern instanceof RegExp) { 45 | // Reset depth counter 46 | that.depth = 0; 47 | // Start recursion into directory tree 48 | that._findDirectory(that.directory, pattern); 49 | } else { 50 | that.emit('error', new Error('Invalid RegExp')); 51 | that.emit('finish'); 52 | } 53 | }); 54 | return this; 55 | }; 56 | 57 | /* 58 | Look in a directory for cache files 59 | */ 60 | NginxCache.prototype._findDirectory = function (directory, pattern) { 61 | const that = this; 62 | // Increment depth counter 63 | that.depth++; 64 | // Get children 65 | fs.readdir(directory, function (err, files) { 66 | if (err) { 67 | if (err.code === 'EACCES' && directory === that.directory) { 68 | that.emit('error', new Error('Permission denied to read files in root of cache directory ' + directory)); 69 | } else { 70 | that.emit('warn', err); 71 | } 72 | } else { 73 | // Loop over children, limiting the number of files open at once 74 | eachSeries(files, function (file, done) { 75 | const child = path.join(directory, file); 76 | fs.stat(child, function (err, stat) { 77 | if (err) { 78 | that.emit('warn', err); 79 | } else { 80 | if (stat.isFile()) { 81 | that._findFile(child, pattern); 82 | } else if (stat.isDirectory()) { 83 | that._findDirectory(child, pattern); 84 | } 85 | } 86 | done(); 87 | }); 88 | }, function () { 89 | // Decrement depth counter 90 | that.depth--; 91 | if (that.depth === 0) { 92 | that.emit('finish'); 93 | } 94 | }); 95 | } 96 | }); 97 | }; 98 | 99 | /* 100 | Look in a cache file for pattern 101 | */ 102 | NginxCache.prototype._findFile = function (file, pattern) { 103 | const that = this; 104 | // Increment depth counter 105 | that.depth++; 106 | // Open read-only file descriptor 107 | fs.open(file, 'r', function (err, fd) { 108 | if (err) { 109 | // The Nginx Cache Manager probably deleted the file 110 | that.emit('warn', err); 111 | } else { 112 | // Read first 1024 bytes from file, enough for headers 113 | const buffer = Buffer.alloc(1024); 114 | fs.read(fd, buffer, 0, buffer.length, 0, function (err, bytesRead, data) { 115 | if (err) { 116 | that.emit('warn', err); 117 | } else { 118 | // Extract the cache key from the Buffer 119 | const key = keyFromBuffer(data, bytesRead); 120 | if (key) { 121 | if (pattern.test(key)) { 122 | // Found a match 123 | that.emit('match', file, key); 124 | } 125 | } else { 126 | that.emit('warn', new Error('Could not find headers at start of ' + file)); 127 | } 128 | } 129 | // Close file descriptor 130 | fs.close(fd, function () { }); 131 | // Decrement depth counter 132 | that.depth--; 133 | if (that.depth === 0) { 134 | that.emit('finish'); 135 | } 136 | }); 137 | } 138 | }); 139 | }; 140 | 141 | /* 142 | Seek '\nKEY: {{key}}\n' in a Buffer and extract '{{key}}' 143 | */ 144 | const keyFromBuffer = function (buffer, length) { 145 | let key = null; 146 | // Seek position in Buffer 147 | let pos = 0; 148 | // Seek '\nKEY: ' for start 149 | let keyStart = -1; 150 | while (pos < (length - 7) && keyStart === -1) { 151 | if ( 152 | buffer[pos] === 0x0a && // '\n' 153 | buffer[pos + 1] === 0x4b && // 'K' 154 | buffer[pos + 2] === 0x45 && // 'E' 155 | buffer[pos + 3] === 0x59 && // 'Y' 156 | buffer[pos + 4] === 0x3a && // ':' 157 | buffer[pos + 5] === 0x20 // ' ' 158 | ) { 159 | keyStart = pos + 6; 160 | } 161 | pos++; 162 | } 163 | if (keyStart !== -1) { 164 | // Seek '\n' for end 165 | let keyEnd = -1; 166 | while (pos < length && keyEnd === -1) { 167 | if (buffer[pos] === 0x0a) { 168 | keyEnd = pos; 169 | } 170 | pos++; 171 | } 172 | if (keyEnd !== -1) { 173 | key = buffer.toString('utf8', keyStart, keyEnd); 174 | } 175 | } 176 | return key; 177 | }; 178 | 179 | module.exports = NginxCache; 180 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nginx-cache", 3 | "version": "2.0.0-alpha", 4 | "author": "Lovell Fuller ", 5 | "description": "Find files in an Nginx cache based on partial URL keys", 6 | "scripts": { 7 | "test": "semistandard && nyc --reporter=lcov mocha" 8 | }, 9 | "main": "index.js", 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/lovell/nginx-cache" 13 | }, 14 | "keywords": [ 15 | "nginx", 16 | "cache", 17 | "manager", 18 | "find", 19 | "purge" 20 | ], 21 | "devDependencies": { 22 | "mocha": "^4.0.1", 23 | "nyc": "^11.0.2", 24 | "semistandard": "^11.0.0" 25 | }, 26 | "license": "Apache-2.0", 27 | "engines": { 28 | "node": ">=4.5.0" 29 | }, 30 | "semistandard": { 31 | "env": [ 32 | "mocha" 33 | ] 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/fixtures/0/012: -------------------------------------------------------------------------------- 1 | Should work OK 2 | KEY: http://test.com/path/120/image123.jpg 3 | Other: header-values 4 | 5 | DATA 6 | -------------------------------------------------------------------------------- /test/fixtures/1/123: -------------------------------------------------------------------------------- 1 | Should work OK 2 | KEY: http://test.com/path/240/image123.jpg 3 | Other: header-values 4 | 5 | DATA 6 | -------------------------------------------------------------------------------- /test/fixtures/2/234: -------------------------------------------------------------------------------- 1 | Should work OK 2 | KEY: https://test.com/path/120/image.jpg 3 | Other: header-values 4 | 5 | DATA 6 | -------------------------------------------------------------------------------- /test/fixtures/3/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lovell/nginx-cache/5eed0db45d49b05541e64c1c3620b024b4b4cbee/test/fixtures/3/.keep -------------------------------------------------------------------------------- /test/fixtures/4/456: -------------------------------------------------------------------------------- 1 | http://test.com/missing/KEY/prefix/so/should/not/match/120/image123.jpg 2 | -------------------------------------------------------------------------------- /test/fixtures/5/567: -------------------------------------------------------------------------------- 1 | No newline after key 2 | KEY: http://test.com/path/480/image123.jpg -------------------------------------------------------------------------------- /test/unit.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path'); 4 | var assert = require('assert'); 5 | 6 | var nginxCache = require('../'); 7 | 8 | var cacheDir = path.join(__dirname, 'fixtures'); 9 | 10 | describe('Constructor', function () { 11 | it('Missing directory', function () { 12 | var isValid = true; 13 | try { 14 | nginxCache(); 15 | } catch (err) { 16 | assert.strictEqual('object', typeof err); 17 | assert.strictEqual(true, err instanceof Error); 18 | isValid = false; 19 | } 20 | assert.strictEqual(false, isValid); 21 | }); 22 | 23 | it('Invalid directory', function () { 24 | var isValid = true; 25 | try { 26 | nginxCache(123); 27 | } catch (err) { 28 | assert.strictEqual('object', typeof err); 29 | assert.strictEqual(true, err instanceof Error); 30 | isValid = false; 31 | } 32 | assert.strictEqual(false, isValid); 33 | }); 34 | }); 35 | 36 | describe('Find', function () { 37 | it('Missing pattern', function (done) { 38 | var isValid = true; 39 | nginxCache(cacheDir) 40 | .find() 41 | .on('error', function (err) { 42 | assert.strictEqual('object', typeof err); 43 | assert.strictEqual(true, err instanceof Error); 44 | isValid = false; 45 | }) 46 | .on('finish', function () { 47 | assert.strictEqual(false, isValid); 48 | done(); 49 | }); 50 | }); 51 | 52 | it('Invalid pattern', function (done) { 53 | var isValid = true; 54 | nginxCache(cacheDir) 55 | .find('name') 56 | .on('error', function (err) { 57 | assert.strictEqual('object', typeof err); 58 | assert.strictEqual(true, err instanceof Error); 59 | isValid = false; 60 | }) 61 | .on('finish', function () { 62 | assert.strictEqual(false, isValid); 63 | done(); 64 | }); 65 | }); 66 | 67 | it('Valid - URL starts with https://', function (done) { 68 | var hadError = false; 69 | var hadWarning = false; 70 | var matchCount = 0; 71 | nginxCache(cacheDir) 72 | .find(new RegExp('^https://')) 73 | .on('match', function (file, url) { 74 | matchCount++; 75 | }) 76 | .on('error', function (err) { 77 | assert.strictEqual('object', typeof err); 78 | assert.strictEqual(true, err instanceof Error); 79 | hadError = true; 80 | }) 81 | .on('warn', function (err) { 82 | assert.strictEqual('object', typeof err); 83 | assert.strictEqual(true, err instanceof Error); 84 | hadWarning = true; 85 | }) 86 | .on('finish', function () { 87 | assert.strictEqual(false, hadError); 88 | assert.strictEqual(true, hadWarning); 89 | assert.strictEqual(1, matchCount); 90 | done(); 91 | }); 92 | }); 93 | 94 | it('Valid - URL contains /120/', function (done) { 95 | var hadError = false; 96 | var hadWarning = false; 97 | var matchCount = 0; 98 | nginxCache(cacheDir) 99 | .find(new RegExp('/120/')) 100 | .on('match', function (file, url) { 101 | matchCount++; 102 | }) 103 | .on('error', function (err) { 104 | assert.strictEqual('object', typeof err); 105 | assert.strictEqual(true, err instanceof Error); 106 | hadError = true; 107 | }) 108 | .on('warn', function (err) { 109 | assert.strictEqual('object', typeof err); 110 | assert.strictEqual(true, err instanceof Error); 111 | hadWarning = true; 112 | }) 113 | .on('finish', function () { 114 | assert.strictEqual(false, hadError); 115 | assert.strictEqual(true, hadWarning); 116 | assert.strictEqual(2, matchCount); 117 | done(); 118 | }); 119 | }); 120 | 121 | it('Valid - URL ends with image123.jpg', function (done) { 122 | var hadError = false; 123 | var hadWarning = false; 124 | var matchCount = 0; 125 | nginxCache(cacheDir) 126 | .find(/image123.jpg$/) 127 | .on('match', function (file, url) { 128 | matchCount++; 129 | }) 130 | .on('error', function (err) { 131 | assert.strictEqual('object', typeof err); 132 | assert.strictEqual(true, err instanceof Error); 133 | hadError = true; 134 | }) 135 | .on('warn', function (err) { 136 | assert.strictEqual('object', typeof err); 137 | assert.strictEqual(true, err instanceof Error); 138 | hadWarning = true; 139 | }) 140 | .on('finish', function () { 141 | assert.strictEqual(false, hadError); 142 | assert.strictEqual(true, hadWarning); 143 | assert.strictEqual(2, matchCount); 144 | done(); 145 | }); 146 | }); 147 | }); 148 | --------------------------------------------------------------------------------