@blankgrimoire/runtime 1.0.0 → 1.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.
- package/dist/engine.d.ts +12 -1
- package/dist/engine.js +44 -4
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/prelude.js +1 -1
- package/dist/runtime.js +31 -4
- package/package.json +6 -4
package/dist/engine.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type Chapter, type ContentRating, type MangaDetails, type MangaStatus, type MangaSummary, type Page, type PagedResult, type SearchQuery, type SiteContext, type ThemeAdapter, type ThemeSelectors, type ThemeSelectorsOverrides } from '@blankgrimoire/types';
|
|
2
2
|
import type { DocumentLike, ParseHTML, WindowLike } from './dom.js';
|
|
3
3
|
export interface ThemeRuntimeOptions {
|
|
4
4
|
adapter: ThemeAdapter;
|
|
@@ -12,6 +12,17 @@ export interface PageKeys {
|
|
|
12
12
|
mangaKey: string;
|
|
13
13
|
chapterKey: string;
|
|
14
14
|
}
|
|
15
|
+
/** Raised when an adapter bundle targets a different contract version. */
|
|
16
|
+
export declare class UnsupportedApiVersionError extends Error {
|
|
17
|
+
readonly expected: number;
|
|
18
|
+
readonly found: unknown;
|
|
19
|
+
constructor(found: unknown);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Reject adapters that do not declare the contract version implemented by
|
|
23
|
+
* this runtime. Adapters created with `defineTheme` always declare it.
|
|
24
|
+
*/
|
|
25
|
+
export declare function assertAdapterAPIVersion(adapter: ThemeAdapter): void;
|
|
15
26
|
/** Merge field-by-field user overrides into the adapter selectors. */
|
|
16
27
|
export declare function resolveSelectors(base: ThemeSelectors, overrides: ThemeSelectorsOverrides): ThemeSelectors;
|
|
17
28
|
/** Normalise site text into the closed `MangaStatus` union. */
|
package/dist/engine.js
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
|
+
import { API_VERSION, } from '@blankgrimoire/types';
|
|
2
3
|
import { attr, firstAttribute, queryAll, resolveURL, textOf } from './dom.js';
|
|
3
4
|
import { runStrategy } from './strategies.js';
|
|
5
|
+
/** Raised when an adapter bundle targets a different contract version. */
|
|
6
|
+
export class UnsupportedApiVersionError extends Error {
|
|
7
|
+
constructor(found) {
|
|
8
|
+
super(`Unsupported adapter apiVersion ${String(found)}; expected ${API_VERSION}`);
|
|
9
|
+
this.expected = API_VERSION;
|
|
10
|
+
this.name = 'UnsupportedApiVersionError';
|
|
11
|
+
this.found = found;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Reject adapters that do not declare the contract version implemented by
|
|
16
|
+
* this runtime. Adapters created with `defineTheme` always declare it.
|
|
17
|
+
*/
|
|
18
|
+
export function assertAdapterAPIVersion(adapter) {
|
|
19
|
+
if (adapter.apiVersion !== API_VERSION) {
|
|
20
|
+
throw new UnsupportedApiVersionError(adapter.apiVersion);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
4
23
|
/** Merge field-by-field user overrides into the adapter selectors. */
|
|
5
24
|
export function resolveSelectors(base, overrides) {
|
|
6
25
|
const resolved = {
|
|
@@ -39,6 +58,19 @@ function readImageURL(root, selector, baseURL) {
|
|
|
39
58
|
const value = firstAttribute(image, ['data-src', 'src', 'data-original']);
|
|
40
59
|
return value ? resolveURL(value, baseURL) : undefined;
|
|
41
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Finds the link element for a list item.
|
|
63
|
+
*
|
|
64
|
+
* Most adapters point at a child anchor. Some sites expose the anchor as the
|
|
65
|
+
* item itself, so the item is used when it already carries an `href`.
|
|
66
|
+
*/
|
|
67
|
+
function selectLink(element, selector) {
|
|
68
|
+
const descendant = selector ? element.querySelector(selector) : null;
|
|
69
|
+
if (descendant) {
|
|
70
|
+
return descendant;
|
|
71
|
+
}
|
|
72
|
+
return attr(element, 'href') ? element : null;
|
|
73
|
+
}
|
|
42
74
|
function splitList(value) {
|
|
43
75
|
if (!value) {
|
|
44
76
|
return undefined;
|
|
@@ -99,6 +131,7 @@ function parseChapterNumber(value) {
|
|
|
99
131
|
}
|
|
100
132
|
/** Search a site. Hooks take precedence over declarative selectors. */
|
|
101
133
|
export async function extractSearch(runtime, query) {
|
|
134
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
102
135
|
const hook = runtime.adapter.hooks?.search;
|
|
103
136
|
if (hook) {
|
|
104
137
|
return hook(runtime.context, query);
|
|
@@ -106,9 +139,10 @@ export async function extractSearch(runtime, query) {
|
|
|
106
139
|
const selectors = selectorsFor(runtime).search;
|
|
107
140
|
const items = [];
|
|
108
141
|
for (const element of queryAll(runtime.document, selectors.item)) {
|
|
109
|
-
const
|
|
110
|
-
const link = element.querySelector(selectors.link);
|
|
142
|
+
const link = selectLink(element, selectors.link);
|
|
111
143
|
const href = link ? attr(link, 'href') : null;
|
|
144
|
+
// When the item is the anchor, its text is the only available title.
|
|
145
|
+
const title = textOf(element.querySelector(selectors.title)) || (link ? textOf(link) : '');
|
|
112
146
|
if (title === '' || !href) {
|
|
113
147
|
continue;
|
|
114
148
|
}
|
|
@@ -129,6 +163,7 @@ export async function extractSearch(runtime, query) {
|
|
|
129
163
|
}
|
|
130
164
|
/** Read series details. Hooks take precedence over declarative selectors. */
|
|
131
165
|
export async function extractDetails(runtime, key) {
|
|
166
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
132
167
|
const hook = runtime.adapter.hooks?.getMangaDetails;
|
|
133
168
|
if (hook) {
|
|
134
169
|
return hook(runtime.context, key);
|
|
@@ -191,6 +226,7 @@ export async function extractDetails(runtime, key) {
|
|
|
191
226
|
}
|
|
192
227
|
/** Read a chapter list. Hooks take precedence over declarative selectors. */
|
|
193
228
|
export async function extractChapters(runtime, key) {
|
|
229
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
194
230
|
const hook = runtime.adapter.hooks?.getChapters;
|
|
195
231
|
if (hook) {
|
|
196
232
|
return hook(runtime.context, key);
|
|
@@ -199,13 +235,15 @@ export async function extractChapters(runtime, key) {
|
|
|
199
235
|
const baseURL = runtime.context.baseURL;
|
|
200
236
|
const chapters = [];
|
|
201
237
|
for (const element of queryAll(runtime.document, selectors.item)) {
|
|
202
|
-
const link = element
|
|
238
|
+
const link = selectLink(element, selectors.link);
|
|
203
239
|
const href = link ? attr(link, 'href') : null;
|
|
204
240
|
if (!href) {
|
|
205
241
|
continue;
|
|
206
242
|
}
|
|
243
|
+
// An item can be the anchor itself, so a missing title selector falls back
|
|
244
|
+
// to the link text.
|
|
207
245
|
const title = selectors.title
|
|
208
|
-
? readText(element, selectors.title)
|
|
246
|
+
? (readText(element, selectors.title) ?? textOf(link)) || undefined
|
|
209
247
|
: textOf(link) || undefined;
|
|
210
248
|
const chapter = { key: resolveURL(href, baseURL) };
|
|
211
249
|
if (title) {
|
|
@@ -239,6 +277,7 @@ export async function extractChapters(runtime, key) {
|
|
|
239
277
|
}
|
|
240
278
|
/** Extract chapter pages. Hooks take precedence over declarative strategies. */
|
|
241
279
|
export async function extractPages(runtime, keys) {
|
|
280
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
242
281
|
const hook = runtime.adapter.hooks?.getPages;
|
|
243
282
|
if (hook) {
|
|
244
283
|
return hook(runtime.context, keys.mangaKey, keys.chapterKey);
|
|
@@ -274,6 +313,7 @@ function toJSON(value) {
|
|
|
274
313
|
* Method aliases match the `ThemeHooks` names.
|
|
275
314
|
*/
|
|
276
315
|
export async function dispatch(runtime, method, argsJSON) {
|
|
316
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
277
317
|
const args = asRecord(argsJSON.trim() === '' ? {} : JSON.parse(argsJSON));
|
|
278
318
|
switch (method) {
|
|
279
319
|
case 'search': {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __apiVersion, __nativeInvoke, __http, __parseHTML, setTimeout, clearTimeout, __timerFire, console, type HttpRequest, type HttpResponse } from './prelude.js';
|
|
2
|
-
import { dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, type ExtractionResult, type PageKeys, type ThemeRuntimeOptions } from './engine.js';
|
|
2
|
+
import { assertAdapterAPIVersion, dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, UnsupportedApiVersionError, type ExtractionResult, type PageKeys, type ThemeRuntimeOptions } from './engine.js';
|
|
3
3
|
import { extractCss, type CssStrategy } from './css.js';
|
|
4
4
|
import { extractScriptJSON, extractHydration, extractAjax, parseJSONText, readPath, runStrategy, toPages, type AjaxStrategy, type HydrationStrategy, type ScriptJSONStrategy, type StrategyEnvironment } from './strategies.js';
|
|
5
5
|
import type { DocumentLike, ElementLike, ParseHTML, WindowLike } from './dom.js';
|
|
6
|
-
export { __apiVersion, __nativeInvoke, __http, __parseHTML, setTimeout, clearTimeout, __timerFire, console, dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, extractCss, extractScriptJSON, extractHydration, extractAjax, parseJSONText, readPath, runStrategy, toPages, };
|
|
6
|
+
export { __apiVersion, __nativeInvoke, __http, __parseHTML, setTimeout, clearTimeout, __timerFire, console, assertAdapterAPIVersion, dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, UnsupportedApiVersionError, extractCss, extractScriptJSON, extractHydration, extractAjax, parseJSONText, readPath, runStrategy, toPages, };
|
|
7
7
|
export type { HttpRequest, HttpResponse, ExtractionResult, PageKeys, ThemeRuntimeOptions, CssStrategy, ScriptJSONStrategy, HydrationStrategy, AjaxStrategy, StrategyEnvironment, DocumentLike, ElementLike, ParseHTML, WindowLike, };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
import { __apiVersion, __nativeInvoke, __http, __parseHTML, setTimeout, clearTimeout, __timerFire, console, } from './prelude.js';
|
|
3
|
-
import { dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, } from './engine.js';
|
|
3
|
+
import { assertAdapterAPIVersion, dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, UnsupportedApiVersionError, } from './engine.js';
|
|
4
4
|
import { extractCss } from './css.js';
|
|
5
5
|
import { extractScriptJSON, extractHydration, extractAjax, parseJSONText, readPath, runStrategy, toPages, } from './strategies.js';
|
|
6
|
-
export { __apiVersion, __nativeInvoke, __http, __parseHTML, setTimeout, clearTimeout, __timerFire, console, dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, extractCss, extractScriptJSON, extractHydration, extractAjax, parseJSONText, readPath, runStrategy, toPages, };
|
|
6
|
+
export { __apiVersion, __nativeInvoke, __http, __parseHTML, setTimeout, clearTimeout, __timerFire, console, assertAdapterAPIVersion, dispatch, extract, extractChapters, extractDetails, extractPages, extractSearch, normalizeContentRating, normalizeStatus, resolveSelectors, UnsupportedApiVersionError, extractCss, extractScriptJSON, extractHydration, extractAjax, parseJSONText, readPath, runStrategy, toPages, };
|
package/dist/prelude.js
CHANGED
package/dist/runtime.js
CHANGED
|
@@ -21,11 +21,13 @@ var BlankGrimoireRuntime = (() => {
|
|
|
21
21
|
// src/index.ts
|
|
22
22
|
var index_exports = {};
|
|
23
23
|
__export(index_exports, {
|
|
24
|
+
UnsupportedApiVersionError: () => UnsupportedApiVersionError,
|
|
24
25
|
__apiVersion: () => __apiVersion,
|
|
25
26
|
__http: () => __http,
|
|
26
27
|
__nativeInvoke: () => __nativeInvoke,
|
|
27
28
|
__parseHTML: () => __parseHTML,
|
|
28
29
|
__timerFire: () => __timerFire,
|
|
30
|
+
assertAdapterAPIVersion: () => assertAdapterAPIVersion,
|
|
29
31
|
clearTimeout: () => clearTimeout,
|
|
30
32
|
console: () => console,
|
|
31
33
|
dispatch: () => dispatch,
|
|
@@ -498,6 +500,19 @@ var BlankGrimoireRuntime = (() => {
|
|
|
498
500
|
}
|
|
499
501
|
|
|
500
502
|
// src/engine.ts
|
|
503
|
+
var UnsupportedApiVersionError = class extends Error {
|
|
504
|
+
constructor(found) {
|
|
505
|
+
super(`Unsupported adapter apiVersion ${String(found)}; expected ${API_VERSION}`);
|
|
506
|
+
this.expected = API_VERSION;
|
|
507
|
+
this.name = "UnsupportedApiVersionError";
|
|
508
|
+
this.found = found;
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
function assertAdapterAPIVersion(adapter) {
|
|
512
|
+
if (adapter.apiVersion !== API_VERSION) {
|
|
513
|
+
throw new UnsupportedApiVersionError(adapter.apiVersion);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
501
516
|
function resolveSelectors(base, overrides) {
|
|
502
517
|
const resolved = {
|
|
503
518
|
search: { ...base.search, ...overrides.search },
|
|
@@ -533,6 +548,13 @@ var BlankGrimoireRuntime = (() => {
|
|
|
533
548
|
const value = firstAttribute(image, ["data-src", "src", "data-original"]);
|
|
534
549
|
return value ? resolveURL(value, baseURL) : void 0;
|
|
535
550
|
}
|
|
551
|
+
function selectLink(element, selector) {
|
|
552
|
+
const descendant = selector ? element.querySelector(selector) : null;
|
|
553
|
+
if (descendant) {
|
|
554
|
+
return descendant;
|
|
555
|
+
}
|
|
556
|
+
return attr(element, "href") ? element : null;
|
|
557
|
+
}
|
|
536
558
|
function splitList(value) {
|
|
537
559
|
if (!value) {
|
|
538
560
|
return void 0;
|
|
@@ -587,6 +609,7 @@ var BlankGrimoireRuntime = (() => {
|
|
|
587
609
|
return Number.isFinite(number) ? number : void 0;
|
|
588
610
|
}
|
|
589
611
|
async function extractSearch(runtime, query) {
|
|
612
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
590
613
|
const hook = runtime.adapter.hooks?.search;
|
|
591
614
|
if (hook) {
|
|
592
615
|
return hook(runtime.context, query);
|
|
@@ -594,9 +617,9 @@ var BlankGrimoireRuntime = (() => {
|
|
|
594
617
|
const selectors = selectorsFor(runtime).search;
|
|
595
618
|
const items = [];
|
|
596
619
|
for (const element of queryAll(runtime.document, selectors.item)) {
|
|
597
|
-
const
|
|
598
|
-
const link = element.querySelector(selectors.link);
|
|
620
|
+
const link = selectLink(element, selectors.link);
|
|
599
621
|
const href = link ? attr(link, "href") : null;
|
|
622
|
+
const title = textOf(element.querySelector(selectors.title)) || (link ? textOf(link) : "");
|
|
600
623
|
if (title === "" || !href) {
|
|
601
624
|
continue;
|
|
602
625
|
}
|
|
@@ -614,6 +637,7 @@ var BlankGrimoireRuntime = (() => {
|
|
|
614
637
|
return { items, hasNextPage };
|
|
615
638
|
}
|
|
616
639
|
async function extractDetails(runtime, key) {
|
|
640
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
617
641
|
const hook = runtime.adapter.hooks?.getMangaDetails;
|
|
618
642
|
if (hook) {
|
|
619
643
|
return hook(runtime.context, key);
|
|
@@ -677,6 +701,7 @@ var BlankGrimoireRuntime = (() => {
|
|
|
677
701
|
return details;
|
|
678
702
|
}
|
|
679
703
|
async function extractChapters(runtime, key) {
|
|
704
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
680
705
|
const hook = runtime.adapter.hooks?.getChapters;
|
|
681
706
|
if (hook) {
|
|
682
707
|
return hook(runtime.context, key);
|
|
@@ -685,12 +710,12 @@ var BlankGrimoireRuntime = (() => {
|
|
|
685
710
|
const baseURL = runtime.context.baseURL;
|
|
686
711
|
const chapters = [];
|
|
687
712
|
for (const element of queryAll(runtime.document, selectors.item)) {
|
|
688
|
-
const link = element
|
|
713
|
+
const link = selectLink(element, selectors.link);
|
|
689
714
|
const href = link ? attr(link, "href") : null;
|
|
690
715
|
if (!href) {
|
|
691
716
|
continue;
|
|
692
717
|
}
|
|
693
|
-
const title = selectors.title ? readText(element, selectors.title) : textOf(link) || void 0;
|
|
718
|
+
const title = selectors.title ? (readText(element, selectors.title) ?? textOf(link)) || void 0 : textOf(link) || void 0;
|
|
694
719
|
const chapter = { key: resolveURL(href, baseURL) };
|
|
695
720
|
if (title) {
|
|
696
721
|
chapter.title = title;
|
|
@@ -721,6 +746,7 @@ var BlankGrimoireRuntime = (() => {
|
|
|
721
746
|
return chapters;
|
|
722
747
|
}
|
|
723
748
|
async function extractPages(runtime, keys) {
|
|
749
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
724
750
|
const hook = runtime.adapter.hooks?.getPages;
|
|
725
751
|
if (hook) {
|
|
726
752
|
return hook(runtime.context, keys.mangaKey, keys.chapterKey);
|
|
@@ -749,6 +775,7 @@ var BlankGrimoireRuntime = (() => {
|
|
|
749
775
|
return JSON.stringify(value) ?? "null";
|
|
750
776
|
}
|
|
751
777
|
async function dispatch(runtime, method, argsJSON) {
|
|
778
|
+
assertAdapterAPIVersion(runtime.adapter);
|
|
752
779
|
const args = asRecord(argsJSON.trim() === "" ? {} : JSON.parse(argsJSON));
|
|
753
780
|
switch (method) {
|
|
754
781
|
case "search": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blankgrimoire/runtime",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Declarative extraction runtime for BlankGrimoire theme adapters.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
19
|
"publishConfig": {
|
|
20
|
-
"access": "public"
|
|
20
|
+
"access": "public",
|
|
21
|
+
"provenance": true
|
|
21
22
|
},
|
|
22
23
|
"repository": {
|
|
23
24
|
"type": "git",
|
|
@@ -25,15 +26,16 @@
|
|
|
25
26
|
"directory": "packages/runtime"
|
|
26
27
|
},
|
|
27
28
|
"dependencies": {
|
|
28
|
-
"@blankgrimoire/types": "1.
|
|
29
|
+
"@blankgrimoire/types": "1.1.0"
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"esbuild": "^0.24.0",
|
|
32
33
|
"jsdom": "^24.0.0"
|
|
33
34
|
},
|
|
34
35
|
"scripts": {
|
|
35
|
-
"build": "tsc -p tsconfig.json &&
|
|
36
|
+
"build": "tsc -p tsconfig.json && node build.mjs",
|
|
36
37
|
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json",
|
|
38
|
+
"pretest": "pnpm --filter @blankgrimoire/types... build && pnpm build",
|
|
37
39
|
"test": "vitest run --passWithNoTests"
|
|
38
40
|
}
|
|
39
41
|
}
|