sync-request-curl
Advanced tools
Comparing version 1.4.2 to 1.5.0
import request from './request'; | ||
export { HttpVerb, BufferEncoding, Options, Response } from './types'; | ||
export type { HttpVerb, BufferEncoding, Options, Response, SetEasyOptionCallback } from './types'; | ||
export default request; |
@@ -36,5 +36,6 @@ "use strict"; | ||
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.TIMEOUT, options.timeout || 0); | ||
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); | ||
curl.setOpt(node_libcurl_1.Curl.option.SSL_VERIFYPEER, Boolean(options.insecure)); | ||
// Query string parameters | ||
@@ -49,4 +50,7 @@ handleQueryString(curl, url, options.qs); | ||
handleBody(curl, options, bufferWrap, httpHeaders); | ||
curl.setOpt(node_libcurl_1.Curl.option.HTTPHEADER, httpHeaders); | ||
if (options.setEasyOptions) { | ||
options.setEasyOptions(curl, node_libcurl_1.Curl.option); | ||
} | ||
// Execute request | ||
curl.setOpt(node_libcurl_1.Curl.option.HTTPHEADER, httpHeaders); | ||
const code = curl.perform(); | ||
@@ -53,0 +57,0 @@ (0, utils_1.checkValidCurlCode)(code, method, url, options); |
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { IncomingHttpHeaders } from 'http'; | ||
import { CurlOption, Easy } from 'node-libcurl'; | ||
export type HttpVerb = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'; | ||
export type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex'; | ||
export interface SetEasyOptionCallback { | ||
(curl: Easy, curlOption: CurlOption): void; | ||
} | ||
export interface Options { | ||
@@ -12,6 +16,8 @@ headers?: IncomingHttpHeaders; | ||
json?: any; | ||
body?: string | Buffer; | ||
timeout?: number; | ||
body?: string | Buffer; | ||
followRedirects?: boolean; | ||
maxRedirects?: number; | ||
insecure?: boolean; | ||
setEasyOptions?: SetEasyOptionCallback; | ||
} | ||
@@ -18,0 +24,0 @@ export type GetBody = <encoding extends BufferEncoding | undefined>(arg?: encoding) => encoding extends BufferEncoding ? string : Buffer; |
@@ -44,4 +44,4 @@ "use strict"; | ||
throw new Error(` | ||
Curl request failed with code ${code} | ||
Please look up libcurl error code! | ||
Curl request failed with code ${code}. | ||
Please look up the Libcurl Error (code ${code}) here: | ||
- https://curl.se/libcurl/c/libcurl-errors.html | ||
@@ -48,0 +48,0 @@ |
import request from './request'; | ||
export { HttpVerb, BufferEncoding, Options, Response } from './types'; | ||
export type { HttpVerb, BufferEncoding, Options, Response, SetEasyOptionCallback } from './types'; | ||
export default request; |
@@ -34,5 +34,6 @@ import { Curl, Easy } from 'node-libcurl'; | ||
curl.setOpt(Curl.option.CUSTOMREQUEST, method); | ||
curl.setOpt(Curl.option.TIMEOUT, options.timeout || false); | ||
curl.setOpt(Curl.option.TIMEOUT, options.timeout || 0); | ||
curl.setOpt(Curl.option.FOLLOWLOCATION, options.followRedirects === undefined || options.followRedirects); | ||
curl.setOpt(Curl.option.MAXREDIRS, options.maxRedirects || Number.MAX_SAFE_INTEGER); | ||
curl.setOpt(Curl.option.SSL_VERIFYPEER, Boolean(options.insecure)); | ||
// Query string parameters | ||
@@ -47,4 +48,7 @@ handleQueryString(curl, url, options.qs); | ||
handleBody(curl, options, bufferWrap, httpHeaders); | ||
curl.setOpt(Curl.option.HTTPHEADER, httpHeaders); | ||
if (options.setEasyOptions) { | ||
options.setEasyOptions(curl, Curl.option); | ||
} | ||
// Execute request | ||
curl.setOpt(Curl.option.HTTPHEADER, httpHeaders); | ||
const code = curl.perform(); | ||
@@ -51,0 +55,0 @@ checkValidCurlCode(code, method, url, options); |
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { IncomingHttpHeaders } from 'http'; | ||
import { CurlOption, Easy } from 'node-libcurl'; | ||
export type HttpVerb = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'; | ||
export type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex'; | ||
export interface SetEasyOptionCallback { | ||
(curl: Easy, curlOption: CurlOption): void; | ||
} | ||
export interface Options { | ||
@@ -12,6 +16,8 @@ headers?: IncomingHttpHeaders; | ||
json?: any; | ||
body?: string | Buffer; | ||
timeout?: number; | ||
body?: string | Buffer; | ||
followRedirects?: boolean; | ||
maxRedirects?: number; | ||
insecure?: boolean; | ||
setEasyOptions?: SetEasyOptionCallback; | ||
} | ||
@@ -18,0 +24,0 @@ export type GetBody = <encoding extends BufferEncoding | undefined>(arg?: encoding) => encoding extends BufferEncoding ? string : Buffer; |
@@ -38,4 +38,4 @@ import { CurlCode } from 'node-libcurl'; | ||
throw new Error(` | ||
Curl request failed with code ${code} | ||
Please look up libcurl error code! | ||
Curl request failed with code ${code}. | ||
Please look up the Libcurl Error (code ${code}) here: | ||
- https://curl.se/libcurl/c/libcurl-errors.html | ||
@@ -42,0 +42,0 @@ |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"files": [ | ||
@@ -19,4 +19,4 @@ "dist" | ||
"test:coverage": "jest --coverage", | ||
"lint": "eslint './**/*.{ts,js}'", | ||
"lint:fix": "eslint --fix './**/*.{ts,js}'", | ||
"lint": "eslint './**/*.ts'", | ||
"lint:fix": "eslint --fix './**/*.ts'", | ||
"build": "rm -rf dist && npm run build:esm && npm run build:cjs", | ||
@@ -61,4 +61,4 @@ "build:esm": "tsc", | ||
"@types/node": "^20.4.8", | ||
"@typescript-eslint/eslint-plugin": "^6.2.1", | ||
"@typescript-eslint/parser": "^6.2.1", | ||
"@typescript-eslint/eslint-plugin": "^6.3.0", | ||
"@typescript-eslint/parser": "^6.3.0", | ||
"eslint": "^8.46.0", | ||
@@ -65,0 +65,0 @@ "eslint-plugin-jest": "^27.2.3", |
@@ -25,5 +25,5 @@ # sync-request-curl | ||
Leverages [node-libcurl](https://github.com/JCMais/node-libcurl) for performance as opposed to spawning child processes like sync-request. | ||
Leverages [node-libcurl](https://github.com/JCMais/node-libcurl) for performance instead of spawning child processes like sync-request. | ||
This library was designed to run on NodeJS. It cannot be used in a browser. | ||
This library was designed to run on NodeJS. It will not work in a browser. | ||
@@ -37,3 +37,4 @@ - [1. Installation](#1-installation) | ||
- [3. License](#3-license) | ||
- [4. Caveats](#4-caveats) | ||
- [4. Windows/MacOS](#4-windowsmacos) | ||
- [5. Caveats](#5-caveats) | ||
@@ -46,4 +47,3 @@ ## 1. Installation | ||
On MacOS, there may be an error in the installation process. | ||
This can often be fixed by following this [GitHub issue](https://github.com/JCMais/node-libcurl/issues/296). | ||
Please also refer to the [Windows/MacOS](#4-windowsmacos) section for known issues and workarounds. | ||
@@ -133,3 +133,3 @@ ## 2. Usage | ||
Only the following subset of options is supported for the time being: | ||
Only the following options from [sync-request](https://www.npmjs.com/package/sync-request) are supported for the time being: | ||
@@ -141,2 +141,3 @@ <table> | ||
<th>Example</th> | ||
<th>Default</th> | ||
</tr> | ||
@@ -149,2 +150,3 @@ <tr> | ||
<td><code>{ message: 'Hello, world!' }</code></td> | ||
<td><code>undefined</code></td> | ||
</tr> | ||
@@ -157,2 +159,3 @@ <tr> | ||
<td><code>{ token: 'abcdefg' }</code></td> | ||
<td><code>undefined</code></td> | ||
</tr> | ||
@@ -165,2 +168,3 @@ <tr> | ||
<code>{ email: 'ab@c.com', password: 'comp1531' }</code></td> | ||
<td><code>undefined</code></td> | ||
</tr> | ||
@@ -173,2 +177,3 @@ <tr> | ||
<td><code>JSON.stringify({ email: 'ab@c.com', password: 'comp1531' })</code></td> | ||
<td><code>undefined</code></td> | ||
</tr> | ||
@@ -181,2 +186,3 @@ <tr> | ||
<td><code>2000<code></td> | ||
<td><code>0</code><br/>(never times out)</td> | ||
</tr> | ||
@@ -186,24 +192,64 @@ <tr> | ||
<td> | ||
Defaults to true, but can be set to false to not follow any redirects (302) automatically | ||
Whether redirects (status code 302) should be followed automatically | ||
</td> | ||
<td><code>false</code></td> | ||
<td><code>true</code></td> | ||
</tr> | ||
<tr> | ||
<td>maxRedirects</td> | ||
<td>Sets the maximum number of redirects to follow before throwing an Error. Defaults to <code>Number.MAX_SAFE_INTEGER</code>.</td> | ||
<td>Sets the maximum number of redirects to follow before throwing an Error.</td> | ||
<td><code>3</code></td> | ||
<td><code>Number.MAX_SAFE_INTEGER</code></td> | ||
</tr> | ||
</table> | ||
In [src/types.ts](src/types.ts), the following is defined: | ||
<br/> | ||
Below are some additional options available from [node-libcurl](https://github.com/JCMais/node-libcurl): | ||
<table> | ||
<tr> | ||
<th>Option</th> | ||
<th>Description</th> | ||
<th>Example</th> | ||
<th>Default</th> | ||
</tr> | ||
<tr> | ||
<td>insecure</td> | ||
<td> Set to false to send insecure requests. This can be useful on Windows which can sometimes have SSL issues (Curlcode 60).</td> | ||
<td><code>true</code></td> | ||
<td><code>false</code></td> | ||
</tr> | ||
<tr> | ||
<td>setEasyOptions</td> | ||
<td>Optional callback to set additional curl options for the <a href='https://curl.se/libcurl/c/easy_setopt_options.html'>Easy Interface</a>. This has priority over existing options.</td> | ||
<td><code>(curl, options) => { curl.setOpt(options.FOLLOWLOCATION, false); };</code></td> | ||
<td><code>undefined</code></td> | ||
</tr> | ||
</table> | ||
<br/> | ||
In [src/types.ts](src/types.ts), the `Options` interface following is defined as: | ||
```typescript | ||
export interface Options { | ||
/* | ||
* sync-request options | ||
*/ | ||
headers?: IncomingHttpHeaders; | ||
qs?: { [key: string]: any }; | ||
json?: any; | ||
body?: string | Buffer; | ||
timeout?: number; | ||
body?: string | Buffer; | ||
followRedirects?: boolean; | ||
maxRedirects?: number; | ||
/* | ||
* node-libcurl options | ||
*/ | ||
insecure?: boolean; | ||
setEasyOptions?: SetEasyOptionCallback; | ||
} | ||
@@ -220,3 +266,3 @@ ``` | ||
In [src/types.ts](src/types.ts), the following is defined: | ||
In [src/types.ts](src/types.ts), the `Response` interface is defined as: | ||
@@ -237,4 +283,28 @@ ```typescript | ||
## 4. Caveats | ||
## 4. Windows/MacOS | ||
### 4.1. Windows | ||
Your requests may unexpectedly fail with a [Libcurl Error](https://curl.se/libcurl/c/libcurl-errors.html) (code 60, CURLE_PEER_FAILED_VERIFICATION) when using NodeJS natively on Windows. | ||
The reason is covered in the below resources: | ||
- https://github.com/JCMais/node-libcurl/issues/301 | ||
- https://stackoverflow.com/a/34883260/22324694 | ||
One quick workaround is to set the `insecure` option to `true` when sending your requests. This is the same as setting the Libcurl Easy's equivalent [SSL_VERIFYPEER](https://curl.se/libcurl/c/CURLOPT_SSL_VERIFYPEER.html) to `0`, or using `curl` in the command line with the [`-k` or `--insecure`](https://curl.se/docs/manpage.html#-k) option. | ||
Alternatively, consider using [Windows Subsystem for Linux (WSL)](https://learn.microsoft.com/en-us/windows/wsl/install). | ||
### 4.2. MacOS | ||
The build for MacOS may fail during the installation process. | ||
In most cases, this can be fixed by following these Github issues: | ||
- https://github.com/JCMais/node-libcurl/issues/296 | ||
- https://github.com/JCMais/node-libcurl/issues/382 | ||
Otherwise, we recommend uninstalling this library and using [sync-request](https://github.com/JCMais/node-libcurl/issues/382) instead. | ||
## 5. Caveats | ||
This library was developed mainly to improve performance with sending synchronous requests in NodeJS. | ||
@@ -244,2 +314,2 @@ | ||
Tested to be working on Arch & Debian Linux. Since [node-libcurl](https://github.com/JCMais/node-libcurl) is the core of this module, it is likely also compatible with other operating systems such as MacOS and Windows. | ||
It has been tested to be working on Arch & Debian Linux and is compatible with Windows/MacOS |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40086
368
302