Open History to see the last commit
1// TODO: RSCs
2import { isValidElement } from 'react'
3import invariant from 'tiny-invariant'
4import type React from 'react'
5β
6export function renderRsc(input: any): React.JSX.Element {
7 if (isValidElement(input)) {
8 return input
9 }
10β
11 if (typeof input === 'object' && !input.state) {
12 input.state = {
13 status: 'pending',
14 promise: Promise.resolve()
15 .then(() => {
16 let element
17β
18 // We're in node
19 // TODO: RSCs
20 // if (reactDom.createFromNodeStream) {
21 // const stream = await import('node:stream')
22β
23 // let body: any = input
24β
25 // // Unwrap the response
26 // if (input instanceof Response) {
27 // body = input.body
28 // }
29β
30 // // Convert ReadableStream to NodeJS stream.Readable
31 // if (body instanceof ReadableStream) {
32 // body = stream.Readable.fromWeb(body as any)
33 // }
34β
35 // if (stream.Readable.isReadable(body)) {
36 // // body = copyStreamToRaw(body)
37 // } else if (input.text) {
38 // // create a readable stream by awaiting the text method
39 // body = new stream.Readable({
40 // async read() {
41 // input.text().then((value: any) => {
42 // this.push(value)
43 // this.push(null)
44 // })
45 // },
46 // })
47 // } else {
48 // console.error('input', input)
49 // throw new Error('Unexpected rsc input type π')
50 // }
51β
52 // element = await reactDom.createFromNodeStream(body)
53 // } else {
54 // // We're in the browser
55 // if (input.body instanceof ReadableStream) {
56 // input = input.body
57 // }
58β
59 // if (input instanceof ReadableStream) {
60 // element = await reactDom.createFromReadableStream(input)
61 // }
62β
63 // if (input instanceof Response) {
64 // // copy to the response body to cache the raw data
65 // element = await reactDom.createFromFetch(input)
66 // }
67 // }
68β
69 // return element
70β
71 invariant(false, 'renderRSC() is coming soon!')
72 })
73 .then((element) => {
74 input.state.value = element
75 input.state.status = 'success'
76 })
77 .catch((err) => {
78 input.state.status = 'error'
79 input.state.error = err
80 }),
81 }
82 }
83β
84 if (input.state.status === 'pending') {
85 throw input.state.promise
86 }
87β
88 return input.state.value
89}