Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First contribution #132

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_modules/
coverage/
.nyc_output/
yarn.lock
package-lock.json
*.tgz
*.lock
*.swp
Expand Down
14 changes: 7 additions & 7 deletions src/ISO8583.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
})
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/ISO8583Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 19 additions & 9 deletions src/unpack/unpack_0_127.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
};