Skip to content

Commit

Permalink
fix: iconVersionId wrong encode decode (#156)
Browse files Browse the repository at this point in the history
* add Icon {proto,schema} and {encode,decode}-conversions

* Update src/lib/decode-conversions.ts

Co-authored-by: Gregor MacLennan <gmaclennan@digital-democracy.org>

* Update proto/icon/v1.proto

Co-authored-by: Gregor MacLennan <gmaclennan@digital-democracy.org>

* reverted icon/v1.proto PixelDensity enum

in protobufs, enums need to start at 0, so we can't align the numbers...

* remove unnecessary type casting, add tests

* call pixelDensity x1,x2,x3 on proto

* encode `blobVersionId` as utf-8 since we have slashes

* merge fix

* make icon `blobVersionId` a struct with {coreId,index}

* fix missing `/${index}` on fixture of Icon.blobVersionId

* add check to blobVersionId

Co-authored-by: Gregor MacLennan <gmaclennan@digital-democracy.org>

---------

Co-authored-by: Tomás Ciccola <tciccola@digital-democracy.com>
Co-authored-by: Gregor MacLennan <gmaclennan@digital-democracy.org>
  • Loading branch information
3 people authored Oct 10, 2023
1 parent c0d7b6f commit 17655d0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 6 deletions.
7 changes: 6 additions & 1 deletion proto/icon/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ message Icon_1 {
png = 1;
}

message BlobVersionId {
bytes coreDiscoveryKey = 1;
int32 index = 2;
}

Size size = 1 [(required) = true];
PixelDensity pixelDensity = 2 [(required) = true];
bytes blobVersionId = 3 [(required) = true];
BlobVersionId blobVersionId = 3 [(required) = true];
MimeType mimeType = 4 [(required) = true];
}

Expand Down
1 change: 1 addition & 0 deletions schema/icon/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"enum": [1,2,3]
},
"blobVersionId": {
"description": "Version id of the icon blob. Each id is id (hex-encoded 32 byte buffer) and index number, separated by '/'",
"type": "string"
},
"mimeType": {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,11 @@ export const convertIcon: ConvertFunction<'icon'> = (message, versionObj) => {

function convertIconVariant(variant: Icon_1_IconVariant) {
const { blobVersionId, mimeType, size, pixelDensity } = variant
if (!blobVersionId) {
throw new Error('Missing required property `blobVersionId`')
}
return {
blobVersionId: blobVersionId.toString('hex'),
blobVersionId: getVersionId(blobVersionId),
mimeType: convertIconMimeType(mimeType),
size: size === 'UNRECOGNIZED' ? 'medium' : size,
pixelDensity: convertIconPixelDensity(pixelDensity),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/encode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function convertIconVariants(variants: Icon['variants']): Icon_1_IconVariant[] {
return variants.map((variant) => {
const { blobVersionId, mimeType, size, pixelDensity } = variant
return {
blobVersionId: Buffer.from(blobVersionId, 'hex'),
blobVersionId: parseVersionId(blobVersionId),
mimeType: convertIconMimeType(mimeType),
size,
pixelDensity: convertIconPixelDensity(pixelDensity),
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/good-docs-completed.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ export const goodDocsCompleted = [
{
size: 'small',
pixelDensity: 1,
blobVersionId: randomBytes(32).toString('hex'),
blobVersionId: randomBytes(32).toString('hex') + '/0',
mimeType: 'image/png',
},
{
size: 'large',
pixelDensity: 3,
blobVersionId: randomBytes(32).toString('hex'),
blobVersionId: randomBytes(32).toString('hex') + '/0',
mimeType: 'image/svg+xml',
},
],
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/good-docs-minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export const goodDocsMinimal = [
{
size: 'small',
pixelDensity: 1,
blobVersionId: randomBytes(32).toString('hex'),
blobVersionId: randomBytes(32).toString('hex') + '/0',
mimeType: 'image/png',
},
],
Expand Down

0 comments on commit 17655d0

Please sign in to comment.