Skip to content

Commit

Permalink
change URI package used and cache modelfile localTypes (hyperledger-a…
Browse files Browse the repository at this point in the history
…rchives#4212)

Signed-off-by: Nick Lincoln <[email protected]>
  • Loading branch information
nklincoln authored and Simon Stone committed Jul 3, 2018
1 parent 575e546 commit a69e89c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
18 changes: 12 additions & 6 deletions packages/composer-common/lib/introspect/modelfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ModelFile {
this.modelManager = modelManager;
this.external = false;
this.declarations = [];
this.localTypes = new Map();
this.imports = [];
this.importShortNames = new Map();
this.importWildcardNamespaces = [];
Expand Down Expand Up @@ -138,6 +139,13 @@ class ModelFile {
}),this.modelFile);
}
}

// Now build local types from Declarations
for(let index in this.declarations) {
let classDeclaration = this.declarations[index];
let localType = this.getNamespace() + '.' + classDeclaration.getName();
this.localTypes.set(localType, this.declarations[index]);
}
}

/**
Expand Down Expand Up @@ -428,13 +436,11 @@ class ModelFile {
type = this.getNamespace() + '.' + type;
}

for(let n=0; n < this.declarations.length; n++) {
let classDeclaration = this.declarations[n];
if(type === this.getNamespace() + '.' + classDeclaration.getName() ) {
return classDeclaration;
}
if (this.localTypes.has(type)) {
return this.localTypes.get(type);
} else {
return null;
}
return null;
}

/**
Expand Down
13 changes: 9 additions & 4 deletions packages/composer-common/lib/model/resourceid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

const URI = require('uri-js');
const URIJS = require('urijs');

const ModelUtils = require('../modelutil');

Expand Down Expand Up @@ -69,14 +69,19 @@ class ResourceId {
* @throws {Error} - On an invalid resource URI.
*/
static fromURI(uri, legacyNamespace, legacyType) {
const uriComponents = URI.parse(uri, { unicodeSupport: true });
let uriComponents;
try {
uriComponents = URIJS.parse(uri);
} catch (err){
throw new Error('Invalid URI: ' + uri);
}

const scheme = uriComponents.scheme;
const scheme = uriComponents.protocol;
// Accept legacy identifiers with missing URI scheme as valid
if (scheme && scheme !== RESOURCE_SCHEME) {
throw new Error('Invalid URI scheme: ' + uri);
}
if (uriComponents.userinfo || uriComponents.host || uriComponents.port || uriComponents.query) {
if (uriComponents.username || uriComponents.password || uriComponents.port || uriComponents.query) {
throw new Error('Invalid resource URI format: ' + uri);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/composer-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"sprintf-js": "1.0.3",
"thenify": "3.2.1",
"thenify-all": "1.6.0",
"uri-js": "3.0.2",
"urijs": "1.19.1",
"uuid": "3.0.1",
"winston": "2.3.1"
},
Expand Down
10 changes: 8 additions & 2 deletions packages/composer-common/test/model/relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,14 @@ describe('Relationship', function () {

it('should error on invalid URI content', function() {
(function () {
Relationship.fromURI(modelManager, 'resource://USER:PASSWORD@HOSTNAME:PORT/org.acme.l1.Person#123');
}).should.throw(/USER:PASSWORD@HOSTNAME:PORT/);
Relationship.fromURI(modelManager, 'resource://NOT-A-URI:SUCH-WRONG/org.acme.l1.Person#123');
}).should.throw(/Invalid URI: resource:\/\/NOT-A-URI:SUCH-WRONG/);
});

it('should error on URI content that Composer does not support', function() {
(function () {
Relationship.fromURI(modelManager, 'resource://USER:PASSWORD@HOSTNAME:1567/org.acme.l1.Person#123');
}).should.throw(/Invalid resource URI format: resource:\/\/USER:PASSWORD@HOSTNAME:1567/);
});

it('should error on missing namespace in URI', function() {
Expand Down

0 comments on commit a69e89c

Please sign in to comment.