@cfworker/web 1.12.5 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/application.d.ts +5 -1
- package/dist/application.js +12 -9
- package/dist/context.d.ts +6 -4
- package/dist/context.js +9 -7
- package/package.json +2 -2
- package/src/application.ts +26 -10
- package/src/context.ts +14 -9
- package/src/declarations.d.ts +4 -0
package/dist/application.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { Middleware } from './middleware.js';
|
|
2
2
|
export declare class Application {
|
|
3
3
|
private readonly middleware;
|
|
4
|
+
private _composedMiddleware?;
|
|
5
|
+
private get composedMiddleware();
|
|
4
6
|
use(middleware: Middleware): this;
|
|
7
|
+
handleRequest(request: Request, env: any, context: {
|
|
8
|
+
waitUntil(promise: Promise<any>): void;
|
|
9
|
+
}): Promise<Response>;
|
|
5
10
|
listen(): void;
|
|
6
|
-
private handleFetch;
|
|
7
11
|
private invokeMiddleware;
|
|
8
12
|
}
|
package/dist/application.js
CHANGED
|
@@ -7,20 +7,23 @@ export class Application {
|
|
|
7
7
|
constructor() {
|
|
8
8
|
this.middleware = [];
|
|
9
9
|
}
|
|
10
|
+
get composedMiddleware() {
|
|
11
|
+
if (!this._composedMiddleware) {
|
|
12
|
+
this._composedMiddleware = composeMiddleware(this.middleware);
|
|
13
|
+
}
|
|
14
|
+
return this._composedMiddleware;
|
|
15
|
+
}
|
|
10
16
|
use(middleware) {
|
|
11
17
|
this.middleware.push(middleware);
|
|
18
|
+
this._composedMiddleware = undefined;
|
|
12
19
|
return this;
|
|
13
20
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
addEventListener('fetch', event => this.handleFetch(event, middleware));
|
|
21
|
+
async handleRequest(request, env, context) {
|
|
22
|
+
return this.invokeMiddleware(new Context(request, env, context), this.composedMiddleware);
|
|
17
23
|
}
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
event.respondWith(
|
|
21
|
-
this.invokeMiddleware(context, middleware),
|
|
22
|
-
context.responded
|
|
23
|
-
]));
|
|
24
|
+
listen() {
|
|
25
|
+
const middleware = this.composedMiddleware;
|
|
26
|
+
addEventListener('fetch', event => event.respondWith(this.invokeMiddleware(Context.fromFetchEvent(event), middleware)));
|
|
24
27
|
}
|
|
25
28
|
async invokeMiddleware(context, middleware) {
|
|
26
29
|
try {
|
package/dist/context.d.ts
CHANGED
|
@@ -2,13 +2,15 @@ import { Cookies } from './cookies.js';
|
|
|
2
2
|
import { Req } from './req.js';
|
|
3
3
|
import { ResponseBuilder } from './response-builder.js';
|
|
4
4
|
export declare class Context {
|
|
5
|
-
|
|
5
|
+
readonly env: any;
|
|
6
|
+
private readonly _context;
|
|
6
7
|
readonly req: Req;
|
|
7
8
|
readonly res: ResponseBuilder;
|
|
8
9
|
readonly cookies: Cookies;
|
|
9
|
-
readonly respondWith: (response: Response) => void;
|
|
10
|
-
readonly responded: Promise<Response>;
|
|
11
10
|
readonly state: any;
|
|
12
|
-
|
|
11
|
+
static fromFetchEvent(event: FetchEvent): Context;
|
|
12
|
+
constructor(request: Request, env: any, _context: {
|
|
13
|
+
waitUntil(promise: Promise<any>): void;
|
|
14
|
+
});
|
|
13
15
|
waitUntil(promise: Promise<any>): void;
|
|
14
16
|
}
|
package/dist/context.js
CHANGED
|
@@ -2,18 +2,20 @@ import { Cookies } from './cookies.js';
|
|
|
2
2
|
import { Req } from './req.js';
|
|
3
3
|
import { ResponseBuilder } from './response-builder.js';
|
|
4
4
|
export class Context {
|
|
5
|
-
constructor(
|
|
6
|
-
this.
|
|
7
|
-
|
|
5
|
+
constructor(request, env, _context) {
|
|
6
|
+
this.env = env;
|
|
7
|
+
this._context = _context;
|
|
8
8
|
this.req = new Req(request);
|
|
9
9
|
this.res = new ResponseBuilder();
|
|
10
10
|
this.cookies = new Cookies(request.headers, this.res.headers);
|
|
11
|
-
this.responded = new Promise(resolve => {
|
|
12
|
-
this.respondWith = resolve;
|
|
13
|
-
});
|
|
14
11
|
this.state = {};
|
|
15
12
|
}
|
|
13
|
+
static fromFetchEvent(event) {
|
|
14
|
+
return new Context(event.request, globalThis, {
|
|
15
|
+
waitUntil: event.waitUntil.bind(event)
|
|
16
|
+
});
|
|
17
|
+
}
|
|
16
18
|
waitUntil(promise) {
|
|
17
|
-
this.
|
|
19
|
+
this._context.waitUntil(promise);
|
|
18
20
|
}
|
|
19
21
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfworker/web",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"description": "Web framework for Cloudflare Workers and service workers, inspired by Koa and fastify",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"middleware",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"test": "cfworker test test/**/*.spec.ts --nocheck"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@cfworker/json-schema": "^1.12.
|
|
37
|
+
"@cfworker/json-schema": "^1.12.6",
|
|
38
38
|
"@cfworker/worker-types": "^1.12.3",
|
|
39
39
|
"@cloudflare/workers-types": "^2.2.2",
|
|
40
40
|
"@types/cookie": "^0.5.1",
|
package/src/application.ts
CHANGED
|
@@ -7,24 +7,40 @@ const resolved = Promise.resolve();
|
|
|
7
7
|
|
|
8
8
|
export class Application {
|
|
9
9
|
private readonly middleware: Middleware[] = [];
|
|
10
|
+
private _composedMiddleware?: Middleware;
|
|
11
|
+
|
|
12
|
+
private get composedMiddleware() {
|
|
13
|
+
if (!this._composedMiddleware) {
|
|
14
|
+
this._composedMiddleware = composeMiddleware(this.middleware);
|
|
15
|
+
}
|
|
16
|
+
return this._composedMiddleware;
|
|
17
|
+
}
|
|
10
18
|
|
|
11
19
|
public use(middleware: Middleware) {
|
|
12
20
|
this.middleware.push(middleware);
|
|
21
|
+
this._composedMiddleware = undefined;
|
|
13
22
|
return this;
|
|
14
23
|
}
|
|
15
24
|
|
|
16
|
-
public
|
|
17
|
-
|
|
18
|
-
|
|
25
|
+
public async handleRequest(
|
|
26
|
+
request: Request,
|
|
27
|
+
env: any,
|
|
28
|
+
context: {
|
|
29
|
+
waitUntil(promise: Promise<any>): void;
|
|
30
|
+
}
|
|
31
|
+
): Promise<Response> {
|
|
32
|
+
return this.invokeMiddleware(
|
|
33
|
+
new Context(request, env, context),
|
|
34
|
+
this.composedMiddleware
|
|
35
|
+
);
|
|
19
36
|
}
|
|
20
37
|
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
event
|
|
24
|
-
|
|
25
|
-
this.invokeMiddleware(
|
|
26
|
-
|
|
27
|
-
])
|
|
38
|
+
public listen() {
|
|
39
|
+
const middleware = this.composedMiddleware;
|
|
40
|
+
addEventListener('fetch', event =>
|
|
41
|
+
event.respondWith(
|
|
42
|
+
this.invokeMiddleware(Context.fromFetchEvent(event), middleware)
|
|
43
|
+
)
|
|
28
44
|
);
|
|
29
45
|
}
|
|
30
46
|
|
package/src/context.ts
CHANGED
|
@@ -6,23 +6,28 @@ export class Context {
|
|
|
6
6
|
public readonly req: Req;
|
|
7
7
|
public readonly res: ResponseBuilder;
|
|
8
8
|
public readonly cookies: Cookies;
|
|
9
|
-
public readonly respondWith!: (response: Response) => void;
|
|
10
|
-
public readonly responded: Promise<Response>;
|
|
11
9
|
public readonly state: any;
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
static fromFetchEvent(event: FetchEvent) {
|
|
12
|
+
return new Context(event.request, globalThis as any, {
|
|
13
|
+
waitUntil: event.waitUntil.bind(event)
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
request: Request,
|
|
19
|
+
public readonly env: any,
|
|
20
|
+
private readonly _context: {
|
|
21
|
+
waitUntil(promise: Promise<any>): void;
|
|
22
|
+
}
|
|
23
|
+
) {
|
|
15
24
|
this.req = new Req(request);
|
|
16
25
|
this.res = new ResponseBuilder();
|
|
17
26
|
this.cookies = new Cookies(request.headers, this.res.headers);
|
|
18
|
-
this.responded = new Promise<Response>(resolve => {
|
|
19
|
-
// @ts-ignore
|
|
20
|
-
this.respondWith = resolve;
|
|
21
|
-
});
|
|
22
27
|
this.state = {};
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
public waitUntil(promise: Promise<any>) {
|
|
26
|
-
this.
|
|
31
|
+
this._context.waitUntil(promise);
|
|
27
32
|
}
|
|
28
33
|
}
|