forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsindexeddb.go
280 lines (224 loc) · 6.81 KB
/
jsindexeddb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// +build js,wasm
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package jsindexeddb
import (
"errors"
"fmt"
"sync"
"syscall/js"
"time"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/messenger"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/introduce"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/route"
"github.com/hyperledger/aries-framework-go/pkg/kms/legacykms"
"github.com/hyperledger/aries-framework-go/pkg/kms/localkms"
"github.com/hyperledger/aries-framework-go/pkg/storage"
"github.com/hyperledger/aries-framework-go/pkg/store/connection"
"github.com/hyperledger/aries-framework-go/pkg/store/did"
"github.com/hyperledger/aries-framework-go/pkg/vdri/peer"
)
const (
dbName = "aries-%s"
defDbName = "aries"
)
var dbVersion = 1 //nolint:gochecknoglobals
// Provider jsindexeddb implementation of storage.Provider interface
type Provider struct {
sync.RWMutex
stores map[string]*js.Value
}
// NewProvider instantiates Provider
// TODO Add unit test for IndexedDB https://github.com/hyperledger/aries-framework-go/issues/834
func NewProvider(dbName string) (*Provider, error) {
p := &Provider{stores: make(map[string]*js.Value)}
db := defDbName
if dbName != "" {
db = fmt.Sprintf(dbName, db)
}
err := p.openDB(db, getStoreNames()...)
if err != nil {
return nil, fmt.Errorf("failed to open IndexDB : %w", err)
}
return p, nil
}
// Close closes all stores created under this store provider
func (p *Provider) Close() error {
return nil
}
// OpenStore open store
func (p *Provider) OpenStore(name string) (storage.Store, error) {
p.RLock()
db, ok := p.stores[name]
p.RUnlock()
if ok {
return &store{name: name, db: db}, nil
}
p.Lock()
defer p.Unlock()
// create new if not found in list of object stores (not the predefined ones)
err := p.openDB(fmt.Sprintf(dbName, name), name)
if err != nil {
return nil, err
}
return &store{name: name, db: p.stores[name]}, nil
}
func (p *Provider) openDB(db string, names ...string) error {
req := js.Global().Get("indexedDB").Call("open", db, dbVersion)
req.Set("onupgradeneeded", js.FuncOf(func(this js.Value, inputs []js.Value) interface{} {
m := make(map[string]interface{})
m["keyPath"] = "key"
for _, name := range names {
fmt.Printf("indexedDB create object store %s\n", name)
this.Get("result").Call("createObjectStore", name, m)
}
return nil
}))
v, err := getResult(req)
if err != nil {
return fmt.Errorf("failed to open indexedDB: %w", err)
}
for _, name := range names {
p.stores[name] = v
}
return nil
}
// CloseStore closes level db store of given name
func (p *Provider) CloseStore(name string) error {
return nil
}
type store struct {
name string
db *js.Value
}
// Put stores the key and the record
func (s *store) Put(k string, v []byte) error {
if k == "" || v == nil {
return errors.New("key and value are mandatory")
}
m := make(map[string]interface{})
m["key"] = k
m["value"] = string(v)
req := s.db.Call("transaction", s.name, "readwrite").Call("objectStore", s.name).Call("put", m)
_, err := getResult(req)
if err != nil {
return fmt.Errorf("failed to store data: %w", err)
}
return nil
}
// Get fetches the record based on key
func (s *store) Get(k string) ([]byte, error) {
if k == "" {
return nil, errors.New("key is mandatory")
}
req := s.db.Call("transaction", s.name).Call("objectStore", s.name).Call("get", k)
data, err := getResult(req)
if err != nil {
return nil, fmt.Errorf("failed to get data: %w", err)
}
if !data.Truthy() {
return nil, storage.ErrDataNotFound
}
return []byte(data.Get("value").String()), nil
}
// Iterator returns iterator for the latest snapshot of the underlying db.
func (s *store) Iterator(start, limit string) storage.StoreIterator {
// TODO Change Store Iterator https://github.com/hyperledger/aries-framework-go/issues/852
if start == "" {
return newIterator(nil, fmt.Errorf("start key is mandatory"))
}
keyRange := js.Global().Get("IDBKeyRange").Call("bound", start, start+"\uffff")
openCursor := s.db.Call("transaction", s.name).Call("objectStore", s.name).Call("getAll", keyRange)
batch, err := getResult(openCursor)
return newIterator(batch, err)
}
// Delete will delete record with k key
func (s *store) Delete(k string) error {
if k == "" {
return errors.New("key is mandatory")
}
req := s.db.Call("transaction", s.name, "readwrite").Call("objectStore", s.name).Call("delete", k)
_, err := getResult(req)
if err != nil {
return fmt.Errorf("failed to delete data with key: %s - error: %w", k, err)
}
return nil
}
type iterator struct {
batch *js.Value
err error
index int
}
// newIterator returns new iterator for given batch
func newIterator(batch *js.Value, err error) *iterator {
return &iterator{batch: batch, err: err, index: -1}
}
// Next moves pointer to next value of iterator.
// It returns false if the iterator is exhausted.
func (s *iterator) Next() bool {
s.index++
if s.batch != nil && s.batch.Index(s.index).Truthy() {
return true
}
return false
}
// Release releases associated resources.
func (s *iterator) Release() {
}
// Error returns error in iterator.
func (s *iterator) Error() error {
return s.err
}
// Key returns the key of the current key/value pair.
func (s *iterator) Key() []byte {
if s.batch != nil && s.batch.Index(s.index).Truthy() {
return []byte(s.batch.Index(s.index).Get("key").String())
}
return nil
}
// Value returns the value of the current key/value pair.
func (s *iterator) Value() []byte {
if s.batch != nil && s.batch.Index(s.index).Truthy() {
return []byte(s.batch.Index(s.index).Get("value").String())
}
return nil
}
func getResult(req js.Value) (*js.Value, error) {
onsuccess := make(chan js.Value)
onerror := make(chan js.Value)
const timeout = 3
req.Set("onsuccess", js.FuncOf(func(this js.Value, inputs []js.Value) interface{} {
onsuccess <- this.Get("result")
return nil
}))
req.Set("onerror", js.FuncOf(func(this js.Value, inputs []js.Value) interface{} {
onerror <- this.Get("error")
return nil
}))
select {
case value := <-onsuccess:
return &value, nil
case value := <-onerror:
return nil, fmt.Errorf("%s %s", value.Get("name").String(),
value.Get("message").String())
case <-time.After(timeout * time.Second):
return nil, errors.New("timeout waiting for eve")
}
}
// since jsindexdb doesn't support adding object stores on fly, using predefined object store names to
// create object store in advance instead of creating a database per store.
// TODO pass store names from higher level packages during initialization [Issue #1347]
func getStoreNames() []string {
return []string{
messenger.MessengerStore,
route.Coordination,
connection.Namespace,
introduce.Introduce,
legacykms.KeyStoreNamespace,
peer.StoreNamespace,
did.StoreName,
localkms.Namespace,
}
}