@hyper-fetch/plugin-devtools 9.9.0 โ 10.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +107 -50
- package/__tests__/devtools/devtools.browser.spec.ts +18 -0
- package/__tests__/devtools/devtools.edge-cases.spec.ts +278 -0
- package/__tests__/devtools/devtools.event-handler.spec.ts +427 -0
- package/__tests__/devtools/devtools.observers.spec.ts +255 -0
- package/__tests__/devtools/devtools.sockets.spec.ts +209 -0
- package/dist/devtools.event.handler.d.ts +8 -2
- package/dist/devtools.event.handler.d.ts.map +1 -1
- package/dist/devtools.observers-reporter.d.ts +19 -0
- package/dist/devtools.observers-reporter.d.ts.map +1 -0
- package/dist/devtools.socket-reporter.d.ts +20 -0
- package/dist/devtools.socket-reporter.d.ts.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +234 -18
- package/dist/index.mjs.map +1 -1
- package/dist/types/events.types.d.ts +70 -1
- package/dist/types/events.types.d.ts.map +1 -1
- package/dist/types/plugin.types.d.ts +12 -0
- package/dist/types/plugin.types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/devtools.event.handler.ts +57 -10
- package/src/devtools.observers-reporter.ts +102 -0
- package/src/devtools.socket-reporter.ts +85 -0
- package/src/index.ts +2 -0
- package/src/types/events.types.ts +71 -0
- package/src/types/plugin.types.ts +13 -0
- package/tsconfig.json +1 -0
- package/__tests__/devtools/index.spec.ts +0 -42
package/README.md
CHANGED
|
@@ -1,95 +1,152 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://hyperfetch.bettertyped.com/docs/integrations/plugin-devtools"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/.github/assets/packages/plugin-devtools.png" alt="Hyper Fetch Devtools Plugin. See every request. As it happens. One plugin streams requests, cache and queues from your app to the HyperFlow desktop devtools." /></a>
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
5
|
<p align="center">
|
|
4
|
-
<
|
|
6
|
+
<a href="https://hyperfetch.bettertyped.com/docs/hyper-flow/connecting"><b>Quick Start</b></a> ยท
|
|
7
|
+
<a href="https://hyperfetch.bettertyped.com/docs/integrations/plugin-devtools">Documentation</a> ยท
|
|
8
|
+
<a href="https://hyperfetch.bettertyped.com/docs/api/plugin-devtools/Functions/DevtoolsPlugin">API Reference</a> ยท
|
|
9
|
+
<a href="https://github.com/BetterTyped/hyper-fetch">GitHub</a> ยท
|
|
10
|
+
<a href="https://discord.gg/7upC2kVmyM">Discord</a>
|
|
5
11
|
</p>
|
|
6
12
|
|
|
7
13
|
<p align="center">
|
|
8
|
-
<a href="https://
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
<a href="https://
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
<a href="https://github.com/BetterTyped/hyper-fetch/blob/main/License.md">
|
|
15
|
-
<img src="https://custom-icon-badges.demolab.com/github/license/BetterTyped/hyper-fetch?logo=law&color=yellow" />
|
|
16
|
-
</a>
|
|
17
|
-
<a href="https://www.npmjs.com/package/@hyper-fetch/plugin-devtools">
|
|
18
|
-
<img src="https://custom-icon-badges.demolab.com/npm/v/@hyper-fetch/plugin-devtools.svg?logo=npm&color=e76f51" />
|
|
19
|
-
</a>
|
|
20
|
-
<a href="https://www.npmjs.com/package/@hyper-fetch/plugin-devtools">
|
|
21
|
-
<img src="https://custom-icon-badges.demolab.com/npm/dm/@hyper-fetch/plugin-devtools?logoColor=fff&logo=trending-up" />
|
|
22
|
-
</a>
|
|
23
|
-
<a href="https://github.com/BetterTyped/hyper-fetch">
|
|
24
|
-
<img src="https://custom-icon-badges.demolab.com/badge/typescript-%23007ACC.svg?logo=typescript&logoColor=white" />
|
|
25
|
-
</a>
|
|
26
|
-
<a href="https://bundlephobia.com/package/@hyper-fetch/plugin-devtools">
|
|
27
|
-
<img src="https://custom-icon-badges.demolab.com/bundlephobia/minzip/@hyper-fetch/plugin-devtools?color=64BC4B&logo=package" />
|
|
28
|
-
</a>
|
|
14
|
+
<a href="https://github.com/BetterTyped/hyper-fetch"><img src="https://img.shields.io/github/stars/BetterTyped/hyper-fetch?style=flat&logo=github&color=333" alt="GitHub stars" /></a>
|
|
15
|
+
<a href="https://www.npmjs.com/package/@hyper-fetch/plugin-devtools"><img src="https://img.shields.io/npm/v/@hyper-fetch/plugin-devtools?logo=npm&color=e76f51" alt="npm version" /></a>
|
|
16
|
+
<a href="https://www.npmjs.com/package/@hyper-fetch/plugin-devtools"><img src="https://api.maciejpyrc.com/portfolio/npm/downloads.svg?packages=@hyper-fetch/plugin-devtools&color=fbc646" alt="Monthly downloads" /></a>
|
|
17
|
+
<a href="https://bundlejs.com/?q=@hyper-fetch/plugin-devtools"><img src="https://api.maciejpyrc.com/portfolio/npm/size.svg?package=@hyper-fetch/plugin-devtools&external=@hyper-fetch/core,@hyper-fetch/sockets" alt="Bundle size" /></a>
|
|
18
|
+
<a href="https://github.com/BetterTyped/hyper-fetch"><img src="https://img.shields.io/badge/TypeScript-strict-3178C6?logo=typescript&logoColor=white" alt="TypeScript" /></a>
|
|
19
|
+
<a href="https://hyperfetch.bettertyped.com/docs/integrations/plugin-devtools"><img src="https://img.shields.io/badge/HyperFlow-devtools-F59E0B" alt="Integration" /></a>
|
|
20
|
+
<a href="https://github.com/BetterTyped/hyper-fetch/blob/main/License.md"><img src="https://img.shields.io/github/license/BetterTyped/hyper-fetch?color=blue" alt="License" /></a>
|
|
29
21
|
</p>
|
|
30
22
|
|
|
31
23
|
## ๐ About
|
|
32
24
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
and
|
|
25
|
+
`@hyper-fetch/plugin-devtools` connects a Hyper Fetch client to [HyperFlow](https://github.com/BetterTyped/hyper-fetch/tree/main/packages/hyper-flow),
|
|
26
|
+
the desktop devtools. It streams every request, cache change and queue update from your app over a local socket, and
|
|
27
|
+
lets HyperFlow edit the cache and control queues while you debug. Add it in development and keep it out of production.
|
|
36
28
|
|
|
37
29
|
## ๐ฏ Key Capabilities
|
|
38
30
|
|
|
39
|
-
- ๐ฎ **See every request live**
|
|
40
|
-
- ๐ **Watch
|
|
41
|
-
-
|
|
42
|
-
-
|
|
43
|
-
-
|
|
31
|
+
- ๐ฎ **See every request live** - Method, endpoint, payload, response, status and timing as they happen
|
|
32
|
+
- ๐ **Watch the cache change** - Entries appear, update, go stale and get invalidated in front of you
|
|
33
|
+
- ๐ฆ **Follow your queues** - Pending, running, paused and canceled requests per queue
|
|
34
|
+
- ๐ก **Inspect sockets too** - Pass `sockets: [socket]` and HyperFlow shows connections, messages and errors
|
|
35
|
+
- ๐ **Find slow endpoints** - Response times and statistics collected per endpoint
|
|
36
|
+
- ๐งช **Debug by editing** - Change cache entries and control queues from HyperFlow, your hooks re-render with it
|
|
37
|
+
- ๐งญ **Every adapter** - fetch, Axios, GraphQL, tRPC, oRPC and Firebase requests all show up the same way
|
|
38
|
+
- ๐ท๏ธ **Many apps, one window** - One `appName` per client, each shown as its own connection
|
|
44
39
|
|
|
45
40
|
## ๐ Quick Start
|
|
46
41
|
|
|
47
42
|
```bash
|
|
48
|
-
npm install @hyper-fetch/plugin-devtools @hyper-fetch/sockets
|
|
43
|
+
npm install -D @hyper-fetch/plugin-devtools @hyper-fetch/sockets
|
|
49
44
|
```
|
|
50
45
|
|
|
51
46
|
```ts
|
|
52
47
|
import { createClient } from "@hyper-fetch/core";
|
|
53
|
-
import { DevtoolsPlugin } from "@hyper-fetch/plugin-devtools";
|
|
54
48
|
|
|
55
|
-
const client = createClient({ url: "https://api.example.com" })
|
|
49
|
+
export const client = createClient({ url: "https://api.example.com" });
|
|
50
|
+
|
|
51
|
+
// A build-time flag drops the plugin and its chunk from production builds
|
|
52
|
+
if (process.env.NODE_ENV === "development") {
|
|
53
|
+
void import("@hyper-fetch/plugin-devtools").then(({ DevtoolsPlugin }) => {
|
|
54
|
+
client.addPlugin(DevtoolsPlugin({ appName: "Web App" }));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
56
57
|
```
|
|
57
58
|
|
|
59
|
+
Then [download HyperFlow](https://hyperfetch.bettertyped.com/docs/hyper-flow/download), start it, and run your app.
|
|
60
|
+
|
|
58
61
|
## ๐ Documentation
|
|
59
62
|
|
|
60
|
-
- [
|
|
61
|
-
- [HyperFlow](https://hyperfetch.bettertyped.com/docs/hyper-flow)
|
|
62
|
-
- [
|
|
63
|
+
- **[Devtools Plugin](https://hyperfetch.bettertyped.com/docs/integrations/plugin-devtools)** - Install, options and production safety
|
|
64
|
+
- **[Connecting HyperFlow](https://hyperfetch.bettertyped.com/docs/hyper-flow/connecting)** - Ports, environments and multiple apps
|
|
65
|
+
- **[HyperFlow](https://hyperfetch.bettertyped.com/docs/hyper-flow)** - Network, cache, queue and socket inspectors
|
|
66
|
+
- **[Adapter compatibility](https://hyperfetch.bettertyped.com/docs/hyper-flow/compatibility)** - What works with each adapter
|
|
67
|
+
- **[API Reference](https://hyperfetch.bettertyped.com/docs/api/plugin-devtools/Functions/DevtoolsPlugin)** - Every option and type
|
|
63
68
|
|
|
64
69
|
## ๐ก Examples
|
|
65
70
|
|
|
66
|
-
###
|
|
71
|
+
### Vite
|
|
67
72
|
|
|
68
73
|
```ts
|
|
69
74
|
import { createClient } from "@hyper-fetch/core";
|
|
70
|
-
import { DevtoolsPlugin } from "@hyper-fetch/plugin-devtools";
|
|
71
75
|
|
|
72
|
-
const client = createClient({ url:
|
|
76
|
+
export const client = createClient({ url: import.meta.env.VITE_API_URL });
|
|
73
77
|
|
|
74
|
-
if (
|
|
75
|
-
|
|
76
|
-
DevtoolsPlugin({
|
|
77
|
-
|
|
78
|
-
}),
|
|
79
|
-
);
|
|
78
|
+
if (import.meta.env.DEV) {
|
|
79
|
+
void import("@hyper-fetch/plugin-devtools").then(({ DevtoolsPlugin }) => {
|
|
80
|
+
client.addPlugin(DevtoolsPlugin({ appName: "Web App", environment: "development" }));
|
|
81
|
+
});
|
|
80
82
|
}
|
|
81
83
|
```
|
|
82
84
|
|
|
83
|
-
###
|
|
85
|
+
### Several clients side by side
|
|
84
86
|
|
|
85
87
|
```ts
|
|
86
|
-
|
|
88
|
+
// One plugin and one appName per client, so HyperFlow shows them as separate connections
|
|
89
|
+
apiClient.addPlugin(DevtoolsPlugin({ appName: "API" }));
|
|
90
|
+
authClient.addPlugin(DevtoolsPlugin({ appName: "Auth" }));
|
|
91
|
+
graphqlClient.addPlugin(DevtoolsPlugin({ appName: "GraphQL" }));
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Inspect sockets
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { Socket } from "@hyper-fetch/sockets";
|
|
98
|
+
|
|
99
|
+
const socket = new Socket({ url: "ws://localhost:3000" });
|
|
100
|
+
|
|
101
|
+
client.addPlugin(DevtoolsPlugin({ appName: "My App", sockets: [socket] }));
|
|
102
|
+
```
|
|
87
103
|
|
|
88
|
-
|
|
89
|
-
|
|
104
|
+
### A custom HyperFlow port
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
client.addPlugin(
|
|
108
|
+
DevtoolsPlugin({
|
|
109
|
+
appName: "My App",
|
|
110
|
+
url: "ws://localhost:4000", // the default is ws://localhost:2137
|
|
111
|
+
debug: true, // log connection events through the client logger
|
|
112
|
+
}),
|
|
90
113
|
);
|
|
91
114
|
```
|
|
92
115
|
|
|
116
|
+
> **Never ship the plugin to production.** It sends headers, payloads, responses and cache contents to whatever listens
|
|
117
|
+
> on its URL, and accepts commands that change your cache. Gate it on a build-time constant and import it dynamically.
|
|
118
|
+
|
|
119
|
+
## ๐งฉ Hyper Fetch Ecosystem
|
|
120
|
+
|
|
121
|
+
One typed client for every backend. Swap the adapter and keep the same `createRequest()` everywhere.
|
|
122
|
+
|
|
123
|
+
<table align="center">
|
|
124
|
+
<tr>
|
|
125
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/core"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-hyper-fetch.svg" width="36" height="36" alt="" /><br /><sub><b>Core</b></sub></a></td>
|
|
126
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/react"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-react.svg" width="36" height="36" alt="" /><br /><sub><b>React</b></sub></a></td>
|
|
127
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/sockets"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-sockets.svg" width="36" height="36" alt="" /><br /><sub><b>Sockets</b></sub></a></td>
|
|
128
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/cli"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-openapi.svg" width="36" height="36" alt="" /><br /><sub><b>OpenAPI CLI</b></sub></a></td>
|
|
129
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/adapter-graphql"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-graphql.svg" width="36" height="36" alt="" /><br /><sub><b>GraphQL</b></sub></a></td>
|
|
130
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/adapter-trpc"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-trpc.svg" width="36" height="36" alt="" /><br /><sub><b>tRPC</b></sub></a></td>
|
|
131
|
+
</tr>
|
|
132
|
+
<tr>
|
|
133
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/adapter-orpc"><picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-orpc-dark.svg" /><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-orpc.svg" width="36" height="36" alt="" /></picture><br /><sub><b>oRPC</b></sub></a></td>
|
|
134
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/hono"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-hono.svg" width="36" height="36" alt="" /><br /><sub><b>Hono</b></sub></a></td>
|
|
135
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/elysia"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-elysia.svg" width="36" height="36" alt="" /><br /><sub><b>Elysia</b></sub></a></td>
|
|
136
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/adapter-firebase"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-firebase.svg" width="36" height="36" alt="" /><br /><sub><b>Firebase</b></sub></a></td>
|
|
137
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/adapter-axios"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/integration-axios.svg" width="36" height="36" alt="" /><br /><sub><b>Axios</b></sub></a></td>
|
|
138
|
+
<td align="center" width="110"><a href="https://github.com/BetterTyped/hyper-fetch/tree/main/packages/hyper-flow"><img src="https://raw.githubusercontent.com/BetterTyped/hyper-fetch/main/documentation/static/img/hyper-flow.svg" width="36" height="36" alt="" /><br /><sub><b>HyperFlow</b></sub></a></td>
|
|
139
|
+
</tr>
|
|
140
|
+
</table>
|
|
141
|
+
|
|
142
|
+
## ๐ Sponsors
|
|
143
|
+
|
|
144
|
+
<p align="center">
|
|
145
|
+
<a href="https://github.com/sponsors/prc5">
|
|
146
|
+
<img src="https://raw.githubusercontent.com/prc5/sponsors/main/packages/other/sponsorkit/sponsors.svg?raw=true" alt="Sponsors" />
|
|
147
|
+
</a>
|
|
148
|
+
</p>
|
|
149
|
+
|
|
93
150
|
## License
|
|
94
151
|
|
|
95
152
|
[Apache-2.0](https://github.com/BetterTyped/hyper-fetch/blob/main/License.md)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { Client } from "@hyper-fetch/core";
|
|
3
|
+
|
|
4
|
+
import { DevtoolsPlugin } from "../../src/index";
|
|
5
|
+
|
|
6
|
+
describe("DevtoolsPlugin [ Browser ]", () => {
|
|
7
|
+
it("should leave the WebSocket to the browser", async () => {
|
|
8
|
+
const plugin = DevtoolsPlugin({ appName: "App", url: "ws://localhost:1" });
|
|
9
|
+
new Client({ url: "http://localhost:5000" }).addPlugin(plugin);
|
|
10
|
+
const socket = plugin.data.eventHandler!.socket;
|
|
11
|
+
|
|
12
|
+
// Without an option the sockets adapter takes the global WebSocket
|
|
13
|
+
expect(socket.adapter.adapterOptions?.WebSocket).toBeUndefined();
|
|
14
|
+
|
|
15
|
+
socket.reconnectAttempts = 0;
|
|
16
|
+
await socket.disconnect();
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { Client } from "@hyper-fetch/core";
|
|
2
|
+
import type { EmitterInstance, SocketInstance } from "@hyper-fetch/sockets";
|
|
3
|
+
import { Socket } from "@hyper-fetch/sockets";
|
|
4
|
+
import type { AddressInfo } from "net";
|
|
5
|
+
import type { WebSocket } from "ws";
|
|
6
|
+
import { WebSocketServer } from "ws";
|
|
7
|
+
|
|
8
|
+
import type { BaseMessagePayload, DevtoolsSocketEventData } from "../../src/index";
|
|
9
|
+
import {
|
|
10
|
+
createSocketReporter,
|
|
11
|
+
DevtoolsPlugin,
|
|
12
|
+
EventSourceType,
|
|
13
|
+
InternalEvents,
|
|
14
|
+
MessageType,
|
|
15
|
+
SocketEvents,
|
|
16
|
+
SocketTopics,
|
|
17
|
+
} from "../../src/index";
|
|
18
|
+
|
|
19
|
+
const createHyperFlowServer = async () => {
|
|
20
|
+
// A random port can be taken by another process (EADDRINUSE never emits "listening"), the system picks a free one
|
|
21
|
+
const wss = new WebSocketServer({ port: 0 });
|
|
22
|
+
await new Promise<void>((resolve) => {
|
|
23
|
+
wss.once("listening", () => resolve());
|
|
24
|
+
});
|
|
25
|
+
const { port } = wss.address() as AddressInfo;
|
|
26
|
+
const received: { data: BaseMessagePayload }[] = [];
|
|
27
|
+
let plugin: WebSocket | null = null;
|
|
28
|
+
wss.on("connection", (ws) => {
|
|
29
|
+
plugin = ws;
|
|
30
|
+
ws.on("message", (raw) => received.push(JSON.parse(raw.toString())));
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
port,
|
|
34
|
+
received,
|
|
35
|
+
initializeApp: () =>
|
|
36
|
+
plugin?.send(
|
|
37
|
+
JSON.stringify({
|
|
38
|
+
topic: SocketTopics.PLUGIN_LISTENER,
|
|
39
|
+
data: { messageType: MessageType.INTERNAL, eventType: InternalEvents.APP_INITIALIZED },
|
|
40
|
+
}),
|
|
41
|
+
),
|
|
42
|
+
events: () => received.map(({ data }) => data).filter((data) => data.messageType === MessageType.EVENT),
|
|
43
|
+
close: async () => {
|
|
44
|
+
wss.clients.forEach((ws) => ws.terminate());
|
|
45
|
+
await new Promise((resolve) => {
|
|
46
|
+
wss.close(resolve);
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
describe("DevtoolsPlugin [ Edge cases ]", () => {
|
|
53
|
+
let hyperFlow: Awaited<ReturnType<typeof createHyperFlowServer>>;
|
|
54
|
+
let plugin: ReturnType<typeof DevtoolsPlugin>;
|
|
55
|
+
let client: Client;
|
|
56
|
+
let socket: Socket;
|
|
57
|
+
|
|
58
|
+
beforeEach(async () => {
|
|
59
|
+
hyperFlow = await createHyperFlowServer();
|
|
60
|
+
socket = new Socket({ url: "ws://localhost:1", reconnect: 0, adapterOptions: { autoConnect: false } });
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
afterEach(async () => {
|
|
64
|
+
const devtoolsSocket = plugin?.data.eventHandler?.socket;
|
|
65
|
+
if (devtoolsSocket) {
|
|
66
|
+
devtoolsSocket.reconnectAttempts = 0;
|
|
67
|
+
await devtoolsSocket.disconnect();
|
|
68
|
+
}
|
|
69
|
+
vi.restoreAllMocks();
|
|
70
|
+
await hyperFlow.close();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const createPlugin = (options: Partial<Parameters<typeof DevtoolsPlugin>[0]> = {}) => {
|
|
74
|
+
plugin = DevtoolsPlugin({ appName: "App", url: `ws://localhost:${hyperFlow.port}`, sockets: [socket], ...options });
|
|
75
|
+
client = new Client({ url: "http://localhost:5000" });
|
|
76
|
+
client.addPlugin(plugin);
|
|
77
|
+
return plugin;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const initialize = async () => {
|
|
81
|
+
await vi.waitFor(() => expect(plugin.data.eventHandler?.isConnected).toBe(true));
|
|
82
|
+
hyperFlow.initializeApp();
|
|
83
|
+
await vi.waitFor(() => expect(plugin.data.eventHandler?.isInitialized).toBe(true));
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const socketEvents = () =>
|
|
87
|
+
hyperFlow
|
|
88
|
+
.events()
|
|
89
|
+
.filter((event) => event.eventSource === EventSourceType.SOCKET)
|
|
90
|
+
.map((event) => ({ name: event.eventName as SocketEvents, data: event.eventData as DevtoolsSocketEventData }));
|
|
91
|
+
|
|
92
|
+
describe("when data can't be sent", () => {
|
|
93
|
+
const createCircular = () => {
|
|
94
|
+
const circular: Record<string, unknown> = { name: "loop" };
|
|
95
|
+
circular.self = circular;
|
|
96
|
+
return circular;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
it("should not break the app or HyperFlow when a live event can't be serialized", async () => {
|
|
100
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
101
|
+
createPlugin();
|
|
102
|
+
await initialize();
|
|
103
|
+
|
|
104
|
+
expect(() => socket.events.emitListenerEvent({ topic: "loop", data: createCircular(), extra: {} })).not.toThrow();
|
|
105
|
+
socket.events.emitListenerEvent({ topic: "after", data: { ok: true }, extra: {} });
|
|
106
|
+
|
|
107
|
+
await vi.waitFor(() => expect(socketEvents().some((event) => event.data.topic === "after")).toBe(true));
|
|
108
|
+
expect(socketEvents().some((event) => event.data.topic === "loop")).toBe(false);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should keep flushing queued events after one that can't be serialized", async () => {
|
|
112
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
113
|
+
createPlugin();
|
|
114
|
+
|
|
115
|
+
// Queued while HyperFlow has not confirmed the handshake yet
|
|
116
|
+
socket.events.emitListenerEvent({ topic: "before", data: 1, extra: {} });
|
|
117
|
+
socket.events.emitListenerEvent({ topic: "loop", data: createCircular(), extra: {} });
|
|
118
|
+
socket.events.emitListenerEvent({ topic: "after", data: 2, extra: {} });
|
|
119
|
+
await initialize();
|
|
120
|
+
|
|
121
|
+
await vi.waitFor(() => {
|
|
122
|
+
const topics = socketEvents().map((event) => event.data.topic);
|
|
123
|
+
expect(topics).toContain("before");
|
|
124
|
+
expect(topics).toContain("after");
|
|
125
|
+
});
|
|
126
|
+
expect(plugin.data.eventHandler?.eventQueue).toHaveLength(0);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("should never let a failing connection to HyperFlow throw into the app", async () => {
|
|
130
|
+
const logged = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
131
|
+
createPlugin();
|
|
132
|
+
await initialize();
|
|
133
|
+
vi.spyOn(plugin.data.eventHandler!.socketEmitter, "emit").mockImplementation(() => {
|
|
134
|
+
throw new Error("Connection broke");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
expect(() => socket.events.emitListenerEvent({ topic: "chat", data: 1, extra: {} })).not.toThrow();
|
|
138
|
+
expect(() => client.cache.events.emitInvalidation("key")).not.toThrow();
|
|
139
|
+
expect(logged).toHaveBeenCalled();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("should send binary data in a form that survives JSON", async () => {
|
|
143
|
+
createPlugin();
|
|
144
|
+
await initialize();
|
|
145
|
+
|
|
146
|
+
socket.events.emitListenerEvent({ topic: "binary", data: new Uint8Array([1, 2, 3]), extra: {} });
|
|
147
|
+
|
|
148
|
+
await vi.waitFor(() => expect(socketEvents().some((event) => event.data.topic === "binary")).toBe(true));
|
|
149
|
+
expect(socketEvents().find((event) => event.data.topic === "binary")?.data.data).toStrictEqual({
|
|
150
|
+
0: 1,
|
|
151
|
+
1: 2,
|
|
152
|
+
2: 3,
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("when sockets report", () => {
|
|
158
|
+
it("should report reconnection attempts and giving up", async () => {
|
|
159
|
+
createPlugin();
|
|
160
|
+
await initialize();
|
|
161
|
+
|
|
162
|
+
socket.events.emitReconnecting({ attempts: 0 });
|
|
163
|
+
socket.events.emitReconnectingFailed({ attempts: 3 });
|
|
164
|
+
|
|
165
|
+
await vi.waitFor(() => {
|
|
166
|
+
const names = socketEvents().map((event) => event.name);
|
|
167
|
+
expect(names).toEqual(expect.arrayContaining([SocketEvents.RECONNECTING, SocketEvents.RECONNECT_FAILED]));
|
|
168
|
+
});
|
|
169
|
+
expect(socketEvents().find((event) => event.name === SocketEvents.RECONNECTING)?.data.attempts).toBe(0);
|
|
170
|
+
expect(socketEvents().find((event) => event.name === SocketEvents.RECONNECT_FAILED)?.data.attempts).toBe(3);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("should report messages that failed to send with their payload and error", async () => {
|
|
174
|
+
createPlugin();
|
|
175
|
+
await initialize();
|
|
176
|
+
const emitter = socket.createEmitter<{ text: string }>()({ topic: "chat" }).setPayload({ text: "lost" });
|
|
177
|
+
|
|
178
|
+
socket.events.emitEmitterError({ error: new TypeError("Socket is closed"), emitter: emitter as EmitterInstance });
|
|
179
|
+
|
|
180
|
+
await vi.waitFor(() =>
|
|
181
|
+
expect(socketEvents().some((event) => event.name === SocketEvents.MESSAGE_FAILED)).toBe(true),
|
|
182
|
+
);
|
|
183
|
+
expect(socketEvents().find((event) => event.name === SocketEvents.MESSAGE_FAILED)?.data).toMatchObject({
|
|
184
|
+
topic: "chat",
|
|
185
|
+
data: { text: "lost" },
|
|
186
|
+
error: { name: "TypeError", message: "Socket is closed" },
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it.each([
|
|
191
|
+
["an Error", new RangeError("Out of range"), { name: "RangeError", message: "Out of range" }],
|
|
192
|
+
["a DOM event", new Event("error"), { name: "Event", message: "error" }],
|
|
193
|
+
["a plain object", { code: 1006 }, { code: 1006 }],
|
|
194
|
+
["a string", "closed", "closed"],
|
|
195
|
+
])("should report %s as an error HyperFlow can read", async (_name, error, expected) => {
|
|
196
|
+
createPlugin();
|
|
197
|
+
await initialize();
|
|
198
|
+
|
|
199
|
+
socket.events.emitError({ error });
|
|
200
|
+
|
|
201
|
+
await vi.waitFor(() => expect(socketEvents().some((event) => event.name === SocketEvents.ERROR)).toBe(true));
|
|
202
|
+
expect(socketEvents().find((event) => event.name === SocketEvents.ERROR)?.data.error).toStrictEqual(expected);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("should not report a connecting state that ended", async () => {
|
|
206
|
+
createPlugin();
|
|
207
|
+
await initialize();
|
|
208
|
+
|
|
209
|
+
socket.events.emitConnecting({ connecting: false });
|
|
210
|
+
socket.events.emitConnecting({ connecting: true });
|
|
211
|
+
|
|
212
|
+
await vi.waitFor(() => expect(socketEvents().some((event) => event.name === SocketEvents.CONNECTING)).toBe(true));
|
|
213
|
+
expect(socketEvents().filter((event) => event.name === SocketEvents.CONNECTING)).toHaveLength(1);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe("given a socket reporter", () => {
|
|
218
|
+
it("should stop reporting once unmounted", () => {
|
|
219
|
+
const report = vi.fn();
|
|
220
|
+
const reporter = createSocketReporter({ socket: socket as SocketInstance, socketId: "chat", report });
|
|
221
|
+
|
|
222
|
+
socket.events.emitConnected();
|
|
223
|
+
reporter.unmount();
|
|
224
|
+
socket.events.emitConnected();
|
|
225
|
+
socket.events.emitListenerEvent({ topic: "x", data: 1, extra: {} });
|
|
226
|
+
|
|
227
|
+
expect(report).toHaveBeenCalledOnce();
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("should describe the current state in its snapshot", () => {
|
|
231
|
+
const report = vi.fn();
|
|
232
|
+
const reporter = createSocketReporter({ socket: socket as SocketInstance, socketId: "chat", report });
|
|
233
|
+
|
|
234
|
+
socket.adapter.setConnecting(true);
|
|
235
|
+
reporter.sendSnapshot();
|
|
236
|
+
socket.adapter.setConnecting(false);
|
|
237
|
+
socket.adapter.setConnected(true);
|
|
238
|
+
reporter.sendSnapshot();
|
|
239
|
+
|
|
240
|
+
expect(report.mock.calls.map(([, data]) => data.status)).toStrictEqual(["connecting", "connected"]);
|
|
241
|
+
expect(report.mock.calls[0][1]).toMatchObject({
|
|
242
|
+
socketId: "chat",
|
|
243
|
+
url: "ws://localhost:1",
|
|
244
|
+
adapterName: "websockets",
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe("given debug mode", () => {
|
|
250
|
+
it("should log the connection to HyperFlow", async () => {
|
|
251
|
+
createPlugin({ debug: true });
|
|
252
|
+
const info = vi.spyOn(plugin.data.eventHandler!.logger, "info");
|
|
253
|
+
const error = vi.spyOn(plugin.data.eventHandler!.logger, "error");
|
|
254
|
+
await initialize();
|
|
255
|
+
|
|
256
|
+
// What the transport calls on a connection error
|
|
257
|
+
plugin.data.eventHandler!.socket.unstable_onErrorCallbacks.forEach((callback) =>
|
|
258
|
+
callback({ error: new Error("Lost") }),
|
|
259
|
+
);
|
|
260
|
+
await plugin.data.eventHandler!.socket.disconnect();
|
|
261
|
+
|
|
262
|
+
await vi.waitFor(() => {
|
|
263
|
+
expect(info.mock.calls.map(([log]) => log.title)).toEqual(expect.arrayContaining(["Disconnected"]));
|
|
264
|
+
expect(error.mock.calls.map(([log]) => log.title)).toContain("Error");
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("should stay quiet without debug mode", async () => {
|
|
269
|
+
createPlugin();
|
|
270
|
+
const info = vi.spyOn(plugin.data.eventHandler!.logger, "info");
|
|
271
|
+
await initialize();
|
|
272
|
+
|
|
273
|
+
await plugin.data.eventHandler!.socket.disconnect();
|
|
274
|
+
|
|
275
|
+
expect(info).not.toHaveBeenCalled();
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
});
|