Skip to content

Commit 3c3dd5c

Browse files
committed
fix: websocket error
1 parent ec49e4e commit 3c3dd5c

File tree

8 files changed

+60
-34
lines changed

8 files changed

+60
-34
lines changed

packages/network-debugger/src/core/request.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { RequestDetail } from '../common'
44
import { getTimestamp } from '../utils'
55
import { MainProcess } from './fork'
66
import { BINARY_TYPES } from './ws/constants'
7-
import { Receiver } from './ws/reveiver'
7+
import { Receiver } from './ws/receiver'
88

99
export interface RequestFn {
1010
(options: RequestOptions | string | URL, callback?: (res: IncomingMessage) => void): ClientRequest
@@ -38,14 +38,14 @@ function proxyClientRequestFactory(
3838
})
3939

4040
if (requestDetail.isWebSocket()) {
41-
actualRequest.on('upgrade', (res: IncomingMessage, socket: Socket, head: Buffer) => {
41+
actualRequest.on('upgrade', async (res: IncomingMessage, socket: Socket, head: Buffer) => {
4242
const originalWrite = socket.write
4343

4444
if (requestDetail.isHiden()) {
4545
return
4646
}
4747

48-
mainProcess.send({
48+
await mainProcess.send({
4949
type: 'Network.webSocketCreated',
5050
data: {
5151
requestId: requestDetail.id,

packages/network-debugger/src/fork/module/common.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ const resetPluginContext = () => (currentPluginContext = null)
1717
export interface CoreCotext {
1818
devtool: DevtoolServer
1919
core: RequestCenter
20+
plugins: PluginInstance<any>[]
2021
}
2122

22-
export type EffectCleaner = () => void
23-
export type PluginHandler = (props: CoreCotext) => EffectCleaner | void
24-
export type PluginInstance = (props: CoreCotext) => EffectCleaner | void
23+
export type PluginHandler<T> = (props: CoreCotext) => T
24+
export type PluginInstance<T> = {
25+
(props: CoreCotext): T
26+
id: string
27+
}
2528

2629
/**
2730
* @description create a plugin for devtool
@@ -48,13 +51,15 @@ export type PluginInstance = (props: CoreCotext) => EffectCleaner | void
4851
* ```
4952
* @returns PluginInstance
5053
*/
51-
export const createPlugin = (fn: PluginHandler): PluginInstance => {
52-
return (props: CoreCotext) => {
54+
export const createPlugin = <T>(id: string, fn: PluginHandler<T>) => {
55+
const plugin: Omit<PluginInstance<T>, 'id'> = (props: CoreCotext) => {
5356
initPluginContext(props.core, props.devtool)
54-
const effectDestroy = fn(props)
57+
const output = fn(props)
5558
resetPluginContext()
56-
return effectDestroy
59+
return output
5760
}
61+
const instance = Object.assign(plugin, { id })
62+
return instance as PluginInstance<T>
5863
}
5964

6065
/**

packages/network-debugger/src/fork/module/debugger/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface ScriptSourceData {
1111
scriptId: string
1212
}
1313

14-
export const debuggerPlugin = createPlugin(({ devtool, core }) => {
14+
export const debuggerPlugin = createPlugin('debugger', ({ devtool, core }) => {
1515
useHandler<ScriptSourceData>('Debugger.getScriptSource', ({ id, data }) => {
1616
if (!id) {
1717
return

packages/network-debugger/src/fork/module/network/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { toMimeType } from '../../devtool'
88
const frameId = '517.528'
99
const loaderId = '517.529'
1010

11-
export const networkPlugin = createPlugin(({ devtool, core }) => {
11+
export const networkPlugin = createPlugin('network', ({ devtool, core }) => {
1212
const requests: Record<string, RequestDetail> = {}
1313

1414
const getRequest = (id: string) => requests[id]
@@ -214,4 +214,10 @@ export const networkPlugin = createPlugin(({ devtool, core }) => {
214214
})
215215
}
216216
})
217+
218+
return {
219+
getRequest
220+
}
217221
})
222+
223+
export type NetworkPluginCore = ReturnType<typeof networkPlugin>

packages/network-debugger/src/fork/module/websocket/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
stringifyNestedObj
88
} from '../../../utils'
99
import { createPlugin, useHandler } from '../common'
10+
import { NetworkPluginCore } from '../network'
1011

1112
export interface WebSocketFrameSent {
1213
requestId: string
@@ -29,10 +30,16 @@ export interface WebSocketCreated {
2930
response: IncomingMessage
3031
}
3132

32-
export const websocketPlugin = createPlugin(({ devtool }) => {
33+
export const websocketPlugin = createPlugin('websocket', ({ devtool, core }) => {
34+
const networkPlugin = core.usePlugin<NetworkPluginCore>('network')
35+
3336
useHandler<WebSocketCreated>(
3437
'Network.webSocketCreated',
35-
async ({ request, data: { response } }) => {
38+
async ({ data: { response, requestId } }) => {
39+
if (!requestId) {
40+
return
41+
}
42+
const request = networkPlugin.getRequest(requestId)
3643
if (!request) {
3744
return
3845
}
@@ -99,8 +106,8 @@ export const websocketPlugin = createPlugin(({ devtool }) => {
99106
})
100107
})
101108

102-
useHandler<WebSocketFrameSent>('Network.webSocketFrameReceived', async ({ request, data }) => {
103-
if (!request) {
109+
useHandler<WebSocketFrameSent>('Network.webSocketFrameReceived', async ({ data }) => {
110+
if (!data.requestId) {
104111
return
105112
}
106113
await devtool.send({
@@ -113,14 +120,14 @@ export const websocketPlugin = createPlugin(({ devtool }) => {
113120
})
114121
})
115122

116-
useHandler<WebSocketFrameSent>('Network.webSocketClosed', async ({ request }) => {
117-
if (!request) {
123+
useHandler<WebSocketFrameSent>('Network.webSocketClosed', async ({ data }) => {
124+
if (!data.requestId) {
118125
return
119126
}
120127
await devtool.send({
121128
method: 'Network.webSocketClosed',
122129
params: {
123-
requestId: request.id,
130+
requestId: data.requestId,
124131
timestamp: getTimestamp()
125132
}
126133
})

packages/network-debugger/src/fork/request-center.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { DevtoolServer } from './devtool'
22
import { PORT, READY_MESSAGE, RequestDetail } from '../common'
3-
import zlib from 'node:zlib'
43
import { Server } from 'ws'
54
import { log } from '../utils'
65
import { ResourceService } from './resource-service'
7-
import { EffectCleaner, PluginInstance } from './module/common'
6+
import { PluginInstance } from './module/common'
87

98
export interface RequestCenterInitOptions {
109
port: number
@@ -26,7 +25,6 @@ export class RequestCenter {
2625
public resourceService: ResourceService
2726
private devtool: DevtoolServer
2827
private server: Server
29-
private effects: Array<EffectCleaner> = []
3028
private listeners: Record<string, Set<DevtoolMessageListener> | undefined> = {}
3129
private options: RequestCenterInitOptions
3230
constructor(options: RequestCenterInitOptions) {
@@ -67,18 +65,29 @@ export class RequestCenter {
6765
this.server = this.initServer()
6866
}
6967

70-
#plugins: PluginInstance[] = []
71-
public loadPlugins(plugins: PluginInstance[]) {
68+
#plugins: PluginInstance<any>[] = []
69+
#pluginOutputs?: Map<string, any>
70+
public loadPlugins(plugins: PluginInstance<any>[]) {
7271
this.#plugins = plugins
73-
const effects = plugins
74-
.map((plugin) =>
75-
plugin({
76-
devtool: this.devtool,
77-
core: this
78-
})
79-
)
80-
.filter(Boolean) as Array<EffectCleaner>
81-
this.effects.push(...effects)
72+
this.#pluginOutputs = new Map()
73+
74+
plugins.forEach((plugin) => {
75+
const output = plugin({
76+
devtool: this.devtool,
77+
core: this,
78+
plugins: this.#plugins
79+
})
80+
this.#pluginOutputs!.set(plugin.id, output)
81+
})
82+
}
83+
84+
public usePlugin<T = null>(id: string) {
85+
if (!this.#pluginOutputs) {
86+
return null as T
87+
}
88+
89+
const output = this.#pluginOutputs.get(id)
90+
return output as T
8291
}
8392

8493
public on(method: string, listener: DevtoolMessageListener) {
@@ -94,7 +103,6 @@ export class RequestCenter {
94103
public close() {
95104
this.server.close()
96105
this.devtool.close()
97-
this.effects.forEach((effect) => effect())
98106
}
99107

100108
private initServer() {

packages/network-debugger/src/fork/tests/pluginTester.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)