@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 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) {
@@ -7,6 +7,5 @@ export declare class Application {
7
7
  handleRequest(request: Request, env: any, context: {
8
8
  waitUntil(promise: Promise<any>): void;
9
9
  }): Promise<Response>;
10
- listen(): void;
11
10
  private invokeMiddleware;
12
11
  }
@@ -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
- constructor() {
8
- this.middleware = [];
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');
@@ -1,5 +1,8 @@
1
1
  import statuses from 'statuses';
2
2
  export class HttpError extends Error {
3
+ status;
4
+ statusText;
5
+ body;
3
6
  constructor(status, body) {
4
7
  const statusText = statuses.message[status];
5
8
  super(statusText);
@@ -1,3 +1,3 @@
1
1
  import { Context } from './context.js';
2
- export declare type Middleware = (context: Context, next: () => Promise<void>) => Promise<void> | void;
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
  }
@@ -1,4 +1,4 @@
1
- export declare type ExtendedBodyInit = BodyInit | boolean | Date | number | object | null;
1
+ export type ExtendedBodyInit = BodyInit | boolean | Date | number | object | null;
2
2
  export declare class ResponseBuilder {
3
3
  readonly headers: Headers;
4
4
  private _status;
@@ -1,13 +1,11 @@
1
1
  import statuses from 'statuses';
2
2
  export class ResponseBuilder {
3
- constructor() {
4
- this.headers = new Headers();
5
- this._status = 404;
6
- this._explicitStatus = false;
7
- this._implicitType = false;
8
- this._body = null;
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 { ParseOptions, TokensToRegexpOptions } from 'path-to-regexp';
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?: PathToRegExpOptions) => ({ req: { url, params } }: Context) => boolean;
17
- export declare type RouteCondition = (context: Context) => boolean;
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?: 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 keys = [];
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,
@@ -6,8 +6,8 @@ export interface RequestSchemas {
6
6
  params?: Schema;
7
7
  search?: Schema;
8
8
  }
9
- export declare type RequestPart = keyof RequestSchemas;
10
- export declare type RequestParser = (data: URLSearchParams | FormData) => any;
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": "1.14.0",
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
- "test": "cfworker test test/**/*.spec.ts --nocheck"
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": "^1.12.6",
38
- "@cfworker/worker-types": "^1.12.3",
39
- "@cloudflare/workers-types": "^2.2.2",
40
- "@types/cookie": "^0.5.1",
41
- "@types/html-escaper": "^3.0.0",
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": "^6.2.1",
47
- "secure-json-parse": "^2.5.0",
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
- "@cfworker/dev": "^1.14.2",
52
- "@types/chai": "^4.3.3",
53
- "@types/mocha": "^10.0.0",
54
- "chai": "^4.3.6",
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.0.0",
57
- "typescript": "^4.8.4"
55
+ "mocha": "^10.7.3",
56
+ "typescript": "^5.5.4",
57
+ "wrangler": "^3.72.0"
58
58
  }
59
59
  }
@@ -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,
@@ -4,3 +4,7 @@ type Buffer = never;
4
4
  interface WorkerGlobalScopeEventMap {
5
5
  fetch: FetchEvent;
6
6
  }
7
+
8
+ interface ErrorConstructor {
9
+ captureStackTrace(thisArg: any, func: any): void;
10
+ }
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?: PathToRegExpOptions) => {
32
- const keys: Key[] = [];
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?: 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": "ES2021",
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
  },