Skip to content

Commit ae75c35

Browse files
Fix for babel/browser support
1 parent bf45d8f commit ae75c35

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

index.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ class CoderConst {
127127
oTools.iterate(val, (row) => { if (!oTools.isBoolean(row)) ret = false; });
128128
return ret;
129129
}
130+
131+
isBuffer(val) {
132+
return Buffer.isBuffer(val) || (val && val.constructor && oTools.isFunction(val.constructor.toString) && !val.constructor.toString().indexOf('function Buffer'));
133+
}
130134
}
131135

132136
class DataEncoder extends CoderConst {
@@ -142,7 +146,7 @@ class DataEncoder extends CoderConst {
142146

143147
auto(val) {
144148
if (this.isBigNumber(val) || oTools.isFloat(val) || oTools.isNumber(val) || val === Infinity || val === -Infinity) return this.int(val);
145-
if (Buffer.isBuffer(val)) return this.bin(val);
149+
if (this.isBuffer(val)) return this.bin(Buffer.from(val));
146150
if (oTools.isUndefined(val)) return this.undef();
147151
if (oTools.isBoolean(val)) return this.bool(val);
148152
if (oTools.isNull(val)) return this.nil();
@@ -269,8 +273,12 @@ class DataDecoder extends CoderConst {
269273
return this.decodeWLen(msg)[0];
270274
}
271275

276+
extract(msg, start, end) {
277+
return Buffer.from(msg.subarray(start, end));
278+
}
279+
272280
decodeWLen(msg) {
273-
if (!Buffer.isBuffer(msg) || msg.length === 0) return undefined;
281+
msg = Buffer.from(msg);
274282
const type = tools.bufToInt8U(msg);
275283
switch (type) {
276284
case this.type.UNDEFINED:
@@ -286,19 +294,19 @@ class DataDecoder extends CoderConst {
286294
return [true, 1];
287295

288296
case this.type.BINFLAGS:
289-
return [tools.bufToBinFlags(msg.subarray(1, 2)), 2];
297+
return [tools.bufToBinFlags(this.extract(msg, 1, 2)), 2];
290298

291299
case this.type.BINARY8:
292300
const bin8len = tools.bufToInt8U(msg, 1) + 2;
293-
return [msg.subarray(2, bin8len), bin8len];
301+
return [this.extract(msg, 2, bin8len), bin8len];
294302

295303
case this.type.BINARY16:
296304
const bin16len = tools.bufToInt16U(msg, 1) + 3;
297-
return [msg.subarray(3, bin16len), bin16len];
305+
return [this.extract(msg, 3, bin16len), bin16len];
298306

299307
case this.type.BINARY32:
300308
const bin32len = tools.bufToInt32U(msg, 1) + 5;
301-
return [msg.subarray(5, bin32len), bin32len];
309+
return [this.extract(msg, 5, bin32len), bin32len];
302310

303311
case this.type.NINT8:
304312
case this.type.INT8:
@@ -325,24 +333,24 @@ class DataDecoder extends CoderConst {
325333
case this.type.STRA8:
326334
case this.type.STR8:
327335
const str8len = tools.bufToInt8U(msg, 1) + 2;
328-
return [msg.subarray(2, str8len).toString(type === this.type.STRA8 ? 'ascii' : 'utf8'), str8len];
336+
return [this.extract(msg, 2, str8len).toString(type === this.type.STRA8 ? 'ascii' : 'utf8'), str8len];
329337

330338
case this.type.STRA16:
331339
case this.type.STR16:
332340
const str16len = tools.bufToInt16U(msg, 1) + 3;
333-
return [msg.subarray(3, str16len).toString(type === this.type.STRA16 ? 'ascii' : 'utf8'), str16len];
341+
return [this.extract(msg, 3, str16len).toString(type === this.type.STRA16 ? 'ascii' : 'utf8'), str16len];
334342

335343
case this.type.STRA32:
336344
case this.type.STR32:
337345
const str32len = tools.bufToInt32U(msg, 1) + 5;
338-
return [msg.subarray(5, str32len).toString(type === this.type.STRA32 ? 'ascii' : 'utf8'), str32len];
346+
return [this.extract(msg, 5, str32len).toString(type === this.type.STRA32 ? 'ascii' : 'utf8'), str32len];
339347

340348
case this.type.CHAR:
341-
return [msg.subarray(1, 2).toString('ascii'), 2];
349+
return [this.extract(msg, 1, 2).toString('ascii'), 2];
342350

343351
case this.type.BIGNUM:
344352
const bnlen = tools.bufToInt8U(msg, 1) + 2;
345-
return [new BigNumber((msg.subarray(2, bnlen)).toString('ascii')), bnlen];
353+
return [new BigNumber((this.extract(msg, 2, bnlen)).toString('ascii')), bnlen];
346354

347355
case this.type.NAN:
348356
return [NaN, 1];
@@ -360,7 +368,7 @@ class DataDecoder extends CoderConst {
360368
let alen = 1 + ((type === this.type.ARRAY8) ? 1 : type === this.type.ARRAY16 ? 2 : 4);
361369
const aret = oTools.iterate(
362370
tools[(type === this.type.ARRAY8) ? 'bufToInt8U' : type === this.type.ARRAY16 ? 'bufToInt16U' : 'bufToInt32U'](msg, 1), () => {
363-
const decoded = this.decodeWLen(msg.subarray(alen, msg.length));
371+
const decoded = this.decodeWLen(this.extract(msg, alen, msg.length));
364372
alen += decoded[1];
365373
return decoded[0];
366374
}, []);
@@ -372,9 +380,9 @@ class DataDecoder extends CoderConst {
372380
let olen = 1 + ((type === this.type.OBJECT8) ? 1 : type === this.type.OBJECT16 ? 2 : 4);
373381
const oret = oTools.iterate(
374382
tools[(type === this.type.OBJECT8) ? 'bufToInt8U' : type === this.type.OBJECT16 ? 'bufToInt16U' : 'bufToInt32U'](msg, 1), (_, __, iter) => {
375-
const key = this.decodeWLen(msg.subarray(olen, msg.length));
383+
const key = this.decodeWLen(this.extract(msg, olen, msg.length));
376384
olen += key[1];
377-
const val = this.decodeWLen(msg.subarray(olen, msg.length));
385+
const val = this.decodeWLen(this.extract(msg, olen, msg.length));
378386
olen += val[1];
379387
iter.key(key[0]);
380388
return val[0];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"bugs" : {
1111
"url": "https://github.com/VasiliyIsaichkin/osmium-coder/issues"
1212
},
13-
"version" : "0.2.3",
13+
"version" : "0.2.8",
1414
"main" : "index.js",
1515
"dependencies": {
1616
"bignumber.js": "^9.0.0",

0 commit comments

Comments
 (0)