@@ -3,6 +3,7 @@ import path from "path"
33import child_process from "child_process"
44import { fileURLToPath } from "url"
55import { gunzipSync } from "zlib"
6+ import https from "https"
67
78export interface GlobalOpts {
89 URL ?: string
@@ -1283,3 +1284,48 @@ export interface DatasetElement {
12831284 contents ?: string
12841285 binaryContents ?: ArrayBuffer
12851286}
1287+
1288+ // Functions for use in daemon tools:
1289+
1290+ export function createServer ( listener : http . RequestListener < typeof http . IncomingMessage , typeof http . ServerResponse > ) : https . Server {
1291+ const certB64 = process . env . CERT
1292+ const privateKeyB64 = process . env . PRIVATE_KEY
1293+ const gptscriptCertB64 = process . env . GPTSCRIPT_CERT
1294+
1295+ if ( ! certB64 ) {
1296+ console . log ( 'Missing CERT env var' )
1297+ process . exit ( 1 )
1298+ } else if ( ! privateKeyB64 ) {
1299+ console . log ( 'Missing PRIVATE_KEY env var' )
1300+ process . exit ( 1 )
1301+ } else if ( ! gptscriptCertB64 ) {
1302+ console . log ( 'Missing GPTSCRIPT_CERT env var' )
1303+ process . exit ( 1 )
1304+ }
1305+
1306+ const cert = Buffer . from ( certB64 , 'base64' ) . toString ( 'utf-8' )
1307+ const privateKey = Buffer . from ( privateKeyB64 , 'base64' ) . toString ( 'utf-8' )
1308+ const gptscriptCert = Buffer . from ( gptscriptCertB64 , 'base64' ) . toString ( 'utf-8' )
1309+
1310+ const options = {
1311+ key : privateKey ,
1312+ cert : cert ,
1313+ ca : gptscriptCert ,
1314+ requestCert : true ,
1315+ rejectUnauthorized : true ,
1316+ }
1317+
1318+ return https . createServer ( options , listener )
1319+ }
1320+
1321+ export function startServer ( server : https . Server ) {
1322+ const port = process . env . PORT
1323+ if ( ! port ) {
1324+ console . log ( 'Missing PORT env var' )
1325+ process . exit ( 1 )
1326+ }
1327+
1328+ server . listen ( port , ( ) => {
1329+ console . log ( `Server listening on port ${ port } ` )
1330+ } )
1331+ }
0 commit comments