Registry / http-networking / centra

centra

JSON →
library2.7.0jsnpmunverified

Centra is a lightweight, promise-based HTTP client specifically designed for Node.js environments. It emphasizes a minimal API surface while providing core functionalities like JSON/form body sending, query parameters, custom headers, timeouts, and response streaming. Currently at version 2.7.0, Centra focuses on developer control and efficiency, offering a lean alternative to more feature-rich clients like Axios or Node-fetch. Its primary differentiator is its small footprint and direct interaction with Node's built-in `http` and `https` modules, making it suitable for performance-critical applications or environments where bundle size is a concern. While it provides a fluent API for common tasks, it also allows direct modification of Node's core HTTP request options for advanced use cases. Release cadence appears to be on-demand rather than fixed, with updates driven by feature needs and bug fixes.

npm install centra
INSTALL
IMPORT
SIG · CENTRA
C
centra
http-networkingjavascriptv2.7.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

centra
const centra = require('centra')
import centra from 'centra'
Centra is a CommonJS-only package. Direct ESM `import` statements are not supported without a transpiler or Node.js's experimental `--experimental-require-module` flag (Node.js 22+).
CentraResponse
const centra = require('centra'); /* ... */ const res = await centra('...').send(); // res is a CentraResponse
The response object returned by `.send()` is a `CentraResponse` instance, providing methods like `.text()`, `.json()`, `.arrayBuffer()`, `.stream()`, and properties like `.statusCode`.
body
centra(url, 'POST').body({ key: 'value' }, 'json').send()
The `.body()` method is used for sending data in the request body. The second argument specifies the content type ('json' or 'form').

This quickstart demonstrates basic GET and POST requests, sending a JSON body, setting a timeout, and streaming a file download using Centra.

const centra = require('centra'); const fs = require('fs'); const path = require('path'); (async () => { try { // Basic GET request and logging text response console.log('Fetching example.com (text response)...'); const textRes = await centra('https://example.com').send(); console.log('Response status:', textRes.statusCode); // console.log('Response body (truncated):', (await textRes.text()).substring(0, 100) + '...'); // POST request with JSON body and handling JSON response console.log('\nSending JSON POST request...'); const jsonRes = await centra('https://jsonplaceholder.typicode.com/posts', 'POST') .header('Content-Type', 'application/json') .body({ title: 'foo', body: 'bar', userId: 1 }, 'json') .timeout(5000) // Set a 5-second timeout .send(); console.log('JSON POST response status:', jsonRes.statusCode); console.log('JSON POST response body:', await jsonRes.json()); // Stream a file download console.log('\nStreaming an image download...'); const imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png'; // Example PNG const imagePath = path.join(__dirname, 'downloaded_image.png'); const imageStream = await centra(imageUrl).send(); if (imageStream.statusCode === 200) { imageStream.stream.pipe(fs.createWriteStream(imagePath)) .on('finish', () => console.log('Image downloaded to', imagePath)) .on('error', (err) => console.error('Stream error:', err)); } else { console.error('Failed to download image, status:', imageStream.statusCode); } } catch (error) { console.error('An error occurred:', error.message); } })();
Debug
Known issues
breakingCentra is a CommonJS-only package. Attempting to `import centra from 'centra'` in an ES Module context will result in a `TypeError: require is not defined` or `ERR_REQUIRE_ESM` unless specific Node.js flags or transpilation are used.
fix
Use `const centra = require('centra')` in CommonJS modules. For ES Modules, consider using a dynamic `import('centra')` or a build step to transpile your code, or ensure your `package.json` does not have `"type": "module"` if intending to use CJS libraries directly.
affects: >=1.0.0
gotchaBy default, Centra does not apply an automatic timeout to requests. Long-running or stalled requests will hang indefinitely, potentially consuming resources. Users must explicitly configure a timeout using the `.timeout(ms)` method.
fix
Always chain a `.timeout(milliseconds)` call before `.send()` to prevent requests from hanging. Example: `centra(url).timeout(5000).send()`.
affects: >=1.0.0
gotchaCentra does not automatically follow HTTP redirects by default. If a server responds with a 3xx status code, Centra will resolve the promise with the redirect response itself, rather than initiating a new request to the redirected URL.
fix
To enable redirect following, use the `.followRedirects(maxRedirects)` method, specifying the maximum number of redirects to follow. Example: `centra(url).followRedirects(5).send()`.
affects: >=1.0.0
gotchaCentra's fluent API allows direct modification of Node's core HTTP request options via `.option(key, value)`. While powerful, incorrectly setting these options can lead to unexpected behavior, connection errors, or security vulnerabilities.
fix
Carefully consult Node.js `http.request` documentation before using `.option()`. Always validate the impact of custom options on connection security, certificates, and agent behavior, especially in production environments.
affects: >=1.0.0
gotchaCentra will resolve the promise even if the HTTP response status code indicates an error (e.g., 4xx or 5xx). The `res.statusCode` property must be explicitly checked to determine if the request was successful from an application perspective.
fix
After `await centra(...).send()`, always check `if (res.statusCode >= 400) { // handle error }`.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined in ES module scope
Attempting to use `require()` in a JavaScript file that Node.js treats as an ES module (e.g., a `.mjs` file or a `.js` file in a package with `"type": "module"` in its `package.json`).
fix
Rename your file to `.cjs` or ensure your `package.json` does not have `"type": "module"` if you intend to use CommonJS. Alternatively, if Node.js 22+ is used, an experimental flag `--experimental-require-module` might allow synchronous `require` of ESM, but `centra` is CJS itself. The most robust fix is to use `const centra = require('centra')` in a CJS context.
TypeError: centra is not a function
This typically occurs if `require('centra')` is used and then `centra` is called as a property (e.g., `const { centra } = require('centra')`), or if `import` is used for a CommonJS default export. Centra's primary export is a function directly.
fix
Ensure you are importing the package as a default function: `const centra = require('centra')`. Centra itself is the function.
Error: Request timed out
The HTTP request exceeded a specified duration without receiving a response, or hung indefinitely because no timeout was configured.
fix
Add a `.timeout(milliseconds)` call to your request chain to ensure requests have a maximum duration. For example, `centra(url).timeout(10000).send()` for a 10-second timeout.
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
The promise returned by `.send()` was rejected (e.g., due to network error, invalid URL, or timeout) and the rejection was not explicitly handled.
fix
Always wrap your `await centra(...).send()` calls in a `try...catch` block within an `async` function, or chain a `.catch(error => { /* handle error */ })` to the promise.
Upgrade
Version history
2.7.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
18
OpenAI (training)
1
Resources