Zblack npm integration
Template:Short description Template:Infobox
ZAP and Zblack: Local npm Library Integration
This article demonstrates how ZAP and Zblack enable seamless use of Node.js, npm libraries, and advanced file system/database operations. The workflow allows users to save code, install npm packages, and run scripts interactively, leveraging the full power of server-side JavaScript in the ZAP/Zblack environment.
ZAP: Using Node.js and npm Libraries Overview
On ZAP, Node.js is embedded, allowing users to:
- Save code files to a folder.
- Use
npm installto add external npm libraries to that folder. requireand use those libraries in Node.js scripts.- Access the full file system, databases, and more (unlike browser JavaScript).
Step 1: Create a .z3 Document and Install npm Libraries
Open ZAP and create a new document. Save this document with the .z3 extension in a directory of your choice (for example, test.z3).
Next, open a terminal in that same directory. For each npm package you want to use, run:
npm install <library-name>
This command will create a node_modules folder in your directory and install the specified libraries. For example, to install the underscore library, use:
npm install underscore
Your directory will now contain your .z3 file and a node_modules folder with the installed npm packages, ready for use in your Zblack project.
Step 2: Write Code in the .z3 File Using Installed npm Libraries
Open your .z3 file in the ZAP Document editor.
Use require() to import any installed npm libraries and write your Node.js code as needed.
Step 3: Install Zblack and Set the Path Variable
Download and install Zblack from Zblack.
Add the Zblack executable to your system’s PATH environment variable so you can run it from any terminal window.
Step 4: Run .z3 Files Using Zblack
In your terminal, navigate to the directory containing your .z3 file.
Run your script with the command:
zblack yourfile.z3
To keep the process interactive, add the -i flag:
zblack -i yourfile.z3
This process enables you to develop and execute JavaScript scripts with full npm support in the Zblack environment.
How It Works
- Local npm packages: Any npm package can be installed locally to your project and required in your scripts. This avoids global installs and makes it easy to copy all files under a folder to a different machine for deployment.
- Server-side features: Unlike browser JavaScript, you can use Node.js modules like
fsfor file/database access. - Zblack execution: Save your code as
.z3files and run withzblack -i yourfile.z3for an interactive session. - Flexible scripting: Switch between libraries (e.g.,
lodash,underscore,csv-parse,chance) as needed for your workflow.
Examples
Example 1:
const _ = require('lodash');
console.log(_.shuffle([1,2,3,4,5]));
Example 2:
const _ = require('underscore');
let people = [
{ name: 'Alice', group: 'A' },
{ name: 'Bob', group: 'B' },
{ name: 'Carol', group: 'A' },
{ name: 'Dave', group: 'B' },
{ name: 'Eve', group: 'A' }
];
let groups = _.groupBy(people, 'group');
for (let key in groups) {
groups[key] = _.shuffle(groups[key]);
}
console.log('Grouped and Shuffled:', groups);
Example 3:
const Chance = require('chance');
const chance = new Chance();
// Generate an array of 5 random names
let names = Array.from({ length: 5 }, () => chance.name());
// Shuffle the names
let shuffled = chance.shuffle(names);
console.log('Random Names:', names);
console.log('Shuffled Names:', shuffled);
Example 4:
const fs = require('fs');
const { parse } = require('csv-parse');
fs.createReadStream('sample.csv')
.pipe(parse({ columns: true, skip_empty_lines: true }))
.on('data', (row) => {
console.log(row);
})
.on('end', () => {
console.log('Finished parsing the file');
})
.on('error', (err) => {
console.error('Error:', err.message);
});