-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblobs.ts
78 lines (67 loc) · 1.77 KB
/
blobs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import * as base64 from "../vendor/deno.land/[email protected]/encoding/base64.ts";
import type { Deploy, Site } from "./context.ts";
declare global {
// Using `var` so that the declaration is hoisted in such a way that we can
// reference it before it's initialized.
// deno-lint-ignore no-var
var netlifyBlobsContext: unknown;
}
/**
* Payload expected by the Blobs client as an environment variable, so that
* user code can initialize a store without providing any metadata.
*
* @see {@link https://github.com/netlify/blobs/blob/68f58181bd60687797557444a1efe1861324deb1/src/environment.ts}
*/
interface BlobsContext {
deployID?: string;
edgeURL: string;
primaryRegion?: string;
siteID: string;
token: string;
uncachedEdgeURL?: string;
}
/**
* Payload sent by our edge nodes as part of the invocation with metadata about
* Blobs, including the URL of the edge endpoint and the access token.
*/
export interface BlobsMetadata {
primary_region?: string;
token?: string;
url?: string;
url_uncached?: string;
}
export function parseBlobsMetadata(blobsHeader: string | null): BlobsMetadata {
if (!blobsHeader) {
return {};
}
try {
const blobsContext: BlobsMetadata = JSON.parse(atob(blobsHeader));
return blobsContext;
} catch {
return {};
}
}
export function setBlobsContext(
metadata: BlobsMetadata,
deploy: Deploy,
site: Site,
) {
const {
primary_region: primaryRegion,
token,
url,
url_uncached: uncachedURL,
} = metadata;
if (!token || !url || !site.id) {
return;
}
const context: BlobsContext = {
deployID: deploy.id,
edgeURL: url,
primaryRegion,
siteID: site.id,
token,
uncachedEdgeURL: uncachedURL,
};
globalThis.netlifyBlobsContext = base64.encode(JSON.stringify(context));
}