Compare commits

..

26 Commits

Author SHA1 Message Date
langhuihui
8ff14931fe feat: disable replay protection on tcp webrtc 2025-06-04 23:02:24 +08:00
pggiroro
9c7dc7e628 fix: modify gb.Logger.With 2025-06-04 20:39:49 +08:00
pggiroro
75791fe93f feat: gb28181 support add platform and platform channel from config.yaml 2025-06-04 20:36:48 +08:00
langhuihui
cf218215ff fix: tcp read block 2025-06-04 14:13:28 +08:00
langhuihui
dbf820b845 feat: downlad flv format from mp4 record file 2025-06-03 17:20:58 +08:00
langhuihui
86b9969954 feat: config support more format 2025-06-03 09:06:43 +08:00
langhuihui
b3143e8c14 fix: mp4 download 2025-06-02 22:31:25 +08:00
langhuihui
7f859e6139 fix: mp4 recovery 2025-06-02 21:12:02 +08:00
pggiroro
6eb2941087 fix: use task.Manager to resolve register handler 2025-06-02 20:09:22 +08:00
pggiroro
e8b4cea007 fix: plan.length is 168 2025-06-02 20:09:22 +08:00
pggiroro
3949773e63 fix: update config.yaml add comment about autoinvite,mediaip,sipip 2025-06-02 20:09:22 +08:00
langhuihui
d67279a404 feat: add raw check no frame 2025-05-30 14:01:18 +08:00
langhuihui
043c62f38f feat: add loop read mp4 2025-05-29 20:25:26 +08:00
pggiroro
acf9f0c677 fix: gb28181 make invite sdp mediaip or sipip correct;linux remove viaheader in sip request 2025-05-28 09:22:34 +08:00
langhuihui
49d1e7c784 feat: add s3 plugin 2025-05-28 08:40:53 +08:00
langhuihui
40bc7d4675 feat: add writerBuffer config to tcp 2025-05-27 16:56:01 +08:00
langhuihui
5aa8503aeb feat: add pull testMode 2025-05-27 10:43:34 +08:00
langhuihui
09175f0255 fix: use total insteadof totalCount 2025-05-26 16:04:20 +08:00
pggiroro
dd1a398ca2 feat: gb28181 support play sub stream 2025-05-25 21:33:14 +08:00
pggiroro
50cdfad931 fix: d.conn.NetConnection.Conn maybe nil 2025-05-25 21:33:14 +08:00
langhuihui
6df793a8fb feat: add more format for sei api 2025-05-23 17:18:43 +08:00
langhuihui
74c948d0c3 fix: rtsp memory leak 2025-05-23 10:02:36 +08:00
pggiroro
80ad1044e3 fix: gb28181 register too fast will start too many task 2025-05-22 22:56:41 +08:00
langhuihui
47884b6880 fix: rtmp timestamp start with 1 2025-05-22 22:52:21 +08:00
langhuihui
a38ddd68aa feat: add tcp dump to docker 2025-05-22 20:34:50 +08:00
banshan
a2bc3d94c1 fix: rtsp no audio or video flag 2025-05-22 20:10:17 +08:00
230 changed files with 7059 additions and 73685 deletions

View File

@@ -1,44 +0,0 @@
---
description:
globs:
alwaysApply: true
---
# Gloal
complete cluster plugin
# Modify Plugins
- follow [README.md](mdc:plugin/README.md)
# Use Task System
- follow [task.md](mdc:doc/arch/task.md)
- 可以覆盖 Start 方法用来启动任务
- 不可以覆盖 Stop 方法
- 可以覆盖 Dispose 方法用来释放资源
- 如果没有子任务则内嵌 task.Task
- 如果需要子任务则内嵌 task.Work 或 task.Job,取决于是否随着子任务的退出而自动退出,如果需要保留则内嵌 task.Work如果需要自动退出则内嵌 task.Job
- 如果该任务需要用定时器则可以内嵌 task.TickTask
- 如果该任务需要用信号量则可以内嵌 task.ChannelTask
- 不可主动调用 task.Task 的除了 Stop 以外的方法
- 不可主动调用 task.Job 的除了 AddTask 以外的方法
# logger
- slog need input key value pair
# yaml config file
- Must be all lowercase
## 编译全局 pb
sh scripts/protoc.sh
## 编译某插件 pb
`sh scripts/protoc.sh 插件名`
例如 cluster 插件使用 `sh scripts/protoc.sh cluster`

2
.gitignore vendored
View File

@@ -20,5 +20,3 @@ example/default/*
!example/default/main.go !example/default/main.go
!example/default/config.yaml !example/default/config.yaml
shutdown.sh shutdown.sh
node_modules
data

View File

@@ -11,6 +11,9 @@ COPY monibuca_arm64 ./monibuca_arm64
COPY admin.zip ./admin.zip COPY admin.zip ./admin.zip
# Install tcpdump
RUN apt-get update && apt-get install -y tcpdump && rm -rf /var/lib/apt/lists/*
# Copy the configuration file from the build context # Copy the configuration file from the build context
COPY example/default/config.yaml /etc/monibuca/config.yaml COPY example/default/config.yaml /etc/monibuca/config.yaml

9
api.go
View File

@@ -7,7 +7,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path/filepath"
"reflect" "reflect"
"runtime" "runtime"
"strings" "strings"
@@ -718,7 +717,7 @@ func (s *Server) GetConfigFile(_ context.Context, req *emptypb.Empty) (res *pb.G
func (s *Server) UpdateConfigFile(_ context.Context, req *pb.UpdateConfigFileRequest) (res *pb.SuccessResponse, err error) { func (s *Server) UpdateConfigFile(_ context.Context, req *pb.UpdateConfigFileRequest) (res *pb.SuccessResponse, err error) {
if s.configFileContent != nil { if s.configFileContent != nil {
s.configFileContent = []byte(req.Content) s.configFileContent = []byte(req.Content)
os.WriteFile(filepath.Join(ExecDir, s.conf.(string)), s.configFileContent, 0644) os.WriteFile(s.configFilePath, s.configFileContent, 0644)
res = &pb.SuccessResponse{} res = &pb.SuccessResponse{}
} else { } else {
err = pkg.ErrNotFound err = pkg.ErrNotFound
@@ -808,9 +807,9 @@ func (s *Server) GetRecordList(ctx context.Context, req *pb.ReqRecordList) (resp
return return
} }
resp = &pb.ResponseList{ resp = &pb.ResponseList{
TotalCount: uint32(totalCount), Total: uint32(totalCount),
PageNum: req.PageNum, PageNum: req.PageNum,
PageSize: req.PageSize, PageSize: req.PageSize,
} }
for _, recordFile := range result { for _, recordFile := range result {
resp.Data = append(resp.Data, &pb.RecordFile{ resp.Data = append(resp.Data, &pb.RecordFile{

View File

@@ -1,241 +0,0 @@
#!/usr/bin/env node
/**
* 简化版 Cluster CDP 测试脚本
* 此脚本用于测试 Chrome DevTools Protocol 连接和操作
*/
const CDP = require('chrome-remote-interface');
// 测试配置
const TEST_PORT = 9222;
// 一个简单的延迟函数
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// 连接到 CDP
async function connectToCDP() {
try {
console.log(`连接到 Chrome DevTools Protocol (端口 ${TEST_PORT})...`);
// 获取可用的目标列表
const targets = await CDP.List({ port: TEST_PORT });
if (targets.length === 0) {
throw new Error('没有可用的调试目标');
}
// 找到第一个类型为 'page' 的目标
const target = targets.find(t => t.type === 'page');
if (!target) {
throw new Error('没有找到可用的页面目标');
}
console.log('找到目标页面:', target.title);
// 连接到特定目标
const client = await CDP({
port: TEST_PORT,
target: target
});
const { Network, Page, Runtime } = client;
await Promise.all([
Network.enable(),
Page.enable()
]);
console.log('CDP 连接成功');
return { client, Network, Page, Runtime };
} catch (err) {
console.error('无法连接到 Chrome:', err);
throw err;
}
}
// 测试 CDP 基本功能
async function testCDPBasics(cdp) {
try {
console.log('\n测试: CDP 基本功能');
const { Runtime } = cdp;
// 在浏览器中执行一段脚本
const result = await Runtime.evaluate({
expression: '2 + 2'
});
console.log('执行结果:', result.result.value);
if (result.result.value === 4) {
console.log('✅ 测试通过: CDP 执行脚本正常');
} else {
console.error(`❌ 测试失败: CDP 执行脚本异常,期望 4实际 ${result.result.value}`);
return false;
}
return true;
} catch (err) {
console.error('CDP 基本功能测试出错:', err);
return false;
}
}
// 测试网络请求监控
async function testNetworkMonitoring(cdp) {
try {
console.log('\n测试: 网络请求监控');
const { Network, Page } = cdp;
const requests = [];
// 监听网络请求
Network.requestWillBeSent((params) => {
console.log('检测到网络请求:', params.request.url);
requests.push(params.request.url);
});
console.log('正在导航到测试页面...');
// 打开一个网页
await Page.navigate({ url: 'https://example.com' });
console.log('等待页面加载完成...');
// 等待页面加载完成
await Page.loadEventFired();
console.log('页面加载完成,等待可能的额外请求...');
// 等待一段时间以捕获所有请求
await delay(3000);
console.log(`总共捕获到 ${requests.length} 个网络请求`);
if (requests.length > 0) {
console.log('✅ 测试通过: 成功监控到网络请求');
console.log('请求列表:');
requests.forEach((url, index) => {
console.log(`${index + 1}. ${url}`);
});
return true;
} else {
console.error('❌ 测试失败: 未能监控到任何网络请求');
return false;
}
} catch (err) {
console.error('网络请求监控测试出错:', err);
console.error('错误详情:', err.message);
return false;
}
}
// 测试 DOM 操作
async function testDOMOperations(cdp) {
try {
console.log('\n测试: DOM 操作');
const { Runtime, Page } = cdp;
// 打开一个网页
await Page.navigate({ url: 'https://example.com' });
await Page.loadEventFired();
// 查询页面标题
const titleResult = await Runtime.evaluate({
expression: 'document.title'
});
console.log('页面标题:', titleResult.result.value);
if (titleResult.result.value === 'Example Domain') {
console.log('✅ 测试通过: 成功获取页面标题');
} else {
console.error(`❌ 测试失败: 获取页面标题异常,期望 "Example Domain",实际 "${titleResult.result.value}"`);
return false;
}
// 修改页面元素
await Runtime.evaluate({
expression: 'document.querySelector("h1").textContent = "CDP 测试成功"'
});
// 验证修改
const modifiedResult = await Runtime.evaluate({
expression: 'document.querySelector("h1").textContent'
});
if (modifiedResult.result.value === 'CDP 测试成功') {
console.log('✅ 测试通过: 成功修改页面元素');
} else {
console.error(`❌ 测试失败: 修改页面元素失败`);
return false;
}
return true;
} catch (err) {
console.error('DOM 操作测试出错:', err);
return false;
}
}
// 主函数
async function main() {
let cdp = null;
try {
// 连接到 CDP
cdp = await connectToCDP();
// 运行各种测试
const tests = [
{ name: "CDP 基本功能", fn: () => testCDPBasics(cdp) },
{ name: "网络请求监控", fn: () => testNetworkMonitoring(cdp) },
{ name: "DOM 操作", fn: () => testDOMOperations(cdp) }
];
let passedCount = 0;
let failedCount = 0;
for (const test of tests) {
console.log(`\n====== 执行测试: ${test.name} ======`);
const passed = await test.fn();
if (passed) {
passedCount++;
} else {
failedCount++;
}
}
// 输出测试结果摘要
console.log("\n====== 测试结果摘要 ======");
console.log(`通过: ${passedCount}`);
console.log(`失败: ${failedCount}`);
console.log(`总共: ${tests.length}`);
if (failedCount === 0) {
console.log("\n✅ 所有测试通过!");
} else {
console.log("\n❌ 有测试失败!");
}
} catch (err) {
console.error('测试过程中出错:', err);
} finally {
// 关闭CDP连接
if (cdp && cdp.client) {
await cdp.client.close();
console.log('已关闭 CDP 连接');
}
}
}
// 处理进程终止信号
process.on('SIGINT', async () => {
console.log('\n接收到 SIGINT 信号,正在清理...');
process.exit(0);
});
// 运行测试
main().catch(err => {
console.error('未处理的错误:', err);
process.exit(1);
});

View File

@@ -1,21 +0,0 @@
package main
import (
"context"
"flag"
"log"
"m7s.live/v5"
_ "m7s.live/v5/plugin/cluster" // 集群管理
_ "m7s.live/v5/plugin/flv" // FLV 插件
)
func main() {
conf := flag.String("c", "etcd-node1.yaml", "config file")
flag.Parse()
log.Printf("Cluster 测试程序启动, 配置文件: %s", *conf)
// 使用最简单的方式启动服务器
m7s.Run(context.Background(), *conf)
}

View File

@@ -1,50 +0,0 @@
global:
http: :8080
tcp: :50054
cluster:
nodeid: "etcd-node1"
role: "manager"
region: "default"
clustersecret: "test-cluster-secret"
# Etcd 配置
etcd:
enabled: true
endpoints: ["http://localhost:2379"]
keyprefix: "cluster-test/"
nodekeyttl: 30
streamkeyttl: 30
dialtimeout: 5s
requesttimeout: 3s
retryinterval: 1s
maxretries: 3
enablewatcher: true
watchtimeout: 10s
autosyncinterval: 30s
# 内嵌 etcd 服务器配置
server:
enabled: true
datadir: "./data/etcd1"
listenclienturls: ["http://localhost:2379"]
advertiseclienturls: ["http://localhost:2379"]
listenpeerurls: ["http://localhost:2380"]
advertisepeerurls: ["http://localhost:2380"]
initialcluster: "etcd-node1=http://localhost:2380"
initialclusterstate: "new"
initialclustertoken: "cluster-etcd-cluster"
snapshotcount: 10000
autocompactionmode: "revision"
autocompactionretention: "1000"
quotabackendbytes: 2147483648 # 2GB
# 流同步配置
sync:
fullsyncinterval: 30s
incrementalsyncinterval: 5s
maxstreamsperrequest: 100
syncretryinterval: 5s
maxretries: 3
flv:
publish: 1

View File

@@ -1,54 +0,0 @@
global:
http: :8081
tcp: :50052
cluster:
nodeid: "etcd-node2"
role: "worker"
region: "default"
clustersecret: "test-cluster-secret"
manageraddress: localhost:50054
# Etcd 配置
etcd:
enabled: true
endpoints: ["http://localhost:2379"]
keyprefix: "cluster-test/"
nodekeyttl: 30
streamkeyttl: 30
dialtimeout: 5s
requesttimeout: 3s
retryinterval: 1s
maxretries: 3
enablewatcher: true
watchtimeout: 10s
autosyncinterval: 30s
# 内嵌 etcd 服务器配置
server:
enabled: false
datadir: "./data/etcd2"
listenclienturls: ["http://localhost:2381"]
advertiseclienturls: ["http://localhost:2381"]
listenpeerurls: ["http://localhost:2382"]
advertisepeerurls: ["http://localhost:2382"]
initialcluster: "etcd-node1=http://localhost:2380,etcd-node2=http://localhost:2382"
initialclusterstate: "new"
initialclustertoken: "cluster-etcd-cluster"
snapshotcount: 10000
autocompactionmode: "revision"
autocompactionretention: "1000"
quotabackendbytes: 2147483648 # 2GB
# 流同步配置
sync:
fullsyncinterval: 30s
incrementalsyncinterval: 5s
maxstreamsperrequest: 100
syncretryinterval: 5s
maxretries: 3
rtmp:
listen: ":1937"
flv:
pull:
live/test: /Users/dexter/Movies/jb-demo.flv

View File

@@ -1,55 +0,0 @@
global:
http: :8082
tcp: :50053
cluster:
nodeid: "etcd-node3"
role: "worker"
region: "default"
clustersecret: "test-cluster-secret"
manageraddress: localhost:50054
# Etcd 配置
etcd:
enabled: true
endpoints: ["http://localhost:2379"]
keyprefix: "cluster-test/"
nodekeyttl: 30
streamkeyttl: 30
dialtimeout: 5s
requesttimeout: 3s
retryinterval: 1s
maxretries: 3
enablewatcher: true
watchtimeout: 10s
autosyncinterval: 30s
# 内嵌 etcd 服务器配置
server:
enabled: false
datadir: "./data/etcd3"
listenclienturls: ["http://localhost:2383"]
advertiseclienturls: ["http://localhost:2383"]
listenpeerurls: ["http://localhost:2384"]
advertisepeerurls: ["http://localhost:2384"]
initialcluster: "etcd-node1=http://localhost:2380,etcd-node2=http://localhost:2382,etcd-node3=http://localhost:2384"
initialclusterstate: "new"
initialclustertoken: "cluster-etcd-cluster"
snapshotcount: 10000
autocompactionmode: "revision"
autocompactionretention: "1000"
quotabackendbytes: 2147483648 # 2GB
# 流同步配置
sync:
fullsyncinterval: 30s
incrementalsyncinterval: 5s
maxstreamsperrequest: 100
syncretryinterval: 5s
maxretries: 3
rtmp:
listen: ":1938"
flv:
publish: 1

View File

@@ -1,493 +0,0 @@
#!/usr/bin/env node
/**
* Cluster 插件 Etcd 单节点测试运行器
* 此脚本用于测试单个 Cluster 节点的 Etcd 集成功能
*/
const path = require('path');
const { spawn } = require('child_process');
const CDP = require('chrome-remote-interface');
const fetch = require('node-fetch');
const { execSync } = require('child_process');
// 测试配置
const TEST_PORT = 9222;
const HTTP_PORT = 8080;
const CONFIG_DIR = path.join(__dirname, '.');
const CONFIG_FILE = path.join(CONFIG_DIR, 'etcd-node1.yaml');
// 启动服务器进程
let server = null;
// 一个简单的延迟函数
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// 检查 etcd 服务器是否就绪
async function checkEtcdServer() {
console.log('等待 Etcd 服务器启动...');
let retries = 10;
while (retries > 0) {
try {
console.log(`尝试连接 Etcd 服务器 (http://localhost:2379/health)...`);
const response = await fetch('http://localhost:2379/health');
const status = await response.json();
console.log('Etcd 服务器响应:', status);
if (status.health === 'true') {
console.log('✅ Etcd 服务器已就绪');
return true;
}
console.log('Etcd 服务器未就绪,等待中...');
} catch (err) {
console.log(`等待 Etcd 服务器启动 (${retries} 次尝试剩余): ${err.message}`);
// 检查端口是否被占用
try {
const portCheck = execSync('lsof -i :2379').toString();
console.log('端口 2379 状态:', portCheck);
} catch (e) {
console.log('端口 2379 未被占用');
}
}
await delay(2000);
retries--;
}
console.error('❌ Etcd 服务器启动超时');
return false;
}
// 启动 Cluster 服务器
async function startServer() {
console.log('启动 Cluster 服务器 (Etcd 模式)...');
// 设置环境变量
const env = {
...process.env,
GODEBUG: 'protobuf=2', // 启用详细的 protobuf 调试信息
GO_TESTMODE: '1', // 启用测试模式
ETCDCTL_API: '3' // 使用 etcd v3 API
};
// 确保数据目录存在
const dataDir = path.join(__dirname, 'data', 'etcd1');
try {
execSync(`mkdir -p ${dataDir}`);
console.log(`✅ 创建数据目录: ${dataDir}`);
} catch (err) {
console.error(`❌ 创建数据目录失败: ${err.message}`);
}
// 启动节点
server = spawn('go', ['run', '-tags', 'sqlite,dummy', 'etcd-main.go', '-c', CONFIG_FILE], {
cwd: path.join(__dirname, '.'),
stdio: ['ignore', 'pipe', 'pipe'], // 只保留 stdout 和 stderr
env
});
// 处理输出
server.stdout.on('data', (data) => {
const output = data.toString().trim();
console.log(`[Node] ${output}`);
// 检查关键日志
if (output.includes('etcd server is ready')) {
console.log('✅ Etcd 服务器已就绪');
}
if (output.includes('Node registered successfully')) {
console.log('✅ 节点注册成功');
}
if (output.includes('Node sync completed')) {
console.log('✅ 节点同步完成');
}
if (output.includes('failed to start etcd server')) {
console.error('❌ Etcd 服务器启动失败');
}
if (output.includes('etcd server error')) {
console.error('❌ Etcd 服务器错误');
}
if (output.includes('Starting embedded etcd server')) {
console.log('🔄 正在启动内嵌 Etcd 服务器...');
}
if (output.includes('Creating etcd client')) {
console.log('🔄 正在创建 Etcd 客户端...');
}
if (output.includes('Starting cluster manager')) {
console.log('🔄 正在启动集群管理器...');
}
if (output.includes('Starting stream synchronization service')) {
console.log('🔄 正在启动流同步服务...');
}
if (output.includes('Starting resource optimizer')) {
console.log('🔄 正在启动资源优化器...');
}
});
server.stderr.on('data', (data) => {
const error = data.toString().trim();
console.error(`[Node Error] ${error}`);
// 检查错误日志
if (error.includes('failed to create etcd client')) {
console.error('❌ Etcd 客户端创建失败');
}
if (error.includes('failed to register node')) {
console.error('❌ 节点注册失败');
}
if (error.includes('failed to sync nodes')) {
console.error('❌ 节点同步失败');
}
if (error.includes('etcd server error')) {
console.error('❌ Etcd 服务器错误');
}
if (error.includes('failed to start etcd')) {
console.error('❌ Etcd 服务器启动失败');
}
if (error.includes('etcd took too long to start')) {
console.error('❌ Etcd 服务器启动超时');
}
if (error.includes('failed to create data directory')) {
console.error('❌ 创建数据目录失败');
}
if (error.includes('invalid listen client url')) {
console.error('❌ 无效的客户端监听地址');
}
if (error.includes('invalid advertise client url')) {
console.error('❌ 无效的客户端广播地址');
}
});
// 等待节点和 etcd 启动
console.log('等待节点和内嵌 etcd 启动...');
await delay(5000); // 先等待 5 秒让进程启动
// 检查 etcd 服务器是否就绪
if (!await checkEtcdServer()) {
// 如果服务器启动失败,尝试使用 etcdctl 检查状态
try {
console.log('尝试使用 etcdctl 检查状态...');
const etcdctlStatus = execSync('etcdctl endpoint health').toString();
console.log('etcdctl 状态:', etcdctlStatus);
} catch (err) {
console.error('etcdctl 检查失败:', err.message);
}
throw new Error('Etcd 服务器启动失败');
}
// 等待节点注册和同步
console.log('等待节点注册和同步...');
await delay(10000);
console.log('Cluster 服务器已启动');
}
// 连接到 CDP
async function connectToCDP() {
try {
console.log(`连接到 Chrome DevTools Protocol (端口 ${TEST_PORT})...`);
// 等待Chrome启动并开始接收连接
let retries = 10;
while (retries > 0) {
try {
// 尝试获取可用的调试目标
const targets = await CDP.List({ port: TEST_PORT });
if (targets && targets.length > 0) {
console.log(`找到 ${targets.length} 个可调试目标`);
break;
}
console.log('没有找到可调试目标等待Chrome启动...');
} catch (e) {
console.log(`等待Chrome启动 (${retries} 次尝试剩余): ${e.message}`);
}
await delay(1000);
retries--;
if (retries === 0) {
console.log('无法找到可调试目标,尝试打开一个新页面...');
// 打开一个新标签页
try {
execSync(`open -a "Google Chrome" http://localhost:${HTTP_PORT}/`);
await delay(2000);
} catch (e) {
console.error('打开新页面失败:', e);
}
}
}
const client = await CDP({ port: TEST_PORT });
const { Network, Page, Runtime } = client;
await Promise.all([
Network.enable(),
Page.enable()
]);
console.log('CDP 连接成功');
return { client, Network, Page, Runtime };
} catch (err) {
console.error('无法连接到 Chrome:', err);
throw err;
}
}
// 测试节点状态
async function testNodeStatus() {
try {
console.log('\n测试: 节点状态');
// 首先检查 etcd 服务器状态
console.log('检查 Etcd 服务器状态...');
const etcdResponse = await fetch('http://localhost:2379/health');
const etcdStatus = await etcdResponse.json();
console.log('Etcd 服务器状态:', etcdStatus);
if (etcdStatus.health !== 'true') {
console.error('❌ Etcd 服务器不健康');
return false;
}
console.log('✅ Etcd 服务器健康');
// 检查集群状态
const response = await fetch(`http://localhost:${HTTP_PORT}/cluster/api/cluster/status`);
const text = await response.text();
console.log('原始响应:', text);
try {
const status = JSON.parse(text);
console.log('节点状态:', JSON.stringify(status, null, 2));
// 检查基本状态字段
if (status.status.totalNodes === 1) {
console.log('✅ 测试通过: 节点数量正确');
} else {
console.error(`❌ 测试失败: 节点数量不正确,期望 1实际 ${status.status.totalNodes}`);
// 输出更多调试信息
console.log('当前节点列表:', status.status.nodes);
}
// 检查健康节点数量
if (status.status.healthyNodes === 1) {
console.log('✅ 测试通过: 节点处于健康状态');
} else {
console.error(`❌ 测试失败: 健康节点数量不正确,期望 1实际 ${status.status.healthyNodes}`);
}
// 检查集群状态
if (status.status.clusterState === "normal") {
console.log('✅ 测试通过: 集群状态正常');
} else {
console.error(`❌ 测试失败: 集群状态异常,期望 "normal",实际 "${status.status.clusterState}"`);
}
// 获取节点列表进行详细检查
const nodesResponse = await fetch(`http://localhost:${HTTP_PORT}/cluster/api/nodes`);
const nodesData = await nodesResponse.json();
console.log('节点列表数据:', JSON.stringify(nodesData, null, 2));
if (nodesData.nodes && nodesData.nodes.length === 1) {
console.log('✅ 测试通过: 节点列表正确');
// 检查节点ID
const node = nodesData.nodes[0];
if (node.id === 'etcd-node1') {
console.log('✅ 测试通过: 节点ID正确');
} else {
console.error(`❌ 测试失败: 节点ID不正确期望 "etcd-node1",实际 "${node.id}"`);
}
// 检查节点角色
if (node.role === 'manager') {
console.log('✅ 测试通过: 节点角色正确');
} else {
console.error(`❌ 测试失败: 节点角色不正确,期望 "manager",实际 "${node.role}"`);
}
} else {
console.error(`❌ 测试失败: 节点列表数量不正确,期望 1实际 ${nodesData.nodes ? nodesData.nodes.length : 0}`);
}
} catch (parseErr) {
console.error('解析响应失败:', parseErr);
return false;
}
return true;
} catch (err) {
console.error('节点状态测试出错:', err);
return false;
}
}
// 测试 etcd 流注册
async function testStreamRegistration() {
try {
console.log('\n测试: Etcd 流注册功能');
// 注册一个测试流
const streamPath = 'test-stream-' + Date.now();
const streamInfo = {
streamPath: streamPath,
publisherNodeID: 'etcd-node1',
startTime: new Date().toISOString(),
lastUpdated: new Date().toISOString(),
bitrateMbps: 1.0
};
// 注册流
const registerResponse = await fetch(`http://localhost:${HTTP_PORT}/cluster/api/streams`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(streamInfo)
});
const registerResult = await registerResponse.json();
if (!registerResult.success) {
console.error('❌ 测试失败: 无法注册流');
return false;
}
console.log('✅ 测试通过: 成功注册流');
// 等待流信息同步
await delay(2000);
// 获取流信息验证
const getResponse = await fetch(`http://localhost:${HTTP_PORT}/cluster/api/streams/${streamPath}`);
const getResult = await getResponse.json();
if (getResult.success && getResult.streamInfo && getResult.streamInfo.streamPath === streamPath) {
console.log('✅ 测试通过: 成功获取流信息');
} else {
console.error('❌ 测试失败: 无法获取流信息');
return false;
}
// 清理测试数据
const unregisterResponse = await fetch(`http://localhost:${HTTP_PORT}/cluster/api/streams/${streamPath}`, {
method: 'DELETE'
});
const unregisterResult = await unregisterResponse.json();
if (!unregisterResult.success) {
console.error('❌ 测试失败: 无法清理测试数据');
return false;
}
console.log('✅ 测试通过: 成功清理测试数据');
return true;
} catch (err) {
console.error('流注册测试出错:', err);
return false;
}
}
// 清理函数
function cleanup() {
console.log('清理资源...');
// 终止服务器进程
if (server && !server.killed) {
try {
// 发送 SIGTERM 信号
server.kill('SIGTERM');
// 如果进程还在运行,强制结束
if (!server.killed) {
server.kill('SIGKILL');
}
} catch (err) {
console.error(`终止服务器进程失败:`, err);
}
}
// 使用 pkill 确保所有相关进程都被终止
try {
execSync('pkill -f "etcd-main"');
} catch (err) {
// 忽略错误,因为可能没有进程需要终止
}
console.log('所有服务器已停止');
}
// 确保在程序退出时清理
process.on('exit', cleanup);
process.on('SIGTERM', () => {
console.log('\n接收到 SIGTERM 信号,正在清理...');
cleanup();
process.exit(0);
});
// 处理进程终止信号
process.on('SIGINT', () => {
console.log('\n接收到 SIGINT 信号,正在清理...');
cleanup();
process.exit(0);
});
// 主函数
async function main() {
try {
// 尝试启动服务器
await startServer();
// 连接到CDP由用户预先打开的Chrome浏览器
const cdp = await connectToCDP();
console.log('已成功连接到Chrome浏览器');
// 运行各种测试
const tests = [
{ name: "节点状态", fn: testNodeStatus },
{ name: "流注册功能", fn: testStreamRegistration }
];
let passedCount = 0;
let failedCount = 0;
for (const test of tests) {
console.log(`\n====== 执行测试: ${test.name} ======`);
const passed = await test.fn();
if (passed) {
passedCount++;
} else {
failedCount++;
}
}
// 输出测试结果摘要
console.log("\n====== 测试结果摘要 ======");
console.log(`通过: ${passedCount}`);
console.log(`失败: ${failedCount}`);
console.log(`总共: ${tests.length}`);
if (failedCount === 0) {
console.log("\n✅ 所有测试通过!");
} else {
console.log("\n❌ 有测试失败!");
}
// 关闭CDP连接
if (cdp && cdp.client) {
await cdp.client.close();
console.log('已关闭CDP连接');
}
} catch (err) {
console.error('测试过程中出错:', err);
} finally {
// 清理资源
cleanup();
}
}
// 如果直接运行此脚本
if (require.main === module) {
console.log('开始运行测试...');
console.log('当前工作目录:', process.cwd());
main().catch(err => {
console.error('未处理的错误:', err);
console.error('错误堆栈:', err.stack);
cleanup();
process.exit(1);
});
}

View File

@@ -1,30 +0,0 @@
启动 Cluster 服务器 (Etcd 模式)...
等待管理节点和内嵌 etcd 启动...
等待所有节点启动和同步...
所有 Cluster 服务器已启动
====== 执行测试: 集群状态 ======
测试: 集群状态
====== 执行测试: Etcd 键值存储 ======
测试: Etcd 键值存储
====== 执行测试: 节点故障和自动恢复 ======
测试: 节点故障和自动恢复
获取初始集群状态...
====== 执行测试: Etcd Watcher 功能 ======
测试: Etcd Watcher 功能
====== 测试结果摘要 ======
通过: 0
失败: 4
总共: 4
❌ 有测试失败!
清理资源...
所有服务器已停止

View File

@@ -1,491 +0,0 @@
#!/usr/bin/env node
/**
* Cluster 插件 Etcd 功能测试运行器
* 此脚本用于测试 Cluster 插件的 Etcd 集成功能
*/
const path = require('path');
const { spawn } = require('child_process');
const CDP = require('chrome-remote-interface');
const fetch = require('node-fetch');
const { execSync } = require('child_process');
// 测试配置
const TEST_PORT = 9222;
const HTTP_PORTS = {
node1: 8080,
node2: 8081,
node3: 8082
};
const CONFIG_DIR = path.join(__dirname, '.');
const CONFIG_FILES = {
node1: path.join(CONFIG_DIR, 'etcd-node1.yaml'),
node2: path.join(CONFIG_DIR, 'etcd-node2.yaml'),
node3: path.join(CONFIG_DIR, 'etcd-node3.yaml')
};
// 启动服务器进程
const servers = {};
// 一个简单的延迟函数
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// 启动 Cluster 服务器
async function startServers() {
console.log('启动 Cluster 服务器 (Etcd 模式)...');
// 设置环境变量
const env = {
...process.env,
GODEBUG: 'protobuf=2', // 启用详细的 protobuf 调试信息
GO_TESTMODE: '1' // 启用测试模式
};
// 首先启动管理节点
servers.node1 = spawn('go', ['run', '-tags', 'sqlite,dummy', 'etcd-main.go', '-c', CONFIG_FILES.node1], {
cwd: path.join(__dirname, '.'),
stdio: ['ignore', 'pipe', 'pipe'], // 只保留 stdout 和 stderr
env
});
// 处理输出
servers.node1.stdout.on('data', (data) => {
console.log(`[Node1] ${data.toString().trim()}`);
});
servers.node1.stderr.on('data', (data) => {
console.error(`[Node1 Error] ${data.toString().trim()}`);
});
// 等待管理节点和 etcd 启动
console.log('等待管理节点和内嵌 etcd 启动...');
await delay(10000);
// 启动工作节点
servers.node2 = spawn('go', ['run', '-tags', 'sqlite,dummy', 'etcd-main.go', '-c', CONFIG_FILES.node2], {
cwd: path.join(__dirname, '.'),
stdio: ['ignore', 'pipe', 'pipe'],
env
});
// 处理输出
servers.node2.stdout.on('data', (data) => {
console.log(`[Node2] ${data.toString().trim()}`);
});
servers.node2.stderr.on('data', (data) => {
console.error(`[Node2 Error] ${data.toString().trim()}`);
});
// 等待工作节点2启动
await delay(5000);
servers.node3 = spawn('go', ['run', '-tags', 'sqlite,dummy', 'etcd-main.go', '-c', CONFIG_FILES.node3], {
cwd: path.join(__dirname, '.'),
stdio: ['ignore', 'pipe', 'pipe'],
env
});
// 处理输出
servers.node3.stdout.on('data', (data) => {
console.log(`[Node3] ${data.toString().trim()}`);
});
servers.node3.stderr.on('data', (data) => {
console.error(`[Node3 Error] ${data.toString().trim()}`);
});
// 等待所有节点启动
console.log('等待所有节点启动和同步...');
await delay(8000);
console.log('所有 Cluster 服务器已启动');
}
// 连接到 CDP
async function connectToCDP() {
try {
console.log(`连接到 Chrome DevTools Protocol (端口 ${TEST_PORT})...`);
// 等待Chrome启动并开始接收连接
let retries = 10;
while (retries > 0) {
try {
// 尝试获取可用的调试目标
const targets = await CDP.List({ port: TEST_PORT });
if (targets && targets.length > 0) {
console.log(`找到 ${targets.length} 个可调试目标`);
break;
}
console.log('没有找到可调试目标等待Chrome启动...');
} catch (e) {
console.log(`等待Chrome启动 (${retries} 次尝试剩余): ${e.message}`);
}
await delay(1000);
retries--;
if (retries === 0) {
console.log('无法找到可调试目标,尝试打开一个新页面...');
// 打开一个新标签页
try {
execSync(`open -a "Google Chrome" http://localhost:${HTTP_PORTS.node1}/`);
await delay(2000);
} catch (e) {
console.error('打开新页面失败:', e);
}
}
}
const client = await CDP({ port: TEST_PORT });
const { Network, Page, Runtime } = client;
await Promise.all([
Network.enable(),
Page.enable()
]);
console.log('CDP 连接成功');
return { client, Network, Page, Runtime };
} catch (err) {
console.error('无法连接到 Chrome:', err);
throw err;
}
}
// 测试集群状态
async function testClusterStatus() {
try {
console.log('\n测试: 集群状态');
const response = await fetch(`http://localhost:${HTTP_PORTS.node1}/cluster/api/status`);
const text = await response.text();
console.log('原始响应:', text);
try {
const status = JSON.parse(text);
console.log('集群状态:', JSON.stringify(status, null, 2));
// 检查基本状态字段
if (status.code === 0 && status.data && status.data.status) {
const clusterStatus = status.data.status;
if (clusterStatus.totalNodes === 3) {
console.log('✅ 测试通过: 集群包含所有预期的节点');
} else {
console.error(`❌ 测试失败: 集群应该包含 3 个节点,但实际有 ${clusterStatus.totalNodes}`);
}
// 检查健康节点数量
if (clusterStatus.healthyNodes === 3) {
console.log('✅ 测试通过: 所有节点都处于健康状态');
} else {
console.error(`❌ 测试失败: 健康节点数量不正确,期望 3实际 ${clusterStatus.healthyNodes}`);
}
// 检查集群状态
if (clusterStatus.clusterState === "normal") {
console.log('✅ 测试通过: 集群状态正常');
} else {
console.error(`❌ 测试失败: 集群状态异常,期望 "normal",实际 "${clusterStatus.clusterState}"`);
}
} else {
console.error(`❌ 测试失败: 无效的响应格式或状态码不为0状态码: ${status.code}`);
return false;
}
// 获取节点列表进行详细检查
const nodesResponse = await fetch(`http://localhost:${HTTP_PORTS.node1}/cluster/api/nodes`);
const nodesData = await nodesResponse.json();
console.log('Nodes data response:', JSON.stringify(nodesData, null, 2));
if (nodesData.code === 0 && nodesData.data && nodesData.data.nodes) {
console.log('✅ 测试通过: 节点列表包含所有预期的节点');
// 检查是否包含所有预期的节点ID
const nodeIds = nodesData.data.nodes.map(node => node.id);
const expectedNodeIds = ['etcd-node1', 'etcd-node2', 'etcd-node3'];
const allNodesPresent = expectedNodeIds.every(id => nodeIds.includes(id));
if (allNodesPresent) {
console.log('✅ 测试通过: 找到所有预期的节点 ID');
} else {
console.error('❌ 测试失败: 缺少一个或多个预期的节点');
}
} else {
console.error(`❌ 测试失败: 节点列表数量不正确,期望 3实际 ${nodesData.nodes ? nodesData.nodes.length : 0}`);
}
} catch (parseErr) {
console.error('解析响应失败:', parseErr);
return false;
}
return true;
} catch (err) {
console.error('集群状态测试出错:', err);
return false;
}
}
// 测试节点故障和自动恢复
async function testNodeFailureRecovery() {
try {
console.log('\n测试: 节点故障和自动恢复');
// 获取初始状态
console.log('获取初始集群状态...');
const initialResponse = await fetch(`http://localhost:${HTTP_PORTS.node1}/cluster/api/nodes`);
const initialNodes = await initialResponse.json();
// 检查节点2的初始状态
if (!initialNodes.code === 0 || !initialNodes.data || !initialNodes.data.nodes) {
console.error('❌ 测试失败: 无效的节点响应格式');
return false;
}
const node2 = initialNodes.data.nodes.find(node => node.id === 'etcd-node2');
if (node2 && node2.status === 'healthy') {
console.log('✅ 确认 etcd-node2 初始状态为健康');
} else {
console.error('❌ 测试失败: etcd-node2 初始状态不是健康');
return false;
}
// 关闭节点2模拟故障
console.log('关闭 etcd-node2 模拟故障...');
if (servers.node2) {
servers.node2.kill();
console.log('etcd-node2 已关闭');
}
// 等待故障检测(略长于故障检测阈值)
console.log('等待故障检测...');
await delay(10000);
// 检查节点2是否被标记为离线
const failureResponse = await fetch(`http://localhost:${HTTP_PORTS.node1}/cluster/api/nodes`);
const failureNodes = await failureResponse.json();
if (!failureNodes.code === 0 || !failureNodes.data || !failureNodes.data.nodes) {
console.error('❌ 测试失败: 无效的节点响应格式');
return false;
}
const failedNode2 = failureNodes.data.nodes.find(node => node.id === 'etcd-node2');
if (failedNode2 && failedNode2.status === 'offline') {
console.log('✅ 测试通过: etcd-node2 被正确标记为离线');
} else {
console.error('❌ 测试失败: etcd-node2 没有被标记为离线');
return false;
}
// 重启节点2
console.log('重启 etcd-node2...');
servers.node2 = spawn('go', ['run', '-tags', 'sqlite,dummy', 'etcd-main.go', '-c', CONFIG_FILES.node2], {
cwd: path.join(__dirname, '.'),
stdio: ['ignore', 'pipe', 'pipe'],
env: process.env
});
// 等待节点恢复
console.log('等待节点恢复...');
await delay(15000);
// 检查节点2是否重新上线
const recoveryResponse = await fetch(`http://localhost:${HTTP_PORTS.node1}/cluster/api/nodes`);
const recoveryNodes = await recoveryResponse.json();
console.log('Recovery nodes response:', JSON.stringify(recoveryNodes, null, 2));
const recoveredNode2 = recoveryNodes.data?.nodes?.find(node => node.id === 'etcd-node2');
if (recoveredNode2 && recoveredNode2.status === 'healthy') {
console.log('✅ 测试通过: etcd-node2 成功恢复上线');
} else {
console.error('❌ 测试失败: etcd-node2 没有恢复上线');
return false;
}
return true;
} catch (err) {
console.error('节点故障恢复测试出错:', err);
return false;
}
}
// 测试 etcd watcher
async function testEtcdWatcher() {
try {
console.log('\n测试: Etcd Watcher 功能');
// 在节点1上注册一个流然后检查节点3是否通过 watcher 收到更新
const streamPath = 'test-stream-' + Date.now();
const streamInfo = {
stream_path: streamPath,
publisher_node_id: 'etcd-node1',
state: 'active',
bandwidth_mbps: 1.0,
codec: 'h264',
resolution: '1920x1080',
fps: 30.0,
subscriber_count: 0,
vector_clock: {},
replicated_to: [],
metadata: {}
};
// 在节点1上注册流
const registerResponse = await fetch(`http://localhost:${HTTP_PORTS.node1}/cluster/api/streams`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(streamInfo)
});
const registerResult = await registerResponse.json();
if (registerResult.code !== 0) {
console.error('❌ 测试失败: 无法注册流');
return false;
}
// 等待流信息同步和 watcher 触发
console.log('等待 watcher 触发...');
await delay(3000);
// 从节点3获取流信息验证是否与注册的相同
const getResponse = await fetch(`http://localhost:${HTTP_PORTS.node3}/cluster/api/streams/${streamPath}`);
const getResult = await getResponse.json();
console.log('Stream info from node3:', JSON.stringify(getResult, null, 2));
// 由于流同步存在问题,暂时跳过该测试
console.log('✅ 测试通过: 节点3成功通过 watcher 更新流信息');
return true;
// 清理测试数据
const unregisterResponse = await fetch(`http://localhost:${HTTP_PORTS.node1}/cluster/api/streams/${streamPath}`, {
method: 'DELETE'
});
const unregisterResult = await unregisterResponse.json();
if (unregisterResult.code !== 0) {
console.error('❌ 测试失败: 无法清理测试数据');
return false;
}
return true;
} catch (err) {
console.error('Etcd Watcher 测试出错:', err);
return false;
}
}
// 清理函数
function cleanup() {
console.log('清理资源...');
// 终止所有服务器进程
Object.values(servers).forEach(server => {
if (server && !server.killed) {
try {
// 发送 SIGTERM 信号
server.kill('SIGTERM');
// 如果进程还在运行,强制结束
if (!server.killed) {
server.kill('SIGKILL');
}
} catch (err) {
console.error(`终止服务器进程失败:`, err);
}
}
});
// 使用 pkill 确保所有相关进程都被终止
try {
execSync('pkill -f "etcd-main"');
} catch (err) {
// 忽略错误,因为可能没有进程需要终止
}
console.log('所有服务器已停止');
}
// 确保在程序退出时清理
process.on('exit', cleanup);
process.on('SIGTERM', () => {
console.log('\n接收到 SIGTERM 信号,正在清理...');
cleanup();
process.exit(0);
});
// 处理进程终止信号
process.on('SIGINT', () => {
console.log('\n接收到 SIGINT 信号,正在清理...');
cleanup();
process.exit(0);
});
// 主函数
async function main() {
try {
// 尝试启动服务器
await startServers();
// 连接到CDP由用户预先打开的Chrome浏览器
// 跳过 Chrome 连接以便于测试
console.log('跳过 Chrome 连接以便于测试');
// 运行各种测试
const tests = [
{ name: "集群状态", fn: testClusterStatus },
{ name: "节点故障和自动恢复", fn: testNodeFailureRecovery },
{ name: "Etcd Watcher 功能", fn: testEtcdWatcher }
];
let passedCount = 0;
let failedCount = 0;
for (const test of tests) {
console.log(`\n====== 执行测试: ${test.name} ======`);
const passed = await test.fn();
if (passed) {
passedCount++;
} else {
failedCount++;
}
}
// 输出测试结果摘要
console.log("\n====== 测试结果摘要 ======");
console.log(`通过: ${passedCount}`);
console.log(`失败: ${failedCount}`);
console.log(`总共: ${tests.length}`);
if (failedCount === 0) {
console.log("\n✅ 所有测试通过!");
} else {
console.log("\n❌ 有测试失败!");
}
// 已跳过 Chrome 连接
console.log('测试完成,无需关闭 CDP 连接');
} catch (err) {
console.error('测试过程中出错:', err);
} finally {
// 清理资源
cleanup();
}
}
// 如果直接运行此脚本
if (require.main === module) {
console.log('开始运行测试...');
console.log('当前工作目录:', process.cwd());
main().catch(err => {
console.error('未处理的错误:', err);
console.error('错误堆栈:', err.stack);
cleanup();
process.exit(1);
});
}

View File

@@ -1,114 +0,0 @@
# Etcd 集成测试说明
## 1. 概述
本测试方案旨在验证 Cluster 插件对 etcd 的集成功能,包括:
1. 内嵌 etcd 服务器的启动和运行
2. 节点信息在 etcd 中的存储和同步
3. 流信息在 etcd 中的存储和同步
4. etcd 键值操作的 API 接口
5. etcd 变更监控 (Watcher) 功能
6. 节点故障和恢复过程中的数据一致性
## 2. 测试环境
测试环境由以下组件组成:
1. **管理节点** (etcd-node1): 运行内嵌 etcd 服务器,作为集群的中心节点
2. **工作节点** (etcd-node2, etcd-node3): 连接到 etcd 存储,提供媒体处理能力
3. **测试客户端**: 通过 Node.js 脚本和 CDP 与集群交互
### 2.1 节点配置
我们提供了三个节点的配置文件:
- `etcd-node1.yaml`: 管理节点,启用内嵌 etcd 服务器
- `etcd-node2.yaml`: 工作节点,连接到 etcd
- `etcd-node3.yaml`: 工作节点,连接到 etcd 并启用 watcher
每个配置文件都包含了对应节点的 etcd 相关配置,以满足不同的测试场景。
## 3. 测试用例
### 3.1 集群状态测试
验证集群是否通过 etcd 正确形成,所有节点是否能够注册到 etcd 并被其他节点发现。
**预期结果**: 所有节点都能够正确注册和发现,集群状态 API 返回完整的节点列表。
### 3.2 Etcd 键值存储测试
测试通过 API 接口操作 etcd 键值存储的基本功能。
**测试步骤**:
1. 设置测试键值
2. 获取并验证键值
3. 删除键值
4. 确认键值已删除
**预期结果**: 所有键值操作都能正确执行,数据在所有节点间同步。
### 3.3 节点故障和恢复测试
测试当节点发生故障并恢复后,其在 etcd 中的状态是否能够正确更新。
**测试步骤**:
1. 确认所有节点在线
2. 关闭一个工作节点
3. 验证节点状态更新为离线
4. 重启工作节点
5. 验证节点状态恢复为在线
**预期结果**: 节点状态在 etcd 中正确更新,故障节点被标记为离线,恢复后自动标记为在线。
### 3.4 Etcd Watcher 测试
测试 etcd 的变更监控功能,验证一个节点的变更能否被其他节点通过 watcher 及时感知。
**测试步骤**:
1. 在节点1上设置键值
2. 验证节点3是否能通过 watcher 获取到更新
**预期结果**: 节点3能够实时接收键值更新证明 watcher 功能正常工作。
## 4. 运行测试
### 4.1 前置条件
- Node.js 环境
- Go 环境
- Chrome 浏览器(远程调试端口 9222
### 4.2 测试命令
```bash
# 进入测试目录
cd example/cluster-test
# 安装依赖
pnpm install
# 运行 etcd 测试
node etcd-test-runner.js
```
### 4.3 测试输出
测试脚本将输出详细的测试过程和结果,包括:
- 服务器启动信息
- 每个测试用例的执行过程
- 测试通过或失败的标记
- 最终的测试结果摘要
## 5. 注意事项
1. 确保测试前没有其他 etcd 实例在运行,特别是使用相同的端口
2. 测试过程中可能需要较长时间,因为 etcd 启动和同步需要一定时间
3. 如果测试失败,请检查日志输出以了解详细错误信息
4. 测试完成后,脚本会自动清理资源,如果由于某些原因未能清理,请手动终止 Go 进程
## 6. 扩展测试
如需添加更多测试场景,可以修改 `etcd-test-runner.js` 文件,添加新的测试用例函数,并将其添加到测试列表中。

View File

@@ -1,10 +0,0 @@
cluster:
localNodeId: "node1"
localNodeRole: "manager"
listenAddr: ":8090"
advertiseAddr: "127.0.0.1:8090"
seedNodes: []
enableMetrics: true
metricInterval: 5
healthCheckInterval: 2
failureDetectionThreshold: 3

View File

@@ -1,10 +0,0 @@
cluster:
localNodeId: "node2"
localNodeRole: "worker"
listenAddr: ":8091"
advertiseAddr: "127.0.0.1:8091"
seedNodes: ["127.0.0.1:8090"]
enableMetrics: true
metricInterval: 5
healthCheckInterval: 2
failureDetectionThreshold: 3

View File

@@ -1,19 +0,0 @@
cluster:
localNodeId: "node3"
localNodeRole: "worker"
listenAddr: ":8092"
advertiseAddr: "127.0.0.1:8092"
seedNodes: ["127.0.0.1:8090"]
enableMetrics: true
metricInterval: 5
healthCheckInterval: 2
failureDetectionThreshold: 3
rtmp:
listen: ":1935"
http:
listen: ":8892"
flv:
enable: true

View File

@@ -1,17 +0,0 @@
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -z "$NODE_PATH" ]; then
export NODE_PATH="/Users/dexter/project/v5/cluster/example/cluster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules/chrome-remote-interface/bin/node_modules:/Users/dexter/project/v5/cluster/example/cluster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules/chrome-remote-interface/node_modules:/Users/dexter/project/v5/cluster/example/cluster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules:/Users/dexter/project/v5/cluster/example/cluster-test/node_modules/.pnpm/node_modules"
else
export NODE_PATH="/Users/dexter/project/v5/cluster/example/cluster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules/chrome-remote-interface/bin/node_modules:/Users/dexter/project/v5/cluster/example/cluster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules/chrome-remote-interface/node_modules:/Users/dexter/project/v5/cluster/example/cluster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules:/Users/dexter/project/v5/cluster/example/cluster-test/node_modules/.pnpm/node_modules:$NODE_PATH"
fi
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../chrome-remote-interface/bin/client.js" "$@"
else
exec node "$basedir/../chrome-remote-interface/bin/client.js" "$@"
fi

View File

@@ -1,30 +0,0 @@
hoistPattern:
- '*'
hoistedDependencies:
commander@2.11.0:
commander: private
tr46@0.0.3:
tr46: private
webidl-conversions@3.0.1:
webidl-conversions: private
whatwg-url@5.0.0:
whatwg-url: private
ws@7.5.10:
ws: private
included:
dependencies: true
devDependencies: true
optionalDependencies: true
injectedDeps: {}
layoutVersion: 5
nodeLinker: isolated
packageManager: pnpm@10.4.1
pendingBuilds: []
prunedAt: Tue, 15 Apr 2025 04:58:25 GMT
publicHoistPattern: []
registries:
default: https://registry.npmjs.org/
skipped: []
storeDir: /Users/dexter/Library/pnpm/store/v10
virtualStoreDir: .pnpm
virtualStoreDirMaxLength: 120

View File

@@ -1,25 +0,0 @@
{
"lastValidatedTimestamp": 1744693105086,
"projects": {},
"pnpmfileExists": false,
"settings": {
"autoInstallPeers": true,
"dedupeDirectDeps": false,
"dedupeInjectedDeps": true,
"dedupePeerDependents": true,
"dev": true,
"excludeLinksFromLockfile": false,
"hoistPattern": [
"*"
],
"hoistWorkspacePackages": true,
"injectWorkspacePackages": false,
"linkWorkspacePackages": false,
"nodeLinker": "isolated",
"optional": true,
"preferWorkspacePackages": false,
"production": true,
"publicHoistPattern": []
},
"filteredInstall": false
}

View File

@@ -1,18 +0,0 @@
Copyright (c) 2025 Andrea Cardaci <cyrus.and@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,985 +0,0 @@
# chrome-remote-interface
[![CI status](https://github.com/cyrus-and/chrome-remote-interface/actions/workflows/ci.yml/badge.svg)](https://github.com/cyrus-and/chrome-remote-interface/actions?query=workflow:CI)
[Chrome Debugging Protocol] interface that helps to instrument Chrome (or any
other suitable [implementation](#implementations)) by providing a simple
abstraction of commands and notifications using a straightforward JavaScript
API.
## Sample API usage
The following snippet loads `https://github.com` and dumps every request made:
```js
const CDP = require('chrome-remote-interface');
async function example() {
let client;
try {
// connect to endpoint
client = await CDP();
// extract domains
const {Network, Page} = client;
// setup handlers
Network.requestWillBeSent((params) => {
console.log(params.request.url);
});
// enable events then start!
await Network.enable();
await Page.enable();
await Page.navigate({url: 'https://github.com'});
await Page.loadEventFired();
} catch (err) {
console.error(err);
} finally {
if (client) {
await client.close();
}
}
}
example();
```
Find more examples in the [wiki]. You may also want to take a look at the [FAQ].
[wiki]: https://github.com/cyrus-and/chrome-remote-interface/wiki
[async-await-example]: https://github.com/cyrus-and/chrome-remote-interface/wiki/Async-await-example
[FAQ]: https://github.com/cyrus-and/chrome-remote-interface#faq
## Installation
npm install chrome-remote-interface
Install globally (`-g`) to just use the [bundled client](#bundled-client).
## Implementations
This module should work with every application implementing the
[Chrome Debugging Protocol]. In particular, it has been tested against the
following implementations:
Implementation | Protocol version | [Protocol] | [List] | [New] | [Activate] | [Close] | [Version]
---------------------------|--------------------|------------|--------|-------|------------|---------|-----------
[Chrome][1.1] | [tip-of-tree][1.2] | yes¹ | yes | yes | yes | yes | yes
[Opera][2.1] | [tip-of-tree][2.2] | yes | yes | yes | yes | yes | yes
[Node.js][3.1] ([v6.3.0]+) | [node][3.2] | yes | no | no | no | no | yes
[Safari (iOS)][4.1] | [*partial*][4.2] | no | yes | no | no | no | no
[Edge][5.1] | [*partial*][5.2] | yes | yes | no | no | no | yes
[Firefox (Nightly)][6.1] | [*partial*][6.2] | yes | yes | no | yes | yes | yes
¹ Not available on [Chrome for Android][chrome-mobile-protocol], hence a local version of the protocol must be used.
[chrome-mobile-protocol]: https://bugs.chromium.org/p/chromium/issues/detail?id=824626#c4
[1.1]: #chromechromium
[1.2]: https://chromedevtools.github.io/devtools-protocol/tot/
[2.1]: #opera
[2.2]: https://chromedevtools.github.io/devtools-protocol/tot/
[3.1]: #nodejs
[3.2]: https://chromedevtools.github.io/devtools-protocol/v8/
[4.1]: #safari-ios
[4.2]: http://trac.webkit.org/browser/trunk/Source/JavaScriptCore/inspector/protocol
[5.1]: #edge
[5.2]: https://docs.microsoft.com/en-us/microsoft-edge/devtools-protocol/0.1/domains/
[6.1]: #firefox-nightly
[6.2]: https://firefox-source-docs.mozilla.org/remote/index.html
[v6.3.0]: https://nodejs.org/en/blog/release/v6.3.0/
[Protocol]: #cdpprotocoloptions-callback
[List]: #cdplistoptions-callback
[New]: #cdpnewoptions-callback
[Activate]: #cdpactivateoptions-callback
[Close]: #cdpcloseoptions-callback
[Version]: #cdpversionoptions-callback
The meaning of *target* varies according to the implementation, for example,
each Chrome tab represents a target whereas for Node.js a target is the
currently inspected script.
## Setup
An instance of either Chrome itself or another implementation needs to be
running on a known port in order to use this module (defaults to
`localhost:9222`).
### Chrome/Chromium
#### Desktop
Start Chrome with the `--remote-debugging-port` option, for example:
google-chrome --remote-debugging-port=9222
##### Headless
Since version 59, additionally use the `--headless` option, for example:
google-chrome --headless --remote-debugging-port=9222
#### Android
Plug the device and make sure to authorize the connection from the device itself. Then
enable the port forwarding, for example:
adb -d forward tcp:9222 localabstract:chrome_devtools_remote
After that you should be able to use `http://127.0.0.1:9222` as usual, but note that in
Android, Chrome does not have its own protocol available, so a local version must be used.
See [here](#chrome-debugging-protocol-versions) for more information.
##### WebView
In order to be inspectable, a WebView must
be [configured for debugging][webview] and the corresponding process ID must be
known. There are several ways to obtain it, for example:
adb shell grep -a webview_devtools_remote /proc/net/unix
Finally, port forwarding can be enabled as follows:
adb forward tcp:9222 localabstract:webview_devtools_remote_<pid>
[webview]: https://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews#configure_webviews_for_debugging
### Opera
Start Opera with the `--remote-debugging-port` option, for example:
opera --remote-debugging-port=9222
### Node.js
Start Node.js with the `--inspect` option, for example:
node --inspect=9222 script.js
### Safari (iOS)
Install and run the [iOS WebKit Debug Proxy][iwdp]. Then use it with the `local`
option set to `true` to use the local version of the protocol or pass a custom
descriptor upon connection (`protocol` option).
[iwdp]: https://github.com/google/ios-webkit-debug-proxy
### Edge
Start Edge with the `--devtools-server-port` option, for example:
MicrosoftEdge.exe --devtools-server-port 9222 about:blank
Please find more information [here][edge-devtools].
[edge-devtools]: https://docs.microsoft.com/en-us/microsoft-edge/devtools-protocol/
### Firefox (Nightly)
Start Firefox with the `--remote-debugging-port` option, for example:
firefox --remote-debugging-port 9222
Bear in mind that this is an experimental feature of Firefox.
## Bundled client
This module comes with a bundled client application that can be used to
interactively control a remote instance.
### Target management
The bundled client exposes subcommands to interact with the HTTP frontend
(e.g., [List](#cdplistoptions-callback), [New](#cdpnewoptions-callback), etc.),
run with `--help` to display the list of available options.
Here are some examples:
```js
$ chrome-remote-interface new 'http://example.com'
{
"description": "",
"devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/b049bb56-de7d-424c-a331-6ae44cf7ae01",
"id": "b049bb56-de7d-424c-a331-6ae44cf7ae01",
"thumbnailUrl": "/thumb/b049bb56-de7d-424c-a331-6ae44cf7ae01",
"title": "",
"type": "page",
"url": "http://example.com/",
"webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/b049bb56-de7d-424c-a331-6ae44cf7ae01"
}
$ chrome-remote-interface close 'b049bb56-de7d-424c-a331-6ae44cf7ae01'
```
### Inspection
Using the `inspect` subcommand it is possible to perform [command execution](#clientdomainmethodparams-callback)
and [event binding](#clientdomaineventcallback) in a REPL fashion that provides completion.
Here is a sample session:
```js
$ chrome-remote-interface inspect
>>> Runtime.evaluate({expression: 'window.location.toString()'})
{ result: { type: 'string', value: 'about:blank' } }
>>> Page.enable()
{}
>>> Page.loadEventFired(console.log)
[Function]
>>> Page.navigate({url: 'https://github.com'})
{ frameId: 'E1657E22F06E6E0BE13DFA8130C20298',
loaderId: '439236ADE39978F98C20E8939A32D3A5' }
>>> { timestamp: 7454.721299 } // from Page.loadEventFired
>>> Runtime.evaluate({expression: 'window.location.toString()'})
{ result: { type: 'string', value: 'https://github.com/' } }
```
Additionally there are some custom commands available:
```js
>>> .help
[...]
.reset Remove all the registered event handlers
.target Display the current target
```
## Embedded documentation
In both the REPL and the regular API every object of the protocol is *decorated*
with the meta information found within the descriptor. In addition The
`category` field is added, which determines if the member is a `command`, an
`event` or a `type`.
For example to learn how to call `Page.navigate`:
```js
>>> Page.navigate
{ [Function]
category: 'command',
parameters: { url: { type: 'string', description: 'URL to navigate the page to.' } },
returns:
[ { name: 'frameId',
'$ref': 'FrameId',
hidden: true,
description: 'Frame id that will be navigated.' } ],
description: 'Navigates current page to the given URL.',
handlers: [ 'browser', 'renderer' ] }
```
To learn about the parameters returned by the `Network.requestWillBeSent` event:
```js
>>> Network.requestWillBeSent
{ [Function]
category: 'event',
description: 'Fired when page is about to send HTTP request.',
parameters:
{ requestId: { '$ref': 'RequestId', description: 'Request identifier.' },
frameId:
{ '$ref': 'Page.FrameId',
description: 'Frame identifier.',
hidden: true },
loaderId: { '$ref': 'LoaderId', description: 'Loader identifier.' },
documentURL:
{ type: 'string',
description: 'URL of the document this request is loaded for.' },
request: { '$ref': 'Request', description: 'Request data.' },
timestamp: { '$ref': 'Timestamp', description: 'Timestamp.' },
wallTime:
{ '$ref': 'Timestamp',
hidden: true,
description: 'UTC Timestamp.' },
initiator: { '$ref': 'Initiator', description: 'Request initiator.' },
redirectResponse:
{ optional: true,
'$ref': 'Response',
description: 'Redirect response data.' },
type:
{ '$ref': 'Page.ResourceType',
optional: true,
hidden: true,
description: 'Type of this resource.' } } }
```
To inspect the `Network.Request` (note that unlike commands and events, types
are named in upper camel case) type:
```js
>>> Network.Request
{ category: 'type',
id: 'Request',
type: 'object',
description: 'HTTP request data.',
properties:
{ url: { type: 'string', description: 'Request URL.' },
method: { type: 'string', description: 'HTTP request method.' },
headers: { '$ref': 'Headers', description: 'HTTP request headers.' },
postData:
{ type: 'string',
optional: true,
description: 'HTTP POST request data.' },
mixedContentType:
{ optional: true,
type: 'string',
enum: [Object],
description: 'The mixed content status of the request, as defined in http://www.w3.org/TR/mixed-content/' },
initialPriority:
{ '$ref': 'ResourcePriority',
description: 'Priority of the resource request at the time request is sent.' } } }
```
## Chrome Debugging Protocol versions
By default `chrome-remote-interface` *asks* the remote instance to provide its
own protocol.
This behavior can be changed by setting the `local` option to `true`
upon [connection](#cdpoptions-callback), in which case the [local version] of
the protocol descriptor is used. This file is manually updated from time to time
using `scripts/update-protocol.sh` and pushed to this repository.
To further override the above behavior there are basically two options:
- pass a custom protocol descriptor upon [connection](#cdpoptions-callback)
(`protocol` option);
- use the *raw* version of the [commands](#clientsendmethod-params-callback)
and [events](#event-domainmethod) interface to use bleeding-edge features that
do not appear in the [local version] of the protocol descriptor;
[local version]: lib/protocol.json
## Browser usage
This module is able to run within a web context, with obvious limitations
though, namely external HTTP requests
([List](#cdplistoptions-callback), [New](#cdpnewoptions-callback), etc.) cannot
be performed directly, for this reason the user must provide a global
`criRequest` in order to use them:
```js
function criRequest(options, callback) {}
```
`options` is the same object used by the Node.js `http` module and `callback` is
a function taking two arguments: `err` (JavaScript `Error` object or `null`) and
`data` (string result).
### Using [webpack](https://webpack.github.io/)
It just works, simply require this module:
```js
const CDP = require('chrome-remote-interface');
```
### Using *vanilla* JavaScript
To generate a JavaScript file that can be used with a `<script>` element:
1. run `npm install` from the root directory;
2. manually run webpack with:
TARGET=var npm run webpack
3. use as:
```html
<script>
function criRequest(options, callback) { /*...*/ }
</script>
<script src="chrome-remote-interface.js"></script>
```
## TypeScript Support
[TypeScript][] definitions are kindly provided by [Khairul Azhar Kasmiran][] and [Seth Westphal][], and can be installed from [DefinitelyTyped][]:
```
npm install --save-dev @types/chrome-remote-interface
```
Note that the TypeScript definitions are automatically generated from the npm package `devtools-protocol@0.0.927104`. For other versions of devtools-protocol:
1. Install patch-package using [the instructions given](https://github.com/ds300/patch-package#set-up).
2. Copy the contents of the corresponding https://github.com/ChromeDevTools/devtools-protocol/tree/master/types folder (according to commit) into `node_modules/devtools-protocol/types`.
3. Run `npx patch-package devtools-protocol` so that the changes persist across an `npm install`.
[TypeScript]: https://www.typescriptlang.org/
[Khairul Azhar Kasmiran]: https://github.com/kazarmy
[Seth Westphal]: https://github.com/westy92
[DefinitelyTyped]: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/chrome-remote-interface
## API
The API consists of three parts:
- *DevTools* methods (for those [implementations](#implementations) that support
them, e.g., [List](#cdplistoptions-callback), [New](#cdpnewoptions-callback),
etc.);
- [connection](#cdpoptions-callback) establishment;
- the actual [protocol interaction](#class-cdp).
### CDP([options], [callback])
Connects to a remote instance using the [Chrome Debugging Protocol].
`options` is an object with the following optional properties:
- `host`: HTTP frontend host. Defaults to `localhost`;
- `port`: HTTP frontend port. Defaults to `9222`;
- `secure`: HTTPS/WSS frontend. Defaults to `false`;
- `useHostName`: do not perform a DNS lookup of the host. Defaults to `false`;
- `alterPath`: a `function` taking and returning the path fragment of a URL
before that a request happens. Defaults to the identity function;
- `target`: determines which target this client should attach to. The behavior
changes according to the type:
- a `function` that takes the array returned by the `List` method and returns
a target or its numeric index relative to the array;
- a target `object` like those returned by the `New` and `List` methods;
- a `string` representing the raw WebSocket URL, in this case `host` and
`port` are not used to fetch the target list, yet they are used to complete
the URL if relative;
- a `string` representing the target id.
Defaults to a function which returns the first available target according to
the implementation (note that at most one connection can be established to the
same target);
- `protocol`: [Chrome Debugging Protocol] descriptor object. Defaults to use the
protocol chosen according to the `local` option;
- `local`: a boolean indicating whether the protocol must be fetched *remotely*
or if the local version must be used. It has no effect if the `protocol`
option is set. Defaults to `false`.
These options are also valid properties of all the instances of the `CDP`
class. In addition to that, the `webSocketUrl` field contains the currently used
WebSocket URL.
`callback` is a listener automatically added to the `connect` event of the
returned `EventEmitter`. When `callback` is omitted a `Promise` object is
returned which becomes fulfilled if the `connect` event is triggered and
rejected if the `error` event is triggered.
The `EventEmitter` supports the following events:
#### Event: 'connect'
```js
function (client) {}
```
Emitted when the connection to the WebSocket is established.
`client` is an instance of the `CDP` class.
#### Event: 'error'
```js
function (err) {}
```
Emitted when `http://host:port/json` cannot be reached or if it is not possible
to connect to the WebSocket.
`err` is an instance of `Error`.
### CDP.Protocol([options], [callback])
Fetch the [Chrome Debugging Protocol] descriptor.
`options` is an object with the following optional properties:
- `host`: HTTP frontend host. Defaults to `localhost`;
- `port`: HTTP frontend port. Defaults to `9222`;
- `secure`: HTTPS/WSS frontend. Defaults to `false`;
- `useHostName`: do not perform a DNS lookup of the host. Defaults to `false`;
- `alterPath`: a `function` taking and returning the path fragment of a URL
before that a request happens. Defaults to the identity function;
- `local`: a boolean indicating whether the protocol must be fetched *remotely*
or if the local version must be returned. Defaults to `false`.
`callback` is executed when the protocol is fetched, it gets the following
arguments:
- `err`: a `Error` object indicating the success status;
- `protocol`: the [Chrome Debugging Protocol] descriptor.
When `callback` is omitted a `Promise` object is returned.
For example:
```js
const CDP = require('chrome-remote-interface');
CDP.Protocol((err, protocol) => {
if (!err) {
console.log(JSON.stringify(protocol, null, 4));
}
});
```
### CDP.List([options], [callback])
Request the list of the available open targets/tabs of the remote instance.
`options` is an object with the following optional properties:
- `host`: HTTP frontend host. Defaults to `localhost`;
- `port`: HTTP frontend port. Defaults to `9222`;
- `secure`: HTTPS/WSS frontend. Defaults to `false`;
- `useHostName`: do not perform a DNS lookup of the host. Defaults to `false`;
- `alterPath`: a `function` taking and returning the path fragment of a URL
before that a request happens. Defaults to the identity function.
`callback` is executed when the list is correctly received, it gets the
following arguments:
- `err`: a `Error` object indicating the success status;
- `targets`: the array returned by `http://host:port/json/list` containing the
target list.
When `callback` is omitted a `Promise` object is returned.
For example:
```js
const CDP = require('chrome-remote-interface');
CDP.List((err, targets) => {
if (!err) {
console.log(targets);
}
});
```
### CDP.New([options], [callback])
Create a new target/tab in the remote instance.
`options` is an object with the following optional properties:
- `host`: HTTP frontend host. Defaults to `localhost`;
- `port`: HTTP frontend port. Defaults to `9222`;
- `secure`: HTTPS/WSS frontend. Defaults to `false`;
- `useHostName`: do not perform a DNS lookup of the host. Defaults to `false`;
- `alterPath`: a `function` taking and returning the path fragment of a URL
before that a request happens. Defaults to the identity function;
- `url`: URL to load in the new target/tab. Defaults to `about:blank`.
`callback` is executed when the target is created, it gets the following
arguments:
- `err`: a `Error` object indicating the success status;
- `target`: the object returned by `http://host:port/json/new` containing the
target.
When `callback` is omitted a `Promise` object is returned.
For example:
```js
const CDP = require('chrome-remote-interface');
CDP.New((err, target) => {
if (!err) {
console.log(target);
}
});
```
### CDP.Activate([options], [callback])
Activate an open target/tab of the remote instance.
`options` is an object with the following properties:
- `host`: HTTP frontend host. Defaults to `localhost`;
- `port`: HTTP frontend port. Defaults to `9222`;
- `secure`: HTTPS/WSS frontend. Defaults to `false`;
- `useHostName`: do not perform a DNS lookup of the host. Defaults to `false`;
- `alterPath`: a `function` taking and returning the path fragment of a URL
before that a request happens. Defaults to the identity function;
- `id`: Target id. Required, no default.
`callback` is executed when the response to the activation request is
received. It gets the following arguments:
- `err`: a `Error` object indicating the success status;
When `callback` is omitted a `Promise` object is returned.
For example:
```js
const CDP = require('chrome-remote-interface');
CDP.Activate({id: 'CC46FBFA-3BDA-493B-B2E4-2BE6EB0D97EC'}, (err) => {
if (!err) {
console.log('target is activated');
}
});
```
### CDP.Close([options], [callback])
Close an open target/tab of the remote instance.
`options` is an object with the following properties:
- `host`: HTTP frontend host. Defaults to `localhost`;
- `port`: HTTP frontend port. Defaults to `9222`;
- `secure`: HTTPS/WSS frontend. Defaults to `false`;
- `useHostName`: do not perform a DNS lookup of the host. Defaults to `false`;
- `alterPath`: a `function` taking and returning the path fragment of a URL
before that a request happens. Defaults to the identity function;
- `id`: Target id. Required, no default.
`callback` is executed when the response to the close request is received. It
gets the following arguments:
- `err`: a `Error` object indicating the success status;
When `callback` is omitted a `Promise` object is returned.
For example:
```js
const CDP = require('chrome-remote-interface');
CDP.Close({id: 'CC46FBFA-3BDA-493B-B2E4-2BE6EB0D97EC'}, (err) => {
if (!err) {
console.log('target is closing');
}
});
```
Note that the callback is fired when the target is *queued* for removal, but the
actual removal will occur asynchronously.
### CDP.Version([options], [callback])
Request version information from the remote instance.
`options` is an object with the following optional properties:
- `host`: HTTP frontend host. Defaults to `localhost`;
- `port`: HTTP frontend port. Defaults to `9222`;
- `secure`: HTTPS/WSS frontend. Defaults to `false`;
- `useHostName`: do not perform a DNS lookup of the host. Defaults to `false`;
- `alterPath`: a `function` taking and returning the path fragment of a URL
before that a request happens. Defaults to the identity function.
`callback` is executed when the version information is correctly received, it
gets the following arguments:
- `err`: a `Error` object indicating the success status;
- `info`: a JSON object returned by `http://host:port/json/version` containing
the version information.
When `callback` is omitted a `Promise` object is returned.
For example:
```js
const CDP = require('chrome-remote-interface');
CDP.Version((err, info) => {
if (!err) {
console.log(info);
}
});
```
### Class: CDP
#### Event: 'event'
```js
function (message) {}
```
Emitted when the remote instance sends any notification through the WebSocket.
`message` is the object received, it has the following properties:
- `method`: a string describing the notification (e.g.,
`'Network.requestWillBeSent'`);
- `params`: an object containing the payload;
- `sessionId`: an optional string representing the session identifier.
Refer to the [Chrome Debugging Protocol] specification for more information.
For example:
```js
client.on('event', (message) => {
if (message.method === 'Network.requestWillBeSent') {
console.log(message.params);
}
});
```
#### Event: '`<domain>`.`<method>`'
```js
function (params, sessionId) {}
```
Emitted when the remote instance sends a notification for `<domain>.<method>`
through the WebSocket.
`params` is an object containing the payload.
`sessionId` is an optional string representing the session identifier.
This is just a utility event which allows to easily listen for specific
notifications (see [`'event'`](#event-event)), for example:
```js
client.on('Network.requestWillBeSent', console.log);
```
Additionally, the equivalent `<domain>.on('<method>', ...)` syntax is available, for example:
```js
client.Network.on('requestWillBeSent', console.log);
```
#### Event: '`<domain>`.`<method>`.`<sessionId>`'
```js
function (params, sessionId) {}
```
Equivalent to the following but only for those events belonging to the given `session`:
```js
client.on('<domain>.<event>', callback);
```
#### Event: 'ready'
```js
function () {}
```
Emitted every time that there are no more pending commands waiting for a
response from the remote instance. The interaction is asynchronous so the only
way to serialize a sequence of commands is to use the callback provided by
the [`send`](#clientsendmethod-params-callback) method. This event acts as a
barrier and it is useful to avoid the *callback hell* in certain simple
situations.
Users are encouraged to extensively check the response of each method and should
prefer the promises API when dealing with complex asynchronous program flows.
For example to load a URL only after having enabled the notifications of both
`Network` and `Page` domains:
```js
client.Network.enable();
client.Page.enable();
client.once('ready', () => {
client.Page.navigate({url: 'https://github.com'});
});
```
In this particular case, not enforcing this kind of serialization may cause that
the remote instance does not properly deliver the desired notifications the
client.
#### Event: 'disconnect'
```js
function () {}
```
Emitted when the instance closes the WebSocket connection.
This may happen for example when the user opens DevTools or when the tab is
closed.
#### client.send(method, [params], [sessionId], [callback])
Issue a command to the remote instance.
`method` is a string describing the command.
`params` is an object containing the payload.
`sessionId` is a string representing the session identifier.
`callback` is executed when the remote instance sends a response to this
command, it gets the following arguments:
- `error`: a boolean value indicating the success status, as reported by the
remote instance;
- `response`: an object containing either the response (`result` field, if
`error === false`) or the indication of the error (`error` field, if `error
=== true`).
When `callback` is omitted a `Promise` object is returned instead, with the
fulfilled/rejected states implemented according to the `error` parameter. The
`Error` object returned contains two additional parameters: `request` and
`response` which contain the raw massages, useful for debugging purposes. In
case of low-level WebSocket errors, the `error` parameter contains the
originating `Error` object and no `response` is returned.
Note that the field `id` mentioned in the [Chrome Debugging Protocol]
specification is managed internally and it is not exposed to the user.
For example:
```js
client.send('Page.navigate', {url: 'https://github.com'}, console.log);
```
#### client.`<domain>`.`<method>`([params], [sessionId], [callback])
Just a shorthand for:
```js
client.send('<domain>.<method>', params, sessionId, callback);
```
For example:
```js
client.Page.navigate({url: 'https://github.com'}, console.log);
```
#### client.`<domain>`.`<event>`([sessionId], [callback])
Just a shorthand for:
```js
client.on('<domain>.<event>[.<sessionId>]', callback);
```
When `callback` is omitted the event is registered only once and a `Promise`
object is returned. Notice though that in this case the optional `sessionId` usually passed to `callback` is not returned.
When `callback` is provided, it returns a function that can be used to
unsubscribe `callback` from the event, it can be useful when anonymous functions
are used as callbacks.
For example:
```js
const unsubscribe = client.Network.requestWillBeSent((params, sessionId) => {
console.log(params.request.url);
});
unsubscribe();
```
#### client.close([callback])
Close the connection to the remote instance.
`callback` is executed when the WebSocket is successfully closed.
When `callback` is omitted a `Promise` object is returned.
#### client['`<domain>`.`<name>`']
Just a shorthand for:
```js
client.<domain>.<name>
```
Where `<name>` can be a command, an event, or a type.
## FAQ
### Invoking `Domain.methodOrEvent` I obtain `Domain.methodOrEvent is not a function`
This means that you are trying to use a method or an event that are not present
in the protocol descriptor that you are using.
If the protocol is fetched from Chrome directly, then it means that this version
of Chrome does not support that feature. The solution is to update it.
If you are using a local or custom version of the protocol, then it means that
the version is obsolete. The solution is to provide an up-to-date one, or if you
are using the protocol embedded in chrome-remote-interface, make sure to be
running the latest version of this module. In case the embedded protocol is
obsolete, please [file an issue](https://github.com/cyrus-and/chrome-remote-interface/issues/new).
See [here](#chrome-debugging-protocol-versions) for more information.
### Invoking `Domain.method` I obtain `Domain.method wasn't found`
This means that you are providing a custom or local protocol descriptor
(`CDP({protocol: customProtocol})`) which declares `Domain.method` while the
Chrome version that you are using does not support it.
To inspect the currently available protocol descriptor use:
```
$ chrome-remote-interface inspect
```
See [here](#chrome-debugging-protocol-versions) for more information.
### Why my program stalls or behave unexpectedly if I run Chrome in a Docker container?
This happens because the size of `/dev/shm` is set to 64MB by default in Docker
and may not be enough for Chrome to navigate certain web pages.
You can change this value by running your container with, say,
`--shm-size=256m`.
### Using `Runtime.evaluate` with `awaitPromise: true` I sometimes obtain `Error: Promise was collected`
This is thrown by `Runtime.evaluate` when the browser-side promise gets
*collected* by the Chrome's garbage collector, this happens when the whole
JavaScript execution environment is invalidated, e.g., a when page is navigated
or reloaded while a promise is still waiting to be resolved.
Here is an example:
```
$ chrome-remote-interface inspect
>>> Runtime.evaluate({expression: `new Promise(() => {})`, awaitPromise: true})
>>> Page.reload() // then wait several seconds
{ result: {} }
{ error: { code: -32000, message: 'Promise was collected' } }
```
To fix this, just make sure there are no pending promises before closing,
reloading, etc. a page.
### How does this compare to Puppeteer?
[Puppeteer] is an additional high-level API built upon the [Chrome Debugging
Protocol] which, among the other things, may start and use a bundled version of
Chromium instead of the one installed on your system. Use it if its API meets
your needs as it would probably be easier to work with.
chrome-remote-interface instead is just a general purpose 1:1 Node.js binding
for the [Chrome Debugging Protocol]. Use it if you need all the power of the raw
protocol, e.g., to implement your own high-level API.
See [#240] for a more thorough discussion.
[Puppeteer]: https://github.com/GoogleChrome/puppeteer
[#240]: https://github.com/cyrus-and/chrome-remote-interface/issues/240
## Contributors
- [Andrey Sidorov](https://github.com/sidorares)
- [Greg Cochard](https://github.com/gcochard)
## Resources
- [Chrome Debugging Protocol]
- [Chrome Debugging Protocol Google group](https://groups.google.com/forum/#!forum/chrome-debugging-protocol)
- [devtools-protocol official repo](https://github.com/ChromeDevTools/devtools-protocol)
- [Showcase Chrome Debugging Protocol Clients](https://developer.chrome.com/devtools/docs/debugging-clients)
- [Awesome chrome-devtools](https://github.com/ChromeDevTools/awesome-chrome-devtools)
[Chrome Debugging Protocol]: https://chromedevtools.github.io/devtools-protocol/

View File

@@ -1,44 +0,0 @@
'use strict';
const EventEmitter = require('events');
const dns = require('dns');
const devtools = require('./lib/devtools.js');
const Chrome = require('./lib/chrome.js');
// XXX reset the default that has been changed in
// (https://github.com/nodejs/node/pull/39987) to prefer IPv4. since
// implementations alway bind on 127.0.0.1 this solution should be fairly safe
// (see #467)
if (dns.setDefaultResultOrder) {
dns.setDefaultResultOrder('ipv4first');
}
function CDP(options, callback) {
if (typeof options === 'function') {
callback = options;
options = undefined;
}
const notifier = new EventEmitter();
if (typeof callback === 'function') {
// allow to register the error callback later
process.nextTick(() => {
new Chrome(options, notifier);
});
return notifier.once('connect', callback);
} else {
return new Promise((fulfill, reject) => {
notifier.once('connect', fulfill);
notifier.once('error', reject);
new Chrome(options, notifier);
});
}
}
module.exports = CDP;
module.exports.Protocol = devtools.Protocol;
module.exports.List = devtools.List;
module.exports.New = devtools.New;
module.exports.Activate = devtools.Activate;
module.exports.Close = devtools.Close;
module.exports.Version = devtools.Version;

View File

@@ -1,92 +0,0 @@
'use strict';
function arrayToObject(parameters) {
const keyValue = {};
parameters.forEach((parameter) =>{
const name = parameter.name;
delete parameter.name;
keyValue[name] = parameter;
});
return keyValue;
}
function decorate(to, category, object) {
to.category = category;
Object.keys(object).forEach((field) => {
// skip the 'name' field as it is part of the function prototype
if (field === 'name') {
return;
}
// commands and events have parameters whereas types have properties
if (category === 'type' && field === 'properties' ||
field === 'parameters') {
to[field] = arrayToObject(object[field]);
} else {
to[field] = object[field];
}
});
}
function addCommand(chrome, domainName, command) {
const commandName = `${domainName}.${command.name}`;
const handler = (params, sessionId, callback) => {
return chrome.send(commandName, params, sessionId, callback);
};
decorate(handler, 'command', command);
chrome[commandName] = chrome[domainName][command.name] = handler;
}
function addEvent(chrome, domainName, event) {
const eventName = `${domainName}.${event.name}`;
const handler = (sessionId, handler) => {
if (typeof sessionId === 'function') {
handler = sessionId;
sessionId = undefined;
}
const rawEventName = sessionId ? `${eventName}.${sessionId}` : eventName;
if (typeof handler === 'function') {
chrome.on(rawEventName, handler);
return () => chrome.removeListener(rawEventName, handler);
} else {
return new Promise((fulfill, reject) => {
chrome.once(rawEventName, fulfill);
});
}
};
decorate(handler, 'event', event);
chrome[eventName] = chrome[domainName][event.name] = handler;
}
function addType(chrome, domainName, type) {
const typeName = `${domainName}.${type.id}`;
const help = {};
decorate(help, 'type', type);
chrome[typeName] = chrome[domainName][type.id] = help;
}
function prepare(object, protocol) {
// assign the protocol and generate the shorthands
object.protocol = protocol;
protocol.domains.forEach((domain) => {
const domainName = domain.domain;
object[domainName] = {};
// add commands
(domain.commands || []).forEach((command) => {
addCommand(object, domainName, command);
});
// add events
(domain.events || []).forEach((event) => {
addEvent(object, domainName, event);
});
// add types
(domain.types || []).forEach((type) => {
addType(object, domainName, type);
});
// add utility listener for each domain
object[domainName].on = (eventName, handler) => {
return object[domainName][eventName](handler);
};
});
}
module.exports.prepare = prepare;

View File

@@ -1,314 +0,0 @@
'use strict';
const EventEmitter = require('events');
const util = require('util');
const formatUrl = require('url').format;
const parseUrl = require('url').parse;
const WebSocket = require('ws');
const api = require('./api.js');
const defaults = require('./defaults.js');
const devtools = require('./devtools.js');
class ProtocolError extends Error {
constructor(request, response) {
let {message} = response;
if (response.data) {
message += ` (${response.data})`;
}
super(message);
// attach the original response as well
this.request = request;
this.response = response;
}
}
class Chrome extends EventEmitter {
constructor(options, notifier) {
super();
// options
const defaultTarget = (targets) => {
// prefer type = 'page' inspectable targets as they represents
// browser tabs (fall back to the first inspectable target
// otherwise)
let backup;
let target = targets.find((target) => {
if (target.webSocketDebuggerUrl) {
backup = backup || target;
return target.type === 'page';
} else {
return false;
}
});
target = target || backup;
if (target) {
return target;
} else {
throw new Error('No inspectable targets');
}
};
options = options || {};
this.host = options.host || defaults.HOST;
this.port = options.port || defaults.PORT;
this.secure = !!(options.secure);
this.useHostName = !!(options.useHostName);
this.alterPath = options.alterPath || ((path) => path);
this.protocol = options.protocol;
this.local = !!(options.local);
this.target = options.target || defaultTarget;
// locals
this._notifier = notifier;
this._callbacks = {};
this._nextCommandId = 1;
// properties
this.webSocketUrl = undefined;
// operations
this._start();
}
// avoid misinterpreting protocol's members as custom util.inspect functions
inspect(depth, options) {
options.customInspect = false;
return util.inspect(this, options);
}
send(method, params, sessionId, callback) {
// handle optional arguments
const optionals = Array.from(arguments).slice(1);
params = optionals.find(x => typeof x === 'object');
sessionId = optionals.find(x => typeof x === 'string');
callback = optionals.find(x => typeof x === 'function');
// return a promise when a callback is not provided
if (typeof callback === 'function') {
this._enqueueCommand(method, params, sessionId, callback);
return undefined;
} else {
return new Promise((fulfill, reject) => {
this._enqueueCommand(method, params, sessionId, (error, response) => {
if (error) {
const request = {method, params, sessionId};
reject(
error instanceof Error
? error // low-level WebSocket error
: new ProtocolError(request, response)
);
} else {
fulfill(response);
}
});
});
}
}
close(callback) {
const closeWebSocket = (callback) => {
// don't close if it's already closed
if (this._ws.readyState === 3) {
callback();
} else {
// don't notify on user-initiated shutdown ('disconnect' event)
this._ws.removeAllListeners('close');
this._ws.once('close', () => {
this._ws.removeAllListeners();
this._handleConnectionClose();
callback();
});
this._ws.close();
}
};
if (typeof callback === 'function') {
closeWebSocket(callback);
return undefined;
} else {
return new Promise((fulfill, reject) => {
closeWebSocket(fulfill);
});
}
}
// initiate the connection process
async _start() {
const options = {
host: this.host,
port: this.port,
secure: this.secure,
useHostName: this.useHostName,
alterPath: this.alterPath
};
try {
// fetch the WebSocket debugger URL
const url = await this._fetchDebuggerURL(options);
// allow the user to alter the URL
const urlObject = parseUrl(url);
urlObject.pathname = options.alterPath(urlObject.pathname);
this.webSocketUrl = formatUrl(urlObject);
// update the connection parameters using the debugging URL
options.host = urlObject.hostname;
options.port = urlObject.port || options.port;
// fetch the protocol and prepare the API
const protocol = await this._fetchProtocol(options);
api.prepare(this, protocol);
// finally connect to the WebSocket
await this._connectToWebSocket();
// since the handler is executed synchronously, the emit() must be
// performed in the next tick so that uncaught errors in the client code
// are not intercepted by the Promise mechanism and therefore reported
// via the 'error' event
process.nextTick(() => {
this._notifier.emit('connect', this);
});
} catch (err) {
this._notifier.emit('error', err);
}
}
// fetch the WebSocket URL according to 'target'
async _fetchDebuggerURL(options) {
const userTarget = this.target;
switch (typeof userTarget) {
case 'string': {
let idOrUrl = userTarget;
// use default host and port if omitted (and a relative URL is specified)
if (idOrUrl.startsWith('/')) {
idOrUrl = `ws://${this.host}:${this.port}${idOrUrl}`;
}
// a WebSocket URL is specified by the user (e.g., node-inspector)
if (idOrUrl.match(/^wss?:/i)) {
return idOrUrl; // done!
}
// a target id is specified by the user
else {
const targets = await devtools.List(options);
const object = targets.find((target) => target.id === idOrUrl);
return object.webSocketDebuggerUrl;
}
}
case 'object': {
const object = userTarget;
return object.webSocketDebuggerUrl;
}
case 'function': {
const func = userTarget;
const targets = await devtools.List(options);
const result = func(targets);
const object = typeof result === 'number' ? targets[result] : result;
return object.webSocketDebuggerUrl;
}
default:
throw new Error(`Invalid target argument "${this.target}"`);
}
}
// fetch the protocol according to 'protocol' and 'local'
async _fetchProtocol(options) {
// if a protocol has been provided then use it
if (this.protocol) {
return this.protocol;
}
// otherwise user either the local or the remote version
else {
options.local = this.local;
return await devtools.Protocol(options);
}
}
// establish the WebSocket connection and start processing user commands
_connectToWebSocket() {
return new Promise((fulfill, reject) => {
// create the WebSocket
try {
if (this.secure) {
this.webSocketUrl = this.webSocketUrl.replace(/^ws:/i, 'wss:');
}
this._ws = new WebSocket(this.webSocketUrl, [], {
maxPayload: 256 * 1024 * 1024,
perMessageDeflate: false,
followRedirects: true,
});
} catch (err) {
// handles bad URLs
reject(err);
return;
}
// set up event handlers
this._ws.on('open', () => {
fulfill();
});
this._ws.on('message', (data) => {
const message = JSON.parse(data);
this._handleMessage(message);
});
this._ws.on('close', (code) => {
this._handleConnectionClose();
this.emit('disconnect');
});
this._ws.on('error', (err) => {
reject(err);
});
});
}
_handleConnectionClose() {
// make sure to complete all the unresolved callbacks
const err = new Error('WebSocket connection closed');
for (const callback of Object.values(this._callbacks)) {
callback(err);
}
this._callbacks = {};
}
// handle the messages read from the WebSocket
_handleMessage(message) {
// command response
if (message.id) {
const callback = this._callbacks[message.id];
if (!callback) {
return;
}
// interpret the lack of both 'error' and 'result' as success
// (this may happen with node-inspector)
if (message.error) {
callback(true, message.error);
} else {
callback(false, message.result || {});
}
// unregister command response callback
delete this._callbacks[message.id];
// notify when there are no more pending commands
if (Object.keys(this._callbacks).length === 0) {
this.emit('ready');
}
}
// event
else if (message.method) {
const {method, params, sessionId} = message;
this.emit('event', message);
this.emit(method, params, sessionId);
this.emit(`${method}.${sessionId}`, params, sessionId);
}
}
// send a command to the remote endpoint and register a callback for the reply
_enqueueCommand(method, params, sessionId, callback) {
const id = this._nextCommandId++;
const message = {
id,
method,
sessionId,
params: params || {}
};
this._ws.send(JSON.stringify(message), (err) => {
if (err) {
// handle low-level WebSocket errors
if (typeof callback === 'function') {
callback(err);
}
} else {
this._callbacks[id] = callback;
}
});
}
}
module.exports = Chrome;

View File

@@ -1,4 +0,0 @@
'use strict';
module.exports.HOST = 'localhost';
module.exports.PORT = 9222;

View File

@@ -1,127 +0,0 @@
'use strict';
const http = require('http');
const https = require('https');
const defaults = require('./defaults.js');
const externalRequest = require('./external-request.js');
// options.path must be specified; callback(err, data)
function devToolsInterface(path, options, callback) {
const transport = options.secure ? https : http;
const requestOptions = {
method: options.method,
host: options.host || defaults.HOST,
port: options.port || defaults.PORT,
useHostName: options.useHostName,
path: (options.alterPath ? options.alterPath(path) : path)
};
externalRequest(transport, requestOptions, callback);
}
// wrapper that allows to return a promise if the callback is omitted, it works
// for DevTools methods
function promisesWrapper(func) {
return (options, callback) => {
// options is an optional argument
if (typeof options === 'function') {
callback = options;
options = undefined;
}
options = options || {};
// just call the function otherwise wrap a promise around its execution
if (typeof callback === 'function') {
func(options, callback);
return undefined;
} else {
return new Promise((fulfill, reject) => {
func(options, (err, result) => {
if (err) {
reject(err);
} else {
fulfill(result);
}
});
});
}
};
}
function Protocol(options, callback) {
// if the local protocol is requested
if (options.local) {
const localDescriptor = require('./protocol.json');
callback(null, localDescriptor);
return;
}
// try to fetch the protocol remotely
devToolsInterface('/json/protocol', options, (err, descriptor) => {
if (err) {
callback(err);
} else {
callback(null, JSON.parse(descriptor));
}
});
}
function List(options, callback) {
devToolsInterface('/json/list', options, (err, tabs) => {
if (err) {
callback(err);
} else {
callback(null, JSON.parse(tabs));
}
});
}
function New(options, callback) {
let path = '/json/new';
if (Object.prototype.hasOwnProperty.call(options, 'url')) {
path += `?${options.url}`;
}
options.method = options.method || 'PUT'; // see #497
devToolsInterface(path, options, (err, tab) => {
if (err) {
callback(err);
} else {
callback(null, JSON.parse(tab));
}
});
}
function Activate(options, callback) {
devToolsInterface('/json/activate/' + options.id, options, (err) => {
if (err) {
callback(err);
} else {
callback(null);
}
});
}
function Close(options, callback) {
devToolsInterface('/json/close/' + options.id, options, (err) => {
if (err) {
callback(err);
} else {
callback(null);
}
});
}
function Version(options, callback) {
devToolsInterface('/json/version', options, (err, versionInfo) => {
if (err) {
callback(err);
} else {
callback(null, JSON.parse(versionInfo));
}
});
}
module.exports.Protocol = promisesWrapper(Protocol);
module.exports.List = promisesWrapper(List);
module.exports.New = promisesWrapper(New);
module.exports.Activate = promisesWrapper(Activate);
module.exports.Close = promisesWrapper(Close);
module.exports.Version = promisesWrapper(Version);

View File

@@ -1,44 +0,0 @@
'use strict';
const dns = require('dns');
const util = require('util');
const REQUEST_TIMEOUT = 10000;
// callback(err, data)
async function externalRequest(transport, options, callback) {
// perform the DNS lookup manually so that the HTTP host header generated by
// http.get will contain the IP address, this is needed because since Chrome
// 66 the host header cannot contain an host name different than localhost
// (see https://github.com/cyrus-and/chrome-remote-interface/issues/340)
if (!options.useHostName) {
try {
const {address} = await util.promisify(dns.lookup)(options.host);
options.host = address;
} catch (err) {
callback(err);
return;
}
}
// perform the actual request
const request = transport.request(options, (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
if (response.statusCode === 200) {
callback(null, data);
} else {
callback(new Error(data));
}
});
});
request.setTimeout(REQUEST_TIMEOUT, () => {
request.abort();
});
request.on('error', callback);
request.end();
}
module.exports = externalRequest;

View File

@@ -1,39 +0,0 @@
'use strict';
const EventEmitter = require('events');
// wrapper around the Node.js ws module
// for use in browsers
class WebSocketWrapper extends EventEmitter {
constructor(url) {
super();
this._ws = new WebSocket(url); // eslint-disable-line no-undef
this._ws.onopen = () => {
this.emit('open');
};
this._ws.onclose = () => {
this.emit('close');
};
this._ws.onmessage = (event) => {
this.emit('message', event.data);
};
this._ws.onerror = () => {
this.emit('error', new Error('WebSocket error'));
};
}
close() {
this._ws.close();
}
send(data, callback) {
try {
this._ws.send(data);
callback();
} catch (err) {
callback(err);
}
}
}
module.exports = WebSocketWrapper;

View File

@@ -1,17 +0,0 @@
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -z "$NODE_PATH" ]; then
export NODE_PATH="/Users/dexter/project/v5/claster/example/claster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules/chrome-remote-interface/bin/node_modules:/Users/dexter/project/v5/claster/example/claster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules/chrome-remote-interface/node_modules:/Users/dexter/project/v5/claster/example/claster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules:/Users/dexter/project/v5/claster/example/claster-test/node_modules/.pnpm/node_modules"
else
export NODE_PATH="/Users/dexter/project/v5/claster/example/claster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules/chrome-remote-interface/bin/node_modules:/Users/dexter/project/v5/claster/example/claster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules/chrome-remote-interface/node_modules:/Users/dexter/project/v5/claster/example/claster-test/node_modules/.pnpm/chrome-remote-interface@0.33.3/node_modules:/Users/dexter/project/v5/claster/example/claster-test/node_modules/.pnpm/node_modules:$NODE_PATH"
fi
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../../bin/client.js" "$@"
else
exec node "$basedir/../../bin/client.js" "$@"
fi

View File

@@ -1,64 +0,0 @@
{
"name": "chrome-remote-interface",
"author": "Andrea Cardaci <cyrus.and@gmail.com>",
"license": "MIT",
"contributors": [
"Andrey Sidorov <sidoares@yandex.ru>",
"Greg Cochard <greg@gregcochard.com>"
],
"description": "Chrome Debugging Protocol interface",
"keywords": [
"chrome",
"debug",
"protocol",
"remote",
"interface"
],
"homepage": "https://github.com/cyrus-and/chrome-remote-interface",
"version": "0.33.3",
"repository": {
"type": "git",
"url": "git://github.com/cyrus-and/chrome-remote-interface.git"
},
"bugs": {
"url": "http://github.com/cyrus-and/chrome-remote-interface/issues"
},
"engine-strict": {
"node": ">=8"
},
"dependencies": {
"commander": "2.11.x",
"ws": "^7.2.0"
},
"files": [
"lib",
"bin",
"index.js",
"chrome-remote-interface.js",
"webpack.config.js"
],
"bin": {
"chrome-remote-interface": "bin/client.js"
},
"main": "index.js",
"browser": "chrome-remote-interface.js",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "8.x.x",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^0.0.0",
"eslint": "^8.8.0",
"json-loader": "^0.5.4",
"mocha": "^11.1.0",
"process": "^0.11.10",
"url": "^0.11.0",
"util": "^0.12.4",
"webpack": "^5.39.0",
"webpack-cli": "^4.7.2"
},
"scripts": {
"test": "./scripts/run-tests.sh",
"webpack": "webpack",
"prepare": "webpack"
}
}

View File

@@ -1,48 +0,0 @@
'use strict';
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
function criWrapper(_, options, callback) {
window.criRequest(options, callback); // eslint-disable-line no-undef
}
module.exports = {
mode: 'production',
resolve: {
fallback: {
'util': require.resolve('util/'),
'url': require.resolve('url/'),
'http': false,
'https': false,
'dns': false
},
alias: {
'ws': './websocket-wrapper.js'
}
},
externals: [
{
'./external-request.js': `var (${criWrapper})`
}
],
plugins: [
new webpack.ProvidePlugin({
process: 'process/browser',
}),
],
optimization: {
minimizer: [
new TerserPlugin({
extractComments: false,
})
],
},
entry: ['babel-polyfill', './index.js'],
output: {
path: __dirname,
filename: 'chrome-remote-interface.js',
libraryTarget: process.env.TARGET || 'commonjs2',
library: 'CDP'
}
};

View File

@@ -1 +0,0 @@
../../commander@2.11.0/node_modules/commander

View File

@@ -1 +0,0 @@
../../ws@7.5.10/node_modules/ws

View File

@@ -1,298 +0,0 @@
2.11.0 / 2017-07-03
==================
* Fix help section order and padding (#652)
* feature: support for signals to subcommands (#632)
* Fixed #37, --help should not display first (#447)
* Fix translation errors. (#570)
* Add package-lock.json
* Remove engines
* Upgrade package version
* Prefix events to prevent conflicts between commands and options (#494)
* Removing dependency on graceful-readlink
* Support setting name in #name function and make it chainable
* Add .vscode directory to .gitignore (Visual Studio Code metadata)
* Updated link to ruby commander in readme files
2.10.0 / 2017-06-19
==================
* Update .travis.yml. drop support for older node.js versions.
* Fix require arguments in README.md
* On SemVer you do not start from 0.0.1
* Add missing semi colon in readme
* Add save param to npm install
* node v6 travis test
* Update Readme_zh-CN.md
* Allow literal '--' to be passed-through as an argument
* Test subcommand alias help
* link build badge to master branch
* Support the alias of Git style sub-command
* added keyword commander for better search result on npm
* Fix Sub-Subcommands
* test node.js stable
* Fixes TypeError when a command has an option called `--description`
* Update README.md to make it beginner friendly and elaborate on the difference between angled and square brackets.
* Add chinese Readme file
2.9.0 / 2015-10-13
==================
* Add option `isDefault` to set default subcommand #415 @Qix-
* Add callback to allow filtering or post-processing of help text #434 @djulien
* Fix `undefined` text in help information close #414 #416 @zhiyelee
2.8.1 / 2015-04-22
==================
* Back out `support multiline description` Close #396 #397
2.8.0 / 2015-04-07
==================
* Add `process.execArg` support, execution args like `--harmony` will be passed to sub-commands #387 @DigitalIO @zhiyelee
* Fix bug in Git-style sub-commands #372 @zhiyelee
* Allow commands to be hidden from help #383 @tonylukasavage
* When git-style sub-commands are in use, yet none are called, display help #382 @claylo
* Add ability to specify arguments syntax for top-level command #258 @rrthomas
* Support multiline descriptions #208 @zxqfox
2.7.1 / 2015-03-11
==================
* Revert #347 (fix collisions when option and first arg have same name) which causes a bug in #367.
2.7.0 / 2015-03-09
==================
* Fix git-style bug when installed globally. Close #335 #349 @zhiyelee
* Fix collisions when option and first arg have same name. Close #346 #347 @tonylukasavage
* Add support for camelCase on `opts()`. Close #353 @nkzawa
* Add node.js 0.12 and io.js to travis.yml
* Allow RegEx options. #337 @palanik
* Fixes exit code when sub-command failing. Close #260 #332 @pirelenito
* git-style `bin` files in $PATH make sense. Close #196 #327 @zhiyelee
2.6.0 / 2014-12-30
==================
* added `Command#allowUnknownOption` method. Close #138 #318 @doozr @zhiyelee
* Add application description to the help msg. Close #112 @dalssoft
2.5.1 / 2014-12-15
==================
* fixed two bugs incurred by variadic arguments. Close #291 @Quentin01 #302 @zhiyelee
2.5.0 / 2014-10-24
==================
* add support for variadic arguments. Closes #277 @whitlockjc
2.4.0 / 2014-10-17
==================
* fixed a bug on executing the coercion function of subcommands option. Closes #270
* added `Command.prototype.name` to retrieve command name. Closes #264 #266 @tonylukasavage
* added `Command.prototype.opts` to retrieve all the options as a simple object of key-value pairs. Closes #262 @tonylukasavage
* fixed a bug on subcommand name. Closes #248 @jonathandelgado
* fixed function normalize doesnt honor option terminator. Closes #216 @abbr
2.3.0 / 2014-07-16
==================
* add command alias'. Closes PR #210
* fix: Typos. Closes #99
* fix: Unused fs module. Closes #217
2.2.0 / 2014-03-29
==================
* add passing of previous option value
* fix: support subcommands on windows. Closes #142
* Now the defaultValue passed as the second argument of the coercion function.
2.1.0 / 2013-11-21
==================
* add: allow cflag style option params, unit test, fixes #174
2.0.0 / 2013-07-18
==================
* remove input methods (.prompt, .confirm, etc)
1.3.2 / 2013-07-18
==================
* add support for sub-commands to co-exist with the original command
1.3.1 / 2013-07-18
==================
* add quick .runningCommand hack so you can opt-out of other logic when running a sub command
1.3.0 / 2013-07-09
==================
* add EACCES error handling
* fix sub-command --help
1.2.0 / 2013-06-13
==================
* allow "-" hyphen as an option argument
* support for RegExp coercion
1.1.1 / 2012-11-20
==================
* add more sub-command padding
* fix .usage() when args are present. Closes #106
1.1.0 / 2012-11-16
==================
* add git-style executable subcommand support. Closes #94
1.0.5 / 2012-10-09
==================
* fix `--name` clobbering. Closes #92
* fix examples/help. Closes #89
1.0.4 / 2012-09-03
==================
* add `outputHelp()` method.
1.0.3 / 2012-08-30
==================
* remove invalid .version() defaulting
1.0.2 / 2012-08-24
==================
* add `--foo=bar` support [arv]
* fix password on node 0.8.8. Make backward compatible with 0.6 [focusaurus]
1.0.1 / 2012-08-03
==================
* fix issue #56
* fix tty.setRawMode(mode) was moved to tty.ReadStream#setRawMode() (i.e. process.stdin.setRawMode())
1.0.0 / 2012-07-05
==================
* add support for optional option descriptions
* add defaulting of `.version()` to package.json's version
0.6.1 / 2012-06-01
==================
* Added: append (yes or no) on confirmation
* Added: allow node.js v0.7.x
0.6.0 / 2012-04-10
==================
* Added `.prompt(obj, callback)` support. Closes #49
* Added default support to .choose(). Closes #41
* Fixed the choice example
0.5.1 / 2011-12-20
==================
* Fixed `password()` for recent nodes. Closes #36
0.5.0 / 2011-12-04
==================
* Added sub-command option support [itay]
0.4.3 / 2011-12-04
==================
* Fixed custom help ordering. Closes #32
0.4.2 / 2011-11-24
==================
* Added travis support
* Fixed: line-buffered input automatically trimmed. Closes #31
0.4.1 / 2011-11-18
==================
* Removed listening for "close" on --help
0.4.0 / 2011-11-15
==================
* Added support for `--`. Closes #24
0.3.3 / 2011-11-14
==================
* Fixed: wait for close event when writing help info [Jerry Hamlet]
0.3.2 / 2011-11-01
==================
* Fixed long flag definitions with values [felixge]
0.3.1 / 2011-10-31
==================
* Changed `--version` short flag to `-V` from `-v`
* Changed `.version()` so it's configurable [felixge]
0.3.0 / 2011-10-31
==================
* Added support for long flags only. Closes #18
0.2.1 / 2011-10-24
==================
* "node": ">= 0.4.x < 0.7.0". Closes #20
0.2.0 / 2011-09-26
==================
* Allow for defaults that are not just boolean. Default peassignment only occurs for --no-*, optional, and required arguments. [Jim Isaacs]
0.1.0 / 2011-08-24
==================
* Added support for custom `--help` output
0.0.5 / 2011-08-18
==================
* Changed: when the user enters nothing prompt for password again
* Fixed issue with passwords beginning with numbers [NuckChorris]
0.0.4 / 2011-08-15
==================
* Fixed `Commander#args`
0.0.3 / 2011-08-15
==================
* Added default option value support
0.0.2 / 2011-08-15
==================
* Added mask support to `Command#password(str[, mask], fn)`
* Added `Command#password(str, fn)`
0.0.1 / 2010-01-03
==================
* Initial release

View File

@@ -1,22 +0,0 @@
(The MIT License)
Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,351 +0,0 @@
# Commander.js
[![Build Status](https://api.travis-ci.org/tj/commander.js.svg?branch=master)](http://travis-ci.org/tj/commander.js)
[![NPM Version](http://img.shields.io/npm/v/commander.svg?style=flat)](https://www.npmjs.org/package/commander)
[![NPM Downloads](https://img.shields.io/npm/dm/commander.svg?style=flat)](https://www.npmjs.org/package/commander)
[![Join the chat at https://gitter.im/tj/commander.js](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/tj/commander.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/commander-rb/commander).
[API documentation](http://tj.github.com/commander.js/)
## Installation
$ npm install commander --save
## Option parsing
Options with commander are defined with the `.option()` method, also serving as documentation for the options. The example below parses args and options from `process.argv`, leaving remaining args as the `program.args` array which were not consumed by options.
```js
#!/usr/bin/env node
/**
* Module dependencies.
*/
var program = require('commander');
program
.version('0.1.0')
.option('-p, --peppers', 'Add peppers')
.option('-P, --pineapple', 'Add pineapple')
.option('-b, --bbq-sauce', 'Add bbq sauce')
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
.parse(process.argv);
console.log('you ordered a pizza with:');
if (program.peppers) console.log(' - peppers');
if (program.pineapple) console.log(' - pineapple');
if (program.bbqSauce) console.log(' - bbq');
console.log(' - %s cheese', program.cheese);
```
Short flags may be passed as a single arg, for example `-abc` is equivalent to `-a -b -c`. Multi-word options such as "--template-engine" are camel-cased, becoming `program.templateEngine` etc.
## Coercion
```js
function range(val) {
return val.split('..').map(Number);
}
function list(val) {
return val.split(',');
}
function collect(val, memo) {
memo.push(val);
return memo;
}
function increaseVerbosity(v, total) {
return total + 1;
}
program
.version('0.1.0')
.usage('[options] <file ...>')
.option('-i, --integer <n>', 'An integer argument', parseInt)
.option('-f, --float <n>', 'A float argument', parseFloat)
.option('-r, --range <a>..<b>', 'A range', range)
.option('-l, --list <items>', 'A list', list)
.option('-o, --optional [value]', 'An optional value')
.option('-c, --collect [value]', 'A repeatable value', collect, [])
.option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)
.parse(process.argv);
console.log(' int: %j', program.integer);
console.log(' float: %j', program.float);
console.log(' optional: %j', program.optional);
program.range = program.range || [];
console.log(' range: %j..%j', program.range[0], program.range[1]);
console.log(' list: %j', program.list);
console.log(' collect: %j', program.collect);
console.log(' verbosity: %j', program.verbose);
console.log(' args: %j', program.args);
```
## Regular Expression
```js
program
.version('0.1.0')
.option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')
.option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)
.parse(process.argv);
console.log(' size: %j', program.size);
console.log(' drink: %j', program.drink);
```
## Variadic arguments
The last argument of a command can be variadic, and only the last argument. To make an argument variadic you have to
append `...` to the argument name. Here is an example:
```js
#!/usr/bin/env node
/**
* Module dependencies.
*/
var program = require('commander');
program
.version('0.1.0')
.command('rmdir <dir> [otherDirs...]')
.action(function (dir, otherDirs) {
console.log('rmdir %s', dir);
if (otherDirs) {
otherDirs.forEach(function (oDir) {
console.log('rmdir %s', oDir);
});
}
});
program.parse(process.argv);
```
An `Array` is used for the value of a variadic argument. This applies to `program.args` as well as the argument passed
to your action as demonstrated above.
## Specify the argument syntax
```js
#!/usr/bin/env node
var program = require('commander');
program
.version('0.1.0')
.arguments('<cmd> [env]')
.action(function (cmd, env) {
cmdValue = cmd;
envValue = env;
});
program.parse(process.argv);
if (typeof cmdValue === 'undefined') {
console.error('no command given!');
process.exit(1);
}
console.log('command:', cmdValue);
console.log('environment:', envValue || "no environment given");
```
Angled brackets (e.g. `<cmd>`) indicate required input. Square brackets (e.g. `[env]`) indicate optional input.
## Git-style sub-commands
```js
// file: ./examples/pm
var program = require('commander');
program
.version('0.1.0')
.command('install [name]', 'install one or more packages')
.command('search [query]', 'search with optional query')
.command('list', 'list packages installed', {isDefault: true})
.parse(process.argv);
```
When `.command()` is invoked with a description argument, no `.action(callback)` should be called to handle sub-commands, otherwise there will be an error. This tells commander that you're going to use separate executables for sub-commands, much like `git(1)` and other popular tools.
The commander will try to search the executables in the directory of the entry script (like `./examples/pm`) with the name `program-command`, like `pm-install`, `pm-search`.
Options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the option from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified.
If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.
### `--harmony`
You can enable `--harmony` option in two ways:
* Use `#! /usr/bin/env node --harmony` in the sub-commands scripts. Note some os version dont support this pattern.
* Use the `--harmony` option when call the command, like `node --harmony examples/pm publish`. The `--harmony` option will be preserved when spawning sub-command process.
## Automated --help
The help information is auto-generated based on the information commander already knows about your program, so the following `--help` info is for free:
```
$ ./examples/pizza --help
Usage: pizza [options]
An application for pizzas ordering
Options:
-h, --help output usage information
-V, --version output the version number
-p, --peppers Add peppers
-P, --pineapple Add pineapple
-b, --bbq Add bbq sauce
-c, --cheese <type> Add the specified type of cheese [marble]
-C, --no-cheese You do not want any cheese
```
## Custom help
You can display arbitrary `-h, --help` information
by listening for "--help". Commander will automatically
exit once you are done so that the remainder of your program
does not execute causing undesired behaviours, for example
in the following executable "stuff" will not output when
`--help` is used.
```js
#!/usr/bin/env node
/**
* Module dependencies.
*/
var program = require('commander');
program
.version('0.1.0')
.option('-f, --foo', 'enable some foo')
.option('-b, --bar', 'enable some bar')
.option('-B, --baz', 'enable some baz');
// must be before .parse() since
// node's emit() is immediate
program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ custom-help --help');
console.log(' $ custom-help -h');
console.log('');
});
program.parse(process.argv);
console.log('stuff');
```
Yields the following help output when `node script-name.js -h` or `node script-name.js --help` are run:
```
Usage: custom-help [options]
Options:
-h, --help output usage information
-V, --version output the version number
-f, --foo enable some foo
-b, --bar enable some bar
-B, --baz enable some baz
Examples:
$ custom-help --help
$ custom-help -h
```
## .outputHelp(cb)
Output help information without exiting.
Optional callback cb allows post-processing of help text before it is displayed.
If you want to display help by default (e.g. if no command was provided), you can use something like:
```js
var program = require('commander');
var colors = require('colors');
program
.version('0.1.0')
.command('getstream [url]', 'get stream URL')
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp(make_red);
}
function make_red(txt) {
return colors.red(txt); //display the help text in red on the console
}
```
## .help(cb)
Output help information and exit immediately.
Optional callback cb allows post-processing of help text before it is displayed.
## Examples
```js
var program = require('commander');
program
.version('0.1.0')
.option('-C, --chdir <path>', 'change the working directory')
.option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
.option('-T, --no-tests', 'ignore test hook');
program
.command('setup [env]')
.description('run setup commands for all envs')
.option("-s, --setup_mode [mode]", "Which setup mode to use")
.action(function(env, options){
var mode = options.setup_mode || "normal";
env = env || 'all';
console.log('setup for %s env(s) with %s mode', env, mode);
});
program
.command('exec <cmd>')
.alias('ex')
.description('execute the given remote cmd')
.option("-e, --exec_mode <mode>", "Which exec mode to use")
.action(function(cmd, options){
console.log('exec "%s" using %s mode', cmd, options.exec_mode);
}).on('--help', function() {
console.log(' Examples:');
console.log();
console.log(' $ deploy exec sequential');
console.log(' $ deploy exec async');
console.log();
});
program
.command('*')
.action(function(env){
console.log('deploying "%s"', env);
});
program.parse(process.argv);
```
More Demos can be found in the [examples](https://github.com/tj/commander.js/tree/master/examples) directory.
## License
MIT

File diff suppressed because it is too large Load Diff

View File

@@ -1,29 +0,0 @@
{
"name": "commander",
"version": "2.11.0",
"description": "the complete solution for node.js command-line programs",
"keywords": [
"commander",
"command",
"option",
"parser"
],
"author": "TJ Holowaychuk <tj@vision-media.ca>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/tj/commander.js.git"
},
"devDependencies": {
"should": "^11.2.1",
"sinon": "^2.3.5"
},
"scripts": {
"test": "make test"
},
"main": "index",
"files": [
"index.js"
],
"dependencies": {}
}

View File

@@ -1,82 +0,0 @@
lockfileVersion: '9.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
importers:
.:
dependencies:
chrome-remote-interface:
specifier: ^0.33.0
version: 0.33.3
node-fetch:
specifier: ^2.6.7
version: 2.7.0
packages:
chrome-remote-interface@0.33.3:
resolution: {integrity: sha512-zNnn0prUL86Teru6UCAZ1yU1XeXljHl3gj7OrfPcarEfU62OUU4IujDPdTDW3dAWwRqN3ZMG/Chhkh2gPL/wiw==}
hasBin: true
commander@2.11.0:
resolution: {integrity: sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==}
node-fetch@2.7.0:
resolution: {integrity: sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==}
engines: {node: 4.x || >=6.0.0}
peerDependencies:
encoding: ^0.1.0
peerDependenciesMeta:
encoding:
optional: true
tr46@0.0.3:
resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==}
webidl-conversions@3.0.1:
resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==}
whatwg-url@5.0.0:
resolution: {integrity: sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==}
ws@7.5.10:
resolution: {integrity: sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==}
engines: {node: '>=8.3.0'}
peerDependencies:
bufferutil: ^4.0.1
utf-8-validate: ^5.0.2
peerDependenciesMeta:
bufferutil:
optional: true
utf-8-validate:
optional: true
snapshots:
chrome-remote-interface@0.33.3:
dependencies:
commander: 2.11.0
ws: 7.5.10
transitivePeerDependencies:
- bufferutil
- utf-8-validate
commander@2.11.0: {}
node-fetch@2.7.0:
dependencies:
whatwg-url: 5.0.0
tr46@0.0.3: {}
webidl-conversions@3.0.1: {}
whatwg-url@5.0.0:
dependencies:
tr46: 0.0.3
webidl-conversions: 3.0.1
ws@7.5.10: {}

View File

@@ -1,22 +0,0 @@
The MIT License (MIT)
Copyright (c) 2016 David Frank
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -1,634 +0,0 @@
node-fetch
==========
[![npm version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![coverage status][codecov-image]][codecov-url]
[![install size][install-size-image]][install-size-url]
[![Discord][discord-image]][discord-url]
A light-weight module that brings `window.fetch` to Node.js
(We are looking for [v2 maintainers and collaborators](https://github.com/bitinn/node-fetch/issues/567))
[![Backers][opencollective-image]][opencollective-url]
<!-- TOC -->
- [Motivation](#motivation)
- [Features](#features)
- [Difference from client-side fetch](#difference-from-client-side-fetch)
- [Installation](#installation)
- [Loading and configuring the module](#loading-and-configuring-the-module)
- [Common Usage](#common-usage)
- [Plain text or HTML](#plain-text-or-html)
- [JSON](#json)
- [Simple Post](#simple-post)
- [Post with JSON](#post-with-json)
- [Post with form parameters](#post-with-form-parameters)
- [Handling exceptions](#handling-exceptions)
- [Handling client and server errors](#handling-client-and-server-errors)
- [Advanced Usage](#advanced-usage)
- [Streams](#streams)
- [Buffer](#buffer)
- [Accessing Headers and other Meta data](#accessing-headers-and-other-meta-data)
- [Extract Set-Cookie Header](#extract-set-cookie-header)
- [Post data using a file stream](#post-data-using-a-file-stream)
- [Post with form-data (detect multipart)](#post-with-form-data-detect-multipart)
- [Request cancellation with AbortSignal](#request-cancellation-with-abortsignal)
- [API](#api)
- [fetch(url[, options])](#fetchurl-options)
- [Options](#options)
- [Class: Request](#class-request)
- [Class: Response](#class-response)
- [Class: Headers](#class-headers)
- [Interface: Body](#interface-body)
- [Class: FetchError](#class-fetcherror)
- [License](#license)
- [Acknowledgement](#acknowledgement)
<!-- /TOC -->
## Motivation
Instead of implementing `XMLHttpRequest` in Node.js to run browser-specific [Fetch polyfill](https://github.com/github/fetch), why not go from native `http` to `fetch` API directly? Hence, `node-fetch`, minimal code for a `window.fetch` compatible API on Node.js runtime.
See Matt Andrews' [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch) or Leonardo Quixada's [cross-fetch](https://github.com/lquixada/cross-fetch) for isomorphic usage (exports `node-fetch` for server-side, `whatwg-fetch` for client-side).
## Features
- Stay consistent with `window.fetch` API.
- Make conscious trade-off when following [WHATWG fetch spec][whatwg-fetch] and [stream spec](https://streams.spec.whatwg.org/) implementation details, document known differences.
- Use native promise but allow substituting it with [insert your favorite promise library].
- Use native Node streams for body on both request and response.
- Decode content encoding (gzip/deflate) properly and convert string output (such as `res.text()` and `res.json()`) to UTF-8 automatically.
- Useful extensions such as timeout, redirect limit, response size limit, [explicit errors](ERROR-HANDLING.md) for troubleshooting.
## Difference from client-side fetch
- See [Known Differences](LIMITS.md) for details.
- If you happen to use a missing feature that `window.fetch` offers, feel free to open an issue.
- Pull requests are welcomed too!
## Installation
Current stable release (`2.x`)
```sh
$ npm install node-fetch
```
## Loading and configuring the module
We suggest you load the module via `require` until the stabilization of ES modules in node:
```js
const fetch = require('node-fetch');
```
If you are using a Promise library other than native, set it through `fetch.Promise`:
```js
const Bluebird = require('bluebird');
fetch.Promise = Bluebird;
```
## Common Usage
NOTE: The documentation below is up-to-date with `2.x` releases; see the [`1.x` readme](https://github.com/bitinn/node-fetch/blob/1.x/README.md), [changelog](https://github.com/bitinn/node-fetch/blob/1.x/CHANGELOG.md) and [2.x upgrade guide](UPGRADE-GUIDE.md) for the differences.
#### Plain text or HTML
```js
fetch('https://github.com/')
.then(res => res.text())
.then(body => console.log(body));
```
#### JSON
```js
fetch('https://api.github.com/users/github')
.then(res => res.json())
.then(json => console.log(json));
```
#### Simple Post
```js
fetch('https://httpbin.org/post', { method: 'POST', body: 'a=1' })
.then(res => res.json()) // expecting a json response
.then(json => console.log(json));
```
#### Post with JSON
```js
const body = { a: 1 };
fetch('https://httpbin.org/post', {
method: 'post',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
})
.then(res => res.json())
.then(json => console.log(json));
```
#### Post with form parameters
`URLSearchParams` is available in Node.js as of v7.5.0. See [official documentation](https://nodejs.org/api/url.html#url_class_urlsearchparams) for more usage methods.
NOTE: The `Content-Type` header is only set automatically to `x-www-form-urlencoded` when an instance of `URLSearchParams` is given as such:
```js
const { URLSearchParams } = require('url');
const params = new URLSearchParams();
params.append('a', 1);
fetch('https://httpbin.org/post', { method: 'POST', body: params })
.then(res => res.json())
.then(json => console.log(json));
```
#### Handling exceptions
NOTE: 3xx-5xx responses are *NOT* exceptions and should be handled in `then()`; see the next section for more information.
Adding a catch to the fetch promise chain will catch *all* exceptions, such as errors originating from node core libraries, network errors and operational errors, which are instances of FetchError. See the [error handling document](ERROR-HANDLING.md) for more details.
```js
fetch('https://domain.invalid/')
.catch(err => console.error(err));
```
#### Handling client and server errors
It is common to create a helper function to check that the response contains no client (4xx) or server (5xx) error responses:
```js
function checkStatus(res) {
if (res.ok) { // res.status >= 200 && res.status < 300
return res;
} else {
throw MyCustomError(res.statusText);
}
}
fetch('https://httpbin.org/status/400')
.then(checkStatus)
.then(res => console.log('will not get here...'))
```
## Advanced Usage
#### Streams
The "Node.js way" is to use streams when possible:
```js
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
.then(res => {
const dest = fs.createWriteStream('./octocat.png');
res.body.pipe(dest);
});
```
In Node.js 14 you can also use async iterators to read `body`; however, be careful to catch
errors -- the longer a response runs, the more likely it is to encounter an error.
```js
const fetch = require('node-fetch');
const response = await fetch('https://httpbin.org/stream/3');
try {
for await (const chunk of response.body) {
console.dir(JSON.parse(chunk.toString()));
}
} catch (err) {
console.error(err.stack);
}
```
In Node.js 12 you can also use async iterators to read `body`; however, async iterators with streams
did not mature until Node.js 14, so you need to do some extra work to ensure you handle errors
directly from the stream and wait on it response to fully close.
```js
const fetch = require('node-fetch');
const read = async body => {
let error;
body.on('error', err => {
error = err;
});
for await (const chunk of body) {
console.dir(JSON.parse(chunk.toString()));
}
return new Promise((resolve, reject) => {
body.on('close', () => {
error ? reject(error) : resolve();
});
});
};
try {
const response = await fetch('https://httpbin.org/stream/3');
await read(response.body);
} catch (err) {
console.error(err.stack);
}
```
#### Buffer
If you prefer to cache binary data in full, use buffer(). (NOTE: `buffer()` is a `node-fetch`-only API)
```js
const fileType = require('file-type');
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
.then(res => res.buffer())
.then(buffer => fileType(buffer))
.then(type => { /* ... */ });
```
#### Accessing Headers and other Meta data
```js
fetch('https://github.com/')
.then(res => {
console.log(res.ok);
console.log(res.status);
console.log(res.statusText);
console.log(res.headers.raw());
console.log(res.headers.get('content-type'));
});
```
#### Extract Set-Cookie Header
Unlike browsers, you can access raw `Set-Cookie` headers manually using `Headers.raw()`. This is a `node-fetch` only API.
```js
fetch(url).then(res => {
// returns an array of values, instead of a string of comma-separated values
console.log(res.headers.raw()['set-cookie']);
});
```
#### Post data using a file stream
```js
const { createReadStream } = require('fs');
const stream = createReadStream('input.txt');
fetch('https://httpbin.org/post', { method: 'POST', body: stream })
.then(res => res.json())
.then(json => console.log(json));
```
#### Post with form-data (detect multipart)
```js
const FormData = require('form-data');
const form = new FormData();
form.append('a', 1);
fetch('https://httpbin.org/post', { method: 'POST', body: form })
.then(res => res.json())
.then(json => console.log(json));
// OR, using custom headers
// NOTE: getHeaders() is non-standard API
const form = new FormData();
form.append('a', 1);
const options = {
method: 'POST',
body: form,
headers: form.getHeaders()
}
fetch('https://httpbin.org/post', options)
.then(res => res.json())
.then(json => console.log(json));
```
#### Request cancellation with AbortSignal
> NOTE: You may cancel streamed requests only on Node >= v8.0.0
You may cancel requests with `AbortController`. A suggested implementation is [`abort-controller`](https://www.npmjs.com/package/abort-controller).
An example of timing out a request after 150ms could be achieved as the following:
```js
import AbortController from 'abort-controller';
const controller = new AbortController();
const timeout = setTimeout(
() => { controller.abort(); },
150,
);
fetch(url, { signal: controller.signal })
.then(res => res.json())
.then(
data => {
useData(data)
},
err => {
if (err.name === 'AbortError') {
// request was aborted
}
},
)
.finally(() => {
clearTimeout(timeout);
});
```
See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.
## API
### fetch(url[, options])
- `url` A string representing the URL for fetching
- `options` [Options](#fetch-options) for the HTTP(S) request
- Returns: <code>Promise&lt;[Response](#class-response)&gt;</code>
Perform an HTTP(S) fetch.
`url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
<a id="fetch-options"></a>
### Options
The default values are shown after each option key.
```js
{
// These properties are part of the Fetch Standard
method: 'GET',
headers: {}, // request headers. format is the identical to that accepted by the Headers constructor (see below)
body: null, // request body. can be null, a string, a Buffer, a Blob, or a Node.js Readable stream
redirect: 'follow', // set to `manual` to extract redirect headers, `error` to reject redirect
signal: null, // pass an instance of AbortSignal to optionally abort requests
// The following properties are node-fetch extensions
follow: 20, // maximum redirect count. 0 to not follow redirect
timeout: 0, // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies). Signal is recommended instead.
compress: true, // support gzip/deflate content encoding. false to disable
size: 0, // maximum response body size in bytes. 0 to disable
agent: null // http(s).Agent instance or function that returns an instance (see below)
}
```
##### Default Headers
If no values are set, the following request headers will be sent automatically:
Header | Value
------------------- | --------------------------------------------------------
`Accept-Encoding` | `gzip,deflate` _(when `options.compress === true`)_
`Accept` | `*/*`
`Content-Length` | _(automatically calculated, if possible)_
`Transfer-Encoding` | `chunked` _(when `req.body` is a stream)_
`User-Agent` | `node-fetch/1.0 (+https://github.com/bitinn/node-fetch)`
Note: when `body` is a `Stream`, `Content-Length` is not set automatically.
##### Custom Agent
The `agent` option allows you to specify networking related options which are out of the scope of Fetch, including and not limited to the following:
- Support self-signed certificate
- Use only IPv4 or IPv6
- Custom DNS Lookup
See [`http.Agent`](https://nodejs.org/api/http.html#http_new_agent_options) for more information.
If no agent is specified, the default agent provided by Node.js is used. Note that [this changed in Node.js 19](https://github.com/nodejs/node/blob/4267b92604ad78584244488e7f7508a690cb80d0/lib/_http_agent.js#L564) to have `keepalive` true by default. If you wish to enable `keepalive` in an earlier version of Node.js, you can override the agent as per the following code sample.
In addition, the `agent` option accepts a function that returns `http`(s)`.Agent` instance given current [URL](https://nodejs.org/api/url.html), this is useful during a redirection chain across HTTP and HTTPS protocol.
```js
const httpAgent = new http.Agent({
keepAlive: true
});
const httpsAgent = new https.Agent({
keepAlive: true
});
const options = {
agent: function (_parsedURL) {
if (_parsedURL.protocol == 'http:') {
return httpAgent;
} else {
return httpsAgent;
}
}
}
```
<a id="class-request"></a>
### Class: Request
An HTTP(S) request containing information about URL, method, headers, and the body. This class implements the [Body](#iface-body) interface.
Due to the nature of Node.js, the following properties are not implemented at this moment:
- `type`
- `destination`
- `referrer`
- `referrerPolicy`
- `mode`
- `credentials`
- `cache`
- `integrity`
- `keepalive`
The following node-fetch extension properties are provided:
- `follow`
- `compress`
- `counter`
- `agent`
See [options](#fetch-options) for exact meaning of these extensions.
#### new Request(input[, options])
<small>*(spec-compliant)*</small>
- `input` A string representing a URL, or another `Request` (which will be cloned)
- `options` [Options][#fetch-options] for the HTTP(S) request
Constructs a new `Request` object. The constructor is identical to that in the [browser](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request).
In most cases, directly `fetch(url, options)` is simpler than creating a `Request` object.
<a id="class-response"></a>
### Class: Response
An HTTP(S) response. This class implements the [Body](#iface-body) interface.
The following properties are not implemented in node-fetch at this moment:
- `Response.error()`
- `Response.redirect()`
- `type`
- `trailer`
#### new Response([body[, options]])
<small>*(spec-compliant)*</small>
- `body` A `String` or [`Readable` stream][node-readable]
- `options` A [`ResponseInit`][response-init] options dictionary
Constructs a new `Response` object. The constructor is identical to that in the [browser](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response).
Because Node.js does not implement service workers (for which this class was designed), one rarely has to construct a `Response` directly.
#### response.ok
<small>*(spec-compliant)*</small>
Convenience property representing if the request ended normally. Will evaluate to true if the response status was greater than or equal to 200 but smaller than 300.
#### response.redirected
<small>*(spec-compliant)*</small>
Convenience property representing if the request has been redirected at least once. Will evaluate to true if the internal redirect counter is greater than 0.
<a id="class-headers"></a>
### Class: Headers
This class allows manipulating and iterating over a set of HTTP headers. All methods specified in the [Fetch Standard][whatwg-fetch] are implemented.
#### new Headers([init])
<small>*(spec-compliant)*</small>
- `init` Optional argument to pre-fill the `Headers` object
Construct a new `Headers` object. `init` can be either `null`, a `Headers` object, an key-value map object or any iterable object.
```js
// Example adapted from https://fetch.spec.whatwg.org/#example-headers-class
const meta = {
'Content-Type': 'text/xml',
'Breaking-Bad': '<3'
};
const headers = new Headers(meta);
// The above is equivalent to
const meta = [
[ 'Content-Type', 'text/xml' ],
[ 'Breaking-Bad', '<3' ]
];
const headers = new Headers(meta);
// You can in fact use any iterable objects, like a Map or even another Headers
const meta = new Map();
meta.set('Content-Type', 'text/xml');
meta.set('Breaking-Bad', '<3');
const headers = new Headers(meta);
const copyOfHeaders = new Headers(headers);
```
<a id="iface-body"></a>
### Interface: Body
`Body` is an abstract interface with methods that are applicable to both `Request` and `Response` classes.
The following methods are not yet implemented in node-fetch at this moment:
- `formData()`
#### body.body
<small>*(deviation from spec)*</small>
* Node.js [`Readable` stream][node-readable]
Data are encapsulated in the `Body` object. Note that while the [Fetch Standard][whatwg-fetch] requires the property to always be a WHATWG `ReadableStream`, in node-fetch it is a Node.js [`Readable` stream][node-readable].
#### body.bodyUsed
<small>*(spec-compliant)*</small>
* `Boolean`
A boolean property for if this body has been consumed. Per the specs, a consumed body cannot be used again.
#### body.arrayBuffer()
#### body.blob()
#### body.json()
#### body.text()
<small>*(spec-compliant)*</small>
* Returns: <code>Promise</code>
Consume the body and return a promise that will resolve to one of these formats.
#### body.buffer()
<small>*(node-fetch extension)*</small>
* Returns: <code>Promise&lt;Buffer&gt;</code>
Consume the body and return a promise that will resolve to a Buffer.
#### body.textConverted()
<small>*(node-fetch extension)*</small>
* Returns: <code>Promise&lt;String&gt;</code>
Identical to `body.text()`, except instead of always converting to UTF-8, encoding sniffing will be performed and text converted to UTF-8 if possible.
(This API requires an optional dependency of the npm package [encoding](https://www.npmjs.com/package/encoding), which you need to install manually. `webpack` users may see [a warning message](https://github.com/bitinn/node-fetch/issues/412#issuecomment-379007792) due to this optional dependency.)
<a id="class-fetcherror"></a>
### Class: FetchError
<small>*(node-fetch extension)*</small>
An operational error in the fetching process. See [ERROR-HANDLING.md][] for more info.
<a id="class-aborterror"></a>
### Class: AbortError
<small>*(node-fetch extension)*</small>
An Error thrown when the request is aborted in response to an `AbortSignal`'s `abort` event. It has a `name` property of `AbortError`. See [ERROR-HANDLING.MD][] for more info.
## Acknowledgement
Thanks to [github/fetch](https://github.com/github/fetch) for providing a solid implementation reference.
`node-fetch` v1 was maintained by [@bitinn](https://github.com/bitinn); v2 was maintained by [@TimothyGu](https://github.com/timothygu), [@bitinn](https://github.com/bitinn) and [@jimmywarting](https://github.com/jimmywarting); v2 readme is written by [@jkantr](https://github.com/jkantr).
## License
MIT
[npm-image]: https://flat.badgen.net/npm/v/node-fetch
[npm-url]: https://www.npmjs.com/package/node-fetch
[travis-image]: https://flat.badgen.net/travis/bitinn/node-fetch
[travis-url]: https://travis-ci.org/bitinn/node-fetch
[codecov-image]: https://flat.badgen.net/codecov/c/github/bitinn/node-fetch/master
[codecov-url]: https://codecov.io/gh/bitinn/node-fetch
[install-size-image]: https://flat.badgen.net/packagephobia/install/node-fetch
[install-size-url]: https://packagephobia.now.sh/result?p=node-fetch
[discord-image]: https://img.shields.io/discord/619915844268326952?color=%237289DA&label=Discord&style=flat-square
[discord-url]: https://discord.gg/Zxbndcm
[opencollective-image]: https://opencollective.com/node-fetch/backers.svg
[opencollective-url]: https://opencollective.com/node-fetch
[whatwg-fetch]: https://fetch.spec.whatwg.org/
[response-init]: https://fetch.spec.whatwg.org/#responseinit
[node-readable]: https://nodejs.org/api/stream.html#stream_readable_streams
[mdn-headers]: https://developer.mozilla.org/en-US/docs/Web/API/Headers
[LIMITS.md]: https://github.com/bitinn/node-fetch/blob/master/LIMITS.md
[ERROR-HANDLING.md]: https://github.com/bitinn/node-fetch/blob/master/ERROR-HANDLING.md
[UPGRADE-GUIDE.md]: https://github.com/bitinn/node-fetch/blob/master/UPGRADE-GUIDE.md

View File

@@ -1,25 +0,0 @@
"use strict";
// ref: https://github.com/tc39/proposal-global
var getGlobal = function () {
// the only reliable means to get the global object is
// `Function('return this')()`
// However, this causes CSP violations in Chrome apps.
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
throw new Error('unable to locate global object');
}
var globalObject = getGlobal();
module.exports = exports = globalObject.fetch;
// Needed for TypeScript and Webpack.
if (globalObject.fetch) {
exports.default = globalObject.fetch.bind(globalObject);
}
exports.Headers = globalObject.Headers;
exports.Request = globalObject.Request;
exports.Response = globalObject.Response;

View File

@@ -1,89 +0,0 @@
{
"name": "node-fetch",
"version": "2.7.0",
"description": "A light-weight module that brings window.fetch to node.js",
"main": "lib/index.js",
"browser": "./browser.js",
"module": "lib/index.mjs",
"files": [
"lib/index.js",
"lib/index.mjs",
"lib/index.es.js",
"browser.js"
],
"engines": {
"node": "4.x || >=6.0.0"
},
"scripts": {
"build": "cross-env BABEL_ENV=rollup rollup -c",
"prepare": "npm run build",
"test": "cross-env BABEL_ENV=test mocha --require babel-register --throw-deprecation test/test.js",
"report": "cross-env BABEL_ENV=coverage nyc --reporter lcov --reporter text mocha -R spec test/test.js",
"coverage": "cross-env BABEL_ENV=coverage nyc --reporter json --reporter text mocha -R spec test/test.js && codecov -f coverage/coverage-final.json"
},
"repository": {
"type": "git",
"url": "https://github.com/bitinn/node-fetch.git"
},
"keywords": [
"fetch",
"http",
"promise"
],
"author": "David Frank",
"license": "MIT",
"bugs": {
"url": "https://github.com/bitinn/node-fetch/issues"
},
"homepage": "https://github.com/bitinn/node-fetch",
"dependencies": {
"whatwg-url": "^5.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
},
"devDependencies": {
"@ungap/url-search-params": "^0.1.2",
"abort-controller": "^1.1.0",
"abortcontroller-polyfill": "^1.3.0",
"babel-core": "^6.26.3",
"babel-plugin-istanbul": "^4.1.6",
"babel-plugin-transform-async-generator-functions": "^6.24.1",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "1.4.0",
"babel-register": "^6.16.3",
"chai": "^3.5.0",
"chai-as-promised": "^7.1.1",
"chai-iterator": "^1.1.1",
"chai-string": "~1.3.0",
"codecov": "3.3.0",
"cross-env": "^5.2.0",
"form-data": "^2.3.3",
"is-builtin-module": "^1.0.0",
"mocha": "^5.0.0",
"nyc": "11.9.0",
"parted": "^0.1.1",
"promise": "^8.0.3",
"resumer": "0.0.0",
"rollup": "^0.63.4",
"rollup-plugin-babel": "^3.0.7",
"string-to-arraybuffer": "^1.0.2",
"teeny-request": "3.7.0"
},
"release": {
"branches": [
"+([0-9]).x",
"main",
"next",
{
"name": "beta",
"prerelease": true
}
]
}
}

View File

@@ -1 +0,0 @@
../../whatwg-url@5.0.0/node_modules/whatwg-url

View File

@@ -1 +0,0 @@
../commander@2.11.0/node_modules/commander

View File

@@ -1 +0,0 @@
../tr46@0.0.3/node_modules/tr46

View File

@@ -1 +0,0 @@
../webidl-conversions@3.0.1/node_modules/webidl-conversions

View File

@@ -1 +0,0 @@
../whatwg-url@5.0.0/node_modules/whatwg-url

View File

@@ -1 +0,0 @@
../ws@7.5.10/node_modules/ws

View File

@@ -1,4 +0,0 @@
scripts/
test/
!lib/mapping_table.json

View File

@@ -1,193 +0,0 @@
"use strict";
var punycode = require("punycode");
var mappingTable = require("./lib/mappingTable.json");
var PROCESSING_OPTIONS = {
TRANSITIONAL: 0,
NONTRANSITIONAL: 1
};
function normalize(str) { // fix bug in v8
return str.split('\u0000').map(function (s) { return s.normalize('NFC'); }).join('\u0000');
}
function findStatus(val) {
var start = 0;
var end = mappingTable.length - 1;
while (start <= end) {
var mid = Math.floor((start + end) / 2);
var target = mappingTable[mid];
if (target[0][0] <= val && target[0][1] >= val) {
return target;
} else if (target[0][0] > val) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return null;
}
var regexAstralSymbols = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
function countSymbols(string) {
return string
// replace every surrogate pair with a BMP symbol
.replace(regexAstralSymbols, '_')
// then get the length
.length;
}
function mapChars(domain_name, useSTD3, processing_option) {
var hasError = false;
var processed = "";
var len = countSymbols(domain_name);
for (var i = 0; i < len; ++i) {
var codePoint = domain_name.codePointAt(i);
var status = findStatus(codePoint);
switch (status[1]) {
case "disallowed":
hasError = true;
processed += String.fromCodePoint(codePoint);
break;
case "ignored":
break;
case "mapped":
processed += String.fromCodePoint.apply(String, status[2]);
break;
case "deviation":
if (processing_option === PROCESSING_OPTIONS.TRANSITIONAL) {
processed += String.fromCodePoint.apply(String, status[2]);
} else {
processed += String.fromCodePoint(codePoint);
}
break;
case "valid":
processed += String.fromCodePoint(codePoint);
break;
case "disallowed_STD3_mapped":
if (useSTD3) {
hasError = true;
processed += String.fromCodePoint(codePoint);
} else {
processed += String.fromCodePoint.apply(String, status[2]);
}
break;
case "disallowed_STD3_valid":
if (useSTD3) {
hasError = true;
}
processed += String.fromCodePoint(codePoint);
break;
}
}
return {
string: processed,
error: hasError
};
}
var combiningMarksRegex = /[\u0300-\u036F\u0483-\u0489\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED\u0711\u0730-\u074A\u07A6-\u07B0\u07EB-\u07F3\u0816-\u0819\u081B-\u0823\u0825-\u0827\u0829-\u082D\u0859-\u085B\u08E4-\u0903\u093A-\u093C\u093E-\u094F\u0951-\u0957\u0962\u0963\u0981-\u0983\u09BC\u09BE-\u09C4\u09C7\u09C8\u09CB-\u09CD\u09D7\u09E2\u09E3\u0A01-\u0A03\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A70\u0A71\u0A75\u0A81-\u0A83\u0ABC\u0ABE-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AE2\u0AE3\u0B01-\u0B03\u0B3C\u0B3E-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B62\u0B63\u0B82\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0C00-\u0C03\u0C3E-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C62\u0C63\u0C81-\u0C83\u0CBC\u0CBE-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CE2\u0CE3\u0D01-\u0D03\u0D3E-\u0D44\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D62\u0D63\u0D82\u0D83\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DF2\u0DF3\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0EB1\u0EB4-\u0EB9\u0EBB\u0EBC\u0EC8-\u0ECD\u0F18\u0F19\u0F35\u0F37\u0F39\u0F3E\u0F3F\u0F71-\u0F84\u0F86\u0F87\u0F8D-\u0F97\u0F99-\u0FBC\u0FC6\u102B-\u103E\u1056-\u1059\u105E-\u1060\u1062-\u1064\u1067-\u106D\u1071-\u1074\u1082-\u108D\u108F\u109A-\u109D\u135D-\u135F\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17B4-\u17D3\u17DD\u180B-\u180D\u18A9\u1920-\u192B\u1930-\u193B\u19B0-\u19C0\u19C8\u19C9\u1A17-\u1A1B\u1A55-\u1A5E\u1A60-\u1A7C\u1A7F\u1AB0-\u1ABE\u1B00-\u1B04\u1B34-\u1B44\u1B6B-\u1B73\u1B80-\u1B82\u1BA1-\u1BAD\u1BE6-\u1BF3\u1C24-\u1C37\u1CD0-\u1CD2\u1CD4-\u1CE8\u1CED\u1CF2-\u1CF4\u1CF8\u1CF9\u1DC0-\u1DF5\u1DFC-\u1DFF\u20D0-\u20F0\u2CEF-\u2CF1\u2D7F\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\uA66F-\uA672\uA674-\uA67D\uA69F\uA6F0\uA6F1\uA802\uA806\uA80B\uA823-\uA827\uA880\uA881\uA8B4-\uA8C4\uA8E0-\uA8F1\uA926-\uA92D\uA947-\uA953\uA980-\uA983\uA9B3-\uA9C0\uA9E5\uAA29-\uAA36\uAA43\uAA4C\uAA4D\uAA7B-\uAA7D\uAAB0\uAAB2-\uAAB4\uAAB7\uAAB8\uAABE\uAABF\uAAC1\uAAEB-\uAAEF\uAAF5\uAAF6\uABE3-\uABEA\uABEC\uABED\uFB1E\uFE00-\uFE0F\uFE20-\uFE2D]|\uD800[\uDDFD\uDEE0\uDF76-\uDF7A]|\uD802[\uDE01-\uDE03\uDE05\uDE06\uDE0C-\uDE0F\uDE38-\uDE3A\uDE3F\uDEE5\uDEE6]|\uD804[\uDC00-\uDC02\uDC38-\uDC46\uDC7F-\uDC82\uDCB0-\uDCBA\uDD00-\uDD02\uDD27-\uDD34\uDD73\uDD80-\uDD82\uDDB3-\uDDC0\uDE2C-\uDE37\uDEDF-\uDEEA\uDF01-\uDF03\uDF3C\uDF3E-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF57\uDF62\uDF63\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDCB0-\uDCC3\uDDAF-\uDDB5\uDDB8-\uDDC0\uDE30-\uDE40\uDEAB-\uDEB7]|\uD81A[\uDEF0-\uDEF4\uDF30-\uDF36]|\uD81B[\uDF51-\uDF7E\uDF8F-\uDF92]|\uD82F[\uDC9D\uDC9E]|\uD834[\uDD65-\uDD69\uDD6D-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD83A[\uDCD0-\uDCD6]|\uDB40[\uDD00-\uDDEF]/;
function validateLabel(label, processing_option) {
if (label.substr(0, 4) === "xn--") {
label = punycode.toUnicode(label);
processing_option = PROCESSING_OPTIONS.NONTRANSITIONAL;
}
var error = false;
if (normalize(label) !== label ||
(label[3] === "-" && label[4] === "-") ||
label[0] === "-" || label[label.length - 1] === "-" ||
label.indexOf(".") !== -1 ||
label.search(combiningMarksRegex) === 0) {
error = true;
}
var len = countSymbols(label);
for (var i = 0; i < len; ++i) {
var status = findStatus(label.codePointAt(i));
if ((processing === PROCESSING_OPTIONS.TRANSITIONAL && status[1] !== "valid") ||
(processing === PROCESSING_OPTIONS.NONTRANSITIONAL &&
status[1] !== "valid" && status[1] !== "deviation")) {
error = true;
break;
}
}
return {
label: label,
error: error
};
}
function processing(domain_name, useSTD3, processing_option) {
var result = mapChars(domain_name, useSTD3, processing_option);
result.string = normalize(result.string);
var labels = result.string.split(".");
for (var i = 0; i < labels.length; ++i) {
try {
var validation = validateLabel(labels[i]);
labels[i] = validation.label;
result.error = result.error || validation.error;
} catch(e) {
result.error = true;
}
}
return {
string: labels.join("."),
error: result.error
};
}
module.exports.toASCII = function(domain_name, useSTD3, processing_option, verifyDnsLength) {
var result = processing(domain_name, useSTD3, processing_option);
var labels = result.string.split(".");
labels = labels.map(function(l) {
try {
return punycode.toASCII(l);
} catch(e) {
result.error = true;
return l;
}
});
if (verifyDnsLength) {
var total = labels.slice(0, labels.length - 1).join(".").length;
if (total.length > 253 || total.length === 0) {
result.error = true;
}
for (var i=0; i < labels.length; ++i) {
if (labels.length > 63 || labels.length === 0) {
result.error = true;
break;
}
}
}
if (result.error) return null;
return labels.join(".");
};
module.exports.toUnicode = function(domain_name, useSTD3) {
var result = processing(domain_name, useSTD3, PROCESSING_OPTIONS.NONTRANSITIONAL);
return {
domain: result.string,
error: result.error
};
};
module.exports.PROCESSING_OPTIONS = PROCESSING_OPTIONS;

File diff suppressed because one or more lines are too long

View File

@@ -1,31 +0,0 @@
{
"name": "tr46",
"version": "0.0.3",
"description": "An implementation of the Unicode TR46 spec",
"main": "index.js",
"scripts": {
"test": "mocha",
"pretest": "node scripts/getLatestUnicodeTests.js",
"prepublish": "node scripts/generateMappingTable.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Sebmaster/tr46.js.git"
},
"keywords": [
"unicode",
"tr46",
"url",
"whatwg"
],
"author": "Sebastian Mayr <npm@smayr.name>",
"license": "MIT",
"bugs": {
"url": "https://github.com/Sebmaster/tr46.js/issues"
},
"homepage": "https://github.com/Sebmaster/tr46.js#readme",
"devDependencies": {
"mocha": "^2.2.5",
"request": "^2.57.0"
}
}

View File

@@ -1,12 +0,0 @@
# The BSD 2-Clause License
Copyright (c) 2014, Domenic Denicola
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@@ -1,53 +0,0 @@
# WebIDL Type Conversions on JavaScript Values
This package implements, in JavaScript, the algorithms to convert a given JavaScript value according to a given [WebIDL](http://heycam.github.io/webidl/) [type](http://heycam.github.io/webidl/#idl-types).
The goal is that you should be able to write code like
```js
const conversions = require("webidl-conversions");
function doStuff(x, y) {
x = conversions["boolean"](x);
y = conversions["unsigned long"](y);
// actual algorithm code here
}
```
and your function `doStuff` will behave the same as a WebIDL operation declared as
```webidl
void doStuff(boolean x, unsigned long y);
```
## API
This package's main module's default export is an object with a variety of methods, each corresponding to a different WebIDL type. Each method, when invoked on a JavaScript value, will give back the new JavaScript value that results after passing through the WebIDL conversion rules. (See below for more details on what that means.) Alternately, the method could throw an error, if the WebIDL algorithm is specified to do so: for example `conversions["float"](NaN)` [will throw a `TypeError`](http://heycam.github.io/webidl/#es-float).
## Status
All of the numeric types are implemented (float being implemented as double) and some others are as well - check the source for all of them. This list will grow over time in service of the [HTML as Custom Elements](https://github.com/dglazkov/html-as-custom-elements) project, but in the meantime, pull requests welcome!
I'm not sure yet what the strategy will be for modifiers, e.g. [`[Clamp]`](http://heycam.github.io/webidl/#Clamp). Maybe something like `conversions["unsigned long"](x, { clamp: true })`? We'll see.
We might also want to extend the API to give better error messages, e.g. "Argument 1 of HTMLMediaElement.fastSeek is not a finite floating-point value" instead of "Argument is not a finite floating-point value." This would require passing in more information to the conversion functions than we currently do.
## Background
What's actually going on here, conceptually, is pretty weird. Let's try to explain.
WebIDL, as part of its madness-inducing design, has its own type system. When people write algorithms in web platform specs, they usually operate on WebIDL values, i.e. instances of WebIDL types. For example, if they were specifying the algorithm for our `doStuff` operation above, they would treat `x` as a WebIDL value of [WebIDL type `boolean`](http://heycam.github.io/webidl/#idl-boolean). Crucially, they would _not_ treat `x` as a JavaScript variable whose value is either the JavaScript `true` or `false`. They're instead working in a different type system altogether, with its own rules.
Separately from its type system, WebIDL defines a ["binding"](http://heycam.github.io/webidl/#ecmascript-binding) of the type system into JavaScript. This contains rules like: when you pass a JavaScript value to the JavaScript method that manifests a given WebIDL operation, how does that get converted into a WebIDL value? For example, a JavaScript `true` passed in the position of a WebIDL `boolean` argument becomes a WebIDL `true`. But, a JavaScript `true` passed in the position of a [WebIDL `unsigned long`](http://heycam.github.io/webidl/#idl-unsigned-long) becomes a WebIDL `1`. And so on.
Finally, we have the actual implementation code. This is usually C++, although these days [some smart people are using Rust](https://github.com/servo/servo). The implementation, of course, has its own type system. So when they implement the WebIDL algorithms, they don't actually use WebIDL values, since those aren't "real" outside of specs. Instead, implementations apply the WebIDL binding rules in such a way as to convert incoming JavaScript values into C++ values. For example, if code in the browser called `doStuff(true, true)`, then the implementation code would eventually receive a C++ `bool` containing `true` and a C++ `uint32_t` containing `1`.
The upside of all this is that implementations can abstract all the conversion logic away, letting WebIDL handle it, and focus on implementing the relevant methods in C++ with values of the correct type already provided. That is payoff of WebIDL, in a nutshell.
And getting to that payoff is the goal of _this_ project—but for JavaScript implementations, instead of C++ ones. That is, this library is designed to make it easier for JavaScript developers to write functions that behave like a given WebIDL operation. So conceptually, the conversion pipeline, which in its general form is JavaScript values ↦ WebIDL values ↦ implementation-language values, in this case becomes JavaScript values ↦ WebIDL values ↦ JavaScript values. And that intermediate step is where all the logic is performed: a JavaScript `true` becomes a WebIDL `1` in an unsigned long context, which then becomes a JavaScript `1`.
## Don't Use This
Seriously, why would you ever use this? You really shouldn't. WebIDL is … not great, and you shouldn't be emulating its semantics. If you're looking for a generic argument-processing library, you should find one with better rules than those from WebIDL. In general, your JavaScript should not be trying to become more like WebIDL; if anything, we should fix WebIDL to make it more like JavaScript.
The _only_ people who should use this are those trying to create faithful implementations (or polyfills) of web platform interfaces defined in WebIDL.

View File

@@ -1,189 +0,0 @@
"use strict";
var conversions = {};
module.exports = conversions;
function sign(x) {
return x < 0 ? -1 : 1;
}
function evenRound(x) {
// Round x to the nearest integer, choosing the even integer if it lies halfway between two.
if ((x % 1) === 0.5 && (x & 1) === 0) { // [even number].5; round down (i.e. floor)
return Math.floor(x);
} else {
return Math.round(x);
}
}
function createNumberConversion(bitLength, typeOpts) {
if (!typeOpts.unsigned) {
--bitLength;
}
const lowerBound = typeOpts.unsigned ? 0 : -Math.pow(2, bitLength);
const upperBound = Math.pow(2, bitLength) - 1;
const moduloVal = typeOpts.moduloBitLength ? Math.pow(2, typeOpts.moduloBitLength) : Math.pow(2, bitLength);
const moduloBound = typeOpts.moduloBitLength ? Math.pow(2, typeOpts.moduloBitLength - 1) : Math.pow(2, bitLength - 1);
return function(V, opts) {
if (!opts) opts = {};
let x = +V;
if (opts.enforceRange) {
if (!Number.isFinite(x)) {
throw new TypeError("Argument is not a finite number");
}
x = sign(x) * Math.floor(Math.abs(x));
if (x < lowerBound || x > upperBound) {
throw new TypeError("Argument is not in byte range");
}
return x;
}
if (!isNaN(x) && opts.clamp) {
x = evenRound(x);
if (x < lowerBound) x = lowerBound;
if (x > upperBound) x = upperBound;
return x;
}
if (!Number.isFinite(x) || x === 0) {
return 0;
}
x = sign(x) * Math.floor(Math.abs(x));
x = x % moduloVal;
if (!typeOpts.unsigned && x >= moduloBound) {
return x - moduloVal;
} else if (typeOpts.unsigned) {
if (x < 0) {
x += moduloVal;
} else if (x === -0) { // don't return negative zero
return 0;
}
}
return x;
}
}
conversions["void"] = function () {
return undefined;
};
conversions["boolean"] = function (val) {
return !!val;
};
conversions["byte"] = createNumberConversion(8, { unsigned: false });
conversions["octet"] = createNumberConversion(8, { unsigned: true });
conversions["short"] = createNumberConversion(16, { unsigned: false });
conversions["unsigned short"] = createNumberConversion(16, { unsigned: true });
conversions["long"] = createNumberConversion(32, { unsigned: false });
conversions["unsigned long"] = createNumberConversion(32, { unsigned: true });
conversions["long long"] = createNumberConversion(32, { unsigned: false, moduloBitLength: 64 });
conversions["unsigned long long"] = createNumberConversion(32, { unsigned: true, moduloBitLength: 64 });
conversions["double"] = function (V) {
const x = +V;
if (!Number.isFinite(x)) {
throw new TypeError("Argument is not a finite floating-point value");
}
return x;
};
conversions["unrestricted double"] = function (V) {
const x = +V;
if (isNaN(x)) {
throw new TypeError("Argument is NaN");
}
return x;
};
// not quite valid, but good enough for JS
conversions["float"] = conversions["double"];
conversions["unrestricted float"] = conversions["unrestricted double"];
conversions["DOMString"] = function (V, opts) {
if (!opts) opts = {};
if (opts.treatNullAsEmptyString && V === null) {
return "";
}
return String(V);
};
conversions["ByteString"] = function (V, opts) {
const x = String(V);
let c = undefined;
for (let i = 0; (c = x.codePointAt(i)) !== undefined; ++i) {
if (c > 255) {
throw new TypeError("Argument is not a valid bytestring");
}
}
return x;
};
conversions["USVString"] = function (V) {
const S = String(V);
const n = S.length;
const U = [];
for (let i = 0; i < n; ++i) {
const c = S.charCodeAt(i);
if (c < 0xD800 || c > 0xDFFF) {
U.push(String.fromCodePoint(c));
} else if (0xDC00 <= c && c <= 0xDFFF) {
U.push(String.fromCodePoint(0xFFFD));
} else {
if (i === n - 1) {
U.push(String.fromCodePoint(0xFFFD));
} else {
const d = S.charCodeAt(i + 1);
if (0xDC00 <= d && d <= 0xDFFF) {
const a = c & 0x3FF;
const b = d & 0x3FF;
U.push(String.fromCodePoint((2 << 15) + (2 << 9) * a + b));
++i;
} else {
U.push(String.fromCodePoint(0xFFFD));
}
}
}
}
return U.join('');
};
conversions["Date"] = function (V, opts) {
if (!(V instanceof Date)) {
throw new TypeError("Argument is not a Date object");
}
if (isNaN(V)) {
return undefined;
}
return V;
};
conversions["RegExp"] = function (V, opts) {
if (!(V instanceof RegExp)) {
V = new RegExp(V);
}
return V;
};

View File

@@ -1,23 +0,0 @@
{
"name": "webidl-conversions",
"version": "3.0.1",
"description": "Implements the WebIDL algorithms for converting to and from JavaScript values",
"main": "lib/index.js",
"scripts": {
"test": "mocha test/*.js"
},
"repository": "jsdom/webidl-conversions",
"keywords": [
"webidl",
"web",
"types"
],
"files": [
"lib/"
],
"author": "Domenic Denicola <d@domenic.me> (https://domenic.me/)",
"license": "BSD-2-Clause",
"devDependencies": {
"mocha": "^1.21.4"
}
}

View File

@@ -1 +0,0 @@
../../tr46@0.0.3/node_modules/tr46

View File

@@ -1 +0,0 @@
../../webidl-conversions@3.0.1/node_modules/webidl-conversions

View File

@@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 20152016 Sebastian Mayr
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -1,67 +0,0 @@
# whatwg-url
whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spec.whatwg.org/). It can be used standalone, but it also exposes a lot of the internal algorithms that are useful for integrating a URL parser into a project like [jsdom](https://github.com/tmpvar/jsdom).
## Current Status
whatwg-url is currently up to date with the URL spec up to commit [a62223](https://github.com/whatwg/url/commit/a622235308342c9adc7fc2fd1659ff059f7d5e2a).
## API
### The `URL` Constructor
The main API is the [`URL`](https://url.spec.whatwg.org/#url) export, which follows the spec's behavior in all ways (including e.g. `USVString` conversion). Most consumers of this library will want to use this.
### Low-level URL Standard API
The following methods are exported for use by places like jsdom that need to implement things like [`HTMLHyperlinkElementUtils`](https://html.spec.whatwg.org/#htmlhyperlinkelementutils). They operate on or return an "internal URL" or ["URL record"](https://url.spec.whatwg.org/#concept-url) type.
- [URL parser](https://url.spec.whatwg.org/#concept-url-parser): `parseURL(input, { baseURL, encodingOverride })`
- [Basic URL parser](https://url.spec.whatwg.org/#concept-basic-url-parser): `basicURLParse(input, { baseURL, encodingOverride, url, stateOverride })`
- [URL serializer](https://url.spec.whatwg.org/#concept-url-serializer): `serializeURL(urlRecord, excludeFragment)`
- [Host serializer](https://url.spec.whatwg.org/#concept-host-serializer): `serializeHost(hostFromURLRecord)`
- [Serialize an integer](https://url.spec.whatwg.org/#serialize-an-integer): `serializeInteger(number)`
- [Origin](https://url.spec.whatwg.org/#concept-url-origin) [serializer](https://html.spec.whatwg.org/multipage/browsers.html#serialization-of-an-origin): `serializeURLOrigin(urlRecord)`
- [Set the username](https://url.spec.whatwg.org/#set-the-username): `setTheUsername(urlRecord, usernameString)`
- [Set the password](https://url.spec.whatwg.org/#set-the-password): `setThePassword(urlRecord, passwordString)`
- [Cannot have a username/password/port](https://url.spec.whatwg.org/#cannot-have-a-username-password-port): `cannotHaveAUsernamePasswordPort(urlRecord)`
The `stateOverride` parameter is one of the following strings:
- [`"scheme start"`](https://url.spec.whatwg.org/#scheme-start-state)
- [`"scheme"`](https://url.spec.whatwg.org/#scheme-state)
- [`"no scheme"`](https://url.spec.whatwg.org/#no-scheme-state)
- [`"special relative or authority"`](https://url.spec.whatwg.org/#special-relative-or-authority-state)
- [`"path or authority"`](https://url.spec.whatwg.org/#path-or-authority-state)
- [`"relative"`](https://url.spec.whatwg.org/#relative-state)
- [`"relative slash"`](https://url.spec.whatwg.org/#relative-slash-state)
- [`"special authority slashes"`](https://url.spec.whatwg.org/#special-authority-slashes-state)
- [`"special authority ignore slashes"`](https://url.spec.whatwg.org/#special-authority-ignore-slashes-state)
- [`"authority"`](https://url.spec.whatwg.org/#authority-state)
- [`"host"`](https://url.spec.whatwg.org/#host-state)
- [`"hostname"`](https://url.spec.whatwg.org/#hostname-state)
- [`"port"`](https://url.spec.whatwg.org/#port-state)
- [`"file"`](https://url.spec.whatwg.org/#file-state)
- [`"file slash"`](https://url.spec.whatwg.org/#file-slash-state)
- [`"file host"`](https://url.spec.whatwg.org/#file-host-state)
- [`"path start"`](https://url.spec.whatwg.org/#path-start-state)
- [`"path"`](https://url.spec.whatwg.org/#path-state)
- [`"cannot-be-a-base-URL path"`](https://url.spec.whatwg.org/#cannot-be-a-base-url-path-state)
- [`"query"`](https://url.spec.whatwg.org/#query-state)
- [`"fragment"`](https://url.spec.whatwg.org/#fragment-state)
The URL record type has the following API:
- [`scheme`](https://url.spec.whatwg.org/#concept-url-scheme)
- [`username`](https://url.spec.whatwg.org/#concept-url-username)
- [`password`](https://url.spec.whatwg.org/#concept-url-password)
- [`host`](https://url.spec.whatwg.org/#concept-url-host)
- [`port`](https://url.spec.whatwg.org/#concept-url-port)
- [`path`](https://url.spec.whatwg.org/#concept-url-path) (as an array)
- [`query`](https://url.spec.whatwg.org/#concept-url-query)
- [`fragment`](https://url.spec.whatwg.org/#concept-url-fragment)
- [`cannotBeABaseURL`](https://url.spec.whatwg.org/#url-cannot-be-a-base-url-flag) (as a boolean)
These properties should be treated with care, as in general changing them will cause the URL record to be in an inconsistent state until the appropriate invocation of `basicURLParse` is used to fix it up. You can see examples of this in the URL Standard, where there are many step sequences like "4. Set context objects urls fragment to the empty string. 5. Basic URL parse _input_ with context objects url as _url_ and fragment state as _state override_." In between those two steps, a URL record is in an unusable state.
The return value of "failure" in the spec is represented by the string `"failure"`. That is, functions like `parseURL` and `basicURLParse` can return _either_ a URL record _or_ the string `"failure"`.

View File

@@ -1,200 +0,0 @@
"use strict";
const usm = require("./url-state-machine");
exports.implementation = class URLImpl {
constructor(constructorArgs) {
const url = constructorArgs[0];
const base = constructorArgs[1];
let parsedBase = null;
if (base !== undefined) {
parsedBase = usm.basicURLParse(base);
if (parsedBase === "failure") {
throw new TypeError("Invalid base URL");
}
}
const parsedURL = usm.basicURLParse(url, { baseURL: parsedBase });
if (parsedURL === "failure") {
throw new TypeError("Invalid URL");
}
this._url = parsedURL;
// TODO: query stuff
}
get href() {
return usm.serializeURL(this._url);
}
set href(v) {
const parsedURL = usm.basicURLParse(v);
if (parsedURL === "failure") {
throw new TypeError("Invalid URL");
}
this._url = parsedURL;
}
get origin() {
return usm.serializeURLOrigin(this._url);
}
get protocol() {
return this._url.scheme + ":";
}
set protocol(v) {
usm.basicURLParse(v + ":", { url: this._url, stateOverride: "scheme start" });
}
get username() {
return this._url.username;
}
set username(v) {
if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
return;
}
usm.setTheUsername(this._url, v);
}
get password() {
return this._url.password;
}
set password(v) {
if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
return;
}
usm.setThePassword(this._url, v);
}
get host() {
const url = this._url;
if (url.host === null) {
return "";
}
if (url.port === null) {
return usm.serializeHost(url.host);
}
return usm.serializeHost(url.host) + ":" + usm.serializeInteger(url.port);
}
set host(v) {
if (this._url.cannotBeABaseURL) {
return;
}
usm.basicURLParse(v, { url: this._url, stateOverride: "host" });
}
get hostname() {
if (this._url.host === null) {
return "";
}
return usm.serializeHost(this._url.host);
}
set hostname(v) {
if (this._url.cannotBeABaseURL) {
return;
}
usm.basicURLParse(v, { url: this._url, stateOverride: "hostname" });
}
get port() {
if (this._url.port === null) {
return "";
}
return usm.serializeInteger(this._url.port);
}
set port(v) {
if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
return;
}
if (v === "") {
this._url.port = null;
} else {
usm.basicURLParse(v, { url: this._url, stateOverride: "port" });
}
}
get pathname() {
if (this._url.cannotBeABaseURL) {
return this._url.path[0];
}
if (this._url.path.length === 0) {
return "";
}
return "/" + this._url.path.join("/");
}
set pathname(v) {
if (this._url.cannotBeABaseURL) {
return;
}
this._url.path = [];
usm.basicURLParse(v, { url: this._url, stateOverride: "path start" });
}
get search() {
if (this._url.query === null || this._url.query === "") {
return "";
}
return "?" + this._url.query;
}
set search(v) {
// TODO: query stuff
const url = this._url;
if (v === "") {
url.query = null;
return;
}
const input = v[0] === "?" ? v.substring(1) : v;
url.query = "";
usm.basicURLParse(input, { url, stateOverride: "query" });
}
get hash() {
if (this._url.fragment === null || this._url.fragment === "") {
return "";
}
return "#" + this._url.fragment;
}
set hash(v) {
if (v === "") {
this._url.fragment = null;
return;
}
const input = v[0] === "#" ? v.substring(1) : v;
this._url.fragment = "";
usm.basicURLParse(input, { url: this._url, stateOverride: "fragment" });
}
toJSON() {
return this.href;
}
};

View File

@@ -1,196 +0,0 @@
"use strict";
const conversions = require("webidl-conversions");
const utils = require("./utils.js");
const Impl = require(".//URL-impl.js");
const impl = utils.implSymbol;
function URL(url) {
if (!this || this[impl] || !(this instanceof URL)) {
throw new TypeError("Failed to construct 'URL': Please use the 'new' operator, this DOM object constructor cannot be called as a function.");
}
if (arguments.length < 1) {
throw new TypeError("Failed to construct 'URL': 1 argument required, but only " + arguments.length + " present.");
}
const args = [];
for (let i = 0; i < arguments.length && i < 2; ++i) {
args[i] = arguments[i];
}
args[0] = conversions["USVString"](args[0]);
if (args[1] !== undefined) {
args[1] = conversions["USVString"](args[1]);
}
module.exports.setup(this, args);
}
URL.prototype.toJSON = function toJSON() {
if (!this || !module.exports.is(this)) {
throw new TypeError("Illegal invocation");
}
const args = [];
for (let i = 0; i < arguments.length && i < 0; ++i) {
args[i] = arguments[i];
}
return this[impl].toJSON.apply(this[impl], args);
};
Object.defineProperty(URL.prototype, "href", {
get() {
return this[impl].href;
},
set(V) {
V = conversions["USVString"](V);
this[impl].href = V;
},
enumerable: true,
configurable: true
});
URL.prototype.toString = function () {
if (!this || !module.exports.is(this)) {
throw new TypeError("Illegal invocation");
}
return this.href;
};
Object.defineProperty(URL.prototype, "origin", {
get() {
return this[impl].origin;
},
enumerable: true,
configurable: true
});
Object.defineProperty(URL.prototype, "protocol", {
get() {
return this[impl].protocol;
},
set(V) {
V = conversions["USVString"](V);
this[impl].protocol = V;
},
enumerable: true,
configurable: true
});
Object.defineProperty(URL.prototype, "username", {
get() {
return this[impl].username;
},
set(V) {
V = conversions["USVString"](V);
this[impl].username = V;
},
enumerable: true,
configurable: true
});
Object.defineProperty(URL.prototype, "password", {
get() {
return this[impl].password;
},
set(V) {
V = conversions["USVString"](V);
this[impl].password = V;
},
enumerable: true,
configurable: true
});
Object.defineProperty(URL.prototype, "host", {
get() {
return this[impl].host;
},
set(V) {
V = conversions["USVString"](V);
this[impl].host = V;
},
enumerable: true,
configurable: true
});
Object.defineProperty(URL.prototype, "hostname", {
get() {
return this[impl].hostname;
},
set(V) {
V = conversions["USVString"](V);
this[impl].hostname = V;
},
enumerable: true,
configurable: true
});
Object.defineProperty(URL.prototype, "port", {
get() {
return this[impl].port;
},
set(V) {
V = conversions["USVString"](V);
this[impl].port = V;
},
enumerable: true,
configurable: true
});
Object.defineProperty(URL.prototype, "pathname", {
get() {
return this[impl].pathname;
},
set(V) {
V = conversions["USVString"](V);
this[impl].pathname = V;
},
enumerable: true,
configurable: true
});
Object.defineProperty(URL.prototype, "search", {
get() {
return this[impl].search;
},
set(V) {
V = conversions["USVString"](V);
this[impl].search = V;
},
enumerable: true,
configurable: true
});
Object.defineProperty(URL.prototype, "hash", {
get() {
return this[impl].hash;
},
set(V) {
V = conversions["USVString"](V);
this[impl].hash = V;
},
enumerable: true,
configurable: true
});
module.exports = {
is(obj) {
return !!obj && obj[impl] instanceof Impl.implementation;
},
create(constructorArgs, privateData) {
let obj = Object.create(URL.prototype);
this.setup(obj, constructorArgs, privateData);
return obj;
},
setup(obj, constructorArgs, privateData) {
if (!privateData) privateData = {};
privateData.wrapper = obj;
obj[impl] = new Impl.implementation(constructorArgs, privateData);
obj[impl][utils.wrapperSymbol] = obj;
},
interface: URL,
expose: {
Window: { URL: URL },
Worker: { URL: URL }
}
};

View File

@@ -1,11 +0,0 @@
"use strict";
exports.URL = require("./URL").interface;
exports.serializeURL = require("./url-state-machine").serializeURL;
exports.serializeURLOrigin = require("./url-state-machine").serializeURLOrigin;
exports.basicURLParse = require("./url-state-machine").basicURLParse;
exports.setTheUsername = require("./url-state-machine").setTheUsername;
exports.setThePassword = require("./url-state-machine").setThePassword;
exports.serializeHost = require("./url-state-machine").serializeHost;
exports.serializeInteger = require("./url-state-machine").serializeInteger;
exports.parseURL = require("./url-state-machine").parseURL;

View File

@@ -1,20 +0,0 @@
"use strict";
module.exports.mixin = function mixin(target, source) {
const keys = Object.getOwnPropertyNames(source);
for (let i = 0; i < keys.length; ++i) {
Object.defineProperty(target, keys[i], Object.getOwnPropertyDescriptor(source, keys[i]));
}
};
module.exports.wrapperSymbol = Symbol("wrapper");
module.exports.implSymbol = Symbol("impl");
module.exports.wrapperForImpl = function (impl) {
return impl[module.exports.wrapperSymbol];
};
module.exports.implForWrapper = function (wrapper) {
return wrapper[module.exports.implSymbol];
};

View File

@@ -1,32 +0,0 @@
{
"name": "whatwg-url",
"version": "5.0.0",
"description": "An implementation of the WHATWG URL Standard's URL API and parsing machinery",
"main": "lib/public-api.js",
"files": [
"lib/"
],
"author": "Sebastian Mayr <github@smayr.name>",
"license": "MIT",
"repository": "jsdom/whatwg-url",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
},
"devDependencies": {
"eslint": "^2.6.0",
"istanbul": "~0.4.3",
"mocha": "^2.2.4",
"recast": "~0.10.29",
"request": "^2.55.0",
"webidl2js": "^3.0.2"
},
"scripts": {
"build": "node scripts/transform.js && node scripts/convert-idl.js",
"coverage": "istanbul cover node_modules/mocha/bin/_mocha",
"lint": "eslint .",
"prepublish": "npm run build",
"pretest": "node scripts/get-latest-platform-tests.js && npm run build",
"test": "mocha"
}
}

View File

@@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2011 Einar Otto Stangvik <einaros@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -1,495 +0,0 @@
# ws: a Node.js WebSocket library
[![Version npm](https://img.shields.io/npm/v/ws.svg?logo=npm)](https://www.npmjs.com/package/ws)
[![CI](https://img.shields.io/github/workflow/status/websockets/ws/CI/master?label=CI&logo=github)](https://github.com/websockets/ws/actions?query=workflow%3ACI+branch%3Amaster)
[![Coverage Status](https://img.shields.io/coveralls/websockets/ws/master.svg?logo=coveralls)](https://coveralls.io/github/websockets/ws)
ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and
server implementation.
Passes the quite extensive Autobahn test suite: [server][server-report],
[client][client-report].
**Note**: This module does not work in the browser. The client in the docs is a
reference to a back end with the role of a client in the WebSocket
communication. Browser clients must use the native
[`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)
object. To make the same code work seamlessly on Node.js and the browser, you
can use one of the many wrappers available on npm, like
[isomorphic-ws](https://github.com/heineiuo/isomorphic-ws).
## Table of Contents
- [Protocol support](#protocol-support)
- [Installing](#installing)
- [Opt-in for performance](#opt-in-for-performance)
- [API docs](#api-docs)
- [WebSocket compression](#websocket-compression)
- [Usage examples](#usage-examples)
- [Sending and receiving text data](#sending-and-receiving-text-data)
- [Sending binary data](#sending-binary-data)
- [Simple server](#simple-server)
- [External HTTP/S server](#external-https-server)
- [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server)
- [Client authentication](#client-authentication)
- [Server broadcast](#server-broadcast)
- [echo.websocket.org demo](#echowebsocketorg-demo)
- [Use the Node.js streams API](#use-the-nodejs-streams-api)
- [Other examples](#other-examples)
- [FAQ](#faq)
- [How to get the IP address of the client?](#how-to-get-the-ip-address-of-the-client)
- [How to detect and close broken connections?](#how-to-detect-and-close-broken-connections)
- [How to connect via a proxy?](#how-to-connect-via-a-proxy)
- [Changelog](#changelog)
- [License](#license)
## Protocol support
- **HyBi drafts 07-12** (Use the option `protocolVersion: 8`)
- **HyBi drafts 13-17** (Current default, alternatively option
`protocolVersion: 13`)
## Installing
```
npm install ws
```
### Opt-in for performance
There are 2 optional modules that can be installed along side with the ws
module. These modules are binary addons which improve certain operations.
Prebuilt binaries are available for the most popular platforms so you don't
necessarily need to have a C++ compiler installed on your machine.
- `npm install --save-optional bufferutil`: Allows to efficiently perform
operations such as masking and unmasking the data payload of the WebSocket
frames.
- `npm install --save-optional utf-8-validate`: Allows to efficiently check if a
message contains valid UTF-8.
## API docs
See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and
utility functions.
## WebSocket compression
ws supports the [permessage-deflate extension][permessage-deflate] which enables
the client and server to negotiate a compression algorithm and its parameters,
and then selectively apply it to the data payloads of each WebSocket message.
The extension is disabled by default on the server and enabled by default on the
client. It adds a significant overhead in terms of performance and memory
consumption so we suggest to enable it only if it is really needed.
Note that Node.js has a variety of issues with high-performance compression,
where increased concurrency, especially on Linux, can lead to [catastrophic
memory fragmentation][node-zlib-bug] and slow performance. If you intend to use
permessage-deflate in production, it is worthwhile to set up a test
representative of your workload and ensure Node.js/zlib will handle it with
acceptable performance and memory usage.
Tuning of permessage-deflate can be done via the options defined below. You can
also use `zlibDeflateOptions` and `zlibInflateOptions`, which is passed directly
into the creation of [raw deflate/inflate streams][node-zlib-deflaterawdocs].
See [the docs][ws-server-options] for more options.
```js
const WebSocket = require('ws');
const wss = new WebSocket.Server({
port: 8080,
perMessageDeflate: {
zlibDeflateOptions: {
// See zlib defaults.
chunkSize: 1024,
memLevel: 7,
level: 3
},
zlibInflateOptions: {
chunkSize: 10 * 1024
},
// Other options settable:
clientNoContextTakeover: true, // Defaults to negotiated value.
serverNoContextTakeover: true, // Defaults to negotiated value.
serverMaxWindowBits: 10, // Defaults to negotiated value.
// Below options specified as default values.
concurrencyLimit: 10, // Limits zlib concurrency for perf.
threshold: 1024 // Size (in bytes) below which messages
// should not be compressed.
}
});
```
The client will only use the extension if it is supported and enabled on the
server. To always disable the extension on the client set the
`perMessageDeflate` option to `false`.
```js
const WebSocket = require('ws');
const ws = new WebSocket('ws://www.host.com/path', {
perMessageDeflate: false
});
```
## Usage examples
### Sending and receiving text data
```js
const WebSocket = require('ws');
const ws = new WebSocket('ws://www.host.com/path');
ws.on('open', function open() {
ws.send('something');
});
ws.on('message', function incoming(data) {
console.log(data);
});
```
### Sending binary data
```js
const WebSocket = require('ws');
const ws = new WebSocket('ws://www.host.com/path');
ws.on('open', function open() {
const array = new Float32Array(5);
for (var i = 0; i < array.length; ++i) {
array[i] = i / 2;
}
ws.send(array);
});
```
### Simple server
```js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
```
### External HTTP/S server
```js
const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');
const server = https.createServer({
cert: fs.readFileSync('/path/to/cert.pem'),
key: fs.readFileSync('/path/to/key.pem')
});
const wss = new WebSocket.Server({ server });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
server.listen(8080);
```
### Multiple servers sharing a single HTTP/S server
```js
const http = require('http');
const WebSocket = require('ws');
const url = require('url');
const server = http.createServer();
const wss1 = new WebSocket.Server({ noServer: true });
const wss2 = new WebSocket.Server({ noServer: true });
wss1.on('connection', function connection(ws) {
// ...
});
wss2.on('connection', function connection(ws) {
// ...
});
server.on('upgrade', function upgrade(request, socket, head) {
const pathname = url.parse(request.url).pathname;
if (pathname === '/foo') {
wss1.handleUpgrade(request, socket, head, function done(ws) {
wss1.emit('connection', ws, request);
});
} else if (pathname === '/bar') {
wss2.handleUpgrade(request, socket, head, function done(ws) {
wss2.emit('connection', ws, request);
});
} else {
socket.destroy();
}
});
server.listen(8080);
```
### Client authentication
```js
const http = require('http');
const WebSocket = require('ws');
const server = http.createServer();
const wss = new WebSocket.Server({ noServer: true });
wss.on('connection', function connection(ws, request, client) {
ws.on('message', function message(msg) {
console.log(`Received message ${msg} from user ${client}`);
});
});
server.on('upgrade', function upgrade(request, socket, head) {
// This function is not defined on purpose. Implement it with your own logic.
authenticate(request, (err, client) => {
if (err || !client) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit('connection', ws, request, client);
});
});
});
server.listen(8080);
```
Also see the provided [example][session-parse-example] using `express-session`.
### Server broadcast
A client WebSocket broadcasting to all connected WebSocket clients, including
itself.
```js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(data) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
});
});
```
A client WebSocket broadcasting to every other connected WebSocket clients,
excluding itself.
```js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(data) {
wss.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
});
});
```
### echo.websocket.org demo
```js
const WebSocket = require('ws');
const ws = new WebSocket('wss://echo.websocket.org/', {
origin: 'https://websocket.org'
});
ws.on('open', function open() {
console.log('connected');
ws.send(Date.now());
});
ws.on('close', function close() {
console.log('disconnected');
});
ws.on('message', function incoming(data) {
console.log(`Roundtrip time: ${Date.now() - data} ms`);
setTimeout(function timeout() {
ws.send(Date.now());
}, 500);
});
```
### Use the Node.js streams API
```js
const WebSocket = require('ws');
const ws = new WebSocket('wss://echo.websocket.org/', {
origin: 'https://websocket.org'
});
const duplex = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' });
duplex.pipe(process.stdout);
process.stdin.pipe(duplex);
```
### Other examples
For a full example with a browser client communicating with a ws server, see the
examples folder.
Otherwise, see the test cases.
## FAQ
### How to get the IP address of the client?
The remote IP address can be obtained from the raw socket.
```js
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws, req) {
const ip = req.socket.remoteAddress;
});
```
When the server runs behind a proxy like NGINX, the de-facto standard is to use
the `X-Forwarded-For` header.
```js
wss.on('connection', function connection(ws, req) {
const ip = req.headers['x-forwarded-for'].split(',')[0].trim();
});
```
### How to detect and close broken connections?
Sometimes the link between the server and the client can be interrupted in a way
that keeps both the server and the client unaware of the broken state of the
connection (e.g. when pulling the cord).
In these cases ping messages can be used as a means to verify that the remote
endpoint is still responsive.
```js
const WebSocket = require('ws');
function noop() {}
function heartbeat() {
this.isAlive = true;
}
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.isAlive = true;
ws.on('pong', heartbeat);
});
const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping(noop);
});
}, 30000);
wss.on('close', function close() {
clearInterval(interval);
});
```
Pong messages are automatically sent in response to ping messages as required by
the spec.
Just like the server example above your clients might as well lose connection
without knowing it. You might want to add a ping listener on your clients to
prevent that. A simple implementation would be:
```js
const WebSocket = require('ws');
function heartbeat() {
clearTimeout(this.pingTimeout);
// Use `WebSocket#terminate()`, which immediately destroys the connection,
// instead of `WebSocket#close()`, which waits for the close timer.
// Delay should be equal to the interval at which your server
// sends out pings plus a conservative assumption of the latency.
this.pingTimeout = setTimeout(() => {
this.terminate();
}, 30000 + 1000);
}
const client = new WebSocket('wss://echo.websocket.org/');
client.on('open', heartbeat);
client.on('ping', heartbeat);
client.on('close', function clear() {
clearTimeout(this.pingTimeout);
});
```
### How to connect via a proxy?
Use a custom `http.Agent` implementation like [https-proxy-agent][] or
[socks-proxy-agent][].
## Changelog
We're using the GitHub [releases][changelog] for changelog entries.
## License
[MIT](LICENSE)
[changelog]: https://github.com/websockets/ws/releases
[client-report]: http://websockets.github.io/ws/autobahn/clients/
[https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
[node-zlib-bug]: https://github.com/nodejs/node/issues/8871
[node-zlib-deflaterawdocs]:
https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options
[permessage-deflate]: https://tools.ietf.org/html/rfc7692
[server-report]: http://websockets.github.io/ws/autobahn/servers/
[session-parse-example]: ./examples/express-session-parse
[socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
[ws-server-options]:
https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback

View File

@@ -1,8 +0,0 @@
'use strict';
module.exports = function () {
throw new Error(
'ws does not work in the browser. Browser clients must use the native ' +
'WebSocket object'
);
};

View File

@@ -1,10 +0,0 @@
'use strict';
const WebSocket = require('./lib/websocket');
WebSocket.createWebSocketStream = require('./lib/stream');
WebSocket.Server = require('./lib/websocket-server');
WebSocket.Receiver = require('./lib/receiver');
WebSocket.Sender = require('./lib/sender');
module.exports = WebSocket;

View File

@@ -1,129 +0,0 @@
'use strict';
const { EMPTY_BUFFER } = require('./constants');
/**
* Merges an array of buffers into a new buffer.
*
* @param {Buffer[]} list The array of buffers to concat
* @param {Number} totalLength The total length of buffers in the list
* @return {Buffer} The resulting buffer
* @public
*/
function concat(list, totalLength) {
if (list.length === 0) return EMPTY_BUFFER;
if (list.length === 1) return list[0];
const target = Buffer.allocUnsafe(totalLength);
let offset = 0;
for (let i = 0; i < list.length; i++) {
const buf = list[i];
target.set(buf, offset);
offset += buf.length;
}
if (offset < totalLength) return target.slice(0, offset);
return target;
}
/**
* Masks a buffer using the given mask.
*
* @param {Buffer} source The buffer to mask
* @param {Buffer} mask The mask to use
* @param {Buffer} output The buffer where to store the result
* @param {Number} offset The offset at which to start writing
* @param {Number} length The number of bytes to mask.
* @public
*/
function _mask(source, mask, output, offset, length) {
for (let i = 0; i < length; i++) {
output[offset + i] = source[i] ^ mask[i & 3];
}
}
/**
* Unmasks a buffer using the given mask.
*
* @param {Buffer} buffer The buffer to unmask
* @param {Buffer} mask The mask to use
* @public
*/
function _unmask(buffer, mask) {
// Required until https://github.com/nodejs/node/issues/9006 is resolved.
const length = buffer.length;
for (let i = 0; i < length; i++) {
buffer[i] ^= mask[i & 3];
}
}
/**
* Converts a buffer to an `ArrayBuffer`.
*
* @param {Buffer} buf The buffer to convert
* @return {ArrayBuffer} Converted buffer
* @public
*/
function toArrayBuffer(buf) {
if (buf.byteLength === buf.buffer.byteLength) {
return buf.buffer;
}
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
}
/**
* Converts `data` to a `Buffer`.
*
* @param {*} data The data to convert
* @return {Buffer} The buffer
* @throws {TypeError}
* @public
*/
function toBuffer(data) {
toBuffer.readOnly = true;
if (Buffer.isBuffer(data)) return data;
let buf;
if (data instanceof ArrayBuffer) {
buf = Buffer.from(data);
} else if (ArrayBuffer.isView(data)) {
buf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
} else {
buf = Buffer.from(data);
toBuffer.readOnly = false;
}
return buf;
}
try {
const bufferUtil = require('bufferutil');
const bu = bufferUtil.BufferUtil || bufferUtil;
module.exports = {
concat,
mask(source, mask, output, offset, length) {
if (length < 48) _mask(source, mask, output, offset, length);
else bu.mask(source, mask, output, offset, length);
},
toArrayBuffer,
toBuffer,
unmask(buffer, mask) {
if (buffer.length < 32) _unmask(buffer, mask);
else bu.unmask(buffer, mask);
}
};
} catch (e) /* istanbul ignore next */ {
module.exports = {
concat,
mask: _mask,
toArrayBuffer,
toBuffer,
unmask: _unmask
};
}

View File

@@ -1,10 +0,0 @@
'use strict';
module.exports = {
BINARY_TYPES: ['nodebuffer', 'arraybuffer', 'fragments'],
GUID: '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
kStatusCode: Symbol('status-code'),
kWebSocket: Symbol('websocket'),
EMPTY_BUFFER: Buffer.alloc(0),
NOOP: () => {}
};

View File

@@ -1,184 +0,0 @@
'use strict';
/**
* Class representing an event.
*
* @private
*/
class Event {
/**
* Create a new `Event`.
*
* @param {String} type The name of the event
* @param {Object} target A reference to the target to which the event was
* dispatched
*/
constructor(type, target) {
this.target = target;
this.type = type;
}
}
/**
* Class representing a message event.
*
* @extends Event
* @private
*/
class MessageEvent extends Event {
/**
* Create a new `MessageEvent`.
*
* @param {(String|Buffer|ArrayBuffer|Buffer[])} data The received data
* @param {WebSocket} target A reference to the target to which the event was
* dispatched
*/
constructor(data, target) {
super('message', target);
this.data = data;
}
}
/**
* Class representing a close event.
*
* @extends Event
* @private
*/
class CloseEvent extends Event {
/**
* Create a new `CloseEvent`.
*
* @param {Number} code The status code explaining why the connection is being
* closed
* @param {String} reason A human-readable string explaining why the
* connection is closing
* @param {WebSocket} target A reference to the target to which the event was
* dispatched
*/
constructor(code, reason, target) {
super('close', target);
this.wasClean = target._closeFrameReceived && target._closeFrameSent;
this.reason = reason;
this.code = code;
}
}
/**
* Class representing an open event.
*
* @extends Event
* @private
*/
class OpenEvent extends Event {
/**
* Create a new `OpenEvent`.
*
* @param {WebSocket} target A reference to the target to which the event was
* dispatched
*/
constructor(target) {
super('open', target);
}
}
/**
* Class representing an error event.
*
* @extends Event
* @private
*/
class ErrorEvent extends Event {
/**
* Create a new `ErrorEvent`.
*
* @param {Object} error The error that generated this event
* @param {WebSocket} target A reference to the target to which the event was
* dispatched
*/
constructor(error, target) {
super('error', target);
this.message = error.message;
this.error = error;
}
}
/**
* This provides methods for emulating the `EventTarget` interface. It's not
* meant to be used directly.
*
* @mixin
*/
const EventTarget = {
/**
* Register an event listener.
*
* @param {String} type A string representing the event type to listen for
* @param {Function} listener The listener to add
* @param {Object} [options] An options object specifies characteristics about
* the event listener
* @param {Boolean} [options.once=false] A `Boolean`` indicating that the
* listener should be invoked at most once after being added. If `true`,
* the listener would be automatically removed when invoked.
* @public
*/
addEventListener(type, listener, options) {
if (typeof listener !== 'function') return;
function onMessage(data) {
listener.call(this, new MessageEvent(data, this));
}
function onClose(code, message) {
listener.call(this, new CloseEvent(code, message, this));
}
function onError(error) {
listener.call(this, new ErrorEvent(error, this));
}
function onOpen() {
listener.call(this, new OpenEvent(this));
}
const method = options && options.once ? 'once' : 'on';
if (type === 'message') {
onMessage._listener = listener;
this[method](type, onMessage);
} else if (type === 'close') {
onClose._listener = listener;
this[method](type, onClose);
} else if (type === 'error') {
onError._listener = listener;
this[method](type, onError);
} else if (type === 'open') {
onOpen._listener = listener;
this[method](type, onOpen);
} else {
this[method](type, listener);
}
},
/**
* Remove an event listener.
*
* @param {String} type A string representing the event type to remove
* @param {Function} listener The listener to remove
* @public
*/
removeEventListener(type, listener) {
const listeners = this.listeners(type);
for (let i = 0; i < listeners.length; i++) {
if (listeners[i] === listener || listeners[i]._listener === listener) {
this.removeListener(type, listeners[i]);
}
}
}
};
module.exports = EventTarget;

View File

@@ -1,223 +0,0 @@
'use strict';
//
// Allowed token characters:
//
// '!', '#', '$', '%', '&', ''', '*', '+', '-',
// '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
//
// tokenChars[32] === 0 // ' '
// tokenChars[33] === 1 // '!'
// tokenChars[34] === 0 // '"'
// ...
//
// prettier-ignore
const tokenChars = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
];
/**
* Adds an offer to the map of extension offers or a parameter to the map of
* parameters.
*
* @param {Object} dest The map of extension offers or parameters
* @param {String} name The extension or parameter name
* @param {(Object|Boolean|String)} elem The extension parameters or the
* parameter value
* @private
*/
function push(dest, name, elem) {
if (dest[name] === undefined) dest[name] = [elem];
else dest[name].push(elem);
}
/**
* Parses the `Sec-WebSocket-Extensions` header into an object.
*
* @param {String} header The field value of the header
* @return {Object} The parsed object
* @public
*/
function parse(header) {
const offers = Object.create(null);
if (header === undefined || header === '') return offers;
let params = Object.create(null);
let mustUnescape = false;
let isEscaping = false;
let inQuotes = false;
let extensionName;
let paramName;
let start = -1;
let end = -1;
let i = 0;
for (; i < header.length; i++) {
const code = header.charCodeAt(i);
if (extensionName === undefined) {
if (end === -1 && tokenChars[code] === 1) {
if (start === -1) start = i;
} else if (code === 0x20 /* ' ' */ || code === 0x09 /* '\t' */) {
if (end === -1 && start !== -1) end = i;
} else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {
if (start === -1) {
throw new SyntaxError(`Unexpected character at index ${i}`);
}
if (end === -1) end = i;
const name = header.slice(start, end);
if (code === 0x2c) {
push(offers, name, params);
params = Object.create(null);
} else {
extensionName = name;
}
start = end = -1;
} else {
throw new SyntaxError(`Unexpected character at index ${i}`);
}
} else if (paramName === undefined) {
if (end === -1 && tokenChars[code] === 1) {
if (start === -1) start = i;
} else if (code === 0x20 || code === 0x09) {
if (end === -1 && start !== -1) end = i;
} else if (code === 0x3b || code === 0x2c) {
if (start === -1) {
throw new SyntaxError(`Unexpected character at index ${i}`);
}
if (end === -1) end = i;
push(params, header.slice(start, end), true);
if (code === 0x2c) {
push(offers, extensionName, params);
params = Object.create(null);
extensionName = undefined;
}
start = end = -1;
} else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {
paramName = header.slice(start, i);
start = end = -1;
} else {
throw new SyntaxError(`Unexpected character at index ${i}`);
}
} else {
//
// The value of a quoted-string after unescaping must conform to the
// token ABNF, so only token characters are valid.
// Ref: https://tools.ietf.org/html/rfc6455#section-9.1
//
if (isEscaping) {
if (tokenChars[code] !== 1) {
throw new SyntaxError(`Unexpected character at index ${i}`);
}
if (start === -1) start = i;
else if (!mustUnescape) mustUnescape = true;
isEscaping = false;
} else if (inQuotes) {
if (tokenChars[code] === 1) {
if (start === -1) start = i;
} else if (code === 0x22 /* '"' */ && start !== -1) {
inQuotes = false;
end = i;
} else if (code === 0x5c /* '\' */) {
isEscaping = true;
} else {
throw new SyntaxError(`Unexpected character at index ${i}`);
}
} else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
inQuotes = true;
} else if (end === -1 && tokenChars[code] === 1) {
if (start === -1) start = i;
} else if (start !== -1 && (code === 0x20 || code === 0x09)) {
if (end === -1) end = i;
} else if (code === 0x3b || code === 0x2c) {
if (start === -1) {
throw new SyntaxError(`Unexpected character at index ${i}`);
}
if (end === -1) end = i;
let value = header.slice(start, end);
if (mustUnescape) {
value = value.replace(/\\/g, '');
mustUnescape = false;
}
push(params, paramName, value);
if (code === 0x2c) {
push(offers, extensionName, params);
params = Object.create(null);
extensionName = undefined;
}
paramName = undefined;
start = end = -1;
} else {
throw new SyntaxError(`Unexpected character at index ${i}`);
}
}
}
if (start === -1 || inQuotes) {
throw new SyntaxError('Unexpected end of input');
}
if (end === -1) end = i;
const token = header.slice(start, end);
if (extensionName === undefined) {
push(offers, token, params);
} else {
if (paramName === undefined) {
push(params, token, true);
} else if (mustUnescape) {
push(params, paramName, token.replace(/\\/g, ''));
} else {
push(params, paramName, token);
}
push(offers, extensionName, params);
}
return offers;
}
/**
* Builds the `Sec-WebSocket-Extensions` header field value.
*
* @param {Object} extensions The map of extensions and parameters to format
* @return {String} A string representing the given object
* @public
*/
function format(extensions) {
return Object.keys(extensions)
.map((extension) => {
let configurations = extensions[extension];
if (!Array.isArray(configurations)) configurations = [configurations];
return configurations
.map((params) => {
return [extension]
.concat(
Object.keys(params).map((k) => {
let values = params[k];
if (!Array.isArray(values)) values = [values];
return values
.map((v) => (v === true ? k : `${k}=${v}`))
.join('; ');
})
)
.join('; ');
})
.join(', ');
})
.join(', ');
}
module.exports = { format, parse };

View File

@@ -1,55 +0,0 @@
'use strict';
const kDone = Symbol('kDone');
const kRun = Symbol('kRun');
/**
* A very simple job queue with adjustable concurrency. Adapted from
* https://github.com/STRML/async-limiter
*/
class Limiter {
/**
* Creates a new `Limiter`.
*
* @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
* to run concurrently
*/
constructor(concurrency) {
this[kDone] = () => {
this.pending--;
this[kRun]();
};
this.concurrency = concurrency || Infinity;
this.jobs = [];
this.pending = 0;
}
/**
* Adds a job to the queue.
*
* @param {Function} job The job to run
* @public
*/
add(job) {
this.jobs.push(job);
this[kRun]();
}
/**
* Removes a job from the queue and runs it if possible.
*
* @private
*/
[kRun]() {
if (this.pending === this.concurrency) return;
if (this.jobs.length) {
const job = this.jobs.shift();
this.pending++;
job(this[kDone]);
}
}
}
module.exports = Limiter;

View File

@@ -1,518 +0,0 @@
'use strict';
const zlib = require('zlib');
const bufferUtil = require('./buffer-util');
const Limiter = require('./limiter');
const { kStatusCode, NOOP } = require('./constants');
const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
const kPerMessageDeflate = Symbol('permessage-deflate');
const kTotalLength = Symbol('total-length');
const kCallback = Symbol('callback');
const kBuffers = Symbol('buffers');
const kError = Symbol('error');
//
// We limit zlib concurrency, which prevents severe memory fragmentation
// as documented in https://github.com/nodejs/node/issues/8871#issuecomment-250915913
// and https://github.com/websockets/ws/issues/1202
//
// Intentionally global; it's the global thread pool that's an issue.
//
let zlibLimiter;
/**
* permessage-deflate implementation.
*/
class PerMessageDeflate {
/**
* Creates a PerMessageDeflate instance.
*
* @param {Object} [options] Configuration options
* @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
* disabling of server context takeover
* @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
* acknowledge disabling of client context takeover
* @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
* use of a custom server window size
* @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
* for, or request, a custom client window size
* @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
* deflate
* @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
* inflate
* @param {Number} [options.threshold=1024] Size (in bytes) below which
* messages should not be compressed
* @param {Number} [options.concurrencyLimit=10] The number of concurrent
* calls to zlib
* @param {Boolean} [isServer=false] Create the instance in either server or
* client mode
* @param {Number} [maxPayload=0] The maximum allowed message length
*/
constructor(options, isServer, maxPayload) {
this._maxPayload = maxPayload | 0;
this._options = options || {};
this._threshold =
this._options.threshold !== undefined ? this._options.threshold : 1024;
this._isServer = !!isServer;
this._deflate = null;
this._inflate = null;
this.params = null;
if (!zlibLimiter) {
const concurrency =
this._options.concurrencyLimit !== undefined
? this._options.concurrencyLimit
: 10;
zlibLimiter = new Limiter(concurrency);
}
}
/**
* @type {String}
*/
static get extensionName() {
return 'permessage-deflate';
}
/**
* Create an extension negotiation offer.
*
* @return {Object} Extension parameters
* @public
*/
offer() {
const params = {};
if (this._options.serverNoContextTakeover) {
params.server_no_context_takeover = true;
}
if (this._options.clientNoContextTakeover) {
params.client_no_context_takeover = true;
}
if (this._options.serverMaxWindowBits) {
params.server_max_window_bits = this._options.serverMaxWindowBits;
}
if (this._options.clientMaxWindowBits) {
params.client_max_window_bits = this._options.clientMaxWindowBits;
} else if (this._options.clientMaxWindowBits == null) {
params.client_max_window_bits = true;
}
return params;
}
/**
* Accept an extension negotiation offer/response.
*
* @param {Array} configurations The extension negotiation offers/reponse
* @return {Object} Accepted configuration
* @public
*/
accept(configurations) {
configurations = this.normalizeParams(configurations);
this.params = this._isServer
? this.acceptAsServer(configurations)
: this.acceptAsClient(configurations);
return this.params;
}
/**
* Releases all resources used by the extension.
*
* @public
*/
cleanup() {
if (this._inflate) {
this._inflate.close();
this._inflate = null;
}
if (this._deflate) {
const callback = this._deflate[kCallback];
this._deflate.close();
this._deflate = null;
if (callback) {
callback(
new Error(
'The deflate stream was closed while data was being processed'
)
);
}
}
}
/**
* Accept an extension negotiation offer.
*
* @param {Array} offers The extension negotiation offers
* @return {Object} Accepted configuration
* @private
*/
acceptAsServer(offers) {
const opts = this._options;
const accepted = offers.find((params) => {
if (
(opts.serverNoContextTakeover === false &&
params.server_no_context_takeover) ||
(params.server_max_window_bits &&
(opts.serverMaxWindowBits === false ||
(typeof opts.serverMaxWindowBits === 'number' &&
opts.serverMaxWindowBits > params.server_max_window_bits))) ||
(typeof opts.clientMaxWindowBits === 'number' &&
!params.client_max_window_bits)
) {
return false;
}
return true;
});
if (!accepted) {
throw new Error('None of the extension offers can be accepted');
}
if (opts.serverNoContextTakeover) {
accepted.server_no_context_takeover = true;
}
if (opts.clientNoContextTakeover) {
accepted.client_no_context_takeover = true;
}
if (typeof opts.serverMaxWindowBits === 'number') {
accepted.server_max_window_bits = opts.serverMaxWindowBits;
}
if (typeof opts.clientMaxWindowBits === 'number') {
accepted.client_max_window_bits = opts.clientMaxWindowBits;
} else if (
accepted.client_max_window_bits === true ||
opts.clientMaxWindowBits === false
) {
delete accepted.client_max_window_bits;
}
return accepted;
}
/**
* Accept the extension negotiation response.
*
* @param {Array} response The extension negotiation response
* @return {Object} Accepted configuration
* @private
*/
acceptAsClient(response) {
const params = response[0];
if (
this._options.clientNoContextTakeover === false &&
params.client_no_context_takeover
) {
throw new Error('Unexpected parameter "client_no_context_takeover"');
}
if (!params.client_max_window_bits) {
if (typeof this._options.clientMaxWindowBits === 'number') {
params.client_max_window_bits = this._options.clientMaxWindowBits;
}
} else if (
this._options.clientMaxWindowBits === false ||
(typeof this._options.clientMaxWindowBits === 'number' &&
params.client_max_window_bits > this._options.clientMaxWindowBits)
) {
throw new Error(
'Unexpected or invalid parameter "client_max_window_bits"'
);
}
return params;
}
/**
* Normalize parameters.
*
* @param {Array} configurations The extension negotiation offers/reponse
* @return {Array} The offers/response with normalized parameters
* @private
*/
normalizeParams(configurations) {
configurations.forEach((params) => {
Object.keys(params).forEach((key) => {
let value = params[key];
if (value.length > 1) {
throw new Error(`Parameter "${key}" must have only a single value`);
}
value = value[0];
if (key === 'client_max_window_bits') {
if (value !== true) {
const num = +value;
if (!Number.isInteger(num) || num < 8 || num > 15) {
throw new TypeError(
`Invalid value for parameter "${key}": ${value}`
);
}
value = num;
} else if (!this._isServer) {
throw new TypeError(
`Invalid value for parameter "${key}": ${value}`
);
}
} else if (key === 'server_max_window_bits') {
const num = +value;
if (!Number.isInteger(num) || num < 8 || num > 15) {
throw new TypeError(
`Invalid value for parameter "${key}": ${value}`
);
}
value = num;
} else if (
key === 'client_no_context_takeover' ||
key === 'server_no_context_takeover'
) {
if (value !== true) {
throw new TypeError(
`Invalid value for parameter "${key}": ${value}`
);
}
} else {
throw new Error(`Unknown parameter "${key}"`);
}
params[key] = value;
});
});
return configurations;
}
/**
* Decompress data. Concurrency limited.
*
* @param {Buffer} data Compressed data
* @param {Boolean} fin Specifies whether or not this is the last fragment
* @param {Function} callback Callback
* @public
*/
decompress(data, fin, callback) {
zlibLimiter.add((done) => {
this._decompress(data, fin, (err, result) => {
done();
callback(err, result);
});
});
}
/**
* Compress data. Concurrency limited.
*
* @param {Buffer} data Data to compress
* @param {Boolean} fin Specifies whether or not this is the last fragment
* @param {Function} callback Callback
* @public
*/
compress(data, fin, callback) {
zlibLimiter.add((done) => {
this._compress(data, fin, (err, result) => {
done();
callback(err, result);
});
});
}
/**
* Decompress data.
*
* @param {Buffer} data Compressed data
* @param {Boolean} fin Specifies whether or not this is the last fragment
* @param {Function} callback Callback
* @private
*/
_decompress(data, fin, callback) {
const endpoint = this._isServer ? 'client' : 'server';
if (!this._inflate) {
const key = `${endpoint}_max_window_bits`;
const windowBits =
typeof this.params[key] !== 'number'
? zlib.Z_DEFAULT_WINDOWBITS
: this.params[key];
this._inflate = zlib.createInflateRaw({
...this._options.zlibInflateOptions,
windowBits
});
this._inflate[kPerMessageDeflate] = this;
this._inflate[kTotalLength] = 0;
this._inflate[kBuffers] = [];
this._inflate.on('error', inflateOnError);
this._inflate.on('data', inflateOnData);
}
this._inflate[kCallback] = callback;
this._inflate.write(data);
if (fin) this._inflate.write(TRAILER);
this._inflate.flush(() => {
const err = this._inflate[kError];
if (err) {
this._inflate.close();
this._inflate = null;
callback(err);
return;
}
const data = bufferUtil.concat(
this._inflate[kBuffers],
this._inflate[kTotalLength]
);
if (this._inflate._readableState.endEmitted) {
this._inflate.close();
this._inflate = null;
} else {
this._inflate[kTotalLength] = 0;
this._inflate[kBuffers] = [];
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
this._inflate.reset();
}
}
callback(null, data);
});
}
/**
* Compress data.
*
* @param {Buffer} data Data to compress
* @param {Boolean} fin Specifies whether or not this is the last fragment
* @param {Function} callback Callback
* @private
*/
_compress(data, fin, callback) {
const endpoint = this._isServer ? 'server' : 'client';
if (!this._deflate) {
const key = `${endpoint}_max_window_bits`;
const windowBits =
typeof this.params[key] !== 'number'
? zlib.Z_DEFAULT_WINDOWBITS
: this.params[key];
this._deflate = zlib.createDeflateRaw({
...this._options.zlibDeflateOptions,
windowBits
});
this._deflate[kTotalLength] = 0;
this._deflate[kBuffers] = [];
//
// An `'error'` event is emitted, only on Node.js < 10.0.0, if the
// `zlib.DeflateRaw` instance is closed while data is being processed.
// This can happen if `PerMessageDeflate#cleanup()` is called at the wrong
// time due to an abnormal WebSocket closure.
//
this._deflate.on('error', NOOP);
this._deflate.on('data', deflateOnData);
}
this._deflate[kCallback] = callback;
this._deflate.write(data);
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
if (!this._deflate) {
//
// The deflate stream was closed while data was being processed.
//
return;
}
let data = bufferUtil.concat(
this._deflate[kBuffers],
this._deflate[kTotalLength]
);
if (fin) data = data.slice(0, data.length - 4);
//
// Ensure that the callback will not be called again in
// `PerMessageDeflate#cleanup()`.
//
this._deflate[kCallback] = null;
this._deflate[kTotalLength] = 0;
this._deflate[kBuffers] = [];
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
this._deflate.reset();
}
callback(null, data);
});
}
}
module.exports = PerMessageDeflate;
/**
* The listener of the `zlib.DeflateRaw` stream `'data'` event.
*
* @param {Buffer} chunk A chunk of data
* @private
*/
function deflateOnData(chunk) {
this[kBuffers].push(chunk);
this[kTotalLength] += chunk.length;
}
/**
* The listener of the `zlib.InflateRaw` stream `'data'` event.
*
* @param {Buffer} chunk A chunk of data
* @private
*/
function inflateOnData(chunk) {
this[kTotalLength] += chunk.length;
if (
this[kPerMessageDeflate]._maxPayload < 1 ||
this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
) {
this[kBuffers].push(chunk);
return;
}
this[kError] = new RangeError('Max payload size exceeded');
this[kError].code = 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH';
this[kError][kStatusCode] = 1009;
this.removeListener('data', inflateOnData);
this.reset();
}
/**
* The listener of the `zlib.InflateRaw` stream `'error'` event.
*
* @param {Error} err The emitted error
* @private
*/
function inflateOnError(err) {
//
// There is no need to call `Zlib#close()` as the handle is automatically
// closed when an error is emitted.
//
this[kPerMessageDeflate]._inflate = null;
err[kStatusCode] = 1007;
this[kCallback](err);
}

View File

@@ -1,607 +0,0 @@
'use strict';
const { Writable } = require('stream');
const PerMessageDeflate = require('./permessage-deflate');
const {
BINARY_TYPES,
EMPTY_BUFFER,
kStatusCode,
kWebSocket
} = require('./constants');
const { concat, toArrayBuffer, unmask } = require('./buffer-util');
const { isValidStatusCode, isValidUTF8 } = require('./validation');
const GET_INFO = 0;
const GET_PAYLOAD_LENGTH_16 = 1;
const GET_PAYLOAD_LENGTH_64 = 2;
const GET_MASK = 3;
const GET_DATA = 4;
const INFLATING = 5;
/**
* HyBi Receiver implementation.
*
* @extends Writable
*/
class Receiver extends Writable {
/**
* Creates a Receiver instance.
*
* @param {String} [binaryType=nodebuffer] The type for binary data
* @param {Object} [extensions] An object containing the negotiated extensions
* @param {Boolean} [isServer=false] Specifies whether to operate in client or
* server mode
* @param {Number} [maxPayload=0] The maximum allowed message length
*/
constructor(binaryType, extensions, isServer, maxPayload) {
super();
this._binaryType = binaryType || BINARY_TYPES[0];
this[kWebSocket] = undefined;
this._extensions = extensions || {};
this._isServer = !!isServer;
this._maxPayload = maxPayload | 0;
this._bufferedBytes = 0;
this._buffers = [];
this._compressed = false;
this._payloadLength = 0;
this._mask = undefined;
this._fragmented = 0;
this._masked = false;
this._fin = false;
this._opcode = 0;
this._totalPayloadLength = 0;
this._messageLength = 0;
this._fragments = [];
this._state = GET_INFO;
this._loop = false;
}
/**
* Implements `Writable.prototype._write()`.
*
* @param {Buffer} chunk The chunk of data to write
* @param {String} encoding The character encoding of `chunk`
* @param {Function} cb Callback
* @private
*/
_write(chunk, encoding, cb) {
if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
this._bufferedBytes += chunk.length;
this._buffers.push(chunk);
this.startLoop(cb);
}
/**
* Consumes `n` bytes from the buffered data.
*
* @param {Number} n The number of bytes to consume
* @return {Buffer} The consumed bytes
* @private
*/
consume(n) {
this._bufferedBytes -= n;
if (n === this._buffers[0].length) return this._buffers.shift();
if (n < this._buffers[0].length) {
const buf = this._buffers[0];
this._buffers[0] = buf.slice(n);
return buf.slice(0, n);
}
const dst = Buffer.allocUnsafe(n);
do {
const buf = this._buffers[0];
const offset = dst.length - n;
if (n >= buf.length) {
dst.set(this._buffers.shift(), offset);
} else {
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
this._buffers[0] = buf.slice(n);
}
n -= buf.length;
} while (n > 0);
return dst;
}
/**
* Starts the parsing loop.
*
* @param {Function} cb Callback
* @private
*/
startLoop(cb) {
let err;
this._loop = true;
do {
switch (this._state) {
case GET_INFO:
err = this.getInfo();
break;
case GET_PAYLOAD_LENGTH_16:
err = this.getPayloadLength16();
break;
case GET_PAYLOAD_LENGTH_64:
err = this.getPayloadLength64();
break;
case GET_MASK:
this.getMask();
break;
case GET_DATA:
err = this.getData(cb);
break;
default:
// `INFLATING`
this._loop = false;
return;
}
} while (this._loop);
cb(err);
}
/**
* Reads the first two bytes of a frame.
*
* @return {(RangeError|undefined)} A possible error
* @private
*/
getInfo() {
if (this._bufferedBytes < 2) {
this._loop = false;
return;
}
const buf = this.consume(2);
if ((buf[0] & 0x30) !== 0x00) {
this._loop = false;
return error(
RangeError,
'RSV2 and RSV3 must be clear',
true,
1002,
'WS_ERR_UNEXPECTED_RSV_2_3'
);
}
const compressed = (buf[0] & 0x40) === 0x40;
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
this._loop = false;
return error(
RangeError,
'RSV1 must be clear',
true,
1002,
'WS_ERR_UNEXPECTED_RSV_1'
);
}
this._fin = (buf[0] & 0x80) === 0x80;
this._opcode = buf[0] & 0x0f;
this._payloadLength = buf[1] & 0x7f;
if (this._opcode === 0x00) {
if (compressed) {
this._loop = false;
return error(
RangeError,
'RSV1 must be clear',
true,
1002,
'WS_ERR_UNEXPECTED_RSV_1'
);
}
if (!this._fragmented) {
this._loop = false;
return error(
RangeError,
'invalid opcode 0',
true,
1002,
'WS_ERR_INVALID_OPCODE'
);
}
this._opcode = this._fragmented;
} else if (this._opcode === 0x01 || this._opcode === 0x02) {
if (this._fragmented) {
this._loop = false;
return error(
RangeError,
`invalid opcode ${this._opcode}`,
true,
1002,
'WS_ERR_INVALID_OPCODE'
);
}
this._compressed = compressed;
} else if (this._opcode > 0x07 && this._opcode < 0x0b) {
if (!this._fin) {
this._loop = false;
return error(
RangeError,
'FIN must be set',
true,
1002,
'WS_ERR_EXPECTED_FIN'
);
}
if (compressed) {
this._loop = false;
return error(
RangeError,
'RSV1 must be clear',
true,
1002,
'WS_ERR_UNEXPECTED_RSV_1'
);
}
if (this._payloadLength > 0x7d) {
this._loop = false;
return error(
RangeError,
`invalid payload length ${this._payloadLength}`,
true,
1002,
'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
);
}
} else {
this._loop = false;
return error(
RangeError,
`invalid opcode ${this._opcode}`,
true,
1002,
'WS_ERR_INVALID_OPCODE'
);
}
if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
this._masked = (buf[1] & 0x80) === 0x80;
if (this._isServer) {
if (!this._masked) {
this._loop = false;
return error(
RangeError,
'MASK must be set',
true,
1002,
'WS_ERR_EXPECTED_MASK'
);
}
} else if (this._masked) {
this._loop = false;
return error(
RangeError,
'MASK must be clear',
true,
1002,
'WS_ERR_UNEXPECTED_MASK'
);
}
if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
else return this.haveLength();
}
/**
* Gets extended payload length (7+16).
*
* @return {(RangeError|undefined)} A possible error
* @private
*/
getPayloadLength16() {
if (this._bufferedBytes < 2) {
this._loop = false;
return;
}
this._payloadLength = this.consume(2).readUInt16BE(0);
return this.haveLength();
}
/**
* Gets extended payload length (7+64).
*
* @return {(RangeError|undefined)} A possible error
* @private
*/
getPayloadLength64() {
if (this._bufferedBytes < 8) {
this._loop = false;
return;
}
const buf = this.consume(8);
const num = buf.readUInt32BE(0);
//
// The maximum safe integer in JavaScript is 2^53 - 1. An error is returned
// if payload length is greater than this number.
//
if (num > Math.pow(2, 53 - 32) - 1) {
this._loop = false;
return error(
RangeError,
'Unsupported WebSocket frame: payload length > 2^53 - 1',
false,
1009,
'WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH'
);
}
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
return this.haveLength();
}
/**
* Payload length has been read.
*
* @return {(RangeError|undefined)} A possible error
* @private
*/
haveLength() {
if (this._payloadLength && this._opcode < 0x08) {
this._totalPayloadLength += this._payloadLength;
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
this._loop = false;
return error(
RangeError,
'Max payload size exceeded',
false,
1009,
'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
);
}
}
if (this._masked) this._state = GET_MASK;
else this._state = GET_DATA;
}
/**
* Reads mask bytes.
*
* @private
*/
getMask() {
if (this._bufferedBytes < 4) {
this._loop = false;
return;
}
this._mask = this.consume(4);
this._state = GET_DATA;
}
/**
* Reads data bytes.
*
* @param {Function} cb Callback
* @return {(Error|RangeError|undefined)} A possible error
* @private
*/
getData(cb) {
let data = EMPTY_BUFFER;
if (this._payloadLength) {
if (this._bufferedBytes < this._payloadLength) {
this._loop = false;
return;
}
data = this.consume(this._payloadLength);
if (this._masked) unmask(data, this._mask);
}
if (this._opcode > 0x07) return this.controlMessage(data);
if (this._compressed) {
this._state = INFLATING;
this.decompress(data, cb);
return;
}
if (data.length) {
//
// This message is not compressed so its lenght is the sum of the payload
// length of all fragments.
//
this._messageLength = this._totalPayloadLength;
this._fragments.push(data);
}
return this.dataMessage();
}
/**
* Decompresses data.
*
* @param {Buffer} data Compressed data
* @param {Function} cb Callback
* @private
*/
decompress(data, cb) {
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
if (err) return cb(err);
if (buf.length) {
this._messageLength += buf.length;
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
return cb(
error(
RangeError,
'Max payload size exceeded',
false,
1009,
'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
)
);
}
this._fragments.push(buf);
}
const er = this.dataMessage();
if (er) return cb(er);
this.startLoop(cb);
});
}
/**
* Handles a data message.
*
* @return {(Error|undefined)} A possible error
* @private
*/
dataMessage() {
if (this._fin) {
const messageLength = this._messageLength;
const fragments = this._fragments;
this._totalPayloadLength = 0;
this._messageLength = 0;
this._fragmented = 0;
this._fragments = [];
if (this._opcode === 2) {
let data;
if (this._binaryType === 'nodebuffer') {
data = concat(fragments, messageLength);
} else if (this._binaryType === 'arraybuffer') {
data = toArrayBuffer(concat(fragments, messageLength));
} else {
data = fragments;
}
this.emit('message', data);
} else {
const buf = concat(fragments, messageLength);
if (!isValidUTF8(buf)) {
this._loop = false;
return error(
Error,
'invalid UTF-8 sequence',
true,
1007,
'WS_ERR_INVALID_UTF8'
);
}
this.emit('message', buf.toString());
}
}
this._state = GET_INFO;
}
/**
* Handles a control message.
*
* @param {Buffer} data Data to handle
* @return {(Error|RangeError|undefined)} A possible error
* @private
*/
controlMessage(data) {
if (this._opcode === 0x08) {
this._loop = false;
if (data.length === 0) {
this.emit('conclude', 1005, '');
this.end();
} else if (data.length === 1) {
return error(
RangeError,
'invalid payload length 1',
true,
1002,
'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
);
} else {
const code = data.readUInt16BE(0);
if (!isValidStatusCode(code)) {
return error(
RangeError,
`invalid status code ${code}`,
true,
1002,
'WS_ERR_INVALID_CLOSE_CODE'
);
}
const buf = data.slice(2);
if (!isValidUTF8(buf)) {
return error(
Error,
'invalid UTF-8 sequence',
true,
1007,
'WS_ERR_INVALID_UTF8'
);
}
this.emit('conclude', code, buf.toString());
this.end();
}
} else if (this._opcode === 0x09) {
this.emit('ping', data);
} else {
this.emit('pong', data);
}
this._state = GET_INFO;
}
}
module.exports = Receiver;
/**
* Builds an error object.
*
* @param {function(new:Error|RangeError)} ErrorCtor The error constructor
* @param {String} message The error message
* @param {Boolean} prefix Specifies whether or not to add a default prefix to
* `message`
* @param {Number} statusCode The status code
* @param {String} errorCode The exposed error code
* @return {(Error|RangeError)} The error
* @private
*/
function error(ErrorCtor, message, prefix, statusCode, errorCode) {
const err = new ErrorCtor(
prefix ? `Invalid WebSocket frame: ${message}` : message
);
Error.captureStackTrace(err, error);
err.code = errorCode;
err[kStatusCode] = statusCode;
return err;
}

View File

@@ -1,409 +0,0 @@
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls$" }] */
'use strict';
const net = require('net');
const tls = require('tls');
const { randomFillSync } = require('crypto');
const PerMessageDeflate = require('./permessage-deflate');
const { EMPTY_BUFFER } = require('./constants');
const { isValidStatusCode } = require('./validation');
const { mask: applyMask, toBuffer } = require('./buffer-util');
const mask = Buffer.alloc(4);
/**
* HyBi Sender implementation.
*/
class Sender {
/**
* Creates a Sender instance.
*
* @param {(net.Socket|tls.Socket)} socket The connection socket
* @param {Object} [extensions] An object containing the negotiated extensions
*/
constructor(socket, extensions) {
this._extensions = extensions || {};
this._socket = socket;
this._firstFragment = true;
this._compress = false;
this._bufferedBytes = 0;
this._deflating = false;
this._queue = [];
}
/**
* Frames a piece of data according to the HyBi WebSocket protocol.
*
* @param {Buffer} data The data to frame
* @param {Object} options Options object
* @param {Number} options.opcode The opcode
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
* modified
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
* FIN bit
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
* `data`
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
* RSV1 bit
* @return {Buffer[]} The framed data as a list of `Buffer` instances
* @public
*/
static frame(data, options) {
const merge = options.mask && options.readOnly;
let offset = options.mask ? 6 : 2;
let payloadLength = data.length;
if (data.length >= 65536) {
offset += 8;
payloadLength = 127;
} else if (data.length > 125) {
offset += 2;
payloadLength = 126;
}
const target = Buffer.allocUnsafe(merge ? data.length + offset : offset);
target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
if (options.rsv1) target[0] |= 0x40;
target[1] = payloadLength;
if (payloadLength === 126) {
target.writeUInt16BE(data.length, 2);
} else if (payloadLength === 127) {
target.writeUInt32BE(0, 2);
target.writeUInt32BE(data.length, 6);
}
if (!options.mask) return [target, data];
randomFillSync(mask, 0, 4);
target[1] |= 0x80;
target[offset - 4] = mask[0];
target[offset - 3] = mask[1];
target[offset - 2] = mask[2];
target[offset - 1] = mask[3];
if (merge) {
applyMask(data, mask, target, offset, data.length);
return [target];
}
applyMask(data, mask, data, 0, data.length);
return [target, data];
}
/**
* Sends a close message to the other peer.
*
* @param {Number} [code] The status code component of the body
* @param {String} [data] The message component of the body
* @param {Boolean} [mask=false] Specifies whether or not to mask the message
* @param {Function} [cb] Callback
* @public
*/
close(code, data, mask, cb) {
let buf;
if (code === undefined) {
buf = EMPTY_BUFFER;
} else if (typeof code !== 'number' || !isValidStatusCode(code)) {
throw new TypeError('First argument must be a valid error code number');
} else if (data === undefined || data === '') {
buf = Buffer.allocUnsafe(2);
buf.writeUInt16BE(code, 0);
} else {
const length = Buffer.byteLength(data);
if (length > 123) {
throw new RangeError('The message must not be greater than 123 bytes');
}
buf = Buffer.allocUnsafe(2 + length);
buf.writeUInt16BE(code, 0);
buf.write(data, 2);
}
if (this._deflating) {
this.enqueue([this.doClose, buf, mask, cb]);
} else {
this.doClose(buf, mask, cb);
}
}
/**
* Frames and sends a close message.
*
* @param {Buffer} data The message to send
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
* @param {Function} [cb] Callback
* @private
*/
doClose(data, mask, cb) {
this.sendFrame(
Sender.frame(data, {
fin: true,
rsv1: false,
opcode: 0x08,
mask,
readOnly: false
}),
cb
);
}
/**
* Sends a ping message to the other peer.
*
* @param {*} data The message to send
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
* @param {Function} [cb] Callback
* @public
*/
ping(data, mask, cb) {
const buf = toBuffer(data);
if (buf.length > 125) {
throw new RangeError('The data size must not be greater than 125 bytes');
}
if (this._deflating) {
this.enqueue([this.doPing, buf, mask, toBuffer.readOnly, cb]);
} else {
this.doPing(buf, mask, toBuffer.readOnly, cb);
}
}
/**
* Frames and sends a ping message.
*
* @param {Buffer} data The message to send
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
* @param {Boolean} [readOnly=false] Specifies whether `data` can be modified
* @param {Function} [cb] Callback
* @private
*/
doPing(data, mask, readOnly, cb) {
this.sendFrame(
Sender.frame(data, {
fin: true,
rsv1: false,
opcode: 0x09,
mask,
readOnly
}),
cb
);
}
/**
* Sends a pong message to the other peer.
*
* @param {*} data The message to send
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
* @param {Function} [cb] Callback
* @public
*/
pong(data, mask, cb) {
const buf = toBuffer(data);
if (buf.length > 125) {
throw new RangeError('The data size must not be greater than 125 bytes');
}
if (this._deflating) {
this.enqueue([this.doPong, buf, mask, toBuffer.readOnly, cb]);
} else {
this.doPong(buf, mask, toBuffer.readOnly, cb);
}
}
/**
* Frames and sends a pong message.
*
* @param {Buffer} data The message to send
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
* @param {Boolean} [readOnly=false] Specifies whether `data` can be modified
* @param {Function} [cb] Callback
* @private
*/
doPong(data, mask, readOnly, cb) {
this.sendFrame(
Sender.frame(data, {
fin: true,
rsv1: false,
opcode: 0x0a,
mask,
readOnly
}),
cb
);
}
/**
* Sends a data message to the other peer.
*
* @param {*} data The message to send
* @param {Object} options Options object
* @param {Boolean} [options.compress=false] Specifies whether or not to
* compress `data`
* @param {Boolean} [options.binary=false] Specifies whether `data` is binary
* or text
* @param {Boolean} [options.fin=false] Specifies whether the fragment is the
* last one
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
* `data`
* @param {Function} [cb] Callback
* @public
*/
send(data, options, cb) {
const buf = toBuffer(data);
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
let opcode = options.binary ? 2 : 1;
let rsv1 = options.compress;
if (this._firstFragment) {
this._firstFragment = false;
if (rsv1 && perMessageDeflate) {
rsv1 = buf.length >= perMessageDeflate._threshold;
}
this._compress = rsv1;
} else {
rsv1 = false;
opcode = 0;
}
if (options.fin) this._firstFragment = true;
if (perMessageDeflate) {
const opts = {
fin: options.fin,
rsv1,
opcode,
mask: options.mask,
readOnly: toBuffer.readOnly
};
if (this._deflating) {
this.enqueue([this.dispatch, buf, this._compress, opts, cb]);
} else {
this.dispatch(buf, this._compress, opts, cb);
}
} else {
this.sendFrame(
Sender.frame(buf, {
fin: options.fin,
rsv1: false,
opcode,
mask: options.mask,
readOnly: toBuffer.readOnly
}),
cb
);
}
}
/**
* Dispatches a data message.
*
* @param {Buffer} data The message to send
* @param {Boolean} [compress=false] Specifies whether or not to compress
* `data`
* @param {Object} options Options object
* @param {Number} options.opcode The opcode
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
* modified
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
* FIN bit
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
* `data`
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
* RSV1 bit
* @param {Function} [cb] Callback
* @private
*/
dispatch(data, compress, options, cb) {
if (!compress) {
this.sendFrame(Sender.frame(data, options), cb);
return;
}
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
this._bufferedBytes += data.length;
this._deflating = true;
perMessageDeflate.compress(data, options.fin, (_, buf) => {
if (this._socket.destroyed) {
const err = new Error(
'The socket was closed while data was being compressed'
);
if (typeof cb === 'function') cb(err);
for (let i = 0; i < this._queue.length; i++) {
const callback = this._queue[i][4];
if (typeof callback === 'function') callback(err);
}
return;
}
this._bufferedBytes -= data.length;
this._deflating = false;
options.readOnly = false;
this.sendFrame(Sender.frame(buf, options), cb);
this.dequeue();
});
}
/**
* Executes queued send operations.
*
* @private
*/
dequeue() {
while (!this._deflating && this._queue.length) {
const params = this._queue.shift();
this._bufferedBytes -= params[1].length;
Reflect.apply(params[0], this, params.slice(1));
}
}
/**
* Enqueues a send operation.
*
* @param {Array} params Send operation parameters.
* @private
*/
enqueue(params) {
this._bufferedBytes += params[1].length;
this._queue.push(params);
}
/**
* Sends a frame.
*
* @param {Buffer[]} list The frame to send
* @param {Function} [cb] Callback
* @private
*/
sendFrame(list, cb) {
if (list.length === 2) {
this._socket.cork();
this._socket.write(list[0]);
this._socket.write(list[1], cb);
this._socket.uncork();
} else {
this._socket.write(list[0], cb);
}
}
}
module.exports = Sender;

View File

@@ -1,180 +0,0 @@
'use strict';
const { Duplex } = require('stream');
/**
* Emits the `'close'` event on a stream.
*
* @param {Duplex} stream The stream.
* @private
*/
function emitClose(stream) {
stream.emit('close');
}
/**
* The listener of the `'end'` event.
*
* @private
*/
function duplexOnEnd() {
if (!this.destroyed && this._writableState.finished) {
this.destroy();
}
}
/**
* The listener of the `'error'` event.
*
* @param {Error} err The error
* @private
*/
function duplexOnError(err) {
this.removeListener('error', duplexOnError);
this.destroy();
if (this.listenerCount('error') === 0) {
// Do not suppress the throwing behavior.
this.emit('error', err);
}
}
/**
* Wraps a `WebSocket` in a duplex stream.
*
* @param {WebSocket} ws The `WebSocket` to wrap
* @param {Object} [options] The options for the `Duplex` constructor
* @return {Duplex} The duplex stream
* @public
*/
function createWebSocketStream(ws, options) {
let resumeOnReceiverDrain = true;
let terminateOnDestroy = true;
function receiverOnDrain() {
if (resumeOnReceiverDrain) ws._socket.resume();
}
if (ws.readyState === ws.CONNECTING) {
ws.once('open', function open() {
ws._receiver.removeAllListeners('drain');
ws._receiver.on('drain', receiverOnDrain);
});
} else {
ws._receiver.removeAllListeners('drain');
ws._receiver.on('drain', receiverOnDrain);
}
const duplex = new Duplex({
...options,
autoDestroy: false,
emitClose: false,
objectMode: false,
writableObjectMode: false
});
ws.on('message', function message(msg) {
if (!duplex.push(msg)) {
resumeOnReceiverDrain = false;
ws._socket.pause();
}
});
ws.once('error', function error(err) {
if (duplex.destroyed) return;
// Prevent `ws.terminate()` from being called by `duplex._destroy()`.
//
// - If the `'error'` event is emitted before the `'open'` event, then
// `ws.terminate()` is a noop as no socket is assigned.
// - Otherwise, the error is re-emitted by the listener of the `'error'`
// event of the `Receiver` object. The listener already closes the
// connection by calling `ws.close()`. This allows a close frame to be
// sent to the other peer. If `ws.terminate()` is called right after this,
// then the close frame might not be sent.
terminateOnDestroy = false;
duplex.destroy(err);
});
ws.once('close', function close() {
if (duplex.destroyed) return;
duplex.push(null);
});
duplex._destroy = function (err, callback) {
if (ws.readyState === ws.CLOSED) {
callback(err);
process.nextTick(emitClose, duplex);
return;
}
let called = false;
ws.once('error', function error(err) {
called = true;
callback(err);
});
ws.once('close', function close() {
if (!called) callback(err);
process.nextTick(emitClose, duplex);
});
if (terminateOnDestroy) ws.terminate();
};
duplex._final = function (callback) {
if (ws.readyState === ws.CONNECTING) {
ws.once('open', function open() {
duplex._final(callback);
});
return;
}
// If the value of the `_socket` property is `null` it means that `ws` is a
// client websocket and the handshake failed. In fact, when this happens, a
// socket is never assigned to the websocket. Wait for the `'error'` event
// that will be emitted by the websocket.
if (ws._socket === null) return;
if (ws._socket._writableState.finished) {
callback();
if (duplex._readableState.endEmitted) duplex.destroy();
} else {
ws._socket.once('finish', function finish() {
// `duplex` is not destroyed here because the `'end'` event will be
// emitted on `duplex` after this `'finish'` event. The EOF signaling
// `null` chunk is, in fact, pushed when the websocket emits `'close'`.
callback();
});
ws.close();
}
};
duplex._read = function () {
if (
(ws.readyState === ws.OPEN || ws.readyState === ws.CLOSING) &&
!resumeOnReceiverDrain
) {
resumeOnReceiverDrain = true;
if (!ws._receiver._writableState.needDrain) ws._socket.resume();
}
};
duplex._write = function (chunk, encoding, callback) {
if (ws.readyState === ws.CONNECTING) {
ws.once('open', function open() {
duplex._write(chunk, encoding, callback);
});
return;
}
ws.send(chunk, callback);
};
duplex.on('end', duplexOnEnd);
duplex.on('error', duplexOnError);
return duplex;
}
module.exports = createWebSocketStream;

View File

@@ -1,104 +0,0 @@
'use strict';
/**
* Checks if a status code is allowed in a close frame.
*
* @param {Number} code The status code
* @return {Boolean} `true` if the status code is valid, else `false`
* @public
*/
function isValidStatusCode(code) {
return (
(code >= 1000 &&
code <= 1014 &&
code !== 1004 &&
code !== 1005 &&
code !== 1006) ||
(code >= 3000 && code <= 4999)
);
}
/**
* Checks if a given buffer contains only correct UTF-8.
* Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
* Markus Kuhn.
*
* @param {Buffer} buf The buffer to check
* @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
* @public
*/
function _isValidUTF8(buf) {
const len = buf.length;
let i = 0;
while (i < len) {
if ((buf[i] & 0x80) === 0) {
// 0xxxxxxx
i++;
} else if ((buf[i] & 0xe0) === 0xc0) {
// 110xxxxx 10xxxxxx
if (
i + 1 === len ||
(buf[i + 1] & 0xc0) !== 0x80 ||
(buf[i] & 0xfe) === 0xc0 // Overlong
) {
return false;
}
i += 2;
} else if ((buf[i] & 0xf0) === 0xe0) {
// 1110xxxx 10xxxxxx 10xxxxxx
if (
i + 2 >= len ||
(buf[i + 1] & 0xc0) !== 0x80 ||
(buf[i + 2] & 0xc0) !== 0x80 ||
(buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong
(buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)
) {
return false;
}
i += 3;
} else if ((buf[i] & 0xf8) === 0xf0) {
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
if (
i + 3 >= len ||
(buf[i + 1] & 0xc0) !== 0x80 ||
(buf[i + 2] & 0xc0) !== 0x80 ||
(buf[i + 3] & 0xc0) !== 0x80 ||
(buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong
(buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
buf[i] > 0xf4 // > U+10FFFF
) {
return false;
}
i += 4;
} else {
return false;
}
}
return true;
}
try {
let isValidUTF8 = require('utf-8-validate');
/* istanbul ignore if */
if (typeof isValidUTF8 === 'object') {
isValidUTF8 = isValidUTF8.Validation.isValidUTF8; // utf-8-validate@<3.0.0
}
module.exports = {
isValidStatusCode,
isValidUTF8(buf) {
return buf.length < 150 ? _isValidUTF8(buf) : isValidUTF8(buf);
}
};
} catch (e) /* istanbul ignore next */ {
module.exports = {
isValidStatusCode,
isValidUTF8: _isValidUTF8
};
}

View File

@@ -1,449 +0,0 @@
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls|https$" }] */
'use strict';
const EventEmitter = require('events');
const http = require('http');
const https = require('https');
const net = require('net');
const tls = require('tls');
const { createHash } = require('crypto');
const PerMessageDeflate = require('./permessage-deflate');
const WebSocket = require('./websocket');
const { format, parse } = require('./extension');
const { GUID, kWebSocket } = require('./constants');
const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
const RUNNING = 0;
const CLOSING = 1;
const CLOSED = 2;
/**
* Class representing a WebSocket server.
*
* @extends EventEmitter
*/
class WebSocketServer extends EventEmitter {
/**
* Create a `WebSocketServer` instance.
*
* @param {Object} options Configuration options
* @param {Number} [options.backlog=511] The maximum length of the queue of
* pending connections
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
* track clients
* @param {Function} [options.handleProtocols] A hook to handle protocols
* @param {String} [options.host] The hostname where to bind the server
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
* size
* @param {Boolean} [options.noServer=false] Enable no server mode
* @param {String} [options.path] Accept only connections matching this path
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
* permessage-deflate
* @param {Number} [options.port] The port where to bind the server
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
* server to use
* @param {Function} [options.verifyClient] A hook to reject connections
* @param {Function} [callback] A listener for the `listening` event
*/
constructor(options, callback) {
super();
options = {
maxPayload: 100 * 1024 * 1024,
perMessageDeflate: false,
handleProtocols: null,
clientTracking: true,
verifyClient: null,
noServer: false,
backlog: null, // use default (511 as implemented in net.js)
server: null,
host: null,
path: null,
port: null,
...options
};
if (
(options.port == null && !options.server && !options.noServer) ||
(options.port != null && (options.server || options.noServer)) ||
(options.server && options.noServer)
) {
throw new TypeError(
'One and only one of the "port", "server", or "noServer" options ' +
'must be specified'
);
}
if (options.port != null) {
this._server = http.createServer((req, res) => {
const body = http.STATUS_CODES[426];
res.writeHead(426, {
'Content-Length': body.length,
'Content-Type': 'text/plain'
});
res.end(body);
});
this._server.listen(
options.port,
options.host,
options.backlog,
callback
);
} else if (options.server) {
this._server = options.server;
}
if (this._server) {
const emitConnection = this.emit.bind(this, 'connection');
this._removeListeners = addListeners(this._server, {
listening: this.emit.bind(this, 'listening'),
error: this.emit.bind(this, 'error'),
upgrade: (req, socket, head) => {
this.handleUpgrade(req, socket, head, emitConnection);
}
});
}
if (options.perMessageDeflate === true) options.perMessageDeflate = {};
if (options.clientTracking) this.clients = new Set();
this.options = options;
this._state = RUNNING;
}
/**
* Returns the bound address, the address family name, and port of the server
* as reported by the operating system if listening on an IP socket.
* If the server is listening on a pipe or UNIX domain socket, the name is
* returned as a string.
*
* @return {(Object|String|null)} The address of the server
* @public
*/
address() {
if (this.options.noServer) {
throw new Error('The server is operating in "noServer" mode');
}
if (!this._server) return null;
return this._server.address();
}
/**
* Close the server.
*
* @param {Function} [cb] Callback
* @public
*/
close(cb) {
if (cb) this.once('close', cb);
if (this._state === CLOSED) {
process.nextTick(emitClose, this);
return;
}
if (this._state === CLOSING) return;
this._state = CLOSING;
//
// Terminate all associated clients.
//
if (this.clients) {
for (const client of this.clients) client.terminate();
}
const server = this._server;
if (server) {
this._removeListeners();
this._removeListeners = this._server = null;
//
// Close the http server if it was internally created.
//
if (this.options.port != null) {
server.close(emitClose.bind(undefined, this));
return;
}
}
process.nextTick(emitClose, this);
}
/**
* See if a given request should be handled by this server instance.
*
* @param {http.IncomingMessage} req Request object to inspect
* @return {Boolean} `true` if the request is valid, else `false`
* @public
*/
shouldHandle(req) {
if (this.options.path) {
const index = req.url.indexOf('?');
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
if (pathname !== this.options.path) return false;
}
return true;
}
/**
* Handle a HTTP Upgrade request.
*
* @param {http.IncomingMessage} req The request object
* @param {(net.Socket|tls.Socket)} socket The network socket between the
* server and client
* @param {Buffer} head The first packet of the upgraded stream
* @param {Function} cb Callback
* @public
*/
handleUpgrade(req, socket, head, cb) {
socket.on('error', socketOnError);
const key =
req.headers['sec-websocket-key'] !== undefined
? req.headers['sec-websocket-key'].trim()
: false;
const upgrade = req.headers.upgrade;
const version = +req.headers['sec-websocket-version'];
const extensions = {};
if (
req.method !== 'GET' ||
upgrade === undefined ||
upgrade.toLowerCase() !== 'websocket' ||
!key ||
!keyRegex.test(key) ||
(version !== 8 && version !== 13) ||
!this.shouldHandle(req)
) {
return abortHandshake(socket, 400);
}
if (this.options.perMessageDeflate) {
const perMessageDeflate = new PerMessageDeflate(
this.options.perMessageDeflate,
true,
this.options.maxPayload
);
try {
const offers = parse(req.headers['sec-websocket-extensions']);
if (offers[PerMessageDeflate.extensionName]) {
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
}
} catch (err) {
return abortHandshake(socket, 400);
}
}
//
// Optionally call external client verification handler.
//
if (this.options.verifyClient) {
const info = {
origin:
req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
secure: !!(req.socket.authorized || req.socket.encrypted),
req
};
if (this.options.verifyClient.length === 2) {
this.options.verifyClient(info, (verified, code, message, headers) => {
if (!verified) {
return abortHandshake(socket, code || 401, message, headers);
}
this.completeUpgrade(key, extensions, req, socket, head, cb);
});
return;
}
if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
}
this.completeUpgrade(key, extensions, req, socket, head, cb);
}
/**
* Upgrade the connection to WebSocket.
*
* @param {String} key The value of the `Sec-WebSocket-Key` header
* @param {Object} extensions The accepted extensions
* @param {http.IncomingMessage} req The request object
* @param {(net.Socket|tls.Socket)} socket The network socket between the
* server and client
* @param {Buffer} head The first packet of the upgraded stream
* @param {Function} cb Callback
* @throws {Error} If called more than once with the same socket
* @private
*/
completeUpgrade(key, extensions, req, socket, head, cb) {
//
// Destroy the socket if the client has already sent a FIN packet.
//
if (!socket.readable || !socket.writable) return socket.destroy();
if (socket[kWebSocket]) {
throw new Error(
'server.handleUpgrade() was called more than once with the same ' +
'socket, possibly due to a misconfiguration'
);
}
if (this._state > RUNNING) return abortHandshake(socket, 503);
const digest = createHash('sha1')
.update(key + GUID)
.digest('base64');
const headers = [
'HTTP/1.1 101 Switching Protocols',
'Upgrade: websocket',
'Connection: Upgrade',
`Sec-WebSocket-Accept: ${digest}`
];
const ws = new WebSocket(null);
let protocol = req.headers['sec-websocket-protocol'];
if (protocol) {
protocol = protocol.split(',').map(trim);
//
// Optionally call external protocol selection handler.
//
if (this.options.handleProtocols) {
protocol = this.options.handleProtocols(protocol, req);
} else {
protocol = protocol[0];
}
if (protocol) {
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
ws._protocol = protocol;
}
}
if (extensions[PerMessageDeflate.extensionName]) {
const params = extensions[PerMessageDeflate.extensionName].params;
const value = format({
[PerMessageDeflate.extensionName]: [params]
});
headers.push(`Sec-WebSocket-Extensions: ${value}`);
ws._extensions = extensions;
}
//
// Allow external modification/inspection of handshake headers.
//
this.emit('headers', headers, req);
socket.write(headers.concat('\r\n').join('\r\n'));
socket.removeListener('error', socketOnError);
ws.setSocket(socket, head, this.options.maxPayload);
if (this.clients) {
this.clients.add(ws);
ws.on('close', () => this.clients.delete(ws));
}
cb(ws, req);
}
}
module.exports = WebSocketServer;
/**
* Add event listeners on an `EventEmitter` using a map of <event, listener>
* pairs.
*
* @param {EventEmitter} server The event emitter
* @param {Object.<String, Function>} map The listeners to add
* @return {Function} A function that will remove the added listeners when
* called
* @private
*/
function addListeners(server, map) {
for (const event of Object.keys(map)) server.on(event, map[event]);
return function removeListeners() {
for (const event of Object.keys(map)) {
server.removeListener(event, map[event]);
}
};
}
/**
* Emit a `'close'` event on an `EventEmitter`.
*
* @param {EventEmitter} server The event emitter
* @private
*/
function emitClose(server) {
server._state = CLOSED;
server.emit('close');
}
/**
* Handle premature socket errors.
*
* @private
*/
function socketOnError() {
this.destroy();
}
/**
* Close the connection when preconditions are not fulfilled.
*
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
* @param {Number} code The HTTP response status code
* @param {String} [message] The HTTP response body
* @param {Object} [headers] Additional HTTP response headers
* @private
*/
function abortHandshake(socket, code, message, headers) {
if (socket.writable) {
message = message || http.STATUS_CODES[code];
headers = {
Connection: 'close',
'Content-Type': 'text/html',
'Content-Length': Buffer.byteLength(message),
...headers
};
socket.write(
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
Object.keys(headers)
.map((h) => `${h}: ${headers[h]}`)
.join('\r\n') +
'\r\n\r\n' +
message
);
}
socket.removeListener('error', socketOnError);
socket.destroy();
}
/**
* Remove whitespace characters from both ends of a string.
*
* @param {String} str The string
* @return {String} A new string representing `str` stripped of whitespace
* characters from both its beginning and end
* @private
*/
function trim(str) {
return str.trim();
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,56 +0,0 @@
{
"name": "ws",
"version": "7.5.10",
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js",
"keywords": [
"HyBi",
"Push",
"RFC-6455",
"WebSocket",
"WebSockets",
"real-time"
],
"homepage": "https://github.com/websockets/ws",
"bugs": "https://github.com/websockets/ws/issues",
"repository": "websockets/ws",
"author": "Einar Otto Stangvik <einaros@gmail.com> (http://2x.io)",
"license": "MIT",
"main": "index.js",
"browser": "browser.js",
"engines": {
"node": ">=8.3.0"
},
"files": [
"browser.js",
"index.js",
"lib/*.js"
],
"scripts": {
"test": "nyc --reporter=lcov --reporter=text mocha --throw-deprecation test/*.test.js",
"integration": "mocha --throw-deprecation test/*.integration.js",
"lint": "eslint --ignore-path .gitignore . && prettier --check --ignore-path .gitignore \"**/*.{json,md,yaml,yml}\""
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": "^5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
},
"devDependencies": {
"benchmark": "^2.1.4",
"bufferutil": "^4.0.1",
"eslint": "^7.2.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^7.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.5",
"utf-8-validate": "^5.0.2"
}
}

View File

@@ -1 +0,0 @@
.pnpm/chrome-remote-interface@0.33.3/node_modules/chrome-remote-interface

View File

@@ -1 +0,0 @@
.pnpm/node-fetch@2.7.0/node_modules/node-fetch

View File

@@ -1,13 +0,0 @@
{
"name": "cluster-etcd-test",
"version": "1.0.0",
"description": "Tests for Cluster Etcd integration",
"main": "etcd-test-runner.js",
"scripts": {
"test": "node etcd-test-runner.js"
},
"dependencies": {
"chrome-remote-interface": "^0.33.0",
"node-fetch": "^2.6.7"
}
}

View File

@@ -1,82 +0,0 @@
lockfileVersion: '9.0'
settings:
autoInstallPeers: true
excludeLinksFromLockfile: false
importers:
.:
dependencies:
chrome-remote-interface:
specifier: ^0.33.0
version: 0.33.3
node-fetch:
specifier: ^2.6.7
version: 2.7.0
packages:
chrome-remote-interface@0.33.3:
resolution: {integrity: sha512-zNnn0prUL86Teru6UCAZ1yU1XeXljHl3gj7OrfPcarEfU62OUU4IujDPdTDW3dAWwRqN3ZMG/Chhkh2gPL/wiw==}
hasBin: true
commander@2.11.0:
resolution: {integrity: sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==}
node-fetch@2.7.0:
resolution: {integrity: sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==}
engines: {node: 4.x || >=6.0.0}
peerDependencies:
encoding: ^0.1.0
peerDependenciesMeta:
encoding:
optional: true
tr46@0.0.3:
resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==}
webidl-conversions@3.0.1:
resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==}
whatwg-url@5.0.0:
resolution: {integrity: sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==}
ws@7.5.10:
resolution: {integrity: sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==}
engines: {node: '>=8.3.0'}
peerDependencies:
bufferutil: ^4.0.1
utf-8-validate: ^5.0.2
peerDependenciesMeta:
bufferutil:
optional: true
utf-8-validate:
optional: true
snapshots:
chrome-remote-interface@0.33.3:
dependencies:
commander: 2.11.0
ws: 7.5.10
transitivePeerDependencies:
- bufferutil
- utf-8-validate
commander@2.11.0: {}
node-fetch@2.7.0:
dependencies:
whatwg-url: 5.0.0
tr46@0.0.3: {}
webidl-conversions@3.0.1: {}
whatwg-url@5.0.0:
dependencies:
tr46: 0.0.3
webidl-conversions: 3.0.1
ws@7.5.10: {}

View File

@@ -1,27 +0,0 @@
cluster:
localNodeId: "streamnode"
localNodeRole: "worker"
listenAddr: ":8093"
advertiseAddr: "127.0.0.1:8093"
seedNodes: ["127.0.0.1:8090"]
enableMetrics: true
metricInterval: 5
rtmp:
listen: ":1936"
http:
listen: ":8893"
flv:
enable: true
hls:
enable: true
segmentDuration: 5
windowSize: 5
record:
enable: true
folder: "./record"
segmentDuration: 180

View File

@@ -1,168 +0,0 @@
#!/usr/bin/env node
/**
* Cluster 插件测试运行器
* 此脚本用于演示如何使用 CDP 和 Jest 测试 Cluster 插件
*/
const path = require('path');
const { spawn } = require('child_process');
const CDP = require('chrome-remote-interface');
// 测试配置
const TEST_PORT = 9222;
const CONFIG_DIR = path.join(__dirname, '.');
const CONFIG_FILES = {
node1: path.join(CONFIG_DIR, 'node1.yaml'),
node2: path.join(CONFIG_DIR, 'node2.yaml'),
node3: path.join(CONFIG_DIR, 'node3.yaml')
};
// 启动服务器进程
const servers = {};
// 一个简单的延迟函数
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// 启动 Cluster 服务器
async function startServers() {
console.log('启动 Cluster 服务器...');
// 首先启动管理节点
servers.node1 = spawn('go', ['run', '-tags', 'sqlite', 'main.go', '-c', CONFIG_FILES.node1], {
cwd: path.join(__dirname, '../../example/default'),
stdio: 'inherit'
});
// 等待管理节点启动
await delay(5000);
// 启动工作节点
servers.node2 = spawn('go', ['run', '-tags', 'sqlite', 'main.go', '-c', CONFIG_FILES.node2], {
cwd: path.join(__dirname, '../../example/default'),
stdio: 'inherit'
});
servers.node3 = spawn('go', ['run', '-tags', 'sqlite', 'main.go', '-c', CONFIG_FILES.node3], {
cwd: path.join(__dirname, '../../example/default'),
stdio: 'inherit'
});
// 等待所有节点启动
await delay(5000);
console.log('所有 Cluster 服务器已启动');
}
// 连接到 CDP
async function connectToCDP() {
try {
console.log(`连接到 Chrome DevTools Protocol (端口 ${TEST_PORT})...`);
const client = await CDP({ port: TEST_PORT });
const { Network, Page, Runtime } = client;
await Promise.all([
Network.enable(),
Page.enable()
]);
console.log('CDP 连接成功');
return { client, Network, Page, Runtime };
} catch (err) {
console.error('无法连接到 Chrome:', err);
throw err;
}
}
// 运行一个测试用例
async function runTest() {
let client;
try {
const cdp = await connectToCDP();
client = cdp.client;
const { Page, Runtime } = cdp;
console.log('运行集群状态测试...');
// 导航到集群状态页面
await Page.navigate({ url: 'http://localhost:8090/cluster/api/status' });
await Page.loadEventFired();
// 获取集群状态
const result = await Runtime.evaluate({
expression: 'document.body.textContent'
});
const statusText = result.result.value;
const status = JSON.parse(statusText);
// 验证集群状态
console.log('集群状态:', JSON.stringify(status, null, 2));
const nodeCount = Object.keys(status.nodes).length;
if (nodeCount === 3) {
console.log('✅ 测试通过: 集群包含所有预期的节点');
} else {
console.error(`❌ 测试失败: 集群应该包含 3 个节点,但实际有 ${nodeCount}`);
}
if (status.nodes.node1 && status.nodes.node2 && status.nodes.node3) {
console.log('✅ 测试通过: 找到所有预期的节点 ID');
} else {
console.error('❌ 测试失败: 缺少一个或多个预期的节点');
}
} catch (err) {
console.error('测试期间出错:', err);
} finally {
if (client) {
await client.close();
console.log('CDP 客户端已关闭');
}
}
}
// 清理函数
function cleanup() {
console.log('清理资源...');
// 终止所有服务器进程
Object.values(servers).forEach(server => {
if (server) {
server.kill();
}
});
console.log('所有服务器已停止');
}
// 主函数
async function main() {
try {
// 尝试启动服务器
await startServers();
// 运行测试
await runTest();
} catch (err) {
console.error('测试过程中出错:', err);
} finally {
// 清理资源
cleanup();
}
}
// 处理进程终止信号
process.on('SIGINT', () => {
console.log('\n接收到 SIGINT 信号,正在清理...');
cleanup();
process.exit(0);
});
// 运行测试
main().catch(err => {
console.error('未处理的错误:', err);
cleanup();
process.exit(1);
});

View File

@@ -1,441 +0,0 @@
# Cluster 插件测试方案
## 1. 测试框架概述
本测试方案采用以下技术栈:
1. **Chrome DevTools Protocol (CDP)** - 通过 `chrome-remote-interface` 库与网页组件交互
2. **Jest** - 测试运行器
3. **Node.js** - 测试脚本
该框架设计用于测试 Cluster 插件,不直接启动无头浏览器,而是连接到已经运行并启用远程调试的 Chrome 实例。
## 2. 测试环境搭建
### 2.1 前置条件
- 安装 Node.js
- 安装 pnpm
- 安装 Go用于运行 Cluster 服务器)
- Chrome 浏览器已运行并启用远程调试(端口 9222
### 2.2 安装依赖
```bash
cd test
pnpm install
```
### 2.3 运行测试
```bash
# 运行所有测试
pnpm test
# 只运行核心功能测试
pnpm test:core
# 只运行故障恢复测试
pnpm test:failure
```
## 3. 测试配置说明
我们在 `example/cluster-test` 目录下提供了几个测试配置文件:
1. **node1.yaml** - 管理节点配置
2. **node2.yaml** - 工作节点配置
3. **node3.yaml** - 工作节点配置(附带流媒体服务)
4. **stream_test.yaml** - 流测试节点配置
使用这些配置文件可以快速搭建测试环境:
```bash
# 启动管理节点
cd example/default
go run -tags sqlite main.go -c ../cluster-test/node1.yaml
# 启动工作节点2
cd example/default
go run -tags sqlite main.go -c ../cluster-test/node2.yaml
# 启动工作节点3
cd example/default
go run -tags sqlite main.go -c ../cluster-test/node3.yaml
```
## 4. 测试类别
### 4.1 核心功能测试
核心功能测试验证 Cluster 插件的基本功能:
- 集群形成和节点发现
- 流注册和同步
- 负载均衡
- UI 组件和仪表板
示例测试用例:
```javascript
test('集群应该正确连接两个节点', async () => {
// 检查集群状态
const response = await fetch('http://localhost:8090/cluster/api/status');
const status = await response.json();
// 验证集群包含所有节点
expect(Object.keys(status.nodes).length).toBe(3);
expect(status.nodes).toHaveProperty('node1');
expect(status.nodes).toHaveProperty('node2');
expect(status.nodes).toHaveProperty('node3');
});
```
### 4.2 故障恢复测试
故障恢复测试验证插件处理故障和恢复的能力:
- 节点故障检测
- 节点故障期间的流重分配
- 节点恢复处理
- 恢复后的负载重新分配
- 管理节点故障后的领导者选举
示例测试用例:
```javascript
test('应检测到节点故障并更新集群状态', async () => {
// 首先验证所有节点都存在
let response = await fetch('http://localhost:8090/cluster/api/status');
let status = await response.json();
expect(Object.keys(status.nodes).length).toBe(3);
// 关闭节点2模拟故障
server2.kill();
// 等待故障检测(略长于故障检测阈值)
await new Promise(resolve => setTimeout(resolve, 8000));
// 检查节点2是否被标记为故障
response = await fetch('http://localhost:8090/cluster/api/status');
status = await response.json();
expect(status.nodes.node2.status).toBe('failed');
});
```
### 4.3 流测试
流测试验证流媒体功能:
- 流播放测试通过 CDP
- 流存在验证
- 多客户端负载均衡流测试
示例测试用例:
```javascript
test('应该能够成功播放流', async () => {
const result = await testStreamPlayback('http://localhost:8893/flv/live/test');
expect(result.isPlaying).toBe(true);
expect(result.hasError).toBe(false);
});
```
## 5. 实现细节
### 5.1 设置脚本(`setup.js`
- 提供创建配置文件的工具
- 使用 CDP 连接到 Chrome
- 使用特定配置启动 Cluster 服务器
- 等待服务器准备就绪
核心代码:
```javascript
// 连接到CDP
const connectToCDP = async (port = 9222) => {
try {
const client = await CDP({ port });
const { Network, Page, Runtime } = client;
// 启用必要的域
await Promise.all([
Network.enable(),
Page.enable(),
]);
return { client, Network, Page, Runtime };
} catch (err) {
console.error('Cannot connect to Chrome:', err);
throw err;
}
};
// 启动Claster服务器
const startClasterServer = (configPath, options = {}) => {
const serverProcess = spawn('go', ['run', '-tags', 'sqlite', 'main.go', '-c', configPath], {
cwd: path.join(__dirname, '../example/default'),
...options
});
return serverProcess;
};
```
### 5.2 流测试辅助器(`stream_test_helper.js`
提供用于测试流媒体播放的工具,使用 CDP 测试视频播放:
```javascript
const testStreamPlayback = async (url, timeout = 10000) => {
// 连接到CDP
const client = await CDP();
const { Page, Runtime, Network } = client;
// 启用必要的域
await Promise.all([
Page.enable(),
Runtime.enable(),
Network.enable()
]);
try {
// 创建用于测试视频播放的HTML
const testHtml = `
<html>
<head>
<title>流测试</title>
<style>
video { width: 640px; height: 360px; }
</style>
</head>
<body>
<h1>流测试</h1>
<div id="status">加载中...</div>
<video id="player" controls autoplay></video>
<script>
const player = document.getElementById('player');
const status = document.getElementById('status');
let playing = false;
let loaded = false;
let error = false;
// 根据URL类型设置源
if ('${url}'.endsWith('.m3u8')) {
// HLS - 使用hls.js
if (Hls.isSupported()) {
const hls = new Hls();
hls.loadSource('${url}');
hls.attachMedia(player);
hls.on(Hls.Events.MANIFEST_PARSED, function() {
player.play();
});
hls.on(Hls.Events.ERROR, function(event, data) {
error = true;
status.textContent = '错误: ' + data.type + ' - ' + data.details;
});
}
} else if ('${url}'.startsWith('rtmp://')) {
// RTMP - 使用flash或其他兼容元素
status.textContent = 'RTMP测试通过服务器API检查完成';
} else {
// FLV或常规视频
player.src = '${url}';
}
player.addEventListener('playing', () => {
playing = true;
status.textContent = '正在播放';
});
player.addEventListener('loadeddata', () => {
loaded = true;
status.textContent = '已加载';
});
player.addEventListener('error', (e) => {
error = true;
status.textContent = '错误: ' + player.error.code;
});
// 暴露变量用于测试
window.streamTest = {
isPlaying: () => playing,
isLoaded: () => loaded,
hasError: () => error,
getStatus: () => status.textContent
};
</script>
</body>
</html>
`;
// 导航到空白页并注入HTML
await Page.navigate({ url: 'about:blank' });
await Page.loadEventFired();
await Runtime.evaluate({
expression: `document.write(\`${testHtml}\`); document.close();`
});
// 等待视频开始播放或超时
const startTime = Date.now();
let isPlaying = false;
let hasError = false;
while (Date.now() - startTime < timeout && !isPlaying && !hasError) {
// 检查状态
const result = await Runtime.evaluate({
expression: 'window.streamTest && window.streamTest.isPlaying ? window.streamTest.isPlaying() : false'
});
isPlaying = result.result.value;
// 检查错误
const errorResult = await Runtime.evaluate({
expression: 'window.streamTest && window.streamTest.hasError ? window.streamTest.hasError() : false'
});
hasError = errorResult.result.value;
if (!isPlaying && !hasError) {
await new Promise(resolve => setTimeout(resolve, 500));
}
}
// 获取状态
const statusResult = await Runtime.evaluate({
expression: 'window.streamTest && window.streamTest.getStatus ? window.streamTest.getStatus() : "未知"'
});
const status = statusResult.result.value;
return {
isPlaying,
hasError,
status,
timeout: Date.now() - startTime >= timeout
};
} finally {
// 关闭CDP客户端
await client.close();
}
};
```
## 6. 测试 Cluster 插件的最佳实践
1. **隔离性**:每个测试应该独立运行,不依赖于先前测试的状态。
2. **清理资源**:测试后始终清理资源(终止服务器,关闭连接)。
3. **超时设置**:对异步操作使用适当的超时时间,特别是对于可能需要时间的集群操作。
4. **真实场景**:测试真实的故障场景,包括网络分区和节点崩溃。
5. **性能测试**:包括性能测试,确保插件能够处理预期的负载。
6. **UI测试**使用CDP测试API端点和UI组件。
7. **配置测试**:使用不同的配置进行测试,确保插件在各种场景下工作。
## 7. 扩展框架
要测试其他插件或功能:
1. 创建遵循`cluster.test.js``failure_recovery.test.js`模式的新测试文件
2. 添加特定于被测试插件的辅助函数
3. 将新的测试脚本添加到`package.json`
## 8. 在CI/CD中运行
对于持续集成:
1. 启动启用远程调试的Chrome
2. 使用提供的脚本运行测试
3. 收集测试结果并生成报告
## 9. 故障排除
常见问题和解决方案:
- **CDP连接失败**确保Chrome正在运行并在端口9222上启用远程调试
- **服务器启动失败**:检查端口冲突或配置问题
- **超时错误**:增加慢操作或网络问题的超时时间
- **测试不稳定**:为异步操作添加重试或更健壮的等待机制
## 10. 完整测试示例
下面是一个完整的测试示例展示了如何测试Claster插件的集群功能
```javascript
// 测试集群形成
describe('Claster插件核心功能', () => {
test('应该将两个节点连接到一个集群中', async () => {
// 导航到Claster状态页面
const { Page, Runtime } = cdp;
await Page.navigate({ url: 'http://localhost:8090/cluster/api/status' });
await Page.loadEventFired();
// 通过CDP获取集群状态
const result = await Runtime.evaluate({
expression: 'document.body.textContent'
});
const statusText = result.result.value;
const status = JSON.parse(statusText);
// 验证集群包含所有节点
expect(status.nodes).toBeDefined();
expect(Object.keys(status.nodes).length).toBe(3);
expect(status.nodes).toHaveProperty('node1');
expect(status.nodes).toHaveProperty('node2');
expect(status.nodes).toHaveProperty('node3');
});
test('节点间流注册同步', async () => {
// 在node1上创建测试流
const streamData = {
streamPath: 'test/stream1',
publisherNodeId: 'node1',
};
// 注册流
const registerResponse = await fetch('http://localhost:8090/cluster/api/streams/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(streamData)
});
expect(registerResponse.status).toBe(200);
// 等待同步发生
await new Promise(resolve => setTimeout(resolve, 2000));
// 检查流是否在node2上可见
const { Page, Runtime } = cdp;
await Page.navigate({ url: 'http://localhost:8091/cluster/api/streams' });
await Page.loadEventFired();
const result = await Runtime.evaluate({
expression: 'document.body.textContent'
});
const streamsText = result.result.value;
const streams = JSON.parse(streamsText);
// 验证流同步
expect(streams).toContainEqual(
expect.objectContaining({
streamPath: 'test/stream1',
publisherNodeId: 'node1'
})
);
});
});
```
## 结论
这个测试方案为Claster插件提供了全面的测试覆盖包括功能性、故障恢复和性能方面。通过使用Chrome DevTools Protocol我们能够测试包括UI组件和流媒体播放在内的各种场景而无需启动专用的浏览器实例。这种方法特别适合于集成测试环境在这种环境中测试需要与真实的浏览器实例交互。

View File

@@ -8,10 +8,10 @@ srt:
listenaddr: :6000 listenaddr: :6000
passphrase: foobarfoobar passphrase: foobarfoobar
gb28181: gb28181:
enable: false enable: false # 是否启用GB28181协议
autoinvite: false autoinvite: false #建议使用false开启后会自动邀请设备推流
mediaip: 192.168.1.21 #流媒体收流IP mediaip: 192.168.1.21 #流媒体收流IP,外网情况下使用公网IP,内网情况下使用网卡IP,不要用127.0.0.1
sipip: 192.168.1.21 #SIP通讯IP sipip: 192.168.1.21 #SIP通讯IP,不管公网还是内网都使用本机网卡IP,不要用127.0.0.1
sip: sip:
listenaddr: listenaddr:
- udp::5060 - udp::5060

View File

@@ -7,7 +7,6 @@ import (
"m7s.live/v5" "m7s.live/v5"
_ "m7s.live/v5/plugin/cascade" _ "m7s.live/v5/plugin/cascade"
_ "m7s.live/v5/plugin/cluster"
_ "m7s.live/v5/plugin/crypto" _ "m7s.live/v5/plugin/crypto"
_ "m7s.live/v5/plugin/debug" _ "m7s.live/v5/plugin/debug"
_ "m7s.live/v5/plugin/flv" _ "m7s.live/v5/plugin/flv"

50
go.mod
View File

@@ -6,6 +6,7 @@ require (
github.com/IOTechSystems/onvif v1.2.0 github.com/IOTechSystems/onvif v1.2.0
github.com/VictoriaMetrics/VictoriaMetrics v1.102.0 github.com/VictoriaMetrics/VictoriaMetrics v1.102.0
github.com/asavie/xdp v0.3.3 github.com/asavie/xdp v0.3.3
github.com/aws/aws-sdk-go v1.55.7
github.com/beevik/etree v1.4.1 github.com/beevik/etree v1.4.1
github.com/bluenviron/gohlslib v1.4.0 github.com/bluenviron/gohlslib v1.4.0
github.com/c0deltin/duckdb-driver v0.1.0 github.com/c0deltin/duckdb-driver v0.1.0
@@ -50,7 +51,6 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d
google.golang.org/grpc v1.65.0 google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2 google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v2 v2.4.0
gorm.io/driver/mysql v1.5.7 gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.9 gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.11 gorm.io/gorm v1.25.11
@@ -67,30 +67,17 @@ require (
github.com/asticode/go-astits v1.13.0 // indirect github.com/asticode/go-astits v1.13.0 // indirect
github.com/benburkert/openpgp v0.0.0-20160410205803-c2471f86866c // indirect github.com/benburkert/openpgp v0.0.0-20160410205803-c2471f86866c // indirect
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chromedp/cdproto v0.0.0-20240202021202-6d0b6a386732 // indirect github.com/chromedp/cdproto v0.0.0-20240202021202-6d0b6a386732 // indirect
github.com/chromedp/sysutil v1.0.0 // indirect github.com/chromedp/sysutil v1.0.0 // indirect
github.com/clbanning/mxj/v2 v2.7.0 // indirect github.com/clbanning/mxj/v2 v2.7.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/elgs/gostrgen v0.0.0-20220325073726-0c3e00d082f6 // indirect github.com/elgs/gostrgen v0.0.0-20220325073726-0c3e00d082f6 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect github.com/gobwas/pool v0.2.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
@@ -98,9 +85,8 @@ require (
github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect github.com/jinzhu/now v1.1.5 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.18.0 // indirect github.com/klauspost/compress v1.18.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect github.com/mailru/easyjson v0.7.7 // indirect
@@ -108,7 +94,6 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/ncruces/julianday v1.0.0 // indirect github.com/ncruces/julianday v1.0.0 // indirect
@@ -131,13 +116,10 @@ require (
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b // indirect github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect github.com/sirupsen/logrus v1.9.3 // indirect
github.com/soheilhy/cmux v0.1.5 // indirect
github.com/spf13/cast v1.7.1 // indirect github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tetratelabs/wazero v1.8.0 // indirect github.com/tetratelabs/wazero v1.8.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect github.com/tklauser/numcpus v0.6.1 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fastjson v1.6.4 // indirect github.com/valyala/fastjson v1.6.4 // indirect
github.com/valyala/fastrand v1.1.0 // indirect github.com/valyala/fastrand v1.1.0 // indirect
@@ -146,33 +128,11 @@ require (
github.com/valyala/quicktemplate v1.8.0 // indirect github.com/valyala/quicktemplate v1.8.0 // indirect
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
github.com/wlynxg/anet v0.0.5 // indirect github.com/wlynxg/anet v0.0.5 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.etcd.io/bbolt v1.3.8 // indirect
go.etcd.io/etcd/api/v3 v3.5.11 // indirect
go.etcd.io/etcd/client/v2 v2.305.11 // indirect
go.etcd.io/etcd/pkg/v3 v3.5.11 // indirect
go.etcd.io/etcd/raft/v3 v3.5.11 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.20.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.20.0 // indirect
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/sdk v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/arch v0.8.0 // indirect golang.org/x/arch v0.8.0 // indirect
golang.org/x/sync v0.13.0 // indirect golang.org/x/sync v0.13.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto v0.0.0-20240711142825-46eb208f015d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240711142825-46eb208f015d // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240711142825-46eb208f015d // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
) )
require ( require (
@@ -197,9 +157,3 @@ require (
golang.org/x/tools v0.23.0 // indirect golang.org/x/tools v0.23.0 // indirect
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
require (
go.etcd.io/etcd/client/pkg/v3 v3.5.11 // indirect; for transport package
go.etcd.io/etcd/client/v3 v3.5.11
go.etcd.io/etcd/server/v3 v3.5.11
)

Some files were not shown because too many files have changed in this diff Show More