@soketi/soketi-js
Version:
Laravel Echo extension that works with Soketi, a Laravel-ready WebSockets service.
43 lines (42 loc) • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SoketiEncryptedPrivateChannel = void 0;
const utf8_1 = require("@stablelib/utf8");
const base64_1 = require("@stablelib/base64");
const soketi_private_channel_1 = require("./soketi-private-channel");
const nacl = require('tweetnacl');
class SoketiEncryptedPrivateChannel extends soketi_private_channel_1.SoketiPrivateChannel {
subscribe() {
this.authenticate().then(response => {
this.sharedSecret = response.shared_secret;
this.socket.emit('subscribe', {
channel: this.name,
auth: this.options.auth || {},
channel_data: response.channel_data,
token: response.auth,
});
}, error => console.log(error));
}
listen(event, callback) {
this.on(this.eventFormatter.format(event), event => {
callback(this.decrypt(event));
});
return this;
}
decrypt(event) {
if (!event.ciphertext || !event.nonce) {
return event;
}
let cipherText = base64_1.decode(event.ciphertext);
let nonce = base64_1.decode(event.nonce);
let key = base64_1.decode(this.sharedSecret);
let raw = utf8_1.decode(nacl.secretbox.open(cipherText, nonce, key));
try {
return JSON.parse(raw);
}
catch (_a) {
return raw;
}
}
}
exports.SoketiEncryptedPrivateChannel = SoketiEncryptedPrivateChannel;