{"version":3,"file":"sender.mjs","sources":["../../../../lib/web/websocket/sender.js"],"sourcesContent":["'use strict'\n\nconst { WebsocketFrameSend } = require('./frame')\nconst { opcodes, sendHints } = require('./constants')\nconst FixedQueue = require('../../dispatcher/fixed-queue')\n\n/**\n * @typedef {object} SendQueueNode\n * @property {Promise<void> | null} promise\n * @property {((...args: any[]) => any)} callback\n * @property {Buffer | null} frame\n */\n\nclass SendQueue {\n  /**\n   * @type {FixedQueue}\n   */\n  #queue = new FixedQueue()\n\n  /**\n   * @type {boolean}\n   */\n  #running = false\n\n  /** @type {import('node:net').Socket} */\n  #socket\n\n  constructor (socket) {\n    this.#socket = socket\n  }\n\n  add (item, cb, hint) {\n    if (hint !== sendHints.blob) {\n      if (!this.#running) {\n        // TODO(@tsctx): support fast-path for string on running\n        if (hint === sendHints.text) {\n          // special fast-path for string\n          const { 0: head, 1: body } = WebsocketFrameSend.createFastTextFrame(item)\n          this.#socket.cork()\n          this.#socket.write(head)\n          this.#socket.write(body, cb)\n          this.#socket.uncork()\n        } else {\n          // direct writing\n          this.#socket.write(createFrame(item, hint), cb)\n        }\n      } else {\n        /** @type {SendQueueNode} */\n        const node = {\n          promise: null,\n          callback: cb,\n          frame: createFrame(item, hint)\n        }\n        this.#queue.push(node)\n      }\n      return\n    }\n\n    /** @type {SendQueueNode} */\n    const node = {\n      promise: item.arrayBuffer().then((ab) => {\n        node.promise = null\n        node.frame = createFrame(ab, hint)\n      }),\n      callback: cb,\n      frame: null\n    }\n\n    this.#queue.push(node)\n\n    if (!this.#running) {\n      this.#run()\n    }\n  }\n\n  async #run () {\n    this.#running = true\n    const queue = this.#queue\n    while (!queue.isEmpty()) {\n      const node = queue.shift()\n      // wait pending promise\n      if (node.promise !== null) {\n        await node.promise\n      }\n      // write\n      this.#socket.write(node.frame, node.callback)\n      // cleanup\n      node.callback = node.frame = null\n    }\n    this.#running = false\n  }\n}\n\nfunction createFrame (data, hint) {\n  return new WebsocketFrameSend(toBuffer(data, hint)).createFrame(hint === sendHints.text ? opcodes.TEXT : opcodes.BINARY)\n}\n\nfunction toBuffer (data, hint) {\n  switch (hint) {\n    case sendHints.text:\n    case sendHints.typedArray:\n      return new Uint8Array(data.buffer, data.byteOffset, data.byteLength)\n    case sendHints.arrayBuffer:\n    case sendHints.blob:\n      return new Uint8Array(data)\n  }\n}\n\nmodule.exports = { SendQueue }\n"],"names":["WebsocketFrameSend","require$$0","opcodes","sendHints","require$$1","FixedQueue","require$$2","createFrame","data","hint","text","typedArray","Uint8Array","buffer","byteOffset","byteLength","arrayBuffer","blob","toBuffer","TEXT","BINARY","sender","SendQueue","queue","running","socket","constructor","this","add","item","cb","node","promise","callback","frame","push","head","body","createFastTextFrame","cork","write","uncork","then","ab","run","isEmpty","shift"],"mappings":"yRAEA,MAAMA,oBAAyBC,gBACzBC,QAASC,WAAgBC,mBACzBC,WAAaC,WAyFnB,SAASC,YAAaC,KAAMC,MAC1B,OAAO,IAAIT,mBAGb,SAAmBQ,KAAMC,MACvB,OAAQA,MACN,KAAKN,UAAUO,KACf,KAAKP,UAAUQ,WACb,OAAO,IAAIC,WAAWJ,KAAKK,OAAQL,KAAKM,WAAYN,KAAKO,YAC3D,KAAKZ,UAAUa,YACf,KAAKb,UAAUc,KACb,OAAO,IAAIL,WAAWJ,MAE5B,CAZgCU,CAASV,KAAMC,OAAOF,YAAYE,OAASN,UAAUO,KAAOR,QAAQiB,KAAOjB,QAAQkB,OACnH,QAaAC,OAAiB,CAAEC;;;;;;;AA/FnB;;;;AAIEC,OAAS,IAAIlB;;;OAKbmB,UAAW;yCAGXC,QAEA,WAAAC,CAAaD,QACXE,MAAKF,OAAUA,MACnB,CAEE,GAAAG,CAAKC,KAAMC,GAAIrB,MACb,GAAIA,OAASN,UAAUc,KAAM,CAC3B,GAAKU,MAAKH,QAaH;;AAEL,MAAMO,KAAO,CACXC,QAAS,KACTC,SAAUH,GACVI,MAAO3B,YAAYsB,KAAMpB,OAE3BkB,MAAKJ,MAAOY,KAAKJ,K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