Skip to content

Commit

Permalink
chore(rsc): Refactor/format code that sends Flight to Studio
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Oct 19, 2024
1 parent bdbc40e commit 47dc368
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
96 changes: 48 additions & 48 deletions packages/vite/src/rsc/rscStudioHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,64 +52,63 @@ const processRenderRscStream = async (
}

const postFlightToStudio = (payload: string, metadata: Record<string, any>) => {
if (shouldSendToStudio()) {
const base64Payload = Buffer.from(payload).toString('base64')
const encodedMetadata = Buffer.from(JSON.stringify(metadata)).toString(
'base64',
)
const jsonBody = JSON.stringify({
flight: {
encodedPayload: base64Payload,
encoding: 'base64',
encodedMetadata,
},
})
if (!shouldSendToStudio()) {
return
}

// Options to configure the HTTP POST request
// TODO (RSC): Get these from the toml and Studio config
const options = {
hostname: 'localhost',
port: getStudioPort(),
path: '/.redwood/functions/rsc-flight',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(jsonBody),
},
}
const base64Payload = Buffer.from(payload).toString('base64')
const encodedMetadata = Buffer.from(JSON.stringify(metadata)).toString(
'base64',
)
const jsonBody = JSON.stringify({
flight: {
encodedPayload: base64Payload,
encoding: 'base64',
encodedMetadata,
},
})

const req = http.request(options, (res) => {
res.setEncoding('utf8')
})
// Options to configure the HTTP POST request
// TODO (RSC): Get these from the toml and Studio config
const options = {
hostname: 'localhost',
port: getStudioPort(),
path: '/.redwood/functions/rsc-flight',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(jsonBody),
},
}

req.on('error', (e: Error) => {
console.error(
`An error occurred sending the Flight Payload to Studio: ${e.message}`,
)
})
const req = http.request(options, (res) => {
res.setEncoding('utf8')
})

req.write(jsonBody)
req.end()
}
req.on('error', (e: Error) => {
console.error('An error occurred sending the Flight Payload to Studio')
console.error(e)
})

req.write(jsonBody)
req.end()
}

const createStudioFlightHandler = (
readable: ReadableStream,
metadata: Record<string, any>,
) => {
if (shouldSendToStudio()) {
processRenderRscStream(readable)
.then((payload) => {
console.debug('Sending RSC Rendered stream to Studio')
postFlightToStudio(payload, metadata)
console.debug('Sent RSC Rendered stream to Studio', payload, metadata)
})
.catch((error) => {
console.error('An error occurred getting RSC Rendered steam:', error)
})
} else {
console.debug('Studio is not enabled')
}
processRenderRscStream(readable)
.then((payload) => {
console.debug('Sending RSC Rendered stream to Studio')
postFlightToStudio(payload, metadata)
console.debug('Sent RSC Rendered stream to Studio')
console.debug('payload to Studio:', payload)
console.debug('metadata to Studio:', metadata)
})
.catch((error) => {
console.error('An error occurred getting RSC Rendered steam:', error)
})
}

interface StudioRenderInput extends Omit<RenderInput, 'serverState'> {
Expand All @@ -123,6 +122,7 @@ export const sendRscFlightToStudio = async (input: StudioRenderInput) => {
console.debug('Studio is not enabled')
return
}

const { rscId, rsaId, args, basePath, req, handleError } = input

try {
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/runFeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ function createWebSocketServer() {
// Event listener for incoming messages. The `data` is a Buffer
ws.on('message', (data) => {
const message = data.toString()
console.log('Received message:', message)
console.log('runFeServer.ts: Received message:')
console.log(message.slice(0, 120) + '...')

// Broadcast the message to all connected clients
wsServer.clients.forEach((client) => {
Expand Down

0 comments on commit 47dc368

Please sign in to comment.