@@ -4,10 +4,24 @@ const {
4
4
createStore,
5
5
findLevelJs
6
6
} = require ( '../../src/utils' )
7
- const { Key } = require ( 'interface-datastore' )
8
7
const fromString = require ( 'uint8arrays/from-string' )
9
8
const toString = require ( 'uint8arrays/to-string' )
10
9
10
+ /**
11
+ * @typedef {import('../../src/types').Migration } Migration
12
+ * @typedef {import('interface-datastore').Datastore } Datastore
13
+ * @typedef {import('../../src/types').MigrationProgressCallback } MigrationProgressCallback
14
+ *
15
+ * @typedef {{ type: 'del', key: string | Uint8Array } | { type: 'put', key: string | Uint8Array, value: Uint8Array } } Operation
16
+ * @typedef {function (string, Uint8Array): Operation[] } UpgradeFunction
17
+ * @typedef {function (Uint8Array, Uint8Array): Operation[] } DowngradeFunction
18
+ */
19
+
20
+ /**
21
+ * @param {string } name
22
+ * @param {Datastore } store
23
+ * @param {(message: string) => void } onProgress
24
+ */
11
25
async function keysToBinary ( name , store , onProgress = ( ) => { } ) {
12
26
let db = findLevelJs ( store )
13
27
@@ -20,14 +34,24 @@ async function keysToBinary (name, store, onProgress = () => {}) {
20
34
21
35
onProgress ( `Upgrading ${ name } ` )
22
36
23
- await withEach ( db , ( key , value ) => {
37
+ /**
38
+ * @type {UpgradeFunction }
39
+ */
40
+ const upgrade = ( key , value ) => {
24
41
return [
25
42
{ type : 'del' , key : key } ,
26
43
{ type : 'put' , key : fromString ( key ) , value : value }
27
44
]
28
- } )
45
+ }
46
+
47
+ await withEach ( db , upgrade )
29
48
}
30
49
50
+ /**
51
+ * @param {string } name
52
+ * @param {Datastore } store
53
+ * @param {(message: string) => void } onProgress
54
+ */
31
55
async function keysToStrings ( name , store , onProgress = ( ) => { } ) {
32
56
let db = findLevelJs ( store )
33
57
@@ -40,14 +64,26 @@ async function keysToStrings (name, store, onProgress = () => {}) {
40
64
41
65
onProgress ( `Downgrading ${ name } ` )
42
66
43
- await withEach ( db , ( key , value ) => {
67
+ /**
68
+ * @type {DowngradeFunction }
69
+ */
70
+ const downgrade = ( key , value ) => {
44
71
return [
45
72
{ type : 'del' , key : key } ,
46
73
{ type : 'put' , key : toString ( key ) , value : value }
47
74
]
48
- } )
75
+ }
76
+
77
+ await withEach ( db , downgrade )
49
78
}
50
79
80
+ /**
81
+ *
82
+ * @param {string } repoPath
83
+ * @param {any } repoOptions
84
+ * @param {MigrationProgressCallback } onProgress
85
+ * @param {* } fn
86
+ */
51
87
async function process ( repoPath , repoOptions , onProgress , fn ) {
52
88
const datastores = Object . keys ( repoOptions . storageBackends )
53
89
. filter ( key => repoOptions . storageBackends [ key ] . name === 'LevelDatastore' )
@@ -63,9 +99,14 @@ async function process (repoPath, repoOptions, onProgress, fn) {
63
99
await store . open ( )
64
100
65
101
try {
66
- await fn ( name , store , ( message ) => {
67
- onProgress ( parseInt ( ( migrated / datastores . length ) * 100 ) , message )
68
- } )
102
+ /**
103
+ * @param {string } message
104
+ */
105
+ const progress = ( message ) => {
106
+ onProgress ( Math . round ( ( migrated / datastores . length ) * 100 ) , message )
107
+ }
108
+
109
+ await fn ( name , store , progress )
69
110
} finally {
70
111
migrated ++
71
112
store . close ( )
@@ -75,6 +116,7 @@ async function process (repoPath, repoOptions, onProgress, fn) {
75
116
onProgress ( 100 , `Migrated ${ datastores . length } dbs` )
76
117
}
77
118
119
+ /** @type {Migration } */
78
120
module . exports = {
79
121
version : 10 ,
80
122
description : 'Migrates datastore-level keys to binary' ,
@@ -87,23 +129,25 @@ module.exports = {
87
129
}
88
130
89
131
/**
90
- * @typedef {Uint8Array|string } Key
91
- * @typedef {Uint8Array } Value
92
- * @typedef {{ type: 'del', key: Key } | { type: 'put', key: Key, value: Value } } Operation
93
- *
94
132
* Uses the upgrade strategy from [email protected] - note we can't call the `.upgrade` command
95
133
* directly because it will be removed in [email protected] and we can't guarantee users will
96
134
* have migrated by then - e.g. they may jump from [email protected] straight to [email protected]
97
135
* so we have to duplicate the code here.
98
136
*
99
- * @param {import('interface-datastore').Datastore } db
100
- * @param {function (Key, Value): Operation[] } fn
137
+ * @param {any } db
138
+ * @param {UpgradeFunction | DowngradeFunction } fn
139
+ * @return {Promise<void> }
101
140
*/
102
141
function withEach ( db , fn ) {
142
+ /**
143
+ * @param {Operation[] } operations
144
+ * @param {(error?: Error) => void } next
145
+ */
103
146
function batch ( operations , next ) {
104
147
const store = db . store ( 'readwrite' )
105
148
const transaction = store . transaction
106
149
let index = 0
150
+ /** @type {Error | undefined } */
107
151
let error
108
152
109
153
transaction . onabort = ( ) => next ( error || transaction . error || new Error ( 'aborted by user' ) )
@@ -132,26 +176,43 @@ function withEach (db, fn) {
132
176
return new Promise ( ( resolve , reject ) => {
133
177
const it = db . iterator ( )
134
178
// raw keys and values only
135
- it . _deserializeKey = it . _deserializeValue = ( data ) => data
179
+ /**
180
+ * @template T
181
+ * @param {T } data
182
+ */
183
+ const id = ( data ) => data
184
+ it . _deserializeKey = it . _deserializeValue = id
136
185
next ( )
137
186
138
187
function next ( ) {
139
- it . next ( ( err , key , value ) => {
188
+ /**
189
+ * @param {Error | undefined } err
190
+ * @param {string | undefined } key
191
+ * @param {Uint8Array } value
192
+ */
193
+ const handleNext = ( err , key , value ) => {
140
194
if ( err || key === undefined ) {
141
- it . end ( ( err2 ) => {
195
+ /**
196
+ * @param {Error | undefined } err2
197
+ */
198
+ const handleEnd = ( err2 ) => {
142
199
if ( err2 ) {
143
200
reject ( err2 )
144
201
return
145
202
}
146
203
147
204
resolve ( )
148
- } )
205
+ }
206
+
207
+ it . end ( handleEnd )
149
208
150
209
return
151
210
}
152
211
212
+ // @ts -ignore
153
213
batch ( fn ( key , value ) , next )
154
- } )
214
+ }
215
+ it . next ( handleNext )
155
216
}
156
217
} )
157
218
}
0 commit comments