Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@chronark/redis
Advanced tools
An HTTP/REST based Redis client built on top of Upstash REST API.
An HTTP/REST based Redis client built on top of Upstash REST API.
It is the only connectionless (HTTP based) Redis client and designed for:
See the list of APIs supported.
Please read the migration guide. For further explanation we wrote a blog post.
npm install @upstash/redis
Create a new redis database on upstash
We support various platforms, such as nodejs, cloudflare and fastly. Platforms
differ slightly when it comes to environment variables and their fetch
api.
Please use the correct import when deploying to special platforms.
Examples: Vercel, Netlify, AWS Lambda
If you are running on nodejs you can set UPSTASH_REDIS_REST_URL
and
UPSTASH_REDIS_REST_TOKEN
as environment variable and create a redis instance
like this:
import { Redis } from "@upstash/redis"
const redis = new Redis({
url: <UPSTASH_REDIS_REST_URL>,
token: <UPSTASH_REDIS_REST_TOKEN>,
})
// or load directly from env
const redis = Redis.fromEnv()
Cloudflare handles environment variables differently than nodejs. Please add
UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
using
wrangler secret put ...
or in the cloudflare dashboard.
Afterwards you can create a redis instance:
import { Redis } from "@upstash/redis/cloudflare"
const redis = new Redis({
url: <UPSTASH_REDIS_REST_URL>,
token: <UPSTASH_REDIS_REST_TOKEN>,
})
// or load directly from global env
// service worker
const redis = Redis.fromEnv()
// module worker
export default {
async fetch(request: Request, env: Bindings) {
const redis = Redis.fromEnv(env)
// ...
}
}
Fastly introduces a concept called
backend. You
need to configure a backend in your fastly.toml
. An example can be found
here.
Until the fastly api stabilizes we recommend creating an instance manually:
import { Redis } from "@upstash/redis/fastly"
const redis = new Redis({
url: <UPSTASH_REDIS_REST_URL>,
token: <UPSTASH_REDIS_REST_TOKEN>,
backend: <BACKEND_NAME>,
})
Most commands allow you to provide a type to make working with typescript easier.
const data = await redis.get<MyCustomType>("key");
// data is typed as `MyCustomType`
The library is no longer automatically trying to load connection secrets from environment variables. You must either supply them yourself:
import { Redis } from "@upstash/redis"
const redis = new Redis({
url: <UPSTASH_REDIS_REST_URL>,
token: <UPSTASH_REDIS_REST_TOKEN>,
})
Or use one of the static constructors to load from environment variables:
// Nodejs
import { Redis } from "@upstash/redis";
const redis = Redis.fromEnv();
// or when deploying to cloudflare workers
import { Redis } from "@upstash/redis/cloudflare";
const redis = Redis.fromEnv();
Errors are now thrown automatically instead of being returned to you.
// old
const { data, error } = await set("key", "value");
if (error) {
throw new Error(error);
}
// new
const data = await redis.set("key", "value"); // error is thrown automatically
v1.0.0
introduces redis pipelines. Pipelining commands allows you to send a
single http request with multiple commands.
import { Redis } from "@upstash/redis";
const redis = new Redis({
/* auth */
});
const p = redis.pipeline();
// Now you can chain multiple commands to create your pipeline:
p.set("key", 2);
p.incr("key");
// or inline:
p.hset("key2", "field", { hello: "world" }).hvals("key2");
// Execute the pipeline once you are done building it:
// `exec` returns an array where each element represents the response of a command in the pipeline.
// You can optionally provide a type like this to get a typed response.
const res = await p.exec<[Type1, Type2, Type3]>();
For more information about pipelines using REST see here.
A low level Command
class can be imported from @upstash/redis
in case you
need more control about types and or (de)serialization.
By default all objects you are storing in redis are serialized using
JSON.stringify
and recursively deserialized as well. Here's an example how you
could customize that behaviour. Keep in mind that you need to provide a fetch
polyfill if you are running on nodejs. We recommend
isomorphic-fetch.
import { Command } from "@upstash/redis/commands"
import { HttpClient } from "@upstash/redis/http"
/**
* TData represents what the user will enter or receive,
* TResult is the raw data returned from upstash, which may need to be
* transformed or parsed.
*/
const deserialize: (raw: TResult) => TData = ...
class CustomGetCommand<TData, TResult> extends Command<TData | null, TResult | null> {
constructor(key: string, ) {
super(["get", key], { deserialize })
}
}
const client = new HttpClient({
baseUrl: <UPSTASH_REDIS_REST_URL>,
headers: {
authorization: `Bearer ${<UPSTASH_REDIS_REST_TOKEN>}`,
},
})
const res = new CustomGetCommand("key").exec(client)
keepalive
@upstash/redis
is trying to reuse connections where possible to minimize
latency. Connections can be reused if the client is stored in memory and not
initialized with every new function invocation. The easiest way to achieve this
is by creating the client outside of your handler:
// Nextjs api route
import { Redis } from "@upstash/redis";
const redis = Redis.fromEnv();
export default async function (req, res) {
// use redis here
}
Whenever your hot lambda receives a new request the client is already initialized and the previously established connection to upstash is reused.
Javascript can not handle numbers larger than 2^53 -1
safely and would return
wrong results when trying to deserialize them. In these cases the default
deserializer will return them as string instead. This might cause a mismatch
with your custom types.
await redis.set("key", "101600000000150081467");
const res = await redis<number>("get");
In this example res
will still be a string despite the type annotation. Please
keep that in mind and adjust accordingly.
See the documentation for details.
pnpm install
Create a new redis database on upstash and copy
the url and token to .env
(See .env.example
for reference)
pnpm test
FAQs
An HTTP/REST based Redis client built on top of Upstash REST API.
The npm package @chronark/redis receives a total of 2 weekly downloads. As such, @chronark/redis popularity was classified as not popular.
We found that @chronark/redis demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.