From 7d3113e837f8c16b2b28230b5468f9d51b384490 Mon Sep 17 00:00:00 2001 From: Callum Loh Date: Tue, 6 Aug 2024 15:51:27 +0100 Subject: [PATCH] Revert "feat: add postBody Functionality" This reverts commit 73e1a3ae320496405b70d60cf02376ba9d724588. --- README.md | 6 ---- src/index.ts | 15 ++------ test/postBody.spec.ts | 83 ------------------------------------------- 3 files changed, 3 insertions(+), 101 deletions(-) delete mode 100644 test/postBody.spec.ts diff --git a/README.md b/README.md index 1644d36..4b31f33 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,6 @@ Library supports all instructions that the Ledge parser supports. Also supports - [varsCookieBlacklist](#varscookieblacklist) - [Custom ESI Vars Function](#custom-esi-vars-function) - [Custom Fetch Function](#custom-fetch-function) - - [Optional Post Body Function](#optional-post-body-function) - [Caching and upstream requests](#caching-and-upstream-requests) - [Edge Side Includes](#edge-side-includes) - [Regular expressions in conditions](#regular-expressions-in-conditions) @@ -59,7 +58,6 @@ new esi( options?: // Config customESIFunction?: // Custom ESI Vars Function fetcher?: // Custom Fetch Function - postBodyFunction?: // Optional function that will be called once the body has been completed ) ``` @@ -247,10 +245,6 @@ new esi(undefined, undefined, customFetcher) ``` -### Optional Post Body Function - -If you need to do extra work once the body has completed streaming, eg you record how many fetches your custom fetcher handles. Or need to fire off some context.waitUntil functions at the end you can fire them after this function has been called. - ## Caching and upstream requests diff --git a/src/index.ts b/src/index.ts index 4adff49..c64e807 100644 --- a/src/index.ts +++ b/src/index.ts @@ -103,7 +103,6 @@ export type customESIVarsFunction = ( request: Request, ) => Promise; export type fetchFunction = (request: string | Request) => Promise; -export type postBodyFunction = () => void | Promise; const processorToken = "ESI"; const processorVersion = 1.0; @@ -112,13 +111,11 @@ export class esi { options: ESIConfig; esiFunction?: customESIVarsFunction; fetcher: fetchFunction; - postBodyFunction?: postBodyFunction; constructor( options?: ESIConfig, customESIFunction?: customESIVarsFunction, fetcher = fetch as fetchFunction, - postBodyFunction?: postBodyFunction, ) { const defaultConfig = { recursionLimit: 10, @@ -126,8 +123,7 @@ export class esi { }; this.options = { ...defaultConfig, ...options }; this.fetcher = fetcher; - this.esiFunction = customESIFunction; - this.postBodyFunction = postBodyFunction; + if (customESIFunction) this.esiFunction = customESIFunction; } async parse(origRequest: Request, recursion = 0): Promise { @@ -256,7 +252,7 @@ export class esi { /** * Flushes output to the Writeable Stream */ - const flush_output = async () => { + async function flush_output() { // Can't call this if we're waiting for an sync option to complete if (pending) { return; @@ -291,13 +287,8 @@ export class esi { if (ended && output.length === 0) { const writer = writable.getWriter(); await writer.close(); - - // we're completely done now notify the post body function - if (this.postBodyFunction) { - this.postBodyFunction(); - } } - }; + } const writer = (text: string, esi: boolean) => { if (esi) { diff --git a/test/postBody.spec.ts b/test/postBody.spec.ts deleted file mode 100644 index d81121e..0000000 --- a/test/postBody.spec.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { checkSurrogate, testResponse, urlHandler } from "./helpers"; -import { esi, ESIConfig } from "../src"; -import { AddressInfo } from "net"; -import http from "http"; - -const esiHead = { - "Content-Type": "text/html", - "Surrogate-Control": `content="ESI/1.0"`, -}; - -let parser: esi; -let config: ESIConfig; -const makeRequest = async function (request: string, details?: RequestInit) { - const reqUrl = new URL(request, `http://localhost:${port}`).toString(); - const req = new Request(reqUrl, details); - return parser.parse(req); -}; - -const routeHandler = new urlHandler(); -// @ts-ignore -const server = http.createServer(routeHandler.route); -let port = 0; -const testingDetails = { - port: 0, - hostname: "localhost", - proto: "http:", - url: `http://localhost:0`, -}; - -beforeAll(() => { - // Setup a basic HTTP server to handle traffic - server.listen(0); - const address = server.address() as AddressInfo; - port = address.port; - testingDetails.port = address.port; - testingDetails.url = `http://localhost:${port}`; -}); - -afterAll((done) => { - server.close(() => { - done(); - }); -}); - -test("TEST 1: postBody Function", async () => { - const url = `/post-body/test-1`; - let count = 0; - const postBody = function () { - expect(count).toEqual(3); - return; - }; - parser = new esi(config, undefined, undefined, postBody); - - const printFragment = function ( - req: http.IncomingMessage, - res: testResponse, - ) { - count++; - const url = new URL(req.url as string, `http://localhost:${port}`); - const query = url.searchParams.get("a") ? url.searchParams.get("a") : ""; - res.end(`FRAGMENT: ${query}\n`); - }; - routeHandler.add(url, function (req, res) { - res.writeHead(200, esiHead); - res.say("1"); - res.write(``); - res.say("2"); - res.write(``); - res.write("3"); - res.end( - ``, - ); - }); - routeHandler.add(`${url}/fragment_1`, printFragment); - routeHandler.add(`${url}/fragment_1?a=2`, printFragment); - routeHandler.add(`${url}/fragment_1?a=3`, printFragment); - const res = await makeRequest(url); - expect(res.ok).toBeTruthy(); - expect(checkSurrogate(res)).toBeTruthy(); - expect(await res.text()).toEqual( - `1\nFRAGMENT: \n2\nFRAGMENT: 2\n3FRAGMENT: 3\n`, - ); -});