@procedures/browser 0.55.0 → 0.55.2

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/dist/run.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,OAAO,EAAc,KAAK,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAChG,OAAO,EAA4B,KAAK,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAEjF,6DAA6D;AAC7D,eAAO,MAAM,kBAAkB,QAAS,CAAC;AAEzC,4FAA4F;AAC5F,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,aAAa,CAAC,CAAC;AAEjG,kCAAkC;AAClC,MAAM,WAAW,eAAgB,SAAQ,kBAAkB;IACzD,oFAAoF;IACpF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,2DAA2D;IAC3D,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,CAAC;IAC9C,wDAAwD;IACxD,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,iDAAiD;IACjD,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC;IAC1C,4FAA4F;IAC5F,QAAQ,CAAC,MAAM,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC;CAC7C;AAED,8BAA8B;AAC9B,MAAM,WAAW,YAAY;IAC3B,4CAA4C;IAC5C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,6CAA6C;IAC7C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,8CAA8C;IAC9C,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;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AAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC;IACxF,qFAAqF;IACrF,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CAChC;AAED,4EAA4E;AAC5E,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAS5D;AA2BD;;;;GAIG;AACH,wBAAsB,mBAAmB,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,EAAE,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAOtI;AAED;;;;;;;;GAQG;AACH,wBAAsB,cAAc,CAAC,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,YAAY,CAAC,CAahI"}
package/dist/run.js CHANGED
@@ -37,26 +37,64 @@ function fileSize(path) {
37
37
  function lastLines(text, count = 5) {
38
38
  return text.trim().split(/\r?\n/).slice(-count).join("\n");
39
39
  }
40
+ /**
41
+ * This function removes a folder and its content. It tries again 3 times, 100 ms apart, because
42
+ * Windows can hold a file for a short time after the browser stops. A removal that fails leaves the
43
+ * folder and does not throw. Thus it does not hide the result or the error of the caller.
44
+ */
45
+ function removeFolder(path) {
46
+ try {
47
+ rmSync(path, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
48
+ }
49
+ catch {
50
+ // The folder stays in the temporary folder of the system
51
+ }
52
+ }
53
+ /**
54
+ * This function makes a new temporary folder, gives its path to `use`, and then removes the folder,
55
+ * also when `use` throws. The name of the folder starts with `prefix`. The folder is in `folder`,
56
+ * else in the temporary folder of the system. Each temporary folder of this package uses this function.
57
+ */
58
+ export async function withTemporaryFolder(prefix, folder, use) {
59
+ const path = mkdtempSync(join(folder ?? tmpdir(), prefix));
60
+ try {
61
+ return await use(path);
62
+ }
63
+ finally {
64
+ removeFolder(path);
65
+ }
66
+ }
40
67
  /**
41
68
  * This function starts the browser one time for a render, and gives the path and the size of the
42
69
  * file. It throws when it finds no browser, at the time limit and after a cancel. It also throws
43
70
  * when the file is missing, empty or not of the kind of the render.
44
71
  *
45
72
  * An existing output file changes only after the check of the new file. Thus a failed render keeps
46
- * an old output file.
73
+ * an old output file. Without the option `output`, the file goes in a new temporary folder. This
74
+ * folder stays after a success, and goes after a failure.
47
75
  */
48
76
  export async function headlessRender(render, source, options) {
49
77
  const browser = findBrowser(options);
50
78
  if (browser === undefined) {
51
79
  throw new Error(`No browser found. Install Microsoft Edge, Google Chrome or Chromium, or set ${BROWSER_ENV} to the browser program.`);
52
80
  }
53
- const work = mkdtempSync(join(tmpdir(), render.prefix));
54
- const profile = join(work, "profile");
55
- const htmlFile = join(work, "page.html");
56
- const output = resolve(options.output ?? join(work, render.file));
57
- const written = join(work, render.file);
58
- let made = false;
81
+ if (options.output !== undefined)
82
+ return renderOnce(render, source, options, browser, resolve(options.output));
83
+ const kept = mkdtempSync(join(tmpdir(), render.prefix));
59
84
  try {
85
+ return await renderOnce(render, source, options, browser, join(kept, render.file));
86
+ }
87
+ catch (error) {
88
+ removeFolder(kept);
89
+ throw error;
90
+ }
91
+ }
92
+ /** This function starts the browser in a temporary folder that always goes, and copies the checked file to `output`. */
93
+ function renderOnce(render, source, options, browser, output) {
94
+ return withTemporaryFolder(render.prefix, undefined, async (work) => {
95
+ const profile = join(work, "profile");
96
+ const htmlFile = join(work, "page.html");
97
+ const written = join(work, render.file);
60
98
  let page;
61
99
  if ("html" in source) {
62
100
  writeFileSync(htmlFile, source.html, "utf8");
@@ -91,21 +129,9 @@ export async function headlessRender(render, source, options) {
91
129
  }
92
130
  if (!render.isValid(written))
93
131
  throw new Error(`The file ${output} is not a ${render.noun}`);
94
- // The temporary folder goes in `finally`, so a copy is enough, also across two drives
95
- if (written !== output)
96
- copyFileSync(written, output);
97
- made = true;
132
+ // The temporary folder goes after this function, so a copy is enough, also across two drives
133
+ copyFileSync(written, output);
98
134
  return { path: output, bytes, browser };
99
- }
100
- finally {
101
- // The temporary page and profile go. A default output stays in its folder after a success.
102
- if (made && options.output === undefined) {
103
- rmSync(profile, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
104
- rmSync(htmlFile, { force: true });
105
- }
106
- else {
107
- rmSync(work, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
108
- }
109
- }
135
+ });
110
136
  }
111
137
  //# sourceMappingURL=run.js.map
package/dist/run.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC/H,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,UAAU,EAA2C,MAAM,2BAA2B,CAAC;AAChG,OAAO,EAAE,WAAW,EAAE,WAAW,EAA2B,MAAM,cAAc,CAAC;AAEjF,6DAA6D;AAC7D,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAC;AA8CzC,4EAA4E;AAC5E,MAAM,UAAU,QAAQ,CAAC,IAAY,EAAE,KAAa;IAClD,MAAM,EAAE,GAAG,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC/B,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACjC,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AAI,KAAK,CAAC,uBAAuB,MAAM,CAAC,IAAI,eAAe,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QACrH,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,YAAY,MAAM,aAAa,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;QAC5F,6FAA6F;QAC7F,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC9B,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;IAC1C,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -60,4 +60,25 @@ export declare function isImageFile(path: string, format: ScreenshotFormat): boo
60
60
  * own.
61
61
  */
62
62
  export declare function renderScreenshot(options: RenderScreenshotOptions): Promise<RenderScreenshotResult>;
63
+ /** The options of `renderScreenshotBytes`: the options of `renderScreenshot`, with no `output`. */
64
+ export interface RenderScreenshotBytesOptions extends Omit<RenderScreenshotOptions, "output"> {
65
+ /** The folder that gets the temporary folder of the picture. The default is the temporary folder of the system. */
66
+ readonly folder?: string | undefined;
67
+ }
68
+ /** The result of `renderScreenshotBytes`: the picture, its format and the browser program. */
69
+ export interface RenderScreenshotBytesResult {
70
+ /** The bytes of the picture. In Node.js, the value is a `Buffer`. */
71
+ readonly data: Uint8Array;
72
+ readonly format: ScreenshotFormat;
73
+ /** The browser program that made the picture. */
74
+ readonly browser: string;
75
+ }
76
+ /**
77
+ * This function loads a page in a headless browser, and gives the picture of the window as bytes.
78
+ * It has the options and the errors of `renderScreenshot`, with no `output`.
79
+ *
80
+ * The function writes the picture to a new temporary folder, and reads it. Then it removes the
81
+ * folder, also after an error. A folder that the system does not let it remove stays, and gives no error.
82
+ */
83
+ export declare function renderScreenshotBytes(options: RenderScreenshotBytesOptions): Promise<RenderScre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CAAC,uBAAuB,EAAE,QAAQ,CAAC;IAC3F,mHAAmH;IACnH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACtC;AAED,8FAA8F;AAC9F,MAAM,WAAW,2BAA2B;IAC1C,qEAAqE;IACrE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC;IAClC,iDAAiD;IACjD,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;GAMG;AACH,wBAAsB,qBAAqB,CAAC,OAAO,EAAE,4BAA4B,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAOvH"}
@@ -3,8 +3,9 @@
3
3
  * uses the headless run of `run.ts`. The browser loads the page in a window of the given size, and
4
4
  * writes a PNG, JPEG or WebP picture of the window.
5
5
  */
6
- import { extname } from "node:path";
7
- import { headlessRender, readHead } from "./run.js";
6
+ import { readFileSync } from "node:fs";
7
+ import { extname, join } from "node:path";
8
+ import { headlessRender, readHead, withTemporaryFolder } from "./run.js";
8
9
  /** The default size of the window: a common size of a desktop screen. */
9
10
  export const DEFAULT_SCREENSHOT_SIZE = { width: 1280, height: 800 };
10
11
  /** The largest side of the window, in CSS pixels. */
@@ -105,4 +106,19 @@ export async function renderScreenshot(options) {
105
106
  };
106
107
  return { ...(await headlessRender(render, source, options)), format };
107
108
  }
109
+ /**
110
+ * This function loads a page in a headless browser, and gives the picture of the window as bytes.
111
+ * It has the options and the errors of `renderScreenshot`, with no `output`.
112
+ *
113
+ * The function writes the picture to a new temporary folder, and reads it. Then it removes the
114
+ * folder, also after an error. A folder that the system does not let it remove stays, and gives no error.
115
+ */
116
+ export async function renderScreenshotBytes(options) {
117
+ const { folder, ...rest } = options;
118
+ const format = rest.format ?? "png";
119
+ return withTemporaryFolder("procedures-screenshot-bytes-", folder, async (work) => {
120
+ const { path, browser } = await renderScreenshot({ ...rest, format, output: join(work, `picture${SCREENSHOT_EXTENSIONS[format]}`) });
121
+ return { data: readFileSync(path), format, browser };
122
+ });
123
+ }
108
124
  //# sourceMappingURL=screenshot.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"screenshot.js","sourceRoot":"","sources":["../src/screenshot.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAiF,MAAM,UAAU,CAAC;AAWnI,yEAAyE;AACzE,MAAM,CAAC,MAAM,uBAAuB,GAAmB,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;AAEpF,qDAAqD;AACrD,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC;AAE1C,0DAA0D;AAC1D,MAAM,CAAC,MAAM,qBAAqB,GAA+C,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AAE9H,sDAAsD;AACtD,MAAM,mBAAmB,GAA+C,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AAE5I,2DAA2D;AAC3D,MAAM,OAAO,GAAwB,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;AA0B3E;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAe,EAAE,MAAyB;IAC3E,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,MAAM,IAAI,KAAK,CAAC;IACjD,MAAM,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC;IAChD,MAAM,QAAQ,GAAG,mBAAmB,CAAC,SAAS,CAAC,CAAC;IAChD,IAAI,QAAQ,KAAK,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,yEAAyE,SAAS,GAAG,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,CAAC,KAAK,UAAU,qBAAqB,CAAC,OAAqC;IAC/E,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;IACpC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,KAAK,CAAC;IACpC,OAAO,mBAAmB,CAAC,8BAA8B,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;QAChF,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,gBAAgB,CAAC,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,IAAI,EAAE,UAAU,qBAAqB,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;QACrI,OAAO,EAAE,IAAI,EAAE,YAAY,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;IACvD,CAAC,CAAC,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@procedures/browser",
4
- "version": "0.55.0",
5
- "description": "Find a Chromium browser that is already installed (Edge, Chrome or Chromium), and render a page with it: a PDF file or a screenshot, with no browser download.",
4
+ "version": "0.55.2",
5
+ "description": "Find a Chromium browser that is already installed (Edge, Chrome or Chromium), and render a page with it: a PDF file or a screenshot, with no browser download. Find and read the bookmarks files of the profiles of Chrome, Edge and Chromium.",
6
6
  "keywords": [
7
7
  "browser",
8
8
  "headless",
@@ -10,6 +10,7 @@
10
10
  "edge",
11
11
  "pdf",
12
12
  "screenshot",
13
+ "bookmarks",
13
14
  "procedures"
14
15
  ],
15
16
  "homepage": "https://mark1russell7.github.io/client/",
@@ -30,6 +31,10 @@
30
31
  ".": {
31
32
  "types": "./dist/index.d.ts",
32
33
  "import": "./dist/index.js"
34
+ },
35
+ "./bookmarks": {
36
+ "types": "./dist/bookmarks.d.ts",
37
+ "import": "./dist/bookmarks.js"
33
38
  }
34
39
  },
35
40
  "files": [
@@ -50,15 +55,20 @@
50
55
  "provenance": true
51
56
  },
52
57
  "dependencies": {
53
- "@procedures/shell": "0.55.0"
58
+ "@procedures/shell": "0.55.2"
54
59
  },
55
60
  "devDependencies": {
56
61
  "typescript": "^7.0.2",
57
62
  "@types/node": "^22.0.0",
58
63
  "@mark1russell7/cue": "^2.1.1",
59
- "vitest": "^5.0.3"
64
+ "vitest": "^5.0.3",
65
+ "fast-check": "^4.10.2"
66
+ },
67
+ "client": {
68
+ "browser": [
69
+ "./bookmarks"
70
+ ]
60
71
  },
61
- "client": null,
62
72
  "scripts": {
63
73
  "build": "tsc -b",
64
74
  "typecheck": "tsc --noEmit && tsc -p tsconfig.test.json",
@@ -0,0 +1,215 @@
1
+ /**
2
+ * The bookmarks files of Chrome. Edge and Chromium write the same format. This file reads the JSON
3
+ * of a bookmarks file, and gives a tree of folders with their sites. It also reads the names of the
4
+ * profiles from the file `Local State`.
5
+ *
6
+ * This file imports no Node built-in, so a page can use it: the entry `@procedures/browser/bookmarks`.
7
+ * The search for the files on the disk is in `profiles.ts`, on the root entry.
8
+ */
9
+
10
+ /** The deepest folder that the parser reads. A deeper folder gives a problem, and the parser does not read it. */
11
+ export const MAX_BOOKMARK_DEPTH = 100;
12
+
13
+ /** The largest bookmarks file that a reader accepts, in bytes. A larger file is not a bookmarks file. */
14
+ export const MAX_BOOKMARKS_BYTES: number = 64 * 1024 * 1024;
15
+
16
+ /**
17
+ * The kind of a bookmarks file. `profile` is the file `Bookmarks` of a profile. `account` is the
18
+ * file `AccountBookmarks`: the bookmarks of a Google account with no sync.
19
+ */
20
+ export type BookmarkFileKind = "profile" | "account";
21
+
22
+ /** The bookmarks files of a profile, by the name of the file, with their kinds. */
23
+ export const BOOKMARK_FILES: Readonly<Record<string, BookmarkFileKind>> = { Bookmarks: "profile", AccountBookmarks: "account" };
24
+
25
+ /** A bookmarks file, with no path. A host can give this to a page and keep the path. */
26
+ export interface BookmarkSource {
27
+ /** The id of the file. The search gives `<browser>:<profile folder>:<file name>`, for example `chrome:Default:Bookmarks`. */
28
+ readonly id: string;
29
+ /** The name of the profile, as the browser shows it. */
30
+ readonly name: string;
31
+ readonly kind: BookmarkFileKind;
32
+ }
33
+
34
+ /** A bookmark of a web site. */
35
+ export interface Bookmark {
36
+ /** The address of the site: an `http` or `https` address with a host. */
37
+ readonly url: string;
38
+ /** The title of the bookmark. A bookmark with an empty title has no title. */
39
+ readonly title?: string;
40
+ /** The ISO 8601 time when the user added the bookmark. */
41
+ readonly added?: string;
42
+ /** The names of the folders from the root to the folder of the bookmark. */
43
+ readonly path: readonly string[];
44
+ }
45
+
46
+ /** A folder of the bookmarks, with its sites and the folders in it. */
47
+ export interface BookmarkFolder {
48
+ /**
49
+ * The key of the folder, after the prefix of the parse: its `guid`, else its `id`, else its place in the file. The place is
50
+ * the name of the root, then the index of each child, for example `bookmark_bar/2/0`. Each parse of a file gives the same keys.
51
+ */
52
+ readonly key: string;
53
+ readonly name: string;
54
+ /** The names of the folders from the root to this folder. The last name is the name of this folder. */
55
+ readonly path: readonly string[];
56
+ readonly folders: readonly BookmarkFolder[];
57
+ /** The sites directly in this folder. Each site has the path of this folder. */
58
+ readonly sites: readonly Bookmark[];
59
+ /** The count of the sites in this folder and in each folder in it. */
60
+ readonly total: number;
61
+ }
62
+
63
+ /** A part of a bookmarks file that the parser cannot read. */
64
+ export interface BookmarkProblem {
65
+ /** The names of the folders to the part. */
66
+ readonly path: readonly string[];
67
+ readonly message: string;
68
+ }
69
+
70
+ /** The result of the parse of a bookmarks file. */
71
+ export interface ChromeBookmarks {
72
+ /** The root folders, for example "Bookmarks bar" and "Other bookmarks". */
73
+ readonly roots: readonly BookmarkFolder[];
74
+ /** The count of the bookmarks that are not web sites, for example `javascript:` or `chrome://` addresses. */
75
+ readonly skipped: number;
76
+ readonly problems: readonly BookmarkProblem[];
77
+ }
78
+
79
+ /** The options of `parseChromeBookmarks`. */
80
+ export interface ParseChromeBookmarksOptions {
81
+ /** The text before each key. Give two files of one profile different prefixes, so that their keys are different. */
82
+ readonly prefix?: string | undefined;
83
+ }
84
+
85
+ /** The order of the roots that Chrome writes. The roots with other names come after them, in the order of their code units. */
86
+ const ROOT_ORDER = ["bookmark_bar", "other", "synced"];
87
+
88
+ /** The time from 1601-01-01 (the start of the Chrome times) to 1970-01-01, in milliseconds. */
89
+ const WINDOWS_EPOCH_MS = 11_644_473_600_000n;
90
+
91
+ /**
92
+ * A web address has the scheme `http` or `https`, a host, and no spaces. The test does not use `URL`, so it is the same in each
93
+ * runtime. The first character of the host is one class, and the rest is `\S*`. Thus no two quantifiers agree on the same
94
+ * character, and the time of the test is linear in the length of the address.
95
+ */
96
+ const WEB = /^https?:\/\/[^\s/?#]\S*$/i;
97
+
98
+ /**
99
+ * This function gives the ISO 8601 time of a Chrome time, or `undefined` for no time. A Chrome time
100
+ * is a text of the microseconds from 1601-01-01 UTC. The function uses `BigInt`, because the value
101
+ * is larger than a safe integer.
102
+ */
103
+ export function chromeTime(value: unknown): string | undefined {
104
+ if (typeof value !== "string" || !/^\d{1,20}$/.test(value)) return undefined;
105
+ const ms = BigInt(value) / 1000n - WINDOWS_EPOCH_MS;
106
+ // The time 0 means no time. A time before 1990 or after 9999 is not a date of a bookmark.
107
+ if (ms < 631_152_000_000n || ms > 253_402_300_799_999n) return undefined;
108
+ return new Date(Number(ms)).toISOString();
109
+ }
110
+
111
+ interface Node {
112
+ readonly type?: unknown;
113
+ readonly name?: unknown;
114
+ readonly url?: unknown;
115
+ readonly children?: unknown;
116
+ readonly guid?: unknown;
117
+ readonly id?: unknown;
118
+ readonly date_added?: unknown;
119
+ }
120
+
121
+ function isNode(value: unknown): value is Node {
122
+ return typeof value === "object" && value !== null && !Array.isArray(value);
123
+ }
124
+
125
+ /** This function gives the `guid` of a folder, else its `id`. An empty text or a value of another type is not a key. */
126
+ function ownKey(node: Node): string | undefined {
127
+ if (typeof node.guid === "string" && node.guid !== "") return node.guid;
128
+ if (typeof node.id === "string" && node.id !== "") return node.id;
129
+ if (typeof node.id === "number" && Number.isFinite(node.id)) return String(node.id);
130
+ return undefined;
131
+ }
132
+
133
+ function rank(name: string): number {
134
+ const index = ROOT_ORDER.indexOf(name);
135
+ return index === -1 ? ROOT_ORDER.length : index;
136
+ }
137
+
138
+ /**
139
+ * This function reads the JSON value of a Chrome bookmarks file: the file `Bookmarks` or
140
+ * `AccountBookmarks` of a profile. It gives a problem for each part that it cannot read, and it
141
+ * reads the other parts. It keeps only the bookmarks of web sites, and counts the others.
142
+ *
143
+ * @throws Error when the value has no roots, so it is not a bookmarks file
144
+ */
145
+ export function parseChromeBookmarks(value: unknown, options: ParseChromeBookmarksOptions = {}): ChromeBookmarks {
146
+ const prefix = options.prefix ?? "";
147
+ if (!isNode(value) || !isNode((value as { roots?: unknown }).roots)) throw new Error("A Chrome bookmarks file has an object roots.");
148
+ const rootNodes = (value as { roots: Record<string, unknown> }).roots;
149
+ const problems: BookmarkProblem[] = [];
150
+ let skipped = 0;
151
+
152
+ // The place of a folder is the name of its root, then the index of each child to it. It is unique in a file.
153
+ const folderOf = (node: Node, parent: readonly string[], fallback: string, depth: number, place: string): BookmarkFolder => {
154
+ const name = typeof node.name === "string" && node.name !== "" ? node.name : fallback;
155
+ const path = [...parent, name];
156
+ const own = ownKey(node) ?? place;
157
+ const folders: BookmarkFolder[] = [];
158
+ const sites: Bookmark[] = [];
159
+ let total = 0;
160
+ const children = Array.isArray(node.children) ? (node.children as unknown[]) : [];
161
+ if (node.children !== undefined && !Array.isArray(node.children)) problems.push({ path, message: "The children of a folder must be a list." });
162
+ for (const [index, child] of children.entries()) {
163
+ if (!isNode(child)) {
164
+ problems.push({ path, message: "A bookmark must be an object." });
165
+ } else if (child.type === "url") {
166
+ if (typeof child.url !== "string" || !WEB.test(child.url)) {
167
+ skipped++;
168
+ continue;
169
+ }
170
+ const title = typeof child.name === "string" && child.name.trim() !== "" ? child.name : undefined;
171
+ const added = chromeTime(child.date_added);
172
+ sites.push({ url: child.url, ...(title === undefined ? {} : { title }), ...(added === undefined ? {} : { added }), path });
173
+ total++;
174
+ } else if (child.type === "folder") {
175
+ if (depth >= MAX_BOOKMARK_DEPTH) {
176
+ problems.push({ path, message: `A folder is deeper than ${MAX_BOOKMARK_DEPTH} folders. The parser does not read it.` });
177
+ continue;
178
+ }
179
+ const folder = folderOf(child, path, "Folder", depth + 1, `${place}/${index}`);
180
+ folders.push(folder);
181
+ total += folder.total;
182
+ } else {
183
+ problems.push({ path, message: `A bookmark has the type ${String(child.type)}, not url or folder.` });
184
+ }
185
+ }
186
+ return { key: `${prefix}${own}`, name, path, folders, sites, total };
187
+ };
188
+
189
+ // The order of the code units, not of a locale, so each runtime gives the same parse. The names are the keys of one
190
+ // object, so no two names are equal.
191
+ const names = Object.keys(rootNodes).sort((a, b) => rank(a) - rank(b) || (a < b ? -1 : 1));
192
+ const roots: BookmarkFolder[] = [];
193
+ for (const name of names) {
194
+ const node = rootNodes[name];
195
+ // The roots object of an old file also has values that are not folders, for example a sync transaction version
196
+ if (!isNode(node) || node.type !== "folder") continue;
197
+ roots.push(folderOf(node, [], name, 1, name));
198
+ }
199
+ return { roots, skipped, problems };
200
+ }
201
+
202
+ /**
203
+ * This function gives the name of each profile from the JSON of the file `Local State` of a
204
+ * Chromium browser, by the folder of the profile. A value that it cannot read gives no names.
205
+ */
206
+ export function profileNames(localState: unknown): Map<string, string> {
207
+ const names = new Map<string, string>();
208
+ const cache = (localState as { profile?: { info_cache?: unknown } } | null)?.profile?.info_cache;
209
+ if (typeof cache !== "object" || cache === null) return names;
210
+ for (const [folder, info] of Object.entries(cache)) {
211
+ const name = (info as { name?: unknown } | null)?.name;
212
+ if (typeof name === "string" && name.trim() !== "") names.set(folder, name);
213
+ }
214
+ return names;
215
+ }
package/src/index.ts CHANGED
@@ -3,3 +3,6 @@ export * from "./browser.js";
3
3
  export { DEFAULT_TIMEOUT_MS, type CommandRunner, type HeadlessOptions, type RenderResult } from "./run.js";
4
4
  export { isPdfFile, pdfArgs, renderPdf, type RenderPdfOptions, type RenderPdfResult } from "./pdf.js";
5
5
  export * from "./screenshot.js";
6
+ // The bookmarks files of Chrome, Edge and Chromium: the parse (also on the browser entry ./bookmarks) and the search on the disk
7
+ export * from "./bookmarks.js";
8
+ export * from "./profiles.js";