|
| 1 | +/// <reference types="node" /> |
| 2 | + |
| 3 | +import type { EventEmitter } from "events"; |
| 4 | + |
| 5 | +type Callback<T = void> = (err: null | Error, value?: T) => void; |
| 6 | + |
| 7 | +declare class RandomAccessStorage< |
| 8 | + TStatObject extends { size: number } = { size: number }, |
| 9 | +> extends EventEmitter { |
| 10 | + /** True if the storage implements `._read`. */ |
| 11 | + readable: boolean; |
| 12 | + |
| 13 | + /** True if the storage implements `._write`. */ |
| 14 | + writable: boolean; |
| 15 | + |
| 16 | + /** True if the storage implements `._del`. */ |
| 17 | + deletable: boolean; |
| 18 | + |
| 19 | + /** True if the storage implements `._truncate`. */ |
| 20 | + truncatable: boolean; |
| 21 | + |
| 22 | + /** True if the storage implements `._stat`. */ |
| 23 | + statable: boolean; |
| 24 | + |
| 25 | + /** True if the storage has been fully opened. */ |
| 26 | + opened: boolean; |
| 27 | + |
| 28 | + /** True if the storage has been fully closed. */ |
| 29 | + closed: boolean; |
| 30 | + |
| 31 | + /** True if the storage has been fully unlinked. */ |
| 32 | + unlinked: boolean; |
| 33 | + |
| 34 | + /** True if the storage is currently being written to. */ |
| 35 | + writing: boolean; |
| 36 | + |
| 37 | + /** Make a new instance. */ |
| 38 | + constructor( |
| 39 | + options?: Readonly<{ |
| 40 | + /** always create storage on first open */ |
| 41 | + createAlways?: boolean; |
| 42 | + /** sets ._open */ |
| 43 | + open?: typeof RandomAccessStorage.prototype._open; |
| 44 | + /** sets ._read */ |
| 45 | + read?: typeof RandomAccessStorage.prototype._read; |
| 46 | + /** sets ._write */ |
| 47 | + write?: typeof RandomAccessStorage.prototype._write; |
| 48 | + /** sets ._del */ |
| 49 | + del?: typeof RandomAccessStorage.prototype._del; |
| 50 | + /** sets ._truncate */ |
| 51 | + truncate?: typeof RandomAccessStorage.prototype._truncate; |
| 52 | + /** sets ._stat */ |
| 53 | + stat?: typeof RandomAccessStorage.prototype._stat; |
| 54 | + /** sets ._suspend */ |
| 55 | + suspend?: typeof RandomAccessStorage.prototype._suspend; |
| 56 | + /** sets ._close */ |
| 57 | + close?: typeof RandomAccessStorage.prototype._close; |
| 58 | + /** sets ._unlink */ |
| 59 | + unlink?: typeof RandomAccessStorage.prototype._unlink; |
| 60 | + }>, |
| 61 | + ); |
| 62 | + |
| 63 | + override on(name: "open" | "close" | "unlink" | "suspend" | "unsuspend", handler: () => unknown): this; |
| 64 | + |
| 65 | + /** |
| 66 | + * Explicitly open the storage. If you do not call this yourself, it will |
| 67 | + * automatically called before any read/write/del/stat operation. |
| 68 | + * |
| 69 | + * It is safe to call this more than once. |
| 70 | + * |
| 71 | + * Triggers *one* call to `_open` if you implement that. |
| 72 | + */ |
| 73 | + open(callback: Callback): void; |
| 74 | + |
| 75 | + /** |
| 76 | + * Implement storage open. |
| 77 | + * |
| 78 | + * - `req.create` is true if the storage should be created. |
| 79 | + * |
| 80 | + * Call `req.callback` when it is fully opened. |
| 81 | + */ |
| 82 | + _open(req: { |
| 83 | + create: boolean; |
| 84 | + callback: Callback; |
| 85 | + }): void; |
| 86 | + |
| 87 | + /** |
| 88 | + * Read the specified bytes from the storage at the specified byte offset. |
| 89 | + * Calls the callback with a `(err, buffer)`. |
| 90 | + */ |
| 91 | + read(offset: number, size: number, callback: Callback<Buffer>): void; |
| 92 | + |
| 93 | + /** |
| 94 | + * Implement storage read. |
| 95 | + * |
| 96 | + * - `req.offset` contains the byte offset you should read at. |
| 97 | + * - `req.size` contains the amount of bytes you should read. |
| 98 | + * |
| 99 | + * Call `req.callback(err, buffer)` when the read is completed. |
| 100 | + * |
| 101 | + * Note that this is guaranteed to run after the storage has been opened and |
| 102 | + * not after it has been closed. |
| 103 | + */ |
| 104 | + _read(req: { offset: number; size: number; callback: Callback<Buffer> }): void; |
| 105 | + |
| 106 | + /** |
| 107 | + * Write the specified buffer to the specified byte offset. Optionally pass a |
| 108 | + * callback that is called with `(err)` when the write has completed. |
| 109 | + */ |
| 110 | + write(offset: number, buffer: Buffer, callback?: Callback): void; |
| 111 | + |
| 112 | + /** |
| 113 | + * Implement storage write. |
| 114 | + * |
| 115 | + * - `req.offset` contains the byte offset you should write at. |
| 116 | + * - `req.data` contains the buffer you should write. |
| 117 | + * |
| 118 | + * Call `req.callback(err)` when the write is completed. |
| 119 | + * |
| 120 | + * Note that this is guaranteed to run after the storage has been opened |
| 121 | + * and not after it has been closed. |
| 122 | + */ |
| 123 | + _write(req: { offset: number; data: Buffer; callback: Callback }): void; |
| 124 | + |
| 125 | + /** |
| 126 | + * Delete the specified amount of bytes at the specified offset. Optionally |
| 127 | + * pass a callback that is called with `(err)` when the delete has |
| 128 | + * completed. |
| 129 | + */ |
| 130 | + del(offset: number, size: number, callback?: Callback): void; |
| 131 | + |
| 132 | + /** |
| 133 | + * Implement storage delete. |
| 134 | + * |
| 135 | + * - `req.offset` contains the byte offset to delete at. |
| 136 | + * - `req.size` contains the amount of bytes to delete. |
| 137 | + * |
| 138 | + * Call `req.callback(err)` when the delete has completed. |
| 139 | + * |
| 140 | + * Note that this is guaranteed to run after the storage has been opened and |
| 141 | + * not after it has been closed. |
| 142 | + */ |
| 143 | + _del(req: { offset: number; size: number; callback: Callback }): void; |
| 144 | + |
| 145 | + /** |
| 146 | + * Truncate the storage at the specified offset. Optionally pass a callback |
| 147 | + * that is called with `(err)` when the truncate has completed. |
| 148 | + */ |
| 149 | + truncate(offset: number, callback?: Callback): void; |
| 150 | + |
| 151 | + /** |
| 152 | + * Implement storage truncate. Defaults to `storage._del(req)`. |
| 153 | + * |
| 154 | + * - `req.offset` contains the byte offset to truncate at. |
| 155 | + * |
| 156 | + * Call `req.callback(err)` when the truncate has completed. |
| 157 | + * |
| 158 | + * Note that this is guaranteed to run after the storage has been opened and |
| 159 | + * not after it has been closed. |
| 160 | + */ |
| 161 | + _truncate(req: { offset: number; callback: Callback }): void; |
| 162 | + |
| 163 | + /** |
| 164 | + * Stat the storage. Should return an object with useful information about the |
| 165 | + * underlying storage. |
| 166 | + */ |
| 167 | + stat(callback: Callback<TStatObject>): void; |
| 168 | + |
| 169 | + /** |
| 170 | + * Implement storage stat. |
| 171 | + * |
| 172 | + * Call `req.callback(err, statObject)` when the stat has completed. |
| 173 | + * |
| 174 | + * Note that this is guaranteed to run after the storage has been opened and |
| 175 | + * not after it has been closed. |
| 176 | + */ |
| 177 | + _stat(req: { callback: Callback<TStatObject> }): void; |
| 178 | + |
| 179 | + /** |
| 180 | + * Suspend (temporarily close) the storage instance. |
| 181 | + */ |
| 182 | + suspend(callback?: Callback): void; |
| 183 | + |
| 184 | + /** |
| 185 | + * Implement storage suspend. Defaults to calling `_close`. |
| 186 | + * |
| 187 | + * Optionally implement this to add a way for your storage instance to |
| 188 | + * temporarily free resources. |
| 189 | + * |
| 190 | + * Call `req.callback(err)` when the storage has been fully suspended. |
| 191 | + */ |
| 192 | + _suspend(req: { callback: Callback }): void; |
| 193 | + |
| 194 | + /** |
| 195 | + * Close the storage instance. |
| 196 | + */ |
| 197 | + close(callback?: Callback): void; |
| 198 | + |
| 199 | + /** |
| 200 | + * Implement storage close |
| 201 | + * |
| 202 | + * Call `req.callback(err)` when the storage is fully closed. |
| 203 | + * |
| 204 | + * Note this is guaranteed to run after all pending read/write/stat/del |
| 205 | + * operations has finished and no methods will run after. |
| 206 | + */ |
| 207 | + _close(req: { callback: Callback }): void; |
| 208 | + |
| 209 | + /** |
| 210 | + * Unlink the storage instance, removing all underlying data. |
| 211 | + */ |
| 212 | + unlink(callback?: Callback): void; |
| 213 | + |
| 214 | + /** |
| 215 | + * Implement storage unlink. |
| 216 | + * |
| 217 | + * Call `req.callback(err)` when the storage has been fully unlinked. |
| 218 | + * |
| 219 | + * Note this is guaranteed to run after `.close()` has been called and no |
| 220 | + * methods will be run after. |
| 221 | + */ |
| 222 | + _unlink(req: { callback: Callback }): void; |
| 223 | +} |
| 224 | + |
| 225 | +export = RandomAccessStorage; |
0 commit comments