Best JavaScript code snippet using wpt
index.spec.ts
Source:index.spec.ts
1import {createReadStream, mkdtempSync, writeFileSync} from 'fs';2import {join} from 'path';3import {tmpdir} from 'os';4import {Readable} from 'stream';5import {Sha256} from '@aws-crypto/sha256-js';6import {toHex} from '@aws-sdk/util-hex-encoding';7import {calculateSha256} from './index';8function createTemporaryFile(contents: string): string {9 const folder = mkdtempSync(10 join(tmpdir(), 'sha256-stream-node-')11 );12 const fileLoc = join(folder, 'test.txt');13 writeFileSync(fileLoc, contents);14 return fileLoc;15}16describe('calculateSha256', () => {17 const temporaryFile = createTemporaryFile(18 "Shot through the bar, but you're too late bizzbuzz you give foo, a bad name."19 );20 it('calculates the SHA256 hash of a stream', async () => {21 const result = await calculateSha256(22 Sha256,23 createReadStream(temporaryFile)24 );25 expect(result instanceof Uint8Array).toBe(true);26 expect(toHex(result)).toBe(27 '24dabf4db3774a3224d571d4c089a9c570c3045dbe1e67ee9ee2e2677f57dbe0'28 );29 });30 it('does not exhaust the input stream', async () => {31 const inputStream = createReadStream(temporaryFile);32 const onSpy = jest.spyOn(inputStream, 'on');33 const pipeSpy = jest.spyOn(inputStream, 'pipe');34 const result = await calculateSha256(35 Sha256,36 inputStream37 );38 expect(result instanceof Uint8Array).toBe(true);39 expect(toHex(result)).toBe(40 '24dabf4db3774a3224d571d4c089a9c570c3045dbe1e67ee9ee2e2677f57dbe0'41 );42 expect(onSpy.mock.calls.length).toBe(0);43 expect(pipeSpy.mock.calls.length).toBe(0);44 });45 it('throws an error when a non-file stream is encountered', async () => {46 const inputStream = new Readable();47 await expect(calculateSha256(Sha256, (inputStream as any))).rejects.toHaveProperty('message');48 });...
Account.ts
Source:Account.ts
...14 }15 public static calculateHash(username: string, password: string): string16 {17 const {calculateSHA256} = Account;18 return calculateSHA256(calculateSHA256(username) + calculateSHA256(password));19 }20 public static validate(obj: Readonly<Record<keyof Account, any>>): boolean21 {22 const {username, hash} = obj;23 return typeof username === 'string'24 && typeof hash === 'string';25 }26 public static from(obj: Readonly<Record<keyof Account, any>>)27 {28 const {username, hash} = obj;29 if (!Account.validate({username, hash}))30 {31 throw new TypeError(`Source object is not a ${Account.name} instance`);32 }33 return new Account(username, hash);34 }35 private static calculateSHA256(text: string): string36 {37 const hash = crypto.createHash('sha256');38 hash.update(text);39 return hash.digest('hex');40 }...
crypto-utils.js
Source:crypto-utils.js
1let crypto = require('crypto');2function calculateSHA256(obj) {3 let data = JSON.stringify(obj);4 return crypto.createHash('sha256').update(data).digest('hex')5}6function checkIntegrity(data, hash) {7 data = String(data)8 return calculateSHA256(data) === hash9}10module.exports = {11 calculateSHA256,12 checkIntegrity...
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.runTest(url, function(err, data) {4 if (err) return console.log(err);5 console.log(data);6});7var wpt = require('wpt');8var wpt = new WebPageTest('www.webpagetest.org');9wpt.getLocations(function(err, data) {10 if (err) return console.log(err);11 console.log(data);12});13var wpt = require('wpt');14var wpt = new WebPageTest('www.webpagetest.org');15wpt.getTesters(function(err, data) {16 if (err) return console.log(err);17 console.log(data);18});19var wpt = require('wpt');20var wpt = new WebPageTest('www.webpagetest.org');21wpt.getTestResults('140131_9X_1Z', function(err, data) {22 if (err) return console.log(err);23 console.log(data);24});25var wpt = require('wpt');26var wpt = new WebPageTest('www.webpagetest.org');27wpt.getTestStatus('140131_9X_1Z', function(err, data) {28 if (err) return console.log(err);29 console.log(data);30});31var wpt = require('wpt');32var wpt = new WebPageTest('www.webpagetest.org');33wpt.getHAR('140131_9X_1Z', function(err, data) {34 if (err) return console.log(err);35 console.log(data);36});37var wpt = require('wpt');38var wpt = new WebPageTest('www.webpagetest.org');39wpt.getScreenshot('140131
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!!