|
| 1 | +var types = ['Command', 'Special', 'System', 'Oplog', 'Normal', 'Conf']; |
| 2 | + |
| 3 | +// eslint-disable-next-line complexity |
| 4 | +function NS(ns) { |
| 5 | + if (!(this instanceof NS)) { |
| 6 | + return new NS(ns); |
| 7 | + } |
| 8 | + |
| 9 | + this.ns = ns; |
| 10 | + this.dotIndex = ns.indexOf('.'); |
| 11 | + if (this.dotIndex === -1) { |
| 12 | + this.database = ns; |
| 13 | + this.collection = ''; |
| 14 | + } else { |
| 15 | + this.database = ns.slice(0, this.dotIndex); |
| 16 | + this.collection = ns.slice(this.dotIndex + 1); |
| 17 | + } |
| 18 | + |
| 19 | + this.system = /^(?:system(?!\.profile$).*|enxcol_)\./.test(this.collection); |
| 20 | + |
| 21 | + this.oplog = /local\.oplog\.(\$main|rs)/.test(ns); |
| 22 | + |
| 23 | + this.command = |
| 24 | + this.collection === '$cmd' || this.collection.indexOf('$cmd.sys') === 0; |
| 25 | + this.special = |
| 26 | + this.oplog || this.command || this.system || this.database === 'config' || /^__mdb_internal_\w/.test(this.database); |
| 27 | + |
| 28 | + this.specialish = |
| 29 | + this.special || ['local', 'admin'].indexOf(this.database) > -1; |
| 30 | + |
| 31 | + this.normal = this.oplog || this.ns.indexOf('$') === -1; |
| 32 | + |
| 33 | + /** |
| 34 | + * @note (imlucas) The following are not valid on windows: |
| 35 | + * `*<>:|?` |
| 36 | + */ |
| 37 | + this.validDatabaseName = |
| 38 | + new RegExp('^[^\\\\/". ]*$').test(this.database) && |
| 39 | + this.database.length <= NS.MAX_DATABASE_NAME_LENGTH; |
| 40 | + this.validCollectionName = |
| 41 | + this.collection.length > 0 && |
| 42 | + (this.oplog || /^[^\0\$]*$/.test(this.collection)); |
| 43 | + |
| 44 | + this.databaseHash = 7; |
| 45 | + this.ns.split('').every( |
| 46 | + function(c, i) { |
| 47 | + if (c === '.') { |
| 48 | + return false; |
| 49 | + } |
| 50 | + this.databaseHash += 11 * this.ns.charCodeAt(i); |
| 51 | + this.databaseHash *= 3; |
| 52 | + return true; |
| 53 | + }.bind(this) |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +NS.prototype.database = ''; |
| 58 | +NS.prototype.databaseHash = 0; |
| 59 | +NS.prototype.collection = ''; |
| 60 | + |
| 61 | +NS.prototype.command = false; |
| 62 | +NS.prototype.special = false; |
| 63 | +NS.prototype.system = false; |
| 64 | +NS.prototype.oplog = false; |
| 65 | +NS.prototype.normal = false; |
| 66 | +NS.prototype.specialish = false; |
| 67 | + |
| 68 | +types.forEach(function(type) { |
| 69 | + NS.prototype['is' + type] = function() { |
| 70 | + return this[type.toLowerCase()]; |
| 71 | + }; |
| 72 | +}); |
| 73 | + |
| 74 | +NS.prototype.toString = function() { |
| 75 | + return this.ns; |
| 76 | +}; |
| 77 | + |
| 78 | +NS.MAX_DATABASE_NAME_LENGTH = 128; |
| 79 | + |
| 80 | +module.exports = NS; |
| 81 | + |
| 82 | +var ns = NS; |
| 83 | +module.exports.sort = function(namespaces) { |
| 84 | + namespaces.sort(function(a, b) { |
| 85 | + if (ns(a).specialish && ns(b).specialish) { |
| 86 | + return 0; |
| 87 | + } |
| 88 | + if (ns(a).specialish && !ns(b).specialish) { |
| 89 | + return 1; |
| 90 | + } |
| 91 | + if (!ns(a).specialish && ns(b).specialish) { |
| 92 | + return -1; |
| 93 | + } |
| 94 | + return a > b ? 1 : -1; |
| 95 | + }); |
| 96 | + return namespaces; |
| 97 | +}; |
0 commit comments