1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 |
x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x2 x1 x1 x1 x1 x2 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 x1 |
/**
* Provides user-friendly {@linkcode serve} on top of Deno's native HTTP server
* and other utilities for creating HTTP servers and clients.
*
* ## File Server
*
* A small program for serving local files over HTTP.
*
* ```sh
* deno run --allow-net --allow-read jsr:@std/http/file-server
* Listening on:
* - Local: http://localhost:8000
* ```
*
* When the `--allow-sys=networkInterfaces` permission is provided, the file
* server will also display the local area network addresses that can be used to
* access the server.
*
* ## HTTP Status Code and Status Text
*
* Helper for processing status code and status text.
*
* ## HTTP errors
*
* Provides error classes for each HTTP error status code as well as utility
* functions for handling HTTP errors in a structured way.
*
* ## Methods
*
* Provides helper functions and types to work with HTTP method strings safely.
*
* ## Negotiation
*
* A set of functions which can be used to negotiate content types, encodings and
* languages when responding to requests.
*
* > Note: some libraries include accept charset functionality by analyzing the
* > `Accept-Charset` header. This is a legacy header that
* > {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Charset | clients omit and servers should ignore}
* > therefore is not provided.
*
* ## User agent handling
*
* The {@linkcode UserAgent} class provides user agent string parsing, allowing
* a user agent flag to be semantically understood.
*
* For example to integrate the user agent provided in the header `User-Agent`
* in an http request would look like this:
*
* ```ts ignore
* import { UserAgent } from "@std/http/user-agent";
*
* Deno.serve((req) => {
* const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
* return new Response(`Hello, ${userAgent.browser.name}
* on ${userAgent.os.name} ${userAgent.os.version}!`);
* });
* ```
*
* ### Routing
*
* {@linkcode route} provides an easy way to route requests to different
* handlers based on the request path and method.
*
* ```ts ignore
* import { route, type Route } from "@std/http/unstable-route";
* import { serveDir } from "@std/http/file-server";
*
* const routes: Route[] = [
* {
* pattern: new URLPattern({ pathname: "/about" }),
* handler: () => new Response("About page"),
* },
* {
* pattern: new URLPattern({ pathname: "/users/:id" }),
* handler: (_req, _info, params) => new Response(params?.pathname.groups.id),
* },
* {
* pattern: new URLPattern({ pathname: "/static/*" }),
* handler: (req: Request) => serveDir(req)
* }
* ];
*
* function defaultHandler(_req: Request) {
* return new Response("Not found", { status: 404 });
* }
*
* Deno.serve(route(routes, defaultHandler));
* ```
*
* @module
*/
import type { Cookie as _interface_Cookie } from "jsr:@std/[email protected]"
/**
* Represents an HTTP Cookie.
*
* @see {@link https://www.rfc-editor.org/rfc/rfc6265.html#section-4.2.1}
*/
interface Cookie extends _interface_Cookie {}
export type { Cookie }
import { getCookies as _function_getCookies } from "jsr:@std/[email protected]"
/**
* Parse cookies of a header
*
* @example Usage
* ```ts
* import { getCookies } from "@std/http/cookie";
* import { assertEquals } from "@std/assert";
*
* const headers = new Headers();
* headers.set("Cookie", "full=of; tasty=chocolate");
*
* const cookies = getCookies(headers);
* assertEquals(cookies, { full: "of", tasty: "chocolate" });
* ```
*
* @param headers The headers instance to get cookies from
* @return Object with cookie names as keys
*/
const getCookies = _function_getCookies as typeof _function_getCookies
export { getCookies }
import { setCookie as _function_setCookie } from "jsr:@std/[email protected]"
/**
* Set the cookie header properly in the headers
*
* @example Usage
* ```ts
* import { Cookie, setCookie } from "@std/http/cookie";
* import { assertEquals } from "@std/assert";
*
* const headers = new Headers();
* const cookie: Cookie = { name: "Space", value: "Cat" };
* setCookie(headers, cookie);
*
* const cookieHeader = headers.get("set-cookie");
*
* assertEquals(cookieHeader, "Space=Cat");
* ```
*
* @param headers The headers instance to set the cookie to
* @param cookie Cookie to set
*/
const setCookie = _function_setCookie as typeof _function_setCookie
export { setCookie }
import { deleteCookie as _function_deleteCookie } from "jsr:@std/[email protected]"
/**
* Set the cookie header with empty value in the headers to delete it.
*
* The attributes (`path`, `domain`, `secure`, `httpOnly`, `partitioned`) need
* to match the values when the cookie was set.
*
* > Note: Deleting a `Cookie` will set its expiration date before now. Forcing
* > the browser to delete it.
*
* @example Usage
* ```ts
* import { deleteCookie } from "@std/http/cookie";
* import { assertEquals } from "@std/assert";
*
* const headers = new Headers();
* deleteCookie(headers, "deno");
*
* const cookieHeader = headers.get("set-cookie");
*
* assertEquals(cookieHeader, "deno=; Expires=Thu, 01 Jan 1970 00:00:00 GMT");
* ```
*
* @param headers The headers instance to delete the cookie from
* @param name Name of cookie
* @param attributes Additional cookie attributes
*/
const deleteCookie = _function_deleteCookie as typeof _function_deleteCookie
export { deleteCookie }
import { getSetCookies as _function_getSetCookies } from "jsr:@std/[email protected]"
/**
* Parse set-cookies of a header
*
* @example Usage
* ```ts
* import { getSetCookies } from "@std/http/cookie";
* import { assertEquals } from "@std/assert";
*
* const headers = new Headers([
* ["Set-Cookie", "lulu=meow; Secure; Max-Age=3600"],
* ["Set-Cookie", "booya=kasha; HttpOnly; Path=/"],
* ]);
*
* const cookies = getSetCookies(headers);
*
* assertEquals(cookies[0], {
* name: "lulu",
* value: "meow",
* secure: true,
* maxAge: 3600
* });
* ```
*
* @param headers The headers instance to get set-cookies from
* @return List of cookies
*/
const getSetCookies = _function_getSetCookies as typeof _function_getSetCookies
export { getSetCookies }
import type { FileInfo as _interface_FileInfo } from "jsr:@std/[email protected]"
/**
* Just the part of {@linkcode Deno.FileInfo} that is required to calculate an `ETag`,
* so partial or user generated file information can be passed.
*/
interface FileInfo extends _interface_FileInfo {}
export type { FileInfo }
import type { ETagOptions as _interface_ETagOptions } from "jsr:@std/[email protected]"
/**
* Options for {@linkcode eTag}.
*/
interface ETagOptions extends _interface_ETagOptions {}
export type { ETagOptions }
import { eTag as _function_eTag } from "jsr:@std/[email protected]"
/** UNDOCUMENTED */
const eTag = _function_eTag as typeof _function_eTag
export { eTag }
import { ifMatch as _function_ifMatch } from "jsr:@std/[email protected]"
/**
* A helper function that takes the value from the `If-Match` header and a
* calculated etag for the target. By using strong comparison, return `true` if
* the values match, otherwise `false`.
*
* See MDN's [`If-Match`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match)
* article for more information on how to use this function.
*
* @example Usage
* ```ts ignore
* import {
* eTag,
* ifMatch,
* } from "@std/http/etag";
* import { assert } from "@std/assert";
*
* const body = "hello deno!";
*
* Deno.serve(async (req) => {
* const ifMatchValue = req.headers.get("if-match");
* const etag = await eTag(body);
* assert(etag);
* if (!ifMatchValue || ifMatch(ifMatchValue, etag)) {
* return new Response(body, { status: 200, headers: { etag } });
* } else {
* return new Response(null, { status: 412, statusText: "Precondition Failed"});
* }
* });
* ```
*
* @param value the If-Match header value.
* @param etag the ETag to check against.
* @return whether or not the parameters match.
*/
const ifMatch = _function_ifMatch as typeof _function_ifMatch
export { ifMatch }
import { ifNoneMatch as _function_ifNoneMatch } from "jsr:@std/[email protected]"
/**
* A helper function that takes the value from the `If-None-Match` header and
* a calculated etag for the target entity and returns `false` if the etag for
* the entity matches the supplied value, otherwise `true`.
*
* See MDN's [`If-None-Match`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match)
* article for more information on how to use this function.
*
* @example Usage
* ```ts ignore
* import {
* eTag,
* ifNoneMatch,
* } from "@std/http/etag";
* import { assert } from "@std/assert";
*
* const body = "hello deno!";
*
* Deno.serve(async (req) => {
* const ifNoneMatchValue = req.headers.get("if-none-match");
* const etag = await eTag(body);
* assert(etag);
* if (!ifNoneMatch(ifNoneMatchValue, etag)) {
* return new Response(null, { status: 304, headers: { etag } });
* } else {
* return new Response(body, { status: 200, headers: { etag } });
* }
* });
* ```
*
* @param value the If-None-Match header value.
* @param etag the ETag to check against.
* @return whether or not the parameters do not match.
*/
const ifNoneMatch = _function_ifNoneMatch as typeof _function_ifNoneMatch
export { ifNoneMatch }
import { STATUS_CODE as _variable_STATUS_CODE } from "jsr:@std/[email protected]"
/**
* Contains the {@linkcode STATUS_CODE} object which contains standard HTTP
* status codes and provides several type guards for handling status codes
* with type safety.
*
* @example The status code and status text
* ```ts
* import {
* STATUS_CODE,
* STATUS_TEXT,
* } from "@std/http/status";
*
* console.log(STATUS_CODE.NotFound); // Returns 404
* console.log(STATUS_TEXT[STATUS_CODE.NotFound]); // Returns "Not Found"
* ```
*
* @example Checking the status code type
* ```ts
* import { isErrorStatus } from "@std/http/status";
*
* const res = await fetch("https://example.com/");
*
* if (isErrorStatus(res.status)) {
* // error handling here...
* }
*
* await res.body?.cancel();
* ```
*
* @module
*/
const STATUS_CODE = _variable_STATUS_CODE as typeof _variable_STATUS_CODE
export { STATUS_CODE }
import type { StatusCode as _typeAlias_StatusCode } from "jsr:@std/[email protected]"
/**
* An HTTP status code.
*/
type StatusCode = _typeAlias_StatusCode
export type { StatusCode }
import { STATUS_TEXT as _variable_STATUS_TEXT } from "jsr:@std/[email protected]"
/**
* A record of all the status codes text.
*/
const STATUS_TEXT = _variable_STATUS_TEXT as typeof _variable_STATUS_TEXT
export { STATUS_TEXT }
import type { StatusText as _typeAlias_StatusText } from "jsr:@std/[email protected]"
/**
* An HTTP status text.
*/
type StatusText = _typeAlias_StatusText
export type { StatusText }
import type { InformationalStatus as _typeAlias_InformationalStatus } from "jsr:@std/[email protected]"
/**
* An HTTP status that is a informational (1XX).
*/
type InformationalStatus = _typeAlias_InformationalStatus
export type { InformationalStatus }
import type { SuccessfulStatus as _typeAlias_SuccessfulStatus } from "jsr:@std/[email protected]"
/**
* An HTTP status that is a success (2XX).
*/
type SuccessfulStatus = _typeAlias_SuccessfulStatus
export type { SuccessfulStatus }
import type { RedirectStatus as _typeAlias_RedirectStatus } from "jsr:@std/[email protected]"
/**
* An HTTP status that is a redirect (3XX).
*/
type RedirectStatus = _typeAlias_RedirectStatus
export type { RedirectStatus }
import type { ClientErrorStatus as _typeAlias_ClientErrorStatus } from "jsr:@std/[email protected]"
/**
* An HTTP status that is a client error (4XX).
*/
type ClientErrorStatus = _typeAlias_ClientErrorStatus
export type { ClientErrorStatus }
import type { ServerErrorStatus as _typeAlias_ServerErrorStatus } from "jsr:@std/[email protected]"
/**
* An HTTP status that is a server error (5XX).
*/
type ServerErrorStatus = _typeAlias_ServerErrorStatus
export type { ServerErrorStatus }
import type { ErrorStatus as _typeAlias_ErrorStatus } from "jsr:@std/[email protected]"
/**
* An HTTP status that is an error (4XX and 5XX).
*/
type ErrorStatus = _typeAlias_ErrorStatus
export type { ErrorStatus }
import { isStatus as _function_isStatus } from "jsr:@std/[email protected]"
/**
* Returns whether the provided number is a valid HTTP status code.
*
* @example Usage
* ```ts
* import { isStatus } from "@std/http/status";
* import { assert } from "@std/assert";
*
* assert(isStatus(404));
* ```
*
* @param status The status to assert against.
* @return Whether or not the provided status is a valid status code.
*/
const isStatus = _function_isStatus as typeof _function_isStatus
export { isStatus }
import { isInformationalStatus as _function_isInformationalStatus } from "jsr:@std/[email protected]"
/**
* A type guard that determines if the status code is informational.
*
* @example Usage
* ```ts
* import { isInformationalStatus } from "@std/http/status";
* import { assert } from "@std/assert";
*
* assert(isInformationalStatus(100));
* ```
*
* @param status The status to assert against.
* @return Whether or not the provided status is an informational status code.
*/
const isInformationalStatus = _function_isInformationalStatus as typeof _function_isInformationalStatus
export { isInformationalStatus }
import { isSuccessfulStatus as _function_isSuccessfulStatus } from "jsr:@std/[email protected]"
/**
* A type guard that determines if the status code is successful.
*
* @example Usage
* ```ts
* import { isSuccessfulStatus } from "@std/http/status";
* import { assert } from "@std/assert";
*
* assert(isSuccessfulStatus(200));
* ```
*
* @param status The status to assert against.
* @return Whether or not the provided status is a successful status code.
*/
const isSuccessfulStatus = _function_isSuccessfulStatus as typeof _function_isSuccessfulStatus
export { isSuccessfulStatus }
import { isRedirectStatus as _function_isRedirectStatus } from "jsr:@std/[email protected]"
/**
* A type guard that determines if the status code is a redirection.
*
* @example Usage
* ```ts
* import { isRedirectStatus } from "@std/http/status";
* import { assert } from "@std/assert";
*
* assert(isRedirectStatus(302));
* ```
*
* @param status The status to assert against.
* @return Whether or not the provided status is a redirect status code.
*/
const isRedirectStatus = _function_isRedirectStatus as typeof _function_isRedirectStatus
export { isRedirectStatus }
import { isClientErrorStatus as _function_isClientErrorStatus } from "jsr:@std/[email protected]"
/**
* A type guard that determines if the status code is a client error.
*
* @example Usage
* ```ts
* import { isClientErrorStatus } from "@std/http/status";
* import { assert } from "@std/assert";
*
* assert(isClientErrorStatus(404));
* ```
*
* @param status The status to assert against.
* @return Whether or not the provided status is a client error status code.
*/
const isClientErrorStatus = _function_isClientErrorStatus as typeof _function_isClientErrorStatus
export { isClientErrorStatus }
import { isServerErrorStatus as _function_isServerErrorStatus } from "jsr:@std/[email protected]"
/**
* A type guard that determines if the status code is a server error.
*
* @example Usage
* ```ts
* import { isServerErrorStatus } from "@std/http/status";
* import { assert } from "@std/assert";
*
* assert(isServerErrorStatus(502));
* ```
*
* @param status The status to assert against.
* @return Whether or not the provided status is a server error status code.
*/
const isServerErrorStatus = _function_isServerErrorStatus as typeof _function_isServerErrorStatus
export { isServerErrorStatus }
import { isErrorStatus as _function_isErrorStatus } from "jsr:@std/[email protected]"
/**
* A type guard that determines if the status code is an error.
*
* @example Usage
* ```ts
* import { isErrorStatus } from "@std/http/status";
* import { assert } from "@std/assert";
*
* assert(isErrorStatus(502));
* ```
*
* @param status The status to assert against.
* @return Whether or not the provided status is an error status code.
*/
const isErrorStatus = _function_isErrorStatus as typeof _function_isErrorStatus
export { isErrorStatus }
import { accepts as _function_accepts } from "jsr:@std/[email protected]"
/** UNDOCUMENTED */
const accepts = _function_accepts as typeof _function_accepts
export { accepts }
import { acceptsEncodings as _function_acceptsEncodings } from "jsr:@std/[email protected]"
/** UNDOCUMENTED */
const acceptsEncodings = _function_acceptsEncodings as typeof _function_acceptsEncodings
export { acceptsEncodings }
import { acceptsLanguages as _function_acceptsLanguages } from "jsr:@std/[email protected]"
/** UNDOCUMENTED */
const acceptsLanguages = _function_acceptsLanguages as typeof _function_acceptsLanguages
export { acceptsLanguages }
import type { ServerSentEventMessage as _interface_ServerSentEventMessage } from "jsr:@std/[email protected]"
/**
* Represents a message in the Server-Sent Event (SSE) protocol.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#fields}
*/
interface ServerSentEventMessage extends _interface_ServerSentEventMessage {}
export type { ServerSentEventMessage }
import { ServerSentEventStream as _class_ServerSentEventStream } from "jsr:@std/[email protected]"
/**
* Transforms server-sent message objects into strings for the client.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events}
*
* @example Usage
* ```ts no-assert
* import {
* type ServerSentEventMessage,
* ServerSentEventStream,
* } from "@std/http/server-sent-event-stream";
*
* const stream = ReadableStream.from<ServerSentEventMessage>([
* { data: "hello there" }
* ]).pipeThrough(new ServerSentEventStream());
* new Response(stream, {
* headers: {
* "content-type": "text/event-stream",
* "cache-control": "no-cache",
* },
* });
* ```
*/
class ServerSentEventStream extends _class_ServerSentEventStream {}
export { ServerSentEventStream }
import type { Browser as _interface_Browser } from "jsr:@std/[email protected]"
/**
* The browser as described by a user agent string.
*/
interface Browser extends _interface_Browser {}
export type { Browser }
import type { Device as _interface_Device } from "jsr:@std/[email protected]"
/**
* The device as described by a user agent string.
*/
interface Device extends _interface_Device {}
export type { Device }
import type { Engine as _interface_Engine } from "jsr:@std/[email protected]"
/**
* The browser engine as described by a user agent string.
*/
interface Engine extends _interface_Engine {}
export type { Engine }
import type { Os as _interface_Os } from "jsr:@std/[email protected]"
/**
* The OS as described by a user agent string.
*/
interface Os extends _interface_Os {}
export type { Os }
import type { Cpu as _interface_Cpu } from "jsr:@std/[email protected]"
/**
* The CPU information as described by a user agent string.
*/
interface Cpu extends _interface_Cpu {}
export type { Cpu }
import { UserAgent as _class_UserAgent } from "jsr:@std/[email protected]"
/**
* A representation of user agent string, which can be used to determine
* environmental information represented by the string. All properties are
* determined lazily.
*
* @example Usage
* ```ts ignore
* import { UserAgent } from "@std/http/user-agent";
*
* Deno.serve((req) => {
* const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
* return new Response(`Hello, ${userAgent.browser.name}
* on ${userAgent.os.name} ${userAgent.os.version}!`);
* });
* ```
*/
class UserAgent extends _class_UserAgent {}
export { UserAgent }
import type { ServeFileOptions as _interface_ServeFileOptions } from "jsr:@std/[email protected]"
/**
* Options for {@linkcode serveFile}.
*/
interface ServeFileOptions extends _interface_ServeFileOptions {}
export type { ServeFileOptions }
import { serveFile as _function_serveFile } from "jsr:@std/[email protected]"
/**
* Resolves a {@linkcode Response} with the requested file as the body.
*
* @example Usage
* ```ts ignore
* import { serveFile } from "@std/http/file-server";
*
* Deno.serve((req) => {
* return serveFile(req, "README.md");
* });
* ```
*
* @param req The server request context used to cleanup the file handle.
* @param filePath Path of the file to serve.
* @param options Additional options.
* @return A response for the request.
*/
const serveFile = _function_serveFile as typeof _function_serveFile
export { serveFile }
import type { ServeDirOptions as _interface_ServeDirOptions } from "jsr:@std/[email protected]"
/**
* Interface for serveDir options.
*/
interface ServeDirOptions extends _interface_ServeDirOptions {}
export type { ServeDirOptions }
import { serveDir as _function_serveDir } from "jsr:@std/[email protected]"
/**
* Serves the files under the given directory root (opts.fsRoot).
*
* @example Usage
* ```ts ignore
* import { serveDir } from "@std/http/file-server";
*
* Deno.serve((req) => {
* const pathname = new URL(req.url).pathname;
* if (pathname.startsWith("/static")) {
* return serveDir(req, {
* fsRoot: "path/to/static/files/dir",
* });
* }
* // Do dynamic responses
* return new Response();
* });
* ```
*
* @example Changing the URL root
*
* Requests to `/static/path/to/file` will be served from `./public/path/to/file`.
*
* ```ts ignore
* import { serveDir } from "@std/http/file-server";
*
* Deno.serve((req) => serveDir(req, {
* fsRoot: "public",
* urlRoot: "static",
* }));
* ```
*
* @param req The request to handle
* @param opts Additional options.
* @return A response for the request.
*/
const serveDir = _function_serveDir as typeof _function_serveDir
export { serveDir }
import type { Handler as _typeAlias_Handler } from "jsr:@std/[email protected]"
/**
* Request handler for {@linkcode Route}.
*
* @experimental
* @param request Request
* @param info Request info
* @param params URL pattern result
*/
type Handler = _typeAlias_Handler
export type { Handler }
import type { Route as _interface_Route } from "jsr:@std/[email protected]"
/**
* Route configuration for {@linkcode route}.
*
* @experimental
*/
interface Route extends _interface_Route {}
export type { Route }
import { route as _function_route } from "jsr:@std/[email protected]"
/**
* Routes requests to different handlers based on the request path and method.
*
* @experimental
* @example Usage
* ```ts ignore
* import { route, type Route } from "@std/http/unstable-route";
* import { serveDir } from "@std/http/file-server";
*
* const routes: Route[] = [
* {
* pattern: new URLPattern({ pathname: "/about" }),
* handler: () => new Response("About page"),
* },
* {
* pattern: new URLPattern({ pathname: "/users/:id" }),
* handler: (_req, _info, params) => new Response(params?.pathname.groups.id),
* },
* {
* pattern: new URLPattern({ pathname: "/static/*" }),
* handler: (req: Request) => serveDir(req)
* },
* {
* pattern: new URLPattern({ pathname: "/api" }),
* method: ["GET", "HEAD"],
* handler: (req: Request) => new Response(req.method === 'HEAD' ? null : 'ok'),
* },
* ];
*
* function defaultHandler(_req: Request) {
* return new Response("Not found", { status: 404 });
* }
*
* Deno.serve(route(routes, defaultHandler));
* ```
*
* @param routes Route configurations
* @param defaultHandler Default request handler that's returned when no route
* matches the given request. Serving HTTP 404 Not Found or 405 Method Not
* Allowed response can be done in this function.
* @return Request handler
*/
const route = _function_route as typeof _function_route
export { route }
|