From 9dbd4ebe07e67870fb2b3c98c782d05360ac0540 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 6 Nov 2023 17:24:13 +0700 Subject: [PATCH] Make `.buffer()` return a `Uint8Array` instead of `Buffer` --- index.d.ts | 3 +-- index.js | 2 +- index.test-d.ts | 3 +-- package.json | 1 + test.js | 4 ++-- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index d8ceaa1..7adda02 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,3 @@ -import type {Buffer} from 'node:buffer'; import type { PuppeteerLaunchOptions, Page, @@ -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: (input: string, options?: Options) => Promise; /** Capture a screenshot of the given `input`. diff --git a/index.js b/index.js index d6aa217..6b15b1a 100644 --- a/index.js +++ b/index.js @@ -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); diff --git a/index.test-d.ts b/index.test-d.ts index d5bb14b..7e37f42 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,4 +1,3 @@ -import type {Buffer} from 'node:buffer'; import {expectType} from 'tsd'; import captureWebsite, {devices} from './index.js'; @@ -18,7 +17,7 @@ expectType>( captureWebsite.base64('https://github.com/sindresorhus/capture-website#readme'), ); -expectType>( +expectType>( captureWebsite.buffer('https://github.com/sindresorhus/capture-website#readme'), ); diff --git a/package.json b/package.json index bd46dc4..de6b8ce 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test.js b/test.js index 313d71c..e75bb62 100644 --- a/test.js +++ b/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'; @@ -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 = (() => { @@ -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 => {