@fastly/expressly 1.0.0--canary.4.2464710183.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.
Files changed (81) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +16 -0
  2. package/.github/workflows/release.yml +85 -0
  3. package/.husky/pre-commit +4 -0
  4. package/.nvmrc +1 -0
  5. package/LICENSE +21 -0
  6. package/README.md +56 -0
  7. package/SECURITY.MD +9 -0
  8. package/dist/index.d.ts +4 -0
  9. package/dist/index.js +5 -0
  10. package/dist/lib/routing/common.d.ts +11 -0
  11. package/dist/lib/routing/common.js +36 -0
  12. package/dist/lib/routing/error-middleware.d.ts +10 -0
  13. package/dist/lib/routing/error-middleware.js +12 -0
  14. package/dist/lib/routing/errors.d.ts +9 -0
  15. package/dist/lib/routing/errors.js +16 -0
  16. package/dist/lib/routing/request/cookie-map.d.ts +9 -0
  17. package/dist/lib/routing/request/cookie-map.js +37 -0
  18. package/dist/lib/routing/request/index.d.ts +27 -0
  19. package/dist/lib/routing/request/index.js +47 -0
  20. package/dist/lib/routing/request-handler.d.ts +10 -0
  21. package/dist/lib/routing/request-handler.js +12 -0
  22. package/dist/lib/routing/response/index.d.ts +24 -0
  23. package/dist/lib/routing/response/index.js +101 -0
  24. package/dist/lib/routing/response/status-codes.d.ts +56 -0
  25. package/dist/lib/routing/response/status-codes.js +57 -0
  26. package/dist/lib/routing/response/surrogate-keys.d.ts +9 -0
  27. package/dist/lib/routing/response/surrogate-keys.js +29 -0
  28. package/dist/lib/routing/router.d.ts +32 -0
  29. package/dist/lib/routing/router.js +165 -0
  30. package/docs/.nvmrc +1 -0
  31. package/docs/README.md +27 -0
  32. package/docs/babel.config.js +3 -0
  33. package/docs/docs/config.md +52 -0
  34. package/docs/docs/handling-data/_category_.json +4 -0
  35. package/docs/docs/handling-data/cookies.md +103 -0
  36. package/docs/docs/handling-data/headers.md +110 -0
  37. package/docs/docs/handling-data/request.md +82 -0
  38. package/docs/docs/handling-data/response.md +162 -0
  39. package/docs/docs/handling-data/search-params.md +45 -0
  40. package/docs/docs/intro.md +61 -0
  41. package/docs/docs/middleware/_category_.json +5 -0
  42. package/docs/docs/middleware/controlling-flow.md +41 -0
  43. package/docs/docs/middleware/error-middleware.md +41 -0
  44. package/docs/docs/middleware/what-is-middleware.md +31 -0
  45. package/docs/docs/origin-data/_category_.json +4 -0
  46. package/docs/docs/origin-data/working-with-origins.md +49 -0
  47. package/docs/docs/routing.md +163 -0
  48. package/docs/docusaurus.config.js +119 -0
  49. package/docs/package.json +48 -0
  50. package/docs/sidebars.js +31 -0
  51. package/docs/src/components/HomepageFeatures.module.css +11 -0
  52. package/docs/src/components/HomepageFeatures.tsx +62 -0
  53. package/docs/src/css/custom.css +45 -0
  54. package/docs/src/pages/index.module.css +35 -0
  55. package/docs/src/pages/index.tsx +109 -0
  56. package/docs/static/.nojekyll +0 -0
  57. package/docs/static/img/favicon.ico +0 -0
  58. package/docs/static/img/logo-black.png +0 -0
  59. package/docs/static/img/logo-transparent.png +0 -0
  60. package/docs/static/img/logo.png +0 -0
  61. package/docs/static-host/Cargo.lock +435 -0
  62. package/docs/static-host/Cargo.toml +16 -0
  63. package/docs/static-host/fastly.toml +9 -0
  64. package/docs/static-host/rust-toolchain +3 -0
  65. package/docs/static-host/src/main.rs +115 -0
  66. package/docs/tsconfig.json +7 -0
  67. package/docs/yarn.lock +8416 -0
  68. package/package.json +56 -0
  69. package/src/index.ts +11 -0
  70. package/src/lib/routing/common.ts +34 -0
  71. package/src/lib/routing/error-middleware.ts +23 -0
  72. package/src/lib/routing/errors.ts +18 -0
  73. package/src/lib/routing/index.d.ts +18 -0
  74. package/src/lib/routing/request/cookie-map.ts +42 -0
  75. package/src/lib/routing/request/index.ts +64 -0
  76. package/src/lib/routing/request-handler.ts +22 -0
  77. package/src/lib/routing/response/index.ts +113 -0
  78. package/src/lib/routing/response/status-codes.ts +57 -0
  79. package/src/lib/routing/response/surrogate-keys.ts +32 -0
  80. package/src/lib/routing/router.ts +191 -0
  81. package/tsconfig.json +20 -0
@@ -0,0 +1,16 @@
1
+ ---
2
+ name: Bug report
3
+ about: Tell us about a bug to help us improve
4
+ title: ''
5
+ labels: bug
6
+ assignees: doramatadora
7
+
8
+ ---
9
+
10
+ **Version**
11
+
12
+ Please provide the version of `expressly` that you're using.
13
+
14
+ **What happened**
15
+
16
+ Please describe what you did, what you expected to happen, and what happened instead.
@@ -0,0 +1,85 @@
1
+ name: CI
2
+
3
+ on:
4
+ - workflow_dispatch
5
+ - pull_request
6
+ - push
7
+
8
+ concurrency:
9
+ group: ${{ github.ref_name }}
10
+ cancel-in-progress: true
11
+
12
+ jobs:
13
+ build:
14
+ name: expressly
15
+ runs-on: ubuntu-latest
16
+ env:
17
+ GH_TOKEN: ${{ secrets.AUTO_GH_TOKEN }}
18
+ NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
19
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
20
+ steps:
21
+ - name: Checkout code
22
+ uses: actions/checkout@v2
23
+ - uses: actions/setup-node@v2
24
+ with:
25
+ node-version: 16
26
+ cache: yarn
27
+ - name: Install dependencies
28
+ run: yarn install --frozen-lockfile --prefer-offline
29
+ - name: Node modules cache
30
+ id: node-modules-cache
31
+ uses: actions/cache@v2
32
+ with:
33
+ path: node_modules
34
+ key: ${{ runner.os }}-expressly-${{ secrets.cache_key_epoch_time }}-${{ hashFiles('yarn.lock') }}
35
+ - name: Compile expressly
36
+ run: yarn build
37
+ - name: Publish
38
+ if: ${{ github.event_name == 'pull_request' }}
39
+ run: |
40
+ git fetch origin 'refs/tags/*:refs/tags/*'
41
+ yarn auto shipit
42
+
43
+ build-docs:
44
+ name: Docs
45
+ runs-on: ubuntu-latest
46
+ defaults:
47
+ run:
48
+ working-directory: ./docs
49
+ steps:
50
+ - name: Checkout code
51
+ uses: actions/checkout@v2
52
+ - uses: actions/setup-node@v2
53
+ with:
54
+ node-version: 16
55
+ cache: yarn
56
+ - name: Install dependencies
57
+ run: yarn install --frozen-lockfile --prefer-offline
58
+ - name: Node modules cache
59
+ id: node-modules-cache
60
+ uses: actions/cache@v2
61
+ with:
62
+ path: node_modules
63
+ key: ${{ runner.os }}-expressly-docs-${{ secrets.cache_key_epoch_time }}-${{ hashFiles('yarn.lock') }}
64
+ - name: Compile docs
65
+ run: yarn build
66
+ - name: Install Rust toolchain
67
+ if: ${{ github.ref_name == 'main' }}
68
+ uses: actions-rs/toolchain@v1
69
+ with:
70
+ toolchain: stable
71
+ target: wasm32-wasi
72
+ - name: Deploy docs to Compute@Edge
73
+ if: ${{ github.ref_name == 'main' }}
74
+ uses: fastly/compute-actions@main
75
+ with:
76
+ project_directory: ./docs/static-host
77
+ env:
78
+ FASTLY_API_TOKEN: ${{ secrets.FASTLY_API_TOKEN }}
79
+ - name: Purge expressly.edgecompute.app
80
+ if: ${{ github.ref_name == 'main' }}
81
+ env:
82
+ FASTLY_API_TOKEN: ${{ secrets.FASTLY_API_TOKEN }}
83
+ run: |
84
+ curl -s -H "Fastly-Key: $FASTLY_API_TOKEN" -X POST "https://api.fastly.com/service/44O0OSuWrOWkzVek5Gg4QN/purge_all"
85
+
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
4
+ yarn pretty-quick --staged
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 16
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Fastly
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.
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # expressly
2
+ Express-style router for Fastly's Compute@Edge.
3
+
4
+ ## Using expressly
5
+
6
+ [Check out the docs](https://expressly.edgecompute.app/).
7
+
8
+ ---
9
+
10
+ First, head over to [developer.fastly.com](https://developer.fastly.com) to get started with JavaScript on Fastly's Compute@Edge:
11
+
12
+ 1. Learn about Compute@Edge: [developer.fastly.com/learning/compute/](https://developer.fastly.com/learning/compute/)
13
+ 2. Create your first JavaScript app: [developer.fastly.com/learning/compute/javascript/](https://developer.fastly.com/learning/compute/javascript/)
14
+
15
+ ### Install expressly
16
+
17
+ Install expressly from the [npm registry](https://www.npmjs.com/package/@fastly/expressly):
18
+
19
+ ```shell
20
+ npm i @fastly/expressly
21
+ ```
22
+
23
+ ```shell
24
+ yarn add @fastly/expressly
25
+ ```
26
+
27
+ ### Your first expressly app
28
+
29
+ Replace the contents of your Compute@Edge app's `src/index.js` with the following:
30
+
31
+ ```javascript
32
+ import { Router } from "@fastly/expressly";
33
+
34
+ const router = new Router();
35
+
36
+ router.get("/", async (req, res) => {
37
+ return res.send("Hello world!");
38
+ });
39
+
40
+ router.listen();
41
+ ```
42
+
43
+ ### Try it out
44
+
45
+ Start your app locally:
46
+
47
+ ```shell
48
+ fastly compute serve
49
+ ```
50
+
51
+ This will start your service on [http://localhost:7676](http://localhost:7676).
52
+
53
+ ## Examples
54
+
55
+ Check out the JavaScript code examples for Compute@Edge on Fastly's [Developer Hub](https://developer.fastly.com/solutions/examples/javascript/).
56
+ ß
package/SECURITY.MD ADDED
@@ -0,0 +1,9 @@
1
+ ## Report a security issue
2
+
3
+ The project team welcomes security reports and is committed to providing prompt attention to security issues. Security issues should be reported privately via [Fastly’s security issue reporting process](https://www.fastly.com/security/report-security-issue).
4
+
5
+ ## Security advisories
6
+
7
+ Remediation of security vulnerabilities is prioritized by the project team. The project team endeavors to coordinate remediation with third-party stakeholders, and is committed to transparency in the disclosure process. The team announces security issues via the [expressly security advisories](https://github.com/fastly/expressly/security/advisories) page on a best-effort basis.
8
+
9
+ Note that communications related to security issues in Fastly-maintained OSS as described here are distinct from [Fastly Security Advisories](https://www.fastly.com/security-advisories).
@@ -0,0 +1,4 @@
1
+ import { Router } from "./lib/routing/router";
2
+ import { ERequest } from "./lib/routing/request";
3
+ import { EResponse } from "./lib/routing/response";
4
+ export { Router, ERequest, EResponse, };
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ /// <reference types="@fastly/js-compute" />
2
+ import { Router } from "./lib/routing/router";
3
+ import { ERequest } from "./lib/routing/request";
4
+ import { EResponse } from "./lib/routing/response";
5
+ export { Router, ERequest, EResponse, };
@@ -0,0 +1,11 @@
1
+ /// <reference types="@fastly/js-compute" />
2
+ export declare class ECommonObject {
3
+ headers: Headers;
4
+ set(headerNameOrObject: string | {
5
+ [key: string]: string;
6
+ }, value?: string): void;
7
+ private appendHeader;
8
+ append(headerNameOrObject: string | {
9
+ [key: string]: string | string[];
10
+ }, value?: string | string[]): void;
11
+ }
@@ -0,0 +1,36 @@
1
+ export class ECommonObject {
2
+ constructor() {
3
+ this.headers = new Headers();
4
+ }
5
+ // Header helpers.
6
+ set(headerNameOrObject, value) {
7
+ if (typeof headerNameOrObject === "string") {
8
+ this.headers.set(headerNameOrObject, value);
9
+ }
10
+ else {
11
+ Object.keys(headerNameOrObject).forEach((headerName) => {
12
+ this.headers.set(headerName, headerNameOrObject[headerName]);
13
+ });
14
+ }
15
+ }
16
+ appendHeader(headerName, headerValue) {
17
+ if (typeof headerValue === "string") {
18
+ this.headers.append(headerName, headerValue);
19
+ }
20
+ else if (Array.isArray(headerValue)) {
21
+ headerValue.forEach((v) => {
22
+ this.headers.append(headerName, v);
23
+ });
24
+ }
25
+ }
26
+ append(headerNameOrObject, value) {
27
+ if (typeof headerNameOrObject === "string") {
28
+ this.appendHeader(headerNameOrObject, value);
29
+ }
30
+ else {
31
+ Object.keys(headerNameOrObject).forEach((headerName) => {
32
+ this.appendHeader(headerName, headerNameOrObject[headerName]);
33
+ });
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,10 @@
1
+ import { ERequest } from "./request";
2
+ import { EResponse } from "./response";
3
+ export declare type ErrorMiddlewareCallback = (err: Error, req: ERequest, res: EResponse) => Promise<any>;
4
+ export declare class ErrorMiddleware {
5
+ private matchFn;
6
+ private callback;
7
+ constructor(matchFn: Function, callback: ErrorMiddlewareCallback);
8
+ check(event: ERequest): 0 | 404 | string[];
9
+ run(err: Error, req: ERequest, res: EResponse): Promise<any>;
10
+ }
@@ -0,0 +1,12 @@
1
+ export class ErrorMiddleware {
2
+ constructor(matchFn, callback) {
3
+ this.matchFn = matchFn;
4
+ this.callback = callback;
5
+ }
6
+ check(event) {
7
+ return this.matchFn(event);
8
+ }
9
+ async run(err, req, res) {
10
+ await this.callback(err, req, res);
11
+ }
12
+ }
@@ -0,0 +1,9 @@
1
+ export declare class ErrorNotFound extends Error {
2
+ status: number;
3
+ constructor();
4
+ }
5
+ export declare class ErrorMethodNotAllowed extends Error {
6
+ status: number;
7
+ allow: string;
8
+ constructor(allowedMethods: string[]);
9
+ }
@@ -0,0 +1,16 @@
1
+ export class ErrorNotFound extends Error {
2
+ constructor() {
3
+ super("Not Found");
4
+ this.status = 404;
5
+ Object.setPrototypeOf(this, ErrorNotFound.prototype);
6
+ }
7
+ }
8
+ export class ErrorMethodNotAllowed extends Error {
9
+ constructor(allowedMethods) {
10
+ super("Method Not Allowed");
11
+ this.status = 405;
12
+ this.allow = "";
13
+ Object.setPrototypeOf(this, ErrorMethodNotAllowed.prototype);
14
+ this.allow = [...new Set(allowedMethods)].join(",");
15
+ }
16
+ }
@@ -0,0 +1,9 @@
1
+ /// <reference types="@fastly/js-compute" />
2
+ export declare class CookieMap extends Map {
3
+ private headers;
4
+ constructor(headers: Headers);
5
+ clear(): void;
6
+ set(key: string, value: string): this;
7
+ delete(key: string): boolean;
8
+ private serialize;
9
+ }
@@ -0,0 +1,37 @@
1
+ import cookie from "cookie";
2
+ export class CookieMap extends Map {
3
+ constructor(headers) {
4
+ super();
5
+ this.headers = headers;
6
+ if (Boolean(this.headers.get("Cookie"))) {
7
+ for (const [key, value] of Object.entries(cookie.parse(this.headers.get("Cookie")))) {
8
+ if (typeof value === "string") {
9
+ super.set(key, value);
10
+ }
11
+ }
12
+ }
13
+ }
14
+ clear() {
15
+ this.headers.delete("Cookie");
16
+ super.clear();
17
+ }
18
+ set(key, value) {
19
+ super.set(key, value);
20
+ this.serialize();
21
+ return this;
22
+ }
23
+ delete(key) {
24
+ const deleteResult = super.delete(key);
25
+ this.serialize();
26
+ return deleteResult;
27
+ }
28
+ serialize() {
29
+ if (this.size) {
30
+ const cookies = [];
31
+ for (const [key, value] of this.entries()) {
32
+ cookies.push(cookie.serialize(key, value));
33
+ }
34
+ this.headers.set("Cookie", cookies.join("; "));
35
+ }
36
+ }
37
+ }
@@ -0,0 +1,27 @@
1
+ /// <reference types="@fastly/js-compute" />
2
+ import { ECommonObject } from "../common";
3
+ import { CookieMap } from "./cookie-map";
4
+ import { EConfig } from "..";
5
+ export declare class ERequest extends ECommonObject {
6
+ private config;
7
+ private event;
8
+ readonly clientInfo: ClientInfo;
9
+ readonly method: string;
10
+ headers: Headers;
11
+ url: URL;
12
+ query: URLSearchParams;
13
+ params: {
14
+ [key: string]: string;
15
+ };
16
+ cookies: CookieMap;
17
+ constructor(config: EConfig, event: FetchEvent);
18
+ get path(): string;
19
+ get ip(): string;
20
+ get protocol(): string;
21
+ get secure(): boolean;
22
+ get subdomains(): Array<string>;
23
+ get hostname(): string;
24
+ json(): Promise<any>;
25
+ text(): Promise<string>;
26
+ arrayBuffer(): Promise<ArrayBuffer>;
27
+ }
@@ -0,0 +1,47 @@
1
+ import { ECommonObject } from "../common";
2
+ import { CookieMap } from "./cookie-map";
3
+ export class ERequest extends ECommonObject {
4
+ constructor(config, event) {
5
+ super();
6
+ this.config = config;
7
+ this.event = event;
8
+ this.params = {};
9
+ this.clientInfo = event.client;
10
+ this.method = event.request.method;
11
+ this.url = new URL(event.request.url);
12
+ this.query = this.url.searchParams;
13
+ this.headers = event.request.headers;
14
+ // Parse cookies.
15
+ if (this.config.parseCookie) {
16
+ this.cookies = new CookieMap(this.headers);
17
+ }
18
+ }
19
+ // Express-like URL helpers.
20
+ get path() {
21
+ return this.url.pathname;
22
+ }
23
+ get ip() {
24
+ return this.clientInfo.address;
25
+ }
26
+ get protocol() {
27
+ return this.url.protocol;
28
+ }
29
+ get secure() {
30
+ return this.url.protocol === "https";
31
+ }
32
+ get subdomains() {
33
+ return this.url.hostname.split(".").slice(0, -2);
34
+ }
35
+ get hostname() {
36
+ return this.url.hostname;
37
+ }
38
+ async json() {
39
+ return await this.event.request.json();
40
+ }
41
+ async text() {
42
+ return await this.event.request.text();
43
+ }
44
+ async arrayBuffer() {
45
+ return await this.event.request.arrayBuffer();
46
+ }
47
+ }
@@ -0,0 +1,10 @@
1
+ import { ERequest } from "./request";
2
+ import { EResponse } from "./response";
3
+ export declare type RequestHandlerCallback = (req: ERequest, res: EResponse) => Promise<any>;
4
+ export declare class RequestHandler {
5
+ private matchFn;
6
+ private callback;
7
+ constructor(matchFn: Function, callback: RequestHandlerCallback);
8
+ check(event: ERequest): 0 | 404 | string[];
9
+ run(req: ERequest, res: EResponse): Promise<any>;
10
+ }
@@ -0,0 +1,12 @@
1
+ export class RequestHandler {
2
+ constructor(matchFn, callback) {
3
+ this.matchFn = matchFn;
4
+ this.callback = callback;
5
+ }
6
+ check(event) {
7
+ return this.matchFn(event);
8
+ }
9
+ async run(req, res) {
10
+ await this.callback(req, res);
11
+ }
12
+ }
@@ -0,0 +1,24 @@
1
+ /// <reference types="@fastly/js-compute" />
2
+ import { ECommonObject } from "../common";
3
+ import { SurrogateKeys } from "./surrogate-keys";
4
+ import { CookieOptions, EConfig } from "..";
5
+ export declare class EResponse extends ECommonObject {
6
+ private config;
7
+ headers: Headers;
8
+ status: number;
9
+ body: BodyInit;
10
+ hasEnded: boolean;
11
+ surrogateKeys: SurrogateKeys;
12
+ constructor(config: EConfig);
13
+ vary(field: string): void;
14
+ cookie(key: string, value: string, options?: CookieOptions): void;
15
+ clearCookie(key: string, options?: CookieOptions): void;
16
+ send(response: BodyInit | Response): void;
17
+ end(body?: string): void;
18
+ sendStatus(status: number): void;
19
+ redirect(url: string, status?: 301 | 302 | 307 | 308): void;
20
+ withStatus(status: number): this;
21
+ json(data: any): void;
22
+ text(data: string): void;
23
+ html(data: string, charset?: string): void;
24
+ }
@@ -0,0 +1,101 @@
1
+ import cookie from "cookie";
2
+ import { ECommonObject } from "../common";
3
+ import { statusText } from "./status-codes";
4
+ import { SurrogateKeys } from "./surrogate-keys";
5
+ export class EResponse extends ECommonObject {
6
+ constructor(config) {
7
+ super();
8
+ this.config = config;
9
+ this.headers = new Headers();
10
+ this.status = 0;
11
+ this.body = null;
12
+ this.hasEnded = false;
13
+ this.surrogateKeys = new SurrogateKeys(this.headers);
14
+ }
15
+ // Header helpers.
16
+ vary(field) {
17
+ this.headers.append("Vary", field);
18
+ }
19
+ // Cookie helpers.
20
+ cookie(key, value, options = {}) {
21
+ if (this.hasEnded)
22
+ return;
23
+ this.headers.append("Set-Cookie", cookie.serialize(key, value, options));
24
+ }
25
+ clearCookie(key, options = {}) {
26
+ if (this.hasEnded)
27
+ return;
28
+ this.headers.append("Set-Cookie", cookie.serialize(key, "", { ...options, expires: "Thu, 01 Jan 1970 00:00:00 GMT" }));
29
+ }
30
+ // Response lifecycle methods.
31
+ send(response) {
32
+ if (this.hasEnded)
33
+ return;
34
+ if (response instanceof Response) {
35
+ this.body = response.body;
36
+ this.headers = response.headers;
37
+ this.status = response.status;
38
+ }
39
+ else {
40
+ this.body = response;
41
+ }
42
+ // EXPERIMENTAL: Content type inference, à la Express.js.
43
+ if (this.config.autoContentType && !this.headers.has("Content-Type") && Boolean(this.body)) {
44
+ if (typeof this.body === "string") {
45
+ this.headers.set("Content-Type", "text/html");
46
+ }
47
+ else if (this.body instanceof ArrayBuffer) {
48
+ this.headers.set("Content-Type", "application/octet-stream");
49
+ }
50
+ else {
51
+ this.headers.set("Content-Type", "application/json");
52
+ }
53
+ }
54
+ this.hasEnded = true;
55
+ }
56
+ // End the response process [without any data], à la Express.js.
57
+ // https://stackoverflow.com/questions/29555290/what-is-the-difference-between-res-end-and-res-send
58
+ end(body) {
59
+ if (this.hasEnded)
60
+ return;
61
+ this.send(body);
62
+ }
63
+ // Send a HTTP status code response and end the response process.
64
+ sendStatus(status) {
65
+ if (this.hasEnded)
66
+ return;
67
+ this.status = status;
68
+ this.end(statusText[status] || null);
69
+ }
70
+ // Perform a redirect (default: 302 Found).
71
+ redirect(url, status = 302) {
72
+ if (this.hasEnded)
73
+ return;
74
+ this.headers.set("Location", url);
75
+ this.sendStatus(status);
76
+ }
77
+ // Chainable response status setter.
78
+ withStatus(status) {
79
+ this.status = status;
80
+ return this;
81
+ }
82
+ // Response body helpers.
83
+ json(data) {
84
+ if (this.hasEnded)
85
+ return;
86
+ this.headers.set("Content-Type", "application/json");
87
+ this.send(JSON.stringify(data));
88
+ }
89
+ text(data) {
90
+ if (this.hasEnded)
91
+ return;
92
+ this.headers.set("Content-Type", "text/plain");
93
+ this.send(data);
94
+ }
95
+ html(data, charset) {
96
+ if (this.hasEnded)
97
+ return;
98
+ this.headers.set("Content-Type", `text/html${charset ? `; charset=${charset}` : ""}`);
99
+ this.send(data);
100
+ }
101
+ }
@@ -0,0 +1,56 @@
1
+ export declare const statusText: {
2
+ 100: string;
3
+ 101: string;
4
+ 103: string;
5
+ 200: string;
6
+ 201: string;
7
+ 202: string;
8
+ 203: string;
9
+ 204: string;
10
+ 205: string;
11
+ 206: string;
12
+ 300: string;
13
+ 301: string;
14
+ 302: string;
15
+ 303: string;
16
+ 304: string;
17
+ 307: string;
18
+ 308: string;
19
+ 400: string;
20
+ 401: string;
21
+ 402: string;
22
+ 403: string;
23
+ 404: string;
24
+ 405: string;
25
+ 406: string;
26
+ 407: string;
27
+ 408: string;
28
+ 409: string;
29
+ 410: string;
30
+ 411: string;
31
+ 412: string;
32
+ 413: string;
33
+ 414: string;
34
+ 415: string;
35
+ 416: string;
36
+ 417: string;
37
+ 418: string;
38
+ 422: string;
39
+ 425: string;
40
+ 426: string;
41
+ 428: string;
42
+ 429: string;
43
+ 431: string;
44
+ 451: string;
45
+ 500: string;
46
+ 501: string;
47
+ 502: string;
48
+ 503: string;
49
+ 504: string;
50
+ 505: string;
51
+ 506: string;
52
+ 507: string;
53
+ 508: string;
54
+ 510: string;
55
+ 511: string;
56
+ };