@@ -6,29 +6,6 @@ const stream = require('node:stream')
66
77const uid = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
88
9- function safeBufferConcat ( chunks , length ) {
10- if ( length === undefined ) {
11- length = 0
12- for ( let i = 0 ; i < chunks . length ; ++ i ) {
13- length += chunks [ i ] . length
14- }
15- }
16- const buffer = Buffer . allocUnsafeSlow ( length )
17-
18- let offset = 0
19- for ( let i = 0 ; i < chunks . length ; ++ i ) {
20- const chunk = chunks [ i ]
21- buffer . set ( chunk , offset )
22- offset += chunk . length
23- }
24-
25- if ( length !== offset ) {
26- buffer . fill ( 0 , offset , buffer . length )
27- }
28-
29- return buffer
30- }
31-
329class ws {
3310 /**
3411 * @param {number } opcode
@@ -72,10 +49,10 @@ class ws {
7249 }
7350
7451 /**
75- * @param {Uint8Array } buffer
52+ * @param {Uint8Array | null } buffer
7653 */
7754 static parseFrame ( buffer ) {
78- if ( buffer . length < 2 ) {
55+ if ( buffer === null || buffer . length < 2 ) {
7956 return null
8057 }
8158
@@ -125,6 +102,7 @@ class ws {
125102 }
126103
127104 static Stream = class extends stream . Writable {
105+ /** @type {Uint8Array | null } */
128106 #head = null
129107 #receivedLength = 0
130108
@@ -133,7 +111,7 @@ class ws {
133111 if ( this . #head === null ) {
134112 this . #head = chunk
135113 } else {
136- this . #head = safeBufferConcat ( [ this . #head, chunk ] )
114+ this . #head = Buffer . concat ( [ this . #head, chunk ] )
137115 }
138116 const head = this . #head
139117 const parsed = ws . parseFrame ( head )
@@ -172,7 +150,7 @@ class ws {
172150 if ( this . #head === null ) {
173151 this . #head = chunk
174152 } else if ( this . #head. length < 2 ) {
175- this . #head = safeBufferConcat ( [ this . #head, chunk ] )
153+ this . #head = Buffer . concat ( [ this . #head, chunk ] )
176154 merged = true
177155 } else {
178156 this . #receivedLength += chunk . length
@@ -187,7 +165,7 @@ class ws {
187165 const start = length - ( parsed . offset + parsed . length )
188166 if ( chunk . length < start ) {
189167 if ( merged ) throw new Error ( 'fatal error' )
190- this . #head = safeBufferConcat ( [ this . #head, chunk ] ) . subarray (
168+ this . #head = Buffer . concat ( [ this . #head, chunk ] ) . subarray (
191169 start
192170 )
193171 } else {
@@ -268,21 +246,21 @@ class ws {
268246
269247 writeFrame ( frame ) {
270248 if ( this . #socket. writable ) {
271- return new Promise ( ( resolve , reject ) => {
249+ return /** @type { Promise<void> } */ ( new Promise ( ( resolve , reject ) => {
272250 this . #socket. write ( frame , ( err ) => {
273251 if ( err ) {
274252 reject ( err )
275253 } else {
276254 resolve ( )
277255 }
278256 } )
279- } )
257+ } ) )
280258 }
281259 }
282260
283261 async close ( ) {
284262 if ( this . #socket. writable ) {
285- await this . writeFrame ( ws . createFrame ( ws . opcode . CLOSE ) )
263+ await this . writeFrame ( ws . createFrame ( ws . opcode . CLOSE , new Uint8Array ( 0 ) ) )
286264 this . #socket. end ( )
287265 }
288266 }
@@ -297,7 +275,9 @@ class ws {
297275 */
298276function setup ( { onConnection, parseBody } ) {
299277 const server = http . createServer ( ( _req , res ) => {
300- res . end ( '' )
278+ // Http handler
279+ res . writeHead ( 404 )
280+ res . end ( '404 Not Found' )
301281 } )
302282
303283 server . on ( 'upgrade' , ( req , socket , _head ) => {
0 commit comments