@cfworker/web 1.14.0 → 2.0.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/accepts.js +5 -4
- package/dist/application.d.ts +0 -1
- package/dist/application.js +2 -7
- package/dist/context.d.ts +0 -1
- package/dist/context.js +6 -5
- package/dist/cookies.js +2 -0
- package/dist/http-error.js +3 -0
- package/dist/middleware.d.ts +1 -1
- package/dist/req.js +12 -0
- package/dist/response-builder.d.ts +1 -1
- package/dist/response-builder.js +6 -8
- package/dist/router.d.ts +4 -5
- package/dist/router.js +13 -12
- package/dist/validate.d.ts +2 -2
- package/package.json +17 -17
- package/src/application.ts +0 -9
- package/src/context.ts +0 -6
- package/src/declarations.d.ts +4 -0
- package/src/router.ts +5 -13
- package/tsconfig.json +1 -7
package/dist/accepts.js
CHANGED
|
@@ -7,12 +7,13 @@ const parseAcceptLanguage = preferredLanguages;
|
|
|
7
7
|
const parseAcceptEncoding = preferredEncodings;
|
|
8
8
|
const parseAcceptCharset = preferredCharsets;
|
|
9
9
|
export class Accepts {
|
|
10
|
+
headers;
|
|
11
|
+
_type = undefined;
|
|
12
|
+
_language = undefined;
|
|
13
|
+
_encoding = undefined;
|
|
14
|
+
_charset = undefined;
|
|
10
15
|
constructor(headers) {
|
|
11
16
|
this.headers = headers;
|
|
12
|
-
this._type = undefined;
|
|
13
|
-
this._language = undefined;
|
|
14
|
-
this._encoding = undefined;
|
|
15
|
-
this._charset = undefined;
|
|
16
17
|
}
|
|
17
18
|
type(...values) {
|
|
18
19
|
if (!this._type) {
|
package/dist/application.d.ts
CHANGED
package/dist/application.js
CHANGED
|
@@ -4,9 +4,8 @@ import { HttpError } from './http-error.js';
|
|
|
4
4
|
import { composeMiddleware } from './middleware.js';
|
|
5
5
|
const resolved = Promise.resolve();
|
|
6
6
|
export class Application {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
7
|
+
middleware = [];
|
|
8
|
+
_composedMiddleware;
|
|
10
9
|
get composedMiddleware() {
|
|
11
10
|
if (!this._composedMiddleware) {
|
|
12
11
|
this._composedMiddleware = composeMiddleware(this.middleware);
|
|
@@ -21,10 +20,6 @@ export class Application {
|
|
|
21
20
|
async handleRequest(request, env, context) {
|
|
22
21
|
return this.invokeMiddleware(new Context(request, env, context), this.composedMiddleware);
|
|
23
22
|
}
|
|
24
|
-
listen() {
|
|
25
|
-
const middleware = this.composedMiddleware;
|
|
26
|
-
addEventListener('fetch', event => event.respondWith(this.invokeMiddleware(Context.fromFetchEvent(event), middleware)));
|
|
27
|
-
}
|
|
28
23
|
async invokeMiddleware(context, middleware) {
|
|
29
24
|
try {
|
|
30
25
|
await middleware(context, () => resolved);
|
package/dist/context.d.ts
CHANGED
|
@@ -8,7 +8,6 @@ export declare class Context {
|
|
|
8
8
|
readonly res: ResponseBuilder;
|
|
9
9
|
readonly cookies: Cookies;
|
|
10
10
|
readonly state: any;
|
|
11
|
-
static fromFetchEvent(event: FetchEvent): Context;
|
|
12
11
|
constructor(request: Request, environmentBindings: any, _context: {
|
|
13
12
|
waitUntil(promise: Promise<any>): void;
|
|
14
13
|
});
|
package/dist/context.js
CHANGED
|
@@ -2,6 +2,12 @@ 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
|
+
environmentBindings;
|
|
6
|
+
_context;
|
|
7
|
+
req;
|
|
8
|
+
res;
|
|
9
|
+
cookies;
|
|
10
|
+
state;
|
|
5
11
|
constructor(request, environmentBindings, _context) {
|
|
6
12
|
this.environmentBindings = environmentBindings;
|
|
7
13
|
this._context = _context;
|
|
@@ -10,11 +16,6 @@ export class Context {
|
|
|
10
16
|
this.cookies = new Cookies(request.headers, this.res.headers);
|
|
11
17
|
this.state = {};
|
|
12
18
|
}
|
|
13
|
-
static fromFetchEvent(event) {
|
|
14
|
-
return new Context(event.request, globalThis, {
|
|
15
|
-
waitUntil: event.waitUntil.bind(event)
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
19
|
waitUntil(promise) {
|
|
19
20
|
this._context.waitUntil(promise);
|
|
20
21
|
}
|
package/dist/cookies.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { parse as parseCookie, serialize as serializeCookie } from 'cookie';
|
|
2
2
|
const noCookies = Object.create(null);
|
|
3
3
|
export class Cookies {
|
|
4
|
+
responseHeaders;
|
|
5
|
+
requestCookies;
|
|
4
6
|
constructor(requestHeaders, responseHeaders) {
|
|
5
7
|
this.responseHeaders = responseHeaders;
|
|
6
8
|
const cookie = requestHeaders.get('cookie');
|
package/dist/http-error.js
CHANGED
package/dist/middleware.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Context } from './context.js';
|
|
2
|
-
export
|
|
2
|
+
export type Middleware = (context: Context, next: () => Promise<void>) => Promise<void> | void;
|
|
3
3
|
export declare function composeMiddleware(middleware: Middleware[]): Middleware;
|
package/dist/req.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { safeParse } from 'secure-json-parse';
|
|
2
2
|
import { Accepts } from './accepts.js';
|
|
3
3
|
export class Req {
|
|
4
|
+
raw;
|
|
5
|
+
method;
|
|
6
|
+
url;
|
|
7
|
+
headers;
|
|
8
|
+
params;
|
|
9
|
+
accepts;
|
|
10
|
+
body;
|
|
4
11
|
constructor(request) {
|
|
5
12
|
this.raw = request;
|
|
6
13
|
this.method = request.method;
|
|
@@ -12,6 +19,11 @@ export class Req {
|
|
|
12
19
|
}
|
|
13
20
|
}
|
|
14
21
|
export class ReqBody {
|
|
22
|
+
request;
|
|
23
|
+
_arrayBuffer;
|
|
24
|
+
_formData;
|
|
25
|
+
_json;
|
|
26
|
+
_text;
|
|
15
27
|
constructor(request) {
|
|
16
28
|
this.request = request;
|
|
17
29
|
}
|
package/dist/response-builder.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import statuses from 'statuses';
|
|
2
2
|
export class ResponseBuilder {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
this._stringifyBody = false;
|
|
10
|
-
}
|
|
3
|
+
headers = new Headers();
|
|
4
|
+
_status = 404;
|
|
5
|
+
_explicitStatus = false;
|
|
6
|
+
_implicitType = false;
|
|
7
|
+
_body = null;
|
|
8
|
+
_stringifyBody = false;
|
|
11
9
|
get status() {
|
|
12
10
|
return this._status;
|
|
13
11
|
}
|
package/dist/router.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PathToRegexpOptions } from 'path-to-regexp';
|
|
2
2
|
import { Context } from './context.js';
|
|
3
3
|
import { Middleware } from './middleware.js';
|
|
4
|
-
export declare type PathToRegExpOptions = TokensToRegexpOptions & ParseOptions;
|
|
5
4
|
export declare const Method: (method: string) => ({ req }: Context) => boolean;
|
|
6
5
|
export declare const Get: ({ req }: Context) => boolean;
|
|
7
6
|
export declare const Post: ({ req }: Context) => boolean;
|
|
@@ -13,14 +12,14 @@ export declare const Options: ({ req }: Context) => boolean;
|
|
|
13
12
|
export declare const Header: (header: string, value: string) => ({ req }: Context) => boolean;
|
|
14
13
|
export declare const Host: (host: string) => ({ req }: Context) => boolean;
|
|
15
14
|
export declare const Referer: (host: string) => ({ req }: Context) => boolean;
|
|
16
|
-
export declare const Path: (pattern: string, options?:
|
|
17
|
-
export
|
|
15
|
+
export declare const Path: (pattern: string, options?: PathToRegexpOptions) => ({ req: { url, params } }: Context) => boolean;
|
|
16
|
+
export type RouteCondition = (context: Context) => boolean;
|
|
18
17
|
export interface Route {
|
|
19
18
|
conditions: RouteCondition[];
|
|
20
19
|
middleware: Middleware;
|
|
21
20
|
}
|
|
22
21
|
export interface RouterOptions {
|
|
23
|
-
pathToRegExpOptions?:
|
|
22
|
+
pathToRegExpOptions?: PathToRegexpOptions;
|
|
24
23
|
}
|
|
25
24
|
export declare const defaultRouterOptions: RouterOptions;
|
|
26
25
|
export declare class Router {
|
package/dist/router.js
CHANGED
|
@@ -18,14 +18,13 @@ export const Header = (header, value) => {
|
|
|
18
18
|
export const Host = (host) => Header('host', host);
|
|
19
19
|
export const Referer = (host) => Header('referer', host);
|
|
20
20
|
export const Path = (pattern, options) => {
|
|
21
|
-
const
|
|
22
|
-
const regExp = pathToRegexp(pattern, keys, options);
|
|
21
|
+
const regExp = pathToRegexp(pattern, options);
|
|
23
22
|
return ({ req: { url, params } }) => {
|
|
24
23
|
const match = url.pathname.match(regExp);
|
|
25
24
|
if (!match) {
|
|
26
25
|
return false;
|
|
27
26
|
}
|
|
28
|
-
collectParameters(keys, match, params);
|
|
27
|
+
collectParameters(regExp.keys, match, params);
|
|
29
28
|
return true;
|
|
30
29
|
};
|
|
31
30
|
};
|
|
@@ -33,17 +32,10 @@ export const defaultRouterOptions = {
|
|
|
33
32
|
pathToRegExpOptions: { strict: true }
|
|
34
33
|
};
|
|
35
34
|
export class Router {
|
|
35
|
+
options;
|
|
36
|
+
routes;
|
|
36
37
|
constructor(options = defaultRouterOptions) {
|
|
37
38
|
this.options = options;
|
|
38
|
-
this.middleware = async (ctx, next) => {
|
|
39
|
-
const resolved = this.resolve(ctx);
|
|
40
|
-
if (resolved) {
|
|
41
|
-
await resolved.middleware(ctx, next);
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
await next();
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
39
|
this.routes = [];
|
|
48
40
|
}
|
|
49
41
|
get(pathname, ...middleware) {
|
|
@@ -74,6 +66,15 @@ export class Router {
|
|
|
74
66
|
const opts = this.options.pathToRegExpOptions;
|
|
75
67
|
return this.compose([Path(pathname, opts)], ...middleware);
|
|
76
68
|
}
|
|
69
|
+
middleware = async (ctx, next) => {
|
|
70
|
+
const resolved = this.resolve(ctx);
|
|
71
|
+
if (resolved) {
|
|
72
|
+
await resolved.middleware(ctx, next);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
await next();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
77
78
|
compose(conditions, ...middleware) {
|
|
78
79
|
this.routes.push({
|
|
79
80
|
conditions,
|
package/dist/validate.d.ts
CHANGED
|
@@ -6,8 +6,8 @@ export interface RequestSchemas {
|
|
|
6
6
|
params?: Schema;
|
|
7
7
|
search?: Schema;
|
|
8
8
|
}
|
|
9
|
-
export
|
|
10
|
-
export
|
|
9
|
+
export type RequestPart = keyof RequestSchemas;
|
|
10
|
+
export type RequestParser = (data: URLSearchParams | FormData) => any;
|
|
11
11
|
declare function middlewareFactory(schemas: RequestSchemas, parser?: RequestParser, lookup?: any): Middleware;
|
|
12
12
|
export declare const messages: Record<RequestPart, string>;
|
|
13
13
|
export interface RequestValidationErrorBody {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfworker/web",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Web framework for Cloudflare Workers and service workers, inspired by Koa and fastify",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"middleware",
|
|
@@ -31,29 +31,29 @@
|
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsc --build",
|
|
33
33
|
"clean": "tsc --build --clean",
|
|
34
|
-
"
|
|
34
|
+
"pretest": "esbuild test/test.ts --target=esnext --bundle --format=esm --conditions=worker,browser --outdir=dist-test --ignore-annotations",
|
|
35
|
+
"test": "node ../../test.mjs"
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
|
-
"@cfworker/json-schema": "^
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"@types/
|
|
41
|
-
"
|
|
42
|
-
"@types/statuses": "^2.0.0",
|
|
43
|
-
"cookie": "^0.5.0",
|
|
38
|
+
"@cfworker/json-schema": "^2.0.0",
|
|
39
|
+
"@types/cookie": "^0.6.0",
|
|
40
|
+
"@types/html-escaper": "^3.0.2",
|
|
41
|
+
"@types/statuses": "^2.0.5",
|
|
42
|
+
"cookie": "^0.6.0",
|
|
44
43
|
"html-escaper": "^3.0.3",
|
|
45
44
|
"negotiator": "^0.6.3",
|
|
46
|
-
"path-to-regexp": "^
|
|
47
|
-
"secure-json-parse": "^2.
|
|
45
|
+
"path-to-regexp": "^7.1.0",
|
|
46
|
+
"secure-json-parse": "^2.7.0",
|
|
48
47
|
"statuses": "^2.0.1"
|
|
49
48
|
},
|
|
50
49
|
"devDependencies": {
|
|
51
|
-
"@
|
|
52
|
-
"@types/
|
|
53
|
-
"
|
|
54
|
-
"
|
|
50
|
+
"@types/chai": "^4.3.17",
|
|
51
|
+
"@types/mocha": "^10.0.7",
|
|
52
|
+
"chai": "^5.1.1",
|
|
53
|
+
"esbuild": "^0.23.1",
|
|
55
54
|
"jsonpointer": "^5.0.1",
|
|
56
|
-
"mocha": "^10.
|
|
57
|
-
"typescript": "^
|
|
55
|
+
"mocha": "^10.7.3",
|
|
56
|
+
"typescript": "^5.5.4",
|
|
57
|
+
"wrangler": "^3.72.0"
|
|
58
58
|
}
|
|
59
59
|
}
|
package/src/application.ts
CHANGED
|
@@ -35,15 +35,6 @@ export class Application {
|
|
|
35
35
|
);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
public listen() {
|
|
39
|
-
const middleware = this.composedMiddleware;
|
|
40
|
-
addEventListener('fetch', event =>
|
|
41
|
-
event.respondWith(
|
|
42
|
-
this.invokeMiddleware(Context.fromFetchEvent(event), middleware)
|
|
43
|
-
)
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
38
|
private async invokeMiddleware(context: Context, middleware: Middleware) {
|
|
48
39
|
try {
|
|
49
40
|
await middleware(context, () => resolved);
|
package/src/context.ts
CHANGED
|
@@ -8,12 +8,6 @@ export class Context {
|
|
|
8
8
|
public readonly cookies: Cookies;
|
|
9
9
|
public readonly state: any;
|
|
10
10
|
|
|
11
|
-
static fromFetchEvent(event: FetchEvent) {
|
|
12
|
-
return new Context(event.request, globalThis as any, {
|
|
13
|
-
waitUntil: event.waitUntil.bind(event)
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
11
|
constructor(
|
|
18
12
|
request: Request,
|
|
19
13
|
public readonly environmentBindings: any,
|
package/src/declarations.d.ts
CHANGED
package/src/router.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Key,
|
|
3
|
-
ParseOptions,
|
|
4
|
-
pathToRegexp,
|
|
5
|
-
TokensToRegexpOptions
|
|
6
|
-
} from 'path-to-regexp';
|
|
1
|
+
import { Key, pathToRegexp, PathToRegexpOptions } from 'path-to-regexp';
|
|
7
2
|
import { Context } from './context.js';
|
|
8
3
|
import { composeMiddleware, Middleware } from './middleware.js';
|
|
9
4
|
|
|
10
|
-
export type PathToRegExpOptions = TokensToRegexpOptions & ParseOptions;
|
|
11
|
-
|
|
12
5
|
export const Method = (method: string) => {
|
|
13
6
|
method = method.toUpperCase();
|
|
14
7
|
return ({ req }: Context) => req.method === method;
|
|
@@ -28,16 +21,15 @@ export const Header = (header: string, value: string) => {
|
|
|
28
21
|
export const Host = (host: string) => Header('host', host);
|
|
29
22
|
export const Referer = (host: string) => Header('referer', host);
|
|
30
23
|
|
|
31
|
-
export const Path = (pattern: string, options?:
|
|
32
|
-
const
|
|
33
|
-
const regExp = pathToRegexp(pattern, keys, options);
|
|
24
|
+
export const Path = (pattern: string, options?: PathToRegexpOptions) => {
|
|
25
|
+
const regExp = pathToRegexp(pattern, options);
|
|
34
26
|
|
|
35
27
|
return ({ req: { url, params } }: Context) => {
|
|
36
28
|
const match = url.pathname.match(regExp);
|
|
37
29
|
if (!match) {
|
|
38
30
|
return false;
|
|
39
31
|
}
|
|
40
|
-
collectParameters(keys, match, params);
|
|
32
|
+
collectParameters(regExp.keys, match, params);
|
|
41
33
|
return true;
|
|
42
34
|
};
|
|
43
35
|
};
|
|
@@ -50,7 +42,7 @@ export interface Route {
|
|
|
50
42
|
}
|
|
51
43
|
|
|
52
44
|
export interface RouterOptions {
|
|
53
|
-
pathToRegExpOptions?:
|
|
45
|
+
pathToRegExpOptions?: PathToRegexpOptions;
|
|
54
46
|
}
|
|
55
47
|
|
|
56
48
|
export const defaultRouterOptions: RouterOptions = {
|
package/tsconfig.json
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../../tsconfig-base",
|
|
3
3
|
"compilerOptions": {
|
|
4
|
-
"target": "
|
|
4
|
+
"target": "ESNext",
|
|
5
5
|
"lib": ["ESNext", "WebWorker", "Webworker.Iterable"],
|
|
6
|
-
"types": [
|
|
7
|
-
"chai",
|
|
8
|
-
"mocha",
|
|
9
|
-
"@cfworker/worker-types",
|
|
10
|
-
"@cloudflare/workers-types"
|
|
11
|
-
],
|
|
12
6
|
"outDir": "dist",
|
|
13
7
|
"rootDir": "./src"
|
|
14
8
|
},
|