How to use socketCache method in wpt

Best JavaScript code snippet using wpt

pending.js

Source:pending.js Github

copy

Full Screen

1/*2Copyright 2012 Timothy J Fontaine <tjfontaine@gmail.com>3Permission is hereby granted, free of charge, to any person obtaining a copy4of this software and associated documentation files (the "Software"), to deal5in the Software without restriction, including without limitation the rights6to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7copies of the Software, and to permit persons to whom the Software is8furnished to do so, subject to the following conditions:9The above copyright notice and this permission notice shall be included in10all copies or substantial portions of the Software.11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR12IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,13FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE14AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER15LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,16OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN17*/18'use strict';19var dgram = require('dgram'),20 net = require('net'),21 util = require('util'),22 Packet = require('./packet').Packet,23 EDNSPacket = require('./packet').EDNSPacket,24 TCPMessage = require('./utils').TCPMessage,25 Socket = require('./utils').Socket;26var SocketCache = function(parent) {27 this._pending = {};28 this._socket = {};29 this._parent = parent;30};31SocketCache.prototype._hash = function(server) {32 if (server.type === 'tcp')33 return server.address + ':' + server.port;34 else35 return 'udp' + net.isIP(server.address);36};37SocketCache.prototype._getPending = function(server) {38 var name = this._hash(server);39 return this._pending[name];40};41SocketCache.prototype._pendingAdd = function(server, cb) {42 var name = this._hash(server);43 if (!this._pending[name]) {44 this._pending[name] = [];45 }46 this._pending[name].push(cb);47};48SocketCache.prototype._pendingRemove = function(server) {49 var name = this._hash(server);50 delete this._pending[name];51};52SocketCache.prototype._toInternalSocket = function(server, socket) {53 var S;54 if (server.type === 'tcp') {55 S = new Socket(null, socket);56 } else {57 S = new Socket(socket, server);58 }59 return S;60};61SocketCache.prototype._pendingEmit = function(server, socket) {62 var S, pending, self = this;63 pending = this._getPending(server);64 if (pending) {65 self._socketAdd(server, socket);66 this._pendingRemove(server);67 S = this._toInternalSocket(server, socket);68 pending.forEach(function(cb) {69 cb(S);70 });71 }72};73SocketCache.prototype._getSocket = function(server) {74 var name = this._hash(server);75 return this._socket[name];76};77SocketCache.prototype._socketRemoveInternal = function(shash, socket) {78 if (socket) {79 delete this._socket[shash];80 if (socket.socket.end) {81 socket.socket.end();82 } else {83 socket.socket.close();84 }85 }86};87SocketCache.prototype._socketRemove = function(server) {88 var cache_name = this._hash(server);89 var socket = this._getSocket(server);90 this._socketRemoveInternal(cache_name, socket);91};92SocketCache.prototype._socketAdd = function(server, socket) {93 var self = this;94 var cache_name = this._hash(server);95 this._socket[cache_name] = {96 last: new Date().getTime(),97 socket: socket98 };99};100SocketCache.prototype._createTcp = function(server) {101 var socket, self = this, tcp;102 socket = net.connect(server.port, server.address);103 socket.on('timeout', function() {104 self._pendingRemove(server);105 self._socketRemove(server);106 });107 socket.on('close', function() {108 self._pendingRemove(server);109 self._socketRemove(server);110 });111 socket.on('connect', function() {112 self._pendingEmit(server, socket);113 });114 tcp = new TCPMessage(socket, function(msg, socket) {115 self._parent.handleMessage(server, msg, socket);116 });117};118SocketCache.prototype._createUdp = function(server) {119 var socket, self = this,120 type = net.isIP(server.address);121 if (type) {122 socket = dgram.createSocket('udp' + type);123 socket.on('message', function(msg, remote) {124 // 20 is the smallest a packet could be when asking for the root125 // we have no way to associate this response to any request, thus if the126 // packet was broken somewhere along the way it will result in a timeout127 if (msg.length >= 20)128 self._parent.handleMessage(server, msg, new Socket(socket, remote));129 });130 socket.on('close', function() {131 self._socketRemove(server);132 });133 socket.on('listening', function() {134 //self._socketAdd(server, socket);135 self._pendingEmit(server, socket);136 });137 socket.bind();138 }139};140SocketCache.prototype.get = function(server, cb) {141 var socket, pending, S;142 socket = this._getSocket(server);143 pending = this._getPending(server);144 if (!socket) {145 this._pendingAdd(server, cb);146 if (!pending) {147 if (server.type === 'tcp') {148 this._createTcp(server);149 } else {150 this._createUdp(server);151 }152 }153 } else {154 socket.last = new Date().getTime();155 S = this._toInternalSocket(server, socket.socket);156 cb(S);157 }158};159SocketCache.prototype.close = function(shash) {160 var socket = this._socket[shash];161 this._socketRemoveInternal(shash, socket);162};163var random_integer = function() {164 return Math.floor(Math.random() * 50000 + 1);165};166var SOCKET_TIMEOUT = 300;167var ServerQueue = module.exports = function(parent, active) {168 var self = this;169 this._queue = {};170 this._active = {};171 this._socketCache = new SocketCache(parent);172 this._max_queue = active;173 var check_sockets = function() {174 var s, now;175 now = new Date().getTime();176 Object.keys(self._socketCache._socket).forEach(function(s) {177 var socket = self._socketCache._socket[s];178 var delta = now - socket.last;179 var m = { server: s, delta: delta };180 if (self._queue[s])181 m.queue = self._queue[s].order.length;182 if (self._active[s])183 m.active = self._active[s].count;184 if (delta > SOCKET_TIMEOUT && self._queue[s].order.length === 0 &&185 self._active[s].count === 0) {186 self._socketCache.close(s);187 }188 });189 if (Object.keys(self._socketCache._socket).length) {190 self._timer = setTimeout(check_sockets, SOCKET_TIMEOUT);191 }192 };193 self._timer = setTimeout(check_sockets, SOCKET_TIMEOUT);194};195ServerQueue.prototype._hash = function(server) {196 if (server.type === 'tcp')197 return server.address + ':' + server.port;198 else199 return 'udp' + net.isIP(server.address);200};201ServerQueue.prototype._getQueue = function(server) {202 var name = this._hash(server);203 if (!this._queue[name]) {204 this._queue[name] = {205 order: []206 };207 }208 return this._queue[name];209};210ServerQueue.prototype._getActive = function(server) {211 var name = this._hash(server);212 if (!this._active[name]) {213 this._active[name] = {214 count: 0215 };216 }217 return this._active[name];218};219ServerQueue.prototype.add = function(server, request, cb) {220 var name, id, queue, active;221 name = this._hash(server);222 queue = this._getQueue(server);223 active = this._getActive(server);224 id = random_integer();225 while (queue[id] || active[id]) id = random_integer();226 queue[id] = {227 request: request,228 cb: cb229 };230 queue.order.splice(0, 0, id);231 request.id = id;232 this.fill(server);233};234ServerQueue.prototype.remove = function(server, id) {235 var idx, queue, active;236 queue = this._getQueue(server);237 active = this._getActive(server);238 delete queue[id];239 idx = queue.order.indexOf(id);240 if (idx > -1)241 queue.order.splice(idx, 1);242 if (active[id]) {243 delete active[id];244 active.count -= 1;245 }246 this.fill(server);247};248ServerQueue.prototype.pop = function(server) {249 var queue, active, id, obj;250 queue = this._getQueue(server);251 active = this._getActive(server);252 id = queue.order.pop();253 obj = queue[id];254 if (id && obj) {255 active[id] = obj.request;256 active.count += 1;257 return obj.cb;258 }259};260ServerQueue.prototype.fill = function(server) {261 var active, cb;262 active = this._getActive(server);263 while (active.count < this._max_queue) {264 cb = this.pop(server);265 if (cb)266 this._socketCache.get(server, cb);267 else268 break;269 }270};271ServerQueue.prototype.getRequest = function(server, id) {272 var active = this._getActive(server);273 return active[id];274};275var PendingRequests = function() {276 this._server_queue = new ServerQueue(this, 100);277 this.autopromote = true;278};279PendingRequests.prototype.send = function(request) {280 var packet;281 this._server_queue.add(request.server, request, function(socket) {282 if (request.try_edns) {283 packet = new EDNSPacket(socket);284 } else {285 packet = new Packet(socket);286 }287 packet.header.id = request.id;288 packet.header.rd = 1;289 packet.question.push(request.question);290 try {291 packet.send();292 } catch (e) {293 request.error(e);294 }295 });296};297PendingRequests.prototype.remove = function(request) {298 if (request.server && request.id)299 this._server_queue.remove(request.server, request.id);300};301PendingRequests.prototype.handleMessage = function(server, msg, socket) {302 var err, request, answer;303 answer = new Packet(socket);304 answer.unpack(msg, this.autopromote);305 answer = answer.promote(this.autopromote);306 request = this._server_queue.getRequest(server, answer.header.id);307 if (request)308 {309 this.remove(request);310 request.handle(err, answer);311 }312};...

Full Screen

Full Screen

cache.js

Source:cache.js Github

copy

Full Screen

1'use strict';2const SocketCache = module.exports;3SocketCache.clear = async function (socket, data) {4 if (data.name === 'post') {5 require('../../posts/cache').reset();6 } else if (data.name === 'object') {7 require('../../database').objectCache.reset();8 } else if (data.name === 'group') {9 require('../../groups').cache.reset();10 } else if (data.name === 'local') {11 require('../../cache').reset();12 }13};14SocketCache.toggle = async function (socket, data) {15 const caches = {16 post: require('../../posts/cache'),17 object: require('../../database').objectCache,18 group: require('../../groups').cache,19 local: require('../../cache'),20 };21 if (!caches[data.name]) {22 return;23 }24 caches[data.name].enabled = data.enabled;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2socket.on('connect', function(){3 console.log('connected to socket');4 var page = wptools.page('Barack Obama');5 page.socketCache(socket, function(err, resp){6 if(err){7 console.log(err);8 }9 else{10 console.log(resp);11 }12 });13});14socket.on('disconnect', function(){15 console.log('disconnected from socket');16});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var fs = require('fs');3var ws = fs.createWriteStream('output.json');4var wp = wptools.page('Barack_Obama');5wp.get(function(err, resp) {6 if (err) {7 console.log(err);8 } else {9 ws.write(JSON.stringify(resp));10 }11});12{13 "namespace": {14 },15 "titles": {16 },17 "thumbnail": {18 },19 "originalimage": {20 },21 "description": "44th President of the United States (2009–2017)",22 "content_urls": {23 "desktop": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptoolbox = require('wptoolbox');2var fs = require('fs');3var path = require('path');4var socketCache = wptoolbox.socketCache;5var socket = socketCache('localhost', 8080);6socket.on('connect', function () {7 var file = fs.createReadStream(path.join(__dirname, 'test.txt'));8 var request = socket.request({9 });10 request.on('response', function (response) {11 console.log(response.statusCode);12 });13 file.pipe(request);14});

Full Screen

Using AI Code Generation

copy

Full Screen

1wptools.requestCache = true;2wptools.requestCacheDir = './cache';3var page = wptools.page('Barack Obama').get(function(err, resp) {4 if (err) {5 console.log(err);6 }7 else {8 console.log(resp);9 }10});

Full Screen

Using AI Code Generation

copy

Full Screen

1const wptSocket = require('wpt-socket');2 if (err) {3 console.log(err);4 } else {5 console.log(data);6 }7});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.0c0e7c8c2b2b7f2b1d2e8d2f9c9d3b3e');3wpt.runTest(url, function(err, data) {4 if (err) return console.error(err);5 console.log('Test submitted to WebPagetest for %s', url);6 console.log('Test ID: %s', data.data.testId);7 wpt.socketCache(data.data.testId, function(err, data) {8 if (err) return console.error(err);9 console.log('Cache data for %s', url);10 console.log(data);11 });12});

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful