Skip to content

Commit 3393fe1

Browse files
stainless-botRobertCraigie
authored andcommitted
chore(types): remove type-level dependency on some platform specifics
chore: unknown commit message
1 parent 5d9bff5 commit 3393fe1

File tree

9 files changed

+44
-39
lines changed

9 files changed

+44
-39
lines changed

src/client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,12 @@ export class OpenAI {
380380

381381
private calculateContentLength(body: unknown): string | null {
382382
if (typeof body === 'string') {
383-
if (typeof Buffer !== 'undefined') {
384-
return Buffer.byteLength(body, 'utf8').toString();
383+
if (typeof (globalThis as any).Buffer !== 'undefined') {
384+
return (globalThis as any).Buffer.byteLength(body, 'utf8').toString();
385385
}
386386

387-
if (typeof TextEncoder !== 'undefined') {
388-
const encoder = new TextEncoder();
387+
if (typeof (globalThis as any).TextEncoder !== 'undefined') {
388+
const encoder = new (globalThis as any).TextEncoder();
389389
const encoded = encoder.encode(body);
390390
return encoded.length.toString();
391391
}

src/internal/decoders/line.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { OpenAIError } from '../../error';
22

3-
export type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
3+
export type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
44

55
/**
66
* A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
@@ -15,7 +15,11 @@ export class LineDecoder {
1515

1616
buffer: string[];
1717
trailingCR: boolean;
18-
textDecoder: any; // TextDecoder found in browsers; not typed to avoid pulling in either "dom" or "node" types.
18+
textDecoder:
19+
| undefined
20+
| {
21+
decode(buffer: Uint8Array | ArrayBuffer): string;
22+
};
1923

2024
constructor() {
2125
this.buffer = [];
@@ -69,12 +73,12 @@ export class LineDecoder {
6973
if (typeof bytes === 'string') return bytes;
7074

7175
// Node:
72-
if (typeof Buffer !== 'undefined') {
73-
if (bytes instanceof Buffer) {
76+
if (typeof (globalThis as any).Buffer !== 'undefined') {
77+
if (bytes instanceof (globalThis as any).Buffer) {
7478
return bytes.toString();
7579
}
7680
if (bytes instanceof Uint8Array) {
77-
return Buffer.from(bytes).toString();
81+
return (globalThis as any).Buffer.from(bytes).toString();
7882
}
7983

8084
throw new OpenAIError(
@@ -83,10 +87,10 @@ export class LineDecoder {
8387
}
8488

8589
// Browser
86-
if (typeof TextDecoder !== 'undefined') {
90+
if (typeof (globalThis as any).TextDecoder !== 'undefined') {
8791
if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
88-
this.textDecoder ??= new TextDecoder('utf8');
89-
return this.textDecoder.decode(bytes);
92+
this.textDecoder ??= new (globalThis as any).TextDecoder('utf8');
93+
return this.textDecoder!.decode(bytes);
9094
}
9195

9296
throw new OpenAIError(

src/internal/detect-platform.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ function getDetectedPlatform(): DetectedPlatform {
2525
if (typeof EdgeRuntime !== 'undefined') {
2626
return 'edge';
2727
}
28-
if (Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]') {
28+
if (
29+
Object.prototype.toString.call(
30+
typeof (globalThis as any).process !== 'undefined' ? (globalThis as any).process : 0,
31+
) === '[object process]'
32+
) {
2933
return 'node';
3034
}
3135
return 'unknown';
@@ -73,18 +77,18 @@ const getPlatformProperties = (): PlatformProperties => {
7377
'X-Stainless-OS': 'Unknown',
7478
'X-Stainless-Arch': `other:${EdgeRuntime}`,
7579
'X-Stainless-Runtime': 'edge',
76-
'X-Stainless-Runtime-Version': process.version,
80+
'X-Stainless-Runtime-Version': (globalThis as any).process.version,
7781
};
7882
}
7983
// Check if Node.js
8084
if (detectedPlatform === 'node') {
8185
return {
8286
'X-Stainless-Lang': 'js',
8387
'X-Stainless-Package-Version': VERSION,
84-
'X-Stainless-OS': normalizePlatform(process.platform),
85-
'X-Stainless-Arch': normalizeArch(process.arch),
88+
'X-Stainless-OS': normalizePlatform((globalThis as any).process.platform),
89+
'X-Stainless-Arch': normalizeArch((globalThis as any).process.arch),
8690
'X-Stainless-Runtime': 'node',
87-
'X-Stainless-Runtime-Version': process.version,
91+
'X-Stainless-Runtime-Version': (globalThis as any).process.version,
8892
};
8993
}
9094

src/internal/shims.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { type ReadableStream } from './shim-types';
1212

1313
export function getDefaultFetch(): Fetch {
1414
if (typeof fetch !== 'undefined') {
15-
return fetch;
15+
return fetch as any;
1616
}
1717

1818
throw new Error(
@@ -97,7 +97,7 @@ export function ReadableStreamFrom<T>(iterable: Iterable<T> | AsyncIterable<T>):
9797

9898
return makeReadableStream({
9999
start() {},
100-
async pull(controller) {
100+
async pull(controller: any) {
101101
const { done, value } = await iter.next();
102102
if (done) {
103103
controller.close();

src/internal/utils/base64.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22

33
import { OpenAIError } from '../../error';
44

5-
// @ts-ignore
6-
declare const Buffer: typeof import('node:buffer').Buffer;
7-
85
export const toBase64 = (data: string | Uint8Array | null | undefined): string => {
96
if (!data) return '';
107

118
if (typeof data === 'string') {
12-
data = new TextEncoder().encode(data);
9+
data = new (globalThis as any).TextEncoder().encode(data);
1310
}
1411

15-
if (typeof Buffer !== 'undefined') {
16-
return Buffer.from(data).toString('base64');
12+
if (typeof (globalThis as any).Buffer !== 'undefined') {
13+
return (globalThis as any).Buffer.from(data).toString('base64');
1714
}
1815

1916
if (typeof btoa !== 'undefined') {
@@ -24,8 +21,8 @@ export const toBase64 = (data: string | Uint8Array | null | undefined): string =
2421
};
2522

2623
export const fromBase64 = (str: string): Uint8Array => {
27-
if (typeof Buffer !== 'undefined') {
28-
return new Uint8Array(Buffer.from(str, 'base64'));
24+
if (typeof (globalThis as any).Buffer !== 'undefined') {
25+
return new Uint8Array((globalThis as any).Buffer.from(str, 'base64'));
2926
}
3027

3128
if (typeof atob !== 'undefined') {

src/internal/utils/env.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
declare const Deno: any;
4-
53
/**
64
* Read an environment variable.
75
*
@@ -10,11 +8,11 @@ declare const Deno: any;
108
* Will return undefined if the environment variable doesn't exist or cannot be accessed.
119
*/
1210
export const readEnv = (env: string): string | undefined => {
13-
if (typeof process !== 'undefined') {
14-
return process.env?.[env]?.trim() ?? undefined;
11+
if (typeof (globalThis as any).process !== 'undefined') {
12+
return (globalThis as any).process.env?.[env]?.trim() ?? undefined;
1513
}
16-
if (typeof Deno !== 'undefined') {
17-
return Deno.env?.get?.(env)?.trim();
14+
if (typeof (globalThis as any).Deno !== 'undefined') {
15+
return (globalThis as any).Deno.env?.get?.(env)?.trim();
1816
}
1917
return undefined;
2018
};

src/internal/utils/sleep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
3+
export const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));

src/streaming.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ReadableStreamToAsyncIterable } from './internal/shims';
66

77
import { APIError } from './error';
88

9-
type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined;
9+
type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
1010

1111
export type ServerSentEvent = {
1212
event: string | null;
@@ -175,7 +175,9 @@ export class Stream<Item> implements AsyncIterable<Item> {
175175
toReadableStream(): ReadableStream {
176176
const self = this;
177177
let iter: AsyncIterator<Item>;
178-
const encoder = new TextEncoder();
178+
const encoder: {
179+
encode(str: string): Uint8Array;
180+
} = new (globalThis as any).TextEncoder();
179181

180182
return makeReadableStream({
181183
async start() {
@@ -240,7 +242,7 @@ async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGene
240242

241243
const binaryChunk =
242244
chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
243-
: typeof chunk === 'string' ? new TextEncoder().encode(chunk)
245+
: typeof chunk === 'string' ? new (globalThis as any).TextEncoder().encode(chunk)
244246
: chunk;
245247

246248
let newData = new Uint8Array(data.length + binaryChunk.length);

tsconfig.dist-src.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// via declaration maps
55
"include": ["index.ts"],
66
"compilerOptions": {
7-
"target": "es2015",
8-
"lib": ["DOM", "DOM.Iterable"],
7+
"target": "ES2015",
8+
"lib": ["DOM", "DOM.Iterable", "ES2018"],
99
"moduleResolution": "node"
1010
}
1111
}

0 commit comments

Comments
 (0)