Published unlisted
Edited
Aug 11, 2022
Insert cell
Insert cell
Insert cell
viewof region = Inputs.radio(["europe-west4", "europe-west1", "us-central1"], {
value: "europe-west1"
})
Insert cell
html`${["europe-west4", "europe-west1", "us-central1"].map(
(region) =>
html`<a target="_blank" href="https://webcode.run/regions/${region}/.stats">Diagnostics from ${region}</a><br>`
)}`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
region
Insert cell
Insert cell
Insert cell
example1_response = (await fetch(remote_cell_link.href)).json()
Insert cell
Insert cell
Insert cell
default_url_example = {
const link = deploy(
"default",
async (req, res) => {
res.send("You can make small URLs with the default handler");
},
{
reusable: true,
region: region
}
);
return html`<a href=${link.href} target="_blank">${link.href}</a>`;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
example_secrets = deploy(
"example_secrets",
(req, res, context) => {
res.send(context.secrets["endpointservices_example_secret"]);
},
{
secrets: ["endpointservices_example_secret"],
region: region
}
)
Insert cell
Insert cell
Insert cell
example_getContext = deploy(
"example_getContext",
(req, res) => {
res.send(getContext()); // Use of global getContext() function
},
{
region: region
}
)
Insert cell
Insert cell
Insert cell
endpoint_with_CDN = deploy(
"endpoint_with_CDN",
(req, res, context) => {
res.header("Cache-Control", "public, max-age=0, s-maxage=99999"); // Cache in shared cache (CDN)
res.send(new Date());
},
{
region: region
}
)
Insert cell
ci.test("Example 6 Enable cache for insane performance", async () => {
const response = await fetch(endpoint_with_CDN.href);
return expect(response.status).toBe(200);
})
Insert cell
Insert cell
Insert cell
europe_west1 = deploy(
"europe-west1",
async (req, res) => {
res.send("Howdy!");
},
{
region: "europe-west1"
}
)
Insert cell
ci.test("Example 6.1: Regions", async () => {
await expect((await fetch(europe_west1.href)).status).toBe(200);
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
pngElement = new Promise((resolve) => {
let img = html`<img src=${pngLink.href} onerror=${() => {
img.err = true;
resolve(img);
}} onload=${() => resolve(img)} />`;
})
Insert cell
ci.test(
"Example 8: Images can be served as binary data",
async () => {
expect(pngElement.err).toBeUndefined();
},
{
region: region
}
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
ci.test(
"Example 10: An orchestrator can call a cell with modifier external",
async () => {
expect(await (await fetch(orchestrator_to_external.href)).text()).toBe(
"observablehq.com/@endpointservices/serverless-cells O"
);
}
)
Insert cell
ci.test("Conformance: name is URI encoded: foo/bar", async () => {
throw new Error("Somehow this causes loops!");
expect(
await (
await fetch(deploy("foo/bar", (req, res) => res.send("OK: foo/bar")).href)
).text()
).toBe("OK: foo/bar");
})
Insert cell
ci.test("Conformance: name is URI encoded: foo", async () => {
expect(
await (
await fetch(
deploy("foo", (req, res) => res.send("OK: foo"), { region }).href
)
).text()
).toBe("OK: foo");
})
Insert cell
metadata_token = deploy("metadata_token", async (req, res) => {
try {
const response = await fetch("http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token", {
headers: {
"Metadata-Flavor": "Google"
}
})
res.status(response.status).send(await response.text())
} catch (err) {
res.status(500).send(err.message)
}
}, {region})
Insert cell
ci.test("Security: Cannot get to metadata server", async () => {
expect(await (await fetch(metadata_token.href)).text()).toBe(
"Failed to fetch"
);
})
Insert cell
Insert cell
streamingResponse = deploy(
"streamingResponse",
async (req, res) => {
res.write("w");
res.write("r");
res.write("i");
res.write("t");
res.write("e");
res.end();
},
{
region: region
}
)
Insert cell
ci.test("Streaming: write works", async () => {
expect(await (await fetch(streamingResponse.href)).text()).toBe("write");
})
Insert cell
deploy(
"redirect",
(req, res) => {
res.redirect(302, pngLink.href);
},
{
region
}
)
Insert cell
ci.test("Example 11: Redirects", async () => {
expect(await (await fetch(orchestrator_to_external.href)).text()).toBe(
"observablehq.com/@endpointservices/serverless-cells O"
);
})
Insert cell
ci.test("Context fields are populated", async () => {
const payload = await (
await fetch(
deploy("contextFields", (req, res) => res.json(req), {
reusable: true,
region
}).href
)
).json();

expect(payload).toHaveProperty("headers");
expect(payload).toHaveProperty("ip");
expect(payload).toHaveProperty("url");
expect(payload).toHaveProperty("method");
expect(payload).toHaveProperty("query");
expect(payload).toHaveProperty("baseUrl");
})
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