rusteerio 0.0.0-stage → 0.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/CONTRIBUTING.md +73 -0
- package/Cargo.lock +1051 -0
- package/Cargo.toml +36 -0
- package/LICENSE +21 -0
- package/README.md +252 -2
- package/RELEASING.md +138 -0
- package/THIRD_PARTY_NOTICES.md +7443 -0
- package/build.rs +3 -0
- package/dist/chunk-DKULX6VO.mjs +3185 -0
- package/dist/chunk-DKULX6VO.mjs.map +1 -0
- package/dist/chunk-OHPGEY3E.mjs +32 -0
- package/dist/chunk-OHPGEY3E.mjs.map +1 -0
- package/dist/index.cjs +3230 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +113 -0
- package/dist/index.d.ts +113 -0
- package/dist/index.mjs +38 -0
- package/dist/index.mjs.map +1 -0
- package/dist/slim.cjs +2441 -0
- package/dist/slim.cjs.map +1 -0
- package/dist/slim.d.mts +11 -0
- package/dist/slim.d.ts +11 -0
- package/dist/slim.mjs +19 -0
- package/dist/slim.mjs.map +1 -0
- package/dist/types-BP6TpxE5.d.mts +532 -0
- package/dist/types-BP6TpxE5.d.ts +532 -0
- package/dist/utils.cjs +61 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.mts +13 -0
- package/dist/utils.d.ts +13 -0
- package/dist/utils.mjs +17 -0
- package/dist/utils.mjs.map +1 -0
- package/native/src/arena.rs +1781 -0
- package/native/src/attributes.rs +923 -0
- package/native/src/codec.rs +172 -0
- package/native/src/diagnostics.rs +1249 -0
- package/native/src/extraction.rs +226 -0
- package/native/src/gc.rs +166 -0
- package/native/src/hook_sink.rs +727 -0
- package/native/src/hook_stream.rs +513 -0
- package/native/src/lib.rs +2095 -0
- package/native/src/location.rs +392 -0
- package/native/src/runtime.rs +189 -0
- package/native/src/selector.rs +1287 -0
- package/native/src/sink.rs +261 -0
- package/native/src/stream.rs +334 -0
- package/native/src/tokenizer.rs +355 -0
- package/native/src/tracker.rs +232 -0
- package/native/vendor/README.md +25 -0
- package/native/vendor/html5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/html5ever-0.35.0/Cargo.lock +828 -0
- package/native/vendor/html5ever-0.35.0/Cargo.toml +85 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/html5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/html5ever-0.35.0/README.md +60 -0
- package/native/vendor/html5ever-0.35.0/benches/html5ever.rs +86 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum-zh.html +19 -0
- package/native/vendor/html5ever-0.35.0/data/bench/lipsum.html +40 -0
- package/native/vendor/html5ever-0.35.0/data/bench/medium-fragment.html +24 -0
- package/native/vendor/html5ever-0.35.0/data/bench/small-fragment.html +7 -0
- package/native/vendor/html5ever-0.35.0/data/bench/strong.html +1 -0
- package/native/vendor/html5ever-0.35.0/data/bench/tiny-fragment.html +1 -0
- package/native/vendor/html5ever-0.35.0/examples/arena.rs +356 -0
- package/native/vendor/html5ever-0.35.0/examples/capi/tokenize.c +74 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tokenize.rs +47 -0
- package/native/vendor/html5ever-0.35.0/examples/noop-tree-builder.rs +132 -0
- package/native/vendor/html5ever-0.35.0/examples/print-tree-actions.rs +185 -0
- package/native/vendor/html5ever-0.35.0/examples/tokenize.rs +111 -0
- package/native/vendor/html5ever-0.35.0/src/driver.rs +148 -0
- package/native/vendor/html5ever-0.35.0/src/lib.rs +30 -0
- package/native/vendor/html5ever-0.35.0/src/macros.rs +34 -0
- package/native/vendor/html5ever-0.35.0/src/serialize/mod.rs +255 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/char_ref/mod.rs +451 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/interface.rs +99 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/mod.rs +2305 -0
- package/native/vendor/html5ever-0.35.0/src/tokenizer/states.rs +97 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/data.rs +170 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/mod.rs +1848 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/rules.rs +1494 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/tag_sets.rs +114 -0
- package/native/vendor/html5ever-0.35.0/src/tree_builder/types.rs +91 -0
- package/native/vendor/html5ever-0.35.0/src/util/str.rs +52 -0
- package/native/vendor/html5gum-0.8.4/Cargo.lock +1006 -0
- package/native/vendor/html5gum-0.8.4/Cargo.toml +109 -0
- package/native/vendor/html5gum-0.8.4/LICENSE +19 -0
- package/native/vendor/html5gum-0.8.4/README.md +110 -0
- package/native/vendor/html5gum-0.8.4/benches/files/0050.html +2131 -0
- package/native/vendor/html5gum-0.8.4/benches/files/6D30.html +1004 -0
- package/native/vendor/html5gum-0.8.4/benches/files.rs +32 -0
- package/native/vendor/html5gum-0.8.4/benches/patterns.rs +31 -0
- package/native/vendor/html5gum-0.8.4/src/arrayvec.rs +29 -0
- package/native/vendor/html5gum-0.8.4/src/char_validator.rs +134 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/callback.rs +676 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/default.rs +207 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/emitter.rs +344 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/html5ever.rs +306 -0
- package/native/vendor/html5gum-0.8.4/src/emitters/mod.rs +25 -0
- package/native/vendor/html5gum-0.8.4/src/entities.rs +3342 -0
- package/native/vendor/html5gum-0.8.4/src/error.rs +96 -0
- package/native/vendor/html5gum-0.8.4/src/htmlstring.rs +130 -0
- package/native/vendor/html5gum-0.8.4/src/lib.rs +66 -0
- package/native/vendor/html5gum-0.8.4/src/machine.rs +2136 -0
- package/native/vendor/html5gum-0.8.4/src/machine_helper.rs +277 -0
- package/native/vendor/html5gum-0.8.4/src/read_helper.rs +244 -0
- package/native/vendor/html5gum-0.8.4/src/reader.rs +440 -0
- package/native/vendor/html5gum-0.8.4/src/span.rs +98 -0
- package/native/vendor/html5gum-0.8.4/src/state.rs +16 -0
- package/native/vendor/html5gum-0.8.4/src/testutils.rs +71 -0
- package/native/vendor/html5gum-0.8.4/src/tokenizer.rs +189 -0
- package/native/vendor/html5gum-0.8.4/src/utils.rs +88 -0
- package/native/vendor/markup5ever-0.35.0/.cargo_vcs_info.json +6 -0
- package/native/vendor/markup5ever-0.35.0/Cargo.toml +45 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-APACHE +201 -0
- package/native/vendor/markup5ever-0.35.0/LICENSE-MIT +25 -0
- package/native/vendor/markup5ever-0.35.0/interface/mod.rs +422 -0
- package/native/vendor/markup5ever-0.35.0/interface/tree_builder.rs +280 -0
- package/native/vendor/markup5ever-0.35.0/lib.rs +59 -0
- package/native/vendor/markup5ever-0.35.0/serialize.rs +77 -0
- package/native/vendor/markup5ever-0.35.0/util/buffer_queue.rs +333 -0
- package/native/vendor/markup5ever-0.35.0/util/smallcharset.rs +88 -0
- package/package.json +137 -4
- package/rusteerio.darwin-arm64.node +0 -0
- package/rusteerio.darwin-x64.node +0 -0
- package/rusteerio.linux-arm64-gnu.node +0 -0
- package/rusteerio.linux-arm64-musl.node +0 -0
- package/rusteerio.linux-x64-gnu.node +0 -0
- package/rusteerio.linux-x64-musl.node +0 -0
- package/rusteerio.win32-arm64-msvc.node +0 -0
- package/rusteerio.win32-x64-msvc.node +0 -0
- package/scripts/check-native.mjs +15 -0
- package/scripts/check-release.mjs +235 -0
- package/scripts/fix-declarations.mjs +27 -0
- package/scripts/generate-notices.mjs +176 -0
- package/scripts/notices/README.md +13 -0
- package/scripts/notices/cheerio-MIT.txt +21 -0
- package/scripts/notices/html5ever-AUTHORS.txt +20 -0
- package/scripts/notices/html5ever-COPYRIGHT.txt +8 -0
- package/scripts/notices/html5ever-LICENSE-APACHE.txt +201 -0
- package/scripts/notices/html5ever-LICENSE-MIT.txt +25 -0
- package/scripts/notices/napi-rs-MIT.txt +43 -0
- package/scripts/notices/neon-MIT.txt +19 -0
- package/scripts/notices/runtime-01c266bced4a434da005.txt +3 -0
- package/scripts/notices/runtime-0a54e647fe54104658b5.txt +14 -0
- package/scripts/notices/runtime-0b74dfa0bcee5c420c6b.txt +25 -0
- package/scripts/notices/runtime-0d542e0c8804e39aa7f3.txt +177 -0
- package/scripts/notices/runtime-0f96a83840e146e43c0e.txt +21 -0
- package/scripts/notices/runtime-123a331b5dbf04c30097.txt +25 -0
- package/scripts/notices/runtime-172020dbfd5b53a226df.txt +34 -0
- package/scripts/notices/runtime-209fbbe0ad52d9235e37.txt +26 -0
- package/scripts/notices/runtime-23f18e03dc49df91622f.txt +23 -0
- package/scripts/notices/runtime-268872b9816f90fd8e85.txt +220 -0
- package/scripts/notices/runtime-35242e7a83f69875e6ed.txt +176 -0
- package/scripts/notices/runtime-378f5840b258e2779c39.txt +25 -0
- package/scripts/notices/runtime-4108245a1f2df9d4e94d.txt +25 -0
- package/scripts/notices/runtime-4455bf75a91154108304.txt +21 -0
- package/scripts/notices/runtime-553fffcd9b1cb158bc3e.txt +13 -0
- package/scripts/notices/runtime-62c7a1e35f56406896d7.txt +176 -0
- package/scripts/notices/runtime-6485b8ed310d3f0340bf.txt +25 -0
- package/scripts/notices/runtime-69fef7b0f322a6555415.txt +201 -0
- package/scripts/notices/runtime-6df43f6f4b5d4587f3d8.txt +187 -0
- package/scripts/notices/runtime-799e9ca9d179295ef372.txt +24 -0
- package/scripts/notices/runtime-7b63ecd5f1902af1b637.txt +25 -0
- package/scripts/notices/runtime-7bc7d5de2a0b793d4e1a.txt +21 -0
- package/scripts/notices/runtime-7e12e5df4bae12cb2158.txt +24 -0
- package/scripts/notices/runtime-861399f8c21c042b1105.txt +12 -0
- package/scripts/notices/runtime-8ada45cd9f843acf64e4.txt +201 -0
- package/scripts/notices/runtime-90eb64f0279b0d9432ac.txt +12 -0
- package/scripts/notices/runtime-a60eea817514531668d7.txt +201 -0
- package/scripts/notices/runtime-a7c936ff1ed8fa340172.txt +25 -0
- package/scripts/notices/runtime-ab6eec6caf0fa5775e41.txt +275 -0
- package/scripts/notices/runtime-b1181a40b2a7b25cf66f.txt +19 -0
- package/scripts/notices/runtime-b71bd43a069ca0641a9e.txt +25 -0
- package/scripts/notices/runtime-c16f8dcf1a368b83be78.txt +201 -0
- package/scripts/notices/runtime-c2cfccb812fe482101a8.txt +21 -0
- package/scripts/notices/runtime-e99d88d232bf57d70f0f.txt +25 -0
- package/scripts/notices/runtime-f32fb3b417a194167cfa.txt +9 -0
- package/scripts/notices/runtime-f5062c9a188d81dfe66b.txt +39 -0
- package/scripts/notices/runtime-f9070a832833fa7488fd.txt +4 -0
- package/scripts/notices/runtime-fab3dd6bdab226f1c086.txt +373 -0
- package/scripts/notices/runtime-fba3318d0c600380177e.txt +62 -0
- package/scripts/notices/runtime-ff8f68cb076caf8cefe7.txt +25 -0
- package/scripts/notices/runtime-ff92bed461f50338dd70.txt +74 -0
- package/scripts/notices/runtime.json +1632 -0
- package/scripts/notices/sources.json +555 -0
- package/scripts/notices/xray-MIT.txt +21 -0
- package/src/attributes.ts +271 -0
- package/src/codec.ts +74 -0
- package/src/dom.ts +87 -0
- package/src/extract-plan.ts +97 -0
- package/src/extract.ts +130 -0
- package/src/hooks.ts +220 -0
- package/src/index.ts +223 -0
- package/src/io.ts +491 -0
- package/src/native.ts +135 -0
- package/src/nodes.ts +290 -0
- package/src/parse.ts +53 -0
- package/src/parser-types.ts +157 -0
- package/src/router.ts +327 -0
- package/src/selection.ts +566 -0
- package/src/slim.ts +12 -0
- package/src/transport.ts +104 -0
- package/src/types.ts +186 -0
- package/src/utils.ts +29 -0
- package/src/workers.ts +21 -0
- package/tsconfig.json +15 -0
- package/tsup.config.ts +16 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
//! Evaluate callback-free extraction maps on native worker threads.
|
|
2
|
+
|
|
3
|
+
use napi::bindgen_prelude::*;
|
|
4
|
+
use serde_json::{json, Map, Value};
|
|
5
|
+
|
|
6
|
+
use crate::NativeDocument;
|
|
7
|
+
|
|
8
|
+
fn invalid(message: &str) -> Error {
|
|
9
|
+
Error::new(Status::InvalidArg, message.to_owned())
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
fn entries(value: &Value) -> Result<&[Value]> {
|
|
13
|
+
value
|
|
14
|
+
.get("entries")
|
|
15
|
+
.and_then(Value::as_array)
|
|
16
|
+
.map(Vec::as_slice)
|
|
17
|
+
.ok_or_else(|| invalid("Extraction plan requires entries"))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
fn extract(
|
|
21
|
+
document: &NativeDocument,
|
|
22
|
+
plan: &[Value],
|
|
23
|
+
contexts: Option<&[u32]>,
|
|
24
|
+
depth: usize,
|
|
25
|
+
) -> Result<Value> {
|
|
26
|
+
if depth > 64 {
|
|
27
|
+
return Err(invalid("Extraction plan nesting exceeds 64 levels"));
|
|
28
|
+
}
|
|
29
|
+
let mut result = Map::new();
|
|
30
|
+
for entry in plan {
|
|
31
|
+
let key = entry
|
|
32
|
+
.get("key")
|
|
33
|
+
.and_then(Value::as_str)
|
|
34
|
+
.ok_or_else(|| invalid("Extraction entry requires a key"))?;
|
|
35
|
+
let selector = entry
|
|
36
|
+
.get("selector")
|
|
37
|
+
.and_then(Value::as_str)
|
|
38
|
+
.ok_or_else(|| invalid("Extraction entry requires a selector"))?;
|
|
39
|
+
let all = entry.get("all").and_then(Value::as_bool).unwrap_or(false);
|
|
40
|
+
let value = entry
|
|
41
|
+
.get("value")
|
|
42
|
+
.ok_or_else(|| invalid("Extraction entry requires a value"))?;
|
|
43
|
+
let ids = document.select_ids(selector, contexts)?;
|
|
44
|
+
let read = |id: u32| -> Result<Value> {
|
|
45
|
+
match value.get("kind").and_then(Value::as_str) {
|
|
46
|
+
Some("map") => extract(document, entries(value)?, Some(&[id]), depth + 1),
|
|
47
|
+
Some("property") => {
|
|
48
|
+
let name = value
|
|
49
|
+
.get("name")
|
|
50
|
+
.and_then(Value::as_str)
|
|
51
|
+
.ok_or_else(|| invalid("Extraction property requires a name"))?;
|
|
52
|
+
if name == "textContent" {
|
|
53
|
+
return Ok(Value::String(document.text_ids(&[id])));
|
|
54
|
+
}
|
|
55
|
+
if matches!(name, "id" | "class" | "title" | "alt")
|
|
56
|
+
|| name.starts_with("data-")
|
|
57
|
+
|| name.starts_with("aria-")
|
|
58
|
+
|| matches!(name, "href" | "src")
|
|
59
|
+
&& document.options.get("baseURI").is_none()
|
|
60
|
+
{
|
|
61
|
+
return Ok(document
|
|
62
|
+
.attribute(id, name)
|
|
63
|
+
.map(Value::String)
|
|
64
|
+
.unwrap_or(Value::Null));
|
|
65
|
+
}
|
|
66
|
+
document.ensure_arena()?;
|
|
67
|
+
let mut arena = document.arena.borrow_mut();
|
|
68
|
+
let arena = arena
|
|
69
|
+
.as_mut()
|
|
70
|
+
.ok_or_else(|| invalid("Missing native document arena"))?;
|
|
71
|
+
if matches!(name, "name" | "type" | "nodeType") {
|
|
72
|
+
return Ok(match arena.node(id) {
|
|
73
|
+
Some(node) if name == "name" => {
|
|
74
|
+
node.name.clone().map(Value::String).unwrap_or(Value::Null)
|
|
75
|
+
}
|
|
76
|
+
Some(node) if name == "type" => Value::String(node.kind.clone()),
|
|
77
|
+
Some(_) => json!(1),
|
|
78
|
+
None => Value::Null,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
crate::attributes::execute(
|
|
82
|
+
arena,
|
|
83
|
+
"prop",
|
|
84
|
+
&json!({ "ids": [id], "name": name }),
|
|
85
|
+
)?
|
|
86
|
+
.ok_or_else(|| invalid("Unsupported extraction property"))
|
|
87
|
+
}
|
|
88
|
+
_ => Err(invalid("Unknown extraction value kind")),
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
if all {
|
|
92
|
+
let mut values = Vec::new();
|
|
93
|
+
for id in ids {
|
|
94
|
+
match read(id)? {
|
|
95
|
+
Value::Null => {}
|
|
96
|
+
Value::Array(items) => values.extend(items),
|
|
97
|
+
item => values.push(item),
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
result.insert(key.into(), Value::Array(values));
|
|
101
|
+
} else if let Some(id) = ids.first() {
|
|
102
|
+
let item = read(*id)?;
|
|
103
|
+
if !item.is_null() {
|
|
104
|
+
result.insert(key.into(), item);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
Ok(Value::Object(result))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
pub(crate) fn execute(document: &NativeDocument, plan: &Value) -> Result<Value> {
|
|
112
|
+
if plan.get("version").and_then(Value::as_u64) != Some(1) {
|
|
113
|
+
return Err(invalid("Unsupported extraction plan version"));
|
|
114
|
+
}
|
|
115
|
+
extract(document, entries(plan)?, None, 0)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
fn extract_arena(
|
|
119
|
+
arena: &mut crate::arena::DomArena,
|
|
120
|
+
plan: &[Value],
|
|
121
|
+
contexts: &[u32],
|
|
122
|
+
depth: usize,
|
|
123
|
+
) -> Result<Value> {
|
|
124
|
+
if depth > 64 {
|
|
125
|
+
return Err(invalid("Extraction plan nesting exceeds 64 levels"));
|
|
126
|
+
}
|
|
127
|
+
let mut result = Map::new();
|
|
128
|
+
for entry in plan {
|
|
129
|
+
let key = entry
|
|
130
|
+
.get("key")
|
|
131
|
+
.and_then(Value::as_str)
|
|
132
|
+
.ok_or_else(|| invalid("Extraction entry requires a key"))?;
|
|
133
|
+
let selector = entry
|
|
134
|
+
.get("selector")
|
|
135
|
+
.and_then(Value::as_str)
|
|
136
|
+
.ok_or_else(|| invalid("Extraction entry requires a selector"))?;
|
|
137
|
+
let all = entry.get("all").and_then(Value::as_bool).unwrap_or(false);
|
|
138
|
+
let value = entry
|
|
139
|
+
.get("value")
|
|
140
|
+
.ok_or_else(|| invalid("Extraction entry requires a value"))?;
|
|
141
|
+
let ids = crate::selector::select(arena, selector, Some(contexts))?;
|
|
142
|
+
let mut read = |id| -> Result<Value> {
|
|
143
|
+
match value.get("kind").and_then(Value::as_str) {
|
|
144
|
+
Some("map") => extract_arena(arena, entries(value)?, &[id], depth + 1),
|
|
145
|
+
Some("property") => {
|
|
146
|
+
let name = value
|
|
147
|
+
.get("name")
|
|
148
|
+
.and_then(Value::as_str)
|
|
149
|
+
.ok_or_else(|| invalid("Extraction property requires a name"))?;
|
|
150
|
+
if matches!(name, "name" | "type" | "nodeType") {
|
|
151
|
+
return Ok(match arena.node(id) {
|
|
152
|
+
Some(node) if name == "name" => {
|
|
153
|
+
node.name.clone().map(Value::String).unwrap_or(Value::Null)
|
|
154
|
+
}
|
|
155
|
+
Some(node) if name == "type" => Value::String(node.kind.clone()),
|
|
156
|
+
Some(_) => json!(1),
|
|
157
|
+
None => Value::Null,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
crate::attributes::execute(arena, "prop", &json!({"ids":[id],"name":name}))?
|
|
161
|
+
.ok_or_else(|| invalid("Unsupported extraction property"))
|
|
162
|
+
}
|
|
163
|
+
_ => Err(invalid("Unknown extraction value kind")),
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
if all {
|
|
167
|
+
let mut values = Vec::new();
|
|
168
|
+
for id in ids {
|
|
169
|
+
match read(id)? {
|
|
170
|
+
Value::Null => {}
|
|
171
|
+
Value::Array(items) => values.extend(items),
|
|
172
|
+
value => values.push(value),
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
result.insert(key.into(), Value::Array(values));
|
|
176
|
+
} else if let Some(id) = ids.first() {
|
|
177
|
+
let value = read(*id)?;
|
|
178
|
+
if !value.is_null() {
|
|
179
|
+
result.insert(key.into(), value);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
Ok(Value::Object(result))
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Use the caller's active options and contexts for pure prefixes around callbacks. */
|
|
187
|
+
pub(crate) fn execute_arena(
|
|
188
|
+
arena: &mut crate::arena::DomArena,
|
|
189
|
+
plan: &Value,
|
|
190
|
+
contexts: &[u32],
|
|
191
|
+
) -> Result<Value> {
|
|
192
|
+
if plan.get("version").and_then(Value::as_u64) != Some(1) {
|
|
193
|
+
return Err(invalid("Unsupported extraction plan version"));
|
|
194
|
+
}
|
|
195
|
+
extract_arena(arena, entries(plan)?, contexts, 0)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
#[cfg(test)]
|
|
199
|
+
mod tests {
|
|
200
|
+
use super::*;
|
|
201
|
+
|
|
202
|
+
#[test]
|
|
203
|
+
fn nested_maps_preserve_native_property_and_missing_value_semantics() {
|
|
204
|
+
let document = NativeDocument::parse_options(
|
|
205
|
+
"<section><a href='/page'>A<b>B</b></a><input checked></section><section><p>C</p></section>",
|
|
206
|
+
true,
|
|
207
|
+
json!({ "baseURI": "https://example.test/base/" }),
|
|
208
|
+
None,
|
|
209
|
+
);
|
|
210
|
+
let plan = json!({"version":1,"entries":[
|
|
211
|
+
{"key":"rows","selector":"section","all":true,"value":{"kind":"map","entries":[
|
|
212
|
+
{"key":"link","selector":"a","all":false,"value":{"kind":"property","name":"href"}},
|
|
213
|
+
{"key":"content","selector":"a","all":false,"value":{"kind":"property","name":"innerHTML"}},
|
|
214
|
+
{"key":"checked","selector":"input","all":true,"value":{"kind":"property","name":"checked"}},
|
|
215
|
+
{"key":"missing","selector":".absent","all":false,"value":{"kind":"property","name":"textContent"}}
|
|
216
|
+
]}}
|
|
217
|
+
]});
|
|
218
|
+
assert_eq!(
|
|
219
|
+
execute(&document, &plan).unwrap(),
|
|
220
|
+
json!({"rows":[
|
|
221
|
+
{"link":"https://example.test/page","content":"A<b>B</b>","checked":[true]},
|
|
222
|
+
{"checked":[]}
|
|
223
|
+
]})
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
}
|
package/native/src/gc.rs
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
//! Reclaim detached native components that have no live JavaScript handles.
|
|
2
|
+
use std::collections::HashMap;
|
|
3
|
+
|
|
4
|
+
use crate::arena::DomArena;
|
|
5
|
+
|
|
6
|
+
#[derive(Debug)]
|
|
7
|
+
pub(crate) struct GcStats {
|
|
8
|
+
pub live: u32,
|
|
9
|
+
pub slots: u32,
|
|
10
|
+
pub retained: u32,
|
|
11
|
+
pub reclaimed: u32,
|
|
12
|
+
pub free: Vec<u32>,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
#[cfg(test)]
|
|
16
|
+
pub(crate) fn collect(arena: &mut DomArena, pins: &HashMap<u32, usize>) -> GcStats {
|
|
17
|
+
collect_managed(arena, pins, true)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
pub(crate) fn collect_managed(
|
|
21
|
+
arena: &mut DomArena,
|
|
22
|
+
pins: &HashMap<u32, usize>,
|
|
23
|
+
keep_root: bool,
|
|
24
|
+
) -> GcStats {
|
|
25
|
+
let mut marked = vec![false; arena.nodes.len()];
|
|
26
|
+
let mut stack = if keep_root { vec![0] } else { Vec::new() };
|
|
27
|
+
stack.extend(
|
|
28
|
+
pins.iter()
|
|
29
|
+
.filter_map(|(id, count)| (*count > 0).then_some(*id)),
|
|
30
|
+
);
|
|
31
|
+
// Following both directions marks the entire parent/child component. A
|
|
32
|
+
// held descendant keeps its detached parent and siblings navigable. The
|
|
33
|
+
// visited bitmap also bounds work for cyclic or inconsistent user graphs.
|
|
34
|
+
while let Some(id) = stack.pop() {
|
|
35
|
+
let Some(node) = arena.nodes.get(id as usize) else {
|
|
36
|
+
continue;
|
|
37
|
+
};
|
|
38
|
+
if marked[id as usize] || node.kind == "released" {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
marked[id as usize] = true;
|
|
42
|
+
stack.extend(node.parent);
|
|
43
|
+
stack.extend(node.children.iter().copied());
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let retained = pins
|
|
47
|
+
.iter()
|
|
48
|
+
.filter(|(id, count)| **count > 0 && marked.get(**id as usize) == Some(&true))
|
|
49
|
+
.count() as u32;
|
|
50
|
+
let mut reclaimed = 0;
|
|
51
|
+
for (index, node) in arena.nodes.iter_mut().enumerate() {
|
|
52
|
+
if marked[index] || node.kind == "released" {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
reclaimed += 1;
|
|
56
|
+
node.kind = "released".into();
|
|
57
|
+
node.name = None;
|
|
58
|
+
node.data = None;
|
|
59
|
+
node.attribs = Default::default();
|
|
60
|
+
node.children = Vec::new();
|
|
61
|
+
node.extras = Default::default();
|
|
62
|
+
node.parent = None;
|
|
63
|
+
node.prev = None;
|
|
64
|
+
node.next = None;
|
|
65
|
+
}
|
|
66
|
+
while arena
|
|
67
|
+
.nodes
|
|
68
|
+
.last()
|
|
69
|
+
.is_some_and(|node| node.kind == "released")
|
|
70
|
+
{
|
|
71
|
+
arena.nodes.pop();
|
|
72
|
+
}
|
|
73
|
+
if arena.nodes.capacity() > arena.nodes.len().saturating_mul(2).saturating_add(64) {
|
|
74
|
+
arena.nodes.shrink_to_fit();
|
|
75
|
+
}
|
|
76
|
+
let free = arena
|
|
77
|
+
.nodes
|
|
78
|
+
.iter()
|
|
79
|
+
.filter(|node| node.kind == "released")
|
|
80
|
+
.map(|node| node.id)
|
|
81
|
+
.collect::<Vec<_>>();
|
|
82
|
+
GcStats {
|
|
83
|
+
live: arena.nodes.len() as u32 - free.len() as u32,
|
|
84
|
+
slots: arena.nodes.len() as u32,
|
|
85
|
+
retained,
|
|
86
|
+
reclaimed,
|
|
87
|
+
free,
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#[cfg(test)]
|
|
92
|
+
mod tests {
|
|
93
|
+
use super::*;
|
|
94
|
+
use crate::arena::DomNode;
|
|
95
|
+
use serde_json::json;
|
|
96
|
+
|
|
97
|
+
fn fixture() -> DomArena {
|
|
98
|
+
let mut nodes = vec![DomNode::new("root")];
|
|
99
|
+
nodes.extend((0..5).map(|_| DomNode::new("tag")));
|
|
100
|
+
nodes[0].children = vec![1, 5];
|
|
101
|
+
nodes[2].children = vec![3, 4];
|
|
102
|
+
nodes[2].name = Some("detached".into());
|
|
103
|
+
nodes[3].data = Some("payload".repeat(100));
|
|
104
|
+
nodes[3].attribs.insert("key".into(), json!("value"));
|
|
105
|
+
nodes[3].extras.insert("data".into(), json!({"held":true}));
|
|
106
|
+
DomArena::from_records(nodes, json!({}))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#[test]
|
|
110
|
+
fn held_detached_child_preserves_its_parent_and_sibling() {
|
|
111
|
+
let mut arena = fixture();
|
|
112
|
+
let stats = collect(&mut arena, &HashMap::from([(3, 2)]));
|
|
113
|
+
assert_eq!(
|
|
114
|
+
(stats.live, stats.slots, stats.retained, stats.reclaimed),
|
|
115
|
+
(6, 6, 1, 0)
|
|
116
|
+
);
|
|
117
|
+
assert!(stats.free.is_empty());
|
|
118
|
+
assert_eq!(arena.nodes[3].parent, Some(2));
|
|
119
|
+
assert_eq!(arena.nodes[3].next, Some(4));
|
|
120
|
+
assert_eq!(arena.nodes[2].children, vec![3, 4]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
#[test]
|
|
124
|
+
fn unpinned_payloads_are_cleared_and_trailing_slots_trimmed() {
|
|
125
|
+
let mut arena = fixture();
|
|
126
|
+
let stats = collect(&mut arena, &HashMap::new());
|
|
127
|
+
assert_eq!((stats.live, stats.slots, stats.reclaimed), (3, 6, 3));
|
|
128
|
+
assert_eq!(stats.free, vec![2, 3, 4]);
|
|
129
|
+
let node = &arena.nodes[3];
|
|
130
|
+
assert_eq!(node.kind, "released");
|
|
131
|
+
assert!(node.data.is_none() && node.name.is_none());
|
|
132
|
+
assert!(node.attribs.is_empty() && node.children.is_empty() && node.extras.is_empty());
|
|
133
|
+
assert!(node.parent.is_none() && node.prev.is_none() && node.next.is_none());
|
|
134
|
+
arena.detach(5);
|
|
135
|
+
let stats = collect(&mut arena, &HashMap::from([(5, 0)]));
|
|
136
|
+
assert_eq!(
|
|
137
|
+
(stats.live, stats.slots, stats.retained, stats.reclaimed),
|
|
138
|
+
(2, 2, 0, 1)
|
|
139
|
+
);
|
|
140
|
+
assert!(stats.free.is_empty());
|
|
141
|
+
assert_eq!(collect(&mut arena, &HashMap::new()).reclaimed, 0);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
#[test]
|
|
145
|
+
fn cyclic_components_are_marked_once() {
|
|
146
|
+
let mut arena = fixture();
|
|
147
|
+
arena.nodes[2].parent = Some(3);
|
|
148
|
+
arena.nodes[3].children.push(2);
|
|
149
|
+
let stats = collect(&mut arena, &HashMap::from([(3, 1)]));
|
|
150
|
+
assert_eq!((stats.live, stats.retained, stats.reclaimed), (6, 1, 0));
|
|
151
|
+
let stats = collect(&mut arena, &HashMap::new());
|
|
152
|
+
assert_eq!(stats.reclaimed, 3);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#[test]
|
|
156
|
+
fn managed_detached_handle_does_not_retain_the_original_document() {
|
|
157
|
+
let mut arena = fixture();
|
|
158
|
+
let stats = collect_managed(&mut arena, &HashMap::from([(3, 1)]), false);
|
|
159
|
+
assert_eq!((stats.live, stats.retained, stats.reclaimed), (3, 1, 3));
|
|
160
|
+
assert_eq!(arena.nodes[0].kind, "released");
|
|
161
|
+
assert_eq!(arena.nodes[1].kind, "released");
|
|
162
|
+
assert_eq!(arena.nodes[2].name.as_deref(), Some("detached"));
|
|
163
|
+
assert_eq!(arena.nodes[3].parent, Some(2));
|
|
164
|
+
assert_eq!(arena.nodes[3].next, Some(4));
|
|
165
|
+
}
|
|
166
|
+
}
|