From dd30d8567c2cd6c093284d67a265eff65faf07e0 Mon Sep 17 00:00:00 2001 From: Mark Banner Date: Wed, 13 Mar 2024 17:38:27 +0000 Subject: [PATCH] Bug 1861918 - Convert kinto-offline-client to an ES module. r=leplatrem,extension-reviewers,robwu Differential Revision: https://phabricator.services.mozilla.com/D204431 --- .eslintignore | 4 -- .prettierignore | 2 +- ...client.js => kinto-offline-client.sys.mjs} | 42 +++---------------- services/common/kinto-storage-adapter.sys.mjs | 4 +- services/common/moz.build | 2 +- services/common/tests/unit/test_kinto.js | 4 +- services/settings/RemoteSettings.worker.mjs | 2 +- .../ExtensionStorageSyncKinto.sys.mjs | 4 +- tools/rewriting/ThirdPartyPaths.txt | 2 +- 9 files changed, 14 insertions(+), 52 deletions(-) rename services/common/{kinto-offline-client.js => kinto-offline-client.sys.mjs} (98%) diff --git a/.eslintignore b/.eslintignore index 58f1acb45c7c..ab309a7cb9de 100644 --- a/.eslintignore +++ b/.eslintignore @@ -209,10 +209,6 @@ remote/shared/messagehandler/test/browser/resources/modules/root/invalid.sys.mjs # services/ exclusions -# Third party services -services/common/kinto-http-client.sys.mjs -services/common/kinto-offline-client.js - # Webpack-bundled library services/fxaccounts/FxAccountsPairingChannel.sys.mjs diff --git a/.prettierignore b/.prettierignore index 63473bdacb2a..1581e680da35 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1399,7 +1399,7 @@ security/nss/ security/sandbox/chromium/ security/sandbox/chromium-shim/ services/common/kinto-http-client.sys.mjs -services/common/kinto-offline-client.js +services/common/kinto-offline-client.sys.mjs testing/gtest/gmock/ testing/gtest/gtest/ testing/mochitest/MochiKit/ diff --git a/services/common/kinto-offline-client.js b/services/common/kinto-offline-client.sys.mjs similarity index 98% rename from services/common/kinto-offline-client.js rename to services/common/kinto-offline-client.sys.mjs index a28fc6e150cc..3435c57f5a3e 100644 --- a/services/common/kinto-offline-client.js +++ b/services/common/kinto-offline-client.sys.mjs @@ -12,36 +12,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -"use strict"; /* * This file is generated from kinto.js - do not modify directly. */ -// This is required because with Babel compiles ES2015 modules into a -// require() form that tries to keep its modules on "this", but -// doesn't specify "this", leaving it to default to the global -// object. However, in strict mode, "this" no longer defaults to the -// global object, so expose the global object explicitly. Babel's -// compiled output will use a variable called "global" if one is -// present. -// -// See https://bugzilla.mozilla.org/show_bug.cgi?id=1394556#c3 for -// more details. -const global = this; - -var EXPORTED_SYMBOLS = ["Kinto"]; - /* * Version 13.0.0 - 7fbf95d */ -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global = global || self, global.Kinto = factory()); -}(this, (function () { 'use strict'; - /** * Base db adapter. * @@ -2592,19 +2571,13 @@ class KintoBase { * See the License for the specific language governing permissions and * limitations under the License. */ -const { setTimeout, clearTimeout } = ChromeUtils.importESModule("resource://gre/modules/Timer.sys.mjs"); -const { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs"); -XPCOMUtils.defineLazyGlobalGetters(global, ["fetch", "indexedDB"]); -ChromeUtils.defineESModuleGetters(global, { +const lazy = {}; +ChromeUtils.defineESModuleGetters(lazy, { EventEmitter: "resource://gre/modules/EventEmitter.sys.mjs", // Use standalone kinto-http module landed in FFx. KintoHttpClient: "resource://services-common/kinto-http-client.sys.mjs" }); -ChromeUtils.defineLazyGetter(global, "generateUUID", () => { - const { generateUUID } = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator); - return generateUUID; -}); -class Kinto extends KintoBase { +export class Kinto extends KintoBase { static get adapters() { return { BaseAdapter, @@ -2612,11 +2585,11 @@ class Kinto extends KintoBase { }; } get ApiClass() { - return KintoHttpClient; + return lazy.KintoHttpClient; } constructor(options = {}) { const events = {}; - EventEmitter.decorate(events); + lazy.EventEmitter.decorate(events); const defaults = { adapter: IDB, events, @@ -2629,7 +2602,7 @@ class Kinto extends KintoBase { return typeof id == "string" && RE_RECORD_ID.test(id); }, generate() { - return generateUUID() + return Services.uuid.generateUUID() .toString() .replace(/[{}]/g, ""); }, @@ -2638,6 +2611,3 @@ class Kinto extends KintoBase { } } - return Kinto; - -}))); diff --git a/services/common/kinto-storage-adapter.sys.mjs b/services/common/kinto-storage-adapter.sys.mjs index 75c6c06a2693..e104a6cbd14e 100644 --- a/services/common/kinto-storage-adapter.sys.mjs +++ b/services/common/kinto-storage-adapter.sys.mjs @@ -13,9 +13,7 @@ */ import { Sqlite } from "resource://gre/modules/Sqlite.sys.mjs"; -const { Kinto } = ChromeUtils.import( - "resource://services-common/kinto-offline-client.js" -); +import { Kinto } from "resource://services-common/kinto-offline-client.sys.mjs"; /** * Filter and sort list against provided filters and order. diff --git a/services/common/moz.build b/services/common/moz.build index 3585dc542616..144ccaff047a 100644 --- a/services/common/moz.build +++ b/services/common/moz.build @@ -20,7 +20,7 @@ EXTRA_COMPONENTS += [ EXTRA_JS_MODULES["services-common"] += [ "async.sys.mjs", "kinto-http-client.sys.mjs", - "kinto-offline-client.js", + "kinto-offline-client.sys.mjs", "kinto-storage-adapter.sys.mjs", "logmanager.sys.mjs", "observers.sys.mjs", diff --git a/services/common/tests/unit/test_kinto.js b/services/common/tests/unit/test_kinto.js index 4b5e8471b1b3..154976d701c3 100644 --- a/services/common/tests/unit/test_kinto.js +++ b/services/common/tests/unit/test_kinto.js @@ -1,8 +1,8 @@ /* Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ */ -const { Kinto } = ChromeUtils.import( - "resource://services-common/kinto-offline-client.js" +const { Kinto } = ChromeUtils.importESModule( + "resource://services-common/kinto-offline-client.sys.mjs" ); const { FirefoxAdapter } = ChromeUtils.importESModule( "resource://services-common/kinto-storage-adapter.sys.mjs" diff --git a/services/settings/RemoteSettings.worker.mjs b/services/settings/RemoteSettings.worker.mjs index 66228f226ef8..bfffe9313d65 100644 --- a/services/settings/RemoteSettings.worker.mjs +++ b/services/settings/RemoteSettings.worker.mjs @@ -140,7 +140,7 @@ let gPendingTransactions = new Set(); /** * Import the records into the Remote Settings Chrome IndexedDB. * - * Note: This duplicates some logics from `kinto-offline-client.js`. + * Note: This duplicates some logics from `kinto-offline-client.sys.mjs`. * * @param {String} bucket * @param {String} collection diff --git a/toolkit/components/extensions/ExtensionStorageSyncKinto.sys.mjs b/toolkit/components/extensions/ExtensionStorageSyncKinto.sys.mjs index 4d58c73c02b2..ace6e16c2c0c 100644 --- a/toolkit/components/extensions/ExtensionStorageSyncKinto.sys.mjs +++ b/toolkit/components/extensions/ExtensionStorageSyncKinto.sys.mjs @@ -42,6 +42,7 @@ ChromeUtils.defineESModuleGetters(lazy, { CryptoUtils: "resource://services-crypto/utils.sys.mjs", ExtensionCommon: "resource://gre/modules/ExtensionCommon.sys.mjs", FirefoxAdapter: "resource://services-common/kinto-storage-adapter.sys.mjs", + Kinto: "resource://services-common/kinto-offline-client.sys.mjs", KintoHttpClient: "resource://services-common/kinto-http-client.sys.mjs", Observers: "resource://services-common/observers.sys.mjs", Utils: "resource://services-sync/util.sys.mjs", @@ -54,9 +55,6 @@ ChromeUtils.defineESModuleGetters(lazy, { * @typedef {any} KeyBundle * @typedef {any} SyncResultObject */ -XPCOMUtils.defineLazyModuleGetters(lazy, { - Kinto: "resource://services-common/kinto-offline-client.js", -}); ChromeUtils.defineLazyGetter(lazy, "fxAccounts", () => { return ChromeUtils.importESModule( diff --git a/tools/rewriting/ThirdPartyPaths.txt b/tools/rewriting/ThirdPartyPaths.txt index 0ed1387fa303..2f07ead2ea67 100644 --- a/tools/rewriting/ThirdPartyPaths.txt +++ b/tools/rewriting/ThirdPartyPaths.txt @@ -156,7 +156,7 @@ security/nss/ security/sandbox/chromium/ security/sandbox/chromium-shim/ services/common/kinto-http-client.sys.mjs -services/common/kinto-offline-client.js +services/common/kinto-offline-client.sys.mjs testing/gtest/gmock/ testing/gtest/gtest/ testing/mochitest/MochiKit/