Buffer.isBuffer is not a function erro in angular6 while using bencode
I am using bencode in aAngular6 and it is throwing an error Buffer.isBuffer is not a function
Here is my funciton in component.ts and is giving an error at this line const decodedData = _.decode(e.data, null, null, 'utf8');
M2M(url, callbacks) { const self = this; self.url = url; self.ws = null; if (!callbacks) { callbacks = {}; } self.listeners = {}; const identity = null; const packets = { 'null': 0, 'request_join': 1, 'request_identify': 2, 'welcome': 3, 'log': 4, 'request_send': 5, 'route': 6, 'ping': 7, 'pong': 8, 'set_identity': 9, 'request_open': 10, 'request_close': 11, 'request_close_all': 12, 'keep_alive': 13, 'notify_open': 14, 'request_login': 15, 'instruction': 16, 'notify_close': 19, 'request_leave': 20, 'route_control': 21, 'request_send_control': 22 }; const packets_lookup = {}; for (const key in packets) { packets_lookup[packets[key]] = key; } self.callback = function (name, data) { if (callbacks[name] !== undefined) { callbacks[name](data); } } ; self.connect = function () { self.identity = null; const ws = new WebSocket(self.url); ws.binaryType = 'arraybuffer'; self.ws = ws; ws.onmessage = function (e) { // console.log(e) const decodedData = _.decode(e.data, null, null, 'utf8'); self.onPacket(decodedData); } ; ws.onopen = function (e) { self.callback('connect', e); // self.sendPacket('request_join'); setInterval(self.sendKeepAlive, 15 * 1000); } ; ws.onclose = function () { self.callback('close', {}); } ; ws.onerror = function (error) { self.callback('error', { error: error }); } ; } ; self.sendKeepAlive = function () { if (self.ws && self.identity) { self.sendPacket('keep_alive'); } } ; self.close = function () { if (self.ws) { // console.log('leaving') self.sendPacket('request_leave'); self.ws.close(); } self.ws = null; } ; const handlers = { 'ping': function (packet_type, packet_body) { self.sendPacket('pong', [self.decode_utf8(packet_body[0])]); }, 'route': function (packet_type, packet_body) { const channel_no = packet_body[0]; const packet_data = packet_body[1]; const listeners = self.listeners[channel_no]; if (listeners) { for (let i = 0; i < listeners.length; i++) { try { listeners[i](channel_no, packet_data); } catch (err) { if (console) { // console.log(err); } } } } self.callback('route', { 'channel': channel_no, 'payload': packet_data }); }, 'welcome': function () { self.callback('onready', {}); }, 'set_identity': function (packet_type, packet_body) { const identity = self.decode_utf8(packet_body[0]); self.identity = identity; self.callback('identity', { 'identity': identity }); }, 'log': function (packet_type, packet_body) { // if (console) { // console.log(self.decode_utf8(packet_body[0])); // } }, 'notify_open': function (packet_type, packet_body) { self.callback('channel_open', { port: packet_body[0] }); }, 'notify_close': function (packet_type, packet_body) { self.callback('channel_close', { port: packet_body[0] }); }, 'request_send': function (packet_type, packet_body) { self.callback('request_send', { body: packet_body }); }, 'request_close': function (packet_type, packet_body) { self.callback('request_close', { body: packet_body }); }, 'request_identify': function (packet_type, packet_body) { self.callback('request_identity', { body: packet_body }); } }; function makePacket(packet_type_number, packet_body) { let packet; if (packet_body !== undefined) { packet = [packet_type_number]; packet.push.apply(packet, packet_body); } else { packet = [packet_type_number]; } const packet_data = _.encode(packet); return packet_data; } self.on = function (name, callback) { callbacks[name] = callback; return this; } ; self.sendPacket = function (packet_type, packet_body) { const packet_type_number = packets[packet_type]; const packet = makePacket(packet_type_number, packet_body); if (self.ws.readyState === 1) { self.ws.send(packet); } } ; self.writeChannel = function (channel_no, payload, key) { self.sendPacket('route', [channel_no, payload, key]); } ; self.writeControl = function (channel_no, payload) { self.sendPacket('request_send_control', [channel_no, payload]); } ; self.onPacket = function (packet) { if (packet) { const packet_type = packet[0]; const packet_body = packet.slice(1); const handler = handlers[packets_lookup[packet_type]]; if (handler === undefined) { return; } handler(packet_type, packet_body); } } ; self.addListener = function (channel_no, callback) { if (self.listeners[channel_no] === undefined) { self.listeners[channel_no] = []; } self.listeners[channel_no].push(callback); } ; return self; }
I guess you are using the angular framework in the browser. For this you would have to compile bencode using something like browserify or webpack since we are using APIs only node.js knows (e.g. Buffer).
I guess you are using the angular framework in the browser. For this you would have to compile
bencodeusing something like browserify or webpack since we are using APIs only node.js knows (e.g. Buffer).
Angular5 already use webpack itself so why i need to compile it with Webpack explicitly.
Okay, I just guessed (hoped) that webpack would use feross/buffer to implement the Buffer api in browsers. Looks like I was wrong on that point then, sorry! I'd have to try this myself and see what gets generated by webpack and how to fix this.