Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x | import EventEmitter from 'events'
import uriTemplates from 'uri-templates'
import { v4 as uuidv4 } from 'uuid'
import GleeQuore from '../index.js'
import GleeQuoreMessage from './message.js'
import { validateData } from './util.js'
import GleeError from '../errors.js'
export type ClusterEvent = {
serverName: string
adapter: GleeQuoreClusterAdapter
}
const ClusterMessageSchema = {
type: 'object',
properties: {
instanceId: { type: 'string' },
payload: { type: 'string' },
headers: {
type: 'object',
propertyNames: { type: 'string' },
additionProperties: { type: 'string' },
},
channel: { type: 'string' },
serverName: { type: 'string' },
broadcast: { type: 'boolean' },
cluster: { type: 'boolean' },
outbound: { type: 'boolean' },
inbound: { type: 'boolean' },
},
required: ['instanceId', 'payload', 'channel', 'serverName', 'broadcast'],
additionalProperties: false,
}
class GleeQuoreClusterAdapter extends EventEmitter {
private _glee: GleeQuore
private _serverName: string
private _serverUrlExpanded: string
private _instanceId: string
/**
* Instantiates a Glee Cluster adapter.
*
* @param {GleeQuore} glee A reference to the Glee app.
*/
constructor(glee: GleeQuore) {
super()
this._instanceId = uuidv4()
this._glee = glee
const serverName = this._glee.options?.cluster?.name || 'cluster'
this._serverName = serverName
const url = this._glee.options?.cluster?.url
Iif (!url) {
console.log(
'Please provide a URL for your cluster adapter in glee.config.js'
)
process.exit(1)
}
const uriTemplateValues = new Map()
process.env.GLEE_SERVER_VARIABLES?.split(',').forEach((t) => {
const [localServerName, variable, value] = t.split(':')
Iif (localServerName === this._serverName)
{uriTemplateValues.set(variable, value)}
})
this._serverUrlExpanded = uriTemplates(url).fill(
Object.fromEntries(uriTemplateValues.entries())
)
function genClusterEvent(ev): ClusterEvent {
return {
...ev,
serverName,
}
}
this.on('error', (err) => {
this._glee.injectError(err)
})
this.on('message', (message) => {
message.cluster = true
this._glee.send(message)
})
this.on('connect', (ev) => {
this._glee.emitInternalEvent('adapter:cluster:connect', genClusterEvent(ev))
})
this.on('reconnect', (ev) => {
this._glee.emitInternalEvent('adapter:cluster:reconnect', genClusterEvent(ev))
})
this.on('close', (ev) => {
this._glee.emitInternalEvent('adapter:cluster:close', genClusterEvent(ev))
})
}
get glee(): GleeQuore {
return this._glee
}
get serverName(): string {
return this._serverName
}
get serverUrlExpanded(): string {
return this._serverUrlExpanded
}
get instanceId(): string {
return this._instanceId
}
/**
* Connects to the remote server.
*/
async connect(): Promise<any> {
throw new Error('Method `connect` is not implemented.')
}
/**
* Sends a message to the remote server.
*
* @param {GleeQuoreMessage} message The message to send.
*/
async send(message: GleeQuoreMessage): Promise<any> { // eslint-disable-line @typescript-eslint/no-unused-vars
throw new Error('Method `send` is not implemented.')
}
/**
* Serialize a message into JSON.
*
* @param {GleeQuoreMessage} message The message to serialize.
* @returns {String} The serialized message,
*/
serializeMessage(message: GleeQuoreMessage): string {
return JSON.stringify({
instanceId: this._instanceId,
payload: message.payload,
headers: message.headers,
channel: message.channel,
serverName: message.serverName,
broadcast: message.broadcast,
cluster: message.cluster,
inbound: message.isInbound(),
outbound: message.isOutbound(),
})
}
/**
* Deserializes the serialized message.
*
* @param {String} serialized The serialized message
* @returns {GleeQuoreMessage} The deserialized message.
*/
deserializeMessage(serialized: string): GleeQuoreMessage {
let messageData
try {
messageData = JSON.parse(serialized)
const { errors, humanReadableError, isValid } = validateData(
messageData,
ClusterMessageSchema
)
Iif (!isValid) {
throw new GleeError({ humanReadableError, errors })
}
} catch (e) {
this._glee.injectError(e)
return
}
let payload = messageData.payload
try {
payload = JSON.parse(messageData.payload)
} catch (e) {
// payload isn't JSON
}
Iif (messageData.instanceId === this._instanceId) return
const message = new GleeQuoreMessage({
payload: payload,
headers: messageData.headers,
channel: messageData.channel,
serverName: messageData.serverName,
broadcast: messageData.broadcast,
cluster: messageData.cluster,
})
if (messageData.inbound && !messageData.outbound) {
message.setInbound()
} else {
message.setOutbound()
}
return message
}
}
export default GleeQuoreClusterAdapter
|