Best JavaScript code snippet using wpt
message_handler.js
Source:message_handler.js
...40 PULL: 6,41 PULL_COMPLETE: 7,42 START_COMPLETE: 843};44function wrapReason(reason) {45 if (typeof reason !== "object" || reason === null) {46 return reason;47 }48 switch (reason.name) {49 case "AbortException":50 return new _util.AbortException(reason.message);51 case "MissingPDFException":52 return new _util.MissingPDFException(reason.message);53 case "UnexpectedResponseException":54 return new _util.UnexpectedResponseException(reason.message, reason.status);55 case "UnknownErrorException":56 return new _util.UnknownErrorException(reason.message, reason.details);57 default:58 return new _util.UnknownErrorException(reason.message, reason.toString());59 }60}61class MessageHandler {62 constructor(sourceName, targetName, comObj) {63 this.sourceName = sourceName;64 this.targetName = targetName;65 this.comObj = comObj;66 this.callbackId = 1;67 this.streamId = 1;68 this.postMessageTransfers = true;69 this.streamSinks = Object.create(null);70 this.streamControllers = Object.create(null);71 this.callbackCapabilities = Object.create(null);72 this.actionHandler = Object.create(null);73 this._onComObjOnMessage = event => {74 const data = event.data;75 if (data.targetName !== this.sourceName) {76 return;77 }78 if (data.stream) {79 this._processStreamMessage(data);80 return;81 }82 if (data.callback) {83 const callbackId = data.callbackId;84 const capability = this.callbackCapabilities[callbackId];85 if (!capability) {86 throw new Error(`Cannot resolve callback ${callbackId}`);87 }88 delete this.callbackCapabilities[callbackId];89 if (data.callback === CallbackKind.DATA) {90 capability.resolve(data.data);91 } else if (data.callback === CallbackKind.ERROR) {92 capability.reject(wrapReason(data.reason));93 } else {94 throw new Error("Unexpected callback case");95 }96 return;97 }98 const action = this.actionHandler[data.action];99 if (!action) {100 throw new Error(`Unknown action from worker: ${data.action}`);101 }102 if (data.callbackId) {103 const cbSourceName = this.sourceName;104 const cbTargetName = data.sourceName;105 new Promise(function (resolve) {106 resolve(action(data.data));107 }).then(function (result) {108 comObj.postMessage({109 sourceName: cbSourceName,110 targetName: cbTargetName,111 callback: CallbackKind.DATA,112 callbackId: data.callbackId,113 data: result114 });115 }, function (reason) {116 comObj.postMessage({117 sourceName: cbSourceName,118 targetName: cbTargetName,119 callback: CallbackKind.ERROR,120 callbackId: data.callbackId,121 reason: wrapReason(reason)122 });123 });124 return;125 }126 if (data.streamId) {127 this._createStreamSink(data);128 return;129 }130 action(data.data);131 };132 comObj.addEventListener("message", this._onComObjOnMessage);133 }134 on(actionName, handler) {135 const ah = this.actionHandler;136 if (ah[actionName]) {137 throw new Error(`There is already an actionName called "${actionName}"`);138 }139 ah[actionName] = handler;140 }141 send(actionName, data, transfers) {142 this._postMessage({143 sourceName: this.sourceName,144 targetName: this.targetName,145 action: actionName,146 data147 }, transfers);148 }149 sendWithPromise(actionName, data, transfers) {150 const callbackId = this.callbackId++;151 const capability = (0, _util.createPromiseCapability)();152 this.callbackCapabilities[callbackId] = capability;153 try {154 this._postMessage({155 sourceName: this.sourceName,156 targetName: this.targetName,157 action: actionName,158 callbackId,159 data160 }, transfers);161 } catch (ex) {162 capability.reject(ex);163 }164 return capability.promise;165 }166 sendWithStream(actionName, data, queueingStrategy, transfers) {167 const streamId = this.streamId++;168 const sourceName = this.sourceName;169 const targetName = this.targetName;170 const comObj = this.comObj;171 return new ReadableStream({172 start: controller => {173 const startCapability = (0, _util.createPromiseCapability)();174 this.streamControllers[streamId] = {175 controller,176 startCall: startCapability,177 pullCall: null,178 cancelCall: null,179 isClosed: false180 };181 this._postMessage({182 sourceName,183 targetName,184 action: actionName,185 streamId,186 data,187 desiredSize: controller.desiredSize188 }, transfers);189 return startCapability.promise;190 },191 pull: controller => {192 const pullCapability = (0, _util.createPromiseCapability)();193 this.streamControllers[streamId].pullCall = pullCapability;194 comObj.postMessage({195 sourceName,196 targetName,197 stream: StreamKind.PULL,198 streamId,199 desiredSize: controller.desiredSize200 });201 return pullCapability.promise;202 },203 cancel: reason => {204 (0, _util.assert)(reason instanceof Error, "cancel must have a valid reason");205 const cancelCapability = (0, _util.createPromiseCapability)();206 this.streamControllers[streamId].cancelCall = cancelCapability;207 this.streamControllers[streamId].isClosed = true;208 comObj.postMessage({209 sourceName,210 targetName,211 stream: StreamKind.CANCEL,212 streamId,213 reason: wrapReason(reason)214 });215 return cancelCapability.promise;216 }217 }, queueingStrategy);218 }219 _createStreamSink(data) {220 const self = this;221 const action = this.actionHandler[data.action];222 const streamId = data.streamId;223 const sourceName = this.sourceName;224 const targetName = data.sourceName;225 const comObj = this.comObj;226 const streamSink = {227 enqueue(chunk, size = 1, transfers) {228 if (this.isCancelled) {229 return;230 }231 const lastDesiredSize = this.desiredSize;232 this.desiredSize -= size;233 if (lastDesiredSize > 0 && this.desiredSize <= 0) {234 this.sinkCapability = (0, _util.createPromiseCapability)();235 this.ready = this.sinkCapability.promise;236 }237 self._postMessage({238 sourceName,239 targetName,240 stream: StreamKind.ENQUEUE,241 streamId,242 chunk243 }, transfers);244 },245 close() {246 if (this.isCancelled) {247 return;248 }249 this.isCancelled = true;250 comObj.postMessage({251 sourceName,252 targetName,253 stream: StreamKind.CLOSE,254 streamId255 });256 delete self.streamSinks[streamId];257 },258 error(reason) {259 (0, _util.assert)(reason instanceof Error, "error must have a valid reason");260 if (this.isCancelled) {261 return;262 }263 this.isCancelled = true;264 comObj.postMessage({265 sourceName,266 targetName,267 stream: StreamKind.ERROR,268 streamId,269 reason: wrapReason(reason)270 });271 },272 sinkCapability: (0, _util.createPromiseCapability)(),273 onPull: null,274 onCancel: null,275 isCancelled: false,276 desiredSize: data.desiredSize,277 ready: null278 };279 streamSink.sinkCapability.resolve();280 streamSink.ready = streamSink.sinkCapability.promise;281 this.streamSinks[streamId] = streamSink;282 new Promise(function (resolve) {283 resolve(action(data.data, streamSink));284 }).then(function () {285 comObj.postMessage({286 sourceName,287 targetName,288 stream: StreamKind.START_COMPLETE,289 streamId,290 success: true291 });292 }, function (reason) {293 comObj.postMessage({294 sourceName,295 targetName,296 stream: StreamKind.START_COMPLETE,297 streamId,298 reason: wrapReason(reason)299 });300 });301 }302 _processStreamMessage(data) {303 const streamId = data.streamId;304 const sourceName = this.sourceName;305 const targetName = data.sourceName;306 const comObj = this.comObj;307 switch (data.stream) {308 case StreamKind.START_COMPLETE:309 if (data.success) {310 this.streamControllers[streamId].startCall.resolve();311 } else {312 this.streamControllers[streamId].startCall.reject(wrapReason(data.reason));313 }314 break;315 case StreamKind.PULL_COMPLETE:316 if (data.success) {317 this.streamControllers[streamId].pullCall.resolve();318 } else {319 this.streamControllers[streamId].pullCall.reject(wrapReason(data.reason));320 }321 break;322 case StreamKind.PULL:323 if (!this.streamSinks[streamId]) {324 comObj.postMessage({325 sourceName,326 targetName,327 stream: StreamKind.PULL_COMPLETE,328 streamId,329 success: true330 });331 break;332 }333 if (this.streamSinks[streamId].desiredSize <= 0 && data.desiredSize > 0) {334 this.streamSinks[streamId].sinkCapability.resolve();335 }336 this.streamSinks[streamId].desiredSize = data.desiredSize;337 const {338 onPull339 } = this.streamSinks[data.streamId];340 new Promise(function (resolve) {341 resolve(onPull && onPull());342 }).then(function () {343 comObj.postMessage({344 sourceName,345 targetName,346 stream: StreamKind.PULL_COMPLETE,347 streamId,348 success: true349 });350 }, function (reason) {351 comObj.postMessage({352 sourceName,353 targetName,354 stream: StreamKind.PULL_COMPLETE,355 streamId,356 reason: wrapReason(reason)357 });358 });359 break;360 case StreamKind.ENQUEUE:361 (0, _util.assert)(this.streamControllers[streamId], "enqueue should have stream controller");362 if (this.streamControllers[streamId].isClosed) {363 break;364 }365 this.streamControllers[streamId].controller.enqueue(data.chunk);366 break;367 case StreamKind.CLOSE:368 (0, _util.assert)(this.streamControllers[streamId], "close should have stream controller");369 if (this.streamControllers[streamId].isClosed) {370 break;371 }372 this.streamControllers[streamId].isClosed = true;373 this.streamControllers[streamId].controller.close();374 this._deleteStreamController(streamId);375 break;376 case StreamKind.ERROR:377 (0, _util.assert)(this.streamControllers[streamId], "error should have stream controller");378 this.streamControllers[streamId].controller.error(wrapReason(data.reason));379 this._deleteStreamController(streamId);380 break;381 case StreamKind.CANCEL_COMPLETE:382 if (data.success) {383 this.streamControllers[streamId].cancelCall.resolve();384 } else {385 this.streamControllers[streamId].cancelCall.reject(wrapReason(data.reason));386 }387 this._deleteStreamController(streamId);388 break;389 case StreamKind.CANCEL:390 if (!this.streamSinks[streamId]) {391 break;392 }393 const {394 onCancel395 } = this.streamSinks[data.streamId];396 new Promise(function (resolve) {397 resolve(onCancel && onCancel(wrapReason(data.reason)));398 }).then(function () {399 comObj.postMessage({400 sourceName,401 targetName,402 stream: StreamKind.CANCEL_COMPLETE,403 streamId,404 success: true405 });406 }, function (reason) {407 comObj.postMessage({408 sourceName,409 targetName,410 stream: StreamKind.CANCEL_COMPLETE,411 streamId,412 reason: wrapReason(reason)413 });414 });415 this.streamSinks[streamId].sinkCapability.reject(wrapReason(data.reason));416 this.streamSinks[streamId].isCancelled = true;417 delete this.streamSinks[streamId];418 break;419 default:420 throw new Error("Unexpected stream case");421 }422 }423 async _deleteStreamController(streamId) {424 await Promise.allSettled([this.streamControllers[streamId].startCall, this.streamControllers[streamId].pullCall, this.streamControllers[streamId].cancelCall].map(function (capability) {425 return capability && capability.promise;426 }));427 delete this.streamControllers[streamId];428 }429 _postMessage(message, transfers) {...
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var wrapReason = wptoolkit.wrapReason;3var reason = "some reason";4var wrappedReason = wrapReason(reason);5console.log(wrappedReason);6var wptoolkit = require('wptoolkit');7var wrapReason = wptoolkit.wrapReason;8var reason = "some reason";9var wrappedReason = wrapReason(reason);10console.log(wrappedReason);11var wptoolkit = require('wptoolkit');12var wrapReason = wptoolkit.wrapReason;13var reason = "some reason";14var wrappedReason = wrapReason(reason);15console.log(wrappedReason);16var wptoolkit = require('wptoolkit');17var wrapReason = wptoolkit.wrapReason;18var reason = "some reason";19var wrappedReason = wrapReason(reason);20console.log(wrappedReason);21var wptoolkit = require('wptoolkit');22var wrapReason = wptoolkit.wrapReason;23var reason = "some reason";24var wrappedReason = wrapReason(reason);25console.log(wrappedReason);26var wptoolkit = require('wptoolkit');27var wrapReason = wptoolkit.wrapReason;28var reason = "some reason";29var wrappedReason = wrapReason(reason);30console.log(wrappedReason);31var wptoolkit = require('wptoolkit');32var wrapReason = wptoolkit.wrapReason;33var reason = "some reason";34var wrappedReason = wrapReason(reason);35console.log(wrappedReason);
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.wrapReason(url, function(err, data) {3 if (!err) {4 console.log(data);5 }6});7### wpt.wrapReason(url, callback)8### wpt.wrapResult(url, callback)9### wpt.wrapLocations(callback)10### wpt.wrapTest(url, callback)
Using AI Code Generation
1const wpt = require('wpt');2const wptHook = new wpt.Hook();3const wpt = require('wpt');4const wptHook = new wpt.Hook();5wptHook.wrapReason('myReason', (reason, ...args) => {6 return reason;7});8wptHook.wrapReason('myReason', (reason, ...args) => {9 return reason;10});11const wpt = require('wpt');12const wptHook = new wpt.Hook();13wptHook.wrapReason('myReason', (reason, ...args) => {14 return reason;15});16const wpt = require('wpt');17const wptHook = new wpt.Hook();18wptHook.wrapReason('myReason', (reason, ...args) => {19 return reason;20});21wptHook.wrapReason('myReason', (reason, ...args) => {22 return reason;23});24Feel free to dive in! [Open an issue](
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.wrapReason('Test reason', function (err, result) {3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Result: ' + result);7 }8});
Using AI Code Generation
1var wptools = require('wptools').wptools;2var wiki = new wptools('Albert Einstein');3wiki.get(function(err, page) {4 console.log(page.wrapReason("Albert Einstein was born in Ulm, Germany"));5 console.log(page.wrapReason("He was born on March 14, 1879"));6 console.log(page.wrapReason("He died at Princeton Hospital"));7});8var wptools = require('wptools').wptools;9var wiki = new wptools('Albert Einstein');10wiki.get(function(err, page) {11 console.log(page.wrapReason("Albert Einstein was born in Ulm, Germany"));12 console.log(page.wrapReason("He was born on March 14, 1879"));13 console.log(page.wrapReason("He died at Princeton Hospital"));14});15var wptools = require('wptools').wptools;16var wiki = new wptools('Albert Einstein');17wiki.get(function(err, page) {18 console.log(page.wrapReason("Albert Einstein was born in Ulm, Germany"));19 console.log(page.wrapReason("He was born on March 14, 1879"));20 console.log(page.wrapReason("He died at Princeton Hospital"));21});22var wptools = require('wptools').wptools;23var wiki = new wptools('Albert Einstein');24wiki.get(function(err, page) {25 console.log(page.wrapReason("Albert Einstein was born in Ulm, Germany"));26 console.log(page.wrapReason("He was born on March 14, 1879"));27 console.log(page.wrapReason("He died at Princeton Hospital"));28});
Using AI Code Generation
1var fs = require('fs');2var wptools = require('wptools');3var test = new wptools.page('test');4test.get(function(err, resp) {5 if (err) {6 console.log(err);7 } else {8 var html = test.wrapReason();9 fs.writeFile('test.html', html, function(err) {10 if (err) {11 console.log(err);12 } else {13 console.log('File saved');14 }15 });16 }17});
Using AI Code Generation
1var wpt=require('webpagetest');2var options = {3};4var wpt = new WebPageTest('www.webpagetest.org', options);5wpt.runTest(url, function(err, data) {6 if (err) return console.error(err);7 console.log('Test submitted to WebPagetest for %s', url);8 wpt.getTestResults(data.data.testId, function(err, data) {9 if (err) return console.error(err);10 console.log('Test completed for %s', url);11 console.log('First View Speed Index: %s', data.data.average.firstView.SpeedIndex);12 console.log('Repeat View Speed Index: %s', data.data.average.repeatView.SpeedIndex);13 });14});
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.wrapReason('Test reason', function (err, result) {3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Result: ' + result);7 }8});
Using AI Code Generation
1var wptools = require('wptools').wptools;2var wiki = new wptools('Albert Einstein');3wiki.get(function(err, page) {4 console.log(page.wrapReason("Albert Einstein was born in Ulm, Germany"));5 console.log(page.wrapReason("He was born on March 14, 1879"));6 console.log(page.wrapReason("He died at Princeton Hospital"));7});8var wptools = require('wptools').wptools;9var wiki = new wptools('Albert Einstein');10wiki.get(function(err, page) {11 console.log(page.wrapReason("Albert Einstein was born in Ulm, Germany"));12 console.log(page.wrapReason("He was born on March 14, 1879"));13 console.log(page.wrapReason("He died at Princeton Hospital"));14});15var wptools = require('wptools').wptools;16var wiki = new wptools('Albert Einstein');17wiki.get(function(err, page) {18 console.log(page.wrapReason("Albert Einstein was born in Ulm, Germany"));19 console.log(page.wrapReason("He was born on March 14, 1879"));20 console.log(page.wrapReason("He died at Princeton Hospital"));21});22var wptools = require('wptools').wptools;23var wiki = new wptools('Albert Einstein');24wiki.get(function(err, page) {25 console.log(page.wrapReason("Albert Einstein was born in Ulm, Germany"));26 console.log(page.wrapReason("He was born on March 14, 1879"));27 console.log(page.wrapReason("He died at Princeton Hospital"));28});
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!!