rari 0.13.2

rari: Runtime Accelerated Rendering Infrastructure - A high-performance React Server Components framework powered by Rust
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/* eslint-disable no-undef */
const TSX_DEFAULT_REGEX = /\/([^/]+)\.tsx#/

if (typeof globalThis !== 'undefined') {
  globalThis['~rsc'] = globalThis['~rsc'] || {}
  if (typeof globalThis['~rsc'].keyCounter === 'undefined')
    globalThis['~rsc'].keyCounter = 0
}

if (typeof globalThis !== 'undefined' && !globalThis['~suspense']) {
  globalThis['~suspense'] = {
    streaming: true,
    promises: {},
    boundaryProps: {},
    discoveredBoundaries: [],
    pendingPromises: [],
    currentBoundaryId: null,
  }
}

async function traverseToRsc(element, clientComponents = {}, depth = 0) {
  if (depth > 100) {
    console.error(
      'RSC traversal depth limit exceeded, returning null to prevent stack overflow',
    )
    return null
  }
  if (!element)
    return null

  if (element && typeof element === 'object' && typeof element.then === 'function') {
    const isInSuspense = globalThis['~suspense']?.currentBoundaryId

    if (isInSuspense) {
      const promiseId = `promise:${globalThis['~rsc'].keyCounter++}`

      if (!globalThis['~suspense'].pendingPromises)
        globalThis['~suspense'].pendingPromises = []

      if (!globalThis['~suspense'].promises)
        globalThis['~suspense'].promises = {}

      globalThis['~suspense'].promises[promiseId] = element
      globalThis['~suspense'].pendingPromises.push({
        id: promiseId,
        boundaryId: globalThis['~suspense'].currentBoundaryId,
        componentPath: 'AsyncPromise',
      })

      return null
    }

    try {
      element = await element
    }
    catch (error) {
      console.error('Error awaiting Promise in RSC traversal:', error)
      return createErrorElement(
        error.message || String(error),
        'AsyncComponent',
      )
    }
  }

  if (
    typeof element === 'string'
    || typeof element === 'number'
    || typeof element === 'boolean'
  ) {
    if (typeof element === 'string' && element.length > 0 && element[0] === '$') {
      return `$${element}`
    }

    return element
  }

  if (Array.isArray(element)) {
    const results = []
    for (const child of element)
      results.push(await traverseToRsc(child, clientComponents, depth + 1))

    return results
  }

  if (
    element
    && typeof element === 'object'
    && element.$$typeof === Symbol.for('react.transitional.element')
    && element.type === Symbol.for('react.fragment')
  ) {
    return await traverseToRsc(element.props.children, clientComponents, depth + 1)
  }

  if (
    element
    && typeof element === 'object'
    && element.$$typeof === Symbol.for('react.transitional.element')
  ) {
    return await traverseReactElement(element, clientComponents, depth + 1)
  }

  if (element && typeof element === 'object' && !element.$$typeof) {
    if (element['~preSerializedSuspense'] && element.rscArray)
      return element.rscArray

    if (element.type && (typeof element.type === 'string' || typeof element.type === 'function' || typeof element.type === 'object')) {
      const hasPropsChildren = element.props && Object.hasOwn(element.props || {}, 'children')
      const mergedProps = {
        ...(element.props || {}),
        children: hasPropsChildren ? element.props.children : element.children,
      }

      const fakeElement = {
        $$typeof: Symbol.for('react.transitional.element'),
        type: element.type,
        props: mergedProps,
        key: element.key ?? null,
      }
      return await traverseReactElement(fakeElement, clientComponents, depth + 1)
    }

    if (!element.type && element.props && Object.hasOwn(element.props, 'fallback')) {
      const mergedProps = {
        ...element.props,
        children: Object.hasOwn(element.props, 'children')
          ? element.props.children
          : element.children,
      }

      const fakeSuspenseElement = {
        $$typeof: Symbol.for('react.transitional.element'),
        type: 'suspense',
        props: mergedProps,
        key: element.key ?? null,
      }

      return await traverseReactElement(fakeSuspenseElement, clientComponents, depth + 1)
    }

    if (element['~rari_lazy'] === true) {
      return {
        '~rari_lazy': true,
        '~rari_promise_id': element['~rari_promise_id'],
        '~rari_component_id': element['~rari_component_id'],
        '~rari_loading_id': element['~rari_loading_id'],
      }
    }

    return null
  }

  return element
}

async function traverseReactElement(element, clientComponents, depth = 0) {
  const { type, props, key } = element

  const uniqueKey = key || `element:${globalThis['~rsc'].keyCounter++}`

  if (isSuspenseComponent(type)) {
    const boundaryId = props?.['~boundaryId'] || `boundary:${globalThis['~rsc'].keyCounter++}`

    if (!globalThis['~suspense'])
      globalThis['~suspense'] = {}
    if (!globalThis['~suspense'].discoveredBoundaries)
      globalThis['~suspense'].discoveredBoundaries = []
    if (!globalThis['~suspense'].pendingPromises)
      globalThis['~suspense'].pendingPromises = []
    if (!globalThis['~suspense'].promises)
      globalThis['~suspense'].promises = {}

    const previousBoundaryId = globalThis['~suspense'].currentBoundaryId
    globalThis['~suspense'].currentBoundaryId = boundaryId

    const defaultFallback = null
    const safeFallback = props?.fallback
      ? await traverseToRsc(props.fallback, clientComponents, depth + 1)
      : defaultFallback

    globalThis['~suspense'].discoveredBoundaries.push({
      id: boundaryId,
      fallback: safeFallback,
      parentId: previousBoundaryId,
    })

    const processedChildren = Array.isArray(props?.children)
      ? props.children
      : [props?.children]

    function detectAsyncComponents(children, depth = 0) {
      if (depth > 10)
        return

      const childArray = Array.isArray(children) ? children : [children]

      for (const child of childArray) {
        if (!child || typeof child !== 'object')
          continue

        if (typeof child.then === 'function') {
          const promiseId = `promise:${globalThis['~rsc'].keyCounter++}`

          if (!globalThis['~suspense'].promises)
            globalThis['~suspense'].promises = {}

          globalThis['~suspense'].promises[promiseId] = child
          globalThis['~suspense'].pendingPromises.push({
            id: promiseId,
            boundaryId,
            componentPath: 'AsyncComponent',
          })

          continue
        }

        if (child.$$typeof === Symbol.for('react.transitional.element') && typeof child.type === 'function') {
          try {
            const isAsyncMarker = child.type._isAsyncComponent && child.type._originalType
            const isAsync = isAsyncMarker
              || child.type.constructor.name === 'AsyncFunction'
              || child.type.toString().trim().startsWith('async ')

            if (isAsync) {
              const promiseId = `promise:${globalThis['~rsc'].keyCounter++}`

              const actualType = isAsyncMarker ? child.type._originalType : child.type

              globalThis['~suspense'].pendingPromises.push({
                id: promiseId,
                boundaryId,
                componentPath: actualType.name || 'anonymous',
                componentType: actualType,
                componentProps: child.props || {},
              })
            }
          }
          catch (error) {
            console.error('Error detecting async component:', error)
          }
        }

        if (child.props && child.props.children)
          detectAsyncComponents(child.props.children, depth + 1)
      }
    }

    detectAsyncComponents(processedChildren)

    const hasPendingPromises = globalThis['~suspense'].pendingPromises.some(
      p => p.boundaryId === boundaryId,
    )

    const hasLazyMarker = processedChildren.some((child) => {
      const isLazy = child && typeof child === 'object' && child['~rari_lazy'] === true
      return isLazy
    })

    let traversedChildren
    if (hasPendingPromises) {
      const isStreaming = globalThis['~rari']?.streaming?.enabled === true

      if (isStreaming) {
        if (!globalThis['~rari'].lazy)
          globalThis['~rari'].lazy = { pending: new Map(), resolved: new Map(), counter: 0 }

        const boundaryPromises = globalThis['~suspense'].pendingPromises.filter(
          p => p.boundaryId === boundaryId,
        )

        for (const pending of boundaryPromises) {
          if (!pending.componentType && globalThis['~suspense'].promises?.[pending.id]) {
            globalThis['~rari'].lazy.pending.set(pending.id, globalThis['~suspense'].promises[pending.id])
          }
          else {
            globalThis['~rari'].lazy.pending.set(pending.id, {
              component: pending.componentType,
              props: pending.componentProps,
              isDeferred: true,
            })
          }
        }

        const lazyMarkers = []
        for (const pending of boundaryPromises) {
          lazyMarkers.push({
            '~rari_lazy': true,
            '~rari_promise_id': pending.id,
            '~rari_component_id': pending.componentPath || 'AsyncComponent',
            '~rari_loading_id': '',
          })
        }

        let traversedChildrenValue
        if (lazyMarkers.length === 1)
          traversedChildrenValue = lazyMarkers[0]
        else if (lazyMarkers.length > 0)
          traversedChildrenValue = lazyMarkers
        else
          traversedChildrenValue = null
        traversedChildren = traversedChildrenValue
      }
      else {
        const boundaryPromises = globalThis['~suspense'].pendingPromises.filter(
          p => p.boundaryId === boundaryId,
        )

        const resolvedComponents = []
        for (const pending of boundaryPromises) {
          if (pending.componentType && pending.componentProps !== undefined) {
            try {
              const result = await pending.componentType(pending.componentProps)
              const traversed = await traverseToRsc(result, clientComponents, depth + 1)
              resolvedComponents.push(traversed)
            }
            catch (error) {
              console.error('Error rendering async component:', error)
              resolvedComponents.push(createErrorElement(error.message, pending.componentPath))
            }
          }
        }

        globalThis['~suspense'].pendingPromises = globalThis['~suspense'].pendingPromises.filter(
          p => p.boundaryId !== boundaryId,
        )

        traversedChildren = resolvedComponents.length === 1 ? resolvedComponents[0] : resolvedComponents
      }
    }
    else if (hasLazyMarker) {
      traversedChildren = await traverseToRsc(props?.children, clientComponents, depth + 1)
    }
    else {
      traversedChildren = await traverseToRsc(props?.children, clientComponents, depth + 1)
    }

    globalThis['~suspense'].currentBoundaryId = previousBoundaryId

    const rscProps = {
      ...props,
      '~boundaryId': boundaryId,
      'fallback': safeFallback,
      'children': traversedChildren,
    }

    return [
      '$',
      'react.suspense',
      null,
      rscProps,
    ]
  }

  if (typeof type === 'function' && type._isAsyncComponent && type._originalType) {
    const asyncType = type._originalType
    const isInSuspense = globalThis['~suspense']?.currentBoundaryId

    if (isInSuspense) {
      const promiseId = `promise:${globalThis['~rsc'].keyCounter++}`

      if (!globalThis['~suspense'].pendingPromises)
        globalThis['~suspense'].pendingPromises = []

      globalThis['~suspense'].pendingPromises.push({
        id: promiseId,
        boundaryId: globalThis['~suspense'].currentBoundaryId,
        componentPath: asyncType.name || 'AsyncComponent',
        componentType: asyncType,
        componentProps: props || {},
      })

      return null
    }

    const result = await asyncType(props)
    return await traverseToRsc(result, clientComponents, depth + 1)
  }

  if (isClientComponent(type, clientComponents)) {
    const componentId = getClientComponentId(type, clientComponents)

    if (componentId && componentId !== null) {
      if (componentId.includes('#')) {
        const [filePath, exportName] = componentId.split('#')
        if (
          typeof globalThis['~rari']?.bridge !== 'undefined'
          && typeof globalThis['~rari'].bridge.registerClientReference === 'function'
        ) {
          try {
            globalThis['~rari'].bridge.registerClientReference(componentId, filePath, exportName)
          }
          catch (error) {
            console.error('Failed to register client reference:', error)
          }
        }
      }

      const processedProps = {}
      if (props) {
        for (const [key, value] of Object.entries(props)) {
          if (key === 'children')
            processedProps[key] = await traverseToRsc(value, clientComponents, depth + 1)
          else
            processedProps[key] = value
        }
      }

      return ['$', componentId, uniqueKey, processedProps]
    }
    else {
      return [
        '$',
        'div',
        uniqueKey,
        {
          'className': 'rsc-unresolved-client',
          'data-rsc-error': 'unresolved-client-component',
          'style': {
            border: '2px dashed #fdcb6e',
            padding: '8px',
            margin: '4px',
            backgroundColor: '#fff9e6',
            color: '#e17055',
          },
          'children': 'WARNING: Unresolved client component',
        },
      ]
    }
  }

  if (type && typeof type === 'object' && Object.keys(type).length === 0) {
    return [
      '$',
      'div',
      uniqueKey,
      {
        'className': 'rsc-missing-component',
        'data-rsc-error': 'empty-object',
        'style': {
          border: '2px dashed #ff6b6b',
          padding: '8px',
          margin: '4px',
          backgroundColor: '#ffe0e0',
          color: '#d63031',
        },
        'children': 'WARNING: Component failed to load (empty object)',
      },
    ]
  }

  if (isServerComponent(type)) {
    const isAsync = typeof type === 'function' && type.constructor.name === 'AsyncFunction'
    const isInSuspense = globalThis['~suspense']?.currentBoundaryId

    if (isAsync && isInSuspense) {
      const promiseId = `promise:${globalThis['~rsc'].keyCounter++}`

      if (!globalThis['~suspense'].pendingPromises)
        globalThis['~suspense'].pendingPromises = []

      globalThis['~suspense'].pendingPromises.push({
        id: promiseId,
        boundaryId: globalThis['~suspense'].currentBoundaryId,
        componentPath: type.name || 'anonymous',
        componentType: type,
        componentProps: props || {},
      })

      return null
    }

    const rendered = renderServerComponent(element)
    return await traverseToRsc(rendered, clientComponents, depth + 1)
  }

  if (typeof type === 'string') {
    return await createRSCHTMLElement(
      type,
      props,
      uniqueKey,
      clientComponents,
      depth,
    )
  }

  if (typeof type === 'function') {
    try {
      let rendered = type(props)

      if (rendered && typeof rendered.then === 'function')
        rendered = await rendered

      if (rendered === element)
        return null

      return await traverseToRsc(rendered, clientComponents, depth + 1)
    }
    catch (error) {
      console.error('Error rendering function component:', error)
      return createErrorElement(
        error.message,
        type.name || 'FunctionComponent',
      )
    }
  }

  if (type === React.Fragment)
    return await traverseToRsc(props.children, clientComponents, depth + 1)

  if (type === Symbol.for('react.fragment'))
    return await traverseToRsc(props.children, clientComponents, depth + 1)

  if (type && type.$$typeof === Symbol.for('react.provider'))
    return await traverseToRsc(props.children, clientComponents, depth + 1)

  if (type && type.$$typeof === Symbol.for('react.consumer'))
    return await traverseToRsc(props.children, clientComponents, depth + 1)

  return [
    '$',
    'div',
    uniqueKey,
    {
      'className': 'rsc-unknown-component',
      'data-rsc-error': 'unknown-component-type',
      'style': {
        border: '2px dashed #74b9ff',
        padding: '8px',
        margin: '4px',
        backgroundColor: '#e6f3ff',
        color: '#0984e3',
      },
      'children': 'WARNING: Unknown component type',
    },
  ]
}

async function createRSCHTMLElement(
  tagName,
  props,
  key,
  clientComponents,
  depth = 0,
) {
  const { children, ...otherProps } = props || {}

  const rscProps = {
    ...otherProps,
    children: children
      ? await traverseToRsc(children, clientComponents, depth + 1)
      : undefined,
  }

  if (rscProps.children === undefined || rscProps.children === null)
    delete rscProps.children

  const uniqueKey = key || `${tagName}:${globalThis['~rsc'].keyCounter++}`
  return ['$', tagName, uniqueKey, rscProps]
}

async function renderServerComponent(element) {
  const { type: Component, props } = element

  try {
    let result
    if (Component.constructor.name === 'AsyncFunction') {
      result = await Component(props)
    }
    else {
      result = Component(props)
      if (result && typeof result.then === 'function')
        result = await result
    }

    return result
  }
  catch (error) {
    console.error('Error rendering server component:', error)
    return createErrorElement(
      error.message,
      Component.name || 'ServerComponent',
    )
  }
}

function isClientComponent(componentType, clientComponents) {
  if (
    componentType
    && componentType.$$typeof === Symbol.for('react.client.reference')
  ) {
    return true
  }

  if (clientComponents && typeof componentType === 'function') {
    const componentName = componentType.name || componentType.displayName
    if (componentName && clientComponents[componentName])
      return true
  }

  if (
    componentType
    && typeof componentType === 'object'
    && Object.keys(componentType).length === 0
  ) {
    return true
  }

  if (componentType && componentType['~isClientComponent'])
    return true

  if (typeof componentType === 'string') {
    if (
      componentType.includes('tsx#')
      || componentType.includes('use client')
    ) {
      return true
    }
  }

  return false
}

function isServerComponent(componentType) {
  if (componentType && componentType['~isServerComponent'])
    return true

  if (
    typeof componentType === 'function'
    && componentType.constructor.name === 'AsyncFunction'
  ) {
    return true
  }

  return false
}

function getClientComponentId(componentType, clientComponents) {
  if (componentType && (typeof componentType === 'object' || typeof componentType === 'function')) {
    const reactClientSymbol = Symbol.for('react.client.reference')
    if (componentType.$$typeof === reactClientSymbol) {
      const clientId = componentType.$$id
      if (clientId)
        return clientId
    }
  }

  if (clientComponents && typeof componentType === 'function') {
    const componentName = componentType.name || componentType.displayName
    if (componentName && clientComponents[componentName])
      return clientComponents[componentName].id || componentName
  }

  if (
    componentType
    && typeof componentType === 'object'
    && Object.keys(componentType).length === 0
  ) {
    return null
  }

  if (typeof componentType === 'string') {
    const componentNames = globalThis['~clientComponentNames'] || {}

    let componentName = componentType
    if (componentType.includes('tsx#default')) {
      const match = componentType.match(TSX_DEFAULT_REGEX)
      if (match)
        componentName = match[1]
    }

    if (componentNames[componentName])
      return componentNames[componentName]

    for (const [name, id] of Object.entries(componentNames)) {
      if (
        name.toLowerCase().includes(componentName.toLowerCase())
        || componentName.toLowerCase().includes(name.toLowerCase())
      ) {
        return id
      }
    }

    return null
  }

  const componentName
    = componentType.name || componentType.displayName || 'UnknownClient'

  if (componentName.startsWith('$L') || componentName.startsWith('client'))
    return componentName

  return null
}

function createErrorElement(message, componentName) {
  const errorId = `error:${globalThis['~rsc'].keyCounter++}`
  return [
    '$',
    'div',
    errorId,
    {
      style: {
        color: 'red',
        border: '1px solid red',
        padding: '10px',
        margin: '10px',
      },
      children: [
        [
          '$',
          'h3',
          `${errorId}-h3`,
          {
            children: `Error in ${componentName}`,
          },
        ],
        [
          '$',
          'p',
          `${errorId}-p`,
          {
            children: message,
          },
        ],
      ],
    },
  ]
}

async function renderToRsc(element, clientComponents = {}) {
  try {
    globalThis['~rsc'].keyCounter = 0
    return await traverseToRsc(element, clientComponents)
  }
  catch (error) {
    console.error('Error in RSC traversal:', error)
    return createErrorElement(error.message, 'RootComponent')
  }
}

function isSuspenseComponent(type) {
  if (
    typeof React !== 'undefined'
    && React.Suspense
    && type === React.Suspense
  ) {
    return true
  }

  if (type && type.$$typeof === Symbol.for('react.suspense'))
    return true

  if (type === Symbol.for('react.suspense'))
    return true

  if (
    typeof type === 'function'
    && (type.name === 'Suspense' || type.displayName === 'Suspense')
  ) {
    return true
  }

  if (type === 'suspense')
    return true

  return false
}

if (typeof globalThis !== 'undefined') {
  globalThis.traverseToRsc = traverseToRsc
  globalThis.renderToRsc = renderToRsc
  globalThis.isClientComponent = isClientComponent
  globalThis.isServerComponent = isServerComponent
  globalThis.getClientComponentId = getClientComponentId
  globalThis.isSuspenseComponent = isSuspenseComponent
}