Repository navigation
chore: add npm run repl with a connected client #3566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Opens a Node REPL with a connected `client` in scope. | ||
| // Uses the built packages, so run `npm run build` after source changes. | ||
| // Target another server with `npm run repl -- --host <host> --port <port>` | ||
| // (either flag alone is fine) or with REDIS_URL. Flags win over REDIS_URL. | ||
| import repl from 'node:repl'; | ||
| import { parseArgs } from 'node:util'; | ||
| import { createClient } from 'redis'; | ||
|
|
||
| const { values: { host, port } } = parseArgs({ | ||
| options: { | ||
| host: { type: 'string', short: 'h' }, | ||
| port: { type: 'string', short: 'p' } | ||
| } | ||
| }); | ||
|
|
||
| const url = host || port | ||
| ? `redis://${host ?? 'localhost'}:${port ?? 6379}` | ||
| : process.env.REDIS_URL ?? 'redis://localhost:6379'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IPv6 hosts build invalid Redis URLsLow Severity
Reviewed by Cursor Bugbot for commit 69ee000. Configure here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IPv6 hosts produce invalid URLsLow Severity Flag-based URL construction interpolates Reviewed by Cursor Bugbot for commit c3e36dd. Configure here. |
||
| const client = createClient({ url }); | ||
| // Connection errors such as ECONNREFUSED carry the reason in `code`, not `message`. | ||
| const reason = err => err.code ?? err.message; | ||
|
|
||
| // Exit if the first connect fails; after that, keep the default reconnects. | ||
| const onConnectError = err => { | ||
| console.error(`Could not connect to ${url}: ${reason(err)}`); | ||
| process.exit(1); | ||
| }; | ||
| client.once('error', onConnectError); | ||
| await client.connect(); | ||
| client.off('error', onConnectError); | ||
| client.on('error', err => console.error('Redis client error:', reason(err))); | ||
| console.log(`Connected to ${url}. Use \`client\`, e.g. await client.ping()`); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
|
|
||
| const server = repl.start('redis> '); | ||
| server.context.client = client; | ||
| server.on('exit', async () => { | ||
| client.destroy(); | ||
| process.exit(); | ||
| }); | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the new host flag is given a bare IPv6 literal such as
--host ::1, this buildsredis://::1:6379;RedisClient.parseURL()passes that to the WHATWG URL parser, which throwsERR_INVALID_URL, even though the client supports bracketed IPv6 Redis URLs. Detect and bracket IPv6 literals or pass host and port as socket options directly.Useful? React with 👍 / 👎.