Skip to content

Commit

Permalink
Merge pull request #10 from galaxiajs/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
Ernxst authored Jul 3, 2024
2 parents f9f3c45 + 1ca2619 commit 3fa58cf
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-peaches-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@galaxiajs/cloudflare-kit": patch
---

Add method to execute code after request handling
Binary file modified bun.lockb
Binary file not shown.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"---------------------------------------------------------------------------------": "",
"test": "turbo run test",
"test:watch": "turbo run test:watch",
"coverage": "turbo run coverage",
"coverage": "vitest run --coverage",
"----------------------------------------------------------------------------------": "",
"release": "changeset publish"
},
Expand All @@ -27,8 +27,10 @@
"@changesets/cli": "^2.27.5",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@vitest/coverage-istanbul": "1.5.0",
"lefthook": "^1.6.14",
"turbo": "^1.13.3"
"turbo": "^1.13.3",
"vitest": "1.5.0"
},
"packageManager": "[email protected]",
"engines": {
Expand Down
42 changes: 10 additions & 32 deletions packages/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,44 +44,23 @@
"default": "./dist/vitest/index.js"
}
},
"files": [
"dist",
"package.json"
],
"files": ["dist", "package.json"],
"typesVersions": {
"*": {
".": [
"./dist/index"
],
"esbuild": [
"./dist/esbuild/index"
],
"cache": [
"./dist/cache/index"
],
"context": [
"./dist/context/index"
],
"cookies": [
"./dist/cookies/index"
],
"user-agent": [
"./dist/user-agent/index"
],
"vitest": [
"./dist/vitest/index"
],
"types": [
"./dist/index"
]
".": ["./dist/index"],
"esbuild": ["./dist/esbuild/index"],
"cache": ["./dist/cache/index"],
"context": ["./dist/context/index"],
"cookies": ["./dist/cookies/index"],
"user-agent": ["./dist/user-agent/index"],
"vitest": ["./dist/vitest/index"],
"types": ["./dist/index"]
}
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"workspaces": [
"examples/**/*"
],
"workspaces": ["examples/**/*"],
"keywords": [
"api-kit",
"web",
Expand Down Expand Up @@ -131,7 +110,6 @@
"@types/node": "^20.13.0",
"@types/set-cookie-parser": "2.4.7",
"@types/ua-parser-js": "0.7.39",
"@vitest/coverage-istanbul": "1.5.0",
"publint": "^0.2.8",
"tsup": "^8.0.2",
"typescript": "^5.4.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/cloudflare/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export {
type $Locals as Locals,
type $Secrets as Secrets,
} from "./modules/env";
export { executionContext, waitUntil } from "./modules/execution-context";
export { executionContext, waitUntil, after } from "./modules/execution-context";
export {
headers,
ip,
Expand Down
20 changes: 15 additions & 5 deletions packages/cloudflare/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "./modules/context/context";
import { createContext, createLocals } from "./modules/context/utils";
import type { Env } from "./modules/env";
import { afters } from "./modules/execution-context";
import type { ExportedWorker, Handler, MakeAsync, Promisable } from "./types";

/**
Expand Down Expand Up @@ -77,12 +78,14 @@ export function handleRequest(ctx: IHandlerContext, handle: () => Promisable<Res
const injected = await createLocals();
return await LocalsContext.run(injected, async () => {
const result = await handle();
let response: Response;

if (result instanceof Response) {
for (const [key, value] of result.headers.entries()) {
ctx.response.headers.append(key, value);
}

return new Response(result.body, {
response = new Response(result.body, {
statusText: result.statusText,
status: result.status,
headers: ctx.response.headers as unknown as Headers,
Expand All @@ -93,12 +96,19 @@ export function handleRequest(ctx: IHandlerContext, handle: () => Promisable<Res
// @ts-expect-error is a CF only type
encodeBody: result.encodeBody,
});
} else {
response = createResponse(result, {
status: ctx.response.status,
headers: ctx.response.headers as unknown as Headers,
});
}

return createResponse(result, {
status: ctx.response.status,
headers: ctx.response.headers as unknown as Headers,
});
ctx.executionContext.waitUntil(handleAfter());
return response;
});
});
}

async function handleAfter() {
await Promise.all(afters.map((r) => r()));
}
29 changes: 29 additions & 0 deletions packages/cloudflare/src/modules/execution-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,32 @@ export function waitUntil(promise: Promise<any>): void {
const ctx = executionContext();
ctx.waitUntil(promise);
}

export const afters: Array<() => any> = [];

/**
* Allows you to schedule work to be executed after a response is finished.
* This is useful for tasks and other side effects that should not block
* the response, such as logging and analytics.
*
* Unlike {@linkcode waitUntil}, this will run _after_ the response has turned,
* rather than being kicked off concurrently during the request cycle.
*
* `after()` will be executed even if the response didn't complete successfully,
* including when an error is thrown or when `redirect()` is called.
*
* Note that you cannot:
*
* - Set cookies
* - Modify response headers
* - Modify response status
*
* Inside `after()` as the response will have already been sent. Doing any of these
* will throw a Cloudflare error.
*
* @param {() => any} callback A function that will be executed after the response is finished.
* @returns {void}
*/
export function after(callback: () => any): void {
afters.push(callback);
}
13 changes: 2 additions & 11 deletions packages/cloudflare/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config";
import paths from "vite-tsconfig-paths";

const COMPATIBILITY_DATE = "2024-04-05";
const COMPATIBILITY_FLAGS = ["nodejs_compat"];

export default defineWorkersConfig({
export default defineWorkersProject({
plugins: [paths()],
esbuild: {
// https://github.com/vitejs/vite/issues/15464#issuecomment-1872485703
target: "es2020",
},
test: {
coverage: {
all: true,
reportOnFailure: true,
provider: "istanbul",
},
poolOptions: {
workers: {
singleWorker: true,
Expand Down
20 changes: 20 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { coverageConfigDefaults, defineConfig } from "vitest/config";

export default defineConfig({
test: {
coverage: {
provider: "istanbul",
reportOnFailure: true,
// TODO: Remove this once https://github.com/vitest-dev/vitest/issues/5856 is fixed
all: false,
exclude: [
...coverageConfigDefaults.exclude,
"**/__test__/**/*",
"**/__tests__/**/*",
"**/__mocks__/**/*",
"**/test/**/*",
"**/tests/**/*",
],
},
},
});
9 changes: 9 additions & 0 deletions vitest.workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineWorkspace } from "vitest/config";

export default defineWorkspace([
"packages/*/vite.config.ts",
"packages/*/vite.config.*.ts",
"packages/*/vitest.config.ts",
"packages/*/vitest.config.*.ts",
"!packages/**/*.d.ts",
]);

0 comments on commit 3fa58cf

Please sign in to comment.