Socket
Socket
Sign inDemoInstall

snyk-module

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

snyk-module - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

.npmignore

72

lib/index.js

@@ -5,5 +5,18 @@ module.exports = moduleToObject;

var debug = require('debug')('snyk:module');
var gitHost = require('hosted-git-info');
var validate = require('validate-npm-package-name');
function moduleToObject(str) {
if (!str) {
throw new Error('requires string to parse into module');
}
var url = looksLikeUrl(str);
if (url) {
// then the string looks like a url, let's try to parse it
return supported(fromUrl(url));
}
var parts = str.split('@');
if (str.indexOf('@') === 0) {

@@ -24,5 +37,43 @@ // put the scoped package name back together

debug('parsed from string');
return supported(module);
}
function looksLikeUrl(str) {
if (str.slice(-1) === '/') {
// strip the trailing slash since we can't parse it properly anyway
str = str.slice(0, -1);
}
var obj = gitHost.fromUrl(str);
return obj;
}
function fromUrl(obj) {
var error = false;
debug('parsed from hosted-git-info');
/* istanbul ignore if */
if (!obj.project || !obj.user) {
// this should never actually occur
error = new Error('not supported: failed to fully parse');
error.code = 501;
throw error;
}
var module = {
name: obj.project,
version: obj.user + '/' + obj.project,
};
if (obj.committish) {
module.version += '#' + obj.committish;
}
return supported(module);
}
function encode(name) {

@@ -34,9 +85,14 @@ return name[0] + encodeURIComponent(name.slice(1));

var error;
// if (module.name.indexOf('@') === 0) {
// debug('not supported %s@%s (private)', module.name, module.version);
// error = new Error('not supported: private module ' + toString(module));
// }
var valid = validate(module.name);
if (!valid.validForNewPackages) {
error = new Error('not supported: invalid package name');
throw error;
}
if (module.version.indexOf('http') === 0 ||
module.version.indexOf('git') === 0) {
module.version.indexOf('git') === 0 ||
module.name.indexOf('://') !== -1) {
// we don't support non-npm modules atm

@@ -57,2 +113,8 @@ debug('not supported %s@%s (ext)', module.name, module.version);

return module.name + '@' + module.version;
}
/* istanbul ignore if */
if (!module.parent) {
// support simple cli testing
console.log(moduleToObject(process.argv[2]));
}

17

package.json

@@ -13,3 +13,5 @@ {

"scripts": {
"test": "tape test/*.test.js | tap-spec",
"lint": "jscs lib/*.js -v",
"cover": "tap test/*.test.js --cov --coverage-report=lcov",
"test": "npm run lint && tap test/*.test.js --cov --timeout=60",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"

@@ -20,10 +22,13 @@ },

"devDependencies": {
"tap-spec": "^4.0.2",
"tape": "^4.1.0",
"semantic-release": "^4.3.5"
"jscs": "^2.8.0",
"semantic-release": "^4.3.5",
"snyk": "^1.1.0",
"tap": "^5.0.1"
},
"dependencies": {
"debug": "^2.2.0"
"debug": "^2.2.0",
"hosted-git-info": "^2.1.4",
"validate-npm-package-name": "^2.2.2"
},
"version": "1.0.2"
"version": "1.1.0"
}

@@ -1,15 +0,59 @@

var tape = require('tape');
var test = require('tap').test;
var mod = require('../');
tape('module string to object', function (t) {
test('module string to object', function (t) {
t.deepEqual(mod('nodemon'), { name: 'nodemon', version: '*' }, 'supports versionless');
t.deepEqual(mod('nodemon@1'), { name: 'nodemon', version: '1' }, 'with version');
// t.throws(function () { mod('@remy/snyk-module'); }, /not supported: private module/, 'private not supported');
t.throws(function () { mod('grunt-sails-linker@git://github.com/Zolmeister/grunt-sails-linker.git'); }, /not supported: external module/, 'external not supported');
t.throws(function () { return mod('ikt@git+http://ikt.pm2.io/ikt.git#master'); }, /not supported: external module/, 'external not supported');
t.deepEqual(mod('@remy/snyk-module'), { name: '@remy/snyk-module', version: '*' }, 'private packages');
t.deepEqual(mod('jsbin/jsbin'), { name: 'jsbin', version: 'jsbin/jsbin' }, 'short github works');
var urls = [
'https://github.com/remy/undefsafe',
'https://github.com/remy/undefsafe/',
'https://github.com/remy/undefsafe.git',
'git@github.com:remy/undefsafe.git',
'git@bitbucket.org:remy/undefsafe.git',
'remy/undefsafe',
];
var expect = {
name: 'undefsafe',
version: 'remy/undefsafe',
};
urls.forEach(function (url) {
t.deepEqual(mod(url), expect, 'short github works');
});
t.deepEqual(mod(urls[0] + '#123'), { name: 'undefsafe', version: 'remy/undefsafe#123'}, 'add hash correctly');
t.throws(function () {
mod('/');
}, /supported: invalid package name/, 'catch invalid package name');
t.throws(function () {
mod(' *');
}, /supported: invalid package name/, 'catch invalid package name');
t.throws(function () {
mod();
}, /requires string/, 'requires args');
// usernames on git urls aren't supported (unsure if this is 100% right tho)
t.throws(function () {
mod('grunt-sails-linker@git://github.com/Zolmeister/grunt-sails-linker.git');
}, /not supported: external module/, 'external not supported');
// privately hosted git repo not supported
t.throws(function () {
mod('ikt@git+http://ikt.pm2.io/ikt.git#master');
}, /not supported: external module/, 'external not supported');
t.end();
});
tape('encoding', function (t) {
test('encoding', function (t) {
t.equal(mod.encode('snyk'), 'snyk', 'vanilla strings unaffected');

@@ -16,0 +60,0 @@ t.equal(mod.encode('@snyk/config'), '@snyk%2Fconfig', 'slash is escaped');

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc