Xuanwo commented on code in PR #6564: URL: https://github.com/apache/opendal/pull/6564#discussion_r2367208550
########## core/src/services/opfs/reader.rs: ########## @@ -0,0 +1,139 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::sync::Arc; +use std::u64; + +use bytes::{Bytes, BytesMut}; +use futures::channel::{mpsc, oneshot}; +use futures::{SinkExt, StreamExt}; +use js_sys::Uint8Array; +use wasm_bindgen::JsCast; +use wasm_bindgen_futures::JsFuture; +use web_sys::{File, ReadableStreamDefaultReader, ReadableStreamReadResult}; + +use crate::raw::{oio, OpRead}; +use crate::*; + +use super::core::OpfsCore; +use super::error::parse_js_error; + +enum ReadRequest { + Read { tx: oneshot::Sender<Result<Bytes>> }, +} + +pub struct OpfsReader { + pos: usize, + end_pos: usize, + tx: mpsc::UnboundedSender<ReadRequest>, +} + +impl OpfsReader { + pub async fn new(core: Arc<OpfsCore>, path: &str, op: &OpRead) -> Result<Self> { + let pos = op.range().offset(); + let end_pos = op + .range() + .size() + .map(|sz| pos + sz) + .unwrap_or(i32::MAX as u64); + + let file_handle = core.file_handle(path).await?; + let file: File = JsFuture::from(file_handle.get_file()) + .await + .and_then(JsCast::dyn_into) + .map_err(parse_js_error)?; + + let read_stream = file + .slice_with_i32_and_i32(pos as i32, end_pos as i32) + .map_err(parse_js_error)? + .stream() + .get_reader() + .dyn_into::<ReadableStreamDefaultReader>() + .map_err(|obj| parse_js_error(obj.into()))?; + + let (tx, rx) = mpsc::unbounded(); + + // everything in wasm-bindgen is non-send, so we need to spawn a new task Review Comment: `oio::Read` and `oio::List` should be able to implemented in a non-Send way. I think we can avoid the those usage of channel. What's the problem we are facing now? ########## .github/workflows/test_behavior_opfs.yml: ########## @@ -0,0 +1,65 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +name: OPFS Test Review Comment: We can add opfs test as a new edge case under at https://github.com/apache/opendal/blob/main/.github/workflows/test_edge.yml ########## core/edge/opfs_on_wasm/src/async_create_dir.rs: ########## Review Comment: I'm not sure if we need to duplicate all the tests for WASM again. ########## .github/workflows/test_behavior.yml: ########## @@ -202,3 +202,17 @@ jobs: with: os: ${{ matrix.os }} cases: ${{ toJson(matrix.cases) }} + + test_opfs: Review Comment: Hi, opfs is a new service, and I didn’t expect it to be added as a new type for behavior tests. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
