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

[WIP] add rw BigInt64 methods #247

Closed
wants to merge 1 commit into from
Closed

Conversation

fanatid
Copy link
Contributor

@fanatid fanatid commented Sep 19, 2019

Original issue #244
This is quick copy code from node. I did not want make everything perfect because have a questions.

Is it ok use let/const/arrow functions/extends/template strings/etc? What if:

  1. Package will start use latest syntax and will be transpiled to what should be supported?
  2. Package will start use latest syntax and distribute it, so anybody who use it will need traspile to what they actually use? This is not used usually, right? But from my understanding this should give best code for projects who uses buffer, because they will receive only transpiled parts which they not support in their environment.

@lgtm-com
Copy link

lgtm-com bot commented Sep 19, 2019

This pull request introduces 1 alert when merging d8b7133 into c7f5cd7 - view on LGTM.com

new alerts:

  • 1 for Expression has no effect

@feross
Copy link
Owner

feross commented Sep 19, 2019

Is it ok use let/const/arrow functions/extends/template strings/etc?

If we want to continue supporting IE11 that means we can use:

But should probably avoid the rest.

If we want to use new syntax in the codebase, that would probably let us port code from Node.js more easily, which I like.

But then we need to do one of these two options:

  • use new syntax, drop IE11, publish a new major version
  • use new syntax, add babel, start shipping a transpiled build

@fanatid
Copy link
Contributor Author

fanatid commented Sep 20, 2019

Why if we add babel we need drop IE11? Not all polyfills work in IE11? Major bump because too much changes?
Projects can not publish not transpiled code because this will be headache for developers who use it?

@feross
Copy link
Owner

feross commented Sep 20, 2019

@fanatid I meant that we need to do one of those two option.

If we start using new syntax without babel then this package stops working in IE11 unless users start transpiling it. So that's a breaking change and we need to publish a new major version.

Or, we use new syntax, add babel, and ship a transpiled build. Then no new major version is needed because nothing breaks from the user's perspective.

@vweevers
Copy link

It might be harder to optimize a transpiled build for performance?

@feross
Copy link
Owner

feross commented Sep 20, 2019

Yeah, true.

I'm happy to just optimize for newer browsers. The current version of buffer works fine in IE11 and folks can always continue using that version if they need IE11 support. We can continue development on a new major version. Let's do it.

@fanatid
Copy link
Contributor Author

fanatid commented Sep 20, 2019

Thanks for explanation Feross.
@vweevers probably yes, but continue manually port new code to old syntax is pain for everybody. Other thing that not traspiled code should be executed faster than manually ported because code should be more effective? (but this will break almost all projects who uses buffer? haha)

So, decision is major version bump with new JS features transpiled for supported browsers? 😄

@feross
Copy link
Owner

feross commented Sep 20, 2019

I think we should not transpile. Users can do that if they want. We can release a new major version that includes new language features. We shouldn't get carried away, though. Tests should still pass in the last two major versions of every major browser (Chrome, Firefox, Safari, Edge, Safari on iOS, Chrome on Android).

@java-james
Copy link

When do you think we can expect resolution and merge of this PR?

@fanatid
Copy link
Contributor Author

fanatid commented Oct 24, 2019

Sorry, do not have time right now to finish it. Maybe later.

@collincusce
Copy link

Heyyy do you have time now? This is a feature I could immediately use! <3

@collincusce
Copy link

@fanatid or @feross there's a lot of missing functions in Buffer and classes like, BN.js which has toArrayLike and tries to use Buffer fails. If this fix patches that, can we get it reviewed and merged?

@fanatid
Copy link
Contributor Author

fanatid commented Dec 14, 2019

Can you describe situations (or show examples) when bn.js fails with Buffer?

@collincusce
Copy link

collincusce commented Dec 14, 2019

Absolutely, I should definitely be more clear. Sorry @fanatid and @feross .. I apologize. Using the current nodejs LTS + Typescript, typechecking fails because these functions are missing. This isn't just about BN.js but also things like elliptic. Using any Buffer as a parameter will give an error. Here's some examples of type conflicts.

Screenshot from 2019-12-14 13-38-15

Screenshot from 2019-12-14 13-29-16

Screenshot from 2019-12-14 13-28-11

All of these I have workarounds for, but most of it is a pretty annoying efficiency hit (like convert from Buffer(node version) to Buffer(js version) or convert to hex before passing to BN etc etc. It would be great if it was a drop-in for the LTS version like it was in the previous LTS version.

Here's a file I built just to handle some of these situations... maybe it'll help you see.

//Singleton, use getInstance()
export class BinTools {
    private static  instance:BinTools;
    private constructor() {}
  
    static getInstance(): BinTools {
        if (!BinTools.instance) {
            BinTools.instance = new BinTools();
        }
        return BinTools.instance;
    }
    copyFrom(buff:Buffer, start:number, end:number){
        return Buffer.from(Uint8Array.prototype.slice.call(buff.slice(start,end)))
    }
    bufferToB58(buff:Buffer) {
        return bs58.encode(Array.prototype.slice.call(buff));
    }

    fromBufferToArrayBuffer(buff:Buffer) {
        let ab = new ArrayBuffer(buff.length);
        let view = new Uint8Array(ab);
        for (let i = 0; i < buff.length; ++i) {
            view[i] = buff[i];
        }
        return ab;
    }
    fromArrayBufferToBuffer(ab:ArrayBuffer) {
        let buf = Buffer.alloc(ab.byteLength);
        let view = new Uint8Array(ab);
        for (let i = 0; i < buf.length; ++i) {
            buf[i] = view[i];
        }
        return buf;
    }

    fromBufferToBN(buff:Buffer){
        return new BN(buff.toString("hex"), 16, "be");
    }

    fromBNToBuffer(bn:BN){
        return Buffer.from(bn.toArray("be"));
    }
}

@fanatid
Copy link
Contributor Author

fanatid commented Dec 14, 2019

Ah, so it's mostly about TypeScript?

@collincusce
Copy link

collincusce commented Dec 14, 2019

Yes sorry if that's not a roadmap item, but it likely would fix Typescript issues if this push was put in

@collincusce
Copy link

Any updates?

Copy link

@Saiv46 Saiv46 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, please someone finish it

Copy link

@kevincfz kevincfz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM: Could someone with write access approve these changes please?

@Tschakki
Copy link

@feross could you merge this one please? :)

@feross
Copy link
Owner

feross commented Jun 17, 2020

I'm okay with shipping a new version of buffer only for modern browsers. We can consider shipping a transpiled version if we want to preserve current browser support, but let's do that in another issue.

@fanatid Do you think this is ready to merge?

@feross
Copy link
Owner

feross commented Jun 17, 2020

I just tried running the ES6 browser tests and they throw an exception because the way that assert.throws is used here:

Screen Shot 2020-06-17 at 5 54 38 PM

Can someone figure out the cause of this? If you do, please either update this PR (@fanatid) or send a PR to this PR (anyone else).

@collincusce
Copy link

collincusce commented Jun 17, 2020

I'm okay with shipping a new version of buffer only for modern browsers.

Please! I'm doing so many hokey things to handle Uint64 types lol, and the real barrier here is just getting TypeScript to agree that it's a Buffer class. Without the functions matching, TypeScript balks. Can we make it match the latest nodejs LTS version?

Full disclosure: I can get it to work, but standard libraries which depend on Buffer have issues. So I've had to do things like write my own base58 encoder/decoder.

@fanatid
Copy link
Contributor Author

fanatid commented Jun 18, 2020

Sorry, I do not have a time to check/update changes =(. Will be happy if somebody fork PR with required fixes / changes.

@shuse2 shuse2 mentioned this pull request Jun 21, 2020
@shuse2
Copy link
Contributor

shuse2 commented Jun 21, 2020

@feross @fanatid
I forked thie PR and add a commit to original commit in #267 to address the issue #247 (comment)

Running npm run test-browser-es6-local, it does not show the thrown error, and still passes the test.

Please check when you have time

@feross
Copy link
Owner

feross commented Nov 3, 2020

Thanks @shuse2 – let's use #267

@feross feross closed this Nov 3, 2020
@feross
Copy link
Owner

feross commented Nov 4, 2020

Released as 6.0.0

@fanatid fanatid deleted the rw-bigint branch November 4, 2020 07:36
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

Successfully merging this pull request may close these issues.

9 participants