Best JavaScript code snippet using wpt
unarchiver.js
Source:unarchiver.js
1"use strict";2const DIST = false;3const EXT = DIST ? '.min.js' : '.js';4function loadScript(path, name) {5 return loadScriptFromPath(path + 'lib/' + name + EXT)6}7function loadScriptFromPath(url) {8 return new Promise((resolve, reject) => {9 if (typeof window === 'object') { // Window10 let script = document.createElement('script');11 script.type = 'text/javascript';12 script.src = url;13 script.onload = resolve;14 document.head.appendChild(script);15 } else if (typeof importScripts === 'function') { // Web Worker16 importScripts(url);17 resolve();18 } else {19 reject();20 }21 })22}23function currentScriptPath() {24 // NOTE: document.currentScript does not work in a Web Worker25 // So we have to parse a stack trace maually26 try {27 throw new Error('');28 } catch (e) {29 let stack = e.stack;30 let line = (stack.indexOf('@') !== -1)31 ? stack.split('@')[1].split('\n')[0] // Chrome and IE32 : stack.split('(')[1].split(')')[0]; // Firefox33 return line.substring(0, line.lastIndexOf('/')) + '/';34 }35}36// Used by libunrar.js to load libunrar.js.mem37let unrarMemoryFileLocation = null;38let g_on_loaded_cb = null;39let unrarReady = new Promise((resolve, reject) => {40 g_on_loaded_cb = resolve;41});42class Unarchiver {43 static async load(formats = null) {44 formats = formats || ['zip', 'rar', 'tar', 'gz', 'xz', 'bz2']45 return Promise.all(formats.map(this._loadFormat));46 }47 static async _loadFormat(format) {48 if (format in Unarchiver.loadedFormats) {49 // Already loaded or loading50 await Unarchiver.loadedFormats[format]51 return;52 }53 let path = currentScriptPath();54 return Unarchiver.loadedFormats[format] = new Promise(async (resolve, reject) => {55 switch (format) {56 case 'zip':57 await loadScript(path, 'jszip');58 break;59 case 'rar':60 unrarMemoryFileLocation = path + 'lib/libunrar.js.mem';61 await loadScript(path, 'libunrar');62 await unrarReady;63 break;64 case 'tar':65 await loadScript(path, 'libuntar');66 break;67 case 'gz':68 await loadScript(path, 'pako_inflate');69 break;70 case 'xz':71 await loadScript(path, 'xz');72 break;73 case 'bz2':74 await loadScript(path, 'bz2');75 break;76 default:77 throw new Error("Unknown archive format '" + format + "'.");78 }79 resolve()80 })81 }82 static open(file, password = null) {83 let _this = this;84 return new Promise((resolve, reject) => {85 let file_name = file.name;86 password = password || null;87 let reader = new FileReader();88 reader.onload = async () => {89 let array_buffer = reader.result;90 // Decompress91 if (_this._isGzip(array_buffer)) {92 await _this._loadFormat('gz');93 array_buffer = pako.inflate(array_buffer).buffer94 } else if (_this._isXZ(array_buffer)) {95 await _this._loadFormat('xz');96 array_buffer = toXZ(new Uint8Array(array_buffer), 0, 0, 0, 2 ** 28).buffer;97 } else if (_this._isBZ2(array_buffer)) {98 await _this._loadFormat('bz2');99 array_buffer = bz2.decompress(new Uint8Array(array_buffer)).buffer;100 }101 let handle = null;102 let entries = [];103 // Unarchive104 let archive_type = null;105 if (_this._isRarFile(array_buffer)) {106 archive_type = 'rar';107 await _this._loadFormat(archive_type);108 handle = _this._rarOpen(file_name, password, array_buffer);109 entries = _this._rarGetEntries(handle);110 } else if (_this._isZipFile(array_buffer)) {111 archive_type = 'zip';112 await _this._loadFormat(archive_type);113 handle = await _this._zipOpen(file_name, password, array_buffer);114 entries = _this._zipGetEntries(handle);115 } else if (_this._isTarFile(array_buffer)) {116 archive_type = 'tar';117 await _this._loadFormat(archive_type);118 handle = _this._tarOpen(file_name, password, array_buffer);119 entries = _this._tarGetEntries(handle);120 } else {121 throw new Error('The archive type is unknown');122 }123 // Sort the entries by name124 entries.sort((a, b) => {125 return a.name.localeCompare(b.name);126 });127 // Return the archive object128 resolve({129 file_name: file_name,130 archive_type: archive_type,131 array_buffer: array_buffer,132 entries: entries,133 handle: handle134 })135 }136 reader.readAsArrayBuffer(file);137 });138 }139 static close(archive) {140 archive.file_name = null;141 archive.archive_type = null;142 archive.array_buffer = null;143 archive.entries = null;144 archive.handle = null;145 }146 static _rarOpen(file_name, password, array_buffer) {147 return {148 file_name: file_name,149 array_buffer: array_buffer,150 password: password,151 rar_files: [{152 name: file_name,153 size: array_buffer.byteLength,154 type: '',155 content: new Uint8Array(array_buffer)156 }]157 };158 }159 static async _zipOpen(file_name, password, array_buffer) {160 return {161 file_name: file_name,162 array_buffer: array_buffer,163 password: password,164 zip: await JSZip.loadAsync(array_buffer)165 };166 }167 static _tarOpen(file_name, password, array_buffer) {168 return {169 file_name: file_name,170 array_buffer: array_buffer,171 password: password172 };173 }174 static _rarGetEntries(rar_handle) {175 return Object.entries(readRARFile(rar_handle.rar_files, rar_handle.password)).map(([_, item]) => {176 let name = item.name;177 let is_file = item.is_file;178 return {179 name: name,180 is_file: item.is_file,181 size_compressed: item.size_compressed,182 size_uncompressed: item.size_uncompressed,183 read: () => {184 return new Promise((resolve, reject) => {185 if (is_file) {186 try {187 readRARContent(rar_handle.rar_files, rar_handle.password, name, (c) => {188 resolve(new File([c], name))189 });190 } catch (e) {191 reject(e);192 }193 } else {194 resolve(null);195 }196 })197 }198 }199 })200 }201 static _zipGetEntries(zip_handle) {202 return Object.entries(zip_handle.zip.files).map(([_, item]) => {203 let name = item.name;204 let is_file = !item.dir;205 let size_compressed = item._data ? item._data.compressedSize : 0;206 let size_uncompressed = item._data ? item._data.uncompressedSize : 0;207 return {208 name: name,209 is_file: is_file,210 size_compressed: size_compressed,211 size_uncompressed: size_uncompressed,212 read: () => {213 return new Promise(async (resolve, reject) => {214 resolve(is_file ? new File([await item.async('blob')], name) : null)215 })216 }217 };218 });219 }220 static _tarGetEntries(tar_handle) {221 // Get all the entries222 return tarGetEntries(tar_handle.file_name, tar_handle.array_buffer).map((entry) => {223 let name = entry.name;224 let is_file = entry.is_file;225 let size = entry.size;226 return {227 name: name,228 is_file: is_file,229 size_compressed: size,230 size_uncompressed: size,231 read: () => {232 return new Promise((resolve, reject) => {233 if (is_file) {234 let data = tarGetEntryData(entry, tar_handle.array_buffer)235 resolve(new File([data.buffer], name));236 } else {237 resolve(null);238 }239 })240 }241 };242 });243 }244 static _isRarFile(array_buffer) {245 // Just return false if the file is smaller than the header246 if (array_buffer.byteLength < 8)247 return false;248 let header = new Uint8Array(array_buffer, 0, 8);249 // Return true if the header matches one of the styles of RAR headers250 return (header[0] == 0x52) // Always this first251 &&252 (253 (header[1] == 0x45 && header[2] == 0x7E && header[3] == 0x5E) ||254 (header[1] == 0x61 && header[2] == 0x72 && header[3] == 0x21 && header[4] == 0x1A && header[5] == 0x07 && ((header[6] == 0x00) || (header[6] == 0x01 && header[7] == 0x00)))255 );256 }257 static _isZipFile(array_buffer) {258 return this._checkHeader([0x50, 0x4b, 0x03, 0x04], array_buffer)259 }260 static _isTarFile(array_buffer) {261 return this._checkHeader([0x75, 0x73, 0x74, 0x61, 0x72], array_buffer, 257, 512)262 }263 static _isGzip(array_buffer) {264 return this._checkHeader([0x1F, 0x8B, 0x08], array_buffer)265 }266 static _isBZ2(array_buffer) {267 return this._checkHeader([0x42, 0x5A, 0x68], array_buffer)268 }269 static _isXZ(array_buffer) {270 return this._checkHeader([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00], array_buffer)271 }272 static _checkHeader(expectedHeader, array_buffer, offset = 0, minBytes = null) {273 let m = offset + expectedHeader.length;274 if (array_buffer.byteLength < (minBytes || m))275 return false;276 let header = new Uint8Array(array_buffer, offset, m);277 for (let i = 0; i < expectedHeader.length; ++i) {278 if (header[i] != expectedHeader[i])279 return false;280 }281 return true;282 }283}284// Set static class variables285// Stores a dictionary of promises, allowing for multiple blocking calls to load...
Mesh.js
Source:Mesh.js
1class Mesh {2 constructor(props, nTriangles) {3 this.vertices = props.vertices;4 this.indices = props.indices;5 this.texCoords = props.texCoords;6 this.normals = props.normals;7 this.tangents = props.tangents;8 this.nTriangles = nTriangles;9 this.vertexBuffer = gl.createBuffer();10 gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);11 gl.bufferData(gl.ARRAY_BUFFER, this.vertices, gl.STATIC_DRAW);12 gl.bindBuffer(gl.ARRAY_BUFFER, null);13 14 if(this.texCoords){15 this.texCoordsBuffer = gl.createBuffer();16 gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordsBuffer);17 gl.bufferData(gl.ARRAY_BUFFER, this.texCoords, gl.STATIC_DRAW);18 gl.bindBuffer(gl.ARRAY_BUFFER, null);19 }20 21 if(this.tangents){ 22 this.tangentsBuffer = gl.createBuffer();23 gl.bindBuffer(gl.ARRAY_BUFFER, this.tangentsBuffer);24 gl.bufferData(gl.ARRAY_BUFFER, this.tangents, gl.STATIC_DRAW);25 gl.bindBuffer(gl.ARRAY_BUFFER, null);26 }27 28 if(this.normals){29 this.normalBuffer = gl.createBuffer();30 gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);31 gl.bufferData(gl.ARRAY_BUFFER, this.normals, gl.STATIC_DRAW);32 gl.bindBuffer(gl.ARRAY_BUFFER, null);33 }34 35 this.indexBuffer = gl.createBuffer();36 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);37 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.indices, gl.STATIC_DRAW);38 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);39 }...
Using AI Code Generation
1var wpt = require('webpagetest');2var webPageTest = new wpt('www.webpagetest.org', 'A.12345678901234567890123456789012');3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 var testId = data.data.testId;8 webPageTest.getTestResults(testId, function(err, data) {9 if (err) {10 console.log(err);11 } else {12 console.log(data);13 }14 });15 }16});17{ statusCode: 200,18 { statusCode: 200,19 { testId: '140703_3E_1',
Using AI Code Generation
1var wpt = require('webpagetest');2var server = new wpt('www.webpagetest.org');3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Test Results: ' + data);7 }8});9var wpt = require('webpagetest');10var server = new wpt('www.webpagetest.org');11 if (err) {12 console.log('Error: ' + err);13 } else {14 console.log('Test Results: ' + data);15 }16});17var wpt = require('webpagetest');18var server = new wpt('www.webpagetest.org');19 if (err) {20 console.log('Error: ' + err);21 } else {22 console.log('Test Results: ' + data);23 }24});25var wpt = require('webpagetest');26var server = new wpt('www.webpagetest.org');27 if (err) {28 console.log('Error: ' + err);29 } else {30 console.log('Test Results: ' + data);31 }32});33var wpt = require('webpagetest');34var server = new wpt('www.webpagetest.org');
Using AI Code Generation
1function array_buffer() {2 var a = new ArrayBuffer(8);3 var b = new Int32Array(a);4 b[0] = 0xdeadbeef;5 b[1] = 0x12345678;6 var c = new Int32Array(a, 4);7 if (c[0] == 0x12345678) {8 return true;9 } else {10 return false;11 }12}13function array_buffer_slice() {14 var a = new ArrayBuffer(8);15 var b = new Int32Array(a);16 b[0] = 0xdeadbeef;17 b[1] = 0x12345678;18 var c = new Int32Array(a.slice(4));19 if (c[0] == 0x12345678) {20 return true;21 } else {22 return false;23 }24}25function data_view() {26 var a = new ArrayBuffer(8);27 var b = new DataView(a);28 b.setInt32(0, 0xdeadbeef);29 b.setInt32(4, 0x12345678);30 var c = new DataView(a, 4);31 if (c.getInt32(0) == 0x12345678) {32 return true;33 } else {34 return false;35 }36}37function data_view_slice() {38 var a = new ArrayBuffer(8);39 var b = new DataView(a);40 b.setInt32(0, 0xdeadbeef);41 b.setInt32(4, 0x12345678);42 var c = new DataView(a.slice(4));43 if (c.getInt32(0) == 0x12345678) {44 return true;45 } else {46 return false;47 }48}49function typed_array() {50 var a = new ArrayBuffer(8);51 var b = new Int32Array(a);52 b[0] = 0xdeadbeef;53 b[1] = 0x12345678;54 var c = new Int32Array(a, 4);55 if (c[0] == 0x
Using AI Code Generation
1var buffer = new ArrayBuffer(8);2var view = new Float64Array(buffer);3var buf = new ArrayBuffer(8);4var bufView = new Uint8Array(buf);5function ftoi(val) {6 view[0] = val;7 return BigInt('0x' + bufView[6].toString(16) + bufView[7].toString(16) + bufView[4].toString(16) + bufView[5].toString(16) + bufView[0].toString(16) + bufView[1].toString(16) + bufView[2].toString(16) + bufView[3].toString(16));8}9function itof(val) {10 var hex = val.toString(16);11 while (hex.length < 16) {12 hex = "0" + hex;13 }14 bufView[6] = parseInt(hex.substring(0, 2), 16);15 bufView[7] = parseInt(hex.substring(2, 4), 16);16 bufView[4] = parseInt(hex.substring(4, 6), 16);17 bufView[5] = parseInt(hex.substring(6, 8), 16);18 bufView[0] = parseInt(hex.substring(8, 10), 16);19 bufView[1] = parseInt(hex.substring(10, 12), 16);20 bufView[2] = parseInt(hex.substring(12, 14), 16);21 bufView[3] = parseInt(hex.substring(14, 16), 16);22 return view[0];23}24function hex(val) {25 return "0x" + val.toString(16);26}27function hex2(val) {28 return "0x" + val.toString(16).padStart(16, "0");29}30function hex3(val) {31 return "0x" + val.toString(16).padStart(16, "0");32}33var buffer = new ArrayBuffer(8);34var view = new Float64Array(buffer);35var buf = new ArrayBuffer(8);36var bufView = new Uint8Array(buf);37function ftoi(val) {38 view[0] = val;39 return BigInt('0x' + bufView[6].toString(16)
Using AI Code Generation
1function test(callback) {2 var array_buffer = new ArrayBuffer(4);3 var array = new Uint32Array(array_buffer);4 array[0] = 0x12345678;5 var array_buffer2 = new ArrayBuffer(4);6 var array2 = new Uint32Array(array_buffer2);7 array2[0] = 0x12345678;8 if (array_buffer === array_buffer2)9 callback(false);10 callback(true);11}12 test(function(result) {13 if (result)14 console.log("PASS");15 console.log("FAIL");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!!