Best JavaScript code snippet using wpt
range-sw.js
Source:range-sw.js
...31 return;32 case 'record-media-range-request':33 return recordMediaRangeRequest(event);34 case 'use-media-range-request':35 useMediaRangeRequest(event);36 return;37 }38});39/**40 * @param {Request} request41 */42function rangeHeaderFilterTest(request) {43 const rangeValue = request.headers.get('Range');44 test(() => {45 assert_range_request(new Request(request), rangeValue, `Untampered`);46 assert_range_request(new Request(request, {}), rangeValue, `Untampered (no init props set)`);47 assert_range_request(new Request(request, { __foo: 'bar' }), rangeValue, `Untampered (only invalid props set)`);48 assert_range_request(new Request(request, { mode: 'cors' }), rangeValue, `More permissive mode`);49 assert_range_request(request.clone(), rangeValue, `Clone`);50 }, "Range headers correctly preserved");51 test(() => {52 assert_range_request(new Request(request, { headers: { Range: 'foo' } }), null, `Tampered - range header set`);53 assert_range_request(new Request(request, { headers: {} }), null, `Tampered - empty headers set`);54 assert_range_request(new Request(request, { mode: 'no-cors' }), null, `Tampered â mode set`);55 assert_range_request(new Request(request, { cache: 'no-cache' }), null, `Tampered â cache mode set`);56 }, "Range headers correctly removed");57 test(() => {58 let headers;59 headers = new Request(request).headers;60 headers.delete('does-not-exist');61 assert_equals(headers.get('Range'), rangeValue, `Preserved if no header actually removed`);62 headers = new Request(request).headers;63 headers.append('foo', 'bar');64 assert_equals(headers.get('Range'), rangeValue, `Preserved if silent-failure on append (due to request-no-cors guard)`);65 headers = new Request(request).headers;66 headers.set('foo', 'bar');67 assert_equals(headers.get('Range'), rangeValue, `Preserved if silent-failure on set (due to request-no-cors guard)`);68 headers = new Request(request).headers;69 headers.append('Range', 'foo');70 assert_equals(headers.get('Range'), rangeValue, `Preserved if silent-failure on append (due to request-no-cors guard)`);71 headers = new Request(request).headers;72 headers.set('Range', 'foo');73 assert_equals(headers.get('Range'), rangeValue, `Preserved if silent-failure on set (due to request-no-cors guard)`);74 headers = new Request(request).headers;75 headers.append('Accept', 'whatever');76 assert_equals(headers.get('Range'), null, `Stripped if header successfully appended`);77 headers = new Request(request).headers;78 headers.set('Accept', 'whatever');79 assert_equals(headers.get('Range'), null, `Stripped if header successfully set`);80 headers = new Request(request).headers;81 headers.delete('Accept');82 assert_equals(headers.get('Range'), null, `Stripped if header successfully deleted`);83 headers = new Request(request).headers;84 headers.delete('Range');85 assert_equals(headers.get('Range'), null, `Stripped if range header successfully deleted`);86 }, "Headers correctly filtered");87 done();88}89function rangeHeaderPassthroughTest(event) {90 /** @type Request */91 const request = event.request;92 const url = new URL(request.url);93 const key = url.searchParams.get('range-received-key');94 event.waitUntil(new Promise(resolve => {95 promise_test(async () => {96 await fetch(event.request);97 const response = await fetch('stash-take.py?key=' + key);98 assert_equals(await response.json(), 'range-header-received');99 resolve();100 }, `Include range header in network request`);101 done();102 }));103 // Just send back any response, it isn't important for the test.104 event.respondWith(new Response(''));105}106let storedRangeResponseP;107function storeRangedResponse(event) {108 /** @type Request */109 const request = event.request;110 const id = new URL(request.url).searchParams.get('id');111 storedRangeResponseP = fetch(event.request);112 broadcast({ id });113 // Just send back any response, it isn't important for the test.114 event.respondWith(new Response(''));115}116function useStoredRangeResponse(event) {117 event.respondWith(async function() {118 const response = await storedRangeResponseP;119 if (!response) throw Error("Expected stored range response");120 return response.clone();121 }());122}123function broadcastAcceptEncoding(event) {124 /** @type Request */125 const request = event.request;126 const id = new URL(request.url).searchParams.get('id');127 broadcast({128 id,129 acceptEncoding: request.headers.get('Accept-Encoding')130 });131 // Just send back any response, it isn't important for the test.132 event.respondWith(new Response(''));133}134let rangeResponse = {};135async function recordMediaRangeRequest(event) {136 /** @type Request */137 const request = event.request;138 const url = new URL(request.url);139 const urlParams = new URLSearchParams(url.search);140 const size = urlParams.get("size");141 const id = urlParams.get('id');142 const key = 'size' + size;143 if (key in rangeResponse) {144 // Don't re-fetch ranges we already have.145 const clonedResponse = rangeResponse[key].clone();146 event.respondWith(clonedResponse);147 } else if (event.request.headers.get("range") === "bytes=0-") {148 // Generate a bogus 206 response to trigger subsequent range requests149 // of the desired size.150 const length = urlParams.get("length") + 100;151 const body = "A".repeat(Number(size));152 event.respondWith(new Response(body, {status: 206, headers: {153 "Content-Type": "audio/mp4",154 "Content-Range": `bytes 0-1/${length}`155 }}));156 } else if (event.request.headers.get("range") === `bytes=${Number(size)}-`) {157 // Pass through actual range requests which will attempt to fetch up to the158 // length in the original response which is bigger than the actual resource159 // to make sure 206 and 416 responses are treated the same.160 rangeResponse[key] = await fetch(event.request);161 // Let the client know we have the range response for the given ID162 broadcast({id});163 } else {164 event.respondWith(Promise.reject(Error("Invalid Request")));165 }166}167function useMediaRangeRequest(event) {168 /** @type Request */169 const request = event.request;170 const url = new URL(request.url);171 const urlParams = new URLSearchParams(url.search);172 const size = urlParams.get("size");173 const key = 'size' + size;174 // Send a clone of the range response to preload.175 if (key in rangeResponse) {176 const clonedResponse = rangeResponse[key].clone();177 event.respondWith(clonedResponse);178 } else {179 event.respondWith(Promise.reject(Error("Invalid Request")));180 }181}
Using AI Code Generation
1const wpt = require('wpt-api');2const options = {3 videoParams: {4 }5};6const test = async () => {7 try {8 const response = await wpt.useMediaRangeRequest(options);9 console.log(response);10 } catch (error) {11 console.log(error);12 }13};14test();15const wpt = require('wpt-api');16const options = {17 videoParams: {18 }19};20const test = sync () => {21 try {22 const esponse= await .test(options);23 console.log(response);24 } catch (error) {25 console.log(error);26 }27};28test();29const wpt = require('wpt-api');30const options = {31 videoParams: {32 }33};34const test = async () => {35 try {36 const response = await wpt.test(options);37 const testId = response.data.testId;38 const results = await wpt.getTestResults(testId);39 console.log(results);40 } catch (error) {41 console.log(error);42 }43};44test();
Using AI Code Generation
1 }2};3const test = async () => {4 try {5 const response = await wpt.useMediaRangeRequest(options);6 console.log(response);7 } catch (error) {8 console.log(error);9 }10};11test();12const wpt = require('wpt-api');13const options = {14 videoParams: {15 }16};17const test = async () => {18 try {19 const response = (
Using AI Code Generation
1var wpt = require('./wpt-api.js');2var testId = '170421_6G_7f3e3c8c3f3b1e2b2a3a1c8f2f2d2c2b';3wpt.useMediaRangeRequest(testId, function(err, data) {4 ifaerr) {5 console.log(err);6 }7 else {8 console.log(data);9 }10});wait wpt.test(options);11 console.log(response);12 } catch (error) {13 console.log(error);14 }15};16test();17const wpt = require('wpt-api');18const options = {19 videoParams: {20 }21};22const test = async () => {23 try {24 const response = await wpt.test(options);25 const testId = response.data.testId;26 const results = await wpt.getTestResults(testId);27 console.log(results);28 } catch (error) {29 console.log(error);30 }31};32test();
Using AI Code Generation
1var wpt = require('./wpt-api.js');2 if (err) {3 console.log(err);4 } else {5 console.log(data);6 }7});
Using AI Code Generation
1var wpt = require('wpt-api');2var wptClient = new wpt('A.0e6e5f6c4d4d4e4a6c5f6f1f6c5d6e5e');3wptClient.useMediaRangeRequest(url, "mobile", function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10### useMediaRangeRequest(url, media, callback)11MIT © [Bhavesh Anand](
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!!