@dvaji/cheerox 0.0.0-stage → 0.1.1

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 (253) hide show
  1. package/CONTRIBUTING.md +74 -0
  2. package/Cargo.lock +1094 -0
  3. package/Cargo.toml +46 -0
  4. package/LICENSE +21 -0
  5. package/README.md +121 -2
  6. package/RELEASING.md +114 -0
  7. package/THIRD_PARTY_NOTICES.md +7579 -0
  8. package/build.rs +3 -0
  9. package/cheerox.darwin-arm64.node +0 -0
  10. package/cheerox.darwin-x64.node +0 -0
  11. package/cheerox.linux-arm64-gnu.node +0 -0
  12. package/cheerox.linux-arm64-musl.node +0 -0
  13. package/cheerox.linux-x64-gnu.node +0 -0
  14. package/cheerox.linux-x64-musl.node +0 -0
  15. package/cheerox.win32-arm64-msvc.node +0 -0
  16. package/cheerox.win32-x64-msvc.node +0 -0
  17. package/dist/cheerio-compat-BS--qPzw.mjs +1648 -0
  18. package/dist/cheerio-compat-BS--qPzw.mjs.map +1 -0
  19. package/dist/cheerio-compat-DRLPzBdX.d.mts +88 -0
  20. package/dist/cheerio-compat-DRsINTwV.d.cts +88 -0
  21. package/dist/cheerio-compat-DRsINTwV.d.ts +88 -0
  22. package/dist/cheerio-compat-DSwBAJ1s.cjs +1719 -0
  23. package/dist/cheerio-compat-DSwBAJ1s.cjs.map +1 -0
  24. package/dist/cheerio-compat.cjs +18 -0
  25. package/dist/cheerio-compat.d.cts +2 -0
  26. package/dist/cheerio-compat.d.mts +2 -0
  27. package/dist/cheerio-compat.d.ts +2 -0
  28. package/dist/cheerio-compat.mjs +1 -0
  29. package/dist/index.cjs +13 -0
  30. package/dist/index.cjs.map +1 -0
  31. package/dist/index.d.cts +28 -0
  32. package/dist/index.d.mts +6 -0
  33. package/dist/index.d.ts +28 -0
  34. package/dist/index.mjs +53 -0
  35. package/dist/index.mjs.map +1 -0
  36. package/dist/legacy-parse.cjs +5 -0
  37. package/dist/legacy-parse.d.mts +8 -0
  38. package/dist/legacy-parse.d.ts +7 -0
  39. package/dist/legacy-parse.mjs +1 -0
  40. package/dist/modern-B7hyW4cI.d.cts +102 -0
  41. package/dist/modern-B7hyW4cI.d.ts +102 -0
  42. package/dist/modern-CHQGP0Yt.cjs +2858 -0
  43. package/dist/modern-CHQGP0Yt.cjs.map +1 -0
  44. package/dist/modern-CJvSD_y5.d.mts +102 -0
  45. package/dist/modern-CvXcTn82.mjs +2769 -0
  46. package/dist/modern-CvXcTn82.mjs.map +1 -0
  47. package/dist/modern.cjs +19 -0
  48. package/dist/modern.d.cts +3 -0
  49. package/dist/modern.d.mts +3 -0
  50. package/dist/modern.d.ts +3 -0
  51. package/dist/modern.mjs +3 -0
  52. package/dist/nodes-jbWWhssj.cjs +4210 -0
  53. package/dist/nodes-jbWWhssj.cjs.map +1 -0
  54. package/dist/nodes-sYxb5qyZ.mjs +3803 -0
  55. package/dist/nodes-sYxb5qyZ.mjs.map +1 -0
  56. package/dist/slim.cjs +16 -0
  57. package/dist/slim.cjs.map +1 -0
  58. package/dist/slim.d.cts +6 -0
  59. package/dist/slim.d.mts +6 -0
  60. package/dist/slim.d.ts +6 -0
  61. package/dist/slim.mjs +12 -0
  62. package/dist/slim.mjs.map +1 -0
  63. package/dist/types-CA01Zg3r.d.cts +526 -0
  64. package/dist/types-CA01Zg3r.d.mts +526 -0
  65. package/dist/types-CA01Zg3r.d.ts +526 -0
  66. package/dist/utils.cjs +31 -0
  67. package/dist/utils.cjs.map +1 -0
  68. package/dist/utils.d.cts +8 -0
  69. package/dist/utils.d.mts +8 -0
  70. package/dist/utils.d.ts +8 -0
  71. package/dist/utils.mjs +25 -0
  72. package/dist/utils.mjs.map +1 -0
  73. package/native/src/arena.rs +2109 -0
  74. package/native/src/attributes.rs +946 -0
  75. package/native/src/codec.rs +298 -0
  76. package/native/src/diagnostics.rs +1249 -0
  77. package/native/src/document_order.rs +711 -0
  78. package/native/src/extraction.rs +321 -0
  79. package/native/src/gc.rs +175 -0
  80. package/native/src/hook_sink.rs +801 -0
  81. package/native/src/hook_stream.rs +513 -0
  82. package/native/src/id_hash.rs +42 -0
  83. package/native/src/immutable.rs +333 -0
  84. package/native/src/lib.rs +4119 -0
  85. package/native/src/location.rs +392 -0
  86. package/native/src/markup_tokenizer.rs +668 -0
  87. package/native/src/runtime.rs +368 -0
  88. package/native/src/selector.rs +1596 -0
  89. package/native/src/sink.rs +274 -0
  90. package/native/src/snapshot.rs +672 -0
  91. package/native/src/stream.rs +334 -0
  92. package/native/src/tokenizer.rs +581 -0
  93. package/native/src/tracker.rs +232 -0
  94. package/native/vendor/README.md +26 -0
  95. package/native/vendor/html5ever-0.35.0/.cargo_vcs_info.json +6 -0
  96. package/native/vendor/html5ever-0.35.0/Cargo.lock +828 -0
  97. package/native/vendor/html5ever-0.35.0/Cargo.toml +85 -0
  98. package/native/vendor/html5ever-0.35.0/LICENSE-APACHE +201 -0
  99. package/native/vendor/html5ever-0.35.0/LICENSE-MIT +25 -0
  100. package/native/vendor/html5ever-0.35.0/README.md +60 -0
  101. package/native/vendor/html5ever-0.35.0/benches/html5ever.rs +86 -0
  102. package/native/vendor/html5ever-0.35.0/data/bench/lipsum-zh.html +19 -0
  103. package/native/vendor/html5ever-0.35.0/data/bench/lipsum.html +40 -0
  104. package/native/vendor/html5ever-0.35.0/data/bench/medium-fragment.html +24 -0
  105. package/native/vendor/html5ever-0.35.0/data/bench/small-fragment.html +7 -0
  106. package/native/vendor/html5ever-0.35.0/data/bench/strong.html +1 -0
  107. package/native/vendor/html5ever-0.35.0/data/bench/tiny-fragment.html +1 -0
  108. package/native/vendor/html5ever-0.35.0/examples/arena.rs +356 -0
  109. package/native/vendor/html5ever-0.35.0/examples/capi/tokenize.c +74 -0
  110. package/native/vendor/html5ever-0.35.0/examples/noop-tokenize.rs +47 -0
  111. package/native/vendor/html5ever-0.35.0/examples/noop-tree-builder.rs +132 -0
  112. package/native/vendor/html5ever-0.35.0/examples/print-tree-actions.rs +185 -0
  113. package/native/vendor/html5ever-0.35.0/examples/tokenize.rs +111 -0
  114. package/native/vendor/html5ever-0.35.0/src/driver.rs +148 -0
  115. package/native/vendor/html5ever-0.35.0/src/lib.rs +30 -0
  116. package/native/vendor/html5ever-0.35.0/src/macros.rs +34 -0
  117. package/native/vendor/html5ever-0.35.0/src/serialize/mod.rs +255 -0
  118. package/native/vendor/html5ever-0.35.0/src/tokenizer/char_ref/mod.rs +530 -0
  119. package/native/vendor/html5ever-0.35.0/src/tokenizer/interface.rs +99 -0
  120. package/native/vendor/html5ever-0.35.0/src/tokenizer/mod.rs +2628 -0
  121. package/native/vendor/html5ever-0.35.0/src/tokenizer/states.rs +97 -0
  122. package/native/vendor/html5ever-0.35.0/src/tree_builder/data.rs +170 -0
  123. package/native/vendor/html5ever-0.35.0/src/tree_builder/mod.rs +1848 -0
  124. package/native/vendor/html5ever-0.35.0/src/tree_builder/rules.rs +1494 -0
  125. package/native/vendor/html5ever-0.35.0/src/tree_builder/tag_sets.rs +114 -0
  126. package/native/vendor/html5ever-0.35.0/src/tree_builder/types.rs +91 -0
  127. package/native/vendor/html5ever-0.35.0/src/util/str.rs +52 -0
  128. package/native/vendor/html5gum-0.8.4/Cargo.lock +1006 -0
  129. package/native/vendor/html5gum-0.8.4/Cargo.toml +109 -0
  130. package/native/vendor/html5gum-0.8.4/LICENSE +19 -0
  131. package/native/vendor/html5gum-0.8.4/README.md +110 -0
  132. package/native/vendor/html5gum-0.8.4/benches/files/0050.html +2131 -0
  133. package/native/vendor/html5gum-0.8.4/benches/files/6D30.html +1004 -0
  134. package/native/vendor/html5gum-0.8.4/benches/files.rs +32 -0
  135. package/native/vendor/html5gum-0.8.4/benches/patterns.rs +31 -0
  136. package/native/vendor/html5gum-0.8.4/src/arrayvec.rs +29 -0
  137. package/native/vendor/html5gum-0.8.4/src/char_validator.rs +134 -0
  138. package/native/vendor/html5gum-0.8.4/src/emitters/callback.rs +676 -0
  139. package/native/vendor/html5gum-0.8.4/src/emitters/default.rs +207 -0
  140. package/native/vendor/html5gum-0.8.4/src/emitters/emitter.rs +344 -0
  141. package/native/vendor/html5gum-0.8.4/src/emitters/html5ever.rs +306 -0
  142. package/native/vendor/html5gum-0.8.4/src/emitters/mod.rs +25 -0
  143. package/native/vendor/html5gum-0.8.4/src/entities.rs +3342 -0
  144. package/native/vendor/html5gum-0.8.4/src/error.rs +96 -0
  145. package/native/vendor/html5gum-0.8.4/src/htmlstring.rs +130 -0
  146. package/native/vendor/html5gum-0.8.4/src/lib.rs +66 -0
  147. package/native/vendor/html5gum-0.8.4/src/machine.rs +2136 -0
  148. package/native/vendor/html5gum-0.8.4/src/machine_helper.rs +277 -0
  149. package/native/vendor/html5gum-0.8.4/src/read_helper.rs +244 -0
  150. package/native/vendor/html5gum-0.8.4/src/reader.rs +440 -0
  151. package/native/vendor/html5gum-0.8.4/src/span.rs +98 -0
  152. package/native/vendor/html5gum-0.8.4/src/state.rs +16 -0
  153. package/native/vendor/html5gum-0.8.4/src/testutils.rs +71 -0
  154. package/native/vendor/html5gum-0.8.4/src/tokenizer.rs +189 -0
  155. package/native/vendor/html5gum-0.8.4/src/utils.rs +88 -0
  156. package/native/vendor/markup5ever-0.35.0/.cargo_vcs_info.json +6 -0
  157. package/native/vendor/markup5ever-0.35.0/Cargo.toml +45 -0
  158. package/native/vendor/markup5ever-0.35.0/LICENSE-APACHE +201 -0
  159. package/native/vendor/markup5ever-0.35.0/LICENSE-MIT +25 -0
  160. package/native/vendor/markup5ever-0.35.0/interface/mod.rs +422 -0
  161. package/native/vendor/markup5ever-0.35.0/interface/tree_builder.rs +280 -0
  162. package/native/vendor/markup5ever-0.35.0/lib.rs +59 -0
  163. package/native/vendor/markup5ever-0.35.0/serialize.rs +77 -0
  164. package/native/vendor/markup5ever-0.35.0/util/buffer_queue.rs +333 -0
  165. package/native/vendor/markup5ever-0.35.0/util/smallcharset.rs +88 -0
  166. package/package.json +167 -4
  167. package/scripts/check-native.mjs +15 -0
  168. package/scripts/check-release.mjs +234 -0
  169. package/scripts/fix-declarations.mjs +39 -0
  170. package/scripts/generate-notices.mjs +188 -0
  171. package/scripts/notices/README.md +13 -0
  172. package/scripts/notices/cheerio-MIT.txt +21 -0
  173. package/scripts/notices/html5ever-AUTHORS.txt +20 -0
  174. package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
  175. package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
  176. package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
  177. package/scripts/notices/htmlparser2-MIT.txt +18 -0
  178. package/scripts/notices/napi-rs-MIT.txt +43 -0
  179. package/scripts/notices/neon-MIT.txt +19 -0
  180. package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
  181. package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
  182. package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
  183. package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
  184. package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
  185. package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
  186. package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
  187. package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
  188. package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
  189. package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
  190. package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
  191. package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
  192. package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
  193. package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
  194. package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
  195. package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
  196. package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
  197. package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
  198. package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
  199. package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
  200. package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
  201. package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
  202. package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
  203. package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
  204. package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
  205. package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
  206. package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
  207. package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
  208. package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
  209. package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
  210. package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
  211. package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
  212. package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
  213. package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
  214. package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
  215. package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
  216. package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
  217. package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
  218. package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
  219. package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
  220. package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
  221. package/scripts/notices/runtime.json +1632 -0
  222. package/scripts/notices/sources.json +561 -0
  223. package/scripts/notices/xray-MIT.txt +21 -0
  224. package/src/attribute-object.ts +71 -0
  225. package/src/attributes.ts +308 -0
  226. package/src/cheerio-compat/css.ts +58 -0
  227. package/src/cheerio-compat/render.ts +106 -0
  228. package/src/cheerio-compat/selectors.ts +51 -0
  229. package/src/cheerio-compat/types.ts +52 -0
  230. package/src/cheerio-compat.ts +2055 -0
  231. package/src/codec.ts +96 -0
  232. package/src/dom.ts +87 -0
  233. package/src/extract-plan.ts +97 -0
  234. package/src/extract.ts +322 -0
  235. package/src/hooks.ts +369 -0
  236. package/src/index.ts +48 -0
  237. package/src/io.ts +491 -0
  238. package/src/match.ts +207 -0
  239. package/src/modern.ts +259 -0
  240. package/src/native.ts +149 -0
  241. package/src/nodes.ts +831 -0
  242. package/src/parse.ts +53 -0
  243. package/src/parser-types.ts +157 -0
  244. package/src/router.ts +1129 -0
  245. package/src/selection.ts +682 -0
  246. package/src/slim.ts +12 -0
  247. package/src/snapshot.ts +291 -0
  248. package/src/transport.ts +113 -0
  249. package/src/types.ts +186 -0
  250. package/src/utils.ts +29 -0
  251. package/src/workers.ts +21 -0
  252. package/tsconfig.json +15 -0
  253. package/tsdown.config.mjs +17 -0
package/src/slim.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { load as loadNative, contains, merge } from './modern.js';
2
+ import type { CheerioAPI, CheerioOptions, LoadContent } from './types.js';
3
+
4
+ export { contains, merge };
5
+ export { dom } from './modern.js';
6
+ export type * from './types.js';
7
+ export type { Cheerio } from './selection.js';
8
+
9
+ /** Parser-compatible slim entry point, backed by the native fragment parser. */
10
+ export function load(content: LoadContent, options?: CheerioOptions | null, _isDocument?: boolean): CheerioAPI {
11
+ return loadNative(content, { ...options, _useHtmlParser2: true }, false);
12
+ }
@@ -0,0 +1,291 @@
1
+ import { decodeString } from './codec.js';
2
+ import type { NativeNodeRecord } from './native.js';
3
+ import { defineAttributeKey, type AttributeObject } from './attribute-object.js';
4
+
5
+ /**
6
+ * The compact record copy returned by `NativeDocument.snapshot()`: the
7
+ * lengths of the node, child, attribute and string lists, whether any string
8
+ * holds codec escapes, then the lists.
9
+ */
10
+ export interface NativeSnapshot {
11
+ data: Uint32Array;
12
+ text: string;
13
+ }
14
+ const HEADER = 5;
15
+
16
+ const STRIDE = 15;
17
+ const ABSENT = 0xffffffff;
18
+ const HAS_START_INDEX = 1;
19
+ const HAS_END_INDEX = 2;
20
+ const HAS_NAMESPACE = 4;
21
+ const EMPTY_ATTRIBUTE_MAPS = 8;
22
+ const OPAQUE = 16;
23
+ const RELEASED = 32;
24
+ const noChildren: number[] = Object.freeze([]) as unknown as number[];
25
+ const noAttributes: Record<string, string> = Object.freeze({}) as Record<string, string>;
26
+
27
+ /**
28
+ * A record read from a snapshot. It stores only its position; fields decode
29
+ * on read, and strings and lists are kept once decoded. Every instance has
30
+ * the same fields, so reads stay monomorphic. Absent fields read `undefined`.
31
+ */
32
+ class SnapshotRecord {
33
+ private decodedData: string | undefined;
34
+ private decodedAttributes: Record<string, string> | undefined;
35
+ private decodedChildren: number[] | undefined;
36
+
37
+ constructor(private readonly snapshot: NodeSnapshot, readonly id: number, private readonly base: number) {
38
+ this.decodedData = undefined;
39
+ this.decodedAttributes = undefined;
40
+ this.decodedChildren = undefined;
41
+ }
42
+
43
+ get type(): string { return this.snapshot.name(this.snapshot.nodes[this.base]); }
44
+ get name(): string | undefined { const name = this.snapshot.nodes[this.base + 6]; return name ? this.snapshot.name(name - 1) : undefined; }
45
+ get data(): string | undefined {
46
+ if (this.decodedData !== undefined) return this.decodedData;
47
+ const data = this.snapshot.nodes[this.base + 7];
48
+ return data ? this.decodedData = this.snapshot.string(data - 1) : undefined;
49
+ }
50
+ get parent(): number | null { const parent = this.snapshot.nodes[this.base + 1]; return parent ? parent - 1 : null; }
51
+ get prev(): number | null { const prev = this.snapshot.nodes[this.base + 2]; return prev ? prev - 1 : null; }
52
+ get next(): number | null { const next = this.snapshot.nodes[this.base + 3]; return next ? next - 1 : null; }
53
+ get startIndex(): number | null | undefined { return this.index(HAS_START_INDEX, 11); }
54
+ get endIndex(): number | null | undefined { return this.index(HAS_END_INDEX, 12); }
55
+ get namespace(): string | undefined { const nodes = this.snapshot.nodes; return nodes[this.base + 10] & HAS_NAMESPACE ? this.snapshot.name(nodes[this.base + 13]) : undefined; }
56
+ get attribsNamespace(): Record<string, string> | undefined { return this.snapshot.nodes[this.base + 10] & EMPTY_ATTRIBUTE_MAPS ? {} : undefined; }
57
+ get attribsPrefix(): Record<string, string> | undefined { return this.snapshot.nodes[this.base + 10] & EMPTY_ATTRIBUTE_MAPS ? {} : undefined; }
58
+ get attribs(): Record<string, string> { return this.decodedAttributes ??= this.snapshot.attributesOf(this.id); }
59
+ get children(): number[] { return this.decodedChildren ??= this.snapshot.childrenOf(this.id); }
60
+
61
+ private index(flag: number, offset: number): number | null | undefined {
62
+ const nodes = this.snapshot.nodes;
63
+ if (!(nodes[this.base + 10] & flag)) return undefined;
64
+ const value = nodes[this.base + offset];
65
+ return value === ABSENT ? null : value;
66
+ }
67
+ }
68
+
69
+ /** Snapshot records extended with fields stored as JSON, which are rare. */
70
+ const recordFields = ['type', 'name', 'data', 'parent', 'prev', 'next', 'startIndex', 'endIndex', 'namespace', 'attribsNamespace', 'attribsPrefix', 'attribs', 'children'] as const;
71
+
72
+ /** A plain copy of a record, for callers that spread or serialize it. */
73
+ export function plainRecord(record: NativeNodeRecord): NativeNodeRecord {
74
+ if (!(record instanceof SnapshotRecord)) return record;
75
+ const copy: Record<string, unknown> = { id: record.id };
76
+ for (const field of recordFields) { const value = (record as any)[field]; if (value !== undefined) copy[field] = value; }
77
+ for (const key of Object.keys(record)) if (!key.startsWith('decoded') && key !== 'snapshot' && key !== 'base' && key !== 'id') copy[key] = (record as any)[key];
78
+ return copy as NativeNodeRecord;
79
+ }
80
+
81
+ /** Decodes node records from a snapshot on demand, exactly as `node()` returns them. */
82
+ export class NodeSnapshot {
83
+ readonly size: number;
84
+ readonly nodes: Uint32Array;
85
+ private readonly children: Uint32Array;
86
+ private readonly attributes: Uint32Array;
87
+ private readonly strings: Uint32Array;
88
+ private readonly text: string;
89
+ private readonly isEncoded: boolean;
90
+ private readonly names: (string | undefined)[] = [];
91
+ /** Per interned type: 1 for element types, 2 for root, 3 for text, 0 otherwise. */
92
+ private readonly typeKinds: (number | undefined)[] = [];
93
+ /** Node object constructors by interned type, resolved once per snapshot. */
94
+ viewConstructors: unknown = undefined;
95
+ /** Text nodes too large for the arrays, read individually while this snapshot is current. */
96
+ readonly largeTexts = new Map<number, string>();
97
+
98
+ constructor(raw: NativeSnapshot) {
99
+ const { data } = raw;
100
+ const childrenStart = HEADER + data[0];
101
+ const attributesStart = childrenStart + data[1];
102
+ const stringsStart = attributesStart + data[2];
103
+ this.text = raw.text;
104
+ this.isEncoded = data[4] === 1;
105
+ this.nodes = data.subarray(HEADER, childrenStart);
106
+ this.children = data.subarray(childrenStart, attributesStart);
107
+ this.attributes = data.subarray(attributesStart, stringsStart);
108
+ this.strings = data.subarray(stringsStart, stringsStart + data[3]);
109
+ this.size = this.nodes.length / STRIDE;
110
+ }
111
+
112
+ /** The record, `null` for a released node, or `undefined` when it must be read individually. */
113
+ record(local: number): NativeNodeRecord | null | undefined {
114
+ if (local >= this.size) return undefined;
115
+ const base = local * STRIDE;
116
+ const flags = this.nodes[base + 10];
117
+ if (flags & RELEASED) return null;
118
+ if (flags & OPAQUE) return undefined;
119
+ const record = new SnapshotRecord(this, local, base);
120
+ const extras = this.nodes[base + 14];
121
+ if (extras) {
122
+ // Fields stored as JSON, such as foreign attribute namespaces, shadow the decoded getters.
123
+ const fields = JSON.parse(this.string(extras - 1));
124
+ for (const key of Object.keys(fields)) Object.defineProperty(record, key, { value: fields[key], writable: true, enumerable: true, configurable: true });
125
+ }
126
+ return record as unknown as NativeNodeRecord;
127
+ }
128
+
129
+ /** Whether a node's fields all live in the arrays, so callers may read them without a record. */
130
+ isPlain(local: number): boolean {
131
+ if (local >= this.size) return false;
132
+ const base = local * STRIDE;
133
+ return (this.nodes[base + 10] & (OPAQUE | RELEASED)) === 0 && this.nodes[base + 14] === 0;
134
+ }
135
+
136
+ typeOf(local: number): string {
137
+ return this.name(this.nodes[local * STRIDE]);
138
+ }
139
+
140
+ nameOf(local: number): string | undefined {
141
+ const name = this.nodes[local * STRIDE + 6];
142
+ return name ? this.name(name - 1) : undefined;
143
+ }
144
+
145
+ dataOf(local: number): string | undefined {
146
+ const data = this.nodes[local * STRIDE + 7];
147
+ return data ? this.string(data - 1) : undefined;
148
+ }
149
+
150
+ /** The parent, previous or next sibling (`offset` 1, 2 or 3), or `null`. */
151
+ linkOf(local: number, offset: number): number | null {
152
+ const link = this.nodes[local * STRIDE + offset];
153
+ return link ? link - 1 : null;
154
+ }
155
+
156
+ namespaceOf(local: number): string | undefined {
157
+ const base = local * STRIDE;
158
+ return this.nodes[base + 10] & HAS_NAMESPACE ? this.name(this.nodes[base + 13]) : undefined;
159
+ }
160
+
161
+ /** A node's attributes as parse5 attribute objects; plain nodes have no namespaced ones. */
162
+ attributeObjects(local: number): { name: string; value: string }[] {
163
+ const base = local * STRIDE;
164
+ const count = this.nodes[base + 9];
165
+ const first = this.nodes[base + 8];
166
+ const attributes = this.attributes;
167
+ const result: { name: string; value: string }[] = new Array(count);
168
+ for (let index = 0; index < count; index++) {
169
+ const pair = (first + index) * 2;
170
+ result[index] = { name: this.name(attributes[pair]), value: this.string(attributes[pair + 1]) };
171
+ }
172
+ return result;
173
+ }
174
+
175
+ /** Copies a node's attributes onto `target`, in order. */
176
+ /** Copies a node's attributes as the shared accessors of `attributes`, in record order. */
177
+ copyAttributes(local: number, attributes: AttributeObject): void {
178
+ const base = local * STRIDE;
179
+ const count = this.nodes[base + 9];
180
+ const first = this.nodes[base + 8];
181
+ const attributes_ = this.attributes;
182
+ for (let index = 0; index < count; index++) {
183
+ const pair = (first + index) * 2;
184
+ defineAttributeKey(attributes, this.name(attributes_[pair]), this.string(attributes_[pair + 1]));
185
+ }
186
+ }
187
+
188
+ /** The flat child id list; a node's children start at `childStart(local)`. */
189
+ get childIds(): Uint32Array { return this.children; }
190
+
191
+ childStart(local: number): number {
192
+ return this.nodes[local * STRIDE + 4];
193
+ }
194
+
195
+ /** Whether a node is an element: a tag, script or style. */
196
+ isElementAt(local: number): boolean {
197
+ return this.typeKind(local) === 1;
198
+ }
199
+
200
+ isRootAt(local: number): boolean {
201
+ return this.typeKind(local) === 2;
202
+ }
203
+
204
+ isTextAt(local: number): boolean {
205
+ return this.typeKind(local) === 3;
206
+ }
207
+
208
+ /** Whether a node's record adds fields stored as JSON, such as source locations. */
209
+ hasExtraFields(local: number): boolean {
210
+ return this.nodes[local * STRIDE + 14] !== 0;
211
+ }
212
+
213
+ /** Whether the slot holds a node, rather than one a collection released. */
214
+ isPresent(local: number): boolean {
215
+ return local < this.size && (this.nodes[local * STRIDE + 10] & RELEASED) === 0;
216
+ }
217
+
218
+ private typeKind(local: number): number {
219
+ const reference = this.nodes[local * STRIDE];
220
+ let kind = this.typeKinds[reference];
221
+ if (kind === undefined) {
222
+ const type = this.name(reference);
223
+ kind = this.typeKinds[reference] = type === 'tag' || type === 'script' || type === 'style' ? 1 : type === 'root' ? 2 : type === 'text' ? 3 : 0;
224
+ }
225
+ return kind;
226
+ }
227
+
228
+ /** The interned reference of a node's type, comparable across nodes of this snapshot. */
229
+ typeReference(local: number): number {
230
+ return this.nodes[local * STRIDE];
231
+ }
232
+
233
+ /** Child ids without materializing the record. */
234
+ childCount(local: number): number {
235
+ return this.nodes[local * STRIDE + 5];
236
+ }
237
+
238
+ childAt(local: number, index: number): number {
239
+ return this.children[this.nodes[local * STRIDE + 4] + index];
240
+ }
241
+
242
+ childrenOf(local: number): number[] {
243
+ const base = local * STRIDE;
244
+ const count = this.nodes[base + 5];
245
+ if (count === 0) return noChildren;
246
+ const first = this.nodes[base + 4];
247
+ return Array.from(this.children.subarray(first, first + count));
248
+ }
249
+
250
+ attributesOf(local: number): Record<string, string> {
251
+ const base = local * STRIDE;
252
+ const count = this.nodes[base + 9];
253
+ if (count === 0) return noAttributes;
254
+ const first = this.nodes[base + 8];
255
+ const attributes = this.attributes;
256
+ const result: Record<string, string> = {};
257
+ for (let index = 0; index < count; index++) {
258
+ const pair = (first + index) * 2;
259
+ const name = this.name(attributes[pair]);
260
+ const value = this.string(attributes[pair + 1]);
261
+ if (name === '__proto__') Object.defineProperty(result, name, { value, writable: true, enumerable: true, configurable: true });
262
+ else result[name] = value;
263
+ }
264
+ return result;
265
+ }
266
+
267
+ /** One attribute's value, without decoding the rest. */
268
+ attributeValue(local: number, name: string): string | undefined {
269
+ const base = local * STRIDE;
270
+ const count = this.nodes[base + 9];
271
+ const first = this.nodes[base + 8];
272
+ const attributes = this.attributes;
273
+ // A repeated name keeps its last value, as decoded attribute maps do.
274
+ for (let index = count - 1; index >= 0; index--) {
275
+ const pair = (first + index) * 2;
276
+ if (this.name(attributes[pair]) === name) return this.string(attributes[pair + 1]);
277
+ }
278
+ return undefined;
279
+ }
280
+
281
+ string(reference: number): string {
282
+ const strings = this.strings;
283
+ const value = this.text.slice(strings[reference * 2], strings[reference * 2 + 1]);
284
+ return this.isEncoded ? decodeString(value) : value;
285
+ }
286
+
287
+ /** Names repeat across nodes, so each decodes once. */
288
+ name(reference: number): string {
289
+ return this.names[reference] ??= this.string(reference);
290
+ }
291
+ }
@@ -0,0 +1,113 @@
1
+ import { decodeJson, decodeString, encodeJson, encodeOptions, encodeString } from './codec.js';
2
+ import type { NativeBinding, NativeDocument } from './native.js';
3
+
4
+ interface TransportRegistry {
5
+ userErrors: WeakSet<object>;
6
+ }
7
+ const registryKey = Symbol.for('cheerox.native-transport.v2');
8
+ const globals = globalThis as typeof globalThis & { [key: symbol]: TransportRegistry | undefined };
9
+ const registry = globals[registryKey] ??= { userErrors: new WeakSet() };
10
+ const transportedKey = Symbol.for('cheerox.native-transport.prototype');
11
+
12
+ const jsonMethods = new Set(['op', 'node', 'exportTree', 'exportTreeWithOffset', 'parserDiagnostics', 'adopt']);
13
+ /** String arguments each document method takes, by position, and how to encode them. */
14
+ const argumentEncoders: Record<string, (args: any[]) => void> = {
15
+ op: (args) => { args[1] = encodeJson(args[1]); },
16
+ create: (args) => { args[0] = encodeString(args[0]); if (typeof args[2] === 'string') args[2] = encodeOptions(args[2]); },
17
+ select: (args) => { args[0] = encodeString(args[0]); },
18
+ matches: (args) => { args[0] = encodeString(args[0]); },
19
+ attr: (args) => { args[1] = encodeString(args[1]); },
20
+ setAttribute: (args) => { args[1] = encodeString(args[1]); if (typeof args[2] === 'string') args[2] = encodeString(args[2]); },
21
+ render: (args) => { if (typeof args[1] === 'string') args[1] = encodeJson(args[1]); },
22
+ };
23
+
24
+ /** Keep the codec at the private addon boundary, including callback RPC. */
25
+ export function wrapNativeBinding(binding: NativeBinding): NativeBinding {
26
+ const raw = binding as NativeBinding & Record<string, any>;
27
+ const { userErrors } = registry;
28
+ const restoreError = (error: unknown): never => {
29
+ if (error instanceof Error && !userErrors.has(error)) {
30
+ const message = decodeString(error.message);
31
+ if (message !== error.message) {
32
+ const stack = error.stack;
33
+ error.message = message;
34
+ if (stack) error.stack = decodeString(stack);
35
+ }
36
+ }
37
+ throw error;
38
+ };
39
+ const invoke = <T>(call: () => T): T => {
40
+ try {
41
+ const result = call();
42
+ return result instanceof Promise ? result.catch(restoreError) as T : result;
43
+ } catch (error) { return restoreError(error); }
44
+ };
45
+ const wrapBridge = (bridge: (request: string) => string) => (request: string): string => {
46
+ try { return encodeJson(bridge(decodeJson(request))); }
47
+ catch (error) {
48
+ if (error !== null && (typeof error === 'object' || typeof error === 'function')) userErrors.add(error);
49
+ throw error;
50
+ }
51
+ };
52
+ // Wrapping the class's methods once, rather than each document in a proxy,
53
+ // keeps native calls free of per-document and per-call allocations.
54
+ const transportMethod = (name: string, method: (...args: any[]) => unknown) => {
55
+ const encodeArguments = argumentEncoders[name];
56
+ const decode = jsonMethods.has(name) ? decodeJson : decodeString;
57
+ const isOperation = name === 'op';
58
+ return function transported(this: NativeDocument, ...args: any[]): unknown {
59
+ try {
60
+ if (encodeArguments) encodeArguments(args);
61
+ const result = method.apply(this, args);
62
+ if (typeof result !== 'string') return result;
63
+ if (isOperation && args[0] === 'serialize' && result.startsWith('{')) {
64
+ const error = JSON.parse(result).__nativeError;
65
+ if (error?.name === 'URIError') throw new URIError(error.message);
66
+ }
67
+ return decode(result);
68
+ } catch (error) { return restoreError(error); }
69
+ };
70
+ };
71
+ const prototype = raw.NativeDocument.prototype as Record<PropertyKey, unknown>;
72
+ if (!Object.hasOwn(prototype, transportedKey)) {
73
+ for (const name of Object.getOwnPropertyNames(prototype)) {
74
+ const method = prototype[name];
75
+ if (name !== 'constructor' && typeof method === 'function') prototype[name] = transportMethod(name, method as (...args: any[]) => unknown);
76
+ }
77
+ Object.defineProperty(prototype, transportedKey, { value: true });
78
+ }
79
+ const wrapStream = (stream: any) => {
80
+ let carry = '';
81
+ return {
82
+ async write(chunk: string) {
83
+ let source = carry + chunk;
84
+ carry = '';
85
+ const last = source.charCodeAt(source.length - 1);
86
+ if (last >= 0xd800 && last <= 0xdbff) { carry = source.slice(-1); source = source.slice(0, -1); }
87
+ if (source) await invoke(() => stream.write(encodeString(source)));
88
+ },
89
+ async finish() {
90
+ if (carry) { const final = carry; carry = ''; await invoke(() => stream.write(encodeString(final))); }
91
+ return invoke(() => stream.finish());
92
+ },
93
+ abort() { carry = ''; return invoke(() => stream.abort()); },
94
+ };
95
+ };
96
+ const wrapConstructor = (Constructor: any, argumentsFor: (args: any[]) => any[], wrap: (value: any) => any) => new Proxy(Constructor, {
97
+ construct(target, args) { return wrap(invoke(() => Reflect.construct(target, argumentsFor(args)))); },
98
+ });
99
+ const wrapped: NativeBinding & Record<string, any> = { ...raw };
100
+ const Document = raw.NativeDocument;
101
+ wrapped.NativeDocument = function NativeDocument(html: string, isDocument?: boolean, options?: string) {
102
+ try { return new Document(encodeString(html), isDocument, encodeOptions(options)); }
103
+ catch (error) { return restoreError(error); }
104
+ } as unknown as NativeBinding['NativeDocument'];
105
+ if (raw.NativeStream) wrapped.NativeStream = wrapConstructor(raw.NativeStream, ([options, isDocument, bridge]) => [encodeOptions(options), isDocument, bridge && wrapBridge(bridge)], wrapStream);
106
+ if (raw.NativeHookStream) wrapped.NativeHookStream = wrapConstructor(raw.NativeHookStream, ([isDocument, options, bridge]) => [isDocument, encodeOptions(options), wrapBridge(bridge)], wrapStream);
107
+ if (raw.parseWithTreeAdapter) wrapped.parseWithTreeAdapter = (source: string, isDocument: boolean, options: string, bridge: (request: string) => string) => invoke(() => raw.parseWithTreeAdapter(encodeString(source), isDocument, encodeOptions(options), wrapBridge(bridge)));
108
+ if (raw.parseAsync) wrapped.parseAsync = (source, isDocument, options) => invoke(() => raw.parseAsync!(encodeString(source), isDocument, encodeOptions(options)));
109
+ if (raw.parseManyAsync) wrapped.parseManyAsync = (sources, isDocument, threads, options, plans) => invoke(() => raw.parseManyAsync!(sources.map(encodeString), isDocument, threads, encodeOptions(options), plans === undefined ? undefined : encodeJson(plans)));
110
+ if (raw.extractPlanManyAsync) wrapped.extractPlanManyAsync = async (sources, plan, threads, options) => decodeJson(await invoke(() => raw.extractPlanManyAsync!(sources.map(encodeString), encodeJson(plan), threads, encodeOptions(options))));
111
+ if (raw.extractManyAsync) wrapped.extractManyAsync = async (sources, queries, threads, isDocument, options) => decodeJson(await invoke(() => raw.extractManyAsync!(sources.map(encodeString), queries.map((query) => ({ ...query, key: encodeString(query.key), selector: encodeString(query.selector), ...(query.attr === undefined ? {} : { attr: encodeString(query.attr) }) })), threads, isDocument, encodeOptions(options))));
112
+ return wrapped;
113
+ }
package/src/types.ts ADDED
@@ -0,0 +1,186 @@
1
+ import type { Cheerio } from './selection.js';
2
+ import type { ParserErrorHandler, TreeAdapter, TokenizerConstructor } from './parser-types.js';
3
+ export type * from './parser-types.js';
4
+
5
+ /** DOM-compatible views over stable nodes owned by the native document. */
6
+ export declare enum ElementType {
7
+ Root = 'root', Text = 'text', Directive = 'directive', Comment = 'comment',
8
+ Script = 'script', Style = 'style', Tag = 'tag', CDATA = 'cdata', Doctype = 'doctype',
9
+ }
10
+
11
+ export interface SourceCodeLocation {
12
+ startLine: number; startCol: number; startOffset: number;
13
+ endLine: number; endCol: number; endOffset: number;
14
+ }
15
+ export interface TagSourceCodeLocation extends SourceCodeLocation { startTag?: SourceCodeLocation; endTag?: SourceCodeLocation; attrs?: Record<string, SourceCodeLocation> }
16
+
17
+ export interface Node {
18
+ readonly type: ElementType;
19
+ parent: ParentNode | null;
20
+ prev: ChildNode | null;
21
+ next: ChildNode | null;
22
+ startIndex: number | null;
23
+ endIndex: number | null;
24
+ readonly nodeType: number;
25
+ parentNode: ParentNode | null;
26
+ previousSibling: ChildNode | null;
27
+ nextSibling: ChildNode | null;
28
+ sourceCodeLocation?: SourceCodeLocation | null;
29
+ cloneNode<T extends Node>(this: T, recursive?: boolean): T;
30
+ }
31
+
32
+ export interface DataNode extends Node { data: string; nodeValue: string }
33
+ export interface Text extends DataNode { type: ElementType.Text; readonly nodeType: 3 }
34
+ export interface Comment extends DataNode { type: ElementType.Comment; readonly nodeType: 8 }
35
+ export interface ProcessingInstruction extends DataNode {
36
+ type: ElementType.Directive;
37
+ name: string;
38
+ readonly nodeType: 1;
39
+ 'x-name'?: string;
40
+ 'x-publicId'?: string;
41
+ 'x-systemId'?: string;
42
+ }
43
+ export interface NodeWithChildren extends Node {
44
+ children: ChildNode[];
45
+ childNodes: ChildNode[];
46
+ readonly firstChild: ChildNode | null;
47
+ readonly lastChild: ChildNode | null;
48
+ }
49
+ export interface Document extends NodeWithChildren { type: ElementType.Root; readonly nodeType: 9; 'x-mode'?: 'quirks' | 'limited-quirks' | 'no-quirks' }
50
+ export interface CDATA extends NodeWithChildren { type: ElementType.CDATA; readonly nodeType: 4 }
51
+ export interface Element extends NodeWithChildren {
52
+ type: ElementType.Tag | ElementType.Script | ElementType.Style;
53
+ readonly nodeType: 1;
54
+ name: string;
55
+ tagName: string;
56
+ attribs: Record<string, string>;
57
+ namespace?: string;
58
+ sourceCodeLocation?: TagSourceCodeLocation | null;
59
+ readonly attributes: { name: string; value: string; namespace?: string; prefix?: string }[];
60
+ 'x-attribsNamespace'?: Record<string, string>;
61
+ 'x-attribsPrefix'?: Record<string, string>;
62
+ }
63
+ export type ParentNode = Document | Element | CDATA;
64
+ export type ChildNode = Text | Comment | ProcessingInstruction | Element | CDATA | Document;
65
+ export type AnyNode = ParentNode | ChildNode;
66
+
67
+ export interface HTMLParser2Options {
68
+ xmlMode?: boolean;
69
+ decodeEntities?: boolean;
70
+ encodeEntities?: boolean | 'utf8';
71
+ lowerCaseTags?: boolean;
72
+ lowerCaseAttributeNames?: boolean;
73
+ recognizeCDATA?: boolean;
74
+ recognizeSelfClosing?: boolean;
75
+ withStartIndices?: boolean;
76
+ withEndIndices?: boolean;
77
+ emptyAttrs?: boolean;
78
+ selfClosingTags?: boolean;
79
+ Tokenizer?: TokenizerConstructor;
80
+ [key: string]: any;
81
+ }
82
+
83
+ export interface CheerioOptions extends HTMLParser2Options {
84
+ xml?: boolean | HTMLParser2Options;
85
+ baseURI?: string | URL;
86
+ quirksMode?: boolean;
87
+ scriptingEnabled?: boolean;
88
+ sourceCodeLocationInfo?: boolean;
89
+ treeAdapter?: TreeAdapter;
90
+ onParseError?: ParserErrorHandler | null;
91
+ pseudos?: Record<string, string | ((element: Element, value?: string | null) => boolean)>;
92
+ _useHtmlParser2?: boolean;
93
+ }
94
+ export type InternalOptions = CheerioOptions;
95
+
96
+ type LowercaseLetters = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';
97
+ type AlphaNumeric = LowercaseLetters | Uppercase<LowercaseLetters> | `${number}`;
98
+ export type SelectorType = `${'.' | '#' | ':' | '|' | '>' | '+' | '~' | '['}${AlphaNumeric}${string}` | `${AlphaNumeric}${string}`;
99
+ export type BasicAcceptedElems<T extends AnyNode = AnyNode> = ArrayLike<T> | T | string;
100
+ export type AcceptedElems<T extends AnyNode = AnyNode> = BasicAcceptedElems<T> | ((this: T, index: number, element: T) => BasicAcceptedElems<T>);
101
+ export type FilterFunction<T> = (this: T, index: number, element: T) => boolean;
102
+ export type AcceptedFilters<T> = string | FilterFunction<T> | T | Cheerio<T>;
103
+ export type LoadContent = string | Buffer | AnyNode | ArrayLike<AnyNode>;
104
+
105
+ export interface CheerioAPI {
106
+ <T extends AnyNode, S extends string>(
107
+ selector?: S | BasicAcceptedElems<T>,
108
+ context?: BasicAcceptedElems<AnyNode> | null,
109
+ root?: BasicAcceptedElems<Document>,
110
+ options?: CheerioOptions,
111
+ ): Cheerio<S extends SelectorType ? Element : T>;
112
+ <T extends AnyNode, S extends string>(
113
+ selector?: S | BasicAcceptedElems<T>,
114
+ context?: BasicAcceptedElems<AnyNode> | null,
115
+ root?: BasicAcceptedElems<Document>,
116
+ options?: CheerioOptions,
117
+ ): Cheerio<AnyNode>;
118
+ html(options?: CheerioOptions): string;
119
+ html(dom?: BasicAcceptedElems<AnyNode> | Cheerio<AnyNode>, options?: CheerioOptions): string;
120
+ xml(dom?: BasicAcceptedElems<AnyNode> | Cheerio<AnyNode>): string;
121
+ text(elements?: ArrayLike<AnyNode>): string;
122
+ root(): Cheerio<Document>;
123
+ contains(container: AnyNode, contained: AnyNode): boolean;
124
+ merge<T>(first: ArrayLike<T>, second: ArrayLike<T>): ArrayLike<T> | undefined;
125
+ parseHTML(data: string, context?: unknown, keepScripts?: boolean): AnyNode[];
126
+ parseHTML(data?: '' | null): null;
127
+ load(content: LoadContent, options?: CheerioOptions | null, isDocument?: boolean): CheerioAPI;
128
+ extract<M extends ExtractMap>(this: CheerioAPI, map: M): ExtractedMap<M>;
129
+ extract<M extends ExtractMap>(this: CheerioAPI, map: M): Record<string, unknown>;
130
+ fn: typeof Cheerio.prototype;
131
+ _root: Document;
132
+ _options: InternalOptions;
133
+ [key: string]: any;
134
+ }
135
+
136
+ type ExtractDescriptorFn = (element: Element, key: string, result: Record<string, unknown>) => unknown;
137
+ export interface ExtractDescriptor {
138
+ selector: string;
139
+ value?: string | ExtractDescriptorFn | ExtractMap;
140
+ }
141
+ type ExtractValue = string | ExtractDescriptor | [string | ExtractDescriptor];
142
+ export type ExtractMap = Record<string, ExtractValue>;
143
+ export type ExtractedValue<V extends ExtractValue> = V extends [string | ExtractDescriptor]
144
+ ? NonNullable<ExtractedValue<V[0]>>[]
145
+ : V extends string ? string | undefined
146
+ : V extends ExtractDescriptor ? V['value'] extends infer U
147
+ ? U extends ExtractMap ? ExtractedMap<U> | undefined
148
+ : U extends ExtractDescriptorFn ? ReturnType<U> | undefined
149
+ : string | undefined
150
+ : never
151
+ : never;
152
+ export type ExtractedMap<M extends ExtractMap> = {
153
+ [key in keyof M]: ExtractedValue<M[key]>;
154
+ };
155
+ export type ExtractionDescriptor = ExtractDescriptor;
156
+ export type ExtractionMap = ExtractMap;
157
+ export type ExtractionResult<M extends ExtractMap> = ExtractedMap<M>;
158
+
159
+ export interface EncodingOptions { defaultEncoding?: string; transportLayerEncodingLabel?: string | null; userEncoding?: string; maxBytes?: number }
160
+ export interface DecodeStreamOptions extends CheerioOptions { encoding?: EncodingOptions }
161
+ /** Structural Node/Undici FormData compatibility without importing its package. */
162
+ export interface FormDataLike extends Iterable<[string, string | import('node:buffer').Blob]> {
163
+ append(name: string, value: unknown, filename?: string): void;
164
+ get(name: string): string | import('node:buffer').Blob | null;
165
+ }
166
+ export interface CheerioRequestOptions extends DecodeStreamOptions {
167
+ requestOptions?: {
168
+ method?: string;
169
+ headers?: Record<string, string | string[] | undefined> | string[] | Iterable<[string, string | string[] | undefined]> | Headers | null;
170
+ body?: string | Buffer | Uint8Array | import('node:stream').Readable | FormData | FormDataLike | null;
171
+ query?: import('node:querystring').ParsedUrlQueryInput;
172
+ onInfo?: (info: { statusCode: number; headers: Record<string, string | string[]> }) => void;
173
+ reset?: boolean;
174
+ typeOfService?: number | null;
175
+ highWaterMark?: number;
176
+ maxRedirections?: number;
177
+ throwOnError?: boolean;
178
+ signal?: unknown;
179
+ headersTimeout?: number | null;
180
+ bodyTimeout?: number | null;
181
+ dispatcher?: unknown;
182
+ [key: string]: unknown;
183
+ };
184
+ }
185
+ export interface ParallelOptions extends CheerioOptions { threads?: number }
186
+ export type SerializedField = { name: string; value: string };
package/src/utils.ts ADDED
@@ -0,0 +1,29 @@
1
+ import type { AnyNode, Element } from './types.js';
2
+ import type { Cheerio } from './selection.js';
3
+
4
+ export function isCheerio(value: unknown): value is Cheerio<AnyNode> {
5
+ return value !== null && typeof value === 'object' && 'cheerio' in value;
6
+ }
7
+
8
+ export function camelCase(value: string): string {
9
+ return value.replace(/[._-](\w|$)/g, (_match, character: string) => character.toUpperCase());
10
+ }
11
+
12
+ export function cssCase(value: string): string {
13
+ return value.replace(/[A-Z]/g, (character) => `-${character.toLowerCase()}`);
14
+ }
15
+
16
+ export function domEach<T, A extends ArrayLike<T>>(array: A, callback: (element: T, index: number) => void): A {
17
+ for (let index = 0; index < array.length; index += 1) callback(array[index], index);
18
+ return array;
19
+ }
20
+
21
+ export function isHtml(value: unknown): value is string {
22
+ if (typeof value !== 'string') return false;
23
+ const start = value.indexOf('<');
24
+ return start >= 0 && /[A-Za-z!]/.test(value[start + 1] ?? '') && value.indexOf('>', start + 2) >= 0;
25
+ }
26
+
27
+ export function isTag(node: AnyNode): node is Element {
28
+ return node.type === 'tag' || node.type === 'script' || node.type === 'style';
29
+ }
package/src/workers.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { requireNative, type NativeWorkerPoolInfo } from './native.js';
2
+
3
+ export interface WorkerOptions { threads: number }
4
+ export type WorkerPoolInfo = NativeWorkerPoolInfo;
5
+
6
+ /** Set the process-wide native CPU-worker budget before starting async work. */
7
+ export function configureWorkers(options: WorkerOptions | number): WorkerPoolInfo {
8
+ const threads = typeof options === 'number' ? options : options?.threads;
9
+ if (!Number.isSafeInteger(threads) || threads < 1 || threads > 256) throw new RangeError('threads must be an integer from 1 to 256');
10
+ const binding = requireNative();
11
+ if (!binding.configureWorkerPool || !binding.workerPoolInfo) throw new Error('This native build does not support a shared worker pool');
12
+ binding.configureWorkerPool(threads);
13
+ return binding.workerPoolInfo();
14
+ }
15
+
16
+ /** Observe the configured budget and actual concurrent native CPU work. */
17
+ export function workerPoolInfo(): WorkerPoolInfo {
18
+ const binding = requireNative();
19
+ if (!binding.workerPoolInfo) throw new Error('This native build does not support worker pool diagnostics');
20
+ return binding.workerPoolInfo();
21
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "Bundler",
6
+ "strict": true,
7
+ "esModuleInterop": true,
8
+ "skipLibCheck": true,
9
+ "declaration": true,
10
+ "stripInternal": true,
11
+ "resolveJsonModule": true,
12
+ "types": ["node"]
13
+ },
14
+ "include": ["src/**/*.ts", "tsup.config.ts"]
15
+ }