-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show simpler uuid format VSCODE-470 (#701)
--------- Co-authored-by: Anna Henningsen <[email protected]>
- Loading branch information
1 parent
8210f7a
commit 10f4267
Showing
5 changed files
with
254 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { expect } from 'chai'; | ||
import { getEJSON } from '../../../utils/ejson'; | ||
|
||
suite('getEJSON', function () { | ||
suite('Valid uuid', function () { | ||
const prettyUuid = { | ||
$uuid: '63b985b8-e8dd-4bda-9087-e4402f1a3ff5', | ||
}; | ||
const rawUuid = { | ||
$binary: { | ||
base64: 'Y7mFuOjdS9qQh+RALxo/9Q==', | ||
subType: '04', | ||
}, | ||
}; | ||
|
||
test('Simplifies top-level uuid', function () { | ||
const ejson = getEJSON({ uuid: rawUuid }); | ||
expect(ejson).to.deep.equal({ uuid: prettyUuid }); | ||
}); | ||
|
||
test('Simplifies nested uuid', function () { | ||
const ejson = getEJSON({ | ||
grandparent: { | ||
parent: { | ||
sibling: 1, | ||
uuid: rawUuid, | ||
}, | ||
}, | ||
}); | ||
expect(ejson).to.deep.equal({ | ||
grandparent: { | ||
parent: { | ||
sibling: 1, | ||
uuid: prettyUuid, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('Simplifies uuid in a nested array', function () { | ||
const ejson = getEJSON({ | ||
items: [ | ||
{ | ||
parent: { | ||
sibling: 1, | ||
uuid: rawUuid, | ||
}, | ||
}, | ||
], | ||
}); | ||
expect(ejson).to.deep.equal({ | ||
items: [ | ||
{ | ||
parent: { | ||
sibling: 1, | ||
uuid: prettyUuid, | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
}); | ||
|
||
suite('Invalid uuid or not an uuid', function () { | ||
test('Ignores another subtype', function () { | ||
const document = { | ||
$binary: { | ||
base64: 'Y7mFuOjdS9qQh+RALxo/9Q==', | ||
subType: '02', | ||
}, | ||
}; | ||
const ejson = getEJSON(document); | ||
expect(ejson).to.deep.equal(document); | ||
}); | ||
|
||
test('Ignores invalid uuid', function () { | ||
const document = { | ||
$binary: { | ||
base64: 'Y7m==', | ||
subType: '04', | ||
}, | ||
}; | ||
const ejson = getEJSON(document); | ||
expect(ejson).to.deep.equal(document); | ||
}); | ||
|
||
test('Ignores null', function () { | ||
const document = { | ||
$binary: { | ||
base64: null, | ||
subType: '04', | ||
}, | ||
}; | ||
const ejson = getEJSON(document); | ||
expect(ejson).to.deep.equal(document); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { EJSON } from 'bson'; | ||
import type { Document } from 'bson'; | ||
|
||
const isObjectOrArray = (value: unknown) => | ||
value !== null && typeof value === 'object'; | ||
|
||
function simplifyEJSON(item: Document[] | Document): Document { | ||
if (!isObjectOrArray(item)) return item; | ||
|
||
if (Array.isArray(item)) { | ||
return item.map((arrayItem) => | ||
isObjectOrArray(arrayItem) ? simplifyEJSON(arrayItem) : arrayItem | ||
); | ||
} | ||
|
||
// UUIDs might be represented as {"$uuid": <canonical textual representation of a UUID>} in EJSON | ||
// Binary subtypes 3 or 4 are used to represent UUIDs in BSON | ||
// But, parsers MUST interpret the $uuid key as BSON Binary subtype 4 | ||
// For this reason, we are applying this representation for subtype 4 only | ||
// see https://github.com/mongodb/specifications/blob/master/source/extended-json.rst#special-rules-for-parsing-uuid-fields | ||
if ( | ||
item.$binary?.subType === '04' && | ||
typeof item.$binary?.base64 === 'string' | ||
) { | ||
const hexString = Buffer.from(item.$binary.base64, 'base64').toString( | ||
'hex' | ||
); | ||
const match = /^(.{8})(.{4})(.{4})(.{4})(.{12})$/.exec(hexString); | ||
if (!match) return item; | ||
const asUUID = match.slice(1, 6).join('-'); | ||
return { $uuid: asUUID }; | ||
} | ||
|
||
return Object.fromEntries( | ||
Object.entries(item).map(([key, value]) => [ | ||
key, | ||
isObjectOrArray(value) ? simplifyEJSON(value) : value, | ||
]) | ||
); | ||
} | ||
|
||
export function getEJSON(item: Document[] | Document) { | ||
const ejson = EJSON.serialize(item); | ||
return simplifyEJSON(ejson); | ||
} |