-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test proxy server so we won't hit Node.js dist so much (#976)
- Loading branch information
Showing
6 changed files
with
80 additions
and
0 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 |
---|---|---|
|
@@ -7,3 +7,4 @@ feature_tests/.tmp | |
*.log | ||
/site/public | ||
.vercel | ||
.proxy |
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,5 @@ | ||
import { server } from "./tests/proxy-server/index.mjs" | ||
|
||
export default function () { | ||
server.listen(8080) | ||
} |
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 @@ | ||
import { server } from "./tests/proxy-server/index.mjs" | ||
|
||
export default () => { | ||
server.close() | ||
} |
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,65 @@ | ||
// @ts-check | ||
|
||
import { createServer } from "node:http" | ||
import path from "node:path" | ||
import fs from "node:fs" | ||
import crypto from "node:crypto" | ||
import fetch from "node-fetch" | ||
import chalk from "chalk" | ||
|
||
const baseDir = path.join(process.cwd(), ".proxy") | ||
try { | ||
fs.mkdirSync(baseDir, { recursive: true }) | ||
} catch (e) {} | ||
|
||
/** @type {Map<string, Promise<{ headers: Record<string, string>, body: ArrayBuffer }>>} */ | ||
const cache = new Map() | ||
|
||
export const server = createServer((req, res) => { | ||
const pathname = req.url ?? "/" | ||
const hash = crypto | ||
.createHash("sha1") | ||
.update(pathname ?? "/") | ||
.digest("hex") | ||
const extension = path.extname(pathname) | ||
const filename = path.join(baseDir, hash) + extension | ||
const headersFilename = path.join(baseDir, hash) + ".headers.json" | ||
try { | ||
const headers = JSON.parse(fs.readFileSync(headersFilename, "utf-8")) | ||
const body = fs.createReadStream(filename) | ||
console.log(chalk.green.dim(`[proxy] hit: ${pathname} -> ${filename}`)) | ||
res.writeHead(200, headers) | ||
body.pipe(res) | ||
} catch { | ||
let promise = cache.get(filename) | ||
if (!promise) { | ||
console.log(chalk.red.dim(`[proxy] miss: ${pathname} -> ${filename}`)) | ||
promise = fetch( | ||
"https://nodejs.org/dist/" + pathname.replace(/^\/+/, ""), | ||
{ | ||
compress: false, | ||
} | ||
).then(async (response) => { | ||
const headers = Object.fromEntries(response.headers.entries()) | ||
const body = await response.arrayBuffer() | ||
fs.writeFileSync(headersFilename, JSON.stringify(headers)) | ||
fs.writeFileSync(filename, Buffer.from(body)) | ||
return { headers, body } | ||
}) | ||
cache.set(filename, promise) | ||
promise.finally(() => cache.delete(filename)) | ||
} | ||
|
||
promise.then( | ||
({ headers, body }) => { | ||
res.writeHead(200, headers) | ||
res.end(Buffer.from(body)) | ||
}, | ||
(err) => { | ||
console.error(err) | ||
res.writeHead(500) | ||
res.end() | ||
} | ||
) | ||
} | ||
}) |
10ad688
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#976