Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]:network.open wrong error argument on callback in connect using bun #439

Open
pierrejoye opened this issue Jan 21, 2025 · 0 comments
Open
Labels

Comments

@pierrejoye
Copy link

Description of the bug

Using bun, escpos-network behaves differently in the open method, specifically the error argument.

It is set to a Socket instance when using bun (1.1.45+196621f25), not using node (22.8.0)

I also opened a bug in bun.

Steps To Reproduce

I extracted the relevant code here and call both open (with a fix attempt) and open2 (current implementation in main). open2 fail with bun.

const net = require('net');
const EventEmitter = require('events');
const util = require('util');

function Network(address, port) {
  EventEmitter.call(this);
  this.address = address;
  this.port = port || 9100;
  this.device = new net.Socket();
  return this;
}

util.inherits(Network, EventEmitter);

Network.prototype.open = function(callback) {
  const self = this;

  this.device.on('error', (err) => {
    console.log('error', err);
    callback && callback(err, self.device);
  }).on('data', buf => {
    // console.log('printer say:', buf);
  }).connect(this.port, this.address, function(err) {
    if (err && err instanceof Error) {
      console.log('connect error', err);
      callback && callback(err, self.device);
      return;
    }
    self.emit('connect', self.device);
    callback && callback(null, self.device);
  });

  return this;
};

Network.prototype.open2 = function(callback){
    var self = this;
    this.device.on("error", (err) => {
      callback && callback(err, self.device);
    }).on('data', buf => {
      // console.log('printer say:', buf);
    }).connect(this.port, this.address, function(err){
      self.emit('connect', self.device);
      callback && callback(err, self.device);
    });
    return this;
  };

// Usage example
let  device = new Network('192.168.1.100', 9100);

device.open(function(error, device) {
  if (error) {
    console.log('Connection error open');
    return;
  }
  console.log('Connected to device open');
  device.end();
});
device = new Network('192.168.1.100', 9100);
device.open2(function(error, device) {
    if (error) {
      console.log('Connection error open2:');
      return;
    }
    console.log('Connected to device open2');
    device.end();
  });

Additional Information

No response

@pierrejoye pierrejoye added the bug label Jan 21, 2025
@pierrejoye pierrejoye changed the title [BUG]: [BUG]:network.open wrong error argument on callback in connect using bun Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant