diff options
Diffstat (limited to 'cli/bench/testdata/npm/hono/dist/middleware')
36 files changed, 853 insertions, 0 deletions
diff --git a/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.d.ts new file mode 100644 index 000000000..072df608f --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.d.ts @@ -0,0 +1,11 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +export declare const basicAuth: (options: { + username: string; + password: string; + realm?: string; + hashFunction?: Function; +}, ...users: { + username: string; + password: string; +}[]) => (ctx: Context, next: Next) => Promise<void>; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.js b/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.js new file mode 100644 index 000000000..c58244a3c --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/basic-auth/index.js @@ -0,0 +1,48 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.basicAuth = void 0; +const buffer_1 = require("../../utils/buffer"); +const encode_1 = require("../../utils/encode"); +const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/; +const USER_PASS_REGEXP = /^([^:]*):(.*)$/; +const auth = (req) => { + const match = CREDENTIALS_REGEXP.exec(req.headers.get('Authorization') || ''); + if (!match) { + return undefined; + } + const userPass = USER_PASS_REGEXP.exec((0, encode_1.decodeBase64)(match[1])); + if (!userPass) { + return undefined; + } + return { username: userPass[1], password: userPass[2] }; +}; +const basicAuth = (options, ...users) => { + if (!options) { + throw new Error('basic auth middleware requires options for "username and password"'); + } + if (!options.realm) { + options.realm = 'Secure Area'; + } + users.unshift({ username: options.username, password: options.password }); + return async (ctx, next) => { + const requestUser = auth(ctx.req); + if (requestUser) { + for (const user of users) { + const usernameEqual = await (0, buffer_1.timingSafeEqual)(user.username, requestUser.username, options.hashFunction); + const passwordEqual = await (0, buffer_1.timingSafeEqual)(user.password, requestUser.password, options.hashFunction); + if (usernameEqual && passwordEqual) { + // Authorized OK + await next(); + return; + } + } + } + ctx.res = new Response('Unauthorized', { + status: 401, + headers: { + 'WWW-Authenticate': 'Basic realm="' + options.realm?.replace(/"/g, '\\"') + '"', + }, + }); + }; +}; +exports.basicAuth = basicAuth; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/bearer-auth/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/bearer-auth/index.d.ts new file mode 100644 index 000000000..972177fc7 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/bearer-auth/index.d.ts @@ -0,0 +1,8 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +export declare const bearerAuth: (options: { + token: string; + realm?: string; + prefix?: string; + hashFunction?: Function; +}) => (c: Context, next: Next) => Promise<void>; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/bearer-auth/index.js b/cli/bench/testdata/npm/hono/dist/middleware/bearer-auth/index.js new file mode 100644 index 000000000..da5a78921 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/bearer-auth/index.js @@ -0,0 +1,61 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.bearerAuth = void 0; +const buffer_1 = require("../../utils/buffer"); +const TOKEN_STRINGS = '[A-Za-z0-9._~+/-]+=*'; +const PREFIX = 'Bearer'; +const bearerAuth = (options) => { + if (!options.token) { + throw new Error('bearer auth middleware requires options for "token"'); + } + if (!options.realm) { + options.realm = ''; + } + if (!options.prefix) { + options.prefix = PREFIX; + } + const realm = options.realm?.replace(/"/g, '\\"'); + return async (c, next) => { + const headerToken = c.req.headers.get('Authorization'); + if (!headerToken) { + // No Authorization header + c.res = new Response('Unauthorized', { + status: 401, + headers: { + 'WWW-Authenticate': `${options.prefix} realm="` + realm + '"', + }, + }); + } + else { + const regexp = new RegExp('^' + options.prefix + ' +(' + TOKEN_STRINGS + ') *$'); + const match = regexp.exec(headerToken); + if (!match) { + // Invalid Request + c.res = new Response('Bad Request', { + status: 400, + headers: { + 'WWW-Authenticate': `${options.prefix} error="invalid_request"`, + }, + }); + } + else { + const equal = await (0, buffer_1.timingSafeEqual)(options.token, match[1], options.hashFunction); + if (!equal) { + // Invalid Token + c.res = new Response('Unauthorized', { + status: 401, + headers: { + 'WWW-Authenticate': `${options.prefix} error="invalid_token"`, + }, + }); + } + else { + // Authorize OK + await next(); + return; + } + } + } + }; +}; +exports.bearerAuth = bearerAuth; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/cache/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/cache/index.d.ts new file mode 100644 index 000000000..c0390c41e --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/cache/index.d.ts @@ -0,0 +1,7 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +export declare const cache: (options: { + cacheName: string; + wait?: boolean; + cacheControl?: string; +}) => (c: Context, next: Next) => Promise<Response | undefined>; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/cache/index.js b/cli/bench/testdata/npm/hono/dist/middleware/cache/index.js new file mode 100644 index 000000000..890058af9 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/cache/index.js @@ -0,0 +1,32 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.cache = void 0; +const cache = (options) => { + if (options.wait === undefined) { + options.wait = false; + } + const addHeader = (response) => { + if (options.cacheControl) + response.headers.append('Cache-Control', options.cacheControl); + }; + return async (c, next) => { + const key = c.req; + const cache = await caches.open(options.cacheName); + const response = await cache.match(key); + if (!response) { + await next(); + addHeader(c.res); + const response = c.res.clone(); + if (options.wait) { + await cache.put(key, response); + } + else { + c.executionCtx.waitUntil(cache.put(key, response)); + } + } + else { + return response; + } + }; +}; +exports.cache = cache; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/compress/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/compress/index.d.ts new file mode 100644 index 000000000..53f39ad86 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/compress/index.d.ts @@ -0,0 +1,8 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +declare type EncodingType = 'gzip' | 'deflate'; +interface CompressionOptions { + encoding?: EncodingType; +} +export declare const compress: (options?: CompressionOptions) => (ctx: Context, next: Next) => Promise<void>; +export {}; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/compress/index.js b/cli/bench/testdata/npm/hono/dist/middleware/compress/index.js new file mode 100644 index 000000000..fd605cf71 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/compress/index.js @@ -0,0 +1,19 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.compress = void 0; +const compress = (options) => { + return async (ctx, next) => { + await next(); + const accepted = ctx.req.headers.get('Accept-Encoding'); + const pattern = options?.encoding ?? /gzip|deflate/; + const match = accepted?.match(pattern); + if (!accepted || !match || !ctx.res.body) { + return; + } + const encoding = match[0]; + const stream = new CompressionStream(encoding); + ctx.res = new Response(ctx.res.body.pipeThrough(stream), ctx.res.clone()); + ctx.res.headers.set('Content-Encoding', encoding); + }; +}; +exports.compress = compress; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/cors/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/cors/index.d.ts new file mode 100644 index 000000000..f353df808 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/cors/index.d.ts @@ -0,0 +1,12 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +declare type CORSOptions = { + origin: string; + allowMethods?: string[]; + allowHeaders?: string[]; + maxAge?: number; + credentials?: boolean; + exposeHeaders?: string[]; +}; +export declare const cors: (options?: CORSOptions) => (c: Context, next: Next) => Promise<void>; +export {}; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/cors/index.js b/cli/bench/testdata/npm/hono/dist/middleware/cors/index.js new file mode 100644 index 000000000..441de0928 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/cors/index.js @@ -0,0 +1,61 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.cors = void 0; +const cors = (options) => { + const defaults = { + origin: '*', + allowMethods: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH'], + allowHeaders: [], + exposeHeaders: [], + }; + const opts = { + ...defaults, + ...options, + }; + return async (c, next) => { + await next(); + function set(key, value) { + c.res.headers.append(key, value); + } + set('Access-Control-Allow-Origin', opts.origin); + // Suppose the server sends a response with an Access-Control-Allow-Origin value with an explicit origin (rather than the "*" wildcard). + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin + if (opts.origin !== '*') { + set('Vary', 'Origin'); + } + if (opts.credentials) { + set('Access-Control-Allow-Credentials', 'true'); + } + if (opts.exposeHeaders?.length) { + set('Access-Control-Expose-Headers', opts.exposeHeaders.join(',')); + } + if (c.req.method === 'OPTIONS') { + // Preflight + if (opts.maxAge != null) { + set('Access-Control-Max-Age', opts.maxAge.toString()); + } + if (opts.allowMethods?.length) { + set('Access-Control-Allow-Methods', opts.allowMethods.join(',')); + } + let headers = opts.allowHeaders; + if (!headers?.length) { + const requestHeaders = c.req.headers.get('Access-Control-Request-Headers'); + if (requestHeaders) { + headers = requestHeaders.split(/\s*,\s*/); + } + } + if (headers?.length) { + set('Access-Control-Allow-Headers', headers.join(',')); + set('Vary', 'Access-Control-Request-Headers'); + } + c.res.headers.delete('Content-Length'); + c.res.headers.delete('Content-Type'); + c.res = new Response(null, { + headers: c.res.headers, + status: 204, + statusText: c.res.statusText, + }); + } + }; +}; +exports.cors = cors; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/etag/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/etag/index.d.ts new file mode 100644 index 000000000..8cb945e14 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/etag/index.d.ts @@ -0,0 +1,7 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +declare type ETagOptions = { + weak: boolean; +}; +export declare const etag: (options?: ETagOptions) => (c: Context, next: Next) => Promise<void>; +export {}; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/etag/index.js b/cli/bench/testdata/npm/hono/dist/middleware/etag/index.js new file mode 100644 index 000000000..60800edf1 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/etag/index.js @@ -0,0 +1,27 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.etag = void 0; +const crypto_1 = require("../../utils/crypto"); +const etag = (options = { weak: false }) => { + return async (c, next) => { + const ifNoneMatch = c.req.header('If-None-Match') || c.req.header('if-none-match'); + await next(); + const res = c.res; + const clone = res.clone(); + const hash = await (0, crypto_1.sha1)(res.body || ''); + const etag = options.weak ? `W/"${hash}"` : `"${hash}"`; + if (ifNoneMatch && ifNoneMatch === etag) { + await clone.blob(); // Force using body + c.res = new Response(null, { + status: 304, + statusText: 'Not Modified', + }); + c.res.headers.delete('Content-Length'); + } + else { + c.res = new Response(clone.body, clone); + c.res.headers.append('ETag', etag); + } + }; +}; +exports.etag = etag; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/html/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/html/index.d.ts new file mode 100644 index 000000000..976a7d1db --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/html/index.d.ts @@ -0,0 +1,3 @@ +import type { HtmlEscapedString } from '../../utils/html'; +export declare const raw: (value: any) => HtmlEscapedString; +export declare const html: (strings: TemplateStringsArray, ...values: any[]) => HtmlEscapedString; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/html/index.js b/cli/bench/testdata/npm/hono/dist/middleware/html/index.js new file mode 100644 index 000000000..598621bf0 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/html/index.js @@ -0,0 +1,36 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.html = exports.raw = void 0; +const html_1 = require("../../utils/html"); +const raw = (value) => { + const escapedString = new String(value); + escapedString.isEscaped = true; + return escapedString; +}; +exports.raw = raw; +const html = (strings, ...values) => { + const buffer = ['']; + for (let i = 0, len = strings.length - 1; i < len; i++) { + buffer[0] += strings[i]; + const children = values[i] instanceof Array ? values[i].flat(Infinity) : [values[i]]; + for (let i = 0, len = children.length; i < len; i++) { + const child = children[i]; + if (typeof child === 'string') { + (0, html_1.escapeToBuffer)(child, buffer); + } + else if (typeof child === 'boolean' || child === null || child === undefined) { + continue; + } + else if ((typeof child === 'object' && child.isEscaped) || + typeof child === 'number') { + buffer[0] += child; + } + else { + (0, html_1.escapeToBuffer)(child.toString(), buffer); + } + } + } + buffer[0] += strings[strings.length - 1]; + return (0, exports.raw)(buffer[0]); +}; +exports.html = html; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.d.ts new file mode 100644 index 000000000..3222b2b36 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.d.ts @@ -0,0 +1,26 @@ +import type { StringBuffer, HtmlEscaped, HtmlEscapedString } from '../../utils/html'; +declare global { + namespace jsx.JSX { + interface IntrinsicElements { + [tagName: string]: Record<string, any>; + } + } +} +declare type Child = string | number | JSXNode | Child[]; +export declare class JSXNode implements HtmlEscaped { + tag: string | Function; + props: Record<string, any>; + children: Child[]; + isEscaped: true; + constructor(tag: string | Function, props: Record<string, any>, children: Child[]); + toString(): string; + toStringToBuffer(buffer: StringBuffer): void; +} +export { jsxFn as jsx }; +declare const jsxFn: (tag: string | Function, props: Record<string, any>, ...children: (string | HtmlEscapedString)[]) => JSXNode; +declare type FC<T = Record<string, any>> = (props: T) => HtmlEscapedString; +export declare const memo: <T>(component: FC<T>, propsAreEqual?: (prevProps: Readonly<T>, nextProps: Readonly<T>) => boolean) => FC<T>; +export declare const Fragment: (props: { + key?: string; + children?: any; +}) => JSXNode; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.js b/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.js new file mode 100644 index 000000000..f72a7ecb9 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/index.js @@ -0,0 +1,193 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Fragment = exports.memo = exports.jsx = exports.JSXNode = void 0; +const html_1 = require("../../utils/html"); +const emptyTags = [ + 'area', + 'base', + 'br', + 'col', + 'embed', + 'hr', + 'img', + 'input', + 'keygen', + 'link', + 'meta', + 'param', + 'source', + 'track', + 'wbr', +]; +const booleanAttributes = [ + 'allowfullscreen', + 'async', + 'autofocus', + 'autoplay', + 'checked', + 'controls', + 'default', + 'defer', + 'disabled', + 'formnovalidate', + 'hidden', + 'inert', + 'ismap', + 'itemscope', + 'loop', + 'multiple', + 'muted', + 'nomodule', + 'novalidate', + 'open', + 'playsinline', + 'readonly', + 'required', + 'reversed', + 'selected', +]; +const childrenToStringToBuffer = (children, buffer) => { + for (let i = 0, len = children.length; i < len; i++) { + const child = children[i]; + if (typeof child === 'string') { + (0, html_1.escapeToBuffer)(child, buffer); + } + else if (typeof child === 'boolean' || child === null || child === undefined) { + continue; + } + else if (child instanceof JSXNode) { + child.toStringToBuffer(buffer); + } + else if (typeof child === 'number' || child.isEscaped) { + buffer[0] += child; + } + else { + // `child` type is `Child[]`, so stringify recursively + childrenToStringToBuffer(child, buffer); + } + } +}; +class JSXNode { + constructor(tag, props, children) { + this.isEscaped = true; + this.tag = tag; + this.props = props; + this.children = children; + } + toString() { + const buffer = ['']; + this.toStringToBuffer(buffer); + return buffer[0]; + } + toStringToBuffer(buffer) { + const tag = this.tag; + const props = this.props; + let { children } = this; + buffer[0] += `<${tag}`; + const propsKeys = Object.keys(props || {}); + for (let i = 0, len = propsKeys.length; i < len; i++) { + const v = props[propsKeys[i]]; + if (typeof v === 'string') { + buffer[0] += ` ${propsKeys[i]}="`; + (0, html_1.escapeToBuffer)(v, buffer); + buffer[0] += '"'; + } + else if (typeof v === 'number') { + buffer[0] += ` ${propsKeys[i]}="${v}"`; + } + else if (v === null || v === undefined) { + // Do nothing + } + else if (typeof v === 'boolean' && booleanAttributes.includes(propsKeys[i])) { + if (v) { + buffer[0] += ` ${propsKeys[i]}=""`; + } + } + else if (propsKeys[i] === 'dangerouslySetInnerHTML') { + if (children.length > 0) { + throw 'Can only set one of `children` or `props.dangerouslySetInnerHTML`.'; + } + const escapedString = new String(v.__html); + escapedString.isEscaped = true; + children = [escapedString]; + } + else { + buffer[0] += ` ${propsKeys[i]}="`; + (0, html_1.escapeToBuffer)(v.toString(), buffer); + buffer[0] += '"'; + } + } + if (emptyTags.includes(tag)) { + buffer[0] += '/>'; + return; + } + buffer[0] += '>'; + childrenToStringToBuffer(children, buffer); + buffer[0] += `</${tag}>`; + } +} +exports.JSXNode = JSXNode; +class JSXFunctionNode extends JSXNode { + toStringToBuffer(buffer) { + const { children } = this; + const res = this.tag.call(null, { + ...this.props, + children: children.length <= 1 ? children[0] : children, + }); + if (res instanceof JSXNode) { + res.toStringToBuffer(buffer); + } + else if (typeof res === 'number' || res.isEscaped) { + buffer[0] += res; + } + else { + (0, html_1.escapeToBuffer)(res, buffer); + } + } +} +class JSXFragmentNode extends JSXNode { + toStringToBuffer(buffer) { + childrenToStringToBuffer(this.children, buffer); + } +} +const jsxFn = (tag, props, ...children) => { + if (typeof tag === 'function') { + return new JSXFunctionNode(tag, props, children); + } + else { + return new JSXNode(tag, props, children); + } +}; +exports.jsx = jsxFn; +const shallowEqual = (a, b) => { + if (a === b) { + return true; + } + const aKeys = Object.keys(a); + const bKeys = Object.keys(b); + if (aKeys.length !== bKeys.length) { + return false; + } + for (let i = 0, len = aKeys.length; i < len; i++) { + if (a[aKeys[i]] !== b[aKeys[i]]) { + return false; + } + } + return true; +}; +const memo = (component, propsAreEqual = shallowEqual) => { + let computed = undefined; + let prevProps = undefined; + return ((props) => { + if (prevProps && !propsAreEqual(prevProps, props)) { + computed = undefined; + } + prevProps = props; + return (computed || (computed = component(props))); + }); +}; +exports.memo = memo; +const Fragment = (props) => { + return new JSXFragmentNode('', {}, props.children || []); +}; +exports.Fragment = Fragment; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.d.ts new file mode 100644 index 000000000..d76fc7124 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.d.ts @@ -0,0 +1,2 @@ +import type { JSXNode } from '.'; +export declare function jsxDEV(tag: string | Function, props: Record<string, any>): JSXNode; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.js b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.js new file mode 100644 index 000000000..6aa4bfa00 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-dev-runtime.js @@ -0,0 +1,10 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.jsxDEV = void 0; +const _1 = require("."); +function jsxDEV(tag, props) { + const children = props.children ?? []; + delete props['children']; + return (0, _1.jsx)(tag, props, children); +} +exports.jsxDEV = jsxDEV; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.d.ts new file mode 100644 index 000000000..4d505cdf8 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.d.ts @@ -0,0 +1,2 @@ +export { jsxDEV as jsx } from './jsx-dev-runtime'; +export { jsxDEV as jsxs } from './jsx-dev-runtime'; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.js b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.js new file mode 100644 index 000000000..ee8c3fe72 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jsx/jsx-runtime.js @@ -0,0 +1,7 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.jsxs = exports.jsx = void 0; +var jsx_dev_runtime_1 = require("./jsx-dev-runtime"); +Object.defineProperty(exports, "jsx", { enumerable: true, get: function () { return jsx_dev_runtime_1.jsxDEV; } }); +var jsx_dev_runtime_2 = require("./jsx-dev-runtime"); +Object.defineProperty(exports, "jsxs", { enumerable: true, get: function () { return jsx_dev_runtime_2.jsxDEV; } }); diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jwt/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/jwt/index.d.ts new file mode 100644 index 000000000..5b13b4816 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jwt/index.d.ts @@ -0,0 +1,7 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +export declare const jwt: (options: { + secret: string; + cookie?: string; + alg?: string; +}) => (ctx: Context, next: Next) => Promise<void>; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/jwt/index.js b/cli/bench/testdata/npm/hono/dist/middleware/jwt/index.js new file mode 100644 index 000000000..6f486560b --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/jwt/index.js @@ -0,0 +1,63 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.jwt = void 0; +const jwt_1 = require("../../utils/jwt"); +const jwt = (options) => { + if (!options) { + throw new Error('JWT auth middleware requires options for "secret'); + } + if (!crypto.subtle || !crypto.subtle.importKey) { + throw new Error('`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.'); + } + return async (ctx, next) => { + const credentials = ctx.req.headers.get('Authorization'); + let token; + if (credentials) { + const parts = credentials.split(/\s+/); + if (parts.length !== 2) { + ctx.res = new Response('Unauthorized', { + status: 401, + headers: { + 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="invalid credentials structure"`, + }, + }); + return; + } + else { + token = parts[1]; + } + } + else if (options.cookie) { + token = ctx.req.cookie(options.cookie); + } + if (!token) { + ctx.res = new Response('Unauthorized', { + status: 401, + headers: { + 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`, + }, + }); + return; + } + let authorized = false; + let msg = ''; + try { + authorized = await jwt_1.Jwt.verify(token, options.secret, options.alg); + } + catch (e) { + msg = `${e}`; + } + if (!authorized) { + ctx.res = new Response('Unauthorized', { + status: 401, + statusText: msg, + headers: { + 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_token",error_description="token verification failure"`, + }, + }); + return; + } + await next(); + }; +}; +exports.jwt = jwt; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/logger/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/logger/index.d.ts new file mode 100644 index 000000000..232305d2f --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/logger/index.d.ts @@ -0,0 +1,5 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +declare type PrintFunc = (str: string, ...rest: string[]) => void; +export declare const logger: (fn?: PrintFunc) => (c: Context, next: Next) => Promise<void>; +export {}; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/logger/index.js b/cli/bench/testdata/npm/hono/dist/middleware/logger/index.js new file mode 100644 index 000000000..44cf994d0 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/logger/index.js @@ -0,0 +1,49 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.logger = void 0; +const url_1 = require("../../utils/url"); +var LogPrefix; +(function (LogPrefix) { + LogPrefix["Outgoing"] = "-->"; + LogPrefix["Incoming"] = "<--"; + LogPrefix["Error"] = "xxx"; +})(LogPrefix || (LogPrefix = {})); +const humanize = (times) => { + const [delimiter, separator] = [',', '.']; + const orderTimes = times.map((v) => v.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1' + delimiter)); + return orderTimes.join(separator); +}; +const time = (start) => { + const delta = Date.now() - start; + return humanize([delta < 1000 ? delta + 'ms' : Math.round(delta / 1000) + 's']); +}; +const colorStatus = (status) => { + const out = { + 7: `\x1b[35m${status}\x1b[0m`, + 5: `\x1b[31m${status}\x1b[0m`, + 4: `\x1b[33m${status}\x1b[0m`, + 3: `\x1b[36m${status}\x1b[0m`, + 2: `\x1b[32m${status}\x1b[0m`, + 1: `\x1b[32m${status}\x1b[0m`, + 0: `\x1b[33m${status}\x1b[0m`, + }; + const calculateStatus = (status / 100) | 0; + return out[calculateStatus]; +}; +function log(fn, prefix, method, path, status = 0, elapsed) { + const out = prefix === LogPrefix.Incoming + ? ` ${prefix} ${method} ${path}` + : ` ${prefix} ${method} ${path} ${colorStatus(status)} ${elapsed}`; + fn(out); +} +const logger = (fn = console.log) => { + return async (c, next) => { + const { method } = c.req; + const path = (0, url_1.getPathFromURL)(c.req.url); + log(fn, LogPrefix.Incoming, method, path); + const start = Date.now(); + await next(); + log(fn, LogPrefix.Outgoing, method, path, c.res.status, time(start)); + }; +}; +exports.logger = logger; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/powered-by/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/powered-by/index.d.ts new file mode 100644 index 000000000..eb7995b80 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/powered-by/index.d.ts @@ -0,0 +1,3 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +export declare const poweredBy: () => (c: Context, next: Next) => Promise<void>; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/powered-by/index.js b/cli/bench/testdata/npm/hono/dist/middleware/powered-by/index.js new file mode 100644 index 000000000..c1f73300b --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/powered-by/index.js @@ -0,0 +1,10 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.poweredBy = void 0; +const poweredBy = () => { + return async (c, next) => { + await next(); + c.res.headers.append('X-Powered-By', 'Hono'); + }; +}; +exports.poweredBy = poweredBy; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/pretty-json/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/pretty-json/index.d.ts new file mode 100644 index 000000000..94736847e --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/pretty-json/index.d.ts @@ -0,0 +1,7 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +declare type prettyOptions = { + space: number; +}; +export declare const prettyJSON: (options?: prettyOptions) => (c: Context, next: Next) => Promise<void>; +export {}; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/pretty-json/index.js b/cli/bench/testdata/npm/hono/dist/middleware/pretty-json/index.js new file mode 100644 index 000000000..08af0fb10 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/pretty-json/index.js @@ -0,0 +1,11 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.prettyJSON = void 0; +const prettyJSON = (options = { space: 2 }) => { + return async (c, next) => { + const pretty = c.req.query('pretty') || c.req.query('pretty') === '' ? true : false; + c.pretty(pretty, options.space); + await next(); + }; +}; +exports.prettyJSON = prettyJSON; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/serve-static/bun.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/bun.d.ts new file mode 100644 index 000000000..be4515aff --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/bun.d.ts @@ -0,0 +1,7 @@ +import type { Context } from '../../context'; +import type { Next } from '../../hono'; +export declare type ServeStaticOptions = { + root?: string; + path?: string; +}; +export declare const serveStatic: (options?: ServeStaticOptions) => (c: Context, next: Next) => Promise<Response | undefined>; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/serve-static/bun.js b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/bun.js new file mode 100644 index 000000000..d29a7d936 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/bun.js @@ -0,0 +1,38 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.serveStatic = void 0; +const filepath_1 = require("../../utils/filepath"); +const mime_1 = require("../../utils/mime"); +// @ts-ignore +const { file } = Bun; +const DEFAULT_DOCUMENT = 'index.html'; +const serveStatic = (options = { root: '' }) => { + return async (c, next) => { + // Do nothing if Response is already set + if (c.res && c.finalized) { + await next(); + } + const url = new URL(c.req.url); + let path = (0, filepath_1.getFilePath)({ + filename: options.path ?? url.pathname, + root: options.root, + defaultDocument: DEFAULT_DOCUMENT, + }); + path = `./${path}`; + const content = file(path); + if (content) { + const mimeType = (0, mime_1.getMimeType)(path); + if (mimeType) { + c.header('Content-Type', mimeType); + } + // Return Response object + return c.body(content); + } + else { + console.warn(`Static file: ${path} is not found`); + await next(); + } + return; + }; +}; +exports.serveStatic = serveStatic; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/serve-static/index.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/index.d.ts new file mode 100644 index 000000000..21bcb4dc3 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/index.d.ts @@ -0,0 +1 @@ +export { serveStatic } from './serve-static'; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/serve-static/index.js b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/index.js new file mode 100644 index 000000000..3748e9530 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/index.js @@ -0,0 +1,5 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.serveStatic = void 0; +var serve_static_1 = require("./serve-static"); +Object.defineProperty(exports, "serveStatic", { enumerable: true, get: function () { return serve_static_1.serveStatic; } }); diff --git a/cli/bench/testdata/npm/hono/dist/middleware/serve-static/module.d.mts b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/module.d.mts new file mode 100644 index 000000000..36cd766f3 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/module.d.mts @@ -0,0 +1,5 @@ +import type { ServeStaticOptions } from './serve-static'; +declare const module: (options?: ServeStaticOptions) => import("../../hono").Handler<string, { + [x: string]: any; +}>; +export { module as serveStatic }; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/serve-static/module.mjs b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/module.mjs new file mode 100644 index 000000000..968fd4433 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/module.mjs @@ -0,0 +1,13 @@ +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore +// For ES module mode +import manifest from '__STATIC_CONTENT_MANIFEST'; +import { serveStatic } from './serve-static'; +const module = (options = { root: '' }) => { + return serveStatic({ + root: options.root, + path: options.path, + manifest: options.manifest ? options.manifest : manifest, + }); +}; +export { module as serveStatic }; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.d.ts b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.d.ts new file mode 100644 index 000000000..5ff60c4ca --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.d.ts @@ -0,0 +1,9 @@ +/// <reference types="@cloudflare/workers-types" /> +import type { Handler } from '../../hono'; +export declare type ServeStaticOptions = { + root?: string; + path?: string; + manifest?: object | string; + namespace?: KVNamespace; +}; +export declare const serveStatic: (options?: ServeStaticOptions) => Handler; diff --git a/cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.js b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.js new file mode 100644 index 000000000..a647b7015 --- /dev/null +++ b/cli/bench/testdata/npm/hono/dist/middleware/serve-static/serve-static.js @@ -0,0 +1,40 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.serveStatic = void 0; +const cloudflare_1 = require("../../utils/cloudflare"); +const filepath_1 = require("../../utils/filepath"); +const mime_1 = require("../../utils/mime"); +const DEFAULT_DOCUMENT = 'index.html'; +// This middleware is available only on Cloudflare Workers. +const serveStatic = (options = { root: '' }) => { + return async (c, next) => { + // Do nothing if Response is already set + if (c.res && c.finalized) { + await next(); + } + const url = new URL(c.req.url); + const path = (0, filepath_1.getFilePath)({ + filename: options.path ?? url.pathname, + root: options.root, + defaultDocument: DEFAULT_DOCUMENT, + }); + const content = await (0, cloudflare_1.getContentFromKVAsset)(path, { + manifest: options.manifest, + namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : undefined, + }); + if (content) { + const mimeType = (0, mime_1.getMimeType)(path); + if (mimeType) { + c.header('Content-Type', mimeType); + } + // Return Response object + return c.body(content); + } + else { + console.warn(`Static file: ${path} is not found`); + await next(); + } + return; + }; +}; +exports.serveStatic = serveStatic; |
