1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const validReasonCodes = [0, 16, 128, 131, 135, 144, 145, 151, 153];
|
4 | const handlePublish = (client, packet, done) => {
|
5 | client.log('handlePublish: packet %o', packet);
|
6 | done = typeof done !== 'undefined' ? done : client.noop;
|
7 | let topic = packet.topic.toString();
|
8 | const message = packet.payload;
|
9 | const { qos } = packet;
|
10 | const { messageId } = packet;
|
11 | const { options } = client;
|
12 | if (client.options.protocolVersion === 5) {
|
13 | let alias;
|
14 | if (packet.properties) {
|
15 | alias = packet.properties.topicAlias;
|
16 | }
|
17 | if (typeof alias !== 'undefined') {
|
18 | if (topic.length === 0) {
|
19 | if (alias > 0 && alias <= 0xffff) {
|
20 | const gotTopic = client['topicAliasRecv'].getTopicByAlias(alias);
|
21 | if (gotTopic) {
|
22 | topic = gotTopic;
|
23 | client.log('handlePublish :: topic complemented by alias. topic: %s - alias: %d', topic, alias);
|
24 | }
|
25 | else {
|
26 | client.log('handlePublish :: unregistered topic alias. alias: %d', alias);
|
27 | client.emit('error', new Error('Received unregistered Topic Alias'));
|
28 | return;
|
29 | }
|
30 | }
|
31 | else {
|
32 | client.log('handlePublish :: topic alias out of range. alias: %d', alias);
|
33 | client.emit('error', new Error('Received Topic Alias is out of range'));
|
34 | return;
|
35 | }
|
36 | }
|
37 | else if (client['topicAliasRecv'].put(topic, alias)) {
|
38 | client.log('handlePublish :: registered topic: %s - alias: %d', topic, alias);
|
39 | }
|
40 | else {
|
41 | client.log('handlePublish :: topic alias out of range. alias: %d', alias);
|
42 | client.emit('error', new Error('Received Topic Alias is out of range'));
|
43 | return;
|
44 | }
|
45 | }
|
46 | }
|
47 | client.log('handlePublish: qos %d', qos);
|
48 | switch (qos) {
|
49 | case 2: {
|
50 | options.customHandleAcks(topic, message, packet, (error, code) => {
|
51 | if (typeof error === 'number') {
|
52 | code = error;
|
53 | error = null;
|
54 | }
|
55 | if (error) {
|
56 | return client.emit('error', error);
|
57 | }
|
58 | if (validReasonCodes.indexOf(code) === -1) {
|
59 | return client.emit('error', new Error('Wrong reason code for pubrec'));
|
60 | }
|
61 | if (code) {
|
62 | client['_sendPacket']({ cmd: 'pubrec', messageId, reasonCode: code }, done);
|
63 | }
|
64 | else {
|
65 | client.incomingStore.put(packet, () => {
|
66 | client['_sendPacket']({ cmd: 'pubrec', messageId }, done);
|
67 | });
|
68 | }
|
69 | });
|
70 | break;
|
71 | }
|
72 | case 1: {
|
73 | options.customHandleAcks(topic, message, packet, (error, code) => {
|
74 | if (typeof error === 'number') {
|
75 | code = error;
|
76 | error = null;
|
77 | }
|
78 | if (error) {
|
79 | return client.emit('error', error);
|
80 | }
|
81 | if (validReasonCodes.indexOf(code) === -1) {
|
82 | return client.emit('error', new Error('Wrong reason code for puback'));
|
83 | }
|
84 | if (!code) {
|
85 | client.emit('message', topic, message, packet);
|
86 | }
|
87 | client.handleMessage(packet, (err) => {
|
88 | if (err) {
|
89 | return done && done(err);
|
90 | }
|
91 | client['_sendPacket']({ cmd: 'puback', messageId, reasonCode: code }, done);
|
92 | });
|
93 | });
|
94 | break;
|
95 | }
|
96 | case 0:
|
97 | client.emit('message', topic, message, packet);
|
98 | client.handleMessage(packet, done);
|
99 | break;
|
100 | default:
|
101 | client.log('handlePublish: unknown QoS. Doing nothing.');
|
102 | break;
|
103 | }
|
104 | };
|
105 | exports.default = handlePublish;
|
106 |
|
\ | No newline at end of file |