-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcommon.js
More file actions
198 lines (168 loc) · 5 KB
/
Copy pathcommon.js
File metadata and controls
198 lines (168 loc) · 5 KB
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
const { HyperdriveOptions, DriveStats, MountStats, CoreStats } = require('./rpc/daemon/common_pb')
const { Stat, Mount } = require('./rpc/daemon/drive_pb.js')
const CHUNK_SIZE = 3.9e6
Stat.IFSOCK = 49152 // 0b1100...
Stat.IFLNK = 40960 // 0b1010...
Stat.IFREG = 32768 // 0b1000...
Stat.IFBLK = 24576 // 0b0110...
Stat.IFDIR = 16384 // 0b0100...
Stat.IFCHR = 8192 // 0b0010...
Stat.IFIFO = 4096 // 0b0001...
function fromHyperdriveOptions (opts) {
const extracted = {
key: opts.getKey() || null,
version: opts.getVersion() || null,
hash: opts.getHash() || null,
writable: opts.getWritable() || false
}
if (extracted.key) extracted.key = Buffer.from(extracted.key)
if (extracted.hash) extracted.hash = Buffer.from(extracted.hash)
return extracted
}
function toHyperdriveOptions (opts) {
opts = opts || {}
const hdopts = new HyperdriveOptions()
hdopts.setKey(opts.key)
hdopts.setVersion(opts.version)
hdopts.setHash(opts.hash)
hdopts.setWritable(opts.writable)
return hdopts
}
function fromMount (mount) {
const key = mount.getKey()
const hash = mount.getHash()
return {
key: key ? Buffer.from(key) : null,
hash: hash ? Buffer.from(hash) : null,
version: mount.getVersion(),
hypercore: mount.getHypercore()
}
}
function toMount (mountOpts) {
mountOpts = mountOpts || {}
const mnt = new Mount()
mnt.setKey(mountOpts.key)
mnt.setVersion(mountOpts.version)
mnt.setHash(mountOpts.hash)
mnt.setHypercore(mountOpts.hypercore)
return mnt
}
function fromStat (stat) {
const metadataMap = stat.getMetadataMap()
if (metadataMap) {
var metadata = {}
for (let key of Object.keys(stat.getMetadataMap())) {
metadata[key] = metadataMap.get(key)
}
}
return {
isSocket: statModeCheck(Stat.IFSOCK),
isSymbolicLink: statModeCheck(Stat.IFLNK),
isFile: statModeCheck(Stat.IFREG),
isBlockDevice: statModeCheck(Stat.IFBLK),
isDirectory: statModeCheck(Stat.IFDIR),
isCharacterDevice: statModeCheck(Stat.IFCHR),
isFIFO: statModeCheck(Stat.IFIFO),
mode: stat.getMode(),
uid: stat.getUid(),
gid: stat.getGid(),
size: stat.getSize(),
mtime: stat.getMtime(),
ctime: stat.getCtime(),
linkname: stat.getLinkname(),
mount: fromMount(stat.getMount()),
metadata
}
}
function statModeCheck (mask) {
return function () {
return (mask & this.mode) === mask
}
}
function toStat (statOpts) {
if (!statOpts) return null
const stat = new Stat()
stat.setMode(statOpts.mode)
stat.setUid(statOpts.uid)
stat.setGid(statOpts.gid)
stat.setSize(statOpts.size)
stat.setMtime(statOpts.mtime)
stat.setCtime(statOpts.ctime)
stat.setLinkname(statOpts.linkname)
stat.setMount(toMount(statOpts.mount))
if (statOpts.metadata) {
const metadataMap = stat.getMetadataMap()
for (let key of Object.keys(statOpts.metadata)) {
metadataMap.set(key, statOpts[key])
}
}
return stat
}
function fromDriveStats (driveStats) {
return driveStats.getStatsList().map(st => getMountStats(st))
function getMountStats (mountStats) {
return {
path: mountStats.getPath(),
metadata: getCoreStats(mountStats.getMetadata()),
content: getCoreStats(mountStats.getContent())
}
}
function getCoreStats (coreStats) {
return {
key: Buffer.from(coreStats.getKey()),
peers: coreStats.getPeers(),
uploadedBytes: coreStats.getUploadedbytes(),
downloadedBytes: coreStats.getDownloadedbytes(),
uploadedBytesCumulative: coreStats.getUploadedbytescumulative(),
downloadedBytesCumulative: coreStats.getDownloadedbytescumulative()
}
}
}
function toDriveStats (stats) {
if (!stats) return null
const driveStats = new DriveStats()
const mountStatsList = []
for (const ms of stats) {
const mountStats = new MountStats()
mountStats.setPath(ms.path)
const metadataStats = new CoreStats()
const contentStats = new CoreStats()
setCoreStats(metadataStats, ms.metadata)
setCoreStats(contentStats, ms.content)
mountStats.setMetadata(metadataStats)
mountStats.setContent(contentStats)
mountStatsList.push(mountStats)
}
driveStats.setStatsList(mountStatsList)
return driveStats
function setCoreStats (coreStats, stats) {
coreStats.setKey(stats.key)
coreStats.setPeers(stats.peers)
coreStats.setUploadedbytes(stats.uploadedBytes)
coreStats.setDownloadedbytes(stats.downloadedBytes)
coreStats.setUploadedbytescumulative(stats.uploadedBytesCumulative)
coreStats.setDownloadedbytescumulative(stats.downloadedBytesCumulative)
}
}
function toChunks (content) {
const chunks = []
for (let i = 0; i * CHUNK_SIZE < content.length; i++) {
const offset = i * CHUNK_SIZE
const len = Math.min(CHUNK_SIZE, content.length - offset)
const chunk = Buffer.allocUnsafe(len)
content.copy(chunk, 0, offset, offset + len)
chunks.push(chunk)
}
return chunks
}
module.exports = {
fromHyperdriveOptions,
toHyperdriveOptions,
fromStat,
toStat,
fromMount,
toMount,
fromDriveStats,
toDriveStats,
toChunks
}