Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 9x 1x 82x 5x 161x 161x 45x 161x 161x 2x 21x 21x 21x 21x 21x 27x 4x | import * as crypto from "crypto"; import * as net from "net"; import { CommandSuite } from "../resp/processor/command-suite"; import { Database } from "../resp/data/database"; import { Dictionary } from "../dictionary"; import { EventEmitter } from "events"; import { Logger } from "../logger"; import { IRespCommand } from "../resp/command/resp-command"; import { IServerContext } from "./server-context"; import { Session } from "./session"; export class RespServerContext extends EventEmitter implements IServerContext { private logger: Logger = new Logger(module.id); private clients: Dictionary<string, net.Socket> = new Dictionary<string, net.Socket>(); private state: Dictionary<string, string> = new Dictionary<string, string>(); private databases: Dictionary<string, Database> = new Dictionary<string, Database>(); private scripts: Dictionary<string, string> = new Dictionary<string, string>(); /** * Instantiate the server contect. * Database ZERO is instantiated by default. Others will be dynamically added. * @param host The host or IP that we're listening to * @param port The port we're listening to * @param commands An empty command suite */ constructor(private host: string, private port: number, private commands: CommandSuite) { super(); this.logger.warn(`REDIS_PORT is ${process.env.REDIS_PORT}`); this.databases.put( "_0", new Database() ); } public publish(channel: string, message: string): number { this.logger.debug(`publish "${message}" to channel "${channel}"`); this.emit( channel, { channel, message } ); return this.listenerCount(channel); } public getHost(): string { return this.host; } public getPort(): number { return this.port; } public getClients(): Dictionary<string, Session> { return this.clients; } public getClientCount(): number { return Object.keys(this.clients).length; } public getCommand(name: string): IRespCommand { return this.commands.getCommand(name); } public getValue(key: string): any { return this.state.get(key); } public putValue(key: string, value: any): void { this.state.put( key, value ); } public removeValue(key: string) { this.state.remove(key); } public addClient(clientId: string, client: Session): void { this.clients.put( clientId, client ); } public removeClient(clientId: string): void { this.clients.remove(clientId); } /** * The limit of 16 databases is enforced in the {@link resp/command/db/select-command.SelectCommand | SELECT} * @param id ordinal db number */ public getDatabase(id: number): Database { let db = this.databases.get(`_${id}`); if (!db) { db = this.databases.put( `_${id}`, new Database() ); } this.logger.debug(`getDatabase _${id}`); return this.databases.get(`_${id}`); } public getString(sha1: string): string { return this.scripts.get(sha1); } public flush(): void { this.scripts.clear(); } /** * Calculate and return the sha1 of code. Store the code * using sha1 as the key. * @param code lua code script */ public setScript(code: string): string { const hash: any = crypto.createHash("sha1"); hash.update(code); const key: string = hash.digest("hex"); this.scripts.put( key, code ); return key; } /** * Retrieve the lua script identified by the sha1 * @param sha1 previously calculated sha1 */ public getScript(sha1: string): string { return this.scripts.get(sha1); } /** * Check if a script exists * @param sha1 sha1 of the script */ public scriptExists(sha1: string): boolean { return this.scripts.exists(sha1); } } |