Skip to content

Commit

Permalink
feat: revert to node-fetch@2
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Dec 5, 2022
1 parent 216aaf8 commit 26f1090
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"dependencies": {
"eventsource-parser": "^0.0.5",
"expiry-map": "^2.0.0",
"node-fetch": "2",
"remark": "^14.0.2",
"strip-markdown": "^5.0.0",
"undici": "^5.13.0",
"uuid": "^9.0.0"
},
"devDependencies": {
Expand Down
50 changes: 29 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ dotenv.config()

/**
* Example CLI for testing functionality.
*
* ```
* npx tsx src/demo.ts
* ```
*/
async function main() {
const api = new ChatGPTAPI({ sessionToken: process.env.SESSION_TOKEN })
Expand Down
19 changes: 14 additions & 5 deletions src/fetch-sse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createParser } from 'eventsource-parser'

import { fetch } from './fetch'
import { streamAsyncIterable } from './stream-async-iterable'

// import { streamAsyncIterable } from './stream-async-iterable'

export async function fetchSSE(
url: string,
Expand All @@ -15,8 +16,16 @@ export async function fetchSSE(
}
})

for await (const chunk of streamAsyncIterable(resp.body)) {
const str = new TextDecoder().decode(chunk)
parser.feed(str)
}
resp.body.on('readable', () => {
let chunk: string | Buffer
while (null !== (chunk = resp.body.read())) {
parser.feed(chunk.toString())
}
})

// TODO: add support for web-compatible `fetch`
// for await (const chunk of streamAsyncIterable(resp.body)) {
// const str = new TextDecoder().decode(chunk)
// parser.feed(str)
// }
}
2 changes: 1 addition & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { fetch } from 'undici'
import fetch from 'node-fetch'

export { fetch }

0 comments on commit 26f1090

Please sign in to comment.