nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.44k stars 276 forks source link

Extendeing Buffer #4408

Closed navycuda closed 3 weeks ago

navycuda commented 1 month ago

Node.js Version

v21.4.0

NPM Version

v10.2.4

Operating System

Darwin 23.3.0 - MacOS ARM64

Subsystem

buffer

Description

I would like to extend the Buffer class with some 24bit functions:

readInt24BE
readInt24LE
readUInt24BE
readUInt24LE
writeInt24BE
writeInt24LE
writeUInt24BE
writeUInt24LE

as well as

static from
static alloc

For instantiating my class.

However when I run any of the above, it will Show <ExtendedBuffer FF FF ... >, but none of the extended buffer methods are available.

Why this important:

I need to be able to read and write different values to an intel hex binary. In my cursor class, I have it setup to dynamically handle which method to call from the ExtendedBuffer depending on byte count, etc. Since it is possible to have 24Bit values, I wanted to extend the Buffer class so I wouldn't have to carve out a specific exception to the 24 bit values in the cursor itself, but let the ExtendedBuffer class handle all of the different possible datatypes.

Minimal Reproduction

No response

Output

No response

Before You Submit

RedYetiDev commented 1 month ago

It's hard for us to help without some example code.

From what I can tell, this issue might be better as a StackOverflow post.

navycuda commented 1 month ago

Here is my ExtendedBuffer class, when I instantiate one using the static methods from or alloc:

https://github.com/navycuda/intel_hex_386/blob/main/src/cursor/ExtendedBuffer.ts

And this is it in use:

https://github.com/navycuda/intel_hex_386/blob/main/src/cursor/Cursor.ts

RedYetiDev commented 1 month ago

If you're asking for general programming help, StackOverflow might be a better place to ask.

navycuda commented 1 month ago

If you're asking for general programming help, StackOverflow might be a better place to ask.

If you have nothing helpful to add, why bother? Just trying to bump your post count? If you actually read what I wrote you'd realize that this isn't a general programming question.

RedYetiDev commented 1 month ago

If you actually read what I wrote you'd realize that this isn't a general programming question.

I'm sorry if my comment upset you. To me, this issue seems like an issue that could find better help of StackOverflow. That doesn't seem the issue is incorrect, it just seems that we aren't the best people to help with it.

For one, your issue is in TypeScript, which influences all the results of the code, and the Node.js project doesn't maintain it.

I'm not trying to discard your issue, I just don't think we are best suited to answer your concern quickly and correctly.

By the way, none of us are "trying to bump [our] post count", we are all volunteers trying to help the community :-)

RedYetiDev commented 3 weeks ago

Can you reproduce this issue in JavaScript, or is it exclusively TypeScript? TypeScript alters the way the JS works, so if it's a TypeScript issue, you should reach out to them.

navycuda commented 3 weeks ago

It's a javascript issue. I believe the issue stems from Buffer not actually being a class, so I can't actually extend it.

RedYetiDev commented 3 weeks ago

I don't know if I would call it can issue, but when a Buffer is constructed, the following is in the constructor:

function Buffer(arg, encodingOrOffset, length) {
  showFlaggedDeprecation();
  // Common case.
  if (typeof arg === 'number') {
    if (typeof encodingOrOffset === 'string') {
      throw new ERR_INVALID_ARG_TYPE('string', 'string', arg);
    }
    return Buffer.alloc(arg);
  }
  return Buffer.from(arg, encodingOrOffset, length);
}

It always returns a buffer. AFAIK there isn't a way around this.

navycuda commented 3 weeks ago

So that means I can't actually extend the Buffer and I'll have to to come up with a different solution.

Thanks for the help!