-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
58 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"barnard59-http": patch | ||
--- | ||
|
||
Added TS declarations |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.ts | ||
!*.d.ts | ||
test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { SpanStatusCode } from '@opentelemetry/api' | ||
import toReadable from 'duplex-to/readable.js' | ||
import type { RequestInit } from 'node-fetch' | ||
import nodeFetch from 'node-fetch' | ||
import tracer from './tracer.js' | ||
|
||
async function fetch({ method = 'GET', url, ...options } = {}) { | ||
export type GetInit = RequestInit & { url: string } | ||
|
||
export default async function fetch({ method = 'GET', url, ...options }: GetInit) { | ||
return await tracer.startActiveSpan('fetch', async span => { | ||
try { | ||
const response = await nodeFetch(url, { method, ...options }) | ||
|
||
return toReadable(response.body) | ||
} catch (e) { | ||
return toReadable(response.body as any) | ||
} catch (e: any) { | ||
span.recordException(e) | ||
span.setStatus({ code: SpanStatusCode.ERROR, message: e.message }) | ||
} finally { | ||
span.end() | ||
} | ||
}) | ||
} | ||
|
||
export default fetch |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { Duplex } from 'node:stream' | ||
import { PassThrough, Readable } from 'node:stream' | ||
import duplexify from 'duplexify' | ||
import tracer from './tracer.js' | ||
|
||
export type PostInit = RequestInit & { url: string } | ||
|
||
export default async function writableFetch({ method = 'POST', url, ...options }: PostInit): Promise<Duplex> { | ||
const inputStream: any = new PassThrough() | ||
const outputStream = new PassThrough() | ||
|
||
tracer.startActiveSpan('writableFetch', span => setTimeout(async () => { | ||
try { | ||
const response = await fetch(url, { method, body: inputStream, ...options }) | ||
|
||
Readable.fromWeb(response.body as any).pipe(outputStream) | ||
} catch (err) { | ||
outputStream.emit('error', err) | ||
} finally { | ||
span.end() | ||
} | ||
}, 0)) | ||
|
||
return duplexify(inputStream, outputStream) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"files": ["./index.ts"], | ||
"compilerOptions": { | ||
"skipLibCheck": true | ||
} | ||
} |