Best JavaScript code snippet using wpt
utils.js
Source:utils.js
...66function parseCosePublicKey(coseKey) {67 // Parse a CTAP2 canonical CBOR encoding form key.68 // https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-client-to-authenticator-protocol-v2.0-id-20180227.html#ctap2-canonical-cbor-encoding-form69 let parsed = new Cbor(coseKey);70 let cbor = parsed.getCBOR();71 let key = {72 type: cbor[1],73 alg: cbor[3],74 };75 if (key.type != 2)76 assert_unreached("Unknown key type: " + key.type);77 key.crv = cbor[-1];78 key.x = new Uint8Array(cbor[-2]);79 key.y = new Uint8Array(cbor[-3]);80 return key;81}82function parseAttestedCredentialData(attestedCredentialData) {83 // Parse the attested credential data according to84 // https://w3c.github.io/webauthn/#attested-credential-data85 let aaguid = attestedCredentialData.slice(0, 16);86 let credentialIdLength = (attestedCredentialData[16] << 8)87 + attestedCredentialData[17];88 let credentialId =89 attestedCredentialData.slice(18, 18 + credentialIdLength);90 let credentialPublicKey = parseCosePublicKey(91 attestedCredentialData.slice(18 + credentialIdLength,92 attestedCredentialData.length));93 return { aaguid, credentialIdLength, credentialId, credentialPublicKey };94}95function parseAuthenticatorData(authenticatorData) {96 // Parse the authenticator data according to97 // https://w3c.github.io/webauthn/#sctn-authenticator-data98 assert_greater_than_equal(authenticatorData.length, 37);99 let flags = authenticatorData[32];100 let counter = authenticatorData.slice(33, 37);101 let attestedCredentialData = authenticatorData.length > 37 ?102 parseAttestedCredentialData(authenticatorData.slice(37)) : null;103 let extensions = null;104 if (attestedCredentialData &&105 authenticatorData.length > 37 + attestedCredentialData.length) {106 extensions = authenticatorData.slice(37 + attestedCredentialData.length);107 }108 return {109 rpIdHash: authenticatorData.slice(0, 32),110 flags: {111 up: !!(flags & 0x01),112 uv: !!(flags & 0x04),113 at: !!(flags & 0x40),114 ed: !!(flags & 0x80),115 },116 counter: (counter[0] << 24)117 + (counter[1] << 16)118 + (counter[2] << 8)119 + counter[3],120 attestedCredentialData,121 extensions,122 };123}124// Taken from125// https://cs.chromium.org/chromium/src/chrome/browser/resources/cryptotoken/cbor.js?rcl=c9b6055cf9c158fb4119afd561a591f8fc95aefe126class Cbor {127 constructor(buffer) {128 this.slice = new Uint8Array(buffer);129 }130 get data() {131 return this.slice;132 }133 get length() {134 return this.slice.length;135 }136 get empty() {137 return this.slice.length == 0;138 }139 get hex() {140 const hexTable = '0123456789abcdef';141 let s = '';142 for (let i = 0; i < this.data.length; i++) {143 s += hexTable.charAt(this.data[i] >> 4);144 s += hexTable.charAt(this.data[i] & 15);145 }146 return s;147 }148 compare(other) {149 if (this.length < other.length) {150 return -1;151 } else if (this.length > other.length) {152 return 1;153 }154 for (let i = 0; i < this.length; i++) {155 if (this.slice[i] < other.slice[i]) {156 return -1;157 } else if (this.slice[i] > other.slice[i]) {158 return 1;159 }160 }161 return 0;162 }163 getU8() {164 if (this.empty) {165 throw('Cbor: empty during getU8');166 }167 const byte = this.slice[0];168 this.slice = this.slice.subarray(1);169 return byte;170 }171 skip(n) {172 if (this.length < n) {173 throw('Cbor: too few bytes to skip');174 }175 this.slice = this.slice.subarray(n);176 }177 getBytes(n) {178 if (this.length < n) {179 throw('Cbor: insufficient bytes in getBytes');180 }181 const ret = this.slice.subarray(0, n);182 this.slice = this.slice.subarray(n);183 return ret;184 }185 getCBORHeader() {186 const copy = new Cbor(this.slice);187 const a = this.getU8();188 const majorType = a >> 5;189 const info = a & 31;190 if (info < 24) {191 return [majorType, info, new Cbor(copy.getBytes(1))];192 } else if (info < 28) {193 const lengthLength = 1 << (info - 24);194 let data = this.getBytes(lengthLength);195 let value = 0;196 for (let i = 0; i < lengthLength; i++) {197 // Javascript has problems handling uint64s given the limited range of198 // a double.199 if (value > 35184372088831) {200 throw('Cbor: cannot represent CBOR number');201 }202 // Not using bitwise operations to avoid truncating to 32 bits.203 value *= 256;204 value += data[i];205 }206 switch (lengthLength) {207 case 1:208 if (value < 24) {209 throw('Cbor: value should have been encoded in single byte');210 }211 break;212 case 2:213 if (value < 256) {214 throw('Cbor: non-minimal integer');215 }216 break;217 case 4:218 if (value < 65536) {219 throw('Cbor: non-minimal integer');220 }221 break;222 case 8:223 if (value < 4294967296) {224 throw('Cbor: non-minimal integer');225 }226 break;227 }228 return [majorType, value, new Cbor(copy.getBytes(1 + lengthLength))];229 } else {230 throw('Cbor: CBOR contains unhandled info value ' + info);231 }232 }233 getCBOR() {234 const [major, value] = this.getCBORHeader();235 switch (major) {236 case 0:237 return value;238 case 1:239 return 0 - (1 + value);240 case 2:241 return this.getBytes(value);242 case 3:243 return this.getBytes(value);244 case 4: {245 let ret = new Array(value);246 for (let i = 0; i < value; i++) {247 ret[i] = this.getCBOR();248 }249 return ret;250 }251 case 5:252 if (value == 0) {253 return {};254 }255 let copy = new Cbor(this.data);256 const [firstKeyMajor] = copy.getCBORHeader();257 if (firstKeyMajor == 3) {258 // String-keyed map.259 let lastKeyHeader = new Cbor(new Uint8Array(0));260 let lastKeyBytes = new Cbor(new Uint8Array(0));261 let ret = {};262 for (let i = 0; i < value; i++) {263 const [keyMajor, keyLength, keyHeader] = this.getCBORHeader();264 if (keyMajor != 3) {265 throw('Cbor: non-string in string-valued map');266 }267 const keyBytes = new Cbor(this.getBytes(keyLength));268 if (i > 0) {269 const headerCmp = lastKeyHeader.compare(keyHeader);270 if (headerCmp > 0 ||271 (headerCmp == 0 && lastKeyBytes.compare(keyBytes) >= 0)) {272 throw(273 'Cbor: map keys in wrong order: ' + lastKeyHeader.hex +274 '/' + lastKeyBytes.hex + ' ' + keyHeader.hex + '/' +275 keyBytes.hex);276 }277 }278 lastKeyHeader = keyHeader;279 lastKeyBytes = keyBytes;280 ret[keyBytes.parseUTF8()] = this.getCBOR();281 }282 return ret;283 } else if (firstKeyMajor == 0 || firstKeyMajor == 1) {284 // Number-keyed map.285 let lastKeyHeader = new Cbor(new Uint8Array(0));286 let ret = {};287 for (let i = 0; i < value; i++) {288 let [keyMajor, keyValue, keyHeader] = this.getCBORHeader();289 if (keyMajor != 0 && keyMajor != 1) {290 throw('Cbor: non-number in number-valued map');291 }292 if (i > 0 && lastKeyHeader.compare(keyHeader) >= 0) {293 throw(294 'Cbor: map keys in wrong order: ' + lastKeyHeader.hex + ' ' +295 keyHeader.hex);296 }297 lastKeyHeader = keyHeader;298 if (keyMajor == 1) {299 keyValue = 0 - (1 + keyValue);300 }301 ret[keyValue] = this.getCBOR();302 }303 return ret;304 } else {305 throw('Cbor: map keyed by invalid major type ' + firstKeyMajor);306 }307 default:308 throw('Cbor: unhandled major type ' + major);309 }310 }311 parseUTF8() {312 return (new TextDecoder('utf-8')).decode(this.slice);313 }...
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.getCBOR(url, function(err, data) {3 if (err) throw err;4 console.log(data);5});6var wpt = require('./wpt.js');7wpt.getXML(url, function(err, data) {8 if (err) throw err;9 console.log(data);10});11var wpt = require('./wpt.js');12wpt.getJSON(url, function(err, data) {13 if (err) throw err;14 console.log(data);15});16var wpt = require('./wpt.js');17wpt.getHar(url, function(err, data) {18 if (err) throw err;19 console.log(data);20});21var wpt = require('./wpt.js');22wpt.getWaterfall(url, function(err, data) {23 if (err) throw err;24 console.log(data);25});26var wpt = require('./wpt.js');27wpt.getScreenshot(url, function(err, data) {28 if (err) throw err;29 console.log(data);30});31var wpt = require('./wpt.js');32wpt.getVideo(url, function(err, data) {33 if (err) throw err;
Using AI Code Generation
1var wpt = require('./wpt.js');2 if(err){3 console.log(err);4 return;5 }6 console.log(data);7});8### getCBOR(url, callback)9### getCBOR(url, options, callback)10var wpt = require('./wpt.js');11 if(err){12 console.log(err);13 return;14 }15 console.log(data);16});17[Anurag Goel](
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var file = fs.createWriteStream('output.json');4var json = wptools.getCBOR('Barack Obama', {format: 'json'}, function(err, res) {5 if (err) {6 console.log(err);7 }8 else {9 file.write(JSON.stringify(res));10 }11});
Using AI Code Generation
1const wptools = require('wptools');2const fs = require('fs');3const promise = wptools.getCBOR('Albert Einstein');4promise.then((data) => {5 data.then((data) => {6 fs.writeFile('data.json', JSON.stringify(data), (err) => {7 if (err) throw err;8 console.log('The file has been saved!');9 });10 }).catch((err) => {11 console.log(err);12 });13}).catch((err) => {14 console.log(err);15});
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var api = new wpt(options);5function getCBOR(url, cb) {6 api.runTest(url, {7 }, function (err, data) {8 if (err) {9 console.log(err);10 return;11 }12 api.getTestResults(data.data.testId, function (err, data) {13 if (err) {14 console.log(err);15 return;16 }17 cb(data.data.median.firstView);18 });19 });20}21getCBOR(url, function (data) {22 console.log(data);23});
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!!