diff --git a/.gitignore b/.gitignore index e20d12c..e0e7f16 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ node_modules/ coverage/ .nyc_output/ yarn.lock +package-lock.json *.tgz *.lock *.swp diff --git a/src/ISO8583.ts b/src/ISO8583.ts index a2116e2..d592e3a 100644 --- a/src/ISO8583.ts +++ b/src/ISO8583.ts @@ -24,8 +24,8 @@ import addStaticMetaData from './pack/addStaticMetaData'; */ export default class ISO8583 extends ISO8583Base { dataString: string = ''; - constructor(message?: Types.ISOMessageT, customFormats?: Types.CustomFormatsT, requiredFieldsSchema?: any) { - super(message, customFormats, requiredFieldsSchema); + constructor(message?: Types.ISOMessageT, customFormats?: Types.CustomFormatsT, requiredFieldsSchema?: any, config?: Types.KeyValueT) { + super(message, customFormats, requiredFieldsSchema, config); } static getFieldDescription( @@ -756,11 +756,11 @@ export default class ISO8583 extends ISO8583Base { return state; } else { return ( - header + - jxon.jsToString({ - MsgType: this.MsgType, - Fields: this.fields, - }) + header + + jxon.jsToString({ + MsgType: this.MsgType, + Fields: this.fields, + }) ); } } diff --git a/src/ISO8583Base.ts b/src/ISO8583Base.ts index 6f474e6..9a09f0d 100644 --- a/src/ISO8583Base.ts +++ b/src/ISO8583Base.ts @@ -93,8 +93,12 @@ export default class ISO8583Base { message?: ISO8583MessageType, customFormats?: Types.CustomFormatsT, requiredFieldsSchema?: Types.RequiredFieldSchemaT, + config?: Types.KeyValueT, ) { this.formats = customFormats || {}; + if (config) { + this.config = config; + } this.hasSpecialFields = false; if (Buffer.isBuffer(message)) { this.BufferMsg = message; diff --git a/src/unpack/unpack_0_127.ts b/src/unpack/unpack_0_127.ts index db41f1a..308d589 100644 --- a/src/unpack/unpack_0_127.ts +++ b/src/unpack/unpack_0_127.ts @@ -30,12 +30,11 @@ export default function (incoming: Buffer, isoJSON: Types.KeyValueStringT, confi const bitmap = T.getHex(slice).split('').map(Number); let thisBuff = incoming.slice(bitmapEnd, incoming.byteLength); - for (let i = 1; i < bitmap.length; i++) { - if (bitmap[i] === 1) { + for (let field = 2; field <= bitmap.length; field++) { + if (bitmap[field - 1] === 1) { // format defined - const field = i + 1; const this_format = this.formats[field] || formats[field]; - if (field === 127) { + if (!config.custom127Encoding && field === 127) { const get127Exts = this.unpack_127_1_63(thisBuff, isoJSON); if (get127Exts.error) { return get127Exts; @@ -64,17 +63,28 @@ export default function (incoming: Buffer, isoJSON: Types.KeyValueStringT, confi if (thisLen === 0) { return T.toErrorObject(['field ', field, ' format not implemented']); } else { - const len = thisBuff.slice(0, thisLen).toString(); - thisBuff = thisBuff.slice(thisLen, thisBuff.byteLength); - isoJSON[field] = thisBuff.slice(0, Number(len)).toString(); - thisBuff = thisBuff.slice(Number(len), thisBuff.byteLength); + const lenBuff: Buffer = thisBuff.subarray(0, thisLen); + const lenString: string = lenBuff.toString(); + let len: number = Number(lenString); + if (isNaN(len)) { // with certain lengths you get NAN and readUIntBE seems to work in those cases, not sure why... + len = lenBuff.readUIntBE(0, thisLen); + } + thisBuff = thisBuff.subarray(thisLen, thisBuff.byteLength); + + let encoding: string = 'hex'; + if (field === 127 && config.custom127Encoding) { + encoding = config.custom127Encoding; + } else if (config.bitmapEncoding) { + encoding = config.bitmapEncoding; + } + isoJSON[field] = thisBuff.subarray(0, len).toString(encoding); + thisBuff = thisBuff.subarray(Number(len), thisBuff.byteLength); } } } else return T.toErrorObject(['field ', field, ' format not implemented']); } } - this.remainingBuffer = thisBuff; return isoJSON; } else return T.toErrorObject(['expecting buffer but got ', typeof incoming]); };