keichi / binary-parser

A blazing-fast declarative parser builder for binary data
MIT License
868 stars 136 forks source link

TextDecoder is not defined #142

Closed agbianchessi closed 4 years ago

agbianchessi commented 4 years ago

I am running nodejs version 10. TextDecoder has been added to global after v11: Globals/TextDecoder...

I suggest you either to change the code to make it more compatible to older nodejs or to document the minimal nodejs version (>= 11.0)...

mohd-akram commented 4 years ago

Possible fix:

diff --git a/lib/binary_parser.ts b/lib/binary_parser.ts
index f037691..6d77392 100644
--- a/lib/binary_parser.ts
+++ b/lib/binary_parser.ts
@@ -651,7 +651,14 @@ export class Parser {
   }

   compile() {
-    this.compiled = new Function('buffer', 'constructorFn', this.getCode());
+    this.compiled = new Function(
+      'TextDecoder',
+      `return function (buffer, constructorFn) { ${this.getCode()} };`
+    )(
+      typeof TextDecoder === 'undefined'
+        ? require('util').TextDecoder
+        : TextDecoder
+    );
   }

   sizeOf(): number {
DimaNazdratenko commented 4 years ago

UP! I have the same problem. It is not ok that you broke package on the old version too. I tried to reinstall more old version like 1.3.2 or 1.4.0 and I have the same problem with "TextDecoder is not defined". I use v10 node. Fix please, because all was worked on 1.4.0 binary parser.

DimaNazdratenko commented 4 years ago

I am running nodejs version 10. TextDecoder has been added to global after v11: Globals/TextDecoder...

I suggest you either to change the code to make it more compatible to older nodejs or to document the minimal nodejs version (>= 11.0)...

I have the same problem and you can decide this by use more older version binary-parser, like 1.4.0 And remove "^" from package.json, because their new minor version broke our applications :)

keichi commented 4 years ago

Sorry about this, I've opened a PR based on @mohd-akram's patch that should fix this issue.

keichi commented 4 years ago

This should have been fixed in v1.6.2. Let me know if the problem still exists.