Skip to content

Commit cd1a9bf

Browse files
committed
Add types for random-access-storage
1 parent 5ebe98a commit cd1a9bf

File tree

5 files changed

+364
-0
lines changed

5 files changed

+364
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"private": true,
3+
"name": "@types/random-access-storage",
4+
"version": "3.0.9999",
5+
"projects": [
6+
"https://github.com/random-access-storage/random-access-storage"
7+
],
8+
"dependencies": {
9+
"@types/node": "*"
10+
},
11+
"devDependencies": {
12+
"@types/random-access-storage": "workspace:."
13+
},
14+
"owners": [
15+
{
16+
"name": "Evan Hahn",
17+
"githubUsername": "EvanHahn"
18+
}
19+
]
20+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/// <reference types="node" />
2+
import RandomAccessStorage = require("random-access-storage");
3+
4+
const storage = new RandomAccessStorage();
5+
6+
// $ExpectType boolean
7+
storage.readable;
8+
// $ExpectType boolean
9+
storage.writable;
10+
// $ExpectType boolean
11+
storage.deletable;
12+
// $ExpectType boolean
13+
storage.truncatable;
14+
// $ExpectType boolean
15+
storage.statable;
16+
// $ExpectType boolean
17+
storage.opened;
18+
// $ExpectType boolean
19+
storage.closed;
20+
// $ExpectType boolean
21+
storage.unlinked;
22+
// $ExpectType boolean
23+
storage.writing;
24+
25+
const events = ["open", "close", "unlink", "suspend", "unsuspend"] as const;
26+
events.forEach(event => {
27+
storage.on(event, () => {});
28+
});
29+
30+
storage.open((_err: null | Error) => {});
31+
storage.read(12, 34, (_err: null | Error, _data?: Buffer) => {});
32+
storage.write(123, Buffer.alloc(0), (_err: null | Error) => {});
33+
storage.del(12, 34, (_err: null | Error) => {});
34+
storage.truncate(123, (_err: null | Error) => {});
35+
storage.stat((_err: null | Error, _stats?: { size: number }) => {});
36+
storage.suspend((_err: null | Error) => {});
37+
storage.close((_err: null | Error) => {});
38+
storage.unlink((_err: null | Error) => {});
39+
40+
new RandomAccessStorage({});
41+
new RandomAccessStorage({
42+
createAlways: true,
43+
open(req) {
44+
// $ExpectType boolean
45+
req.create;
46+
req.callback(null);
47+
req.callback(new Error("test error"));
48+
},
49+
read(req) {
50+
// $ExpectType number
51+
req.offset;
52+
// $ExpectType number
53+
req.size;
54+
req.callback(null, Buffer.alloc(0));
55+
req.callback(new Error("test error"));
56+
},
57+
write(req) {
58+
// $ExpectType number
59+
req.offset;
60+
// $ExpectType Buffer
61+
req.data;
62+
req.callback(null);
63+
req.callback(new Error("test error"));
64+
},
65+
del(req) {
66+
// $ExpectType number
67+
req.offset;
68+
// $ExpectType number
69+
req.size;
70+
req.callback(null);
71+
req.callback(new Error("test error"));
72+
},
73+
truncate(req) {
74+
// $ExpectType number
75+
req.offset;
76+
req.callback(null);
77+
req.callback(new Error("test error"));
78+
},
79+
stat(req) {
80+
req.callback(null, { size: 123 });
81+
req.callback(new Error("test error"));
82+
},
83+
suspend(req) {
84+
req.callback(null);
85+
req.callback(new Error("test error"));
86+
},
87+
close(req) {
88+
req.callback(null);
89+
req.callback(new Error("test error"));
90+
},
91+
unlink(req) {
92+
req.callback(null);
93+
req.callback(new Error("test error"));
94+
},
95+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"types": [],
12+
"noEmit": true,
13+
"forceConsistentCasingInFileNames": true
14+
},
15+
"files": [
16+
"index.d.ts",
17+
"random-access-storage-tests.ts"
18+
]
19+
}

0 commit comments

Comments
 (0)