{"action":"created","issue":{"url":"https://api.github.com/repos/keichi/binary-parser/issues/60","repository_url":"https://api.github.com/repos/keichi/binary-parser","labels_url":"https://api.github.com/repos/keichi/binary-parser/issues/60/labels{/name}","comments_url":"https://api.github.com/repos/keichi/binary-parser/issues/60/comments","events_url":"https://api.github.com/repos/keichi/binary-parser/issues/60/events","html_url":"https://github.com/keichi/binary-parser/issues/60","id":284303941,"number":60,"title":"Buffer is not defined on generated code calling compiled","user":{"login":"dror1544","id":15153757,"avatar_url":"https://avatars2.githubusercontent.com/u/15153757?v=4","gravatar_id":"","url":"https://api.github.com/users/dror1544","html_url":"https://github.com/dror1544","followers_url":"https://api.github.com/users/dror1544/followers","following_url":"https://api.github.com/users/dror1544/following{/other_user}","gists_url":"https://api.github.com/users/dror1544/gists{/gist_id}","starred_url":"https://api.github.com/users/dror1544/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dror1544/subscriptions","organizations_url":"https://api.github.com/users/dror1544/orgs","repos_url":"https://api.github.com/users/dror1544/repos","events_url":"https://api.github.com/users/dror1544/events{/privacy}","received_events_url":"https://api.github.com/users/dror1544/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2017-12-23T12:24:32Z","updated_at":"2018-04-15T22:14:04Z","closed_at":null,"author_association":"NONE","body":"I am trying to use Parser in Angular 5 (using @types/binary-parser). \r\nIn order to test the module first I use the IP header example. \r\nwhen doing that I get the following error \r\n\r\n> ERROR ReferenceError: Buffer is not defined\r\n    at Parser.eval [as compiled] (eval at Parser.compile (webpack-internal:///../../../../binary-parser/lib/binary_parser.js), <anonymous>:3:1)\r\n    at Parser.parse (webpack-internal:///../../../../binary-parser/lib/binary_parser.js:337)\r\n    at AppComponent.parse (webpack-internal:///../../../../../src/app/app.component.ts:47)\r\n    at AppComponent.ngOnInit (webpack-internal:///../../../../../src/app/app.component.ts:21)\r\n    at checkAndUpdateDirectiveInline (webpack-internal:///../../../core/esm5/core.js:12596)\r\n    at checkAndUpdateNodeInline (webpack-internal:///../../../core/esm5/core.js:14123)\r\n    at checkAndUpdateNode (webpack-internal:///../../../core/esm5/core.js:14066)\r\n    at debugCheckAndUpdateNode (webpack-internal:///../../../core/esm5/core.js:14959)\r\n    at debugCheckDirectivesFn (webpack-internal:///../../../core/esm5/core.js:14900)\r\n    at Object.eval [as updateDirectives] (ng:///AppModule/AppComponent_Host.ngfactory.js:8)\r\n\r\nIf I call Buffer,isBuffer(buf) on my code before calling .parse() it works just fine. I removed this piece of code from binary_parser.js in function getCode and the generated code works fine.\r\n It seems that I am missing something in the declaration of Buffer. It is included in my app.Module. so I can not put the finger on what am I missing?\r\n This is my code as I used it:\r\n\r\n`import { Parser } from 'binary-parser';\r\nimport { Component } from '@angular/core';\r\nimport { OnInit } from '@angular/core/src/metadata/lifecycle_hooks';\r\nimport { Buffer } from 'buffer';\r\n\r\n@Component({\r\n  selector: 'app-root',\r\n  templateUrl: './app.component.html',\r\n  styleUrls: ['./app.component.css']\r\n})\r\nexport class AppComponent implements OnInit{\r\n  \r\n  title = 'app';\r\n\r\n  ngOnInit(): void {\r\n    this.parse()\r\n  }\r\n  parse(){\r\n\r\n    let ipHeader = new Parser()\r\n    .endianess('big')\r\n    .bit4('version')\r\n    .bit4('headerLength')\r\n    .uint8('tos')\r\n    .uint16('packetLength')\r\n    .uint16('id')\r\n    .bit3('offset')\r\n    .bit13('fragOffset')\r\n    .uint8('ttl')\r\n    .uint8('protocol')\r\n    .uint16('checksum')\r\n    .array('src', {\r\n        type: 'uint8',\r\n        length: 4\r\n    })\r\n    .array('dst', {\r\n        type: 'uint8',\r\n        length: 4\r\n    });\r\n\r\n    const buf = new Buffer('450002c5939900002c06ef98adc24f6c850186d1', 'hex');\r\n    Buffer.isBuffer(buf);\r\n   // Parse buffer and show result\r\n   console.log(ipHeader.parse(buf));\r\n  }\r\n\r\n}`\r\n"},"comment":{"url":"https://api.github.com/repos/keichi/binary-parser/issues/comments/381442299","html_url":"https://github.com/keichi/binary-parser/issues/60#issuecomment-381442299","issue_url":"https://api.github.com/repos/keichi/binary-parser/issues/60","id":381442299,"user":{"login":"Xample","id":944605,"avatar_url":"https://avatars1.githubusercontent.com/u/944605?v=4","gravatar_id":"","url":"https://api.github.com/users/Xample","html_url":"https://github.com/Xample","followers_url":"https://api.github.com/users/Xample/followers","following_url":"https://api.github.com/users/Xample/following{/other_user}","gists_url":"https://api.github.com/users/Xample/gists{/gist_id}","starred_url":"https://api.github.com/users/Xample/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Xample/subscriptions","organizations_url":"https://api.github.com/users/Xample/orgs","repos_url":"https://api.github.com/users/Xample/repos","events_url":"https://api.github.com/users/Xample/events{/privacy}","received_events_url":"https://api.github.com/users/Xample/received_events","type":"User","site_admin":false},"created_at":"2018-04-15T22:14:04Z","updated_at":"2018-04-15T22:14:04Z","author_association":"NONE","body":"The code is ran at line [322](https://github.com/keichi/binary-parser/blob/master/lib/binary_parser.js#L322)\r\n within ```this``` context, we just need to import and provide the Buffer object in another context.\r\n`var Buffer = require(\"buffer\").Buffer;\r\n`\r\n`  this.compiled = vm.runInNewContext(src,{Buffer});\r\n`\r\nI will open a pull request"}}
{"actor":{"display_login":"Xample"}}