@metamask/snaps-controllers 14.0.0 → 14.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,16 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ## [14.0.0]
10
+ ## [14.0.1]
11
11
 
12
- ### Changed
12
+ ### Fixed
13
13
 
14
- - **BREAKING:** Move `CronjobController` init to separate function ([#3507](https://github.com/MetaMask/snaps/pull/3507))
14
+ - Handle scheduled events close to current time gracefully ([#3510](https://github.com/MetaMask/snaps/pull/3510))
15
+
16
+ ## [14.0.0]
15
17
 
16
18
  ### Added
17
19
 
18
20
  - Add support for `onAssetsMarketData` handler ([#3496](https://github.com/MetaMask/snaps/pull/3496))
19
21
 
22
+ ### Changed
23
+
24
+ - **BREAKING:** Move `CronjobController` init to separate function ([#3507](https://github.com/MetaMask/snaps/pull/3507))
25
+
20
26
  ## [13.1.1]
21
27
 
22
28
  ### Fixed
@@ -836,7 +842,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
836
842
  - The version of the package no longer needs to match the version of all other
837
843
  MetaMask Snaps packages.
838
844
 
839
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@14.0.0...HEAD
845
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@14.0.1...HEAD
846
+ [14.0.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@14.0.0...@metamask/snaps-controllers@14.0.1
840
847
  [14.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@13.1.1...@metamask/snaps-controllers@14.0.0
841
848
  [13.1.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@13.1.0...@metamask/snaps-controllers@13.1.1
842
849
  [13.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@13.0.0...@metamask/snaps-controllers@13.1.0
@@ -191,6 +191,12 @@ class CronjobController extends base_controller_1.BaseController {
191
191
  if (ms > exports.DAILY_TIMEOUT) {
192
192
  return;
193
193
  }
194
+ // When an event is supposed to be scheduled close to the current time
195
+ // we may end up needing to execute immediately instead.
196
+ if (ms <= 0) {
197
+ this.#execute(event);
198
+ return;
199
+ }
194
200
  const timer = new Timer_1.Timer(ms);
195
201
  timer.start(() => {
196
202
  this.#execute(event);
@@ -1 +1 @@
1
- {"version":3,"file":"CronjobController.cjs","sourceRoot":"","sources":["../../src/cronjob/CronjobController.ts"],"names":[],"mappings":";;;AAKA,+DAA2D;AAE3D,mEAGqC;AAGrC,uDAI+B;AAC/B,2CAAmE;AACnE,iCAAkC;AAClC,iCAAiC;AACjC,mCAAgC;AAEhC,uCAA4E;AAS5E,sDAAqD;AACrD,8CAAuC;AA4D1B,QAAA,aAAa,GAAG,IAAA,sBAAc,EAAC,EAAE,EAAE,gBAAQ,CAAC,IAAI,CAAC,CAAC;AAoD/D,MAAM,cAAc,GAAG,mBAAmB,CAAC;AAE3C;;;;GAIG;AACH,MAAa,iBAAkB,SAAQ,gCAItC;IACU,OAAO,CAAqB;IAErC,WAAW,GAAU,IAAI,aAAK,CAAC,qBAAa,CAAC,CAAC;IAE9C,YAAY,EAAE,SAAS,EAAE,KAAK,EAAyB;QACrD,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE;gBACR,MAAM,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE;aAC5C;YACD,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE;gBACL,MAAM,EAAE,EAAE;gBACV,GAAG,KAAK;aACT;SACF,CAAC,CAAC;QAEH,IAAI,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;QAEzB,IAAI,CAAC,eAAe,CAAC,SAAS,CAC5B,8BAA8B,EAC9B,IAAI,CAAC,yBAAyB,CAC/B,CAAC;QAEF,IAAI,CAAC,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MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAEvB,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;YACrD,MAAM,SAAS,GAAG,gBAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE;gBAC7C,OAAO,EAAE,IAAI;aACd,CAAC;iBACC,KAAK,EAAE;iBACP,QAAQ,EAAE,CAAC;YAEd,IAAI,CAAC,KAAK,CAAC,SAAS,IAAI,SAAS,GAAG,GAAG,EAAE,CAAC;gBACxC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACzB,CAAC;QACH,CAAC;IACH,CAAC;CACF;AA7cD,8CA6cC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { GetPermissions } from '@metamask/permission-controller';\nimport {\n getCronjobCaveatJobs,\n SnapEndowments,\n} from '@metamask/snaps-rpc-methods';\nimport type { BackgroundEvent, SnapId } from '@metamask/snaps-sdk';\nimport type { TruncatedSnap } from '@metamask/snaps-utils';\nimport {\n toCensoredISO8601String,\n HandlerType,\n logError,\n} from '@metamask/snaps-utils';\nimport { assert, Duration, inMilliseconds } from '@metamask/utils';\nimport { castDraft } from 'immer';\nimport { DateTime } from 'luxon';\nimport { nanoid } from 'nanoid';\n\nimport { getCronjobSpecificationSchedule, getExecutionDate } from './utils';\nimport type {\n HandleSnapRequest,\n SnapDisabled,\n SnapEnabled,\n SnapInstalled,\n SnapUninstalled,\n SnapUpdated,\n} from '..';\nimport { METAMASK_ORIGIN } from '../snaps/constants';\nimport { Timer } from '../snaps/Timer';\n\nexport type CronjobControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n CronjobControllerState\n>;\nexport type CronjobControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n CronjobControllerState\n>;\n\n/**\n * Initialise the CronjobController. This should be called after all controllers\n * are created.\n */\nexport type CronjobControllerInitAction = {\n type: `${typeof controllerName}:init`;\n handler: CronjobController['init'];\n};\n\nexport type Schedule = {\n type: `${typeof controllerName}:schedule`;\n handler: CronjobController['schedule'];\n};\n\nexport type Cancel = {\n type: `${typeof controllerName}:cancel`;\n handler: CronjobController['cancel'];\n};\n\nexport type Get = {\n type: `${typeof controllerName}:get`;\n handler: CronjobController['get'];\n};\n\nexport type CronjobControllerActions =\n | CronjobControllerGetStateAction\n | HandleSnapRequest\n | GetPermissions\n | Schedule\n | Cancel\n | Get\n | CronjobControllerInitAction;\n\nexport type CronjobControllerEvents =\n | CronjobControllerStateChangeEvent\n | SnapInstalled\n | SnapUninstalled\n | SnapUpdated\n | SnapEnabled\n | SnapDisabled;\n\nexport type CronjobControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n CronjobControllerActions,\n CronjobControllerEvents,\n CronjobControllerActions['type'],\n CronjobControllerEvents['type']\n>;\n\nexport const DAILY_TIMEOUT = inMilliseconds(24, Duration.Hour);\n\nexport type CronjobControllerArgs = {\n messenger: CronjobControllerMessenger;\n\n /**\n * Persisted state that will be used for rehydration.\n */\n state?: CronjobControllerState;\n};\n\n/**\n * Represents a background event that is scheduled to be executed by the\n * cronjob controller.\n */\nexport type InternalBackgroundEvent = BackgroundEvent & {\n /**\n * Whether the event is recurring.\n */\n recurring: boolean;\n\n /**\n * The cron expression or ISO 8601 duration string that defines the event's\n * schedule.\n */\n schedule: string;\n};\n\n/**\n * A schedulable background event, which is a subset of the\n * {@link InternalBackgroundEvent} type, containing only the fields required to\n * schedule an event. Other fields will be populated by the cronjob controller\n * automatically.\n */\nexport type SchedulableBackgroundEvent = Omit<\n InternalBackgroundEvent,\n 'scheduledAt' | 'date' | 'id'\n> & {\n /**\n * The optional ID of the event. If not provided, a new ID will be\n * generated.\n */\n id?: string;\n};\n\nexport type CronjobControllerState = {\n /**\n * Background events and cronjobs that are scheduled to be executed.\n */\n events: Record<string, InternalBackgroundEvent>;\n};\n\nconst controllerName = 'CronjobController';\n\n/**\n * The cronjob controller is responsible for managing cronjobs and background\n * events for Snaps. It allows Snaps to schedule events that will be executed\n * at a later time.\n */\nexport class CronjobController extends BaseController<\n typeof controllerName,\n CronjobControllerState,\n CronjobControllerMessenger\n> {\n readonly #timers: Map<string, Timer>;\n\n #dailyTimer: Timer = new Timer(DAILY_TIMEOUT);\n\n constructor({ messenger, state }: CronjobControllerArgs) {\n super({\n messenger,\n metadata: {\n events: { persist: true, anonymous: false },\n },\n name: controllerName,\n state: {\n events: {},\n ...state,\n },\n });\n\n this.#timers = new Map();\n\n this.messagingSystem.subscribe(\n 'SnapController:snapInstalled',\n this.#handleSnapInstalledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapUninstalled',\n this.#handleSnapUninstalledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapEnabled',\n this.#handleSnapEnabledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapDisabled',\n this.#handleSnapDisabledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapUpdated',\n this.#handleSnapUpdatedEvent,\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:init`,\n (...args) => this.init(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:schedule`,\n (...args) => this.schedule(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:cancel`,\n (...args) => this.cancel(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:get`,\n (...args) => this.get(...args),\n );\n }\n\n /**\n * Initialize the CronjobController.\n *\n * This starts the daily timer, clears out expired events\n * and reschedules any remaining events.\n */\n init() {\n this.#start();\n this.#clear();\n this.#reschedule();\n }\n\n /**\n * Schedule a non-recurring background event.\n *\n * @param event - The event to schedule.\n * @returns The ID of the scheduled event.\n */\n schedule(event: Omit<SchedulableBackgroundEvent, 'recurring'>) {\n return this.#add({\n ...event,\n recurring: false,\n });\n }\n\n /**\n * Cancel an event.\n *\n * @param origin - The origin making the cancel call.\n * @param id - The id of the event to cancel.\n * @throws If the event does not exist.\n */\n cancel(origin: string, id: string) {\n assert(\n this.state.events[id],\n `A background event with the id of \"${id}\" does not exist.`,\n );\n\n assert(\n this.state.events[id].snapId === origin,\n 'Only the origin that scheduled this event can cancel it.',\n );\n\n this.#cancel(id);\n }\n\n /**\n * Get a list of a Snap's background events.\n *\n * @param snapId - The id of the Snap to fetch background events for.\n * @returns An array of background events.\n */\n get(snapId: SnapId): InternalBackgroundEvent[] {\n return Object.values(this.state.events)\n .filter(\n (snapEvent) => snapEvent.snapId === snapId && !snapEvent.recurring,\n )\n .map((event) => ({\n ...event,\n date: toCensoredISO8601String(event.date),\n scheduledAt: toCensoredISO8601String(event.scheduledAt),\n }));\n }\n\n /**\n * Register cronjobs for a given Snap by getting specification from the\n * permission caveats. Once registered, each job will be scheduled.\n *\n * @param snapId - The snap ID to register jobs for.\n */\n register(snapId: SnapId) {\n const jobs = this.#getSnapCronjobs(snapId);\n jobs?.forEach((job) => this.#add(job));\n }\n\n /**\n * Unregister all cronjobs and background events for a given Snap.\n *\n * @param snapId - ID of a snap.\n */\n unregister(snapId: SnapId) {\n for (const [id, event] of Object.entries(this.state.events)) {\n if (event.snapId === snapId) {\n this.#cancel(id);\n }\n }\n }\n\n /**\n * Run controller teardown process and unsubscribe from Snap events.\n */\n destroy() {\n super.destroy();\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapInstalled',\n this.#handleSnapInstalledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapUninstalled',\n this.#handleSnapUninstalledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapEnabled',\n this.#handleSnapEnabledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapDisabled',\n this.#handleSnapDisabledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapUpdated',\n this.#handleSnapUpdatedEvent,\n );\n\n // Cancel all timers and clear the map.\n this.#timers.forEach((timer) => timer.cancel());\n this.#timers.clear();\n\n if (this.#dailyTimer.status === 'running') {\n this.#dailyTimer.cancel();\n }\n }\n\n /**\n * Start the daily timer that will reschedule events every 24 hours.\n */\n #start() {\n this.#dailyTimer = new Timer(DAILY_TIMEOUT);\n this.#dailyTimer.start(() => {\n this.#reschedule();\n this.#start();\n });\n }\n\n /**\n * Add a cronjob or background event to the controller state and schedule it\n * for execution.\n *\n * @param event - The event to schedule.\n * @returns The ID of the scheduled event.\n */\n #add(event: SchedulableBackgroundEvent) {\n const id = event.id ?? nanoid();\n const internalEvent: InternalBackgroundEvent = {\n ...event,\n id,\n date: getExecutionDate(event.schedule),\n scheduledAt: new Date().toISOString(),\n };\n\n this.update((state) => {\n state.events[internalEvent.id] = castDraft(internalEvent);\n });\n\n this.#schedule(internalEvent);\n return id;\n }\n\n /**\n * Get the next execution date for a given event and start a timer for it.\n *\n * @param event - The event to schedule.\n */\n #schedule(event: InternalBackgroundEvent) {\n const date = getExecutionDate(event.schedule);\n this.update((state) => {\n state.events[event.id].date = date;\n });\n\n this.#startTimer({\n ...event,\n date,\n });\n }\n\n /**\n * Set up and start a timer for the given event.\n *\n * @param event - The event to schedule.\n * @throws If the event is scheduled in the past.\n */\n #startTimer(event: InternalBackgroundEvent) {\n const ms =\n DateTime.fromISO(event.date, { setZone: true }).toMillis() - Date.now();\n\n // We don't schedule this job yet as it is too far in the future.\n if (ms > DAILY_TIMEOUT) {\n return;\n }\n\n const timer = new Timer(ms);\n timer.start(() => {\n this.#execute(event);\n });\n\n this.#timers.set(event.id, timer);\n }\n\n /**\n * Execute a background event. This method is called when the event's timer\n * expires.\n *\n * If the event is not recurring, it will be removed from the state after\n * execution. If it is recurring, it will be rescheduled.\n *\n * @param event - The event to execute.\n */\n #execute(event: InternalBackgroundEvent) {\n this.messagingSystem\n .call('SnapController:handleRequest', {\n snapId: event.snapId,\n origin: METAMASK_ORIGIN,\n handler: HandlerType.OnCronjob,\n request: event.request,\n })\n .catch((error) => {\n logError(\n `An error occurred while executing an event for Snap \"${event.snapId}\":`,\n error,\n );\n });\n\n this.#timers.delete(event.id);\n\n // Non-recurring events are removed from the state after execution, and\n // recurring events are rescheduled.\n if (!event.recurring) {\n this.update((state) => {\n delete state.events[event.id];\n });\n\n return;\n }\n\n this.#schedule(event);\n }\n\n /**\n * Cancel a background event by its ID. Unlike {@link cancel}, this method\n * does not check the origin of the event, so it can be used internally.\n *\n * @param id - The ID of the background event to cancel.\n */\n #cancel(id: string) {\n const timer = this.#timers.get(id);\n timer?.cancel();\n this.#timers.delete(id);\n\n this.update((state) => {\n delete state.events[id];\n });\n }\n\n /**\n * Retrieve all cronjob specifications for a Snap.\n *\n * @param snapId - ID of a Snap.\n * @returns Array of cronjob specifications.\n */\n #getSnapCronjobs(snapId: SnapId): SchedulableBackgroundEvent[] {\n const permissions = this.messagingSystem.call(\n 'PermissionController:getPermissions',\n snapId,\n );\n\n const permission = permissions?.[SnapEndowments.Cronjob];\n const definitions = getCronjobCaveatJobs(permission);\n\n if (!definitions) {\n return [];\n }\n\n return definitions.map((definition, idx) => {\n return {\n snapId,\n id: `cronjob-${snapId}-${idx}`,\n request: definition.request,\n schedule: getCronjobSpecificationSchedule(definition),\n recurring: true,\n };\n });\n }\n\n /**\n * Handle events that should cause cron jobs to be registered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapInstalledEvent = (snap: TruncatedSnap) => {\n this.register(snap.id);\n };\n\n /**\n * Handle the Snap enabled event. This checks if the Snap has any cronjobs or\n * background events that need to be rescheduled.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapEnabledEvent = (snap: TruncatedSnap) => {\n const events = this.get(snap.id);\n this.#reschedule(events);\n this.register(snap.id);\n };\n\n /**\n * Handle events that should cause cronjobs and background events to be\n * unregistered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapUninstalledEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n };\n\n /**\n * Handle events that should cause cronjobs and background events to be\n * unregistered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapDisabledEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n };\n\n /**\n * Handle cron jobs on 'snapUpdated' event.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapUpdatedEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n this.register(snap.id);\n };\n\n /**\n * Reschedule events that are yet to be executed. This should be called on\n * controller initialization and once every 24 hours to ensure that\n * background events are scheduled correctly.\n *\n * @param events - An array of events to reschedule. Defaults to all events in\n * the controller state.\n */\n #reschedule(events = Object.values(this.state.events)) {\n const now = Date.now();\n\n for (const event of events) {\n if (this.#timers.has(event.id)) {\n // If the timer for this event already exists, we don't need to\n // reschedule it.\n continue;\n }\n\n const eventDate = DateTime.fromISO(event.date, {\n setZone: true,\n })\n .toUTC()\n .toMillis();\n\n // If the event is recurring and the date is in the past, execute it\n // immediately.\n if (event.recurring && eventDate <= now) {\n this.#execute(event);\n }\n\n this.#schedule(event);\n }\n }\n\n /**\n * Clear non-recurring events that are past their scheduled time.\n */\n #clear() {\n const now = Date.now();\n\n for (const event of Object.values(this.state.events)) {\n const eventDate = DateTime.fromISO(event.date, {\n setZone: true,\n })\n .toUTC()\n .toMillis();\n\n if (!event.recurring && eventDate < now) {\n this.#cancel(event.id);\n }\n }\n }\n}\n"]}
1
+ {"version":3,"file":"CronjobController.cjs","sourceRoot":"","sources":["../../src/cronjob/CronjobController.ts"],"names":[],"mappings":";;;AAKA,+DAA2D;AAE3D,mEAGqC;AAGrC,uDAI+B;AAC/B,2CAAmE;AACnE,iCAAkC;AAClC,iCAAiC;AACjC,mCAAgC;AAEhC,uCAA4E;AAS5E,sDAAqD;AACrD,8CAAuC;AA4D1B,QAAA,aAAa,GAAG,IAAA,sBAAc,EAAC,EAAE,EAAE,gBAAQ,CAAC,IAAI,CAAC,CAAC;AAo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CAodC","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { GetPermissions } from '@metamask/permission-controller';\nimport {\n getCronjobCaveatJobs,\n SnapEndowments,\n} from '@metamask/snaps-rpc-methods';\nimport type { BackgroundEvent, SnapId } from '@metamask/snaps-sdk';\nimport type { TruncatedSnap } from '@metamask/snaps-utils';\nimport {\n toCensoredISO8601String,\n HandlerType,\n logError,\n} from '@metamask/snaps-utils';\nimport { assert, Duration, inMilliseconds } from '@metamask/utils';\nimport { castDraft } from 'immer';\nimport { DateTime } from 'luxon';\nimport { nanoid } from 'nanoid';\n\nimport { getCronjobSpecificationSchedule, getExecutionDate } from './utils';\nimport type {\n HandleSnapRequest,\n SnapDisabled,\n SnapEnabled,\n SnapInstalled,\n SnapUninstalled,\n SnapUpdated,\n} from '..';\nimport { METAMASK_ORIGIN } from '../snaps/constants';\nimport { Timer } from '../snaps/Timer';\n\nexport type CronjobControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n CronjobControllerState\n>;\nexport type CronjobControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n CronjobControllerState\n>;\n\n/**\n * Initialise the CronjobController. This should be called after all controllers\n * are created.\n */\nexport type CronjobControllerInitAction = {\n type: `${typeof controllerName}:init`;\n handler: CronjobController['init'];\n};\n\nexport type Schedule = {\n type: `${typeof controllerName}:schedule`;\n handler: CronjobController['schedule'];\n};\n\nexport type Cancel = {\n type: `${typeof controllerName}:cancel`;\n handler: CronjobController['cancel'];\n};\n\nexport type Get = {\n type: `${typeof controllerName}:get`;\n handler: CronjobController['get'];\n};\n\nexport type CronjobControllerActions =\n | CronjobControllerGetStateAction\n | HandleSnapRequest\n | GetPermissions\n | Schedule\n | Cancel\n | Get\n | CronjobControllerInitAction;\n\nexport type CronjobControllerEvents =\n | CronjobControllerStateChangeEvent\n | SnapInstalled\n | SnapUninstalled\n | SnapUpdated\n | SnapEnabled\n | SnapDisabled;\n\nexport type CronjobControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n CronjobControllerActions,\n CronjobControllerEvents,\n CronjobControllerActions['type'],\n CronjobControllerEvents['type']\n>;\n\nexport const DAILY_TIMEOUT = inMilliseconds(24, Duration.Hour);\n\nexport type CronjobControllerArgs = {\n messenger: CronjobControllerMessenger;\n\n /**\n * Persisted state that will be used for rehydration.\n */\n state?: CronjobControllerState;\n};\n\n/**\n * Represents a background event that is scheduled to be executed by the\n * cronjob controller.\n */\nexport type InternalBackgroundEvent = BackgroundEvent & {\n /**\n * Whether the event is recurring.\n */\n recurring: boolean;\n\n /**\n * The cron expression or ISO 8601 duration string that defines the event's\n * schedule.\n */\n schedule: string;\n};\n\n/**\n * A schedulable background event, which is a subset of the\n * {@link InternalBackgroundEvent} type, containing only the fields required to\n * schedule an event. Other fields will be populated by the cronjob controller\n * automatically.\n */\nexport type SchedulableBackgroundEvent = Omit<\n InternalBackgroundEvent,\n 'scheduledAt' | 'date' | 'id'\n> & {\n /**\n * The optional ID of the event. If not provided, a new ID will be\n * generated.\n */\n id?: string;\n};\n\nexport type CronjobControllerState = {\n /**\n * Background events and cronjobs that are scheduled to be executed.\n */\n events: Record<string, InternalBackgroundEvent>;\n};\n\nconst controllerName = 'CronjobController';\n\n/**\n * The cronjob controller is responsible for managing cronjobs and background\n * events for Snaps. It allows Snaps to schedule events that will be executed\n * at a later time.\n */\nexport class CronjobController extends BaseController<\n typeof controllerName,\n CronjobControllerState,\n CronjobControllerMessenger\n> {\n readonly #timers: Map<string, Timer>;\n\n #dailyTimer: Timer = new Timer(DAILY_TIMEOUT);\n\n constructor({ messenger, state }: CronjobControllerArgs) {\n super({\n messenger,\n metadata: {\n events: { persist: true, anonymous: false },\n },\n name: controllerName,\n state: {\n events: {},\n ...state,\n },\n });\n\n this.#timers = new Map();\n\n this.messagingSystem.subscribe(\n 'SnapController:snapInstalled',\n this.#handleSnapInstalledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapUninstalled',\n this.#handleSnapUninstalledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapEnabled',\n this.#handleSnapEnabledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapDisabled',\n this.#handleSnapDisabledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapUpdated',\n this.#handleSnapUpdatedEvent,\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:init`,\n (...args) => this.init(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:schedule`,\n (...args) => this.schedule(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:cancel`,\n (...args) => this.cancel(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:get`,\n (...args) => this.get(...args),\n );\n }\n\n /**\n * Initialize the CronjobController.\n *\n * This starts the daily timer, clears out expired events\n * and reschedules any remaining events.\n */\n init() {\n this.#start();\n this.#clear();\n this.#reschedule();\n }\n\n /**\n * Schedule a non-recurring background event.\n *\n * @param event - The event to schedule.\n * @returns The ID of the scheduled event.\n */\n schedule(event: Omit<SchedulableBackgroundEvent, 'recurring'>) {\n return this.#add({\n ...event,\n recurring: false,\n });\n }\n\n /**\n * Cancel an event.\n *\n * @param origin - The origin making the cancel call.\n * @param id - The id of the event to cancel.\n * @throws If the event does not exist.\n */\n cancel(origin: string, id: string) {\n assert(\n this.state.events[id],\n `A background event with the id of \"${id}\" does not exist.`,\n );\n\n assert(\n this.state.events[id].snapId === origin,\n 'Only the origin that scheduled this event can cancel it.',\n );\n\n this.#cancel(id);\n }\n\n /**\n * Get a list of a Snap's background events.\n *\n * @param snapId - The id of the Snap to fetch background events for.\n * @returns An array of background events.\n */\n get(snapId: SnapId): InternalBackgroundEvent[] {\n return Object.values(this.state.events)\n .filter(\n (snapEvent) => snapEvent.snapId === snapId && !snapEvent.recurring,\n )\n .map((event) => ({\n ...event,\n date: toCensoredISO8601String(event.date),\n scheduledAt: toCensoredISO8601String(event.scheduledAt),\n }));\n }\n\n /**\n * Register cronjobs for a given Snap by getting specification from the\n * permission caveats. Once registered, each job will be scheduled.\n *\n * @param snapId - The snap ID to register jobs for.\n */\n register(snapId: SnapId) {\n const jobs = this.#getSnapCronjobs(snapId);\n jobs?.forEach((job) => this.#add(job));\n }\n\n /**\n * Unregister all cronjobs and background events for a given Snap.\n *\n * @param snapId - ID of a snap.\n */\n unregister(snapId: SnapId) {\n for (const [id, event] of Object.entries(this.state.events)) {\n if (event.snapId === snapId) {\n this.#cancel(id);\n }\n }\n }\n\n /**\n * Run controller teardown process and unsubscribe from Snap events.\n */\n destroy() {\n super.destroy();\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapInstalled',\n this.#handleSnapInstalledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapUninstalled',\n this.#handleSnapUninstalledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapEnabled',\n this.#handleSnapEnabledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapDisabled',\n this.#handleSnapDisabledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapUpdated',\n this.#handleSnapUpdatedEvent,\n );\n\n // Cancel all timers and clear the map.\n this.#timers.forEach((timer) => timer.cancel());\n this.#timers.clear();\n\n if (this.#dailyTimer.status === 'running') {\n this.#dailyTimer.cancel();\n }\n }\n\n /**\n * Start the daily timer that will reschedule events every 24 hours.\n */\n #start() {\n this.#dailyTimer = new Timer(DAILY_TIMEOUT);\n this.#dailyTimer.start(() => {\n this.#reschedule();\n this.#start();\n });\n }\n\n /**\n * Add a cronjob or background event to the controller state and schedule it\n * for execution.\n *\n * @param event - The event to schedule.\n * @returns The ID of the scheduled event.\n */\n #add(event: SchedulableBackgroundEvent) {\n const id = event.id ?? nanoid();\n const internalEvent: InternalBackgroundEvent = {\n ...event,\n id,\n date: getExecutionDate(event.schedule),\n scheduledAt: new Date().toISOString(),\n };\n\n this.update((state) => {\n state.events[internalEvent.id] = castDraft(internalEvent);\n });\n\n this.#schedule(internalEvent);\n return id;\n }\n\n /**\n * Get the next execution date for a given event and start a timer for it.\n *\n * @param event - The event to schedule.\n */\n #schedule(event: InternalBackgroundEvent) {\n const date = getExecutionDate(event.schedule);\n this.update((state) => {\n state.events[event.id].date = date;\n });\n\n this.#startTimer({\n ...event,\n date,\n });\n }\n\n /**\n * Set up and start a timer for the given event.\n *\n * @param event - The event to schedule.\n * @throws If the event is scheduled in the past.\n */\n #startTimer(event: InternalBackgroundEvent) {\n const ms =\n DateTime.fromISO(event.date, { setZone: true }).toMillis() - Date.now();\n\n // We don't schedule this job yet as it is too far in the future.\n if (ms > DAILY_TIMEOUT) {\n return;\n }\n\n // When an event is supposed to be scheduled close to the current time\n // we may end up needing to execute immediately instead.\n if (ms <= 0) {\n this.#execute(event);\n return;\n }\n\n const timer = new Timer(ms);\n timer.start(() => {\n this.#execute(event);\n });\n\n this.#timers.set(event.id, timer);\n }\n\n /**\n * Execute a background event. This method is called when the event's timer\n * expires.\n *\n * If the event is not recurring, it will be removed from the state after\n * execution. If it is recurring, it will be rescheduled.\n *\n * @param event - The event to execute.\n */\n #execute(event: InternalBackgroundEvent) {\n this.messagingSystem\n .call('SnapController:handleRequest', {\n snapId: event.snapId,\n origin: METAMASK_ORIGIN,\n handler: HandlerType.OnCronjob,\n request: event.request,\n })\n .catch((error) => {\n logError(\n `An error occurred while executing an event for Snap \"${event.snapId}\":`,\n error,\n );\n });\n\n this.#timers.delete(event.id);\n\n // Non-recurring events are removed from the state after execution, and\n // recurring events are rescheduled.\n if (!event.recurring) {\n this.update((state) => {\n delete state.events[event.id];\n });\n\n return;\n }\n\n this.#schedule(event);\n }\n\n /**\n * Cancel a background event by its ID. Unlike {@link cancel}, this method\n * does not check the origin of the event, so it can be used internally.\n *\n * @param id - The ID of the background event to cancel.\n */\n #cancel(id: string) {\n const timer = this.#timers.get(id);\n timer?.cancel();\n this.#timers.delete(id);\n\n this.update((state) => {\n delete state.events[id];\n });\n }\n\n /**\n * Retrieve all cronjob specifications for a Snap.\n *\n * @param snapId - ID of a Snap.\n * @returns Array of cronjob specifications.\n */\n #getSnapCronjobs(snapId: SnapId): SchedulableBackgroundEvent[] {\n const permissions = this.messagingSystem.call(\n 'PermissionController:getPermissions',\n snapId,\n );\n\n const permission = permissions?.[SnapEndowments.Cronjob];\n const definitions = getCronjobCaveatJobs(permission);\n\n if (!definitions) {\n return [];\n }\n\n return definitions.map((definition, idx) => {\n return {\n snapId,\n id: `cronjob-${snapId}-${idx}`,\n request: definition.request,\n schedule: getCronjobSpecificationSchedule(definition),\n recurring: true,\n };\n });\n }\n\n /**\n * Handle events that should cause cron jobs to be registered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapInstalledEvent = (snap: TruncatedSnap) => {\n this.register(snap.id);\n };\n\n /**\n * Handle the Snap enabled event. This checks if the Snap has any cronjobs or\n * background events that need to be rescheduled.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapEnabledEvent = (snap: TruncatedSnap) => {\n const events = this.get(snap.id);\n this.#reschedule(events);\n this.register(snap.id);\n };\n\n /**\n * Handle events that should cause cronjobs and background events to be\n * unregistered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapUninstalledEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n };\n\n /**\n * Handle events that should cause cronjobs and background events to be\n * unregistered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapDisabledEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n };\n\n /**\n * Handle cron jobs on 'snapUpdated' event.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapUpdatedEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n this.register(snap.id);\n };\n\n /**\n * Reschedule events that are yet to be executed. This should be called on\n * controller initialization and once every 24 hours to ensure that\n * background events are scheduled correctly.\n *\n * @param events - An array of events to reschedule. Defaults to all events in\n * the controller state.\n */\n #reschedule(events = Object.values(this.state.events)) {\n const now = Date.now();\n\n for (const event of events) {\n if (this.#timers.has(event.id)) {\n // If the timer for this event already exists, we don't need to\n // reschedule it.\n continue;\n }\n\n const eventDate = DateTime.fromISO(event.date, {\n setZone: true,\n })\n .toUTC()\n .toMillis();\n\n // If the event is recurring and the date is in the past, execute it\n // immediately.\n if (event.recurring && eventDate <= now) {\n this.#execute(event);\n }\n\n this.#schedule(event);\n }\n }\n\n /**\n * Clear non-recurring events that are past their scheduled time.\n */\n #clear() {\n const now = Date.now();\n\n for (const event of Object.values(this.state.events)) {\n const eventDate = DateTime.fromISO(event.date, {\n setZone: true,\n })\n .toUTC()\n .toMillis();\n\n if (!event.recurring && eventDate < now) {\n this.#cancel(event.id);\n }\n }\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"CronjobController.d.cts","sourceRoot":"","sources":["../../src/cronjob/CronjobController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,cAAc,EAAE,wCAAwC;AAKtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,4BAA4B;AAanE,OAAO,KAAK,EACV,iBAAiB,EACjB,YAAY,EACZ,WAAW,EACX,aAAa,EACb,eAAe,EACf,WAAW,EACZ,qBAAW;AAIZ,MAAM,MAAM,+BAA+B,GAAG,wBAAwB,CACpE,OAAO,cAAc,EACrB,sBAAsB,CACvB,CAAC;AACF,MAAM,MAAM,iCAAiC,GAAG,0BAA0B,CACxE,OAAO,cAAc,EACrB,sBAAsB,CACvB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,2BAA2B,GAAG;IACxC,IAAI,EAAE,GAAG,OAAO,cAAc,OAAO,CAAC;IACtC,OAAO,EAAE,iBAAiB,CAAC,MAAM,CAAC,CAAC;CAC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BAAkB,SAAQ,cAAc,CACnD,OAAO,cAAc,EACrB,sBAAsB,EACtB,0BAA0B,CAC3B;;gBAKa,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,qBAAqB;IA6DvD;;;;;OAKG;IACH,IAAI;IAMJ;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,0BAA0B,EAAE,WAAW,CAAC;IAO7D;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;IAcjC;;;;;OAKG;IACH,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,uBAAuB,EAAE;IAY9C;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM;IAKvB;;;;OAIG;IACH,UAAU,CAAC,MAAM,EAAE,MAAM;IAQzB;;OAEG;IACH,OAAO;CAmTR"}
@@ -188,6 +188,12 @@ export class CronjobController extends BaseController {
188
188
  if (ms > DAILY_TIMEOUT) {
189
189
  return;
190
190
  }
191
+ // When an event is supposed to be scheduled close to the current time
192
+ // we may end up needing to execute immediately instead.
193
+ if (ms <= 0) {
194
+ this.#execute(event);
195
+ return;
196
+ }
191
197
  const timer = new Timer(ms);
192
198
  timer.start(() => {
193
199
  this.#execute(event);
@@ -1 +1 @@
1
- {"version":3,"file":"CronjobController.mjs","sourceRoot":"","sources":["../../src/cronjob/CronjobController.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,EACL,oBAAoB,EACpB,cAAc,EACf,oCAAoC;AAGrC,OAAO,EACL,uBAAuB,EACvB,WAAW,EACX,QAAQ,EACT,8BAA8B;AAC/B,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,wBAAwB;AACnE,OAAO,EAAE,SAAS,EAAE,cAAc;AAClC,OAAO,EAAE,QAAQ,EAAE,cAAc;AACjC,OAAO,EAAE,MAAM,EAAE,eAAe;AAEhC,OAAO,EAAE,+BAA+B,E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edMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { GetPermissions } from '@metamask/permission-controller';\nimport {\n getCronjobCaveatJobs,\n SnapEndowments,\n} from '@metamask/snaps-rpc-methods';\nimport type { BackgroundEvent, SnapId } from '@metamask/snaps-sdk';\nimport type { TruncatedSnap } from '@metamask/snaps-utils';\nimport {\n toCensoredISO8601String,\n HandlerType,\n logError,\n} from '@metamask/snaps-utils';\nimport { assert, Duration, inMilliseconds } from '@metamask/utils';\nimport { castDraft } from 'immer';\nimport { DateTime } from 'luxon';\nimport { nanoid } from 'nanoid';\n\nimport { getCronjobSpecificationSchedule, getExecutionDate } from './utils';\nimport type {\n HandleSnapRequest,\n SnapDisabled,\n SnapEnabled,\n SnapInstalled,\n SnapUninstalled,\n SnapUpdated,\n} from '..';\nimport { METAMASK_ORIGIN } from '../snaps/constants';\nimport { Timer } from '../snaps/Timer';\n\nexport type CronjobControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n CronjobControllerState\n>;\nexport type CronjobControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n CronjobControllerState\n>;\n\n/**\n * Initialise the CronjobController. This should be called after all controllers\n * are created.\n */\nexport type CronjobControllerInitAction = {\n type: `${typeof controllerName}:init`;\n handler: CronjobController['init'];\n};\n\nexport type Schedule = {\n type: `${typeof controllerName}:schedule`;\n handler: CronjobController['schedule'];\n};\n\nexport type Cancel = {\n type: `${typeof controllerName}:cancel`;\n handler: CronjobController['cancel'];\n};\n\nexport type Get = {\n type: `${typeof controllerName}:get`;\n handler: CronjobController['get'];\n};\n\nexport type CronjobControllerActions =\n | CronjobControllerGetStateAction\n | HandleSnapRequest\n | GetPermissions\n | Schedule\n | Cancel\n | Get\n | CronjobControllerInitAction;\n\nexport type CronjobControllerEvents =\n | CronjobControllerStateChangeEvent\n | SnapInstalled\n | SnapUninstalled\n | SnapUpdated\n | SnapEnabled\n | SnapDisabled;\n\nexport type CronjobControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n CronjobControllerActions,\n CronjobControllerEvents,\n CronjobControllerActions['type'],\n CronjobControllerEvents['type']\n>;\n\nexport const DAILY_TIMEOUT = inMilliseconds(24, Duration.Hour);\n\nexport type CronjobControllerArgs = {\n messenger: CronjobControllerMessenger;\n\n /**\n * Persisted state that will be used for rehydration.\n */\n state?: CronjobControllerState;\n};\n\n/**\n * Represents a background event that is scheduled to be executed by the\n * cronjob controller.\n */\nexport type InternalBackgroundEvent = BackgroundEvent & {\n /**\n * Whether the event is recurring.\n */\n recurring: boolean;\n\n /**\n * The cron expression or ISO 8601 duration string that defines the event's\n * schedule.\n */\n schedule: string;\n};\n\n/**\n * A schedulable background event, which is a subset of the\n * {@link InternalBackgroundEvent} type, containing only the fields required to\n * schedule an event. Other fields will be populated by the cronjob controller\n * automatically.\n */\nexport type SchedulableBackgroundEvent = Omit<\n InternalBackgroundEvent,\n 'scheduledAt' | 'date' | 'id'\n> & {\n /**\n * The optional ID of the event. If not provided, a new ID will be\n * generated.\n */\n id?: string;\n};\n\nexport type CronjobControllerState = {\n /**\n * Background events and cronjobs that are scheduled to be executed.\n */\n events: Record<string, InternalBackgroundEvent>;\n};\n\nconst controllerName = 'CronjobController';\n\n/**\n * The cronjob controller is responsible for managing cronjobs and background\n * events for Snaps. It allows Snaps to schedule events that will be executed\n * at a later time.\n */\nexport class CronjobController extends BaseController<\n typeof controllerName,\n CronjobControllerState,\n CronjobControllerMessenger\n> {\n readonly #timers: Map<string, Timer>;\n\n #dailyTimer: Timer = new Timer(DAILY_TIMEOUT);\n\n constructor({ messenger, state }: CronjobControllerArgs) {\n super({\n messenger,\n metadata: {\n events: { persist: true, anonymous: false },\n },\n name: controllerName,\n state: {\n events: {},\n ...state,\n },\n });\n\n this.#timers = new Map();\n\n this.messagingSystem.subscribe(\n 'SnapController:snapInstalled',\n this.#handleSnapInstalledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapUninstalled',\n this.#handleSnapUninstalledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapEnabled',\n this.#handleSnapEnabledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapDisabled',\n this.#handleSnapDisabledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapUpdated',\n this.#handleSnapUpdatedEvent,\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:init`,\n (...args) => this.init(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:schedule`,\n (...args) => this.schedule(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:cancel`,\n (...args) => this.cancel(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:get`,\n (...args) => this.get(...args),\n );\n }\n\n /**\n * Initialize the CronjobController.\n *\n * This starts the daily timer, clears out expired events\n * and reschedules any remaining events.\n */\n init() {\n this.#start();\n this.#clear();\n this.#reschedule();\n }\n\n /**\n * Schedule a non-recurring background event.\n *\n * @param event - The event to schedule.\n * @returns The ID of the scheduled event.\n */\n schedule(event: Omit<SchedulableBackgroundEvent, 'recurring'>) {\n return this.#add({\n ...event,\n recurring: false,\n });\n }\n\n /**\n * Cancel an event.\n *\n * @param origin - The origin making the cancel call.\n * @param id - The id of the event to cancel.\n * @throws If the event does not exist.\n */\n cancel(origin: string, id: string) {\n assert(\n this.state.events[id],\n `A background event with the id of \"${id}\" does not exist.`,\n );\n\n assert(\n this.state.events[id].snapId === origin,\n 'Only the origin that scheduled this event can cancel it.',\n );\n\n this.#cancel(id);\n }\n\n /**\n * Get a list of a Snap's background events.\n *\n * @param snapId - The id of the Snap to fetch background events for.\n * @returns An array of background events.\n */\n get(snapId: SnapId): InternalBackgroundEvent[] {\n return Object.values(this.state.events)\n .filter(\n (snapEvent) => snapEvent.snapId === snapId && !snapEvent.recurring,\n )\n .map((event) => ({\n ...event,\n date: toCensoredISO8601String(event.date),\n scheduledAt: toCensoredISO8601String(event.scheduledAt),\n }));\n }\n\n /**\n * Register cronjobs for a given Snap by getting specification from the\n * permission caveats. Once registered, each job will be scheduled.\n *\n * @param snapId - The snap ID to register jobs for.\n */\n register(snapId: SnapId) {\n const jobs = this.#getSnapCronjobs(snapId);\n jobs?.forEach((job) => this.#add(job));\n }\n\n /**\n * Unregister all cronjobs and background events for a given Snap.\n *\n * @param snapId - ID of a snap.\n */\n unregister(snapId: SnapId) {\n for (const [id, event] of Object.entries(this.state.events)) {\n if (event.snapId === snapId) {\n this.#cancel(id);\n }\n }\n }\n\n /**\n * Run controller teardown process and unsubscribe from Snap events.\n */\n destroy() {\n super.destroy();\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapInstalled',\n this.#handleSnapInstalledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapUninstalled',\n this.#handleSnapUninstalledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapEnabled',\n this.#handleSnapEnabledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapDisabled',\n this.#handleSnapDisabledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapUpdated',\n this.#handleSnapUpdatedEvent,\n );\n\n // Cancel all timers and clear the map.\n this.#timers.forEach((timer) => timer.cancel());\n this.#timers.clear();\n\n if (this.#dailyTimer.status === 'running') {\n this.#dailyTimer.cancel();\n }\n }\n\n /**\n * Start the daily timer that will reschedule events every 24 hours.\n */\n #start() {\n this.#dailyTimer = new Timer(DAILY_TIMEOUT);\n this.#dailyTimer.start(() => {\n this.#reschedule();\n this.#start();\n });\n }\n\n /**\n * Add a cronjob or background event to the controller state and schedule it\n * for execution.\n *\n * @param event - The event to schedule.\n * @returns The ID of the scheduled event.\n */\n #add(event: SchedulableBackgroundEvent) {\n const id = event.id ?? nanoid();\n const internalEvent: InternalBackgroundEvent = {\n ...event,\n id,\n date: getExecutionDate(event.schedule),\n scheduledAt: new Date().toISOString(),\n };\n\n this.update((state) => {\n state.events[internalEvent.id] = castDraft(internalEvent);\n });\n\n this.#schedule(internalEvent);\n return id;\n }\n\n /**\n * Get the next execution date for a given event and start a timer for it.\n *\n * @param event - The event to schedule.\n */\n #schedule(event: InternalBackgroundEvent) {\n const date = getExecutionDate(event.schedule);\n this.update((state) => {\n state.events[event.id].date = date;\n });\n\n this.#startTimer({\n ...event,\n date,\n });\n }\n\n /**\n * Set up and start a timer for the given event.\n *\n * @param event - The event to schedule.\n * @throws If the event is scheduled in the past.\n */\n #startTimer(event: InternalBackgroundEvent) {\n const ms =\n DateTime.fromISO(event.date, { setZone: true }).toMillis() - Date.now();\n\n // We don't schedule this job yet as it is too far in the future.\n if (ms > DAILY_TIMEOUT) {\n return;\n }\n\n const timer = new Timer(ms);\n timer.start(() => {\n this.#execute(event);\n });\n\n this.#timers.set(event.id, timer);\n }\n\n /**\n * Execute a background event. This method is called when the event's timer\n * expires.\n *\n * If the event is not recurring, it will be removed from the state after\n * execution. If it is recurring, it will be rescheduled.\n *\n * @param event - The event to execute.\n */\n #execute(event: InternalBackgroundEvent) {\n this.messagingSystem\n .call('SnapController:handleRequest', {\n snapId: event.snapId,\n origin: METAMASK_ORIGIN,\n handler: HandlerType.OnCronjob,\n request: event.request,\n })\n .catch((error) => {\n logError(\n `An error occurred while executing an event for Snap \"${event.snapId}\":`,\n error,\n );\n });\n\n this.#timers.delete(event.id);\n\n // Non-recurring events are removed from the state after execution, and\n // recurring events are rescheduled.\n if (!event.recurring) {\n this.update((state) => {\n delete state.events[event.id];\n });\n\n return;\n }\n\n this.#schedule(event);\n }\n\n /**\n * Cancel a background event by its ID. Unlike {@link cancel}, this method\n * does not check the origin of the event, so it can be used internally.\n *\n * @param id - The ID of the background event to cancel.\n */\n #cancel(id: string) {\n const timer = this.#timers.get(id);\n timer?.cancel();\n this.#timers.delete(id);\n\n this.update((state) => {\n delete state.events[id];\n });\n }\n\n /**\n * Retrieve all cronjob specifications for a Snap.\n *\n * @param snapId - ID of a Snap.\n * @returns Array of cronjob specifications.\n */\n #getSnapCronjobs(snapId: SnapId): SchedulableBackgroundEvent[] {\n const permissions = this.messagingSystem.call(\n 'PermissionController:getPermissions',\n snapId,\n );\n\n const permission = permissions?.[SnapEndowments.Cronjob];\n const definitions = getCronjobCaveatJobs(permission);\n\n if (!definitions) {\n return [];\n }\n\n return definitions.map((definition, idx) => {\n return {\n snapId,\n id: `cronjob-${snapId}-${idx}`,\n request: definition.request,\n schedule: getCronjobSpecificationSchedule(definition),\n recurring: true,\n };\n });\n }\n\n /**\n * Handle events that should cause cron jobs to be registered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapInstalledEvent = (snap: TruncatedSnap) => {\n this.register(snap.id);\n };\n\n /**\n * Handle the Snap enabled event. This checks if the Snap has any cronjobs or\n * background events that need to be rescheduled.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapEnabledEvent = (snap: TruncatedSnap) => {\n const events = this.get(snap.id);\n this.#reschedule(events);\n this.register(snap.id);\n };\n\n /**\n * Handle events that should cause cronjobs and background events to be\n * unregistered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapUninstalledEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n };\n\n /**\n * Handle events that should cause cronjobs and background events to be\n * unregistered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapDisabledEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n };\n\n /**\n * Handle cron jobs on 'snapUpdated' event.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapUpdatedEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n this.register(snap.id);\n };\n\n /**\n * Reschedule events that are yet to be executed. This should be called on\n * controller initialization and once every 24 hours to ensure that\n * background events are scheduled correctly.\n *\n * @param events - An array of events to reschedule. Defaults to all events in\n * the controller state.\n */\n #reschedule(events = Object.values(this.state.events)) {\n const now = Date.now();\n\n for (const event of events) {\n if (this.#timers.has(event.id)) {\n // If the timer for this event already exists, we don't need to\n // reschedule it.\n continue;\n }\n\n const eventDate = DateTime.fromISO(event.date, {\n setZone: true,\n })\n .toUTC()\n .toMillis();\n\n // If the event is recurring and the date is in the past, execute it\n // immediately.\n if (event.recurring && eventDate <= now) {\n this.#execute(event);\n }\n\n this.#schedule(event);\n }\n }\n\n /**\n * Clear non-recurring events that are past their scheduled time.\n */\n #clear() {\n const now = Date.now();\n\n for (const event of Object.values(this.state.events)) {\n const eventDate = DateTime.fromISO(event.date, {\n setZone: true,\n })\n .toUTC()\n .toMillis();\n\n if (!event.recurring && eventDate < now) {\n this.#cancel(event.id);\n }\n }\n }\n}\n"]}
1
+ {"version":3,"file":"CronjobController.mjs","sourceRoot":"","sources":["../../src/cronjob/CronjobController.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,EACL,oBAAoB,EACpB,cAAc,EACf,oCAAoC;AAGrC,OAAO,EACL,uBAAuB,EACvB,WAAW,EACX,QAAQ,EACT,8BAA8B;AAC/B,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,cAAc,EAAE,wBAAwB;AACnE,OAAO,EAAE,SAAS,EAAE,cAAc;AAClC,OAAO,EAAE,QAAQ,EAAE,cAAc;AACjC,OAAO,EAAE,MAAM,EAAE,eAAe;AAEhC,OAAO,EAAE,+BAA+B,EAAE,gBAAgB,EAAE,oBAAgB;AAS5E,OAAO,EAAE,eAAe,EAAE,+BAA2B;AACrD,OAAO,EAAE,KAAK,EAAE,2BAAuB;AA4DvC,MAAM,CAAC,MAAM,aAAa,GAAG,cAAc,CAAC,EAAE,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC;AAoD/D,MAAM,cAAc,GAAG,mBAAmB,CAAC;AAE3C;;;;GAIG;AACH,MAAM,OAAO,iBAAkB,SAAQ,cAItC;IACU,OAAO,CAAqB;IAErC,WAAW,GAAU,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;IAE9C,YAAY,EAAE,SAAS,EAAE,KAAK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E,CAAC;gBAC/B,+DAA+D;gBAC/D,iBAAiB;gBACjB,SAAS;YACX,CAAC;YAED,MAAM,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE;gBAC7C,OAAO,EAAE,IAAI;aACd,CAAC;iBACC,KAAK,EAAE;iBACP,QAAQ,EAAE,CAAC;YAEd,oEAAoE;YACpE,eAAe;YACf,IAAI,KAAK,CAAC,SAAS,IAAI,SAAS,IAAI,GAAG,EAAE,CAAC;gBACxC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;YACvB,CAAC;YAED,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACxB,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAEvB,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;YACrD,MAAM,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE;gBAC7C,OAAO,EAAE,IAAI;aACd,CAAC;iBACC,KAAK,EAAE;iBACP,QAAQ,EAAE,CAAC;YAEd,IAAI,CAAC,KAAK,CAAC,SAAS,IAAI,SAAS,GAAG,GAAG,EAAE,CAAC;gBACxC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACzB,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["import type {\n RestrictedMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { GetPermissions } from '@metamask/permission-controller';\nimport {\n getCronjobCaveatJobs,\n SnapEndowments,\n} from '@metamask/snaps-rpc-methods';\nimport type { BackgroundEvent, SnapId } from '@metamask/snaps-sdk';\nimport type { TruncatedSnap } from '@metamask/snaps-utils';\nimport {\n toCensoredISO8601String,\n HandlerType,\n logError,\n} from '@metamask/snaps-utils';\nimport { assert, Duration, inMilliseconds } from '@metamask/utils';\nimport { castDraft } from 'immer';\nimport { DateTime } from 'luxon';\nimport { nanoid } from 'nanoid';\n\nimport { getCronjobSpecificationSchedule, getExecutionDate } from './utils';\nimport type {\n HandleSnapRequest,\n SnapDisabled,\n SnapEnabled,\n SnapInstalled,\n SnapUninstalled,\n SnapUpdated,\n} from '..';\nimport { METAMASK_ORIGIN } from '../snaps/constants';\nimport { Timer } from '../snaps/Timer';\n\nexport type CronjobControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n CronjobControllerState\n>;\nexport type CronjobControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n CronjobControllerState\n>;\n\n/**\n * Initialise the CronjobController. This should be called after all controllers\n * are created.\n */\nexport type CronjobControllerInitAction = {\n type: `${typeof controllerName}:init`;\n handler: CronjobController['init'];\n};\n\nexport type Schedule = {\n type: `${typeof controllerName}:schedule`;\n handler: CronjobController['schedule'];\n};\n\nexport type Cancel = {\n type: `${typeof controllerName}:cancel`;\n handler: CronjobController['cancel'];\n};\n\nexport type Get = {\n type: `${typeof controllerName}:get`;\n handler: CronjobController['get'];\n};\n\nexport type CronjobControllerActions =\n | CronjobControllerGetStateAction\n | HandleSnapRequest\n | GetPermissions\n | Schedule\n | Cancel\n | Get\n | CronjobControllerInitAction;\n\nexport type CronjobControllerEvents =\n | CronjobControllerStateChangeEvent\n | SnapInstalled\n | SnapUninstalled\n | SnapUpdated\n | SnapEnabled\n | SnapDisabled;\n\nexport type CronjobControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n CronjobControllerActions,\n CronjobControllerEvents,\n CronjobControllerActions['type'],\n CronjobControllerEvents['type']\n>;\n\nexport const DAILY_TIMEOUT = inMilliseconds(24, Duration.Hour);\n\nexport type CronjobControllerArgs = {\n messenger: CronjobControllerMessenger;\n\n /**\n * Persisted state that will be used for rehydration.\n */\n state?: CronjobControllerState;\n};\n\n/**\n * Represents a background event that is scheduled to be executed by the\n * cronjob controller.\n */\nexport type InternalBackgroundEvent = BackgroundEvent & {\n /**\n * Whether the event is recurring.\n */\n recurring: boolean;\n\n /**\n * The cron expression or ISO 8601 duration string that defines the event's\n * schedule.\n */\n schedule: string;\n};\n\n/**\n * A schedulable background event, which is a subset of the\n * {@link InternalBackgroundEvent} type, containing only the fields required to\n * schedule an event. Other fields will be populated by the cronjob controller\n * automatically.\n */\nexport type SchedulableBackgroundEvent = Omit<\n InternalBackgroundEvent,\n 'scheduledAt' | 'date' | 'id'\n> & {\n /**\n * The optional ID of the event. If not provided, a new ID will be\n * generated.\n */\n id?: string;\n};\n\nexport type CronjobControllerState = {\n /**\n * Background events and cronjobs that are scheduled to be executed.\n */\n events: Record<string, InternalBackgroundEvent>;\n};\n\nconst controllerName = 'CronjobController';\n\n/**\n * The cronjob controller is responsible for managing cronjobs and background\n * events for Snaps. It allows Snaps to schedule events that will be executed\n * at a later time.\n */\nexport class CronjobController extends BaseController<\n typeof controllerName,\n CronjobControllerState,\n CronjobControllerMessenger\n> {\n readonly #timers: Map<string, Timer>;\n\n #dailyTimer: Timer = new Timer(DAILY_TIMEOUT);\n\n constructor({ messenger, state }: CronjobControllerArgs) {\n super({\n messenger,\n metadata: {\n events: { persist: true, anonymous: false },\n },\n name: controllerName,\n state: {\n events: {},\n ...state,\n },\n });\n\n this.#timers = new Map();\n\n this.messagingSystem.subscribe(\n 'SnapController:snapInstalled',\n this.#handleSnapInstalledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapUninstalled',\n this.#handleSnapUninstalledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapEnabled',\n this.#handleSnapEnabledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapDisabled',\n this.#handleSnapDisabledEvent,\n );\n\n this.messagingSystem.subscribe(\n 'SnapController:snapUpdated',\n this.#handleSnapUpdatedEvent,\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:init`,\n (...args) => this.init(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:schedule`,\n (...args) => this.schedule(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:cancel`,\n (...args) => this.cancel(...args),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:get`,\n (...args) => this.get(...args),\n );\n }\n\n /**\n * Initialize the CronjobController.\n *\n * This starts the daily timer, clears out expired events\n * and reschedules any remaining events.\n */\n init() {\n this.#start();\n this.#clear();\n this.#reschedule();\n }\n\n /**\n * Schedule a non-recurring background event.\n *\n * @param event - The event to schedule.\n * @returns The ID of the scheduled event.\n */\n schedule(event: Omit<SchedulableBackgroundEvent, 'recurring'>) {\n return this.#add({\n ...event,\n recurring: false,\n });\n }\n\n /**\n * Cancel an event.\n *\n * @param origin - The origin making the cancel call.\n * @param id - The id of the event to cancel.\n * @throws If the event does not exist.\n */\n cancel(origin: string, id: string) {\n assert(\n this.state.events[id],\n `A background event with the id of \"${id}\" does not exist.`,\n );\n\n assert(\n this.state.events[id].snapId === origin,\n 'Only the origin that scheduled this event can cancel it.',\n );\n\n this.#cancel(id);\n }\n\n /**\n * Get a list of a Snap's background events.\n *\n * @param snapId - The id of the Snap to fetch background events for.\n * @returns An array of background events.\n */\n get(snapId: SnapId): InternalBackgroundEvent[] {\n return Object.values(this.state.events)\n .filter(\n (snapEvent) => snapEvent.snapId === snapId && !snapEvent.recurring,\n )\n .map((event) => ({\n ...event,\n date: toCensoredISO8601String(event.date),\n scheduledAt: toCensoredISO8601String(event.scheduledAt),\n }));\n }\n\n /**\n * Register cronjobs for a given Snap by getting specification from the\n * permission caveats. Once registered, each job will be scheduled.\n *\n * @param snapId - The snap ID to register jobs for.\n */\n register(snapId: SnapId) {\n const jobs = this.#getSnapCronjobs(snapId);\n jobs?.forEach((job) => this.#add(job));\n }\n\n /**\n * Unregister all cronjobs and background events for a given Snap.\n *\n * @param snapId - ID of a snap.\n */\n unregister(snapId: SnapId) {\n for (const [id, event] of Object.entries(this.state.events)) {\n if (event.snapId === snapId) {\n this.#cancel(id);\n }\n }\n }\n\n /**\n * Run controller teardown process and unsubscribe from Snap events.\n */\n destroy() {\n super.destroy();\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapInstalled',\n this.#handleSnapInstalledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapUninstalled',\n this.#handleSnapUninstalledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapEnabled',\n this.#handleSnapEnabledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapDisabled',\n this.#handleSnapDisabledEvent,\n );\n\n this.messagingSystem.unsubscribe(\n 'SnapController:snapUpdated',\n this.#handleSnapUpdatedEvent,\n );\n\n // Cancel all timers and clear the map.\n this.#timers.forEach((timer) => timer.cancel());\n this.#timers.clear();\n\n if (this.#dailyTimer.status === 'running') {\n this.#dailyTimer.cancel();\n }\n }\n\n /**\n * Start the daily timer that will reschedule events every 24 hours.\n */\n #start() {\n this.#dailyTimer = new Timer(DAILY_TIMEOUT);\n this.#dailyTimer.start(() => {\n this.#reschedule();\n this.#start();\n });\n }\n\n /**\n * Add a cronjob or background event to the controller state and schedule it\n * for execution.\n *\n * @param event - The event to schedule.\n * @returns The ID of the scheduled event.\n */\n #add(event: SchedulableBackgroundEvent) {\n const id = event.id ?? nanoid();\n const internalEvent: InternalBackgroundEvent = {\n ...event,\n id,\n date: getExecutionDate(event.schedule),\n scheduledAt: new Date().toISOString(),\n };\n\n this.update((state) => {\n state.events[internalEvent.id] = castDraft(internalEvent);\n });\n\n this.#schedule(internalEvent);\n return id;\n }\n\n /**\n * Get the next execution date for a given event and start a timer for it.\n *\n * @param event - The event to schedule.\n */\n #schedule(event: InternalBackgroundEvent) {\n const date = getExecutionDate(event.schedule);\n this.update((state) => {\n state.events[event.id].date = date;\n });\n\n this.#startTimer({\n ...event,\n date,\n });\n }\n\n /**\n * Set up and start a timer for the given event.\n *\n * @param event - The event to schedule.\n * @throws If the event is scheduled in the past.\n */\n #startTimer(event: InternalBackgroundEvent) {\n const ms =\n DateTime.fromISO(event.date, { setZone: true }).toMillis() - Date.now();\n\n // We don't schedule this job yet as it is too far in the future.\n if (ms > DAILY_TIMEOUT) {\n return;\n }\n\n // When an event is supposed to be scheduled close to the current time\n // we may end up needing to execute immediately instead.\n if (ms <= 0) {\n this.#execute(event);\n return;\n }\n\n const timer = new Timer(ms);\n timer.start(() => {\n this.#execute(event);\n });\n\n this.#timers.set(event.id, timer);\n }\n\n /**\n * Execute a background event. This method is called when the event's timer\n * expires.\n *\n * If the event is not recurring, it will be removed from the state after\n * execution. If it is recurring, it will be rescheduled.\n *\n * @param event - The event to execute.\n */\n #execute(event: InternalBackgroundEvent) {\n this.messagingSystem\n .call('SnapController:handleRequest', {\n snapId: event.snapId,\n origin: METAMASK_ORIGIN,\n handler: HandlerType.OnCronjob,\n request: event.request,\n })\n .catch((error) => {\n logError(\n `An error occurred while executing an event for Snap \"${event.snapId}\":`,\n error,\n );\n });\n\n this.#timers.delete(event.id);\n\n // Non-recurring events are removed from the state after execution, and\n // recurring events are rescheduled.\n if (!event.recurring) {\n this.update((state) => {\n delete state.events[event.id];\n });\n\n return;\n }\n\n this.#schedule(event);\n }\n\n /**\n * Cancel a background event by its ID. Unlike {@link cancel}, this method\n * does not check the origin of the event, so it can be used internally.\n *\n * @param id - The ID of the background event to cancel.\n */\n #cancel(id: string) {\n const timer = this.#timers.get(id);\n timer?.cancel();\n this.#timers.delete(id);\n\n this.update((state) => {\n delete state.events[id];\n });\n }\n\n /**\n * Retrieve all cronjob specifications for a Snap.\n *\n * @param snapId - ID of a Snap.\n * @returns Array of cronjob specifications.\n */\n #getSnapCronjobs(snapId: SnapId): SchedulableBackgroundEvent[] {\n const permissions = this.messagingSystem.call(\n 'PermissionController:getPermissions',\n snapId,\n );\n\n const permission = permissions?.[SnapEndowments.Cronjob];\n const definitions = getCronjobCaveatJobs(permission);\n\n if (!definitions) {\n return [];\n }\n\n return definitions.map((definition, idx) => {\n return {\n snapId,\n id: `cronjob-${snapId}-${idx}`,\n request: definition.request,\n schedule: getCronjobSpecificationSchedule(definition),\n recurring: true,\n };\n });\n }\n\n /**\n * Handle events that should cause cron jobs to be registered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapInstalledEvent = (snap: TruncatedSnap) => {\n this.register(snap.id);\n };\n\n /**\n * Handle the Snap enabled event. This checks if the Snap has any cronjobs or\n * background events that need to be rescheduled.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapEnabledEvent = (snap: TruncatedSnap) => {\n const events = this.get(snap.id);\n this.#reschedule(events);\n this.register(snap.id);\n };\n\n /**\n * Handle events that should cause cronjobs and background events to be\n * unregistered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapUninstalledEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n };\n\n /**\n * Handle events that should cause cronjobs and background events to be\n * unregistered.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapDisabledEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n };\n\n /**\n * Handle cron jobs on 'snapUpdated' event.\n *\n * @param snap - Basic Snap information.\n */\n readonly #handleSnapUpdatedEvent = (snap: TruncatedSnap) => {\n this.unregister(snap.id);\n this.register(snap.id);\n };\n\n /**\n * Reschedule events that are yet to be executed. This should be called on\n * controller initialization and once every 24 hours to ensure that\n * background events are scheduled correctly.\n *\n * @param events - An array of events to reschedule. Defaults to all events in\n * the controller state.\n */\n #reschedule(events = Object.values(this.state.events)) {\n const now = Date.now();\n\n for (const event of events) {\n if (this.#timers.has(event.id)) {\n // If the timer for this event already exists, we don't need to\n // reschedule it.\n continue;\n }\n\n const eventDate = DateTime.fromISO(event.date, {\n setZone: true,\n })\n .toUTC()\n .toMillis();\n\n // If the event is recurring and the date is in the past, execute it\n // immediately.\n if (event.recurring && eventDate <= now) {\n this.#execute(event);\n }\n\n this.#schedule(event);\n }\n }\n\n /**\n * Clear non-recurring events that are past their scheduled time.\n */\n #clear() {\n const now = Date.now();\n\n for (const event of Object.values(this.state.events)) {\n const eventDate = DateTime.fromISO(event.date, {\n setZone: true,\n })\n .toUTC()\n .toMillis();\n\n if (!event.recurring && eventDate < now) {\n this.#cancel(event.id);\n }\n }\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-controllers",
3
- "version": "14.0.0",
3
+ "version": "14.0.1",
4
4
  "description": "Controllers for MetaMask Snaps",
5
5
  "keywords": [
6
6
  "MetaMask",