helper.js (1408B)
1 'use strict'; 2 3 // See the max returned value from 4 // https://whatpr.org/fetch/1647.html#available-deferred-fetch-quota 5 const QUOTA_PER_ORIGIN = 64 * 1024; // 64 kibibytes. 6 // See the "minimal quota" from 7 // https://whatpr.org/fetch/1647.html#available-deferred-fetch-quota 8 const QUOTA_PER_CROSS_ORIGIN = 8 * 1024; // 8 kibibytes. 9 // See the "quotaForRequestOrigin" from 10 // https://whatpr.org/fetch/1647.html#available-deferred-fetch-quota 11 const QUOTA_PER_REPORTING_ORIGIN = 64 * 1024; // 64 kibibytes. 12 13 14 /** 15 * Calculates max possible remaining quota for a fetchLater request. 16 * 17 * Note that this function does NOT know how many fetchLater calls has been made 18 * before calling, nor does it know anything about requests within the URL 19 * origin, and both of them take up quota for the origin. 20 * 21 * @param {number} maxQuota The max quota to deduct from. Caller must ensure 22 * this is actually used by its owned context. 23 * @param {String} url The URL for a fetchLater call. 24 * @param {Headers=} headers The headers for a fetchLater call. 25 * @return {number} The max possible per-origin quota for fetchLater calls. 26 */ 27 function getRemainingQuota(maxQuota, url, headers = {}) { 28 let quota = maxQuota - url.length; 29 if (headers instanceof Headers) { 30 for (const kv of headers.entries()) { 31 quota -= kv[0].length + kv[1].length; 32 } 33 } 34 return quota > 0 ? quota : 0; 35 }