Best JavaScript code snippet using wpt
ReactServerStreamConfigNode.js
Source:ReactServerStreamConfigNode.js  
1/**2 * Copyright (c) Facebook, Inc. and its affiliates.3 *4 * This source code is licensed under the MIT license found in the5 * LICENSE file in the root directory of this source tree.6 *7 * @flow8 */9import type {Writable} from 'stream';10import {TextEncoder} from 'util';11type MightBeFlushable = {12  flush?: () => void,13  ...14};15export type Destination = Writable & MightBeFlushable;16export type PrecomputedChunk = Uint8Array;17export type Chunk = string;18export function scheduleWork(callback: () => void) {19  setImmediate(callback);20}21export function flushBuffered(destination: Destination) {22  // If we don't have any more data to send right now.23  // Flush whatever is in the buffer to the wire.24  if (typeof destination.flush === 'function') {25    // By convention the Zlib streams provide a flush function for this purpose.26    // For Express, compression middleware adds this method.27    destination.flush();28  }29}30const VIEW_SIZE = 2048;31let currentView = null;32let writtenBytes = 0;33let destinationHasCapacity = true;34export function beginWriting(destination: Destination) {35  currentView = new Uint8Array(VIEW_SIZE);36  writtenBytes = 0;37  destinationHasCapacity = true;38}39function writeStringChunk(destination: Destination, stringChunk: string) {40  if (stringChunk.length === 0) {41    return;42  }43  // maximum possible view needed to encode entire string44  if (stringChunk.length * 3 > VIEW_SIZE) {45    if (writtenBytes > 0) {46      writeToDestination(47        destination,48        ((currentView: any): Uint8Array).subarray(0, writtenBytes),49      );50      currentView = new Uint8Array(VIEW_SIZE);51      writtenBytes = 0;52    }53    writeToDestination(destination, textEncoder.encode(stringChunk));54    return;55  }56  let target: Uint8Array = (currentView: any);57  if (writtenBytes > 0) {58    target = ((currentView: any): Uint8Array).subarray(writtenBytes);59  }60  const {read, written} = textEncoder.encodeInto(stringChunk, target);61  writtenBytes += written;62  if (read < stringChunk.length) {63    writeToDestination(destination, (currentView: any));64    currentView = new Uint8Array(VIEW_SIZE);65    writtenBytes = textEncoder.encodeInto(stringChunk.slice(read), currentView)66      .written;67  }68  if (writtenBytes === VIEW_SIZE) {69    writeToDestination(destination, (currentView: any));70    currentView = new Uint8Array(VIEW_SIZE);71    writtenBytes = 0;72  }73}74function writeViewChunk(destination: Destination, chunk: PrecomputedChunk) {75  if (chunk.byteLength === 0) {76    return;77  }78  if (chunk.byteLength > VIEW_SIZE) {79    // this chunk may overflow a single view which implies it was not80    // one that is cached by the streaming renderer. We will enqueu81    // it directly and expect it is not re-used82    if (writtenBytes > 0) {83      writeToDestination(84        destination,85        ((currentView: any): Uint8Array).subarray(0, writtenBytes),86      );87      currentView = new Uint8Array(VIEW_SIZE);88      writtenBytes = 0;89    }90    writeToDestination(destination, chunk);91    return;92  }93  let bytesToWrite = chunk;94  const allowableBytes = ((currentView: any): Uint8Array).length - writtenBytes;95  if (allowableBytes < bytesToWrite.byteLength) {96    // this chunk would overflow the current view. We enqueue a full view97    // and start a new view with the remaining chunk98    if (allowableBytes === 0) {99      // the current view is already full, send it100      writeToDestination(destination, (currentView: any));101    } else {102      // fill up the current view and apply the remaining chunk bytes103      // to a new view.104      ((currentView: any): Uint8Array).set(105        bytesToWrite.subarray(0, allowableBytes),106        writtenBytes,107      );108      writtenBytes += allowableBytes;109      writeToDestination(destination, (currentView: any));110      bytesToWrite = bytesToWrite.subarray(allowableBytes);111    }112    currentView = new Uint8Array(VIEW_SIZE);113    writtenBytes = 0;114  }115  ((currentView: any): Uint8Array).set(bytesToWrite, writtenBytes);116  writtenBytes += bytesToWrite.byteLength;117  if (writtenBytes === VIEW_SIZE) {118    writeToDestination(destination, (currentView: any));119    currentView = new Uint8Array(VIEW_SIZE);120    writtenBytes = 0;121  }122}123export function writeChunk(124  destination: Destination,125  chunk: PrecomputedChunk | Chunk,126): void {127  if (typeof chunk === 'string') {128    writeStringChunk(destination, chunk);129  } else {130    writeViewChunk(destination, ((chunk: any): PrecomputedChunk));131  }132}133function writeToDestination(destination: Destination, view: Uint8Array) {134  const currentHasCapacity = destination.write(view);135  destinationHasCapacity = destinationHasCapacity && currentHasCapacity;136}137export function writeChunkAndReturn(138  destination: Destination,139  chunk: PrecomputedChunk | Chunk,140): boolean {141  writeChunk(destination, chunk);142  return destinationHasCapacity;143}144export function completeWriting(destination: Destination) {145  if (currentView && writtenBytes > 0) {146    destination.write(currentView.subarray(0, writtenBytes));147  }148  currentView = null;149  writtenBytes = 0;150  destinationHasCapacity = true;151}152export function close(destination: Destination) {153  destination.end();154}155const textEncoder = new TextEncoder();156export function stringToChunk(content: string): Chunk {157  return content;158}159export function stringToPrecomputedChunk(content: string): PrecomputedChunk {160  return textEncoder.encode(content);161}162export function closeWithError(destination: Destination, error: mixed): void {163  // $FlowFixMe: This is an Error object or the destination accepts other types.164  destination.destroy(error);...ReactServerStreamConfigBrowser.js
Source:ReactServerStreamConfigBrowser.js  
1/**2 * Copyright (c) Facebook, Inc. and its affiliates.3 *4 * This source code is licensed under the MIT license found in the5 * LICENSE file in the root directory of this source tree.6 *7 * @flow8 */9export type Destination = ReadableStreamController;10export type PrecomputedChunk = Uint8Array;11export type Chunk = Uint8Array;12export function scheduleWork(callback: () => void) {13  callback();14}15export function flushBuffered(destination: Destination) {16  // WHATWG Streams do not yet have a way to flush the underlying17  // transform streams. https://github.com/whatwg/streams/issues/96018}19const VIEW_SIZE = 512;20let currentView = null;21let writtenBytes = 0;22export function beginWriting(destination: Destination) {23  currentView = new Uint8Array(VIEW_SIZE);24  writtenBytes = 0;25}26export function writeChunk(27  destination: Destination,28  chunk: PrecomputedChunk | Chunk,29): void {30  if (chunk.length === 0) {31    return;32  }33  if (chunk.length > VIEW_SIZE) {34    // this chunk may overflow a single view which implies it was not35    // one that is cached by the streaming renderer. We will enqueu36    // it directly and expect it is not re-used37    if (writtenBytes > 0) {38      destination.enqueue(39        new Uint8Array(40          ((currentView: any): Uint8Array).buffer,41          0,42          writtenBytes,43        ),44      );45      currentView = new Uint8Array(VIEW_SIZE);46      writtenBytes = 0;47    }48    destination.enqueue(chunk);49    return;50  }51  let bytesToWrite = chunk;52  const allowableBytes = ((currentView: any): Uint8Array).length - writtenBytes;53  if (allowableBytes < bytesToWrite.length) {54    // this chunk would overflow the current view. We enqueue a full view55    // and start a new view with the remaining chunk56    if (allowableBytes === 0) {57      // the current view is already full, send it58      destination.enqueue(currentView);59    } else {60      // fill up the current view and apply the remaining chunk bytes61      // to a new view.62      ((currentView: any): Uint8Array).set(63        bytesToWrite.subarray(0, allowableBytes),64        writtenBytes,65      );66      // writtenBytes += allowableBytes; // this can be skipped because we are going to immediately reset the view67      destination.enqueue(currentView);68      bytesToWrite = bytesToWrite.subarray(allowableBytes);69    }70    currentView = new Uint8Array(VIEW_SIZE);71    writtenBytes = 0;72  }73  ((currentView: any): Uint8Array).set(bytesToWrite, writtenBytes);74  writtenBytes += bytesToWrite.length;75}76export function writeChunkAndReturn(77  destination: Destination,78  chunk: PrecomputedChunk | Chunk,79): boolean {80  writeChunk(destination, chunk);81  // in web streams there is no backpressure so we can alwas write more82  return true;83}84export function completeWriting(destination: Destination) {85  if (currentView && writtenBytes > 0) {86    destination.enqueue(new Uint8Array(currentView.buffer, 0, writtenBytes));87    currentView = null;88    writtenBytes = 0;89  }90}91export function close(destination: Destination) {92  destination.close();93}94const textEncoder = new TextEncoder();95export function stringToChunk(content: string): Chunk {96  return textEncoder.encode(content);97}98export function stringToPrecomputedChunk(content: string): PrecomputedChunk {99  return textEncoder.encode(content);100}101export function closeWithError(destination: Destination, error: mixed): void {102  if (typeof destination.error === 'function') {103    // $FlowFixMe: This is an Error object or the destination accepts other types.104    destination.error(error);105  } else {106    // Earlier implementations doesn't support this method. In that environment you're107    // supposed to throw from a promise returned but we don't return a promise in our108    // approach. We could fork this implementation but this is environment is an edge109    // case to begin with. It's even less common to run this in an older environment.110    // Even then, this is not where errors are supposed to happen and they get reported111    // to a global callback in addition to this anyway. So it's fine just to close this.112    destination.close();113  }...Using AI Code Generation
1var wpt = require('webpagetest');2var test = wpt('www.webpagetest.org');3  if (err) return console.error(err);4  console.log(data);5  test.getTestStatus(data.data.testId, function(err, data) {6    if (err) return console.error(err);7    console.log(data);8  });9});Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var fs = require('fs');4var options = {5};6wpt.runTest(url, options, function(err, data) {7  if (err) return console.error(err);8  var testId = data.data.testId;9  console.log('Test ID: ' + testId);10  wpt.getTestResults(testId, function(err, data) {11    if (err) return console.error(err);12    console.log(data);13    var testId = data.data.testId;14    var testUrl = data.data.summary;15    var testResults = data.data.median.firstView;16    var testResultsString = JSON.stringify(testResults);17    var testResultsObject = JSON.parse(testResultsString);18    console.log(testResultsObject);19    var testResultsBytes = testResultsObject.bytesIn;20    console.log(testResultsBytes);21    fs.writeFile('test.txt', testResultsBytes, function(err) {22      if (err) {23        return console.log(err);24      }25      console.log('The file was saved!');26    });27  });28});Using AI Code Generation
1var fs = require('fs');2var wpt = require('webpagetest');3var client = wpt('www.webpagetest.org');4var url = 'www.google.com';5var options = {6};7client.runTest(url, options, function(err, data) {8  if (err) return console.error(err);9  console.log(data);10  client.getTestResults(data.data.testId, function(err, data) {11    if (err) return console.error(err);12    console.log(data);13    client.getTestStatus(data.data.testId, function(err, data) {14      if (err) return console.error(err);15      console.log(data);16      client.getTestResults(data.data.testId, function(err, data) {17        if (err) return console.error(err);18        console.log(data);19        client.getTestResults(data.data.testId, function(err, data) {20          if (err) return console.error(err);21          console.log(data);22        });23      });24    });25  });26});Using AI Code Generation
1var wptStream = require('wpt-stream');2var stream = new wptStream();3var data = 'some data';4stream.write(data);5console.log(stream.writtenBytes);6#### wptStream.write(data)7#### wptStream.on('data', function(data) {})8#### wptStream.on('end', function() {})9#### wptStream.on('error', function(error) {})10#### wptStream.on('close', function() {})11#### wptStream.pause()12#### wptStream.resume()13#### wptStream.destroy()14#### wptStream.destroySoon()Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var path = require('path');4wptools.page('wikipedia').get(function(err, response) {5    if (err) {6        console.log(err);7        return;8    }9    var url = response.image();10    wptools.bytes(url, function(err, response) {11        if (err) {12            console.log(err);13            return;14        }15        fs.writeFile(path.join(__dirname, 'test.jpg'), response, function(err) {16            if (err) {17                console.log(err);18                return;19            }20            console.log('file written');21        });22    });23});24### wptools.page()25    * `imageInfo()` - Returns an object with the following properties:Using AI Code Generation
1var wpt = require('wpt');2var wpt = new wpt();3wpt.writtenBytes(100, function(error, writtenBytes){4    if(error){5        console.log(error);6    }7    else{8        console.log(writtenBytes);9    }10});11wpt.writtenBytes(1000, function(error, writtenBytes){12    if(error){13        console.log(error);14    }15    else{16        console.log(writtenBytes);17    }18});19wpt.writtenBytes(10000, function(error, writtenBytes){20    if(error){21        console.log(error);22    }23    else{24        console.log(writtenBytes);25    }26});27wpt.writtenBytes(100000, function(error, writtenBytes){28    if(error){29        console.log(error);30    }31    else{32        console.log(writtenBytes);33    }34});35wpt.writtenBytes(1000000, function(error, writtenBytes){36    if(error){37        console.log(error);38    }39    else{40        console.log(writtenBytes);41    }42});43wpt.writtenBytes(10000000, function(error, writtenBytes){44    if(error){45        console.log(error);46    }47    else{48        console.log(writtenBytes);49    }50});51wpt.writtenBytes(100000000, function(error, writtenBytes){52    if(error){53        console.log(error);54    }55    else{56        console.log(writtenBytes);57    }58});59wpt.writtenBytes(1000000000, function(error, writtenBytes){60    if(error){61        console.log(errorUsing AI Code Generation
1var fs = require('fs');2var wpt = require('webpagetest');3var options = {4};5var webpagetest = new wpt('www.webpagetest.org', options.key);6var runTest = function (url) {7  webpagetest.runTest(url, options, function (err, data) {8    if (err) {9      console.log(err);10    } else {11      console.log('Test submitted');12      console.log('Test ID: ' + data.data.testId);13      setTimeout(function () {14        checkTestStatus(data.data.testId);15      }, 10000);16    }17  });18};19var checkTestStatus = function (testId) {20  webpagetest.getTestStatus(testId, function (err, data) {21    if (err) {22      console.log(err);23    } else {24      console.log('Test Status: ' + data.data.statusText);25      if (data.data.statusText === 'Test Complete') {26        console.log('Test Results: ' + data.data.summary);27        console.log('Test ID: ' + data.data.testId);28        console.log('Test URL: ' + data.data.userUrl);29        console.log('Test Location: ' + data.data.location);30        console.log('Test First View: ' + data.data.runs[1].firstView);31        console.log('Test Repeat View: ' + data.data.runs[1].repeatView);32        console.log('Test First View: ' + data.data.runs[1].firstView);33        console.log('Test Repeat View: ' + data.data.runs[1].repeatView);34        console.log('Test PNG Screenshot: ' + data.data.runs[1].firstView.images.png);35        console.log('Test JPEG Screenshot: ' + data.data.runs[1].firstView.images.jpeg);36        console.log('Test Video: ' + data.data.runs[1].firstView.video);Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var test = wpt(options);5test.runTest(url, function(err, data) {6  if (err) return console.error(err);7  console.log('Test submitted. Polling for results.');8  test.waitForTestComplete(data.data.testId, function(err, data) {9    if (err) return console.error(err);10    console.log('Test completed. View results at: ' + data.data.summary);11    test.getTestResults(data.data.testId, function(err, data) {12      if (err) return console.error(err);13      console.log(data.data.runs[1].firstView.writtenBytes);14    });15  });16});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!!
