Repository navigation
Expand file tree
/
Copy pathWriteOperation.js
More file actions
246 lines (227 loc) · 7.03 KB
/
Copy pathWriteOperation.js
File metadata and controls
246 lines (227 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import fs from 'fs';
import { createWriteStream } from 'fs';
import { Mutex } from 'another-mutex';
/**
* Mutex map to ensure sequential
* writes per file: each file path
* gets its own mutex to prevent
* concurrent write operations.
*/
const writeMutexes = new Map();
/**
* Gets or creates a mutex for one file path.
* @param {string} filePath - The file path to get a mutex for
* @returns {Mutex} The mutex instance for this file path
*/
function getMutex(filePath) {
if (!writeMutexes.has(filePath)) {
writeMutexes.set(filePath, new Mutex());
}
return writeMutexes.get(filePath);
}
/**
* Represents a chainable write operation for files, device files, and FIFOs.
* Supports mutex-based sequential writing, customizable retry strategies, and device-specific handling.
*/
class WriteOperation {
/**
* Creates a new WriteOperation instance.
* @param {string} filePath - Path to the file, device, or FIFO to write to
* @param {string|Buffer} data - Data to write to the file
* @param {Object} options - Configuration options
* @param {Function} [options.writeFileExistsRetryStrategy] - Custom retry strategy for file existence checks
* @param {Function} [options.writeFileRetryStrategy] - Custom retry strategy for write failures
*/
constructor(filePath, data, options) {
this._filePath = filePath;
this._data = data;
this._options = options;
this._finishCallback = null;
this._errorCallback = null;
}
/**
* Sets the callback for when writing completes successfully.
* @param {Function} callback - Function to call when writing finishes () => void
* @returns {WriteOperation} This operation for chaining
*/
onFinish(callback) {
this._finishCallback = callback;
return this;
}
/**
* Sets the error callback.
* @param {Function} callback - Function to call on error (error: Error) => void
* @returns {WriteOperation} This operation for chaining
*/
onError(callback) {
this._errorCallback = callback;
return this;
}
/**
* Executes the write operation.
* This should be called last in the chain.
*/
write() {
this._execute();
return this;
}
/**
* Starts the write operation execution by acquiring a mutex lock.
* Ensures sequential writes per file path.
* @private
*/
_execute() {
const mtx = getMutex(this._filePath);
mtx.lock()
.then((unlock) => {
this._waitForFileAndWrite(1, unlock);
})
.catch((err) => {
this._handleError(err);
});
}
/**
* Waits for file/directory to be ready and then starts writing.
* For device files and FIFOs, checks file existence.
* For regular files, creates parent directories if needed.
* @param {number} attempt - Current attempt number
* @param {Function} unlock - Function to unlock the mutex.
* @private
*/
_waitForFileAndWrite(attempt, unlock) {
// For device files and FIFOs, check file existence
if (this._filePath.startsWith('/dev/') || this._isNamedPipe()) {
fs.access(this._filePath, fs.constants.F_OK, (err) => {
if (err) {
this._handleFileExistsError(err, attempt, unlock);
} else {
this._performWrite(1, unlock);
}
});
} else {
// For regular files, create parent directory if needed
const dir = this._filePath.substring(0, this._filePath.lastIndexOf('/'));
if (dir) {
fs.mkdir(dir, { recursive: true }, (err) => {
if (err && err.code !== 'EEXIST') {
this._handleFileExistsError(err, attempt, unlock);
} else {
this._performWrite(1, unlock);
}
});
} else {
this._performWrite(1, unlock);
}
}
}
/**
* Handles file existence errors using the configured retry strategy.
* @param {Error} error - The error that occurred
* @param {number} attempt - Current attempt number
* @param {Function} unlock - Function to unlock the mutex.
* @private
*/
_handleFileExistsError(error, attempt, unlock) {
try {
const delay = this._options.writeFileExistsRetryStrategy(error, attempt, this._filePath);
if (typeof delay === 'number') {
setTimeout(() => this._waitForFileAndWrite(attempt + 1, unlock), delay);
} else {
setTimeout(() => this._waitForFileAndWrite(attempt + 1, unlock), 1);
}
} catch (err) {
unlock();
this._handleError(err);
}
}
/**
* Performs the actual write operation with retry logic.
* @param {number} attempt - Current attempt number
* @param {Function} unlock - Function to unlock the mutex.
* @private
*/
_performWrite(attempt, unlock) {
try {
const writeStream = this._createWriteStream();
writeStream.on('finish', () => {
unlock();
if (this._finishCallback) {
this._finishCallback();
}
});
writeStream.on('error', (error) => {
try {
const delay = this._options.writeFileRetryStrategy(error, attempt, this._filePath);
if (typeof delay === 'number') {
setTimeout(() => this._performWrite(attempt + 1, unlock), delay);
} else {
this._performWrite(attempt + 1, unlock);
}
} catch (err) {
unlock();
this._handleError(err);
}
});
// Write the data
if (typeof this._data === 'string') {
writeStream.write(this._data, 'utf8');
} else {
writeStream.write(this._data);
}
writeStream.end();
} catch (error) {
unlock();
this._handleError(error);
}
}
/**
* Creates a write stream with appropriate flags for different file types.
* Handles device files (character/block), FIFOs, and regular files differently.
* @returns {fs.WriteStream} The configured write stream
* @private
*/
_createWriteStream() {
const isDeviceFile = this._filePath.startsWith('/dev/');
if (isDeviceFile) {
const stats = fs.statSync(this._filePath);
if (stats.isCharacterDevice()) {
try {
return createWriteStream(this._filePath, { flags: 'r+' });
} catch (err) {
return createWriteStream(this._filePath, { flags: 'w' });
}
} else if (stats.isBlockDevice()) {
return createWriteStream(this._filePath, { flags: 'w' });
} else {
return createWriteStream(this._filePath, { flags: 'r+' });
}
} else {
// regular files and FIFOs
return createWriteStream(this._filePath);
}
}
/**
* Checks if the file path refers to a named pipe (FIFO).
* @returns {boolean} True if the file is a FIFO, false otherwise
* @private
*/
_isNamedPipe() {
try {
const stats = fs.statSync(this._filePath);
return stats.isFIFO();
} catch (err) {
return false;
}
}
/**
* Handles errors by calling the registered error callback.
* @param {Error} error - The error to handle
* @private
*/
_handleError(error) {
if (this._errorCallback) {
this._errorCallback(error);
}
}
}
export default WriteOperation;