mongodb-build-info
Advanced tools
Comparing version 1.2.0 to 1.3.0
33
index.js
@@ -1,5 +0,7 @@ | ||
const ATLAS_REGEX = /mongodb\.net[:/]/i; | ||
const LOCALHOST_REGEX = /(localhost|127\.0\.0\.1)/i; | ||
const DIGITAL_OCEAN_REGEX = /\.mongo\.ondigitalocean\.com[:/]/i; | ||
const { default: ConnectionString } = require('mongodb-connection-string-url'); | ||
const ATLAS_REGEX = /\.mongodb(-dev)?\.net$/i; | ||
const LOCALHOST_REGEX = /^(localhost|127\.0\.0\.1)$/i; | ||
const DIGITAL_OCEAN_REGEX = /\.mongo\.ondigitalocean\.com$/i; | ||
function getDataLake(buildInfo) { | ||
@@ -31,12 +33,31 @@ const res = { | ||
function getHostnameFromHost(host) { | ||
return host.split(':')[0]; | ||
} | ||
function getHostnameFromUrl(url) { | ||
if (typeof url !== 'string') { | ||
return ''; | ||
} | ||
try { | ||
const connectionString = new ConnectionString(url); | ||
const firstHost = connectionString.hosts[0]; | ||
return firstHost.split(':')[0]; | ||
} catch (e) { | ||
// we assume is already an hostname, will further be checked against regexes | ||
return getHostnameFromHost(url); | ||
} | ||
} | ||
function isAtlas(uri) { | ||
return !!uri.match(ATLAS_REGEX); | ||
return !!getHostnameFromUrl(uri).match(ATLAS_REGEX); | ||
} | ||
function isLocalhost(uri) { | ||
return !!uri.match(LOCALHOST_REGEX); | ||
return !!getHostnameFromUrl(uri).match(LOCALHOST_REGEX); | ||
} | ||
function isDigitalOcean(uri) { | ||
return !!uri.match(DIGITAL_OCEAN_REGEX); | ||
return !!getHostnameFromUrl(uri).match(DIGITAL_OCEAN_REGEX); | ||
} | ||
@@ -43,0 +64,0 @@ |
{ | ||
"name": "mongodb-build-info", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Extract information from mongodb's buildInfo", | ||
"main": "index.js", | ||
"scripts": { | ||
"check": "mongodb-js-precommit", | ||
"test": "mocha test/index.spec.js" | ||
"lint": "eslint .", | ||
"depcheck": "depcheck", | ||
"check": "npm run depcheck && npm run lint", | ||
"test": "mocha test/index.spec.js", | ||
"release": "release-it" | ||
}, | ||
@@ -28,6 +31,12 @@ "repository": { | ||
"chai": "^4.2.0", | ||
"depcheck": "^1.4.2", | ||
"eslint": "^8.3.0", | ||
"eslint-config-mongodb-js": "^5.0.3", | ||
"mocha": "^7.1.2", | ||
"mongodb-js-precommit": "^2.2.1" | ||
} | ||
"release-it": "^14.11.8" | ||
}, | ||
"dependencies": { | ||
"mongodb-connection-string-url": "^2.2.0" | ||
}, | ||
"release-it": {} | ||
} |
@@ -53,6 +53,37 @@ const expect = require('chai').expect; | ||
expect(isAtlas('mongodb+srv://admin:catscatscats@cat-data-sets.cats.mongodb.net/admin')).to.be.true; | ||
expect(isAtlas('mongodb://admin:catscatscats@cat-data-sets.cats.mongodb.net/admin')).to.be.true; | ||
expect(isAtlas('mongodb://admin:catscatscats@cat-data-sets.cats1.mongodb.net,cat-data-sets.cats2.mongodb.net/admin')).to.be.true; | ||
}); | ||
it('works with host only', () => { | ||
expect(isAtlas('cat-data-sets.cats.mongodb.net:27017')).to.be.true; | ||
}); | ||
it('works with hostname', () => { | ||
expect(isAtlas('cat-data-sets.cats.mongodb.net')).to.be.true; | ||
}); | ||
it('returns true with atlas dev', () => { | ||
expect(isAtlas('mongodb+srv://admin:catscatscats@cat-data-sets.cats.mongodb-dev.net/admin')).to.be.true; | ||
expect(isAtlas('cat-data-sets.cats.mongodb-dev.net')).to.be.true; | ||
}); | ||
it('returns false if not atlas', () => { | ||
expect(isAtlas('cat-data-sets.cats.mangodb.net')).to.be.false; | ||
expect(isAtlas('cat-data-sets.catsmongodb.net')).to.be.false; | ||
expect(isAtlas('cat-data-sets.cats.mongodb.netx')).to.be.false; | ||
expect(isAtlas('cat-data-sets.cats.mongodb.com')).to.be.false; | ||
expect(isAtlas('localhost')).to.be.false; | ||
}); | ||
it('does not throw and returns with invalid argument', () => { | ||
expect(isAtlas(123)).to.be.false; | ||
expect(isAtlas('')).to.be.false; | ||
expect(isAtlas({})).to.be.false; | ||
expect(isAtlas(undefined)).to.be.false; | ||
expect(isAtlas(null)).to.be.false; | ||
}); | ||
}); | ||
it('isLocalhost', () => { | ||
context('isLocalhost', () => { | ||
it('reports on localhost', () => { | ||
@@ -65,2 +96,28 @@ expect(isLocalhost('localhost:27019')).to.be.true; | ||
}); | ||
it('works as url', () => { | ||
expect(isLocalhost('mongodb://127.0.0.1:27019')).to.be.true; | ||
expect(isLocalhost('mongodb+srv://127.0.0.1')).to.be.true; | ||
expect(isLocalhost('mongodb://localhost')).to.be.true; | ||
expect(isLocalhost('mongodb://localhost:27019')).to.be.true; | ||
}); | ||
it('works as hostname', () => { | ||
expect(isLocalhost('127.0.0.1')).to.be.true; | ||
expect(isLocalhost('localhost')).to.be.true; | ||
}); | ||
it('does not report if localhost or 127.0.0.1 is not the hostname', () => { | ||
expect(isLocalhost('127.0.0.2')).to.be.false; | ||
expect(isLocalhost('remotehost')).to.be.false; | ||
expect(isLocalhost('mongodb://remotelocalhost')).to.be.false; | ||
}); | ||
it('does not throw and returns with invalid argument', () => { | ||
expect(isLocalhost(123)).to.be.false; | ||
expect(isLocalhost('')).to.be.false; | ||
expect(isLocalhost({})).to.be.false; | ||
expect(isLocalhost(undefined)).to.be.false; | ||
expect(isLocalhost(null)).to.be.false; | ||
}); | ||
}); | ||
@@ -72,2 +129,24 @@ | ||
}); | ||
it('works with hostname only', () => { | ||
expect(isDigitalOcean('dave-a1234321.mongo.ondigitalocean.com')).to.be.true; | ||
}); | ||
it('works with host only', () => { | ||
expect(isDigitalOcean('dave-a1234321.mongo.ondigitalocean.com:27017')).to.be.true; | ||
}); | ||
it('returns false if not digitalocean', () => { | ||
expect(isDigitalOcean('dave-a1234321.mongo.ondigitalocean.com2')).to.be.false; | ||
expect(isDigitalOcean('dave-a1234321mongo.ondigitalocean.com')).to.be.false; | ||
expect(isDigitalOcean('dave-a1234321.mongoxondigitalocean.com')).to.be.false; | ||
}); | ||
it('does not throw and returns with invalid argument', () => { | ||
expect(isDigitalOcean(123)).to.be.false; | ||
expect(isDigitalOcean('')).to.be.false; | ||
expect(isDigitalOcean({})).to.be.false; | ||
expect(isDigitalOcean(undefined)).to.be.false; | ||
expect(isDigitalOcean(null)).to.be.false; | ||
}); | ||
}); | ||
@@ -74,0 +153,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
28713
11
364
1
6
+ Added@types/node@22.9.0(transitive)
+ Added@types/webidl-conversions@7.0.3(transitive)
+ Added@types/whatwg-url@8.2.2(transitive)
+ Addedmongodb-connection-string-url@2.6.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedtr46@3.0.0(transitive)
+ Addedundici-types@6.19.8(transitive)
+ Addedwebidl-conversions@7.0.0(transitive)
+ Addedwhatwg-url@11.0.0(transitive)