-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
268 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: '3.1' | ||
services: | ||
|
||
redis: | ||
build: | ||
context: . | ||
dockerfile: ./docker/redis/Dockerfile | ||
restart: always | ||
ports: | ||
- "6379:6379" | ||
environment: | ||
REDIS_PASSWORD: redis | ||
volumes: | ||
- redis:/data | ||
|
||
volumes: | ||
postgres: | ||
redis: |
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,6 @@ | ||
FROM redis:7-alpine | ||
|
||
COPY docker/redis/redis.conf /usr/local/etc/redis/redis.conf | ||
COPY docker/redis/start.sh /usr/bin/start.sh | ||
|
||
CMD ["/usr/bin/start.sh"] |
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,18 @@ | ||
# Important Redis defaults (for reference): | ||
# ----------------------------------------- | ||
# bind 127.0.0.1 -::1 | ||
# port 6379 | ||
# save 3600 1 | ||
# save 300 100 | ||
# save 60 10000 | ||
# appendonly no | ||
# dir /var/lib/redis | ||
# maxmemory <no default!> | ||
# maxmemory-policy noeviction | ||
|
||
bind 0.0.0.0 -::1 | ||
|
||
dir /data | ||
maxmemory-policy noeviction | ||
maxmemory 460mb | ||
appendonly no |
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,17 @@ | ||
#!/bin/sh | ||
|
||
# This script is inspired by https://github.com/mra-clubhouse/fly-redis/tree/custom-redis. | ||
|
||
# Clear environment variables. | ||
set -e | ||
|
||
sysctl vm.overcommit_memory=1 || true | ||
sysctl net.core.somaxconn=1024 || true | ||
|
||
if [[ -z "${REDIS_PASSWORD}" ]]; then | ||
echo "The REDIS_PASSWORD environment variable is required" | ||
exit 1 | ||
fi | ||
|
||
|
||
redis-server /usr/local/etc/redis/redis.conf --requirepass $REDIS_PASSWORD |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export { InMemorySessions } from './inmemory' | ||
export { RedisSessions } from './redis' | ||
export { InMemorySessions } from './stores/inmemory' | ||
export { RedisSessions } from './stores/redis' | ||
|
||
export { SessionUtils } from './utils' | ||
|
||
export type { ISessions, Session, SessionId } from './types' |
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
Empty file.
Empty file.
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,143 @@ | ||
import { InMemorySessions } from '../src/stores/inmemory' | ||
import { SessionUtils } from '../src/utils' | ||
import { filter } from './utils' | ||
|
||
describe('inmemory', () => { | ||
test('correctly finds user from a given session id', async () => { | ||
const sessions = new InMemorySessions<string, {}>() | ||
|
||
const validSessionId = await sessions.createSession({ | ||
userId: 'user-id', | ||
label: 'test', | ||
meta: {}, | ||
}) | ||
|
||
await expect(sessions.getUserIdFromSession(validSessionId)).resolves.not.toBeNull() | ||
|
||
const invalidSessionId = SessionUtils.toSessionId('invalid-session-id') | ||
|
||
await expect(sessions.getUserIdFromSession(invalidSessionId)).resolves.toBeNull() | ||
}) | ||
|
||
test('correctly parses userId from a given session id', async () => { | ||
const sessions = new InMemorySessions<string, {}>() | ||
|
||
const userId = 'user-id' | ||
const sessionId = await sessions.createSession({ | ||
userId, | ||
label: 'test', | ||
meta: {}, | ||
}) | ||
|
||
await expect(sessions.getUserIdFromSession(sessionId)).resolves.toEqual(userId) | ||
}) | ||
|
||
test('correctly parses meta from a given session id', async () => { | ||
// NOTE: We want to test all serializable types here. | ||
|
||
const sessions = new InMemorySessions< | ||
string, | ||
{ | ||
string: string | ||
number: number | ||
boolean: boolean | ||
array: string[] | ||
object: { [key: string]: string } | ||
} | ||
>() | ||
|
||
const meta = { | ||
string: 'string', | ||
number: 1, | ||
boolean: true, | ||
array: ['string', 'string'], | ||
object: { | ||
string: 'string', | ||
}, | ||
} | ||
|
||
const sessionId = await sessions.createSession({ | ||
userId: 'user-id', | ||
label: 'test', | ||
meta, | ||
}) | ||
|
||
await expect(sessions.getSessionMeta(sessionId)).resolves.toEqual(meta) | ||
}) | ||
|
||
test('correctly destroys a session', async () => { | ||
const sessions = new InMemorySessions<string, {}>() | ||
|
||
const sessionId = await sessions.createSession({ | ||
userId: 'user-id', | ||
label: 'test', | ||
meta: {}, | ||
}) | ||
|
||
await sessions.destroySession(sessionId) | ||
|
||
await expect(sessions.getUserIdFromSession(sessionId)).resolves.toBeNull() | ||
}) | ||
|
||
test('correctly lists all sessions', async () => { | ||
const sessions = new InMemorySessions<string, {}>() | ||
|
||
await sessions.createSession({ userId: 'user-id', label: '#1', meta: {} }) | ||
await sessions.createSession({ userId: 'user-id', label: '#2', meta: {} }) | ||
await sessions.createSession({ userId: 'user-id', label: '#3', meta: {} }) | ||
|
||
await expect( | ||
// We filter out random values and dates. | ||
sessions.listSessions().then((r) => r.map((o) => filter(o, ['label', 'userId']))), | ||
).resolves.toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"label": "#1", | ||
"userId": "user-id", | ||
}, | ||
{ | ||
"label": "#2", | ||
"userId": "user-id", | ||
}, | ||
{ | ||
"label": "#3", | ||
"userId": "user-id", | ||
}, | ||
] | ||
`) | ||
}) | ||
|
||
test('correclty lists sessions for a given user', async () => { | ||
const sessions = new InMemorySessions<string, {}>() | ||
|
||
const userId = 'user-id' | ||
|
||
await sessions.createSession({ userId, label: '#1', meta: {} }) | ||
await sessions.createSession({ userId, label: '#2', meta: {} }) | ||
await sessions.createSession({ userId, label: '#3', meta: {} }) | ||
|
||
const otherUserId = 'other-user-id' | ||
await sessions.createSession({ userId: otherUserId, label: '#1', meta: {} }) | ||
await sessions.createSession({ userId: otherUserId, label: '#2', meta: {} }) | ||
|
||
await expect( | ||
// We filter out random values and dates. | ||
sessions.getSessionsForUser(userId).then((r) => r.map((o) => filter(o, ['label', 'userId']))), | ||
).resolves.toMatchInlineSnapshot(` | ||
[ | ||
{ | ||
"label": "#1", | ||
"userId": "user-id", | ||
}, | ||
{ | ||
"label": "#2", | ||
"userId": "user-id", | ||
}, | ||
{ | ||
"label": "#3", | ||
"userId": "user-id", | ||
}, | ||
] | ||
`) | ||
}) | ||
}) |
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,13 @@ | ||
import { SessionUtils } from '../src/utils' | ||
|
||
describe('utils', () => { | ||
test('correctly parses and formats session auth token', () => { | ||
const random = Math.random().toString(16) | ||
const sessionId = SessionUtils.toSessionId(random) | ||
|
||
const authToken = SessionUtils.getAuthTokenForSessionId(sessionId) | ||
const parsedSessionId = SessionUtils.getSessionIdFromAuthToken(authToken) | ||
|
||
expect(parsedSessionId).toEqual(sessionId) | ||
}) | ||
}) |
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,10 @@ | ||
/** | ||
* Filters out desired keys from an object. | ||
*/ | ||
export function filter<T extends object, KS extends keyof T>(obj: T, keys: KS[]): Pick<T, KS> { | ||
const result: any = {} | ||
for (const key of keys) { | ||
result[key] = obj[key] | ||
} | ||
return result | ||
} |