-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
917d5c5
commit d614ced
Showing
12 changed files
with
175 additions
and
67 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
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,63 @@ | ||
import { Event } from './event'; | ||
import { HttpRequest } from './http'; | ||
|
||
// TODO: the types in this file are from lib/dom | ||
// we need to replace them with a platform agnostic version | ||
|
||
function formatFetchHeaders(headers: HeadersInit | Headers | undefined): HttpRequest['requestHeaders'] { | ||
if (headers) { | ||
if (Array.isArray(headers)) { | ||
return headers.reduce<HttpRequest['requestHeaders']>((acc, [key, value]) => { | ||
acc[key] = value; | ||
return acc; | ||
}, {}); | ||
} else if (headers instanceof Headers) { | ||
return Object.fromEntries(headers.entries()); | ||
} else { | ||
return headers; | ||
} | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
export function fetchRequestToEvent(id: string, input: RequestInfo | URL, init?: RequestInit): Event { | ||
let url: URL; | ||
if (typeof input === 'string') { | ||
url = new URL(input); | ||
} else if (input instanceof Request) { | ||
url = new URL(input.url); | ||
} else { | ||
url = input; | ||
} | ||
|
||
return { | ||
id, | ||
parentId: undefined, | ||
timestamp: Date.now(), | ||
http: { | ||
method: (init?.method ?? 'GET') as HttpRequest['method'], | ||
host: url.host, | ||
port: parseInt(url.port, 10), | ||
path: url.pathname, | ||
url: url.toString(), | ||
requestHeaders: formatFetchHeaders(init?.headers), | ||
requestBody: init?.body?.toString() ?? undefined, | ||
}, | ||
}; | ||
} | ||
|
||
export async function fetchResponseToEvent(req: Event, response: Response): Promise<Event> { | ||
return { | ||
...req, | ||
|
||
http: { | ||
...req.http!, | ||
httpVersion: response.type, | ||
statusCode: response.status, | ||
statusMessage: response.statusText, | ||
responseHeaders: formatFetchHeaders(response.headers), | ||
responseBody: await response.text(), | ||
}, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,22 @@ | ||
import { HttpRequest } from '.'; | ||
|
||
export type FilterableHttpRequest = Pick<HttpRequest, 'host' | 'method'>; | ||
|
||
export interface Options { | ||
/** | ||
* A unique identifier for the application | ||
*/ | ||
serviceName: string; | ||
|
||
/** | ||
* Set to true to enable debugging of exported messages | ||
* @default false | ||
*/ | ||
debug?: boolean; | ||
|
||
/** | ||
* Define a function to filter http requests | ||
* @default undefined | ||
*/ | ||
filter?: (request: FilterableHttpRequest) => boolean; | ||
} |
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,27 @@ | ||
import { Plugin, fetchRequestToEvent, fetchResponseToEvent } from '@envyjs/core'; | ||
|
||
import { generateId } from './id'; | ||
|
||
export const Fetch: Plugin = (_options, exporter) => { | ||
const { fetch: originalFetch } = global; | ||
global.fetch = async (...args) => { | ||
const id = generateId(); | ||
const startTs = performance.now(); | ||
|
||
// export the initial request data | ||
const reqEvent = fetchRequestToEvent(id, ...args); | ||
exporter.send(reqEvent); | ||
|
||
// execute the actual request | ||
const response = await originalFetch(...args); | ||
const responseClone = response.clone(); | ||
const resEvent = await fetchResponseToEvent(reqEvent, responseClone); | ||
|
||
resEvent.http!.duration = performance.now() - startTs; | ||
|
||
// export the final request data which now includes response | ||
exporter.send(resEvent); | ||
|
||
return response; | ||
}; | ||
}; |
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
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 |
---|---|---|
|
@@ -3465,6 +3465,13 @@ buffer@^5.5.0||^6.0.0: | |
base64-js "^1.3.1" | ||
ieee754 "^1.2.1" | ||
|
||
[email protected]: | ||
version "4.0.7" | ||
resolved "https://registry.yarnpkg.com/bufferutil/-/bufferutil-4.0.7.tgz#60c0d19ba2c992dd8273d3f73772ffc894c153ad" | ||
integrity sha512-kukuqc39WOHtdxtw4UScxF/WVnMFVSQVKhtx3AjZJzhd0RGZZldcrfSEbVsWWe6KNH253574cq5F+wpv0G9pJw== | ||
dependencies: | ||
node-gyp-build "^4.3.0" | ||
|
||
bundle-name@^3.0.0: | ||
version "3.0.0" | ||
resolved "https://registry.yarnpkg.com/bundle-name/-/bundle-name-3.0.0.tgz#ba59bcc9ac785fb67ccdbf104a2bf60c099f0e1a" | ||
|
@@ -7204,6 +7211,11 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/node-gyp-build-optional-packages/-/node-gyp-build-optional-packages-5.0.7.tgz#5d2632bbde0ab2f6e22f1bbac2199b07244ae0b3" | ||
integrity sha512-YlCCc6Wffkx0kHkmam79GKvDQ6x+QZkMjFGrIMxgFNILFvGSbCp2fCBC55pGTT9gVaz8Na5CLmxt/urtzRv36w== | ||
|
||
node-gyp-build@^4.3.0: | ||
version "4.6.1" | ||
resolved "https://registry.yarnpkg.com/node-gyp-build/-/node-gyp-build-4.6.1.tgz#24b6d075e5e391b8d5539d98c7fc5c210cac8a3e" | ||
integrity sha512-24vnklJmyRS8ViBNI8KbtK/r/DmXQMRiOMXTNz2nrTnAYUwjmEEbnnpB/+kt+yWRv73bPsSPRFddrcIbAxSiMQ== | ||
|
||
node-int64@^0.4.0: | ||
version "0.4.0" | ||
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b" | ||
|
@@ -9299,6 +9311,13 @@ use-latest@^1.2.1: | |
dependencies: | ||
use-isomorphic-layout-effect "^1.1.1" | ||
|
||
[email protected]: | ||
version "6.0.3" | ||
resolved "https://registry.yarnpkg.com/utf-8-validate/-/utf-8-validate-6.0.3.tgz#7d8c936d854e86b24d1d655f138ee27d2636d777" | ||
integrity sha512-uIuGf9TWQ/y+0Lp+KGZCMuJWc3N9BHA+l/UmHd/oUHwJJDeysyTRxNQVkbzsIWfGFbRe3OcgML/i0mvVRPOyDA== | ||
dependencies: | ||
node-gyp-build "^4.3.0" | ||
|
||
util-deprecate@^1.0.1, util-deprecate@^1.0.2, util-deprecate@~1.0.1: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" | ||
|