@tinyhttp/send
Advanced tools
Comparing version 2.2.1 to 2.2.2
@@ -0,4 +1,4 @@ | ||
import { Stats, statSync, createReadStream } from "node:fs"; | ||
import { parse, format } from "@tinyhttp/content-type"; | ||
import { eTag } from "@tinyhttp/etag"; | ||
import { Stats, statSync, createReadStream } from "node:fs"; | ||
import { STATUS_CODES } from "node:http"; | ||
@@ -9,7 +9,6 @@ import { isAbsolute, join, extname } from "node:path"; | ||
res.setHeader("Content-Type", "application/json"); | ||
if (typeof body === "object" && body != null) | ||
if ((typeof body === "number" || typeof body === "boolean" || typeof body === "object") && body != null) | ||
res.end(JSON.stringify(body, null, 2), ...args); | ||
else if (typeof body === "string") | ||
res.end(body, ...args); | ||
else if (body == null) { | ||
else if (typeof body === "string") res.end(body, ...args); | ||
else { | ||
res.removeHeader("Content-Length"); | ||
@@ -24,5 +23,4 @@ res.removeHeader("Transfer-Encoding"); | ||
return eTag(body, { weak: true }); | ||
} else { | ||
return eTag(!Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body, { weak: true }); | ||
} | ||
return eTag(!Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body, { weak: true }); | ||
}; | ||
@@ -44,4 +42,3 @@ function setCharset(type, charset) { | ||
res.setHeader("Content-Type", setCharset(type, "utf-8")); | ||
} else | ||
res.setHeader("Content-Type", setCharset("text/html", "utf-8")); | ||
} else res.setHeader("Content-Type", setCharset("text/html", "utf-8")); | ||
} | ||
@@ -53,4 +50,3 @@ const encoding = "utf8"; | ||
} | ||
if (req.fresh) | ||
res.statusCode = 304; | ||
if (req.fresh) res.statusCode = 304; | ||
if (res.statusCode === 204 || res.statusCode === 304) { | ||
@@ -70,11 +66,9 @@ res.removeHeader("Content-Type"); | ||
return res; | ||
} else if (Buffer.isBuffer(body)) { | ||
if (!res.getHeader("Content-Type")) | ||
res.setHeader("content-type", "application/octet-stream"); | ||
} | ||
if (Buffer.isBuffer(body)) { | ||
if (!res.getHeader("Content-Type")) res.setHeader("content-type", "application/octet-stream"); | ||
res.end(bodyToSend); | ||
} else | ||
json(res)(bodyToSend, encoding); | ||
} else json(res)(bodyToSend, encoding); | ||
} else { | ||
if (typeof bodyToSend !== "string") | ||
bodyToSend = bodyToSend.toString(); | ||
if (typeof bodyToSend !== "string") bodyToSend = bodyToSend.toString(); | ||
res.end(bodyToSend, encoding); | ||
@@ -96,15 +90,10 @@ } | ||
let cc = caching.maxAge != null && `public,max-age=${caching.maxAge}`; | ||
if (cc && caching.immutable) | ||
cc += ",immutable"; | ||
else if (cc && caching.maxAge === 0) | ||
cc += ",must-revalidate"; | ||
if (cc) | ||
res.setHeader("Cache-Control", cc); | ||
if (cc && caching.immutable) cc += ",immutable"; | ||
else if (cc && caching.maxAge === 0) cc += ",must-revalidate"; | ||
if (cc) res.setHeader("Cache-Control", cc); | ||
}; | ||
const sendFile = (req, res) => (path, opts = {}, cb) => { | ||
const { root, headers = {}, encoding = "utf-8", caching, ...options } = opts; | ||
if (!isAbsolute(path) && !root) | ||
throw new TypeError("path must be absolute"); | ||
if (caching) | ||
enableCaching(res, caching); | ||
if (!isAbsolute(path) && !root) throw new TypeError("path must be absolute"); | ||
if (caching) enableCaching(res, caching); | ||
const filePath = root ? join(root, path) : path; | ||
@@ -114,11 +103,10 @@ const stats = statSync(filePath); | ||
headers["Last-Modified"] = stats.mtime.toUTCString(); | ||
headers["ETag"] = createETag(stats, encoding); | ||
if (!res.getHeader("Content-Type")) | ||
headers["Content-Type"] = mime.getType(extname(path)) + "; charset=utf-8"; | ||
headers.ETag = createETag(stats, encoding); | ||
if (!res.getHeader("Content-Type")) headers["Content-Type"] = `${mime.getType(extname(path))}; charset=utf-8`; | ||
let status2 = res.statusCode || 200; | ||
if (req.headers["range"]) { | ||
if (req.headers.range) { | ||
status2 = 206; | ||
const [x, y] = req.headers.range.replace("bytes=", "").split("-"); | ||
const end = options.end = parseInt(y, 10) || stats.size - 1; | ||
const start = options.start = parseInt(x, 10) || 0; | ||
const end = options.end = Number.parseInt(y, 10) || stats.size - 1; | ||
const start = options.start = Number.parseInt(x, 10) || 0; | ||
if (start >= stats.size || end >= stats.size) { | ||
@@ -136,8 +124,6 @@ res.writeHead(416, { | ||
} | ||
for (const [k, v] of Object.entries(headers)) | ||
res.setHeader(k, v); | ||
for (const [k, v] of Object.entries(headers)) res.setHeader(k, v); | ||
res.writeHead(status2, headers); | ||
const stream = createReadStream(filePath, options); | ||
if (cb) | ||
stream.on("error", (err) => cb(err)).on("end", () => cb()); | ||
if (cb) stream.on("error", (err) => cb(err)).on("end", () => cb()); | ||
stream.pipe(res); | ||
@@ -144,0 +130,0 @@ return res; |
@@ -1,3 +0,3 @@ | ||
/// <reference types="node" /> | ||
import { ServerResponse as S } from 'node:http'; | ||
type Res = Pick<S, 'setHeader' | 'end' | 'removeHeader'>; | ||
@@ -8,4 +8,4 @@ /** | ||
*/ | ||
export declare const json: <Response_1 extends Res = Res>(res: Response_1) => (body: any, ...args: any[]) => Response_1; | ||
export declare const json: <Response extends Res = Res>(res: Response) => (body: any, ...args: any[]) => Response; | ||
export {}; | ||
//# sourceMappingURL=json.d.ts.map |
@@ -1,3 +0,3 @@ | ||
/// <reference types="node" /> | ||
import type { IncomingMessage as I, ServerResponse as S } from 'node:http'; | ||
import { IncomingMessage as I, ServerResponse as S } from 'node:http'; | ||
type Req = Pick<I, 'method'> & { | ||
@@ -18,4 +18,4 @@ fresh?: boolean; | ||
*/ | ||
export declare const send: <Request_1 extends Req = Req, Response_1 extends Res = Res>(req: Request_1, res: Response_1) => (body: any) => Response_1; | ||
export declare const send: <Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) => (body: any) => Response; | ||
export {}; | ||
//# sourceMappingURL=send.d.ts.map |
@@ -1,5 +0,3 @@ | ||
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import type { IncomingMessage as I, ServerResponse as S } from 'node:http'; | ||
import { IncomingMessage as I, ServerResponse as S } from 'node:http'; | ||
export type ReadStreamOptions = Partial<{ | ||
@@ -40,4 +38,4 @@ flags: string; | ||
*/ | ||
export declare const sendFile: <Request_1 extends Req = Req, Response_1 extends Res = Res>(req: Request_1, res: Response_1) => (path: string, opts?: SendFileOptions, cb?: (err?: any) => void) => Response_1; | ||
export declare const sendFile: <Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) => (path: string, opts?: SendFileOptions, cb?: (err?: any) => void) => Response; | ||
export {}; | ||
//# sourceMappingURL=sendFile.d.ts.map |
@@ -1,3 +0,3 @@ | ||
/// <reference types="node" /> | ||
import { IncomingMessage as I, ServerResponse as S } from 'node:http'; | ||
type Req = Pick<I, 'method'>; | ||
@@ -13,4 +13,4 @@ type Res = Pick<S, 'setHeader' | 'removeHeader' | 'end' | 'getHeader' | 'statusCode'>; | ||
*/ | ||
export declare const sendStatus: <Request_1 extends Req = Req, Response_1 extends Res = Res>(req: Request_1, res: Response_1) => (statusCode: number) => Response_1; | ||
export declare const sendStatus: <Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) => (statusCode: number) => Response; | ||
export {}; | ||
//# sourceMappingURL=sendStatus.d.ts.map |
@@ -1,3 +0,3 @@ | ||
/// <reference types="node" /> | ||
import type { ServerResponse } from 'node:http'; | ||
import { ServerResponse } from 'node:http'; | ||
type Res = Pick<ServerResponse, 'statusCode'>; | ||
@@ -9,4 +9,4 @@ /** | ||
*/ | ||
export declare const status: <Response_1 extends Res = Res>(res: Response_1) => (status: number) => Response_1; | ||
export declare const status: <Response extends Res = Res>(res: Response) => (status: number) => Response; | ||
export {}; | ||
//# sourceMappingURL=status.d.ts.map |
@@ -1,6 +0,5 @@ | ||
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { Stats } from 'node:fs'; | ||
export declare const createETag: (body: Buffer | string | Stats, encoding: BufferEncoding) => string; | ||
export declare function setCharset(type: string, charset: string): string; | ||
//# sourceMappingURL=utils.d.ts.map |
{ | ||
"name": "@tinyhttp/send", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"type": "module", | ||
@@ -31,8 +31,5 @@ "description": "json, send, sendFile, status and sendStatus methods for tinyhttp", | ||
"@tinyhttp/content-type": "^0.1.4", | ||
"mime": "4.0.1", | ||
"@tinyhttp/etag": "2.1.1" | ||
"mime": "4.0.0-beta.1", | ||
"@tinyhttp/etag": "2.1.2" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
@@ -39,0 +36,0 @@ "dev": "vite", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29569
20
229
+ Added@tinyhttp/etag@2.1.2(transitive)
+ Addedmime@4.0.0-beta.1(transitive)
- Removed@tinyhttp/etag@2.1.1(transitive)
- Removedmime@4.0.1(transitive)
Updated@tinyhttp/etag@2.1.2
Updatedmime@4.0.0-beta.1