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