{"record":{"id":"517d75e0da2ea43f","repo":"denoland/deno","slug":"messageevent-constructor-expected-eventinitdict-p","errorCode":null,"errorMessage":"MessageEvent constructor: Expected eventInitDict.ports[${i}] (${formatForBranded(p)}) to be an instance of MessagePort.","messagePattern":"MessageEvent constructor: Expected eventInitDict\\.ports\\[(.+?)\\] \\((.+?)\\) to be an instance of MessagePort\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/web/02_event.js","lineNumber":1374,"sourceCode":"            formatForRaw(ports)\n          }) is not iterable.`,\n        );\n      }\n      const MessagePortProto = getMessagePortPrototype();\n      const arr = [];\n      let i = 0;\n      // Iterate using the user's own iterator so values that aren't real\n      // arrays (e.g. a `RegExp` with a custom `Symbol.iterator` -- covered\n      // by WPT's no-regexp-special-casing test) still produce the\n      // expected `ports` array. SafeArrayIterator can't be used here\n      // because it walks the value as if it were an Array.\n      // deno-lint-ignore deno-internal/prefer-primordials\n      for (const p of ports) {\n        if (\n          p === null || typeof p !== \"object\" ||\n          !ObjectPrototypeIsPrototypeOf(MessagePortProto, p)\n        ) {\n          throw new TypeError(\n            `MessageEvent constructor: Expected eventInitDict.ports[${i}] (${\n              formatForBranded(p)\n            }) to be an instance of MessagePort.`,\n          );\n        }\n        arr[i++] = p;\n      }\n      // `ports` is a FrozenArray<MessagePort> per the HTML spec.\n      this.ports = ObjectFreeze(arr);\n    }\n    // origin and lastEventId are USVString per spec, so coerce to string\n    // (Node's test passes numbers and expects string coercion).\n    this.origin = eventInitDict?.origin === undefined\n      ? \"\"\n      : String(eventInitDict.origin);\n    this.lastEventId = eventInitDict?.lastEventId === undefined\n      ? \"\"\n      : String(eventInitDict.lastEventId);","sourceCodeStart":1356,"sourceCodeEnd":1392,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/web/02_event.js#L1356-L1392","documentation":"After the iterability check, MessageEvent's constructor iterates eventInitDict.ports and verifies every element against the web MessagePort prototype (ext/web/02_event.js:1366-1377). An element that is null, a primitive, or not branded as a MessagePort throws TypeError 'Expected eventInitDict.ports[i] ... to be an instance of MessagePort' with the offending index. Deno iterates using the user's own iterator so exotic iterables still work — only the element type is enforced.","triggerScenarios":"new MessageEvent('message', { data, ports: [new MessageChannel().port1, null] }); arrays containing a node:worker_threads port, a number, a string, or a plain object; sparse arrays with holes (undefined elements).","commonSituations":"Port arrays built with placeholders/sentinels (null) for closed or unused channels; mixing Node worker_threads MessagePort objects into web MessageEvent construction; arrays produced by concat/map that accidentally inject undefined.","solutions":["Filter the array to real web MessagePorts before constructing: ports.filter((p) => p instanceof MessagePort).","Pass only ports obtained from MessageChannel or a worker/web-worker onmessage event — never node:worker_threads ports.","Remove null/undefined placeholders instead of leaving holes in ports."],"exampleFix":"// before\nnew MessageEvent('message', { data, ports: [port1, null] });\n// TypeError: Expected eventInitDict.ports[1] (null) to be an instance of MessagePort.\n\n// after\nnew MessageEvent('message', { data, ports: [port1] });","handlingStrategy":"type-guard","validationCode":"const ports = rawPorts.filter((p) => p instanceof MessagePort);\nnew MessageEvent('message', { data, ports });","typeGuard":"const areAllMessagePorts = (a) =>\n  Array.isArray(a) && a.every((p) => p instanceof MessagePort);","tryCatchPattern":null,"preventionTips":["Filter null/undefined sentinels out of ports arrays","Never mix node:worker_threads ports into web MessageEvent","Validate ports at the boundary where worker messages enter your code"],"tags":["messageevent","messageport","worker","validation"],"backgroundTag":"invalid-eventinit-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}