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/. */
6 * Common logic shared by RemoteSettingsWorker.js (Worker) and the main thread.
9 export var SharedUtils = {
11 * Check that the specified content matches the expected size and SHA-256 hash.
12 * @param {ArrayBuffer} buffer binary content
13 * @param {Number} size expected file size
14 * @param {String} size expected file SHA-256 as hex string
17 async checkContentHash(buffer, size, hash) {
18 const bytes = new Uint8Array(buffer);
19 // Has expected size? (saves computing hash)
20 if (bytes.length !== size) {
23 // Has expected content?
24 const hashBuffer = await crypto.subtle.digest("SHA-256", bytes);
25 const hashBytes = new Uint8Array(hashBuffer);
26 const toHex = b => b.toString(16).padStart(2, "0");
27 const hashStr = Array.from(hashBytes, toHex).join("");
28 return hashStr == hash;
32 * Load (from disk) the JSON file distributed with the release for this collection.
33 * @param {String} bucket
34 * @param {String} collection
36 async loadJSONDump(bucket, collection) {
37 // When using the preview bucket, we still want to load the main dump.
38 // But we store it locally in the preview bucket.
39 const jsonBucket = bucket.replace("-preview", "");
40 const fileURI = `resource://app/defaults/settings/${jsonBucket}/${collection}.json`;
43 response = await fetch(fileURI);
45 // Return null if file is missing.
46 return { data: null, timestamp: null };
48 // Will throw if JSON is invalid.
49 return response.json();