Skip to content

Commit 8c663fd

Browse files
chore: add getEnv helper
1 parent 04d0a85 commit 8c663fd

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/gptscript.ts

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import http from "http"
22
import path from "path"
33
import child_process from "child_process"
44
import {fileURLToPath} from "url"
5+
import {gunzipSync} from "zlib";
56

67
export interface GlobalOpts {
78
APIKey?: string
@@ -809,6 +810,22 @@ export interface PromptResponse {
809810
responses: Record<string, string>
810811
}
811812

813+
export function getEnv(key: string, def: string = ''): string {
814+
let v = process.env[key] || ''
815+
if (v == '') {
816+
return def
817+
}
818+
819+
if (v.startsWith('{"_gz":"') && v.endsWith('"}')) {
820+
try {
821+
return gunzipSync(Buffer.from(v.slice(8, -2), 'base64')).toString('utf8')
822+
} catch (e) {
823+
}
824+
}
825+
826+
return v
827+
}
828+
812829
function getCmdPath(): string {
813830
if (process.env.GPTSCRIPT_BIN) {
814831
return process.env.GPTSCRIPT_BIN

tests/gptscript.test.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as gptscript from "../src/gptscript"
2-
import {ArgumentSchemaType, PropertyType, RunEventType, ToolType} from "../src/gptscript"
2+
import {ArgumentSchemaType, getEnv, PropertyType, RunEventType, ToolType} from "../src/gptscript"
33
import path from "path"
44
import {fileURLToPath} from "url"
55

@@ -535,4 +535,15 @@ describe("gptscript module", () => {
535535

536536
expect(run.err).toEqual("")
537537
})
538+
539+
test("test get_env default", async () => {
540+
const env = getEnv('TEST_ENV_MISSING', 'foo')
541+
expect(env).toEqual('foo')
542+
})
543+
544+
test("test get_env", async () => {
545+
process.env.TEST_ENV = '{"_gz":"H4sIAEosrGYC/ytJLS5RKEvMKU0FACtB3ewKAAAA"}'
546+
const env = getEnv('TEST_ENV', 'missing')
547+
expect(env).toEqual('test value')
548+
})
538549
})

0 commit comments

Comments
 (0)