Skip to main content
Module

x/keywork/mod.ts

A library for building V8 Isolate web apps on Cloudflare Workers, Deno, and Node.JS
Latest
import * as keywork from "https://deno.land/x/keywork@v6.2.1/mod.ts";

Keywork is a modular and opinionated library, providing structured guidence as your web app grows, without locking you into a specific pattern.

Features are categorized into modules that can be imported directly:

// highlight-next-line
import { RequestRouter } from 'keywork/router'

const app = new RequestRouter()

app.get('/', () => 'Hello there! 👋')

export default app
// highlight-next-line
import { RequestRouter } from 'https://deno.land/x/keywork/modules/router/mod.ts'
import { serve } from 'https://deno.land/std@0.140.0/http/server.ts'

const app = new RequestRouter()
serve((request) => app.fetch(request))
// highlight-next-line
import { RequestRouter } from 'https://esm.sh/keywork/router'

const app = new RequestRouter()

app.get('/', () => 'Hello there! 👋')

:::tip

While Keywork has many modules, modern bundlers and transpilers such as ESBuild will intelligently bundle only the Keywork modules you include in your web app.

A full list of Keywork's modules are available in the navigation menu, however the most popular are shown below.

::

Namespaces

Keywork includes error utilities that pair nicely with HTTP request handlers.

Keywork uses the JavaScript Events API internally to handle HTTP requests.

Keywork includes utilities for working with files, such as determining the MIME type while handling an incoming HTTP request.

Keywork includes utilities for working with incoming HTTP requests, and extends the native Request class for use with Cloudflare Workers

Keywork includes utilities for generating IDs.

Middleware are instances of the Keywork#Router.RequestRouter | RequestRouter, each with single responsibility e.g. (users, blog posts, payments, authentication, etc.)

While optional, Keywork uses React as its primary HTML templating engine.

Designed with familiarity in mind, the server-side routing API is inspired by Express.js, React Router, and the native Cloudflare Workers platform.

Keywork includes promise-based utilities for working with the DOM.

Keywork uses JavaScript's built-in URL Pattern API to create pattern matchers. The syntax is based on path-to-regexp. Wildcards, named capture groups, regular groups, and group modifiers are all supported.

Classes

An error class that feels fits nicely into an incoming HTTP request handler.

Extends the lifetime of the install and activate events dispatched on the global scope as part of the service worker lifecycle.

An event object containing contextual data for a single and specific incoming HTTP request.

A client-side cachable response.

An error response sent to a client when a request is deemed to be invalid in some way.

A response that returns a full HTML document.

A response sent to the client containing a JSON object.

Given that a request's etag header matches an server entity or resource, a NotModifiedResponse should be sent to the requestor as an indication that the client's cache is still applicable.

A basic implementation of Twitter's original Snowflake ID system.

A isomorphic logger available in both the browser and worker environments.

Handles incoming requests for static assets uploaded to Cloudflare Pages.

Middleware that given a Response with a body, compresses the contents according to a client's Accept-Encoding request header.

A router that proxies requests directly to a Cloudflare Worker environment binding, such as a service binding.

Middleware to manage and authenticate your users.

Handles incoming requests for static assets uploaded to Cloudflare KV.

Routes incoming HTTP requests from the user's browser to your app's route endpoints.

Routes incoming HTTP requests from the user's browser to your app's route endpoints.

Variables

Used in place of the reference-sensitive instanceof

A record of all the status codes text.

Returns the given fileName extension.

Determines if the given HTTP status status is a client error.

Determines if the given HTTP status code is informational.

Determines if the given HTTP status status is a redirection.

Determines if the given HTTP status status is a server error.

Determines if the given HTTP status code is successful.

Given a standard uppercase HTTP method verb such as GET, return the router method name.

Given a standardized uppercase method verb such as GET, return the normalized form.

Stringifies JSON into an indented string.

An asset environment binding available within Cloudflare Pages.

An asset environment binding available within Cloudflare Pages.

The default cookie serialization options.

The default session cookie key.

Embeds the given SSR props in the DOM for client-side hydration.

The global key where SSR props are assigned. This includes a ':' character to prevent document.querySelector from matching this key.

A server-side render of a given HTML document.

The default ID assigned to the React root element.

The default ID assigned to the React root style element.

Used in place of the reference-sensitive instanceof

Resolves a POSIX-like path into slash delineated segments.

Functions

Infers the appropriate Response constructor for the given ResponseLike body.

Clones a given Response as a mutable instance.

Checks if a given MIME-type is compressible.

Checks if the given object is an instance of Request

Checks if the given object is an instance of Response

Checks if the given object is shaped like a Request

A convenience wrapper around React.createContext that creates a context and hook for the child components within a Provider's context.

Renders the given React content to an HTML stream.

A route request handler for redirecting requests.

Checks if a given object is shaped like a KeyworkFetcher

Utility function for parsing middleware options.

Converts a given ArrayBuffer to a base64 encoded string.

Converts a given ArrayBuffer to a string.

Convenience method to get a substring's start offset.

Converts a given string to an ArrayBuffer.

Promise wrapper around requestAnimationFrame

Creates a promise that blocks until the DOM has loaded.

Converts a given URLPatternLike to URLPattern

Converts a given URLPatternInput to URLPatternI nit

Interfaces

A JSON representation of a KeyworkResourceError

Additional data associated with the IsomorphicFetchEvent.

A simple session manager to aid in authenticating users.

An environment binding available within Worker Sites. This is often used with the @cloudflare/kv-asset-handler package.

Options to configure the Worker Router.

Cloudflare Usage

Events are handled by defining and exporting an object with method handlers that correspond to event names:

Middleware implementation of fetch

Public endpoints to aid in debugging your app. Available at /keywork/*

Options to configure the Worker Router.

A function or method that handles incoming requests and replies with a Response.

An environment binding within a worker that has a fetch method. This usually is related to static assets uploaded to Cloudflare KV via Wrangler's Worker Sites.

Type Aliases

HTTP method verbs.

An object that has a url property.

Either a full Response, or a more primitive value to be processed.

Normalized HTTP methods in a JavaScript friendly format.

The compression format used when encoding the response body.

A component which wraps the current SSR routes. Use this if you need to inject a provider into the SSR pipeline.

Either:

Middleware declaration in the convenient shape of Map's constructor parameters.

Either an instance of URLPattern, or a string representing the pathname portion of a URLPattern