Skip to content

Commit 938f444

Browse files
committed
fix: review
1 parent 247e1e1 commit 938f444

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/exec_auth.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,14 @@ export class ExecAuth implements Authenticator {
100100
opts = { ...opts, env };
101101
}
102102

103-
return new Promise((accept, reject) => {
103+
return new Promise((resolve, reject) => {
104104
let stdoutData: string = '';
105105
let stderrData: string = '';
106+
let savedError: Error | undefined = undefined;
106107

107108
const subprocess = this.execFn(exec.command, exec.args, opts);
109+
subprocess.stdout.setEncoding('utf8');
110+
subprocess.stderr.setEncoding('utf8');
108111

109112
subprocess.stdout.on('data', (data: Buffer | string) => {
110113
stdoutData += data.toString('utf8');
@@ -115,16 +118,26 @@ export class ExecAuth implements Authenticator {
115118
});
116119

117120
subprocess.on('error', (error) => {
121+
savedError = error;
118122
throw error;
119123
});
120124

121125
subprocess.on('close', (code) => {
126+
if (savedError) {
127+
reject(savedError);
128+
return;
129+
}
122130
if (code !== 0) {
123-
throw new Error(stderrData);
131+
reject(new Error(stderrData));
132+
return;
133+
}
134+
try {
135+
const obj = JSON.parse(stdoutData) as Credential;
136+
this.tokenCache[user.name] = obj;
137+
resolve(obj);
138+
} catch (error) {
139+
reject(error);
124140
}
125-
const obj = JSON.parse(stdoutData) as Credential;
126-
this.tokenCache[user.name] = obj;
127-
accept(obj);
128141
});
129142
});
130143
}

src/exec_auth_test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ describe('ExecAuth', () => {
6565
): child_process.ChildProcessWithoutNullStreams => {
6666
return {
6767
stdout: {
68+
setEncoding: () => {},
6869
on: (_data: string, f: (data: Buffer | string) => void) => {
6970
f(Buffer.from(JSON.stringify({ status: { token: 'foo' } })));
7071
},
7172
},
7273
stderr: {
74+
setEncoding: () => {},
7375
on: () => {},
7476
},
7577
on: (op: string, f: any) => {
@@ -110,6 +112,7 @@ describe('ExecAuth', () => {
110112
): child_process.ChildProcessWithoutNullStreams => {
111113
return {
112114
stdout: {
115+
setEncoding: () => {},
113116
on: (_data: string, f: (data: Buffer | string) => void) => {
114117
f(
115118
Buffer.from(
@@ -121,6 +124,7 @@ describe('ExecAuth', () => {
121124
},
122125
},
123126
stderr: {
127+
setEncoding: () => {},
124128
on: () => {},
125129
},
126130
on: (op: string, f: any) => {
@@ -168,6 +172,7 @@ describe('ExecAuth', () => {
168172
execCount++;
169173
return {
170174
stdout: {
175+
setEncoding: () => {},
171176
on: (_data: string, f: (data: Buffer | string) => void) => {
172177
f(
173178
Buffer.from(
@@ -179,6 +184,7 @@ describe('ExecAuth', () => {
179184
},
180185
},
181186
stderr: {
187+
setEncoding: () => {},
182188
on: () => {},
183189
},
184190
on: (op: string, f: any) => {
@@ -253,9 +259,11 @@ describe('ExecAuth', () => {
253259
): child_process.ChildProcessWithoutNullStreams => {
254260
return {
255261
stdout: {
262+
setEncoding: () => {},
256263
on: (_data: string, f: (data: Buffer | string) => void) => {},
257264
},
258265
stderr: {
266+
setEncoding: () => {},
259267
on: () => {},
260268
},
261269
on: (op: string, f: any) => {
@@ -297,11 +305,13 @@ describe('ExecAuth', () => {
297305
): child_process.ChildProcessWithoutNullStreams => {
298306
return {
299307
stdout: {
308+
setEncoding: () => {},
300309
on: (_data: string, f: (data: Buffer | string) => void) => {
301310
f(Buffer.from(JSON.stringify({ status: { token: 'foo' } })));
302311
},
303312
},
304313
stderr: {
314+
setEncoding: () => {},
305315
on: (_data: string, f: (data: Buffer | string) => void) => {
306316
f(Buffer.from('Some error!'));
307317
},
@@ -346,11 +356,13 @@ describe('ExecAuth', () => {
346356
optsOut = options;
347357
return {
348358
stdout: {
359+
setEncoding: () => {},
349360
on: (_data: string, f: (data: Buffer | string) => void) => {
350361
f(Buffer.from(JSON.stringify({ status: { token: 'foo' } })));
351362
},
352363
},
353364
stderr: {
365+
setEncoding: () => {},
354366
on: () => {},
355367
},
356368
on: (op: string, f: any) => {
@@ -402,11 +414,13 @@ describe('ExecAuth', () => {
402414
): child_process.ChildProcessWithoutNullStreams => {
403415
return {
404416
stdout: {
417+
setEncoding: () => {},
405418
on: (_data: string, f: (data: Buffer | string) => void) => {
406419
f(Buffer.from(JSON.stringify({ status: { token: 'foo' } })));
407420
},
408421
},
409422
stderr: {
423+
setEncoding: () => {},
410424
on: () => {},
411425
},
412426
on: (op: string, f: any) => {

0 commit comments

Comments
 (0)