Skip to content

Commit 586b46c

Browse files
committed
refactor: convert image package to TS
1 parent 3007d34 commit 586b46c

File tree

15 files changed

+336
-150
lines changed

15 files changed

+336
-150
lines changed

packages/image/babel.config.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/image/globals.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'vitest-fetch-mock';
2+
3+
declare global {
4+
const BROWSER: boolean;
5+
}

packages/image/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"watch": "rimraf ./lib && rollup -c -w"
2222
},
2323
"dependencies": {
24-
"@babel/runtime": "^7.20.13",
2524
"@react-pdf/png-js": "^3.0.0",
2625
"jay-peg": "^1.1.1"
2726
},

packages/image/rollup.config.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
1-
import babel from '@rollup/plugin-babel';
1+
import { dts } from 'rollup-plugin-dts';
2+
import del from 'rollup-plugin-delete';
3+
import typescript from '@rollup/plugin-typescript';
24
import replace from '@rollup/plugin-replace';
35
import ignore from 'rollup-plugin-ignore';
46
import nodePolyfills from 'rollup-plugin-polyfill-node';
57

68
import pkg from './package.json' with { type: 'json' };
79

8-
const input = './src/index.js';
9-
10-
const babelConfig = () => ({
11-
babelrc: true,
12-
exclude: 'node_modules/**',
13-
babelHelpers: 'runtime',
14-
});
10+
const input = './src/index.ts';
1511

1612
const getExternal = ({ browser }) => [
17-
/@babel\/runtime/,
1813
...Object.keys(pkg.dependencies),
1914
...(browser ? [] : ['fs', 'path', 'url']),
2015
];
2116

2217
const getPlugins = ({ browser }) => [
23-
babel(babelConfig()),
18+
typescript(),
2419
replace({
2520
preventAssignment: true,
2621
values: { BROWSER: JSON.stringify(browser) },
2722
}),
2823
...(browser
29-
? [
30-
ignore(['fs', 'path', 'url']),
31-
nodePolyfills({
32-
include: [/node_modules\/.+\.js/, /\/image\/src\/.*\.js/],
33-
}),
34-
]
24+
? [ignore(['fs', 'path', 'url']), nodePolyfills({ include: null })]
3525
: []),
3626
];
3727

@@ -49,4 +39,10 @@ const browserConfig = {
4939
plugins: getPlugins({ browser: true }),
5040
};
5141

52-
export default [serverConfig, browserConfig];
42+
const dtsConfig = {
43+
input: './lib/types/index.d.ts',
44+
output: [{ file: 'lib/index.d.ts', format: 'es' }],
45+
plugins: [dts(), del({ targets: 'lib/types', hook: 'buildEnd' })],
46+
};
47+
48+
export default [serverConfig, browserConfig, dtsConfig];
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const createCache = ({ limit = 100 } = {}) => {
2-
let cache = {};
1+
const createCache = <T>({ limit = 100 } = {}) => {
2+
let cache: Record<string, T> = {};
33
let keys = [];
44

55
return {
6-
get: (key) => cache[key],
7-
set: (key, value) => {
6+
get: (key: string): T | undefined => cache[key],
7+
set: (key: string, value: T) => {
88
keys.push(key);
99
if (keys.length > limit) {
1010
delete cache[keys.shift()];
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import resolveImage from './resolve';
22

3+
export type { ImageSrc } from './types';
4+
35
export default resolveImage;
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
import _JPEG from 'jay-peg';
2+
import { Image } from './types';
23

3-
class JPEG {
4-
data = null;
5-
6-
width = null;
7-
8-
height = null;
4+
class JPEG implements Image {
5+
data: Buffer;
6+
width: number;
7+
height: number;
8+
format: 'jpeg';
99

1010
constructor(data) {
1111
this.data = data;
12+
this.format = 'jpeg';
1213

1314
if (data.readUInt16BE(0) !== 0xffd8) {
1415
throw new Error('SOI not found in JPEG');
1516
}
1617

1718
const markers = _JPEG.decode(this.data);
1819

20+
let orientation;
21+
1922
for (let i = 0; i < markers.length; i += 1) {
2023
const marker = markers[i];
2124

2225
if (marker.name === 'EXIF' && marker.entries.orientation) {
23-
this.orientation = marker.entries.orientation;
26+
orientation = marker.entries.orientation;
2427
}
2528

2629
if (marker.name === 'SOF') {
@@ -29,14 +32,14 @@ class JPEG {
2932
}
3033
}
3134

32-
if (this.orientation > 4) {
35+
if (orientation > 4) {
3336
[this.width, this.height] = [this.height, this.width];
3437
}
3538
}
36-
}
3739

38-
JPEG.isValid = (data) => {
39-
return data && Buffer.isBuffer(data) && data.readUInt16BE(0) === 0xffd8;
40-
};
40+
static isValid(data) {
41+
return data && Buffer.isBuffer(data) && data.readUInt16BE(0) === 0xffd8;
42+
}
43+
}
4144

4245
export default JPEG;

packages/image/src/png.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/image/src/png.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import _PNG from '@react-pdf/png-js';
2+
3+
import { Image } from './types';
4+
5+
class PNG implements Image {
6+
data: Buffer;
7+
width: number;
8+
height: number;
9+
format: 'png';
10+
11+
constructor(data: Buffer) {
12+
const png = new _PNG(data);
13+
14+
this.data = data;
15+
this.width = png.width;
16+
this.height = png.height;
17+
this.format = 'png';
18+
}
19+
20+
static isValid(data: Buffer): boolean {
21+
try {
22+
return !!new PNG(data);
23+
} catch {
24+
return false;
25+
}
26+
}
27+
}
28+
29+
export default PNG;

0 commit comments

Comments
 (0)