-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplitStorageWrapper.ts
194 lines (173 loc) · 6.95 KB
/
SplitStorageWrapper.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Creates a storage wrapper for a given Split Storage Durable Object
* https://developers.cloudflare.com/workers/runtime-apis/durable-objects#object-stubs
*
* @param {DurableObjectStub} durableObject durable object stub
* @returns {import("@splitsoftware/splitio-commons/src/storages/types").IPluggableStorageWrapper} storage wrapper
*/
export function SplitStorageWrapper(durableObject: DurableObjectStub) {
function doFetch(path: string, param: string, param2?: any) {
// For now, fetch requires a valid URL. So we have to provide a dummy URL that will be ignored at the other end
// See https://github.com/cloudflare/workers-chat-demo/blob/master/src/chat.mjs#L518
return durableObject.fetch(
`https://dummy-url/${path}?param=${param}`,
param2 && {
method: "POST",
body: JSON.stringify(param2)
}
);
}
return {
/** Key-Value operations */
/**
* Get the value of given `key`.
*
* @function get
* @param {string} key Item to retrieve
* @returns {Promise<string | null>} A promise that resolves with the element value associated with the specified `key`,
* or null if the key does not exist.
*/
async get(key: string) {
return (await doFetch("get", key)).json<string>();
},
/**
* Add or update an item with a specified `key` and `value`.
*
* @function set
* @param {string} key Item to update
* @param {string} value Value to set
* @returns {Promise<boolean>} A promise that resolves if the operation success, whether the key was added or updated.
*/
async set(key: string, value: string) {
return (await doFetch("set", key, value)).ok;
},
/**
* Add or update an item with a specified `key` and `value`.
*
* @function getAndSet
* @param {string} key Item to update
* @param {string} value Value to set
* @returns {Promise<string | null>} A promise that resolves with the previous value associated to the given `key`, or null if not set.
*/
async getAndSet(key: string, value: string) {
return (await doFetch("getAndSet", key, value)).json<string>();
},
/**
* Removes the specified item by `key`.
*
* @function del
* @param {string} key Item to delete
* @returns {Promise<boolean>} A promise that resolves if the operation success, whether the key existed and was removed or it didn't exist.
*/
async del(key: string) {
return (await doFetch("del", key)).ok;
},
/**
* Returns all keys matching the given prefix.
*
* @function getKeysByPrefix
* @param {string} prefix String prefix to match
* @returns {Promise<string[]>} A promise that resolves with the list of keys that match the given `prefix`.
*/
async getKeysByPrefix(prefix: string) {
return (await doFetch("getKeysByPrefix", prefix)).json<string[]>();
},
/**
* Returns the values of all given `keys`.
*
* @function getMany
* @param {string[]} keys List of keys to retrieve
* @returns {Promise<(string | null)[]>} A promise that resolves with the list of items associated with the specified list of `keys`.
* For every key that does not hold a string value or does not exist, null is returned.
*/
async getMany(keys: string[]) {
return (await doFetch("getMany", keys.join(","))).json<string[]>();
},
/** Integer operations */
/**
* Increments the number stored at `key` by `increment`, or set it to `increment` if the value doesn't exist.
*
* @function incr
* @param {string} key Key to increment
* @param {number} increment Value to increment by. Defaults to 1.
* @returns {Promise<number>} A promise that resolves with the value of key after the increment.
*/
async incr(key: string, increment = 1) {
return (await doFetch("incr", key, increment)).json<number>();
},
/**
* Decrements the number stored at `key` by `decrement`, or set it to minus `decrement` if the value doesn't exist.
*
* @function decr
* @param {string} key Key to decrement
* @param {number} decrement Value to decrement by. Defaults to 1.
* @returns {Promise<number>} A promise that resolves with the value of key after the decrement.
*/
async decr(key: string, decrement = 1) {
return (await doFetch("decr", key, decrement)).json<number>();
},
/** Set operations */
/**
* Returns if item is a member of a set.
*
* @function itemContains
* @param {string} key Set key
* @param {string} item Item value
* @returns {Promise<boolean>} A promise that resolves with true boolean value if `item` is a member of the set stored at `key`,
* or false if it is not a member or `key` set does not exist.
*/
async itemContains(key: string, item: string) {
return (await doFetch("itemContains", key, item)).json<boolean>();
},
/**
* Add the specified `items` to the set stored at `key`. Those items that are already part of the set are ignored.
* If key does not exist, an empty set is created before adding the items.
*
* @function addItems
* @param {string} key Set key
* @param {string} items Items to add
* @returns {Promise<boolean | void>} A promise that resolves if the operation success.
*/
async addItems(key: string, items: string[]) {
await doFetch("addItems", key, items);
},
/**
* Remove the specified `items` from the set stored at `key`. Those items that are not part of the set are ignored.
*
* @function removeItems
* @param {string} key Set key
* @param {string} items Items to remove
* @returns {Promise<boolean | void>} A promise that resolves if the operation success. If key does not exist, the promise also resolves.
*/
async removeItems(key: string, items: string[]) {
await doFetch("removeItems", key, items);
},
/**
* Returns all the items of the `key` set.
*
* @function getItems
* @param {string} key Set key
* @returns {Promise<string[]>} A promise that resolves with the list of items. If key does not exist, the result is an empty list.
*/
async getItems(key: string) {
return (await doFetch("getItems", key)).json<string[]>();
},
// No-op. No need to connect to DurableObject stub
async connect() {
// Never throws if Durable Object namespace binding is properly configured
if (!durableObject) throw new Error("Durable Object stub not provided");
},
// No-op. No need to disconnect from DurableObject stub
async disconnect() {},
/** Queue operations */
// Since Split SDK must run in partial consumer mode, events and impressions are
// not tracked and so there is no need to implement Queue operations
async pushItems(key: string, items: string[]) {},
async popItems(key: string, count: number) {
return [];
},
async getItemsCount(key: string) {
return 0;
}
};
}