mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-06-15 14:28:32 +00:00
feat(search): lint & beautify, update content type
This commit is contained in:
@ -146,7 +146,10 @@ export const POST = async (req: Request) => {
|
||||
}
|
||||
} catch (error) {
|
||||
reject(
|
||||
Response.json({ message: 'Error parsing data' }, { status: 500 }),
|
||||
Response.json(
|
||||
{ message: 'Error parsing data' },
|
||||
{ status: 500 },
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
@ -157,7 +160,10 @@ export const POST = async (req: Request) => {
|
||||
|
||||
emitter.on('error', (error: any) => {
|
||||
reject(
|
||||
Response.json({ message: 'Search error', error }, { status: 500 }),
|
||||
Response.json(
|
||||
{ message: 'Search error', error },
|
||||
{ status: 500 },
|
||||
),
|
||||
);
|
||||
});
|
||||
},
|
||||
@ -165,52 +171,56 @@ export const POST = async (req: Request) => {
|
||||
}
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
// Create an AbortController to handle cancellation
|
||||
|
||||
const abortController = new AbortController();
|
||||
const { signal } = abortController;
|
||||
|
||||
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
let sources: any[] = [];
|
||||
|
||||
// Send an initial message to keep the connection alive
|
||||
controller.enqueue(encoder.encode(JSON.stringify({
|
||||
type: 'init',
|
||||
data: 'Stream connected'
|
||||
}) + '\n'));
|
||||
controller.enqueue(
|
||||
encoder.encode(
|
||||
JSON.stringify({
|
||||
type: 'init',
|
||||
data: 'Stream connected',
|
||||
}) + '\n',
|
||||
),
|
||||
);
|
||||
|
||||
// Set up cleanup function for when client disconnects
|
||||
signal.addEventListener('abort', () => {
|
||||
// Remove all listeners from emitter to prevent memory leaks
|
||||
emitter.removeAllListeners();
|
||||
|
||||
// Close the controller if it's still active
|
||||
|
||||
try {
|
||||
controller.close();
|
||||
} catch (error) {
|
||||
// Controller might already be closed
|
||||
}
|
||||
} catch (error) {}
|
||||
});
|
||||
|
||||
emitter.on('data', (data: string) => {
|
||||
// Check if request has been cancelled before processing
|
||||
if (signal.aborted) return;
|
||||
|
||||
|
||||
try {
|
||||
const parsedData = JSON.parse(data);
|
||||
|
||||
|
||||
if (parsedData.type === 'response') {
|
||||
controller.enqueue(encoder.encode(JSON.stringify({
|
||||
type: 'response',
|
||||
data: parsedData.data
|
||||
}) + '\n'));
|
||||
controller.enqueue(
|
||||
encoder.encode(
|
||||
JSON.stringify({
|
||||
type: 'response',
|
||||
data: parsedData.data,
|
||||
}) + '\n',
|
||||
),
|
||||
);
|
||||
} else if (parsedData.type === 'sources') {
|
||||
sources = parsedData.data;
|
||||
controller.enqueue(encoder.encode(JSON.stringify({
|
||||
type: 'sources',
|
||||
data: sources
|
||||
}) + '\n'));
|
||||
controller.enqueue(
|
||||
encoder.encode(
|
||||
JSON.stringify({
|
||||
type: 'sources',
|
||||
data: sources,
|
||||
}) + '\n',
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
controller.error(error);
|
||||
@ -218,32 +228,34 @@ export const POST = async (req: Request) => {
|
||||
});
|
||||
|
||||
emitter.on('end', () => {
|
||||
// Check if request has been cancelled before processing
|
||||
if (signal.aborted) return;
|
||||
|
||||
controller.enqueue(encoder.encode(JSON.stringify({
|
||||
type: 'done'
|
||||
}) + '\n'));
|
||||
|
||||
controller.enqueue(
|
||||
encoder.encode(
|
||||
JSON.stringify({
|
||||
type: 'done',
|
||||
}) + '\n',
|
||||
),
|
||||
);
|
||||
controller.close();
|
||||
});
|
||||
|
||||
emitter.on('error', (error: any) => {
|
||||
// Check if request has been cancelled before processing
|
||||
if (signal.aborted) return;
|
||||
|
||||
|
||||
controller.error(error);
|
||||
});
|
||||
},
|
||||
cancel() {
|
||||
abortController.abort();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache, no-transform',
|
||||
'Connection': 'keep-alive',
|
||||
Connection: 'keep-alive',
|
||||
},
|
||||
});
|
||||
} catch (err: any) {
|
||||
|
Reference in New Issue
Block a user