-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
998f8cd
commit 4cb15ce
Showing
7 changed files
with
110 additions
and
10 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
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,81 @@ | ||
import { get } from '@vercel/edge-config'; | ||
|
||
import { KvAdapter } from '../types'; | ||
|
||
export class VercelEdgeConfigAdapter implements KvAdapter { | ||
private config_id = ''; | ||
|
||
constructor() { | ||
const config_matcher = /(?:https:\/\/edge-config.vercel.com\/)(.*)\?(?:.*)/; | ||
|
||
if ( | ||
!process.env.EDGE_CONFIG || | ||
!process.env.VERCEL_TEAM_ID || | ||
!process.env.VERCEL_ACCESS_TOKEN | ||
) { | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
`EDGE_CONFIG, VERCEL_TEAM_ID, and VERCEL_ACCESS_TOKEN must be defined to use the Edge Config adapter`, | ||
); | ||
|
||
return; | ||
} | ||
|
||
const match = config_matcher.exec(process.env.EDGE_CONFIG); | ||
|
||
if (!match || !match[1]) { | ||
// eslint-disable-next-line no-console | ||
console.error(`EDGE_CONFIG is malformed, unable to initialize Edge Config adapter`); | ||
|
||
return; | ||
} | ||
|
||
this.config_id = match[1]; | ||
} | ||
|
||
async get<Data>(key: string) { | ||
const keyHash = await this.hashString(key); | ||
|
||
return get<string>(keyHash).then( | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
(result) => (result !== undefined ? (JSON.parse(result) as Data) : null), | ||
); | ||
} | ||
|
||
async mget<Data>(...keys: string[]) { | ||
return Promise.all(keys.map((key) => this.get<Data>(key))); | ||
} | ||
|
||
async set<Data>(key: string, value: Data) { | ||
const keyHash = await this.hashString(key); | ||
|
||
const url = `https://api.vercel.com/v1/edge-config/${this.config_id}/items?teamId=${process.env.VERCEL_TEAM_ID ?? ''}`; | ||
|
||
void fetch(url, { | ||
method: 'PATCH', | ||
headers: { | ||
Authorization: `Bearer ${process.env.VERCEL_ACCESS_TOKEN ?? ''}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
items: [ | ||
{ | ||
operation: 'upsert', | ||
key: keyHash, | ||
value: JSON.stringify(value), | ||
}, | ||
], | ||
}), | ||
}); | ||
|
||
return value; | ||
} | ||
|
||
private async hashString(key: string) { | ||
return crypto.subtle.digest('SHA-1', new TextEncoder().encode(key)).then((buf) => { | ||
return Array.prototype.map | ||
.call(new Uint8Array(buf), (x: string) => `00${parseInt(x, 10).toString(16)}`.slice(-2)) | ||
.join(''); | ||
}); | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.