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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 30x 30x 20x 20x 20x 20x 3x 3x 3x 1x 2x 7x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 21x 21x 21x 21x 22x 22x 22x 22x 22x 1x 21x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 3x 3x 3x 3x 10x 10x 3x 3x 3x 3x 7x 7x 7x 7x 10x 3x 20x 20x 20x 20x 20x 20x 19x 19x 1x 1x 1x 3x 3x 3x 3x 3x 8x 8x 8x 8x 8x 7x 7x 7x 7x 25x 25x 6x 6x 6x 6x 6x 6x 6x 6x 5x 5x 5x 2x 3x 5x 11x 11x 11x 11x 3x 3x 3x 25x 25x 7x 7x 7x 19x 19x 20x 20x 20x 1x 19x 19x | import {Logger} from "../../logger"; import LuaBitLib from "../../lua/bit/lua-bit"; import LuaRedisLib from "../../lua/call/lua-redis"; import {IRequest} from "../../server/request"; import {Database} from "../data/database"; import {RedisToken} from "../protocol/redis-token"; import {IRespCommand} from "./resp-command"; /* Tslint:disable-next-line */ const fengari = require("fengari"), {lua} = fengari, {lauxlib} = fengari, {lualib} = fengari; /** * ### Available since 2.6.0. * ### SCRIPT LOAD script * Load a script into the scripts cache, without executing it. After the specified command * is loaded into the script cache it will be callable using (not implemented) EvalshaComamnd | EVALSHA with * the correct SHA1 digest of the script, exactly like after the first successful invocation * of EVAL. * * The script is guaranteed to stay in the script cache forever (unless {@link ScriptCommand | SCRIPT FLUSH} * is called). * **NOTE: unit-redis-ness does not persist the script thru restarts** * * The command works in the same way even if the script was already present in the script cache. * * Please refer to the (not implemented) EvalCommand | EVAL documentation for detailed information about * Redis Lua scripting. * * ### Return value * Bulk string reply This command returns the SHA1 digest of the script added into the script cache. */ export class ScriptCommand extends IRespCommand { private static HELPERS = ` local unpack = table.unpack local pack = table.pack local function dump(o) if type(o) == 'table' then local s = '{ ' for k,v in pairs(o) do if type(k) ~= 'number' then k = '"'..k..'"' end s = s .. '['..k..'] = ' .. dump(v) .. ',' end return s .. '} ' else return tostring(o) end end `; private logger: Logger = new Logger(module.id); private DEFAULT_ERROR: string = "ERR Unknown subcommand or wrong number of arguments for '%s'. Try SCRIPT HELP."; constructor(maxParams: number, minParams: number, name: string) { super(); this.constructor.prototype.maxParams = maxParams; this.constructor.prototype.minParams = minParams; this.constructor.prototype.name = name; } public execSync(request: IRequest, db: Database): RedisToken { this.logger.debug( `${request.getCommand()}.execute(%s)`, ...request.getParams() ); let sha1: string | null; switch (request.getCommand().toLowerCase()) { case "eval": // Replace the sha1 in the cache sha1 = this.loadScript( request, 0 ); Eif (sha1) { this.logger.debug(`Generated sha1 ${sha1}`); return this.executeLua( `${sha1}`, request ); } return RedisToken.error("ERR Parsing script"); case "evalsha": sha1 = request.getParam(0); const code: string = request.getServerContext().getScript(sha1); if (!code) { return RedisToken.error("NOSCRIPT No matching script. Please use EVAL."); } return this.executeLua( sha1, request ); default: switch (request.getParam(0)) { case "load": sha1 = this.loadScript( request, 1 ); if (sha1) { this.logger.debug(`Returning sha1 ${sha1}`); return RedisToken.string(`${sha1}`); } return RedisToken.error("ERR Parsing script"); case "exists": // Array 0/1 const exists: any = request.getServerContext().getScript(request.getParam(1)); this.logger.debug(`EXISTS is ${request.getServerContext().scriptExists(request.getParam(1))}`); this.logger.debug( "The retured value is \"%s\"", exists ); return RedisToken.array([ RedisToken.integer(request.getServerContext().scriptExists(request.getParam(1)) ? 1 : 0) ]); case "flush": request.getServerContext().flush(); return RedisToken.status("OK"); case "kill": case "debug": default: return RedisToken.error(this.DEFAULT_ERROR.replace( "%s", request.getParam(0) )); } } } /** * Attempt to parse a lua script. * Returns a sha1 and stores the script if it can * Adds some helper function(s) to the top of the script * @param request the client request * @returns */ private loadScript(request: IRequest, scriptIndex: number): string | null { const code = request.getParam(scriptIndex); this.logger.debug("loadScript(): Validating lua script \"<script>\""); const L: any = lauxlib.luaL_newstate(); this.logger.debug("Opening lua libraries"); lualib.luaL_openlibs(L); const loadStatus = lauxlib.luaL_loadstring( L, fengari.to_luastring(code) ); if (loadStatus !== lua.LUA_OK) { this.logger.warn( "Caught loadStatus error \"%s\"", loadStatus ); let info: string = "Unknown LUA error %s"; switch (loadStatus) { case lua.LUA_YIELD: // 1, case lua.LUA_ERRRUN: // 2, case lua.LUA_ERRSYNTAX: // 3, case lua.LUA_ERRMEM: // 4, case lua.LUA_ERRGCMM: // 5, case lua.LUA_ERRERR: // 6 default: info = `LUA ERR Error: code ${loadStatus} evaluating "%s"`; break; } return null; } this.logger.debug("Caching script"); const sha1: string = request.getServerContext().setScript(code); this.logger.debug( "Script sha1 is \"%s\"", sha1 ); return sha1; } private executeLua(sha1: string, request: IRequest): RedisToken { this.logger.debug( "executeLua sha1: %s", sha1 ); let returnValue: RedisToken; const code: string = `${ScriptCommand.HELPERS}\r\n${request.getServerContext().getScript(sha1)}`, keycount: string = request.getParam(1); this.logger.debug( "key count %s", keycount ); if (isNaN(Number(keycount))) { return RedisToken.error("ERR value is not an integer or out of range"); } if (Number(keycount) < 0) { return RedisToken.error("ERR Number of keys can't be negative"); } // Setup the script const L: any = lauxlib.luaL_newstate(); this.logger.debug("executeLua: Opening lua libraries"); lualib.luaL_openlibs(L); LuaBitLib.LoadLibrary(L); LuaRedisLib.LoadLibrary( L, request ); this.logger.debug( "executeLua(): Validating lua script \"%s\"", code ); const loadStatus = lauxlib.luaL_loadstring( L, fengari.to_luastring(`${code}`) ); Iif (loadStatus !== lua.LUA_OK) { const er: any = lauxlib.luaL_error( L, fengari.to_luastring("Unexpected error parsing error (%s)"), code ); returnValue = RedisToken.error(er); this.logger.warn(er); } else { const stack: number = lua.lua_gettop(L); this.logger.debug(`The stack top is ${stack}`); this.logger.debug(`Calling script "${code}"`); const ok = lua.lua_call( L, 0, lua.LUA_MULTRET ); Eif (ok === undefined) { this.logger.debug("The LUA call was ok."); Iif (stack === 0) { returnValue = RedisToken.nullString(); } else { returnValue = this.assembleLuaResponses( L, request ); } } else { this.logger.warn(`The LUA call was NOT OK: ${ok}`); const er: any = lauxlib.luaL_error( L, fengari.to_luastring("processing error (%s)"), code ); returnValue = RedisToken.error(er); this.logger.warn(er); } } this.logger.debug("Releasing lua state"); lua.lua_close(L); return returnValue; } private collectTable(L: any): RedisToken[] { this.logger.debug("collectTable(L)"); let values: RedisToken[] = []; lua.lua_pushnil(L); let value: any; while (lua.lua_next( L, -2 ) !== 0) { const luaType: number = lua.lua_type( L, -1 ); switch (luaType) { case lua.LUA_TNIL: values = []; this.logger.debug("NIL: null resets table contents"); break; case lua.LUA_TNUMBER: value = LuaRedisLib.stringFrom(lua.lua_tostring( L, -1 )); this.logger.debug(`FOUND NUMBER: ${value}`); values.unshift(RedisToken.integer(Number(parseInt( value, 10 )))); break; case lua.LUA_TBOOLEAN: value = lua.lua_toboolean( L, -1 ); this.logger.debug(`FOUND BOOLEAN ${value}`); if (value) { values.unshift(RedisToken.integer(1)); } else { values.unshift(RedisToken.nullString()); } break; case lua.LUA_TSTRING: value = LuaRedisLib.stringFrom(lua.lua_tostring( L, -1 )); this.logger.debug(`FOUND STRING ${value}`); values.unshift(RedisToken.string(value)); break; case lua.LUA_TTABLE: this.logger.debug("FOUND EMBEDDED TABLE"); values.unshift(RedisToken.array(this.collectTable(L))); break; default: values.unshift(RedisToken.error(`Can't manage luaType ${luaType} in collectTable(L)`)); } lua.lua_pop( L, 1 ); } return values; } private assembleLuaResponses(L: any, request: IRequest): RedisToken { const results: any[] = []; this.logger.debug("assembleResponses(L)"); let tempString: string = "", wxReturnStr: string = ""; const nargs: number = lua.lua_gettop(L); for (let i = 1; i <= nargs; i++) { const type: number = lua.lua_type( L, i ); switch (type) { case lua.LUA_TNIL: this.logger.debug("Found LUA_TNIL"); results.unshift(RedisToken.nullString()); break; case lua.LUA_TBOOLEAN: this.logger.debug("Processing LUA_TBOOLEAN"); tempString = LuaRedisLib.stringFrom(lua.lua_toboolean( L, i )) ? "true" : "false"; results.unshift(RedisToken.boolean(Boolean(tempString))); break; case lua.LUA_TSTRING: this.logger.debug("Processing LUA_TSTRING"); tempString = LuaRedisLib.stringFrom(lua.lua_tostring( L, i )); Iif (!isNaN(Number(tempString))) { this.logger.debug(`Adjusting ${tempString} to integer-like string: ${String(parseInt( tempString, 10 ))}`); tempString = String(parseInt( tempString, 10 )); } results.unshift(RedisToken.string(tempString)); break; case lua.LUA_TNUMBER: this.logger.debug("Processing LUA_TNUMBER"); tempString = String(parseInt( lua.lua_tonumber( L, i ), 10 )); this.logger.debug(`The value is ${tempString} as ${Number(tempString)}`); results.unshift(RedisToken.integer(Number(tempString))); break; case lua.LUA_TTABLE: this.logger.debug("Processing LUA_TTABLE"); // NOTE: the stacked results are retrieved in reverse order const values: RedisToken[] = []; let value: any; lua.lua_pushnil(L); while (lua.lua_next( L, -2 ) !== 0) { const luaType: number = lua.lua_type( L, -1 ); switch (luaType) { case lua.LUA_TNIL: value = null; values.unshift(RedisToken.nullString()); this.logger.debug("NIL: null should reset table contents"); break; case lua.LUA_TNUMBER: let tempvalue: any = LuaRedisLib.stringFrom(lua.lua_tostring( L, -1 )); tempvalue = Number(parseInt( tempvalue, 10 )); this.logger.debug(`FOUND NUMBER: ${tempvalue}`); Eif (tempvalue.constructor.name === "Number") { this.logger.debug(`pushing number to stack : ${tempvalue}`); values.unshift(RedisToken.integer(tempvalue)); } else { this.logger.debug("Replacing NUMBER with saved result"); values.unshift(tempvalue); } value = tempvalue; break; case lua.LUA_TBOOLEAN: value = lua.lua_toboolean( L, -1 ); this.logger.debug(`FOUND BOOLEAN ${value}`); if (value) { values.unshift(RedisToken.integer(1)); } else { values.unshift(RedisToken.nullString()); } break; case lua.LUA_TSTRING: value = LuaRedisLib.stringFrom(lua.lua_tostring( L, -1 )); this.logger.debug(`FOUND STRING ${value}`); values.unshift(RedisToken.string(value)); break; case lua.LUA_TTABLE: this.logger.debug("FOUND TABLE in LUA_TTABLE"); /* * NOTE: This is a bit of a kludge which only serves to pass unit tests. * TODO: Refactor this to always return the table in the proper order */ values.push(RedisToken.array(values.length > 0 ? this.collectTable(L).reverse() : this.collectTable(L))); break; default: throw new Error(`Can't manage luaType: ${luaType}`); } lua.lua_pop( L, 1 ); wxReturnStr += `${value}\n`; } this.logger.debug( "The complete table is %j", `${values}` ); results.unshift(RedisToken.array(values)); break; default: tempString = lua.lua_typename( L, type ); results.unshift(RedisToken.string(`Unknown type ${tempString}`)); break; } wxReturnStr += `${tempString}\n`; tempString = ""; } lua.lua_pop( L, nargs ); this.logger.debug(`lua_print(L): STACK (${results.length}) is: ${wxReturnStr}`); if (results.length === 0) { return RedisToken.nullString(); } Eif (results.length === 1) { return results[0]; } return RedisToken.array(results); } } |