Skip to content

Commit

Permalink
fix: better typing for makeSecureBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Feb 21, 2024
1 parent 5876135 commit 7b9d593
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/crypto/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ interface CredType {
password: string;
}

const makeSecureBuffer = (password: string | undefined, encoding: CryptoEncoding): SecureBuffer<string> => {
const makeSecureBuffer = (password: string, encoding: CryptoEncoding): SecureBuffer<string> => {
const newSb = new SecureBuffer<string>();
newSb.consume(Buffer.from(ensure(password), encoding));
newSb.consume(Buffer.from(password, encoding));
return newSb;
};

Expand All @@ -142,18 +142,22 @@ const keychainPromises = {
return new Promise((resolve, reject): {} =>
_keychain.getPassword({ service, account }, (err: Nullable<Error>, password?: string) => {
if (err) return reject(err);
detectCryptoVersion(password);
Cache.set(cacheKey, makeSecureBuffer(password, ENCODING[getCryptoVersion()]));
return resolve({ username: account, password: ensure(password) });
const pwd = ensure(password, 'Expected the keychain password to be set');
detectCryptoVersion(pwd);
Cache.set(cacheKey, makeSecureBuffer(pwd, ENCODING[getCryptoVersion()]));
return resolve({ username: account, password: pwd });
})
);
} else {
// If the password is cached, we know the crypto version and encoding because it was
// detected by the non-cache code path just above this.
const encoding = ENCODING[getCryptoVersion()];
const pw = sb.value((buffer) => buffer.toString(encoding));
Cache.set(cacheKey, makeSecureBuffer(pw, encoding));
return new Promise((resolve): void => resolve({ username: account, password: ensure(pw) }));
const pwd = ensure(
sb.value((buffer) => buffer.toString(encoding)),
'Expected the keychain password to be set'
);
Cache.set(cacheKey, makeSecureBuffer(pwd, encoding));
return new Promise((resolve): void => resolve({ username: account, password: pwd }));
}
},

Expand Down

2 comments on commit 7b9d593

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: 7b9d593 Previous: 1071303 Ratio
Child logger creation 490979 ops/sec (±0.96%) 488730 ops/sec (±0.92%) 1.00
Logging a string on root logger 814866 ops/sec (±7.57%) 820383 ops/sec (±8.80%) 1.01
Logging an object on root logger 665919 ops/sec (±6.61%) 654174 ops/sec (±7.97%) 0.98
Logging an object with a message on root logger 5229 ops/sec (±219.58%) 5736 ops/sec (±211.22%) 1.10
Logging an object with a redacted prop on root logger 448096 ops/sec (±8.48%) 461732 ops/sec (±8.97%) 1.03
Logging a nested 3-level object on root logger 384930 ops/sec (±7.78%) 376128 ops/sec (±7.91%) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: 7b9d593 Previous: 1071303 Ratio
Child logger creation 349454 ops/sec (±0.53%) 328142 ops/sec (±0.72%) 0.94
Logging a string on root logger 863109 ops/sec (±8.30%) 758472 ops/sec (±8.33%) 0.88
Logging an object on root logger 596276 ops/sec (±20.41%) 567257 ops/sec (±7.70%) 0.95
Logging an object with a message on root logger 18920 ops/sec (±185.86%) 13352 ops/sec (±188.32%) 0.71
Logging an object with a redacted prop on root logger 477783 ops/sec (±6.57%) 452237 ops/sec (±7.07%) 0.95
Logging a nested 3-level object on root logger 311458 ops/sec (±4.72%) 334072 ops/sec (±4.95%) 1.07

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.