Best JavaScript code snippet using wpt
content_disposition.js
Source:content_disposition.js
...11 let filename = rfc2616unquote(tmp);12 filename = unescape(filename);13 filename = rfc5987decode(filename);14 filename = rfc2047decode(filename);15 return fixupEncoding(filename);16 }17 tmp = rfc2231getparam(contentDisposition);18 if (tmp) {19 let filename = rfc2047decode(tmp);20 return fixupEncoding(filename);21 }22 tmp = toParamRegExp('filename', 'i').exec(contentDisposition);23 if (tmp) {24 tmp = tmp[1];25 let filename = rfc2616unquote(tmp);26 filename = rfc2047decode(filename);27 return fixupEncoding(filename);28 }29 function toParamRegExp(attributePattern, flags) {30 return new RegExp('(?:^|;)\\s*' + attributePattern + '\\s*=\\s*' + '(' + '[^";\\s][^;\\s]*' + '|' + '"(?:[^"\\\\]|\\\\"?)+"?' + ')', flags);31 }32 function textdecode(encoding, value) {33 if (encoding) {34 if (!/^[\x00-\xFF]+$/.test(value)) {35 return value;36 }37 try {38 let decoder = new TextDecoder(encoding, {39 fatal: true40 });41 let bytes = Array.from(value, function (ch) {42 return ch.charCodeAt(0) & 0xFF;43 });44 value = decoder.decode(new Uint8Array(bytes));45 needsEncodingFixup = false;46 } catch (e) {47 if (/^utf-?8$/i.test(encoding)) {48 try {49 value = decodeURIComponent(escape(value));50 needsEncodingFixup = false;51 } catch (err) {}52 }53 }54 }55 return value;56 }57 function fixupEncoding(value) {58 if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {59 value = textdecode('utf-8', value);60 if (needsEncodingFixup) {61 value = textdecode('iso-8859-1', value);62 }63 }64 return value;65 }66 function rfc2231getparam(contentDisposition) {67 let matches = [],68 match;69 let iter = toParamRegExp('filename\\*((?!0\\d)\\d+)(\\*?)', 'ig');70 while ((match = iter.exec(contentDisposition)) !== null) {71 let [, n, quot, part] = match;...
content_disposition.ts
Source:content_disposition.ts
...9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.10 */11import {toParamRegExp, unquote} from "./headers.ts";12let needsEncodingFixup = false;13function fixupEncoding(value: string): string {14 if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {15 value = textDecode("utf-8", value);16 if (needsEncodingFixup) {17 value = textDecode("iso-8859-1", value);18 }19 }20 return value;21}22const FILENAME_STAR_REGEX = toParamRegExp("filename\\*", "i");23const FILENAME_START_ITER_REGEX = toParamRegExp(24 "filename\\*((?!0\\d)\\d+)(\\*?)",25 "ig"26);27const FILENAME_REGEX = toParamRegExp("filename", "i");28function rfc2047decode(value: string): string {29 // deno-lint-ignore no-control-regex30 if (!value.startsWith("=?") || /[\x00-\x19\x80-\xff]/.test(value)) {31 return value;32 }33 return value.replace(34 /=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g,35 (_: string, charset: string, encoding: string, text: string) => {36 if (encoding === "q" || encoding === "Q") {37 text = text.replace(/_/g, " ");38 text = text.replace(/=([0-9a-fA-F]{2})/g, (_, hex) =>39 String.fromCharCode(parseInt(hex, 16))40 );41 return textDecode(charset, text);42 }43 try {44 text = atob(text);45 // deno-lint-ignore no-empty46 } catch {}47 return textDecode(charset, text);48 }49 );50}51function rfc2231getParam(header: string): string {52 const matches: [string, string][] = [];53 let match: RegExpExecArray | null;54 while ((match = FILENAME_START_ITER_REGEX.exec(header))) {55 const [, ns, quote, part] = match;56 const n = parseInt(ns, 10);57 if (n in matches) {58 if (n === 0) {59 break;60 }61 continue;62 }63 matches[n] = [quote, part];64 }65 const parts: string[] = [];66 for (let n = 0; n < matches.length; ++n) {67 if (!(n in matches)) {68 break;69 }70 let [quote, part] = matches[n];71 part = unquote(part);72 if (quote) {73 part = unescape(part);74 if (n === 0) {75 part = rfc5987decode(part);76 }77 }78 parts.push(part);79 }80 return parts.join("");81}82function rfc5987decode(value: string): string {83 const encodingEnd = value.indexOf(`'`);84 if (encodingEnd === -1) {85 return value;86 }87 const encoding = value.slice(0, encodingEnd);88 const langValue = value.slice(encodingEnd + 1);89 return textDecode(encoding, langValue.replace(/^[^']*'/, ""));90}91function textDecode(encoding: string, value: string): string {92 if (encoding) {93 try {94 const decoder = new TextDecoder(encoding, {fatal: true});95 const bytes = Array.from(value, (c) => c.charCodeAt(0));96 if (bytes.every((code) => code <= 0xff)) {97 value = decoder.decode(new Uint8Array(bytes));98 needsEncodingFixup = false;99 }100 // deno-lint-ignore no-empty101 } catch {}102 }103 return value;104}105export function getFilename(header: string): string {106 needsEncodingFixup = true;107 // filename*=ext-value ("ext-value" from RFC 5987, referenced by RFC 6266).108 let matches = FILENAME_STAR_REGEX.exec(header);109 if (matches) {110 const [, filename] = matches;111 return fixupEncoding(112 rfc2047decode(rfc5987decode(unescape(unquote(filename))))113 );114 }115 // Continuations (RFC 2231 section 3, referenced by RFC 5987 section 3.1).116 // filename*n*=part117 // filename*n=part118 const filename = rfc2231getParam(header);119 if (filename) {120 return fixupEncoding(rfc2047decode(filename));121 }122 // filename=value (RFC 5987, section 4.1).123 matches = FILENAME_REGEX.exec(header);124 if (matches) {125 const [, filename] = matches;126 return fixupEncoding(rfc2047decode(unquote(filename)));127 }128 return "";...
Using AI Code Generation
1var decoder = new TextDecoder("utf-8", {fatal: true});2var result = decoder.decode(new Uint8Array([0xC3, 0x28]), {stream: true});3decoder.decode(new Uint8Array([0xA9]), {stream: true});4decoder.decode(new Uint8Array([0x29]), {stream: true});5Exception thrown: TypeError: undefined is not an object (evaluating 'this._decoder.fixupEncoding')6 > [Constructor(DOMString label, optional TextDecoderOptions options),7> Exposed=(Window,Worker)]8> interface TextDecoder {9> readonly attribute DOMString encoding;10> + readonly attribute boolean fatal; 11 > [Constructor(DOMString label, optional TextDecoderOptions options),12> Exposed=(Window,Worker)]13> interface TextDecoder {14> readonly attribute DOMString encoding;15> + readonly attribute boolean fatal;
Using AI Code Generation
1var wpt = require('webpagetest');2var wptObj = new wpt('www.webpagetest.org');3wptObj.fixupEncoding('This is a test', function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10{ Error: ENOENT, open 'C:\Users\kelly\AppData\Local\Temp\wpt-1303-0'11 at Object.fs.openSync (fs.js:439:18)12 at Object.fs.readFileSync (fs.js:290:15)13 at Object.fs.readFileSync (C:\Users\kelly\AppData\Roaming\npm\node_modules\webpagetest\node_modules\request\node_modules\graceful-fs\polyfills.js:207:23)14 at Object.wpt.fixupEncoding (C:\Users\kelly\AppData\Roaming\npm\node_modules\webpagetest\lib\main.js:104:26)15 at Object. (C:\Users\kelly\Documents\GitHub\wpt-test\test.js:8:3)16 at Module._compile (module.js:456:26)17 at Object.Module._extensions..js (module.js:474:10)18 at Module.load (module.js:356:32)19 at Function.Module._load (module.js:312:12) errno: -4058, code: 'ENOENT', path: 'C:\Users\kelly\AppData\Local\Temp\wpt-1303-0' }
Using AI Code Generation
1var wptools = require('wptools');2var wiki = wptools.page('Beethoven');3wiki.get(function(err, resp) {4 if (err) {5 console.log(err);6 } else {7 console.log(resp);8 }9});10{11 "infobox": {12 "death_date": "March 26, 1827 (aged 56)",13 "spouse": "Marie von Breuning (m. 1792\u20131798; div. 1798), Antonie Brentano (m. 1801; div. 1802), Therese Malfatti (m. 1802; div. 1809), Josepha Brunsvik (m. 1812; div. 1815), Constanze Weber (m. 1815; div. 1826), Giulietta Guicciardi (m. 1826; div. 1827)",14 },
Using AI Code Generation
1var wptextdecoder = new TextDecoder('windows-1252');2var str = "Hello World";3var uint8array = new Uint8Array(str.length);4for (var i = 0; i < str.length; i++) {5 uint8array[i] = str.charCodeAt(i);6}7var fixedstring = wptextdecoder.fixupEncoding(uint8array);8console.log(fixedstring);9TextDecoder.fixupEncoding(input)10Recommended Posts: Node.js | TextDecoder.prototype.decode()11Node.js | TextDecoder.prototype.encoding()12Node.js | TextDecoder.prototype.fatal()13Node.js | TextDecoder.prototype.ignoreBOM()14Node.js | TextDecoder.prototype.surrogateStrategy()15Node.js | TextDecoder.prototype()16Node.js | TextEncoder.prototype.encode()17Node.js | TextEncoder.prototype()18Node.js | TextEncoder.prototype.encoding()19Node.js | TextEncoder.prototype.fatal()20Node.js | TextEncoder.prototype.ignoreBOM()21Node.js | TextEncoder.prototype.surrogateStrategy()22Node.js | TextDecoder()23Node.js | TextEncoder()24Node.js | TextEncoder.prototype.encodeInto()
Using AI Code Generation
1var wpt = require('webpagetest');2 console.log(data);3});4{ statusCode: '200',5{ encoding: 'ISO-8859-1' }6{ 'Dulles:Chrome':7 { browser: 'Chrome',8 'to': 'Google' },9 { browser: 'Firefox',10 'to': 'Google' },11 { browser: 'IE10',12 'to': 'Google' },
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.5e6d5f6c5e6d5f6c5e6d5f6c5e6d5f6c');3 if (err) {4 console.log(err);5 } else {6 console.log('Test status: ' + data.statusCode);7 console.log('Test status text: ' + data.statusText);8 console.log('Test ID: ' + data.data.testId);9 console.log('Test URL: ' + data.data.summary);10 console.log('Test results: ' + data.data.jsonUrl);11 }12});13var wpt = require('webpagetest');14var wpt = new WebPageTest('www.webpagetest.org', 'A.5e6d5f6c5e6d5f6c5e6d5f6c5e6d5f6c');15wpt.getLocations(function(err, data) {16 if (err) {17 console.log(err);18 } else {19 console.log('Test status: ' + data.statusCode);20 console.log('Test status text: ' + data.statusText);21 console.log('Test ID: ' + data.data.testId);22 console.log('Test URL: ' + data.data.summary);23 console.log('Test results: ' + data.data.jsonUrl);24 }25});26var wpt = require('webpagetest');27var wpt = new WebPageTest('www.webpagetest.org', 'A.5e6d5f6c5e6d5f6c5e6d5f6c5e6d5f6c');28wpt.getTesters(function(err, data) {29 if (err) {30 console.log(err);31 } else {32 console.log('Test status: ' + data.statusCode);
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!!