mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 08:09:08 -05:00
refactor(flash): move remoteAddr to options bag (#17913)
Applies suggestion from #17912
This commit is contained in:
parent
9aebc8bc19
commit
484b6fe2fa
3 changed files with 29 additions and 15 deletions
|
@ -89,14 +89,13 @@ Deno.test({ permissions: { net: true } }, async function httpServerBasic() {
|
||||||
const listeningPromise = deferred();
|
const listeningPromise = deferred();
|
||||||
|
|
||||||
const server = Deno.serve({
|
const server = Deno.serve({
|
||||||
handler: async (request, getRemoteAddr) => {
|
handler: async (request, { remoteAddr }) => {
|
||||||
// FIXME(bartlomieju):
|
// FIXME(bartlomieju):
|
||||||
// make sure that request can be inspected
|
// make sure that request can be inspected
|
||||||
console.log(request);
|
console.log(request);
|
||||||
assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/");
|
assertEquals(new URL(request.url).href, "http://127.0.0.1:4501/");
|
||||||
assertEquals(await request.text(), "");
|
assertEquals(await request.text(), "");
|
||||||
const addr = getRemoteAddr();
|
assertEquals(remoteAddr.hostname, "127.0.0.1");
|
||||||
assertEquals(addr.hostname, "127.0.0.1");
|
|
||||||
promise.resolve();
|
promise.resolve();
|
||||||
return new Response("Hello World", { headers: { "foo": "bar" } });
|
return new Response("Hello World", { headers: { "foo": "bar" } });
|
||||||
},
|
},
|
||||||
|
|
18
cli/tsc/dts/lib.deno.unstable.d.ts
vendored
18
cli/tsc/dts/lib.deno.unstable.d.ts
vendored
|
@ -514,7 +514,7 @@ declare namespace Deno {
|
||||||
* All `UnsafeCallback` are always thread safe in that they can be called from
|
* All `UnsafeCallback` are always thread safe in that they can be called from
|
||||||
* foreign threads without crashing. However, they do not wake up the Deno event
|
* foreign threads without crashing. However, they do not wake up the Deno event
|
||||||
* loop by default.
|
* loop by default.
|
||||||
*
|
*
|
||||||
* If a callback is to be called from foreign threads, use the `threadSafe()`
|
* If a callback is to be called from foreign threads, use the `threadSafe()`
|
||||||
* static constructor or explicitly call `ref()` to have the callback wake up
|
* static constructor or explicitly call `ref()` to have the callback wake up
|
||||||
* the Deno event loop when called from foreign threads. This also stops
|
* the Deno event loop when called from foreign threads. This also stops
|
||||||
|
@ -580,7 +580,7 @@ declare namespace Deno {
|
||||||
/**
|
/**
|
||||||
* Decrements the callback's reference counting and returns the new
|
* Decrements the callback's reference counting and returns the new
|
||||||
* reference count.
|
* reference count.
|
||||||
*
|
*
|
||||||
* Calling `unref()` does not stop a callback from waking up the Deno
|
* Calling `unref()` does not stop a callback from waking up the Deno
|
||||||
* event loop when called from foreign threads.
|
* event loop when called from foreign threads.
|
||||||
*
|
*
|
||||||
|
@ -1163,6 +1163,18 @@ declare namespace Deno {
|
||||||
*/
|
*/
|
||||||
export function funlockSync(rid: number): void;
|
export function funlockSync(rid: number): void;
|
||||||
|
|
||||||
|
/** **UNSTABLE**: New API, yet to be vetted.
|
||||||
|
*
|
||||||
|
* Information for a HTTP request.
|
||||||
|
*
|
||||||
|
* @category HTTP Server
|
||||||
|
*/
|
||||||
|
export interface ServeHandlerInfo {
|
||||||
|
/** The remote address of the connection. */
|
||||||
|
remoteAddr: Deno.NetAddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** **UNSTABLE**: New API, yet to be vetted.
|
/** **UNSTABLE**: New API, yet to be vetted.
|
||||||
*
|
*
|
||||||
* A handler for HTTP requests. Consumes a request and returns a response.
|
* A handler for HTTP requests. Consumes a request and returns a response.
|
||||||
|
@ -1173,7 +1185,7 @@ declare namespace Deno {
|
||||||
*
|
*
|
||||||
* @category HTTP Server
|
* @category HTTP Server
|
||||||
*/
|
*/
|
||||||
export type ServeHandler = (request: Request, getRemoteAddr: () => Deno.NetAddr) => Response | Promise<Response>;
|
export type ServeHandler = (request: Request, info: ServeHandlerInfo) => Response | Promise<Response>;
|
||||||
|
|
||||||
/** **UNSTABLE**: New API, yet to be vetted.
|
/** **UNSTABLE**: New API, yet to be vetted.
|
||||||
*
|
*
|
||||||
|
|
|
@ -579,16 +579,19 @@ function createServe(opFn) {
|
||||||
);
|
);
|
||||||
|
|
||||||
let resp;
|
let resp;
|
||||||
|
let remoteAddr;
|
||||||
try {
|
try {
|
||||||
resp = handler(req, () => {
|
resp = handler(req, {
|
||||||
const { 0: hostname, 1: port } = core.ops.op_flash_addr(
|
get remoteAddr() {
|
||||||
serverId,
|
if (!remoteAddr) {
|
||||||
i,
|
const { 0: hostname, 1: port } = core.ops.op_flash_addr(
|
||||||
);
|
serverId,
|
||||||
return {
|
i,
|
||||||
hostname,
|
);
|
||||||
port,
|
remoteAddr = { hostname, port };
|
||||||
};
|
}
|
||||||
|
return remoteAddr;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
if (ObjectPrototypeIsPrototypeOf(PromisePrototype, resp)) {
|
if (ObjectPrototypeIsPrototypeOf(PromisePrototype, resp)) {
|
||||||
PromisePrototypeCatch(
|
PromisePrototypeCatch(
|
||||||
|
|
Loading…
Reference in a new issue