@cfworker/web 1.9.0 → 1.11.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.d.ts +1 -0
- package/dist/accepts.js +4 -5
- package/dist/application.js +5 -3
- package/dist/context.d.ts +1 -0
- package/dist/context.js +0 -7
- package/dist/cookies.d.ts +1 -0
- package/dist/cookies.js +0 -2
- package/dist/http-error.d.ts +1 -0
- package/dist/http-error.js +1 -4
- package/dist/middleware.d.ts +2 -3
- package/dist/middleware.js +28 -2
- package/dist/req.d.ts +2 -1
- package/dist/req.js +1 -13
- package/dist/response-builder.d.ts +1 -0
- package/dist/response-builder.js +9 -9
- package/dist/router.js +9 -11
- package/dist/validate.d.ts +1 -0
- package/package.json +15 -19
- package/src/application.ts +2 -2
- package/src/declarations.d.ts +2 -0
- package/src/http-error.ts +1 -1
- package/src/middleware.ts +31 -3
- package/src/req.ts +1 -1
- package/src/response-builder.ts +1 -4
- package/src/validate.ts +1 -1
- package/tsconfig.json +9 -3
- package/LICENSE.md +0 -21
package/dist/accepts.d.ts
CHANGED
package/dist/accepts.js
CHANGED
|
@@ -7,13 +7,12 @@ 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;
|
|
15
10
|
constructor(headers) {
|
|
16
11
|
this.headers = headers;
|
|
12
|
+
this._type = undefined;
|
|
13
|
+
this._language = undefined;
|
|
14
|
+
this._encoding = undefined;
|
|
15
|
+
this._charset = undefined;
|
|
17
16
|
}
|
|
18
17
|
type(...values) {
|
|
19
18
|
if (!this._type) {
|
package/dist/application.js
CHANGED
|
@@ -4,7 +4,9 @@ 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
|
-
|
|
7
|
+
constructor() {
|
|
8
|
+
this.middleware = [];
|
|
9
|
+
}
|
|
8
10
|
use(middleware) {
|
|
9
11
|
this.middleware.push(middleware);
|
|
10
12
|
return this;
|
|
@@ -26,12 +28,12 @@ export class Application {
|
|
|
26
28
|
return context.res.create();
|
|
27
29
|
}
|
|
28
30
|
catch (err) {
|
|
29
|
-
console.error(err);
|
|
31
|
+
console.error(err?.stack ?? err);
|
|
30
32
|
if (err instanceof HttpError) {
|
|
31
33
|
return err.toResponse();
|
|
32
34
|
}
|
|
33
35
|
const status = 500;
|
|
34
|
-
const statusText = statuses[500];
|
|
36
|
+
const statusText = statuses.message[500];
|
|
35
37
|
const headers = { 'content-type': 'text/plain' };
|
|
36
38
|
return new Response(statusText, { status, statusText, headers });
|
|
37
39
|
}
|
package/dist/context.d.ts
CHANGED
package/dist/context.js
CHANGED
|
@@ -2,13 +2,6 @@ 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
|
-
event;
|
|
6
|
-
req;
|
|
7
|
-
res;
|
|
8
|
-
cookies;
|
|
9
|
-
respondWith;
|
|
10
|
-
responded;
|
|
11
|
-
state;
|
|
12
5
|
constructor(event) {
|
|
13
6
|
this.event = event;
|
|
14
7
|
const request = event.request;
|
package/dist/cookies.d.ts
CHANGED
package/dist/cookies.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
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;
|
|
6
4
|
constructor(requestHeaders, responseHeaders) {
|
|
7
5
|
this.responseHeaders = responseHeaders;
|
|
8
6
|
const cookie = requestHeaders.get('cookie');
|
package/dist/http-error.d.ts
CHANGED
package/dist/http-error.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import statuses from 'statuses';
|
|
2
2
|
export class HttpError extends Error {
|
|
3
|
-
status;
|
|
4
|
-
statusText;
|
|
5
|
-
body;
|
|
6
3
|
constructor(status, body) {
|
|
7
|
-
const statusText = statuses[
|
|
4
|
+
const statusText = statuses.message[status];
|
|
8
5
|
super(statusText);
|
|
9
6
|
this.name = this.constructor.name;
|
|
10
7
|
if (typeof Error.captureStackTrace === 'function') {
|
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
|
@@ -1,13 +1,6 @@
|
|
|
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
|
-
raw;
|
|
5
|
-
method;
|
|
6
|
-
url;
|
|
7
|
-
headers;
|
|
8
|
-
params;
|
|
9
|
-
accepts;
|
|
10
|
-
body;
|
|
11
4
|
constructor(request) {
|
|
12
5
|
this.raw = request;
|
|
13
6
|
this.method = request.method;
|
|
@@ -19,11 +12,6 @@ export class Req {
|
|
|
19
12
|
}
|
|
20
13
|
}
|
|
21
14
|
export class ReqBody {
|
|
22
|
-
request;
|
|
23
|
-
_arrayBuffer;
|
|
24
|
-
_formData;
|
|
25
|
-
_json;
|
|
26
|
-
_text;
|
|
27
15
|
constructor(request) {
|
|
28
16
|
this.request = request;
|
|
29
17
|
}
|
package/dist/response-builder.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import statuses from 'statuses';
|
|
2
2
|
export class ResponseBuilder {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
+
}
|
|
9
11
|
get status() {
|
|
10
12
|
return this._status;
|
|
11
13
|
}
|
|
@@ -17,7 +19,7 @@ export class ResponseBuilder {
|
|
|
17
19
|
}
|
|
18
20
|
}
|
|
19
21
|
get statusText() {
|
|
20
|
-
return statuses[this._status];
|
|
22
|
+
return statuses.message[this._status];
|
|
21
23
|
}
|
|
22
24
|
get body() {
|
|
23
25
|
return this._body;
|
|
@@ -129,5 +131,3 @@ export class ResponseBuilder {
|
|
|
129
131
|
return new Response(body, { status, statusText, headers });
|
|
130
132
|
}
|
|
131
133
|
}
|
|
132
|
-
const Blob = self.Blob || class {
|
|
133
|
-
};
|
package/dist/router.js
CHANGED
|
@@ -33,10 +33,17 @@ export const defaultRouterOptions = {
|
|
|
33
33
|
pathToRegExpOptions: { strict: true }
|
|
34
34
|
};
|
|
35
35
|
export class Router {
|
|
36
|
-
options;
|
|
37
|
-
routes;
|
|
38
36
|
constructor(options = defaultRouterOptions) {
|
|
39
37
|
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
|
+
};
|
|
40
47
|
this.routes = [];
|
|
41
48
|
}
|
|
42
49
|
get(pathname, ...middleware) {
|
|
@@ -67,15 +74,6 @@ export class Router {
|
|
|
67
74
|
const opts = this.options.pathToRegExpOptions;
|
|
68
75
|
return this.compose([Path(pathname, opts)], ...middleware);
|
|
69
76
|
}
|
|
70
|
-
middleware = async (ctx, next) => {
|
|
71
|
-
const resolved = this.resolve(ctx);
|
|
72
|
-
if (resolved) {
|
|
73
|
-
await resolved.middleware(ctx, next);
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
await next();
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
77
|
compose(conditions, ...middleware) {
|
|
80
78
|
this.routes.push({
|
|
81
79
|
conditions,
|
package/dist/validate.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfworker/web",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Web framework for Cloudflare Workers and service workers, inspired by Koa and fastify",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"middleware",
|
|
@@ -34,31 +34,27 @@
|
|
|
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.11.0",
|
|
38
|
+
"@cfworker/worker-types": "^1.11.0",
|
|
39
|
+
"@cloudflare/workers-types": "^3.2.0",
|
|
40
|
+
"@types/cookie": "^0.4.1",
|
|
41
41
|
"@types/html-escaper": "^3.0.0",
|
|
42
|
-
"@types/json-pointer": "^1.0.
|
|
43
|
-
"@types/
|
|
44
|
-
"@types/secure-json-parse": "^1.0.2",
|
|
45
|
-
"@types/statuses": "^1.5.0",
|
|
42
|
+
"@types/json-pointer": "^1.0.31",
|
|
43
|
+
"@types/statuses": "^2.0.0",
|
|
46
44
|
"cookie": "^0.4.1",
|
|
47
45
|
"html-escaper": "^3.0.3",
|
|
48
|
-
"koa-compose": "^4.1.0",
|
|
49
46
|
"negotiator": "^0.6.2",
|
|
50
47
|
"path-to-regexp": "^6.2.0",
|
|
51
|
-
"secure-json-parse": "^2.
|
|
52
|
-
"statuses": "^
|
|
48
|
+
"secure-json-parse": "^2.4.0",
|
|
49
|
+
"statuses": "^2.0.1"
|
|
53
50
|
},
|
|
54
51
|
"devDependencies": {
|
|
55
|
-
"@cfworker/dev": "^1.
|
|
56
|
-
"@types/chai": "^4.2.
|
|
57
|
-
"@types/mocha": "^
|
|
52
|
+
"@cfworker/dev": "^1.11.0",
|
|
53
|
+
"@types/chai": "^4.2.22",
|
|
54
|
+
"@types/mocha": "^9.0.0",
|
|
58
55
|
"chai": "^4.3.4",
|
|
59
56
|
"json-pointer": "^0.6.1",
|
|
60
|
-
"mocha": "^
|
|
61
|
-
"typescript": "^4.
|
|
62
|
-
}
|
|
63
|
-
"gitHead": "d333ff940566a7acc384ab791d8b73bed2a9a211"
|
|
57
|
+
"mocha": "^9.1.3",
|
|
58
|
+
"typescript": "^4.5.2"
|
|
59
|
+
}
|
|
64
60
|
}
|
package/src/application.ts
CHANGED
|
@@ -33,14 +33,14 @@ export class Application {
|
|
|
33
33
|
await middleware(context, () => resolved);
|
|
34
34
|
return context.res.create();
|
|
35
35
|
} catch (err) {
|
|
36
|
-
console.error(err);
|
|
36
|
+
console.error((err as any)?.stack ?? err);
|
|
37
37
|
|
|
38
38
|
if (err instanceof HttpError) {
|
|
39
39
|
return err.toResponse();
|
|
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/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
package/tsconfig.json
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../../tsconfig-base",
|
|
3
3
|
"compilerOptions": {
|
|
4
|
-
"target": "
|
|
5
|
-
"lib": ["esnext"
|
|
6
|
-
"types": [
|
|
4
|
+
"target": "ES2021",
|
|
5
|
+
"lib": ["esnext"],
|
|
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/LICENSE.md
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020 Jeremy Danyow
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|