Skip to content

Commit 36ea56c

Browse files
authored
use ras 3 (#32)
1 parent e4dab4b commit 36ea56c

File tree

3 files changed

+27
-49
lines changed

3 files changed

+27
-49
lines changed

index.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = class RandomAccessFile extends RandomAccessStorage {
2424
constructor (filename, opts = {}) {
2525
const size = opts.size || (opts.truncate ? 0 : -1)
2626

27-
super({ createAlways: size >= 0 })
27+
super()
2828

2929
if (opts.directory) filename = path.join(opts.directory, path.resolve('/', filename).replace(/^\w+:\\/, ''))
3030

@@ -44,20 +44,20 @@ module.exports = class RandomAccessFile extends RandomAccessStorage {
4444
this._lock = opts.lock === true
4545
this._sparse = opts.sparse === true
4646
this._alloc = opts.alloc || Buffer.allocUnsafe
47+
this._alwaysCreate = size >= 0
4748
}
4849

4950
_open (req) {
51+
const create = this._alwaysCreate || this.writing // .writing comes from RAS
5052
const self = this
51-
const mode = this.mode | (req.create ? CREAT : 0)
53+
const mode = this.mode | (create ? CREAT : 0)
5254

53-
if (req.create) fs.mkdir(path.dirname(this.filename), { recursive: true }, ondir)
55+
if (create) fs.mkdir(path.dirname(this.filename), { recursive: true }, ondir)
5456
else ondir(null)
5557

5658
function ondir (err) {
5759
if (err) return req.callback(err)
58-
59-
if (self.fd) fs.close(self.fd, oncloseold)
60-
else fs.open(self.filename, mode, onopen)
60+
fs.open(self.filename, mode, onopen)
6161
}
6262

6363
function onopen (err, fd) {
@@ -90,13 +90,6 @@ module.exports = class RandomAccessFile extends RandomAccessStorage {
9090
fs.ftruncate(self.fd, self._size, ontruncate)
9191
}
9292

93-
function oncloseold (err) {
94-
if (err) return onerrorafteropen(err)
95-
96-
self.fd = 0
97-
fs.open(self.filename, mode, onopen)
98-
}
99-
10093
function ontruncate (err) {
10194
if (err) return onerrorafteropen(err)
10295

@@ -192,7 +185,7 @@ module.exports = class RandomAccessFile extends RandomAccessStorage {
192185
}
193186
}
194187

195-
_destroy (req) {
188+
_unlink (req) {
196189
const self = this
197190

198191
const root = this.directory && path.resolve(path.join(this.directory, '.'))

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"browser.js"
1313
],
1414
"dependencies": {
15-
"random-access-storage": "^2.2.0"
15+
"random-access-storage": "^3.0.0"
1616
},
1717
"optionalDependencies": {
1818
"fs-native-extensions": "^1.1.0"

test/basic.js

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test('write and read', function (t) {
1919
file.read(0, 5, function (err, buf) {
2020
t.absent(err, 'no error')
2121
t.alike(buf, Buffer.from('hello'))
22-
file.destroy(() => t.pass())
22+
file.unlink(() => t.pass())
2323
})
2424
})
2525
})
@@ -31,7 +31,7 @@ test('read before write', function (t) {
3131

3232
file.read(0, 0, function (err, buf) {
3333
t.ok(err, 'not created')
34-
file.destroy(() => t.pass())
34+
file.unlink(() => t.pass())
3535
})
3636
})
3737

@@ -42,7 +42,7 @@ test('read range before write', function (t) {
4242

4343
file.read(0, 5, function (err, buf) {
4444
t.ok(err, 'not created')
45-
file.destroy(() => t.pass())
45+
file.unlink(() => t.pass())
4646
})
4747
})
4848

@@ -55,7 +55,7 @@ test('read range > file', function (t) {
5555
t.absent(err, 'no error')
5656
file.read(0, 10, function (err, buf) {
5757
t.ok(err, 'not satisfiable')
58-
file.destroy(() => t.pass())
58+
file.unlink(() => t.pass())
5959
})
6060
})
6161
})
@@ -130,7 +130,7 @@ test('truncate with size', function (t) {
130130
file.stat(function (err, st) {
131131
t.absent(err, 'no error')
132132
t.is(st.size, 100)
133-
file.destroy(() => t.pass())
133+
file.unlink(() => t.pass())
134134
})
135135
})
136136

@@ -159,7 +159,7 @@ test('mkdir path', function (t) {
159159
file.read(0, 5, function (err, buf) {
160160
t.absent(err, 'no error')
161161
t.alike(buf, Buffer.from('hello'))
162-
file.destroy(() => t.pass())
162+
file.unlink(() => t.pass())
163163
})
164164
})
165165
})
@@ -192,7 +192,7 @@ test('write/read big chunks', async function (t) {
192192

193193
await io
194194

195-
file.destroy(() => t.pass())
195+
file.unlink(() => t.pass())
196196
})
197197

198198
test('rmdir option', function (t) {
@@ -206,11 +206,11 @@ test('rmdir option', function (t) {
206206
file.read(0, 2, function (err, buf) {
207207
t.absent(err, 'no error')
208208
t.alike(buf, Buffer.from('hi'))
209-
file.destroy(ondestroy)
209+
file.unlink(onunlink)
210210
})
211211
})
212212

213-
function ondestroy (err) {
213+
function onunlink (err) {
214214
t.absent(err, 'no error')
215215
fs.stat(path.join(tmp, 'rmdir'), function (err) {
216216
t.is(err && err.code, 'ENOENT', 'should be removed')
@@ -231,11 +231,11 @@ test('rmdir option with non empty parent', function (t) {
231231
file.read(0, 2, function (err, buf) {
232232
t.absent(err, 'no error')
233233
t.alike(buf, Buffer.from('hi'))
234-
file.destroy(ondestroy)
234+
file.unlink(onunlink)
235235
})
236236
})
237237

238-
function ondestroy (err) {
238+
function onunlink (err) {
239239
t.absent(err, 'no error')
240240
fs.stat(path.join(tmp, 'rmdir'), function (err) {
241241
t.absent(err, 'should not be removed')
@@ -264,7 +264,7 @@ test('del, partial file block', function (t) {
264264
file.read(50, 50, function (err, buf) {
265265
t.absent(err, 'no error')
266266
t.alike(buf, Buffer.alloc(50))
267-
file.destroy(() => t.pass())
267+
file.unlink(() => t.pass())
268268
})
269269
})
270270
})
@@ -289,7 +289,7 @@ test('del, whole file block', function (t) {
289289
t.absent(err, 'no error')
290290
t.comment(before.blocks + ' -> ' + after.blocks + ' blocks')
291291
t.ok(after.blocks < before.blocks, 'fewer blocks')
292-
file.destroy(() => t.pass())
292+
file.unlink(() => t.pass())
293293
})
294294
})
295295
})
@@ -314,7 +314,7 @@ test('del, partial and whole', function (t) {
314314
t.absent(err, 'no error')
315315
t.comment(before.blocks + ' -> ' + after.blocks + ' blocks')
316316
t.ok(after.blocks < before.blocks, 'fewer blocks')
317-
file.destroy(() => t.pass())
317+
file.unlink(() => t.pass())
318318
})
319319
})
320320
})
@@ -333,7 +333,7 @@ test('del, infinity', function (t) {
333333
file.stat(function (err, st) {
334334
t.absent(err, 'no error')
335335
t.is(st.size, 0)
336-
file.destroy(() => t.pass())
336+
file.unlink(() => t.pass())
337337
})
338338
})
339339
})
@@ -353,7 +353,7 @@ test('truncate', function (t) {
353353
file.stat(function (err, st) {
354354
t.absent(err, 'no error')
355355
t.is(st.size, 20)
356-
file.destroy(() => t.pass())
356+
file.unlink(() => t.pass())
357357
})
358358
})
359359
})
@@ -397,21 +397,6 @@ test('open and close many times', function (t) {
397397
}
398398
})
399399

400-
test('trigger bad open', function (t) {
401-
t.plan(3)
402-
403-
const file = new RAF(gen(), { truncate: true })
404-
405-
file.fd = 10000
406-
file.open(function (err) {
407-
t.ok(err, 'should error trying to close old fd')
408-
file.open(function (err) {
409-
t.absent(err, 'no error')
410-
file.destroy(() => t.pass())
411-
})
412-
})
413-
})
414-
415400
test('cannot escape directory', function (t) {
416401
t.plan(2)
417402

@@ -430,7 +415,7 @@ test('directory filename resolves correctly', function (t) {
430415
t.is(file.filename, path.join(tmp, name))
431416
})
432417

433-
test('destroy', async function (t) {
418+
test('unlink', async function (t) {
434419
t.plan(5)
435420

436421
const name = gen()
@@ -441,11 +426,11 @@ test('destroy', async function (t) {
441426
file.read(0, 2, function (err, buf) {
442427
t.absent(err, 'no error')
443428
t.alike(buf, Buffer.from('hi'))
444-
file.destroy(ondestroy)
429+
file.unlink(onunlink)
445430
})
446431
})
447432

448-
function ondestroy (err) {
433+
function onunlink (err) {
449434
t.absent(err, 'no error')
450435
fs.stat(name, function (err) {
451436
t.is(err && err.code, 'ENOENT', 'should be removed')

0 commit comments

Comments
 (0)