Uuid: Expose bytes to uuid

Created on 17 Feb 2017  路  10Comments  路  Source: uuidjs/uuid

Do you have any interest in exposing the lib method bytesToUuid?

Usecase: I am writing two uuid's to a buffer which I am hex encoding for use in a http header, but when I decode them, I would like to store are traditional string representation in the XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX format.

It would be nice to do something like:

var buf = Buffer.from(hexEncodedString, 'hex');
var id1 = uuid.bytesToUuid(buf.slice(0, 16));
var id2 = uuid.bytesToUuid(buf.slice(17, 32));

Maybe there is another way to do this that I don't know about, so let me know if that is the case. Otherwise I can submit a PR to expose it.

Most helpful comment

Passes our test suite. I'll deploy shortly, but I don't expect any problems. Will let you know if anything comes up, but assume it's working fine for us unless you hear otherwise.

All 10 comments

No, this isn't something I expect will ever be part of the public API.

That said, require('uuid/lib/bytesToUuid') works. Just keep in mind that changes that break that will not be considered "breaking changes" from a versioning perspective.

Would it be possible to reconsider this? With native ES module support in Node, the "deep" import no longer works at all.

It'd be convenient to have a general-purpose way to convert a Node buffer with the bytes for a UUID into the standard hex representation, and using the bytesToUuid here seems like one of the nicer ways to do so.

As a comparison, the UUID type in the Python standard library exposes a bytes parameter when constructing a UUID that allows doing exactly this: https://docs.python.org/3/library/uuid.html#uuid.UUID.

The absence of an exposed bytesToUuid method might tempt one to use uuidv4({ random: ... }), which ... can then break subtly when that function is given, say, a UUIDv5, and lead to hard-to-diagnose bugs.

We're currently considering to extend the API surface of this library (https://github.com/uuidjs/uuid/pull/455#issuecomment-634721504), thank you for bringing up that use case.

We'll definitely discuss this!

@wesleytodd @taion we have just released [email protected] which includes https://github.com/uuidjs/uuid#uuidstringifyarr-offset would you mind testing it and report if that works for you?

Added in #479

Passes our test suite. I'll deploy shortly, but I don't expect any problems. Will let you know if anything comes up, but assume it's working fine for us unless you hear otherwise.

Closing. @taion, please comment here if you have any issues and we'll reopen, or just open a new issue. Thanks for the input!

We have just released [email protected] which should fix this issue. Please report back if there any problems.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ctavan picture ctavan  路  5Comments

ORESoftware picture ORESoftware  路  5Comments

lnogueir picture lnogueir  路  5Comments

anton-marchenko picture anton-marchenko  路  10Comments

ctavan picture ctavan  路  5Comments