Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases from #11 for reference implementation #40

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
45 changes: 43 additions & 2 deletions package-name-map/src/package-name-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,46 @@ interface FindPackageResult {
package?: Package;
}

/**
* Performs initialization-time scope validation
* Ensuring valid property types
*/
function validateScope(scope: Scope) {
if (scope.path_prefix !== undefined && typeof scope.path_prefix !== 'string') {
throw new Error(`path_prefix must be a valid string.`);
}
if (scope.packages !== undefined) {
if (typeof scope.packages !== 'object') {
throw new Error(`packages must be a valid object.`);
}
Object.entries(scope.packages).forEach(validatePackage);
}
if (scope.scopes !== undefined) {
if (typeof scope.scopes !== 'object') {
throw new Error(`scopes must be a valid object.`);
}
Object.values(scope.scopes).forEach(validateScope);
}
}
function validatePackage([pkgName, pkg]: [string, Package]) {
// package name validation
if (pkgName.startsWith('/') || pkgName.endsWith('/')) {
throw new Error(
`Invalid package name ${pkgName}, package names cannot start or end with a path separator.`
);
}
if (pkg.path !== undefined && typeof pkg.path !== 'string') {
throw new Error(
`Invalid package for ${pkgName}, path expected to be a string.`
);
}
if (pkg.main !== undefined && typeof pkg.main !== 'string') {
throw new Error(
`Invalid package for ${pkgName}, main expected to be a string.`
);
}
}

/**
* An object that can resolve specifiers using its package name map
* configuration object.
Expand All @@ -66,6 +106,7 @@ export class PackageNameMap {
private _map: Scope;

constructor(map: Scope, baseURL: string) {
validateScope(map);
this._map = map;
this.baseURL = new URL('.', baseURL).href;
}
Expand Down Expand Up @@ -134,7 +175,7 @@ export class PackageNameMap {
}
}

const ensureTrailingSlash = (s: string) => (s.endsWith('/') ? s : s + '/');
const ensureTrailingSlash = (s: string) => (s.length === 0 || s.endsWith('/') ? s : s + '/');

/**
* Performs successive URL resolution of fragments.
Expand Down Expand Up @@ -217,7 +258,7 @@ const findPackage = (

do {
const scope = scopes[scopePathIndex].scope;
if (scope.packages !== undefined) {
if (scope.packages) {
for (const [pkgName, pkg] of Object.entries(scope.packages)) {
if (isPathSegmentPrefix(pkgName, specifier)) {
foundPackage = pkg;
Expand Down
245 changes: 240 additions & 5 deletions package-name-map/src/test/package-name-map_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const {assert} = chai;

suite('PackageNameMap', () => {
suite('resolve', () => {
const referrerURL = 'http://foo.com/bar/baz.html';
const referrerURL = 'http://bar.com/bar/baz.html';
const baseURL = 'http://foo.com/resources/package-name-map.json';

suite('does not modify already valid specifiers', () => {
Expand All @@ -42,13 +42,13 @@ suite('PackageNameMap', () => {
});

test('does not modify valid paths', () => {
assert.equal(map.resolve('/foo', referrerURL), 'http://foo.com/foo');
assert.equal(map.resolve('/foo', referrerURL), 'http://bar.com/foo');
assert.equal(
map.resolve('./foo', referrerURL),
'http://foo.com/bar/foo'
'http://bar.com/bar/foo'
);
assert.equal(map.resolve('./foo', referrerURL), 'http://foo.com/bar/foo');
assert.equal(map.resolve('../foo', referrerURL), 'http://foo.com/foo');
assert.equal(map.resolve('./foo', referrerURL), 'http://bar.com/bar/foo');
assert.equal(map.resolve('../foo', referrerURL), 'http://bar.com/foo');
});
});

Expand Down Expand Up @@ -281,3 +281,238 @@ suite('isPathSegmentPrefix', () => {
assert.isFalse(isPathSegmentPrefix('a', 'ab/c'));
});
});

suite('some pathological cases', () => {

const referrerURL = 'http://foo.com/bar/baz.html';
const baseURL = 'http://foo.com/resources/package-name-map.json';

suite('package map init validations', () => {
test('Invalid path_prefix type', () => {
try {
new PackageNameMap(
{
scopes: {
'/': {
path_prefix: <any>0,
packages: {
'moment': {
main: 'moment.js',
},
},
},
},
},
baseURL
);
assert.fail();
}
catch (err) {
assert.equal(err.message, 'path_prefix must be a valid string.');
}
});

test('Invalid packages type', () => {
try {
new PackageNameMap(
{
scopes: {
'/': {
path_prefix: 'node_modules',
packages: <any>'blah'
},
},
},
baseURL
);
assert.fail();
}
catch (err) {
assert.equal(err.message, 'packages must be a valid object.');
}
});

test('Invalid scopes type', () => {
try {
new PackageNameMap(
{
scopes: <any>'asdf'
},
baseURL
);
assert.fail();
}
catch (err) {
assert.equal(err.message, 'scopes must be a valid object.');
}
});

test('Invalid path', () => {
try {
new PackageNameMap(
{
path_prefix: '/node_modules',
packages: {
'moment': {
path: <any>1,
main: 'moment.js',
},
},
},
baseURL
);
assert.fail();
}
catch (err) {
assert.equal(err.message, `Invalid package for moment, path expected to be a string.`);
}
});

test('Invalid main', () => {
try {
new PackageNameMap(
{
path_prefix: '/node_modules',
packages: {
'moment': {
main: <any>1,
},
},
},
baseURL
);
assert.fail();
}
catch (err) {
assert.equal(err.message, `Invalid package for moment, main expected to be a string.`);
}
});
});

suite('scopes', () => {
test('empty string scope', () => {
const map = new PackageNameMap(
{
scopes: {
'': {
path_prefix: 'node_modules',
packages: {
'moment': {
main: 'moment.js',
},
},
},
},
},
referrerURL
);

assert.equal(map.resolve('moment', referrerURL), 'http://foo.com/bar/node_modules/moment/moment.js');
});
});

suite('package name validations', () => {
test('dot segments at start of package name', () => {
const map = new PackageNameMap(
{
path_prefix: '/node_modules',
packages: {
'..\\moment': {
main: 'moment.js',
},
},
},
referrerURL
);
assert.equal(map.resolve('..\\moment', referrerURL), 'http://foo.com/moment/moment.js');
});

test('dot segments and separator at start of package name', () => {
const map = new PackageNameMap(
{
path_prefix: 'baz',
packages: {
'..\\/moment': {
main: 'moment.js',
},
},
},
referrerURL
);
assert.equal(map.resolve('..\\/moment', referrerURL), 'http://foo.com/bar//moment/moment.js');
});

test('dot segments within package name', () => {
const map = new PackageNameMap(
{
path_prefix: '/node_modules',
packages: {
'moment/../notmoment': {
main: 'moment.js',
},
},
},
baseURL
);
assert.equal(map.resolve('moment/../notmoment', referrerURL), 'http://foo.com/node_modules/notmoment/moment.js');
});

test('trailing slash', () => {
try {
new PackageNameMap(
{
path_prefix: '/node_modules',
packages: {
'moment/': {
path: 'y',
main: 'moment.js',
},
},
},
baseURL
);
assert.fail();
}
catch (err) {
assert.equal(err.message, `Invalid package name moment/, package names cannot start or end with a path separator.`);
}
});

test('leading slash', () => {
try {
new PackageNameMap(
{
path_prefix: '/node_modules',
packages: {
'/moment': {
path: 'y',
main: 'moment.js',
},
},
},
baseURL
);
assert.fail();
}
catch (err) {
assert.equal(err.message, `Invalid package name /moment, package names cannot start or end with a path separator.`);
}
});

test('url', () => {
const map = new PackageNameMap(
{
path_prefix: '/node_modules',
packages: {
'http://foo.com/pkg': {
path: 'y',
main: 'moment.js',
},
},
},
baseURL
);
assert.equal(map.resolve('http://foo.com/pkg', referrerURL), 'http://foo.com/pkg');
});
});
});