Skip to content

Commit

Permalink
websocket: use FastBuffer (#3213)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx authored May 7, 2024
1 parent 7150bed commit 18c0df2
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { webidl } = require('../fetch/webidl')
const { URLSerializer } = require('../fetch/data-url')
const { getGlobalOrigin } = require('../fetch/global')
const { environmentSettingsObject } = require('../fetch/util')
const { staticPropertyDescriptors, states, sentCloseFrameState, opcodes } = require('./constants')
const {
kWebSocketURL,
Expand Down Expand Up @@ -30,6 +30,8 @@ const { ErrorEvent } = require('./events')

let experimentalWarned = false

const FastBuffer = Buffer[Symbol.species]

// https://websockets.spec.whatwg.org/#interface-definition
class WebSocket extends EventTarget {
#events = {
Expand Down Expand Up @@ -66,7 +68,7 @@ class WebSocket extends EventTarget {
protocols = options.protocols

// 1. Let baseURL be this's relevant settings object's API base URL.
const baseURL = getGlobalOrigin()
const baseURL = environmentSettingsObject.settingsObject.baseUrl

// 1. Let urlRecord be the result of applying the URL parser to url with baseURL.
let urlRecord
Expand Down Expand Up @@ -262,7 +264,7 @@ class WebSocket extends EventTarget {
// increase the bufferedAmount attribute by the length of the
// ArrayBuffer in bytes.

const value = Buffer.from(data)
const value = new FastBuffer(data)
const frame = new WebsocketFrameSend(value)
const buffer = frame.createFrame(opcodes.BINARY)

Expand All @@ -283,7 +285,7 @@ class WebSocket extends EventTarget {
// not throw an exception must increase the bufferedAmount attribute
// by the length of data’s buffer in bytes.

const ab = Buffer.from(data, data.byteOffset, data.byteLength)
const ab = new FastBuffer(data, data.byteOffset, data.byteLength)

const frame = new WebsocketFrameSend(ab)
const buffer = frame.createFrame(opcodes.BINARY)
Expand All @@ -307,7 +309,7 @@ class WebSocket extends EventTarget {
const frame = new WebsocketFrameSend()

data.arrayBuffer().then((ab) => {
const value = Buffer.from(ab)
const value = new FastBuffer(ab)
frame.frameData = value
const buffer = frame.createFrame(opcodes.BINARY)

Expand Down

0 comments on commit 18c0df2

Please sign in to comment.