@upstash/redis 0.0.0-ci.e1d39d33 → 0.0.0-ci.efe72030
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -274
- package/esm/pkg/commands/lpos.js +19 -0
- package/esm/pkg/commands/mod.js +1 -0
- package/esm/pkg/commands/script_exists.js +1 -10
- package/esm/pkg/commands/sdiffstore.js +1 -1
- package/esm/pkg/commands/zrange.js +6 -0
- package/esm/pkg/http.js +35 -7
- package/esm/pkg/pipeline.js +10 -1
- package/esm/pkg/redis.js +22 -1
- package/esm/platforms/cloudflare.js +5 -25
- package/esm/platforms/fastly.js +4 -25
- package/esm/platforms/node_with_fetch.js +4 -25
- package/esm/platforms/nodejs.js +4 -25
- package/package.json +24 -13
- package/script/pkg/commands/lpos.js +23 -0
- package/script/pkg/commands/mod.js +1 -0
- package/script/pkg/commands/script_exists.js +1 -10
- package/script/pkg/commands/sdiffstore.js +1 -1
- package/script/pkg/commands/zrange.js +6 -0
- package/script/pkg/http.js +35 -7
- package/script/pkg/pipeline.js +9 -0
- package/script/pkg/redis.js +21 -0
- package/script/platforms/cloudflare.js +5 -25
- package/script/platforms/fastly.js +4 -25
- package/script/platforms/node_with_fetch.js +4 -25
- package/script/platforms/nodejs.js +4 -25
- package/types/pkg/commands/bitop.d.ts +0 -1
- package/types/pkg/commands/bitpos.d.ts +1 -1
- package/types/pkg/commands/hmset.d.ts +2 -2
- package/types/pkg/commands/lpop.d.ts +1 -1
- package/types/pkg/commands/lpos.d.ts +15 -0
- package/types/pkg/commands/mget.d.ts +1 -1
- package/types/pkg/commands/mod.d.ts +1 -0
- package/types/pkg/commands/rpop.d.ts +2 -2
- package/types/pkg/commands/script_exists.d.ts +2 -4
- package/types/pkg/commands/sdiffstore.d.ts +1 -1
- package/types/pkg/commands/sinterstore.d.ts +2 -2
- package/types/pkg/commands/spop.d.ts +2 -2
- package/types/pkg/commands/zadd.d.ts +1 -1
- package/types/pkg/commands/zrange.d.ts +7 -0
- package/types/pkg/http.d.ts +27 -3
- package/types/pkg/pipeline.d.ts +14 -6
- package/types/pkg/redis.d.ts +22 -10
- package/types/platforms/cloudflare.d.ts +6 -2
- package/types/platforms/fastly.d.ts +5 -1
- package/types/platforms/node_with_fetch.d.ts +20 -1
- package/types/platforms/nodejs.d.ts +20 -1
package/README.md
CHANGED
|
@@ -11,9 +11,9 @@ It is the only connectionless (HTTP based) Redis client and designed for:
|
|
|
11
11
|
|
|
12
12
|
- Serverless functions (AWS Lambda ...)
|
|
13
13
|
- Cloudflare Workers (see
|
|
14
|
-
[the example](https://github.com/upstash/upstash-redis/tree/
|
|
14
|
+
[the example](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers))
|
|
15
15
|
- Fastly Compute@Edge (see
|
|
16
|
-
[the example](https://github.com/upstash/upstash-redis/tree/
|
|
16
|
+
[the example](https://github.com/upstash/upstash-redis/tree/main/examples/fastly))
|
|
17
17
|
- Next.js, Jamstack ...
|
|
18
18
|
- Client side web/mobile applications
|
|
19
19
|
- WebAssembly
|
|
@@ -23,24 +23,6 @@ See
|
|
|
23
23
|
[the list of APIs](https://docs.upstash.com/features/restapi#rest---redis-api-compatibility)
|
|
24
24
|
supported.
|
|
25
25
|
|
|
26
|
-
## Upgrading to v1.4.0 **(ReferenceError: fetch is not defined)**
|
|
27
|
-
|
|
28
|
-
If you are running on nodejs v17 and earlier, `fetch` will not be natively
|
|
29
|
-
supported. Platforms like Vercel, Netlify, Deno, Fastly etc. provide a polyfill
|
|
30
|
-
for you. But if you are running on bare node, you need to either specify a
|
|
31
|
-
polyfill yourself or change the import path to:
|
|
32
|
-
|
|
33
|
-
```typescript
|
|
34
|
-
import { Redis } from "@upstash/redis/with-fetch";
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
## Upgrading from v0.2.0?
|
|
38
|
-
|
|
39
|
-
Please read the
|
|
40
|
-
[migration guide](https://github.com/upstash/upstash-redis#migrating-to-v1). For
|
|
41
|
-
further explanation we wrote a
|
|
42
|
-
[blog post](https://blog.upstash.com/upstash-redis-sdk-v1).
|
|
43
|
-
|
|
44
26
|
## Quick Start
|
|
45
27
|
|
|
46
28
|
### Install
|
|
@@ -61,133 +43,7 @@ import { Redis } from "https://deno.land/x/upstash_redis/mod.ts";
|
|
|
61
43
|
|
|
62
44
|
Create a new redis database on [upstash](https://console.upstash.com/)
|
|
63
45
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
We support various platforms, such as nodejs, cloudflare and fastly. Platforms
|
|
67
|
-
differ slightly when it comes to environment variables and their `fetch` api.
|
|
68
|
-
Please use the correct import when deploying to special platforms.
|
|
69
|
-
|
|
70
|
-
#### Node.js
|
|
71
|
-
|
|
72
|
-
Examples: Vercel, Netlify, AWS Lambda
|
|
73
|
-
|
|
74
|
-
If you are running on nodejs you can set `UPSTASH_REDIS_REST_URL` and
|
|
75
|
-
`UPSTASH_REDIS_REST_TOKEN` as environment variable and create a redis instance
|
|
76
|
-
like this:
|
|
77
|
-
|
|
78
|
-
```ts
|
|
79
|
-
import { Redis } from "@upstash/redis"
|
|
80
|
-
|
|
81
|
-
const redis = new Redis({
|
|
82
|
-
url: <UPSTASH_REDIS_REST_URL>,
|
|
83
|
-
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
// or load directly from env
|
|
87
|
-
const redis = Redis.fromEnv()
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
If you are running on nodejs v17 and earlier, `fetch` will not be natively
|
|
91
|
-
supported. Platforms like Vercel, Netlify, Deno, Fastly etc. provide a polyfill
|
|
92
|
-
for you. But if you are running on bare node, you need to either specify a
|
|
93
|
-
polyfill yourself or change the import path to:
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
import { Redis } from "@upstash/redis/with-fetch";
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
- [Code example](https://github.com/upstash/upstash-redis/blob/main/examples/nodejs)
|
|
100
|
-
|
|
101
|
-
#### Cloudflare Workers
|
|
102
|
-
|
|
103
|
-
Cloudflare handles environment variables differently than nodejs. Please add
|
|
104
|
-
`UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` using
|
|
105
|
-
`wrangler secret put ...` or in the cloudflare dashboard.
|
|
106
|
-
|
|
107
|
-
Afterwards you can create a redis instance:
|
|
108
|
-
|
|
109
|
-
```ts
|
|
110
|
-
import { Redis } from "@upstash/redis/cloudflare"
|
|
111
|
-
|
|
112
|
-
const redis = new Redis({
|
|
113
|
-
url: <UPSTASH_REDIS_REST_URL>,
|
|
114
|
-
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
// or load directly from global env
|
|
119
|
-
|
|
120
|
-
// service worker
|
|
121
|
-
const redis = Redis.fromEnv()
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
// module worker
|
|
125
|
-
export default {
|
|
126
|
-
async fetch(request: Request, env: Bindings) {
|
|
127
|
-
const redis = Redis.fromEnv(env)
|
|
128
|
-
// ...
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
- [Code example service worker](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers)
|
|
134
|
-
- [Code example module worker](https://github.com/upstash/upstash-redis/tree/main/examples/cloudflare-workers-modules)
|
|
135
|
-
- [Documentation](https://docs.upstash.com/redis/tutorials/cloudflare_workers_with_redis)
|
|
136
|
-
|
|
137
|
-
#### Fastly
|
|
138
|
-
|
|
139
|
-
Fastly introduces a concept called
|
|
140
|
-
[backend](https://developer.fastly.com/reference/api/services/backend/). You
|
|
141
|
-
need to configure a backend in your `fastly.toml`. An example can be found
|
|
142
|
-
[here](https://github.com/upstash/upstash-redis/blob/main/examples/fastly/fastly.toml).
|
|
143
|
-
Until the fastly api stabilizes we recommend creating an instance manually:
|
|
144
|
-
|
|
145
|
-
```ts
|
|
146
|
-
import { Redis } from "@upstash/redis/fastly"
|
|
147
|
-
|
|
148
|
-
const redis = new Redis({
|
|
149
|
-
url: <UPSTASH_REDIS_REST_URL>,
|
|
150
|
-
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
151
|
-
backend: <BACKEND_NAME>,
|
|
152
|
-
})
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
- [Code example](https://github.com/upstash/upstash-redis/tree/main/examples/fastly)
|
|
156
|
-
- [Documentation](https://blog.upstash.com/fastly-compute-edge-with-redi)
|
|
157
|
-
|
|
158
|
-
#### Deno
|
|
159
|
-
|
|
160
|
-
Examples: [Deno Deploy](https://deno.com/deploy),
|
|
161
|
-
[Netlify Edge](https://www.netlify.com/products/edge/)
|
|
162
|
-
|
|
163
|
-
```ts
|
|
164
|
-
import { Redis } from "https://deno.land/x/upstash_redis/mod.ts"
|
|
165
|
-
|
|
166
|
-
const redis = new Redis({
|
|
167
|
-
url: <UPSTASH_REDIS_REST_URL>,
|
|
168
|
-
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
// or
|
|
172
|
-
const redis = Redis.fromEnv();
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
### Working with types
|
|
176
|
-
|
|
177
|
-
Most commands allow you to provide a type to make working with typescript
|
|
178
|
-
easier.
|
|
179
|
-
|
|
180
|
-
```ts
|
|
181
|
-
const data = await redis.get<MyCustomType>("key");
|
|
182
|
-
// data is typed as `MyCustomType`
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
## Migrating to v1
|
|
186
|
-
|
|
187
|
-
### Explicit authentication
|
|
188
|
-
|
|
189
|
-
The library is no longer automatically trying to load connection secrets from
|
|
190
|
-
environment variables. You must either supply them yourself:
|
|
46
|
+
## Basic Usage:
|
|
191
47
|
|
|
192
48
|
```ts
|
|
193
49
|
import { Redis } from "@upstash/redis"
|
|
@@ -196,146 +52,52 @@ const redis = new Redis({
|
|
|
196
52
|
url: <UPSTASH_REDIS_REST_URL>,
|
|
197
53
|
token: <UPSTASH_REDIS_REST_TOKEN>,
|
|
198
54
|
})
|
|
199
|
-
```
|
|
200
55
|
|
|
201
|
-
|
|
56
|
+
// string
|
|
57
|
+
await redis.set('key', 'value');
|
|
58
|
+
let data = await redis.get('key');
|
|
59
|
+
console.log(data)
|
|
202
60
|
|
|
203
|
-
|
|
204
|
-
// Nodejs
|
|
205
|
-
import { Redis } from "@upstash/redis";
|
|
206
|
-
const redis = Redis.fromEnv();
|
|
207
|
-
```
|
|
61
|
+
await redis.set('key2', 'value2', {ex: 1});
|
|
208
62
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
```
|
|
63
|
+
// sorted set
|
|
64
|
+
await redis.zadd('scores', { score: 1, member: 'team1' })
|
|
65
|
+
data = await redis.zrange('scores', 0, 100 )
|
|
66
|
+
console.log(data)
|
|
214
67
|
|
|
215
|
-
|
|
68
|
+
// list
|
|
69
|
+
await redis.lpush('elements', 'magnesium')
|
|
70
|
+
data = await redis.lrange('elements', 0, 100 )
|
|
71
|
+
console.log(data)
|
|
216
72
|
|
|
217
|
-
|
|
73
|
+
// hash
|
|
74
|
+
await redis.hset('people', {name: 'joe'})
|
|
75
|
+
data = await redis.hget('people', 'name' )
|
|
76
|
+
console.log(data)
|
|
218
77
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
throw new Error(error);
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// new
|
|
227
|
-
const data = await redis.set("key", "value"); // error is thrown automatically
|
|
78
|
+
// sets
|
|
79
|
+
await redis.sadd('animals', 'cat')
|
|
80
|
+
data = await redis.spop('animals', 1)
|
|
81
|
+
console.log(data)
|
|
228
82
|
```
|
|
229
83
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
`v1.0.0` introduces redis pipelines. Pipelining commands allows you to send a
|
|
233
|
-
single http request with multiple commands.
|
|
234
|
-
|
|
235
|
-
```ts
|
|
236
|
-
import { Redis } from "@upstash/redis";
|
|
237
|
-
|
|
238
|
-
const redis = new Redis({
|
|
239
|
-
/* auth */
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
const p = redis.pipeline();
|
|
84
|
+
### Upgrading to v1.4.0 **(ReferenceError: fetch is not defined)**
|
|
243
85
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
// or inline:
|
|
250
|
-
p.hset("key2", "field", { hello: "world" }).hvals("key2");
|
|
251
|
-
|
|
252
|
-
// Execute the pipeline once you are done building it:
|
|
253
|
-
// `exec` returns an array where each element represents the response of a command in the pipeline.
|
|
254
|
-
// You can optionally provide a type like this to get a typed response.
|
|
255
|
-
const res = await p.exec<[Type1, Type2, Type3]>();
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
For more information about pipelines using REST see
|
|
259
|
-
[here](https://blog.upstash.com/pipeline).
|
|
260
|
-
|
|
261
|
-
### Advanced
|
|
262
|
-
|
|
263
|
-
A low level `Command` class can be imported from `@upstash/redis` in case you
|
|
264
|
-
need more control about types and or (de)serialization.
|
|
265
|
-
|
|
266
|
-
By default all objects you are storing in redis are serialized using
|
|
267
|
-
`JSON.stringify` and recursively deserialized as well. Here's an example how you
|
|
268
|
-
could customize that behaviour. Keep in mind that you need to provide a `fetch`
|
|
269
|
-
polyfill if you are running on nodejs. We recommend
|
|
270
|
-
[isomorphic-fetch](https://www.npmjs.com/package/isomorphic-fetch).
|
|
271
|
-
|
|
272
|
-
```ts
|
|
273
|
-
import { Command } from "@upstash/redis/commands"
|
|
274
|
-
import { HttpClient } from "@upstash/redis/http"
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* TData represents what the user will enter or receive,
|
|
278
|
-
* TResult is the raw data returned from upstash, which may need to be
|
|
279
|
-
* transformed or parsed.
|
|
280
|
-
*/
|
|
281
|
-
const deserialize: (raw: TResult) => TData = ...
|
|
282
|
-
|
|
283
|
-
class CustomGetCommand<TData, TResult> extends Command<TData | null, TResult | null> {
|
|
284
|
-
constructor(key: string, ) {
|
|
285
|
-
super(["get", key], { deserialize })
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const client = new HttpClient({
|
|
290
|
-
baseUrl: <UPSTASH_REDIS_REST_URL>,
|
|
291
|
-
headers: {
|
|
292
|
-
authorization: `Bearer ${<UPSTASH_REDIS_REST_TOKEN>}`,
|
|
293
|
-
},
|
|
294
|
-
})
|
|
295
|
-
|
|
296
|
-
const res = new CustomGetCommand("key").exec(client)
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
### Additional information
|
|
300
|
-
|
|
301
|
-
#### `keepalive`
|
|
302
|
-
|
|
303
|
-
`@upstash/redis` is capable of reusing connections where possible to minimize
|
|
304
|
-
latency. Connections can be reused if the client is stored in memory and not
|
|
305
|
-
initialized with every new function invocation. The easiest way to achieve this
|
|
306
|
-
is by creating the client outside of your handler and adding an https agent:
|
|
307
|
-
|
|
308
|
-
```ts
|
|
309
|
-
// Nextjs api route
|
|
310
|
-
import { Redis } from "@upstash/redis";
|
|
311
|
-
import https from "https";
|
|
312
|
-
|
|
313
|
-
const redis = Redis.fromEnv({
|
|
314
|
-
agent: new https.Agent({ keepAlive: true }),
|
|
315
|
-
});
|
|
86
|
+
If you are running on nodejs v17 and earlier, `fetch` will not be natively
|
|
87
|
+
supported. Platforms like Vercel, Netlify, Deno, Fastly etc. provide a polyfill
|
|
88
|
+
for you. But if you are running on bare node, you need to either specify a
|
|
89
|
+
polyfill yourself or change the import path to:
|
|
316
90
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
91
|
+
```typescript
|
|
92
|
+
import { Redis } from "@upstash/redis/with-fetch";
|
|
320
93
|
```
|
|
321
94
|
|
|
322
|
-
|
|
323
|
-
initialized and the previously established connection to upstash is reused.
|
|
324
|
-
|
|
325
|
-
#### Javascript MAX_SAFE_INTEGER
|
|
326
|
-
|
|
327
|
-
Javascript can not handle numbers larger than `2^53 -1` safely and would return
|
|
328
|
-
wrong results when trying to deserialize them. In these cases the default
|
|
329
|
-
deserializer will return them as string instead. This might cause a mismatch
|
|
330
|
-
with your custom types.
|
|
331
|
-
|
|
332
|
-
```ts
|
|
333
|
-
await redis.set("key", "101600000000150081467");
|
|
334
|
-
const res = await redis<number>("get");
|
|
335
|
-
```
|
|
95
|
+
### Upgrading from v0.2.0?
|
|
336
96
|
|
|
337
|
-
|
|
338
|
-
|
|
97
|
+
Please read the
|
|
98
|
+
[migration guide](https://docs.upstash.com/redis/sdks/javascriptsdk/migration).
|
|
99
|
+
For further explanation we wrote a
|
|
100
|
+
[blog post](https://blog.upstash.com/upstash-redis-sdk-v1).
|
|
339
101
|
|
|
340
102
|
## Docs
|
|
341
103
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Command } from "./command.js";
|
|
2
|
+
/**
|
|
3
|
+
* @see https://redis.io/commands/lpos
|
|
4
|
+
*/
|
|
5
|
+
export class LPosCommand extends Command {
|
|
6
|
+
constructor(cmd, opts) {
|
|
7
|
+
const args = ["lpos", cmd[0], cmd[1]];
|
|
8
|
+
if (typeof cmd[2]?.rank === "number") {
|
|
9
|
+
args.push("rank", cmd[2].rank);
|
|
10
|
+
}
|
|
11
|
+
if (typeof cmd[2]?.count === "number") {
|
|
12
|
+
args.push("count", cmd[2].count);
|
|
13
|
+
}
|
|
14
|
+
if (typeof cmd[2]?.maxLen === "number") {
|
|
15
|
+
args.push("maxLen", cmd[2].maxLen);
|
|
16
|
+
}
|
|
17
|
+
super(args, opts);
|
|
18
|
+
}
|
|
19
|
+
}
|
package/esm/pkg/commands/mod.js
CHANGED
|
@@ -5,16 +5,7 @@ import { Command } from "./command.js";
|
|
|
5
5
|
export class ScriptExistsCommand extends Command {
|
|
6
6
|
constructor(hashes, opts) {
|
|
7
7
|
super(["script", "exists", ...hashes], {
|
|
8
|
-
deserialize: (result) =>
|
|
9
|
-
/**
|
|
10
|
-
* This isn't very pretty but it does the job.
|
|
11
|
-
* The user facing api is clean and will return a single `string` if they provided
|
|
12
|
-
* a single script hash, and an array of strings of the same length when given an
|
|
13
|
-
* array of hashes.
|
|
14
|
-
*/
|
|
15
|
-
const parsed = result;
|
|
16
|
-
return parsed.length === 1 ? parsed[0] : parsed;
|
|
17
|
-
},
|
|
8
|
+
deserialize: (result) => result,
|
|
18
9
|
...opts,
|
|
19
10
|
});
|
|
20
11
|
}
|
|
@@ -12,6 +12,12 @@ export class ZRangeCommand extends Command {
|
|
|
12
12
|
if (opts?.byLex) {
|
|
13
13
|
command.push("bylex");
|
|
14
14
|
}
|
|
15
|
+
if (opts?.rev) {
|
|
16
|
+
command.push("rev");
|
|
17
|
+
}
|
|
18
|
+
if (typeof opts?.count !== "undefined" && typeof opts?.offset !== "undefined") {
|
|
19
|
+
command.push("limit", opts.offset, opts.count);
|
|
20
|
+
}
|
|
15
21
|
if (opts?.withScores) {
|
|
16
22
|
command.push("withscores");
|
|
17
23
|
}
|
package/esm/pkg/http.js
CHANGED
|
@@ -19,14 +19,30 @@ export class HttpClient {
|
|
|
19
19
|
writable: true,
|
|
20
20
|
value: void 0
|
|
21
21
|
});
|
|
22
|
+
Object.defineProperty(this, "retry", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: void 0
|
|
27
|
+
});
|
|
22
28
|
this.baseUrl = config.baseUrl.replace(/\/$/, "");
|
|
23
29
|
this.headers = { "Content-Type": "application/json", ...config.headers };
|
|
24
|
-
this.options = config.options;
|
|
30
|
+
this.options = { backend: config.options?.backend };
|
|
31
|
+
if (typeof config?.retry === "boolean" && config?.retry === false) {
|
|
32
|
+
this.retry = {
|
|
33
|
+
attempts: 1,
|
|
34
|
+
backoff: () => 0,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
this.retry = {
|
|
39
|
+
attempts: config?.retry?.retries ?? 5,
|
|
40
|
+
backoff: config?.retry?.backoff ??
|
|
41
|
+
((retryCount) => Math.exp(retryCount) * 50),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
25
44
|
}
|
|
26
45
|
async request(req) {
|
|
27
|
-
if (!req.path) {
|
|
28
|
-
req.path = [];
|
|
29
|
-
}
|
|
30
46
|
const requestOptions = {
|
|
31
47
|
method: "POST",
|
|
32
48
|
headers: this.headers,
|
|
@@ -37,9 +53,21 @@ export class HttpClient {
|
|
|
37
53
|
*/
|
|
38
54
|
backend: this.options?.backend,
|
|
39
55
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
56
|
+
let res = null;
|
|
57
|
+
let error = null;
|
|
58
|
+
for (let i = 0; i <= this.retry.attempts; i++) {
|
|
59
|
+
try {
|
|
60
|
+
res = await fetch([this.baseUrl, ...(req.path ?? [])].join("/"), requestOptions);
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
error = err;
|
|
65
|
+
await new Promise((r) => setTimeout(r, this.retry.backoff(i)));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (!res) {
|
|
69
|
+
throw error ?? new Error("Exhausted all retries");
|
|
70
|
+
}
|
|
43
71
|
const body = (await res.json());
|
|
44
72
|
if (!res.ok) {
|
|
45
73
|
throw new UpstashError(body.error);
|
package/esm/pkg/pipeline.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppendCommand, BitCountCommand, BitOpCommand, BitPosCommand, DBSizeCommand, DecrByCommand, DecrCommand, DelCommand, EchoCommand, EvalCommand, EvalshaCommand, ExistsCommand, ExpireAtCommand, ExpireCommand, FlushAllCommand, FlushDBCommand, GetBitCommand, GetCommand, GetRangeCommand, GetSetCommand, HDelCommand, HExistsCommand, HGetAllCommand, HGetCommand, HIncrByCommand, HIncrByFloatCommand, HKeysCommand, HLenCommand, HMGetCommand, HMSetCommand, HScanCommand, HSetCommand, HSetNXCommand, HStrLenCommand, HValsCommand, IncrByCommand, IncrByFloatCommand, IncrCommand, KeysCommand, LIndexCommand, LInsertCommand, LLenCommand, LPopCommand, LPushCommand, LPushXCommand, LRangeCommand, LRemCommand, LSetCommand, LTrimCommand, MGetCommand, MSetCommand, MSetNXCommand, PersistCommand, PExpireAtCommand, PExpireCommand, PingCommand, PSetEXCommand, PTtlCommand, PublishCommand, RandomKeyCommand, RenameCommand, RenameNXCommand, RPopCommand, RPushCommand, RPushXCommand, SAddCommand, ScanCommand, SCardCommand, ScriptExistsCommand, ScriptFlushCommand, ScriptLoadCommand, SDiffCommand, SDiffStoreCommand, SetBitCommand, SetCommand, SetExCommand, SetNxCommand, SetRangeCommand, SInterCommand, SInterStoreCommand, SIsMemberCommand, SMembersCommand, SMoveCommand, SPopCommand, SRandMemberCommand, SRemCommand, SScanCommand, StrLenCommand, SUnionCommand, SUnionStoreCommand, TimeCommand, TouchCommand, TtlCommand, TypeCommand, UnlinkCommand, ZAddCommand, ZCardCommand, ZCountCommand, ZIncrByCommand, ZInterStoreCommand, ZLexCountCommand, ZPopMaxCommand, ZPopMinCommand, ZRangeCommand, ZRankCommand, ZRemCommand, ZRemRangeByLexCommand, ZRemRangeByRankCommand, ZRemRangeByScoreCommand, ZRevRankCommand, ZScanCommand, ZScoreCommand, ZUnionStoreCommand, } from "./commands/mod.js";
|
|
1
|
+
import { AppendCommand, BitCountCommand, BitOpCommand, BitPosCommand, DBSizeCommand, DecrByCommand, DecrCommand, DelCommand, EchoCommand, EvalCommand, EvalshaCommand, ExistsCommand, ExpireAtCommand, ExpireCommand, FlushAllCommand, FlushDBCommand, GetBitCommand, GetCommand, GetRangeCommand, GetSetCommand, HDelCommand, HExistsCommand, HGetAllCommand, HGetCommand, HIncrByCommand, HIncrByFloatCommand, HKeysCommand, HLenCommand, HMGetCommand, HMSetCommand, HScanCommand, HSetCommand, HSetNXCommand, HStrLenCommand, HValsCommand, IncrByCommand, IncrByFloatCommand, IncrCommand, KeysCommand, LIndexCommand, LInsertCommand, LLenCommand, LPopCommand, LPosCommand, LPushCommand, LPushXCommand, LRangeCommand, LRemCommand, LSetCommand, LTrimCommand, MGetCommand, MSetCommand, MSetNXCommand, PersistCommand, PExpireAtCommand, PExpireCommand, PingCommand, PSetEXCommand, PTtlCommand, PublishCommand, RandomKeyCommand, RenameCommand, RenameNXCommand, RPopCommand, RPushCommand, RPushXCommand, SAddCommand, ScanCommand, SCardCommand, ScriptExistsCommand, ScriptFlushCommand, ScriptLoadCommand, SDiffCommand, SDiffStoreCommand, SetBitCommand, SetCommand, SetExCommand, SetNxCommand, SetRangeCommand, SInterCommand, SInterStoreCommand, SIsMemberCommand, SMembersCommand, SMoveCommand, SPopCommand, SRandMemberCommand, SRemCommand, SScanCommand, StrLenCommand, SUnionCommand, SUnionStoreCommand, TimeCommand, TouchCommand, TtlCommand, TypeCommand, UnlinkCommand, ZAddCommand, ZCardCommand, ZCountCommand, ZIncrByCommand, ZInterStoreCommand, ZLexCountCommand, ZPopMaxCommand, ZPopMinCommand, ZRangeCommand, ZRankCommand, ZRemCommand, ZRemRangeByLexCommand, ZRemRangeByRankCommand, ZRemRangeByScoreCommand, ZRevRankCommand, ZScanCommand, ZScoreCommand, ZUnionStoreCommand, } from "./commands/mod.js";
|
|
2
2
|
import { UpstashError } from "./error.js";
|
|
3
3
|
/**
|
|
4
4
|
* Upstash REST API supports command pipelining to send multiple commands in
|
|
@@ -475,6 +475,15 @@ export class Pipeline {
|
|
|
475
475
|
writable: true,
|
|
476
476
|
value: (...args) => this.chain(new LPopCommand(args, this.commandOptions))
|
|
477
477
|
});
|
|
478
|
+
/**
|
|
479
|
+
* @see https://redis.io/commands/lpos
|
|
480
|
+
*/
|
|
481
|
+
Object.defineProperty(this, "lpos", {
|
|
482
|
+
enumerable: true,
|
|
483
|
+
configurable: true,
|
|
484
|
+
writable: true,
|
|
485
|
+
value: (...args) => this.chain(new LPosCommand(args, this.commandOptions))
|
|
486
|
+
});
|
|
478
487
|
/**
|
|
479
488
|
* @see https://redis.io/commands/lpush
|
|
480
489
|
*/
|
package/esm/pkg/redis.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AppendCommand, BitCountCommand, BitOpCommand, BitPosCommand, DBSizeCommand, DecrByCommand, DecrCommand, DelCommand, EchoCommand, EvalCommand, EvalshaCommand, ExistsCommand, ExpireAtCommand, ExpireCommand, FlushAllCommand, FlushDBCommand, GetBitCommand, GetCommand, GetRangeCommand, GetSetCommand, HDelCommand, HExistsCommand, HGetAllCommand, HGetCommand, HIncrByCommand, HIncrByFloatCommand, HKeysCommand, HLenCommand, HMGetCommand, HMSetCommand, HScanCommand, HSetCommand, HSetNXCommand, HStrLenCommand, HValsCommand, IncrByCommand, IncrByFloatCommand, IncrCommand, KeysCommand, LIndexCommand, LInsertCommand, LLenCommand, LPopCommand, LPushCommand, LPushXCommand, LRangeCommand, LRemCommand, LSetCommand, LTrimCommand, MGetCommand, MSetCommand, MSetNXCommand, PersistCommand, PExpireAtCommand, PExpireCommand, PingCommand, PSetEXCommand, PTtlCommand, PublishCommand, RandomKeyCommand, RenameCommand, RenameNXCommand, RPopCommand, RPushCommand, RPushXCommand, SAddCommand, ScanCommand, SCardCommand, ScriptExistsCommand, ScriptFlushCommand, ScriptLoadCommand, SDiffCommand, SDiffStoreCommand, SetBitCommand, SetCommand, SetExCommand, SetNxCommand, SetRangeCommand, SInterCommand, SInterStoreCommand, SIsMemberCommand, SMembersCommand, SMoveCommand, SPopCommand, SRandMemberCommand, SRemCommand, SScanCommand, StrLenCommand, SUnionCommand, SUnionStoreCommand, TimeCommand, TouchCommand, TtlCommand, TypeCommand, UnlinkCommand, ZAddCommand, ZCardCommand, ZCountCommand, ZIncrByCommand, ZInterStoreCommand, ZLexCountCommand, ZPopMaxCommand, ZPopMinCommand, ZRangeCommand, ZRankCommand, ZRemCommand, ZRemRangeByLexCommand, ZRemRangeByRankCommand, ZRemRangeByScoreCommand, ZRevRankCommand, ZScanCommand, ZScoreCommand, ZUnionStoreCommand, } from "./commands/mod.js";
|
|
1
|
+
import { AppendCommand, BitCountCommand, BitOpCommand, BitPosCommand, DBSizeCommand, DecrByCommand, DecrCommand, DelCommand, EchoCommand, EvalCommand, EvalshaCommand, ExistsCommand, ExpireAtCommand, ExpireCommand, FlushAllCommand, FlushDBCommand, GetBitCommand, GetCommand, GetRangeCommand, GetSetCommand, HDelCommand, HExistsCommand, HGetAllCommand, HGetCommand, HIncrByCommand, HIncrByFloatCommand, HKeysCommand, HLenCommand, HMGetCommand, HMSetCommand, HScanCommand, HSetCommand, HSetNXCommand, HStrLenCommand, HValsCommand, IncrByCommand, IncrByFloatCommand, IncrCommand, KeysCommand, LIndexCommand, LInsertCommand, LLenCommand, LPopCommand, LPosCommand, LPushCommand, LPushXCommand, LRangeCommand, LRemCommand, LSetCommand, LTrimCommand, MGetCommand, MSetCommand, MSetNXCommand, PersistCommand, PExpireAtCommand, PExpireCommand, PingCommand, PSetEXCommand, PTtlCommand, PublishCommand, RandomKeyCommand, RenameCommand, RenameNXCommand, RPopCommand, RPushCommand, RPushXCommand, SAddCommand, ScanCommand, SCardCommand, ScriptExistsCommand, ScriptFlushCommand, ScriptLoadCommand, SDiffCommand, SDiffStoreCommand, SetBitCommand, SetCommand, SetExCommand, SetNxCommand, SetRangeCommand, SInterCommand, SInterStoreCommand, SIsMemberCommand, SMembersCommand, SMoveCommand, SPopCommand, SRandMemberCommand, SRemCommand, SScanCommand, StrLenCommand, SUnionCommand, SUnionStoreCommand, TimeCommand, TouchCommand, TtlCommand, TypeCommand, UnlinkCommand, ZAddCommand, ZCardCommand, ZCountCommand, ZIncrByCommand, ZInterStoreCommand, ZLexCountCommand, ZPopMaxCommand, ZPopMinCommand, ZRangeCommand, ZRankCommand, ZRemCommand, ZRemRangeByLexCommand, ZRemRangeByRankCommand, ZRemRangeByScoreCommand, ZRevRankCommand, ZScanCommand, ZScoreCommand, ZUnionStoreCommand, } from "./commands/mod.js";
|
|
2
2
|
import { Pipeline } from "./pipeline.js";
|
|
3
3
|
/**
|
|
4
4
|
* Serverless redis client for upstash.
|
|
@@ -28,6 +28,18 @@ export class Redis {
|
|
|
28
28
|
writable: true,
|
|
29
29
|
value: void 0
|
|
30
30
|
});
|
|
31
|
+
/**
|
|
32
|
+
* Wrap a new middleware around the HTTP client.
|
|
33
|
+
*/
|
|
34
|
+
Object.defineProperty(this, "use", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: (middleware) => {
|
|
39
|
+
const makeRequest = this.client.request.bind(this.client);
|
|
40
|
+
this.client.request = (req) => middleware(req, makeRequest);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
31
43
|
/**
|
|
32
44
|
* Create a new pipeline that allows you to send requests in bulk.
|
|
33
45
|
*
|
|
@@ -426,6 +438,15 @@ export class Redis {
|
|
|
426
438
|
writable: true,
|
|
427
439
|
value: (...args) => new LPopCommand(args, this.opts).exec(this.client)
|
|
428
440
|
});
|
|
441
|
+
/**
|
|
442
|
+
* @see https://redis.io/commands/lpos
|
|
443
|
+
*/
|
|
444
|
+
Object.defineProperty(this, "lpos", {
|
|
445
|
+
enumerable: true,
|
|
446
|
+
configurable: true,
|
|
447
|
+
writable: true,
|
|
448
|
+
value: (...args) => new LPosCommand(args, this.opts).exec(this.client)
|
|
449
|
+
});
|
|
429
450
|
/**
|
|
430
451
|
* @see https://redis.io/commands/lpush
|
|
431
452
|
*/
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as core from "../pkg/redis.js";
|
|
2
|
-
import {
|
|
2
|
+
import { HttpClient } from "../pkg/http.js";
|
|
3
3
|
/**
|
|
4
4
|
* Serverless redis client for upstash.
|
|
5
5
|
*/
|
|
@@ -26,7 +26,8 @@ export class Redis extends core.Redis {
|
|
|
26
26
|
/\r|\n/.test(config.token)) {
|
|
27
27
|
console.warn("The redis token contains whitespace or newline, which can cause errors!");
|
|
28
28
|
}
|
|
29
|
-
const client =
|
|
29
|
+
const client = new HttpClient({
|
|
30
|
+
retry: config.retry,
|
|
30
31
|
baseUrl: config.url,
|
|
31
32
|
headers: { authorization: `Bearer ${config.token}` },
|
|
32
33
|
});
|
|
@@ -44,9 +45,8 @@ export class Redis extends core.Redis {
|
|
|
44
45
|
* ```ts
|
|
45
46
|
* const redis = Redis.fromEnv(env)
|
|
46
47
|
* ```
|
|
47
|
-
*
|
|
48
48
|
*/
|
|
49
|
-
static fromEnv(env) {
|
|
49
|
+
static fromEnv(env, opts) {
|
|
50
50
|
// @ts-ignore These will be defined by cloudflare
|
|
51
51
|
const url = env?.UPSTASH_REDIS_REST_URL ?? UPSTASH_REDIS_REST_URL;
|
|
52
52
|
// @ts-ignore These will be defined by cloudflare
|
|
@@ -57,26 +57,6 @@ export class Redis extends core.Redis {
|
|
|
57
57
|
if (!token) {
|
|
58
58
|
throw new Error("Unable to find environment variable: `UPSTASH_REDIS_REST_TOKEN`. Please add it via `wrangler secret put UPSTASH_REDIS_REST_TOKEN`");
|
|
59
59
|
}
|
|
60
|
-
return new Redis({ url, token });
|
|
60
|
+
return new Redis({ ...opts, url, token });
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
function defaultRequester(config) {
|
|
64
|
-
return {
|
|
65
|
-
request: async function (req) {
|
|
66
|
-
if (!req.path) {
|
|
67
|
-
req.path = [];
|
|
68
|
-
}
|
|
69
|
-
const res = await fetch([config.baseUrl, ...req.path].join("/"), {
|
|
70
|
-
method: "POST",
|
|
71
|
-
headers: { "Content-Type": "application/json", ...config.headers },
|
|
72
|
-
body: JSON.stringify(req.body),
|
|
73
|
-
keepalive: true,
|
|
74
|
-
});
|
|
75
|
-
const body = (await res.json());
|
|
76
|
-
if (!res.ok) {
|
|
77
|
-
throw new UpstashError(body.error);
|
|
78
|
-
}
|
|
79
|
-
return body;
|
|
80
|
-
},
|
|
81
|
-
};
|
|
82
|
-
}
|
package/esm/platforms/fastly.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as core from "../pkg/redis.js";
|
|
2
|
-
import {
|
|
2
|
+
import { HttpClient } from "../pkg/http.js";
|
|
3
3
|
/**
|
|
4
4
|
* Serverless redis client for upstash.
|
|
5
5
|
*/
|
|
@@ -27,35 +27,14 @@ export class Redis extends core.Redis {
|
|
|
27
27
|
/\r|\n/.test(config.token)) {
|
|
28
28
|
console.warn("The redis token contains whitespace or newline, which can cause errors!");
|
|
29
29
|
}
|
|
30
|
-
const client =
|
|
30
|
+
const client = new HttpClient({
|
|
31
31
|
baseUrl: config.url,
|
|
32
|
+
retry: config.retry,
|
|
32
33
|
headers: { authorization: `Bearer ${config.token}` },
|
|
33
|
-
backend: config.backend,
|
|
34
|
+
options: { backend: config.backend },
|
|
34
35
|
});
|
|
35
36
|
super(client, {
|
|
36
37
|
automaticDeserialization: config.automaticDeserialization,
|
|
37
38
|
});
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
|
-
function defaultRequester(config) {
|
|
41
|
-
return {
|
|
42
|
-
request: async function (req) {
|
|
43
|
-
if (!req.path) {
|
|
44
|
-
req.path = [];
|
|
45
|
-
}
|
|
46
|
-
const res = await fetch([config.baseUrl, ...req.path].join("/"), {
|
|
47
|
-
method: "POST",
|
|
48
|
-
headers: { "Content-Type": "application/json", ...config.headers },
|
|
49
|
-
body: JSON.stringify(req.body),
|
|
50
|
-
keepalive: true,
|
|
51
|
-
// @ts-expect-error fastly requires `backend`
|
|
52
|
-
backend: config.backend,
|
|
53
|
-
});
|
|
54
|
-
const body = (await res.json());
|
|
55
|
-
if (!res.ok) {
|
|
56
|
-
throw new UpstashError(body.error);
|
|
57
|
-
}
|
|
58
|
-
return body;
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
}
|