MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / ElectronFetcher

Class ElectronFetcher

apps/desktop/main/api/adapters.ts:28–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26// ==================== ElectronFetcher ====================
27
28export class ElectronFetcher implements HttpFetcher {
29 fetchToTempFile(baseUrl: string, remoteSessionId: string, token: string, params: FetchParams): Promise<string> {
30 return new Promise<string>((resolve, reject) => {
31 const url = buildPullUrl(baseUrl, remoteSessionId, params)
32 const request = net.request(url)
33
34 if (token) request.setHeader('Authorization', `Bearer ${token}`)
35 request.setHeader('Accept', 'application/json')
36
37 let tempFile = ''
38 let writeStream: fs.WriteStream | null = null
39
40 request.on('response', (response) => {
41 if (response.statusCode !== 200) {
42 reject(new Error(`HTTP ${response.statusCode}`))
43 return
44 }
45
46 const contentType = (response.headers['content-type'] as string) || 'application/json'
47 const isJsonl = contentType.includes('ndjson') || contentType.includes('jsonl')
48 tempFile = getTempFilePath(isJsonl ? '.jsonl' : '.json')
49 writeStream = fs.createWriteStream(tempFile)
50
51 response.on('data', (chunk: Buffer) => writeStream!.write(chunk))
52 response.on('end', () => writeStream!.end(() => resolve(tempFile)))
53 response.on('error', (err: Error) => {
54 writeStream?.end()
55 cleanupTempFile(tempFile)
56 reject(err)
57 })
58 })
59
60 request.on('error', (err: Error) => {
61 if (writeStream) writeStream.end()
62 if (tempFile) cleanupTempFile(tempFile)
63 reject(err)
64 })
65
66 request.end()
67 })
68 }
69}
70
71function cleanupTempFile(filePath: string): void {
72 try {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected