1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const buffer_1 = require("buffer");
|
4 | const readable_stream_1 = require("readable-stream");
|
5 | const BufferedDuplex_1 = require("../BufferedDuplex");
|
6 | let my;
|
7 | let proxy;
|
8 | let stream;
|
9 | let isInitialized = false;
|
10 | function buildProxy() {
|
11 | const _proxy = new readable_stream_1.Transform();
|
12 | _proxy._write = (chunk, encoding, next) => {
|
13 | my.sendSocketMessage({
|
14 | data: chunk.buffer,
|
15 | success() {
|
16 | next();
|
17 | },
|
18 | fail() {
|
19 | next(new Error());
|
20 | },
|
21 | });
|
22 | };
|
23 | _proxy._flush = (done) => {
|
24 | my.closeSocket({
|
25 | success() {
|
26 | done();
|
27 | },
|
28 | });
|
29 | };
|
30 | return _proxy;
|
31 | }
|
32 | function setDefaultOpts(opts) {
|
33 | if (!opts.hostname) {
|
34 | opts.hostname = 'localhost';
|
35 | }
|
36 | if (!opts.path) {
|
37 | opts.path = '/';
|
38 | }
|
39 | if (!opts.wsOptions) {
|
40 | opts.wsOptions = {};
|
41 | }
|
42 | }
|
43 | function buildUrl(opts, client) {
|
44 | const protocol = opts.protocol === 'alis' ? 'wss' : 'ws';
|
45 | let url = `${protocol}://${opts.hostname}${opts.path}`;
|
46 | if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
47 | url = `${protocol}://${opts.hostname}:${opts.port}${opts.path}`;
|
48 | }
|
49 | if (typeof opts.transformWsUrl === 'function') {
|
50 | url = opts.transformWsUrl(url, opts, client);
|
51 | }
|
52 | return url;
|
53 | }
|
54 | function bindEventHandler() {
|
55 | if (isInitialized)
|
56 | return;
|
57 | isInitialized = true;
|
58 | my.onSocketOpen(() => {
|
59 | stream.socketReady();
|
60 | });
|
61 | my.onSocketMessage((res) => {
|
62 | if (typeof res.data === 'string') {
|
63 | const buffer = buffer_1.Buffer.from(res.data, 'base64');
|
64 | proxy.push(buffer);
|
65 | }
|
66 | else {
|
67 | const reader = new FileReader();
|
68 | reader.addEventListener('load', () => {
|
69 | let data = reader.result;
|
70 | if (data instanceof ArrayBuffer)
|
71 | data = buffer_1.Buffer.from(data);
|
72 | else
|
73 | data = buffer_1.Buffer.from(data, 'utf8');
|
74 | proxy.push(data);
|
75 | });
|
76 | reader.readAsArrayBuffer(res.data);
|
77 | }
|
78 | });
|
79 | my.onSocketClose(() => {
|
80 | stream.end();
|
81 | stream.destroy();
|
82 | });
|
83 | my.onSocketError((err) => {
|
84 | stream.destroy(err);
|
85 | });
|
86 | }
|
87 | const buildStream = (client, opts) => {
|
88 | opts.hostname = opts.hostname || opts.host;
|
89 | if (!opts.hostname) {
|
90 | throw new Error('Could not determine host. Specify host manually.');
|
91 | }
|
92 | const websocketSubProtocol = opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
|
93 | ? 'mqttv3.1'
|
94 | : 'mqtt';
|
95 | setDefaultOpts(opts);
|
96 | const url = buildUrl(opts, client);
|
97 | my = opts.my;
|
98 | my.connectSocket({
|
99 | url,
|
100 | protocols: websocketSubProtocol,
|
101 | });
|
102 | proxy = buildProxy();
|
103 | stream = new BufferedDuplex_1.BufferedDuplex(opts, proxy, my);
|
104 | bindEventHandler();
|
105 | return stream;
|
106 | };
|
107 | exports.default = buildStream;
|
108 |
|
\ | No newline at end of file |