hono 0.0.15 → 0.0.16

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.
@@ -0,0 +1,11 @@
1
+ import type { Context } from '../../context';
2
+ declare type CORSOptions = {
3
+ origin: string;
4
+ allowMethods?: string[];
5
+ allowHeaders?: string[];
6
+ maxAge?: number;
7
+ credentials?: boolean;
8
+ exposeHeaders?: string[];
9
+ };
10
+ export declare const cors: (options?: CORSOptions) => (c: Context, next: Function) => Promise<void>;
11
+ export {};
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cors = void 0;
4
+ const cors = (options) => {
5
+ const defaults = {
6
+ origin: '*',
7
+ allowMethods: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH'],
8
+ allowHeaders: [],
9
+ exposeHeaders: [],
10
+ };
11
+ const opts = Object.assign(Object.assign({}, defaults), options);
12
+ return async (c, next) => {
13
+ await next();
14
+ function set(key, value) {
15
+ c.res.headers.append(key, value);
16
+ }
17
+ set('Access-Control-Allow-Origin', opts.origin);
18
+ // Suppose the server sends a response with an Access-Control-Allow-Origin value with an explicit origin (rather than the "*" wildcard).
19
+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
20
+ if (opts.origin !== '*') {
21
+ set('Vary', 'Origin');
22
+ }
23
+ if (opts.credentials) {
24
+ set('Access-Control-Allow-Credentials', 'true');
25
+ }
26
+ if (opts.exposeHeaders.length) {
27
+ set('Access-Control-Expose-Headers', opts.exposeHeaders.join(','));
28
+ }
29
+ if (c.req.method === 'OPTIONS') {
30
+ // Preflight
31
+ if (opts.maxAge != null) {
32
+ set('Access-Control-Max-Age', opts.maxAge.toString());
33
+ }
34
+ if (opts.allowMethods.length) {
35
+ set('Access-Control-Allow-Methods', opts.allowMethods.join(','));
36
+ }
37
+ let headers = opts.allowHeaders;
38
+ if (!headers.length) {
39
+ const requestHeaders = c.req.headers.get('Access-Control-Request-Headers');
40
+ if (requestHeaders) {
41
+ headers = requestHeaders.split(/\s*,\s*/);
42
+ }
43
+ }
44
+ if (headers.length) {
45
+ set('Access-Control-Allow-Headers', headers.join(','));
46
+ set('Vary', 'Access-Control-Request-Headers');
47
+ }
48
+ c.res = new Response(null, {
49
+ headers: c.res.headers,
50
+ status: 204,
51
+ statusText: c.res.statusText,
52
+ });
53
+ }
54
+ };
55
+ };
56
+ exports.cors = cors;
@@ -8,9 +8,21 @@ const defaultMiddleware = async (c, next) => {
8
8
  return url.searchParams.get(key);
9
9
  };
10
10
  await next();
11
+ /*
12
+ TODO:
13
+ Adding Content-Length header make it more slower.
14
+ This should not be default middleware...
11
15
  if (c.res.body) {
12
- const buff = await c.res.clone().arrayBuffer();
13
- c.res.headers.append('Content-Length', buff.byteLength.toString());
16
+ // Do not clone Response, ex: c.res.clone().arrayBuffer()
17
+ const response = new Response(c.res.body, {
18
+ status: c.res.status,
19
+ statusText: c.res.statusText,
20
+ headers: c.res.headers,
21
+ })
22
+ c.res = response
23
+ const buff = await c.res.clone().arrayBuffer()
24
+ c.res.headers.append('Content-Length', buff.byteLength.toString())
14
25
  }
26
+ */
15
27
  };
16
28
  exports.defaultMiddleware = defaultMiddleware;
@@ -12,4 +12,12 @@ export declare class Middleware {
12
12
  realm?: string;
13
13
  }) => (ctx: import("./context").Context, next: Function) => Promise<any>;
14
14
  static bodyParse: () => (ctx: import("./context").Context, next: Function) => Promise<void>;
15
+ static cors: (options?: {
16
+ origin: string;
17
+ allowMethods?: string[];
18
+ allowHeaders?: string[];
19
+ maxAge?: number;
20
+ credentials?: boolean;
21
+ exposeHeaders?: string[];
22
+ }) => (c: import("./context").Context, next: Function) => Promise<void>;
15
23
  }
@@ -6,6 +6,7 @@ const powered_by_1 = require("./middleware/powered-by/powered-by");
6
6
  const logger_1 = require("./middleware/logger/logger");
7
7
  const basic_auth_1 = require("./middleware/basic-auth/basic-auth");
8
8
  const body_parse_1 = require("./middleware/body-parse/body-parse");
9
+ const cors_1 = require("./middleware/cors/cors");
9
10
  class Middleware {
10
11
  }
11
12
  exports.Middleware = Middleware;
@@ -14,3 +15,4 @@ Middleware.poweredBy = powered_by_1.poweredBy;
14
15
  Middleware.logger = logger_1.logger;
15
16
  Middleware.basicAuth = basic_auth_1.basicAuth;
16
17
  Middleware.bodyParse = body_parse_1.bodyParse;
18
+ Middleware.cors = cors_1.cors;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "[炎] Ultrafast web framework for Cloudflare Workers.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",