Skip to content

Commit

Permalink
Return Uint8Array instead of Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 6, 2023
1 parent d34d622 commit 0f98b0c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ Type: `string`

### pageres.run()

Run pageres. Returns `Promise<Buffer[]>`.
Run pageres.

Returns `Promise<Uint8Array[]>`.

## Task runners

Expand Down
9 changes: 4 additions & 5 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import process from 'node:process';
import type {Buffer} from 'node:buffer';
import path from 'node:path';
import {pathToFileURL} from 'node:url';
import fs from 'node:fs';
Expand Down Expand Up @@ -231,9 +230,9 @@ type Stats = {
};

/**
Buffer data representing a screenshot. Includes the filename from the template in {@link Options.filename}.
Data representing a screenshot. Includes the filename from the template in {@link Options.filename}.
*/
export type Screenshot = Buffer & {filename: string};
export type Screenshot = Uint8Array & {filename: string};

const getResolutionsMemoized = pMemoize(getResolutions);
// @ts-expect-error - TS is not very smart.
Expand Down Expand Up @@ -347,7 +346,7 @@ export default class Pageres extends EventEmitter {
/**
Run pageres.
@returns List of screenshot buffer data.
@returns List of screenshot data.
@example
```
Expand Down Expand Up @@ -515,7 +514,7 @@ export default class Pageres extends EventEmitter {
};
}

const screenshot = await captureWebsite.buffer(url, finalOptions) as Screenshot;
const screenshot = new Uint8Array(await captureWebsite.buffer(url, finalOptions)) as Screenshot;
screenshot.filename = filename;
return screenshot;
}
Expand Down
5 changes: 2 additions & 3 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {Buffer} from 'node:buffer';
import process from 'node:process';
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
Expand All @@ -20,8 +19,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));

const hasScreenshotsWithFilenames = (screenshots: readonly Screenshot[], filenames: readonly string[]): boolean => screenshots.some(screenshot => filenames.includes(screenshot.filename));

const getPngPixels = async (buffer: Buffer): Promise<Buffer> => {
const png = new PNG(buffer);
const getPngPixels = async (data: Uint8Array): Promise<Uint8Array> => {
const png = new PNG(data);
const {pixels} = await pify(png.parse.bind(png))();
return pixels;
};
Expand Down

0 comments on commit 0f98b0c

Please sign in to comment.