@procedures/browser 0.55.1 → 0.56.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/screenshot.ts CHANGED
@@ -4,8 +4,9 @@
4
4
  * writes a PNG, JPEG or WebP picture of the window.
5
5
  */
6
6
 
7
- import { extname } from "node:path";
8
- import { headlessRender, readHead, type HeadlessOptions, type HeadlessRender, type PageSource, type RenderResult } from "./run.js";
7
+ import { readFileSync } from "node:fs";
8
+ import { extname, join } from "node:path";
9
+ import { headlessRender, readHead, withTemporaryFolder, type HeadlessOptions, type HeadlessRender, type PageSource, type RenderResult } from "./run.js";
9
10
 
10
11
  /** The formats of a screenshot. The browser chooses the format from the extension of the file. */
11
12
  export type ScreenshotFormat = "png" | "jpeg" | "webp";
@@ -139,3 +140,34 @@ export async function renderScreenshot(options: RenderScreenshotOptions): Promis
139
140
  };
140
141
  return { ...(await headlessRender(render, source, options)), format };
141
142
  }
143
+
144
+ /** The options of `renderScreenshotBytes`: the options of `renderScreenshot`, with no `output`. */
145
+ export interface RenderScreenshotBytesOptions extends Omit<RenderScreenshotOptions, "output"> {
146
+ /** The folder that gets the temporary folder of the picture. The default is the temporary folder of the system. */
147
+ readonly folder?: string | undefined;
148
+ }
149
+
150
+ /** The result of `renderScreenshotBytes`: the picture, its format and the browser program. */
151
+ export interface RenderScreenshotBytesResult {
152
+ /** The bytes of the picture. In Node.js, the value is a `Buffer`. */
153
+ readonly data: Uint8Array;
154
+ readonly format: ScreenshotFormat;
155
+ /** The browser program that made the picture. */
156
+ readonly browser: string;
157
+ }
158
+
159
+ /**
160
+ * This function loads a page in a headless browser, and gives the picture of the window as bytes.
161
+ * It has the options and the errors of `renderScreenshot`, with no `output`.
162
+ *
163
+ * The function writes the picture to a new temporary folder, and reads it. Then it removes the
164
+ * folder, also after an error. A folder that the system does not let it remove stays, and gives no error.
165
+ */
166
+ export async function renderScreenshotBytes(options: RenderScreenshotBytesOptions): Promise<RenderScreenshotBytesResult> {
167
+ const { folder, ...rest } = options;
168
+ const format = rest.format ?? "png";
169
+ return withTemporaryFolder("procedures-screenshot-bytes-", folder, async (work) => {
170
+ const { path, browser } = await renderScreenshot({ ...rest, format, output: join(work, `picture${SCREENSHOT_EXTENSIONS[format]}`) });
171
+ return { data: readFileSync(path), format, browser };
172
+ });
173
+ }