File size: 915 Bytes
5dfbe50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import * as fs from 'fs';
import * as https from 'https';
import { config } from '../config';

// Create HTTPS agent with certificate authentication
let httpsAgent: https.Agent | undefined;

if (config.vespaCertPath && config.vespaKeyPath) {
  try {
    httpsAgent = new https.Agent({
      cert: fs.readFileSync(config.vespaCertPath),
      key: fs.readFileSync(config.vespaKeyPath),
      rejectUnauthorized: false
    });
  } catch (error) {
    console.error('Failed to load Vespa certificates:', error);
  }
}

export async function vespaFetch(url: string, options: RequestInit = {}) {
  // For Node.js 18+, we need to use undici or node-fetch with agent support
  const fetch = globalThis.fetch;
  
  if (httpsAgent) {
    // @ts-ignore - agent is not in standard fetch types but works in Node.js
    return fetch(url, {
      ...options,
      agent: httpsAgent
    });
  }
  
  return fetch(url, options);
}