Skip to content

Commit

Permalink
Reduced uses of Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Sep 24, 2023
1 parent d467ce7 commit 01c3deb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export abstract class Icon {
* @param data PNG data.
* @returns Image data.
*/
protected _decodePngToRgba(data: Readonly<Buffer>) {
protected _decodePngToRgba(data: Readonly<Uint8Array>) {
const image = UPNG.decode(data);
return {
width: image.width,
Expand All @@ -33,7 +33,7 @@ export abstract class Icon {
* @returns PNG data.
*/
protected _encodeRgbaToPng(imageData: Readonly<IImageData>) {
return Buffer.from(
return new Uint8Array(
(
UPNG.encode as (
imgs: ArrayBuffer[],
Expand Down
2 changes: 1 addition & 1 deletion src/icon/icns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class IconIcns extends Icon {
* @param raw Use raw PNG data without re-encoding for the PNG types.
*/
public addFromPng(
data: Readonly<Buffer>,
data: Readonly<Uint8Array>,
types: readonly string[],
raw = false
) {
Expand Down
34 changes: 24 additions & 10 deletions src/icon/ico.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ void describe('icon/ico', () => {
const dest = encodeFile('ico', name, 'all.ico');
const ico = new IconIco();
for (const size of sizes) {
// eslint-disable-next-line no-await-in-loop
const png = await readFile(specIconFilePng(name, size));
const png = new Uint8Array(
// eslint-disable-next-line no-await-in-loop
await readFile(specIconFilePng(name, size))
);
ico.addFromPng(png);
}
const data = Buffer.from(ico.encode());
Expand All @@ -30,8 +32,10 @@ void describe('icon/ico', () => {
const dest = encodeFile('ico', name, 'all-bmp.ico');
const ico = new IconIco();
for (const size of sizes) {
// eslint-disable-next-line no-await-in-loop
const png = await readFile(specIconFilePng(name, size));
const png = new Uint8Array(
// eslint-disable-next-line no-await-in-loop
await readFile(specIconFilePng(name, size))
);
ico.addFromPng(png, false);
}
const data = Buffer.from(ico.encode());
Expand All @@ -43,8 +47,10 @@ void describe('icon/ico', () => {
const dest = encodeFile('ico', name, 'all-png.ico');
const ico = new IconIco();
for (const size of sizes) {
// eslint-disable-next-line no-await-in-loop
const png = await readFile(specIconFilePng(name, size));
const png = new Uint8Array(
// eslint-disable-next-line no-await-in-loop
await readFile(specIconFilePng(name, size))
);
ico.addFromPng(png, true);
}
const data = Buffer.from(ico.encode());
Expand All @@ -56,7 +62,9 @@ void describe('icon/ico', () => {
void it(`${size}`, async () => {
const dest = encodeFile('ico', name, `${size}.ico`);
const ico = new IconIco();
const png = await readFile(specIconFilePng(name, size));
const png = new Uint8Array(
await readFile(specIconFilePng(name, size))
);
ico.addFromPng(png);
const data = Buffer.from(ico.encode());
await mkdir(dirname(dest), {recursive: true});
Expand All @@ -68,7 +76,9 @@ void describe('icon/ico', () => {
void it(`${size}-bmp`, async () => {
const dest = encodeFile('ico', name, `${size}-bmp.ico`);
const ico = new IconIco();
const png = await readFile(specIconFilePng(name, size));
const png = new Uint8Array(
await readFile(specIconFilePng(name, size))
);
ico.addFromPng(png, false);
const data = Buffer.from(ico.encode());
await mkdir(dirname(dest), {recursive: true});
Expand All @@ -80,7 +90,9 @@ void describe('icon/ico', () => {
void it(`${size}-png`, async () => {
const dest = encodeFile('ico', name, `${size}-png.ico`);
const ico = new IconIco();
const png = await readFile(specIconFilePng(name, size));
const png = new Uint8Array(
await readFile(specIconFilePng(name, size))
);
ico.addFromPng(png, true);
const data = Buffer.from(ico.encode());
await mkdir(dirname(dest), {recursive: true});
Expand All @@ -96,7 +108,9 @@ void describe('icon/ico', () => {
`${size}-png-raw.ico`
);
const ico = new IconIco();
const png = await readFile(specIconFilePng(name, size));
const png = new Uint8Array(
await readFile(specIconFilePng(name, size))
);
ico.addFromPng(png, true, true);
const data = Buffer.from(ico.encode());
await mkdir(dirname(dest), {recursive: true});
Expand Down
18 changes: 9 additions & 9 deletions src/icon/ico.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface IIconIcoEntry {
/**
* Icon data.
*/
readonly data: Readonly<Buffer>;
readonly data: Readonly<Uint8Array>;
}

/**
Expand All @@ -47,7 +47,7 @@ export class IconIco extends Icon {
* @param raw Use raw PNG data without re-encoding, if using PNG format.
*/
public addFromPng(
data: Readonly<Buffer>,
data: Readonly<Uint8Array>,
png: boolean | null = null,
raw = false
) {
Expand All @@ -59,7 +59,7 @@ export class IconIco extends Icon {
this.entries.push({
width: ihdr.width,
height: ihdr.height,
data: Buffer.concat([data as Buffer], data.length)
data: new Uint8Array(data)
});
return;
}
Expand Down Expand Up @@ -103,15 +103,15 @@ export class IconIco extends Icon {
public encode() {
const {entries} = this;
const dir = this._encodeIcoDir(entries.length);
const dirs: Buffer[] = [];
const imgs: Buffer[] = [];
const dirs = [];
const imgs = [];
let offset = dir.length + entries.length * 16;
for (const entry of entries) {
const {data} = entry;
const dataSize = data.length;
const ent = this._encodeIcoDirEntry(entry, offset);
dirs.push(ent);
imgs.push(data as Buffer);
imgs.push(data);
offset += dataSize;
}
return concatUint8Arrays([dir, ...dirs, ...imgs]);
Expand Down Expand Up @@ -141,7 +141,7 @@ export class IconIco extends Icon {
encoded.setUint16(0, 0, true);
encoded.setUint16(2, 1, true);
encoded.setUint16(4, count, true);
return Buffer.from(r);
return r;
}

/**
Expand All @@ -167,7 +167,7 @@ export class IconIco extends Icon {
encoded.setUint16(6, 32, true);
encoded.setUint32(8, data.length, true);
encoded.setUint32(12, offset, true);
return Buffer.from(r);
return r;
}

/**
Expand Down Expand Up @@ -248,6 +248,6 @@ export class IconIco extends Icon {
}
}
}
return Buffer.from(r);
return r;
}
}

0 comments on commit 01c3deb

Please sign in to comment.