Skip to content

Commit

Permalink
Updated types
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Mar 15, 2024
1 parent 44b62bb commit bccf50c
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { Cred } from './cred.js';
import type { TextDecoder as _TextDecoder, TextEncoder as _TextEncoder } from 'node:util';

declare global {
function setImmediate(callback: () => unknown): void;
function atob(data: string): string;
function btoa(data: string): string;
const TextDecoder: typeof _TextDecoder;
const TextEncoder: typeof _TextEncoder;
}

declare const globalThis: {
TextDecoder?: typeof _TextDecoder;
TextEncoder?: typeof _TextEncoder;
setImmediate?: (callback: () => unknown) => void;
}

/**
Expand Down Expand Up @@ -153,19 +156,19 @@ export const setImmediate = typeof globalThis.setImmediate == 'function' ? globa
export function encode(input: string, encoding: BufferEncoding = 'utf8'): Uint8Array {
switch (encoding) {
case 'ascii':
return new TextEncoder().encode(input).map(v => v & 0x7f);
return new globalThis.TextEncoder().encode(input).map(v => v & 0x7f);
case 'latin1':
case 'binary':
case 'utf8':
case 'utf-8':
case 'base64':
case 'base64url':
case 'hex':
return new TextEncoder().encode(input);
return new globalThis.TextEncoder().encode(input);
case 'utf16le':
case 'ucs2':
case 'ucs-2':
return new TextEncoder().encode(input).slice(0, -1);
return new globalThis.TextEncoder().encode(input).slice(0, -1);
default:
throw new ApiError(ErrorCode.EINVAL, 'Invalid encoding: ' + encoding);
}
Expand All @@ -180,10 +183,10 @@ export function decode(input?: Uint8Array, encoding: BufferEncoding = 'utf8'): s
case 'ascii':
case 'utf8':
case 'utf-8':
return new TextDecoder().decode(input);
return new globalThis.TextDecoder().decode(input);
case 'latin1':
case 'binary':
return new TextDecoder('latin1').decode(input);
return new globalThis.TextDecoder('latin1').decode(input);
case 'utf16le':
case 'ucs2':
case 'ucs-2':
Expand Down

0 comments on commit bccf50c

Please sign in to comment.