Best JavaScript code snippet using wpt
unitoken.jquery.js
Source:unitoken.jquery.js
1/*! UniToken jQuery Plugin v1.3 | (c) 2013 Florent Morselli | www.morselli.fr2This plugin aims to create an interface to access on UniToken keys and retreive files, certificates and methods provided them.3*/4(function($) {5 var PLUGIN_MAJOR_VERSION = 1;6 var PLUGIN_MINOR_VERSION = 3;7 var PLUGIN_VERSION = PLUGIN_MAJOR_VERSION<<4+PLUGIN_MINOR_VERSION;8 // --------- macro code -----------------9 var USER_LEVEL_GUEST = 0x00;10 var USER_LEVEL_USER = 0x01;11 var USER_LEVEL_ADMIN = 0x02;12 // --------- response codes -----------------13 var OK = 0x00000000;14 var NOT_INITIALIZE = 0xC0000001;15 var BUFFER_TOO_SMALL = 0xC0000002;16 var ARGUMENTS_BAD = 0xC0000003;17 var NO_PERMISSION = 0xC0000004;18 var NO_TOKEN = 0xC0000006;19 var TOKEN_HANDLE_INVALID = 0xC0000007;20 var SLOT_FULL = 0xC0000008;21 var SLOT_ID_INVALID = 0xC0000009;22 var DEVICE_ERROR = 0xC0000010;23 var ALREADY_INITIALIZE = 0xC0000011;24 var DEVICE_TX = 0xC0000012;25 var PIN_ERROR = 0xC0000013;26 var USER_LOCKED = 0xC0000014;27 var USER_FULL = 0xC0000015;28 var NO_USER = 0xC0000016;29 var DEVICE_NEED_OPEN = 0xC0000017;30 var KEY_EXIST = 0xC0000018;31 var NO_KEY = 0xC0000019;32 var KEY_FULL = 0xC0000020;33 var KEY_HANDLE_INVALID = 0xC0000021;34 var KEY_GENERATE = 0xC0000022;35 var NOT_ALLOWED = 0xC0000023;36 var OPERATE_INVALID = 0xC0000024;37 var FS_INITIALIZE_FAILED = 0xCF000001;38 var FS_NOSPACE = 0xCF000002;39 var FS_OFFSET_INVALID = 0xCF000003;40 var FS_DATA_LEN_RANGE = 0xCF000004;41 var FS_NO_OPEN = 0xCF000005;42 var FS_NO_FILE = 0xCF000006;43 var FS_LIST_END = 0xCF000007;44 var FS_NAME_LEN_RANGE = 0xCF000008;45 var FS_FILE_EXIST = 0xCF00000A;46 var KEY_DES = 0xA0000000;47 var KEY_DES3 = 0xA0000001;48 var KEY_AES = 0xA0000002;49 var KEY_RSA = 0xA0000003;50 var KEY_RSA_PUBLIC = 0xA0000004;51 var KEY_RSA_PRIVATE = 0xA0000005;52 var KEY_MD5HMAC = 0xA0000006;53 var KEY_MD5 = 0xA0000007;54 var KEY_SHA1HMAC = 0xA0000008;55 var KEY_SHA1 = 0xA0000009;56 var KEY_BAD_TYPE = 0xA0000010;57 var defaults = {58 debug: false,59 /*auto_logoff: {60 enabled: false,61 interval: 30000062 },*/63 events: {64 token_inserted: undefined,65 token_removed: undefined,66 token_detection_interval: 5000,67 token_udpated: undefined,68 key_created: undefined,69 key_deleted: undefined,70 random_data: undefined,71 //file_created: undefined,72 //file_updated: undefined,73 //file_deleted: undefined,74 logon_success: undefined,75 logon_error: undefined,76 logoff: undefined77 }78 };79 var settings = {};80 var inArray = function(needle, haystack)81 {82 var length = haystack.length;83 for (var i = 0; i < length; i++)84 {85 if(haystack[i].hid == needle.hid )86 return i;87 };88 return -1;89 }90 var getCodeMessage = function(code)91 {92 code >>>= 0;93 var msg;94 switch(code)95 {96 case OK:97 msg = "No error";98 break;99 case NOT_INITIALIZE:100 msg = "Library not initialized";101 break;102 case BUFFER_TOO_SMALL:103 msg = "Buffer is not big enough";104 break;105 case ARGUMENTS_BAD:106 msg = "Invalid parameters";107 break;108 case NO_PERMISSION:109 msg = "No permission";110 break;111 case NO_TOKEN:112 msg = "No token available";113 break;114 case TOKEN_HANDLE_INVALID:115 msg = "Invalid token handle";116 break;117 case SLOT_FULL:118 msg = "Slot is full. No more token can be added";119 break;120 case SLOT_ID_INVALID:121 msg = "Invalid slot";122 break;123 case DEVICE_ERROR:124 msg = "Device error";125 break;126 case ALREADY_INITIALIZE:127 msg = "The token is already initialized";128 break;129 case DEVICE_TX:130 msg = "Communication error";131 break;132 case PIN_ERROR:133 msg = "PIN error";134 break;135 case USER_LOCKED:136 msg = "User has been locked";137 break;138 case USER_FULL:139 msg = "No more users can logon";140 break;141 case NO_USER:142 msg = "No such user available";143 break;144 case DEVICE_NEED_OPEN:145 msg = "Device is not opened yet";146 break;147 case KEY_EXIST:148 msg = "The key already exists";149 break;150 case NO_KEY:151 msg = "No key available";152 break;153 case KEY_FULL:154 msg = "The token is full and can't generate new keys";155 break;156 case KEY_HANDLE_INVALID:157 msg = "Key handle error";158 break;159 case KEY_GENERATE:160 msg = "Key generation failed";161 break;162 case NOT_ALLOWED:163 msg = "Operation not allowed";164 break;165 case OPERATE_INVALID:166 msg = "Invalid operation";167 break;168 case FS_INITIALIZE_FAILED:169 msg = "File initialization failed";170 break;171 case FS_NOSPACE:172 msg = "Not enough filesystem space";173 break;174 case FS_OFFSET_INVALID:175 msg = "Designated file offset is invalid";176 break;177 case FS_DATA_LEN_RANGE:178 msg = "Beyond data range";179 break;180 case FS_NO_OPEN:181 msg = "The file is not opened";182 break;183 case FS_NO_FILE:184 msg = "No file";185 break;186 case FS_LIST_END:187 msg = "End of file list";188 break;189 case FS_NAME_LEN_RANGE:190 msg = "Filename is too long";191 break;192 case FS_FILE_EXIST:193 msg = "The file already exists";194 break;195 case KEY_BAD_TYPE:196 msg = "Key type not recognized";197 break;198 default:199 msg = "The result of the method has code: 0x"+code.toString(16);200 };201 return msg;202 }203 var log = {};204 log.message = function(msg)205 {206 if(settings.debug)207 console.log(msg);208 }209 log.code = function(code)210 {211 if(settings.debug)212 log.message( getCodeMessage(code) );213 }214 215 var prepareResponse = function(code, data)216 {217 return {218 code: code >>>0,219 message: getCodeMessage(code),220 result: data221 };222 }223 var File = function(iface, handle, filename)224 {225 var file = this;226 var interface_ = iface;227 var handle_ = handle;228 var filename_ = filename;229 230 var init = function()231 {}232 file.open = function(){233 log.message('Opening '+filename_);234 var result = interface_.UT_FS_OpenFile(handle_, filename_);235 log.code(result);236 return prepareResponse(result,undefined);237 }238 file.close = function(){239 log.message('Closing '+filename_);240 var result = interface_.UT_FS_CloseFile(handle_, filename_);241 log.code(result);242 return prepareResponse(result,undefined);243 }244 file.remove = function(){245 log.message('Removing '+filename_);246 var result = interface_.UT_FS_DeleteFile(handle_, filename_);247 log.code(result);248 return prepareResponse(result,undefined);249 }250 var size = function(){251 log.message('Getting size of '+filename_);252 var result = interface_.UT_FS_GetFileSize(handle_, filename_);253 log.code(result);254 if(result == OK)255 {256 var value = interface_.FileSize;257 return prepareResponse(result,value);258 }259 260 return undefined;261 }262 var permission = function(){263 log.message('Getting permission of '+filename_);264 var result = interface_.UT_FS_GetFilePermission(handle_, filename_);265 log.code(result);266 if(result == OK)267 {268 var value = interface_.FilePermission;269 return prepareResponse(result,value);270 }271 272 return undefined;273 }274 file.details = function(){275 return {276 'filename': filename_,277 'size': size(),278 'permission': permission()279 };280 }281 file.read = function(offset, len){282 log.message('Trying to read '+filename_+' from offset '+offset+' with length '+len);283 var result = interface_.UT_FS_ReadFile(handle_, filename_, offset, len);284 log.code(result);285 if(result == OK)286 {287 var value = interface_.OutBuf;288 return prepareResponse(result,value);289 }290 return prepareResponse(result,undefined);291 }292 file.write = function(offset, len, data){293 log.message('Trying to write '+filename_+' from offset '+offset+' with data length '+len);294 295 var result = interface_.UT_FS_WriteFile(handle_, filename_, offset, len, data);296 log.code(result);297 return prepareResponse(result,undefined);298 }299 300 init();301 };302 var Token = function(iface, slot)303 {304 var token = this;305 var interface_ = iface;306 var slot_ = slot;307 var handle_ = undefined;308 var auto_logoff_ = undefined;309 310 var resetAutoLogoff = function()311 {312 if(settings.auto_logoff == true && settings.auto_logoff_interval > 0)313 auto_logoff_ = setTimeout(function(){314 log.message('Auto logoff after inactivity');315 token.logoff();316 }, settings.auto_logoff_interval);317 }318 319 320 var init = function()321 {322 log.message('New token interface initialized on slot '+slot_);323 resetAutoLogoff();324 }325 326 var open = function()327 {328 log.message('Trying to open token on slot '+slot_);329 var result = interface_.UT_OpenDevice(slot_);330 log.code(result);331 if(result == OK)332 {333 handle_ = interface_.Handle;334 log.message('Handle is 0x'+handle_.toString(16));335 }336 return prepareResponse(result,undefined);337 }338 339 //Useless340 var close = function()341 {342 log.message('Trying to close token on slot '+slot_);343 var result = interface_.UT_CloseDevice(handle_);344 345 log.code(result);346 return prepareResponse(result,undefined);347 }348 349 /*350 Token informations351 */352 var getSlot = function()353 {354 return slot_;355 }356 357 var getType = function()358 {359 log.message('Trying to get type of token on slot '+slot_);360 361 var result = interface_.UT_GetTokenType(handle_);362 363 log.code(result);364 365 if(result == OK)366 {367 var value = interface_.TokenType;368 log.message('Type is '+value);369 return value;370 }371 return undefined;372 }373 374 var getFirmwareVersion = function()375 {376 log.message('Trying to get firmware version of token on slot '+slot_);377 378 var result = interface_.UT_GetFirmwareVersion(handle_);379 380 log.code(result);381 382 if(result == OK)383 {384 var value = interface_.FirmwareVersion;385 log.message('Firmware version is 0x'+value.toString(16));386 return value;387 }388 389 return undefined;390 }391 392 token.logon = function(type, password)393 {394 log.message('Trying to logon token on slot '+slot_);395 396 var result = interface_.UT_Logon(handle_, type, password);397 398 log.code(result);399 400 if( result == OK && settings.events.logon_success != undefined )401 settings.events.logon_success(type);402 if( result != OK && settings.events.logon_error != undefined )403 settings.events.logon_error(type);404 405 return prepareResponse(result,undefined);406 }407 408 token.logoff = function()409 {410 log.message('Trying to logoff token on slot '+slot_);411 412 var result = interface_.UT_Logoff(handle_);413 414 log.code(result);415 416 if( result == OK && settings.events.logoff != undefined )417 settings.events.logoff();418 419 return prepareResponse(result,undefined);420 }421 422 var getCurrentUserLevel = function()423 {424 log.message('Trying to get current user level of token on slot '+slot_);425 426 var result = interface_.UT_GetCurrentUserLevel(handle_);427 428 log.code(result);429 430 if(result == OK)431 {432 var value = interface_.Level;433 log.message('Current user level is '+(value==0?'guest':(value==1?'user':'admin')));434 return value;435 }436 437 return undefined;438 }439 440 token.changePin = function(oldPassword, newPassword, mode)441 {442 log.message('Trying to change PIN of token on slot '+slot_);443 444 if(!mode)445 {446 log.message('Mode is not set. Using current permission');447 mode = 0x1;448 }449 var type = getCurrentUserLevel();450 if( mode == 0x2)451 type = USER_LEVEL_USER;452 log.message('Will change password of '+(type==1?'user':'admin'));453 var result = interface_.UT_ChangePin(handle_, type, mode, oldPassword, newPassword);454 455 log.code(result);456 457 if( result == OK && settings.events.token_udpated != undefined )458 settings.events.token_udpated(token.getDetails());459 460 return prepareResponse(result,undefined);461 }462 463 var getID = function()464 {465 log.message('Trying to get ID of token on slot '+slot_);466 467 var result = interface_.UT_GetID(handle_);468 469 log.code(result);470 471 if(result == OK)472 {473 var value = interface_.TokenId;474 log.message('The ID is '+value);475 return value;476 }477 return undefined;478 }479 480 token.setID = function(id)481 {482 log.message('Trying to set ID of token on slot '+slot_);483 484 var result = interface_.UT_SetID(handle_, id);485 486 log.code(result);487 488 if( result == OK && settings.events.token_udpated != undefined )489 settings.events.token_udpated(token.getDetails());490 491 return prepareResponse(result,undefined);492 }493 494 var getHID = function()495 {496 log.message('Trying to get HID of token on slot '+slot_);497 498 var result = interface_.UT_GetHID(handle_);499 500 log.code(result);501 502 if(result == OK)503 {504 var value = interface_.HID;505 log.message('The ID is '+value+' (0x'+value.toString(16)+')');506 return value;507 }508 509 return undefined;510 }511 512 var getSoftID = function()513 {514 log.message('Trying to get Soft ID of token on slot '+slot_);515 516 var result = interface_.UT_GetSoftID(handle_);517 518 log.code(result);519 520 if(result == OK)521 {522 var value = interface_.SoftID;523 log.message('The ID is '+value+' (0x'+value.toString(16)+')');524 return value;525 }526 527 return undefined;528 }529 530 token.setSoftID = function(id)531 {532 log.message('Trying to set Soft ID of token on slot '+slot_);533 534 var result = interface_.UT_SetSoftID(handle_, id);535 536 log.code(result);537 538 if( result == OK && settings.events.token_udpated != undefined )539 settings.events.token_udpated(token.getDetails());540 541 return prepareResponse(result,undefined);542 }543 544 token.setLed = function(mode)545 {546 log.message('Trying to set LED mode of token on slot '+slot_);547 548 var result = interface_.UT_LED(handle_, mode);549 550 log.code(result);551 552 if( result == OK && settings.events.token_udpated != undefined )553 settings.events.token_udpated(token.getDetails());554 555 return prepareResponse(result,undefined);556 }557 558 var getAttemptCount = function()559 {560 log.message('Trying to get attempt count of token on slot '+slot_);561 562 var result = interface_.UT_GetAttemptCount(handle_);563 564 log.code(result);565 566 if(result == OK)567 {568 var value = interface_.UserAttempt;569 log.message('Attempt count is at '+value);570 return value;571 }572 573 return undefined;574 }575 576 token.setAttemptCount = function(count)577 {578 log.message('Trying to set attempt count of token on slot '+slot_);579 580 var result = interface_.UT_SetAttemptCount(handle_, count);581 582 log.code(result);583 584 if( result == OK && settings.events.token_udpated != undefined )585 settings.events.token_udpated(token.getDetails());586 587 return prepareResponse(result,undefined);588 }589 590 token.format = function()591 {592 log.message('Trying to format token on slot '+slot_);593 594 var result = interface_.UT_Format(handle_);595 596 log.code(result);597 return prepareResponse(result,undefined);598 }599 600 token.getDetails = function()601 {602 log.message('Trying to get details of token on slot '+slot_);603 604 return {605 'slot': getSlot(),606 'type': getType(),607 'firmware_version': getFirmwareVersion(),608 'id': getID(),609 'hid': getHID(),610 'soft_id': getSoftID(),611 'attempt_count': getAttemptCount(),612 'user_level': getCurrentUserLevel(),613 'token': this614 };615 }616 617 618 /*619 Password, keys and hash620 */621 token.getRandomData = function(length)622 {623 log.message('Trying to get random data from token on slot '+slot_);624 625 var result = interface_.UT_Rand(handle_, length);626 627 log.code(result);628 629 if(result == OK)630 {631 var value = interface_.OutBuf;632 log.message('Data: '+value);633 634 if( settings.events.random_data != undefined )635 settings.events.random_data(value);636 637 return prepareResponse(result,value);638 }639 return prepareResponse(result,undefined);640 }641 642 token.generateDESKey = function(){ return generateKey(KEY_DES);}643 token.generateDES3Key = function(){ return generateKey(KEY_DES3);}644 token.generateAES16Key = function(){ return generateKey(KEY_AES,16);}645 token.generateAES24Key = function(){ return generateKey(KEY_AES,24);}646 token.generateRSA128KeyPair = function(){ return generateKey(KEY_RSA,128);}647 token.generateRSA256KeyPair = function(){ return generateKey(KEY_RSA,256);}648 token.generateMD5HMACKey = function(length){ return generateKey(KEY_MD5HMAC,length);}649 token.generateSHA1HMACKey = function(length){ return generateKey(KEY_SHA1HMAC,length);}650 var generateKey = function(type, length)651 {652 log.message('Trying to get generate a 0x'+type.toString(16)+' key with token on slot '+slot_);653 654 var result = undefined;655 656 switch(type)657 {658 case KEY_DES:659 result = interface_.UT_DESGenerateKey(handle_);660 break;661 case KEY_DES3:662 result = interface_.UT_DES3GenerateKey(handle_);663 break;664 case KEY_AES:665 result = interface_.UT_AESGenerateKey(handle_, length);666 break;667 case KEY_RSA:668 result = interface_.UT_RSAGenerateKeyPair(handle_, length, 65537);669 break;670 case KEY_MD5HMAC:671 result = interface_.UT_MD5HMACGenerateKey(handle_, length);672 break;673 case KEY_SHA1HMAC:674 result = interface_.UT_SHA1HMACGenerateKey(handle_, length);675 break;676 default:677 result = KEY_BAD_TYPE;678 break;679 };680 681 log.code(result);682 683 if(result == OK)684 {685 var value = interface_.KeyHandle;686 log.message('Key handle is 0x'+value.toString(16));687 688 if( settings.events.key_created != undefined )689 settings.events.key_created(type, value);690 691 return prepareResponse(result,value);692 }693 return prepareResponse(result,undefined);694 }695 696 token.encryptDES = function(key, data, data_length){ return encrypt(KEY_DES, key, data, data_length);}697 token.encryptDES3 = function(key, data, data_length){ return encrypt(KEY_DES3, key, data, data_length);}698 token.encryptAES = function(key, data, data_length){ return encrypt(KEY_AES, key, data, data_length);}699 token.encryptRSAPublicKey = function(key, data, data_length){ return encrypt(KEY_RSA_PUBLIC, key, data, data_length);}700 token.encryptRSAPrivateKey = function(key, data, data_length){ return encrypt(KEY_RSA_PRIVATE, key, data, data_length);}701 token.MD5HMAC = function(key, data, data_length){ return encrypt(KEY_MD5HMAC, key, data, data_length);}702 token.MD5 = function(data, data_length){ return encrypt(KEY_MD5, data, data_length);}703 token.SHA1HMAC = function(key, data, data_length){ return encrypt(KEY_SHA1HMAC, key, data, data_length);}704 token.SHA1 = function(data, data_length){ return encrypt(KEY_SHA1, data, data_length);}705 var encrypt = function(type,key, data, data_length)706 {707 log.message('Trying to encrypt data with key 0x'+key.toString(16)+' of token on slot '+slot_);708 709 var result = undefined;710 711 switch(type)712 {713 case KEY_DES:714 result = interface_.UT_DESEncrypt(handle_, key, data, data_length);715 break;716 case KEY_DES3:717 result = interface_.UT_DES3Encrypt(handle_, key, data, data_length);718 break;719 case KEY_AES:720 result = interface_.UT_AESEncrypt(handle_, key, data, data_length);721 break;722 case KEY_RSA_PUBLIC:723 result = interface_.UT_RSAPubKeyEncrypt(handle_, key, data, data_length);724 break;725 case KEY_RSA_PRIVATE:726 result = interface_.UT_RSAPriKeyEncrypt(handle_, key, data, data_length);727 break;728 case KEY_MD5HMAC:729 result = interface_.UT_MD5HMAC(handle_, key, data, data_length);730 break;731 case KEY_MD5:732 result = interface_.UT_MD5(handle_, data, data_length);733 break;734 case KEY_SHA1HMAC:735 result = interface_.UT_SHA1HMAC(handle_, key, data, data_length);736 break;737 case KEY_SHA1:738 result = interface_.UT_SHA1(handle_, data, data_length);739 break;740 default:741 result = KEY_BAD_TYPE;742 break;743 };744 745 log.code(result);746 747 if(result == OK)748 {749 if(type == KEY_MD5HMAC || type == KEY_MD5 || type == KEY_SHA1HMAC || type == KEY_SHA1)750 return {data: interface_.Degist, length: interface_.Degist.length};751 return {752 data: interface_.Encrypt,753 length: interface_.EncryptLength754 };755 }756 return prepareResponse(result,undefined);757 }758 759 token.decryptDES = function(key, data, data_length){ return decrypt(KEY_DES, key, data, data_length);}760 token.decryptDES3 = function(key, data, data_length){ return decrypt(KEY_DES3, key, data, data_length);}761 token.decryptAES = function(key, data, data_length){ return decrypt(KEY_AES, key, data, data_length);}762 token.decryptRSAPublicKey = function(key, data, data_length){ return decrypt(KEY_RSA_PUBLIC, key, data, data_length);}763 token.decryptRSArivateKey = function(key, data, data_length){ return decrypt(KEY_RSA_PRIVATE, key, data, data_length);}764 var decrypt = function(type,key, data, data_length)765 {766 log.message('Trying to decrypt data with key 0x'+key.toString(16)+' of token on slot '+slot_);767 768 var result = undefined;769 770 switch(type)771 {772 case KEY_DES:773 result = interface_.UT_DESDecrypt(handle_, key, data, data_length);774 break;775 case KEY_DES3:776 result = interface_.UT_DES3Decrypt(handle_, key, data, data_length);777 break;778 case KEY_AES:779 result = interface_.UT_AESDecrypt(handle_, key, data, data_length);780 break;781 case KEY_RSA_PRIVATE:782 result = interface_.UT_RSAPriKeyDecrypt(handle_, key, data, data_length);783 break;784 case KEY_RSA_PUBLIC:785 result = interface_.UT_RSAPubKeyDecrypt(handle_, key, data, data_length);786 break;787 default:788 result = KEY_BAD_TYPE;789 break;790 };791 792 log.code(result);793 794 if(result == OK)795 return {796 data: interface_.OutBuf,797 length: interface_.OutBufLength798 };799 return prepareResponse(result,undefined);800 }801 802 token.deleteDESKeys = function(){ return deleteKeys(KEY_DES);}803 token.deleteDES3Keys = function(){ return deleteKeys(KEY_DES3);}804 token.deleteAESKeys = function(){ return deleteKeys(KEY_AES);}805 token.deleteRSAKeyPairs = function(){ return deleteKeys(KEY_RSA);}806 token.deleteMD5HMACKeys = function(){ return deleteKeys(KEY_MD5HMAC);}807 token.deleteSHA1HMACKeys = function(){ return deleteKeys(KEY_SHA1HMAC);}808 var deleteKeys = function(type)809 {810 var keys = get_Keys(type);811 if( keys.code != OK)812 return;813 for (var i = 0; i < keys.result.length; i++)814 {815 if(type == KEY_RSA)816 deleteKey(type, keys.result[i].pub, keys.result[i].pub);817 else818 deleteKey(type, keys.result[i].handle);819 };820 }821 token.deleteDESKey = function(key){ return deleteKey(KEY_DES, key);}822 token.deleteDES3Key = function(key){ return deleteKey(KEY_DES3, key);}823 token.deleteAESKey = function(key){ return deleteKey(KEY_AES, key);}824 token.deleteRSAKeyPair = function(pub_key, priv_key){ return deleteKey(KEY_RSA, pub_key, priv_key);}825 token.deleteMD5HMACKey = function(key){ return deleteKey(KEY_MD5HMAC, key);}826 token.deleteSHA1HMACKey = function(key){ return deleteKey(KEY_SHA1HMAC, key);}827 var deleteKey = function(type, key, key2)828 {829 log.message('Trying to delete key 0x'+key.toString(16)+' of token on slot '+slot_);830 831 var result = undefined;832 833 switch(type)834 {835 case KEY_DES:836 result = interface_.UT_DESDeleteKey(handle_, key);837 break;838 case KEY_DES3:839 result = interface_.UT_DES3DeleteKey(handle_, key);840 break;841 case KEY_AES:842 result = interface_.UT_AESDeleteKey(handle_, key);843 break;844 case KEY_RSA:845 result = interface_.RSADeleteKeyPair(handle_, key, key2);846 break;847 case KEY_MD5HMAC:848 result = interface_.UT_MD5DeleteKey(handle_, key);849 break;850 case KEY_SHA1HMAC:851 result = interface_.UT_SHA1DeleteKey(handle_, key);852 break;853 default:854 result = KEY_BAD_TYPE;855 break;856 };857 858 log.code(result);859 860 if( result == OK && settings.events.key_deleted != undefined )861 settings.events.key_deleted(type, key, key2);862 return prepareResponse(result,undefined);863 }864 865 token.countKeys = function()866 {867 var des = token.countDESKeys();868 var des3 = token.countDES3Keys();869 var aes = token.countAESKeys();870 var rsa = token.countRSAKeys();871 var md5 = token.countMD5HMACKeys();872 var sha1 = token.countSHA1HMACKeys();873 874 var list = {875 DES: des,876 DES3: des3,877 AES: aes,878 RSA: rsa,879 MD5HMAC: md5,880 SHA1HMAC: sha1,881 total: des+des3+aes+rsa+md5+sha1882 };883 return prepareResponse(OK,list);884 }885 token.countDESKeys = function(){ return count_Keys(KEY_DES);}886 token.countDES3Keys = function(){ return count_Keys(KEY_DES3);}887 token.countAESKeys = function(){ return count_Keys(KEY_AES);}888 token.countRSAKeys = function(){ return count_Keys(KEY_RSA);}889 token.countMD5HMACKeys = function(){ return count_Keys(KEY_MD5HMAC);}890 token.countSHA1HMACKeys = function(){ return count_Keys(KEY_SHA1HMAC);}891 var count_Keys = function(type)892 {893 log.message('Trying to count keys 0x'+type.toString(16)+' of token on slot '+slot_);894 895 var result = undefined;896 897 switch(type)898 {899 case KEY_DES:900 result = interface_.UT_DESGetKeyCount(handle_);901 break;902 case KEY_DES3:903 result = interface_.UT_DES3GetKeyCount(handle_);904 break;905 case KEY_AES:906 result = interface_.UT_AESGetKeyCount(handle_);907 break;908 case KEY_RSA:909 result = interface_.UT_RSAGetKeyPairCount(handle_);910 break;911 case KEY_MD5HMAC:912 result = interface_.UT_MD5GetKeyCount(handle_);913 break;914 case KEY_SHA1HMAC:915 result = interface_.UT_SHA1GetKeyCount(handle_);916 break;917 default:918 result = KEY_BAD_TYPE;919 break;920 };921 922 log.code(result);923 924 if(result == OK)925 return interface_.KeyCount;926 if(result>>>0 == NO_KEY )927 return 0;928 return prepareResponse(result,undefined);929 }930 931 token.getKeys = function()932 {933 var list = {934 DES: token.getDESKeys(),935 DES3: token.getDES3Keys(),936 AES: token.getAESKeys(),937 //RSA: token.getRSAKeys(),938 MD5HMAC: token.getMD5HMACKeys(),939 SHA1HMAC: token.getSHA1HMACKeys()940 };941 return prepareResponse(OK,list);942 }943 token.getDESKeys = function(){ return get_Keys(KEY_DES);}944 token.getDES3Keys = function(){ return get_Keys(KEY_DES3);}945 token.getAESKeys = function(){ return get_Keys(KEY_AES);}946 token.getRSAKeys = function(){ return get_Keys(KEY_RSA);}947 token.getMD5HMACKeys = function(){ return get_Keys(KEY_MD5HMAC);}948 token.getSHA1HMACKeys = function(){ return get_Keys(KEY_SHA1HMAC);}949 var get_Keys = function(type)950 {951 log.message('Trying to get first key 0x'+type.toString(16)+' of token on slot '+slot_);952 953 var list = [];954 var result = getKey(type, true);955 956 log.code(result);957 958 if ( result == OK)959 {960 log.message('Key found! We add it in the list');961 list = createKeyResponse(type, list);962 }963 else964 {965 return prepareResponse(OK,list);966 }967 968 log.message('Trying to get next keys 0x'+type.toString(16)+' of token on slot '+slot_);969 970 result = getKey(type, false);971 972 log.code(result);973 974 while( result == OK )975 {976 log.message('Key found! We add it in the list');977 list = createKeyResponse(type, list);978 result = getKey(type, false);979 }980 return prepareResponse(OK,list);981 }982 var createKeyResponse = function(type, list){983 var handle = interface_.KeyHandle;984 var length = undefined;985 switch(type)986 {987 case KEY_RSA:988 var pub = interface_.PubKeyHandle;989 var priv = interface_.PriKeyHandle;990 var mod = undefined;991 var result = UT_RSAGetKeyPairModulus(handle_, handle);992 if(result == OK)993 mod = interface_.ModulusBits;994 list.push({995 pub_key: pub,996 priv_key:priv,997 modulus: mod998 });999 break;1000 case KEY_AES:1001 var result = interface_.UT_AESGetKeyLen(handle_, handle);1002 if(result == OK)1003 len = interface_.KeyLength;1004 list.push({1005 handle:handle,1006 length:len1007 });1008 break;1009 case KEY_MD5HMAC:1010 var result = interface_.UT_MD5GetKeyLen(handle_, handle);1011 if(result == OK)1012 len = interface_.KeyLength;1013 list.push({1014 handle:handle,1015 length:len1016 });1017 break;1018 case KEY_SHA1HMAC:1019 var result = interface_.UT_SHA1GetKeyLen(handle_, handle);1020 if(result == OK)1021 len = interface_.KeyLength;1022 list.push({1023 handle:handle,1024 length:len1025 });1026 break;1027 default:1028 list.push({1029 handle:handle1030 });1031 break;1032 }1033 return list;1034 }1035 1036 var getKey = function(type, first)1037 {1038 var result = undefined;1039 if(first)1040 {1041 switch(type)1042 {1043 case KEY_DES:1044 result = interface_.UT_DESGetFirstKeyHandle(handle_);1045 break;1046 case KEY_DES3:1047 result = interface_.UT_DES3GetFirstKeyHandle(handle_);1048 break;1049 case KEY_AES:1050 result = interface_.UT_AESGetFirstKeyHandle(handle_);1051 break;1052 case KEY_RSA:1053 result = interface_.UT_RSAGetFirstKeyPairHandle(handle_);1054 break;1055 case KEY_MD5HMAC:1056 result = interface_.UT_MD5GetFirstKeyHandle(handle_);1057 break;1058 case KEY_SHA1HMAC:1059 result = interface_.UT_SHA1GetFirstKeyHandle(handle_);1060 break;1061 default:1062 result = KEY_BAD_TYPE;1063 break;1064 };1065 }1066 else1067 {1068 switch(type)1069 {1070 case KEY_DES:1071 result = interface_.UT_DESGetNextKeyHandle(handle_);1072 break;1073 case KEY_DES3:1074 result = interface_.UT_DES3GetNextKeyHandle(handle_);1075 break;1076 case KEY_AES:1077 result = interface_.UT_AESGetNextKeyHandle(handle_);1078 break;1079 case KEY_RSA:1080 result = interface_.UT_RSAGetNextKeyPairHandle(handle_);1081 break;1082 case KEY_MD5HMAC:1083 result = interface_.UT_MD5GetNextKeyHandle(handle_);1084 break;1085 case KEY_SHA1HMAC:1086 result = interface_.UT_SHA1GetNextKeyHandle(handle_);1087 break;1088 default:1089 result = KEY_BAD_TYPE;1090 break;1091 };1092 }1093 return result;1094 }1095 token.getRSAPublicKeyHandle = function(key){ return getRSAKeyHandle(KEY_RSA_PRIVATE, key);}1096 token.getRSAPrivateKeyHandle = function(key){ return getRSAKeyHandle(KEY_RSA_PUBLIC, key);}1097 var getRSAKeyHandle = function(type, key)1098 {1099 log.message('Trying to get handle of RSA key from token on slot '+slot_);1100 var result = undefined;1101 var value = undefined1102 switch(type)1103 {1104 case KEY_RSA_PUBLIC:1105 result = interface_.embed1.UT_RSAGetPubKeyHandle(handle_, key);1106 if(result == OK)1107 {1108 value = interface_.PubKeyHandle;1109 }1110 break;1111 case KEY_RSA_PRIVATE:1112 result = interface_.embed1.UT_RSAGetPriKeyHandle(handle_, key);1113 if(result == OK)1114 {1115 value = interface_.PriKeyHandle;1116 }1117 break;1118 default:1119 result = KEY_BAD_TYPE;1120 break;1121 }1122 log.code(result);1123 1124 if(result == OK)1125 {1126 log.message('RSA key handle is: '+value);1127 return prepareResponse(result,value);1128 }1129 return result;1130 }1131 /*1132 Import / Export1133 NOTA: this plugin can not import, the functions are not yet implemented by the Firefox plugin.1134 */1135 token.exportRSAPublicKey = function(key){ return exportKey(KEY_RSA_PUBLIC, key);}1136 var exportKey = function(type, key)1137 {1138 log.message('Trying to export key from token on slot '+slot_);1139 var result = undefined;1140 switch(type)1141 {1142 case KEY_RSA_PUBLIC:1143 result = interface_.embed1.UT_RSAPubKeyExport(handle_, key);1144 break;1145 default:1146 result = KEY_BAD_TYPE;1147 break;1148 }1149 log.code(result);1150 1151 if(result == OK)1152 {1153 var value = interface_.base64pubkey;1154 log.message('RSA public key is: '+value);1155 return prepareResponse(result,value);1156 }1157 return result;1158 }1159 /*1160 Filesystem1161 */1162 token.getSpace = function()1163 {1164 var result = interface_.UT_FS_GetSpace(handle_);1165 1166 log.code(result);1167 1168 if(result == OK)1169 {1170 var value = {used:interface_.UsedSpace, free: interface_.FreeSpace, total:interface_.UsedSpace+interface_.FreeSpace};1171 return prepareResponse(result,value);1172 }1173 1174 return prepareResponse(result,undefined);1175 }1176 token.countFiles = function()1177 {1178 var result = interface_.UT_FS_GetFileCount(handle_);1179 1180 log.code(result);1181 1182 if(result == OK)1183 {1184 var value = interface_.FileCount;1185 return prepareResponse(result,value);1186 }1187 return prepareResponse(result,undefined);1188 }1189 token.getFiles = function()1190 {1191 var list = [];1192 1193 var result = interface_.UT_FS_GetFirstFileName(handle_);1194 if ( result == OK)1195 {1196 var file = token.getFile(interface_.FileName);1197 list.push({details:file.details(), file: file});1198 }1199 else1200 {1201 return list;1202 }1203 1204 result = interface_.UT_FS_GetNextFileName(handle_);1205 while( result == OK )1206 {1207 var file = token.getFile(interface_.FileName);1208 list.push({details:file.details(), file: file});1209 result = interface_.UT_FS_GetNextFileName(handle_);1210 }1211 return list;1212 }1213 token.getFile = function(filename)1214 {1215 //if( handle_ == undefined )1216 //return undefined;1217 return new File(interface_, handle_, filename);1218 }1219 1220 init();1221 open();1222 }1223 1224 $.Interface = function(element)1225 {1226 var element_ = $(element)[0];1227 var interface_ = this;1228 var tokens_ = undefined;1229 var auto_detection_ = undefined;1230 var token_detection = function()1231 {1232 var list_current = tokens_;1233 var list_now = listTokens();1234 var found = false;1235 if(settings.events.token_inserted != undefined)1236 {1237 for (var i = 0; i < list_now.length; i++)1238 {1239 if( inArray(list_now[i],list_current) == -1 )1240 {1241 settings.events.token_inserted( list_now[i] );1242 found = true;1243 }1244 };1245 }1246 if(settings.events.token_removed != undefined)1247 {1248 for (var i = 0; i < list_current.length; i++)1249 {1250 if( inArray(list_current[i],list_now) == -1 )1251 {1252 settings.events.token_removed( list_current[i] );1253 found = true;1254 }1255 };1256 }1257 1258 if( found )1259 interface_.refreshTokens();1260 }1261 interface_.startAutodetection = function()1262 {1263 interface_.stopAutodetection();1264 if(settings.events.token_detection_interval > 0 && (settings.events.token_inserted != undefined || settings.events.stoken_removed != undefined))1265 auto_detection_ = setInterval(function(){token_detection();},settings.token_detection_interval);1266 }1267 interface_.stopAutodetection = function()1268 {1269 if(auto_detection_ != undefined)1270 clearInterval(auto_detection_);1271 }1272 1273 var init = function()1274 {1275 log.message('New interface created');1276 log.message('Trying to initialize it');1277 interface_.refreshTokens();1278 interface_.startAutodetection();1279 1280 var result = element_.UT_Initialize();1281 1282 log.code(result);1283 }1284 1285 var close = function()1286 {1287 element_.UT_Finalize();1288 log.message('Interface is finalized.');1289 }1290 1291 var listTokens = function()1292 {1293 log.message('Trying to get first token');1294 var list = [];1295 var result = element_.UT_GetFirstToken();1296 log.code(result);1297 if (result != OK)1298 {1299 return list;1300 }1301 1302 list = updateTokenList(list);1303 1304 log.message('Trying to get next token');1305 result = element_.UT_GetNextToken();1306 log.code(result);1307 1308 while(result == OK)1309 {1310 list = updateTokenList(list);1311 result = element_.UT_GetNextToken();1312 }1313 return list;1314 }1315 1316 interface_.refreshTokens = function()1317 {1318 tokens_ = listTokens();1319 }1320 1321 interface_.getTokens = function()1322 {1323 if(tokens_ != undefined)1324 return tokens_;1325 interface_.refreshTokens();1326 return tokens_;1327 }1328 1329 var updateTokenList = function(list)1330 {1331 log.message('Will add the token to the list');1332 var details = getTokenDetails(element_.SlotID);1333 if( details != undefined)1334 list.push( details );1335 return list;1336 }1337 1338 interface_.getToken = function(slot)1339 {1340 return new Token(element_, slot);1341 }1342 1343 var getTokenDetails = function(slot)1344 {1345 var token = interface_.getToken(slot);1346 var list = token.getDetails();1347 return list;1348 }1349 1350 interface_.getLibraryVersion = function()1351 {1352 var result = element_.UT_GetLibraryVersion();1353 if( result == OK )1354 return element_.Version;1355 return undefined;1356 }1357 1358 init();1359 }1360 $.fn.unitoken = function(options) {1361 settings = $.extend({}, defaults, options);1362 return this.each(function() {1363 if (undefined == $(this).data('unitoken'))1364 {1365 if( $(this).attr("type") !== "application/mozilla-npsecuunitoken-scriptable-plugin" )1366 {1367 console.log("Selected element is not an embed UniToken scriptable plugin." );1368 return;1369 }1370 1371 var plugin = new $.Interface(this)1372 $(this).data('unitoken', plugin);1373 }1374 return $(this);1375 });1376 }1377 $.fn.getTokens = function() {1378 if (undefined == $(this).data('unitoken'))1379 {1380 console.log("Selected element is not an UniToken object or must be initialized. Please use 'unitoken' function first." );1381 return;1382 }1383 1384 return $(this).data('unitoken').getTokens();1385 }1386 $.fn.refreshTokens = function() {1387 if (undefined == $(this).data('unitoken'))1388 {1389 console.log("Selected element is not an UniToken object or must be initialized. Please use 'unitoken' function first." );1390 return;1391 }1392 $(this).data('unitoken').refreshTokens()1393 return $(this);1394 }1395 $.fn.getToken = function(slot) {1396 if (undefined == $(this).data('unitoken'))1397 {1398 console.log("Selected element is not an UniToken object or must be initialized. Please use 'unitoken' function first." );1399 return;1400 }1401 1402 return $(this).data('unitoken').getToken(slot);1403 }1404 $.fn.getLibraryVersion = function() {1405 if (undefined == $(this).data('unitoken'))1406 {1407 console.log("Selected element is not an UniToken object or must be initialized. Please use 'unitoken' function first." );1408 return;1409 }1410 return $(this).data('unitoken').getLibraryVersion();1411 }1412 $.fn.getPluginVersion = function() {1413 return PLUGIN_VERSION;1414 }...
executions.js
Source:executions.js
1const traversal = require('tosca.lib.traversal');2const tosca = require('tosca.lib.utils');3// TODO: not here4for (let vertexId in clout.vertexes) {5 let vertex = clout.vertexes[vertexId];6 if (!tosca.isNodeTemplate(vertex))7 continue;8 let nodeTemplate = vertex.properties;9 for (let artifactName in nodeTemplate.artifacts) {10 let artifact = nodeTemplate.artifacts[artifactName];11 if ('cloud.puccini.turandot.orchestration::Key' in artifact.types)12 artifact.$artifact = puccini.loadString(artifact.sourcePath);13 }14}15traversal.coerce();16let executions = [];17for (let vertexId in clout.vertexes) {18 let vertex = clout.vertexes[vertexId];19 if (!tosca.isNodeTemplate(vertex))20 continue;21 let nodeTemplate = vertex.properties;22 // Sort names23 let interfaceNames = [];24 for (let interfaceName in nodeTemplate.interfaces)25 interfaceNames.push(interfaceName);26 interfaceNames.sort();27 for (let i = 0, l = interfaceNames.length; i < l; i++) {28 let interfaceName = interfaceNames[i];29 let interface_ = nodeTemplate.interfaces[interfaceName];30 if ('cloud.puccini.turandot.orchestration::Execution' in interface_.types) {31 let execution = {nodeTemplate: nodeTemplate.name};32 if (interface_.inputs.mode)33 execution.mode = interface_.inputs.mode;34 else {35 let last = interfaceName.lastIndexOf('.');36 if (last !== -1)37 execution.mode = interfaceName.substring(0, last);38 else39 execution.mode = interfaceName;40 }41 if (interface_.inputs.requirements)42 execution.requirements = interface_.inputs.requirements;43 if ('cloud.puccini.turandot.orchestration::Scriptlet' in interface_.types) {44 execution.type = 'clout';45 execution.scriptlet = interface_.inputs.scriptlet;46 execution.arguments = {47 service: puccini.arguments.service,48 nodeTemplate: nodeTemplate.name49 };50 // TODO: verify that the scriptlet exists51 if (interface_.inputs.arguments)52 for (let k in interface_.inputs.arguments)53 execution.arguments[k] = interface_.inputs.arguments[k];54 } else if ('cloud.puccini.kubernetes::Command' in interface_.types) {55 let artifacts = getArtifacts(nodeTemplate, interface_.inputs.artifacts);56 if (artifacts)57 execution.artifacts = artifacts;58 if ('cloud.puccini.kubernetes::ContainerCommand' in interface_.types) {59 execution.type = 'container';60 if (interface_.inputs.container)61 execution.container = interface_.inputs.container;62 let metadata = getKubernetesMetadata(nodeTemplate);63 if (metadata.namespace)64 execution.namespace = metadata.namespace;65 if (interface_.inputs.selector)66 execution.selector = interface_.inputs.selector;67 else {68 if (metadata)69 execution.selector = {matchLabels: metadata.labels};70 // TODO: matchExpressions71 else72 throw 'no pod selector for execution';73 }74 execution.pods = interface_.inputs.pods;75 } else if ('cloud.puccini.kubernetes::SSHCommand' in interface_.types) {76 execution.type = 'ssh';77 execution.host = interface_.inputs.host || '';78 execution.username = interface_.inputs.username || '';79 execution.key = interface_.inputs.key || '';80 }81 // Process special "$$" command arguments82 execution.command = interface_.inputs.command.slice();83 for (let ii = 1, ll = execution.command.length; ii < ll; ii++) {84 let arg = execution.command[ii];85 if (arg.substring(0, 2) === '$$') {86 arg = execution[arg.substring(2)];87 if (arg !== undefined)88 execution.command[ii] = arg;89 }90 }91 }92 executions.push(execution);93 }94 }95}96puccini.write({executions: executions});97function getKubernetesMetadata(nodeTemplate) {98 for (let capabilityName in nodeTemplate.capabilities) {99 let capability = nodeTemplate.capabilities[capabilityName];100 if ('cloud.puccini.kubernetes::Metadata' in capability.types)101 return puccini.deepCopy(capability.properties);102 }103 return null;104}105function getArtifacts(nodeTemplate, artifactNames) {106 let artifacts = [];107 if (artifactNames && nodeTemplate.artifacts)108 for (let i = 0, l = artifactNames.length; i < l; i++) {109 let artifactName = artifactNames[i];110 let artifact = nodeTemplate.artifacts[artifactName];111 if (artifact === undefined)112 throw puccini.sprintf('artifact not found: %s', artifactName);113 let info = {114 sourceUrl: artifact.sourcePath,115 targetPath: artifact.targetPath116 };117 if (('cloud.puccini.turandot.orchestration::Deployable' in artifact.types) && (artifact.properties.permissions !== undefined))118 info.permissions = artifact.properties.permissions;119 artifacts.push(info);120 }121 return artifacts;...
interfaces.js
Source:interfaces.js
1'use strict';2/**3 * Expose `Interfaces`.4 */5module.exports = Interfaces;6/**7 * Module dependencies.8 */9var utils = require('../../../utils'),10 SectionProcessor = require('../../../configuration/section-processor')11;12utils.extend(SectionProcessor, Interfaces);13/**14 * Initialize a new section processor interfaces for the config.15 */16function Interfaces() {17 SectionProcessor.call(this);18 this.contract = {19 __any: {20 extends: {21 type: 'string'22 },23 methods: {24 type: 'embedded_object',25 embed: {26 arguments: {27 type: 'string_array',28 default: []29 },30 returns: {31 type: 'string'32 }33 },34 default: {}35 },36 getters: {37 type: 'string_object',38 default: {}39 },40 setters: {41 type: 'string_object',42 default: {}43 }44 },45 type: 'embedded',46 namespace: true47 };48}49/**50 * @interface {danf:configuration.sectionProcessor}51 */52Interfaces.prototype.interpretModuleConfig = function(config, module, modulesTree) {53 for (var interfaceName in config) {54 var interface_ = config[interfaceName];55 // Handle method arguments namespacing.56 if (interface_.methods) {57 for (var name in interface_.methods) {58 var method = interface_.methods[name];59 if (method.arguments) {60 for (var i = 0; i < method.arguments.length; i++) {61 method.arguments[i] = namespaceType.call(62 this,63 method.arguments[i],64 module,65 modulesTree66 );67 }68 }69 }70 }71 // Handle getters namespacing.72 if (interface_.getters) {73 for (var name in interface_.getters) {74 interface_.getters[name] = namespaceType.call(75 this,76 interface_.getters[name],77 module,78 modulesTree79 );80 }81 }82 // Handle setters namespacing.83 if (interface_.setters) {84 for (var name in interface_.setters) {85 interface_.setters[name] = namespaceType.call(86 this,87 interface_.setters[name],88 module,89 modulesTree90 );91 }92 }93 }94 return config;95}96/**97 * Namespace a type.98 *99 * @param {string} type The type.100 * @param {danf:configuration.module} module The module.101 * @param {danf:configuration.modulesTree} modulesTree The modules tree.102 * @return {string} The namespaced type.103 * @api private104 */105var namespaceType = function(type, module, modulesTree) {106 var argument = type.split('/'),107 types = argument[0].split('|')108 ;109 for (var i = 0; i < types.length; i++) {110 if (Object.isInterfaceType(types[i])) {111 types[i] = this._namespacer.prefix(types[i], module, modulesTree);112 }113 }114 type = types.join('|');115 return argument[1] ? '{0}/{1}'.format(type, argument[1]) : type;...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.runTest(url, function(err, data) {4 if (err) return console.error(err);5 wpt.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data.data.average.firstView.SpeedIndex);8 });9});10var wpt = require('webpagetest');11var wpt = new WebPageTest('www.webpagetest.org');12wpt.runTest(url, function(err, data) {13 if (err) return console.error(err);14 wpt.getTestResults(data.data.testId, function(err, data) {15 if (err) return console.error(err);16 console.log(data.data.average.firstView.SpeedIndex);17 });18});19var wpt = require('webpagetest');20var wpt = new WebPageTest('www.webpagetest.org');21wpt.runTest(url, function(err, data) {22 if (err) return console.error(err);23 wpt.getTestResults(data.data.testId, function(err, data) {24 if (err) return console.error(err);25 console.log(data.data.average.firstView.SpeedIndex);26 });27});28var wpt = require('webpagetest');29var wpt = new WebPageTest('www.webpagetest.org');30wpt.runTest(url, function(err, data) {31 if (err) return console.error(err);32 wpt.getTestResults(data.data.testId, function(err, data) {33 if (err) return console.error(err);34 console.log(data.data.average.firstView.SpeedIndex);35 });36});37var wpt = require('webpagetest');38var wpt = new WebPageTest('www.webpagetest.org');
Using AI Code Generation
1var wpt_interface = require('./wpt_interface');2wpt_interface.interface_method();3module.exports = {4 interface_method: function() {5 console.log("This is interface method");6 }7}8var wpt_interface = require('./wpt_interface');9module.exports = {10 interface_method: function() {11 console.log("This is interface method of wpt_interface_2");12 }13}14var wpt_interface = require('./wpt_interface');15module.exports = {16 interface_method: function() {17 console.log("This is interface method of wpt_interface_3");18 }19}20var wpt_interface = require('./wpt_interface');21module.exports = {22 interface_method: function() {23 console.log("This is interface method of wpt_interface_4");24 }25}
Using AI Code Generation
1var wptools = require('wptools');2var wiki = require('wikijs').default;3var express = require('express');4var app = express();5var bodyParser = require('body-parser');6app.use(bodyParser.urlencoded({ extended: false }));7app.use(bodyParser.json());8var port = 3000;9app.listen(port, function() {10 console.log('listening on port ' + port);11});12app.get('/', function(req, res) {13 res.send('Hello World!');14});15app.post('/api', function(req, res) {16 console.log(req.body);17 var text = req.body.text;18 var url = req.body.url;19 console.log(text);20 console.log(url);21 var query = wptools.page(text).get();22 query.then(function(response) {23 var infobox = response.infobox;24 var data = response.data;25 var wikidata = response.wikidata;26 var categories = response.categories;27 var coordinates = response.coordinates;28 var langlinks = response.langlinks;29 var lang = response.lang;30 var imageinfo = response.imageinfo;31 var image = response.image;32 var pageid = response.pageid;33 var title = response.title;34 var url = response.url;35 var extract = response.extract;36 var images = response.images;37 var links = response.links;38 var related = response.related;39 var html = response.html;40 var text = response.text;41 var references = response.references;42 var disambiguation = response.disambiguation;43 var random = response.random;44 var redirect = response.redirect;45 var revision = response.revision;46 var sections = response.sections;47 var templates = response.templates;48 var url_raw = response.url_raw;49 var url_wiki = response.url_wiki;50 var url_canonical = response.url_canonical;51 var url_edit = response.url_edit;52 var url_history = response.url_history;53 var url_delete = response.url_delete;54 var url_talk = response.url_talk;55 var url_talk_raw = response.url_talk_raw;56 var url_talk_wiki = response.url_talk_wiki;57 var url_talk_canonical = response.url_talk_canonical;58 var url_talk_edit = response.url_talk_edit;
Using AI Code Generation
1var wptoolbox = require('wptoolbox');2var myInterface = wptoolbox.interface_();3myInterface.method();4var wptoolbox = require('wptoolbox');5var myInterface = wptoolbox.interface_();6myInterface.method();7var wptoolbox = require('wptoolbox');8var myInterface = wptoolbox.interface_();9myInterface.method();10var wptoolbox = require('wptoolbox');11var myInterface = wptoolbox.interface_();12myInterface.method();13var wptoolbox = require('wptoolbox');14var myInterface = wptoolbox.interface_();15myInterface.method();16var wptoolbox = require('wptoolbox');17var myInterface = wptoolbox.interface_();18myInterface.method();19var wptoolbox = require('wptoolbox');20var myInterface = wptoolbox.interface_();21myInterface.method();22var wptoolbox = require('wptoolbox');23var myInterface = wptoolbox.interface_();24myInterface.method();25var wptoolbox = require('wptoolbox');26var myInterface = wptoolbox.interface_();27myInterface.method();28var wptoolbox = require('wptoolbox');
Using AI Code Generation
1let wpt = require('wpt-api')2let wpt = new wpt('my_api_key')3wpt.interface( (err, res) => {4 if (err) {5 console.log(err)6 } else {7 console.log(res)8 }9})
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.3e3d3a3c3b3c3b3c3b3c3b3c3b3c3b3');3 if (err) return console.error(err);4 console.log('Test Submitted');5 wpt.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log('Test Results', data);8 });9});10{11 "dependencies": {12 }13}14{15 "dependencies": {16 "wpt": {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!