ferrijs-std 0.2.1

Node and web standard library for the ferrijs QuickJS runtime: WHATWG Streams, Events, AbortController, Buffer, crypto, fs, os, url, zlib and the capability model they enforce (partly derived from awslabs/llrt, Apache-2.0).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#![allow(clippy::uninlined_format_args)]

use std::borrow::Cow;
use std::path::PathBuf;

use either::Either;
use crate::buffer::{ArrayBufferView, Buffer};
use crate::encoding::Encoder;
use crate::utils::{
    object::ObjectExt,
    result::{OptionExt, ResultExt},
};
use rquickjs::function::Opt;
use rquickjs::{Ctx, Error, Exception, FromJs, Null, Object, Result, Value};
use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt, SeekFrom};

use super::{read_file, Stats};

const DEFAULT_BUFFER_SIZE: usize = 16384;
const DEFAULT_ENCODING: &str = "utf8";

#[allow(dead_code)]
#[rquickjs::class]
#[derive(rquickjs::class::Trace, rquickjs::JsLifetime)]
pub struct FileHandle {
    #[qjs(skip_trace)]
    file: Option<File>,
    #[qjs(skip_trace)]
    path: PathBuf,
}

impl FileHandle {
    pub fn new(file: File, path: PathBuf) -> Self {
        Self {
            file: Some(file),
            path,
        }
    }

    fn file(&self, ctx: &Ctx<'_>) -> Result<&File> {
        self.file.as_ref().or_throw_msg(ctx, "FileHandle is closed")
    }

    fn file_mut(&mut self, ctx: &Ctx<'_>) -> Result<&mut File> {
        self.file.as_mut().or_throw_msg(ctx, "FileHandle is closed")
    }
}

#[rquickjs::methods(rename_all = "camelCase")]
impl FileHandle {
    #[allow(unused_variables)]
    async fn chmod(&self, ctx: Ctx<'_>, mode: u32) -> Result<()> {
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let perm = std::fs::Permissions::from_mode(mode);
            self.file(&ctx)?
                .set_permissions(perm)
                .await
                .or_throw_msg(&ctx, "Can't modify file permissions")?;
        }
        Ok(())
    }

    #[allow(unused_variables)]
    async fn chown(&self, ctx: Ctx<'_>, uid: u32, gid: u32) -> Result<()> {
        #[cfg(unix)]
        {
            let path = self.path.clone();
            tokio::task::spawn_blocking(move || {
                std::os::unix::fs::chown(&path, Some(uid), Some(gid))
            })
            .await
            .or_throw(&ctx)?
            .or_throw_msg(&ctx, "Can't modify file owner")?;
        }
        Ok(())
    }

    async fn close(&mut self) {
        if let Some(file) = self.file.take() {
            drop(file.into_std().await);
        }
    }

    async fn datasync(&self, ctx: Ctx<'_>) -> Result<()> {
        self.file(&ctx)?
            .sync_data()
            .await
            .or_throw_msg(&ctx, "Can't sync file data")?;
        Ok(())
    }

    #[qjs(get)]
    async fn fd(&self, ctx: Ctx<'_>) -> Result<i32> {
        #[cfg(unix)]
        {
            use std::os::fd::AsRawFd;
            Ok(self.file(&ctx)?.as_raw_fd())
        }
        #[cfg(windows)]
        {
            use std::os::windows::io::AsRawHandle;
            let handle = self.file(&ctx)?.as_raw_handle();
            Ok(handle as i32)
        }
        #[cfg(not(any(unix, windows)))]
        {
            Ok(0)
        }
    }

    async fn read<'js>(
        &mut self,
        ctx: Ctx<'js>,
        buffer_or_options: Opt<Either<ArrayBufferView<'js>, ReadOptions<'js>>>,
        options_or_offset: Opt<Either<ReadOptions<'js>, usize>>,
        length: Opt<usize>,
        position: Opt<Option<u64>>, // -1 is not supported
    ) -> Result<Object<'js>> {
        let options_1 = match buffer_or_options.0 {
            Some(Either::Left(buffer)) => ReadOptions {
                buffer: Some(buffer),
                ..Default::default()
            },
            Some(Either::Right(options)) => options,
            None => ReadOptions::default(),
        };
        let options_2 = match options_or_offset.0 {
            Some(Either::Left(options)) => options,
            Some(Either::Right(offset)) => ReadOptions {
                offset: Some(offset),
                ..Default::default()
            },
            None => ReadOptions::default(),
        };

        let mut buffer = options_1
            .buffer
            .or(options_2.buffer)
            .unwrap_or_else_ok(|| {
                ArrayBufferView::from_buffer(&ctx, Buffer::alloc(DEFAULT_BUFFER_SIZE))
            })?;
        let offset = options_1.offset.or(options_2.offset).unwrap_or(0);
        let length = options_1
            .length
            .or(options_2.length)
            .or(length.0)
            .unwrap_or_else(|| buffer.len() - offset);
        let position = options_1
            .position
            .or(options_2.position)
            .or(position.0.flatten());
        validate_length_offset(&ctx, length, offset, buffer.len())?;

        // It is not safe to pass the buffer from `ArrayBufferView` to `File::read`
        // since the read is done in a different thread and we cannot garantee
        // that multiple read calls are not done with the same buffer.
        // Ideally, we should make our own version of `BufReader` to reuse the buffer
        // instead of doing an allocation on each read.
        let mut buf = vec![0u8; length];
        let file = self.file_mut(&ctx)?;

        // Tokio doesn't offer an API for positional reads. This means we have
        // to seek to the position, read the file, and then seek back to the original
        // position. See https://github.com/tokio-rs/tokio/issues/699
        let mut cursor = None;
        if let Some(position) = position {
            cursor = Some(
                file.seek(SeekFrom::Current(0))
                    .await
                    .or_throw_msg(&ctx, "Can't get cursor")?,
            );
            file.seek(SeekFrom::Start(position))
                .await
                .or_throw_msg(&ctx, "Can't seek file")?;
        }

        let bytes_read = file
            .read(&mut buf)
            .await
            .or_throw_msg(&ctx, "Failed to read file")?;

        // Reset the file at the original position. If there is an error while
        // resetting the cursor, we close the file pre-emptively since future
        // reads would be invalid.
        if let Some(cursor) = cursor {
            if let Err(err) = file
                .seek(SeekFrom::Start(cursor))
                .await
                .or_throw_msg(&ctx, "Failed to reset cursor")
            {
                self.close().await;
                return Err(err);
            }
        }

        let dst_buf = buffer
            .as_bytes_mut()
            .or_throw_msg(&ctx, "Buffer is detached")?;
        dst_buf[offset..].copy_from_slice(&buf);

        let result = Object::new(ctx)?;
        result.set("bytesRead", bytes_read)?;
        result.set("buffer", buffer)?;
        Ok(result)
    }

    async fn read_file<'js>(
        &mut self,
        ctx: Ctx<'js>,
        options: Opt<Either<String, read_file::ReadFileOptions>>,
    ) -> Result<Value<'js>> {
        let size = self
            .file(&ctx)?
            .metadata()
            .await
            .map(|m| m.len() as usize)
            .ok();
        let mut bytes = Vec::new();
        bytes
            .try_reserve_exact(size.unwrap_or(0))
            .or_throw_msg(&ctx, "Out of memory")?;

        self.file_mut(&ctx)?
            .read_to_end(&mut bytes)
            .await
            .or_throw_msg(&ctx, "Failed to read file")?;
        read_file::handle_read_file_bytes(&ctx, options, bytes)
    }

    async fn stat(&self, ctx: Ctx<'_>) -> Result<Stats> {
        let metadata = self
            .file(&ctx)?
            .metadata()
            .await
            .or_throw_msg(&ctx, "Can't stat file")?;
        Ok(Stats::new(metadata))
    }

    async fn sync(&self, ctx: Ctx<'_>) -> Result<()> {
        self.file(&ctx)?
            .sync_all()
            .await
            .or_throw_msg(&ctx, "Can't sync file")
    }

    async fn truncate(&mut self, ctx: Ctx<'_>, len: Opt<u64>) -> Result<()> {
        let len = len.0.unwrap_or(0);
        self.file_mut(&ctx)?
            .set_len(len)
            .await
            .or_throw_msg(&ctx, "Can't truncate file")
    }

    // Setting times not supported in tokio
    // See https://github.com/tokio-rs/tokio/issues/6368
    // async fn utimes(&mut self,  ctx: Ctx<'_>, atime: Value<'_>, mtime: Value<'_>) -> Result<()>

    async fn write<'js>(
        &mut self,
        ctx: Ctx<'js>,
        buffer_or_string: Either<ArrayBufferView<'js>, String>,
        offset_or_options_or_position: Opt<Either<Either<usize, Null>, WriteOptions>>,
        length_or_encoding: Opt<Either<usize, String>>,
        position: Opt<Option<u64>>,
    ) -> Result<Object<'js>> {
        let mut options = match offset_or_options_or_position.0 {
            Some(Either::Left(Either::Left(offset_or_position))) => {
                if buffer_or_string.is_left() {
                    WriteOptions {
                        offset: Some(offset_or_position),
                        ..Default::default()
                    }
                } else {
                    WriteOptions::default()
                }
            },
            Some(Either::Right(options)) => options,
            _ => WriteOptions::default(),
        };
        if let Some(Either::Left(length)) = length_or_encoding.0 {
            options.length = Some(length);
        }

        let buffer = match &buffer_or_string {
            Either::Left(buffer) => {
                let buffer = buffer.as_bytes().or_throw_msg(&ctx, "Buffer is detached")?;
                Cow::Borrowed(buffer)
            },
            Either::Right(string) => {
                let encoding = length_or_encoding
                    .0
                    .and_then(|e| e.right())
                    .unwrap_or_else(|| DEFAULT_ENCODING.to_string());
                let buffer = Encoder::from_str(&encoding)
                    .and_then(|enc| enc.decode_from_string(string.clone()))
                    .or_throw(&ctx)?;
                Cow::Owned(buffer)
            },
        };

        let offset = options.offset.unwrap_or(0);
        let length = options.length.unwrap_or(buffer.len() - offset);
        let position = options.position.or(position.0.flatten());
        validate_length_offset(&ctx, length, offset, buffer.len())?;

        let file = self.file_mut(&ctx)?;

        // Tokio doesn't offer an API for positional writes. This means we have
        // to seek to the position, write to the file, and then seek back to the original
        // position. See https://github.com/tokio-rs/tokio/issues/699
        let mut cursor = None;
        if let Some(position) = position {
            cursor = Some(
                file.seek(SeekFrom::Current(0))
                    .await
                    .or_throw_msg(&ctx, "Can't get cursor")?,
            );
            file.seek(SeekFrom::Start(position))
                .await
                .or_throw_msg(&ctx, "Can't seek file")?;
        }

        file.write_all(&buffer[offset..length])
            .await
            .or_throw_msg(&ctx, "Failed to write to file")?;

        // Reset the file at the original position. If there is an error while
        // resetting the cursor, we close the file pre-emptively since future
        // writes would be invalid.
        if let Some(cursor) = cursor {
            if let Err(err) = file
                .seek(SeekFrom::Start(cursor))
                .await
                .or_throw_msg(&ctx, "Failed to reset cursor")
            {
                self.close().await;
                return Err(err);
            }
        }

        let result = Object::new(ctx)?;
        result.set("bytesWritten", length)?;
        result.set("buffer", buffer_or_string)?;
        Ok(result)
    }

    async fn write_file<'js>(
        &mut self,
        ctx: Ctx<'js>,
        data: Either<ArrayBufferView<'js>, String>,
        options_or_encoding: Opt<Either<WriteFileOptions, String>>,
    ) -> Result<()> {
        let file = self.file_mut(&ctx)?;

        // Always overwrite the whole file
        file.set_len(0)
            .await
            .or_throw_msg(&ctx, "Failed to truncate file")?;

        let encoding = match options_or_encoding.0 {
            Some(Either::Left(options)) => options.encoding,
            Some(Either::Right(encoding)) => Some(encoding),
            _ => None,
        }
        .unwrap_or_else(|| DEFAULT_ENCODING.to_string());

        let buffer = match &data {
            Either::Left(buffer) => {
                let buffer = buffer.as_bytes().or_throw_msg(&ctx, "Buffer is detached")?;
                Cow::Borrowed(buffer)
            },
            Either::Right(string) => {
                let buffer = Encoder::from_str(&encoding)
                    .and_then(|enc| enc.decode_from_string(string.clone()))
                    .or_throw(&ctx)?;
                Cow::Owned(buffer)
            },
        };

        file.write_all(&buffer)
            .await
            .or_throw_msg(&ctx, "Failed to write to file")?;
        Ok(())
    }
}

fn validate_length_offset(
    ctx: &Ctx<'_>,
    length: usize,
    offset: usize,
    buffer_length: usize,
) -> Result<()> {
    if offset > buffer_length {
        return Err(Exception::throw_range(
            ctx,
            &format!("offset ({}) <= {}", offset, buffer_length),
        ));
    }
    if length > buffer_length - offset {
        return Err(Exception::throw_range(
            ctx,
            &format!("length ({}) <= {}", length, buffer_length - offset),
        ));
    }
    Ok(())
}

#[derive(Default)]
struct ReadOptions<'js> {
    buffer: Option<ArrayBufferView<'js>>,
    offset: Option<usize>,
    length: Option<usize>,
    position: Option<u64>,
}

impl<'js> FromJs<'js> for ReadOptions<'js> {
    fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
        let ty_name = value.type_name();
        let obj = value
            .as_object()
            .ok_or(Error::new_from_js(ty_name, "Object"))?;

        let buffer = obj.get_optional::<_, ArrayBufferView<'js>>("buffer")?;
        let offset = obj.get_optional::<_, usize>("offset")?;
        let length = obj.get_optional::<_, usize>("length")?;
        let position = obj.get_optional::<_, u64>("position")?;

        Ok(Self {
            buffer,
            offset,
            length,
            position,
        })
    }
}

#[derive(Default)]
struct WriteOptions {
    offset: Option<usize>,
    length: Option<usize>,
    position: Option<u64>,
}

impl<'js> FromJs<'js> for WriteOptions {
    fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
        let ty_name = value.type_name();
        let obj = value
            .as_object()
            .ok_or(Error::new_from_js(ty_name, "Object"))?;

        let offset = obj.get_optional::<_, usize>("offset")?;
        let length = obj.get_optional::<_, usize>("length")?;
        let position = obj.get_optional::<_, u64>("position")?;

        Ok(Self {
            offset,
            length,
            position,
        })
    }
}

#[derive(Default)]
struct WriteFileOptions {
    encoding: Option<String>,
}

impl<'js> FromJs<'js> for WriteFileOptions {
    fn from_js(_ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
        let ty_name = value.type_name();
        let obj = value
            .as_object()
            .ok_or(Error::new_from_js(ty_name, "Object"))?;

        let encoding = obj.get_optional::<_, String>("encoding")?;

        Ok(Self { encoding })
    }
}