Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tinyhttp/send

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tinyhttp/send - npm Package Compare versions

Comparing version 2.2.2 to 2.2.3

dist/json.js

138

dist/index.js

@@ -1,132 +0,6 @@

import { Stats, statSync, createReadStream } from "node:fs";
import { parse, format } from "@tinyhttp/content-type";
import { eTag } from "@tinyhttp/etag";
import { STATUS_CODES } from "node:http";
import { isAbsolute, join, extname } from "node:path";
import mime from "mime";
const json = (res) => (body, ...args) => {
res.setHeader("Content-Type", "application/json");
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 {
res.removeHeader("Content-Length");
res.removeHeader("Transfer-Encoding");
res.end(null, ...args);
}
return res;
};
const createETag = (body, encoding) => {
if (body instanceof Stats) {
return eTag(body, { weak: true });
}
return eTag(!Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body, { weak: true });
};
function setCharset(type, charset) {
const parsed = parse(type);
parsed.parameters.charset = charset;
return format(parsed);
}
const send = (req, res) => (body) => {
let bodyToSend = body;
if (Buffer.isBuffer(body)) {
bodyToSend = body;
} else if (typeof body === "object" && body !== null) {
bodyToSend = JSON.stringify(body, null, 2);
} else if (typeof body === "string") {
const type = res.getHeader("Content-Type");
if (type && typeof type === "string") {
res.setHeader("Content-Type", setCharset(type, "utf-8"));
} else res.setHeader("Content-Type", setCharset("text/html", "utf-8"));
}
const encoding = "utf8";
let etag;
if (body && !res.getHeader("etag") && (etag = createETag(bodyToSend, encoding))) {
res.setHeader("etag", etag);
}
if (req.fresh) res.statusCode = 304;
if (res.statusCode === 204 || res.statusCode === 304) {
res.removeHeader("Content-Type");
res.removeHeader("Content-Length");
res.removeHeader("Transfer-Encoding");
bodyToSend = "";
}
if (req.method === "HEAD") {
res.end("");
return res;
}
if (typeof body === "object") {
if (body == null) {
res.end("");
return res;
}
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 {
if (typeof bodyToSend !== "string") bodyToSend = bodyToSend.toString();
res.end(bodyToSend, encoding);
}
return res;
};
const sendStatus = (req, res) => (statusCode) => {
const body = STATUS_CODES[statusCode] || String(statusCode);
res.statusCode = statusCode;
res.setHeader("Content-Type", "text/plain");
return send(req, res)(body);
};
const status = (res) => (status2) => {
res.statusCode = status2;
return res;
};
const enableCaching = (res, caching) => {
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);
};
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);
const filePath = root ? join(root, path) : path;
const stats = statSync(filePath);
headers["Content-Encoding"] = encoding;
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`;
let status2 = res.statusCode || 200;
if (req.headers.range) {
status2 = 206;
const [x, y] = req.headers.range.replace("bytes=", "").split("-");
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) {
res.writeHead(416, {
"Content-Range": `bytes */${stats.size}`
}).end();
return res;
}
headers["Content-Range"] = `bytes ${start}-${end}/${stats.size}`;
headers["Content-Length"] = end - start + 1;
headers["Accept-Ranges"] = "bytes";
} else {
headers["Content-Length"] = stats.size;
}
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());
stream.pipe(res);
return res;
};
export {
enableCaching,
json,
send,
sendFile,
sendStatus,
status
};
//# sourceMappingURL=index.js.map
export * from './json.js';
export * from './send.js';
export * from './sendStatus.js';
export * from './status.js';
export * from './sendFile.js';
//# sourceMappingURL=index.js.map

@@ -1,3 +0,2 @@

import { ServerResponse as S } from 'node:http';
import type { ServerResponse as S } from 'node:http';
type Res = Pick<S, 'setHeader' | 'end' | 'removeHeader'>;

@@ -4,0 +3,0 @@ /**

@@ -1,3 +0,2 @@

import { IncomingMessage as I, ServerResponse as S } from 'node:http';
import type { IncomingMessage as I, ServerResponse as S } from 'node:http';
type Req = Pick<I, 'method'> & {

@@ -4,0 +3,0 @@ fresh?: boolean;

@@ -1,3 +0,2 @@

import { IncomingMessage as I, ServerResponse as S } from 'node:http';
import type { IncomingMessage as I, ServerResponse as S } from 'node:http';
export type ReadStreamOptions = Partial<{

@@ -4,0 +3,0 @@ flags: string;

@@ -1,3 +0,2 @@

import { IncomingMessage as I, ServerResponse as S } from 'node:http';
import type { IncomingMessage as I, ServerResponse as S } from 'node:http';
type Req = Pick<I, 'method'>;

@@ -4,0 +3,0 @@ type Res = Pick<S, 'setHeader' | 'removeHeader' | 'end' | 'getHeader' | 'statusCode'>;

@@ -1,3 +0,2 @@

import { ServerResponse } from 'node:http';
import type { ServerResponse } from 'node:http';
type Res = Pick<ServerResponse, 'statusCode'>;

@@ -4,0 +3,0 @@ /**

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.2",
"version": "2.2.3",
"type": "module",

@@ -31,9 +31,8 @@ "description": "json, send, sendFile, status and sendStatus methods for tinyhttp",

"@tinyhttp/content-type": "^0.1.4",
"mime": "4.0.0-beta.1",
"mime": "4.0.4",
"@tinyhttp/etag": "2.1.2"
},
"scripts": {
"dev": "vite",
"build": "vite build"
"build": "tsc"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc