1use super::*;
7
8pub(crate) fn readable_stream_ctor(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
10 let source = arg(a, 0);
11 let obj = match this {
12 Value::Object(o) => o,
13 _ => Obj::plain(),
14 };
15
16 {
17 let mut o = obj.borrow_mut();
18 o.props.insert("locked".into(), Value::Bool(false));
19 o.props.insert("_state".into(), Value::str("readable"));
20 o.props.insert("_chunks".into(), Value::Object(Obj::array(alloc::vec![])));
21 o.props.insert("getReader".into(), nv("ReadableStream.getReader", readable_stream_get_reader));
22 o.props.insert("cancel".into(), nv("ReadableStream.cancel", readable_stream_cancel));
23 o.props.insert("pipeTo".into(), nv("ReadableStream.pipeTo", readable_stream_pipe_to));
24 o.props.insert("pipeThrough".into(), nv("ReadableStream.pipeThrough", readable_stream_pipe_through));
25 }
26
27 if let Value::Object(src_obj) = source {
29 if let Some(start_fn) = src_obj.borrow().props.get("start").cloned() {
30 let controller = make_readable_stream_controller(Value::Object(obj.clone()));
31 let _ = it.call_listener(&start_fn, Value::Object(src_obj.clone()), &[controller], "start");
32 }
33 }
34
35 Ok(Value::Object(obj))
36}
37
38pub(crate) fn make_readable_stream_from_string(text: &str) -> Value {
40 let stream_obj = Obj::plain();
41 {
42 let mut o = stream_obj.borrow_mut();
43 o.props.insert("locked".into(), Value::Bool(false));
44 o.props.insert("_state".into(), Value::str("closed"));
45 let chunks = if !text.is_empty() {
46 alloc::vec![Value::str(text)]
47 } else {
48 alloc::vec![]
49 };
50 o.props.insert("_chunks".into(), Value::Object(Obj::array(chunks)));
51 o.props.insert("getReader".into(), nv("ReadableStream.getReader", readable_stream_get_reader));
52 o.props.insert("cancel".into(), nv("ReadableStream.cancel", readable_stream_cancel));
53 o.props.insert("pipeTo".into(), nv("ReadableStream.pipeTo", readable_stream_pipe_to));
54 o.props.insert("pipeThrough".into(), nv("ReadableStream.pipeThrough", readable_stream_pipe_through));
55 }
56 Value::Object(stream_obj)
57}
58
59pub(crate) fn make_readable_stream_from_bytes(it: &mut Interp, bytes: &[u8]) -> Value {
63 let stream = make_readable_stream_from_string("");
64 if let Value::Object(o) = &stream {
65 let chunks = if bytes.is_empty() {
66 alloc::vec![]
67 } else {
68 alloc::vec![crate::os_lib::ecma::builtins::typed_array::make_uint8array(it, bytes.to_vec())]
69 };
70 o.borrow_mut().props.insert("_chunks".into(), Value::Object(Obj::array(chunks)));
71 }
72 stream
73}
74
75pub(crate) fn readable_stream_get_reader(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
77 if let Value::Object(stream_obj) = &this {
78 let is_locked = stream_obj.borrow().props.get("locked").map(|v| v.truthy()).unwrap_or(false);
79 if is_locked {
80 return Err(it.type_error("ReadableStream is already locked by another reader"));
81 }
82 stream_obj.borrow_mut().props.insert("locked".into(), Value::Bool(true));
83
84 let reader = Obj::plain();
85 {
86 let mut r = reader.borrow_mut();
87 r.props.insert("_stream".into(), this.clone());
88 r.props.insert("read".into(), nv("ReadableStreamDefaultReader.read", readable_reader_read));
89 r.props.insert("releaseLock".into(), nv("ReadableStreamDefaultReader.releaseLock", readable_reader_release_lock));
90 r.props.insert("cancel".into(), nv("ReadableStreamDefaultReader.cancel", readable_reader_cancel));
91 r.props.insert("closed".into(), Value::Object(make_resolved_promise(Value::Undefined)));
92 }
93 return Ok(Value::Object(reader));
94 }
95 Err(it.type_error("getReader called on non-ReadableStream"))
96}
97
98pub(crate) fn readable_stream_cancel(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
100 if let Value::Object(stream_obj) = &this {
101 let mut s = stream_obj.borrow_mut();
102 s.props.insert("_state".into(), Value::str("closed"));
103 if let Some(Value::Object(chunks_obj)) = s.props.get("_chunks") {
104 if let ObjKind::Array(ref mut vec) = chunks_obj.borrow_mut().kind {
105 vec.clear();
106 }
107 }
108 }
109 Ok(resolved_promise(it, Value::Undefined))
110}
111
112pub(crate) fn readable_reader_read(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
114 if let Value::Object(reader_obj) = &this {
115 let stream_val = reader_obj.borrow().props.get("_stream").cloned();
116 if let Some(Value::Object(stream_obj)) = stream_val {
117 let s = stream_obj.borrow_mut();
118 let state = s.props.get("_state").map(|v| v.to_js_string()).unwrap_or_else(|| "readable".into());
119
120 let chunk_opt = if let Some(Value::Object(chunks_obj)) = s.props.get("_chunks") {
122 if let ObjKind::Array(ref mut vec) = chunks_obj.borrow_mut().kind {
123 if !vec.is_empty() {
124 Some(vec.remove(0))
125 } else {
126 None
127 }
128 } else {
129 None
130 }
131 } else {
132 None
133 };
134
135 drop(s);
136
137 let res_obj = Obj::plain();
138 if let Some(chunk) = chunk_opt {
139 res_obj.borrow_mut().props.insert("value".into(), chunk);
140 res_obj.borrow_mut().props.insert("done".into(), Value::Bool(false));
141 } else {
142 let is_closed = state == "closed";
143 res_obj.borrow_mut().props.insert("value".into(), Value::Undefined);
144 res_obj.borrow_mut().props.insert("done".into(), Value::Bool(is_closed));
145 }
146 return Ok(resolved_promise(it, Value::Object(res_obj)));
147 }
148 }
149 let res_obj = Obj::plain();
150 res_obj.borrow_mut().props.insert("value".into(), Value::Undefined);
151 res_obj.borrow_mut().props.insert("done".into(), Value::Bool(true));
152 Ok(resolved_promise(it, Value::Object(res_obj)))
153}
154
155pub(crate) fn readable_reader_release_lock(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
157 if let Value::Object(reader_obj) = &this {
158 let stream_val = reader_obj.borrow().props.get("_stream").cloned();
159 if let Some(Value::Object(stream_obj)) = stream_val {
160 let is_locked = stream_obj.borrow().props.get("locked").map(|v| v.truthy()).unwrap_or(false);
161 if !is_locked {
162 return Err(it.type_error("Cannot release lock: stream is not locked"));
163 }
164 stream_obj.borrow_mut().props.insert("locked".into(), Value::Bool(false));
165 return Ok(Value::Undefined);
166 }
167 }
168 Err(it.type_error("Cannot release lock"))
169}
170
171pub(crate) fn readable_reader_cancel(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
173 if let Value::Object(reader_obj) = &this {
174 let stream_val = reader_obj.borrow().props.get("_stream").cloned();
175 if let Some(stream) = stream_val {
176 return readable_stream_cancel(it, stream, a);
177 }
178 }
179 Ok(resolved_promise(it, Value::Undefined))
180}
181
182fn make_readable_stream_controller(stream: Value) -> Value {
184 let ctrl = Obj::plain();
185 {
186 let mut c = ctrl.borrow_mut();
187 c.props.insert("_stream".into(), stream);
188 c.props.insert("enqueue".into(), nv("ReadableStreamDefaultController.enqueue", controller_enqueue));
189 c.props.insert("close".into(), nv("ReadableStreamDefaultController.close", controller_close));
190 c.props.insert("error".into(), nv("ReadableStreamDefaultController.error", controller_error));
191 c.props.insert("desiredSize".into(), Value::Number(1.0));
192 }
193 Value::Object(ctrl)
194}
195
196pub(crate) fn controller_enqueue(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
197 if let Value::Object(ctrl_obj) = &this {
198 let stream_val = ctrl_obj.borrow().props.get("_stream").cloned();
199 if let Some(Value::Object(stream_obj)) = stream_val {
200 let s = stream_obj.borrow_mut();
201 let state = s.props.get("_state").map(|v| v.to_js_string()).unwrap_or_else(|| "readable".into());
202 if state != "readable" {
203 return Err(it.type_error("Cannot enqueue a chunk into a closed stream"));
204 }
205 let chunk = arg(a, 0);
206 if let Some(Value::Object(chunks_obj)) = s.props.get("_chunks") {
207 if let ObjKind::Array(ref mut vec) = chunks_obj.borrow_mut().kind {
208 vec.push(chunk);
209 }
210 }
211 return Ok(Value::Undefined);
212 }
213 }
214 Ok(Value::Undefined)
215}
216
217pub(crate) fn controller_close(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
218 if let Value::Object(ctrl_obj) = &this {
219 let stream_val = ctrl_obj.borrow().props.get("_stream").cloned();
220 if let Some(Value::Object(stream_obj)) = stream_val {
221 let mut s = stream_obj.borrow_mut();
222 let state = s.props.get("_state").map(|v| v.to_js_string()).unwrap_or_else(|| "readable".into());
223 if state == "closed" {
224 return Err(it.type_error("Cannot close an already closed stream"));
225 }
226 s.props.insert("_state".into(), Value::str("closed"));
227 drop(s);
228 flush_stream_waiters(it, &stream_obj);
229 return Ok(Value::Undefined);
230 }
231 }
232 Ok(Value::Undefined)
233}
234
235pub(crate) fn controller_error(_it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
236 if let Value::Object(ctrl_obj) = &this {
237 let stream_val = ctrl_obj.borrow().props.get("_stream").cloned();
238 if let Some(Value::Object(stream_obj)) = stream_val {
239 let mut s = stream_obj.borrow_mut();
240 s.props.insert("_state".into(), Value::str("errored"));
241 let reason = arg(a, 0);
242 s.props.insert("_error".into(), reason);
243 if let Some(Value::Object(chunks_obj)) = s.props.get("_chunks") {
244 if let ObjKind::Array(ref mut vec) = chunks_obj.borrow_mut().kind {
245 vec.clear();
246 }
247 }
248 }
249 }
250 Ok(Value::Undefined)
251}
252
253pub(crate) fn writable_stream_ctor(_it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
255 let obj = match this {
256 Value::Object(o) => o,
257 _ => Obj::plain(),
258 };
259
260 {
261 let mut o = obj.borrow_mut();
262 o.props.insert("locked".into(), Value::Bool(false));
263 o.props.insert("_state".into(), Value::str("writable"));
264 o.props.insert("_buffer".into(), Value::Object(Obj::array(alloc::vec![])));
265 o.props.insert("getWriter".into(), nv("WritableStream.getWriter", writable_stream_get_writer));
266 o.props.insert("abort".into(), nv("WritableStream.abort", writable_stream_abort));
267 o.props.insert("close".into(), nv("WritableStream.close", writable_stream_close));
268 }
269
270 Ok(Value::Object(obj))
271}
272
273pub(crate) fn writable_stream_get_writer(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
274 if let Value::Object(stream_obj) = &this {
275 let is_locked = stream_obj.borrow().props.get("locked").map(|v| v.truthy()).unwrap_or(false);
276 if is_locked {
277 return Err(it.type_error("WritableStream is already locked by another writer"));
278 }
279 stream_obj.borrow_mut().props.insert("locked".into(), Value::Bool(true));
280
281 let writer = Obj::plain();
282 {
283 let mut w = writer.borrow_mut();
284 w.props.insert("_stream".into(), this.clone());
285 w.props.insert("write".into(), nv("WritableStreamDefaultWriter.write", writable_writer_write));
286 w.props.insert("close".into(), nv("WritableStreamDefaultWriter.close", writable_writer_close));
287 w.props.insert("abort".into(), nv("WritableStreamDefaultWriter.abort", writable_writer_abort));
288 w.props.insert("releaseLock".into(), nv("WritableStreamDefaultWriter.releaseLock", writable_writer_release_lock));
289 }
290 return Ok(Value::Object(writer));
291 }
292 Err(it.type_error("getWriter called on non-WritableStream"))
293}
294
295pub(crate) fn writable_stream_abort(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
296 if let Value::Object(stream_obj) = &this {
297 let mut s = stream_obj.borrow_mut();
298 s.props.insert("_state".into(), Value::str("errored"));
299 s.props.insert("_error".into(), arg(a, 0));
300 }
301 Ok(resolved_promise(it, Value::Undefined))
302}
303
304pub(crate) fn writable_stream_close(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
305 if let Value::Object(stream_obj) = &this {
306 stream_obj.borrow_mut().props.insert("_state".into(), Value::str("closed"));
307 compression_finish(it, stream_obj);
308 if let Err(err) = decompression_finish(it, stream_obj) {
309 return Ok(rejected_promise(it, err));
310 }
311 }
312 Ok(resolved_promise(it, Value::Undefined))
313}
314
315pub(crate) fn compression_stream_ctor(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
323 let format = arg(a, 0).to_js_string();
324 if !matches!(format.as_str(), "gzip" | "deflate" | "deflate-raw") {
325 let msg = alloc::format!(
326 "Failed to construct 'CompressionStream': Unsupported compression format: '{}'",
327 format
328 );
329 return Err(it.type_error(&msg));
330 }
331 let obj = match this {
332 Value::Object(o) => o,
333 _ => Obj::plain(),
334 };
335 let readable = make_readable_stream_from_string("");
336 if let Value::Object(r) = &readable {
337 r.borrow_mut().props.insert("_state".into(), Value::str("readable"));
338 }
339 let writable = writable_stream_ctor(it, Value::Undefined, &[])?;
340 if let Value::Object(w) = &writable {
341 let mut w = w.borrow_mut();
342 w.props.insert("_compress_format".into(), Value::str(format));
343 w.props.insert("_compress_readable".into(), readable.clone());
344 }
345 {
346 let mut o = obj.borrow_mut();
347 o.props.insert("readable".into(), readable);
348 o.props.insert("writable".into(), writable);
349 }
350 Ok(Value::Object(obj))
351}
352
353pub(crate) fn decompression_stream_ctor(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
355 let format = arg(a, 0).to_js_string();
356 if !matches!(format.as_str(), "gzip" | "deflate" | "deflate-raw") {
357 let msg = alloc::format!(
358 "Failed to construct 'DecompressionStream': Unsupported compression format: '{}'",
359 format
360 );
361 return Err(it.type_error(&msg));
362 }
363 let obj = match this {
364 Value::Object(o) => o,
365 _ => Obj::plain(),
366 };
367 let readable = make_readable_stream_from_string("");
368 if let Value::Object(r) = &readable {
369 r.borrow_mut().props.insert("_state".into(), Value::str("readable"));
370 }
371 let writable = writable_stream_ctor(it, Value::Undefined, &[])?;
372 if let Value::Object(w) = &writable {
373 let mut w = w.borrow_mut();
374 w.props.insert("_decompress_format".into(), Value::str(format));
375 w.props.insert("_decompress_readable".into(), readable.clone());
376 }
377 {
378 let mut o = obj.borrow_mut();
379 o.props.insert("readable".into(), readable);
380 o.props.insert("writable".into(), writable);
381 }
382 Ok(Value::Object(obj))
383}
384
385fn decompression_finish(it: &mut Interp, writable: &ObjRef) -> Result<(), Value> {
387 let (format, readable, chunks) = {
388 let w = writable.borrow();
389 let format = match w.props.get("_decompress_format") {
390 Some(v) => v.to_js_string(),
391 None => return Ok(()),
392 };
393 let readable = match w.props.get("_decompress_readable") {
394 Some(Value::Object(r)) => r.clone(),
395 _ => return Ok(()),
396 };
397 let chunks: Vec<Value> = match w.props.get("_buffer") {
398 Some(Value::Object(b)) => match &b.borrow().kind {
399 ObjKind::Array(v) => v.clone(),
400 _ => Vec::new(),
401 },
402 _ => Vec::new(),
403 };
404 (format, readable, chunks)
405 };
406 let data = stream_chunks_bytes(&chunks);
407 let out = crate::os_lib::deflate_enc::decompress_format(&format, &data)
408 .map_err(|e| it.type_error(e))?;
409 let chunk = crate::os_lib::ecma::builtins::typed_array::make_uint8array(it, out);
410 {
411 let mut r = readable.borrow_mut();
412 if let Some(Value::Object(c)) = r.props.get("_chunks") {
413 if let ObjKind::Array(ref mut v) = c.borrow_mut().kind {
414 v.push(chunk);
415 }
416 }
417 r.props.insert("_state".into(), Value::str("closed"));
418 }
419 flush_stream_waiters(it, &readable);
420 Ok(())
421}
422
423fn compression_finish(it: &mut Interp, writable: &ObjRef) {
425 let (format, readable, chunks) = {
426 let w = writable.borrow();
427 let format = match w.props.get("_compress_format") {
428 Some(v) => v.to_js_string(),
429 None => return,
430 };
431 let readable = match w.props.get("_compress_readable") {
432 Some(Value::Object(r)) => r.clone(),
433 _ => return,
434 };
435 let chunks: Vec<Value> = match w.props.get("_buffer") {
436 Some(Value::Object(b)) => match &b.borrow().kind {
437 ObjKind::Array(v) => v.clone(),
438 _ => Vec::new(),
439 },
440 _ => Vec::new(),
441 };
442 (format, readable, chunks)
443 };
444 let data = stream_chunks_bytes(&chunks);
445 let out = match format.as_str() {
446 "gzip" => crate::os_lib::deflate_enc::gzip(&data),
447 "deflate" => crate::os_lib::deflate_enc::zlib(&data),
448 _ => crate::os_lib::deflate_enc::deflate_raw(&data),
449 };
450 let chunk = crate::os_lib::ecma::builtins::typed_array::make_uint8array(it, out);
451 {
452 let mut r = readable.borrow_mut();
453 if let Some(Value::Object(c)) = r.props.get("_chunks") {
454 if let ObjKind::Array(ref mut v) = c.borrow_mut().kind {
455 v.push(chunk);
456 }
457 }
458 r.props.insert("_state".into(), Value::str("closed"));
459 }
460 flush_stream_waiters(it, &readable);
461}
462
463fn stream_chunks_bytes(chunks: &[Value]) -> Vec<u8> {
465 let mut f = Vec::new();
466 for c in chunks {
467 blob_part_bytes(c, &mut f);
468 }
469 f.into_iter().map(|b| b as u8).collect()
470}
471
472pub(crate) fn read_stream_body(it: &mut Interp, stream: &ObjRef, kind: &str) -> Value {
475 let state = new_pending();
476 let promise = Value::Object(Obj::promise(state.clone()));
477 let closed = stream.borrow().props.get("_state").is_some_and(|v| v.to_js_string() == "closed");
478 if closed {
479 settle_stream_read(it, stream, &state, kind);
480 return promise;
481 }
482 let waiter = Obj::plain();
483 {
484 let mut w = waiter.borrow_mut();
485 w.props.insert("promise".into(), promise.clone());
486 w.props.insert("kind".into(), Value::str(kind));
487 }
488 let list = stream.borrow().props.get("_waiters").cloned();
489 match list {
490 Some(Value::Object(l)) => {
491 if let ObjKind::Array(ref mut v) = l.borrow_mut().kind {
492 v.push(Value::Object(waiter));
493 }
494 }
495 _ => {
496 let l = Value::Object(Obj::array(alloc::vec![Value::Object(waiter)]));
497 stream.borrow_mut().props.insert("_waiters".into(), l);
498 }
499 }
500 promise
501}
502
503pub(crate) fn flush_stream_waiters(it: &mut Interp, stream: &ObjRef) {
505 let list = stream.borrow_mut().props.shift_remove("_waiters");
506 let waiters: Vec<Value> = match list {
507 Some(Value::Object(l)) => match &l.borrow().kind {
508 ObjKind::Array(v) => v.clone(),
509 _ => Vec::new(),
510 },
511 _ => Vec::new(),
512 };
513 for w in waiters {
514 let Value::Object(w) = w else { continue };
515 let (promise, kind) = {
516 let b = w.borrow();
517 let kind = b.props.get("kind").map(|v| v.to_js_string()).unwrap_or_default();
518 (b.props.get("promise").cloned(), kind)
519 };
520 let state = match promise {
521 Some(Value::Object(p)) => match &p.borrow().kind {
522 ObjKind::PromiseObj(st) => st.clone(),
523 _ => continue,
524 },
525 _ => continue,
526 };
527 settle_stream_read(it, stream, &state, &kind);
528 }
529}
530
531fn settle_stream_read(
532 it: &mut Interp,
533 stream: &ObjRef,
534 state: &PromiseRc<PromiseRefCell<PromiseState>>,
535 kind: &str,
536) {
537 let chunks: Vec<Value> = match stream.borrow().props.get("_chunks") {
538 Some(Value::Object(c)) => match &c.borrow().kind {
539 ObjKind::Array(v) => v.clone(),
540 _ => Vec::new(),
541 },
542 _ => Vec::new(),
543 };
544 let bytes = stream_chunks_bytes(&chunks);
545 let v = match kind {
546 "text" => Value::str(String::from_utf8_lossy(&bytes).into_owned()),
547 "json" => {
548 let text = Value::str(String::from_utf8_lossy(&bytes).into_owned());
549 match json_parse(it, Value::Undefined, &[text]) {
550 Ok(v) => v,
551 Err(e) => {
552 it.promise_reject(state, e);
553 return;
554 }
555 }
556 }
557 "blob" => make_blob(bytes.iter().map(|&b| b as f64).collect(), String::new()),
558 "bytes" => crate::os_lib::ecma::builtins::typed_array::make_uint8array(it, bytes),
559 _ => make_arraybuffer_with_bytes(bytes.iter().map(|&b| b as f64).collect()),
560 };
561 it.promise_resolve(state, v);
562}
563
564pub(crate) fn writable_writer_write(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
565 if let Value::Object(writer_obj) = &this {
566 let stream_val = writer_obj.borrow().props.get("_stream").cloned();
567 if let Some(Value::Object(stream_obj)) = stream_val {
568 let s = stream_obj.borrow_mut();
569 let state = s.props.get("_state").map(|v| v.to_js_string()).unwrap_or_else(|| "writable".into());
570 if state != "writable" {
571 let err = it.type_error("Cannot write to a closed or errored stream");
572 return Ok(rejected_promise(it, err));
573 }
574 let chunk = arg(a, 0);
575 if let Some(Value::Object(buf_obj)) = s.props.get("_buffer") {
576 if let ObjKind::Array(ref mut vec) = buf_obj.borrow_mut().kind {
577 vec.push(chunk);
578 }
579 }
580 return Ok(resolved_promise(it, Value::Undefined));
581 }
582 }
583 Ok(resolved_promise(it, Value::Undefined))
584}
585
586pub(crate) fn writable_writer_close(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
587 if let Value::Object(writer_obj) = &this {
588 let stream_val = writer_obj.borrow().props.get("_stream").cloned();
589 if let Some(stream) = stream_val {
590 return writable_stream_close(it, stream, a);
591 }
592 }
593 Ok(resolved_promise(it, Value::Undefined))
594}
595
596pub(crate) fn writable_writer_abort(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
597 if let Value::Object(writer_obj) = &this {
598 let stream_val = writer_obj.borrow().props.get("_stream").cloned();
599 if let Some(stream) = stream_val {
600 return writable_stream_abort(it, stream, a);
601 }
602 }
603 Ok(resolved_promise(it, Value::Undefined))
604}
605
606pub(crate) fn writable_writer_release_lock(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
607 if let Value::Object(writer_obj) = &this {
608 let stream_val = writer_obj.borrow().props.get("_stream").cloned();
609 if let Some(Value::Object(stream_obj)) = stream_val {
610 let is_locked = stream_obj.borrow().props.get("locked").map(|v| v.truthy()).unwrap_or(false);
611 if !is_locked {
612 return Err(it.type_error("Cannot release lock: stream is not locked"));
613 }
614 stream_obj.borrow_mut().props.insert("locked".into(), Value::Bool(false));
615 return Ok(Value::Undefined);
616 }
617 }
618 Err(it.type_error("Cannot release lock"))
619}
620
621pub(crate) fn transform_stream_ctor(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
623 let obj = match this {
624 Value::Object(o) => o,
625 _ => Obj::plain(),
626 };
627
628 let readable = make_readable_stream_from_string("");
629 let writable = writable_stream_ctor(it, Value::Undefined, &[])?;
630
631 {
632 let mut o = obj.borrow_mut();
633 o.props.insert("readable".into(), readable);
634 o.props.insert("writable".into(), writable);
635 }
636
637 Ok(Value::Object(obj))
638}
639
640pub(crate) fn readable_stream_pipe_to(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
642 let dest = arg(a, 0);
643 if let Value::Object(src_obj) = &this {
644 if let Value::Object(dest_obj) = &dest {
645 let src_chunks: Vec<Value> = match src_obj.borrow().props.get("_chunks") {
646 Some(Value::Object(arr)) => match &arr.borrow().kind {
647 ObjKind::Array(vec) => vec.clone(),
648 _ => Vec::new(),
649 },
650 _ => Vec::new(),
651 };
652
653 if let Some(Value::Object(dest_buf)) = dest_obj.borrow().props.get("_buffer") {
654 if let ObjKind::Array(ref mut d_vec) = dest_buf.borrow_mut().kind {
655 for c in src_chunks {
656 d_vec.push(c);
657 }
658 }
659 }
660
661 return Ok(resolved_promise(it, Value::Undefined));
662 }
663 }
664 Err(it.type_error("Failed to execute 'pipeTo': Parameter 1 is not of type 'WritableStream'."))
665}
666
667pub(crate) fn readable_stream_pipe_through(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
669 let transform = arg(a, 0);
670 if let Value::Object(t_obj) = &transform {
671 let writable = t_obj.borrow().props.get("writable").cloned();
672 let readable = t_obj.borrow().props.get("readable").cloned();
673 if let (Some(w), Some(r)) = (writable, readable) {
674 let _ = readable_stream_pipe_to(it, this, &[w])?;
675 return Ok(r);
676 }
677 }
678 Err(it.type_error("Failed to execute 'pipeThrough': Invalid transform stream."))
679}
680
681pub(crate) fn text_encoder_stream_ctor(it: &mut Interp, this: Value, _a: &[Value]) -> Result<Value, Value> {
683 let obj = match this {
684 Value::Object(o) => o,
685 _ => Obj::plain(),
686 };
687 let readable = make_readable_stream_from_string("");
688 let writable = writable_stream_ctor(it, Value::Undefined, &[])?;
689 {
690 let mut o = obj.borrow_mut();
691 o.props.insert("encoding".into(), Value::str("utf-8"));
692 o.props.insert("readable".into(), readable);
693 o.props.insert("writable".into(), writable);
694 }
695 Ok(Value::Object(obj))
696}
697
698pub(crate) fn text_decoder_stream_ctor(it: &mut Interp, this: Value, a: &[Value]) -> Result<Value, Value> {
700 let (enc, fatal, ignore_bom) = super::text_clone::text_decoder_params(it, a, "TextDecoderStream")?;
701 let obj = match this {
702 Value::Object(o) => o,
703 _ => Obj::plain(),
704 };
705 let readable = make_readable_stream_from_string("");
706 let writable = writable_stream_ctor(it, Value::Undefined, &[])?;
707 {
708 let mut o = obj.borrow_mut();
709 o.props.insert("_enc".into(), Value::str(enc));
710 o.props.insert("_fatal".into(), Value::Bool(fatal));
711 o.props.insert("_ignore_bom".into(), Value::Bool(ignore_bom));
712 super::text_clone::insert_decoder_accessors(&mut o);
713 o.props.insert("readable".into(), readable);
714 o.props.insert("writable".into(), writable);
715 }
716 Ok(Value::Object(obj))
717}