Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/mean-tools-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-pdf/image": patch
---

refactor: image strict type checking
4 changes: 4 additions & 0 deletions packages/image/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'vitest-fetch-mock';

declare module 'jay-peg';

declare module '@react-pdf/png-js';

declare global {
const BROWSER: boolean;
}
6 changes: 3 additions & 3 deletions packages/image/src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const createCache = <T>({ limit = 100 } = {}) => {
let cache: Record<string, T> = {};
let keys = [];
let keys: string[] = [];

return {
get: (key: string): T | undefined => cache[key],
get: (key: string | null): T | null => (key ? cache[key] : null),
set: (key: string, value: T) => {
keys.push(key);
if (keys.length > limit) {
delete cache[keys.shift()];
delete cache[keys.shift()!];
}
cache[key] = value;
},
Expand Down
5 changes: 4 additions & 1 deletion packages/image/src/jpeg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _JPEG from 'jay-peg';

import { Image } from './types';

class JPEG implements Image {
Expand All @@ -10,6 +11,8 @@ class JPEG implements Image {
constructor(data: Buffer) {
this.data = data;
this.format = 'jpeg';
this.width = 0;
this.height = 0;

if (data.readUInt16BE(0) !== 0xffd8) {
throw new Error('SOI not found in JPEG');
Expand Down Expand Up @@ -37,7 +40,7 @@ class JPEG implements Image {
}
}

static isValid(data) {
static isValid(data: Buffer) {
return data && Buffer.isBuffer(data) && data.readUInt16BE(0) === 0xffd8;
}
}
Expand Down
10 changes: 6 additions & 4 deletions packages/image/src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
RemoteImageSrc,
} from './types';

export const IMAGE_CACHE = createCache<Promise<Image>>({ limit: 30 });
export const IMAGE_CACHE = createCache<Promise<Image | null>>({ limit: 30 });

const isBuffer = Buffer.isBuffer;

Expand Down Expand Up @@ -45,7 +45,7 @@ const getAbsoluteLocalPath = (src: string) => {
path: pathname,
} = url.parse(src);

const absolutePath = path.resolve(pathname);
const absolutePath = pathname ? path.resolve(pathname) : undefined;

if ((protocol && protocol !== 'file:') || auth || host || port || hostname) {
return undefined;
Expand Down Expand Up @@ -123,12 +123,14 @@ function getImage(body: Buffer, format: string): Image | null {

const resolveBase64Image = async ({ uri }: Base64ImageSrc) => {
const match = /^data:image\/([a-zA-Z]*);base64,([^"]*)/g.exec(uri);

if (!match) throw new Error(`Invalid base64 image: ${uri}`);

const format = match[1];
const data = match[2];

if (!isValidFormat(format)) {
if (!isValidFormat(format))
throw new Error(`Base64 image invalid format: ${format}`);
}

return getImage(Buffer.from(data, 'base64'), format);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/image/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"moduleResolution": "Node",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": false,
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"types": ["vitest/globals"],
Expand Down