@microfox/puppeteer-sls
Version:
Puppeteer SLS - Run puppeteer on a serverless function (AWS Lambda, etc.)
222 lines (212 loc) • 6.62 kB
TypeScript
import { S3SpaceConfig } from '@microfox/s3-space';
import { Page } from 'puppeteer-core';
/**
* Options for opening a new page.
*/
interface OpenPageOptions {
/**
* The URL to navigate to.
*/
url: string;
/**
* Viewport dimensions.
*/
defaultViewport?: {
width: number;
height: number;
};
/**
* Whether to run in headless mode.
* @default false
*/
headless?: boolean;
/**
* Whether to close the browser after the operation is complete.
* @default false
*/
isLocal?: boolean;
/**
* whether to wait complete page load
* @default true
*/
waitUntil?: 'networkidle2' | 'networkidle0' | 'domcontentloaded' | 'load';
}
/**
* Opens a new page in a browser instance and navigates to a URL.
*
* @param options - The options for opening the page.
* @returns A promise that resolves to the Puppeteer page object.
*/
declare function openPage({ url, defaultViewport, headless, isLocal, waitUntil, }: OpenPageOptions): Promise<{
browser: any;
page: any;
}>;
/**
* Options for taking a screenshot.
*/
interface TakeSnapShotOptions extends OpenPageOptions {
/**
* The file name for the screenshot.
*/
fileName: string;
/**
* The quality of the screenshot. Only for jpeg.
* @default 100
*/
quality?: number;
/**
* The encoding of the screenshot.
* @default 'binary'
*/
encoding?: 'base64' | 'binary';
/**
* S3 configuration for uploading the screenshot.
*/
s3Config: S3SpaceConfig;
}
/**
* Opens a page and takes a screenshot, uploads it to S3 and returns public URL.
*
* @param options - The options for taking the screenshot.
* @returns A promise that resolves with the public URL of the screenshot.
*/
declare function takeSnapShot(options: TakeSnapShotOptions): Promise<string | undefined>;
type ExtractWebpageContentOptions = OpenPageOptions & {
/**
* Whether to convert the content to Markdown.
* @default false
*/
toMarkdown?: boolean;
/**
* Whether to convert the content to HTML.
* @default false
*/
toHTML?: boolean;
};
/**
* Opens a page and converts its content to Markdown.
*
* @param options - The options for opening the page.
* @returns A promise that resolves to the Markdown content of the page.
*/
declare function extractToMarkdown(page: Page): Promise<string>;
/**
* Opens a page and extracts its full HTML content.
*
* @param options - The options for opening the page.
* @returns A promise that resolves to the HTML content of the page.
*/
declare function extractHTML(page: Page): Promise<string>;
/**
* Opens a page and extracts its text content.
*
* @param options - The options for opening the page.
* @returns A promise that resolves to the text content of the page.
*/
declare function extractWebpage(options: ExtractWebpageContentOptions): Promise<{
content: any;
browser: any;
markdown: string | undefined;
html: string | undefined;
page: any;
}>;
type ExtractedLink = {
href: string;
displayName: string;
type: 'statix' | 'webpage';
inPageLink: boolean;
externalLink: boolean;
internalLink: boolean;
staticType?: 'video' | 'image' | 'file' | 'other';
fileExtension?: string;
};
/**
* Opens a page and extracts all links.
*
* @param options - The options for opening the page.
* @returns A promise that resolves to an array of links.
*/
declare function extractLinks(page: Page): Promise<ExtractedLink[]>;
interface ExtractLinksOptions extends OpenPageOptions {
onlyExternal?: boolean;
onlyInternal?: boolean;
onlyInPage?: boolean;
onlyStatic?: boolean;
}
declare function extractLinksFromUrl(options: ExtractLinksOptions): Promise<ExtractedLink[]>;
interface AccessibilityInfo {
color: string;
contrastRatio: number;
}
interface ExtractedImage {
src: string | null;
srcset: string | null;
responsiveImages: {
url: string;
size: string;
}[] | null;
alt: string | null;
imgPermalink: string | null;
pagePermalink: string;
width: number | null;
height: number | null;
dominantColor?: string | null;
secondaryColor?: string | null;
accentColor?: string | null;
accessibility?: AccessibilityInfo | null;
palette?: string[] | null;
colorExtractionLogs?: string[];
}
declare function extractImages(page: Page, options?: {
enableColorExtraction?: boolean;
}): Promise<ExtractedImage[]>;
declare function extractBackgroundImages(page: Page): Promise<ExtractedImage[]>;
declare function extractImagesFromURL(options: OpenPageOptions & {
deepExtract?: boolean;
enableColorExtraction?: boolean;
}): Promise<ExtractedImage[]>;
interface ExtractedVideo {
src: string | null;
sources: {
src: string;
type: string | null;
}[] | null;
poster: string | null;
videoPermalink: string | null;
pagePermalink: string;
width: number | null;
height: number | null;
}
declare function extractVideos(page: Page): Promise<ExtractedVideo[]>;
declare function extractVideosFromURL(options: OpenPageOptions & {
deepExtract?: boolean;
}): Promise<ExtractedVideo[]>;
/**
* @description PuppeteerSLS is a class that provides a set of functions for running Puppeteer on serverless functions.
* every instance starts from a new browser instance.
* @example
* ```ts
* const puppeteerSLS = new PuppeteerSLS();
* const images = await puppeteerSLS.extractImagesFromURL('https://www.google.com');
* ```
*/
declare const PuppeteerSLS: {
extractImagesFromURL: typeof extractImagesFromURL;
extractLinksFromUrl: typeof extractLinksFromUrl;
extractWebpage: typeof extractWebpage;
openPage: typeof openPage;
takeSnapShot: typeof takeSnapShot;
};
declare const puppeteerLaunchProps: (defaultViewport?: {
width: number;
height: number;
}, _isLocal?: boolean, headless?: boolean) => Promise<{
args: string[];
defaultViewport: {
width: number;
height: number;
};
executablePath: string;
headless: boolean;
}>;
export { type AccessibilityInfo, type ExtractLinksOptions, type ExtractWebpageContentOptions, type ExtractedImage, type ExtractedLink, type ExtractedVideo, type OpenPageOptions, PuppeteerSLS, type TakeSnapShotOptions, extractBackgroundImages, extractHTML, extractImages, extractImagesFromURL, extractLinks, extractLinksFromUrl, extractToMarkdown, extractVideos, extractVideosFromURL, extractWebpage, openPage, puppeteerLaunchProps, takeSnapShot };