From 17655d00fb119a7cd7e6253ed63b27dffd4f1bae Mon Sep 17 00:00:00 2001 From: tomasciccola <117094913+tomasciccola@users.noreply.github.com> Date: Tue, 10 Oct 2023 10:29:05 -0300 Subject: [PATCH] fix: iconVersionId wrong encode decode (#156) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add Icon {proto,schema} and {encode,decode}-conversions * Update src/lib/decode-conversions.ts Co-authored-by: Gregor MacLennan * Update proto/icon/v1.proto Co-authored-by: Gregor MacLennan * 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 --------- Co-authored-by: Tomás Ciccola Co-authored-by: Gregor MacLennan --- proto/icon/v1.proto | 7 ++++++- schema/icon/v1.json | 1 + src/lib/decode-conversions.ts | 5 ++++- src/lib/encode-conversions.ts | 2 +- test/fixtures/good-docs-completed.js | 4 ++-- test/fixtures/good-docs-minimal.js | 2 +- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/proto/icon/v1.proto b/proto/icon/v1.proto index 364595e..6014244 100644 --- a/proto/icon/v1.proto +++ b/proto/icon/v1.proto @@ -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]; } diff --git a/schema/icon/v1.json b/schema/icon/v1.json index 1fde761..28c1cd6 100644 --- a/schema/icon/v1.json +++ b/schema/icon/v1.json @@ -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": { diff --git a/src/lib/decode-conversions.ts b/src/lib/decode-conversions.ts index b7b0e1c..11eca04 100644 --- a/src/lib/decode-conversions.ts +++ b/src/lib/decode-conversions.ts @@ -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), diff --git a/src/lib/encode-conversions.ts b/src/lib/encode-conversions.ts index 95594ac..447e16a 100644 --- a/src/lib/encode-conversions.ts +++ b/src/lib/encode-conversions.ts @@ -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), diff --git a/test/fixtures/good-docs-completed.js b/test/fixtures/good-docs-completed.js index 3de85c2..45a8def 100644 --- a/test/fixtures/good-docs-completed.js +++ b/test/fixtures/good-docs-completed.js @@ -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', }, ], diff --git a/test/fixtures/good-docs-minimal.js b/test/fixtures/good-docs-minimal.js index 748de0e..3ce92ac 100644 --- a/test/fixtures/good-docs-minimal.js +++ b/test/fixtures/good-docs-minimal.js @@ -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', }, ],