Skip to content

Commit

Permalink
blazed.js version 1.2.5
Browse files Browse the repository at this point in the history
Patch Notes -
1. Made some minor bug fixes and feature improvements.
2. Improved Readme.md
  • Loading branch information
BlazeInferno64 authored Dec 20, 2024
1 parent 132bd63 commit 5377901
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
[![NPM Downloads](https://img.shields.io/npm/dm/blazed.js.svg?style=round-square)](https://npm-stat.com/charts.html?package=blazed.js)
[![NPM Version](http://img.shields.io/npm/v/blazed.js.svg?style=flat)](https://npmjs.com/package/blazed.js)
[![install size](https://packagephobia.com/badge?p=blazed.js)](https://packagephobia.com/result?p=blazed.js)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/blazed.js?style=round-square)](https://bundlephobia.com/package/blazed.js@latest)
[![Gitpod Ready-to-code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod&style=round-square)](https://gitpod.io/#https://github.com/blazeinferno64/blazed.js)


# blazed.js

Blazing-fast, light weight, high-performance, promise-based HTTP client

# Setup/Installation
# Installation

To get started with `blazed.js`, simply run the following command in your terminal:

Expand All @@ -20,7 +27,7 @@ If you're not familiar with promises, check out the [MDN documentation](https://

With `blazed.js`, you can send advanced HTTP requests directly from your Node.js app, featuring automatic JSON parsing and more.

## Built on Top of Node.js HTTP
## Built on Top of Node.js HTTP library

Under the hood, `blazed.js` leverages the built-in [HTTP library](https://nodejs.org/api/http.html) in Node.js, ensuring a seamless and efficient experience.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blazed.js",
"version": "1.2.1",
"version": "1.2.5",
"main": "index.js",
"types": "typings/index.d.ts",
"scripts": {
Expand Down Expand Up @@ -39,7 +39,8 @@
"git",
"htmx",
"www",
"npm"
"npm",
"high-peformance"
],
"jest": {
"testMatch": [
Expand Down
30 changes: 17 additions & 13 deletions src/blazed.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Author(s) -> BlazeInferno64
//
// Last updated: 07/12/2024
// Last updated: 20/12/2024

"use strict";

Expand All @@ -12,7 +12,6 @@ const https = require('https');
const { dataUriToBuffer } = require("data-uri-to-buffer");
const { Buffer } = require("buffer");
const { EventEmitter } = require("events");
const { URL } = require("url");
const emitter = new EventEmitter();

const urlParser = require("./utils/url");
Expand Down Expand Up @@ -47,6 +46,10 @@ const _makeRequest = (method, url, data, headers = {}, redirectCount = 5, timeou
(async () => {
try {
const parsedURL = await urlParser.parseThisURL(url, method);
// Validate whether it has supported protocol or not.
if (!supportedSchemas.has(parsedURL.protocol)) {
throw new Error(`${packageJson ? packageJson.name : 'blazed.js'} cannot load the given url '${url}'. URL scheme "${parsedURL.protocol.replace(/:$/, '')}" is not supported.`)
}
// Handle 'data:' URLs directly
if (parsedURL.protocol === 'data:') {
const myData = dataUriToBuffer(url);
Expand All @@ -59,10 +62,6 @@ const _makeRequest = (method, url, data, headers = {}, redirectCount = 5, timeou
};
return resolve(responseObject);
}
// Validate whether it has supported protocol or not
if (!supportedSchemas.has(parsedURL.protocol)) {
throw new Error(`${packageJson ? packageJson.name : 'blazed.js'} cannot load the given url: ${url}. URL scheme "${parsedURL.protocol.replace(/:$/, '')}" is not supported.`)
}
// Validate every HTTP headers provided by the user
for (const key in headers) {
await headerParser.parseThisHeaderName(key);
Expand All @@ -76,16 +75,19 @@ const _makeRequest = (method, url, data, headers = {}, redirectCount = 5, timeou
'Connection': 'keep-alive',
'Cache-Control': 'no-cache',
'Content-Length': 0,
'X-Requested-With': `${packageJson ? packageJson.name : 'blazed.js'}`,
...headers, // Spread the user-provided headers
},
};

// Since some web servers block HTTP requests without User-Agent header
// Since some web servers block HTTP requests without 'User-Agent' header
// Therefore add a custom User-Agent header by default if not provided
if (!requestOptions.headers['User-Agent']) {
requestOptions.headers['User-Agent'] = packageJson ? `${packageJson.name}/v${packageJson.version}` : 'blazed.js';
}
// Optionally add another HTTP header named 'X-Requested-With'
if (!requestOptions.headers['X-Requested-With']) {
requestOptions.headers['X-Requested-With'] = `${packageJson ? packageJson.name : 'blazed.js'}`;
}
// Also if any data is present then add some extra headers to the HTTP request
if (data) {
requestOptions.headers['Content-Length'] = Buffer.byteLength(JSON.stringify(data));
Expand All @@ -96,7 +98,7 @@ const _makeRequest = (method, url, data, headers = {}, redirectCount = 5, timeou
emitter.emit("beforeRequest", url, requestOptions);

// Main request part of blazed.js for handling any ongoing requests
const request = httpModule.request(new URL(url), requestOptions, (response) => {
const request = httpModule.request(parsedURL, requestOptions, (response) => {
handleResponse(response, resolve, reject, redirectCount = 5, url, data, method, headers, requestOptions, request, parsedURL);
});

Expand All @@ -117,7 +119,7 @@ const _makeRequest = (method, url, data, headers = {}, redirectCount = 5, timeou
if (method === HTTP_METHODS.CONNECT) {
const info = await urlParser.parseThisURL(url);
const connectionInfo = {
message: `Successfully established a connection to "${url}"`,
message: `Connection successfull to "${url}"`,
protocol: info.protocol.replace(":", ""),
remoteAddress: request.socket.remoteAddress,
remotePort: request.socket.remotePort,
Expand Down Expand Up @@ -255,13 +257,15 @@ const handleResponse = (response, resolve, reject, redirectCount = 5, originalUr
response.on('error', reject);
if (response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) {
if (redirectCount > 0) {
const redirectUrl = response.headers.location;
// Check if the response has an locatiin header or not.
// If its present then construct another HTTP redirect URL using it.
const redirectUrl = response.headers['location'] || response.headers.location;

if (!redirectUrl) {
const err = `Undefined_Redirect_Location`;
// Process the undefined redirect error
// Process the undefined redirect error.
return reject(async () => {
await utilErrors.processError(err, originalUrl, false, false,false, method, reject);
await utilErrors.processError(err, originalUrl, false, false, false, method, reject);
});
}
const redirObj = {
Expand Down

0 comments on commit 5377901

Please sign in to comment.