@hono/node-server
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -27,91 +27,2 @@ "use strict"; | ||
var import_node_crypto = __toESM(require("crypto")); | ||
// src/utils.ts | ||
var buildOutgoingHttpHeaders = (headers) => { | ||
const res = {}; | ||
const cookies = []; | ||
for (const [k, v] of headers) { | ||
if (k === "set-cookie") { | ||
cookies.push(v); | ||
} else { | ||
res[k] = v; | ||
} | ||
} | ||
if (cookies.length > 0) { | ||
res["set-cookie"] = cookies; | ||
} | ||
res["content-type"] ??= "text/plain;charset=UTF-8"; | ||
return res; | ||
}; | ||
// src/response.ts | ||
var responseCache = Symbol("responseCache"); | ||
var cacheKey = Symbol("cache"); | ||
var GlobalResponse = global.Response; | ||
var Response = class _Response { | ||
#body; | ||
#init; | ||
get cache() { | ||
delete this[cacheKey]; | ||
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init); | ||
} | ||
constructor(body, init) { | ||
this.#body = body; | ||
if (init instanceof _Response) { | ||
const cachedGlobalResponse = init[responseCache]; | ||
if (cachedGlobalResponse) { | ||
this.#init = cachedGlobalResponse; | ||
this.cache; | ||
return; | ||
} else { | ||
this.#init = init.#init; | ||
} | ||
} else { | ||
this.#init = init; | ||
} | ||
if (typeof body === "string" || body instanceof ReadableStream) { | ||
let headers = init?.headers || { "content-type": "text/plain;charset=UTF-8" }; | ||
if (headers instanceof Headers) { | ||
headers = buildOutgoingHttpHeaders(headers); | ||
} | ||
; | ||
this[cacheKey] = [init?.status || 200, body, headers]; | ||
} | ||
} | ||
}; | ||
[ | ||
"body", | ||
"bodyUsed", | ||
"headers", | ||
"ok", | ||
"redirected", | ||
"status", | ||
"statusText", | ||
"trailers", | ||
"type", | ||
"url" | ||
].forEach((k) => { | ||
Object.defineProperty(Response.prototype, k, { | ||
get() { | ||
return this.cache[k]; | ||
} | ||
}); | ||
}); | ||
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => { | ||
Object.defineProperty(Response.prototype, k, { | ||
value: function() { | ||
return this.cache[k](); | ||
} | ||
}); | ||
}); | ||
Object.setPrototypeOf(Response, GlobalResponse); | ||
Object.setPrototypeOf(Response.prototype, GlobalResponse.prototype); | ||
Object.defineProperty(global, "Response", { | ||
value: Response | ||
}); | ||
// src/globals.ts | ||
Object.defineProperty(global, "Response", { | ||
value: Response | ||
}); | ||
var webFetch = global.fetch; | ||
@@ -118,0 +29,0 @@ if (typeof global.crypto === "undefined") { |
export { createAdaptorServer, serve } from './server.js'; | ||
export { getRequestListener } from './listener.js'; | ||
export { Http2Bindings, HttpBindings } from './types.js'; | ||
import 'node:net'; | ||
import './types.js'; | ||
import 'node:http'; | ||
import 'node:http2'; | ||
import 'node:https'; |
@@ -45,2 +45,18 @@ "use strict"; | ||
var import_node_stream = require("stream"); | ||
var GlobalRequest = global.Request; | ||
var Request = class extends GlobalRequest { | ||
constructor(input, options) { | ||
if (typeof input === "object" && getRequestCache in input) { | ||
input = input[getRequestCache](); | ||
} | ||
if (options?.body instanceof ReadableStream) { | ||
; | ||
options.duplex = "half"; | ||
} | ||
super(input, options); | ||
} | ||
}; | ||
Object.defineProperty(global, "Request", { | ||
value: Request | ||
}); | ||
var newRequestFromIncoming = (method, url, incoming) => { | ||
@@ -62,3 +78,2 @@ const headerRecord = []; | ||
init.body = import_node_stream.Readable.toWeb(incoming); | ||
init.duplex = "half"; | ||
} | ||
@@ -113,3 +128,3 @@ return new Request(url, init); | ||
}); | ||
Object.setPrototypeOf(requestPrototype, global.Request.prototype); | ||
Object.setPrototypeOf(requestPrototype, Request.prototype); | ||
var newRequest = (incoming) => { | ||
@@ -143,4 +158,5 @@ const req = Object.create(requestPrototype); | ||
}); | ||
if (error) | ||
if (error) { | ||
writable.destroy(error); | ||
} | ||
} | ||
@@ -249,5 +265,2 @@ function onDrain() { | ||
var import_node_crypto = __toESM(require("crypto")); | ||
Object.defineProperty(global, "Response", { | ||
value: Response2 | ||
}); | ||
var webFetch = global.fetch; | ||
@@ -279,4 +292,5 @@ if (typeof global.crypto === "undefined") { | ||
console.error(e); | ||
if (!outgoing.headersSent) | ||
if (!outgoing.headersSent) { | ||
outgoing.writeHead(500, { "Content-Type": "text/plain" }); | ||
} | ||
outgoing.end(`Error: ${err.message}`); | ||
@@ -300,7 +314,6 @@ outgoing.destroy(err); | ||
var responseViaResponseObject = async (res, outgoing) => { | ||
if (res instanceof Promise) { | ||
res = await res.catch(handleFetchError); | ||
} | ||
res = res instanceof Promise ? await res.catch(handleFetchError) : res; | ||
try { | ||
if (cacheKey in res) { | ||
const isCached = cacheKey in res; | ||
if (isCached) { | ||
return responseViaCache(res, outgoing); | ||
@@ -314,4 +327,11 @@ } | ||
try { | ||
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant | ||
resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) { | ||
const { | ||
"transfer-encoding": transferEncoding, | ||
"content-encoding": contentEncoding, | ||
"content-length": contentLength, | ||
"x-accel-buffering": accelBuffering, | ||
"content-type": contentType | ||
} = resHeaderRecord; | ||
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant | ||
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) { | ||
outgoing.writeHead(res.status, resHeaderRecord); | ||
@@ -338,3 +358,3 @@ await writeFromReadableStream(res.body, outgoing); | ||
try { | ||
res = fetchCallback(req); | ||
res = fetchCallback(req, { incoming, outgoing }); | ||
if (cacheKey in res) { | ||
@@ -341,0 +361,0 @@ return responseViaCache(res, outgoing); |
@@ -40,2 +40,18 @@ "use strict"; | ||
var import_node_stream = require("stream"); | ||
var GlobalRequest = global.Request; | ||
var Request = class extends GlobalRequest { | ||
constructor(input, options) { | ||
if (typeof input === "object" && getRequestCache in input) { | ||
input = input[getRequestCache](); | ||
} | ||
if (options?.body instanceof ReadableStream) { | ||
; | ||
options.duplex = "half"; | ||
} | ||
super(input, options); | ||
} | ||
}; | ||
Object.defineProperty(global, "Request", { | ||
value: Request | ||
}); | ||
var newRequestFromIncoming = (method, url, incoming) => { | ||
@@ -57,3 +73,2 @@ const headerRecord = []; | ||
init.body = import_node_stream.Readable.toWeb(incoming); | ||
init.duplex = "half"; | ||
} | ||
@@ -108,3 +123,3 @@ return new Request(url, init); | ||
}); | ||
Object.setPrototypeOf(requestPrototype, global.Request.prototype); | ||
Object.setPrototypeOf(requestPrototype, Request.prototype); | ||
var newRequest = (incoming) => { | ||
@@ -138,4 +153,5 @@ const req = Object.create(requestPrototype); | ||
}); | ||
if (error) | ||
if (error) { | ||
writable.destroy(error); | ||
} | ||
} | ||
@@ -244,5 +260,2 @@ function onDrain() { | ||
var import_node_crypto = __toESM(require("crypto")); | ||
Object.defineProperty(global, "Response", { | ||
value: Response2 | ||
}); | ||
var webFetch = global.fetch; | ||
@@ -274,4 +287,5 @@ if (typeof global.crypto === "undefined") { | ||
console.error(e); | ||
if (!outgoing.headersSent) | ||
if (!outgoing.headersSent) { | ||
outgoing.writeHead(500, { "Content-Type": "text/plain" }); | ||
} | ||
outgoing.end(`Error: ${err.message}`); | ||
@@ -295,7 +309,6 @@ outgoing.destroy(err); | ||
var responseViaResponseObject = async (res, outgoing) => { | ||
if (res instanceof Promise) { | ||
res = await res.catch(handleFetchError); | ||
} | ||
res = res instanceof Promise ? await res.catch(handleFetchError) : res; | ||
try { | ||
if (cacheKey in res) { | ||
const isCached = cacheKey in res; | ||
if (isCached) { | ||
return responseViaCache(res, outgoing); | ||
@@ -309,4 +322,11 @@ } | ||
try { | ||
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant | ||
resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) { | ||
const { | ||
"transfer-encoding": transferEncoding, | ||
"content-encoding": contentEncoding, | ||
"content-length": contentLength, | ||
"x-accel-buffering": accelBuffering, | ||
"content-type": contentType | ||
} = resHeaderRecord; | ||
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant | ||
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) { | ||
outgoing.writeHead(res.status, resHeaderRecord); | ||
@@ -333,3 +353,3 @@ await writeFromReadableStream(res.body, outgoing); | ||
try { | ||
res = fetchCallback(req); | ||
res = fetchCallback(req, { incoming, outgoing }); | ||
if (cacheKey in res) { | ||
@@ -336,0 +356,0 @@ return responseViaCache(res, outgoing); |
import { IncomingMessage } from 'node:http'; | ||
import { Http2ServerRequest } from 'node:http2'; | ||
declare const GlobalRequest: { | ||
new (input: RequestInfo | URL, init?: RequestInit | undefined): globalThis.Request; | ||
prototype: globalThis.Request; | ||
}; | ||
declare class Request extends GlobalRequest { | ||
constructor(input: string | Request, options?: RequestInit); | ||
} | ||
declare const newRequest: (incoming: IncomingMessage | Http2ServerRequest) => any; | ||
export { newRequest }; | ||
export { GlobalRequest, Request, newRequest }; |
@@ -23,2 +23,4 @@ "use strict"; | ||
__export(request_exports, { | ||
GlobalRequest: () => GlobalRequest, | ||
Request: () => Request, | ||
newRequest: () => newRequest | ||
@@ -29,2 +31,18 @@ }); | ||
var import_node_stream = require("stream"); | ||
var GlobalRequest = global.Request; | ||
var Request = class extends GlobalRequest { | ||
constructor(input, options) { | ||
if (typeof input === "object" && getRequestCache in input) { | ||
input = input[getRequestCache](); | ||
} | ||
if (options?.body instanceof ReadableStream) { | ||
; | ||
options.duplex = "half"; | ||
} | ||
super(input, options); | ||
} | ||
}; | ||
Object.defineProperty(global, "Request", { | ||
value: Request | ||
}); | ||
var newRequestFromIncoming = (method, url, incoming) => { | ||
@@ -46,3 +64,2 @@ const headerRecord = []; | ||
init.body = import_node_stream.Readable.toWeb(incoming); | ||
init.duplex = "half"; | ||
} | ||
@@ -97,3 +114,3 @@ return new Request(url, init); | ||
}); | ||
Object.setPrototypeOf(requestPrototype, global.Request.prototype); | ||
Object.setPrototypeOf(requestPrototype, Request.prototype); | ||
var newRequest = (incoming) => { | ||
@@ -109,3 +126,5 @@ const req = Object.create(requestPrototype); | ||
0 && (module.exports = { | ||
GlobalRequest, | ||
Request, | ||
newRequest | ||
}); |
@@ -161,4 +161,5 @@ "use strict"; | ||
return async (c, next) => { | ||
if (c.finalized) | ||
if (c.finalized) { | ||
return next(); | ||
} | ||
const url = new URL(c.req.url); | ||
@@ -171,4 +172,5 @@ const filename = options.path ?? decodeURIComponent(url.pathname); | ||
}); | ||
if (!path) | ||
if (!path) { | ||
return next(); | ||
} | ||
path = `./${path}`; | ||
@@ -175,0 +177,0 @@ if (!(0, import_fs.existsSync)(path)) { |
@@ -42,2 +42,18 @@ "use strict"; | ||
var import_node_stream = require("stream"); | ||
var GlobalRequest = global.Request; | ||
var Request = class extends GlobalRequest { | ||
constructor(input, options) { | ||
if (typeof input === "object" && getRequestCache in input) { | ||
input = input[getRequestCache](); | ||
} | ||
if (options?.body instanceof ReadableStream) { | ||
; | ||
options.duplex = "half"; | ||
} | ||
super(input, options); | ||
} | ||
}; | ||
Object.defineProperty(global, "Request", { | ||
value: Request | ||
}); | ||
var newRequestFromIncoming = (method, url, incoming) => { | ||
@@ -59,3 +75,2 @@ const headerRecord = []; | ||
init.body = import_node_stream.Readable.toWeb(incoming); | ||
init.duplex = "half"; | ||
} | ||
@@ -110,3 +125,3 @@ return new Request(url, init); | ||
}); | ||
Object.setPrototypeOf(requestPrototype, global.Request.prototype); | ||
Object.setPrototypeOf(requestPrototype, Request.prototype); | ||
var newRequest = (incoming) => { | ||
@@ -140,4 +155,5 @@ const req = Object.create(requestPrototype); | ||
}); | ||
if (error) | ||
if (error) { | ||
writable.destroy(error); | ||
} | ||
} | ||
@@ -246,5 +262,2 @@ function onDrain() { | ||
var import_node_crypto = __toESM(require("crypto")); | ||
Object.defineProperty(global, "Response", { | ||
value: Response2 | ||
}); | ||
var webFetch = global.fetch; | ||
@@ -276,4 +289,5 @@ if (typeof global.crypto === "undefined") { | ||
console.error(e); | ||
if (!outgoing.headersSent) | ||
if (!outgoing.headersSent) { | ||
outgoing.writeHead(500, { "Content-Type": "text/plain" }); | ||
} | ||
outgoing.end(`Error: ${err.message}`); | ||
@@ -297,7 +311,6 @@ outgoing.destroy(err); | ||
var responseViaResponseObject = async (res, outgoing) => { | ||
if (res instanceof Promise) { | ||
res = await res.catch(handleFetchError); | ||
} | ||
res = res instanceof Promise ? await res.catch(handleFetchError) : res; | ||
try { | ||
if (cacheKey in res) { | ||
const isCached = cacheKey in res; | ||
if (isCached) { | ||
return responseViaCache(res, outgoing); | ||
@@ -311,4 +324,11 @@ } | ||
try { | ||
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant | ||
resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) { | ||
const { | ||
"transfer-encoding": transferEncoding, | ||
"content-encoding": contentEncoding, | ||
"content-length": contentLength, | ||
"x-accel-buffering": accelBuffering, | ||
"content-type": contentType | ||
} = resHeaderRecord; | ||
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant | ||
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) { | ||
outgoing.writeHead(res.status, resHeaderRecord); | ||
@@ -335,3 +355,3 @@ await writeFromReadableStream(res.body, outgoing); | ||
try { | ||
res = fetchCallback(req); | ||
res = fetchCallback(req, { incoming, outgoing }); | ||
if (cacheKey in res) { | ||
@@ -338,0 +358,0 @@ return responseViaCache(res, outgoing); |
@@ -1,6 +0,14 @@ | ||
import { Server, ServerOptions as ServerOptions$1, createServer } from 'node:http'; | ||
import { Http2Server, Http2SecureServer, ServerOptions as ServerOptions$3, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2'; | ||
import { IncomingMessage, ServerResponse, Server, ServerOptions as ServerOptions$1, createServer } from 'node:http'; | ||
import { Http2ServerRequest, Http2ServerResponse, Http2Server, Http2SecureServer, ServerOptions as ServerOptions$3, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2'; | ||
import { ServerOptions as ServerOptions$2, createServer as createServer$1 } from 'node:https'; | ||
type FetchCallback = (request: Request) => Promise<unknown> | unknown; | ||
type HttpBindings = { | ||
incoming: IncomingMessage; | ||
outgoing: ServerResponse; | ||
}; | ||
type Http2Bindings = { | ||
incoming: Http2ServerRequest; | ||
outgoing: Http2ServerResponse; | ||
}; | ||
type FetchCallback = (request: Request, env: HttpBindings | Http2Bindings) => Promise<unknown> | unknown; | ||
type NextHandlerOption = { | ||
@@ -33,2 +41,2 @@ fetch: FetchCallback; | ||
export { FetchCallback, NextHandlerOption, Options, ServerType }; | ||
export { FetchCallback, Http2Bindings, HttpBindings, NextHandlerOption, Options, ServerType }; |
@@ -45,4 +45,5 @@ "use strict"; | ||
}); | ||
if (error) | ||
if (error) { | ||
writable.destroy(error); | ||
} | ||
} | ||
@@ -49,0 +50,0 @@ function onDrain() { |
@@ -40,2 +40,18 @@ "use strict"; | ||
var import_node_stream = require("stream"); | ||
var GlobalRequest = global.Request; | ||
var Request = class extends GlobalRequest { | ||
constructor(input, options) { | ||
if (typeof input === "object" && getRequestCache in input) { | ||
input = input[getRequestCache](); | ||
} | ||
if (options?.body instanceof ReadableStream) { | ||
; | ||
options.duplex = "half"; | ||
} | ||
super(input, options); | ||
} | ||
}; | ||
Object.defineProperty(global, "Request", { | ||
value: Request | ||
}); | ||
var newRequestFromIncoming = (method, url, incoming) => { | ||
@@ -57,3 +73,2 @@ const headerRecord = []; | ||
init.body = import_node_stream.Readable.toWeb(incoming); | ||
init.duplex = "half"; | ||
} | ||
@@ -108,3 +123,3 @@ return new Request(url, init); | ||
}); | ||
Object.setPrototypeOf(requestPrototype, global.Request.prototype); | ||
Object.setPrototypeOf(requestPrototype, Request.prototype); | ||
var newRequest = (incoming) => { | ||
@@ -138,4 +153,5 @@ const req = Object.create(requestPrototype); | ||
}); | ||
if (error) | ||
if (error) { | ||
writable.destroy(error); | ||
} | ||
} | ||
@@ -244,5 +260,2 @@ function onDrain() { | ||
var import_node_crypto = __toESM(require("crypto")); | ||
Object.defineProperty(global, "Response", { | ||
value: Response2 | ||
}); | ||
var webFetch = global.fetch; | ||
@@ -274,4 +287,5 @@ if (typeof global.crypto === "undefined") { | ||
console.error(e); | ||
if (!outgoing.headersSent) | ||
if (!outgoing.headersSent) { | ||
outgoing.writeHead(500, { "Content-Type": "text/plain" }); | ||
} | ||
outgoing.end(`Error: ${err.message}`); | ||
@@ -295,7 +309,6 @@ outgoing.destroy(err); | ||
var responseViaResponseObject = async (res, outgoing) => { | ||
if (res instanceof Promise) { | ||
res = await res.catch(handleFetchError); | ||
} | ||
res = res instanceof Promise ? await res.catch(handleFetchError) : res; | ||
try { | ||
if (cacheKey in res) { | ||
const isCached = cacheKey in res; | ||
if (isCached) { | ||
return responseViaCache(res, outgoing); | ||
@@ -309,4 +322,11 @@ } | ||
try { | ||
if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant | ||
resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) { | ||
const { | ||
"transfer-encoding": transferEncoding, | ||
"content-encoding": contentEncoding, | ||
"content-length": contentLength, | ||
"x-accel-buffering": accelBuffering, | ||
"content-type": contentType | ||
} = resHeaderRecord; | ||
if (transferEncoding || contentEncoding || contentLength || // nginx buffering variant | ||
accelBuffering && regBuffer.test(accelBuffering) || !regContentType.test(contentType)) { | ||
outgoing.writeHead(res.status, resHeaderRecord); | ||
@@ -333,3 +353,3 @@ await writeFromReadableStream(res.body, outgoing); | ||
try { | ||
res = fetchCallback(req); | ||
res = fetchCallback(req, { incoming, outgoing }); | ||
if (cacheKey in res) { | ||
@@ -336,0 +356,0 @@ return responseViaCache(res, outgoing); |
{ | ||
"name": "@hono/node-server", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Node.js Adapter for Hono", | ||
@@ -48,3 +48,5 @@ "main": "dist/index.js", | ||
"lint": "eslint --ext js,ts src test", | ||
"lint:fix": "eslint --ext js,ts src test --fix" | ||
"lint:fix": "eslint --ext js,ts src test --fix", | ||
"format": "prettier --check \"src/**/*.{js,ts}\" \"test/**/*.{js,ts}\"", | ||
"format:fix": "prettier --write \"src/**/*.{js,ts}\" \"test/**/*.{js,ts}\"" | ||
}, | ||
@@ -66,3 +68,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@hono/eslint-config": "^0.0.2", | ||
"@hono/eslint-config": "^0.0.4", | ||
"@types/jest": "^29.5.3", | ||
@@ -76,2 +78,3 @@ "@types/node": "^20.10.0", | ||
"np": "^7.7.0", | ||
"prettier": "^3.2.4", | ||
"publint": "^0.1.16", | ||
@@ -78,0 +81,0 @@ "supertest": "^6.3.3", |
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
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
125941
3937
15