ios-dev/comments/Home/node_modules/@tinyhttp/send/dist/index.js.map

1 line
12 KiB
Plaintext
Raw Normal View History

2024-03-11 14:47:28 +03:00
{"version":3,"file":"index.js","sources":["../src/json.ts","../src/utils.ts","../src/send.ts","../src/sendStatus.ts","../src/status.ts","../src/sendFile.ts"],"sourcesContent":["import { ServerResponse as S } from 'node:http'\n\ntype Res = Pick<S, 'setHeader' | 'end' | 'removeHeader'>\n\n/**\n * Respond with stringified JSON object\n * @param res Response\n */\nexport const json =\n <Response extends Res = Res>(res: Response) =>\n (body: any, ...args: any[]): Response => {\n res.setHeader('Content-Type', 'application/json')\n if (typeof body === 'object' && body != null) res.end(JSON.stringify(body, null, 2), ...args)\n else if (typeof body === 'string') res.end(body, ...args)\n else if (body == null) {\n res.removeHeader('Content-Length')\n res.removeHeader('Transfer-Encoding')\n res.end(null, ...args)\n }\n\n return res\n }\n","import { parse, format } from '@tinyhttp/content-type'\nimport { eTag } from '@tinyhttp/etag'\nimport { Stats } from 'node:fs'\n\nexport const createETag = (body: Buffer | string | Stats, encoding: BufferEncoding): string => {\n if (body instanceof Stats) {\n return eTag(body, { weak: true })\n } else {\n return eTag(!Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body, { weak: true })\n }\n}\n\nexport function setCharset(type: string, charset: string): string {\n const parsed = parse(type)\n parsed.parameters.charset = charset\n return format(parsed)\n}\n","import type { IncomingMessage as I, ServerResponse as S } from 'node:http'\nimport { json } from './json.js'\nimport { setCharset, createETag } from './utils.js'\n\ntype Req = Pick<I, 'method'> & { fresh?: boolean }\n\ntype Res = Pick<S, 'setHeader' | 'removeHeader' | 'end' | 'getHeader' | 'statusCode'>\n\n/**\n * Sends the HTTP response.\n *\n * The body parameter can be a Buffer object, a string, an object, or an array.\n *\n * This method performs many useful tasks for simple non-streaming responses.\n * For example, it automatically assigns the Content-Length HTTP response header field (unless previously defined) and provides automatic HEAD and HTTP cache freshness support.\n *\n * @param req Request\n * @param res Response\n */\nexport const send =\n <Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) =>\n (body: any): Response => {\n let bodyToSend = body\n\n if (Buffer.isBuffer(body)) {\n bodyToSend = body\n } else if (typeof body === 'object' && body !== null) {\n // in case of object - turn it to json\n bodyToSend = JSON.stringify(body, null, 2)\n } else if (typeof body === 'string') {\n // reflect this in content-type\n const type = res.getHeader('Content-Type')\n\n if (type && typeof type === 'string') {\n res.setHeader('Content-Type', setCharset(type, 'utf-8'))\n } else res.setHeader('Content-Type', setCharset('text/html', 'utf-8'))\n }\n\n // Set encoding\n const encoding: 'utf8' | undefined = 'utf8'\n\n // populate ETag\n let etag: string | undefined\n if (body && !res.getHeader('etag') && (etag = createETag(bodyToSend, encoding))) {\n res.setHeader('etag', etag)\n }\n\n // freshness\n if (req.fresh) res.statusCode = 304\n\n // strip irrelevant headers\n if (res.statusCode === 204 || res.statusCode === 304) {\n res.removeHeader('Content-Type')\n res.removeHeader('Content-Length')\n res.removeHeader('Transfer-Encoding')\n bodyToSend = ''\n }\n\n if (req.method === 'HEAD') {\n res.end('')\n return res\n }\n\n if (typeof body === 'object') {\n if (body == null) {\n res.end('')\n return res\n } else if (Buffer.isBuffer(body)) {\n if (!res.getHeader('Content-Type')) res.setHeader('content-type', 'application/octet-stream')\n res.end(bodyToSend)\n } else json(res)(bodyToSend, encoding)\n } else {\n if (typeof bodyToSend !== 'string') bodyToSend = bodyToSend.toString()\n\n res.end(bodyToSend, encoding)\n }\n\n return res\n }\n","import { IncomingMessage