Skip to content

Commit

Permalink
feat: allow to return debug info using X-Debug header
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Nov 20, 2023
1 parent 75ae70b commit 972ac79
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions deno.lock

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

2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'https://deno.land/std/dotenv/mod.ts';
export { createHash } from 'https://deno.land/std@0.80.0/hash/mod.ts';
export { default as rollupPluginVirtual } from 'npm:@rollup/plugin-virtual@3.0.2';
export type {
InputOptions,
Expand All @@ -11,3 +10,4 @@ export { load as dotenvLoad } from 'https://deno.land/std/dotenv/mod.ts';
export { default as httpz } from 'npm:http-z@6.1.2';
export { serve } from 'https://deno.land/std/http/mod.ts';
export { default as request } from 'npm:request@2.88.2';
export { ulid } from 'https://deno.land/std@0.207.0/ulid/mod.ts';
21 changes: 19 additions & 2 deletions src/esm-proxy-request-handler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { cloneHeaders, _internals } from './utils.ts';
import { resolveConfig } from './resolve-config.ts';
import { toSystemjs } from './to-systemjs.ts';
import { createHash } from '../deps.ts';
import { ulid } from '../deps.ts';

const markNames = ['body', 'fetch1', 'fetch2', 'curl', 'node', 'tosystemjs', 'total'] as const;

export async function esmProxyRequestHandler(
req: Request,
): Promise<Response | never> {
const reqHash = createHash('md5').update(req.url).toString();
const reqHash = ulid();
const prefix = (name: string) => `${reqHash}-${name}`;
markNames.forEach(name => {
const prefixedName = prefix(name);
Expand Down Expand Up @@ -52,6 +52,23 @@ export async function esmProxyRequestHandler(
const esmOriginRegExp = new RegExp(esmOrigin, 'ig');
return (str: string) => str.replace(esmOriginRegExp, selfOrigin);
})();
if (!!req.headers.get('X-Debug')) {
return Response.json({
BASE_PATH,
ESM_ORIGIN,
HOMEPAGE,
OUTPUT_BANNER,
REDIRECT_DETECT,
REDIRECT_FAILURE_CACHE,
selfUrl,
basePath,
esmOrigin,
realUrl,
selfOrigin,
esmUrl,
xRealOrigin: req.headers.get('X-Real-Origin'),
});
}
mark('fetch1');
let esmResponse = await _internals.fetch(esmUrl.toString(), {
headers: req.headers,
Expand Down

0 comments on commit 972ac79

Please sign in to comment.