-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
do not delete entries from other clients
- Loading branch information
Roy Razon
committed
Sep 10, 2023
1 parent
0d983e1
commit ac9a32f
Showing
8 changed files
with
241 additions
and
121 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 was deleted.
Oops, something went wrong.
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,68 @@ | ||
import { describe, expect, it, beforeEach } from '@jest/globals' | ||
import { ArrayMap, arrayMap } from './array-map' | ||
|
||
describe('arrayMap', () => { | ||
let a: ArrayMap<string, { x: number }> | ||
const expectedValues = [{ x: 12 }, { x: 13 }] as const | ||
beforeEach(() => { | ||
a = arrayMap() | ||
a.add('foo', expectedValues[0]) | ||
a.add('foo', expectedValues[1]) | ||
}) | ||
|
||
describe('when the key does not exist', () => { | ||
it('returns undefined', () => { | ||
expect(arrayMap().get('bar')).toBeUndefined() | ||
}) | ||
}) | ||
|
||
describe('when the key exists', () => { | ||
let values: { x: number }[] | undefined | ||
beforeEach(() => { | ||
values = a.get('foo') | ||
}) | ||
it('returns the values', () => { | ||
expect(values).toBeDefined() | ||
expect(values).toHaveLength(2) | ||
expect(values).toContain(expectedValues[0]) | ||
expect(values).toContain(expectedValues[1]) | ||
}) | ||
|
||
describe('when delete is called with a predicate that returns false for everything', () => { | ||
beforeEach(() => { | ||
a.delete('foo', () => false) | ||
values = a.get('foo') | ||
}) | ||
it('does not delete the values', () => { | ||
expect(values).toBeDefined() | ||
expect(values).toHaveLength(2) | ||
expect(values).toContain(expectedValues[0]) | ||
expect(values).toContain(expectedValues[1]) | ||
}) | ||
}) | ||
|
||
describe('when delete is called with a predicate that returns true for everything', () => { | ||
beforeEach(() => { | ||
a.delete('foo', () => true) | ||
values = a.get('foo') | ||
}) | ||
it('deletes the values', () => { | ||
expect(values).toBeUndefined() | ||
}) | ||
}) | ||
|
||
describe('when delete is called with a predicate that returns true for a specific value', () => { | ||
beforeEach(() => { | ||
a.delete('foo', ({ x }) => x === expectedValues[0].x) | ||
values = a.get('foo') | ||
}) | ||
|
||
it('deletes the specific value', () => { | ||
expect(values).toBeDefined() | ||
expect(values).toHaveLength(1) | ||
expect(values).not.toContain(expectedValues[0]) | ||
expect(values).toContain(expectedValues[1]) | ||
}) | ||
}) | ||
}) | ||
}) |
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,33 @@ | ||
export type ArrayMap<K, V> = { | ||
get: (key: K) => readonly V[] | undefined | ||
add: (key: K, value: V) => void | ||
delete: (key: K, pred: (value: V) => boolean) => void | ||
} | ||
|
||
export const arrayMap = <K, V>(): ArrayMap<K, V> => { | ||
const map = new Map<K, V[]>() | ||
return { | ||
get: (key: K) => map.get(key), | ||
add: (key: K, value: V) => { | ||
let ar = map.get(key) | ||
if (ar === undefined) { | ||
ar = [] | ||
map.set(key, ar) | ||
} | ||
ar.push(value) | ||
}, | ||
delete: (key: K, pred: (value: V) => boolean) => { | ||
let ar = map.get(key) | ||
if (ar === undefined) { | ||
return undefined | ||
} | ||
ar = ar.filter(value => !pred(value)) | ||
if (ar.length === 0) { | ||
map.delete(key) | ||
} else { | ||
map.set(key, ar) | ||
} | ||
return undefined | ||
}, | ||
} | ||
} |
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 @@ | ||
import { KeyObject } from 'crypto' | ||
import { Logger } from 'pino' | ||
import { arrayMap } from './array-map' | ||
|
||
export { activeTunnelStoreKey } from './key' | ||
|
||
export type ScriptInjection = { | ||
pathRegex?: RegExp | ||
src: string | ||
async?: boolean | ||
defer?: boolean | ||
} | ||
|
||
export type ActiveTunnel = { | ||
envId: string | ||
clientId: string | ||
tunnelPath: string | ||
target: string | ||
hostname: string | ||
publicKey: KeyObject | ||
publicKeyThumbprint: string | ||
access: 'private' | 'public' | ||
meta: Record<string, unknown> | ||
inject?: ScriptInjection[] | ||
} | ||
|
||
export class KeyAlreadyExistsError extends Error { | ||
constructor(readonly key: string) { | ||
super(`key already exists: "${key}"`) | ||
} | ||
} | ||
|
||
export type TransactionDescriptor = { txId: number } | ||
|
||
export type ActiveTunnelStore = { | ||
get: (key: string) => Promise<ActiveTunnel | undefined> | ||
getByPkThumbprint: (pkThumbprint: string) => Promise<readonly ActiveTunnel[] | undefined> | ||
set: (key: string, value: ActiveTunnel) => Promise<TransactionDescriptor> | ||
delete: (key: string, tx?: TransactionDescriptor) => Promise<void> | ||
} | ||
|
||
const txIdGenerator = () => { | ||
let nextTxId = 0 | ||
return { | ||
next: () => { | ||
const result = nextTxId | ||
nextTxId += 1 | ||
return result | ||
}, | ||
} | ||
} | ||
|
||
export const inMemoryActiveTunnelStore = ({ log }: { log: Logger }): ActiveTunnelStore => { | ||
const keyToTunnel = new Map<string, ActiveTunnel & { txId: number }>() | ||
const pkThumbprintToTunnel = arrayMap<string, ActiveTunnel>() | ||
const txIdGen = txIdGenerator() | ||
return { | ||
get: async key => keyToTunnel.get(key), | ||
getByPkThumbprint: async pkThumbprint => pkThumbprintToTunnel.get(pkThumbprint), | ||
set: async (key, value) => { | ||
if (keyToTunnel.has(key)) { | ||
throw new KeyAlreadyExistsError(key) | ||
} | ||
const txId = txIdGen.next() | ||
log.debug('setting tunnel %s: %j', key, txId, value) | ||
keyToTunnel.set(key, Object.assign(value, { txId })) | ||
pkThumbprintToTunnel.add(value.publicKeyThumbprint, value) | ||
return { txId } | ||
}, | ||
delete: async (key, tx) => { | ||
const tunnel = keyToTunnel.get(key) | ||
if (tunnel && (tx === undefined || tunnel.txId === tx.txId)) { | ||
pkThumbprintToTunnel.delete(tunnel.publicKeyThumbprint, ({ hostname }) => hostname === key) | ||
keyToTunnel.delete(key) | ||
} | ||
}, | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
tunnel-server/src/tunnel-store.test.ts → tunnel-server/src/tunnel-store/key.test.ts
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
Oops, something went wrong.