-
Notifications
You must be signed in to change notification settings - Fork 12
/
s3leveldown.js
343 lines (290 loc) · 9.41 KB
/
s3leveldown.js
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
, AbstractIterator = require('abstract-leveldown').AbstractIterator
, ltgt = require('ltgt')
, debug = require('debug')('S3LevelDown')
, AWS = require('@aws-sdk/client-s3')
const staticS3 = new AWS.S3Client({ apiVersion: '2006-03-01' })
function lt(value) {
return ltgt.compare(value, this._finish) < 0
}
function lte(value) {
return ltgt.compare(value, this._finish) <= 0
}
function getStartAfterKey(key) {
const keyMinusOneNum = (key.charCodeAt(key.length - 1) - 1)
const keyMinusOne = keyMinusOneNum >= 0 ? (String.fromCharCode(keyMinusOneNum) + '\uFFFF') : ''
return key.substring(0, key.length - 1) + keyMinusOne
}
function nullEmptyUndefined(v) {
return typeof v === 'undefined' || v === null || v === ''
}
class S3Iterator extends AbstractIterator {
constructor(db, options) {
super(db)
const self = this
self._limit = options.limit
if (self._limit === -1)
self._limit = Infinity
self.keyAsBuffer = options.keyAsBuffer !== false
self.valueAsBuffer = options.valueAsBuffer !== false
self.fetchValues = options.values
self._reverse = options.reverse
self._options = options
self._done = 0
self.bucket = db.bucket
self.db = db
self.s3ListObjectMaxKeys = options.s3ListObjectMaxKeys || 1000
if (!self._reverse && self._limit < self.s3ListObjectMaxKeys) {
self.s3ListObjectMaxKeys = self._limit
}
self._start = ltgt.lowerBound(options)
self._finish = ltgt.upperBound(options)
if (!nullEmptyUndefined(self._finish)) {
if (ltgt.upperBoundInclusive(options))
self._test = lte
else
self._test = lt
}
if (!nullEmptyUndefined(self._start))
self.startAfter = ltgt.lowerBoundInclusive(options) ? getStartAfterKey(self._start) : self._start
debug('new iterator %o', self._options)
}
_next(callback) {
const self = this
if (self._done++ >= self._limit ||
(self.data && self.dataUpto == self.data.length && !self.s3nextContinuationToken))
return setImmediate(callback)
if (!self.data || self.dataUpto == self.data.length) {
listObjects()
} else {
fireCallback()
}
function listObjects() {
const params = {
Bucket: self.bucket,
MaxKeys: self.s3ListObjectMaxKeys
}
if (self.db.folderPrefix !== '') {
params.Prefix = self.db.folderPrefix
}
if (self.s3nextContinuationToken) {
params.ContinuationToken = self.s3nextContinuationToken
debug('listObjectsV2 ContinuationToken %s', params.ContinuationToken)
}
else if (typeof self.startAfter !== 'undefined') {
params.StartAfter = self.db.folderPrefix + self.startAfter
}
self.db.s3.send(new AWS.ListObjectsV2Command(params), function (err, data) {
if (err) {
debug('listObjectsV2 error %s', err.message)
callback(err)
} else {
if (data.KeyCount === 0) {
debug('listObjectsV2 empty')
return setImmediate(callback)
}
debug('listObjectsV2 %d keys', data.KeyCount)
if (self.data && self.dataUpto === 0) {
self.data = self.data.concat(data.Contents)
} else {
self.data = data.Contents
}
self.dataUpto = 0
self.s3nextContinuationToken = data.NextContinuationToken
if (self._reverse && self.s3nextContinuationToken &&
data.Contents.every(function (x) {
return self._test(x.Key.substring(self.db.folderPrefix.length, x.Key.length))
})) {
listObjects()
} else {
fireCallback()
}
}
})
}
function fireCallback() {
let index, key
for (; ;) {
index = (!self._reverse) ? self.dataUpto : (self.data.length - 1 - self.dataUpto)
const awskey = self.data[index].Key
key = awskey.substring(self.db.folderPrefix.length, awskey.length)
debug('iterator data index %d: %s', index, key)
self.dataUpto++
if (self._test(key)) {
break
}
if (!self._reverse || self.dataUpto === self.data.length) {
return setImmediate(callback)
}
}
if (self.fetchValues) {
if (self.data[index].Size === 0)
getCallback(null, '')
else
self.db._get(key, { asBuffer: self.valueAsBuffer }, getCallback)
}
else
getCallback()
function getCallback(err, value) {
debug('iterator data getCallback %s = %s', key, value)
if (err) {
if (err.message == 'NotFound') {
// collection changed while we were iterating, skip this key
return setImmediate(function () {
self._next(callback)
})
}
return setImmediate(function () {
callback(err)
})
}
if (self.keyAsBuffer && !(key instanceof Buffer))
key = Buffer.from(key)
if (!self.keyAsBuffer && (key instanceof Buffer))
key = key.toString('utf8')
if (self.fetchValues) {
if (self.valueAsBuffer && !(value instanceof Buffer))
value = Buffer.from(value)
if (!self.valueAsBuffer && (value instanceof Buffer))
value = value.toString('utf8')
}
setImmediate(function () {
debug('_next result %s=%s', key, value)
callback(null, key, value)
})
}
}
}
_test() { return true }
}
class S3LevelDown extends AbstractLevelDOWN {
constructor(location, s3) {
super()
if (typeof location !== 'string') {
throw new Error('constructor requires a location string argument')
}
this.s3 = s3 || staticS3
if (location.indexOf('/') !== -1) {
this.folderPrefix = location.substring(location.indexOf('/') + 1, location.length) + '/'
this.bucket = location.substring(0, location.indexOf('/'))
} else {
this.folderPrefix = ''
this.bucket = location
}
debug('db init %s %s', this.bucket, this.folderPrefix)
}
_open(options, callback) {
this.s3.send(new AWS.HeadBucketCommand({ Bucket: this.bucket }), (err) => {
if (err) {
// error, bucket is not found
if (options.createIfMissing && err['$metadata'].httpStatusCode === 404) {
// try to create it
this.s3.send(new AWS.CreateBucketCommand({ Bucket: this.bucket }), (err) => {
if (err) {
setImmediate(() => callback(err))
} else {
setImmediate(callback)
}
})
} else {
setImmediate(() => callback(new Error(`Bucket ${this.bucket} does not exists or is inaccessible`)))
}
} else {
setImmediate(callback)
}
})
}
_put(key, value, options, callback) {
if (nullEmptyUndefined(value))
value = Buffer.from('')
if (!(value instanceof Buffer || value instanceof String))
value = String(value)
this.s3.send(new AWS.PutObjectCommand({
Bucket: this.bucket,
Key: this.folderPrefix + key,
Body: value
}), function (err) {
if (err) {
debug('Error s3 upload: %s %s', key, err.message)
callback(err)
} else {
debug('Successful s3 upload: %s', key)
callback()
}
})
}
_get(key, options, callback) {
this.s3.send(new AWS.GetObjectCommand({
Bucket: this.bucket,
Key: this.folderPrefix + key
}), async function (err, data) {
if (err) {
debug('Error s3 getObject: %s %s', key, err.message)
if (err.Code === 'NoSuchKey') {
callback(new Error('NotFound'))
} else {
callback(err)
}
} else {
let value
try {
debug('s3 getObject callback as %s: %s', options.asBuffer ? 'buf' : 'string', key)
if (options && options.asBuffer) {
const byteArray = await data.Body?.transformToByteArray()
value = Buffer.from(byteArray.buffer, byteArray.byteOffset, byteArray.byteLength)
} else {
value = await data.Body?.transformToString('utf8')
}
} catch (err) {
callback(err, null)
return
}
callback(null, value)
}
})
}
_del(key, options, callback) {
this.s3.send(new AWS.DeleteObjectCommand({
Bucket: this.bucket,
Key: this.folderPrefix + key
}), function (err) {
if (err) {
debug('Error s3 delete: %s %s', key, err.message)
callback(err)
} else {
debug('Successful s3 delete: %s', key)
callback()
}
})
}
_batch(array, options, callback) {
const len = array.length, self = this
let i = 0;
function act(action, cb) {
if (!action) {
return setImmediate(cb)
}
const key = (action.key instanceof Buffer) ? action.key : String(action.key)
const value = action.value
if (action.type === 'put') {
self._put(key, value, null, cb)
} else if (action.type === 'del') {
self._del(key, null, cb)
}
}
function actCallback(err) {
if (err) {
return setImmediate(function () { callback(err) })
}
if (++i >= len) {
return setImmediate(callback)
}
act(array[i], actCallback)
}
act(array[i], actCallback)
}
_iterator(options) {
return new S3Iterator(this, options)
}
}
module.exports = S3LevelDown