Skip to content

Commit a1de252

Browse files
authored
feat: Exposes a RecipeEnvironment to recipes run in a context. (#900)
Allows requests to call toolshed in Deno environment.
1 parent 3636222 commit a1de252

File tree

6 files changed

+56
-5
lines changed

6 files changed

+56
-5
lines changed

background-charm-service/src/worker.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import {
55
isStream,
66
onError,
77
setBobbyServerUrl,
8+
setRecipeEnvironment,
89
storage,
910
} from "@commontools/runner";
1011
import {
1112
type DID,
1213
Identity,
1314
KeyPairRaw,
1415
openSession,
15-
type Session,
1616
} from "@commontools/identity";
1717

1818
let initialized = false;
@@ -28,7 +28,7 @@ onError((e: Error) => {
2828
});
2929

3030
async function setup(
31-
data: { did: string; toolshedUrl: string; rawIdentity: KeyPairRaw},
31+
data: { did: string; toolshedUrl: string; rawIdentity: KeyPairRaw },
3232
) {
3333
if (initialized) {
3434
console.log(`Worker: Already initialized, skipping setup`);
@@ -46,11 +46,15 @@ async function setup(
4646
throw new Error("Worker missing rawIdentity");
4747
}
4848

49-
const identity = await Identity.deserialize(rawIdentity);
49+
const identity = await Identity.deserialize(rawIdentity);
50+
const apiUrl = new URL(toolshedUrl);
5051
// Initialize storage and remote connection
51-
storage.setRemoteStorage(new URL(toolshedUrl));
52+
storage.setRemoteStorage(apiUrl);
5253
setBobbyServerUrl(toolshedUrl);
5354
storage.setSigner(identity);
55+
setRecipeEnvironment({
56+
apiUrl,
57+
});
5458

5559
// Initialize session
5660
spaceId = did as DID;

builder/src/env.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { isDeno } from "@commontools/utils/env";
2+
3+
// Environment configuration provided to recipes. Could
4+
// eventually be e.g. `import.meta` exposed to recipes.
5+
//
6+
// /!\ These should not be globals (outside of recipe execution context).
7+
// /!\ Execution needs to be sandboxed to prevent recipes setting these values.
8+
9+
// Environment configuration available to recipes.
10+
export interface RecipeEnvironment {
11+
readonly apiUrl: URL;
12+
}
13+
14+
let globalEnv = {
15+
apiUrl: isDeno()
16+
? new URL("http://localhost:8000")
17+
: new URL(new URL(globalThis.location.href).origin),
18+
};
19+
20+
// Sets the `RecipeEnvironment` for all recipes executed
21+
// within this JavaScript context.
22+
export function setRecipeEnvironment(env: RecipeEnvironment) {
23+
globalEnv = env;
24+
}
25+
26+
// Gets the `RecipeEnvironment` for all recipes executed
27+
// within this JavaScript context.
28+
//
29+
// User-visible.
30+
export function getRecipeEnvironment(): RecipeEnvironment {
31+
return globalEnv;
32+
}

builder/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export {
99
lift,
1010
render,
1111
} from "./module.ts";
12+
export {
13+
getRecipeEnvironment,
14+
type RecipeEnvironment,
15+
setRecipeEnvironment,
16+
} from "./env.ts";
1217
export {
1318
getTopFrame,
1419
popFrame,

recipes/gmail.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { h } from "@commontools/html";
22
import {
33
cell,
44
derive,
5+
getRecipeEnvironment,
56
handler,
67
ID,
78
JSONSchema,
@@ -21,6 +22,8 @@ const turndown = new TurndownService({
2122
emDelimiter: "*",
2223
});
2324

25+
const env = getRecipeEnvironment();
26+
2427
turndown.addRule("removeStyleTags", {
2528
filter: ["style"],
2629
replacement: function () {
@@ -178,7 +181,7 @@ const refreshAuthToken = async (auth: Cell<Auth>) => {
178181
console.log("refreshAuthToken", body);
179182

180183
const refresh_response = await fetch(
181-
"/api/integrations/google-oauth/refresh",
184+
new URL("/api/integrations/google-oauth/refresh", env.apiUrl),
182185
{
183186
method: "POST",
184187
body: JSON.stringify(body),

runner/src/env.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Expose `getRecipeEnvironment` even if unused so that (dynamic) recipes
2+
// can still import from the host context.
3+
export {
4+
getRecipeEnvironment,
5+
setRecipeEnvironment,
6+
} from "@commontools/builder";

runner/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export {
77
run as addAction,
88
unschedule as removeAction,
99
} from "./scheduler.ts";
10+
export { getRecipeEnvironment, setRecipeEnvironment } from "./env.ts";
1011
export type { DocImpl } from "./doc.ts";
1112
export type { Cell, CellLink, Stream } from "./cell.ts";
1213
export type { QueryResult } from "./query-result-proxy.ts";

0 commit comments

Comments
 (0)