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

Unexpected/incompatible byte representation #503

Closed
bjornharrtell opened this issue Aug 3, 2020 · 4 comments
Closed

Unexpected/incompatible byte representation #503

bjornharrtell opened this issue Aug 3, 2020 · 4 comments

Comments

@bjornharrtell
Copy link

I can roundtrip GUID 75b097d1-b891-4834-9b12-4a94d8c42504 with the following node repl code:

> const uuid = require('uuid')
> uuid.parse('75b097d1-b891-4834-9b12-4a94d8c42504')
Uint8Array(16) [
  117, 176, 151, 209, 184,
  145,  72,  52, 155,  18,
   74, 148, 216, 196,  37,
    4
]
> const a = uuid.parse('75b097d1-b891-4834-9b12-4a94d8c42504')
undefined
> uuid.stringify(a)
'75b097d1-b891-4834-9b12-4a94d8c42504'

But I suspect the byte representation is not as expected. I would expect the byte array to be [209, 151, 176, 117, 145, 184, 52, 72, 155, 18, 74, 148, 216, 196, 37, 4]. I found a gist at https://gist.github.com/daboxu/4f1dd0a254326ac2361f8e78f89e97ae that produces that array from 75b097d1-b891-4834-9b12-4a94d8c42504 and it's also what is produced by fx. C# / .NET.

@bjornharrtell
Copy link
Author

I now understand that [209, 151, 176, 117, 145, 184, 52, 72, 155, 18, 74, 148, 216, 196, 37, 4] is the mixed-endian encoded variant which I suppose is not supported by this package.

@ctavan
Copy link
Member

ctavan commented Aug 3, 2020

@bjornharrtell wow, thanks for reporting!

I think we should mention that in the documentation. Reference: https://stackoverflow.com/a/45671539/1053532

@broofa
Copy link
Member

broofa commented Aug 3, 2020

That's an interesting internal data structure MSFT is using to represent GUIDs. It's strange they use multi-byte ints for the first half of the bytes (int, short, short), but individual bytes for the second half. I assume it's because (per wikipedia) "The current version of the Microsoft guidgen tool produces standard variant-1 UUIDs.", where the timestamp is contained in the first half of the UUID.

However in most other GUIDs (versions 4 most notably, but also 3 and 5), time fields don't exist so the distinction between time and non-time fields in such a data structure is not meaningful. Thus, I believe the structure we're using - a simple byte array with order that corresponds to the UUID string ordering - is better suited for most purposes. It's version and platform neutral, and generally more intuitive.

(Put up a PR to explicitly document how bytes are ordered though.)

@broofa broofa closed this as completed in cfb3cdb Aug 3, 2020
@benrr101
Copy link

benrr101 commented Mar 7, 2021

I was confounded by this issue today. Not sure who's right here, but according to this SO answer the RFC states the int/short/short values should be stored in network byte order (ie, big-endian). If that's true, then I don't think the package is actually RFC compliant.

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

4 participants