react-lib-tools 0.0.43 → 0.0.45

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-lib-tools",
3
- "version": "0.0.43",
3
+ "version": "0.0.45",
4
4
  "type": "module",
5
5
  "author": "Brian Vaughn <brian.david.vaughn@gmail.com> (https://github.com/bvaughn/)",
6
6
  "contributors": [
@@ -3,7 +3,9 @@ import { writeFile } from "node:fs/promises";
3
3
  import { join } from "node:path";
4
4
  import type { SiteSearchRecord } from "react-lib-tools";
5
5
  import { crawlPage } from "./utils/search/crawlPage";
6
+ import { scheduleWork } from "./utils/search/scheduleWork";
6
7
  import type { SiteSearchPage } from "./utils/search/types";
8
+ import { waitForConnection } from "./utils/search/waitForConnection";
7
9
 
8
10
  export async function compileSearchIndex({
9
11
  chromeExecutablePath,
@@ -18,14 +20,23 @@ export async function compileSearchIndex({
18
20
  } = {}) {
19
21
  const recordsMap: Record<string, SiteSearchPage> = {};
20
22
 
21
- await crawlPage({
22
- chromeExecutablePath,
23
- filterSelector,
24
- host,
25
- path: "/",
26
- records: recordsMap
23
+ const url = new URL(host);
24
+
25
+ await waitForConnection({
26
+ host: url.hostname,
27
+ port: parseInt(url.port)
27
28
  });
28
29
 
30
+ await scheduleWork(async () =>
31
+ crawlPage({
32
+ chromeExecutablePath,
33
+ filterSelector,
34
+ host,
35
+ path: "/",
36
+ records: recordsMap
37
+ })
38
+ );
39
+
29
40
  const records = Object.values(recordsMap);
30
41
 
31
42
  const searchIndex = Fuse.createIndex<SiteSearchRecord>(
@@ -1,4 +1,5 @@
1
1
  import puppeteer from "puppeteer";
2
+ import { scheduleWork } from "./scheduleWork";
2
3
  import { stopWords } from "./stopWords";
3
4
  import type { SiteSearchPage } from "./types";
4
5
 
@@ -64,10 +65,6 @@ export async function crawlPage({
64
65
  }
65
66
  }
66
67
 
67
- if (child.hasAttribute("data-demo")) {
68
- continue;
69
- }
70
-
71
68
  if (filterSelector && child.querySelector(filterSelector)) {
72
69
  continue;
73
70
  }
@@ -102,24 +99,30 @@ export async function crawlPage({
102
99
  title: result.title
103
100
  };
104
101
 
105
- await Promise.all(
106
- result.paths
107
- .filter((current) => !records[current])
108
- .map((current) => {
109
- records[current] = {
110
- path: current,
111
- text: "",
112
- title: ""
113
- };
102
+ const filteredPaths = result.paths.filter((current) => {
103
+ if (!records[current]) {
104
+ records[current] = {
105
+ path: current,
106
+ text: "",
107
+ title: ""
108
+ };
114
109
 
115
- return crawlPage({
110
+ return path;
111
+ }
112
+ });
113
+
114
+ await browser.close();
115
+
116
+ scheduleWork(
117
+ ...filteredPaths.map(
118
+ (current) => () =>
119
+ crawlPage({
120
+ chromeExecutablePath,
116
121
  filterSelector,
117
122
  host,
118
123
  path: current,
119
124
  records
120
- });
121
- })
125
+ })
126
+ )
122
127
  );
123
-
124
- await browser.close();
125
128
  }
@@ -0,0 +1,51 @@
1
+ type Work = () => Promise<unknown>;
2
+
3
+ const CONCURRENCY_LIMIT = 5;
4
+
5
+ const active = new Set<Work>();
6
+ const pending: Work[] = [];
7
+
8
+ let promise: Promise<unknown> | undefined = undefined;
9
+ let promiseResolver: (() => void) | undefined = undefined;
10
+
11
+ export async function scheduleWork(...work: Work[]) {
12
+ if (work.length === 0) {
13
+ return;
14
+ }
15
+
16
+ pending.push(...work);
17
+
18
+ if (!promise) {
19
+ promise = new Promise<void>((resolve) => {
20
+ promiseResolver = resolve;
21
+ });
22
+ }
23
+
24
+ for (let index = 0; index < CONCURRENCY_LIMIT; index++) {
25
+ doNextUnitOfWork();
26
+ }
27
+
28
+ await promise;
29
+ }
30
+
31
+ async function doNextUnitOfWork() {
32
+ if (active.size >= CONCURRENCY_LIMIT) {
33
+ return;
34
+ }
35
+
36
+ const next = pending.shift();
37
+ if (next) {
38
+ active.add(next);
39
+
40
+ await next();
41
+
42
+ active.delete(next);
43
+
44
+ doNextUnitOfWork();
45
+ } else if (active.size === 0) {
46
+ promiseResolver?.();
47
+
48
+ promise = undefined;
49
+ promiseResolver = undefined;
50
+ }
51
+ }
@@ -0,0 +1,46 @@
1
+ import { createConnection } from "net";
2
+
3
+ export async function waitForConnection({
4
+ host = "localhost",
5
+ port = 3000,
6
+ retryAttempts = 10,
7
+ retryDelay = 1_000
8
+ }: {
9
+ host?: string;
10
+ port?: number;
11
+ retryAttempts?: number;
12
+ retryDelay?: number;
13
+ } = {}) {
14
+ console.log(`Waiting for connection ${host}:${port} ...`);
15
+
16
+ return new Promise<void>((resolve, reject) => {
17
+ const socket = createConnection({ host, port });
18
+ socket.addListener("connect", () => {
19
+ console.log("Connected");
20
+
21
+ socket.destroy();
22
+
23
+ resolve();
24
+ });
25
+ socket.addListener("error", () => {
26
+ socket.destroy();
27
+
28
+ if (retryAttempts > 1) {
29
+ console.log(`Retrying after ${retryDelay} ms...`);
30
+
31
+ setTimeout(() => {
32
+ waitForConnection({
33
+ host,
34
+ port,
35
+ retryAttempts: retryAttempts - 1,
36
+ retryDelay
37
+ }).then(resolve, reject);
38
+ }, retryDelay);
39
+ } else {
40
+ console.error("Host not available");
41
+
42
+ process.exit(1);
43
+ }
44
+ });
45
+ });
46
+ }