diff --git a/.gitignore b/.gitignore index 4fedc91..44c6b2e 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,5 @@ typings/ # dotenv environment variables file .env +# Intellij +.idea \ No newline at end of file diff --git a/dist/src/message-builder.d.ts b/dist/src/message-builder.d.ts index f154c94..7d0d6e2 100644 --- a/dist/src/message-builder.d.ts +++ b/dist/src/message-builder.d.ts @@ -33,4 +33,4 @@ * */ import { Message } from './message-constants'; -export declare function getMessage(msg: Message, isAck: boolean): Buffer; +export declare function getMessage(message: Message, isAck: boolean): Buffer; diff --git a/dist/src/message-builder.js b/dist/src/message-builder.js index 63b0c5e..3a5017d 100644 --- a/dist/src/message-builder.js +++ b/dist/src/message-builder.js @@ -38,16 +38,16 @@ const message_constants_1 = require("./message-constants"); const utils_1 = require("./utils"); const constants_1 = require("./constants"); const message_validator_1 = require("./message-validator"); -function getMessage(msg, isAck) { - const message = msg; +function getMessage(message, isAck) { let action = message.action; // convert action to write ack if necessary if (message.isWriteAck && !utils_1.isWriteAck(message.action)) { action = utils_1.ACTION_TO_WRITE_ACK[message.action]; } + const actionTopic = message_constants_1.ACTIONS[message.topic]; if (message.isAck || isAck) { action |= 0x80; - if (message_constants_1.ACTIONS[message.topic][message.action] === undefined) { + if (!actionTopic || actionTopic[message.action] === undefined) { throw new Error(`message ${message_constants_1.TOPIC[message.topic]} ${message.action} should not have an ack`); } } @@ -60,7 +60,7 @@ function getMessage(msg, isAck) { } const metaError = message_validator_1.validateMeta(message.topic, action, meta); if (metaError) { - throw new Error(`invalid ${message_constants_1.TOPIC[message.topic]} ${message_constants_1.ACTIONS[message.topic][action] || action}: ${metaError}`); + throw new Error(`invalid ${message_constants_1.TOPIC[message.topic]} ${actionTopic[action] || action}: ${metaError}`); } const metaStr = JSON.stringify(meta); const metaBuff = metaStr === '{}' ? null : Buffer.from(metaStr, 'utf8'); diff --git a/dist/src/message-builder.js.map b/dist/src/message-builder.js.map index 6d6b32b..ed429d0 100644 --- a/dist/src/message-builder.js.map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MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;AACpC,CAAC;AAED,kBAAmB,GAAY,EAAE,KAAY,EAAE,MAAmB,EAAE,IAAmB,EAAE,OAAsB;IAC7G,MAAM,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;IACzC,MAAM,aAAa,GAAG,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;IAClD,MAAM,mBAAmB,GAAG,yBAAa,GAAG,UAAU,GAAG,aAAa,CAAA;IACtE,MAAM,aAAa,GAAW,MAAM,CAAC,WAAW,CAAC,mBAAmB,CAAC,CAAA;IAErE,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,KAAK,CAAA;IAC9C,aAAa,CAAC,CAAC,CAAC,GAAG,MAAM,CAAA;IACzB,aAAa,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;IAC3C,aAAa,CAAC,WAAW,CAAC,aAAa,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;IAE9C,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QACT,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,yBAAa,CAAC,CAAA;IACzC,CAAC;IACD,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;QACZ,OAAO,CAAC,IAAI,CAAC,aAAa,EAAE,yBAAa,GAAG,UAAU,CAAC,CAAA;IACzD,CAAC;IACD,MAAM,CAAC,aAAa,CAAA;AACtB,CAAC"} \ No newline at end of file diff --git a/dist/test/message-parserSpec.js b/dist/test/message-parserSpec.js index e821522..527f6f7 100644 --- a/dist/test/message-parserSpec.js +++ b/dist/test/message-parserSpec.js @@ -15,7 +15,7 @@ describe('message parser', () => { } it(`parses ${message_constants_1.TOPIC[topic]} messages ${authAction} correctly`, () => { const result = message_parser_1.parse(spec.urp.value)[0]; - chai_1.expect(result.parseError).to.be.undefined; + chai_1.expect(result.parseError).to.be.an('undefined'); if (!result.parseError && (!result.payloadEncoding || result.payloadEncoding === message_constants_1.PAYLOAD_ENCODING.JSON)) { chai_1.expect(message_parser_1.parseData(result)).to.equal(true); diff --git a/dist/test/message-parserSpec.js.map b/dist/test/message-parserSpec.js.map index 32ff0c0..f3a410e 100644 --- a/dist/test/message-parserSpec.js.map +++ b/dist/test/message-parserSpec.js.map @@ -1 +1 @@ -{"version":3,"file":"message-parserSpec.js","sourceRoot":"","sources":["../../test/message-parserSpec.ts"],"names":[],"mappings":";;AAAA,gEAGiC;AACjC,0DAAwD;AACxD,yCAAqC;AACrC,+BAA6B;AAE7B,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC9B,GAAG,CAAC,CAAC,MAAM,QAAQ,IAAI,mBAAQ,CAAC,CAAC,CAAC;QAChC,MAAM,KAAK,GAAU,MAAM,CAAC,QAAQ,CAAC,CAAA;QACrC,GAAG,CAAC,CAAC,MAAM,UAAU,IAAI,mBAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACzC,MAAM,IAAI,GAAG,mBAAQ,CAAC,KAAK,CAAC,CAAC,UAAU,CAAC,CAAA;YACxC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;gBACV,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,yBAAK,CAAC,KAAK,CAAC,EAAE,UAAU,EAAE,cAAc,CAAC,CAAA;gBACpE,QAAQ,CAAA;YACV,CAAC;YACD,EAAE,CAAE,UAAU,yBAAK,CAAC,KAAK,CAAC,aAAa,UAAU,YAAY,EAAE,GAAG,EAAE;gBAClE,MAAM,MAAM,GAAG,sBAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;gBACvC,aAAM,CAAC,MAAM,CA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U,YAAY,EAAE,GAAG,EAAE;gBAClE,MAAM,MAAM,GAAG,sBAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;gBACvC,aAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,WAAW,CAAC,CAAA;gBAC/C,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU;oBACpB,CAAC,CAAC,MAAM,CAAC,eAAe,IAAI,MAAM,CAAC,eAAe,KAAK,oCAAgB,CAAC,IAAI,CAC9E,CAAC,CAAC,CAAC;oBACD,aAAM,CAAC,0BAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;gBAC1C,CAAC;gBACD,aAAM,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;YAC5C,CAAC,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;AAEH,CAAC,CAAC,CAAA"} \ No newline at end of file diff --git a/dist/test/messagesSpec.js b/dist/test/messagesSpec.js index 979f26a..5b455ce 100644 --- a/dist/test/messagesSpec.js +++ b/dist/test/messagesSpec.js @@ -9,7 +9,7 @@ describe('protocol', () => { for (const action in message_constants_1.ACTIONS[topic]) { if (isNaN(Number(action))) { it(`contains message for ${message_constants_1.TOPIC[topic]} with action ${action} in protocol`, () => { - chai_1.expect(messages_1.MESSAGES[topic][action]).not.to.be.undefined; + chai_1.expect(messages_1.MESSAGES[topic][action]).not.to.be.an('undefined'); }); } } diff --git a/dist/test/messagesSpec.js.map b/dist/test/messagesSpec.js.map index 8403d31..29a3288 100644 --- a/dist/test/messagesSpec.js.map +++ b/dist/test/messagesSpec.js.map @@ -1 +1 @@ -{"version":3,"file":"messagesSpec.js","sourceRoot":"","sources":["../../test/messagesSpec.ts"],"names":[],"mappings":";;AAAA,+BAA6B;AAE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CAAA;gBACzD,CAAC;YACH,CAAC,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;AACH,CAAC,CAAC,CAAA"} \ No newline at end of file diff --git a/src/message-builder.ts b/src/message-builder.ts index f927887..3ac72e3 100644 --- a/src/message-builder.ts +++ b/src/message-builder.ts @@ -58,8 +58,7 @@ import { hasPayload, } from './message-validator' -export function getMessage (msg: Message, isAck: boolean): Buffer { - const message = msg as any +export function getMessage (message: Message, isAck: boolean): Buffer { let action = message.action // convert action to write ack if necessary @@ -67,9 +66,11 @@ export function getMessage (msg: Message, isAck: boolean): Buffer { action = ACTION_TO_WRITE_ACK[message.action] } + const actionTopic = ACTIONS[message.topic] + if (message.isAck || isAck) { action |= 0x80 - if ((ACTIONS as any)[message.topic][message.action] === undefined) { + if (!actionTopic || actionTopic[message.action] === undefined) { throw new Error(`message ${TOPIC[message.topic]} ${message.action} should not have an ack`) } } @@ -84,7 +85,7 @@ export function getMessage (msg: Message, isAck: boolean): Buffer { const metaError = validateMeta(message.topic, action, meta) if (metaError) { - throw new Error(`invalid ${TOPIC[message.topic]} ${(ACTIONS as any)[message.topic][action] || action}: ${metaError}`) + throw new Error(`invalid ${TOPIC[message.topic]} ${actionTopic[action] || action}: ${metaError}`) } const metaStr = JSON.stringify(meta) diff --git a/src/message-constants.ts b/src/message-constants.ts index 10ee341..005ba38 100644 --- a/src/message-constants.ts +++ b/src/message-constants.ts @@ -97,7 +97,7 @@ export interface RecordAckMessage extends RecordMessage { } export interface ParseError { - parseError: boolean + parseError: true action: PARSER_ACTIONS parsedMessage: Message diff --git a/test/message-parserSpec.ts b/test/message-parserSpec.ts index df5760e..f3a6053 100644 --- a/test/message-parserSpec.ts +++ b/test/message-parserSpec.ts @@ -17,7 +17,7 @@ describe('message parser', () => { } it (`parses ${TOPIC[topic]} messages ${authAction} correctly`, () => { const result = parse(spec.urp.value)[0] - expect(result.parseError).to.be.undefined + expect(result.parseError).to.be.an('undefined') if (!result.parseError && (!result.payloadEncoding || result.payloadEncoding === PAYLOAD_ENCODING.JSON) ) { diff --git a/test/messagesSpec.ts b/test/messagesSpec.ts index 5735ff6..5f8f00c 100644 --- a/test/messagesSpec.ts +++ b/test/messagesSpec.ts @@ -16,7 +16,7 @@ describe('protocol', () => { for (const action in ACTIONS[topic]) { if (isNaN(Number(action))) { it (`contains message for ${TOPIC[topic]} with action ${action} in protocol`, () => { - expect(MESSAGES[topic][action]).not.to.be.undefined + expect(MESSAGES[topic][action]).not.to.be.an('undefined') }) } }