@taujs/react 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +33 -22
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -345,6 +345,8 @@ function createStreamController(writable, logger) {
345
345
 
346
346
  // src/SSRRender.tsx
347
347
  import { jsx as jsx3 } from "react/jsx-runtime";
348
+ var NOOP = () => {
349
+ };
348
350
  function createRenderer({
349
351
  appComponent,
350
352
  headContent,
@@ -385,7 +387,13 @@ function createRenderer({
385
387
  }
386
388
  };
387
389
  const renderStream = (writable, callbacks = {}, initialData, location, bootstrapModules, meta = {}, cspNonce, signal, opts) => {
388
- const { onAllReady, onError, onHead, onShellReady, onFinish } = callbacks;
390
+ const cb = {
391
+ onHead: callbacks.onHead ?? NOOP,
392
+ onShellReady: callbacks.onShellReady ?? NOOP,
393
+ onAllReady: callbacks.onAllReady ?? NOOP,
394
+ onFinish: callbacks.onFinish ?? NOOP,
395
+ onError: callbacks.onError ?? NOOP
396
+ };
389
397
  const { log, warn, error } = createUILogger(opts?.logger ?? logger, {
390
398
  debugCategory: "ssr",
391
399
  context: { scope: "react-streaming" },
@@ -412,17 +420,17 @@ function createRenderer({
412
420
  const { cleanup: guardsCleanup } = wireWritableGuards(writable, {
413
421
  benignAbort: (why) => controller.benignAbort(why),
414
422
  fatalAbort: (err) => {
415
- onError?.(err);
423
+ cb.onError(err);
416
424
  controller.fatalAbort(err);
417
425
  },
418
- onError,
426
+ onError: cb.onError,
419
427
  onFinish: () => controller.complete("Stream finished (normal completion)")
420
428
  });
421
429
  controller.setGuardsCleanup(guardsCleanup);
422
430
  const stopShellTimer = startShellTimer(effectiveShellTimeout, () => {
423
431
  if (controller.isAborted) return;
424
432
  const timeoutErr = new Error(`Shell not ready after ${effectiveShellTimeout}ms`);
425
- onError?.(timeoutErr);
433
+ cb.onError(timeoutErr);
426
434
  controller.fatalAbort(timeoutErr);
427
435
  });
428
436
  controller.setStopShellTimer(stopShellTimer);
@@ -451,16 +459,15 @@ function createRenderer({
451
459
  }
452
460
  }
453
461
  const head = headContent({ data: headData ?? {}, meta });
454
- const canCork = effectiveUseCork && typeof writable?.cork === "function" && typeof writable?.uncork === "function";
455
- if (canCork) {
462
+ const canCork = effectiveUseCork && typeof writable.cork === "function" && typeof writable.uncork === "function";
463
+ if (canCork)
456
464
  try {
457
465
  writable.cork();
458
466
  } catch {
459
467
  }
460
- }
461
468
  let wroteOk = true;
462
469
  try {
463
- const res = writable?.write ? writable.write(head) : true;
470
+ const res = typeof writable.write === "function" ? writable.write(head) : true;
464
471
  wroteOk = res !== false;
465
472
  } finally {
466
473
  if (canCork) {
@@ -472,21 +479,25 @@ function createRenderer({
472
479
  }
473
480
  let forceWait = false;
474
481
  try {
475
- const ret = onHead?.(head);
476
- forceWait = ret === false;
482
+ forceWait = cb.onHead(head) === false;
477
483
  } catch (cbErr) {
478
484
  warn("onHead callback threw:", cbErr);
479
485
  }
480
486
  const startPipe = () => stream.pipe(writable);
481
- if (forceWait || !wroteOk) writable?.once?.("drain", startPipe);
482
- else startPipe();
487
+ if (forceWait || !wroteOk) {
488
+ if (typeof writable.once === "function") {
489
+ writable.once("drain", startPipe);
490
+ } else {
491
+ startPipe();
492
+ }
493
+ } else startPipe();
483
494
  try {
484
- onShellReady?.();
495
+ cb.onShellReady();
485
496
  } catch (cbErr) {
486
497
  warn("onShellReady callback threw:", cbErr);
487
498
  }
488
499
  } catch (err) {
489
- onError?.(err);
500
+ cb.onError(err);
490
501
  controller.fatalAbort(err);
491
502
  }
492
503
  },
@@ -496,18 +507,18 @@ function createRenderer({
496
507
  const deliver = () => {
497
508
  try {
498
509
  const data = store.getSnapshot();
499
- onAllReady?.(data);
500
- onFinish?.(data);
510
+ cb.onAllReady(data);
511
+ cb.onFinish(data);
501
512
  } catch (thrown) {
502
513
  if (thrown && typeof thrown.then === "function") {
503
514
  thrown.then(deliver).catch((e) => {
504
515
  error("Data promise rejected:", e);
505
- onError?.(e);
516
+ cb.onError(e);
506
517
  controller.fatalAbort(e);
507
518
  });
508
519
  } else {
509
520
  error("Unexpected throw from getSnapshot:", thrown);
510
- onError?.(thrown);
521
+ cb.onError(thrown);
511
522
  controller.fatalAbort(thrown);
512
523
  }
513
524
  }
@@ -520,24 +531,24 @@ function createRenderer({
520
531
  stopShellTimer();
521
532
  } catch {
522
533
  }
523
- onError?.(err);
534
+ cb.onError(err);
524
535
  controller.fatalAbort(err);
525
536
  },
526
537
  onError(err) {
527
538
  if (controller.isAborted) return;
528
539
  const msg = String(err?.message ?? "");
529
- warn?.("React stream error:", msg);
540
+ warn("React stream error:", msg);
530
541
  if (isBenignStreamErr(err)) {
531
542
  controller.benignAbort("Client disconnected before stream finished");
532
543
  return;
533
544
  }
534
- onError?.(err);
545
+ cb.onError(err);
535
546
  controller.fatalAbort(err);
536
547
  }
537
548
  });
538
549
  controller.setStreamAbort(() => stream.abort());
539
550
  } catch (err) {
540
- onError?.(err);
551
+ cb.onError(err);
541
552
  controller.fatalAbort(err);
542
553
  }
543
554
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taujs/react",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "taujs | τjs",
5
5
  "author": "Aoede <taujs@aoede.uk.net> (https://www.aoede.uk.net)",
6
6
  "license": "MIT",