@miniflare/web-sockets
Advanced tools
Comparing version 2.11.0 to 2.12.0
@@ -0,1 +1,3 @@ | ||
/// <reference types="node" /> | ||
import { Dispatcher } from 'undici'; | ||
@@ -34,3 +36,3 @@ import { InputGatedEventTarget } from '@miniflare/shared'; | ||
declare const kClose: unique symbol; | ||
/* Excluded from this release type: _kClose */ | ||
@@ -41,2 +43,4 @@ declare const kClosedIncoming: unique symbol; | ||
declare const kConstructOnly: unique symbol; | ||
declare const kCoupled: unique symbol; | ||
@@ -57,3 +61,3 @@ | ||
export declare function upgradingFetch(this: Dispatcher | void, input: RequestInfo, init?: RequestInit): Promise<Response>; | ||
export declare function upgradingFetch(this: Dispatcher | unknown, input: RequestInfo, init?: RequestInit): Promise<Response>; | ||
@@ -71,2 +75,4 @@ export declare class WebSocket extends InputGatedEventTarget<WebSocketEventMap> { | ||
[kClosedIncoming]: boolean; | ||
constructor(url: string | URL, protocols?: string | string[]); | ||
constructor(flag: typeof kConstructOnly); | ||
protected [kWrapListener]<Type extends keyof WebSocketEventMap>(listener: (event: WebSocketEventMap[Type]) => void): (event: WebSocketEventMap[Type]) => void; | ||
@@ -76,9 +82,10 @@ get readyState(): number; | ||
send(message: ArrayBuffer | string): void; | ||
[kSend](message: ArrayBuffer | string): void; | ||
/* Excluded from this release type: [kSend] */ | ||
close(code?: number, reason?: string): void; | ||
[kClose](code?: number, reason?: string): void; | ||
[kError](error?: Error): void; | ||
/* Excluded from this release type: [_kClose] */ | ||
/* Excluded from this release type: [kError] */ | ||
} | ||
export declare type WebSocketEventMap = { | ||
open: Event; | ||
message: MessageEvent; | ||
@@ -85,0 +92,0 @@ close: CloseEvent; |
@@ -60,2 +60,3 @@ var __create = Object.create; | ||
WebSocketPlugin: () => WebSocketPlugin, | ||
_kClose: () => _kClose, | ||
coupleWebSocket: () => coupleWebSocket, | ||
@@ -67,14 +68,12 @@ upgradingFetch: () => upgradingFetch | ||
var import_url = __toModule(require("url")); | ||
var import_core = __toModule(require("@miniflare/core")); | ||
var import_shared3 = __toModule(require("@miniflare/shared")); | ||
var import_core2 = __toModule(require("@miniflare/core")); | ||
var import_shared2 = __toModule(require("@miniflare/shared")); | ||
var import_ws2 = __toModule(require("ws")); | ||
// packages/web-sockets/src/couple.ts | ||
var import_events = __toModule(require("events")); | ||
var import_shared2 = __toModule(require("@miniflare/shared")); | ||
var import_ws = __toModule(require("ws")); | ||
// packages/web-sockets/src/websocket.ts | ||
var import_assert = __toModule(require("assert")); | ||
var import_events = __toModule(require("events")); | ||
var import_core = __toModule(require("@miniflare/core")); | ||
var import_shared = __toModule(require("@miniflare/shared")); | ||
var import_ws = __toModule(require("ws")); | ||
var MessageEvent = class extends Event { | ||
@@ -111,9 +110,12 @@ data; | ||
var kSend = Symbol("kSend"); | ||
var kClose = Symbol("kClose"); | ||
var _kClose = Symbol("kClose"); | ||
var kError = Symbol("kError"); | ||
var _dispatchQueue, _a, _b, _c, _d, _e, _queuingDispatchToPair, queuingDispatchToPair_fn; | ||
var kConstructOnly = Symbol("kConstructOnly"); | ||
var _userConstructed, _dispatchQueue, _a, _b, _c, _d, _e, _queuingDispatchToPair, queuingDispatchToPair_fn, _accept, accept_fn; | ||
var _WebSocket = class extends import_shared.InputGatedEventTarget { | ||
constructor() { | ||
super(...arguments); | ||
constructor(url, protocols) { | ||
super(); | ||
__privateAdd(this, _queuingDispatchToPair); | ||
__privateAdd(this, _accept); | ||
__privateAdd(this, _userConstructed, void 0); | ||
__privateAdd(this, _dispatchQueue, []); | ||
@@ -125,2 +127,30 @@ __publicField(this, _a); | ||
__publicField(this, _e, false); | ||
if (url === kConstructOnly) { | ||
__privateSet(this, _userConstructed, false); | ||
return; | ||
} | ||
__privateSet(this, _userConstructed, true); | ||
try { | ||
if (!(url instanceof URL)) | ||
url = new URL(url); | ||
} catch { | ||
throw new import_core.DOMException("WebSocket Constructor: The url is invalid.", "SyntaxError"); | ||
} | ||
if (url.protocol !== "ws:" && url.protocol !== "wss:") { | ||
throw new import_core.DOMException("WebSocket Constructor: The url scheme must be ws or wss.", "SyntaxError"); | ||
} | ||
if (url.hash !== "") { | ||
throw new import_core.DOMException("WebSocket Constructor: The url fragment must be empty.", "SyntaxError"); | ||
} | ||
const pair = new _WebSocket(kConstructOnly); | ||
this[kPair] = pair; | ||
pair[kPair] = this; | ||
const ws = new import_ws.default(url, protocols); | ||
void coupleWebSocket(ws, pair).then(() => { | ||
__privateMethod(this, _accept, accept_fn).call(this); | ||
this.dispatchEvent(new Event("open")); | ||
}, (error) => { | ||
__privateMethod(this, _accept, accept_fn).call(this); | ||
pair[kError](error); | ||
}); | ||
} | ||
@@ -144,3 +174,5 @@ [(_a = kPair, _b = kAccepted, _c = kCoupled, _d = kClosedOutgoing, _e = kClosedIncoming, import_shared.kWrapListener)](listener) { | ||
get readyState() { | ||
if (this[kClosedOutgoing] && this[kClosedIncoming]) { | ||
if (__privateGet(this, _userConstructed) && !this[kAccepted]) { | ||
return _WebSocket.READY_STATE_CONNECTING; | ||
} else if (this[kClosedOutgoing] && this[kClosedIncoming]) { | ||
return _WebSocket.READY_STATE_CLOSED; | ||
@@ -153,13 +185,6 @@ } else if (this[kClosedOutgoing] || this[kClosedIncoming]) { | ||
accept() { | ||
if (this[kCoupled]) { | ||
throw new TypeError("Can't accept() WebSocket that was already used in a response."); | ||
if (__privateGet(this, _userConstructed)) { | ||
throw new TypeError("Websockets obtained from the 'new WebSocket()' constructor cannot call accept"); | ||
} | ||
if (this[kAccepted]) | ||
return; | ||
this[kAccepted] = true; | ||
if (__privateGet(this, _dispatchQueue) !== void 0) { | ||
for (const event of __privateGet(this, _dispatchQueue)) | ||
this.dispatchEvent(event); | ||
__privateSet(this, _dispatchQueue, void 0); | ||
} | ||
__privateMethod(this, _accept, accept_fn).call(this); | ||
} | ||
@@ -191,5 +216,5 @@ send(message) { | ||
} | ||
this[kClose](code, reason); | ||
this[_kClose](code, reason); | ||
} | ||
[kClose](code, reason) { | ||
[_kClose](code, reason) { | ||
if (this[kClosedOutgoing]) | ||
@@ -208,2 +233,3 @@ throw new TypeError("WebSocket already closed"); | ||
var WebSocket = _WebSocket; | ||
_userConstructed = new WeakMap(); | ||
_dispatchQueue = new WeakMap(); | ||
@@ -221,2 +247,16 @@ _queuingDispatchToPair = new WeakSet(); | ||
}; | ||
_accept = new WeakSet(); | ||
accept_fn = function() { | ||
if (this[kCoupled]) { | ||
throw new TypeError("Can't accept() WebSocket that was already used in a response."); | ||
} | ||
if (this[kAccepted]) | ||
return; | ||
this[kAccepted] = true; | ||
if (__privateGet(this, _dispatchQueue) !== void 0) { | ||
for (const event of __privateGet(this, _dispatchQueue)) | ||
this.dispatchEvent(event); | ||
__privateSet(this, _dispatchQueue, void 0); | ||
} | ||
}; | ||
__publicField(WebSocket, "READY_STATE_CONNECTING", 0); | ||
@@ -230,9 +270,7 @@ __publicField(WebSocket, "READY_STATE_OPEN", 1); | ||
} | ||
this[0] = new WebSocket(); | ||
this[1] = new WebSocket(); | ||
this[0] = new WebSocket(kConstructOnly); | ||
this[1] = new WebSocket(kConstructOnly); | ||
this[0][kPair] = this[1]; | ||
this[1][kPair] = this[0]; | ||
}; | ||
// packages/web-sockets/src/couple.ts | ||
async function coupleWebSocket(ws, pair) { | ||
@@ -247,3 +285,3 @@ if (pair[kCoupled]) { | ||
if (!pair[kClosedOutgoing]) { | ||
pair[kSend](isBinary ? (0, import_shared2.viewToBuffer)(message) : message.toString()); | ||
pair[kSend](isBinary ? (0, import_shared.viewToBuffer)(message) : message.toString()); | ||
} | ||
@@ -253,3 +291,3 @@ }); | ||
if (!pair[kClosedOutgoing]) { | ||
pair[kClose](code, reason.toString()); | ||
pair[_kClose](code, reason.toString()); | ||
} | ||
@@ -266,2 +304,4 @@ }); | ||
ws.close(); | ||
} else if (e.code === 1006) { | ||
ws.terminate(); | ||
} else { | ||
@@ -282,5 +322,5 @@ ws.close(e.code, e.reason); | ||
async function upgradingFetch(input, init) { | ||
const request = new import_core.Request(input, init); | ||
const request = new import_core2.Request(input, init); | ||
if (request.method === "GET" && request.headers.get("upgrade") === "websocket") { | ||
(0, import_shared3.getRequestContext)()?.incrementExternalSubrequests(); | ||
(0, import_shared2.getRequestContext)()?.incrementExternalSubrequests(); | ||
const url = new import_url.URL(request.url); | ||
@@ -310,7 +350,7 @@ if (url.protocol !== "http:" && url.protocol !== "https:") { | ||
ws.once("upgrade", (req) => { | ||
headersResolve((0, import_core._headersFromIncomingRequest)(req)); | ||
headersResolve((0, import_core2._headersFromIncomingRequest)(req)); | ||
}); | ||
const [worker, client] = Object.values(new WebSocketPair()); | ||
await coupleWebSocket(ws, client); | ||
return new import_core.Response(null, { | ||
return new import_core2.Response(null, { | ||
status: 101, | ||
@@ -321,10 +361,9 @@ webSocket: worker, | ||
} | ||
return import_core.fetch.call(this, request); | ||
return import_core2.fetch.call(this, request); | ||
} | ||
// packages/web-sockets/src/plugin.ts | ||
var import_core2 = __toModule(require("@miniflare/core")); | ||
var import_shared4 = __toModule(require("@miniflare/shared")); | ||
var constructError = "Failed to construct 'WebSocket': the constructor is not implemented."; | ||
var WebSocketPlugin = class extends import_shared4.Plugin { | ||
var import_core3 = __toModule(require("@miniflare/core")); | ||
var import_shared3 = __toModule(require("@miniflare/shared")); | ||
var WebSocketPlugin = class extends import_shared3.Plugin { | ||
#webSockets = new Set(); | ||
@@ -334,3 +373,3 @@ #upgradingFetch; | ||
super(ctx); | ||
this.#upgradingFetch = (0, import_core2.createCompatFetch)(ctx, upgradingFetch.bind(ctx.fetchMock)); | ||
this.#upgradingFetch = (0, import_core3.createCompatFetch)(ctx, upgradingFetch.bind(ctx.fetchMock)); | ||
} | ||
@@ -343,10 +382,3 @@ setup() { | ||
WebSocketPair, | ||
WebSocket: new Proxy(WebSocket, { | ||
construct() { | ||
throw new Error(constructError); | ||
}, | ||
apply() { | ||
throw new Error(constructError); | ||
} | ||
}), | ||
WebSocket, | ||
fetch: this.fetch | ||
@@ -381,2 +413,3 @@ } | ||
WebSocketPlugin, | ||
_kClose, | ||
coupleWebSocket, | ||
@@ -383,0 +416,0 @@ upgradingFetch |
{ | ||
"name": "@miniflare/web-sockets", | ||
"version": "2.11.0", | ||
"version": "2.12.0", | ||
"description": "WebSocket module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers", | ||
@@ -38,11 +38,11 @@ "keywords": [ | ||
"dependencies": { | ||
"@miniflare/core": "2.11.0", | ||
"@miniflare/shared": "2.11.0", | ||
"undici": "5.9.1", | ||
"@miniflare/core": "2.12.0", | ||
"@miniflare/shared": "2.12.0", | ||
"undici": "5.11.0", | ||
"ws": "^8.2.2" | ||
}, | ||
"devDependencies": { | ||
"@miniflare/shared-test": "2.11.0", | ||
"@miniflare/shared-test": "2.12.0", | ||
"@types/ws": "^8.2.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25454
482
+ Added@miniflare/core@2.12.0(transitive)
+ Added@miniflare/queues@2.12.0(transitive)
+ Added@miniflare/shared@2.12.0(transitive)
+ Added@miniflare/watcher@2.12.0(transitive)
+ Addedundici@5.11.0(transitive)
- Removed@miniflare/core@2.11.0(transitive)
- Removed@miniflare/queues@2.11.0(transitive)
- Removed@miniflare/shared@2.11.0(transitive)
- Removed@miniflare/watcher@2.11.0(transitive)
- Removedundici@5.9.1(transitive)
Updated@miniflare/core@2.12.0
Updated@miniflare/shared@2.12.0
Updatedundici@5.11.0