Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,058 changes: 5,999 additions & 1,059 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions packages/ns/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"mongodb-js/node"
]
}
5 changes: 5 additions & 0 deletions packages/ns/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
node_modules/
.dist/
.build/
npm-debug.log
3 changes: 3 additions & 0 deletions packages/ns/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.travis.yml
test.js
.eslintrc
20 changes: 20 additions & 0 deletions packages/ns/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Lucas Hrabovsky <[email protected]>

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.
35 changes: 35 additions & 0 deletions packages/ns/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# mongodb-ns

Handle dem namespaces like the kernel do.

```
var ns = require('mongodb-ns');

var bacon = ns('canadian-things.songs-aboot-bacon');
console.log(bacon.toString() + '\n', bacon);
```

will output

```
canadian-things.songs-aboot-bacon
NS {
ns: 'canadian-things.songs-aboot-bacon',
dotIndex: 15,
database: 'canadian-things',
collection: 'songs-aboot-bacon',
system: false,
oplog: false,
command: false,
special: false,
specialish: false,
normal: true,
validDatabaseName: true,
validCollectionName: true,
databaseHash: 23620443216 }
```


## license

MIT
31 changes: 31 additions & 0 deletions packages/ns/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class NS {
ns: string;
dotIndex: number;
database: string;
collection: string;
system: boolean;
isSystem(): boolean;
oplog: boolean;
isOplog(): boolean;
command: boolean;
isCommand(): boolean;
special: boolean;
isSpecial(): boolean;
specialish: boolean;
normal: boolean;
isNormal(): boolean;
validDatabaseName: boolean;
validCollectionName: boolean;
databaseHash: number;
toString(): string;
static sort(namespaces: (string | NS)[]): typeof namespaces;
static MAX_DATABASE_NAME_LENGTH: number;
// Assigned in the constructor, but will always be undefined
// isConf: undefined;
}

declare const toNS: ((namespace: string | NS) => NS) & {
sort: typeof NS['sort'];
};

export default toNS;
97 changes: 97 additions & 0 deletions packages/ns/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
var types = ['Command', 'Special', 'System', 'Oplog', 'Normal', 'Conf'];

// eslint-disable-next-line complexity
function NS(ns) {
if (!(this instanceof NS)) {
return new NS(ns);
}

this.ns = ns;
this.dotIndex = ns.indexOf('.');
if (this.dotIndex === -1) {
this.database = ns;
this.collection = '';
} else {
this.database = ns.slice(0, this.dotIndex);
this.collection = ns.slice(this.dotIndex + 1);
}

this.system = /^(?:system(?!\.profile$).*|enxcol_)\./.test(this.collection);

this.oplog = /local\.oplog\.(\$main|rs)/.test(ns);

this.command =
this.collection === '$cmd' || this.collection.indexOf('$cmd.sys') === 0;
this.special =
this.oplog || this.command || this.system || this.database === 'config' || /^__mdb_internal_\w/.test(this.database);

this.specialish =
this.special || ['local', 'admin'].indexOf(this.database) > -1;

this.normal = this.oplog || this.ns.indexOf('$') === -1;

/**
* @note (imlucas) The following are not valid on windows:
* `*<>:|?`
*/
this.validDatabaseName =
new RegExp('^[^\\\\/". ]*$').test(this.database) &&
this.database.length <= NS.MAX_DATABASE_NAME_LENGTH;
this.validCollectionName =
this.collection.length > 0 &&
(this.oplog || /^[^\0\$]*$/.test(this.collection));

this.databaseHash = 7;
this.ns.split('').every(
function(c, i) {
if (c === '.') {
return false;
}
this.databaseHash += 11 * this.ns.charCodeAt(i);
this.databaseHash *= 3;
return true;
}.bind(this)
);
}

NS.prototype.database = '';
NS.prototype.databaseHash = 0;
NS.prototype.collection = '';

NS.prototype.command = false;
NS.prototype.special = false;
NS.prototype.system = false;
NS.prototype.oplog = false;
NS.prototype.normal = false;
NS.prototype.specialish = false;

types.forEach(function(type) {
NS.prototype['is' + type] = function() {
return this[type.toLowerCase()];
};
});

NS.prototype.toString = function() {
return this.ns;
};

NS.MAX_DATABASE_NAME_LENGTH = 128;

module.exports = NS;

var ns = NS;
module.exports.sort = function(namespaces) {
namespaces.sort(function(a, b) {
if (ns(a).specialish && ns(b).specialish) {
return 0;
}
if (ns(a).specialish && !ns(b).specialish) {
return 1;
}
if (!ns(a).specialish && ns(b).specialish) {
return -1;
}
return a > b ? 1 : -1;
});
return namespaces;
};
31 changes: 31 additions & 0 deletions packages/ns/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "mongodb-ns",
"version": "2.4.3",
"author": {
"name": "MongoDB Inc",
"email": "[email protected]"
},
"description": "MongoDB namespace parsing and validation",
"main": "./index.js",
"types": "./index.d.ts",
"scripts": {
"test": "mocha",
"check": "mongodb-js-precommit"
},
"homepage": "https://github.com/mongodb-js/devtools-shared",
"repository": {
"type": "git",
"url": "git://github.com/mongodb-js/ns.git"
},
"keywords": [
"mongodb",
"mongodb.js"
],
"license": "MIT",
"dependencies": {},
"devDependencies": {
"eslint-config-mongodb-js": "^5.0.3",
"mocha": "^7.0.0",
"mongodb-js-precommit": "^2.0.0"
}
}
Loading
Loading