ferrijs-std 0.1.0

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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! `performance`: High Resolution Time, User Timing and the Performance
//! Timeline.
//!
//! `now()` reads a monotonic [`Instant`], never the wall clock. That is
//! the whole point of the API — a `Date.now()` delta can go backwards
//! when NTP steps the clock mid-measurement, and a timing number that
//! silently goes backwards is worse than no timing number. The wall
//! clock appears exactly once, as `timeOrigin`, which is what the
//! monotonic readings are relative to.
//!
//! [`monotonic_base`] is also what `process.hrtime` counts from, so the
//! two clocks are correlatable the way Node's are (both derive from one
//! libuv hrtime there).
//!
//! Covered: `now`, `timeOrigin`, `toJSON`, `mark`, `measure`,
//! `clearMarks`, `clearMeasures`, `getEntries`, `getEntriesByName`,
//! `getEntriesByType`, and the `PerformanceEntry` / `PerformanceMark` /
//! `PerformanceMeasure` classes with `PerformanceEntry` as their
//! prototype, so `mark instanceof PerformanceEntry` holds.
//!
//! Not covered: `PerformanceObserver` (it needs a task-queue hook this
//! runtime has no equivalent of), the resource/navigation timing entry
//! types (no document), and Node's `eventLoopUtilization` / `nodeTiming`.
//! A buffer size limit is not implemented either; nothing here evicts,
//! so a program marking in a hot loop grows the buffer until it calls
//! `clearMarks`, exactly as the spec's `maxBufferSize` exists to bound.

use std::time::Instant;

use rquickjs::class::Trace;
use rquickjs::function::Opt;
use rquickjs::{Class, Ctx, Exception, Object, Value};

/// Monotonic base for `performance.now()`, and the wall-clock instant it
/// corresponds to (`performance.timeOrigin`). Both are fixed at first
/// use, which is process start for any real session.
static PROCESS_START: std::sync::LazyLock<Instant> = std::sync::LazyLock::new(Instant::now);
static TIME_ORIGIN: std::sync::LazyLock<f64> = std::sync::LazyLock::new(|| {
  // Touch the monotonic base first so the two are taken together.
  let _ = *PROCESS_START;
  std::time::SystemTime::now()
    .duration_since(std::time::UNIX_EPOCH)
    .map_or(0.0, |d| d.as_secs_f64() * 1000.0)
});

/// The process-wide monotonic origin. `process.hrtime` counts from this
/// too, so a script can line its readings up against `performance.now()`.
#[must_use]
pub fn monotonic_base() -> Instant {
  *PROCESS_START
}

/// `performance.now()` in fractional milliseconds.
#[must_use]
pub fn now_ms() -> f64 {
  PROCESS_START.elapsed().as_secs_f64() * 1000.0
}

/// `performance.timeOrigin`: Unix-epoch milliseconds at the monotonic
/// base.
#[must_use]
pub fn time_origin_ms() -> f64 {
  *TIME_ORIGIN
}

const MARK: &str = "mark";
const MEASURE: &str = "measure";

/// Read `PerformanceMarkOptions` into `(startTime, detail)`.
///
/// Shared by `performance.mark()` and `new PerformanceMark()` so the two
/// cannot disagree about a default or about which values are refused.
fn mark_options<'js>(ctx: &Ctx<'js>, options: Option<&Value<'js>>) -> rquickjs::Result<(f64, Value<'js>)> {
  let mut start_time = now_ms();
  let mut detail = Value::new_null(ctx.clone());
  let Some(options) = options.and_then(rquickjs::Value::as_object) else {
    return Ok((start_time, detail));
  };
  if let Some(given) = options
    .get::<_, Value<'js>>("startTime")
    .ok()
    .filter(|v| !v.is_undefined())
  {
    let Some(n) = given.as_number() else {
      return Err(Exception::throw_type(ctx, "startTime must be a number"));
    };
    if n < 0.0 {
      return Err(Exception::throw_type(ctx, "startTime cannot be negative"));
    }
    start_time = n;
  }
  if let Some(given) = options.get::<_, Value<'js>>("detail").ok().filter(|v| !v.is_undefined()) {
    detail = given;
  }
  Ok((start_time, detail))
}

/// `PerformanceEntry` — the base every timeline entry reads as.
///
/// Marks and measures are their own classes so `entryType` cannot
/// disagree with the constructor, and both chain their prototype here.
#[derive(Trace, Clone)]
#[rquickjs::class(rename = "PerformanceEntry")]
pub struct PerformanceEntryJs {
  #[qjs(skip_trace)]
  name: String,
  #[qjs(skip_trace)]
  entry_type: String,
  #[qjs(skip_trace)]
  start_time: f64,
  #[qjs(skip_trace)]
  duration: f64,
}

#[allow(unsafe_code)]
unsafe impl rquickjs::JsLifetime<'_> for PerformanceEntryJs {
  type Changed<'to> = PerformanceEntryJs;
}

#[rquickjs::methods(rename_all = "camelCase")]
impl PerformanceEntryJs {
  /// Exposed as a global but not constructible: its IDL declares no
  /// constructor, and the global has to exist anyway for
  /// `mark instanceof PerformanceEntry` to be answerable. rquickjs only
  /// puts a class on `globalThis` when it HAS a constructor, so the
  /// throwing one is what makes the name reachable.
  #[qjs(constructor)]
  fn constructor(ctx: Ctx<'_>) -> rquickjs::Result<Self> {
    Err(Exception::throw_type(&ctx, "Illegal constructor"))
  }

  #[qjs(get)]
  fn name(&self) -> String {
    self.name.clone()
  }

  #[qjs(get)]
  fn entry_type(&self) -> String {
    self.entry_type.clone()
  }

  #[qjs(get)]
  fn start_time(&self) -> f64 {
    self.start_time
  }

  #[qjs(get)]
  fn duration(&self) -> f64 {
    self.duration
  }

  #[qjs(rename = "toJSON")]
  fn to_json<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result<Object<'js>> {
    let o = Object::new(ctx)?;
    o.set("name", self.name.clone())?;
    o.set("entryType", self.entry_type.clone())?;
    o.set("startTime", self.start_time)?;
    o.set("duration", self.duration)?;
    Ok(o)
  }
}

/// `PerformanceMark` — a `PerformanceEntry` plus the `detail` its
/// creator attached.
#[derive(Trace)]
#[rquickjs::class(rename = "PerformanceMark")]
pub struct PerformanceMarkJs<'js> {
  #[qjs(skip_trace)]
  name: String,
  #[qjs(skip_trace)]
  start_time: f64,
  detail: Value<'js>,
}

#[allow(unsafe_code)]
unsafe impl<'js> rquickjs::JsLifetime<'js> for PerformanceMarkJs<'js> {
  type Changed<'to> = PerformanceMarkJs<'to>;
}

#[rquickjs::methods(rename_all = "camelCase")]
impl<'js> PerformanceMarkJs<'js> {
  /// `new PerformanceMark(name, { detail?, startTime? })`. Unlike
  /// `performance.mark()`, a directly constructed mark is NOT added to
  /// the timeline — the spec buffers only what `mark()` records.
  #[qjs(constructor)]
  fn constructor(ctx: Ctx<'js>, name: String, options: Opt<Value<'js>>) -> rquickjs::Result<Self> {
    let (start_time, detail) = mark_options(&ctx, options.0.as_ref())?;
    Ok(Self {
      name,
      start_time,
      detail,
    })
  }

  #[qjs(get)]
  fn name(&self) -> String {
    self.name.clone()
  }

  #[qjs(get)]
  fn entry_type(&self) -> &'static str {
    MARK
  }

  #[qjs(get)]
  fn start_time(&self) -> f64 {
    self.start_time
  }

  /// Always 0: a mark is an instant, not an interval.
  #[qjs(get)]
  fn duration(&self) -> f64 {
    0.0
  }

  #[qjs(get)]
  fn detail(&self) -> Value<'js> {
    self.detail.clone()
  }

  /// `detail` is included, matching what browsers serialize. The IDL's
  /// default serializer covers only `PerformanceEntry`'s own
  /// attributes, so this is the more useful reading of an ambiguity
  /// rather than a strict one.
  #[qjs(rename = "toJSON")]
  fn to_json(&self, ctx: Ctx<'js>) -> rquickjs::Result<Object<'js>> {
    let o = Object::new(ctx)?;
    o.set("name", self.name.clone())?;
    o.set("entryType", MARK)?;
    o.set("startTime", self.start_time)?;
    o.set("duration", 0.0)?;
    o.set("detail", self.detail.clone())?;
    Ok(o)
  }
}

/// `PerformanceMeasure` — an interval between two points on the
/// timeline.
#[derive(Trace)]
#[rquickjs::class(rename = "PerformanceMeasure")]
pub struct PerformanceMeasureJs<'js> {
  #[qjs(skip_trace)]
  name: String,
  #[qjs(skip_trace)]
  start_time: f64,
  #[qjs(skip_trace)]
  duration: f64,
  detail: Value<'js>,
}

#[allow(unsafe_code)]
unsafe impl<'js> rquickjs::JsLifetime<'js> for PerformanceMeasureJs<'js> {
  type Changed<'to> = PerformanceMeasureJs<'to>;
}

#[rquickjs::methods(rename_all = "camelCase")]
impl<'js> PerformanceMeasureJs<'js> {
  /// Not constructible, same as `PerformanceEntry`: a measure only ever
  /// comes from `performance.measure()`.
  #[qjs(constructor)]
  fn constructor(ctx: Ctx<'js>) -> rquickjs::Result<Self> {
    Err(Exception::throw_type(&ctx, "Illegal constructor"))
  }

  #[qjs(get)]
  fn name(&self) -> String {
    self.name.clone()
  }

  #[qjs(get)]
  fn entry_type(&self) -> &'static str {
    MEASURE
  }

  #[qjs(get)]
  fn start_time(&self) -> f64 {
    self.start_time
  }

  #[qjs(get)]
  fn duration(&self) -> f64 {
    self.duration
  }

  #[qjs(get)]
  fn detail(&self) -> Value<'js> {
    self.detail.clone()
  }

  #[qjs(rename = "toJSON")]
  fn to_json(&self, ctx: Ctx<'js>) -> rquickjs::Result<Object<'js>> {
    let o = Object::new(ctx)?;
    o.set("name", self.name.clone())?;
    o.set("entryType", MEASURE)?;
    o.set("startTime", self.start_time)?;
    o.set("duration", self.duration)?;
    o.set("detail", self.detail.clone())?;
    Ok(o)
  }
}

/// One buffered entry.
///
/// The name / type / start time are kept Rust-side alongside the JS
/// value so a `measure` resolving a mark name, and every
/// `getEntriesBy*` filter, is a Rust comparison rather than a property
/// read back out of the interpreter for each candidate.
#[derive(Trace)]
struct Buffered<'js> {
  #[qjs(skip_trace)]
  name: String,
  #[qjs(skip_trace)]
  is_mark: bool,
  #[qjs(skip_trace)]
  start_time: f64,
  value: Value<'js>,
}

/// `performance`.
#[derive(Trace)]
#[rquickjs::class(rename = "Performance")]
pub struct PerformanceJs<'js> {
  entries: Vec<Buffered<'js>>,
}

#[allow(unsafe_code)]
unsafe impl<'js> rquickjs::JsLifetime<'js> for PerformanceJs<'js> {
  type Changed<'to> = PerformanceJs<'to>;
}

impl Default for PerformanceJs<'_> {
  fn default() -> Self {
    Self::new()
  }
}

impl<'js> PerformanceJs<'js> {
  #[must_use]
  pub fn new() -> Self {
    Self { entries: Vec::new() }
  }

  /// The start time a mark name resolves to: the MOST RECENT mark with
  /// that name, per User Timing's "convert a mark to a timestamp".
  fn resolve_mark(&self, ctx: &Ctx<'js>, name: &str) -> rquickjs::Result<f64> {
    self
      .entries
      .iter()
      .rev()
      .find(|e| e.is_mark && e.name == name)
      .map(|e| e.start_time)
      .ok_or_else(|| Exception::throw_syntax(ctx, &format!("the mark {name:?} does not exist")))
  }

  /// A `start` / `end` member of `PerformanceMeasureOptions`, or a mark
  /// name. A number must be non-negative; a string names a mark.
  fn resolve_timestamp(&self, ctx: &Ctx<'js>, value: &Value<'js>) -> rquickjs::Result<f64> {
    if let Some(name) = value.as_string() {
      return self.resolve_mark(ctx, &name.to_string()?);
    }
    let Some(n) = value.as_number() else {
      return Err(Exception::throw_type(
        ctx,
        "a performance timestamp must be a mark name or a number",
      ));
    };
    if n < 0.0 {
      return Err(Exception::throw_type(ctx, "a performance timestamp cannot be negative"));
    }
    Ok(n)
  }
}

#[rquickjs::methods(rename_all = "camelCase")]
impl<'js> PerformanceJs<'js> {
  #[qjs(constructor)]
  fn constructor(ctx: Ctx<'js>) -> rquickjs::Result<Self> {
    Err(Exception::throw_type(&ctx, "Illegal constructor"))
  }

  /// Unix-epoch milliseconds the monotonic readings are relative to.
  #[qjs(get)]
  fn time_origin(&self) -> f64 {
    time_origin_ms()
  }

  /// Monotonic milliseconds since `timeOrigin`.
  fn now(&self) -> f64 {
    now_ms()
  }

  #[qjs(rename = "toJSON")]
  fn to_json(&self, ctx: Ctx<'js>) -> rquickjs::Result<Object<'js>> {
    let o = Object::new(ctx)?;
    o.set("timeOrigin", time_origin_ms())?;
    Ok(o)
  }

  /// `mark(name, { detail?, startTime? })`.
  fn mark(&mut self, ctx: Ctx<'js>, name: String, options: Opt<Value<'js>>) -> rquickjs::Result<Value<'js>> {
    let (start_time, detail) = mark_options(&ctx, options.0.as_ref())?;

    let entry = Class::instance(
      ctx.clone(),
      PerformanceMarkJs {
        name: name.clone(),
        start_time,
        detail,
      },
    )?;
    let value = entry.into_value();
    self.entries.push(Buffered {
      name,
      is_mark: true,
      start_time,
      value: value.clone(),
    });
    Ok(value)
  }

  /// `measure(name, startMarkOrOptions?, endMark?)`.
  ///
  /// The three-way overload the spec defines: a bare name measures from
  /// the time origin to now; a string names the start mark; an options
  /// bag carries any two of `start` / `end` / `duration` and the third
  /// is derived.
  fn measure(
    &mut self,
    ctx: Ctx<'js>,
    name: String,
    start_or_options: Opt<Value<'js>>,
    end_mark: Opt<Value<'js>>,
  ) -> rquickjs::Result<Value<'js>> {
    let arg = start_or_options.0.filter(|v| !v.is_undefined());
    let end_arg = end_mark.0.filter(|v| !v.is_undefined());
    let options = arg.as_ref().and_then(|v| v.as_object()).filter(|o| !o.is_array());

    let mut detail = Value::new_null(ctx.clone());
    let (start_time, duration) = if let Some(options) = options {
      let get = |key: &str| -> Option<Value<'js>> {
        options.get::<_, Value<'js>>(key).ok().filter(|v| !v.is_undefined())
      };
      let (start, end, dur) = (get("start"), get("end"), get("duration"));

      // Both an options bag and a trailing endMark is ambiguous about
      // which one wins, so the spec refuses rather than picking.
      if end_arg.is_some() {
        return Err(Exception::throw_type(
          &ctx,
          "measure() takes an endMark or a PerformanceMeasureOptions object, not both",
        ));
      }
      if start.is_none() && end.is_none() && dur.is_none() {
        return Err(Exception::throw_type(
          &ctx,
          "PerformanceMeasureOptions must set at least one of start, end or duration",
        ));
      }
      // All three over-constrain the interval: they can disagree.
      if start.is_some() && end.is_some() && dur.is_some() {
        return Err(Exception::throw_type(
          &ctx,
          "PerformanceMeasureOptions cannot set all of start, end and duration",
        ));
      }
      if let Some(given) = get("detail") {
        detail = given;
      }

      let dur = dur.map(|d| self.resolve_timestamp(&ctx, &d)).transpose()?;
      let start = start.map(|s| self.resolve_timestamp(&ctx, &s)).transpose()?;
      let end = end.map(|e| self.resolve_timestamp(&ctx, &e)).transpose()?;

      match (start, end, dur) {
        (Some(s), Some(e), _) => (s, e - s),
        (Some(s), None, Some(d)) => (s, d),
        (None, Some(e), Some(d)) => (e - d, d),
        (Some(s), None, None) => (s, now_ms() - s),
        (None, Some(e), None) => (0.0, e),
        (None, None, Some(d)) => (now_ms() - d, d),
        (None, None, None) => unreachable!("the all-absent case is refused above"),
      }
    } else {
      let start = match &arg {
        Some(v) => self.resolve_timestamp(&ctx, v)?,
        None => 0.0,
      };
      let end = match &end_arg {
        Some(v) => self.resolve_timestamp(&ctx, v)?,
        None => now_ms(),
      };
      (start, end - start)
    };

    let entry = Class::instance(
      ctx.clone(),
      PerformanceMeasureJs {
        name: name.clone(),
        start_time,
        duration,
        detail,
      },
    )?;
    let value = entry.into_value();
    self.entries.push(Buffered {
      name,
      is_mark: false,
      start_time,
      value: value.clone(),
    });
    Ok(value)
  }

  /// Drop every mark, or every mark with `name`.
  fn clear_marks(&mut self, name: Opt<String>) {
    match name.0 {
      Some(name) => self.entries.retain(|e| !(e.is_mark && e.name == name)),
      None => self.entries.retain(|e| !e.is_mark),
    }
  }

  /// Drop every measure, or every measure with `name`.
  fn clear_measures(&mut self, name: Opt<String>) {
    match name.0 {
      Some(name) => self.entries.retain(|e| !(!e.is_mark && e.name == name)),
      None => self.entries.retain(|e| e.is_mark),
    }
  }

  /// Every entry, in chronological order of `startTime`.
  ///
  /// Insertion order is not enough: `mark(name, { startTime })` can
  /// backdate an entry, so a later call may belong earlier on the
  /// timeline. The sort is stable, so entries sharing a `startTime`
  /// keep the order they were recorded in.
  fn get_entries(&self) -> Vec<Value<'js>> {
    let mut out: Vec<&Buffered<'js>> = self.entries.iter().collect();
    out.sort_by(|a, b| a.start_time.total_cmp(&b.start_time));
    out.into_iter().map(|e| e.value.clone()).collect()
  }

  fn get_entries_by_name(&self, name: String, entry_type: Opt<String>) -> Vec<Value<'js>> {
    let wanted = entry_type.0;
    let mut out: Vec<&Buffered<'js>> = self
      .entries
      .iter()
      .filter(|e| e.name == name)
      .filter(|e| match wanted.as_deref() {
        Some(t) => t == if e.is_mark { MARK } else { MEASURE },
        None => true,
      })
      .collect();
    out.sort_by(|a, b| a.start_time.total_cmp(&b.start_time));
    out.into_iter().map(|e| e.value.clone()).collect()
  }

  fn get_entries_by_type(&self, entry_type: String) -> Vec<Value<'js>> {
    let want_mark = entry_type == MARK;
    if !want_mark && entry_type != MEASURE {
      return Vec::new();
    }
    let mut out: Vec<&Buffered<'js>> = self.entries.iter().filter(|e| e.is_mark == want_mark).collect();
    out.sort_by(|a, b| a.start_time.total_cmp(&b.start_time));
    out.into_iter().map(|e| e.value.clone()).collect()
  }
}

/// Define the four classes and install the `performance` instance.
///
/// # Errors
///
/// Propagates the class definitions and the global write.
pub fn init(ctx: &Ctx<'_>) -> rquickjs::Result<()> {
  let globals = ctx.globals();
  Class::<PerformanceEntryJs>::define(&globals)?;
  Class::<PerformanceMarkJs>::define(&globals)?;
  Class::<PerformanceMeasureJs>::define(&globals)?;
  Class::<PerformanceJs>::define(&globals)?;
  chain_entry_prototypes(ctx)?;

  let performance = Class::instance(ctx.clone(), PerformanceJs::new())?;
  globals.set("performance", performance)?;
  Ok(())
}

/// Point `PerformanceMark.prototype` and `PerformanceMeasure.prototype`
/// at `PerformanceEntry.prototype`, which is what makes a mark an
/// instance of `PerformanceEntry` — the relationship the timeline's
/// whole type hierarchy is expressed in.
fn chain_entry_prototypes(ctx: &Ctx<'_>) -> rquickjs::Result<()> {
  let Some(entry_proto) = Class::<PerformanceEntryJs>::prototype(ctx)? else {
    return Ok(());
  };
  if let Some(mark_proto) = Class::<PerformanceMarkJs>::prototype(ctx)? {
    mark_proto.set_prototype(Some(&entry_proto))?;
  }
  if let Some(measure_proto) = Class::<PerformanceMeasureJs>::prototype(ctx)? {
    measure_proto.set_prototype(Some(&entry_proto))?;
  }
  Ok(())
}