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

browser.js: .compare() unsupported syntax #7

Closed
cesare-montresor opened this issue Jan 17, 2023 · 2 comments
Closed

browser.js: .compare() unsupported syntax #7

cesare-montresor opened this issue Jan 17, 2023 · 2 comments

Comments

@cesare-montresor
Copy link

To my understanding, it seams that the objects created by b4a.from() don't expose the same signatures as nodejs Buffer leading to potentially non-crossplatform code.
I have no bulletproof idea on how to fix it, however, I wonder if attempting to inject methods to the javascript TypedArray prototype, could deliver a clean solution.

Works on NodeJS, but doesn't on browser.

const b4a = require('b4a')

const buf = b4a.from('a lot of binary data coming from tar file')
const USTAR_MAGIC = b4a.from('ustar\x00')

if ( USTAR_MAGIC.compare(buf, MAGIC_OFFSET, MAGIC_OFFSET + 6) === 0 ){
    console.log("eureka!")
}

src: https://github.com/mafintosh/tar-stream/blob/ca0e270f11dc61f507f7972e53071b8fa99e66bf/headers.js#L265

Works on browser.

const b4a = require('b4a')

const buf = b4a.from('a lot of binary data coming from tar file')
const USTAR_MAGIC = b4a.from('ustar\x00')

const magicBuf = new b4a.alloc(6,0,'hex')
b4a.copy(buf,magicBuf,0,MAGIC_OFFSET, MAGIC_OFFSET + 6)

if ( b4a.compare(USTAR_MAGIC, magicBuf) === 0 ){
    console.log("eureka!")
}

src: https://github.com/cesare-montresor/tar-stream/blob/628855745845fdd3a13d9d99f47253df70223331/headers.js#L265

@mafintosh
Copy link
Contributor

In general, just the b4a functions and only "link" to the uint8arrays methods for node buffers is a great practice

@kasperisager
Copy link
Contributor

Agree with @mafintosh, this is the intended behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants