@cfworker/web 1.10.2 → 1.12.2

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.
@@ -33,7 +33,7 @@ export class Application {
33
33
  return err.toResponse();
34
34
  }
35
35
  const status = 500;
36
- const statusText = statuses[500];
36
+ const statusText = statuses.message[500];
37
37
  const headers = { 'content-type': 'text/plain' };
38
38
  return new Response(statusText, { status, statusText, headers });
39
39
  }
@@ -1,7 +1,7 @@
1
1
  import statuses from 'statuses';
2
2
  export class HttpError extends Error {
3
3
  constructor(status, body) {
4
- const statusText = statuses[statuses(status)];
4
+ const statusText = statuses.message[status];
5
5
  super(statusText);
6
6
  this.name = this.constructor.name;
7
7
  if (typeof Error.captureStackTrace === 'function') {
package/dist/index.d.ts CHANGED
@@ -9,5 +9,4 @@ export * from './normalize-pathname.js';
9
9
  export * from './req.js';
10
10
  export * from './response-builder.js';
11
11
  export * from './router.js';
12
- export * from './to-object.js';
13
12
  export * from './validate.js';
package/dist/index.js CHANGED
@@ -9,5 +9,4 @@ export * from './normalize-pathname.js';
9
9
  export * from './req.js';
10
10
  export * from './response-builder.js';
11
11
  export * from './router.js';
12
- export * from './to-object.js';
13
12
  export * from './validate.js';
@@ -1,4 +1,3 @@
1
- import compose, { Middleware as GenericMiddleware } from 'koa-compose';
2
1
  import { Context } from './context.js';
3
- export declare type Middleware = GenericMiddleware<Context>;
4
- export declare const composeMiddleware: typeof compose;
2
+ export declare type Middleware = (context: Context, next: () => Promise<void>) => Promise<void> | void;
3
+ export declare function composeMiddleware(middleware: Middleware[]): Middleware;
@@ -1,2 +1,28 @@
1
- import compose from 'koa-compose';
2
- export const composeMiddleware = compose;
1
+ export function composeMiddleware(middleware) {
2
+ if (!Array.isArray(middleware))
3
+ throw new TypeError('Middleware stack must be an array!');
4
+ for (const fn of middleware) {
5
+ if (typeof fn !== 'function')
6
+ throw new TypeError('Middleware must be composed of functions!');
7
+ }
8
+ return function (context, next) {
9
+ let index = -1;
10
+ return dispatch(0);
11
+ function dispatch(i) {
12
+ if (i <= index)
13
+ return Promise.reject(new Error('next() called multiple times'));
14
+ index = i;
15
+ let fn = middleware[i];
16
+ if (i === middleware.length)
17
+ fn = next;
18
+ if (!fn)
19
+ return Promise.resolve();
20
+ try {
21
+ return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
22
+ }
23
+ catch (err) {
24
+ return Promise.reject(err);
25
+ }
26
+ }
27
+ };
28
+ }
package/dist/req.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Reviver } from 'secure-json-parse';
1
+ import { Reviver } from 'secure-json-parse/index.js';
2
2
  import { Accepts } from './accepts.js';
3
3
  export declare class Req {
4
4
  readonly raw: Request;
package/dist/req.js CHANGED
@@ -1,4 +1,4 @@
1
- import { safeParse } from 'secure-json-parse';
1
+ import { safeParse } from 'secure-json-parse/index.js';
2
2
  import { Accepts } from './accepts.js';
3
3
  export class Req {
4
4
  constructor(request) {
@@ -19,7 +19,7 @@ export class ResponseBuilder {
19
19
  }
20
20
  }
21
21
  get statusText() {
22
- return statuses[this._status];
22
+ return statuses.message[this._status];
23
23
  }
24
24
  get body() {
25
25
  return this._body;
@@ -131,5 +131,3 @@ export class ResponseBuilder {
131
131
  return new Response(body, { status, statusText, headers });
132
132
  }
133
133
  }
134
- const Blob = self.Blob || class {
135
- };
package/dist/validate.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { dereference, validate as schemaValidate } from '@cfworker/json-schema';
2
2
  import { HttpError } from './http-error.js';
3
- import { toObject } from './to-object.js';
4
3
  const draft = '2019-09';
5
4
  const hasBody = {
6
5
  POST: true,
@@ -8,7 +7,7 @@ const hasBody = {
8
7
  DELETE: true,
9
8
  PATCH: true
10
9
  };
11
- function middlewareFactory(schemas, parser = toObject, lookup = Object.create(null)) {
10
+ function middlewareFactory(schemas, parser = Object.fromEntries, lookup = Object.create(null)) {
12
11
  const { headers: $headers, search: $search, params: $params, body: $body } = schemas;
13
12
  if ($headers) {
14
13
  dereference($headers, { ...lookup });
@@ -27,7 +26,7 @@ function middlewareFactory(schemas, parser = toObject, lookup = Object.create(nu
27
26
  validateRequestPart('params', req.params, $params, lookup);
28
27
  }
29
28
  if ($headers) {
30
- const headers = toObject(req.headers);
29
+ const headers = Object.fromEntries(req.headers);
31
30
  validateRequestPart('headers', headers, $headers, lookup);
32
31
  }
33
32
  if ($search) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cfworker/web",
3
- "version": "1.10.2",
3
+ "version": "1.12.2",
4
4
  "description": "Web framework for Cloudflare Workers and service workers, inspired by Koa and fastify",
5
5
  "keywords": [
6
6
  "middleware",
@@ -34,30 +34,26 @@
34
34
  "test": "cfworker test test/**/*.spec.ts --nocheck"
35
35
  },
36
36
  "dependencies": {
37
- "@cfworker/json-schema": "^1.10.2",
38
- "@cfworker/worker-types": "^1.10.2",
39
- "@cloudflare/workers-types": "^2.1.0",
40
- "@types/cookie": "^0.4.0",
37
+ "@cfworker/json-schema": "^1.12.2",
38
+ "@cfworker/worker-types": "^1.12.1",
39
+ "@cloudflare/workers-types": "^2.2.2",
40
+ "@types/cookie": "^0.4.1",
41
41
  "@types/html-escaper": "^3.0.0",
42
- "@types/json-pointer": "^1.0.30",
43
- "@types/koa-compose": "^3.2.5",
44
- "@types/secure-json-parse": "^1.0.2",
45
- "@types/statuses": "^1.5.0",
46
- "cookie": "^0.4.1",
42
+ "@types/statuses": "^2.0.0",
43
+ "cookie": "^0.4.2",
47
44
  "html-escaper": "^3.0.3",
48
- "koa-compose": "^4.1.0",
49
- "negotiator": "^0.6.2",
45
+ "negotiator": "^0.6.3",
50
46
  "path-to-regexp": "^6.2.0",
51
- "secure-json-parse": "^2.3.2",
52
- "statuses": "^1.5.0"
47
+ "secure-json-parse": "^2.4.0",
48
+ "statuses": "^2.0.1"
53
49
  },
54
50
  "devDependencies": {
55
- "@cfworker/dev": "^1.10.2",
56
- "@types/chai": "^4.2.16",
57
- "@types/mocha": "^8.2.2",
58
- "chai": "^4.3.4",
59
- "json-pointer": "^0.6.1",
60
- "mocha": "^8.3.2",
61
- "typescript": "^4.5.2"
51
+ "@cfworker/dev": "^1.14.0",
52
+ "@types/chai": "^4.3.0",
53
+ "@types/mocha": "^9.1.0",
54
+ "chai": "^4.3.6",
55
+ "jsonpointer": "^5.0.0",
56
+ "mocha": "^9.2.1",
57
+ "typescript": "^4.5.5"
62
58
  }
63
59
  }
@@ -40,7 +40,7 @@ export class Application {
40
40
  }
41
41
 
42
42
  const status = 500;
43
- const statusText = statuses[500];
43
+ const statusText = statuses.message[500];
44
44
  const headers = { 'content-type': 'text/plain' };
45
45
  return new Response(statusText, { status, statusText, headers });
46
46
  }
@@ -0,0 +1,2 @@
1
+ // secure-json-parse's types rely on node's Buffer
2
+ type Buffer = never;
package/src/http-error.ts CHANGED
@@ -6,7 +6,7 @@ export class HttpError extends Error {
6
6
  public readonly body: string | object;
7
7
 
8
8
  constructor(status: number, body: string | object) {
9
- const statusText = statuses[statuses(status)]!;
9
+ const statusText = statuses.message[status]!;
10
10
 
11
11
  super(statusText);
12
12
 
package/src/index.ts CHANGED
@@ -9,5 +9,4 @@ export * from './normalize-pathname.js';
9
9
  export * from './req.js';
10
10
  export * from './response-builder.js';
11
11
  export * from './router.js';
12
- export * from './to-object.js';
13
12
  export * from './validate.js';
package/src/middleware.ts CHANGED
@@ -1,6 +1,34 @@
1
- import compose, { Middleware as GenericMiddleware } from 'koa-compose';
2
1
  import { Context } from './context.js';
3
2
 
4
- export type Middleware = GenericMiddleware<Context>;
3
+ export type Middleware = (
4
+ context: Context,
5
+ next: () => Promise<void>
6
+ ) => Promise<void> | void;
5
7
 
6
- export const composeMiddleware = compose;
8
+ export function composeMiddleware(middleware: Middleware[]): Middleware {
9
+ if (!Array.isArray(middleware))
10
+ throw new TypeError('Middleware stack must be an array!');
11
+ for (const fn of middleware) {
12
+ if (typeof fn !== 'function')
13
+ throw new TypeError('Middleware must be composed of functions!');
14
+ }
15
+
16
+ return function (context, next) {
17
+ // last called middleware #
18
+ let index = -1;
19
+ return dispatch(0);
20
+ function dispatch(i: number): Promise<void> {
21
+ if (i <= index)
22
+ return Promise.reject(new Error('next() called multiple times'));
23
+ index = i;
24
+ let fn = middleware[i];
25
+ if (i === middleware.length) fn = next;
26
+ if (!fn) return Promise.resolve();
27
+ try {
28
+ return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
29
+ } catch (err) {
30
+ return Promise.reject(err);
31
+ }
32
+ }
33
+ };
34
+ }
package/src/req.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Reviver, safeParse } from 'secure-json-parse';
1
+ import { Reviver, safeParse } from 'secure-json-parse/index.js';
2
2
  import { Accepts } from './accepts.js';
3
3
 
4
4
  export class Req {
@@ -28,7 +28,7 @@ export class ResponseBuilder {
28
28
  }
29
29
 
30
30
  get statusText() {
31
- return statuses[this._status]!;
31
+ return statuses.message[this._status]!;
32
32
  }
33
33
 
34
34
  get body() {
@@ -156,6 +156,3 @@ export class ResponseBuilder {
156
156
  return new Response(body, { status, statusText, headers });
157
157
  }
158
158
  }
159
-
160
- // Cloudflare workers do not define the Blob class.
161
- const Blob = self.Blob || ((class {} as any) as Blob);
package/src/validate.ts CHANGED
@@ -6,7 +6,6 @@ import {
6
6
  } from '@cfworker/json-schema';
7
7
  import { HttpError } from './http-error.js';
8
8
  import { Middleware } from './middleware.js';
9
- import { toObject } from './to-object.js';
10
9
 
11
10
  export interface RequestSchemas {
12
11
  body?: Schema;
@@ -30,7 +29,7 @@ const hasBody: Record<string, true> = {
30
29
 
31
30
  function middlewareFactory(
32
31
  schemas: RequestSchemas,
33
- parser: RequestParser = toObject,
32
+ parser: RequestParser = Object.fromEntries,
34
33
  lookup = Object.create(null)
35
34
  ): Middleware {
36
35
  const {
@@ -58,7 +57,7 @@ function middlewareFactory(
58
57
  validateRequestPart('params', req.params, $params, lookup);
59
58
  }
60
59
  if ($headers) {
61
- const headers = toObject(req.headers);
60
+ const headers = Object.fromEntries(req.headers);
62
61
  validateRequestPart('headers', headers, $headers, lookup);
63
62
  }
64
63
  if ($search) {
package/tsconfig.json CHANGED
@@ -2,11 +2,17 @@
2
2
  "extends": "../../tsconfig-base",
3
3
  "compilerOptions": {
4
4
  "target": "ES2021",
5
- "lib": ["esnext", "webworker"],
6
- "types": ["@cfworker/worker-types", "@cloudflare/workers-types"],
5
+ "lib": ["ESNext", "WebWorker", "Webworker.Iterable"],
6
+ "types": [
7
+ "chai",
8
+ "mocha",
9
+ "@cfworker/worker-types",
10
+ "@cloudflare/workers-types"
11
+ ],
7
12
  "outDir": "dist",
8
13
  "rootDir": "./src"
9
14
  },
15
+
10
16
  "include": ["src"],
11
17
  "references": [{ "path": "../json-schema" }]
12
18
  }
@@ -1,3 +0,0 @@
1
- export declare function toObject<TKey extends string | number | symbol, TValue>(iterable: {
2
- [Symbol.iterator](): IterableIterator<[TKey, TValue]>;
3
- }): Record<TKey, TValue>;
package/dist/to-object.js DELETED
@@ -1,7 +0,0 @@
1
- export function toObject(iterable) {
2
- const obj = Object.create(null);
3
- for (const [key, value] of iterable) {
4
- obj[key] = value;
5
- }
6
- return obj;
7
- }
package/src/to-object.ts DELETED
@@ -1,15 +0,0 @@
1
- /**
2
- * Convert an iterable to an object.
3
- */
4
- export function toObject<
5
- TKey extends string | number | symbol,
6
- TValue
7
- >(iterable: {
8
- [Symbol.iterator](): IterableIterator<[TKey, TValue]>;
9
- }): Record<TKey, TValue> {
10
- const obj = Object.create(null) as Record<TKey, TValue>;
11
- for (const [key, value] of iterable) {
12
- obj[key] = value;
13
- }
14
- return obj;
15
- }