MCPcopy
hub / github.com/vercel/next.js / init

Function init

turbopack/crates/turbopack-cli/js/src/entry/websocket.ts:42–92  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

40 const { timeout = 5 * 1000 } = options
41
42 function init() {
43 if (source) source.close()
44
45 console.log('[HMR] connecting...')
46
47 function handleOnline() {
48 const connected = { type: 'turbopack-connected' as const }
49 messageCallbacks.forEach((cb) => {
50 cb(connected)
51 })
52
53 if (options.log) console.log('[HMR] connected')
54 // lastActivity = Date.now()
55 }
56
57 function handleMessage(event: MessageEvent) {
58 // lastActivity = Date.now()
59
60 const message = {
61 type: 'turbopack-message' as const,
62 data: JSON.parse(event.data),
63 }
64 messageCallbacks.forEach((cb) => {
65 cb(message)
66 })
67 }
68
69 // let timer: NodeJS.Timeout
70
71 function handleDisconnect() {
72 source.close()
73 setTimeout(init, timeout)
74 }
75
76 const { hostname, port } = location
77 const protocol = getSocketProtocol(options.assetPrefix || '')
78 const assetPrefix = options.assetPrefix.replace(/^\/+/, '')
79
80 let url = `${protocol}://${hostname}:${port}${
81 assetPrefix ? `/${assetPrefix}` : ''
82 }`
83
84 if (assetPrefix.startsWith('http')) {
85 url = `${protocol}://${assetPrefix.split('://')[1]}`
86 }
87
88 source = new window.WebSocket(`${url}${options.path}`)
89 source.onopen = handleOnline
90 source.onerror = handleDisconnect
91 source.onmessage = handleMessage
92 }
93
94 init()
95}

Callers 1

connectHMRFunction · 0.70

Calls 6

startsWithMethod · 0.80
splitMethod · 0.80
getSocketProtocolFunction · 0.70
closeMethod · 0.65
replaceMethod · 0.65
logMethod · 0.45

Tested by

no test coverage detected