Skip to content
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
19 changes: 18 additions & 1 deletion packages/nativescript-inspector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ function createInspectorSocket(
url: string,
handlers: InspectorSocketHandlers,
): InspectorSocket {
if (typeof WebSocket === "function") {
if (typeof WebSocket !== "undefined" && isBrowserWebSocket(WebSocket)) {
const socket = new WebSocket(url) as any;
socket.onmessage = (event: { data: string }) => {
handlers.onMessage(String(event.data));
Expand All @@ -912,6 +912,23 @@ function createInspectorSocket(
);
}

// NativeScript exposes Objective-C class as a global "function", and
// recent iOS SDKs ship a top-level `WebSocket` class in Network.framework.
// `typeof WebSocket === "function"` therefore matches that native class and
// `new WebSocket(url)` fails with "No initializer found that matches
// constructor invocation." Require browser-style readyState constants and a
// `send`/`close` prototype to confirm the global is a real WebSocket polyfill
// (e.g. @valor/nativescript-websockets) before using it.
function isBrowserWebSocket(ctor: any): boolean {
return (
typeof ctor === "function" &&
ctor.CONNECTING === 0 &&
ctor.OPEN === 1 &&
typeof ctor.prototype?.send === "function" &&
typeof ctor.prototype?.close === "function"
);
}

function createNSURLSessionWebSocket(
url: string,
handlers: InspectorSocketHandlers,
Expand Down
Loading