diff options
| author | yaling888 <[email protected]> | 2022-05-09 07:54:25 +0800 |
|---|---|---|
| committer | yaling888 <[email protected]> | 2022-05-09 07:54:25 +0800 |
| commit | c4eff6250b546b284bd45513f24eaa3722d16117 (patch) | |
| tree | ff2e5a89f7ec5591d94dcbc072aa81a00a173289 /src/api | |
| parent | b5347e624866850a0d9d2c88e24f213fb9654352 (diff) | |
| parent | fd5049ef768202e64b903ddda5c116858d14f5b8 (diff) | |
chore: merge 'tracking' into master
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/connections.ts | 4 | ||||
| -rw-r--r-- | src/api/traffic.ts | 30 |
2 files changed, 18 insertions, 16 deletions
diff --git a/src/api/connections.ts b/src/api/connections.ts index 256e4fd..1e86a86 100644 --- a/src/api/connections.ts +++ b/src/api/connections.ts @@ -21,7 +21,7 @@ export type ConnectionItem = { sourcePort: string; destinationPort: string; host: string; - process: string; + process?: string; }; upload: number; download: number; @@ -30,7 +30,7 @@ export type ConnectionItem = { chains: string[]; // e.g. 'Match', 'DomainKeyword' rule: string; - rulePayload: string; + rulePayload?: string; }; type ConnectionsData = { downloadTotal: number; diff --git a/src/api/traffic.ts b/src/api/traffic.ts index e50ec5e..cd18aac 100644 --- a/src/api/traffic.ts +++ b/src/api/traffic.ts @@ -1,31 +1,33 @@ +import { ClashAPIConfig } from '$src/types'; + import { buildWebSocketURL, getURLAndInit } from '../misc/request-helper'; + const endpoint = '/traffic'; const textDecoder = new TextDecoder('utf-8'); const Size = 150; const traffic = { - labels: Array(Size), - // labels: [], + labels: Array(Size).fill(0), up: Array(Size), down: Array(Size), size: Size, subscribers: [], - appendData(o) { + appendData(o: { up: number; down: number }) { + this.up.shift(); + this.down.shift(); + this.labels.shift(); + + const l = Date.now(); this.up.push(o.up); this.down.push(o.down); - const t = new Date(); - const l = '' + t.getMinutes() + t.getSeconds(); this.labels.push(l); - if (this.up.length > this.size) this.up.shift(); - if (this.down.length > this.size) this.down.shift(); - if (this.labels.length > this.size) this.labels.shift(); this.subscribers.forEach((f) => f(o)); }, - subscribe(listener) { + subscribe(listener: (x:any) => void) { this.subscribers.push(listener); return () => { const idx = this.subscribers.indexOf(listener); @@ -37,11 +39,11 @@ const traffic = { let fetched = false; let decoded = ''; -function parseAndAppend(x) { +function parseAndAppend(x: string) { traffic.appendData(JSON.parse(x)); } -function pump(reader) { +function pump(reader: ReadableStreamDefaultReader) { return reader.read().then(({ done, value }) => { const str = textDecoder.decode(value, { stream: !done }); decoded += str; @@ -73,8 +75,8 @@ function pump(reader) { // other value CLOSED // similar to ws readyState but not the same // https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState -let wsState; -function fetchData(apiConfig) { +let wsState: number; +function fetchData(apiConfig: ClashAPIConfig) { if (fetched || wsState === 1) return traffic; wsState = 1; const url = buildWebSocketURL(apiConfig, endpoint); @@ -92,7 +94,7 @@ function fetchData(apiConfig) { return traffic; } -function fetchDataWithFetch(apiConfig) { +function fetchDataWithFetch(apiConfig: ClashAPIConfig) { if (fetched) return traffic; fetched = true; const { url, init } = getURLAndInit(apiConfig); |
