Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 46059f1

Browse files
authoredMar 13, 2023
feat!: update to latest datastore interface (#144)
- Updates datastore interface - Open/close are no longer part of the interface - Converts this module to typescript - Updates all dependencies BREAKING CHANGE: update to latest datastore interface
·
v9.0.4v9.0.0
1 parent eb19c0f commit 46059f1

29 files changed

+559
-1187
lines changed
 

‎package.json‎

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,43 +49,43 @@
4949
"exports": {
5050
".": {
5151
"types": "./dist/src/index.d.ts",
52-
"import": "./src/index.js"
52+
"import": "./dist/src/index.js"
5353
},
5454
"./base": {
55-
"types": "./src/base.d.ts",
56-
"import": "./src/base.js"
55+
"types": "./dist/src/base.d.ts",
56+
"import": "./dist/src/base.js"
5757
},
5858
"./errors": {
59-
"types": "./src/errors.d.ts",
60-
"import": "./src/errors.js"
59+
"types": "./dist/src/errors.d.ts",
60+
"import": "./dist/src/errors.js"
6161
},
6262
"./keytransform": {
63-
"types": "./src/keytransform.d.ts",
64-
"import": "./src/keytransform.js"
63+
"types": "./dist/src/keytransform.d.ts",
64+
"import": "./dist/src/keytransform.js"
6565
},
6666
"./memory": {
67-
"types": "./src/memory.d.ts",
68-
"import": "./src/memory.js"
67+
"types": "./dist/src/memory.d.ts",
68+
"import": "./dist/src/memory.js"
6969
},
7070
"./mount": {
71-
"types": "./src/mount.d.ts",
72-
"import": "./src/mount.js"
71+
"types": "./dist/src/mount.d.ts",
72+
"import": "./dist/src/mount.js"
7373
},
7474
"./namespace": {
75-
"types": "./src/namespace.d.ts",
76-
"import": "./src/namespace.js"
75+
"types": "./dist/src/namespace.d.ts",
76+
"import": "./dist/src/namespace.js"
7777
},
7878
"./shard": {
79-
"types": "./src/shard.d.ts",
80-
"import": "./src/shard.js"
79+
"types": "./dist/src/shard.d.ts",
80+
"import": "./dist/src/shard.js"
8181
},
8282
"./sharding": {
83-
"types": "./src/sharding.d.ts",
84-
"import": "./src/sharding.js"
83+
"types": "./dist/src/sharding.d.ts",
84+
"import": "./dist/src/sharding.js"
8585
},
8686
"./tiered": {
87-
"types": "./src/tiered.d.ts",
88-
"import": "./src/tiered.js"
87+
"types": "./dist/src/tiered.d.ts",
88+
"import": "./dist/src/tiered.js"
8989
}
9090
},
9191
"eslintConfig": {
@@ -196,19 +196,21 @@
196196
"dependencies": {
197197
"@libp2p/logger": "^2.0.0",
198198
"err-code": "^3.0.1",
199-
"interface-datastore": "^7.0.0",
199+
"interface-store": "^4.0.0",
200200
"it-all": "^2.0.0",
201201
"it-drain": "^2.0.0",
202202
"it-filter": "^2.0.0",
203203
"it-map": "^2.0.0",
204204
"it-merge": "^2.0.0",
205205
"it-pipe": "^2.0.3",
206206
"it-pushable": "^3.0.0",
207+
"it-sort": "^2.0.1",
207208
"it-take": "^2.0.0",
208209
"uint8arrays": "^4.0.2"
209210
},
210211
"devDependencies": {
211-
"aegir": "^37.4.5",
212-
"interface-datastore-tests": "^3.0.0"
212+
"aegir": "^38.1.7",
213+
"interface-datastore": "^8.0.0",
214+
"interface-datastore-tests": "^4.0.0"
213215
}
214216
}

‎src/base.js‎

Lines changed: 0 additions & 226 deletions
This file was deleted.

‎src/base.ts‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import sort from 'it-sort'
2+
import drain from 'it-drain'
3+
import filter from 'it-filter'
4+
import take from 'it-take'
5+
import type { Batch, Datastore, Key, KeyQuery, Options, Pair, Query } from 'interface-datastore'
6+
import type { AwaitIterable } from 'interface-store'
7+
8+
export class BaseDatastore implements Datastore {
9+
async open (): Promise<void> {
10+
11+
}
12+
13+
async close (): Promise<void> {
14+
15+
}
16+
17+
async put (key: Key, val: Uint8Array, options?: Options): Promise<void> {
18+
await Promise.reject(new Error('.put is not implemented'))
19+
}
20+
21+
async get (key: Key, options?: Options): Promise<Uint8Array> {
22+
return await Promise.reject(new Error('.get is not implemented'))
23+
}
24+
25+
async has (key: Key, options?: Options): Promise<boolean> {
26+
return await Promise.reject(new Error('.has is not implemented'))
27+
}
28+
29+
async delete (key: Key, options?: Options): Promise<void> {
30+
await Promise.reject(new Error('.delete is not implemented'))
31+
}
32+
33+
async * putMany (source: AwaitIterable<Pair>, options: Options = {}): AsyncIterable<Pair> {
34+
for await (const { key, value } of source) {
35+
await this.put(key, value, options)
36+
yield { key, value }
37+
}
38+
}
39+
40+
async * getMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Uint8Array> {
41+
for await (const key of source) {
42+
yield this.get(key, options)
43+
}
44+
}
45+
46+
async * deleteMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Key> {
47+
for await (const key of source) {
48+
await this.delete(key, options)
49+
yield key
50+
}
51+
}
52+
53+
batch (): Batch {
54+
let puts: Pair[] = []
55+
let dels: Key[] = []
56+
57+
return {
58+
put (key, value) {
59+
puts.push({ key, value })
60+
},
61+
62+
delete (key) {
63+
dels.push(key)
64+
},
65+
commit: async (options) => {
66+
await drain(this.putMany(puts, options))
67+
puts = []
68+
await drain(this.deleteMany(dels, options))
69+
dels = []
70+
}
71+
}
72+
}
73+
74+
/**
75+
* Extending classes should override `query` or implement this method
76+
*/
77+
// eslint-disable-next-line require-yield
78+
async * _all (q: Query, options?: Options): AsyncIterable<Pair> {
79+
throw new Error('._all is not implemented')
80+
}
81+
82+
/**
83+
* Extending classes should override `queryKeys` or implement this method
84+
*/
85+
// eslint-disable-next-line require-yield
86+
async * _allKeys (q: KeyQuery, options?: Options): AsyncIterable<Key> {
87+
throw new Error('._allKeys is not implemented')
88+
}
89+
90+
query (q: Query, options?: Options): AsyncIterable<Pair> {
91+
let it = this._all(q, options)
92+
93+
if (q.prefix != null) {
94+
const prefix = q.prefix
95+
it = filter(it, (e) => e.key.toString().startsWith(prefix))
96+
}
97+
98+
if (Array.isArray(q.filters)) {
99+
it = q.filters.reduce((it, f) => filter(it, f), it)
100+
}
101+
102+
if (Array.isArray(q.orders)) {
103+
it = q.orders.reduce((it, f) => sort(it, f), it)
104+
}
105+
106+
if (q.offset != null) {
107+
let i = 0
108+
const offset = q.offset
109+
it = filter(it, () => i++ >= offset)
110+
}
111+
112+
if (q.limit != null) {
113+
it = take(it, q.limit)
114+
}
115+
116+
return it
117+
}
118+
119+
queryKeys (q: KeyQuery, options?: Options): AsyncIterable<Key> {
120+
let it = this._allKeys(q, options)
121+
122+
if (q.prefix != null) {
123+
const prefix = q.prefix
124+
it = filter(it, (key) =>
125+
key.toString().startsWith(prefix)
126+
)
127+
}
128+
129+
if (Array.isArray(q.filters)) {
130+
it = q.filters.reduce((it, f) => filter(it, f), it)
131+
}
132+
133+
if (Array.isArray(q.orders)) {
134+
it = q.orders.reduce((it, f) => sort(it, f), it)
135+
}
136+
137+
if (q.offset != null) {
138+
const offset = q.offset
139+
let i = 0
140+
it = filter(it, () => i++ >= offset)
141+
}
142+
143+
if (q.limit != null) {
144+
it = take(it, q.limit)
145+
}
146+
147+
return it
148+
}
149+
}

‎src/errors.js‎

Lines changed: 0 additions & 41 deletions
This file was deleted.

‎src/errors.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import errCode from 'err-code'
2+
3+
export function dbOpenFailedError (err?: Error): Error {
4+
err = err ?? new Error('Cannot open database')
5+
return errCode(err, 'ERR_DB_OPEN_FAILED')
6+
}
7+
8+
export function dbDeleteFailedError (err?: Error): Error {
9+
err = err ?? new Error('Delete failed')
10+
return errCode(err, 'ERR_DB_DELETE_FAILED')
11+
}
12+
13+
export function dbWriteFailedError (err?: Error): Error {
14+
err = err ?? new Error('Write failed')
15+
return errCode(err, 'ERR_DB_WRITE_FAILED')
16+
}
17+
18+
export function notFoundError (err?: Error): Error {
19+
err = err ?? new Error('Not Found')
20+
return errCode(err, 'ERR_NOT_FOUND')
21+
}
22+
23+
export function abortedError (err?: Error): Error {
24+
err = err ?? new Error('Aborted')
25+
return errCode(err, 'ERR_ABORTED')
26+
}

‎src/index.js‎ renamed to ‎src/index.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Key } from 'interface-datastore'
2+
13
import * as Errors from './errors.js'
24
import * as shard from './shard.js'
35

@@ -12,7 +14,15 @@ export { NamespaceDatastore } from './namespace.js'
1214
export { Errors }
1315
export { shard }
1416

15-
/**
16-
* @typedef {import("./types").Shard } Shard
17-
* @typedef {import("./types").KeyTransform } KeyTransform
18-
*/
17+
export interface Shard {
18+
name: string
19+
param: number
20+
readonly _padding: string
21+
fun: (s: string) => string
22+
toString: () => string
23+
}
24+
25+
export interface KeyTransform {
26+
convert: (key: Key) => Key
27+
invert: (key: Key) => Key
28+
}
Lines changed: 28 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,43 @@
11
import { BaseDatastore } from './base.js'
22
import map from 'it-map'
33
import { pipe } from 'it-pipe'
4-
5-
/**
6-
* @typedef {import('interface-datastore').Datastore} Datastore
7-
* @typedef {import('interface-datastore').Options} Options
8-
* @typedef {import('interface-datastore').Batch} Batch
9-
* @typedef {import('interface-datastore').Query} Query
10-
* @typedef {import('interface-datastore').KeyQuery} KeyQuery
11-
* @typedef {import('interface-datastore').Key} Key
12-
* @typedef {import('interface-datastore').Pair} Pair
13-
* @typedef {import('./types').KeyTransform} KeyTransform
14-
*/
15-
16-
/**
17-
* @template TEntry
18-
* @typedef {import('interface-store').AwaitIterable<TEntry>} AwaitIterable
19-
*/
4+
import type { KeyTransform } from './index.js'
5+
import type { Batch, Datastore, Key, KeyQuery, Options, Pair, Query } from 'interface-datastore'
6+
import type { AwaitIterable } from 'interface-store'
207

218
/**
229
* A datastore shim, that wraps around a given datastore, changing
2310
* the way keys look to the user, for example namespacing
2411
* keys, reversing them, etc.
25-
*
26-
* @implements {Datastore}
2712
*/
2813
export class KeyTransformDatastore extends BaseDatastore {
29-
/**
30-
* @param {Datastore} child
31-
* @param {KeyTransform} transform
32-
*/
33-
constructor (child, transform) {
14+
private readonly child: Datastore
15+
public transform: KeyTransform
16+
17+
constructor (child: Datastore, transform: KeyTransform) {
3418
super()
3519

3620
this.child = child
3721
this.transform = transform
3822
}
3923

40-
open () {
41-
return this.child.open()
42-
}
43-
44-
/**
45-
* @param {Key} key
46-
* @param {Uint8Array} val
47-
* @param {Options} [options]
48-
*/
49-
put (key, val, options) {
50-
return this.child.put(this.transform.convert(key), val, options)
24+
async put (key: Key, val: Uint8Array, options?: Options): Promise<void> {
25+
await this.child.put(this.transform.convert(key), val, options)
5126
}
5227

53-
/**
54-
* @param {Key} key
55-
* @param {Options} [options]
56-
*/
57-
get (key, options) {
58-
return this.child.get(this.transform.convert(key), options)
28+
async get (key: Key, options?: Options): Promise<Uint8Array> {
29+
return await this.child.get(this.transform.convert(key), options)
5930
}
6031

61-
/**
62-
* @param {Key} key
63-
* @param {Options} [options]
64-
*/
65-
has (key, options) {
66-
return this.child.has(this.transform.convert(key), options)
32+
async has (key: Key, options?: Options): Promise<boolean> {
33+
return await this.child.has(this.transform.convert(key), options)
6734
}
6835

69-
/**
70-
* @param {Key} key
71-
* @param {Options} [options]
72-
*/
73-
delete (key, options) {
74-
return this.child.delete(this.transform.convert(key), options)
36+
async delete (key: Key, options?: Options): Promise<void> {
37+
await this.child.delete(this.transform.convert(key), options)
7538
}
7639

77-
/**
78-
* @param {AwaitIterable<Pair>} source
79-
* @param {Options} [options]
80-
* @returns {AsyncIterable<Pair>}
81-
*/
82-
async * putMany (source, options = {}) {
40+
async * putMany (source: AwaitIterable<Pair>, options: Options = {}): AsyncIterable<Pair> {
8341
const transform = this.transform
8442
const child = this.child
8543

@@ -103,12 +61,7 @@ export class KeyTransformDatastore extends BaseDatastore {
10361
)
10462
}
10563

106-
/**
107-
* @param {AwaitIterable<Key>} source
108-
* @param {Options} [options]
109-
* @returns {AsyncIterable<Uint8Array>}
110-
*/
111-
async * getMany (source, options = {}) {
64+
async * getMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Uint8Array> {
11265
const transform = this.transform
11366
const child = this.child
11467

@@ -123,12 +76,7 @@ export class KeyTransformDatastore extends BaseDatastore {
12376
)
12477
}
12578

126-
/**
127-
* @param {AwaitIterable<Key>} source
128-
* @param {Options} [options]
129-
* @returns {AsyncIterable<Key>}
130-
*/
131-
async * deleteMany (source, options = {}) {
79+
async * deleteMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Key> {
13280
const transform = this.transform
13381
const child = this.child
13482

@@ -146,10 +94,7 @@ export class KeyTransformDatastore extends BaseDatastore {
14694
)
14795
}
14896

149-
/**
150-
* @returns {Batch}
151-
*/
152-
batch () {
97+
batch (): Batch {
15398
const b = this.child.batch()
15499
return {
155100
put: (key, value) => {
@@ -158,23 +103,18 @@ export class KeyTransformDatastore extends BaseDatastore {
158103
delete: (key) => {
159104
b.delete(this.transform.convert(key))
160105
},
161-
commit: (options) => {
162-
return b.commit(options)
106+
commit: async (options) => {
107+
await b.commit(options)
163108
}
164109
}
165110
}
166111

167-
/**
168-
* @param {Query} q
169-
* @param {Options} [options]
170-
*/
171-
query (q, options) {
172-
/** @type {Query} */
173-
const query = {
112+
query (q: Query, options?: Options): AsyncIterable<Pair> {
113+
const query: Query = {
174114
...q
175115
}
176116

177-
query.filters = (query.filters || []).map(filter => {
117+
query.filters = (query.filters ?? []).map(filter => {
178118
return ({ key, value }) => filter({ key: this.transform.convert(key), value })
179119
})
180120

@@ -186,7 +126,7 @@ export class KeyTransformDatastore extends BaseDatastore {
186126
})
187127
}
188128

189-
if (query.orders) {
129+
if (query.orders != null) {
190130
query.orders = query.orders.map(order => {
191131
return (a, b) => order(
192132
{ key: this.transform.invert(a.key), value: a.value },
@@ -203,16 +143,12 @@ export class KeyTransformDatastore extends BaseDatastore {
203143
})
204144
}
205145

206-
/**
207-
* @param {KeyQuery} q
208-
* @param {Options} [options]
209-
*/
210-
queryKeys (q, options) {
146+
queryKeys (q: KeyQuery, options?: Options): AsyncIterable<Key> {
211147
const query = {
212148
...q
213149
}
214150

215-
query.filters = (query.filters || []).map(filter => {
151+
query.filters = (query.filters ?? []).map(filter => {
216152
return (key) => filter(this.transform.convert(key))
217153
})
218154

@@ -224,7 +160,7 @@ export class KeyTransformDatastore extends BaseDatastore {
224160
})
225161
}
226162

227-
if (query.orders) {
163+
if (query.orders != null) {
228164
query.orders = query.orders.map(order => {
229165
return (a, b) => order(
230166
this.transform.invert(a),
@@ -237,8 +173,4 @@ export class KeyTransformDatastore extends BaseDatastore {
237173
return this.transform.invert(key)
238174
})
239175
}
240-
241-
close () {
242-
return this.child.close()
243-
}
244176
}

‎src/memory.js‎

Lines changed: 0 additions & 71 deletions
This file was deleted.

‎src/memory.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { BaseDatastore } from './base.js'
2+
import { Key } from 'interface-datastore/key'
3+
import * as Errors from './errors.js'
4+
import type { Pair } from 'interface-datastore'
5+
6+
export class MemoryDatastore extends BaseDatastore {
7+
private readonly data: Map<string, Uint8Array>
8+
9+
constructor () {
10+
super()
11+
12+
this.data = new Map()
13+
}
14+
15+
async put (key: Key, val: Uint8Array): Promise<void> { // eslint-disable-line require-await
16+
this.data.set(key.toString(), val)
17+
}
18+
19+
async get (key: Key): Promise<Uint8Array> {
20+
const result = this.data.get(key.toString())
21+
22+
if (result == null) {
23+
throw Errors.notFoundError()
24+
}
25+
26+
return result
27+
}
28+
29+
async has (key: Key): Promise<boolean> { // eslint-disable-line require-await
30+
return this.data.has(key.toString())
31+
}
32+
33+
async delete (key: Key): Promise<void> { // eslint-disable-line require-await
34+
this.data.delete(key.toString())
35+
}
36+
37+
async * _all (): AsyncIterable<Pair> {
38+
for (const [key, value] of this.data.entries()) {
39+
yield { key: new Key(key), value }
40+
}
41+
}
42+
43+
async * _allKeys (): AsyncIterable<Key> {
44+
for (const key of this.data.keys()) {
45+
yield new Key(key)
46+
}
47+
}
48+
}

‎src/mount.js‎ renamed to ‎src/mount.ts‎

Lines changed: 30 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,27 @@ import take from 'it-take'
33
import merge from 'it-merge'
44
import { BaseDatastore } from './base.js'
55
import * as Errors from './errors.js'
6-
import {
7-
sortAll
8-
} from './utils.js'
9-
10-
/**
11-
* @typedef {import('interface-datastore').Datastore} Datastore
12-
* @typedef {import('interface-datastore').Key} Key
13-
* @typedef {import('interface-datastore').Pair} Pair
14-
* @typedef {import('interface-datastore').Options} Options
15-
* @typedef {import('interface-datastore').Batch} Batch
16-
* @typedef {import('interface-datastore').Query} Query
17-
* @typedef {import('interface-datastore').KeyQuery} KeyQuery
18-
* @typedef {import('./types').KeyTransform} KeyTransform
19-
*/
6+
import sort from 'it-sort'
7+
import type { Batch, Datastore, Key, KeyQuery, Pair, Query } from 'interface-datastore'
8+
import type { Options } from 'interface-store'
209

2110
/**
2211
* A datastore that can combine multiple stores inside various
2312
* key prefixes
24-
*
25-
* @implements {Datastore}
2613
*/
2714
export class MountDatastore extends BaseDatastore {
28-
/**
29-
* @param {Array<{prefix: Key, datastore: Datastore}>} mounts
30-
*/
31-
constructor (mounts) {
15+
private readonly mounts: Array<{ prefix: Key, datastore: Datastore }>
16+
17+
constructor (mounts: Array<{ prefix: Key, datastore: Datastore }>) {
3218
super()
3319

3420
this.mounts = mounts.slice()
3521
}
3622

37-
async open () {
38-
await Promise.all(this.mounts.map((m) => m.datastore.open()))
39-
}
40-
4123
/**
4224
* Lookup the matching datastore for the given key
43-
*
44-
* @private
45-
* @param {Key} key
46-
* @returns {{datastore: Datastore, mountpoint: Key} | undefined}
4725
*/
48-
_lookup (key) {
26+
private _lookup (key: Key): { datastore: Datastore, mountpoint: Key } | undefined {
4927
for (const mount of this.mounts) {
5028
if (mount.prefix.toString() === key.toString() || mount.prefix.isAncestorOf(key)) {
5129
return {
@@ -56,73 +34,48 @@ export class MountDatastore extends BaseDatastore {
5634
}
5735
}
5836

59-
/**
60-
* @param {Key} key
61-
* @param {Uint8Array} value
62-
* @param {Options} [options]
63-
*/
64-
put (key, value, options) {
37+
async put (key: Key, value: Uint8Array, options?: Options): Promise<void> {
6538
const match = this._lookup(key)
6639
if (match == null) {
6740
throw Errors.dbWriteFailedError(new Error('No datastore mounted for this key'))
6841
}
6942

70-
return match.datastore.put(key, value, options)
43+
await match.datastore.put(key, value, options)
7144
}
7245

7346
/**
7447
* @param {Key} key
7548
* @param {Options} [options]
7649
*/
77-
get (key, options) {
50+
async get (key: Key, options: Options = {}): Promise<Uint8Array> {
7851
const match = this._lookup(key)
7952
if (match == null) {
8053
throw Errors.notFoundError(new Error('No datastore mounted for this key'))
8154
}
82-
return match.datastore.get(key, options)
55+
return await match.datastore.get(key, options)
8356
}
8457

85-
/**
86-
* @param {Key} key
87-
* @param {Options} [options]
88-
*/
89-
has (key, options) {
58+
async has (key: Key, options?: Options): Promise<boolean> {
9059
const match = this._lookup(key)
9160
if (match == null) {
92-
return Promise.resolve(false)
61+
return await Promise.resolve(false)
9362
}
94-
return match.datastore.has(key, options)
63+
return await match.datastore.has(key, options)
9564
}
9665

97-
/**
98-
* @param {Key} key
99-
* @param {Options} [options]
100-
*/
101-
delete (key, options) {
66+
async delete (key: Key, options?: Options): Promise<void> {
10267
const match = this._lookup(key)
10368
if (match == null) {
10469
throw Errors.dbDeleteFailedError(new Error('No datastore mounted for this key'))
10570
}
10671

107-
return match.datastore.delete(key, options)
72+
await match.datastore.delete(key, options)
10873
}
10974

110-
async close () {
111-
await Promise.all(this.mounts.map((m) => {
112-
return m.datastore.close()
113-
}))
114-
}
75+
batch (): Batch {
76+
const batchMounts: Record<string, Batch> = {}
11577

116-
/**
117-
* @returns {Batch}
118-
*/
119-
batch () {
120-
/** @type {Record<string, Batch>} */
121-
const batchMounts = {}
122-
/**
123-
* @param {Key} key
124-
*/
125-
const lookup = (key) => {
78+
const lookup = (key: Key): { batch: Batch } => {
12679
const match = this._lookup(key)
12780
if (match == null) {
12881
throw new Error('No datastore mounted for this key')
@@ -148,41 +101,33 @@ export class MountDatastore extends BaseDatastore {
148101
match.batch.delete(key)
149102
},
150103
commit: async (options) => {
151-
await Promise.all(Object.keys(batchMounts).map(p => batchMounts[p].commit(options)))
104+
await Promise.all(Object.keys(batchMounts).map(async p => { await batchMounts[p].commit(options) }))
152105
}
153106
}
154107
}
155108

156-
/**
157-
* @param {Query} q
158-
* @param {Options} [options]
159-
*/
160-
query (q, options) {
109+
query (q: Query, options?: Options): AsyncIterable<Pair> {
161110
const qs = this.mounts.map(m => {
162111
return m.datastore.query({
163112
prefix: q.prefix,
164113
filters: q.filters
165114
}, options)
166115
})
167116

168-
/** @type AsyncIterable<Pair> */
169117
let it = merge(...qs)
170-
if (q.filters) q.filters.forEach(f => { it = filter(it, f) })
171-
if (q.orders) q.orders.forEach(o => { it = sortAll(it, o) })
118+
if (q.filters != null) q.filters.forEach(f => { it = filter(it, f) })
119+
if (q.orders != null) q.orders.forEach(o => { it = sort(it, o) })
172120
if (q.offset != null) {
173121
let i = 0
174-
it = filter(it, () => i++ >= /** @type {number} */ (q.offset))
122+
const offset = q.offset
123+
it = filter(it, () => i++ >= offset)
175124
}
176125
if (q.limit != null) it = take(it, q.limit)
177126

178127
return it
179128
}
180129

181-
/**
182-
* @param {KeyQuery} q
183-
* @param {Options} [options]
184-
*/
185-
queryKeys (q, options) {
130+
queryKeys (q: KeyQuery, options?: Options): AsyncIterable<Key> {
186131
const qs = this.mounts.map(m => {
187132
return m.datastore.queryKeys({
188133
prefix: q.prefix,
@@ -192,11 +137,12 @@ export class MountDatastore extends BaseDatastore {
192137

193138
/** @type AsyncIterable<Key> */
194139
let it = merge(...qs)
195-
if (q.filters) q.filters.forEach(f => { it = filter(it, f) })
196-
if (q.orders) q.orders.forEach(o => { it = sortAll(it, o) })
140+
if (q.filters != null) q.filters.forEach(f => { it = filter(it, f) })
141+
if (q.orders != null) q.orders.forEach(o => { it = sort(it, o) })
197142
if (q.offset != null) {
198143
let i = 0
199-
it = filter(it, () => i++ >= /** @type {number} */ (q.offset))
144+
const offset = q.offset
145+
it = filter(it, () => i++ >= offset)
200146
}
201147
if (q.limit != null) it = take(it, q.limit)
202148

‎src/namespace.js‎ renamed to ‎src/namespace.ts‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import { Key } from 'interface-datastore'
2+
import type { Datastore } from 'interface-datastore'
23
import { KeyTransformDatastore } from './keytransform.js'
3-
/**
4-
* @typedef {import('interface-datastore').Datastore} Datastore
5-
* @typedef {import('interface-datastore').Query} Query
6-
* @typedef {import('interface-datastore').KeyQuery} KeyQuery
7-
* @typedef {import('interface-datastore').Options} Options
8-
* @typedef {import('interface-datastore').Batch} Batch
9-
* @typedef {import('./types').KeyTransform} KeyTransform
10-
*/
114

125
/**
136
* Wraps a given datastore into a keytransform which
@@ -18,11 +11,7 @@ import { KeyTransformDatastore } from './keytransform.js'
1811
* `/hello/world`.
1912
*/
2013
export class NamespaceDatastore extends KeyTransformDatastore {
21-
/**
22-
* @param {Datastore} child
23-
* @param {Key} prefix
24-
*/
25-
constructor (child, prefix) {
14+
constructor (child: Datastore, prefix: Key) {
2615
super(child, {
2716
convert (key) {
2817
return prefix.child(key)

‎src/shard-readme.js‎

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,74 @@
1+
import type { Datastore } from 'interface-datastore'
12
import { Key } from 'interface-datastore/key'
2-
// @ts-expect-error readme is unused
3-
// eslint-disable-next-line no-unused-vars
4-
import readme from './shard-readme.js'
5-
6-
/**
7-
* @typedef {import('interface-datastore').Datastore} Datastore
8-
* @typedef {import('./types').Shard} Shard
9-
*/
3+
import type { Shard } from './index.js'
104

115
export const PREFIX = '/repo/flatfs/shard/'
126
export const SHARDING_FN = 'SHARDING'
13-
export const README_FN = '_README'
147

15-
/**
16-
* @implements {Shard}
17-
*/
18-
export class ShardBase {
19-
/**
20-
* @param {any} param
21-
*/
22-
constructor (param) {
8+
export class ShardBase implements Shard {
9+
public param: number
10+
public name: string
11+
public _padding: string
12+
13+
constructor (param: number) {
2314
this.param = param
2415
this.name = 'base'
2516
this._padding = ''
2617
}
2718

28-
/**
29-
* @param {string} s
30-
*/
31-
fun (s) {
19+
fun (s: string): string {
3220
return 'implement me'
3321
}
3422

35-
toString () {
23+
toString (): string {
3624
return `${PREFIX}v1/${this.name}/${this.param}`
3725
}
3826
}
39-
/**
40-
* @implements {Shard}
41-
*/
27+
4228
export class Prefix extends ShardBase {
43-
/**
44-
* @param {number} prefixLen
45-
*/
46-
constructor (prefixLen) {
29+
constructor (prefixLen: number) {
4730
super(prefixLen)
4831
this._padding = ''.padStart(prefixLen, '_')
4932
this.name = 'prefix'
5033
}
5134

52-
/**
53-
* @param {string} noslash
54-
*/
55-
fun (noslash) {
35+
fun (noslash: string): string {
5636
return (noslash + this._padding).slice(0, this.param)
5737
}
5838
}
5939

6040
export class Suffix extends ShardBase {
61-
/**
62-
* @param {number} suffixLen
63-
*/
64-
constructor (suffixLen) {
41+
constructor (suffixLen: number) {
6542
super(suffixLen)
43+
6644
this._padding = ''.padStart(suffixLen, '_')
6745
this.name = 'suffix'
6846
}
6947

70-
/**
71-
* @param {string} noslash
72-
*/
73-
fun (noslash) {
48+
fun (noslash: string): string {
7449
const s = this._padding + noslash
7550
return s.slice(s.length - this.param)
7651
}
7752
}
7853

7954
export class NextToLast extends ShardBase {
80-
/**
81-
* @param {number} suffixLen
82-
*/
83-
constructor (suffixLen) {
55+
constructor (suffixLen: number) {
8456
super(suffixLen)
8557
this._padding = ''.padStart(suffixLen + 1, '_')
8658
this.name = 'next-to-last'
8759
}
8860

89-
/**
90-
* @param {string} noslash
91-
*/
92-
fun (noslash) {
61+
fun (noslash: string): string {
9362
const s = this._padding + noslash
9463
const offset = s.length - this.param - 1
9564
return s.slice(offset, offset + this.param)
9665
}
9766
}
9867

9968
/**
100-
* Convert a given string to the matching sharding function.
101-
*
102-
* @param {string} str
103-
* @returns {Shard}
69+
* Convert a given string to the matching sharding function
10470
*/
105-
export function parseShardFun (str) {
71+
export function parseShardFun (str: string): Shard {
10672
str = str.trim()
10773

10874
if (str.length === 0) {
@@ -122,7 +88,7 @@ export function parseShardFun (str) {
12288

12389
const name = parts[1]
12490

125-
if (!parts[2]) {
91+
if (parts[2] == null || parts[2] === '') {
12692
throw new Error('missing param')
12793
}
12894

@@ -140,16 +106,10 @@ export function parseShardFun (str) {
140106
}
141107
}
142108

143-
/**
144-
* @param {string | Uint8Array} path
145-
* @param {Datastore} store
146-
*/
147-
export const readShardFun = async (path, store) => {
109+
export const readShardFun = async (path: string | Uint8Array, store: Datastore): Promise<Shard> => {
148110
const key = new Key(path).child(new Key(SHARDING_FN))
149-
// @ts-ignore
111+
// @ts-expect-error
150112
const get = typeof store.getRaw === 'function' ? store.getRaw.bind(store) : store.get.bind(store)
151113
const res = await get(key)
152-
return parseShardFun(new TextDecoder().decode(res || '').trim())
114+
return parseShardFun(new TextDecoder().decode(res ?? '').trim())
153115
}
154-
155-
export { default as readme } from './shard-readme.js'

‎src/sharding.js‎

Lines changed: 0 additions & 247 deletions
This file was deleted.

‎src/sharding.ts‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import { Batch, Key, KeyQuery, KeyQueryFilter, Options, Pair, Query, QueryFilter } from 'interface-datastore'
2+
import {
3+
readShardFun,
4+
SHARDING_FN
5+
} from './shard.js'
6+
import { BaseDatastore } from './base.js'
7+
import { KeyTransformDatastore } from './keytransform.js'
8+
import * as Errors from './errors.js'
9+
import type { Shard } from './index.js'
10+
import type { Datastore } from 'interface-datastore'
11+
import type { AwaitIterable } from 'interface-store'
12+
13+
const shardKey = new Key(SHARDING_FN)
14+
15+
/**
16+
* Backend independent abstraction of go-ds-flatfs.
17+
*
18+
* Wraps another datastore such that all values are stored
19+
* sharded according to the given sharding function.
20+
*/
21+
export class ShardingDatastore extends BaseDatastore {
22+
private readonly child: KeyTransformDatastore
23+
private shard: Shard
24+
25+
constructor (store: Datastore, shard: Shard) {
26+
super()
27+
28+
this.child = new KeyTransformDatastore(store, {
29+
convert: this._convertKey.bind(this),
30+
invert: this._invertKey.bind(this)
31+
})
32+
this.shard = shard
33+
}
34+
35+
async open (): Promise<void> {
36+
this.shard = await ShardingDatastore.create(this.child, this.shard)
37+
}
38+
39+
_convertKey (key: Key): Key {
40+
const s = key.toString()
41+
if (s === shardKey.toString()) {
42+
return key
43+
}
44+
45+
const parent = new Key(this.shard.fun(s))
46+
return parent.child(key)
47+
}
48+
49+
_invertKey (key: Key): Key {
50+
const s = key.toString()
51+
if (s === shardKey.toString()) {
52+
return key
53+
}
54+
return Key.withNamespaces(key.list().slice(1))
55+
}
56+
57+
static async create (store: Datastore, shard: Shard): Promise<Shard> {
58+
const hasShard = await store.has(shardKey)
59+
if (!hasShard && shard == null) {
60+
throw Errors.dbOpenFailedError(Error('Shard is required when datastore doesn\'t have a shard key already.'))
61+
}
62+
if (!hasShard) {
63+
// @ts-expect-error i have no idea what putRaw is or saw any implementation
64+
const put = typeof store.putRaw === 'function' ? store.putRaw.bind(store) : store.put.bind(store)
65+
await Promise.all([
66+
put(shardKey, new TextEncoder().encode(shard.toString() + '\n'))
67+
])
68+
69+
return shard
70+
}
71+
72+
// test shards
73+
const diskShard = await readShardFun('/', store)
74+
const a = diskShard.toString()
75+
const b = shard.toString()
76+
if (a !== b) {
77+
throw new Error(`specified fun ${b} does not match repo shard fun ${a}`)
78+
}
79+
return diskShard
80+
}
81+
82+
async put (key: Key, val: Uint8Array, options?: Options): Promise<void> {
83+
await this.child.put(key, val, options)
84+
}
85+
86+
async get (key: Key, options?: Options): Promise<Uint8Array> {
87+
return await this.child.get(key, options)
88+
}
89+
90+
async has (key: Key, options?: Options): Promise<boolean> {
91+
return await this.child.has(key, options)
92+
}
93+
94+
async delete (key: Key, options?: Options): Promise<void> {
95+
await this.child.delete(key, options)
96+
}
97+
98+
async * putMany (source: AwaitIterable<Pair>, options: Options = {}): AsyncIterable<Pair> {
99+
yield * this.child.putMany(source, options)
100+
}
101+
102+
async * getMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Uint8Array> {
103+
yield * this.child.getMany(source, options)
104+
}
105+
106+
async * deleteMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Key> {
107+
yield * this.child.deleteMany(source, options)
108+
}
109+
110+
batch (): Batch {
111+
return this.child.batch()
112+
}
113+
114+
query (q: Query, options?: Options): AsyncIterable<Pair> {
115+
const omitShard: QueryFilter = ({ key }) => key.toString() !== shardKey.toString()
116+
117+
const tq: Query = {
118+
...q,
119+
filters: [
120+
omitShard
121+
].concat(q.filters ?? [])
122+
}
123+
124+
return this.child.query(tq, options)
125+
}
126+
127+
queryKeys (q: KeyQuery, options?: Options): AsyncIterable<Key> {
128+
const omitShard: KeyQueryFilter = (key) => key.toString() !== shardKey.toString()
129+
130+
const tq: KeyQuery = {
131+
...q,
132+
filters: [
133+
omitShard
134+
].concat(q.filters ?? [])
135+
}
136+
137+
return this.child.queryKeys(tq, options)
138+
}
139+
}

‎src/tiered.js‎ renamed to ‎src/tiered.ts‎

Lines changed: 27 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,11 @@ import * as Errors from './errors.js'
33
import { logger } from '@libp2p/logger'
44
import { pushable } from 'it-pushable'
55
import drain from 'it-drain'
6+
import type { Batch, Datastore, Key, KeyQuery, Options, Pair, Query } from 'interface-datastore'
7+
import type { AwaitIterable } from 'interface-store'
68

79
const log = logger('datastore:core:tiered')
810

9-
/**
10-
* @typedef {import('interface-datastore').Datastore} Datastore
11-
* @typedef {import('interface-datastore').Options} Options
12-
* @typedef {import('interface-datastore').Batch} Batch
13-
* @typedef {import('interface-datastore').Query} Query
14-
* @typedef {import('interface-datastore').KeyQuery} KeyQuery
15-
* @typedef {import('interface-datastore').Key} Key
16-
* @typedef {import('interface-datastore').Pair} Pair
17-
*/
18-
19-
/**
20-
* @template TEntry
21-
* @typedef {import('interface-store').AwaitIterable<TEntry>} AwaitIterable
22-
*/
23-
2411
/**
2512
* A datastore that can combine multiple stores. Puts and deletes
2613
* will write through to all datastores. Has and get will
@@ -29,57 +16,35 @@ const log = logger('datastore:core:tiered')
2916
*
3017
*/
3118
export class TieredDatastore extends BaseDatastore {
32-
/**
33-
* @param {Datastore[]} stores
34-
*/
35-
constructor (stores) {
19+
private readonly stores: Datastore[]
20+
21+
constructor (stores: Datastore[]) {
3622
super()
3723

3824
this.stores = stores.slice()
3925
}
4026

41-
async open () {
27+
async put (key: Key, value: Uint8Array, options?: Options): Promise<void> {
4228
try {
43-
await Promise.all(this.stores.map((store) => store.open()))
44-
} catch (/** @type {any} */ err) {
45-
throw Errors.dbOpenFailedError(err)
46-
}
47-
}
48-
49-
/**
50-
* @param {Key} key
51-
* @param {Uint8Array} value
52-
* @param {Options} [options]
53-
*/
54-
async put (key, value, options) {
55-
try {
56-
await Promise.all(this.stores.map(store => store.put(key, value, options)))
57-
} catch (/** @type {any} */ err) {
29+
await Promise.all(this.stores.map(async store => { await store.put(key, value, options) }))
30+
} catch (err: any) {
5831
throw Errors.dbWriteFailedError(err)
5932
}
6033
}
6134

62-
/**
63-
* @param {Key} key
64-
* @param {Options} [options]
65-
*/
66-
async get (key, options) {
35+
async get (key: Key, options?: Options): Promise<Uint8Array> {
6736
for (const store of this.stores) {
6837
try {
6938
const res = await store.get(key, options)
70-
if (res) return res
39+
if (res != null) return res
7140
} catch (err) {
7241
log.error(err)
7342
}
7443
}
7544
throw Errors.notFoundError()
7645
}
7746

78-
/**
79-
* @param {Key} key
80-
* @param {Options} [options]
81-
*/
82-
async has (key, options) {
47+
async has (key: Key, options?: Options): Promise<boolean> {
8348
for (const s of this.stores) {
8449
if (await s.has(key, options)) {
8550
return true
@@ -89,27 +54,18 @@ export class TieredDatastore extends BaseDatastore {
8954
return false
9055
}
9156

92-
/**
93-
* @param {Key} key
94-
* @param {Options} [options]
95-
*/
96-
async delete (key, options) {
57+
async delete (key: Key, options?: Options): Promise<void> {
9758
try {
98-
await Promise.all(this.stores.map(store => store.delete(key, options)))
99-
} catch (/** @type {any} */ err) {
59+
await Promise.all(this.stores.map(async store => { await store.delete(key, options) }))
60+
} catch (err: any) {
10061
throw Errors.dbDeleteFailedError(err)
10162
}
10263
}
10364

104-
/**
105-
* @param {AwaitIterable<Pair>} source
106-
* @param {Options} [options]
107-
* @returns {AsyncIterable<Pair>}
108-
*/
109-
async * putMany (source, options = {}) {
110-
let error
65+
async * putMany (source: AwaitIterable<Pair>, options: Options = {}): AsyncIterable<Pair> {
66+
let error: Error | undefined
11167
const pushables = this.stores.map(store => {
112-
const source = pushable({
68+
const source = pushable<Pair>({
11369
objectMode: true
11470
})
11571

@@ -124,7 +80,7 @@ export class TieredDatastore extends BaseDatastore {
12480

12581
try {
12682
for await (const pair of source) {
127-
if (error) {
83+
if (error != null) {
12884
throw error
12985
}
13086

@@ -137,15 +93,10 @@ export class TieredDatastore extends BaseDatastore {
13793
}
13894
}
13995

140-
/**
141-
* @param {AwaitIterable<Key>} source
142-
* @param {Options} [options]
143-
* @returns {AsyncIterable<Key>}
144-
*/
145-
async * deleteMany (source, options = {}) {
146-
let error
96+
async * deleteMany (source: AwaitIterable<Key>, options: Options = {}): AsyncIterable<Key> {
97+
let error: Error | undefined
14798
const pushables = this.stores.map(store => {
148-
const source = pushable({
99+
const source = pushable<Key>({
149100
objectMode: true
150101
})
151102

@@ -160,7 +111,7 @@ export class TieredDatastore extends BaseDatastore {
160111

161112
try {
162113
for await (const key of source) {
163-
if (error) {
114+
if (error != null) {
164115
throw error
165116
}
166117

@@ -173,22 +124,15 @@ export class TieredDatastore extends BaseDatastore {
173124
}
174125
}
175126

176-
async close () {
177-
await Promise.all(this.stores.map(store => store.close()))
178-
}
179-
180-
/**
181-
* @returns {Batch}
182-
*/
183-
batch () {
127+
batch (): Batch {
184128
const batches = this.stores.map(store => store.batch())
185129

186130
return {
187131
put: (key, value) => {
188-
batches.forEach(b => b.put(key, value))
132+
batches.forEach(b => { b.put(key, value) })
189133
},
190134
delete: (key) => {
191-
batches.forEach(b => b.delete(key))
135+
batches.forEach(b => { b.delete(key) })
192136
},
193137
commit: async (options) => {
194138
for (const batch of batches) {
@@ -198,19 +142,11 @@ export class TieredDatastore extends BaseDatastore {
198142
}
199143
}
200144

201-
/**
202-
* @param {Query} q
203-
* @param {Options} [options]
204-
*/
205-
query (q, options) {
145+
query (q: Query, options?: Options): AsyncIterable<Pair> {
206146
return this.stores[this.stores.length - 1].query(q, options)
207147
}
208148

209-
/**
210-
* @param {KeyQuery} q
211-
* @param {Options} [options]
212-
*/
213-
queryKeys (q, options) {
149+
queryKeys (q: KeyQuery, options?: Options): AsyncIterable<Key> {
214150
return this.stores[this.stores.length - 1].queryKeys(q, options)
215151
}
216152
}

‎src/types.ts‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎src/utils.js‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎src/utils.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
export const replaceStartWith = (s: string, r: string): string => {
3+
const matcher = new RegExp('^' + r)
4+
return s.replace(matcher, '')
5+
}

‎test/keytransform.spec.js‎ renamed to ‎test/keytransform.spec.ts‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@ describe('KeyTransformDatastore', () => {
1010
it('basic', async () => {
1111
const mStore = new MemoryDatastore()
1212
const transform = {
13-
/**
14-
* @param {Key} key
15-
*/
16-
convert (key) {
13+
convert (key: Key): Key {
1714
return new Key('/abc').child(key)
1815
},
19-
/**
20-
* @param {Key} key
21-
*/
22-
invert (key) {
16+
invert (key: Key): Key {
2317
const l = key.list()
2418
if (l[0] !== 'abc') {
2519
throw new Error('missing prefix, convert failed?')
@@ -38,9 +32,9 @@ describe('KeyTransformDatastore', () => {
3832
'foo/bar/bazb',
3933
'foo/bar/baz/barb'
4034
].map((s) => new Key(s))
41-
await Promise.all(keys.map((key) => kStore.put(key, key.uint8Array())))
42-
const kResults = Promise.all(keys.map((key) => kStore.get(key)))
43-
const mResults = Promise.all(keys.map((key) => mStore.get(new Key('abc').child(key))))
35+
await Promise.all(keys.map(async (key) => { await kStore.put(key, key.uint8Array()) }))
36+
const kResults = Promise.all(keys.map(async (key) => await kStore.get(key)))
37+
const mResults = Promise.all(keys.map(async (key) => await mStore.get(new Key('abc').child(key))))
4438
const results = await Promise.all([kResults, mResults])
4539
expect(results[0]).to.eql(results[1])
4640

@@ -54,6 +48,5 @@ describe('KeyTransformDatastore', () => {
5448
expect(transform.invert(kA)).to.eql(kB)
5549
expect(kA).to.eql(transform.convert(kB))
5650
})
57-
await kStore.close()
5851
})
5952
})
File renamed without changes.

‎test/mount.spec.js‎ renamed to ‎test/mount.spec.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ import { MountDatastore } from '../src/mount.js'
99
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1010
import { interfaceDatastoreTests } from 'interface-datastore-tests'
1111
import { KeyTransformDatastore } from '../src/keytransform.js'
12+
import type { Datastore } from 'interface-datastore'
1213

13-
/**
14-
* @param {import('interface-datastore').Datastore} datastore
15-
* @param {Key} prefix
16-
*/
17-
const stripPrefixDatastore = (datastore, prefix) => {
14+
const stripPrefixDatastore = (datastore: Datastore, prefix: Key): Datastore => {
1815
return new KeyTransformDatastore(
1916
datastore, {
2017
convert: (key) => {

‎test/namespace.spec.js‎ renamed to ‎test/namespace.spec.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ describe('NamespaceDatastore', () => {
2626
'foo/bar/baz/barb'
2727
].map((s) => new Key(s))
2828

29-
await Promise.all(keys.map(key => store.put(key, uint8ArrayFromString(key.toString()))))
30-
const nResults = Promise.all(keys.map((key) => store.get(key)))
31-
const mResults = Promise.all(keys.map((key) => mStore.get(new Key(prefix).child(key))))
29+
await Promise.all(keys.map(async key => { await store.put(key, uint8ArrayFromString(key.toString())) }))
30+
const nResults = Promise.all(keys.map(async (key) => await store.get(key)))
31+
const mResults = Promise.all(keys.map(async (key) => await mStore.get(new Key(prefix).child(key))))
3232
const results = await Promise.all([nResults, mResults])
3333
const mRes = await all(mStore.query({}))
3434
const nRes = await all(store.query({}))
@@ -41,7 +41,6 @@ describe('NamespaceDatastore', () => {
4141
expect(store.transform.invert(kA)).to.eql(kB)
4242
expect(kA).to.eql(store.transform.convert(kB))
4343
})
44-
await store.close()
4544

4645
expect(results[0]).to.eql(results[1])
4746
}))
File renamed without changes.

‎test/sharding.spec.js‎ renamed to ‎test/sharding.spec.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
77
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
88
import {
99
NextToLast,
10-
SHARDING_FN,
11-
README_FN,
12-
readme
10+
SHARDING_FN
1311
} from '../src/shard.js'
1412
import {
1513
ShardingDatastore
@@ -23,11 +21,9 @@ describe('ShardingDatastore', () => {
2321
const store = new ShardingDatastore(ms, shard)
2422
await store.open()
2523
const res = await Promise.all([
26-
ms.get(new Key(SHARDING_FN)),
27-
ms.get(new Key(README_FN))
24+
store.get(new Key(SHARDING_FN))
2825
])
2926
expect(uint8ArrayToString(res[0])).to.eql(shard.toString() + '\n')
30-
expect(uint8ArrayToString(res[1])).to.eql(readme)
3127
})
3228

3329
it('open - empty', () => {

‎test/tiered.spec.js‎ renamed to ‎test/tiered.spec.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ import { MemoryDatastore } from '../src/memory.js'
66
import { TieredDatastore } from '../src/tiered.js'
77
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
88
import { interfaceDatastoreTests } from 'interface-datastore-tests'
9+
import type { Datastore } from 'interface-datastore'
910

1011
/**
1112
* @typedef {import('interface-datastore').Datastore} Datastore
1213
*/
1314

1415
describe('Tiered', () => {
1516
describe('all stores', () => {
16-
/** @type {Datastore[]} */
17-
const ms = []
18-
/** @type {TieredDatastore} */
19-
let store
17+
const ms: Datastore[] = []
18+
let store: TieredDatastore
2019
beforeEach(() => {
2120
ms.push(new MemoryDatastore())
2221
ms.push(new MemoryDatastore())

‎test/utils.spec.js‎

Lines changed: 0 additions & 121 deletions
This file was deleted.

‎test/utils.spec.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* eslint-env mocha */
2+
3+
import { expect } from 'aegir/chai'
4+
import * as utils from '../src/utils.js'
5+
6+
describe('utils', () => {
7+
it('replaceStartWith', () => {
8+
expect(
9+
utils.replaceStartWith('helloworld', 'hello')
10+
).to.eql(
11+
'world'
12+
)
13+
14+
expect(
15+
utils.replaceStartWith('helloworld', 'world')
16+
).to.eql(
17+
'helloworld'
18+
)
19+
})
20+
})

‎tsconfig.json‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
2-
"extends": "aegir/src/config/tsconfig.aegir.json",
3-
"compilerOptions": {
4-
"outDir": "dist",
5-
"emitDeclarationOnly": true
6-
},
7-
"include": [
8-
"test",
9-
"src"
10-
]
2+
"extends": "aegir/src/config/tsconfig.aegir.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": [
7+
"src",
8+
"test"
9+
]
1110
}

0 commit comments

Comments
 (0)
This repository has been archived.