Skip to content

Commit

Permalink
[FABN-909] remove duplicated npm dependencies
Browse files Browse the repository at this point in the history
Along with
- move Msp.js back to fabric-client
- remove unused npm dependencies in fabric-client
- add dependencies to fabric-common, make it self-contained
- use async/await to replace promise.then somewhere
- move most files under fabric-client/test/impl/ to fabric-common/test/impl/

Change-Id: I3d6e573837ae8e7941e01c9542bb3c903735c050
Signed-off-by: davidkhala <[email protected]>
  • Loading branch information
davidkhala committed Apr 9, 2019
1 parent dc75c20 commit 7feb93a
Show file tree
Hide file tree
Showing 25 changed files with 144 additions and 165 deletions.
5 changes: 0 additions & 5 deletions fabric-ca-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@
"types": "./types/index.d.ts",
"dependencies": {
"@types/bytebuffer": "^5.0.34",
"bn.js": "^4.11.3",
"elliptic": "^6.2.3",
"fabric-common": "file:../fabric-common",
"fabric-protos": "file:../fabric-protos",
"fs-extra": "^6.0.1",
"js-sha3": "^0.7.0",
"jsrsasign": "^7.2.2",
"jssha": "^2.1.0",
"url": "^0.11.0",
"util": "^0.10.3"
},
Expand Down
8 changes: 4 additions & 4 deletions fabric-common/lib/Msp.js → fabric-client/lib/msp/Msp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/
'use strict';

const CryptoAlgorithms = require('./CryptoAlgorithms');
const Identity = require('./Identity');
const SigningIdentity = require('./SigningIdentity');
const utils = require('./Utils');
const CryptoAlgorithms = require('fabric-common/lib/CryptoAlgorithms');
const Identity = require('fabric-common/lib/Identity');
const SigningIdentity = require('fabric-common/lib/SigningIdentity');
const utils = require('fabric-common/lib/Utils');
const logger = utils.getLogger('msp.js');

const fabprotos = require('fabric-protos');
Expand Down
3 changes: 2 additions & 1 deletion fabric-client/lib/msp/msp-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
const util = require('util');
const fabprotos = require('fabric-protos');

const {Utils: utils, MSP} = require('fabric-common');
const {Utils: utils} = require('fabric-common');
const MSP = require('./Msp.js');
const logger = utils.getLogger('MSPManager.js');

/**
Expand Down
27 changes: 11 additions & 16 deletions fabric-client/lib/packager/Golang.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class GolangPackager extends BasePackager {
* @param {string} [metadataPath] The path to the top-level directory containing metadata descriptors.
* @returns {Promise.<TResult>}
*/
package(chaincodePath, metadataPath) {
logger.debug('packaging GOLANG from %s', chaincodePath);
async package(chaincodePath, metadataPath) {
logger.debug(`packaging GOLANG from ${chaincodePath}`);

// Determine the user's $GOPATH
const goPath = process.env.GOPATH;
Expand All @@ -48,20 +48,15 @@ class GolangPackager extends BasePackager {

const buffer = new sbuf.WritableStreamBuffer();

return this.findSource(goPath, projDir).then((srcDescriptors) => {
if (metadataPath) {
return super.findMetadataDescriptors(metadataPath)
.then((metaDescriptors) => {
return srcDescriptors.concat(metaDescriptors);
});
} else {
return srcDescriptors;
}
}).then((descriptors) => {
return super.generateTarGz(descriptors, buffer);
}).then(() => {
return buffer.getContents();
});
const srcDescriptors = await this.findSource(goPath, projDir);

let descriptors = srcDescriptors;
if (metadataPath) {
const metaDescriptors = await super.findMetadataDescriptors(metadataPath);
descriptors = srcDescriptors.concat(metaDescriptors);
}
await super.generateTarGz(descriptors, buffer);
return buffer.getContents();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions fabric-client/lib/packager/Java.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class JavaPackager extends BasePackager {
* @returns {Promise.<byte[]>}
*/
async package(chaincodePath, metadataPath) {
logger.debug('packaging Java source from %s', chaincodePath);
logger.debug(`packaging Java source from ${chaincodePath}`);

const buffer = new sbuf.WritableStreamBuffer();
let descriptors = await this.findSource(chaincodePath);
if (metadataPath) {
logger.debug('packaging metadata files from %s', metadataPath);
logger.debug(`packaging metadata files from ${metadataPath}`);

const metaDescriptors = await super.findMetadataDescriptors(metadataPath);
descriptors = descriptors.concat(metaDescriptors);
Expand Down Expand Up @@ -71,7 +71,7 @@ class JavaPackager extends BasePackager {
descriptors.push(desc);
});
} else {
logger.debug(' No files found at this path %s', filePath);
logger.debug(` No files found at this path ${filePath}`);
}

return descriptors;
Expand Down
63 changes: 28 additions & 35 deletions fabric-client/lib/packager/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class NodePackager extends BasePackager {
* @param {string} [metadataPath] The path to the top-level directory containing metadata descriptors
* @returns {Promise.<TResult>}
*/
package(chaincodePath, metadataPath) {
logger.debug('packaging Node from %s', chaincodePath);
async package(chaincodePath, metadataPath) {
logger.debug(`packaging Node from ${chaincodePath}`);

// Compose the path to the chaincode project directory
const projDir = chaincodePath;
Expand All @@ -45,20 +45,14 @@ class NodePackager extends BasePackager {
// will need to assemble sources from multiple packages

const buffer = new sbuf.WritableStreamBuffer();
return this.findSource(projDir).then((srcDescriptors) => {
if (metadataPath) {
return super.findMetadataDescriptors(metadataPath)
.then((metaDescriptors) => {
return srcDescriptors.concat(metaDescriptors);
});
} else {
return srcDescriptors;
}
}).then((descriptors) => {
return super.generateTarGz(descriptors, buffer);
}).then(() => {
return buffer.getContents();
});
const srcDescriptors = await this.findSource(projDir);
let descriptors = srcDescriptors;
if (metadataPath) {
const metaDescriptors = await super.findMetadataDescriptors(metadataPath);
descriptors = srcDescriptors.concat(metaDescriptors);
}
await super.generateTarGz(descriptors, buffer);
return buffer.getContents();
}

/**
Expand All @@ -68,36 +62,35 @@ class NodePackager extends BasePackager {
* @param filePath
* @returns {Promise}
*/
findSource(filePath) {
return walk({
async findSource(filePath) {
let files = await walk({
path: filePath,
// applies filtering based on the same rules as "npm publish":
// if .npmignore exists, uses rules it specifies
ignoreFiles: ['.npmignore'],
// follow symlink dirs
follow: true
}).then((files) => {
const descriptors = [];

if (!files) {
files = [];
}
});
const descriptors = [];

// ignore the node_modules folder by default
files = files.filter(f => f.indexOf('node_modules') !== 0);
if (!files) {
files = [];
}

files.forEach((entry) => {
const desc = {
name: path.join('src', entry).split('\\').join('/'), // for windows style paths
fqp: path.join(filePath, entry)
};
// ignore the node_modules folder by default
files = files.filter(f => f.indexOf('node_modules') !== 0);

logger.debug('adding entry', desc);
descriptors.push(desc);
});
files.forEach((entry) => {
const desc = {
name: path.join('src', entry).split('\\').join('/'), // for windows style paths
fqp: path.join(filePath, entry)
};

return descriptors;
logger.debug('adding entry', desc);
descriptors.push(desc);
});

return descriptors;
}
}

Expand Down
8 changes: 0 additions & 8 deletions fabric-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@
"types": "./types/index.d.ts",
"dependencies": {
"@types/bytebuffer": "^5.0.34",
"bn.js": "^4.11.3",
"callsite": "^1.0.0",
"elliptic": "^6.2.3",
"fabric-common": "file:../fabric-common",
"fabric-protos": "file:../fabric-protos",
"fs-extra": "^6.0.1",
"hoek": "^4.2.1",
"ignore-walk": "^3.0.0",
"js-sha3": "^0.7.0",
"js-yaml": "^3.13.0",
"jsrsasign": "^7.2.2",
"jssha": "^2.1.0",
"klaw": "^2.0.0",
"long": "^4.0.0",
"nano": "^6.4.4",
"nconf": "^0.10.0",
"pkcs11js": "^1.0.6",
"promise-settle": "^0.3.0",
"stream-buffers": "3.0.1",
"tar-stream": "1.6.1",
Expand Down
3 changes: 2 additions & 1 deletion fabric-client/test/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const ChannelEventHub = require('fabric-client/lib/ChannelEventHub');
const Chaincode = require('fabric-client/lib/Chaincode');
const Client = require('fabric-client/lib/Client');
const Constants = require('fabric-client/lib/Constants');
const {Identity, SigningIdentity, Utils: sdk_utils, MSP} = require('fabric-common');
const {Identity, SigningIdentity, Utils: sdk_utils} = require('fabric-common');
const MSP = require('fabric-client/lib/msp/Msp');
const MSPManager = require('fabric-client/lib/msp/msp-manager');
const Orderer = require('fabric-client/lib/Orderer');
const Peer = require('fabric-client/lib/Peer');
Expand Down
5 changes: 3 additions & 2 deletions fabric-client/test/msp/msp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
'use strict';


const {Config, CryptoAlgorithms, Identity, SigningIdentity, Utils: utils, MSP} = require('fabric-common');
const {Config, CryptoAlgorithms, Identity, SigningIdentity, Utils: utils} = require('fabric-common');
const path = require('path');
const fs = require('fs');

require('chai');
const sinon = require('sinon');

const rewire = require('rewire');
const MspRewire = rewire('../../../fabric-common/lib/Msp.js');
const MSP = require('../../lib/msp/Msp.js');
const MspRewire = rewire('../../lib/msp/Msp.js');

const certificateAsPEM = fs.readFileSync(path.join(__dirname, '..', 'data', 'cert.pem')).toString('utf8');

Expand Down
19 changes: 11 additions & 8 deletions fabric-client/test/packager/Java.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ describe('Java', () => {
revert = [];
sandbox = sinon.createSandbox();
FakeLogger = {
error: () => {},
debug: () => {}
error: () => {
},
debug: () => {
}
};
sinon.stub(FakeLogger);
revert.push(Java.__set__('logger', FakeLogger));
Expand All @@ -65,14 +67,14 @@ describe('Java', () => {
java.findSource = findSourceStub;
});

it('should return the package when given the metadata path', async() => {
it('should return the package when given the metadata path', async () => {
findSourceStub.resolves(['descriptor2']);
await java.package('ccpath', 'metadatapath');
sinon.assert.calledWith(generateTarGzStub, ['descriptor2', 'descriptor1'], new bufferStub());
sinon.assert.called(getContentsStub);
});

it('should return the package when not given the metadata path', async() => {
it('should return the package when not given the metadata path', async () => {
findSourceStub.resolves(['descriptor2']);
await java.package('ccpath');
sinon.assert.calledWith(generateTarGzStub, ['descriptor2'], new bufferStub());
Expand All @@ -88,17 +90,18 @@ describe('Java', () => {
revert.push(Java.__set__('walk', walkStub));
});

it('should return a list of descriptors if files are returned', async() => {
it('should return a list of descriptors if files are returned', async () => {
walkStub.resolves(['FILE_1']);
const descriptors = await java.findSource('path');
sinon.assert.calledWith(FakeLogger.debug, 'adding descriptor entry', {fqp: 'path/FILE_1', name: 'src/FILE_1'});
descriptors.should.deep.equal([{fqp: 'path/FILE_1', name: 'src/FILE_1'}]);
});

it('should return a list of descriptors if no files are returned', async() => {
it('should return a list of descriptors if no files are returned', async () => {
walkStub.resolves();
const descriptors = await java.findSource('path');
sinon.assert.calledWith(FakeLogger.debug, ' No files found at this path %s', 'path');
const _path = 'path';
const descriptors = await java.findSource(_path);
sinon.assert.calledWith(FakeLogger.debug, ` No files found at this path ${_path}`);
descriptors.should.deep.equal([]);
});
});
Expand Down
4 changes: 1 addition & 3 deletions fabric-common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const SigningIdentity = require('./lib/SigningIdentity');
const Utils = require('./lib/Utils');
const User = require('./lib/User');
const BaseClient = require('./lib/BaseClient');
const MSP = require('./lib/Msp');

module.exports = {
Config,
Expand All @@ -30,6 +29,5 @@ module.exports = {
SigningIdentity,
Utils,
User,
BaseClient,
MSP
BaseClient
};
2 changes: 1 addition & 1 deletion fabric-common/lib/impl/CouchDBKeyValueStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const {KeyValueStore, Utils: utils} = require('fabric-common');
const {KeyValueStore, Utils: utils} = require('../../');
const util = require('util');
const nano = require('nano');

Expand Down
2 changes: 1 addition & 1 deletion fabric-common/lib/impl/CryptoKeyStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

'use strict';
const {Utils: utils} = require('fabric-common');
const {Utils: utils} = require('../../');
const jsrsasign = require('jsrsasign');
const KEYUTIL = jsrsasign.KEYUTIL;

Expand Down
2 changes: 1 addition & 1 deletion fabric-common/lib/impl/CryptoSuite_ECDSA_AES.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'use strict';

// requires
const {CryptoAlgorithms, CryptoSuite, HashPrimitives, Utils: utils} = require('fabric-common');
const {CryptoAlgorithms, CryptoSuite, HashPrimitives, Utils: utils} = require('../../');
const elliptic = require('elliptic');
const EC = elliptic.ec;
const jsrsa = require('jsrsasign');
Expand Down
2 changes: 1 addition & 1 deletion fabric-common/lib/impl/FileKeyValueStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const {KeyValueStore, Utils: utils} = require('fabric-common');
const {KeyValueStore, Utils: utils} = require('../../');
const fs = require('fs-extra');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion fabric-common/lib/impl/aes/pkcs11_key.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const {Key} = require('fabric-common');
const {Key} = require('../../../');

const PKCS11_AES_KEY = class extends Key {

Expand Down
2 changes: 1 addition & 1 deletion fabric-common/lib/impl/bccsp_pkcs11.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const {CryptoAlgorithms, CryptoSuite, HashPrimitives, Utils: utils} = require('fabric-common');
const {CryptoAlgorithms, CryptoSuite, HashPrimitives, Utils: utils} = require('../../');

const aesKey = require('./aes/pkcs11_key.js');
const ecdsaKey = require('./ecdsa/pkcs11_key.js');
Expand Down
2 changes: 1 addition & 1 deletion fabric-common/lib/impl/ecdsa/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const {HashPrimitives, Key, Utils: utils} = require('fabric-common');
const {HashPrimitives, Key, Utils: utils} = require('../../../');
const jsrsa = require('jsrsasign');
const asn1 = jsrsa.asn1;
const KEYUTIL = jsrsa.KEYUTIL;
Expand Down
2 changes: 1 addition & 1 deletion fabric-common/lib/impl/ecdsa/pkcs11_key.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const {Key} = require('fabric-common');
const {Key} = require('../../../');
const jsrsa = require('jsrsasign');
const asn1 = jsrsa.asn1;

Expand Down
Loading

0 comments on commit 7feb93a

Please sign in to comment.