Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer globalThis over global and window #60

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ export async function encryptWithKey<R>(
): Promise<EncryptionResult> {
const data = JSON.stringify(dataObj);
const dataBuffer = Buffer.from(data, STRING_ENCODING);
const vector = global.crypto.getRandomValues(new Uint8Array(16));
const vector = globalThis.crypto.getRandomValues(new Uint8Array(16));
const key = unwrapKey(encryptionKey);

const buf = await global.crypto.subtle.encrypt(
const buf = await globalThis.crypto.subtle.encrypt(
{
name: DERIVED_KEY_FORMAT,
iv: vector,
Expand Down Expand Up @@ -247,7 +247,7 @@ export async function importKey(

if (isExportedEncryptionKey(exportedEncryptionKey)) {
return {
key: await window.crypto.subtle.importKey(
key: await globalThis.crypto.subtle.importKey(
EXPORT_FORMAT,
exportedEncryptionKey.key,
DERIVED_KEY_FORMAT,
Expand All @@ -258,7 +258,7 @@ export async function importKey(
};
}

return await window.crypto.subtle.importKey(
return await globalThis.crypto.subtle.importKey(
EXPORT_FORMAT,
exportedEncryptionKey,
DERIVED_KEY_FORMAT,
Expand All @@ -279,7 +279,7 @@ export async function exportKey(
): Promise<string> {
if (isEncryptionKey(encryptionKey)) {
return JSON.stringify({
key: await window.crypto.subtle.exportKey(
key: await globalThis.crypto.subtle.exportKey(
EXPORT_FORMAT,
encryptionKey.key,
),
Expand All @@ -288,7 +288,7 @@ export async function exportKey(
}

return JSON.stringify(
await window.crypto.subtle.exportKey(EXPORT_FORMAT, encryptionKey),
await globalThis.crypto.subtle.exportKey(EXPORT_FORMAT, encryptionKey),
);
}

Expand Down Expand Up @@ -332,15 +332,15 @@ export async function keyFromPassword(
const passBuffer = Buffer.from(password, STRING_ENCODING);
const saltBuffer = Buffer.from(salt, 'base64');

const key = await global.crypto.subtle.importKey(
const key = await globalThis.crypto.subtle.importKey(
'raw',
passBuffer,
{ name: 'PBKDF2' },
false,
['deriveBits', 'deriveKey'],
);

const derivedKey = await global.crypto.subtle.deriveKey(
const derivedKey = await globalThis.crypto.subtle.deriveKey(
{
name: 'PBKDF2',
salt: saltBuffer,
Expand Down Expand Up @@ -414,7 +414,7 @@ function unprefixedHex(num: number): string {
*/
export function generateSalt(byteCount = 32): string {
const view = new Uint8Array(byteCount);
global.crypto.getRandomValues(view);
globalThis.crypto.getRandomValues(view);
// Uint8Array is a fixed length array and thus does not have methods like pop, etc
// so TypeScript complains about casting it to an array. Array.from() works here for
// getting the proper type, but it results in a functional difference. In order to
Expand Down
Loading