How Can I bundle a simple node.js application - amazon-managed-blockchain

I am working with hyperledger-fabric on amazon managed blockchain. There I have written the chaincode with node.js. The problem is, the dependencies I'm using is not supported in amazon managed blockchain's peer. That's why I need to bundle my chaincode with the node modules. How can I do tha?

Here are steps for bundling Node.js chaincode with external dependencies on Amazon Managed Blockchain Hyperledger Fabric 2.2 networks:
Why bundling is needed:
Due to stringent security requirements, peer nodes in Amazon Managed Blockchain do not have access to the open internet. This means that peer nodes cannot download external dependencies at runtime when building/executing chaincode. If you suspect missing node_modules/ are responsible for errors in your chaincode, you can verify this by viewing Chaincode logs in Amazon CloudWatch, where reference to missing node_modules / dependencies will be clearly evident.
How to bundle dependencies
First, navigate to the root directory of the chaincode you wish to deploy. Your package.json file should be present in this directory. From this directory, run npm i to install node_modules. Then, move those node_modules to a new directory -- Example:
mv node_modules/ lib
Moving the dependencies to lib/ will allow you to package the installed NPM packages (dependencies) in the chaincode tar.gz file in the following steps. Because the node_modules are stored in lib/, the Node.js start script in package.json has been modified slightly to tell the container environment that runs the chaincode where to find the dependencies at runtime: "start": "NODE_PATH=lib node <entrypoint filename>.js"
{
"name": "chaincode",
"version": "1.0.0",
"scripts": {
"test": "NODE_PATH=lib mocha *_test.js",
"start": "NODE_PATH=lib node products.js"
},
"dependencies": {
"fabric-shim": "^2.0.0"
},
"devDependencies": {
"#theledger/fabric-mock-stub": "^2.0.3",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"chai-datetime": "^1.6.0",
"moment": "^2.25.3"
}
}
With the node_modules bundled in lib/ and the start script for the chaincode modified to point to those node_modules, one can now package, install, approve and commit this chaincode as normal using the Chaincode Lifecycle commands.

Related

a turborepo pipeline task that depends on a root package.json task that does not execute in each package

I have a root package.json task that does not execute in every package that calls yarn tsc.
Currently, I have it as a prebuild script in the root package.json:
"prebuild": "yarn tsc",
Is there anyway to specify this as a dependsOn element of build in the turborepo.json?
If I add tsc like below then it will get executed in the each package which is not what I want.
"pipeline": {
"tsc": {
"dependsOn": ["generate"],
"outputs": ["dist-types/**"]
},
"build": {
"dependsOn": ["tsc", "^build"],
"outputs": ["dist/**"]
},
Based on the documentation, your individual projects within the mono-repo need to implement a corresponding script (e.g. if you have tsc as a pipeline in turbo.json, you will need a tsc script in your package.json in your individual projects). Any project without a script matching the pipeline name will gracefully be skipped when the pipeline is run.
In your case, you would move your tsc script from the root level package.json into each project in which you want the pipeline to run against.

Firebase Functions local "file:" dependencies

I'm using a react-crud-shared as dependency for react-crud-backend which uses Firebase Cloud Functions.
At react-crud-backend I have the following:
{
"name": "react-crud-backend",
"description": "Cloud Functions for Firebase",
"scripts": {
...
},
"dependencies": {
...
"react-crud-shared": "file:../shared",
...
},
"engines": {
"node": "8"
},
"private": true,
"devDependencies": {
...
}
}
At react-crud-shared I have the following:
{
"name": "react-crud-shared",
"version": "0.0.1",
"description": "",
"main": "src/index.js",
"private": true,
"dependencies": {
"lodash": "^4.17.11"
}
}
It works fine on development: "firebase serve --only functions", but an error is thrown on deployment:
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'react-crud-shared'
Is there a way to make it work without having to publish the private repository to NPM?
Thanks
EDIT: I found a solution for this that I like much better. I commented on this github issue here: https://github.com/firebase/firebase-tools/issues/968#issuecomment-460323113 . Basically, I have a preinstall script the runs npm pack to copy over the package under the functions directory before I use firebase deploy.
FWIW I have the exact same problem. Not exactly sure how I'm going to solve it, but this information from the doc was helpful (https://firebase.google.com/docs/functions/handle-dependencies):
To specify a dependency for your function, add it to your package.json file. If you are deploying through the gcloud command-line tool, you can also pre-install dependencies and deploy them alongside your function. By default, the node_modules folder is added to your .gcloudignore file and is not uploaded as part of your deployment. To deploy pre-installed dependencies, remove node_modules/ from the .gcloudignore file before deploying your function.
Note: Deploying pre-installed dependencies works with gcloud only; the Firebase CLI ignores the local node_modules folder.
Thus, it appears you could first run "npm install" locally, and then use gcloud for deployment, as it would copy up your node_modules directory, which would have your peer dependency in it.
Really kind of stinks, though, that I would have to switch to gcloud from firebase CLI for deployment. Ugh.
node_modules are (ordinary) being ignored for the deployment;
one can still deploy private modules with a directory structure like that:
functions/
index.js
package.json
react-crud-shared/
package.json
and a package.json alike that:
{
"dependencies": {
...
"react-crud-shared": "file:./react-crud-shared"
}
}
another method would be to blank the ignores:
{
"functions": {
"ignore": []
}
}
just think the first one is better, because this would push the whole local node_modules directory.
beside these workaround methods ...
one can install internally published modules from Cloud Source Repositories, via git+https://.
If the goal is to only share module between web and function, you may simple put the shared package under functions as file:react-crud-shared and then reference the package from web using file:../functions/react-crud-shared.
functions/
package.json
...
react-crud-shared/
package.json
...
web/
package.json
...
in functions/package.json
{
"dependencies": {
...
"react-crud-shared": "file:react-crud-shared"
}
}
in web/package.json
{
"dependencies": {
...
"react-crud-shared": "file:../functions/react-crud-shared"
}
}
It works perfectly fine for my case since I use shared protobufjs for cleaner typescript.
What I have found is that the location of your private package has to be inside your cloud functions folder (default to be functions)
So if you move your private package inside your cloud function folder and set the path of that package correctly in package.json, it should work.

How to handle assets in Symfony 4

I am building an application with Symfony 4 and I'd like to follow the best practices for web assets. I use Encore/Webpack for SCSS and JS and it works well; the resulting JS+CSS are nicely stored in /public/build folder. I'm stuck at how to store and use static assets like images, movies, sounds.
Should images be stored in 'public/images' folder or in 'assets/images'?
And some followup questions:
If the images are stored in public/images, will I get any benefit if I pollute the templates with asset('...') calls?
If the images are stored in assets/images, then:
How are they moved into public/images to be served via http? ./bin/console assets:install did nothing, saying: '[OK] No assets were provided by any bundle.'.
How do I use them in SCSS? Via relative paths?
Regards,
Should images be stored in 'public/images' folder or in 'assets/images'?
Everything in public/ is available through the browser. In here, only production ready and build things should be put.
As your images don't need any processing (I assume), you can (should) indeed put the images there.
Now, assume you're needing to do some processing (e.g. ugly JPEG compression), you would put the images in assets/, do some processing and then put only the processed images in public/.
If the images are stored in public/images, will I get any benefit if I pollute the templates with asset('...') calls?
Yes, asset() doesn't have anything to do with Encore or asset build management. The only thing it does is fixing your URLs. This means that if you move your app to sub directories on your server (example.com/app/), the URLs will automatically adapt. Read more about it in the Asset component documentation.
Another good way to reference images with asset() method in Symfony 4 is copying images in public/build when building assets with Encore.
Use copyFiles() in Webpack Encore
Webpack Encore provides a function to copy your images on the public directory to allow asset() to access those files : copyFiles().
In your webpack.config.js
Encore.
...
.copyFiles({
from: './assets/images',
to: 'images/[path][name].[ext]',
pattern: /\.(png|jpg|jpeg)$/
})
Error: Encore.copyFiles is not a recognized property or method.
Please by sure that you are actualy using symfony/webpack-encore-bundle and not
symfony/webpack-encore-pack as described here.
composer require symfony/webpack-encore-bundle
composer remove symfony/webpack-encore-pack
yarn install
yarn upgrade
yarn run watch
My package.json
{
"devDependencies": {
"#symfony/webpack-encore": "^0.22.0",
"bootstrap": "^4.1.3",
"node-sass": "^4.10.0",
"sass-loader": "^7.0.1",
"url-loader": "^1.0.1",
"webpack-notifier": "^1.6.0"
},
"license": "UNLICENSED",
"private": true,
"scripts": {
"dev-server": "encore dev-server",
"dev": "encore dev",
"watch": "encore dev --watch",
"build": "encore production --progress"
}
}
Edit webpack.config.js after
Encore
.setOutputPath('../build/')
Add the following lines. You can also do more configuration by uncommenting the lines
.enableVersioning()
.copyFiles({
from: './assets/images',
// optional target path, relative to the output dir
to: 'images/[path][name].[ext]',
// if versioning is enabled, add the file hash too
//to: 'images/[path][name].[hash:8].[ext]',
// only copy files matching this pattern
//pattern: /\.(png|jpg|jpeg)$/
})
Now you can use image by
Source: https://symfony.com/doc/current/frontend/encore/copy-files.html
Details: http://toihid.com/?p=332

phpunit composer install dependicies via symlink

I am currently installing phpunit on a per project basis.
This works extremely well, however, the install takes time because I install the directories from cache each time. Is there a way to symlink the directories to one install, or some kind of clever trick I can use to make it do that.
If I do a path repository like this, with phpunit cloned and composer installed on my disk:
"require-dev": {
"phpunit/phpunit": "dev-master"
},
"repositories": [
{
"type": "path",
"url": "../phpunit",
"options": {
"symlink": true
}
}
],
This installs only links the phpunit/phpunit directory, and not the rest of the dependencies like:
"phpunit/php-code-coverage": "^5.2",
"phpunit/php-file-iterator": "^1.4",
"phpunit/php-text-template": "^1.2",etc
Composer can install packages globally and then you can refer to the files via it's path, or put the appropriate directory into your PATH variable (in ~/.composer/vendor/bin). Another, often more popular method is to download the phpunit.phar file, and use that to run PHPunit. This also has the advantage of being a single file that can also be committed to a project - though you will want to occasionally update it to the latest version (at least within the major version, 5.7+ or 6.1+). The .Phar file can also be installed globally on the server, in the same way that composer is suggested to.

Is there an offline installer for gruntjs?

I am trying to find a way to intall gruntjs without internet access. Is there a way to download an installer? Stack overflow is giving me a hard time about the format of this question and I"m not sure what else to write. It's a pretty straightforward request.
Sort of. You'll need internet access at some point to acquire the Grunt source from Github. Grunt is a node module, and therefore needs to installed to the node_modules folder in your project. You can do this via npm, but you can also obtain the source for the module and copy it directly into the folder.
Go to: https://github.com/gruntjs/grunt to acquire the Grunt source code, and move it to your node_modules folder.
This will install grunt... but you'll still have problems. Grunt has many dependencies, listed here: https://github.com/gruntjs/grunt/blob/master/package.json
"dependencies": {
"async": "~0.1.22",
"coffee-script": "~1.3.3",
"colors": "~0.6.0-1",
"dateformat": "1.0.2-1.2.3",
"eventemitter2": "~0.4.9",
"findup-sync": "~0.1.0",
"glob": "~3.1.21",
"hooker": "~0.2.3",
"iconv-lite": "~0.2.5",
"minimatch": "~0.2.6",
"nopt": "~1.0.10",
"rimraf": "~2.0.2",
"lodash": "~0.9.0",
"underscore.string": "~2.2.0-rc",
"which": "~1.0.5",
"js-yaml": "~2.0.2",
"exit": "~0.1.0"
},
"devDependencies": {
"temporary": "~0.0.4",
"grunt-contrib-jshint": "~0.6.4",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-bump": "git://github.com/gruntjs/grunt-contrib-bump#b9bfc07",
"difflet": "~0.2.3",
"semver": "2.1.0",
"shelljs": "~0.2.5"
}
}
Each one of these dependencies would also need to be manually installed in the same fashion into a node_modules folder inside of your projects' node_modules/grunt folder. Each of those dependencies may have dependencies of their own, so you would then have to follow the same procedure there.
Because of this, while it's technically feasible to install Grunt without using npm it's certainly not practical.
You can install node on another machine, zip up your node_modules directory and send it to yourself (email, dropbox, whatever channel you have open), and then unpack it on the machine. 7-zip compressed it to about 1.3mb for me.

Resources