Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sync-request-curl

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sync-request-curl - npm Package Compare versions

Comparing version 1.3.2 to 1.3.3

94

dist/cjs/request.js

@@ -5,24 +5,15 @@ "use strict";

const utils_1 = require("./utils");
const request = (method, url, options = {}) => {
const curl = new node_libcurl_1.Easy();
curl.setOpt(node_libcurl_1.Curl.option.CUSTOMREQUEST, method);
curl.setOpt(node_libcurl_1.Curl.option.TIMEOUT, options.timeout || false);
curl.setOpt(node_libcurl_1.Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || options.followRedirects);
curl.setOpt(node_libcurl_1.Curl.option.MAXREDIRS, options.maxRedirects || Number.MAX_SAFE_INTEGER);
// ======================================================================= //
// Handle query string
// ==========================//
url = options.qs && Object.keys(options.qs).length ? (0, utils_1.handleQs)(url, options.qs) : url;
const handleQueryString = (curl, url, qs) => {
url = qs && Object.keys(qs).length ? (0, utils_1.handleQs)(url, qs) : url;
curl.setOpt(node_libcurl_1.Curl.option.URL, url);
// ======================================================================= //
// Handle headers
// ==========================//
// Incoming headers
const httpHeaders = options.headers
? Object.entries(options.headers)
return url;
};
const handleIncomingHeaders = (curl, headers) => {
return headers
? Object.entries(headers)
.filter(([_, value]) => value !== undefined)
.map(([key, value]) => value === '' ? `${key};` : `${key}: ${value}`)
: [];
// Outgoing headers
const returnedHeaderArray = [];
};
const handleOutgoingHeaders = (curl, returnedHeaderArray) => {
curl.setOpt(node_libcurl_1.Curl.option.HEADERFUNCTION, (headerLine) => {

@@ -32,5 +23,4 @@ returnedHeaderArray.push(headerLine.toString('utf-8').trim());

});
// ======================================================================= //
// Handle JSON/body
// ==========================//
};
const handleBody = (curl, options, buffer, httpHeaders) => {
if (options.json) {

@@ -43,12 +33,8 @@ httpHeaders.push('Content-Type: application/json');

}
let body = Buffer.alloc(0);
curl.setOpt(node_libcurl_1.Curl.option.WRITEFUNCTION, (buff, nmemb, size) => {
body = Buffer.concat([body, buff.slice(0, nmemb * size)]);
buffer.body = Buffer.concat([buffer.body, buff.subarray(0, nmemb * size)]);
return nmemb * size;
});
// ======================================================================= //
// Execute request
// ==========================//
curl.setOpt(node_libcurl_1.Curl.option.HTTPHEADER, httpHeaders);
const code = curl.perform();
};
const checkValidCurlCode = (code, method, url, options) => {
if (code !== node_libcurl_1.CurlCode.CURLE_OK) {

@@ -67,27 +53,37 @@ throw new Error(`

}
};
const checkGetBodyStatus = (statusCode, body) => {
if (statusCode >= 300) {
throw new Error(`
Server responded with status code ${statusCode}
Body: ${body.toString()}
Use 'res.body' instead of 'res.getBody()' to not have any errors thrown.
The status code (in this case, ${statusCode}) can be checked manually with res.statusCode.
`);
}
};
const request = (method, url, options = {}) => {
const curl = new node_libcurl_1.Easy();
curl.setOpt(node_libcurl_1.Curl.option.CUSTOMREQUEST, method);
curl.setOpt(node_libcurl_1.Curl.option.TIMEOUT, options.timeout || false);
curl.setOpt(node_libcurl_1.Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || options.followRedirects);
curl.setOpt(node_libcurl_1.Curl.option.MAXREDIRS, options.maxRedirects || Number.MAX_SAFE_INTEGER);
handleQueryString(curl, url, options.qs);
const httpHeaders = handleIncomingHeaders(curl, options.headers);
const returnedHeaderArray = [];
handleOutgoingHeaders(curl, returnedHeaderArray);
const bufferWrap = { body: Buffer.alloc(0) };
handleBody(curl, options, bufferWrap, httpHeaders);
curl.setOpt(node_libcurl_1.Curl.option.HTTPHEADER, httpHeaders);
const code = curl.perform();
checkValidCurlCode(code, method, url, options);
url = curl.getInfo('EFFECTIVE_URL').data;
// ======================================================================= //
// Finalising return
// ==========================//
const statusCode = curl.getInfo('RESPONSE_CODE').data;
/* istanbul ignore next */
if (typeof statusCode !== 'number' || isNaN(statusCode)) {
throw new Error(`Status code ${statusCode} is either NaN or not of type number! Type: ${typeof statusCode}`);
}
const body = bufferWrap.body;
const headers = (0, utils_1.parseHeaders)(returnedHeaderArray);
const getBody = (encoding) => {
if (statusCode >= 300) {
throw new Error(`
Server responded with status code ${statusCode}
Body: ${body.toString()}
Use 'res.body' instead of 'res.getBody()' to not have any errors thrown.
The status code (in this case, ${statusCode}) can be checked manually with res.statusCode.
`);
}
if (typeof encoding === 'string') {
return body.toString(encoding);
}
return body;
checkGetBodyStatus(statusCode, body);
return typeof encoding === 'string' ? body.toString(encoding) : body;
};

@@ -94,0 +90,0 @@ curl.close();

import { Curl, CurlCode, Easy } from 'node-libcurl';
import { handleQs, parseHeaders } from './utils';
const request = (method, url, options = {}) => {
const curl = new Easy();
curl.setOpt(Curl.option.CUSTOMREQUEST, method);
curl.setOpt(Curl.option.TIMEOUT, options.timeout || false);
curl.setOpt(Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || options.followRedirects);
curl.setOpt(Curl.option.MAXREDIRS, options.maxRedirects || Number.MAX_SAFE_INTEGER);
// ======================================================================= //
// Handle query string
// ==========================//
url = options.qs && Object.keys(options.qs).length ? handleQs(url, options.qs) : url;
const handleQueryString = (curl, url, qs) => {
url = qs && Object.keys(qs).length ? handleQs(url, qs) : url;
curl.setOpt(Curl.option.URL, url);
// ======================================================================= //
// Handle headers
// ==========================//
// Incoming headers
const httpHeaders = options.headers
? Object.entries(options.headers)
return url;
};
const handleIncomingHeaders = (curl, headers) => {
return headers
? Object.entries(headers)
.filter(([_, value]) => value !== undefined)
.map(([key, value]) => value === '' ? `${key};` : `${key}: ${value}`)
: [];
// Outgoing headers
const returnedHeaderArray = [];
};
const handleOutgoingHeaders = (curl, returnedHeaderArray) => {
curl.setOpt(Curl.option.HEADERFUNCTION, (headerLine) => {

@@ -29,5 +20,4 @@ returnedHeaderArray.push(headerLine.toString('utf-8').trim());

});
// ======================================================================= //
// Handle JSON/body
// ==========================//
};
const handleBody = (curl, options, buffer, httpHeaders) => {
if (options.json) {

@@ -40,12 +30,8 @@ httpHeaders.push('Content-Type: application/json');

}
let body = Buffer.alloc(0);
curl.setOpt(Curl.option.WRITEFUNCTION, (buff, nmemb, size) => {
body = Buffer.concat([body, buff.slice(0, nmemb * size)]);
buffer.body = Buffer.concat([buffer.body, buff.subarray(0, nmemb * size)]);
return nmemb * size;
});
// ======================================================================= //
// Execute request
// ==========================//
curl.setOpt(Curl.option.HTTPHEADER, httpHeaders);
const code = curl.perform();
};
const checkValidCurlCode = (code, method, url, options) => {
if (code !== CurlCode.CURLE_OK) {

@@ -64,27 +50,37 @@ throw new Error(`

}
};
const checkGetBodyStatus = (statusCode, body) => {
if (statusCode >= 300) {
throw new Error(`
Server responded with status code ${statusCode}
Body: ${body.toString()}
Use 'res.body' instead of 'res.getBody()' to not have any errors thrown.
The status code (in this case, ${statusCode}) can be checked manually with res.statusCode.
`);
}
};
const request = (method, url, options = {}) => {
const curl = new Easy();
curl.setOpt(Curl.option.CUSTOMREQUEST, method);
curl.setOpt(Curl.option.TIMEOUT, options.timeout || false);
curl.setOpt(Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || options.followRedirects);
curl.setOpt(Curl.option.MAXREDIRS, options.maxRedirects || Number.MAX_SAFE_INTEGER);
handleQueryString(curl, url, options.qs);
const httpHeaders = handleIncomingHeaders(curl, options.headers);
const returnedHeaderArray = [];
handleOutgoingHeaders(curl, returnedHeaderArray);
const bufferWrap = { body: Buffer.alloc(0) };
handleBody(curl, options, bufferWrap, httpHeaders);
curl.setOpt(Curl.option.HTTPHEADER, httpHeaders);
const code = curl.perform();
checkValidCurlCode(code, method, url, options);
url = curl.getInfo('EFFECTIVE_URL').data;
// ======================================================================= //
// Finalising return
// ==========================//
const statusCode = curl.getInfo('RESPONSE_CODE').data;
/* istanbul ignore next */
if (typeof statusCode !== 'number' || isNaN(statusCode)) {
throw new Error(`Status code ${statusCode} is either NaN or not of type number! Type: ${typeof statusCode}`);
}
const body = bufferWrap.body;
const headers = parseHeaders(returnedHeaderArray);
const getBody = (encoding) => {
if (statusCode >= 300) {
throw new Error(`
Server responded with status code ${statusCode}
Body: ${body.toString()}
Use 'res.body' instead of 'res.getBody()' to not have any errors thrown.
The status code (in this case, ${statusCode}) can be checked manually with res.statusCode.
`);
}
if (typeof encoding === 'string') {
return body.toString(encoding);
}
return body;
checkGetBodyStatus(statusCode, body);
return typeof encoding === 'string' ? body.toString(encoding) : body;
};

@@ -91,0 +87,0 @@ curl.close();

@@ -7,3 +7,3 @@ {

},
"version": "1.3.2",
"version": "1.3.3",
"files": [

@@ -16,3 +16,3 @@ "dist"

"start": "ts-node tests/server",
"dev": "ts-node-dev tests/server",
"dev": "ts-node-dev --watch tests/app tests/server",
"test": "jest",

@@ -19,0 +19,0 @@ "test:coverage": "jest --coverage",

# sync-request-curl
[![Pipeline](https://github.com/nktnet1/sync-request-curl/actions/workflows/pipeline.yml/badge.svg)](https://github.com/nktnet1/sync-request-curl/actions/workflows/pipeline.yml)
[![pipeline](https://github.com/nktnet1/sync-request-curl/actions/workflows/pipeline.yml/badge.svg)](https://github.com/nktnet1/sync-request-curl/actions/workflows/pipeline.yml)
 
[![codecov](https://codecov.io/gh/nktnet1/sync-request-curl/branch/main/graph/badge.svg?token=RAC7SKJTGU)](https://codecov.io/gh/nktnet1/sync-request-curl)
 
![NPM License](https://img.shields.io/npm/l/sync-request-curl)
 
![NPM Downloads](https://img.shields.io/npm/dw/sync-request-curl)
 
![Release](https://img.shields.io/npm/v/sync-request-curl)
 
[![Maintainability](https://api.codeclimate.com/v1/badges/3ec8c0ddebe848926277/maintainability)](https://codeclimate.com/github/nktnet1/sync-request-curl/maintainability)

@@ -84,3 +92,2 @@ Make synchronous web requests similar to [sync-request](https://github.com/ForbesLindesay/sync-request), but 20 times more quickly.

);
console.log('Status Code:', response.statusCode);

@@ -141,3 +148,3 @@ const jsonBody = JSON.parse(response.body.toString());

<td>
<code>{ email: 'example@email.com', password: 'comp1531' }</code></td>
<code>{ email: 'ab@c.com', password: 'comp1531' }</code></td>
</tr>

@@ -149,3 +156,3 @@ <tr>

</td>
<td><code>JSON.stringify({ email: 'ab@cd.com', password: 'comp1531' })</code></td>
<td><code>JSON.stringify({ email: 'ab@c.com', password: 'comp1531' })</code></td>
</tr>

@@ -152,0 +159,0 @@ <tr>

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc