Skip to content

Commit

Permalink
Make .buffer() return a 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 96723f7 commit 9dbd4eb
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions index.d.ts
@@ -1,4 +1,3 @@
import type {Buffer} from 'node:buffer';
import type {
PuppeteerLaunchOptions,
Page,
Expand Down Expand Up @@ -466,7 +465,7 @@ declare const captureWebsite: {
@param input - The URL, file URL, data URL, local file path to the website, or HTML.
@returns The screenshot as binary.
*/
buffer: (input: string, options?: Options) => Promise<Buffer>;
buffer: (input: string, options?: Options) => Promise<Uint8Array>;

/**
Capture a screenshot of the given `input`.
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -452,7 +452,7 @@ captureWebsite.file = async (url, filePath, options = {}) => {
});
};

captureWebsite.buffer = async (url, options) => internalCaptureWebsite(url, options);
captureWebsite.buffer = async (url, options) => new Uint8Array(await internalCaptureWebsite(url, options));

captureWebsite.base64 = async (url, options) => {
const screenshot = await internalCaptureWebsite(url, options);
Expand Down
3 changes: 1 addition & 2 deletions index.test-d.ts
@@ -1,4 +1,3 @@
import type {Buffer} from 'node:buffer';
import {expectType} from 'tsd';
import captureWebsite, {devices} from './index.js';

Expand All @@ -18,7 +17,7 @@ expectType<Promise<string>>(
captureWebsite.base64('https://github.com/sindresorhus/capture-website#readme'),
);

expectType<Promise<Buffer>>(
expectType<Promise<Uint8Array>>(
captureWebsite.buffer('https://github.com/sindresorhus/capture-website#readme'),
);

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -59,6 +59,7 @@
"png-js": "^1.0.0",
"tempy": "^3.1.0",
"tsd": "^0.29.0",
"uint8array-extras": "^0.5.0",
"xo": "^0.56.0"
}
}
4 changes: 2 additions & 2 deletions test.js
@@ -1,5 +1,4 @@
/* global document */
import {Buffer} from 'node:buffer';
import fs from 'node:fs';
import test from 'ava';
import {imageDimensionsFromData} from 'image-dimensions';
Expand All @@ -13,6 +12,7 @@ import delay from 'delay';
import toughCookie from 'tough-cookie';
import fileUrl from 'file-url';
import {KnownDevices} from 'puppeteer';
import {base64ToUint8Array} from 'uint8array-extras';
import captureWebsite from './index.js';

const defaultResponse = (() => {
Expand Down Expand Up @@ -109,7 +109,7 @@ test('captureWebsite.base64()', async t => {
});

t.is(typeof screenshot, 'string');
t.true(isPng(Buffer.from(screenshot, 'base64')));
t.true(isPng(base64ToUint8Array(screenshot)));
});

test('`type` option', async t => {
Expand Down

0 comments on commit 9dbd4eb

Please sign in to comment.