Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket: use FastBuffer #3213

Merged
merged 1 commit into from
May 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading