Skip to content

Commit 3872afb

Browse files
committed
fix: Update for Deno 1.4
1 parent 58b9267 commit 3872afb

File tree

3 files changed

+24
-197
lines changed

3 files changed

+24
-197
lines changed

deno/WebSocketPolyfill.ts

Lines changed: 0 additions & 167 deletions
This file was deleted.

deno/index.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import './WebSocketPolyfill.ts';
2-
import { connectWebSocket, WebSocket } from 'https://deno.land/std/ws/mod.ts';
31
import {
42
serializeStructure,
53
deserializeStructure,
@@ -18,43 +16,39 @@ let ports = new Map<number | string, MessagePortData>();
1816
init();
1917

2018
async function init() {
21-
const socket = await connectWebSocket(address);
19+
const socket = new WebSocket(address);
2220

2321
let onMessage = patchGlobalThis((json) => socket.send(json));
2422

25-
const messages = async (): Promise<void> => {
26-
for await (const message of socket) {
27-
if (typeof message === 'string') {
28-
onMessage(message);
29-
}
30-
}
23+
socket.onmessage = (message) => {
24+
onMessage(message.data);
3125
};
32-
33-
messages().catch((err) => {
26+
socket.onerror = (err) => {
3427
console.error(err);
35-
if (!socket.isClosed) {
28+
if (socket.readyState !== WebSocket.CLOSED) {
3629
socket.close();
3730
}
38-
});
39-
40-
sendMessage(
41-
{
42-
type: 'init',
43-
},
44-
socket
45-
);
46-
47-
if (scriptType === 'script') {
48-
Function(script)();
49-
} else if (scriptType === 'import') {
50-
import(script);
51-
} else {
52-
throw new Error('Unsupported scrypt type: ' + scriptType);
53-
}
31+
};
32+
socket.onopen = () => {
33+
sendMessage(
34+
{
35+
type: 'init',
36+
},
37+
socket
38+
);
39+
40+
if (scriptType === 'script') {
41+
Function(script)();
42+
} else if (scriptType === 'import') {
43+
import(script);
44+
} else {
45+
throw new Error('Unsupported scrypt type: ' + scriptType);
46+
}
47+
};
5448
}
5549

5650
async function sendMessage(message: any, socket: WebSocket) {
57-
if (socket.isClosed) {
51+
if (socket.readyState !== WebSocket.OPEN) {
5852
return;
5953
}
6054
const structured = serializeStructure(message);

src/DenoWorker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export class DenoWorker {
227227
if (typeof addr === 'string') {
228228
connectAddress = addr;
229229
} else {
230-
connectAddress = `http://${addr.address}:${addr.port}`;
230+
connectAddress = `ws://${addr.address}:${addr.port}`;
231231
allowAddress = `${addr.address}:${addr.port}`;
232232
}
233233

0 commit comments

Comments
 (0)