Closed navycuda closed 3 weeks 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.
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
export declare class ExtendedBuffer extends Buffer {
constructor();
static from(arrayBuffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, length?: number): ExtendedBuffer;
static from(data: Uint8Array | ReadonlyArray<number>): ExtendedBuffer;
static from(data: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>): ExtendedBuffer;
static from(str: WithImplicitCoercion<string> | {
[Symbol.toPrimitive](hint: 'string'): string;
}, encoding?: BufferEncoding): ExtendedBuffer;
static alloc(size: number, fill?: string | Buffer | number, encoding?: BufferEncoding): ExtendedBuffer;
readInt24BE: ReadInteger;
readInt24LE: ReadInteger;
readUInt24BE: ReadInteger;
readUInt24LE: ReadInteger;
writeInt24BE: WriteInteger;
writeInt24LE: WriteInteger;
writeUInt24BE: WriteInteger;
writeUInt24LE: WriteInteger;
}
And this is it in use:
https://github.com/navycuda/intel_hex_386/blob/main/src/cursor/Cursor.ts
or - Integer as example
function integer(signed:boolean,byteLength:ByteLength):number;
function integer(signed:boolean,byteLength:ByteLength,elementLength:number):number[];
function integer(signed:boolean,byteLength:ByteLength,elementLength?:number):number|number[]{
const u = signed ? '' : 'U';
const method = `read${u}Int${byteLength * 8}${byteOrder}` as keyof Buffer;
if (elementLength === undefined){
const buffer = getExtendedBuffer(byteLength);
return (buffer[method] as any)(0,byteLength);
}
const integers:number[] = [];
for (let i = 0; i < elementLength; i++){
integers.push(integer(signed,byteLength));
}
return integers;
}
If you're asking for general programming help, StackOverflow might be a better place to ask.
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.
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 :-)
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.
It's a javascript issue. I believe the issue stems from Buffer not actually being a class, so I can't actually extend it.
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.
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!
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:
as well as
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