@capgo/capacitor-network-diagnostics

中文文档
8.0.15发布于 2026/10/15MPL-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4万

Capacitor plugin for native network diagnostics.

原始使用文档
文档目录18 节
  1. ➡️ Get started for free
  2. Key features
  3. Install
  4. What It Tests
  5. Usage
  6. Combined Diagnostic Run
  7. Platform Notes
  8. API
  9. getNetworkStatus()
  10. testUrl(...)
  11. testPort(...)
  12. testWebSocket(...)
  13. testDownloadSpeed(...)
  14. testPacketLoss(...)
  15. runDiagnostics(...)
  16. getPluginVersion()
  17. Interfaces
  18. Type Aliases

@capgo/capacitor-network-diagnostics

Find out why your Capacitor app cannot reach your backend: test connection type, HTTP reachability, TCP ports, WebSockets, download speed and packet loss from the device itself.

Capgo - Build Capacitor apps with AI, ship updates instantly

Capgo: push fixes to your Capacitor users in minutes, build signed iOS and Android apps without a Mac, and roll back in one click.

➡️ Get started for free

14-day unlimited free trial. No credit card required

🤖 Building with an AI coding agent? Install Capgo skills so Claude Code, Codex, Cursor, Copilot, or Gemini CLI know how to install and use @capgo/capacitor-network-diagnostics in your Capacitor app: npx skills add Cap-go/capgo-skills. Then ask your agent: "Add @capgo/capacitor-network-diagnostics to my Capacitor app and wire it up." Agent setup guides · Hosted MCP

@capgo/capacitor-network-diagnostics for Capacitor apps

Key features

  • Network status: getNetworkStatus() returns the native connection type and network flags.
  • Reachability: testUrl() for HTTP or HTTPS and testPort() for a TCP host and port.
  • WebSockets: testWebSocket() checks that a handshake completes.
  • Performance: testDownloadSpeed() and testPacketLoss().
  • One report: runDiagnostics() runs several checks and returns a compact summary.
  • Platforms: iOS, Android and Web. Web runs the browser-capable checks. TCP port tests need native code.

Install

You can use our AI-Assisted Setup to install the plugin. Add the Capgo skills to your AI tool using the following command:

npx skills add https://github.com/cap-go/capacitor-skills --skill capacitor-plugins

Then use the following prompt:

Use the `capacitor-plugins` skill from `cap-go/capacitor-skills` to install the `@capgo/capacitor-network-diagnostics` plugin in my project.

If you prefer Manual Setup, install the plugin by running the following commands and follow the platform-specific instructions below:

npm install @capgo/capacitor-network-diagnostics
npx cap sync

What It Tests

  • Current native connection type: WiFi, cellular, ethernet, VPN, none, or unknown.
  • OS network flags: validated internet on Android, captive portal on Android, expensive or constrained path where available.
  • Native HTTP/HTTPS URL reachability with status code and latency.
  • Native TCP host:port connectivity.
  • Native WebSocket handshake for ws:// and wss://.
  • Download throughput against your own test file endpoint.
  • Packet loss using repeated TCP connects or HTTP requests.

Raw ICMP ping is not consistently available to App Store and Play Store apps. testPacketLoss therefore measures application-level loss with TCP or HTTP probes.

Usage

import { NetworkDiagnostics } from '@capgo/capacitor-network-diagnostics';

const status = await NetworkDiagnostics.getNetworkStatus();

const api = await NetworkDiagnostics.testUrl({
  url: 'https://api.example.com/health',
  method: 'HEAD',
  timeoutMs: 5000,
});

const port = await NetworkDiagnostics.testPort({
  host: 'api.example.com',
  port: 443,
  timeoutMs: 3000,
});

const ws = await NetworkDiagnostics.testWebSocket({
  url: 'wss://ws.example.com/socket',
  timeoutMs: 5000,
});

const packetLoss = await NetworkDiagnostics.testPacketLoss({
  mode: 'tcp',
  host: 'api.example.com',
  port: 443,
  count: 10,
});

console.log({ status, api, port, ws, packetLoss });

Combined Diagnostic Run

const report = await NetworkDiagnostics.runDiagnostics({
  urls: [{ url: 'https://api.example.com/health' }],
  ports: [{ host: 'api.example.com', port: 443 }],
  websockets: [{ url: 'wss://ws.example.com/socket' }],
  download: {
    url: 'https://speed.example.com/5mb.bin',
    maxBytes: 5 * 1024 * 1024,
  },
  packetLoss: {
    mode: 'tcp',
    host: 'api.example.com',
    port: 443,
    count: 10,
  },
});

console.log(report.issues);

Platform Notes

  • iOS: no extra permissions are required. Connection type comes from Network.framework.
  • Android: the plugin declares android.permission.INTERNET and android.permission.ACCESS_NETWORK_STATE.
  • Web: provided as a development fallback. Browsers cannot open raw TCP sockets, and URL checks are limited by CORS.

API

Native network diagnostics API.

getNetworkStatus()

getNetworkStatus() => Promise<NetworkStatusResult>

Read the current native connection type and platform network flags.

Returns: Promise<NetworkStatusResult>


testUrl(...)

testUrl(options: UrlTestOptions) => Promise<UrlTestResult>

Test whether an HTTP or HTTPS URL can be reached from native networking.

ParamType
optionsUrlTestOptions

Returns: Promise<UrlTestResult>


testPort(...)

testPort(options: PortTestOptions) => Promise<PortTestResult>

Test whether a TCP host:port can be opened from native networking.

ParamType
optionsPortTestOptions

Returns: Promise<PortTestResult>


testWebSocket(...)

testWebSocket(options: WebSocketTestOptions) => Promise<WebSocketTestResult>

Test whether a WebSocket URL can complete its native handshake.

ParamType
optionsWebSocketTestOptions

Returns: Promise<WebSocketTestResult>


testDownloadSpeed(...)

testDownloadSpeed(options: DownloadSpeedTestOptions) => Promise<DownloadSpeedTestResult>

Measure download throughput from a native HTTP request.

ParamType
optionsDownloadSpeedTestOptions

Returns: Promise<DownloadSpeedTestResult>


testPacketLoss(...)

testPacketLoss(options: PacketLossTestOptions) => Promise<PacketLossTestResult>

Estimate application-level packet loss with repeated TCP or HTTP probes.

ParamType
optionsPacketLossTestOptions

Returns: Promise<PacketLossTestResult>


runDiagnostics(...)

runDiagnostics(options?: RunDiagnosticsOptions | undefined) => Promise<RunDiagnosticsResult>

Run several diagnostics and return a compact issue list.

ParamType
optionsRunDiagnosticsOptions

Returns: Promise<RunDiagnosticsResult>


getPluginVersion()

getPluginVersion() => Promise<PluginVersionResult>

Returns the platform implementation version marker.

Returns: Promise<PluginVersionResult>


Interfaces

NetworkStatusResult

Current native network state.

PropTypeDescription
connectedbooleanTrue when the platform reports an active network path.
connectionTypeConnectionTypeBest-effort active transport type.
internetReachablebooleanTrue when the OS marks the network as internet-capable or validated.
expensivebooleanTrue for metered or expensive network paths.
constrainedbooleanTrue when the OS reports a low-data or constrained network path.
captivePortalbooleanTrue when Android reports captive portal capability.
detailsRecord<string, string | number | boolean>Native platform details useful for debugging.

UrlTestResult

Native HTTP URL reachability result.

PropType
urlstring
methodUrlTestMethod
okboolean
reachableboolean
durationMsnumber
statusCodenumber
finalUrlstring
errorCodestring
errorMessagestring

UrlTestOptions

Options for native HTTP URL reachability checks.

PropTypeDescription
urlstringHTTP or HTTPS URL to test.
methodUrlTestMethodHTTP method. Defaults to HEAD.
timeoutMsnumberRequest timeout in milliseconds. Defaults to 10000.
followRedirectsbooleanFollow redirects. Defaults to true.

PortTestResult

Native TCP port check result.

PropType
hoststring
portnumber
openboolean
durationMsnumber
errorCodestring
errorMessagestring

PortTestOptions

Options for native TCP port checks.

PropTypeDescription
hoststringHostname or IP address.
portnumberTCP port to open.
timeoutMsnumberSocket timeout in milliseconds. Defaults to 5000.

WebSocketTestResult

Native WebSocket handshake result.

PropType
urlstring
openboolean
durationMsnumber
protocolstring
statusCodenumber
errorCodestring
errorMessagestring

WebSocketTestOptions

Options for native WebSocket handshake checks.

PropTypeDescription
urlstringws:// or wss:// URL to test.
timeoutMsnumberHandshake timeout in milliseconds. Defaults to 10000.

DownloadSpeedTestResult

Native download speed measurement result.

PropType
urlstring
okboolean
durationMsnumber
bytesDownloadednumber
bytesPerSecondnumber
mbpsnumber
statusCodenumber
errorCodestring
errorMessagestring

DownloadSpeedTestOptions

Options for native download speed measurement.

PropTypeDescription
urlstringHTTP or HTTPS URL returning a downloadable body.
maxBytesnumberMaximum bytes to read before stopping. Defaults to 5242880 (5 MiB).
timeoutMsnumberRequest timeout in milliseconds. Defaults to 30000.

PacketLossTestResult

Application-level packet loss result.

PropType
modePacketLossMode
targetstring
sentnumber
receivednumber
lostnumber
lossPercentnumber
averageLatencyMsnumber
minLatencyMsnumber
maxLatencyMsnumber
errorCodestring
errorMessagestring

PacketLossTestOptions

Options for packet loss measurement.

Native apps cannot rely on raw ICMP ping on both iOS and Android, so this method measures application-level loss with repeated TCP connects or HTTP requests.

PropTypeDescription
modePacketLossModeProbe mode. Defaults to tcp when host/port is provided, otherwise http.
hoststringHostname or IP address for TCP probes.
portnumberTCP port for TCP probes.
urlstringHTTP or HTTPS URL for HTTP probes.
countnumberNumber of probes to send. Defaults to 10.
timeoutMsnumberPer-probe timeout in milliseconds. Defaults to 3000.
intervalMsnumberDelay between probes in milliseconds. Defaults to 250.

RunDiagnosticsResult

Combined native network diagnostic result.

PropType
statusNetworkStatusResult
urlsUrlTestResult[]
portsPortTestResult[]
websocketsWebSocketTestResult[]
issuesstring[]
downloadDownloadSpeedTestResult
packetLossPacketLossTestResult

RunDiagnosticsOptions

Options for a combined native network diagnostic run.

PropType
urlsUrlTestOptions[]
portsPortTestOptions[]
websocketsWebSocketTestOptions[]
downloadDownloadSpeedTestOptions
packetLossPacketLossTestOptions

PluginVersionResult

Plugin version payload.

PropTypeDescription
versionstringVersion identifier returned by the platform implementation.

Type Aliases

ConnectionType

'none' | 'wifi' | 'cellular' | 'ethernet' | 'vpn' | 'other' | 'unknown'

Record

Construct a type with a set of properties K of type T

{ [P in K]: T; }

UrlTestMethod

'HEAD' | 'GET'

PacketLossMode

'tcp' | 'http'

版本与发布记录

@capgo/capacitor-network-diagnostics 共发布 15 个版本,首次发布于 2026/05/30;过去 12 个月发布了 15 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.0.15latest2026/10/1525 天前
8.0.142026/10/1327 天前
8.0.132026/10/091 个月前
8.0.122026/10/081 个月前
8.0.112026/10/081 个月前
8.0.102026/09/161 个月前

关于 @capgo/capacitor-network-diagnostics 的常见问题

@capgo/capacitor-network-diagnostics 是什么?

Capacitor plugin for native network diagnostics.

如何安装 @capgo/capacitor-network-diagnostics?

在项目目录运行 npm install @capgo/capacitor-network-diagnostics,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@capacitor/core(>=8.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@capgo/capacitor-network-diagnostics 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@capgo/capacitor-network-diagnostics 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@capgo/capacitor-network-diagnostics 可以商用吗?

@capgo/capacitor-network-diagnostics 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@capgo/capacitor-network-diagnostics 的下载量有多少?

根据 npm 官方下载统计,@capgo/capacitor-network-diagnostics 在 2026/10/09 – 2026/11/07 这 30 天内被下载 39,707 次,日均约 1,324 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@capgo/capacitor-network-diagnostics 还在维护吗?

最新版本 8.0.15 发布于 2026/10/15(25 天前);至今共发布 15 个版本,其中过去 12 个月发布了 15 个;首次发布于 2026/05/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。