1 | # random-access-file
|
2 |
|
3 | Continuous reading or writing to a file using random offsets and lengths
|
4 |
|
5 | ```
|
6 | npm install random-access-file
|
7 | ```
|
8 |
|
9 | ## Why?
|
10 |
|
11 | If you are receiving a file in multiple pieces in a distributed system it can be useful to write these pieces to disk one by one in various places throughout the file without having to open and close a file descriptor all the time.
|
12 |
|
13 | random-access-file allows you to do just this.
|
14 |
|
15 | ## Usage
|
16 |
|
17 | ``` js
|
18 | const RandomAccessFile = require('random-access-file')
|
19 |
|
20 | const file = new RandomAccessFile('my-file.txt')
|
21 |
|
22 | file.write(10, Buffer.from('hello'), function(err) {
|
23 | // write a buffer to offset 10
|
24 | file.read(10, 5, function(err, buffer) {
|
25 | console.log(buffer) // read 5 bytes from offset 10
|
26 | file.close(function() {
|
27 | console.log('file is closed')
|
28 | })
|
29 | })
|
30 | })
|
31 | ```
|
32 |
|
33 | file will use an open file descriptor. When you are done with the file you should call `file.close()`.
|
34 |
|
35 | ## API
|
36 |
|
37 | #### `const file = new RandomAccessFile(filename, [options])`
|
38 |
|
39 | Create a new file. Options include:
|
40 |
|
41 | ``` js
|
42 | {
|
43 | truncate: false, // truncate the file before reading / writing
|
44 | size: someSize, // truncate the file to this size first
|
45 | readable: true, // should the file be opened as readable?
|
46 | writable: true, // should the file be opened as writable?
|
47 | lock: false, // lock the file
|
48 | sparse: false // mark the file as sparse
|
49 | }
|
50 | ```
|
51 |
|
52 | #### `file.write(offset, buffer, [callback])`
|
53 |
|
54 | Write a buffer at a specific offset.
|
55 |
|
56 | #### `file.read(offset, length, callback)`
|
57 |
|
58 | Read a buffer at a specific offset. Callback is called with the buffer read.
|
59 |
|
60 | #### `file.del(offset, length, callback)`
|
61 |
|
62 | Delete a portion of the file. Any partial file blocks in the deleted portion are zeroed and, if the file is sparse, the remaining file blocks unlinked in-place.
|
63 |
|
64 | #### `file.truncate(offset, callback)`
|
65 |
|
66 | Truncate the file length to this offset.
|
67 |
|
68 | #### `file.stat(callback)`
|
69 |
|
70 | Stat the storage. Should return an object with useful information about the underlying storage, including:
|
71 |
|
72 | ```js
|
73 | {
|
74 | size: number // how many bytes of data is stored?
|
75 | }
|
76 | ```
|
77 |
|
78 | #### `file.close([callback])`
|
79 |
|
80 | Close the underlying file descriptor.
|
81 |
|
82 | #### `file.destroy([callback])`
|
83 |
|
84 | Unlink the underlying file.
|
85 |
|
86 | #### `file.on('open')`
|
87 |
|
88 | Emitted when the file descriptor has been opened. You can access the fd using `file.fd`.
|
89 | You do not need to wait for this event before doing any reads/writes.
|
90 |
|
91 | #### `file.on('close')`
|
92 |
|
93 | Emitted when the file has been closed.
|
94 |
|
95 | ## License
|
96 |
|
97 | MIT
|