UNPKG

4.47 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ReasonCodes = void 0;
4exports.ReasonCodes = {
5 0: '',
6 1: 'Unacceptable protocol version',
7 2: 'Identifier rejected',
8 3: 'Server unavailable',
9 4: 'Bad username or password',
10 5: 'Not authorized',
11 16: 'No matching subscribers',
12 17: 'No subscription existed',
13 128: 'Unspecified error',
14 129: 'Malformed Packet',
15 130: 'Protocol Error',
16 131: 'Implementation specific error',
17 132: 'Unsupported Protocol Version',
18 133: 'Client Identifier not valid',
19 134: 'Bad User Name or Password',
20 135: 'Not authorized',
21 136: 'Server unavailable',
22 137: 'Server busy',
23 138: 'Banned',
24 139: 'Server shutting down',
25 140: 'Bad authentication method',
26 141: 'Keep Alive timeout',
27 142: 'Session taken over',
28 143: 'Topic Filter invalid',
29 144: 'Topic Name invalid',
30 145: 'Packet identifier in use',
31 146: 'Packet Identifier not found',
32 147: 'Receive Maximum exceeded',
33 148: 'Topic Alias invalid',
34 149: 'Packet too large',
35 150: 'Message rate too high',
36 151: 'Quota exceeded',
37 152: 'Administrative action',
38 153: 'Payload format invalid',
39 154: 'Retain not supported',
40 155: 'QoS not supported',
41 156: 'Use another server',
42 157: 'Server moved',
43 158: 'Shared Subscriptions not supported',
44 159: 'Connection rate exceeded',
45 160: 'Maximum connect time',
46 161: 'Subscription Identifiers not supported',
47 162: 'Wildcard Subscriptions not supported',
48};
49const handleAck = (client, packet) => {
50 const { messageId } = packet;
51 const type = packet.cmd;
52 let response = null;
53 const cb = client.outgoing[messageId] ? client.outgoing[messageId].cb : null;
54 let err;
55 if (!cb) {
56 client.log('_handleAck :: Server sent an ack in error. Ignoring.');
57 return;
58 }
59 client.log('_handleAck :: packet type', type);
60 switch (type) {
61 case 'pubcomp':
62 case 'puback': {
63 const pubackRC = packet.reasonCode;
64 if (pubackRC && pubackRC > 0 && pubackRC !== 16) {
65 err = new Error(`Publish error: ${exports.ReasonCodes[pubackRC]}`);
66 err.code = pubackRC;
67 client['_removeOutgoingAndStoreMessage'](messageId, () => {
68 cb(err, packet);
69 });
70 }
71 else {
72 client['_removeOutgoingAndStoreMessage'](messageId, cb);
73 }
74 break;
75 }
76 case 'pubrec': {
77 response = {
78 cmd: 'pubrel',
79 qos: 2,
80 messageId,
81 };
82 const pubrecRC = packet.reasonCode;
83 if (pubrecRC && pubrecRC > 0 && pubrecRC !== 16) {
84 err = new Error(`Publish error: ${exports.ReasonCodes[pubrecRC]}`);
85 err.code = pubrecRC;
86 client['_removeOutgoingAndStoreMessage'](messageId, () => {
87 cb(err, packet);
88 });
89 }
90 else {
91 client['_sendPacket'](response);
92 }
93 break;
94 }
95 case 'suback': {
96 delete client.outgoing[messageId];
97 client.messageIdProvider.deallocate(messageId);
98 const granted = packet.granted;
99 for (let grantedI = 0; grantedI < granted.length; grantedI++) {
100 if ((granted[grantedI] & 0x80) !== 0) {
101 const topics = client.messageIdToTopic[messageId];
102 if (topics) {
103 topics.forEach((topic) => {
104 delete client['_resubscribeTopics'][topic];
105 });
106 }
107 }
108 }
109 delete client.messageIdToTopic[messageId];
110 client['_invokeStoreProcessingQueue']();
111 cb(null, packet);
112 break;
113 }
114 case 'unsuback': {
115 delete client.outgoing[messageId];
116 client.messageIdProvider.deallocate(messageId);
117 client['_invokeStoreProcessingQueue']();
118 cb(null);
119 break;
120 }
121 default:
122 client.emit('error', new Error('unrecognized packet type'));
123 }
124 if (client.disconnecting && Object.keys(client.outgoing).length === 0) {
125 client.emit('outgoingEmpty');
126 }
127};
128exports.default = handleAck;
129//# sourceMappingURL=ack.js.map
\No newline at end of file