-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathamqp-websocket-client.ts
More file actions
206 lines (192 loc) · 6.52 KB
/
amqp-websocket-client.ts
File metadata and controls
206 lines (192 loc) · 6.52 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { AMQPBaseClient, VERSION, MIN_FRAME_SIZE } from "./amqp-base-client.js"
import { AMQPView } from "./amqp-view.js"
import { AMQPError } from "./amqp-error.js"
import { AMQPChannel } from "./amqp-channel.js"
import { AMQPConsumer } from "./amqp-consumer.js"
import { AMQPMessage } from "./amqp-message.js"
import type { Logger } from "./types.js"
export interface AMQPWebSocketInit {
url: string
vhost?: string
username?: string
password?: string
name?: string
frameMax?: number
heartbeat?: number
logger?: Logger | null
}
/**
* WebSocket client for AMQP 0-9-1 servers.
*/
export class AMQPWebSocketClient extends AMQPBaseClient {
/** WebSocket URL to connect to. */
readonly url: string
private socket?: WebSocket | undefined
private framePos = 0
private frameSize = 0
private frameBuffer: Uint8Array
/**
* @param url to the websocket endpoint, example: wss://server/ws/amqp
* @param logger - optional logger instance, defaults to null (no logging)
*/
constructor(
url: string,
vhost?: string,
username?: string,
password?: string,
name?: string,
frameMax?: number,
heartbeat?: number,
logger?: Logger | null,
)
constructor(init: AMQPWebSocketInit)
constructor(
url: string | AMQPWebSocketInit,
vhost = "/",
username = "guest",
password = "guest",
name?: string,
frameMax = MIN_FRAME_SIZE,
heartbeat = 0,
logger?: Logger | null,
) {
if (typeof url === "object") {
vhost = url.vhost ?? vhost
username = url.username ?? username
password = url.password ?? password
name = url.name ?? name
frameMax = url.frameMax ?? frameMax
heartbeat = url.heartbeat ?? heartbeat
logger = url.logger ?? logger
url = url.url
}
super(vhost, username, password, name, AMQPWebSocketClient.platform(), frameMax, heartbeat, 0, logger)
this.url = url
this.frameBuffer = new Uint8Array(frameMax)
}
/**
* Establish a AMQP connection over WebSocket
*/
override connect(): Promise<AMQPBaseClient> {
this.framePos = 0
this.frameSize = 0
this.channels = [new AMQPChannel(this, 0)]
const socket = new WebSocket(this.url)
this.socket = socket
socket.binaryType = "arraybuffer"
socket.onmessage = this.handleMessage.bind(this)
return new Promise((resolve, reject) => {
this.connectPromise = [resolve, reject]
socket.addEventListener("close", reject)
socket.addEventListener("error", reject)
socket.addEventListener("open", () => {
socket.addEventListener("error", (ev: Event) => {
if (!this.closed) {
const err = new AMQPError(ev.toString(), this)
this.closed = true
this.channels.forEach((ch) => ch?.setClosed(err))
this.channels = [new AMQPChannel(this, 0)]
this.onerror(err)
this.ondisconnect?.(err)
this.socket = undefined
}
})
socket.addEventListener("close", (ev: CloseEvent) => {
const clientClosed = this.closed
this.closed = true
if (!(ev.wasClean && clientClosed)) {
const err = new AMQPError(`connection not cleanly closed (${ev.code})`, this)
this.channels.forEach((ch) => ch?.setClosed(err))
this.channels = [new AMQPChannel(this, 0)]
this.onerror(err)
if (!clientClosed) {
this.ondisconnect?.(err)
}
} else {
this.channels.forEach((ch) => ch?.setClosed())
this.channels = [new AMQPChannel(this, 0)]
}
this.socket = undefined
})
socket.send(new Uint8Array([65, 77, 81, 80, 0, 0, 9, 1]))
})
})
}
/**
* @param bytes to send
* @return fulfilled when the data is enqueued
*/
override send(bytes: Uint8Array): Promise<void> {
return new Promise((resolve, reject) => {
if (this.socket) {
try {
this.socket.send(bytes)
resolve()
} catch (err) {
this.closeSocket()
reject(err)
}
} else {
reject(new AMQPError("Socket not connected", this))
}
})
}
protected override closeSocket() {
this.closed = true
if (this.socket) this.socket.close()
this.socket = undefined
}
private handleMessage(event: MessageEvent) {
const buf: ArrayBuffer = event.data
const bufView = new DataView(buf)
// A socket read can contain 0 or more frames, so find frame boundries
let bufPos = 0
while (bufPos < buf.byteLength) {
// read frame size of next frame
if (this.frameSize === 0) {
// first 7 bytes of a frame was split over two reads, this reads the second part
if (this.framePos !== 0) {
const len = 7 - this.framePos
this.frameBuffer.set(new Uint8Array(buf, bufPos, len), this.framePos)
this.frameSize = new DataView(this.frameBuffer.buffer).getInt32(bufPos + 3) + 8
this.framePos += len
bufPos += len
continue
}
// frame header is split over multiple reads, copy to frameBuffer
if (bufPos + 3 + 4 > buf.byteLength) {
const len = buf.byteLength - bufPos
this.frameBuffer.set(new Uint8Array(buf, bufPos), this.framePos)
this.framePos += len
break
}
this.frameSize = bufView.getInt32(bufPos + 3) + 8
// avoid copying if the whole frame is in the read buffer
if (buf.byteLength - bufPos >= this.frameSize) {
const view = new AMQPView(buf, bufPos, this.frameSize)
this.parseFrames(view)
bufPos += this.frameSize
this.frameSize = 0
continue
}
}
const leftOfFrame = this.frameSize - this.framePos
const copyBytes = Math.min(leftOfFrame, buf.byteLength - bufPos)
this.frameBuffer.set(new Uint8Array(buf, bufPos, copyBytes), this.framePos)
this.framePos += copyBytes
bufPos += copyBytes
if (this.framePos === this.frameSize) {
const view = new AMQPView(this.frameBuffer.buffer, 0, this.frameSize)
this.parseFrames(view)
this.frameSize = this.framePos = 0
}
}
}
static platform(): string {
if (typeof navigator !== "undefined") return navigator.userAgent
else return `${process.release.name} ${process.version} ${process.platform} ${process.arch}`
}
}
export { AMQPBaseClient, AMQPChannel, AMQPConsumer, AMQPError, AMQPMessage, AMQPView, VERSION }
export { AMQPSession } from "./amqp-session.js"
export type { AMQPSessionOptions } from "./amqp-session.js"