MCPcopy Create free account
hub / github.com/callstackincubator/react-native-fast-io / createServer

Function createServer

example/server/createServer.ts:6–71  ·  view source on GitHub ↗
(payload: string | Bun.BufferSource, port: number)

Source from the content-addressed store, hash-verified

4 * Creates a WebSocket server that sends configurable responses and handles file uploads
5 */
6export function createServer(payload: string | Bun.BufferSource, port: number) {
7 const server = Bun.serve({
8 async fetch(req, server) {
9 // Handle WebSocket upgrade
10 if (server.upgrade(req)) {
11 return
12 }
13
14 // Handle POST /upload
15 if (req.method === 'POST' && req.url.endsWith('/upload')) {
16 try {
17 console.log('Waiting for body...')
18
19 if (!req.body) {
20 return new Response('No body provided', { status: 400 })
21 }
22
23 const writer = Bun.file('uploaded_file.jpg.gz').writer()
24
25 for await (const chunk of req.body) {
26 // Write each chunk to file
27 writer.write(chunk)
28 // Debug
29 console.log('Chunk saved:', chunk.length, 'bytes')
30 }
31
32 await writer.end()
33
34 console.log('Upload complete, file saved')
35
36 return new Response('Upload successful', { status: 200 })
37 } catch (error) {
38 console.error('Upload error:', error)
39 return new Response('Upload failed', { status: 500 })
40 }
41 }
42
43 // Default response for other routes
44 return new Response('Not found', { status: 404 })
45 },
46 websocket: {
47 message(ws, message: string) {
48 if (typeof message !== 'string') {
49 return
50 }
51 const count = parseInt(message)
52 if (isNaN(count)) {
53 return
54 }
55 try {
56 for (let i = 0; i < count; i++) {
57 ws.send(payload)
58 }
59 } catch (error) {
60 console.error('Failed to parse message:', error)
61 }
62 },
63 },

Callers 1

runFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected