Skip to content

Commit bccab80

Browse files
chore(internal): refactor utils
1 parent 8c53526 commit bccab80

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

src/client.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,12 @@ export class Openint {
635635
fetchOptions.method = method.toUpperCase();
636636
}
637637

638-
return (
638+
try {
639639
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
640-
this.fetch.call(undefined, url, fetchOptions).finally(() => {
641-
clearTimeout(timeout);
642-
})
643-
);
640+
return await this.fetch.call(undefined, url, fetchOptions);
641+
} finally {
642+
clearTimeout(timeout);
643+
}
644644
}
645645

646646
private shouldRetry(response: Response): boolean {

src/internal/headers.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
type HeaderValue = string | undefined | null;
44
export type HeadersLike =
55
| Headers
6-
| readonly [string, HeaderValue][]
6+
| readonly HeaderValue[][]
77
| Record<string, HeaderValue | readonly HeaderValue[]>
88
| undefined
99
| null
@@ -40,7 +40,7 @@ function* iterateHeaders(headers: HeadersLike): IterableIterator<readonly [strin
4040
}
4141

4242
let shouldClear = false;
43-
let iter: Iterable<readonly [string, HeaderValue | readonly HeaderValue[]]>;
43+
let iter: Iterable<readonly (HeaderValue | readonly HeaderValue[])[]>;
4444
if (headers instanceof Headers) {
4545
iter = headers.entries();
4646
} else if (isArray(headers)) {
@@ -51,6 +51,7 @@ function* iterateHeaders(headers: HeadersLike): IterableIterator<readonly [strin
5151
}
5252
for (let row of iter) {
5353
const name = row[0];
54+
if (typeof name !== 'string') throw new TypeError('expected header name to be a string');
5455
const values = isArray(row[1]) ? row[1] : [row[1]];
5556
let didClear = false;
5657
for (const value of values) {

src/internal/utils/base64.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
import { OpenintError } from '../../core/error';
4+
import { encodeUTF8 } from './bytes';
45

56
export const toBase64 = (data: string | Uint8Array | null | undefined): string => {
67
if (!data) return '';
78

8-
if (typeof data === 'string') {
9-
data = new (globalThis as any).TextEncoder().encode(data);
10-
}
11-
129
if (typeof (globalThis as any).Buffer !== 'undefined') {
1310
return (globalThis as any).Buffer.from(data).toString('base64');
1411
}
1512

13+
if (typeof data === 'string') {
14+
data = encodeUTF8(data);
15+
}
16+
1617
if (typeof btoa !== 'undefined') {
1718
return btoa(String.fromCharCode.apply(null, data as any));
1819
}

src/internal/utils/bytes.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export function concatBytes(buffers: Uint8Array[]): Uint8Array {
2+
let length = 0;
3+
for (const buffer of buffers) {
4+
length += buffer.length;
5+
}
6+
const output = new Uint8Array(length);
7+
let index = 0;
8+
for (const buffer of buffers) {
9+
output.set(buffer, index);
10+
index += buffer.length;
11+
}
12+
13+
return output;
14+
}
15+
16+
let encodeUTF8_: (str: string) => Uint8Array;
17+
export function encodeUTF8(str: string) {
18+
let encoder;
19+
return (
20+
encodeUTF8_ ??
21+
((encoder = new (globalThis as any).TextEncoder()), (encodeUTF8_ = encoder.encode.bind(encoder)))
22+
)(str);
23+
}
24+
25+
let decodeUTF8_: (bytes: Uint8Array) => string;
26+
export function decodeUTF8(bytes: Uint8Array) {
27+
let decoder;
28+
return (
29+
decodeUTF8_ ??
30+
((decoder = new (globalThis as any).TextDecoder()), (decodeUTF8_ = decoder.decode.bind(decoder)))
31+
)(bytes);
32+
}

0 commit comments

Comments
 (0)