chenyc
2025-12-09 545c24c6a711d71b65f3d4e8122fee3837fb1edc
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReasonCodes = void 0;
const shared_1 = require("../shared");
exports.ReasonCodes = {
    0: '',
    1: 'Unacceptable protocol version',
    2: 'Identifier rejected',
    3: 'Server unavailable',
    4: 'Bad username or password',
    5: 'Not authorized',
    16: 'No matching subscribers',
    17: 'No subscription existed',
    128: 'Unspecified error',
    129: 'Malformed Packet',
    130: 'Protocol Error',
    131: 'Implementation specific error',
    132: 'Unsupported Protocol Version',
    133: 'Client Identifier not valid',
    134: 'Bad User Name or Password',
    135: 'Not authorized',
    136: 'Server unavailable',
    137: 'Server busy',
    138: 'Banned',
    139: 'Server shutting down',
    140: 'Bad authentication method',
    141: 'Keep Alive timeout',
    142: 'Session taken over',
    143: 'Topic Filter invalid',
    144: 'Topic Name invalid',
    145: 'Packet identifier in use',
    146: 'Packet Identifier not found',
    147: 'Receive Maximum exceeded',
    148: 'Topic Alias invalid',
    149: 'Packet too large',
    150: 'Message rate too high',
    151: 'Quota exceeded',
    152: 'Administrative action',
    153: 'Payload format invalid',
    154: 'Retain not supported',
    155: 'QoS not supported',
    156: 'Use another server',
    157: 'Server moved',
    158: 'Shared Subscriptions not supported',
    159: 'Connection rate exceeded',
    160: 'Maximum connect time',
    161: 'Subscription Identifiers not supported',
    162: 'Wildcard Subscriptions not supported',
};
const handleAck = (client, packet) => {
    const { messageId } = packet;
    const type = packet.cmd;
    let response = null;
    const cb = client.outgoing[messageId] ? client.outgoing[messageId].cb : null;
    let err = null;
    if (!cb) {
        client.log('_handleAck :: Server sent an ack in error. Ignoring.');
        return;
    }
    client.log('_handleAck :: packet type', type);
    switch (type) {
        case 'pubcomp':
        case 'puback': {
            const pubackRC = packet.reasonCode;
            if (pubackRC && pubackRC > 0 && pubackRC !== 16) {
                err = new shared_1.ErrorWithReasonCode(`Publish error: ${exports.ReasonCodes[pubackRC]}`, pubackRC);
                client['_removeOutgoingAndStoreMessage'](messageId, () => {
                    cb(err, packet);
                });
            }
            else {
                client['_removeOutgoingAndStoreMessage'](messageId, cb);
            }
            break;
        }
        case 'pubrec': {
            response = {
                cmd: 'pubrel',
                qos: 2,
                messageId,
            };
            const pubrecRC = packet.reasonCode;
            if (pubrecRC && pubrecRC > 0 && pubrecRC !== 16) {
                err = new shared_1.ErrorWithReasonCode(`Publish error: ${exports.ReasonCodes[pubrecRC]}`, pubrecRC);
                client['_removeOutgoingAndStoreMessage'](messageId, () => {
                    cb(err, packet);
                });
            }
            else {
                client['_sendPacket'](response);
            }
            break;
        }
        case 'suback': {
            delete client.outgoing[messageId];
            client.messageIdProvider.deallocate(messageId);
            const granted = packet.granted;
            for (let grantedI = 0; grantedI < granted.length; grantedI++) {
                const subackRC = granted[grantedI];
                if ((subackRC & 0x80) !== 0) {
                    err = new Error(`Subscribe error: ${exports.ReasonCodes[subackRC]}`);
                    err.code = subackRC;
                    const topics = client.messageIdToTopic[messageId];
                    if (topics) {
                        topics.forEach((topic) => {
                            delete client['_resubscribeTopics'][topic];
                        });
                    }
                }
            }
            delete client.messageIdToTopic[messageId];
            client['_invokeStoreProcessingQueue']();
            cb(err, packet);
            break;
        }
        case 'unsuback': {
            delete client.outgoing[messageId];
            client.messageIdProvider.deallocate(messageId);
            client['_invokeStoreProcessingQueue']();
            cb(null, packet);
            break;
        }
        default:
            client.emit('error', new Error('unrecognized packet type'));
    }
    if (client.disconnecting && Object.keys(client.outgoing).length === 0) {
        client.emit('outgoingEmpty');
    }
};
exports.default = handleAck;
//# sourceMappingURL=ack.js.map