Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(runtime): share text codec state and finalizers

ldm0c1b9ccd0cad2
diff --git a/moli-renderer-v8/src/common_runtime.rs b/moli-renderer-v8/src/common_runtime.rs
index 1cb62d3d0..872ef04af 100644
--- a/moli-renderer-v8/src/common_runtime.rs
+++ b/moli-renderer-v8/src/common_runtime.rs
@@ -15,6 +15,7 @@ use std::{
use crate::{
native_bridge::JsContextHost,
+ text_codec::TextCodecStore,
util::{ContextHostLifecycle, page_context_is_disconnected},
v8_finalizer::V8FinalizerRegistry,
worker::WorkerGlobalState,
@@ -23,9 +24,20 @@ use crate::{
#[derive(Default)]
pub(crate) struct CommonRuntimeState {
pub(crate) finalizers: RefCell<V8FinalizerRegistry>,
+ pub(crate) text_codecs: TextCodecStore,
}
impl CommonRuntimeState {
+ /// Resolve native components without borrowing the Window or Worker
+ /// host. Retained backing state and callback execution permission remain
+ /// separate: asynchronous callbacks must still revalidate their identities.
+ pub(crate) fn from_scope(scope: &mut v8::PinScope<'_, '_>) -> Option<Rc<Self>> {
+ let context = scope.get_current_context();
+ let binding = context.get_slot::<CommonRuntimeBinding>()?;
+ let (runtime, _) = binding.resolve_owner(context)?;
+ Some(runtime)
+ }
+
pub(crate) fn clear_for_context_teardown(&self) {
// Cleanup may reenter native components. Drop the component borrow
// before executing any cleanup, just as registration releases owner
@@ -119,7 +131,7 @@ impl CommonRuntimeBinding {
) -> Option<(Rc<Self>, RuntimeExecutionContext)> {
let context = scope.get_current_context();
let binding = context.get_slot::<Self>()?;
- let owner = binding.resolve_owner(context)?;
+ let (_, owner) = binding.resolve_owner(context)?;
Some((binding, owner))
}
@@ -130,9 +142,9 @@ impl CommonRuntimeBinding {
fn resolve_owner(
&self,
context: v8::Local<'_, v8::Context>,
- ) -> Option<RuntimeExecutionContext> {
- self.runtime()?;
- match &self.owner {
+ ) -> Option<(Rc<CommonRuntimeState>, RuntimeExecutionContext)> {
+ let runtime = self.runtime()?;
+ let owner = match &self.owner {
RuntimeOwner::Window {
host_ptr,
lifecycle,
@@ -142,13 +154,14 @@ impl CommonRuntimeBinding {
{
return None;
}
- Some(RuntimeExecutionContext::Window(*host_ptr))
+ RuntimeExecutionContext::Window(*host_ptr)
}
RuntimeOwner::Worker { state, .. } => {
state.upgrade()?;
- Some(RuntimeExecutionContext::Worker)
+ RuntimeExecutionContext::Worker
}
- }
+ };
+ Some((runtime, owner))
}
pub(crate) fn execution_context(
@@ -160,7 +173,7 @@ impl CommonRuntimeBinding {
if !std::ptr::eq(self, installed.as_ref()) {
return None;
}
- self.resolve_owner(context)
+ self.resolve_owner(context).map(|(_, owner)| owner)
}
/// Recheck the exact Context and owner immediately before author code.
@@ -236,6 +249,10 @@ mod tests {
let (binding, owner) = CommonRuntimeBinding::from_scope(scope).unwrap();
assert!(matches!(owner, RuntimeExecutionContext::Window(ptr) if ptr == host));
assert!(Rc::ptr_eq(&binding.runtime().unwrap(), &common));
+ assert!(Rc::ptr_eq(
+ &CommonRuntimeState::from_scope(scope).unwrap(),
+ &common
+ ));
assert!(binding.callback_execution_context(scope).is_some());
binding
};
@@ -253,6 +270,7 @@ mod tests {
assert!(!Rc::ptr_eq(&binding.runtime().unwrap(), &common));
other_lifecycle.set(ContextHostLifecycle::Destroyed);
assert!(CommonRuntimeBinding::from_scope(scope).is_none());
+ assert!(CommonRuntimeState::from_scope(scope).is_none());
// A previously resolved owner is not a cached execution permit.
assert!(binding.callback_execution_context(scope).is_none());
}
diff --git a/moli-renderer-v8/src/context_bootstrap/constructors/text_codecs.rs b/moli-renderer-v8/src/context_bootstrap/constructors/text_codecs.rs
index 6bcca5cfd..093c8858a 100644
--- a/moli-renderer-v8/src/context_bootstrap/constructors/text_codecs.rs
+++ b/moli-renderer-v8/src/context_bootstrap/constructors/text_codecs.rs
@@ -415,15 +415,8 @@ fn init_text_decoder_state(
fatal: bool,
ignore_bom: bool,
) {
- if let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) {
- unsafe { &mut *host_ptr }
- .text_codecs_mut()
- .init_decoder(scope, decoder, encoding, fatal, ignore_bom);
- return;
- }
- if let Some(worker_state) = crate::worker::get_worker_state(scope) {
- worker_state
- .borrow_mut()
+ if let Some(common) = crate::common_runtime::CommonRuntimeState::from_scope(scope) {
+ common
.text_codecs
.init_decoder(scope, decoder, encoding, fatal, ignore_bom);
}
@@ -435,17 +428,6 @@ fn decode_text_decoder_state(
bytes: &[u8],
stream: bool,
) -> Option<Result<String, TextDecodeError>> {
- if let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) {
- return Some(
- unsafe { &mut *host_ptr }
- .text_codecs_mut()
- .decode(decoder_id, bytes, stream),
- );
- }
- crate::worker::get_worker_state(scope).map(|worker_state| {
- worker_state
- .borrow_mut()
- .text_codecs
- .decode(decoder_id, bytes, stream)
- })
+ let common = crate::common_runtime::CommonRuntimeState::from_scope(scope)?;
+ Some(common.text_codecs.decode(decoder_id, bytes, stream))
}
diff --git a/moli-renderer-v8/src/context_bootstrap/stream_adapter/writable.rs b/moli-renderer-v8/src/context_bootstrap/stream_adapter/writable.rs
index 9855208d0..ca1a1a570 100644
--- a/moli-renderer-v8/src/context_bootstrap/stream_adapter/writable.rs
+++ b/moli-renderer-v8/src/context_bootstrap/stream_adapter/writable.rs
@@ -2287,15 +2287,13 @@ fn decode_text_decoder_stream<'s>(
bytes: &[u8],
stream_decode: bool,
) -> Result<String, TextDecodeError> {
- let Some(host_ptr) = context_host_ptr_from_global_bridge(scope) else {
+ let Some(common) = crate::common_runtime::CommonRuntimeState::from_scope(scope) else {
return Ok(String::new());
};
let Some(decoder_id) = text_decoder_stream_decoder_id(scope, stream) else {
return Ok(String::new());
};
- unsafe { &mut *host_ptr }
- .text_codecs_mut()
- .decode(decoder_id, bytes, stream_decode)
+ common.text_codecs.decode(decoder_id, bytes, stream_decode)
}
fn text_decoder_stream_decoder_id<'s>(
diff --git a/moli-renderer-v8/src/context_bootstrap/streams/constructors.rs b/moli-renderer-v8/src/context_bootstrap/streams/constructors.rs
index 00ea081ea..f1681aefc 100644
--- a/moli-renderer-v8/src/context_bootstrap/streams/constructors.rs
+++ b/moli-renderer-v8/src/context_bootstrap/streams/constructors.rs
@@ -391,11 +391,11 @@ pub(in crate::context_bootstrap) fn text_decoder_stream_constructor_callback<'s>
0.0,
None,
);
- if let Some(host_ptr) = context_host_ptr_from_global_bridge(scope)
- && let Some(writable) = object_property_as_object(scope, args.this(), "writable")
+ if let Some(writable) = object_property_as_object(scope, args.this(), "writable")
+ && let Some(common) = crate::common_runtime::CommonRuntimeState::from_scope(scope)
{
- unsafe { &mut *host_ptr }
- .text_codecs_mut()
+ common
+ .text_codecs
.init_decoder(scope, writable, encoding, fatal, ignore_bom);
}
TextDecoderStreamObjectDeclaration::new(encoding_name, fatal, ignore_bom)
diff --git a/moli-renderer-v8/src/native_bridge/context_host/core.rs b/moli-renderer-v8/src/native_bridge/context_host/core.rs
index 1cb330e59..c6808ee11 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/core.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/core.rs
@@ -378,7 +378,6 @@ impl JsContextHost {
custom_element_registry_associations: LinkedHashMap::new(),
next_scoped_custom_elements_registry_id: 1,
observers: ObserverStore::default(),
- text_codecs: TextCodecStore::default(),
child_browsing_contexts: IndexMap::new(),
frame_owner_store,
frame_parser_classic_scripts: FrameParserClassicScriptRunnerStore::default(),
@@ -2457,10 +2456,6 @@ impl JsContextHost {
false
}
- pub(crate) fn text_codecs_mut(&mut self) -> &mut TextCodecStore {
- &mut self.text_codecs
- }
-
pub(crate) fn renderer_backend_node_id_for_live_handle(
&mut self,
handle: DomHandle,
diff --git a/moli-renderer-v8/src/native_bridge/context_host/mod.rs b/moli-renderer-v8/src/native_bridge/context_host/mod.rs
index b5b44ffc4..51662f75a 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/mod.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/mod.rs
@@ -35,7 +35,6 @@ use crate::{
script_provenance::CompiledStringProvenance,
shared_worker_runtime::SharedWorkerClientEndpointOwner,
style_engine::MoliStyleEngine,
- text_codec::TextCodecStore,
types::{
BroadcastChannelId, DedicatedWorkerId, ImageRequestKey,
InFlightWorkerSubresourceFetchState, MessagePortId, NetworkBodySourceId,
@@ -855,7 +854,6 @@ pub(crate) struct JsContextHost {
LinkedHashMap<DomHandle, CustomElementRegistryAssociation>,
next_scoped_custom_elements_registry_id: u64,
observers: ObserverStore,
- text_codecs: TextCodecStore,
// Key order is browsing-context insertion order and therefore frame-tree sibling order.
child_browsing_contexts: IndexMap<DomHandle, ChildBrowsingContextEntry>,
frame_owner_store: FrameOwnerStore,
diff --git a/moli-renderer-v8/src/script_vm/tests/webidl_fetch/webidl_and_transform_streams.rs b/moli-renderer-v8/src/script_vm/tests/webidl_fetch/webidl_and_transform_streams.rs
index 1260f42ca..5a67c9979 100644
--- a/moli-renderer-v8/src/script_vm/tests/webidl_fetch/webidl_and_transform_streams.rs
+++ b/moli-renderer-v8/src/script_vm/tests/webidl_fetch/webidl_and_transform_streams.rs
@@ -1,5 +1,19 @@
use super::*;
+#[test]
+fn text_codec_runtime_preserves_streaming_bom_and_fatal_errors() {
+ let mut vm = new_storage_test_vm("https://text-codec-runtime.test/");
+ let source = format!(
+ "({}).then(result => globalThis.__textCodecRuntimeResult = result, error => globalThis.__textCodecRuntimeResult = {{ error: String(error) }});",
+ include_str!("../../../../tests/fixtures/text-codec-runtime.js"),
+ );
+ vm.eval(&source).expect("queue shared text codec probe");
+ assert_eq!(
+ vm.eval("JSON.stringify(__textCodecRuntimeResult)").unwrap(),
+ r#"{"split":"€","utf8":"hé","legacy":"太平洋","bomKept":true,"fatal":["TypeError","TypeError"]}"#,
+ );
+}
+
#[test]
fn webidl_required_nullable_dictionary_rejects_undefined_member() {
let vm = new_storage_test_vm("https://webidl-dictionary-nullable-required.test/");
diff --git a/moli-renderer-v8/src/text_codec.rs b/moli-renderer-v8/src/text_codec.rs
index d226dd1c2..5fe8ccd92 100644
--- a/moli-renderer-v8/src/text_codec.rs
+++ b/moli-renderer-v8/src/text_codec.rs
@@ -9,12 +9,10 @@ use parking_lot::Mutex;
use super::util::{get_private_value, set_private_value};
const TEXT_DECODER_ID_SLOT: &str = "__lmTextDecoderId";
-const DECODER_WEAK_COMPACTION_INTERVAL: usize = 64;
+#[derive(Default)]
pub(super) struct TextCodecStore {
state: Arc<Mutex<TextCodecStoreState>>,
- decoder_weaks: Vec<v8::Weak<v8::Object>>,
- decoder_weak_insertions_since_compaction: usize,
}
#[derive(Default)]
@@ -36,16 +34,6 @@ pub(super) enum TextDecodeError {
MalformedInput,
}
-impl Default for TextCodecStore {
- fn default() -> Self {
- Self {
- state: Arc::new(Mutex::new(TextCodecStoreState::default())),
- decoder_weaks: Vec::new(),
- decoder_weak_insertions_since_compaction: 0,
- }
- }
-}
-
impl TextDecodeError {
pub(super) fn message(&self) -> &'static str {
match self {
@@ -56,7 +44,7 @@ impl TextDecodeError {
impl TextCodecStore {
pub(super) fn init_decoder(
- &mut self,
+ &self,
scope: &mut v8::PinScope<'_, '_>,
decoder: v8::Local<'_, v8::Object>,
encoding: &'static Encoding,
@@ -78,12 +66,15 @@ impl TextCodecStore {
TEXT_DECODER_ID_SLOT,
v8::Number::new(scope, decoder_id as f64).into(),
);
- self.track_decoder_lifetime(scope, decoder, decoder_id);
+ let state = Arc::downgrade(&self.state);
+ crate::v8_finalizer::track_context_owned_v8_finalizer(scope, decoder, move || {
+ remove_decoder_state(&state, decoder_id);
+ });
decoder_id
}
pub(super) fn decode(
- &mut self,
+ &self,
decoder_id: u32,
bytes: &[u8],
stream: bool,
@@ -105,32 +96,6 @@ impl TextCodecStore {
.filter(|value| value.is_finite() && *value >= 1.0)
.map(|value| value as u32)
}
-
- fn track_decoder_lifetime(
- &mut self,
- scope: &mut v8::PinScope<'_, '_>,
- decoder: v8::Local<'_, v8::Object>,
- decoder_id: u32,
- ) {
- let state = Arc::downgrade(&self.state);
- let weak = v8::Weak::with_guaranteed_finalizer(
- scope,
- decoder,
- Box::new(move || remove_decoder_state(&state, decoder_id)),
- );
- self.decoder_weaks.push(weak);
- self.decoder_weak_insertions_since_compaction = self
- .decoder_weak_insertions_since_compaction
- .saturating_add(1);
- if self.decoder_weak_insertions_since_compaction >= DECODER_WEAK_COMPACTION_INTERVAL {
- self.compact_decoder_weaks();
- }
- }
-
- fn compact_decoder_weaks(&mut self) {
- self.decoder_weaks.retain(|weak| !weak.is_empty());
- self.decoder_weak_insertions_since_compaction = 0;
- }
}
impl TextCodecStoreState {
@@ -249,6 +214,75 @@ fn new_encoding_decoder(encoding: &'static Encoding, ignore_bom: bool) -> Decode
mod tests {
use super::*;
+ #[test]
+ fn common_text_codec_state_releases_collected_and_teardown_decoders() {
+ use crate::{
+ common_runtime::{CommonRuntimeBinding, CommonRuntimeState},
+ native_bridge::JsContextHost,
+ util::ContextHostLifecycle,
+ };
+ use std::{cell::Cell, rc::Rc};
+
+ crate::ensure_v8_for_test();
+ let common = Rc::new(CommonRuntimeState::default());
+ let lifecycle = Rc::new(Cell::new(ContextHostLifecycle::Active));
+ let mut isolate = v8::Isolate::new(Default::default());
+ let (context, retained_decoder, retained_id) = {
+ let scope = std::pin::pin!(v8::HandleScope::new(&mut isolate));
+ let scope = &mut scope.init();
+ let context = v8::Context::new(scope, Default::default());
+ // Component lookup needs an owner identity, but never dereferences
+ // the opaque host address in this native backing lifetime test.
+ CommonRuntimeBinding::install_window(
+ context,
+ &common,
+ std::ptr::NonNull::<JsContextHost>::dangling().as_ptr(),
+ &lifecycle,
+ );
+ let scope = &mut v8::ContextScope::new(scope, context);
+ let collected = v8::Object::new(scope);
+ common
+ .text_codecs
+ .init_decoder(scope, collected, encoding_rs::UTF_8, false, false);
+ let retained = v8::Object::new(scope);
+ let id =
+ common
+ .text_codecs
+ .init_decoder(scope, retained, encoding_rs::UTF_8, false, false);
+ assert_eq!(common.text_codecs.state.lock().decoders.len(), 2);
+ (
+ v8::Global::new(scope, context),
+ v8::Global::new(scope, retained),
+ id,
+ )
+ };
+ for _ in 0..2 {
+ isolate.low_memory_notification();
+ }
+ assert_eq!(common.text_codecs.state.lock().decoders.len(), 1);
+ assert_eq!(
+ common
+ .text_codecs
+ .decode(retained_id, &[0xE2, 0x82], true)
+ .unwrap(),
+ ""
+ );
+ assert_eq!(
+ common
+ .text_codecs
+ .decode(retained_id, &[0xAC], false)
+ .unwrap(),
+ "€"
+ );
+ common.clear_for_context_teardown();
+ assert_eq!(common.text_codecs.state.lock().decoders.len(), 0);
+ assert_eq!(common.finalizer_count(), 0);
+ common.clear_for_context_teardown();
+ drop(retained_decoder);
+ drop(context);
+ drop(isolate);
+ }
+
#[test]
fn text_decoder_state_streams_split_utf8_sequences() {
let mut state = TextDecoderState::new(encoding_rs::UTF_8, false, false);
diff --git a/moli-renderer-v8/src/v8_finalizer.rs b/moli-renderer-v8/src/v8_finalizer.rs
index 701d46ae4..2cec6a1b9 100644
--- a/moli-renderer-v8/src/v8_finalizer.rs
+++ b/moli-renderer-v8/src/v8_finalizer.rs
@@ -94,8 +94,7 @@ pub(crate) fn track_context_owned_v8_finalizer(
) {
let cleanup: FinalizerCleanup = Box::new(cleanup);
- let common = crate::common_runtime::CommonRuntimeBinding::from_scope(scope)
- .and_then(|(binding, _)| binding.runtime())
+ let common = crate::common_runtime::CommonRuntimeState::from_scope(scope)
.expect("V8 finalizer object must belong to a page or worker context");
let entry = ContextOwnedV8Finalizer::new(scope, object, cleanup);
let retired = common.finalizers.borrow_mut().push(entry);
diff --git a/moli-renderer-v8/src/worker/global_scope/mod.rs b/moli-renderer-v8/src/worker/global_scope/mod.rs
index 037d45be2..0dec4f57b 100644
--- a/moli-renderer-v8/src/worker/global_scope/mod.rs
+++ b/moli-renderer-v8/src/worker/global_scope/mod.rs
@@ -105,7 +105,6 @@ use crate::runtime::{
ServiceWorkerShowNotificationResult, ServiceWorkerSyncGetTagsResult,
ServiceWorkerSyncRegistrationResult,
};
-use crate::text_codec::TextCodecStore;
use crate::types::{BroadcastChannelId, DedicatedWorkerId, MessagePortId, NetworkBodySourceId};
use crate::util::{
get_private_value, global_constructor_object, global_constructor_prototype, set_private_value,
diff --git a/moli-renderer-v8/src/worker/global_scope/state.rs b/moli-renderer-v8/src/worker/global_scope/state.rs
index d438acede..a33396eeb 100644
--- a/moli-renderer-v8/src/worker/global_scope/state.rs
+++ b/moli-renderer-v8/src/worker/global_scope/state.rs
@@ -89,9 +89,6 @@ pub(crate) struct WorkerGlobalState {
pub(in crate::worker) next_xhr_id: u32,
/// Worker-owned CSP report requests paused for Fetch domain request-stage interception.
pub(in crate::worker) pending_csp_reports: HashMap<u32, PendingWorkerCspReport>,
- /// Worker-local TextDecoder state. TextEncoder is stateless, but TextDecoder
- /// can stream and needs decoder state tied to this worker's isolate.
- pub(crate) text_codecs: TextCodecStore,
/// Worker-local AbortController/AbortSignal runtime state.
pub(in crate::worker) abort: Rc<RefCell<WorkerAbortStore>>,
/// Worker-thread view of the creator's browser-context/partition runtime.
diff --git a/moli-renderer-v8/src/worker/thread/mod.rs b/moli-renderer-v8/src/worker/thread/mod.rs
index 3e0ee37b7..07cc93917 100644
--- a/moli-renderer-v8/src/worker/thread/mod.rs
+++ b/moli-renderer-v8/src/worker/thread/mod.rs
@@ -1658,7 +1658,6 @@ async fn worker_main(
pending_xhrs: std::collections::HashMap::new(),
next_xhr_id: 0,
pending_csp_reports: std::collections::HashMap::new(),
- text_codecs: crate::text_codec::TextCodecStore::default(),
abort: Rc::new(RefCell::new(super::abort::WorkerAbortStore::default())),
worker_context_runtime,
service_worker_runtime,
diff --git a/moli-renderer-v8/src/worker/thread/tests/mod.rs b/moli-renderer-v8/src/worker/thread/tests/mod.rs
index 7b468fc75..b021c5490 100644
--- a/moli-renderer-v8/src/worker/thread/tests/mod.rs
+++ b/moli-renderer-v8/src/worker/thread/tests/mod.rs
@@ -1,6 +1,7 @@
mod abort_signal_events;
mod callback_execution;
mod error_reporting_guard;
+mod text_codecs;
use std::pin::pin;
use std::sync::Arc;
use std::time::Duration;
diff --git a/moli-renderer-v8/src/worker/thread/tests/text_codecs.rs b/moli-renderer-v8/src/worker/thread/tests/text_codecs.rs
new file mode 100644
index 000000000..dcd789f28
--- /dev/null
+++ b/moli-renderer-v8/src/worker/thread/tests/text_codecs.rs
@@ -0,0 +1,42 @@
+use super::*;
+
+#[tokio::test]
+async fn worker_close_preserves_decoder_backing_for_queued_microtasks() {
+ ensure_v8();
+ let mut handle = spawn_worker(
+ r#"
+ queueMicrotask(() => {
+ const decoder = new TextDecoder();
+ decoder.decode(new Uint8Array([0xE2, 0x82]), { stream: true });
+ postMessage(decoder.decode(new Uint8Array([0xAC])));
+ });
+ close();
+ "#
+ .into(),
+ "https://text-codec-runtime.test/after-close.js".into(),
+ );
+ let message = timeout(TIMEOUT, handle.recv())
+ .await
+ .expect("post-close text codec probe timed out")
+ .expect("worker closed before draining its codec microtask");
+ assert_eq!(expect_post_json(message), r#""€""#);
+ assert!(timeout(TIMEOUT, handle.recv()).await.unwrap().is_none());
+}
+
+#[tokio::test]
+async fn worker_text_codec_runtime_preserves_streaming_bom_and_fatal_errors() {
+ ensure_v8();
+ let source = format!(
+ "({}).then(result => {{ postMessage(result); close(); }}, error => {{ postMessage({{ error: String(error) }}); close(); }});",
+ include_str!("../../../../tests/fixtures/text-codec-runtime.js"),
+ );
+ let mut handle = spawn_worker(source, "https://text-codec-runtime.test/worker.js".into());
+ let message = timeout(TIMEOUT, handle.recv())
+ .await
+ .expect("worker text codec probe timed out")
+ .expect("worker closed before reporting text codec results");
+ assert_eq!(
+ expect_post_json(message),
+ r#"{"split":"€","utf8":"hé","legacy":"太平洋","bomKept":true,"fatal":["TypeError","TypeError"]}"#,
+ );
+}
diff --git a/moli-renderer-v8/tests/fixtures/text-codec-runtime.js b/moli-renderer-v8/tests/fixtures/text-codec-runtime.js
new file mode 100644
index 000000000..26f8f640b
--- /dev/null
+++ b/moli-renderer-v8/tests/fixtures/text-codec-runtime.js
@@ -0,0 +1,43 @@
+(async () => {
+ async function decodeStream(stream, chunks) {
+ const writer = stream.writable.getWriter();
+ const reader = stream.readable.getReader();
+ const writing = (async () => {
+ for (const chunk of chunks) await writer.write(new Uint8Array(chunk));
+ await writer.close();
+ })();
+ let text = "";
+ for (;;) {
+ const { value, done } = await reader.read();
+ if (done) break;
+ text += value;
+ }
+ await writing;
+ return text;
+ }
+
+ const decoder = new TextDecoder();
+ const split = decoder.decode(new Uint8Array([0xE2]), { stream: true })
+ + decoder.decode(new Uint8Array([0x82]), { stream: true })
+ + decoder.decode(new Uint8Array([0xAC]));
+ const utf8 = await decodeStream(new TextDecoderStream(), [
+ [0xEF], [0xBB, 0xBF, 0x68, 0xC3], [0xA9]
+ ]);
+ const legacy = await decodeStream(new TextDecoderStream("gbk"), [
+ [0xCC], [0xAB, 0xC6, 0xBD, 0xD1, 0xF3]
+ ]);
+ const bom = await decodeStream(new TextDecoderStream("utf-8", { ignoreBOM: true }), [
+ [0xEF], [0xBB, 0xBF, 0x41]
+ ]);
+
+ const fatalStream = new TextDecoderStream("utf-8", { fatal: true });
+ const writer = fatalStream.writable.getWriter();
+ const reader = fatalStream.readable.getReader();
+ writer.closed.catch(() => {});
+ reader.closed.catch(() => {});
+ const fatal = await Promise.all([
+ writer.write(new Uint8Array([0xFF])).then(() => "unexpected", error => error.name),
+ reader.read().then(() => "unexpected", error => error.name)
+ ]);
+ return { split, utf8, legacy, bomKept: bom === "\uFEFFA", fatal };
+})()