-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-types.ts
More file actions
72 lines (61 loc) · 1.8 KB
/
Copy pathtest-types.ts
File metadata and controls
72 lines (61 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import JSONRPCWebSocket, {
type AnyMessageEventData,
type ConnectionOptions,
type MessageEventData,
type RawWebSocketData,
SocketEvent,
} from './dist/index';
const genericOptions: ConnectionOptions = {
url: 'ws://localhost',
inboundMode: 'raw',
};
void genericOptions;
const client = new JSONRPCWebSocket({
url: 'ws://localhost',
autoReconnect: false,
});
client.on(SocketEvent.Message, (event) => {
const decoded: true = event.decoded;
event.data;
const rawData: ArrayBuffer = event.rawData;
void decoded;
void rawData;
});
const rawClient = new JSONRPCWebSocket({
url: 'ws://localhost',
autoReconnect: false,
inboundMode: 'raw',
});
rawClient.on(SocketEvent.Message, (event) => {
const decoded: false = event.decoded;
const rawData: RawWebSocketData = event.rawData;
void decoded;
void rawData;
// @ts-expect-error raw inbound events do not expose decoded data
event.data;
});
rawClient.sendRaw('raw text');
rawClient.sendRaw(new Blob());
rawClient.sendRaw(new ArrayBuffer(0));
rawClient.sendRaw(new Uint8Array(new ArrayBuffer(0)));
rawClient.sendRaw(new Uint8Array(0));
// @ts-expect-error DOM WebSocket BufferSource does not accept SharedArrayBuffer-backed views
rawClient.sendRaw(new Uint8Array(new SharedArrayBuffer(0)));
const defaultMessageEvent: MessageEventData<'messagepack'> = {
decoded: true,
data: { jsonrpc: '2.0', method: 'notify' },
rawData: new ArrayBuffer(0),
};
const rawMessageEvent: MessageEventData<'raw'> = {
decoded: false,
rawData: 'raw text',
};
const anyMessageEvent: MessageEventData =
Math.random() > 0.5 ? defaultMessageEvent : rawMessageEvent;
const explicitAnyMessageEvent: AnyMessageEventData = anyMessageEvent;
void explicitAnyMessageEvent;
if (anyMessageEvent.decoded) {
anyMessageEvent.data;
} else {
anyMessageEvent.rawData;
}