-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless-redis.js
94 lines (75 loc) · 2.26 KB
/
serverless-redis.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Adapted from https://bitbucket.org/atlassian/atlassian-connect-express/src/664d8284b66420229728b5a0501d6145bc2bf279/lib/store/redis.js
* to use `@upstash/redis` instead of `redis` for running on serverless
* environments like Vercel.
*
* Used under the Apache License, Version 2.0
*/
import { Redis } from "@upstash/redis"
const redisKey = (key, clientKey) => {
return clientKey ? `${clientKey}:${key}` : key;
};
/**
* @implements {typeof import("atlassian-connect-express").AddOn["settings"]}
*/
export class ServerlessRedisAdapter {
constructor(logger, opts) {
this.client = new Redis({
url: process.env["DB_URL"] || opts.url,
token: process.env["DB_TOKEN"] || opts.token,
});
}
async get(key, clientKey) {
const val = await this.client.get(redisKey(key, clientKey));
try {
return JSON.parse(val);
} catch (e) {
return val;
}
}
async saveInstallation(val, clientKey) {
const clientSetting = await this.set("clientInfo", val, clientKey);
const forgeInstallationId = clientSetting.installationId;
if (forgeInstallationId) {
await this.associateInstallations(forgeInstallationId, clientKey);
}
return clientSetting;
}
async set(key, val, clientKey) {
let strVal = val;
if (typeof val !== "string") {
strVal = JSON.stringify(val);
}
await this.client.set(redisKey(key, clientKey), strVal);
return this.get(key, clientKey);
}
async del(key, clientKey) {
await this.client.del(redisKey(key, clientKey));
}
async getAllClientInfos() {
const keys = await this.client.keys("*:clientInfo");
return Promise.all(
keys.map(key => {
return this.get(key);
})
);
}
isMemoryStore() {
return false;
}
async associateInstallations(forgeInstallationId, clientKey) {
await this.client.set(installationKey(forgeInstallationId), clientKey);
}
async deleteAssociation(forgeInstallationId) {
await this.client.del(installationKey(forgeInstallationId));
}
async getClientSettingsForForgeInstallation(forgeInstallationId) {
const clientKey = await this.client.get(
installationKey(forgeInstallationId)
);
if (!clientKey) {
return null;
}
return this.get("clientInfo", clientKey);
}
}