-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedgekv.js
298 lines (278 loc) · 12.7 KB
/
edgekv.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
(c) Copyright 2020 Akamai Technologies, Inc. Licensed under Apache 2 license.
Version: 0.4.1
Purpose: Provide a helper class to simplify the interaction with EdgeKV in an EdgeWorker.
Repo: https://github.com/akamai/edgeworkers-examples/tree/master/edgekv/lib
*/
import { TextDecoderStream } from 'text-encode-transform';
import { WritableStream } from 'streams';
import { httpRequest } from 'http-request';
/**
* You must include edgekv_tokens.js in your bundle for this class to function properly.
* edgekv_tokens.js must include all namespaces you are going to use in the bundle.
*/
import { edgekv_access_tokens } from './edgekv_tokens.js';
export class EdgeKV {
#namespace;
#group;
#edgekv_uri;
#token_override;
/**
* Constructor to allow setting default namespace and group
* These defaults can be overriden when making individual GET, PUT, and DELETE operations
* @param {string} [namespace] the default namespace to use for all GET, PUT, and DELETE operations
* Namespace must be 32 characters or less, consisting of A-Z a-z 0-9 _ or -
* @param {string} [group] the default group to use for all GET, PUT, and DELETE operations
* Group must be 128 characters or less, consisting of A-Z a-z 0-9 _ or -
*/
constructor(namespace = "default", group = "default") {
if (typeof namespace === "object") {
this.#namespace = namespace.namespace || "default";
this.#group = namespace.group || "default";
this.#edgekv_uri = namespace.edgekv_uri || "https://edgekv.akamai-edge-svcs.net";
this.#token_override = namespace.token_override || null;
} else {
this.#namespace = namespace;
this.#group = group;
this.#edgekv_uri = "https://edgekv.akamai-edge-svcs.net";
this.#token_override = null;
}
}
throwError(failed_reason, status, body) {
throw {
failed: failed_reason,
status: status,
body: body,
toString: function () { return JSON.stringify(this); }
};
}
async requestHandlerTemplate(response_provider, handler_200, handler_large_200, error_text, default_value) {
try {
let response = await response_provider();
switch (response.status) {
case 200:
// need to handle content length > 128000 bytes differently in EdgeWorkers
let contentLength = response.getHeader('Content-Length');
if (!contentLength || contentLength.length == 0 || contentLength[0] >= 128000) {
return handler_large_200(response);
} else {
return handler_200(response);
}
case 404:
return default_value;
default:
let content = "";
try {
content = await response.text();
content = JSON.parse(content);
} catch (error) { }
throw { status: response.status, body: content };
}
} catch (error) {
if (error.hasOwnProperty('status')) {
this.throwError(error_text + " FAILED", error.status, error.body);
}
this.throwError(error_text + " FAILED", 0, error.toString());
}
}
validate({ namespace = null, group = null, item = null }) {
if (!namespace || !/^[A-Za-z0-9_-]{1,32}$/.test(namespace)) {
throw "Namespace is not valid. Must be 32 characters or less, consisting of A-Z a-z 0-9 _ or -";
}
if (!group || !/^[A-Za-z0-9_-]{1,128}$/.test(group)) {
throw "Group is not valid. Must be 128 characters or less, consisting of A-Z a-z 0-9 _ or -";
}
if (!item || !/^[A-Za-z0-9_-]{1,512}$/.test(item)) {
throw "Item is not valid. Must be 512 characters or less, consisting of A-Z a-z 0-9 _ or -";
}
}
getNamespaceToken(namespace) {
if (this.#token_override) {
return this.#token_override;
}
let name = "namespace-" + namespace;
if (!(name in edgekv_access_tokens)) {
throw "MISSING ACCESS TOKEN. No EdgeKV Access Token defined for namespace '" + namespace + "'.";
}
return edgekv_access_tokens[name]["value"];
}
addTimeout(options, timeout) {
if (timeout && (typeof timeout !== 'number' || !isFinite(timeout) || timeout <= 0 || timeout > 1000)) {
throw "Timeout is not valid. Must be a number greater than 0 and less than 1000.";
}
if (timeout) {
options.timeout = timeout;
}
return options;
}
async streamText(response_body) {
let result = "";
await response_body
.pipeThrough(new TextDecoderStream())
.pipeTo(new WritableStream({
write(chunk) {
result += chunk;
}
}), { preventAbort: true });
return result;
}
async streamJson(response_body) {
return JSON.parse(await this.streamText(response_body));
}
putRequest({ namespace = this.#namespace, group = this.#group, item, value, timeout = null } = {}) {
this.validate({ namespace: namespace, group: group, item: item });
let uri = this.#edgekv_uri + "/api/v1/namespaces/" + namespace + "/groups/" + group + "/items/" + item;
return httpRequest(uri, this.addTimeout({
method: "PUT",
body: typeof value === "object" ? JSON.stringify(value) : value,
headers: { "X-Akamai-EdgeDB-Auth": [this.getNamespaceToken(namespace)] }
}, timeout));
}
/**
* async PUT text into an item in the EdgeKV.
* @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default
* @param {string} [$0.group=this.#group] specify a group other than the default
* @param {string} $0.item item key to put into the EdgeKV
* @param {string} $0.value text value to put into the EdgeKV
* @param {number} [$0.timeout=null] the maximum time, between 1 and 1000 milliseconds, to wait for the response
* @returns {string} if the operation was successful, the response from the EdgeKV
* @throws {object} if the operation was not successful,
* an object describing the non-200 response from the EdgeKV: {failed, status, body}
*/
async putText({ namespace = this.#namespace, group = this.#group, item, value, timeout = null } = {}) {
return this.requestHandlerTemplate(
() => this.putRequest({ namespace: namespace, group: group, item: item, value: value, timeout: timeout }),
(response) => response.text(),
(response) => this.streamText(response.body),
"PUT",
null
);
}
/**
* PUT text into an item in the EdgeKV while only waiting for the request to send and not for the response.
* @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default
* @param {string} [$0.group=this.#group] specify a group other than the default
* @param {string} $0.item item key to put into the EdgeKV
* @param {string} $0.value text value to put into the EdgeKV
* @throws {object} if the operation was not successful at sending the request,
* an object describing the error: {failed, status, body}
*/
putTextNoWait({ namespace = this.#namespace, group = this.#group, item, value } = {}) {
try {
this.putRequest({ namespace: namespace, group: group, item: item, value: value });
} catch (error) {
this.throwError("PUT FAILED", 0, error.toString());
}
}
/**
* async PUT json into an item in the EdgeKV.
* @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default
* @param {string} [$0.group=this.#group] specify a group other than the default
* @param {string} $0.item item key to put into the EdgeKV
* @param {object} $0.value json value to put into the EdgeKV
* @param {number} [$0.timeout=null] the maximum time, between 1 and 1000 milliseconds, to wait for the response
* @returns {string} if the operation was successful, the response from the EdgeKV
* @throws {object} if the operation was not successful,
* an object describing the non-200 response from the EdgeKV: {failed, status, body}
*/
async putJson({ namespace = this.#namespace, group = this.#group, item, value, timeout = null } = {}) {
return this.requestHandlerTemplate(
() => this.putRequest({ namespace: namespace, group: group, item: item, value: JSON.stringify(value), timeout: timeout }),
(response) => response.text(),
(response) => this.streamText(response.body),
"PUT",
null
);
}
/**
* PUT json into an item in the EdgeKV while only waiting for the request to send and not for the response.
* @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default
* @param {string} [$0.group=this.#group] specify a group other than the default
* @param {string} $0.item item key to put into the EdgeKV
* @param {object} $0.value json value to put into the EdgeKV
* @throws {object} if the operation was not successful at sending the request,
* an object describing the error: {failed, status, body}
*/
putJsonNoWait({ namespace = this.#namespace, group = this.#group, item, value } = {}) {
try {
this.putRequest({ namespace: namespace, group: group, item: item, value: JSON.stringify(value) });
} catch (error) {
this.throwError("PUT FAILED", 0, error.toString());
}
}
getRequest({ namespace = this.#namespace, group = this.#group, item, timeout = null } = {}) {
this.validate({ namespace: namespace, group: group, item: item });
let uri = this.#edgekv_uri + "/api/v1/namespaces/" + namespace + "/groups/" + group + "/items/" + item;
return httpRequest(uri, this.addTimeout({
method: "GET",
headers: { "X-Akamai-EdgeDB-Auth": [this.getNamespaceToken(namespace)] }
}, timeout));
}
/**
* async GET text from an item in the EdgeKV.
* @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default
* @param {string} [$0.group=this.#group] specify a group other than the default
* @param {string} $0.item item key to get from the EdgeKV
* @param {string} [$0.default_value=null] the default value to return if a 404 response is returned from EdgeKV
* @param {number} [$0.timeout=null] the maximum time, between 1 and 1000 milliseconds, to wait for the response
* @returns {string} if the operation was successful, the text response from the EdgeKV or the default_value on 404
* @throws {object} if the operation was not successful,
* an object describing the non-200 and non-404 response from the EdgeKV: {failed, status, body}
*/
async getText({ namespace = this.#namespace, group = this.#group, item, default_value = null, timeout = null } = {}) {
return this.requestHandlerTemplate(
() => this.getRequest({ namespace: namespace, group: group, item: item, timeout: timeout }),
(response) => response.text(),
(response) => this.streamText(response.body),
"GET TEXT",
default_value
);
}
/**
* async GET json from an item in the EdgeKV.
* @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default
* @param {string} [$0.group=this.#group] specify a group other than the default
* @param {string} $0.item item key to get from the EdgeKV
* @param {object} [$0.default_value=null] the default value to return if a 404 response is returned from EdgeKV
* @param {number} [$0.timeout=null] the maximum time, between 1 and 1000 milliseconds, to wait for the response
* @returns {object} if the operation was successful, the json response from the EdgeKV or the default_value on 404
* @throws {object} if the operation was not successful,
* an object describing the non-200 and non-404 response from the EdgeKV: {failed, status, body}
*/
async getJson({ namespace = this.#namespace, group = this.#group, item, default_value = null, timeout = null } = {}) {
return this.requestHandlerTemplate(
() => this.getRequest({ namespace: namespace, group: group, item: item, timeout: timeout }),
(response) => response.json(),
(response) => this.streamJson(response.body),
"GET JSON",
default_value
);
}
deleteRequest({ namespace = this.#namespace, group = this.#group, item, timeout = null } = {}) {
this.validate({ namespace: namespace, group: group, item: item });
let uri = this.#edgekv_uri + "/api/v1/namespaces/" + namespace + "/groups/" + group + "/items/" + item;
return httpRequest(uri, this.addTimeout({
method: "DELETE",
headers: { "X-Akamai-EdgeDB-Auth": [this.getNamespaceToken(namespace)] }
}, timeout));
}
/**
* async DELETE an item in the EdgeKV.
* @param {string} [$0.namepsace=this.#namespace] specify a namespace other than the default
* @param {string} [$0.group=this.#group] specify a group other than the default
* @param {string} $0.item item key to delete from the EdgeKV
* @param {number} [$0.timeout=null] the maximum time, between 1 and 1000 milliseconds, to wait for the response
* @returns {string} if the operation was successful, the text response from the EdgeKV
* @throws {object} if the operation was not successful,
* an object describing the non-200 response from the EdgeKV: {failed, status, body}
*/
async delete({ namespace = this.#namespace, group = this.#group, item, timeout = null } = {}) {
return this.requestHandlerTemplate(
() => this.deleteRequest({ namespace: namespace, group: group, item: item, timeout: timeout }),
(response) => response.text(),
(response) => this.streamText(response.body),
"DELETE",
null
);
}
}