| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Logger} from './logger'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import path from 'node:path'; |
| b69ab31 | | | 11 | // Import from the generated files |
| b69ab31 | | | 12 | import { |
| b69ab31 | | | 13 | EdenFSNotificationsClient, |
| b69ab31 | | | 14 | type EdenFSSubscription, |
| b69ab31 | | | 15 | type SubscriptionCallback, |
| b69ab31 | | | 16 | type SubscriptionOptions, |
| b69ab31 | | | 17 | } from './__generated__/node-edenfs-notifications-client/index.js'; |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | export type EdenFSSubscriptionManager = { |
| b69ab31 | | | 20 | path: string; |
| b69ab31 | | | 21 | name: string; |
| b69ab31 | | | 22 | subscriptionCount: number; |
| b69ab31 | | | 23 | internalSubscription: EdenFSSubscription; |
| b69ab31 | | | 24 | }; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | /** |
| b69ab31 | | | 27 | * Provides directory watching functionality using EdenFS notify API. |
| b69ab31 | | | 28 | */ |
| b69ab31 | | | 29 | export class EdenFSNotifications { |
| b69ab31 | | | 30 | private client: EdenFSNotificationsClient; |
| b69ab31 | | | 31 | private subscriptions: Map<string, EdenFSSubscriptionManager> = new Map(); |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | constructor( |
| b69ab31 | | | 34 | private logger: Logger, |
| b69ab31 | | | 35 | private mountPoint: string, |
| b69ab31 | | | 36 | ) { |
| b69ab31 | | | 37 | this.client = new EdenFSNotificationsClient({ |
| b69ab31 | | | 38 | mountPoint: this.mountPoint, |
| b69ab31 | | | 39 | timeout: 30000, |
| b69ab31 | | | 40 | edenBinaryPath: process.env.EDEN_PATH ? process.env.EDEN_PATH : 'eden', |
| b69ab31 | | | 41 | }); |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | /** |
| b69ab31 | | | 45 | * Watch a directory recursively for changes using EdenFS notifications. |
| b69ab31 | | | 46 | */ |
| b69ab31 | | | 47 | public async watchDirectoryRecursive( |
| b69ab31 | | | 48 | localDirectoryPath: string, |
| b69ab31 | | | 49 | rawSubscriptionName: string, |
| b69ab31 | | | 50 | subscriptionOptions: SubscriptionOptions, |
| b69ab31 | | | 51 | callback: SubscriptionCallback, |
| b69ab31 | | | 52 | ): Promise<EdenFSSubscription> { |
| b69ab31 | | | 53 | // Subscriptions should be unique by name and by folder |
| b69ab31 | | | 54 | const subscriptionName = this.fixupName(localDirectoryPath, rawSubscriptionName); |
| b69ab31 | | | 55 | const existingSubscription = this.getSubscription(subscriptionName); |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | if (existingSubscription) { |
| b69ab31 | | | 58 | existingSubscription.subscriptionCount++; |
| b69ab31 | | | 59 | return existingSubscription.internalSubscription; |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | const internalSubscription = this.client.subscribe(subscriptionOptions, callback); |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | const subscription: EdenFSSubscriptionManager = { |
| b69ab31 | | | 65 | path: localDirectoryPath, |
| b69ab31 | | | 66 | name: subscriptionName, |
| b69ab31 | | | 67 | subscriptionCount: 1, |
| b69ab31 | | | 68 | internalSubscription, |
| b69ab31 | | | 69 | }; |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | this.setSubscription(subscriptionName, subscription); |
| b69ab31 | | | 72 | await internalSubscription.start(); |
| b69ab31 | | | 73 | this.logger.log(`edenfs subscription started: ${subscriptionName}`); |
| b69ab31 | | | 74 | return internalSubscription; |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | |
| b69ab31 | | | 77 | private getSubscription(entryPath: string): EdenFSSubscriptionManager | undefined { |
| b69ab31 | | | 78 | return this.subscriptions.get(path.normalize(entryPath)); |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | private setSubscription(entryPath: string, subscription: EdenFSSubscriptionManager): void { |
| b69ab31 | | | 82 | const key = path.normalize(entryPath); |
| b69ab31 | | | 83 | this.subscriptions.set(key, subscription); |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | private deleteSubscription(entryPath: string): void { |
| b69ab31 | | | 87 | const key = path.normalize(entryPath); |
| b69ab31 | | | 88 | this.subscriptions.delete(key); |
| b69ab31 | | | 89 | } |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | private fixupName(path: string, name: string): string { |
| b69ab31 | | | 92 | const refinedPath = path.replace(/\\/g, '-').replace(/\//g, '-'); |
| b69ab31 | | | 93 | return `${refinedPath}-${name}`; |
| b69ab31 | | | 94 | } |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | /** |
| b69ab31 | | | 97 | * Remove a subscription (unwatch) |
| b69ab31 | | | 98 | */ |
| b69ab31 | | | 99 | public unwatch(path: string, name: string) { |
| b69ab31 | | | 100 | const subscriptionName = this.fixupName(path, name); |
| b69ab31 | | | 101 | const subscription = this.getSubscription(subscriptionName); |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | if (subscription == null) { |
| b69ab31 | | | 104 | this.logger.error(`No watcher entity found with path [${path}] name [${name}]`); |
| b69ab31 | | | 105 | return; |
| b69ab31 | | | 106 | } |
| b69ab31 | | | 107 | |
| b69ab31 | | | 108 | if (--subscription.subscriptionCount === 0) { |
| b69ab31 | | | 109 | subscription.internalSubscription.stop(); |
| b69ab31 | | | 110 | this.deleteSubscription(subscriptionName); |
| b69ab31 | | | 111 | this.logger.log(`edenfs subscription destroyed: ${subscriptionName}`); |
| b69ab31 | | | 112 | } |
| b69ab31 | | | 113 | } |
| b69ab31 | | | 114 | } |