Skip to content

Commit 9285e9d

Browse files
committed
Fixes encoder option must not modify the content of object to encode
1 parent 775553b commit 9285e9d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lib/binary_parser.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,16 +493,26 @@ Parser.prototype.generate = function(ctx) {
493493
};
494494

495495
Parser.prototype.generateEncode = function(ctx) {
496-
// Transform with the possibly provided encoder before encoding
497496
var varName = ctx.generateVariable(this.varName);
497+
var savVarName = ctx.generateTmpVariable();
498+
499+
// Transform with the possibly provided encoder before encoding
498500
if (this.options.encoder) {
501+
// Save varName before applying encoder
502+
ctx.pushCode("var {0} = {1};", savVarName, varName);
499503
this.generateEncoder(ctx, varName, this.options.encoder);
500504
}
501505

502506
if (this.type) {
503507
this["generate_encode" + this.type](ctx);
504508
}
505509

510+
if (this.options.encoder) {
511+
// Restore varName after encoder transformation so that next parsers will
512+
// have access to original field value (but not nested ones)
513+
ctx.pushCode("{0} = {1};", varName, savVarName);
514+
}
515+
506516
return this.generateEncodeNext(ctx);
507517
};
508518

0 commit comments

Comments
 (0)