diff --git a/examples/basic-video-chat/p2pcf.js b/examples/basic-video-chat/p2pcf.js index 9bae268..7e63c5c 100644 --- a/examples/basic-video-chat/p2pcf.js +++ b/examples/basic-video-chat/p2pcf.js @@ -1905,134 +1905,11 @@ var require_buffer = __commonJS({ } }); -// node_modules/process/browser.js -var require_browser = __commonJS({ - "node_modules/process/browser.js"(exports, module) { +// node_modules/process-fast/index.js +var require_process_fast = __commonJS({ + "node_modules/process-fast/index.js"(exports, module) { init_esbuild_inject(); var process2 = module.exports = {}; - var cachedSetTimeout; - var cachedClearTimeout; - function defaultSetTimout() { - throw new Error("setTimeout has not been defined"); - } - function defaultClearTimeout() { - throw new Error("clearTimeout has not been defined"); - } - (function() { - try { - if (typeof setTimeout === "function") { - cachedSetTimeout = setTimeout; - } else { - cachedSetTimeout = defaultSetTimout; - } - } catch (e) { - cachedSetTimeout = defaultSetTimout; - } - try { - if (typeof clearTimeout === "function") { - cachedClearTimeout = clearTimeout; - } else { - cachedClearTimeout = defaultClearTimeout; - } - } catch (e) { - cachedClearTimeout = defaultClearTimeout; - } - })(); - function runTimeout(fun) { - if (cachedSetTimeout === setTimeout) { - return setTimeout(fun, 0); - } - if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { - cachedSetTimeout = setTimeout; - return setTimeout(fun, 0); - } - try { - return cachedSetTimeout(fun, 0); - } catch (e) { - try { - return cachedSetTimeout.call(null, fun, 0); - } catch (e2) { - return cachedSetTimeout.call(this, fun, 0); - } - } - } - function runClearTimeout(marker) { - if (cachedClearTimeout === clearTimeout) { - return clearTimeout(marker); - } - if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { - cachedClearTimeout = clearTimeout; - return clearTimeout(marker); - } - try { - return cachedClearTimeout(marker); - } catch (e) { - try { - return cachedClearTimeout.call(null, marker); - } catch (e2) { - return cachedClearTimeout.call(this, marker); - } - } - } - var queue = []; - var draining = false; - var currentQueue; - var queueIndex = -1; - function cleanUpNextTick() { - if (!draining || !currentQueue) { - return; - } - draining = false; - if (currentQueue.length) { - queue = currentQueue.concat(queue); - } else { - queueIndex = -1; - } - if (queue.length) { - drainQueue(); - } - } - function drainQueue() { - if (draining) { - return; - } - var timeout = runTimeout(cleanUpNextTick); - draining = true; - var len = queue.length; - while (len) { - currentQueue = queue; - queue = []; - while (++queueIndex < len) { - if (currentQueue) { - currentQueue[queueIndex].run(); - } - } - queueIndex = -1; - len = queue.length; - } - currentQueue = null; - draining = false; - runClearTimeout(timeout); - } - process2.nextTick = function(fun) { - var args = new Array(arguments.length - 1); - if (arguments.length > 1) { - for (var i = 1; i < arguments.length; i++) { - args[i - 1] = arguments[i]; - } - } - queue.push(new Item(fun, args)); - if (queue.length === 1 && !draining) { - runTimeout(drainQueue); - } - }; - function Item(fun, array) { - this.fun = fun; - this.array = array; - } - Item.prototype.run = function() { - this.fun.apply(null, this.array); - }; process2.title = "browser"; process2.browser = true; process2.env = {}; @@ -2050,21 +1927,16 @@ var require_browser = __commonJS({ process2.emit = noop; process2.prependListener = noop; process2.prependOnceListener = noop; - process2.listeners = function(name) { - return []; - }; - process2.binding = function(name) { + process2.nextTick = (func, ...args) => window.queueMicrotask(() => func(...args)); + process2.listeners = (name) => []; + process2.cwd = () => "/"; + process2.umask = () => 0; + process2.binding = (name) => { throw new Error("process.binding is not supported"); }; - process2.cwd = function() { - return "/"; - }; - process2.chdir = function(dir) { + process2.chdir = (dir) => { throw new Error("process.chdir is not supported"); }; - process2.umask = function() { - return 0; - }; } }); @@ -2073,7 +1945,7 @@ var Buffer2, process; var init_esbuild_inject = __esm({ "esbuild.inject.js"() { Buffer2 = require_buffer().Buffer; - process = require_browser(); + process = require_process_fast(); } }); @@ -2586,9 +2458,9 @@ var require_ms = __commonJS({ } }); -// node_modules/simple-peer/node_modules/debug/src/common.js +// node_modules/debug/src/common.js var require_common = __commonJS({ - "node_modules/simple-peer/node_modules/debug/src/common.js"(exports, module) { + "node_modules/debug/src/common.js"(exports, module) { init_esbuild_inject(); function setup(env) { createDebug.debug = createDebug; @@ -2750,9 +2622,9 @@ var require_common = __commonJS({ } }); -// node_modules/simple-peer/node_modules/debug/src/browser.js -var require_browser2 = __commonJS({ - "node_modules/simple-peer/node_modules/debug/src/browser.js"(exports, module) { +// node_modules/debug/src/browser.js +var require_browser = __commonJS({ + "node_modules/debug/src/browser.js"(exports, module) { init_esbuild_inject(); exports.formatArgs = formatArgs; exports.save = save; @@ -2936,7 +2808,6 @@ var require_safe_buffer = __commonJS({ function SafeBuffer(arg, encodingOrOffset, length) { return Buffer3(arg, encodingOrOffset, length); } - SafeBuffer.prototype = Object.create(Buffer3.prototype); copyProps(Buffer3, SafeBuffer); SafeBuffer.from = function(arg, encodingOrOffset, length) { if (typeof arg === "number") { @@ -2976,7 +2847,7 @@ var require_safe_buffer = __commonJS({ }); // node_modules/randombytes/browser.js -var require_browser3 = __commonJS({ +var require_browser2 = __commonJS({ "node_modules/randombytes/browser.js"(exports, module) { "use strict"; init_esbuild_inject(); @@ -3542,7 +3413,7 @@ var require_inherits_browser = __commonJS({ }); // node_modules/util-deprecate/browser.js -var require_browser4 = __commonJS({ +var require_browser3 = __commonJS({ "node_modules/util-deprecate/browser.js"(exports, module) { init_esbuild_inject(); module.exports = deprecate; @@ -3598,7 +3469,7 @@ var require_stream_writable = __commonJS({ var Duplex; Writable.WritableState = WritableState; var internalUtil = { - deprecate: require_browser4() + deprecate: require_browser3() }; var Stream = require_stream_browser(); var Buffer3 = require_buffer().Buffer; @@ -5789,9 +5660,9 @@ var require_err_code = __commonJS({ var require_simple_peer = __commonJS({ "node_modules/simple-peer/index.js"(exports, module) { init_esbuild_inject(); - var debug = require_browser2()("simple-peer"); + var debug = require_browser()("simple-peer"); var getBrowserRTC2 = require_get_browser_rtc(); - var randombytes = require_browser3(); + var randombytes = require_browser2(); var stream = require_readable_browser(); var queueMicrotask2 = require_queue_microtask(); var errCode = require_err_code(); @@ -6657,173 +6528,11 @@ var require_simple_peer = __commonJS({ } }); -// node_modules/base64-arraybuffer/dist/base64-arraybuffer.umd.js -var require_base64_arraybuffer_umd = __commonJS({ - "node_modules/base64-arraybuffer/dist/base64-arraybuffer.umd.js"(exports, module) { - init_esbuild_inject(); - (function(global2, factory) { - typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global2 = typeof globalThis !== "undefined" ? globalThis : global2 || self, factory(global2["base64-arraybuffer"] = {})); - })(exports, function(exports2) { - "use strict"; - var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - var lookup = typeof Uint8Array === "undefined" ? [] : new Uint8Array(256); - for (var i = 0; i < chars.length; i++) { - lookup[chars.charCodeAt(i)] = i; - } - var encode = function(arraybuffer) { - var bytes = new Uint8Array(arraybuffer), i2, len = bytes.length, base64 = ""; - for (i2 = 0; i2 < len; i2 += 3) { - base64 += chars[bytes[i2] >> 2]; - base64 += chars[(bytes[i2] & 3) << 4 | bytes[i2 + 1] >> 4]; - base64 += chars[(bytes[i2 + 1] & 15) << 2 | bytes[i2 + 2] >> 6]; - base64 += chars[bytes[i2 + 2] & 63]; - } - if (len % 3 === 2) { - base64 = base64.substring(0, base64.length - 1) + "="; - } else if (len % 3 === 1) { - base64 = base64.substring(0, base64.length - 2) + "=="; - } - return base64; - }; - var decode = function(base64) { - var bufferLength = base64.length * 0.75, len = base64.length, i2, p = 0, encoded1, encoded2, encoded3, encoded4; - if (base64[base64.length - 1] === "=") { - bufferLength--; - if (base64[base64.length - 2] === "=") { - bufferLength--; - } - } - var arraybuffer = new ArrayBuffer(bufferLength), bytes = new Uint8Array(arraybuffer); - for (i2 = 0; i2 < len; i2 += 4) { - encoded1 = lookup[base64.charCodeAt(i2)]; - encoded2 = lookup[base64.charCodeAt(i2 + 1)]; - encoded3 = lookup[base64.charCodeAt(i2 + 2)]; - encoded4 = lookup[base64.charCodeAt(i2 + 3)]; - bytes[p++] = encoded1 << 2 | encoded2 >> 4; - bytes[p++] = (encoded2 & 15) << 4 | encoded3 >> 2; - bytes[p++] = (encoded3 & 3) << 6 | encoded4 & 63; - } - return arraybuffer; - }; - exports2.decode = decode; - exports2.encode = encode; - Object.defineProperty(exports2, "__esModule", { value: true }); - }); - } -}); - -// node_modules/convert-hex/convert-hex.js -var require_convert_hex = __commonJS({ - "node_modules/convert-hex/convert-hex.js"(exports, module) { - init_esbuild_inject(); - !function(globals) { - "use strict"; - var convertHex = { - bytesToHex: function(bytes) { - return arrBytesToHex(bytes); - }, - hexToBytes: function(hex) { - if (hex.length % 2 === 1) - throw new Error("hexToBytes can't have a string with an odd number of characters."); - if (hex.indexOf("0x") === 0) - hex = hex.slice(2); - return hex.match(/../g).map(function(x) { - return parseInt(x, 16); - }); - } - }; - function arrBytesToHex(bytes) { - return bytes.map(function(x) { - return padLeft(x.toString(16), 2); - }).join(""); - } - function padLeft(orig, len) { - if (orig.length > len) - return orig; - return Array(len - orig.length + 1).join("0") + orig; - } - if (typeof module !== "undefined" && module.exports) { - module.exports = convertHex; - } else { - globals.convertHex = convertHex; - } - }(exports); - } -}); - -// node_modules/random-string/lib/random-string.js -var require_random_string = __commonJS({ - "node_modules/random-string/lib/random-string.js"(exports, module) { - "use strict"; - init_esbuild_inject(); - var numbers = "0123456789"; - var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - var specials = "!$%^&*()_+|~-=`{}[]:;<>?,./"; - function _defaults(opts) { - opts || (opts = {}); - return { - length: opts.length || 8, - numeric: typeof opts.numeric === "boolean" ? opts.numeric : true, - letters: typeof opts.letters === "boolean" ? opts.letters : true, - special: typeof opts.special === "boolean" ? opts.special : false, - exclude: Array.isArray(opts.exclude) ? opts.exclude : [] - }; - } - function _buildChars(opts) { - var chars = ""; - if (opts.numeric) { - chars += numbers; - } - if (opts.letters) { - chars += letters; - } - if (opts.special) { - chars += specials; - } - for (var i = 0; i <= opts.exclude.length; i++) { - chars = chars.replace(opts.exclude[i], ""); - } - return chars; - } - module.exports = function randomString(opts) { - opts = _defaults(opts); - var i, rn, rnd = "", len = opts.length, exclude = opts.exclude, randomChars = _buildChars(opts); - for (i = 1; i <= len; i++) { - rnd += randomChars.substring(rn = Math.floor(Math.random() * randomChars.length), rn + 1); - } - return rnd; - }; - } -}); - -// node_modules/array-buffer-to-hex/index.js -var require_array_buffer_to_hex = __commonJS({ - "node_modules/array-buffer-to-hex/index.js"(exports, module) { - init_esbuild_inject(); - module.exports = function arrayBufferToHex2(arrayBuffer) { - if (typeof arrayBuffer !== "object" || arrayBuffer === null || typeof arrayBuffer.byteLength !== "number") { - throw new TypeError("Expected input to be an ArrayBuffer"); - } - var view = new Uint8Array(arrayBuffer); - var result = ""; - var value; - for (var i = 0; i < view.length; i++) { - value = view[i].toString(16); - result += value.length === 1 ? "0" + value : value; - } - return result; - }; - } -}); - // src/p2pcf.js init_esbuild_inject(); var import_get_browser_rtc = __toESM(require_get_browser_rtc()); var import_events = __toESM(require_events()); var import_simple_peer = __toESM(require_simple_peer()); -var import_base64_arraybuffer = __toESM(require_base64_arraybuffer_umd()); -var import_convert_hex = __toESM(require_convert_hex()); -var import_random_string = __toESM(require_random_string()); var MAX_MESSAGE_LENGTH_BYTES = 16e3; var CHUNK_HEADER_LENGTH_BYTES = 12; var CHUNK_MAGIC_WORD = 8121; @@ -6874,10 +6583,8 @@ var iOS = !!ua.match(/iPad/i) || !!ua.match(/iPhone/i); var webkit = !!ua.match(/WebKit/i); var iOSSafari = !!(iOS && webkit && !ua.match(/CriOS/i)); var isFirefox = !!(navigator?.userAgent.toLowerCase().indexOf("firefox") > -1); -var { decode: base64ToArrayBuffer } = require_base64_arraybuffer_umd(); -var arrayBufferToHex = require_array_buffer_to_hex(); -var hexToBase64 = (hex) => (0, import_base64_arraybuffer.encode)((0, import_convert_hex.hexToBytes)(hex)); -var base64ToHex = (b64) => arrayBufferToHex(base64ToArrayBuffer(b64)); +var hexToBase64 = (hex) => btoa(hex); +var base64ToHex = (b64) => atob(b64); function createSdp(isOffer, iceUFrag, icePwd, dtlsFingerprintBase64) { const dtlsHex = base64ToHex(dtlsFingerprintBase64); let dtlsFingerprint = ""; @@ -6959,7 +6666,7 @@ var P2PCF = class extends import_events.default { this.connectedSessions = []; this.clientId = clientId; this.roomId = roomId; - this.sessionId = (0, import_random_string.default)({ length: 20 }); + this.sessionId = self.crypto.randomUUID(); this.packages = []; this.dataTimestamp = null; this.lastPackages = null; @@ -6994,7 +6701,7 @@ var P2PCF = class extends import_events.default { window.history.replaceState( { ...window.history.state, - _p2pcfContextId: (0, import_random_string.default)({ length: 20 }) + _p2pcfContextId: self.crypto.randomUUID() }, window.location.href ); @@ -7264,8 +6971,8 @@ var P2PCF = class extends import_events.default { remoteSessionId, remoteDataTimestamp ); - const remoteUfrag = (0, import_random_string.default)({ length: 12 }); - const remotePwd = (0, import_random_string.default)({ length: 32 }); + const remoteUfrag = self.crypto.randomUUID().replace(/-/g, ""); + const remotePwd = self.crypto.randomUUID().replace(/-/g, ""); const peer2 = new import_simple_peer.default({ config: peerOptions, iceCompleteTimeout: 3e3, @@ -7710,5 +7417,4 @@ export { */ /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh */ /*! queue-microtask. MIT License. Feross Aboukhadijeh */ -/*! safe-buffer. MIT License. Feross Aboukhadijeh */ /*! simple-peer. MIT License. Feross Aboukhadijeh */ diff --git a/package.json b/package.json index ce8b72b..b0a29ca 100644 --- a/package.json +++ b/package.json @@ -14,15 +14,11 @@ "start:worker": "wrangler dev" }, "dependencies": { - "array-buffer-to-hex": "^1.0.0", - "base64-arraybuffer": "^1.0.2", "buffer": "^6.0.3", - "convert-hex": "^0.1.0", "crypto-browserify": "^3.12.0", "events": "^3.3.0", "get-browser-rtc": "^1.1.0", - "process-fast": "^1.0.0", - "random-string": "^0.2.0", + "process": "^0.11.10", "simple-peer": "^9.11.1", "stream-browserify": "^3.0.0" }, diff --git a/src/p2pcf.js b/src/p2pcf.js index 4702ec6..cb80f90 100644 --- a/src/p2pcf.js +++ b/src/p2pcf.js @@ -7,9 +7,6 @@ import getBrowserRTC from 'get-browser-rtc' import EventEmitter from 'events' import Peer from 'simple-peer' -import { encode as arrayBufferToBase64 } from 'base64-arraybuffer' -import { hexToBytes } from 'convert-hex' -import randomstring from 'random-string' // Based on Chrome const MAX_MESSAGE_LENGTH_BYTES = 16000 @@ -73,11 +70,8 @@ const webkit = !!ua.match(/WebKit/i) const iOSSafari = !!(iOS && webkit && !ua.match(/CriOS/i)) const isFirefox = !!(navigator?.userAgent.toLowerCase().indexOf('firefox') > -1) -const { decode: base64ToArrayBuffer } = require('base64-arraybuffer') -const arrayBufferToHex = require('array-buffer-to-hex') - -const hexToBase64 = hex => arrayBufferToBase64(hexToBytes(hex)) -const base64ToHex = b64 => arrayBufferToHex(base64ToArrayBuffer(b64)) +const hexToBase64 = hex => btoa(hex) +const base64ToHex = b64 => atob(b64) function createSdp (isOffer, iceUFrag, icePwd, dtlsFingerprintBase64) { const dtlsHex = base64ToHex(dtlsFingerprintBase64) @@ -177,7 +171,7 @@ export default class P2PCF extends EventEmitter { this.connectedSessions = [] this.clientId = clientId this.roomId = roomId - this.sessionId = randomstring({ length: 20 }) + this.sessionId = self.crypto.randomUUID() this.packages = [] this.dataTimestamp = null this.lastPackages = null @@ -224,7 +218,7 @@ export default class P2PCF extends EventEmitter { window.history.replaceState( { ...window.history.state, - _p2pcfContextId: randomstring({ length: 20 }) + _p2pcfContextId: self.crypto.randomUUID() }, window.location.href ) @@ -607,8 +601,8 @@ export default class P2PCF extends EventEmitter { remoteDataTimestamp ) - const remoteUfrag = randomstring({ length: 12 }) - const remotePwd = randomstring({ length: 32 }) + const remoteUfrag = self.crypto.randomUUID().replace(/-/g, '') + const remotePwd = self.crypto.randomUUID().replace(/-/g, '') const peer = new Peer({ config: peerOptions, iceCompleteTimeout: 3000,