@d-zero/beholder 2.1.6 → 3.1.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/README.md +26 -0
  3. package/dist/dom-evaluation.d.ts +72 -24
  4. package/dist/dom-evaluation.js +310 -84
  5. package/dist/extract-meta.d.ts +98 -0
  6. package/dist/extract-meta.js +75 -0
  7. package/dist/index.d.ts +3 -1
  8. package/dist/index.js +1 -0
  9. package/dist/meta/classify.d.ts +52 -0
  10. package/dist/meta/classify.js +731 -0
  11. package/dist/meta/collect-head.d.ts +63 -0
  12. package/dist/meta/collect-head.js +223 -0
  13. package/dist/meta/id-extractors.d.ts +40 -0
  14. package/dist/meta/id-extractors.js +196 -0
  15. package/dist/meta/keys.d.ts +41 -0
  16. package/dist/meta/keys.js +507 -0
  17. package/dist/meta/parsers.d.ts +74 -0
  18. package/dist/meta/parsers.js +293 -0
  19. package/dist/meta/tag-detection.d.ts +59 -0
  20. package/dist/meta/tag-detection.js +120 -0
  21. package/dist/meta/types.d.ts +874 -0
  22. package/dist/meta/types.js +12 -0
  23. package/dist/scraper.js +15 -13
  24. package/dist/types.d.ts +3 -38
  25. package/package.json +8 -5
  26. package/src/dom-evaluation.spec.ts +301 -73
  27. package/src/dom-evaluation.ts +417 -88
  28. package/src/extract-meta.spec.ts +247 -0
  29. package/src/extract-meta.ts +121 -0
  30. package/src/index.ts +45 -0
  31. package/src/meta/classify.spec.ts +281 -0
  32. package/src/meta/classify.ts +810 -0
  33. package/src/meta/collect-head.ts +247 -0
  34. package/src/meta/id-extractors.spec.ts +69 -0
  35. package/src/meta/id-extractors.ts +206 -0
  36. package/src/meta/keys.ts +568 -0
  37. package/src/meta/parsers.spec.ts +178 -0
  38. package/src/meta/parsers.ts +304 -0
  39. package/src/meta/simple-wappalyzer.d.ts +37 -0
  40. package/src/meta/tag-detection.spec.ts +134 -0
  41. package/src/meta/tag-detection.ts +161 -0
  42. package/src/meta/types.ts +949 -0
  43. package/src/scraper.ts +19 -13
  44. package/src/types.ts +49 -55
  45. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,247 @@
1
+ import { JSDOM } from 'jsdom';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ import { extractMetaFromDocument } from './extract-meta.js';
5
+
6
+ const URL = 'https://example.com/';
7
+
8
+ /**
9
+ *
10
+ * @param html
11
+ */
12
+ function mkDom(html: string): JSDOM {
13
+ return new JSDOM(html, { url: URL });
14
+ }
15
+
16
+ /**
17
+ *
18
+ * @param dom
19
+ */
20
+ function asWindow(dom: JSDOM): Window {
21
+ return dom.window as unknown as Window;
22
+ }
23
+
24
+ describe('extractMetaFromDocument', () => {
25
+ it('extracts <title>, lang and basic <meta name=description>', async () => {
26
+ const html = `<!doctype html>
27
+ <html lang="ja">
28
+ <head>
29
+ <title>Example Title</title>
30
+ <meta name="description" content="An example page">
31
+ <meta name="keywords" content="a, b, c">
32
+ </head>
33
+ <body></body>
34
+ </html>`;
35
+ const dom = mkDom(html);
36
+ const meta = await extractMetaFromDocument(asWindow(dom), { url: URL, html });
37
+
38
+ expect(meta.title).toBe('Example Title');
39
+ expect(meta.lang).toBe('ja');
40
+ expect(meta.description).toBe('An example page');
41
+ expect(meta.keywords).toBe('a, b, c');
42
+ });
43
+
44
+ it('parses og:* and twitter:* meta tags', async () => {
45
+ const html = `<!doctype html>
46
+ <html>
47
+ <head>
48
+ <title>OG</title>
49
+ <meta property="og:title" content="OG Title">
50
+ <meta property="og:type" content="article">
51
+ <meta property="og:image" content="https://example.com/a.png">
52
+ <meta property="og:image" content="https://example.com/b.png">
53
+ <meta name="twitter:card" content="summary_large_image">
54
+ <meta name="twitter:site" content="@example">
55
+ </head>
56
+ <body></body>
57
+ </html>`;
58
+ const dom = mkDom(html);
59
+ const meta = await extractMetaFromDocument(asWindow(dom), { url: URL, html });
60
+
61
+ expect(meta.og?.title).toBe('OG Title');
62
+ expect(meta.og?.type).toBe('article');
63
+ expect(meta.og?.image).toEqual([
64
+ 'https://example.com/a.png',
65
+ 'https://example.com/b.png',
66
+ ]);
67
+ expect(meta.twitter?.card).toBe('summary_large_image');
68
+ expect(meta.twitter?.site).toBe('@example');
69
+ });
70
+
71
+ it('parses viewport, robots and theme-color (with media branches)', async () => {
72
+ const html = `<!doctype html>
73
+ <html>
74
+ <head>
75
+ <title>X</title>
76
+ <meta name="viewport" content="width=device-width, initial-scale=1">
77
+ <meta name="robots" content="noindex, nofollow">
78
+ <meta name="theme-color" content="#000000">
79
+ <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#111111">
80
+ <meta name="theme-color" media="(prefers-color-scheme: light)" content="#eeeeee">
81
+ </head>
82
+ </html>`;
83
+ const dom = mkDom(html);
84
+ const meta = await extractMetaFromDocument(asWindow(dom), { url: URL, html });
85
+
86
+ expect(meta.viewport?.width).toBe('device-width');
87
+ expect(meta.viewport?.initialScale).toBe(1);
88
+ expect(meta.robots?.noindex).toBe(true);
89
+ expect(meta.robots?.nofollow).toBe(true);
90
+ expect(meta.themeColor).toBe('#000000');
91
+ expect(meta.themeColorDark).toBe('#111111');
92
+ expect(meta.themeColorLight).toBe('#eeeeee');
93
+ });
94
+
95
+ it('captures <link rel="canonical"> and alternate hreflang', async () => {
96
+ const html = `<!doctype html>
97
+ <html>
98
+ <head>
99
+ <title>L</title>
100
+ <link rel="canonical" href="https://example.com/canonical">
101
+ <link rel="alternate" hreflang="en" href="https://example.com/en">
102
+ <link rel="alternate" hreflang="ja" href="https://example.com/ja">
103
+ </head>
104
+ </html>`;
105
+ const dom = mkDom(html);
106
+ const meta = await extractMetaFromDocument(asWindow(dom), { url: URL, html });
107
+
108
+ expect(meta.link?.canonical).toBe('https://example.com/canonical');
109
+ const hreflangs = meta.link?.alternateHreflang.map((e) => e.hreflang) ?? [];
110
+ expect(hreflangs).toEqual(['en', 'ja']);
111
+ });
112
+
113
+ it('parses inline JSON-LD scripts', async () => {
114
+ const data = { '@context': 'https://schema.org', '@type': 'WebPage', name: 'X' };
115
+ const html = `<!doctype html>
116
+ <html>
117
+ <head>
118
+ <title>J</title>
119
+ <script type="application/ld+json">${JSON.stringify(data)}</script>
120
+ </head>
121
+ </html>`;
122
+ const dom = mkDom(html);
123
+ const meta = await extractMetaFromDocument(asWindow(dom), { url: URL, html });
124
+
125
+ expect(meta.jsonLd).toHaveLength(1);
126
+ const first = meta.jsonLd[0];
127
+ expect(first?.parsed).toEqual(data);
128
+ });
129
+
130
+ it('captures itemtype/itemscope (microdata) and prefix/vocab (RDFa) from <html>', async () => {
131
+ const html = `<!doctype html>
132
+ <html itemscope itemtype="https://schema.org/WebPage" prefix="og: https://ogp.me/ns#" vocab="https://schema.org/" typeof="WebPage">
133
+ <head><title>M</title></head>
134
+ </html>`;
135
+ const dom = mkDom(html);
136
+ const meta = await extractMetaFromDocument(asWindow(dom), { url: URL, html });
137
+
138
+ expect(meta.microdata?.itemscope).toBe(true);
139
+ expect(meta.microdata?.itemtype).toBe('https://schema.org/WebPage');
140
+ expect(meta.rdfa?.prefix).toBe('og: https://ogp.me/ns#');
141
+ expect(meta.rdfa?.vocab).toBe('https://schema.org/');
142
+ expect(meta.rdfa?.typeOf).toBe('WebPage');
143
+ });
144
+
145
+ it('captures <base href> and <iframe src>', async () => {
146
+ const html = `<!doctype html>
147
+ <html>
148
+ <head>
149
+ <title>B</title>
150
+ <base href="https://example.com/sub/">
151
+ </head>
152
+ <body>
153
+ <iframe src="https://www.youtube.com/embed/abc"></iframe>
154
+ </body>
155
+ </html>`;
156
+ const dom = mkDom(html);
157
+ const meta = await extractMetaFromDocument(asWindow(dom), { url: URL, html });
158
+
159
+ expect(meta.baseHref).toBe('https://example.com/sub/');
160
+ expect(meta.others.iframe).toEqual([
161
+ { src: 'https://www.youtube.com/embed/abc', location: 'body' },
162
+ ]);
163
+ });
164
+
165
+ it('falls back to documentElement.outerHTML when context.html is omitted', async () => {
166
+ const html = `<!doctype html><html><head><title>FB</title></head></html>`;
167
+ const dom = mkDom(html);
168
+ const meta = await extractMetaFromDocument(asWindow(dom), { url: URL });
169
+ expect(meta.title).toBe('FB');
170
+ expect(meta.tags).toBeDefined();
171
+ expect(meta.tags.entries).toBeInstanceOf(Array);
172
+ });
173
+
174
+ it('returns includeRaw when requested', async () => {
175
+ const html = `<!doctype html><html><head><title>R</title></head></html>`;
176
+ const dom = mkDom(html);
177
+ const meta = await extractMetaFromDocument(asWindow(dom), {
178
+ url: URL,
179
+ html,
180
+ includeRaw: true,
181
+ });
182
+ expect(meta._raw).toBeInstanceOf(Array);
183
+ expect(meta._raw?.some((e) => e.kind === 'title')).toBe(true);
184
+ });
185
+
186
+ it("emits a 'window-global' raw entry when known globals are present on the window", async () => {
187
+ const html = `<!doctype html><html><head><title>WG</title></head></html>`;
188
+ const dom = mkDom(html);
189
+ // jsdom does not execute scripts by default, so simulate a tag library
190
+ // having installed itself onto `window` (the production trigger for the
191
+ // `window-global` branch in `collectHeadFromDocument`).
192
+ (dom.window as unknown as Record<string, unknown>).dataLayer = [];
193
+ (dom.window as unknown as Record<string, unknown>).fbq = () => {};
194
+
195
+ const meta = await extractMetaFromDocument(asWindow(dom), {
196
+ url: URL,
197
+ html,
198
+ includeRaw: true,
199
+ });
200
+
201
+ const globalEntry = meta._raw?.find((e) => e.kind === 'window-global');
202
+ expect(globalEntry).toBeDefined();
203
+ // Force a type error if the narrow ever fails, rather than letting the
204
+ // trailing `expect` calls silently skip via an `if` branch.
205
+ if (globalEntry === undefined || globalEntry.kind !== 'window-global') {
206
+ throw new Error('expected a window-global raw entry');
207
+ }
208
+ expect(globalEntry.names).toContain('dataLayer');
209
+ expect(globalEntry.names).toContain('fbq');
210
+ });
211
+
212
+ it('forwards headers and statusCode to the tag-detection layer', async () => {
213
+ // We can't assert Wappalyzer's internal decisions without coupling to its
214
+ // signature table, but we can at least verify that supplying headers and
215
+ // statusCode does not throw and that the returned Meta is still well-formed.
216
+ const html = `<!doctype html><html><head><title>H</title></head></html>`;
217
+ const dom = mkDom(html);
218
+ const meta = await extractMetaFromDocument(asWindow(dom), {
219
+ url: URL,
220
+ html,
221
+ headers: {
222
+ 'content-type': 'text/html; charset=utf-8',
223
+ 'x-powered-by': 'Express',
224
+ },
225
+ statusCode: 200,
226
+ });
227
+ expect(meta.title).toBe('H');
228
+ expect(Array.isArray(meta.tags.entries)).toBe(true);
229
+ });
230
+
231
+ it('records parseError for malformed inline JSON-LD', async () => {
232
+ const html = `<!doctype html>
233
+ <html>
234
+ <head>
235
+ <title>JE</title>
236
+ <script type="application/ld+json">{ this is not valid json</script>
237
+ </head>
238
+ </html>`;
239
+ const dom = mkDom(html);
240
+ const meta = await extractMetaFromDocument(asWindow(dom), { url: URL, html });
241
+
242
+ expect(meta.jsonLd).toHaveLength(1);
243
+ const first = meta.jsonLd[0];
244
+ expect(first?.parsed).toBeUndefined();
245
+ expect(typeof first?.parseError).toBe('string');
246
+ });
247
+ });
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Public, Puppeteer-free entry point for extracting {@link Meta} from an
3
+ * already-parsed DOM (e.g. jsdom).
4
+ *
5
+ * WHY this exists alongside `Scraper.scrapeStart()` / `getMeta(page, …)`:
6
+ * callers who already have an HTML string (from `fetch`, a fixture, an
7
+ * archive) should not be forced to spin up Chromium just to read a few `<meta>`
8
+ * tags. This module reuses the same `collectHead → detectTags → classify`
9
+ * pipeline as the Puppeteer path — the `Meta` shape returned here is
10
+ * identical to what `Scraper` produces, so downstream consumers do not branch
11
+ * on the source.
12
+ *
13
+ * See {@link extractMetaFromDocument} for the usage example.
14
+ * @module
15
+ */
16
+
17
+ import type { Meta } from './types.js';
18
+
19
+ import { classify } from './meta/classify.js';
20
+ import { collectHeadFromDocument, WINDOW_GLOBALS_TO_CHECK } from './meta/collect-head.js';
21
+ import { detectTags } from './meta/tag-detection.js';
22
+
23
+ /**
24
+ * Inputs for {@link extractMetaFromDocument}.
25
+ *
26
+ * `url`/`statusCode`/`headers` mirror the inputs to the underlying
27
+ * `simple-wappalyzer` driver. They are not consumed by the DOM-walk side of
28
+ * the pipeline.
29
+ *
30
+ * `html` is optional: when omitted, `document.documentElement.outerHTML` is
31
+ * read off the passed window — matching the fallback `getMeta(page, …)` does
32
+ * via `page.content()`.
33
+ */
34
+ export type ExtractMetaContext = {
35
+ /** The fully resolved URL of the page (used by Wappalyzer + AMP fields). */
36
+ readonly url: string;
37
+ /**
38
+ * Rendered HTML used for technology detection. Defaults to
39
+ * `window.document.documentElement.outerHTML` when omitted.
40
+ *
41
+ * WHY allow override: callers that fetched the raw HTML string from the
42
+ * network already have the *pre-script-execution* markup, which is what
43
+ * Wappalyzer's HTML patterns are tuned for. The serialized DOM from
44
+ * `outerHTML` reflects whatever scripts have already mutated; provide the
45
+ * raw string to get more stable detections.
46
+ */
47
+ readonly html?: string;
48
+ /** HTTP status code, surfaced to the Wappalyzer driver. */
49
+ readonly statusCode?: number;
50
+ /**
51
+ * Response headers; case is preserved by the caller, lowercased internally
52
+ * by `detectTags`.
53
+ */
54
+ readonly headers?: Record<string, string | string[] | undefined>;
55
+ /**
56
+ * When `true`, the returned `Meta` includes `_raw: RawHeadEntry[]` for
57
+ * debugging. Default `false` to keep the serialized payload small.
58
+ */
59
+ readonly includeRaw?: boolean;
60
+ };
61
+
62
+ /**
63
+ * Extracts a `Meta` object from a DOM provided by the caller.
64
+ *
65
+ * Pipeline:
66
+ *
67
+ * 1. {@link collectHeadFromDocument} walks `window.document` and returns a
68
+ * serializable `RawHeadEntry[]`.
69
+ * 2. {@link detectTags} runs `simple-wappalyzer` over the HTML + headers to
70
+ * detect third-party technologies.
71
+ * 3. {@link classify} folds the two signals together into a typed `Meta`.
72
+ *
73
+ * Step (1) is synchronous and runs first; step (2) is awaited next. The two
74
+ * are independent in principle, but the current shape is sequential — keeping
75
+ * it that way avoids forcing the synchronous DOM walk into a microtask just to
76
+ * gain a few milliseconds of overlap with the Wappalyzer call.
77
+ * @param window - The window whose `document` will be walked. jsdom's
78
+ * `dom.window` works; pass any object satisfying the `Window`
79
+ * type. The function never mutates the document.
80
+ * @param context - URL / HTML / headers / status code context. See
81
+ * {@link ExtractMetaContext}.
82
+ * @returns The extracted `Meta` (always defined; empty fields stay empty).
83
+ * @example
84
+ * ```ts
85
+ * import { JSDOM } from 'jsdom';
86
+ * import { extractMetaFromDocument } from '@d-zero/beholder';
87
+ *
88
+ * const url = 'https://example.com/';
89
+ * const html = await (await fetch(url)).text();
90
+ * const dom = new JSDOM(html, { url });
91
+ *
92
+ * // The `as unknown as Window` cast is needed because jsdom's `DOMWindow` is
93
+ * // not structurally identical to lib.dom's `Window` (a few rare globals
94
+ * // differ), but the runtime shape is compatible for this function's needs.
95
+ * const meta = await extractMetaFromDocument(dom.window as unknown as Window, {
96
+ * url,
97
+ * html,
98
+ * });
99
+ *
100
+ * meta.title; // <title>
101
+ * meta.og?.image; // og:image[]
102
+ * meta.tags.entries; // Wappalyzer detections + extracted IDs
103
+ * ```
104
+ */
105
+ export async function extractMetaFromDocument(
106
+ window: Window,
107
+ context: ExtractMetaContext,
108
+ ): Promise<Meta> {
109
+ const raw = collectHeadFromDocument(window, WINDOW_GLOBALS_TO_CHECK);
110
+ const html = context.html ?? window.document.documentElement.outerHTML;
111
+ const tags = await detectTags({
112
+ url: context.url,
113
+ html,
114
+ ...(context.statusCode === undefined ? {} : { statusCode: context.statusCode }),
115
+ ...(context.headers === undefined ? {} : { headers: context.headers }),
116
+ });
117
+ return classify(raw, {
118
+ tags,
119
+ ...(context.includeRaw ? { includeRaw: true } : {}),
120
+ });
121
+ }
package/src/index.ts CHANGED
@@ -12,6 +12,8 @@
12
12
  */
13
13
  export { default as default } from './scraper.js';
14
14
  export { isError } from './is-error.js';
15
+ export { extractMetaFromDocument } from './extract-meta.js';
16
+ export type { ExtractMetaContext } from './extract-meta.js';
15
17
  export { detectCompress } from '@d-zero/shared/detect-compress';
16
18
  export type { CompressType } from '@d-zero/shared/detect-compress';
17
19
  export { detectCDN } from '@d-zero/shared/detect-cdn';
@@ -25,4 +27,47 @@ export type {
25
27
  ImageElement,
26
28
  SkippedPageData,
27
29
  NetworkLog,
30
+ OpenGraphMeta,
31
+ OgArticleMeta,
32
+ OgBookMeta,
33
+ OgProfileMeta,
34
+ OgMusicMeta,
35
+ OgVideoNsMeta,
36
+ TwitterMeta,
37
+ FbMeta,
38
+ FediverseMeta,
39
+ AppleMeta,
40
+ MsApplicationMeta,
41
+ VerificationMeta,
42
+ GoogleMeta,
43
+ GeoMeta,
44
+ CitationMeta,
45
+ RdfaMeta,
46
+ MicrodataMeta,
47
+ AmpMeta,
48
+ LegacyMeta,
49
+ MobileMeta,
50
+ MicroformatsMeta,
51
+ PinterestMeta,
52
+ SlackMeta,
53
+ LinkedInMeta,
54
+ ExperimentalMeta,
55
+ WikiMeta,
56
+ LinkMeta,
57
+ LinkEntry,
58
+ JsonLdEntry,
59
+ OthersBucket,
60
+ ScriptEntry,
61
+ IframeEntry,
62
+ TagsMeta,
63
+ TagDetail,
64
+ TagEntry,
65
+ TagSource,
66
+ ViewportMeta,
67
+ RobotsMeta,
68
+ ReferrerMeta,
69
+ FormatDetectionMeta,
70
+ HttpEquivMeta,
71
+ HttpEquivRefresh,
72
+ RawHeadEntry,
28
73
  } from './types.js';