节点- libcurl
这个 快点 用于 Node.js 的特性丰富的 URL 传输库.
利布尔 node.js. libcurl 的正式描述:
libcurl是一个自由而易于使用的客户端URL传输库,支持DICT,FILE,FTP,FTPS,Gopher,HTTP,HTTPS,IMAP,IMAP,IMAP,LDAP,POP3,POP3S,RTMP,RTSP,SCP,SFTP,SMTP,SMTPS,Telnet和TFTP. libcurl支持SSL证书,HTTP POST,HTTP PUT,FTP上传,基于HTTP格式的上传,代理,cookie,用户+password认证(Basic, Digest, NTLM, Negotiate, Kerberos),文件传输恢复,http代理隧道等!
快速启动
说明数字 :
- 此库不能用于浏览器, 取决于本地代码 .
安装
npm i node-libcurl --save
或者说
pnpm i node-libcurl --save
或者说
yarn add node-libcurl
简单请求 - Async / 使用卷轴等待
此 API 是实验性的, 在没有大版本突起的情况下可更改
const { curly } = require('node-libcurl');
const { statusCode, data, headers } = await curly.get('https://www.google.com')
任何选项都可以使用 FULLNAME 或一个 lowerPascalCase 格式 :
const querystring = require('querystring');
const { curly } = require('node-libcurl');
const { statusCode, data, headers } = await curly.post('https://httpbin.com/post', {
postFields: querystring.stringify({
field: 'value',
}),
// can use `postFields` or `POSTFIELDS`
})
JSON Post的例子:
const { curly } = require('node-libcurl')
const { data } = await curly.post('https://httpbin.com/post', {
postFields: JSON.stringify({ field: 'value' }),
httpHeader: [
'Content-Type: application/json',
'Accept: application/json'
],
})
console.log(data)
简单请求 - 使用 Curl 类
const { Curl } = require('node-libcurl');
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();
设置 HTTP 信头
通过指定信头的字符串阵列
curl.setOpt(Curl.option.HTTPHEADER,
['Content-Type: application/x-amz-json-1.1'])
格式提交(Content-Type:应用程序/x-www-form-urlencoded)
const querystring = require('querystring');
const { Curl } = require('node-libcurl');
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);
多部分上传 / HttpPost libcurl 选项( Content- Type: 多部分/ 形式数据)
const { Curl } = require('node-libcurl');
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);
二进制数据
当请求二进制数据时,请确保完成其中之一:
- 自己传
WRITEFUNCTION(单位:千美元)https://curl.haxx.se/libcurl/c/ CURLOPT WRITEF CONUTION.html 互联网档案馆的存檔,存档日期2014-12-21:
curl.setOpt('WRITEFUNCTION', (buffer, size, nmemb) => {
// something
})
- 启用以下旗帜之一 :
curl.enable(CurlFeature.NoDataParsing)
// or
curl.enable(CurlFeature.Raw)
背后的推理是,默认 Curl 实例将尝试将收到的数据和信头解码为 utf8 字符串, 如下所示: https://github.com/JCMais/node-libcurl/blob/b55b13529c9d11fdcdd7959137d8030b39427800/lib/Curl.ts#L391
更多实例请检查 示例文件夹。 。 。 .
SSL 服务器
API 密码
最新稳定版本的 API 文档可在 https://node-libcurl-docs.netlify.app/modules/ (中文(简体) )lib 索引.html (中文(简体) )。 。 。 .
开发分支文件,可在 https://develop--node-libcurl-docs.netlify.app/modules/ 互联网档案馆的存檔,存档日期2014-12-22lib 索引.html (中文(简体) )。 。 。 .
此库提供了 Typescript 类型定义.
几乎所有 CURL 选项 将被支持,如果您通过一个非错误,则将出现错误.
更多使用示例请检查 示例文件夹。 。 。 .
特别说明
READFUNCTION 选项
缓冲器作为第一个参数传递到带有 READFUNCTION 选项的初始化时, libcurl在上传缓冲器中使用的大小( 可以用 UPLOAD_BUFFERSIZE),使用 node::Buffer::Data(buf); 基本上与 Buffer#allocUnsafe 因此,它对于其正确使用具有各种影响: https://nodejs.org/pt-br/docs/guides/buffer-conbusctor-deprencation/#提及-buffer-allocunsafe 互联网档案馆的存檔,存档日期2013-12-12
所以,小心点,一定要回来 没错 您已在此调用中写入缓冲器的数据量 。 只有这个具体数额将被复制并移交给libcurl.
共同问题
见 (原始内容存档于2019-09-29) (英语). Common ISSUES.md
基准
见 ./基准
警卫
见 警卫
支持的 Libcurl 版本
附加词仅针对 libcurl 版本进行测试 7.81.0 和最新的一个.
代码本身仅用于编译版本大于或等于 7.81.0,任何小于此的 libcurl 版本 没有 已支持.
7.81.0版本于2022年1月5日发售,是和Ubuntu 22.04一起发运的版本. 从那时起,Libcurl上已经有超过5541个bug修复.
企业
node-libcurl 现作为本报告的一部分提供 潮汐订阅。 。 。 .
节点-libcurl和其他上千个软件包的维护者与Tidelift合作,为您用于构建应用程序的开源依赖提供商业支持和维护. 节省时间,降低风险,改善代码健康,同时向维护者支付使用的确切依赖性. 学多取.
详细安装
此软件包的最新版本有预建二进制( 感谢 节点前显示(中文(简体) ) 可用于:
- 节点.js:活动 LTS + 当前版本的最新两个版本(参见 https://github.com/nodejs/放行 (英语)(中文(简体) )
- 电子:最新2个主要版本
并在以下平台:
- Linux 64 比特 & ARM64 & Alpine( 穆斯尔, 64比特)
- macOS 64 位 (英特尔) & ARM64 (M1+)
- 窗口 64 位元
安装中 yarn add node-libcurl 或者说 npm install node-libcurl 如果下载预建的二进制,则不需要编译。 然而,如果你试图安装 nw.js 或者说 electron 需要更多的步骤,请检查下方的相应部分.
预建的二进制是静态构建的,其库有以下版本,特征和协议(图书馆版本可能在Node.js版本之间更改):
Version: libcurl/8.17.0 OpenSSL/3.5.2 zlib/1.3.1 brotli/1.1.0 zstd/1.5.7 libidn2/2.1.1 libssh2/1.10.0 nghttp2/1.66.0 ngtcp2/1.17.0 nghttp3/1.12.0 OpenLDAP/2.6.9
Protocols: dict, file, ftp, ftps, gopher, gophers, http, https, imap, imaps, ldap, ldaps, mqtt, pop3, pop3s, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp, ws, wss
Features: AsynchDNS, IDN, IPv6, Largefile, NTLM, SSL, libz, brotli, TLS-SRP, HTTP2, UnixSockets, HTTPS-proxy, alt-svc
如果没有与您的系统相匹配的预建二进制可用,或者安装失败,那么您需要一个能够编译节点.js addons的环境,这意味着:
- p 3.x 已安装
- 更新了 C++ 编译器,能够编译 C++17 (Electron QQ v32的 C++20).
如果您不想使用预建的二进制, 即使它在您的系统上有效, 您可以在安装时通过一面旗帜 :
与
npm
npm install node-libcurl --build-from-source
与
yarn
npm_config_build_from_source=true yarn add node-libcurl
关于预建二进制/ 直接安装的重要注释
这些笔记在Windows上建设时不重要
预建的二进制与 brotli, (中文(简体) ), libidn2, (中文(简体) ), libssh2, (中文(简体) ), openLDAP, (中文(简体) ), OpenSSL nghttp2, (中文(简体) ), zstd 财务报告和审定财务报表 zlib。 。 。 .
这个 brotli, (中文(简体) ), nghttp2, (中文(简体) ), OpenSSL 财务报告和审定财务报表 zlib 版本 必须 与 Node.js 使用的版本相匹配,这对通过混合不同版本的库符号来避免任何可能的问题是必要的,因为 Node.js 也导出其去向的一些符号.
如果您要用上面提到的库来构建加词, 您必须确定它们的版本是ABI 兼容一个节点. js 使用的, 否则您很可能会撞上分块断层 .
如果你想自己构建一个静态链接的加通版本,就需要通过 curl_static_build=true 调用安装时的旗帜.
如果使用
npm数字 :
npm install node-libcurl --build-from-source --curl_static_build=true
如果使用
yarn或者说pnpm数字 :
npm_config_build_from_source=true npm_config_curl_static_build=true yarn/pnpm add node-libcurl
构建过程将使用 curl-config 在路径中可用,如果您想要将它覆盖到您的 libcurl 安装之一, 您可以设置 curl_config_bin 变量,如上文提到的 curl_static_build。 。 。 .
如果你不想用的话 curl-config,您可以通过两个额外的变量来控制构建过程:
curl_include_dirs要搜索信头文件的分隔目录列表curl_libraries空格分隔的要传递到链接的旗帜列表
缺少软件包
静态链接版本目前不支持 GSS-API, (中文(简体) ), SPNEGO, (中文(简体) ), KERBEROS, (中文(简体) ), RTMP, (中文(简体) ), Metalink, (中文(简体) ), PSL 财务报告和审定财务报表 Alt-svc。 。 。 .
用于构建 Kerberos 的脚本存在于 ./scripts/ci 文件夹,但删除的原因有两个:
- 如果和海姆达尔一起建造,加通就会变得太大
- 如果与麻省理工学院Kerberos共同建造,加通将受到他们的许可条件的约束.
电子/NW.js
如果建造一个 Electron 或者说 NW.js 您需要通过安装命令的额外参数.
如果您不想使用预建的二进制, 请通过 npm_config_build_from_source=true 编号 : --build-from-source 旗到安装命令.
NW.js(高级节点-webkit)
在NW.js上从源头开始建设,您首先需要确保您在全球安装了 nw-gyp:
yarn global add nw-gyp 或者说 npm i -g nw-gyp 或者说 pnpm i -g nw-gyp
如果在 Windows 上, 您还需要添加步骤, 当前可用的 win delay load hook.cc 上
nw-gyp无法使用此加词, 因此必须应用补丁 。 该补丁可见于./scripts/ci/patches/win_delay_load_hook.cc.patch,并且应当应用到文件<nw-gyp-folder>/src/win_delay_load_hook.cc。 。 。 .
然后说:
线条
npm_config_runtime=node-webkit npm_config_target=0.38.2 yarn add node-libcurl
无
npm install node-libcurl --runtime=node-webkit --target=0.38.2 --save
地点 --target 是您当前使用的 NW.js 版本
电子( aka 原子壳)
线条
npm_config_runtime=electron npm_config_target=X.Y.Z npm_config_disturl=https://www.electronjs.org/headers yarn add node-libcurl
无
npm install node-libcurl --runtime=electron --target=X.Y.Z --disturl=https://www.electronjs.org/headers --save
何处 --target 是您正在使用的电子版本, 在我们的情况中, 我们只是使用 本地安装返回的版本 electron 二进制.
您也可以将这些参数放入.npmrc文件中,比如:
runtime = electron
target = 5.0.1
target_arch = x64
dist_url = https://atom.io/download/atom-shell
以 Linux 为基础
要在基于linux的系统上构建加词,您必须具备:
- 页:1
- libcurl dev 文件
- 蟒蛇 >= 3
- 操作系统没有超过他们的EOL.
如果你是在一个基于Debian的系统, 你可以通过运行来得到这些:
sudo apt-get install python libcurl4-openssl-dev build-essential
如果您不想使用 libcurl 版本与您的系统一起运出, 由于它可能非常老化, 你可以安装 libcurl 从源, curl_config_bin。 。 。 .
如果您需要一些实例来检查 CI 配置文件( I).travis.yml, (中文(简体) ), .circleci/config.yml)和 scripts/ci/ 文件夹.
建立在 macOS 上
在macOS上,你必须:
- 马克OS QQ 13(索诺马语)
- Xcode 命令行工具
您可以检查是否有 Xcode 命令行工具运行 :
xcode-select -p
它应该返回他们的路径,如果它不返回, 你必须通过运行安装它:
xcode-select --install
Xcode 10 macOS 莫哈韦
以防您从源头安装加词出错, 您正在使用 macOS 版本 QQ Mojave, 请检查您是否收到以下错误 :
CXX(target) Release/obj.target/node_libcurl/src/node_libcurl.o
clang: error: no such file or directory: '/usr/include'
如果是这样的话,这是因为命令行工具的最新版本没有添加 /usr/include 默认文件夹。 检查 Xcode 10 发布注释 详细情况.
这个 /usr/include 现可上网查阅 $(xcrun --show-sdk-path)/usr/include。要正确构建 libcurl ,您需要将路径传递到 npm_config_curl_include_dirs 环境变量 :
npm_config_curl_include_dirs="$(xcrun --show-sdk-path)/usr/include" yarn add node-libcurl
土生土长
既然这样的话
error: use of undeclared identifier 'curl_ws_start_frame'; did you mean 'curl_ws_frame'?
这意味着你的系统正在使用macOS SDK的libcurl头条,这些头条已经过时(版本在8.16下方,需要WebSocket支持).
您必须使用 curl 通过Homebrew安装,您可以使用以下步骤来确保系统接取Homebrew curl 从源头建起时头/ libs .
- 安装
curl和Homebrew(如果你还没有):
brew install curl
- 添加本乡人
curl因此,构建工具可以找到信头、库和curl-config数字 :
export PATH="$(brew --prefix curl)/bin:$PATH" # optional
export CPPFLAGS="-I$(brew --prefix curl)/include"
export LDFLAGS="-L$(brew --prefix curl)/lib"
export PKG_CONFIG_PATH="$(brew --prefix curl)/lib/pkgconfig"
注:在Intel macs上,Homebrew通常安装在以下 /usr/local 和苹果硅 /opt/homebrew。使用 $(brew --prefix curl) 是两个架构中最便携的选项.
- 构建安装( S)
node-libcurl从源.
可选: 删除先前安装的本地副本 node-libcurl 首先要确保清洁的建筑:
# remove previously installed copy (local environment only)
rm -rf node_modules/node-libcurl
然后从源而起. 根据您的 macOS Xcode/ CLT 版本, 您可能还需要指出 SDK 的 /usr/include 目录 :
如果您喜欢明确使用 Homebrew 安装 curl 信头而非 SDK 包含路径, 您可以使用 :
npm_config_curl_include_dirs="$(brew --prefix curl)/include" npm_config_curl_libraries="-L$(brew --prefix curl)/lib -lcurl" npm install node-libcurl --build-from-source
或者,如果你想使用 SDK 包含路径 :
# build & install
npm_config_curl_include_dirs="$(xcrun --show-sdk-path)/usr/include" npm install node-libcurl --build-from-source
这应该可以让你的机器用Homebrew来建造附加装置 curl 安装.
以视窗为基础
如果使用预建的二进制安装, 您只需要拥有该二进制 视觉 c++ 2017 运行时库。 。 。 .
如果从源头建造,你必须:
- Py 3.x
- Visual Studio QQ 2019 (英语) (启用 Clang/LLVM 支持, 请见下项)
- 视觉工作室的 Clang/LLVM 支持
- 鼻音
Python 3.x和Visual Studio编译器可以通过运行来安装:
npm install --global --production windows-build-tools
nasm 可从上述链接的网站或使用巧克力获取:
choco install nasm
目前没有支持使用其他libcurl版本,仅次于该版本 卷曲为窗口 子模块 (帮助添加此功能).
关于在Windows上构建加词的重要说明是,我们必须用包含在标题文件中的"hack"来做一些 node-gyp编号 :nw-gyp。原因是因为我们正在使用一个独立的OpenSSL版本,我们不想使用由Node.js提供的OpenSSL头条,默认是添加到 <nw-gyp-or-node-gyp-folder>/include/node/openssl,所以我们要做的是,在编译之前,文件夹将被重新命名为 openssl.disabled。成功安装后,文件夹将重新命名为原名, 不过 如果在编译过程中发生任何错误, 文件夹将保留重命名, 直到编译成功 。 提供更多信息,说明为何需要这样做,并可在问题上找到一些背景 164号。 。 。 .
关于已过时的节点-gyp版本的说明
国家预防机制有自己的内部版本: node-gyp,可能不是最新版本。 如果您看到这样的输出 :
[info] it worked if it ends with ok
[info] using [email protected]
[info] using [email protected] | win32 | x64
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info ok
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
节点- gyp 版本在哪里 10.1.0你可能会面临与ClangCL这样的问题:
C:\Users\internal\AppData\Local\node\corepack\v1\pnpm\9.9.0\dist\node_modules\node-gyp\src\win_delay_load_hook.cc(37,9): warning : unknown pragma ignored [-Wunknown-pragmas] [F:\jc\node-libcurl\build\deps\
curl-for-windows\libcurl.vcxproj]
/LTCG:INCREMENTAL: no such file or directory
D:\Software\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(1522,5): error MSB6006: "llvm-lib.exe" exited with code 1. [F:\jc\node-libcurl\build\deps\curl-fo
r-windows\libcurl.vcxproj]
C:\Users\internal\AppData\Local\node\corepack\v1\pnpm\9.9.0\dist\node_modules\node-gyp\src\win_delay_load_hook.cc(12,9): warning : unknown pragma ignored [-Wunknown-pragmas] [F:\jc\node-libcurl\build\deps\
curl-for-windows\brotli\brotli.vcxproj]
C:\Users\internal\AppData\Local\node\corepack\v1\pnpm\9.9.0\dist\node_modules\node-gyp\src\win_delay_load_hook.cc(37,9): warning : unknown pragma ignored [-Wunknown-pragmas] [F:\jc\node-libcurl\build\deps\
curl-for-windows\brotli\brotli.vcxproj]
/LTCG:INCREMENTAL: no such file or directory
D:\Software\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppCommon.targets(1522,5): error MSB6006: "llvm-lib.exe" exited with code 1. [F:\jc\node-libcurl\build\deps\curl-fo
r-windows\brotli\brotli.vcxproj]
唯一的解决方案是安装全球可用的节点-gyp版本:
npm install --global node-gyp@latest
然后通过设置 npm_config_node_gyp 环境变量( 假设电源) :
$globalNodeGypPath = Join-Path (npm prefix -g) "node_modules\node-gyp\bin\node-gyp.js"
$env:npm_config_node_gyp=$globalNodeGypPath
pnpm install node-libcurl
获取帮助
如果您的问题与加词或用法直接相关,您可以通过以下方式获得帮助:
- 张贴一个问题在
stack-overflow并使用node-libcurl标签. - 加入我们的 Discord 服务器 把你的问题发过去.
捐款
读取 捐款额
捐赠/帕特里昂
有人问我是否有办法支持我的工作, 我为此创建了Patreon网页: https://www.patreon.com/jonathancardoso (英语)
如果您想通过 PayPal 捐赠, 请使用我的 GitHub 配置文件中可用的同样电子邮件 : https://github.com/JCMais 页面存档备份,存于互联网档案馆
谢谢你读到这里
最初这个加词是基于来自 相声/正声,事物已经改变,大多数甚至所有代码都被重写.