Skip to content

Commit 90e303f

Browse files
committed
feat(search): lint & beautify, update content type
1 parent 5d60ab1 commit 90e303f

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

src/app/api/search/route.ts

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ export const POST = async (req: Request) => {
146146
}
147147
} catch (error) {
148148
reject(
149-
Response.json({ message: 'Error parsing data' }, { status: 500 }),
149+
Response.json(
150+
{ message: 'Error parsing data' },
151+
{ status: 500 },
152+
),
150153
);
151154
}
152155
});
@@ -157,93 +160,102 @@ export const POST = async (req: Request) => {
157160

158161
emitter.on('error', (error: any) => {
159162
reject(
160-
Response.json({ message: 'Search error', error }, { status: 500 }),
163+
Response.json(
164+
{ message: 'Search error', error },
165+
{ status: 500 },
166+
),
161167
);
162168
});
163169
},
164170
);
165171
}
166172

167173
const encoder = new TextEncoder();
168-
169-
// Create an AbortController to handle cancellation
174+
170175
const abortController = new AbortController();
171176
const { signal } = abortController;
172-
177+
173178
const stream = new ReadableStream({
174179
start(controller) {
175180
let sources: any[] = [];
176181

177-
// Send an initial message to keep the connection alive
178-
controller.enqueue(encoder.encode(JSON.stringify({
179-
type: 'init',
180-
data: 'Stream connected'
181-
}) + '\n'));
182+
controller.enqueue(
183+
encoder.encode(
184+
JSON.stringify({
185+
type: 'init',
186+
data: 'Stream connected',
187+
}) + '\n',
188+
),
189+
);
182190

183-
// Set up cleanup function for when client disconnects
184191
signal.addEventListener('abort', () => {
185-
// Remove all listeners from emitter to prevent memory leaks
186192
emitter.removeAllListeners();
187-
188-
// Close the controller if it's still active
193+
189194
try {
190195
controller.close();
191-
} catch (error) {
192-
// Controller might already be closed
193-
}
196+
} catch (error) {}
194197
});
195198

196199
emitter.on('data', (data: string) => {
197-
// Check if request has been cancelled before processing
198200
if (signal.aborted) return;
199-
201+
200202
try {
201203
const parsedData = JSON.parse(data);
202-
204+
203205
if (parsedData.type === 'response') {
204-
controller.enqueue(encoder.encode(JSON.stringify({
205-
type: 'response',
206-
data: parsedData.data
207-
}) + '\n'));
206+
controller.enqueue(
207+
encoder.encode(
208+
JSON.stringify({
209+
type: 'response',
210+
data: parsedData.data,
211+
}) + '\n',
212+
),
213+
);
208214
} else if (parsedData.type === 'sources') {
209215
sources = parsedData.data;
210-
controller.enqueue(encoder.encode(JSON.stringify({
211-
type: 'sources',
212-
data: sources
213-
}) + '\n'));
216+
controller.enqueue(
217+
encoder.encode(
218+
JSON.stringify({
219+
type: 'sources',
220+
data: sources,
221+
}) + '\n',
222+
),
223+
);
214224
}
215225
} catch (error) {
216226
controller.error(error);
217227
}
218228
});
219229

220230
emitter.on('end', () => {
221-
// Check if request has been cancelled before processing
222231
if (signal.aborted) return;
223-
224-
controller.enqueue(encoder.encode(JSON.stringify({
225-
type: 'done'
226-
}) + '\n'));
232+
233+
controller.enqueue(
234+
encoder.encode(
235+
JSON.stringify({
236+
type: 'done',
237+
}) + '\n',
238+
),
239+
);
227240
controller.close();
228241
});
229242

230243
emitter.on('error', (error: any) => {
231-
// Check if request has been cancelled before processing
232244
if (signal.aborted) return;
233-
245+
234246
controller.error(error);
235247
});
236248
},
237249
cancel() {
238250
abortController.abort();
239-
}
251+
},
240252
});
241253

242254
return new Response(stream, {
243255
headers: {
244-
'Content-Type': 'application/json',
256+
'Content-Type': 'text/event-stream',
245257
'Cache-Control': 'no-cache, no-transform',
246-
'Connection': 'keep-alive',
258+
Connection: 'keep-alive',
247259
},
248260
});
249261
} catch (err: any) {

0 commit comments

Comments
 (0)