@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.
- package/dist/application.js +1 -1
- package/dist/http-error.js +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/middleware.d.ts +2 -3
- package/dist/middleware.js +28 -2
- package/dist/req.d.ts +1 -1
- package/dist/req.js +1 -1
- package/dist/response-builder.js +1 -3
- package/dist/validate.js +2 -3
- package/package.json +17 -21
- package/src/application.ts +1 -1
- package/src/declarations.d.ts +2 -0
- package/src/http-error.ts +1 -1
- package/src/index.ts +0 -1
- package/src/middleware.ts +31 -3
- package/src/req.ts +1 -1
- package/src/response-builder.ts +1 -4
- package/src/validate.ts +2 -3
- package/tsconfig.json +8 -2
- package/dist/to-object.d.ts +0 -3
- package/dist/to-object.js +0 -7
- package/src/to-object.ts +0 -15
package/dist/application.js
CHANGED
|
@@ -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
|
}
|
package/dist/http-error.js
CHANGED
|
@@ -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[
|
|
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
package/dist/index.js
CHANGED
package/dist/middleware.d.ts
CHANGED
|
@@ -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 =
|
|
4
|
-
export declare
|
|
2
|
+
export declare type Middleware = (context: Context, next: () => Promise<void>) => Promise<void> | void;
|
|
3
|
+
export declare function composeMiddleware(middleware: Middleware[]): Middleware;
|
package/dist/middleware.js
CHANGED
|
@@ -1,2 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
package/dist/req.js
CHANGED
package/dist/response-builder.js
CHANGED
|
@@ -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 =
|
|
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 =
|
|
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.
|
|
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.
|
|
38
|
-
"@cfworker/worker-types": "^1.
|
|
39
|
-
"@cloudflare/workers-types": "^2.
|
|
40
|
-
"@types/cookie": "^0.4.
|
|
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/
|
|
43
|
-
"
|
|
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
|
-
"
|
|
49
|
-
"negotiator": "^0.6.2",
|
|
45
|
+
"negotiator": "^0.6.3",
|
|
50
46
|
"path-to-regexp": "^6.2.0",
|
|
51
|
-
"secure-json-parse": "^2.
|
|
52
|
-
"statuses": "^
|
|
47
|
+
"secure-json-parse": "^2.4.0",
|
|
48
|
+
"statuses": "^2.0.1"
|
|
53
49
|
},
|
|
54
50
|
"devDependencies": {
|
|
55
|
-
"@cfworker/dev": "^1.
|
|
56
|
-
"@types/chai": "^4.
|
|
57
|
-
"@types/mocha": "^
|
|
58
|
-
"chai": "^4.3.
|
|
59
|
-
"
|
|
60
|
-
"mocha": "^
|
|
61
|
-
"typescript": "^4.5.
|
|
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
|
}
|
package/src/application.ts
CHANGED
|
@@ -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
|
}
|
package/src/http-error.ts
CHANGED
package/src/index.ts
CHANGED
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 =
|
|
3
|
+
export type Middleware = (
|
|
4
|
+
context: Context,
|
|
5
|
+
next: () => Promise<void>
|
|
6
|
+
) => Promise<void> | void;
|
|
5
7
|
|
|
6
|
-
export
|
|
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
package/src/response-builder.ts
CHANGED
|
@@ -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 =
|
|
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 =
|
|
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": ["
|
|
6
|
-
"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
|
}
|
package/dist/to-object.d.ts
DELETED
package/dist/to-object.js
DELETED
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
|
-
}
|