Skip to content

Commit 556a33d

Browse files
committed
polyfill ReadableStream
1 parent 309ee09 commit 556a33d

File tree

6 files changed

+2794
-27
lines changed

6 files changed

+2794
-27
lines changed

index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
<input type="submit">
1414
</form>
1515
<form id="chat-gpt">
16-
<input id="chat-input" type="text" value="write me a django REST api that mimics the pet store example">
16+
<input id="chat-input" type="text" value="hello!">
1717
<input type="submit">
1818
</form>
1919
<pre id="app"></pre>
2020
<script type="module" src="/script.ts"></script>
2121
</body>
2222

23-
</html>
23+
</html>

index.test.ts

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import axios from "axios";
2+
import fetchAdapter from "./index";
3+
import { EventSourceParseCallback, createParser } from "eventsource-parser";
4+
5+
it("post request", async () => {
6+
const ax = axios.create({
7+
adapter: fetchAdapter,
8+
});
9+
let response = await ax.request({
10+
url: "https://httpbin.org/post",
11+
method: "post",
12+
data: { hello: "world" },
13+
adapter: fetchAdapter,
14+
});
15+
16+
expect(JSON.parse(response.data.data)).toStrictEqual({ hello: "world" });
17+
});
18+
19+
it("stream openai", async () => {
20+
const ax = axios.create({
21+
adapter: fetchAdapter,
22+
});
23+
let response = await ax.request({
24+
url: "https://api.openai.com/v1/chat/completions",
25+
method: "post",
26+
responseType: "stream",
27+
headers: {
28+
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
29+
},
30+
data: {
31+
stream: true,
32+
model: "gpt-3.5-turbo",
33+
messages: [
34+
{ role: "system", content: "You are a helpful assistant." },
35+
{ role: "user", content: "Hello!" },
36+
],
37+
},
38+
adapter: fetchAdapter,
39+
});
40+
const decoder = new TextDecoder();
41+
42+
const stream = new ReadableStream({
43+
async start(controller) {
44+
const encoder = new TextEncoder();
45+
const onParse: EventSourceParseCallback = (event) => {
46+
if (event.type === "event") {
47+
const data = event.data;
48+
49+
if (data === "[DONE]") {
50+
controller.close();
51+
return;
52+
}
53+
54+
try {
55+
const json = JSON.parse(data);
56+
const text = json.choices[0].delta.content;
57+
const queue = encoder.encode(text);
58+
controller.enqueue(queue);
59+
} catch (e) {
60+
controller.error(e);
61+
}
62+
}
63+
};
64+
65+
const parser = createParser(onParse);
66+
67+
const reader = response.data.getReader();
68+
let done = false;
69+
while (!done) {
70+
const { value: chunk, done: doneReading } = await reader.read();
71+
done = doneReading;
72+
parser.feed(decoder.decode(chunk));
73+
}
74+
},
75+
});
76+
77+
const reader = stream.getReader();
78+
let done = false;
79+
while (!done) {
80+
const { value, done: doneReading } = await reader.read();
81+
done = doneReading;
82+
console.log(decoder.decode(value));
83+
}
84+
85+
console.log("Stream finished");
86+
}, 20000);

index.ts

+36-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ import {
44
AxiosRequestConfig,
55
AxiosResponse,
66
} from "axios";
7+
import { ReadableStream } from "web-streams-polyfill";
78
import settle from "./settle";
89
import buildURL from "./helpers/buildURL";
910
import buildFullPath from "./core/buildFullPath";
11+
const SafeHeaders =
12+
typeof Headers !== "undefined" ? Headers : require("node-fetch").Headers;
13+
const SafeRequest =
14+
typeof Request !== "undefined" ? Request : require("node-fetch").Request;
15+
const safeFetch = typeof fetch !== "undefined" ? fetch : require("node-fetch");
1016
import { isUndefined, isStandardBrowserEnv, isFormData } from "./utils";
1117

1218
/**
@@ -51,8 +57,10 @@ async function getResponse(
5157
): Promise<AxiosResponse | Error> {
5258
let stageOne: Response;
5359
try {
54-
stageOne = await fetch(request);
60+
stageOne = await safeFetch(request);
5561
} catch (e) {
62+
if (e instanceof Error)
63+
return createError(e.message, config, "ERR_NETWORK", request);
5664
return createError("Network Error", config, "ERR_NETWORK", request);
5765
}
5866

@@ -69,14 +77,37 @@ async function getResponse(
6977
data = await stageOne.json();
7078
break;
7179
case "stream":
72-
data = stageOne.body;
80+
// Check if the stream is a NodeJS stream or a browser stream.
81+
// @ts-ignore - TS doesn't know about `pipe` on streams.
82+
const isNodeJsStream = typeof stageOne.body.pipe === "function";
83+
data = isNodeJsStream
84+
? nodeToWebReadableStream(stageOne.body)
85+
: stageOne.body;
7386
break;
7487
default:
7588
data = await stageOne.text();
7689
break;
7790
}
7891
}
7992

93+
function nodeToWebReadableStream(nodeReadable) {
94+
return new ReadableStream({
95+
start(controller) {
96+
nodeReadable.on("data", (chunk) => {
97+
controller.enqueue(chunk);
98+
});
99+
100+
nodeReadable.on("end", () => {
101+
controller.close();
102+
});
103+
104+
nodeReadable.on("error", (err) => {
105+
controller.error(err);
106+
});
107+
},
108+
});
109+
}
110+
80111
const response: AxiosResponse<any> = {
81112
data,
82113
status: stageOne.status,
@@ -96,14 +127,14 @@ type Writeable<T> = { -readonly [P in keyof T]: T[P] };
96127
*/
97128
function createRequest(config: AxiosRequestConfig): Request {
98129
const headers = config.headers
99-
? new Headers(
130+
? new SafeHeaders(
100131
Object.keys(config.headers).reduce((obj, key) => {
101132
if (config.headers === undefined) throw Error();
102133
obj[key] = String(config.headers[key]);
103134
return obj;
104135
}, {})
105136
)
106-
: new Headers({});
137+
: new SafeHeaders({});
107138

108139
// HTTP basic authentication
109140
if (config.auth) {
@@ -141,7 +172,7 @@ function createRequest(config: AxiosRequestConfig): Request {
141172
const url = buildURL(fullPath, config.params, config.paramsSerializer);
142173

143174
// Expected browser to throw error if there is any wrong configuration value
144-
return new Request(url, options);
175+
return new SafeRequest(url, options);
145176
}
146177

147178
/**

jest.config.ts

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/**
2+
* For a detailed explanation regarding each configuration property, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
import type { Config } from "jest";
7+
8+
const config: Config = {
9+
preset: "ts-jest",
10+
// All imported modules in your tests should be mocked automatically
11+
// automock: false,
12+
13+
// Stop running tests after `n` failures
14+
// bail: 0,
15+
16+
// The directory where Jest should store its cached dependency information
17+
// cacheDirectory: "/private/var/folders/23/qpmp482j7j594sxdcnrmzdcr0000gn/T/jest_dx",
18+
19+
// Automatically clear mock calls, instances, contexts and results before every test
20+
clearMocks: true,
21+
22+
// Indicates whether the coverage information should be collected while executing the test
23+
// collectCoverage: false,
24+
25+
// An array of glob patterns indicating a set of files for which coverage information should be collected
26+
// collectCoverageFrom: undefined,
27+
28+
// The directory where Jest should output its coverage files
29+
// coverageDirectory: undefined,
30+
31+
// An array of regexp pattern strings used to skip coverage collection
32+
// coveragePathIgnorePatterns: [
33+
// "/node_modules/"
34+
// ],
35+
36+
// Indicates which provider should be used to instrument code for coverage
37+
coverageProvider: "v8",
38+
39+
// A list of reporter names that Jest uses when writing coverage reports
40+
// coverageReporters: [
41+
// "json",
42+
// "text",
43+
// "lcov",
44+
// "clover"
45+
// ],
46+
47+
// An object that configures minimum threshold enforcement for coverage results
48+
// coverageThreshold: undefined,
49+
50+
// A path to a custom dependency extractor
51+
// dependencyExtractor: undefined,
52+
53+
// Make calling deprecated APIs throw helpful error messages
54+
// errorOnDeprecated: false,
55+
56+
// The default configuration for fake timers
57+
// fakeTimers: {
58+
// "enableGlobally": false
59+
// },
60+
61+
// Force coverage collection from ignored files using an array of glob patterns
62+
// forceCoverageMatch: [],
63+
64+
// A path to a module which exports an async function that is triggered once before all test suites
65+
// globalSetup: undefined,
66+
67+
// A path to a module which exports an async function that is triggered once after all test suites
68+
// globalTeardown: undefined,
69+
70+
// A set of global variables that need to be available in all test environments
71+
// globals: {},
72+
73+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
74+
// maxWorkers: "50%",
75+
76+
// An array of directory names to be searched recursively up from the requiring module's location
77+
// moduleDirectories: [
78+
// "node_modules"
79+
// ],
80+
81+
// An array of file extensions your modules use
82+
// moduleFileExtensions: [
83+
// "js",
84+
// "mjs",
85+
// "cjs",
86+
// "jsx",
87+
// "ts",
88+
// "tsx",
89+
// "json",
90+
// "node"
91+
// ],
92+
93+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
94+
// moduleNameMapper: {},
95+
96+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
97+
// modulePathIgnorePatterns: [],
98+
99+
// Activates notifications for test results
100+
// notify: false,
101+
102+
// An enum that specifies notification mode. Requires { notify: true }
103+
// notifyMode: "failure-change",
104+
105+
// A preset that is used as a base for Jest's configuration
106+
// preset: undefined,
107+
108+
// Run tests from one or more projects
109+
// projects: undefined,
110+
111+
// Use this configuration option to add custom reporters to Jest
112+
// reporters: undefined,
113+
114+
// Automatically reset mock state before every test
115+
// resetMocks: false,
116+
117+
// Reset the module registry before running each individual test
118+
// resetModules: false,
119+
120+
// A path to a custom resolver
121+
// resolver: undefined,
122+
123+
// Automatically restore mock state and implementation before every test
124+
// restoreMocks: false,
125+
126+
// The root directory that Jest should scan for tests and modules within
127+
// rootDir: undefined,
128+
129+
// A list of paths to directories that Jest should use to search for files in
130+
// roots: [
131+
// "<rootDir>"
132+
// ],
133+
134+
// Allows you to use a custom runner instead of Jest's default test runner
135+
// runner: "jest-runner",
136+
137+
// The paths to modules that run some code to configure or set up the testing environment before each test
138+
// setupFiles: [],
139+
140+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
141+
// setupFilesAfterEnv: [],
142+
143+
// The number of seconds after which a test is considered as slow and reported as such in the results.
144+
// slowTestThreshold: 5,
145+
146+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
147+
// snapshotSerializers: [],
148+
149+
// The test environment that will be used for testing
150+
testEnvironment: "node",
151+
152+
// Options that will be passed to the testEnvironment
153+
// testEnvironmentOptions: {},
154+
155+
// Adds a location field to test results
156+
// testLocationInResults: false,
157+
158+
// The glob patterns Jest uses to detect test files
159+
// testMatch: [
160+
// "**/__tests__/**/*.[jt]s?(x)",
161+
// "**/?(*.)+(spec|test).[tj]s?(x)"
162+
// ],
163+
164+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
165+
// testPathIgnorePatterns: [
166+
// "/node_modules/"
167+
// ],
168+
169+
// The regexp pattern or array of patterns that Jest uses to detect test files
170+
// testRegex: [],
171+
172+
// This option allows the use of a custom results processor
173+
// testResultsProcessor: undefined,
174+
175+
// This option allows use of a custom test runner
176+
// testRunner: "jest-circus/runner",
177+
178+
// A map from regular expressions to paths to transformers
179+
// transform: undefined,
180+
181+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
182+
// transformIgnorePatterns: [
183+
// "/node_modules/",
184+
// "\\.pnp\\.[^\\/]+$"
185+
// ],
186+
187+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
188+
// unmockedModulePathPatterns: undefined,
189+
190+
// Indicates whether each individual test should be reported during the run
191+
// verbose: undefined,
192+
193+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
194+
// watchPathIgnorePatterns: [],
195+
196+
// Whether to use watchman for file crawling
197+
// watchman: true,
198+
};
199+
200+
export default config;

0 commit comments

Comments
 (0)