deploy = function (label, handler, options) {
onVersionPublished;
if (typeof label !== "string")
throw new Error(
"The first parameter 'name' must be a unique string to disambiguate different endpoints, using'default' will exclude it from the URL"
);
options = options || {};
const modifiers = options.modifiers || ["external"];
if (typeof options.livecode === "string") {
options.livecode = options.livecode.toUpperCase();
}
const session =
options.livecode === "PUBLIC" ? generateSessionId(label) : undefined;
const isExternal = modifiers.includes("external");
const isTerminal = modifiers.includes("terminal");
const isOrchestrator = modifiers.includes("orchestrator");
window["deployments"] = window["deployments"] || {};
window["deployments"][label] = (req, context) =>
new Promise((resolve, reject) => {
req.query = req.query || {};
const res = new Response(req, resolve);
window["@endpointservices.context"] = context;
try {
Promise.resolve(handler(req, res, context)).catch((err) => reject(err));
} catch (err) {
reject(err);
}
});
window["deployments"][label].config = {
reusable: options.reusable || false,
modifiers: options.modifiers,
secrets: options.secrets
};
const host =
options.host && /http(s?):\/\//.exec(options.host)
? options.host
: "https://" + (options.host || `webcode.run`);
const region = options.region ? `/regions/${options.region}` : "";
try {
if (options.hostNotebook) options.hostNotebook = "/" + options.hostNotebook;
const namespace = subdomain();
const notebook = options.hostNotebook
? options.hostNotebook.split("/")[1]
: getContext().notebook;
const name = label === "default" && !session ? "" : `;${label}`;
const correlation = session ? `;${session}` : "";
const link =
`${host}${region}/observablehq.com` +
(options.hostNotebook ||
`${notebook.startsWith("d/") ? "" : `/@${namespace}`}/${notebook}`) +
`${name}${correlation}`;
return html`<a href="${link}" target="_blank">${link}</a>`;
} catch (err) {
console.error("Links don't work when embedded, but the deployed code does");
return Object.defineProperty({}, "href", {
get: () => {
throw new Error(
"Serverless cells do not work on embedded or unshared pages, unless you set the hostNotebook option"
);
}
});
}
}