-
-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from n4ze3m/next
1.8.1
- Loading branch information
Showing
5 changed files
with
87 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// copied from https://github.com/glani/parse-redis-url-simple/blob/master/src/index.ts | ||
import { parse } from "url"; | ||
|
||
const redisDefaultPort = 6379; | ||
const sentinelDefaultPort = 26379; | ||
|
||
export interface IRedisUrl { | ||
database?: string; | ||
host: string; | ||
password?: string; | ||
port: number; | ||
} | ||
|
||
const predefinedSeparatorRegexp = /,|;|\s/; | ||
|
||
function preparePassword(auth: string | null, encoding?: BufferEncoding) { | ||
if (!auth) { | ||
return undefined; | ||
} | ||
|
||
const vv = (encoding ? Buffer.from(auth, encoding).toString() : auth).split( | ||
":" | ||
); | ||
return vv.length > 1 ? vv[1] : vv[0]; | ||
} | ||
|
||
function prepareResult( | ||
v: string, | ||
sentinel: boolean, | ||
encoding?: BufferEncoding | ||
): IRedisUrl { | ||
if (v.search("://") === -1) { | ||
v = "redis://" + v; | ||
} | ||
const urlWithStringQuery = parse(v); | ||
|
||
return { | ||
database: sentinel | ||
? undefined | ||
: (urlWithStringQuery.pathname || "/0").substr(1) || "0", | ||
host: urlWithStringQuery.hostname || "localhost", | ||
password: sentinel | ||
? undefined | ||
: preparePassword(urlWithStringQuery.auth, encoding), | ||
port: Number( | ||
urlWithStringQuery.port || | ||
(sentinel ? sentinelDefaultPort : redisDefaultPort) | ||
), | ||
}; | ||
} | ||
|
||
export function parseRedisUrl( | ||
value?: string, | ||
sentinel: boolean = false, | ||
separatorRegexp: RegExp = predefinedSeparatorRegexp, | ||
encoding?: BufferEncoding | ||
): IRedisUrl | undefined { | ||
if (!value) { | ||
return { | ||
database: sentinel ? undefined : "0", | ||
host: "localhost", | ||
port: sentinel ? sentinelDefaultPort : redisDefaultPort, | ||
}; | ||
} | ||
|
||
const result = new Array<IRedisUrl>(); | ||
const urlValues = value | ||
.split(separatorRegexp) | ||
.map((value1) => value1.trim()) | ||
.filter((value1) => value1 && value1.length); | ||
|
||
for (const urlValue of urlValues) { | ||
const parsedResult = prepareResult(urlValue, sentinel, encoding); | ||
result.push(parsedResult); | ||
} | ||
|
||
return result.length > 0 ? result[0] : undefined; | ||
} |