Public
Edited
Nov 30
2 forks
Importers
55 stars
Insert cell
Insert cell
Insert cell
deploy = function (label, handler, options) {
onVersionPublished; // Ensure all users of this have the onPublish hook installed
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();
}
// We have to generate sessions if we are live coding
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 {
// Bug fix
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"
);
}
});
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more