Skip to content

Commit 39cf43e

Browse files
committed
chore: update dependencies
BREAKING CHANGE: Node.js 6 and 8 are no longer supported.
1 parent 6f4238b commit 39cf43e

14 files changed

+152
-106
lines changed

‎.travis.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
language: node_js
22
node_js:
3-
- 11
43
- 10
5-
- 8
6-
- 6
4+
- 12
75
after_script: 'bash <(curl -s https://codecov.io/bash)'

‎package.json

+24-14
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
],
2222
"scripts": {
2323
"clean": "rimraf lib lib-esm",
24-
"eslint": "eslint src --ext ts --cache",
24+
"eslint": "eslint src --ext ts",
2525
"eslint-fix": "npm run eslint -- --fix",
2626
"prepublishOnly": "npm run tsc",
2727
"test": "npm run test-coverage && npm run eslint",
2828
"test-coverage": "npm run test-only -- --coverage",
2929
"test-only": "jest",
3030
"tsc": "npm run clean && npm run tsc-cjs && npm run tsc-esm",
31-
"tsc-cjs": "tsc",
31+
"tsc-cjs": "tsc --project tsconfig.cjs.json",
3232
"tsc-esm": "tsc --project tsconfig.esm.json"
3333
},
3434
"repository": {
@@ -44,23 +44,33 @@
4444
"testEnvironment": "node"
4545
},
4646
"devDependencies": {
47-
"@types/jest": "^24.0.11",
48-
"@typescript-eslint/eslint-plugin": "^1.5.0",
49-
"@typescript-eslint/parser": "^1.5.0",
50-
"eslint": "^5.15.3",
51-
"eslint-config-cheminfo": "^1.20.1",
52-
"eslint-config-cheminfo-typescript": "^3.0.0",
53-
"eslint-plugin-import": "^2.16.0",
54-
"eslint-plugin-jest": "^22.4.1",
55-
"jest": "^24.5.0",
47+
"@types/jest": "^24.0.19",
48+
"@types/node": "^12.11.2",
49+
"@typescript-eslint/eslint-plugin": "^2.5.0",
50+
"@typescript-eslint/parser": "^2.5.0",
51+
"eslint": "^6.5.1",
52+
"eslint-config-cheminfo": "^2.0.3",
53+
"eslint-config-cheminfo-typescript": "^4.1.1",
54+
"eslint-plugin-import": "^2.18.2",
55+
"eslint-plugin-jest": "^22.20.0",
56+
"eslint-plugin-prettier": "^3.1.1",
57+
"jest": "^24.9.0",
5658
"pngjs": "^3.4.0",
57-
"rimraf": "^2.6.3",
58-
"ts-jest": "^24.0.0",
59-
"typescript": "^3.3.4000"
59+
"prettier": "^1.18.2",
60+
"rimraf": "^3.0.0",
61+
"ts-jest": "^24.1.0",
62+
"typescript": "^3.6.4"
6063
},
6164
"dependencies": {
6265
"@types/pako": "^1.0.1",
6366
"iobuffer": "^4.0.1",
6467
"pako": "^1.0.10"
68+
},
69+
"prettier": {
70+
"arrowParens": "always",
71+
"semi": true,
72+
"singleQuote": true,
73+
"tabWidth": 2,
74+
"trailingComma": "all"
6575
}
6676
}

‎src/.npmignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__tests__
2+
.npmignore

‎src/PNGDecoder.ts

+52-24
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,20 @@ import { IOBuffer } from 'iobuffer';
22
import { Inflate as Inflator } from 'pako';
33

44
import { pngSignature, crc } from './common';
5-
import { IDecodedPNG, DecoderInputType, IPNGDecoderOptions, PNGDataArray, IndexedColors } from './types';
6-
import { ColorType, CompressionMethod, FilterMethod, InterlaceMethod } from './internalTypes';
5+
import {
6+
IDecodedPNG,
7+
DecoderInputType,
8+
IPNGDecoderOptions,
9+
PNGDataArray,
10+
IndexedColors,
11+
BitDepth,
12+
} from './types';
13+
import {
14+
ColorType,
15+
CompressionMethod,
16+
FilterMethod,
17+
InterlaceMethod,
18+
} from './internalTypes';
719

820
const empty = new Uint8Array(0);
921
const NULL = '\0';
@@ -28,9 +40,13 @@ export default class PNGDecoder extends IOBuffer {
2840
const { checkCrc = false } = options;
2941
this._checkCrc = checkCrc;
3042
this._inflator = new Inflator();
31-
// @ts-ignore
3243
this._png = {
33-
text: {}
44+
width: -1,
45+
height: -1,
46+
channels: -1,
47+
data: new Uint8Array(0),
48+
depth: 1,
49+
text: {},
3450
};
3551
this._end = false;
3652
this._hasPalette = false;
@@ -57,7 +73,7 @@ export default class PNGDecoder extends IOBuffer {
5773
for (let i = 0; i < pngSignature.length; i++) {
5874
if (this.readUint8() !== pngSignature[i]) {
5975
throw new Error(
60-
`wrong PNG signature. Byte at ${i} should be ${pngSignature[i]}.`
76+
`wrong PNG signature. Byte at ${i} should be ${pngSignature[i]}.`,
6177
);
6278
}
6379
}
@@ -103,13 +119,13 @@ export default class PNGDecoder extends IOBuffer {
103119
new Uint8Array(
104120
this.buffer,
105121
this.byteOffset + this.offset - crcLength - 4,
106-
crcLength
122+
crcLength,
107123
),
108-
crcLength
124+
crcLength,
109125
); // "- 4" because we already advanced by reading the CRC
110126
if (actualCrc !== expectedCrc) {
111127
throw new Error(
112-
`CRC mismatch for chunk ${type}. Expected ${expectedCrc}, found ${actualCrc}`
128+
`CRC mismatch for chunk ${type}. Expected ${expectedCrc}, found ${actualCrc}`,
113129
);
114130
}
115131
} else {
@@ -122,8 +138,7 @@ export default class PNGDecoder extends IOBuffer {
122138
const image = this._png;
123139
image.width = this.readUint32();
124140
image.height = this.readUint32();
125-
// @ts-ignore
126-
image.depth = this.readUint8();
141+
image.depth = checkBitDepth(this.readUint8());
127142

128143
const colorType: ColorType = this.readUint8();
129144
let channels: number;
@@ -151,7 +166,7 @@ export default class PNGDecoder extends IOBuffer {
151166
this._compressionMethod = this.readUint8();
152167
if (this._compressionMethod !== CompressionMethod.DEFLATE) {
153168
throw new Error(
154-
`Unsupported compression method: ${this._compressionMethod}`
169+
`Unsupported compression method: ${this._compressionMethod}`,
155170
);
156171
}
157172

@@ -163,7 +178,7 @@ export default class PNGDecoder extends IOBuffer {
163178
private decodePLTE(length: number): void {
164179
if (length % 3 !== 0) {
165180
throw new RangeError(
166-
`PLTE field length must be a multiple of 3. Got ${length}`
181+
`PLTE field length must be a multiple of 3. Got ${length}`,
167182
);
168183
}
169184
const l = length / 3;
@@ -179,7 +194,7 @@ export default class PNGDecoder extends IOBuffer {
179194
private decodeIDAT(length: number): void {
180195
this._inflator.push(
181196
new Uint8Array(this.buffer, this.offset + this.byteOffset, length),
182-
false
197+
false,
183198
);
184199
this.skip(length);
185200
}
@@ -206,10 +221,10 @@ export default class PNGDecoder extends IOBuffer {
206221
this._inflator.push(empty, true);
207222
if (this._inflator.err) {
208223
throw new Error(
209-
`Error while decompressing the data: ${this._inflator.err}`
224+
`Error while decompressing the data: ${this._inflator.err}`,
210225
);
211226
}
212-
var data = this._inflator.result;
227+
const data = this._inflator.result;
213228

214229
if (this._filterMethod !== FilterMethod.ADAPTIVE) {
215230
throw new Error(`Filter method ${this._filterMethod} not supported`);
@@ -219,7 +234,7 @@ export default class PNGDecoder extends IOBuffer {
219234
this.decodeInterlaceNull(data as Uint8Array);
220235
} else {
221236
throw new Error(
222-
`Interlace method ${this._interlaceMethod} not supported`
237+
`Interlace method ${this._interlaceMethod} not supported`,
223238
);
224239
}
225240
}
@@ -254,7 +269,7 @@ export default class PNGDecoder extends IOBuffer {
254269
newLine,
255270
prevLine,
256271
bytesPerLine,
257-
bytesPerPixel
272+
bytesPerPixel,
258273
);
259274
break;
260275
case 4:
@@ -263,7 +278,7 @@ export default class PNGDecoder extends IOBuffer {
263278
newLine,
264279
prevLine,
265280
bytesPerLine,
266-
bytesPerPixel
281+
bytesPerPixel,
267282
);
268283
break;
269284
default:
@@ -295,7 +310,7 @@ export default class PNGDecoder extends IOBuffer {
295310
function unfilterNone(
296311
currentLine: PNGDataArray,
297312
newLine: PNGDataArray,
298-
bytesPerLine: number
313+
bytesPerLine: number,
299314
): void {
300315
for (let i = 0; i < bytesPerLine; i++) {
301316
newLine[i] = currentLine[i];
@@ -306,7 +321,7 @@ function unfilterSub(
306321
currentLine: PNGDataArray,
307322
newLine: PNGDataArray,
308323
bytesPerLine: number,
309-
bytesPerPixel: number
324+
bytesPerPixel: number,
310325
): void {
311326
let i = 0;
312327
for (; i < bytesPerPixel; i++) {
@@ -322,7 +337,7 @@ function unfilterUp(
322337
currentLine: PNGDataArray,
323338
newLine: PNGDataArray,
324339
prevLine: PNGDataArray,
325-
bytesPerLine: number
340+
bytesPerLine: number,
326341
): void {
327342
let i = 0;
328343
if (prevLine.length === 0) {
@@ -342,7 +357,7 @@ function unfilterAverage(
342357
newLine: PNGDataArray,
343358
prevLine: PNGDataArray,
344359
bytesPerLine: number,
345-
bytesPerPixel: number
360+
bytesPerPixel: number,
346361
): void {
347362
let i = 0;
348363
if (prevLine.length === 0) {
@@ -369,7 +384,7 @@ function unfilterPaeth(
369384
newLine: PNGDataArray,
370385
prevLine: PNGDataArray,
371386
bytesPerLine: number,
372-
bytesPerPixel: number
387+
bytesPerPixel: number,
373388
): void {
374389
let i = 0;
375390
if (prevLine.length === 0) {
@@ -389,7 +404,7 @@ function unfilterPaeth(
389404
paethPredictor(
390405
newLine[i - bytesPerPixel],
391406
prevLine[i],
392-
prevLine[i - bytesPerPixel]
407+
prevLine[i - bytesPerPixel],
393408
)) &
394409
0xff;
395410
}
@@ -409,3 +424,16 @@ function paethPredictor(a: number, b: number, c: number): number {
409424
function swap16(val: number): number {
410425
return ((val & 0xff) << 8) | ((val >> 8) & 0xff);
411426
}
427+
428+
function checkBitDepth(value: number): BitDepth {
429+
if (
430+
value !== 1 &&
431+
value !== 2 &&
432+
value !== 4 &&
433+
value !== 8 &&
434+
value !== 16
435+
) {
436+
throw new Error(`invalid bit depth: ${value}`);
437+
}
438+
return value;
439+
}

‎src/PNGEncoder.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import {
88
IImageData,
99
IDecodedPNG,
1010
PNGDataArray,
11-
BitDepth
11+
BitDepth,
1212
} from './types';
1313
import {
1414
ColorType,
1515
CompressionMethod,
1616
FilterMethod,
17-
InterlaceMethod
17+
InterlaceMethod,
1818
} from './internalTypes';
1919

2020
const defaultZlibOptions: DeflateFunctionOptions = {
21-
level: 3
21+
level: 3,
2222
};
2323

2424
export default class PNGEncoder extends IOBuffer {
@@ -28,11 +28,9 @@ export default class PNGEncoder extends IOBuffer {
2828

2929
public constructor(data: IImageData, options: IPNGEncoderOptions = {}) {
3030
super();
31-
// @ts-ignore
32-
this._png = {};
3331
this._colorType = ColorType.UNKNOWN;
3432
this._zlibOptions = Object.assign({}, defaultZlibOptions, options.zlib);
35-
this._checkData(data);
33+
this._png = this._checkData(data);
3634
this.setBigEndian();
3735
}
3836

@@ -107,22 +105,24 @@ export default class PNGEncoder extends IOBuffer {
107105
this.encodeIDAT(compressed);
108106
}
109107

110-
private _checkData(data: IImageData): void {
111-
this._png.width = checkInteger(data.width, 'width');
112-
this._png.height = checkInteger(data.height, 'height');
113-
this._png.data = data.data;
108+
private _checkData(data: IImageData): IDecodedPNG {
114109
const { colorType, channels, depth } = getColorType(data);
110+
const png: IDecodedPNG = {
111+
width: checkInteger(data.width, 'width'),
112+
height: checkInteger(data.height, 'height'),
113+
channels: channels,
114+
data: data.data,
115+
depth: depth,
116+
text: {},
117+
};
115118
this._colorType = colorType;
116-
this._png.channels = channels;
117-
this._png.depth = depth;
118-
const expectedSize = this._png.width * this._png.height * channels;
119-
if (this._png.data.length !== expectedSize) {
119+
const expectedSize = png.width * png.height * channels;
120+
if (png.data.length !== expectedSize) {
120121
throw new RangeError(
121-
`wrong data size. Found ${
122-
this._png.data.length
123-
}, expected ${expectedSize}`
122+
`wrong data size. Found ${png.data.length}, expected ${expectedSize}`,
124123
);
125124
}
125+
return png;
126126
}
127127

128128
private writeCrc(length: number): void {
@@ -131,10 +131,10 @@ export default class PNGEncoder extends IOBuffer {
131131
new Uint8Array(
132132
this.buffer,
133133
this.byteOffset + this.offset - length,
134-
length
134+
length,
135135
),
136-
length
137-
)
136+
length,
137+
),
138138
);
139139
}
140140
}
@@ -147,7 +147,7 @@ function checkInteger(value: number, name: string): number {
147147
}
148148

149149
function getColorType(
150-
data: IImageData
150+
data: IImageData,
151151
): { channels: number; depth: BitDepth; colorType: ColorType } {
152152
const { channels = 4, depth = 8 } = data;
153153
if (channels !== 4 && channels !== 3 && channels !== 2 && channels !== 1) {
@@ -181,7 +181,7 @@ function writeDataBytes(
181181
data: PNGDataArray,
182182
newData: IOBuffer,
183183
slotsPerLine: number,
184-
offset: number
184+
offset: number,
185185
): number {
186186
for (let j = 0; j < slotsPerLine; j++) {
187187
newData.writeByte(data[offset++]);
@@ -193,7 +193,7 @@ function writeDataUint16(
193193
data: PNGDataArray,
194194
newData: IOBuffer,
195195
slotsPerLine: number,
196-
offset: number
196+
offset: number,
197197
): number {
198198
for (let j = 0; j < slotsPerLine; j++) {
199199
newData.writeUint16(data[offset++]);

0 commit comments

Comments
 (0)