Difference between revisions of "Zblack npm integration"
(Created page with "=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...") |
|||
| Line 56: | Line 56: | ||
==How It Works== | ==How It Works== | ||
| − | Local npm packages: As shown in the node_modules screenshot, you can install any npm package locally to your project and require it in your scripts. | + | Local npm packages: As shown in the node_modules screenshot, you can install any npm package locally to your project and require it in your scripts. Any npm library related to a project file can be installed into the same folder (which will appear in the node_modules subfolder), simply with: |
| + | |||
| + | npm install <module> | ||
| + | |||
| + | This avoid global install of related libraries, and makes it easy to simply copy all files under a folder to a different machine for easy deployment. | ||
Server-side features: Unlike browser JavaScript, you can use Node.js modules like fs for file/database access. | Server-side features: Unlike browser JavaScript, you can use Node.js modules like fs for file/database access. | ||
Revision as of 13:41, 9 June 2025
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 you to save code, install npm packages, and run scripts interactively, leveraging the full power of server-side JavaScript in your ZAP/Zblack environment.
ZAP: Using Node.js and npm Libraries Overview
On ZAP, Node.js is embedded, allowing you to:
Save code files to a folder.
Use npm install to add external npm libraries to that folder.
require and use those libraries in your Node.js scripts.
Access the full file system, databases, and more (unlike browser JS).
Step 1: Create a .z3 Document and Install npm Libraries
Open ZAP (ZAP Download), 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 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 the 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:
text 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: As shown in the node_modules screenshot, you can install any npm package locally to your project and require it in your scripts. Any npm library related to a project file can be installed into the same folder (which will appear in the node_modules subfolder), simply with:
npm install <module>
This avoid global install of related libraries, and makes it easy to simply copy all files under a folder to a different machine for easy deployment.
Server-side features: Unlike browser JavaScript, you can use Node.js modules like fs for file/database access.
Zblack execution: Save your code as .z3 files and run with zblack -i yourfile.z3 for an interactive session.
Flexible scripting: Switch between libraries (lodash, underscore, csv-parse, chance, etc.) 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);
});