[]previous and next page

mergeHandlerOptions

Merges the handler options of the file (baked) with the entry's (user).

Merges the handler options of the file (baked) with the entry's (user). The result is a new object; neither input is changed.

The rules, in short (the entry is user):

  • a plain object on both sides is merged key by key, recursively: security.csp from the entry keeps the file's security.headers;
  • any other value replaces the file's: a primitive, false, null, an array, a function, a class instance (its reference is kept). An array is never concatenated: allowedHosts in the entry replaces the file's list;
  • undefined means "not set": it does NOT erase a file value. A key that neither side sets is absent from the result;
  • the keys __proto__, constructor and prototype are ignored at every depth;
  • a merge may create at most 10 000 objects; more throws a RangeError (a cycle in the input).

Import#

ts
import { mergeHandlerOptions } from "@shadow-js/dusk/server";

Signature#

ts
function mergeHandlerOptions(
  baked: Partial<HandlerOptions>,
  user: Partial<HandlerOptions> | undefined,
): Partial<HandlerOptions>

Parameters#

Name Description
baked The file's options, as serverOptionsFromConfig returns them.
user The entry's options (undefined: none).

Returns#

The merged options.

Throws#

  • RangeError when the inputs are cyclic or nest beyond the object bound.

Examples#

ts
const options = mergeHandlerOptions(
  { security: { allowedHosts: ["example.com"], headers: { frameOptions: "DENY" } } },
  { security: { csp: { directives: { "default-src": ["'self'"] } } } },
);
// security: { allowedHosts: ["example.com"], headers: { frameOptions: "DENY" }, csp: { ... } }