======================== CODE SNIPPETS ======================== TITLE: Setup and Build node-libcurl-ja3 DESCRIPTION: Instructions for setting up the development environment and building the node-libcurl-ja3 addon. This includes installing dependencies, building the addon, and rebuilding if necessary. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/CONTRIBUTING.md#_snippet_0 LANGUAGE: Shell CODE: ``` $ scripts/build.sh ``` LANGUAGE: Shell CODE: ``` $ yarn install ``` LANGUAGE: Shell CODE: ``` $ yarn pregyp build ``` LANGUAGE: Shell CODE: ``` $ yarn pregyp rebuild ``` ---------------------------------------- TITLE: Start Local Server DESCRIPTION: Starts a local server for benchmarking purposes. This command is typically used to set up the environment before running performance tests. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/benchmark/README.md#_snippet_0 LANGUAGE: bash CODE: ``` yarn start-server ``` ---------------------------------------- TITLE: Install node-libcurl-ja3 with yarn DESCRIPTION: Installs the node-libcurl-ja3 package using yarn, setting an environment variable to force a build from source. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_13 LANGUAGE: sh CODE: ``` npm_config_build_from_source=true yarn add node-libcurl-ja3 ``` ---------------------------------------- TITLE: Start Benchmark DESCRIPTION: Initiates the benchmark tests to measure the performance of different HTTP clients. This command executes the predefined performance tests. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/benchmark/README.md#_snippet_1 LANGUAGE: bash CODE: ``` yarn start ``` ---------------------------------------- TITLE: Install node-libcurl-ja3 DESCRIPTION: Installs the node-libcurl-ja3 package using either npm or yarn. This is the first step to using the library in a Node.js project. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_0 LANGUAGE: shell CODE: ``` npm i node-libcurl-ja3 --save yarn add node-libcurl-ja3 ``` ---------------------------------------- TITLE: Install node-libcurl-ja3 with npm DESCRIPTION: Installs the node-libcurl-ja3 package using npm, forcing a build from source if prebuilt binaries are not suitable or available. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_12 LANGUAGE: sh CODE: ``` npm install node-libcurl-ja3 --build-from-source ``` ---------------------------------------- TITLE: Install Xcode Command Line Tools on macOS DESCRIPTION: Installs the Xcode Command Line Tools on macOS if they are not already present. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_16 LANGUAGE: sh CODE: ``` xcode-select --install ``` ---------------------------------------- TITLE: Install build dependencies on macOS using Homebrew DESCRIPTION: Installs the required build tools and libraries on macOS using the Homebrew package manager. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_17 LANGUAGE: sh CODE: ``` brew install automake bash cmake libtool make meson ninja ``` ---------------------------------------- TITLE: Install build dependencies on Debian-based Linux DESCRIPTION: Installs the necessary development tools and libraries required to compile node-libcurl-ja3 on Debian-based Linux distributions. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_14 LANGUAGE: bash CODE: ``` sudo apt install -qqy autoconf automake build-essential cmake curl libtool ninja-build pkg-config ``` ---------------------------------------- TITLE: Simple GET Request - Async/Await DESCRIPTION: Demonstrates a simple GET request to 'http://www.google.com' using the async/await syntax provided by the 'curly' helper. It retrieves the status code, data, and headers of the response. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_3 LANGUAGE: javascript CODE: ``` const { curly } = require('node-libcurl-ja3'); const { statusCode, data, headers } = await curly.get('http://www.google.com') ``` ---------------------------------------- TITLE: Check for Xcode Command Line Tools on macOS DESCRIPTION: Verifies if the Xcode Command Line Tools are installed on macOS by checking for their installation path. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_15 LANGUAGE: sh CODE: ``` xcode-select -p ``` ---------------------------------------- TITLE: Node.js HTTP Client Benchmarks (GET) DESCRIPTION: This snippet presents benchmark results for different Node.js HTTP clients performing GET requests. It compares native http.request, axios, superagent, request, fetch, and various configurations of node-libcurl. The results show node-libcurl's superior performance, especially with its Curl and reusing instance options. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/benchmark/README.md#_snippet_2 LANGUAGE: text CODE: ``` node.js http.request - GET x 3.42 ops/sec ±0.95% (21 runs sampled) axios - GET x 3.44 ops/sec ±1.16% (21 runs sampled) superagent - GET x 3.41 ops/sec ±1.31% (21 runs sampled) request - GET x 3.43 ops/sec ±1.37% (21 runs sampled) fetch - GET x 3.38 ops/sec ±0.83% (21 runs sampled) node-libcurl curly - GET x 6.61 ops/sec ±5.47% (36 runs sampled) node-libcurl Curl - GET x 6.82 ops/sec ±0.09% (36 runs sampled) node-libcurl Curl - reusing instance - GET x 6.80 ops/sec ±0.10% (36 runs sampled) node-libcurl Easy - GET x 3.46 ops/sec ±1.18% (13 runs sampled) node-libcurl Easy - reusing instance - GET x 3.44 ops/sec ±1.02% (21 runs sampled) Fastest is node-libcurl Curl - GET real 1m5,417s user 0m3,469s sys 0m0,229s ``` ---------------------------------------- TITLE: Simple Impersonate Request - Async/Await DESCRIPTION: Demonstrates how to make a GET request impersonating a Chrome browser using the 'curly' API. It fetches JSON data from a specified URL and logs the JA3N hash from the response. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_1 LANGUAGE: javascript CODE: ``` const { Browser, impersonate } = require('node-libcurl-ja3'); const curly = impersonate(Browser.Chrome); const { data } = await curly.get('https://tls.browserleaks.com/json'); console.log(data.ja3n_hash); // 8e19337e7524d2573be54efb2b0784c9 ``` ---------------------------------------- TITLE: Simple Impersonate Request - Curl Class DESCRIPTION: Shows how to perform an HTTP GET request impersonating a Chrome browser using the Curl class. It configures the URL, follows redirects, and handles the response data and errors, logging status code and data length. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_2 LANGUAGE: javascript CODE: ``` const { Browser, Curl } = require('node-libcurl-ja3'); const curl = Curl.impersonate(Browser.Chrome); curl.setOpt('URL', 'tls.browserleaks.com/json'); curl.setOpt('FOLLOWLOCATION', true); curl.on('end', function (statusCode, data, headers) { console.info(statusCode); console.info('---'); console.info(data.length); console.info('---'); console.info(this.getInfo('TOTAL_TIME')); this.close(); }); curl.on('error', curl.close.bind(curl)); curl.perform(); ``` ---------------------------------------- TITLE: Publishing Releases with Yarn DESCRIPTION: This snippet shows how to publish a release using Yarn, which can be an alternative if `np` encounters issues. It's a straightforward command to publish the package. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/CONTRIBUTING.md#_snippet_3 LANGUAGE: bash CODE: ``` yarn publish ``` ---------------------------------------- TITLE: Publishing Prereleases with np, skipping Yarn cleanup DESCRIPTION: This snippet shows how to publish a prerelease using `np` while skipping Yarn's cleanup process and using npm for publishing. This is useful if `np` has issues running with Yarn. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/CONTRIBUTING.md#_snippet_7 LANGUAGE: shell CODE: ``` yarn np prerelease --no-yarn --no-cleanup --any-branch --tag next ``` ---------------------------------------- TITLE: Publishing Prereleases with np and Yarn DESCRIPTION: This snippet demonstrates how to publish a prerelease version using `np` from the `develop` branch. It includes options to specify the branch and a tag for the prerelease. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/CONTRIBUTING.md#_snippet_6 LANGUAGE: shell CODE: ``` yarn np prerelease --any-branch --tag next ``` ---------------------------------------- TITLE: Publishing Releases with npm DESCRIPTION: This snippet provides the commands to publish a release using npm, serving as a fallback if both `np` and `yarn` have problems. It involves versioning the package and then publishing it. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/CONTRIBUTING.md#_snippet_4 LANGUAGE: bash CODE: ``` npm version [major|minor|patch] npm publish ``` ---------------------------------------- TITLE: Debugging with lldb and llnode DESCRIPTION: Steps to set up and use lldb with the llnode plugin for debugging Node.js applications, particularly for core dumps. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/CONTRIBUTING.md#_snippet_1 LANGUAGE: Shell CODE: ``` sudo apt-get install lldb ``` LANGUAGE: Shell CODE: ``` npm i -g llnode ``` LANGUAGE: Shell CODE: ``` llnode -- /path/to/bin/node --abort_on_uncaught_exception script.js ``` ---------------------------------------- TITLE: Pushing Tags and Merging Branches DESCRIPTION: This snippet covers the final steps after publishing a release, including pushing tags, checking out the develop branch, merging master into develop, and pushing the changes. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/CONTRIBUTING.md#_snippet_5 LANGUAGE: bash CODE: ``` git push --follow-tags git checkout develop git merge master git push ``` ---------------------------------------- TITLE: Publishing Major/Minor/Patch Releases with np DESCRIPTION: This snippet demonstrates the commands to publish major, minor, or patch releases using the `np` tool. It includes checking out the master branch, merging from develop, and then executing `np` with the desired version increment. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/CONTRIBUTING.md#_snippet_2 LANGUAGE: bash CODE: ``` git checkout master git merge develop npx np [major|minor|patch] ``` ---------------------------------------- TITLE: Simple Request - Curl Class DESCRIPTION: Demonstrates making a request using the Curl class directly. It sets the URL and FOLLOWLOCATION options, then uses event listeners for 'end' and 'error' to handle the response and errors. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_6 LANGUAGE: javascript CODE: ``` const { Curl } = require('node-libcurl-ja3'); const curl = new Curl(); curl.setOpt('URL', 'www.google.com'); curl.setOpt('FOLLOWLOCATION', true); curl.on('end', function (statusCode, data, headers) { console.info(statusCode); console.info('---'); console.info(data.length); console.info('---'); console.info(this.getInfo( 'TOTAL_TIME')); this.close(); }); curl.on('error', curl.close.bind(curl)); curl.perform(); ``` ---------------------------------------- TITLE: Handling Binary Data - WRITEFUNCTION DESCRIPTION: Explains how to handle binary data by providing a custom WRITEFUNCTION callback. This function receives the buffer, size, and number of members, allowing for custom data processing. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_10 LANGUAGE: javascript CODE: ``` curl.setOpt('WRITEFUNCTION', (buffer, size, nmemb) => { // something }) ``` ---------------------------------------- TITLE: POST Request with Form Data - Async/Await DESCRIPTION: Shows how to perform a POST request with form-encoded data using the 'curly' helper and async/await. It sends data to 'http://httpbin.com/post' and demonstrates using 'postFields' or 'POSTFIELDS' for the data. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_4 LANGUAGE: javascript CODE: ``` const querystring = require('querystring'); const { curly } = require('node-libcurl-ja3'); const { statusCode, data, headers } = await curly.post('http://httpbin.com/post', { postFields: querystring.stringify({ field: 'value', }), // can use `postFields` or `POSTFIELDS` }) ``` ---------------------------------------- TITLE: JSON POST Request - Async/Await DESCRIPTION: Illustrates sending a JSON payload in a POST request using the 'curly' helper and async/await. It sets the 'Content-Type' and 'Accept' headers to 'application/json' and logs the received data. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_5 LANGUAGE: javascript CODE: ``` const { curly } = require('node-libcurl-ja3') const { data } = await curly.post('http://httpbin.com/post', { postFields: JSON.stringify({ field: 'value' }), httpHeader: ['Content-Type: application/json', 'Accept: application/json' ], }) console.log(data) ``` ---------------------------------------- TITLE: Create Certificate File and Use with node-libcurl-ja3 DESCRIPTION: This JavaScript snippet demonstrates how to create a certificate file ('cert.pem') using Node.js's built-in `tls.rootCertificates` and then use this file with `node-libcurl-ja3` by setting the `caInfo` option. This is a common solution for SSL certificate verification issues. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/COMMON_ISSUES.md#_snippet_0 LANGUAGE: javascript CODE: ``` const fs = require('fs') const path = require('path') const tls = require('tls') const { curly } = require('node-libcurl-ja3') // important steps const certFilePath = path.join(__dirname, 'cert.pem') const tlsData = tls.rootCertificates.join('\n') fs.writeFileSync(certFilePath, tlsData) async function run() { return curly.post('https://httpbin.org/anything', { postFields: JSON.stringify({ a: 'b' }), httpHeader: ['Content-type: application/json'], caInfo: certFilePath, verbose: true, }) } run() .then(({ data, statusCode, headers }) => console.log( require('util').inspect( { data: JSON.parse(data), statusCode, headers, }, null, 4, ), ), ) .catch((error) => console.error(`Something went wrong`, { error })) ``` ---------------------------------------- TITLE: Handling Binary Data - Enable Features DESCRIPTION: Shows alternative methods for handling binary data by enabling specific features: CurlFeature.NoDataParsing or CurlFeature.Raw. This prevents the default UTF8 decoding of data and headers. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_11 LANGUAGE: javascript CODE: ``` curl.enable(CurlFeature.NoDataParsing) // or curl.enable(CurlFeature.Raw) ``` ---------------------------------------- TITLE: Setting HTTP Headers - Curl Class DESCRIPTION: Shows how to set custom HTTP headers for a request using the Curl class. It demonstrates setting the 'Content-Type' header using the Curl.option.HTTPHEADER enum. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_7 LANGUAGE: javascript CODE: ``` curl.setOpt(Curl.option.HTTPHEADER, ['Content-Type: application/x-amz-json-1.1']) ``` ---------------------------------------- TITLE: Multipart Upload (multipart/form-data) - Curl Class DESCRIPTION: Illustrates performing a multipart/form-data upload using the Curl class. It sets the URL and uses the HTTPPOST option to specify file uploads and field contents. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_9 LANGUAGE: javascript CODE: ``` const { Curl } = require('node-libcurl-ja3'); const curl = new Curl(); const close = curl.close.bind(curl); curl.setOpt(Curl.option.URL, '127.0.0.1/upload.php'); curl.setOpt(Curl.option.HTTPPOST, [ { name: 'input-name', file: '/file/path', type: 'text/html' }, { name: 'input-name2', contents: 'field-contents' } ]); curl.on('end', close); curl.on('error', close); ``` ---------------------------------------- TITLE: Form Submission (application/x-www-form-urlencoded) - Curl Class DESCRIPTION: Demonstrates submitting form data with 'Content-Type: application/x-www-form-urlencoded' using the Curl class. It sets the URL, POST option, and POSTFIELDS with querystring-encoded data. SOURCE: https://github.com/andrewmackrodt/node-libcurl-ja3/blob/develop/README.md#_snippet_8 LANGUAGE: javascript CODE: ``` const querystring = require('querystring'); const { Curl } = require('node-libcurl-ja3'); const curl = new Curl(); const close = curl.close.bind(curl); curl.setOpt(Curl.option.URL, '127.0.0.1/upload'); curl.setOpt(Curl.option.POST, true) curl.setOpt(Curl.option.POSTFIELDS, querystring.stringify({ field: 'value', })); curl.on('end', close); curl.on('error', close); ```