Skip to content

Commit

Permalink
Remove usage of Buffer from snaps-utils (#2238)
Browse files Browse the repository at this point in the history
Remove one usage of `Buffer` since the exported function is used in both
in the browser and in the Node.js export.
  • Loading branch information
FrederikBolding authored Mar 1, 2024
1 parent 455366f commit 4d65e1d
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,6 @@
"depcheck>semver>lru-cache>yallist": true
}
},
"external:../snaps-utils/src/icon.ts": {
"builtin": {
"buffer": true
}
},
"readable-stream": {
"packages": {
"browserify>browser-resolve": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@
"depcheck>semver>lru-cache>yallist": true
}
},
"external:../snaps-utils/src/icon.ts": {
"builtin": {
"buffer": true
}
},
"istanbul-lib-report>supports-color>has-flag": {
"globals": {
"process.argv": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@
"depcheck>semver>lru-cache>yallist": true
}
},
"external:../snaps-utils/src/icon.ts": {
"builtin": {
"buffer": true
}
},
"istanbul-lib-report>supports-color>has-flag": {
"globals": {
"process.argv": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
{
"resources": {
"external:../snaps-utils/src/icon.ts": {
"builtin": {
"buffer": true
}
}
}
"resources": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@
"depcheck>semver>lru-cache>yallist": true
}
},
"external:../snaps-utils/src/icon.ts": {
"builtin": {
"buffer": true
}
},
"readable-stream": {
"packages": {
"browserify>browser-resolve": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,6 @@
"depcheck>semver>lru-cache>yallist": true
}
},
"external:../snaps-utils/src/icon.ts": {
"builtin": {
"buffer": true
}
},
"readable-stream": {
"packages": {
"browserify>browser-resolve": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@
"depcheck>semver>lru-cache>yallist": true
}
},
"external:../snaps-utils/src/icon.ts": {
"builtin": {
"buffer": true
}
},
"readable-stream": {
"packages": {
"browserify>browser-resolve": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-utils/coverage.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"branches": 96.44,
"branches": 96.47,
"functions": 98.62,
"lines": 98.74,
"statements": 94.48
Expand Down
10 changes: 10 additions & 0 deletions packages/snaps-utils/src/icon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ describe('assertIsSnapIcon', () => {
);
});

it('asserts that the file is an appropriate size when the file is represented by a string', () => {
const icon = new VirtualFile({
value: '1'.repeat(SVG_MAX_BYTE_SIZE + 1),
path: 'foo.svg',
});
expect(() => assertIsSnapIcon(icon)).toThrow(
`The specified SVG icon exceeds the maximum size of ${SVG_MAX_BYTE_SIZE_TEXT}.`,
);
});

it('asserts that the file is a valid SVG', () => {
const icon = new VirtualFile({
value: stringToBytes('foo'),
Expand Down
9 changes: 7 additions & 2 deletions packages/snaps-utils/src/icon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isSvg, parseSvg } from '@metamask/snaps-sdk';
import { assert } from '@metamask/utils';
import { assert, stringToBytes } from '@metamask/utils';

import type { VirtualFile } from './virtual-file';

Expand All @@ -16,8 +16,13 @@ export const SVG_MAX_BYTE_SIZE_TEXT = `${Math.floor(
export function assertIsSnapIcon(icon: VirtualFile) {
assert(icon.path.endsWith('.svg'), 'Expected snap icon to end in ".svg".');

const byteLength =
typeof icon.value === 'string'
? stringToBytes(icon.value).byteLength
: icon.value.byteLength;

assert(
Buffer.byteLength(icon.value, 'utf8') <= SVG_MAX_BYTE_SIZE,
byteLength <= SVG_MAX_BYTE_SIZE,
`The specified SVG icon exceeds the maximum size of ${SVG_MAX_BYTE_SIZE_TEXT}.`,
);

Expand Down

0 comments on commit 4d65e1d

Please sign in to comment.