-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
98 lines (93 loc) · 2.39 KB
/
index.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
var fs = require('fs')
var promisify = require('util').promisify
var native = bindings(['./build/Release/flock.node', './flock.node'])
function bindings (paths) {
for (var i = 0; i < paths.length; ++i) {
try {
return require(paths[i])
} catch (err) {
continue
}
}
throw new Error('Could not locate the bindings file. Tried:\n' +
paths.map(it => ` =>${it}`).join('\n'))
}
function normalizeOptionsToMode (options) {
if (options == null) {
options = {}
}
if (typeof options !== 'object') {
throw new TypeError('Expect an object to be second argument of flock')
}
var mode = 0
if (!options.wait) {
mode = mode | native.LOCK_NB
}
if (options.exclusive) {
mode = mode | native.LOCK_EX
} else {
mode = mode | native.LOCK_SH
}
return mode
}
module.exports.lock = lock
module.exports.lockAsync = promisify(lock)
function lock (path, options, callback) {
if (typeof options === 'function') {
callback = options
options = null
}
if (typeof callback !== 'function') {
throw new TypeError('Expect argument callback of flock.lock to be a function')
}
var mode = normalizeOptionsToMode(options)
fs.open(path, 'w', (err, fd) => {
if (err) {
callback(err)
return
}
native.flock(fd, mode, ret => {
if (ret !== 0) {
var msg = native.stdErr(ret)
callback(new Error(msg))
return
}
callback(null, fd)
})
})
}
module.exports.upgrade = upgrade
module.exports.upgradeAsync = promisify(upgrade)
function upgrade (fd, options, callback) {
if (typeof options === 'function') {
callback = options
options = null
}
if (typeof callback !== 'function') {
throw new TypeError('Expect argument callback of flock.upgrade to be a function')
}
var mode = normalizeOptionsToMode(options)
native.flock(fd, mode, (ret) => {
if (ret !== 0) {
var msg = native.stdErr(ret)
callback(new Error(msg))
return
}
callback(null, fd)
})
}
module.exports.unlock = unlock
module.exports.unlockAsync = promisify(unlock)
function unlock (fd, callback) {
if (typeof callback !== 'function') {
throw new TypeError('Expect a function to be second argument of flock.unlock')
}
native.flock(fd, native.LOCK_UN, (ret) => {
if (ret !== 0) {
var msg = native.stdErr(ret)
callback(new Error(msg))
return
}
fs.close(fd, callback)
})
}