SharedUtils.sys.mjs (1827B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /** 6 * Common logic shared by RemoteSettingsWorker.js (Worker) and the main thread. 7 */ 8 9 export var SharedUtils = { 10 /** 11 * Check that the specified content matches the expected size and SHA-256 hash. 12 * 13 * @param {ArrayBuffer} buffer binary content 14 * @param {number} size expected file size 15 * @param {string} size expected file SHA-256 as hex string 16 * @returns {boolean} 17 */ 18 async checkContentHash(buffer, size, hash) { 19 const bytes = new Uint8Array(buffer); 20 // Has expected size? (saves computing hash) 21 if (bytes.length !== size) { 22 return false; 23 } 24 // Has expected content? 25 const hashBuffer = await crypto.subtle.digest("SHA-256", bytes); 26 const hashBytes = new Uint8Array(hashBuffer); 27 const toHex = b => b.toString(16).padStart(2, "0"); 28 const hashStr = Array.from(hashBytes, toHex).join(""); 29 return hashStr == hash; 30 }, 31 32 /** 33 * Load (from disk) the JSON file distributed with the release for this collection. 34 * 35 * @param {string} bucket 36 * @param {string} collection 37 */ 38 async loadJSONDump(bucket, collection) { 39 // When using the preview bucket, we still want to load the main dump. 40 // But we store it locally in the preview bucket. 41 const jsonBucket = bucket.replace("-preview", ""); 42 const fileURI = `resource://app/defaults/settings/${jsonBucket}/${collection}.json`; 43 let response; 44 try { 45 response = await fetch(fileURI); 46 } catch (e) { 47 // Return null if file is missing. 48 return { data: null, timestamp: null }; 49 } 50 // Will throw if JSON is invalid. 51 return response.json(); 52 }, 53 };