Best JavaScript code snippet using best
background.ts
Source:background.ts
1/* Background script. */2const browser = chrome;3let PUBLIC_SUFFIX_LIST = Object();4let CF_IPv4_LIST: string[] = []5let CF_IPv6_LIST: string[] = []6let AWS_LIST: {[ipaddr: string]: string} = {}7let ACTIVE_URL = '';8let DNS_CACHE: {[iso8601_date: string]: {[hostname: string]: string[]}} = {} // Saving the DNS CACHE, per day9const getDate = () => new Date().toISOString().substr(0,10)10const DEBUG = true;11const RESERVED_IPV4 = {12 "0.0.0.0/8": "Current network",13 "10.0.0.0/8": "Private network Used for local communications within a private network.",14 "100.64.0.0/10": "Private network Shared address space for communications between a service provider and its subscribers when using a carrier-grade NAT.",15 "127.0.0.0/8": "Host Used for loopback addresses to the local host.",16 "169.254.0.0/16": "Subnet Used for link-local addresses between two hosts on a single link when no IP address is otherwise specified, such as would have normally been retrieved from a DHCP server.",17 "172.16.0.0/12": "Private network Used for local communications within a private network.",18 "192.0.0.0/24": "Private network IETF Protocol Assignments.",19 "192.0.2.0/24": "Documentation Assigned as TEST-NET-1, documentation and examples.",20 "192.88.99.0/24": "Internet Reserved. Formerly used for IPv6 to IPv4 relay (included IPv6 address block 2002::/16).",21 "192.168.0.0/16": "Private network Used for local communications within a private network.",22 "198.18.0.0/15": "Private network Used for benchmark testing of inter-network communications between two separate subnets.",23 "198.51.100.0/24": "Documentation Assigned as TEST-NET-2, documentation and examples.",24 "203.0.113.0/24": "Documentation Assigned as TEST-NET-3, documentation and examples.",25 "224.0.0.0/4": "Internet In use for IP multicast.[11] (Former Class D network.)",26 "233.252.0.0/24":"Documentation Assigned as MCAST-TEST-NET, documentation and examples.",27 "240.0.0.0/4": "Internet Reserved for future use.[13] (Former Class E network.)",28 "255.255.255.255/32": "Subnet Reserved for the `limited broadcast` destination address."29}30const RESERVED_IPV6 = {31}32// For a tab, record the last url matching the regex and the timestamp of when it was visited33const EXTN_URL = chrome.runtime.getURL('')34const formatLog = (requestID: string, msg: string, data: any) => {35 console.log(`Alpaca|${new Date().getTime()}|${requestID}|${msg}: `, data)36}37browser.runtime.onInstalled.addListener(async () => {38 PUBLIC_SUFFIX_LIST = await setPublicSuffixList(); // Only check PSL on install39 const respv4 = await fetch('https://www.cloudflare.com/ips-v4')40 CF_IPv4_LIST = (await respv4.text()).split('\n')41 const respv6 = await fetch('https://www.cloudflare.com/ips-v6')42 CF_IPv6_LIST = (await respv6.text()).split('\n')43 formatLog('0', `Loaded PSL with this many entries`, PUBLIC_SUFFIX_LIST.length);44 formatLog('0', 'Alpaca Chrome extension has been installed at', EXTN_URL);45 const aws_resp = await fetch('https://ip-ranges.amazonaws.com/ip-ranges.json')46 const aws_text = await aws_resp.text()47 const aws_json = JSON.parse(aws_text)48 for (const i of aws_json.prefixes) {49 AWS_LIST[i.ip_prefix] = i.region50 };51 for (const i of aws_json.ipv6_prefixes) {52 AWS_LIST[i.ipv6_prefix] = i.region53 }54 /*55 Use CSS from https://sharkcoder.com/visual/shapes to make text more distinct56 2px px border57 orange | cloudflare58 red | fastly59 teal | cloudfront60 1px dark green border61 green | example.com, fe80::, ::, 169.254.x.x, private ip addresses62 grey border 2px: cloud; white border 2px: company63 blue1 | azure, similar color for microsoft64 blue2 | gcp, google65 blue3 | aws, amazon66 blue4 | facebook67 blue5 | apple68 /*69 /*70 const gcp_resp = await fetch('https://www.gstatic.com/ipranges/cloud.json')71 const gcp_text = await gcp_resp.text()72 const gcp_json = JSON.parse(gcp_text)73 for (const i of gcp_json.prefixes) {74 GCP_LIST[i.ip_prefix] = i.region75 };76 for (const i of gcp_json.ipv6_prefixes) {77 GCP_LIST[i.ipv6_prefix] = i.region78 }79 const azure_resp = await fetch('https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519')80 */81 formatLog('0', 'Alpaca Chrome extension loaded with this number of AWS entries', Object.keys(AWS_LIST).length);82});83// Don't need tab of update because we need to query tabs for url anyway84chrome.tabs.onActivated.addListener((_activeInfo)=> {85 console.log("Alpaca| Tab activated", _activeInfo);86 chrome.tabs.query({active: true}, (tabs) => {87 ACTIVE_URL = tabs[0].url || tabs[0].pendingUrl || ACTIVE_URL; // If url isn't available, page is still loading88 });89});90chrome.tabs.onUpdated.addListener((_tabID, _changeInfo, tab) => {91 if (_changeInfo.status === 'complete') {92 if (DEBUG)93 console.log("Tab update completed", _tabID, _changeInfo, tab);94 sendMessage(_tabID, _changeInfo.url || tab.url || '');95 }96 ACTIVE_URL = tab.url || ACTIVE_URL;97});98chrome.runtime.onMessage.addListener(99 function(request, sender, sendResponse) {100 parseArgs(request, sender).then(sendResponse)101 return true;102 }103);104function sendMessage(tab_id: number, url: string) {105 if (DEBUG)106 console.log("Attempting to send to active tabs and got these vars", tab_id, url)107 if (tab_id && url.startsWith('http')) {108 if (DEBUG)109 console.log("Alpaca| Sending message to tab", tab_id, "at", url)110 chrome.tabs.sendMessage(tab_id, {event: "tab_updated", url: url}, function(response) {111 console.log("Alpaca| Sent message and received response", response)112 });113 }114}115async function setPublicSuffixList() {116 const PUBLIC_SUFFIX_URL = 'https://publicsuffix.org/list/public_suffix_list.dat'117 const resp = await fetch(PUBLIC_SUFFIX_URL)118 let resptext = await resp.text()119 resptext = resptext.split('// ===END ICANN DOMAINS===')[0] // Use only public domains120 const public_suffixes = [...resptext.matchAll(/\n([^\n\/].*)/g)].map((i) => i[1])121 return public_suffixes122}123// Remove subdomains, protocol, searches, and hashes from domain, 'https://blog.github.com?search=true' => 'github.com'124/*function getBaseDomain(url: string): string {125 const hostname = (new URL(url)).hostname126 const parts = hostname.split('.')127 let rightside = parts.pop() as string;128 let publicSuffix;129 // Be as greedy as possible when it comes to possible effective TLDs.130 for (const part of parts.reverse()) {131 publicSuffix = rightside132 rightside = `${part}.${rightside}`133 if (!PUBLIC_SUFFIX_LIST.includes(rightside)) { // If the parsed domain from the right is no longer in PSL134 const re = new RegExp(`(.*)\.${publicSuffix}$`, 'g');135 let registeredDomain = (re.exec(hostname) as string[])[1]; 136 // remove subdomains137 if (registeredDomain.includes('.')) registeredDomain = registeredDomain.split('.').reverse()[0]138 return registeredDomain + '.' + publicSuffix139 }140 }141 return ''142}*/143export interface Question {144 name: string;145 type: number;146}147export interface Answer {148 name: string; // hostname149 type: number;150 TTL: number;151 data: string; // IP address152}153export interface DNSQuery {154 Status: number;155 TC: boolean;156 RD: boolean;157 RA: boolean;158 AD: boolean;159 CD: boolean;160 Question: Question[];161 Answer: Answer[];162}163async function DNSLookup(domain: string): Promise<[string[], number, string]> {164 const A_TYPE = 1165 const AAAA_TYPE = 28166 const todaysDate = getDate()167 DNS_CACHE[todaysDate] = DNS_CACHE[todaysDate] || {}168 if (DNS_CACHE[todaysDate][domain]) {169 if (DEBUG)170 console.log("Hit cache for", domain)171 return [DNS_CACHE[todaysDate][domain], 0, '']172 } else {173 if (DEBUG)174 console.log("missed", DNS_CACHE)175 }176 177 try {178 const required_headers = {headers: {'accept': 'application/dns-json'}}179 const respA = await fetch(`https://cloudflare-dns.com/dns-query?name=${domain}&type=A`, required_headers);180 const respTextA = await respA.text();181 if (DEBUG)182 console.log("Alpaca| Sending IPv4 DNS query for", domain, "Got", respTextA)183 const respJSONA: DNSQuery = JSON.parse(respTextA);184 let answers: Answer[] = [];185 if (respJSONA.Status !== 0) {186 return [[], respJSONA.Status, JSON.stringify(respJSONA.Answer)]187 }188 if (respJSONA.Answer) {189 const ipv4_answers = respJSONA.Answer.filter((rr) => rr.type === A_TYPE)190 answers = answers.concat(ipv4_answers)191 } else {192 // get IPv6 addresses in addition to IPv4193 const respAAAA = await fetch(`https://cloudflare-dns.com/dns-query?name=${domain}&type=AAAA`, required_headers);194 const respTextAAAA = await respAAAA.text();195 if (DEBUG)196 console.log("Alpaca| Sending IPv6 DNS query for", domain, "Got", respTextAAAA)197 const respJSONAAAA: DNSQuery = JSON.parse(respTextAAAA);198 if (respJSONAAAA.Status !== 0) {199 return [[], respJSONAAAA.Status, JSON.stringify(respJSONAAAA.Answer)]200 }201 if (respJSONA.Question && respJSONAAAA.Question && !respJSONA.Answer && !respJSONAAAA.Answer) {202 // We've exhausted both IPv4 and IPv6 record requests203 return [[], 3, 'NXDOMAIN: Domain name not exist. No A or AAAA records found for ' + domain + '.'] 204 }205 if (respJSONAAAA.Status === 0 && respJSONAAAA.Answer) {206 const ipv6_answers = respJSONAAAA.Answer.filter((rr) => rr.type === AAAA_TYPE)207 answers = answers.concat(ipv6_answers)208 }209 }210 let ip_addrs = []211 for (const rr of answers) {212 ip_addrs.push(rr.data)213 }214 console.log("Alpaca| Found IP ADDRs", ip_addrs, "for domains", domain, ". Adding to cache")215 DNS_CACHE[todaysDate][domain] = ip_addrs216 return [ip_addrs, 0, '']217 } catch(e) {218 const err_msg = 'Encountered problem looking up DNS for ' + domain + ':' + e219 console.error();220 return [[], 0, err_msg];221 }222}223// Check the status code of a URL224async function fetchURL(url: string) {225 const resp = await fetch(url, { method: 'GET', redirect: 'follow'});226 console.log("Got url and resp:", url, resp);227 return resp;228}229async function parseArgs(request: any, sender: any) {230 if (DEBUG)231 console.log(sender.tab ? "Alpaca| Got request from content script: " + sender.tab.url + JSON.stringify(request): "Got request from the extension");232 if (request.requestName === "CF_IPV4_LIST") {233 return {data: CF_IPv4_LIST};234 } else if (request.requestName === "CF_IPV6_LIST") {235 return {data: CF_IPv6_LIST};236 } else if (request.requestName === "PUBLIC_SUFFIX_LIST") {237 return {data: PUBLIC_SUFFIX_LIST};238 } else if (request.requestName === "DNS_LOOKUP") { 239 // domain should match PSL because it's been checked in content script240 let domain = request.domain241 if (domain.includes('://')) { // Get rid of scheme242 domain = domain.split('://')[1]243 }244 const [ip_addrs, dns_code, err_msg] = await DNSLookup(domain)245 return {data: ip_addrs, dns_code: dns_code, error: err_msg};246 } else if (request.requestName === "URL_FETCH") {247 try {248 const resp = await fetchURL(request.url)249 return {status_code: resp.status, error: ''}250 } catch (e) {251 return {status_code: 404, error: e}252 }253 }254 return {data: "UNKNOWN REQUEST NAME"};...
aws-wrapper.ts
Source:aws-wrapper.ts
1/*2 * Copyright (c) 2019, salesforce.com, inc.3 * All rights reserved.4 * SPDX-License-Identifier: MIT5 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT6*/7import AWS from 'aws-sdk';8import path from 'path';9import chalk from 'chalk';10import { lookup } from 'mime-types';11export const AWS_TEXT = chalk.reset.inverse.yellow.bold(' AWS-S3 ') + ' ';12const VERSION = 'v1.4';13const PREFIX = `public/${VERSION}`;14const BENCHMARKS = 'benchmarks';15const BRANCHES = 'branches';16const ONE_YEAR = 1000 * 60 * 60 * 24 * 365;17export class S3 {18 s3: AWS.S3;19 bucket: string;20 host: string;21 version: string;22 constructor({ bucket, version }: { bucket?: string, version?: string } = {}) {23 const bucketName = bucket || process.env.AWS_BUCKET_NAME;24 if (bucketName === undefined) {25 throw new Error('Bucket cannot be undefined');26 }27 this.s3 = new AWS.S3(...arguments);28 this.bucket = bucketName;29 this.host = `https://${this.bucket}.s3.amazonaws.com/`;30 this.version = version || VERSION;31 }32 async getBenchmarkUrlsForCommit(projectName: string, searchCommit: string) {33 console.log(AWS_TEXT, `Resolving objects for commit ${searchCommit}...`);34 const benchmarks = await this.getObjectsInFolder(projectName, BENCHMARKS, searchCommit);35 return benchmarks.map(bm => this.getBenchmarkStatsUrl(projectName, searchCommit, bm));36 }37 getBenchmarkStatsUrl(projectName: string, searchCommit: string, benchmark: string) {38 return this.host + path.join(PREFIX, projectName, BENCHMARKS, searchCommit, benchmark);39 }40 getProjects() {41 return this.getObjectsInFolder('');42 }43 listBranches(projectName: string) {44 return this.getObjectsInFolder(projectName, BRANCHES);45 }46 getCommits(projectName: string, branchName: string) {47 return this.getObjectsInFolder(projectName, BRANCHES, branchName);48 }49 listBenchmarks(projectName:string, commit: string) {50 return this.getObjectsInFolder(projectName, BENCHMARKS, commit);51 }52 getObjectsInFolder(...args: string[]): Promise<string[]> {53 return new Promise((resolve, reject) => {54 const opts = {55 Bucket: this.bucket,56 Delimiter: '/',57 Prefix: path.join(PREFIX, args.join('/')) + '/',58 };59 this.s3.listObjectsV2(opts, (err, data) => {60 if (err) {61 return reject(err);62 }63 const branches = data.CommonPrefixes!.map(p => {64 const parts = p.Prefix!.split('/');65 return parts[parts.length - 2];66 });67 return resolve(branches);68 });69 });70 }71 // _recursiveListBenchmarks(opts, callback, results) {72 // this.s3.listObjectsV2(opts, (err, data) => {73 // if (err) {74 // callback(err);75 // } else {76 // if (data.Contents) {77 // results.push(...data.Contents);78 // }79 // if (data.NextContinuationToken) {80 // opts.ContinuationToken = data.NextContinuationToken;81 // this._recursiveListBenchmarks(opts, callback, results);82 // } else {83 // callback(null, results);84 // }85 // }86 // });87 // }88 storeBranchCommitIndex(projectName: string, branch: string, commit: string) {89 const url = path.join(PREFIX, `${projectName}/${BRANCHES}/${branch}/${commit}`, 'index.json');90 const s3 = this.s3;91 const bucket = this.bucket;92 return new Promise((resolve, reject) => {93 s3.putObject(94 {95 Bucket: bucket,96 Key: url,97 Body: `{ time: ${'' + new Date()} }`,98 Expires: new Date(Date.now() + ONE_YEAR),99 ContentType: lookup(url) || undefined,100 },101 (err, data) => {102 if (err) {103 return reject(err);104 }105 return resolve(data);106 },107 );108 });109 }110 storeBenchmarkFile(relativePath: string, body: string | Buffer, { projectName, commit, benchmarkName }: { projectName: string, commit: string, benchmarkName: string }) {111 const url = path.join(PREFIX, projectName, BENCHMARKS, commit, benchmarkName, relativePath);112 const s3 = this.s3;113 const bucket = this.bucket;114 return new Promise((resolve, reject) => {115 s3.putObject(116 {117 Bucket: bucket,118 Key: url,119 Body: body,120 Expires: new Date(Date.now() + ONE_YEAR),121 ContentType: lookup(url) || undefined,122 },123 (err, data) => {124 if (err) {125 return reject(err);126 }127 console.log(AWS_TEXT + url);128 return resolve(data);129 },130 );131 });132 }...
index.ts
Source:index.ts
1/*2 * Copyright (c) 2019, salesforce.com, inc.3 * All rights reserved.4 * SPDX-License-Identifier: MIT5 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT6*/7import { S3, AWS_TEXT } from './aws-wrapper';8import fs from 'fs';9import chalk from 'chalk';10import fetch from 'node-fetch';11import { BenchmarkResultsSnapshot, FrozenGlobalConfig } from '@best/types';12const INIT_RUNNING_TEXT = chalk.bold.dim('\nPushing to AWS(S3)...');13let S3_INSTANCE: S3;14function getS3Instance() {15 if (!S3_INSTANCE) {16 S3_INSTANCE = new S3();17 }18 return S3_INSTANCE;19}20export function initialize(/* config */) {21 getS3Instance();22}23export async function storeBenchmarkResults(24 fileMap: { [key: string]: string },25 { benchmarkInfo: { benchmarkName, benchmarkSignature }, projectConfig } : BenchmarkResultsSnapshot,26 globalConfig: FrozenGlobalConfig,27) {28 const { gitInfo: { localChanges: gitLocalChanges, branch: gitBranch, lastCommit: { hash: gitCommit } } } = globalConfig;29 const { projectName } = projectConfig;30 // Replace slashes with underscores so we prevent ambiguous URLs31 const branch = (gitLocalChanges ? `local/${gitBranch}` : gitBranch).replace(/\//g, '_');32 const commit = gitLocalChanges ? `${gitCommit}_${benchmarkSignature.slice(0, 7)}` : gitCommit.slice(0, 7);33 console.log(INIT_RUNNING_TEXT);34 const s3 = getS3Instance();35 console.log('Bucket:', `https://${s3.bucket}.s3.amazonaws.com/`, '\n');36 await Promise.all(37 Object.keys(fileMap).map(file => {38 const buffer = fs.readFileSync(fileMap[file]);39 return s3.storeBenchmarkFile(file, buffer, {40 projectName,41 commit,42 benchmarkName,43 });44 }),45 );46 // This will allow us to search in the bucket by brach/commit47 await s3.storeBranchCommitIndex(projectName, branch, commit);48}49export async function getAllBenchmarkStatsPerCommit(projectName: string, commit: string) {50 const s3 = getS3Instance();51 const benchmarks = await s3.getBenchmarkUrlsForCommit(projectName, commit);52 if (benchmarks && benchmarks.length) {53 console.log(AWS_TEXT + ` Fetching benchmarks for commit ${commit}...`);54 return Promise.all(55 benchmarks.map(async url => {56 const fullUrl = url + '/stats.json';57 console.log(AWS_TEXT + ` Fetching benchmark ${fullUrl}`);58 const response = await fetch(fullUrl);59 const json = await response.json();60 return Object.assign(json, { projectName });61 }),62 );63 }64 return benchmarks;65}66export function getProjects() {67 const s3 = getS3Instance();68 return s3.getProjects();69}70export function getCommits(projectName: string, branch: string) {71 const s3 = getS3Instance();72 return s3.getCommits(projectName, branch);...
Using AI Code Generation
1var AWS = require('aws-sdk');2var fs = require('fs');3var BestFitLine = require('./bestFitLine.js');4var bestFitLine = new BestFitLine();5var s3 = new AWS.S3();6var params = {7};8var data = s3.getObject(params, function(err, data) {9 else {10 var dataStr = data.Body.toString();11 var dataArray = dataStr.split("\n");12 dataArray = dataArray.map(function(item) {13 return item.split(" ").map(function(item) {14 return parseFloat(item);15 });16 });17 var result = bestFitLine.AWS_TEXT(dataArray);18 console.log(result);19});
Using AI Code Generation
1var AWS = require('aws-sdk');2var fs = require('fs');3AWS.config.loadFromPath('./config.json');4var cognitoidentity = new AWS.CognitoIdentity();5var s3 = new AWS.S3();6var rekognition = new AWS.Rekognition();7var comprehend = new AWS.Comprehend();8var translate = new AWS.Translate();9var polly = new AWS.Polly();10var dynamodb = new AWS.DynamoDB();11var lex = new AWS.Lex();12var sns = new AWS.SNS();13var sqs = new AWS.SQS();14var ses = new AWS.SES();15var apigateway = new AWS.APIGateway();16var cognitosync = new AWS.CognitoSync();17var elastictranscoder = new AWS.ElasticTranscoder();18var simpledb = new AWS.SimpleDB();19var cloudwatch = new AWS.CloudWatch();20var cloudwatchevents = new AWS.CloudWatchEvents();21var cloudwatchlogs = new AWS.CloudWatchLogs();22var kinesis = new AWS.Kinesis();23var kinesisanalytics = new AWS.KinesisAnalytics();24var kinesisvideo = new AWS.KinesisVideo();
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!!