WebSocket.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const Blob = require("./Blob.js");
  5. const EventHandlerNonNull = require("./EventHandlerNonNull.js");
  6. const BinaryType = require("./BinaryType.js");
  7. const implSymbol = utils.implSymbol;
  8. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  9. const EventTarget = require("./EventTarget.js");
  10. const interfaceName = "WebSocket";
  11. exports.is = value => {
  12. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  13. };
  14. exports.isImpl = value => {
  15. return utils.isObject(value) && value instanceof Impl.implementation;
  16. };
  17. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  18. if (exports.is(value)) {
  19. return utils.implForWrapper(value);
  20. }
  21. throw new globalObject.TypeError(`${context} is not of type 'WebSocket'.`);
  22. };
  23. function makeWrapper(globalObject, newTarget) {
  24. let proto;
  25. if (newTarget !== undefined) {
  26. proto = newTarget.prototype;
  27. }
  28. if (!utils.isObject(proto)) {
  29. proto = globalObject[ctorRegistrySymbol]["WebSocket"].prototype;
  30. }
  31. return Object.create(proto);
  32. }
  33. exports.create = (globalObject, constructorArgs, privateData) => {
  34. const wrapper = makeWrapper(globalObject);
  35. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  36. };
  37. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  38. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  39. return utils.implForWrapper(wrapper);
  40. };
  41. exports._internalSetup = (wrapper, globalObject) => {
  42. EventTarget._internalSetup(wrapper, globalObject);
  43. };
  44. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  45. privateData.wrapper = wrapper;
  46. exports._internalSetup(wrapper, globalObject);
  47. Object.defineProperty(wrapper, implSymbol, {
  48. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  49. configurable: true
  50. });
  51. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  52. if (Impl.init) {
  53. Impl.init(wrapper[implSymbol]);
  54. }
  55. return wrapper;
  56. };
  57. exports.new = (globalObject, newTarget) => {
  58. const wrapper = makeWrapper(globalObject, newTarget);
  59. exports._internalSetup(wrapper, globalObject);
  60. Object.defineProperty(wrapper, implSymbol, {
  61. value: Object.create(Impl.implementation.prototype),
  62. configurable: true
  63. });
  64. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  65. if (Impl.init) {
  66. Impl.init(wrapper[implSymbol]);
  67. }
  68. return wrapper[implSymbol];
  69. };
  70. const exposed = new Set(["Window", "Worker"]);
  71. exports.install = (globalObject, globalNames) => {
  72. if (!globalNames.some(globalName => exposed.has(globalName))) {
  73. return;
  74. }
  75. const ctorRegistry = utils.initCtorRegistry(globalObject);
  76. class WebSocket extends globalObject.EventTarget {
  77. constructor(url) {
  78. if (arguments.length < 1) {
  79. throw new globalObject.TypeError(
  80. `Failed to construct 'WebSocket': 1 argument required, but only ${arguments.length} present.`
  81. );
  82. }
  83. const args = [];
  84. {
  85. let curArg = arguments[0];
  86. curArg = conversions["USVString"](curArg, {
  87. context: "Failed to construct 'WebSocket': parameter 1",
  88. globals: globalObject
  89. });
  90. args.push(curArg);
  91. }
  92. {
  93. let curArg = arguments[1];
  94. if (curArg !== undefined) {
  95. if (utils.isObject(curArg)) {
  96. if (curArg[Symbol.iterator] !== undefined) {
  97. if (!utils.isObject(curArg)) {
  98. throw new globalObject.TypeError(
  99. "Failed to construct 'WebSocket': parameter 2" + " sequence" + " is not an iterable object."
  100. );
  101. } else {
  102. const V = [];
  103. const tmp = curArg;
  104. for (let nextItem of tmp) {
  105. nextItem = conversions["DOMString"](nextItem, {
  106. context: "Failed to construct 'WebSocket': parameter 2" + " sequence" + "'s element",
  107. globals: globalObject
  108. });
  109. V.push(nextItem);
  110. }
  111. curArg = V;
  112. }
  113. } else {
  114. }
  115. } else {
  116. curArg = conversions["DOMString"](curArg, {
  117. context: "Failed to construct 'WebSocket': parameter 2",
  118. globals: globalObject
  119. });
  120. }
  121. } else {
  122. curArg = [];
  123. }
  124. args.push(curArg);
  125. }
  126. return exports.setup(Object.create(new.target.prototype), globalObject, args);
  127. }
  128. close() {
  129. const esValue = this !== null && this !== undefined ? this : globalObject;
  130. if (!exports.is(esValue)) {
  131. throw new globalObject.TypeError("'close' called on an object that is not a valid instance of WebSocket.");
  132. }
  133. const args = [];
  134. {
  135. let curArg = arguments[0];
  136. if (curArg !== undefined) {
  137. curArg = conversions["unsigned short"](curArg, {
  138. context: "Failed to execute 'close' on 'WebSocket': parameter 1",
  139. globals: globalObject,
  140. clamp: true
  141. });
  142. }
  143. args.push(curArg);
  144. }
  145. {
  146. let curArg = arguments[1];
  147. if (curArg !== undefined) {
  148. curArg = conversions["USVString"](curArg, {
  149. context: "Failed to execute 'close' on 'WebSocket': parameter 2",
  150. globals: globalObject
  151. });
  152. }
  153. args.push(curArg);
  154. }
  155. return esValue[implSymbol].close(...args);
  156. }
  157. send(data) {
  158. const esValue = this !== null && this !== undefined ? this : globalObject;
  159. if (!exports.is(esValue)) {
  160. throw new globalObject.TypeError("'send' called on an object that is not a valid instance of WebSocket.");
  161. }
  162. if (arguments.length < 1) {
  163. throw new globalObject.TypeError(
  164. `Failed to execute 'send' on 'WebSocket': 1 argument required, but only ${arguments.length} present.`
  165. );
  166. }
  167. const args = [];
  168. {
  169. let curArg = arguments[0];
  170. if (Blob.is(curArg)) {
  171. {
  172. let curArg = arguments[0];
  173. curArg = Blob.convert(globalObject, curArg, {
  174. context: "Failed to execute 'send' on 'WebSocket': parameter 1"
  175. });
  176. args.push(curArg);
  177. }
  178. } else if (utils.isArrayBuffer(curArg)) {
  179. {
  180. let curArg = arguments[0];
  181. curArg = conversions["ArrayBuffer"](curArg, {
  182. context: "Failed to execute 'send' on 'WebSocket': parameter 1",
  183. globals: globalObject
  184. });
  185. args.push(curArg);
  186. }
  187. } else if (ArrayBuffer.isView(curArg)) {
  188. {
  189. let curArg = arguments[0];
  190. if (ArrayBuffer.isView(curArg)) {
  191. curArg = conversions["ArrayBufferView"](curArg, {
  192. context: "Failed to execute 'send' on 'WebSocket': parameter 1",
  193. globals: globalObject
  194. });
  195. } else {
  196. throw new globalObject.TypeError(
  197. "Failed to execute 'send' on 'WebSocket': parameter 1" + " is not of any supported type."
  198. );
  199. }
  200. args.push(curArg);
  201. }
  202. } else {
  203. {
  204. let curArg = arguments[0];
  205. curArg = conversions["USVString"](curArg, {
  206. context: "Failed to execute 'send' on 'WebSocket': parameter 1",
  207. globals: globalObject
  208. });
  209. args.push(curArg);
  210. }
  211. }
  212. }
  213. return esValue[implSymbol].send(...args);
  214. }
  215. get url() {
  216. const esValue = this !== null && this !== undefined ? this : globalObject;
  217. if (!exports.is(esValue)) {
  218. throw new globalObject.TypeError("'get url' called on an object that is not a valid instance of WebSocket.");
  219. }
  220. return esValue[implSymbol]["url"];
  221. }
  222. get readyState() {
  223. const esValue = this !== null && this !== undefined ? this : globalObject;
  224. if (!exports.is(esValue)) {
  225. throw new globalObject.TypeError(
  226. "'get readyState' called on an object that is not a valid instance of WebSocket."
  227. );
  228. }
  229. return esValue[implSymbol]["readyState"];
  230. }
  231. get bufferedAmount() {
  232. const esValue = this !== null && this !== undefined ? this : globalObject;
  233. if (!exports.is(esValue)) {
  234. throw new globalObject.TypeError(
  235. "'get bufferedAmount' called on an object that is not a valid instance of WebSocket."
  236. );
  237. }
  238. return esValue[implSymbol]["bufferedAmount"];
  239. }
  240. get onopen() {
  241. const esValue = this !== null && this !== undefined ? this : globalObject;
  242. if (!exports.is(esValue)) {
  243. throw new globalObject.TypeError("'get onopen' called on an object that is not a valid instance of WebSocket.");
  244. }
  245. return utils.tryWrapperForImpl(esValue[implSymbol]["onopen"]);
  246. }
  247. set onopen(V) {
  248. const esValue = this !== null && this !== undefined ? this : globalObject;
  249. if (!exports.is(esValue)) {
  250. throw new globalObject.TypeError("'set onopen' called on an object that is not a valid instance of WebSocket.");
  251. }
  252. if (!utils.isObject(V)) {
  253. V = null;
  254. } else {
  255. V = EventHandlerNonNull.convert(globalObject, V, {
  256. context: "Failed to set the 'onopen' property on 'WebSocket': The provided value"
  257. });
  258. }
  259. esValue[implSymbol]["onopen"] = V;
  260. }
  261. get onerror() {
  262. const esValue = this !== null && this !== undefined ? this : globalObject;
  263. if (!exports.is(esValue)) {
  264. throw new globalObject.TypeError(
  265. "'get onerror' called on an object that is not a valid instance of WebSocket."
  266. );
  267. }
  268. return utils.tryWrapperForImpl(esValue[implSymbol]["onerror"]);
  269. }
  270. set onerror(V) {
  271. const esValue = this !== null && this !== undefined ? this : globalObject;
  272. if (!exports.is(esValue)) {
  273. throw new globalObject.TypeError(
  274. "'set onerror' called on an object that is not a valid instance of WebSocket."
  275. );
  276. }
  277. if (!utils.isObject(V)) {
  278. V = null;
  279. } else {
  280. V = EventHandlerNonNull.convert(globalObject, V, {
  281. context: "Failed to set the 'onerror' property on 'WebSocket': The provided value"
  282. });
  283. }
  284. esValue[implSymbol]["onerror"] = V;
  285. }
  286. get onclose() {
  287. const esValue = this !== null && this !== undefined ? this : globalObject;
  288. if (!exports.is(esValue)) {
  289. throw new globalObject.TypeError(
  290. "'get onclose' called on an object that is not a valid instance of WebSocket."
  291. );
  292. }
  293. return utils.tryWrapperForImpl(esValue[implSymbol]["onclose"]);
  294. }
  295. set onclose(V) {
  296. const esValue = this !== null && this !== undefined ? this : globalObject;
  297. if (!exports.is(esValue)) {
  298. throw new globalObject.TypeError(
  299. "'set onclose' called on an object that is not a valid instance of WebSocket."
  300. );
  301. }
  302. if (!utils.isObject(V)) {
  303. V = null;
  304. } else {
  305. V = EventHandlerNonNull.convert(globalObject, V, {
  306. context: "Failed to set the 'onclose' property on 'WebSocket': The provided value"
  307. });
  308. }
  309. esValue[implSymbol]["onclose"] = V;
  310. }
  311. get extensions() {
  312. const esValue = this !== null && this !== undefined ? this : globalObject;
  313. if (!exports.is(esValue)) {
  314. throw new globalObject.TypeError(
  315. "'get extensions' called on an object that is not a valid instance of WebSocket."
  316. );
  317. }
  318. return esValue[implSymbol]["extensions"];
  319. }
  320. get protocol() {
  321. const esValue = this !== null && this !== undefined ? this : globalObject;
  322. if (!exports.is(esValue)) {
  323. throw new globalObject.TypeError(
  324. "'get protocol' called on an object that is not a valid instance of WebSocket."
  325. );
  326. }
  327. return esValue[implSymbol]["protocol"];
  328. }
  329. get onmessage() {
  330. const esValue = this !== null && this !== undefined ? this : globalObject;
  331. if (!exports.is(esValue)) {
  332. throw new globalObject.TypeError(
  333. "'get onmessage' called on an object that is not a valid instance of WebSocket."
  334. );
  335. }
  336. return utils.tryWrapperForImpl(esValue[implSymbol]["onmessage"]);
  337. }
  338. set onmessage(V) {
  339. const esValue = this !== null && this !== undefined ? this : globalObject;
  340. if (!exports.is(esValue)) {
  341. throw new globalObject.TypeError(
  342. "'set onmessage' called on an object that is not a valid instance of WebSocket."
  343. );
  344. }
  345. if (!utils.isObject(V)) {
  346. V = null;
  347. } else {
  348. V = EventHandlerNonNull.convert(globalObject, V, {
  349. context: "Failed to set the 'onmessage' property on 'WebSocket': The provided value"
  350. });
  351. }
  352. esValue[implSymbol]["onmessage"] = V;
  353. }
  354. get binaryType() {
  355. const esValue = this !== null && this !== undefined ? this : globalObject;
  356. if (!exports.is(esValue)) {
  357. throw new globalObject.TypeError(
  358. "'get binaryType' called on an object that is not a valid instance of WebSocket."
  359. );
  360. }
  361. return utils.tryWrapperForImpl(esValue[implSymbol]["binaryType"]);
  362. }
  363. set binaryType(V) {
  364. const esValue = this !== null && this !== undefined ? this : globalObject;
  365. if (!exports.is(esValue)) {
  366. throw new globalObject.TypeError(
  367. "'set binaryType' called on an object that is not a valid instance of WebSocket."
  368. );
  369. }
  370. V = `${V}`;
  371. if (!BinaryType.enumerationValues.has(V)) {
  372. return;
  373. }
  374. esValue[implSymbol]["binaryType"] = V;
  375. }
  376. }
  377. Object.defineProperties(WebSocket.prototype, {
  378. close: { enumerable: true },
  379. send: { enumerable: true },
  380. url: { enumerable: true },
  381. readyState: { enumerable: true },
  382. bufferedAmount: { enumerable: true },
  383. onopen: { enumerable: true },
  384. onerror: { enumerable: true },
  385. onclose: { enumerable: true },
  386. extensions: { enumerable: true },
  387. protocol: { enumerable: true },
  388. onmessage: { enumerable: true },
  389. binaryType: { enumerable: true },
  390. [Symbol.toStringTag]: { value: "WebSocket", configurable: true },
  391. CONNECTING: { value: 0, enumerable: true },
  392. OPEN: { value: 1, enumerable: true },
  393. CLOSING: { value: 2, enumerable: true },
  394. CLOSED: { value: 3, enumerable: true }
  395. });
  396. Object.defineProperties(WebSocket, {
  397. CONNECTING: { value: 0, enumerable: true },
  398. OPEN: { value: 1, enumerable: true },
  399. CLOSING: { value: 2, enumerable: true },
  400. CLOSED: { value: 3, enumerable: true }
  401. });
  402. ctorRegistry[interfaceName] = WebSocket;
  403. Object.defineProperty(globalObject, interfaceName, {
  404. configurable: true,
  405. writable: true,
  406. value: WebSocket
  407. });
  408. };
  409. const Impl = require("../../jsdom/living/websockets/WebSocket-impl.js");