Skip to content

Commit d5552a5

Browse files
feat: support ESM and web platform runtimes (#25)
We have added or improved support for: - EcmaScript Modules (ESM) for Node and others (in addition to CJS) - Cloudflare Workers - Vercel Edge Runtime - Deno - Browser (tested with webpack) - though you should not expose your secret key. In Node, we still use libraries like `node-fetch` and `formdata-node`; in others, we use the native APIs. Co-authored-by: Stainless Bot <[email protected]>
1 parent e74e156 commit d5552a5

36 files changed

+1276
-693
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
yarn-error.log
33
dist
4+
/*.tgz

.npmignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
CHANGELOG.md
2+
/ecosystem-tests
3+
/node_modules

_shims/agent.node.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
import KeepAliveAgent from 'agentkeepalive';
6+
import type { Agent } from 'node:http';
7+
import { AbortController as AbortControllerPolyfill } from 'abort-controller';
8+
9+
const defaultHttpAgent: Agent = new KeepAliveAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
10+
const defaultHttpsAgent: Agent = new KeepAliveAgent.HttpsAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
11+
12+
// Polyfill global object if needed.
13+
if (typeof AbortController === 'undefined') {
14+
AbortController = AbortControllerPolyfill as any as typeof AbortController;
15+
}
16+
17+
export const getDefaultAgent = (url: string): Agent | undefined => {
18+
if (defaultHttpsAgent && url.startsWith('https')) return defaultHttpsAgent;
19+
return defaultHttpAgent;
20+
};

_shims/agent.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*
4+
* This is a stub for non-node environments.
5+
* In node environments, it gets replaced agent.node.ts by the package export map
6+
*/
7+
8+
import type { Agent } from 'node:http';
9+
10+
export const getDefaultAgent = (url: string): Agent | undefined => {
11+
return undefined;
12+
};

_shims/fetch.node.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
import fetch, { Request, Response, Headers } from 'node-fetch';
6+
import type { RequestInfo, RequestInit, BodyInit } from 'node-fetch';
7+
8+
export { fetch, Request, Response, Headers };
9+
export type { RequestInfo, RequestInit, BodyInit };
10+
11+
export const isPolyfilled = true;

_shims/fetch.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
import type * as nf from 'node-fetch';
6+
7+
const _fetch: typeof nf.default =
8+
// @ts-ignore
9+
fetch as any;
10+
type _fetch = typeof nf.default;
11+
const _Request: typeof nf.Request =
12+
// @ts-ignore
13+
Request as any;
14+
type _Request = nf.Request;
15+
const _Response: typeof nf.Response =
16+
// @ts-ignore
17+
Response as any;
18+
type _Response = nf.Response;
19+
const _Headers: typeof nf.Headers =
20+
// @ts-ignore
21+
Headers as any;
22+
type _Headers = nf.Headers;
23+
24+
export const isPolyfilled = false;
25+
26+
export { _fetch as fetch, _Request as Request, _Response as Response, _Headers as Headers };
27+
28+
type _RequestInfo = nf.RequestInfo;
29+
type _RequestInit = nf.RequestInit;
30+
type _BodyInit = nf.BodyInit;
31+
32+
export type { _RequestInit as RequestInit, _RequestInfo as RequestInfo, _BodyInit as BodyInit };

_shims/fileFromPath.node.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
import type { FilePropertyBag } from 'formdata-node';
6+
import { fileFromPath as _fileFromPath } from 'formdata-node/file-from-path';
7+
import type { File } from './formdata.node';
8+
9+
export type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>;
10+
11+
let warned = false;
12+
13+
/**
14+
* @deprecated use fs.createReadStream('./my/file.txt') instead
15+
*/
16+
export async function fileFromPath(path: string): Promise<File>;
17+
export async function fileFromPath(path: string, filename?: string): Promise<File>;
18+
export async function fileFromPath(path: string, options?: FileFromPathOptions): Promise<File>;
19+
export async function fileFromPath(
20+
path: string,
21+
filename?: string,
22+
options?: FileFromPathOptions,
23+
): Promise<File>;
24+
export async function fileFromPath(path: string, ...args: any[]): Promise<File> {
25+
if (!warned) {
26+
console.warn(`fileFromPath is deprecated; use fs.createReadStream(${JSON.stringify(path)}) instead`);
27+
warned = true;
28+
}
29+
return await _fileFromPath(path, ...args);
30+
}

_shims/fileFromPath.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*
4+
* This is a stub that gets replaced by fileFromPath.node.js for node environments
5+
* in the package export map
6+
*/
7+
8+
import type { FilePropertyBag } from 'formdata-node';
9+
import type { File } from './formdata';
10+
11+
export type FileFromPathOptions = Omit<FilePropertyBag, 'lastModified'>;
12+
13+
/**
14+
* This is a stub for non-node environments that just throws an error.
15+
* In node environments, this module will be replaced by util/node/fileFromPath by the
16+
* package import map.
17+
*/
18+
export async function fileFromPath(path: string): Promise<File>;
19+
export async function fileFromPath(path: string, filename?: string): Promise<File>;
20+
export async function fileFromPath(path: string, options?: FileFromPathOptions): Promise<File>;
21+
export async function fileFromPath(
22+
path: string,
23+
filename?: string,
24+
options?: FileFromPathOptions,
25+
): Promise<File>;
26+
export async function fileFromPath(): Promise<File> {
27+
throw new Error(
28+
'The `fileFromPath` function is only supported in Node. See the README for more details: https://www.github.com/anthropics/anthropic-sdk-typescript#file-uploads',
29+
);
30+
}

_shims/formdata.node.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Disclaimer: modules in _shims aren't intended to be imported by SDK users.
3+
*/
4+
5+
import { FormData, File, Blob } from 'formdata-node';
6+
7+
export { FormData, File, Blob };
8+
9+
export const isPolyfilled = true;

0 commit comments

Comments
 (0)