From f285908d00f2a5939cdb713607680b4de58c6a34 Mon Sep 17 00:00:00 2001 From: Brett Logan Date: Mon, 24 Aug 2020 16:42:05 -0400 Subject: [PATCH] Support for Offline JavaScript Chaincode Installation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Background: Chaincode written in Node.js or Typescript requires access to the npm modules from the official NPM registry or a local registry available from the chaincode build container. For networks running behind firewalls with no internet access this poses the problem that the user must set up and maintain a local NPM registry to serve the necessary packages. This is added complexity and maintenance out of band that is difficult for users to configure and maintain. Proposal: Instead of using a local registry, we modify the `fabric-nodeenv` image to support npm tarballs created via the `npm pack` command. The users packages their chaincode in a tarball named `chaincode.pkg` after adding the `bundledDependencies` field to their `package.json`. If the `chaincode.pkg` file is present, the `fabric-nodeenv` chaincode builder simply extracts the archive and moves the chaincode source and modules into the proper directory. A chaincode directory looks like this structure today: ``` Bretts-MBP:package btl5037$ tree . ├── index.js ├── lib │ ├── META-INF │ │ └── statedb │ │ └── couchdb │ │ └── indexes │ │ └── indexOwner.json │ └── asset_transfer_ledger_chaincode.js └── package.json ``` The user modifies their `package.json` to include the `bundledDependencies` field: ``` { "name": "asset-transfer-ledger-queries", "version": "1.0.0", "description": "asset chaincode implemented in node.js", "main": "index.js", "engines": { "node": ">=12", "npm": ">=5.3.0" }, "scripts": { "start": "fabric-chaincode-node start" }, "engine-strict": true, "license": "Apache-2.0", "bundledDependencies": { "fabric-contract-api": "^2.0.0", "fabric-shim": "^2.0.0" }, "dependencies": { "fabric-contract-api": "^2.0.0", "fabric-shim": "^2.0.0" } } ``` The user then runs `npm install && npm pack && mv asset-transfer-ledger-queries-1.0.0.tgz chaincode.pkg` The `fabric-nodeenv` builder would then clean up the directory and extract `chaincode.pkg` and execute a `mv package/* .` to stage the extracted Javascript source code and metadata. That is all that is it, chaincode runs totally offline. Signed-off-by: Brett Logan --- docker/fabric-nodeenv/build.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docker/fabric-nodeenv/build.sh b/docker/fabric-nodeenv/build.sh index 09e6c8de..3b83c2b9 100755 --- a/docker/fabric-nodeenv/build.sh +++ b/docker/fabric-nodeenv/build.sh @@ -7,8 +7,13 @@ INPUT_DIR=/chaincode/input OUTPUT_DIR=/chaincode/output cp -R ${INPUT_DIR}/src/. ${OUTPUT_DIR} cd ${OUTPUT_DIR} -if [ -f package-lock.json -o -f npm-shrinkwrap.json ]; then +if [ -f chaincode.pkg ]; then + ls --ignore="chaincode.pkg" | xargs rm -rf + tar -xvf chaincode.pkg + mv package/* . + rm -rf chaincode.pkg package +elif [ -f package-lock.json -o -f npm-shrinkwrap.json ]; then npm ci --only=production else npm install --production -fi \ No newline at end of file +fi