| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * This software contains information and intellectual property that is |
| b69ab31 | | | 3 | * confidential and proprietary to Facebook, Inc. and its affiliates. |
| b69ab31 | | | 4 | * |
| b69ab31 | | | 5 | * @generated |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | /* eslint-disable */ |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | /* |
| b69ab31 | | | 11 | * This file is synced between fbcode/eden/fs/facebook/prototypes/node-edenfs-notifications-client/index.js. |
| b69ab31 | | | 12 | * The authoritative copy is the one in eden/fs/. |
| b69ab31 | | | 13 | * Use `yarn sync-edenfs-notifications` to perform the sync. |
| b69ab31 | | | 14 | * |
| b69ab31 | | | 15 | * This file is intended to be self contained so it may be copied/referenced from other extensions, |
| b69ab31 | | | 16 | * which is why it should not import anything and why it reimplements many types. |
| b69ab31 | | | 17 | */ |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | /** |
| b69ab31 | | | 20 | * JavaScript interface for EdenFS CLI notify endpoint |
| b69ab31 | | | 21 | * |
| b69ab31 | | | 22 | * This module provides a JavaScript wrapper around the EdenFS CLI notify commands, |
| b69ab31 | | | 23 | * allowing you to monitor filesystem changes in EdenFS mounts. |
| b69ab31 | | | 24 | * |
| b69ab31 | | | 25 | * @author cqd |
| b69ab31 | | | 26 | * @version 1.0.0 |
| b69ab31 | | | 27 | * |
| b69ab31 | | | 28 | * @format |
| b69ab31 | | | 29 | * @flow |
| b69ab31 | | | 30 | * @ts-check |
| b69ab31 | | | 31 | */ |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | const {spawn, execFile} = require('child_process'); |
| b69ab31 | | | 34 | const {EventEmitter} = require('events'); |
| b69ab31 | | | 35 | const path = require('path'); |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | /** |
| b69ab31 | | | 38 | * EdenFS Notifications Client |
| b69ab31 | | | 39 | * Provides methods to interact with EdenFS notifications via the EdenFS CLI |
| b69ab31 | | | 40 | */ |
| b69ab31 | | | 41 | class EdenFSNotificationsClient extends EventEmitter { |
| b69ab31 | | | 42 | /** @type {string} */ |
| b69ab31 | | | 43 | mountPoint; |
| b69ab31 | | | 44 | /** @type {number} */ |
| b69ab31 | | | 45 | timeout; |
| b69ab31 | | | 46 | /** @type {string} */ |
| b69ab31 | | | 47 | edenBinaryPath; |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | DEFAULT_EDENFS_RECONNECT_DELAY_MS = 100; |
| b69ab31 | | | 50 | MAXIMUM_EDENFS_RECONNECT_DELAY_MS = 60 * 1000; |
| b69ab31 | | | 51 | |
| b69ab31 | | | 52 | constructor(options) { |
| b69ab31 | | | 53 | super(); |
| b69ab31 | | | 54 | this.mountPoint = options?.mountPoint ?? null; |
| b69ab31 | | | 55 | this.timeout = options?.timeout ?? 30000; // 30 seconds default timeout |
| b69ab31 | | | 56 | this.edenBinaryPath = options?.edenBinaryPath ?? 'eden'; |
| b69ab31 | | | 57 | } |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | /** |
| b69ab31 | | | 60 | * Get the current EdenFS status |
| b69ab31 | | | 61 | * @returns {boolean} Edenfs running/not running |
| b69ab31 | | | 62 | */ |
| b69ab31 | | | 63 | async getStatus(options = {}) { |
| b69ab31 | | | 64 | const args = ['status']; |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | if (options.useCase) { |
| b69ab31 | | | 67 | args.push('--use-case', options.useCase); |
| b69ab31 | | | 68 | } else { |
| b69ab31 | | | 69 | args.push('--use-case', 'node-client'); |
| b69ab31 | | | 70 | } |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | return new Promise((resolve, reject) => { |
| b69ab31 | | | 73 | execFile(this.edenBinaryPath, args, {timeout: this.timeout}, (error, stdout, stderr) => { |
| b69ab31 | | | 74 | if (error) { |
| b69ab31 | | | 75 | reject( |
| b69ab31 | | | 76 | new Error( |
| b69ab31 | | | 77 | `Failed to get status: ${error.message}\nStdout: ${stdout}\nStderr: ${stderr}`, |
| b69ab31 | | | 78 | ), |
| b69ab31 | | | 79 | ); |
| b69ab31 | | | 80 | return; |
| b69ab31 | | | 81 | } |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | try { |
| b69ab31 | | | 84 | const result = stdout.trim(); |
| b69ab31 | | | 85 | resolve(result); |
| b69ab31 | | | 86 | } catch (parseError) { |
| b69ab31 | | | 87 | reject(new Error(`Failed to parse response: ${parseError.message}\nStdout: ${stdout}`)); |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | }); |
| b69ab31 | | | 90 | }); |
| b69ab31 | | | 91 | } |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | /** |
| b69ab31 | | | 94 | * Wait until EdenFS is ready |
| b69ab31 | | | 95 | * @returns {Promise<boolean>} True=Healthy, False=Timeout |
| b69ab31 | | | 96 | */ |
| b69ab31 | | | 97 | async waitReady(options={}) { |
| b69ab31 | | | 98 | const maxDelay = this.MAXIMUM_EDENFS_RECONNECT_DELAY_MS; |
| b69ab31 | | | 99 | let delay = this.DEFAULT_EDENFS_RECONNECT_DELAY_MS; |
| b69ab31 | | | 100 | const start = Date.now(); |
| b69ab31 | | | 101 | const deadline = start + (options.timeout ?? this.timeout); |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | // Helper: sleep for ms |
| b69ab31 | | | 104 | const sleep = ms => new Promise(res => setTimeout(res, ms)); |
| b69ab31 | | | 105 | |
| b69ab31 | | | 106 | // If timeout=0, wait forever |
| b69ab31 | | | 107 | while (options.timeout == 0 || Date.now() < deadline) { |
| b69ab31 | | | 108 | try { |
| b69ab31 | | | 109 | const status = await this.getStatus({useCase: options.useCase ?? undefined}); |
| b69ab31 | | | 110 | // Consider any truthy/non-empty status string as "healthy" |
| b69ab31 | | | 111 | if (status && typeof status === 'string' && status.trim().length > 0) { |
| b69ab31 | | | 112 | return true; |
| b69ab31 | | | 113 | } |
| b69ab31 | | | 114 | } catch (e) { |
| b69ab31 | | | 115 | // Swallow and retry with backoff |
| b69ab31 | | | 116 | } |
| b69ab31 | | | 117 | |
| b69ab31 | | | 118 | // Exponential backoff (capped) |
| b69ab31 | | | 119 | await sleep(delay); |
| b69ab31 | | | 120 | delay = Math.min(delay * 2, maxDelay); |
| b69ab31 | | | 121 | } |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | return false; |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | } |
| b69ab31 | | | 126 | |
| b69ab31 | | | 127 | /** |
| b69ab31 | | | 128 | * Get the current EdenFS journal position |
| b69ab31 | | | 129 | * @param {Object} options - Options for changes-since command |
| b69ab31 | | | 130 | * @param {string} options.useCase - Use case for the command |
| b69ab31 | | | 131 | * @param {string} options.mountPoint - Path to the mount point (optional if set in constructor) |
| b69ab31 | | | 132 | * @returns {Promise<JournalPosition|string>} Journal position |
| b69ab31 | | | 133 | */ |
| b69ab31 | | | 134 | async getPosition(options = {}) { |
| b69ab31 | | | 135 | const mountPoint = options?.mountPoint ?? this.mountPoint; |
| b69ab31 | | | 136 | const args = ['notify', 'get-position']; |
| b69ab31 | | | 137 | |
| b69ab31 | | | 138 | if (options.useCase) { |
| b69ab31 | | | 139 | args.push('--use-case', options.useCase); |
| b69ab31 | | | 140 | } else { |
| b69ab31 | | | 141 | args.push('--use-case', 'node-client'); |
| b69ab31 | | | 142 | } |
| b69ab31 | | | 143 | |
| b69ab31 | | | 144 | if (mountPoint) { |
| b69ab31 | | | 145 | args.push(mountPoint); |
| b69ab31 | | | 146 | } |
| b69ab31 | | | 147 | |
| b69ab31 | | | 148 | return new Promise((resolve, reject) => { |
| b69ab31 | | | 149 | execFile(this.edenBinaryPath, args, {timeout: this.timeout}, (error, stdout, stderr) => { |
| b69ab31 | | | 150 | if (error) { |
| b69ab31 | | | 151 | reject( |
| b69ab31 | | | 152 | new Error( |
| b69ab31 | | | 153 | `Failed to get position: ${error.message}\nStdout: ${stdout}\nStderr: ${stderr}`, |
| b69ab31 | | | 154 | ), |
| b69ab31 | | | 155 | ); |
| b69ab31 | | | 156 | return; |
| b69ab31 | | | 157 | } |
| b69ab31 | | | 158 | |
| b69ab31 | | | 159 | try { |
| b69ab31 | | | 160 | const result = stdout.trim(); |
| b69ab31 | | | 161 | resolve(result); |
| b69ab31 | | | 162 | } catch (parseError) { |
| b69ab31 | | | 163 | reject(new Error(`Failed to parse response: ${parseError.message}\nStdout: ${stdout}`)); |
| b69ab31 | | | 164 | } |
| b69ab31 | | | 165 | }); |
| b69ab31 | | | 166 | }); |
| b69ab31 | | | 167 | } |
| b69ab31 | | | 168 | |
| b69ab31 | | | 169 | /** |
| b69ab31 | | | 170 | * Get changes since a specific journal position |
| b69ab31 | | | 171 | * @param {Object} options - Options for changes-since command |
| b69ab31 | | | 172 | * @param {string} options.position - Journal position to start from |
| b69ab31 | | | 173 | * @param {string} options.useCase - Use case for the command |
| b69ab31 | | | 174 | * @param {string} options.mountPoint - Path to the mount point (optional if set in constructor) |
| b69ab31 | | | 175 | * @param {string} options.relativeRoot - Relative root to scope results |
| b69ab31 | | | 176 | * @param {boolean} options.includeVcsRoots - Include VCS roots in output |
| b69ab31 | | | 177 | * @param {string[]} options.includedRoots - Included roots in output |
| b69ab31 | | | 178 | * @param {string[]} options.excludedRoots - Excluded roots in output |
| b69ab31 | | | 179 | * @param {string[]} options.includedSuffixes - Included suffixes in output |
| b69ab31 | | | 180 | * @param {string[]} options.excludedSuffixes - Excluded suffixes in output |
| b69ab31 | | | 181 | * @param {boolean} options.json - Return JSON format (default: true) |
| b69ab31 | | | 182 | * @param {string[]} options.deferredStates - States to wait for deassertion |
| b69ab31 | | | 183 | * @returns {Promise<Object|string>} Changes since position |
| b69ab31 | | | 184 | */ |
| b69ab31 | | | 185 | async getChangesSince(options = {}) { |
| b69ab31 | | | 186 | const mountPoint = options?.mountPoint ?? this.mountPoint; |
| b69ab31 | | | 187 | const args = ['notify', 'changes-since']; |
| b69ab31 | | | 188 | |
| b69ab31 | | | 189 | if (options.useCase) { |
| b69ab31 | | | 190 | args.push('--use-case', options.useCase); |
| b69ab31 | | | 191 | } else { |
| b69ab31 | | | 192 | args.push('--use-case', 'node-client'); |
| b69ab31 | | | 193 | } |
| b69ab31 | | | 194 | |
| b69ab31 | | | 195 | if (options.position) { |
| b69ab31 | | | 196 | args.push( |
| b69ab31 | | | 197 | '--position', |
| b69ab31 | | | 198 | typeof options.position === 'string' ? options.position : JSON.stringify(options.position), |
| b69ab31 | | | 199 | ); |
| b69ab31 | | | 200 | } |
| b69ab31 | | | 201 | |
| b69ab31 | | | 202 | if (options.relativeRoot) { |
| b69ab31 | | | 203 | args.push('--relative-root', options.relativeRoot); |
| b69ab31 | | | 204 | } |
| b69ab31 | | | 205 | |
| b69ab31 | | | 206 | if (options.includeVcsRoots) { |
| b69ab31 | | | 207 | args.push('--include-vcs-roots'); |
| b69ab31 | | | 208 | } |
| b69ab31 | | | 209 | |
| b69ab31 | | | 210 | if (options.includedRoots) { |
| b69ab31 | | | 211 | options.includedRoots.forEach(root => { |
| b69ab31 | | | 212 | args.push('--included-roots', root); |
| b69ab31 | | | 213 | }); |
| b69ab31 | | | 214 | } |
| b69ab31 | | | 215 | |
| b69ab31 | | | 216 | if (options.excludedRoots) { |
| b69ab31 | | | 217 | options.excludedRoots.forEach(root => { |
| b69ab31 | | | 218 | args.push('--excluded-roots', root); |
| b69ab31 | | | 219 | }); |
| b69ab31 | | | 220 | } |
| b69ab31 | | | 221 | |
| b69ab31 | | | 222 | if (options.includedSuffixes) { |
| b69ab31 | | | 223 | options.includedSuffixes.forEach(suffix => { |
| b69ab31 | | | 224 | args.push('--included-suffixes', suffix); |
| b69ab31 | | | 225 | }); |
| b69ab31 | | | 226 | } |
| b69ab31 | | | 227 | |
| b69ab31 | | | 228 | if (options.excludedSuffixes) { |
| b69ab31 | | | 229 | options.excludedSuffixes.forEach(suffix => { |
| b69ab31 | | | 230 | args.push('--excluded-suffixes', suffix); |
| b69ab31 | | | 231 | }); |
| b69ab31 | | | 232 | } |
| b69ab31 | | | 233 | |
| b69ab31 | | | 234 | if (options.deferredStates) { |
| b69ab31 | | | 235 | options.deferredStates.forEach(state => { |
| b69ab31 | | | 236 | args.push('--deferred-states', state); |
| b69ab31 | | | 237 | }); |
| b69ab31 | | | 238 | } |
| b69ab31 | | | 239 | |
| b69ab31 | | | 240 | args.push('--json'); |
| b69ab31 | | | 241 | args.push('--formatted-position'); |
| b69ab31 | | | 242 | |
| b69ab31 | | | 243 | if (mountPoint) { |
| b69ab31 | | | 244 | args.push(mountPoint); |
| b69ab31 | | | 245 | } |
| b69ab31 | | | 246 | |
| b69ab31 | | | 247 | return new Promise((resolve, reject) => { |
| b69ab31 | | | 248 | execFile(this.edenBinaryPath, args, {timeout: this.timeout}, (error, stdout, stderr) => { |
| b69ab31 | | | 249 | if (error) { |
| b69ab31 | | | 250 | reject(new Error(`Failed to get changes: ${error.message}\nStderr: ${stderr}`)); |
| b69ab31 | | | 251 | return; |
| b69ab31 | | | 252 | } |
| b69ab31 | | | 253 | |
| b69ab31 | | | 254 | try { |
| b69ab31 | | | 255 | const result = JSON.parse(stdout.trim()); |
| b69ab31 | | | 256 | resolve(result); |
| b69ab31 | | | 257 | } catch (parseError) { |
| b69ab31 | | | 258 | reject(new Error(`Failed to parse response: ${parseError.message}`)); |
| b69ab31 | | | 259 | } |
| b69ab31 | | | 260 | }); |
| b69ab31 | | | 261 | }); |
| b69ab31 | | | 262 | } |
| b69ab31 | | | 263 | |
| b69ab31 | | | 264 | /** |
| b69ab31 | | | 265 | * Subscribe to filesystem changes |
| b69ab31 | | | 266 | * @param {Object} options - Options for subscription |
| b69ab31 | | | 267 | * @param {string} options.position - Journal position to start from (optional) |
| b69ab31 | | | 268 | * @param {string} options.useCase - Use case for the command |
| b69ab31 | | | 269 | * @param {string} options.mountPoint - Path to the mount point (optional if set in constructor) |
| b69ab31 | | | 270 | * @param {number} options.throttle - Throttle in milliseconds between events (default: 0) |
| b69ab31 | | | 271 | * @param {string} options.relativeRoot - Relative root to scope results |
| b69ab31 | | | 272 | * @param {boolean} options.includeVcsRoots - Include VCS roots in output |
| b69ab31 | | | 273 | * @param {string[]} options.includedRoots - Included roots in output |
| b69ab31 | | | 274 | * @param {string[]} options.excludedRoots - Excluded roots in output |
| b69ab31 | | | 275 | * @param {string[]} options.includedSuffixes - Included suffixes in output |
| b69ab31 | | | 276 | * @param {string[]} options.excludedSuffixes - Excluded suffixes in output |
| b69ab31 | | | 277 | * @param {string[]} options.deferredStates - States to wait for deassertion |
| b69ab31 | | | 278 | * @param {CommandCallback} callback |
| b69ab31 | | | 279 | * @returns {EdenFSSubscription} Subscription object |
| b69ab31 | | | 280 | */ |
| b69ab31 | | | 281 | subscribe(options = {}, callback = () => {}) { |
| b69ab31 | | | 282 | options['edenBinaryPath'] = this.edenBinaryPath; |
| b69ab31 | | | 283 | let sub = new EdenFSSubscription(this, options, callback); |
| b69ab31 | | | 284 | sub.on('change', change => { |
| b69ab31 | | | 285 | callback(null, change); |
| b69ab31 | | | 286 | }); |
| b69ab31 | | | 287 | sub.on('error', error => { |
| b69ab31 | | | 288 | callback(error, null); |
| b69ab31 | | | 289 | }); |
| b69ab31 | | | 290 | sub.on('close', () => { |
| b69ab31 | | | 291 | // Received when the underlying gets killed, pass double null to indicate |
| b69ab31 | | | 292 | // this since no error or message is available |
| b69ab31 | | | 293 | callback(null, null); |
| b69ab31 | | | 294 | }); |
| b69ab31 | | | 295 | return sub; |
| b69ab31 | | | 296 | } |
| b69ab31 | | | 297 | |
| b69ab31 | | | 298 | /** |
| b69ab31 | | | 299 | * Enter a specific state |
| b69ab31 | | | 300 | * @param {string} state - State name to enter |
| b69ab31 | | | 301 | * @param {Object} options - Options for enterState command |
| b69ab31 | | | 302 | * @param {number} [options.duration] - Duration in seconds to maintain state |
| b69ab31 | | | 303 | * @param {string} [options.useCase] - Use case for the command |
| b69ab31 | | | 304 | * @param {string} options.mountPoint - Path to the mount point (optional if set in constructor) |
| b69ab31 | | | 305 | * @returns {Promise<void>} |
| b69ab31 | | | 306 | */ |
| b69ab31 | | | 307 | async enterState(state, options = {}) { |
| b69ab31 | | | 308 | const mountPoint = options?.mountPoint ?? this.mountPoint; |
| b69ab31 | | | 309 | if (!state || typeof state !== 'string') { |
| b69ab31 | | | 310 | throw new Error('State name must be a non-empty string'); |
| b69ab31 | | | 311 | } |
| b69ab31 | | | 312 | |
| b69ab31 | | | 313 | const args = ['notify', 'enter-state', state]; |
| b69ab31 | | | 314 | if (options.duration !== undefined) { |
| b69ab31 | | | 315 | args.push('--duration', options.duration.toString()); |
| b69ab31 | | | 316 | } |
| b69ab31 | | | 317 | |
| b69ab31 | | | 318 | if (options.useCase) { |
| b69ab31 | | | 319 | args.push('--use-case', options.useCase); |
| b69ab31 | | | 320 | } else { |
| b69ab31 | | | 321 | args.push('--use-case', 'node-client'); |
| b69ab31 | | | 322 | } |
| b69ab31 | | | 323 | |
| b69ab31 | | | 324 | if (mountPoint) { |
| b69ab31 | | | 325 | args.push(mountPoint); |
| b69ab31 | | | 326 | } |
| b69ab31 | | | 327 | |
| b69ab31 | | | 328 | return new Promise((resolve, reject) => { |
| b69ab31 | | | 329 | execFile(this.edenBinaryPath, args, {timeout: this.timeout}, (error, stdout, stderr) => { |
| b69ab31 | | | 330 | if (error) { |
| b69ab31 | | | 331 | reject( |
| b69ab31 | | | 332 | new Error( |
| b69ab31 | | | 333 | `Failed to enter state: ${error.message}\nStdout: ${stdout}\nStderr: ${stderr}`, |
| b69ab31 | | | 334 | ), |
| b69ab31 | | | 335 | ); |
| b69ab31 | | | 336 | return; |
| b69ab31 | | | 337 | } |
| b69ab31 | | | 338 | resolve(); |
| b69ab31 | | | 339 | }); |
| b69ab31 | | | 340 | }); |
| b69ab31 | | | 341 | } |
| b69ab31 | | | 342 | } |
| b69ab31 | | | 343 | |
| b69ab31 | | | 344 | /** |
| b69ab31 | | | 345 | * EdenFS Subscription |
| b69ab31 | | | 346 | * Handles real-time filesystem change notifications |
| b69ab31 | | | 347 | */ |
| b69ab31 | | | 348 | class EdenFSSubscription extends EventEmitter { |
| b69ab31 | | | 349 | /** @type {EdenFSNotificationsClient} */ |
| b69ab31 | | | 350 | client; |
| b69ab31 | | | 351 | /** @type {Object} */ |
| b69ab31 | | | 352 | options; |
| b69ab31 | | | 353 | /** @type {any} */ |
| b69ab31 | | | 354 | process; |
| b69ab31 | | | 355 | /** @type {string} */ |
| b69ab31 | | | 356 | edenBinaryPath; |
| b69ab31 | | | 357 | /** @type {string} */ |
| b69ab31 | | | 358 | errData; |
| b69ab31 | | | 359 | /** @type {NodeJS.Timeout | null} */ |
| b69ab31 | | | 360 | killTimeout; |
| b69ab31 | | | 361 | |
| b69ab31 | | | 362 | constructor(client, options = {}) { |
| b69ab31 | | | 363 | super(); |
| b69ab31 | | | 364 | this.client = client; |
| b69ab31 | | | 365 | this.options = options; |
| b69ab31 | | | 366 | this.process = null; |
| b69ab31 | | | 367 | this.edenBinaryPath = options?.edenBinaryPath ?? 'eden'; |
| b69ab31 | | | 368 | this.errData = ''; |
| b69ab31 | | | 369 | this.killTimeout = null; |
| b69ab31 | | | 370 | } |
| b69ab31 | | | 371 | |
| b69ab31 | | | 372 | /** |
| b69ab31 | | | 373 | * Start the subscription |
| b69ab31 | | | 374 | * @returns {Promise<void>} |
| b69ab31 | | | 375 | */ |
| b69ab31 | | | 376 | async start() { |
| b69ab31 | | | 377 | const mountPoint = this.options.mountPoint || this.client.mountPoint; |
| b69ab31 | | | 378 | const args = ['notify', 'changes-since', '--subscribe', '--json', '--formatted-position']; |
| b69ab31 | | | 379 | |
| b69ab31 | | | 380 | if (this.options.useCase) { |
| b69ab31 | | | 381 | args.push('--use-case', this.options.useCase); |
| b69ab31 | | | 382 | } else { |
| b69ab31 | | | 383 | args.push('--use-case', 'node-client'); |
| b69ab31 | | | 384 | } |
| b69ab31 | | | 385 | |
| b69ab31 | | | 386 | if (this.options.position) { |
| b69ab31 | | | 387 | args.push( |
| b69ab31 | | | 388 | '--position', |
| b69ab31 | | | 389 | typeof this.options.position === 'string' |
| b69ab31 | | | 390 | ? this.options.position |
| b69ab31 | | | 391 | : JSON.stringify(this.options.position), |
| b69ab31 | | | 392 | ); |
| b69ab31 | | | 393 | } |
| b69ab31 | | | 394 | |
| b69ab31 | | | 395 | if (this.options.throttle !== undefined) { |
| b69ab31 | | | 396 | args.push('--throttle', this.options.throttle.toString()); |
| b69ab31 | | | 397 | } |
| b69ab31 | | | 398 | |
| b69ab31 | | | 399 | if (this.options.relativeRoot) { |
| b69ab31 | | | 400 | args.push('--relative-root', this.options.relativeRoot); |
| b69ab31 | | | 401 | } |
| b69ab31 | | | 402 | |
| b69ab31 | | | 403 | if (this.options.includeVcsRoots) { |
| b69ab31 | | | 404 | args.push('--include-vcs-roots'); |
| b69ab31 | | | 405 | } |
| b69ab31 | | | 406 | |
| b69ab31 | | | 407 | if (this.options.includedRoots) { |
| b69ab31 | | | 408 | this.options.includedRoots.forEach(root => { |
| b69ab31 | | | 409 | args.push('--included-roots', root); |
| b69ab31 | | | 410 | }); |
| b69ab31 | | | 411 | } |
| b69ab31 | | | 412 | |
| b69ab31 | | | 413 | if (this.options.excludedRoots) { |
| b69ab31 | | | 414 | this.options.excludedRoots.forEach(root => { |
| b69ab31 | | | 415 | args.push('--excluded-roots', root); |
| b69ab31 | | | 416 | }); |
| b69ab31 | | | 417 | } |
| b69ab31 | | | 418 | |
| b69ab31 | | | 419 | if (this.options.includedSuffixes) { |
| b69ab31 | | | 420 | this.options.includedSuffixes.forEach(suffix => { |
| b69ab31 | | | 421 | args.push('--included-suffixes', suffix); |
| b69ab31 | | | 422 | }); |
| b69ab31 | | | 423 | } |
| b69ab31 | | | 424 | |
| b69ab31 | | | 425 | if (this.options.excludedSuffixes) { |
| b69ab31 | | | 426 | this.options.excludedSuffixes.forEach(suffix => { |
| b69ab31 | | | 427 | args.push('--excluded-suffixes', suffix); |
| b69ab31 | | | 428 | }); |
| b69ab31 | | | 429 | } |
| b69ab31 | | | 430 | |
| b69ab31 | | | 431 | if (this.options.deferredStates) { |
| b69ab31 | | | 432 | this.options.deferredStates.forEach(state => { |
| b69ab31 | | | 433 | args.push('--deferred-states', state); |
| b69ab31 | | | 434 | }); |
| b69ab31 | | | 435 | } |
| b69ab31 | | | 436 | |
| b69ab31 | | | 437 | if (mountPoint) { |
| b69ab31 | | | 438 | args.push(mountPoint); |
| b69ab31 | | | 439 | } |
| b69ab31 | | | 440 | |
| b69ab31 | | | 441 | return new Promise((resolve, reject) => { |
| b69ab31 | | | 442 | this.process = spawn(this.edenBinaryPath, args, { |
| b69ab31 | | | 443 | stdio: ['pipe', 'pipe', 'pipe'], |
| b69ab31 | | | 444 | }); |
| b69ab31 | | | 445 | |
| b69ab31 | | | 446 | let buffer = ''; |
| b69ab31 | | | 447 | |
| b69ab31 | | | 448 | const readline = require('readline'); |
| b69ab31 | | | 449 | const rl = readline.createInterface({input: this.process.stdout}); |
| b69ab31 | | | 450 | |
| b69ab31 | | | 451 | rl.on('line', line => { |
| b69ab31 | | | 452 | if (line.trim()) { |
| b69ab31 | | | 453 | try { |
| b69ab31 | | | 454 | const event = JSON.parse(line); |
| b69ab31 | | | 455 | this.emit('change', event); |
| b69ab31 | | | 456 | } catch (error) { |
| b69ab31 | | | 457 | this.emit('error', new Error(`Failed to parse event ${line}: ${error.message}`)); |
| b69ab31 | | | 458 | } |
| b69ab31 | | | 459 | } |
| b69ab31 | | | 460 | }); |
| b69ab31 | | | 461 | |
| b69ab31 | | | 462 | this.process.stderr.on('data', data => { |
| b69ab31 | | | 463 | this.errData += data.toString() + '\n'; |
| b69ab31 | | | 464 | }); |
| b69ab31 | | | 465 | |
| b69ab31 | | | 466 | this.process.on('close', (code, signal) => { |
| b69ab31 | | | 467 | if (code !== 0 && code !== null) { |
| b69ab31 | | | 468 | this.emit( |
| b69ab31 | | | 469 | 'error', |
| b69ab31 | | | 470 | new Error(`EdenFS process exited with code ${code}\nstderr: ${this.errData}`), |
| b69ab31 | | | 471 | ); |
| b69ab31 | | | 472 | } else if (signal !== null && signal !== 'SIGTERM') { |
| b69ab31 | | | 473 | this.emit('error', new Error(`EdenFS process killed with signal ${signal}`)); |
| b69ab31 | | | 474 | } else { |
| b69ab31 | | | 475 | this.emit('close'); |
| b69ab31 | | | 476 | } |
| b69ab31 | | | 477 | }); |
| b69ab31 | | | 478 | |
| b69ab31 | | | 479 | this.process.on('error', error => { |
| b69ab31 | | | 480 | this.emit('error', error); |
| b69ab31 | | | 481 | reject(error); |
| b69ab31 | | | 482 | }); |
| b69ab31 | | | 483 | |
| b69ab31 | | | 484 | this.process.on('spawn', () => { |
| b69ab31 | | | 485 | resolve(); |
| b69ab31 | | | 486 | }); |
| b69ab31 | | | 487 | |
| b69ab31 | | | 488 | this.process.on('exit', (code, signal) => { |
| b69ab31 | | | 489 | if (this.killTimeout !== null) { |
| b69ab31 | | | 490 | clearTimeout(this.killTimeout); |
| b69ab31 | | | 491 | this.killTimeout = null; |
| b69ab31 | | | 492 | } |
| b69ab31 | | | 493 | this.emit('exit'); |
| b69ab31 | | | 494 | }); |
| b69ab31 | | | 495 | }); |
| b69ab31 | | | 496 | } |
| b69ab31 | | | 497 | |
| b69ab31 | | | 498 | /** |
| b69ab31 | | | 499 | * Stop the subscription |
| b69ab31 | | | 500 | */ |
| b69ab31 | | | 501 | stop() { |
| b69ab31 | | | 502 | if (this.process) { |
| b69ab31 | | | 503 | this.process.kill('SIGTERM'); |
| b69ab31 | | | 504 | this.killTimeout = setTimeout(() => { |
| b69ab31 | | | 505 | this.process.kill('SIGKILL'); |
| b69ab31 | | | 506 | }, 500); |
| b69ab31 | | | 507 | } |
| b69ab31 | | | 508 | } |
| b69ab31 | | | 509 | } |
| b69ab31 | | | 510 | |
| b69ab31 | | | 511 | /** |
| b69ab31 | | | 512 | * Utility functions for working with EdenFS notify data |
| b69ab31 | | | 513 | */ |
| b69ab31 | | | 514 | class EdenFSUtils { |
| b69ab31 | | | 515 | /** |
| b69ab31 | | | 516 | * Convert byte array path to string |
| b69ab31 | | | 517 | * @param {number[]} pathBytes - Array of byte values representing a path |
| b69ab31 | | | 518 | * @returns {string} Path string |
| b69ab31 | | | 519 | */ |
| b69ab31 | | | 520 | static bytesToPath(pathBytes) { |
| b69ab31 | | | 521 | return Buffer.from(pathBytes).toString('utf8'); |
| b69ab31 | | | 522 | } |
| b69ab31 | | | 523 | |
| b69ab31 | | | 524 | /** |
| b69ab31 | | | 525 | * Convert byte array to hex string |
| b69ab31 | | | 526 | * @param {number[]} bytes - Array of byte values |
| b69ab31 | | | 527 | * @returns {string} Hexadecimal string |
| b69ab31 | | | 528 | */ |
| b69ab31 | | | 529 | static bytesToHex(bytes) { |
| b69ab31 | | | 530 | return Buffer.from(bytes).toString('hex'); |
| b69ab31 | | | 531 | } |
| b69ab31 | | | 532 | |
| b69ab31 | | | 533 | /** |
| b69ab31 | | | 534 | * Extract file type from change |
| b69ab31 | | | 535 | * @param {Object} change - change object |
| b69ab31 | | | 536 | * @returns {{string}} File Type |
| b69ab31 | | | 537 | */ |
| b69ab31 | | | 538 | static extractFileType(smallChange) { |
| b69ab31 | | | 539 | if (smallChange.Added && smallChange.Added.file_type) { |
| b69ab31 | | | 540 | return smallChange.Added.file_type; |
| b69ab31 | | | 541 | } else if (smallChange.Modified && smallChange.Modified.file_type) { |
| b69ab31 | | | 542 | return smallChange.Modified.file_type; |
| b69ab31 | | | 543 | } else if (smallChange.Removed && smallChange.Removed.file_type) { |
| b69ab31 | | | 544 | return smallChange.Removed.file_type; |
| b69ab31 | | | 545 | } else if (smallChange.Renamed) { |
| b69ab31 | | | 546 | return smallChange.Renamed.file_type; |
| b69ab31 | | | 547 | } else if (smallChange.Replaced) { |
| b69ab31 | | | 548 | return smallChange.Replaced.file_type; |
| b69ab31 | | | 549 | } |
| b69ab31 | | | 550 | } |
| b69ab31 | | | 551 | |
| b69ab31 | | | 552 | /** |
| b69ab31 | | | 553 | * Extract file path(s) from change |
| b69ab31 | | | 554 | * @param {Object} change - change object |
| b69ab31 | | | 555 | * @returns {{string, string | undefined}} First file path, and possible second file path |
| b69ab31 | | | 556 | */ |
| b69ab31 | | | 557 | static extractPath(smallChange) { |
| b69ab31 | | | 558 | if (smallChange.Added && smallChange.Added.path) { |
| b69ab31 | | | 559 | return [this.bytesToPath(smallChange.Added.path), undefined]; |
| b69ab31 | | | 560 | } else if (smallChange.Modified && smallChange.Modified.path) { |
| b69ab31 | | | 561 | return [this.bytesToPath(smallChange.Modified.path), undefined]; |
| b69ab31 | | | 562 | } else if (smallChange.Removed && smallChange.Removed.path) { |
| b69ab31 | | | 563 | return [this.bytesToPath(smallChange.Removed.path), undefined]; |
| b69ab31 | | | 564 | } else if (smallChange.Renamed) { |
| b69ab31 | | | 565 | return [this.bytesToPath(smallChange.Renamed.from), this.bytesToPath(smallChange.Renamed.to)]; |
| b69ab31 | | | 566 | } else if (smallChange.Replaced) { |
| b69ab31 | | | 567 | return [ |
| b69ab31 | | | 568 | this.bytesToPath(smallChange.Replaced.from), |
| b69ab31 | | | 569 | this.bytesToPath(smallChange.Replaced.to), |
| b69ab31 | | | 570 | ]; |
| b69ab31 | | | 571 | } else { |
| b69ab31 | | | 572 | return ['', undefined]; |
| b69ab31 | | | 573 | } |
| b69ab31 | | | 574 | } |
| b69ab31 | | | 575 | |
| b69ab31 | | | 576 | /** |
| b69ab31 | | | 577 | * Extract file paths from changes |
| b69ab31 | | | 578 | * @param {Object[]} changes - Array of change objects |
| b69ab31 | | | 579 | * @returns {string[]} Array of file paths |
| b69ab31 | | | 580 | */ |
| b69ab31 | | | 581 | static extractPaths(changes) { |
| b69ab31 | | | 582 | const paths = []; |
| b69ab31 | | | 583 | |
| b69ab31 | | | 584 | changes.forEach(change => { |
| b69ab31 | | | 585 | if (change.SmallChange) { |
| b69ab31 | | | 586 | let [path1, path2] = this.extractPath(change.SmallChange); |
| b69ab31 | | | 587 | if (path1) { |
| b69ab31 | | | 588 | paths.push(path1); |
| b69ab31 | | | 589 | } |
| b69ab31 | | | 590 | if (path2) { |
| b69ab31 | | | 591 | paths.push(path2); |
| b69ab31 | | | 592 | } |
| b69ab31 | | | 593 | } |
| b69ab31 | | | 594 | }); |
| b69ab31 | | | 595 | |
| b69ab31 | | | 596 | return paths; |
| b69ab31 | | | 597 | } |
| b69ab31 | | | 598 | |
| b69ab31 | | | 599 | /** |
| b69ab31 | | | 600 | * Get change type from a change object |
| b69ab31 | | | 601 | * @param {Object} change - Change object |
| b69ab31 | | | 602 | * @returns {string} Change type |
| b69ab31 | | | 603 | */ |
| b69ab31 | | | 604 | static getChangeType(change) { |
| b69ab31 | | | 605 | if (change.SmallChange) { |
| b69ab31 | | | 606 | const smallChange = change.SmallChange; |
| b69ab31 | | | 607 | |
| b69ab31 | | | 608 | if (smallChange.Added) return 'added'; |
| b69ab31 | | | 609 | if (smallChange.Modified) return 'modified'; |
| b69ab31 | | | 610 | if (smallChange.Removed) return 'removed'; |
| b69ab31 | | | 611 | if (smallChange.Renamed) return 'renamed'; |
| b69ab31 | | | 612 | if (smallChange.Replaced) return 'replaced'; |
| b69ab31 | | | 613 | } else if (change.LargeChange) { |
| b69ab31 | | | 614 | const largeChange = change.LargeChange; |
| b69ab31 | | | 615 | if (largeChange.DirectoryRenamed) return 'directory renamed'; |
| b69ab31 | | | 616 | if (largeChange.CommitTransition) return 'commit transition'; |
| b69ab31 | | | 617 | if (largeChange.LostChange) return 'lost change'; |
| b69ab31 | | | 618 | } else if (change.StateChange) { |
| b69ab31 | | | 619 | const stateChange = change.StateChange; |
| b69ab31 | | | 620 | if (stateChange.StateEntered) return 'state entered'; |
| b69ab31 | | | 621 | if (stateChange.StateLeft) return 'state left'; |
| b69ab31 | | | 622 | } |
| b69ab31 | | | 623 | |
| b69ab31 | | | 624 | return 'unknown'; |
| b69ab31 | | | 625 | } |
| b69ab31 | | | 626 | } |
| b69ab31 | | | 627 | |
| b69ab31 | | | 628 | module.exports = { |
| b69ab31 | | | 629 | EdenFSNotificationsClient, |
| b69ab31 | | | 630 | EdenFSSubscription, |
| b69ab31 | | | 631 | EdenFSUtils, |
| b69ab31 | | | 632 | }; |