-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marcos Candeia <[email protected]>
- Loading branch information
Showing
7 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,7 @@ export { | |
View, | ||
} from "npm:@opentelemetry/[email protected]"; | ||
|
||
export * as supabase from "jsr:@supabase/[email protected]"; | ||
export { logs, SeverityNumber } from "npm:@opentelemetry/[email protected]"; | ||
export type { Logger } from "npm:@opentelemetry/[email protected]"; | ||
export { OTLPLogExporter } from "npm:@opentelemetry/[email protected]"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import type { supabase } from "../../deps.ts"; | ||
import getSupabaseClient from "../../supabase.ts"; | ||
import { randId as ulid } from "../../utils/rand.ts"; | ||
import { singleFlight } from "../core/utils.ts"; | ||
import type { | ||
RealtimeDecofileProvider, | ||
VersionedDecofile, | ||
} from "./realtime.ts"; | ||
|
||
const TABLE = "configs"; | ||
const fetchDecofile = ( | ||
site: string, | ||
): PromiseLike<{ data: VersionedDecofile | null; error: unknown }> => { | ||
return getSupabaseClient().from(TABLE).select("state, revision").eq( | ||
"site", | ||
site, | ||
).maybeSingle(); | ||
}; | ||
|
||
const JITTER_TIME_MS = 2000; | ||
type Fetcher = () => ReturnType<typeof fetchDecofile>; | ||
// Supabase client setup | ||
const subscribeForDecofileChanges = ( | ||
site: string, | ||
fetcher: Fetcher, | ||
) => | ||
( | ||
callback: (res: VersionedDecofile) => unknown, | ||
subscriptionCallback: ( | ||
status: `${supabase.REALTIME_SUBSCRIBE_STATES}`, | ||
err?: Error, | ||
) => void, | ||
) => { | ||
return getSupabaseClient() | ||
.channel("changes") | ||
.on( | ||
"postgres_changes", | ||
{ | ||
event: "*", | ||
schema: "public", | ||
table: TABLE, | ||
filter: `site=eq.${site}`, | ||
}, | ||
(payload) => { | ||
const newPayload = payload.new as VersionedDecofile; | ||
if (newPayload?.state === undefined) { | ||
console.warn("state is too big, fetching from supabase"); | ||
// we have added a jitter of 2s to prevent too many requests being issued at same time. | ||
const jitter = Math.floor(JITTER_TIME_MS * Math.random()); | ||
setTimeout(() => { | ||
fetcher().then(async (resp) => { | ||
const { data, error } = resp; | ||
if (error || !data) { | ||
console.error("error when fetching config", error, "retrying"); | ||
const { data: secondTryData, error: secondTryError } = | ||
await fetcher(); | ||
|
||
if (secondTryError || !secondTryData) { | ||
console.error("error when fetching config", error); | ||
return; | ||
} | ||
|
||
callback(secondTryData); | ||
return; | ||
} | ||
callback(data); | ||
}); | ||
}, jitter); | ||
} else { | ||
callback(newPayload); | ||
} | ||
}, | ||
) | ||
.subscribe(subscriptionCallback); | ||
}; | ||
|
||
/** | ||
* Create a supabase decofile provider based on `configs` table. | ||
* @param site the site name | ||
* @returns the supabaseDecofileProvider. | ||
*/ | ||
export const fromConfigsTable = ( | ||
site: string, | ||
): RealtimeDecofileProvider => { | ||
const sf = singleFlight<{ data: VersionedDecofile | null; error: unknown }>(); | ||
const fetcher = () => | ||
sf.do( | ||
"flight", | ||
async () => { | ||
const { data, error } = await fetchDecofile(site); | ||
return { | ||
data: data ?? | ||
{ state: {}, revision: ulid() }, | ||
error, | ||
}; | ||
}, | ||
); | ||
return { | ||
get: fetcher, | ||
subscribe: subscribeForDecofileChanges(site, fetcher), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { supabase } from "./deps.ts"; | ||
|
||
let client: supabase.SupabaseClient | null = null; | ||
|
||
const SUPABASE_LIVE_ENDPOINT_ENV_VAR = "SUPABASE_LIVE_ENDPOINT"; | ||
const SUPABASE_LIVE_ANON_KEY_ENV_VAR = "SUPABASE_LIVE_ANON_KEY"; | ||
const DEFAULT_SUPABASE_LIVE_ENDPOINT = | ||
"https://ozksgdmyrqcxcwhnbepg.supabase.co"; | ||
// From supabase docs: | ||
// "This key is safe to use in a browser if you have enabled Row Level Security for your tables and configured policies." | ||
export const SUPABASE_LIVE_ENDPOINT = typeof Deno !== "undefined" | ||
? Deno.env.get(SUPABASE_LIVE_ENDPOINT_ENV_VAR) ?? | ||
DEFAULT_SUPABASE_LIVE_ENDPOINT | ||
: DEFAULT_SUPABASE_LIVE_ENDPOINT; | ||
|
||
const DEFAULT_SUPABASE_LIVE_ANON_KEY = | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im96a3NnZG15cnFjeGN3aG5iZXBnIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTY3MjM3NDYsImV4cCI6MTk3MjI5OTc0Nn0.HMdsG6NZlq6dvYFCK1_tuJh38TmsqLeN8H4OktTHt_M"; | ||
|
||
export const SUPABASE_LIVE_ANON_KEY = typeof Deno !== "undefined" | ||
? Deno.env.get(SUPABASE_LIVE_ANON_KEY_ENV_VAR) ?? | ||
DEFAULT_SUPABASE_LIVE_ANON_KEY | ||
: DEFAULT_SUPABASE_LIVE_ANON_KEY; | ||
|
||
let userEndpoint = SUPABASE_LIVE_ENDPOINT; | ||
let userKey = SUPABASE_LIVE_ANON_KEY; | ||
|
||
export function setupSupabase(endpoint: string, key: string) { | ||
userEndpoint = endpoint; | ||
userKey = key; | ||
} | ||
|
||
export default function getSupabaseClient(accessToken?: string) { | ||
if (!client) { | ||
client = supabase.createClient(userEndpoint, accessToken || userKey); | ||
} | ||
|
||
return client; | ||
} |