Skip to content

Commit 52b0439

Browse files
committed
Update based on feedback
1 parent 04e3309 commit 52b0439

File tree

3 files changed

+78
-64
lines changed

3 files changed

+78
-64
lines changed

src/core.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class APIPromise<T> extends Promise<WithRequestID<T>> {
130130
* instance, you can use {@link withResponse()}.
131131
*
132132
* 👋 Getting the wrong TypeScript type for `Response`?
133-
* Try setting `"moduleResolution": "NodeNext"` if you can,
133+
* Try setting `'moduleResolution': 'NodeNext'` if you can,
134134
* or add one of these imports before your first `import … from 'openai'`:
135135
* - `import 'openai/shims/node'` (if you're running on Node)
136136
* - `import 'openai/shims/web'` (otherwise)
@@ -149,7 +149,7 @@ export class APIPromise<T> extends Promise<WithRequestID<T>> {
149149
*
150150
*
151151
* 👋 Getting the wrong TypeScript type for `Response`?
152-
* Try setting `"moduleResolution": "NodeNext"` if you can,
152+
* Try setting `'moduleResolution': 'NodeNext'` if you can,
153153
* or add one of these imports before your first `import … from 'openai'`:
154154
* - `import 'openai/shims/node'` (if you're running on Node)
155155
* - `import 'openai/shims/web'` (otherwise)
@@ -1117,7 +1117,7 @@ export function hasOwn(obj: Object, key: string): boolean {
11171117
}
11181118

11191119
/**
1120-
* Copies headers from "newHeaders" onto "targetHeaders",
1120+
* Copies headers from 'newHeaders' onto 'targetHeaders',
11211121
* using lower-case for all properties,
11221122
* ignoring any keys with undefined values,
11231123
* and deleting any keys with null values.
@@ -1138,28 +1138,33 @@ function applyHeadersMut(targetHeaders: Headers, newHeaders: Headers): void {
11381138
}
11391139
}
11401140

1141+
const SENSITIVE_HEADERS = new Set(['authorization', 'api-key']);
1142+
11411143
export function debug(action: string, ...args: any[]) {
1142-
const sensitiveHeaders = ["authorization", "api-key"];
11431144
if (typeof process !== 'undefined' && process?.env?.['DEBUG'] === 'true') {
1144-
for (const arg of args) {
1145+
const modifiedArgs = args.map((arg) => {
11451146
if (!arg) {
1146-
break;
1147+
return arg;
11471148
}
1148-
// Check for sensitive headers in request body "headers"
1149-
if (arg["headers"]) {
1150-
for (const header in arg["headers"]){
1151-
if (sensitiveHeaders.includes(header.toLowerCase())){
1152-
arg["headers"][header] = "REDACTED";
1149+
1150+
const modifiedArg = { ...arg };
1151+
// Check for sensitive headers in request body 'headers' object
1152+
if (modifiedArg['headers']) {
1153+
for (const header in modifiedArg['headers']){
1154+
if (SENSITIVE_HEADERS.has(header.toLowerCase())){
1155+
modifiedArg['headers'][header] = 'REDACTED';
11531156
}
11541157
}
11551158
}
11561159
// Check for sensitive headers in headers object
1157-
for (const header in arg){
1158-
if (sensitiveHeaders.includes(header.toLowerCase())){
1159-
arg[header] = "REDACTED";
1160+
for (const header in modifiedArg){
1161+
if (SENSITIVE_HEADERS.has(header.toLowerCase())){
1162+
modifiedArg[header] = 'REDACTED';
11601163
}
11611164
}
1162-
}
1165+
return modifiedArg;
1166+
})
1167+
console.log(`OpenAI:DEBUG:${action}`, ...modifiedArgs);
11631168
}
11641169
}
11651170

tests/index.test.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import OpenAI from 'openai';
44
import { APIUserAbortError } from 'openai';
5-
import { Headers } from 'openai/core';
5+
import { debug, Headers } from 'openai/core';
66
import defaultFetch, { Response, type RequestInit, type RequestInfo } from 'node-fetch';
77

88
describe('instantiate client', () => {
@@ -411,3 +411,60 @@ describe('retries', () => {
411411
expect(count).toEqual(3);
412412
});
413413
});
414+
415+
describe('Debug', () => {
416+
const env = process.env;
417+
const spy = jest.spyOn(console, 'log');
418+
419+
beforeEach(() => {
420+
jest.resetModules();
421+
process.env = { ...env };
422+
process.env['DEBUG']= 'true';
423+
});
424+
425+
afterEach(() => {
426+
process.env = env;
427+
});
428+
429+
test('body request object with Authorization header', function(){
430+
// Test request body includes headers object with Authorization
431+
const headersTest = {
432+
headers: {
433+
Authorization: 'fakeAuthorization'
434+
}
435+
}
436+
debug('request', headersTest);
437+
expect(spy).toHaveBeenCalledWith('OpenAI:DEBUG:request', {
438+
headers: {
439+
Authorization: 'REDACTED'
440+
}
441+
});
442+
});
443+
444+
test('body request object with api-key header', function(){
445+
// Test request body includes headers object with api-ley
446+
const apiKeyTest = {
447+
headers: {
448+
'api-key': 'fakeKey'
449+
}
450+
}
451+
debug('request', apiKeyTest);
452+
expect(spy).toHaveBeenCalledWith('OpenAI:DEBUG:request', {
453+
headers: {
454+
'api-key': 'REDACTED'
455+
}
456+
});
457+
});
458+
459+
test('header object with Authorization header', function(){
460+
// Test headers object with authorization header
461+
const authorizationTest = {
462+
authorization: 'fakeValue'
463+
}
464+
debug('request', authorizationTest);
465+
expect(spy).toHaveBeenCalledWith('OpenAI:DEBUG:request', {
466+
authorization: 'REDACTED'
467+
});
468+
});
469+
470+
});

tests/qs/utils.test.ts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { debug } from 'openai/core';
21
import { combine, merge, is_buffer, assign_single_source } from 'openai/internal/qs/utils';
32

43
describe('merge()', function () {
@@ -168,50 +167,3 @@ test('is_buffer()', function () {
168167
// t.equal(is_buffer(buffer), true, 'real Buffer instance is a buffer');
169168
expect(is_buffer(buffer)).toEqual(true);
170169
});
171-
172-
test("debug()", function(){
173-
const originalDebugValue = process.env["DEBUG"];
174-
175-
const spy = jest.spyOn(console, "log");
176-
177-
// Debug enabled
178-
process.env["DEBUG"]= "true";
179-
180-
// Test request body includes headers object with Authorization
181-
const headersTest = {
182-
headers: {
183-
Authorization: "fakeAuthorization"
184-
}
185-
}
186-
debug("request", headersTest);
187-
expect(spy).toHaveBeenCalledWith("OpenAI:DEBUG:request", {
188-
headers: {
189-
Authorization: "REDACTED"
190-
}
191-
});
192-
193-
// Test request body includes headers object with api-ley
194-
const apiKeyTest = {
195-
headers: {
196-
"api-key": "fakeKey"
197-
}
198-
}
199-
debug("request", apiKeyTest);
200-
expect(spy).toHaveBeenCalledWith("OpenAI:DEBUG:request", {
201-
headers: {
202-
"api-key": "REDACTED"
203-
}
204-
});
205-
206-
// Test headers object with authorization header
207-
const authorizationTest = {
208-
authorization: "fakeValue"
209-
}
210-
debug("request", authorizationTest);
211-
expect(spy).toHaveBeenCalledWith("OpenAI:DEBUG:request", {
212-
authorization: "REDACTED"
213-
});
214-
215-
process.env["DEBUG"] = originalDebugValue;
216-
217-
})

0 commit comments

Comments
 (0)