File tree Expand file tree Collapse file tree 6 files changed +56
-5
lines changed
background-charm-service/src Expand file tree Collapse file tree 6 files changed +56
-5
lines changed Original file line number Diff line number Diff line change @@ -5,14 +5,14 @@ import {
55 isStream ,
66 onError ,
77 setBobbyServerUrl ,
8+ setRecipeEnvironment ,
89 storage ,
910} from "@commontools/runner" ;
1011import {
1112 type DID ,
1213 Identity ,
1314 KeyPairRaw ,
1415 openSession ,
15- type Session ,
1616} from "@commontools/identity" ;
1717
1818let initialized = false ;
@@ -28,7 +28,7 @@ onError((e: Error) => {
2828} ) ;
2929
3030async 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 ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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" ;
1217export {
1318 getTopFrame ,
1419 popFrame ,
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { h } from "@commontools/html";
22import {
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+
2427turndown . 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 ) ,
Original file line number Diff line number Diff line change 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" ;
Original file line number Diff line number Diff line change 77 run as addAction ,
88 unschedule as removeAction ,
99} from "./scheduler.ts" ;
10+ export { getRecipeEnvironment , setRecipeEnvironment } from "./env.ts" ;
1011export type { DocImpl } from "./doc.ts" ;
1112export type { Cell , CellLink , Stream } from "./cell.ts" ;
1213export type { QueryResult } from "./query-result-proxy.ts" ;
You can’t perform that action at this time.
0 commit comments