How to use SAME_SITE method in wpt

Best JavaScript code snippet using wpt

Promise.js

Source:Promise.js Github

copy

Full Screen

1'use strict';2const { Request, Response, NextFunction } = require('express');3 4/**5 * @class Promise6 * @constructor7 * @description Better Promise Management8 The callback is replaced by Promises and now the Promise chain is replaced by the async/await.9 This greatly enhances the coding experience.10 One problem with this implementation is to write the ugly try/catch block.11 In order to give it sugar, I have created a middleware function asyncHandler.12 * @version 1.0.013 * @author Khdir, Abdullah <abdullahkhder77@gmail.com>14*/15module.exports = class Promise {16 17 constructor() {18 }19 static asyncFunction = (request = Request, response = Response, next = NextFunction) => new Promise();20 static asyncHandler = (execution = Promise.asyncFunction) => (req = Request, res = Response, next = NextFunction) => {21 /**22 * @function setCookie23 * @description sets a simple cookie24 * @param {String} object 25 * @param {Object} object26 * @param {Number} expires milliseconds * seconds * minutes * hours * days * weeks * months (one day is the auto value)27 * @param {String} domain28 * @param {String} path29 * @param {Bool} secure30 * @param {Bool} http_only31 * @param {String} same_site32 * @returns void33 * @example34 * Set-Cookie: <cookie-name>=<cookie-value>35 Set-Cookie: <cookie-name>=<cookie-value>; Expires=<date>36 Set-Cookie: <cookie-name>=<cookie-value>; Max-Age=<number>37 Set-Cookie: <cookie-name>=<cookie-value>; Domain=<domain-value>38 Set-Cookie: <cookie-name>=<cookie-value>; Path=<path-value>39 Set-Cookie: <cookie-name>=<cookie-value>; Secure40 Set-Cookie: <cookie-name>=<cookie-value>; HttpOnly41 Set-Cookie: <cookie-name>=<cookie-value>; SameSite=Strict42 Set-Cookie: <cookie-name>=<cookie-value>; SameSite=Lax43 Set-Cookie: <cookie-name>=<cookie-value>; SameSite=None; Secure44 */45 if(typeof res !== 'undefined') {46 /**47 * @method setCookie48 * @description Sets a new cookie49 * @version 1.0.050 * @author Khdir, Abdullah <abdullahkhder77@gmail.com>51 * @param {Object} object52 * @param {Date} expires53 * @param {String} domain54 * @param {String} path55 * @param {Boolean} secure56 * @param {Boolean} http_only57 * @param {String} same_site58 * @return void59 */60 res.setCookie = (61 object,62 expires = new Date((1000 * 60 * 60 * 24 * 1 * 1 * 1) + Date.now()).toUTCString(),63 domain = null,64 path = '/',65 secure = true,66 http_only = true,67 same_site = 'Strict',68 ) => {69 let _cookie = '';70 let options = {};71 options.expires = expires;72 if (!expires) {73 expires = expires;74 options.expires = new Date((1000 * 60 * 60 * 24 * 1 * 1 * 1) + Date.now());75 }76 if (typeof object === 'string') {77 const _key = object.substr(0, object.indexOf('='));78 const _value = object.substr(_key.length + 1);79 _cookie = object + '; ' + 'Expires=' + expires + '; ';80 81 domain ? _cookie = _cookie + 'Domain='+domain+'; ' : _cookie;82 domain ? options.domain = domain : options.domain = '';83 path ? _cookie = _cookie + 'Path='+path+'; ' : _cookie;84 path ? options.path = path : options.path = '';85 86 secure ? _cookie = _cookie + 'secure; ' : _cookie;87 secure ? options.secure = true : options.secure = null;88 89 http_only ? _cookie = _cookie + 'http_only; ' : _cookie;90 http_only ? options.HttpOnly = true : options.HttpOnly = false;91 92 same_site ? _cookie = _cookie + 'SameSite='+same_site+'; ' : _cookie;93 same_site ? options.SameSite = same_site : options.SameSite = 'null';94 95 res.setHeader('Set-Cookie', _cookie);96 res.cookie(_key, _value, options);97 } else if (typeof object === 'object') {98 var _key = '';99 var _value = '';100 for (const key in object) {101 if (Object.hasOwnProperty.call(object, key)) {102 _key = key;103 _value = object[key];104 _cookie = key + '=' + object[key]+ '; ' + 'Expires=' + expires + '; '; 105 }106 }107 console.log(_cookie);108 domain ? _cookie = _cookie + 'Domain='+domain+'; ' : _cookie;109 domain ? options.domain = domain : options.domain = '';110 path ? _cookie = _cookie + 'Path='+path+'; ' : _cookie;111 path ? options.path = path : options.path = '';112 113 secure ? _cookie = _cookie + 'secure; ' : _cookie;114 secure ? options.secure = true : options.secure = null;115 116 http_only ? _cookie = _cookie + 'http_only; ' : _cookie;117 http_only ? options.HttpOnly = true : options.HttpOnly = false;118 119 same_site ? _cookie = _cookie + 'SameSite='+same_site+'; ' : _cookie;120 same_site ? options.SameSite = same_site : options.SameSite = 'null';121 122 res.setHeader('Set-Cookie', _cookie);123 res.cookie(_key, _value, options);124 }125 },126 /**127 * @method onLogOut128 * @description Deletes the registered cookie on logging out129 * @version 1.0.0130 * @author Khdir, Abdullah <abdullahkhder77@gmail.com>131 * @param {String} name132 * @param {Array} options133 * @return void134 */135 res.onLogOut = (name, options = []) => {136 res.clearCookie(name, options);137 },138 /**139 * @method storeInPrivateCache140 * @description Checks wether the cookie should be saved publicly or privatly141 * @version 1.0.0142 * @author Khdir, Abdullah <abdullahkhder77@gmail.com>143 * @return void144 */145 res.storeInPrivateCache = () => {146 if (typeof req.session !== 'undefined') {147 if (typeof req.session.is_authenticated !== 'undefined') {148 if (req.session.is_authenticated === true) {149 return true;150 }151 }152 }153 return false;154 },155 /**156 * @method longTimeNoCache157 * @description Caches any type of files that dont change very often158 * @version 1.0.0159 * @author Khdir, Abdullah <abdullahkhder77@gmail.com>160 * @return void161 */162 res.longTimeNoCache = () => {163 /*164 * You can add a long max-age value and immutable because the content will never change.165 * <script src=/public/js/main.js></script>166 * <img src=/public/img/hero.png?hash=deadbeef width=900 height=400>167 */168 /*169 * For Pictures, files or libs that will never change, not in the near future 170 */171 const store_cache_privatly = res.storeInPrivateCache();172 if (store_cache_privatly === true) {173 res.set('Cache-Control', 'private, max-age=31536000, immutable'); // 1 year174 } else {175 res.set('Cache-Control', 'public, max-age=31536000, immutable'); // 1 year176 }177 return;178 },179 /**180 * @method updatedContentAlways181 * @description Update the cache of files that change very often182 * @version 1.0.0183 * @author Khdir, Abdullah <abdullahkhder77@gmail.com>184 * @return void185 */186 res.updatedContentAlways = () => {187 /*188 * For content that's generated dynamically, 189 * or that's static but updated often, 190 * you want a user to always receive the most up-to-date version. 191 * e.g. for landing page192 */193 const store_cache_privatly = res.storeInPrivateCache();194 if (store_cache_privatly === true) {195 res.set('Cache-Control', 'private, no-cache');196 } else {197 res.set('Cache-Control', 'public, no-cache');198 }199 return;200 },201 /**202 * @method noCacheNeeded203 * @description Does not cache any response204 * @version 1.0.0205 * @author Khdir, Abdullah <abdullahkhder77@gmail.com>206 * @return void207 */208 res.noCacheNeeded = () => {209 /*210 * Preventing storing211 * If you don't want a response stored in caches, use the no-store directive.212 */213 res.set('Cache-Control', 'no-store');214 return;215 },216 /**217 * @method cacheLastResOnErr218 * @description Caches a response and on server side errors return the response for one day only219 * @version 1.0.0220 * @author Khdir, Abdullah <abdullahkhder77@gmail.com>221 * @return void222 */223 res.cacheLastResOnErr = () => {224 /*225 * The stale-if-error response directive indicates 226 * that the cache can reuse a stale response when an origin server responds with an error 227 * (500, 502, 503, or 504).228 * In the example above, the response is fresh for 7 days (604800s). 229 * After 7 days it becomes stale, but it can be used for an extra 1 day (86400s) 230 * if the server responds with an error.231 * After a period of time, 232 * the stored response became stale normally. 233 * This means that the client will receive an error response as-is if the origin server sends it.234 */235 const store_cache_privatly = res.storeInPrivateCache();236 if (store_cache_privatly === true) {237 res.set('Cache-Control', 'private, max-age=604800, stale-if-error=86400');238 } else {239 res.set('Cache-Control', 'public, max-age=604800, stale-if-error=86400');240 }241 return;242 },243 /**244 * @method cacheLandingPage245 * @description Caches a response for 180 seconds for a page that changes frequently246 * @version 1.0.0247 * @author Khdir, Abdullah <abdullahkhder77@gmail.com>248 * @return void249 */250 res.cacheLandingPage = () => {251 /*252 * 3 minutes cache for landing page 253 */254 const store_cache_privatly = res.storeInPrivateCache();255 if (store_cache_privatly === true) {256 res.set('Cache-control', 'private, max-age=180')257 } else {258 res.set('Cache-control', 'public, max-age=180')259 }260 return;261 }262 }263 execution(req, res, next).catch(next);264 };...

Full Screen

Full Screen

cookies.js

Source:cookies.js Github

copy

Full Screen

1/*!2 * Cookies.js - 0.4.03 *4 * Copyright (c) 2014, Scott Hamper5 * Licensed under the MIT license,6 * http://www.opensource.org/licenses/MIT7 */8var Cookies = function (key, value, options) {9 return arguments.length === 1 ?10 Cookies.get(key) : Cookies.set(key, value, options);11};12var ssl = window.location.protocol.toLowerCase() === 'https:';13// Allows for setter injection in unit tests14Cookies._document = document;15Cookies._navigator = navigator;16Cookies.defaults = {17 path: '/',18 same_site: ssl ? 'None' : 'Lax'19};20Cookies.get = function (key) {21 if (Cookies._cachedDocumentCookie !== Cookies._document.cookie) {22 Cookies._renewCache();23 }24 return Cookies._cache[key];25};26Cookies.set = function (key, value, options) {27 options = Cookies._getExtendedOptions(options);28 options.expires = Cookies._getExpiresDate(value === undefined ? -1 : options.expires);29 Cookies._document.cookie = Cookies._generateCookieString(key, value, options);30 return Cookies;31};32Cookies.expire = function (key, options) {33 return Cookies.set(key, undefined, options);34};35Cookies._getExtendedOptions = function (options) {36 var same_site = options?.same_site || Cookies.defaults.same_site;37 var same_siteNone = same_site.toLowerCase() === 'none';38 var defaultSecure = (same_siteNone && ssl) || undefined;39 return {40 path: options?.path || Cookies.defaults.path,41 domain: options?.domain || Cookies.defaults.domain,42 expires: options?.expires || Cookies.defaults.expires,43 secure: options?.secure !== undefined ? options.secure : defaultSecure,44 same_site: same_site45 };46};47Cookies._isValidDate = function (date) {48 return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());49};50Cookies._getExpiresDate = function (expires, now) {51 now = now || new Date();52 switch (typeof expires) {53 case 'number': expires = new Date(now.getTime() + expires * 1000); break;54 case 'string': expires = new Date(expires); break;55 }56 if (expires && !Cookies._isValidDate(expires)) {57 throw new Error('`expires` parameter cannot be converted to a valid Date instance');58 }59 return expires;60};61Cookies._generateCookieString = function (key, value, options) {62 key = key.replace(/[^#$&+\^`|]/g, encodeURIComponent);63 key = key.replace(/\(/g, '%28').replace(/\)/g, '%29');64 value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);65 options = options || {};66 var cookieString = key + '=' + value;67 cookieString += options.path ? ';path=' + options.path : '';68 cookieString += options.domain ? ';domain=' + options.domain : '';69 cookieString += options.expires ? ';expires=' + options.expires.toUTCString() : '';70 cookieString += options.secure ? ';Secure' : '';71 cookieString += options.same_site ? ';SameSite=' + options.same_site : '';72 return cookieString;73};74Cookies._getCookieObjectFromString = function (documentCookie) {75 var cookieObject = {};76 var cookiesArray = documentCookie ? documentCookie.split('; ') : [];77 for (var i = 0; i < cookiesArray.length; i++) {78 var cookieKvp = Cookies._getKeyValuePairFromCookieString(cookiesArray[i]);79 if (cookieObject[cookieKvp.key] === undefined) {80 cookieObject[cookieKvp.key] = cookieKvp.value;81 }82 }83 return cookieObject;84};85// fix "URIError: malformed" error86Cookies.decodeURIComponentX = function (str) {87 var out = '', arr, i = 0, l, x;88 arr = str.split(/(%(?:D0|D1)%.{2})/);89 for ( l = arr.length; i < l; i++ ) {90 try {91 x = decodeURIComponent( arr[i] );92 } catch (e) {93 x = arr[i];94 }95 out += x;96 }97 return out98}99Cookies._getKeyValuePairFromCookieString = function (cookieString) {100 // "=" is a valid character in a cookie value according to RFC6265, so cannot `split('=')`101 var separatorIndex = cookieString.indexOf('=');102 // IE omits the "=" when the cookie value is an empty string103 separatorIndex = separatorIndex < 0 ? cookieString.length : separatorIndex;104 return {105 key: Cookies.decodeURIComponentX(cookieString.substr(0, separatorIndex)),106 value: Cookies.decodeURIComponentX(cookieString.substr(separatorIndex + 1))107 };108};109Cookies._renewCache = function () {110 Cookies._cache = Cookies._getCookieObjectFromString(Cookies._document.cookie);111 Cookies._cachedDocumentCookie = Cookies._document.cookie;112};113Cookies._areEnabled = function () {114 var testKey = 'cookies.js';115 var areEnabled = Cookies.set(testKey, 1).get(testKey) === '1';116 Cookies.expire(testKey);117 return areEnabled;118};119Cookies.enabled = Cookies._areEnabled();...

Full Screen

Full Screen

config.js

Source:config.js Github

copy

Full Screen

1require('dotenv').config();2const {3 NODE_ENV,4 PORT,5 DEVELOPMENT_MONGODB_URI,6 TEST_MONGODB_URI,7 PRODUCTION_MONGODB_URI,8 SECRET,9 DEVELOPMENT_ORIGIN,10 PRODUCTION_ORIGIN,11 DOMAIN,12} = process.env;13let MONGODB_URI = '';14let SECURE_COOKIE_CONFIG = null;15let SAME_SITE = null;16let ORIGIN = null;17if (NODE_ENV === 'development') {18 MONGODB_URI = DEVELOPMENT_MONGODB_URI;19 SECURE_COOKIE_CONFIG = false;20 SAME_SITE = false;21 ORIGIN = DEVELOPMENT_ORIGIN;22} else if (NODE_ENV === 'test') {23 MONGODB_URI = TEST_MONGODB_URI;24 SECURE_COOKIE_CONFIG = false;25 SAME_SITE = false;26 ORIGIN = DEVELOPMENT_ORIGIN;27} else if (NODE_ENV === 'production') {28 MONGODB_URI = PRODUCTION_MONGODB_URI;29 SECURE_COOKIE_CONFIG = true;30 SAME_SITE = 'none';31 ORIGIN = PRODUCTION_ORIGIN;32}33module.exports = {34 MONGODB_URI,35 PORT,36 SECRET,37 SECURE_COOKIE_CONFIG,38 SAME_SITE,39 ORIGIN,40 DOMAIN,...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var wpt = require('webpagetest');4var wpt = new WebPageTest('www.webpagetest.org');5var wpt = require('webpagetest');6var wpt = new WebPageTest('www.webpagetest.org');

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful