1
0
mirror of synced 2025-12-25 02:17:36 -05:00

Merge branch 'main' of github.com:github/docs-internal into refactor-site-tree

This commit is contained in:
Sarah Schneider
2021-03-29 16:46:46 -04:00
13 changed files with 167 additions and 235 deletions

File diff suppressed because one or more lines are too long

View File

@@ -9081,7 +9081,7 @@
},
{
"name": "associatedPullRequests",
"description": "<p>The pull requests associated with a commit.</p>",
"description": "<p>The merged Pull Request that introduced the commit to the repository. If the\ncommit is not present in the default branch, additionally returns open Pull\nRequests associated with the commit.</p>",
"type": "PullRequestConnection",
"id": "pullrequestconnection",
"kind": "objects",

View File

@@ -8268,7 +8268,7 @@
},
{
"name": "associatedPullRequests",
"description": "<p>The pull requests associated with a commit.</p>",
"description": "<p>The merged Pull Request that introduced the commit to the repository. If the\ncommit is not present in the default branch, additionally returns open Pull\nRequests associated with the commit.</p>",
"type": "PullRequestConnection",
"id": "pullrequestconnection",
"kind": "objects",

View File

@@ -1,42 +1,21 @@
const Redis = require('ioredis')
const InMemoryRedis = require('ioredis-mock')
const createRedisClient = require('./redis/create-client')
const InMemoryRedis = require('redis-mock')
const { promisify } = require('util')
const { CI, NODE_ENV, REDIS_URL, REDIS_MAX_DB } = process.env
const { CI, NODE_ENV, REDIS_URL } = process.env
// Do not use real a Redis client for CI, tests, or if the REDIS_URL is not provided
const useRealRedis = !CI && NODE_ENV !== 'test' && !!REDIS_URL
// By default, every Redis instance supports database numbers 0 - 15
const redisMaxDb = REDIS_MAX_DB || 15
// Enable better stack traces in non-production environments
const redisBaseOptions = {
showFriendlyErrorStack: NODE_ENV !== 'production'
}
class RedisAccessor {
constructor ({ databaseNumber = 0, prefix = null, allowSetFailures = false } = {}) {
if (!Number.isInteger(databaseNumber) || databaseNumber < 0 || databaseNumber > redisMaxDb) {
throw new TypeError(
`Redis database number must be an integer between 0 and ${redisMaxDb} but was: ${JSON.stringify(databaseNumber)}`
)
}
constructor ({ databaseNumber = 0, prefix = null, allowSetFailures = false, name = null } = {}) {
const redisClient = useRealRedis
? new Redis(REDIS_URL, {
...redisBaseOptions,
? createRedisClient({
url: REDIS_URL,
db: databaseNumber,
// Only add this configuration for TLS-enabled REDIS_URL values.
// Otherwise, it breaks for local Redis instances without TLS enabled.
...REDIS_URL.startsWith('rediss://') && {
tls: {
// Required for production Heroku Redis
rejectUnauthorized: false
}
}
name: name || 'redis-accessor'
})
: new InMemoryRedis()
: InMemoryRedis.createClient()
this._client = redisClient
@@ -96,6 +75,7 @@ class RedisAccessor {
}
async set (key, value, options = {}) {
const setAsync = promisify(this._client.set).bind(this._client)
const fullKey = this.prefix(key)
if (typeof value !== 'string' || !value) {
@@ -106,7 +86,7 @@ class RedisAccessor {
const setArgs = this.constructor.translateSetArguments(options)
try {
const result = await this._client.set(fullKey, value, ...setArgs)
const result = await setAsync(fullKey, value, ...setArgs)
return result === 'OK'
} catch (err) {
const errorText = `Failed to set value in Redis.
@@ -124,14 +104,10 @@ Error: ${err.message}`
}
async get (key) {
const value = await this._client.get(this.prefix(key))
const getAsync = promisify(this._client.get).bind(this._client)
const value = await getAsync(this.prefix(key))
return value
}
async exists (key) {
const result = await this._client.exists(this.prefix(key))
return result === 1
}
}
module.exports = RedisAccessor

View File

@@ -6,10 +6,12 @@ const { REDIS_MIN_DB, REDIS_MAX_DB } = process.env
const redisMinDb = REDIS_MIN_DB || 0
const redisMaxDb = REDIS_MAX_DB || 15
function formatRedisError (error = {}) {
const { code } = error
const preamble = error.constructor.name + (code ? ` with code "${code}"` : '')
return preamble + error.toString()
function formatRedisError (error) {
const errorCode = error ? error.code : null
const errorName = error ? error.constructor.name : 'Error'
const errorMsg = error ? error.toString() : 'unknown error'
const preamble = errorName + (errorCode ? ` with code "${errorCode}"` : '')
return preamble + ': ' + errorMsg
}
module.exports = function createClient (options = {}) {
@@ -45,7 +47,7 @@ module.exports = function createClient (options = {}) {
})
// If a `name` was provided, use it in the prefix for logging event messages
const logPrefix = '[redis' + (name ? ` (${name})` : '') + '] '
const logPrefix = '[redis' + (name ? ` (${name})` : '') + ']'
// Add event listeners for basic logging
client.on('connect', () => { console.log(logPrefix, 'Connection opened') })