swarmwire-lib 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/fs.js +26 -0
- package/package.json +1 -1
package/lib/fs.js
CHANGED
|
@@ -8,12 +8,25 @@
|
|
|
8
8
|
import fs from 'fs'
|
|
9
9
|
import b4a from 'b4a'
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* `fd:N` names an already-open descriptor instead of a path (Android hands out descriptors
|
|
13
|
+
* for picked files, with no path). Read-only, files only; the caller keeps it open.
|
|
14
|
+
* shortcut: sync fstat/read on the fd, upgrade if the blocking reads ever show up in profiles.
|
|
15
|
+
*/
|
|
16
|
+
const fdOf = (path) => (/^fd:\d+$/.test(path) ? Number(path.slice(3)) : null)
|
|
17
|
+
|
|
11
18
|
/**
|
|
12
19
|
* @param {string} path
|
|
13
20
|
* @returns {Promise<{size: number, mtimeMs: number, directory: boolean}|null>} null if it
|
|
14
21
|
* does not exist
|
|
15
22
|
*/
|
|
16
23
|
export async function stat (path) {
|
|
24
|
+
const fd = fdOf(path)
|
|
25
|
+
if (fd !== null) {
|
|
26
|
+
const st = fs.fstatSync(fd)
|
|
27
|
+
return { size: st.size, mtimeMs: Math.floor(st.mtimeMs), directory: false }
|
|
28
|
+
}
|
|
29
|
+
|
|
17
30
|
try {
|
|
18
31
|
const st = await fs.promises.stat(path)
|
|
19
32
|
return { size: st.size, mtimeMs: Math.floor(st.mtimeMs), directory: st.isDirectory() }
|
|
@@ -25,6 +38,19 @@ export async function stat (path) {
|
|
|
25
38
|
|
|
26
39
|
/** Open a file for reading byte ranges out of it, in place. */
|
|
27
40
|
export async function openRead (path) {
|
|
41
|
+
const fd = fdOf(path)
|
|
42
|
+
if (fd !== null) {
|
|
43
|
+
return {
|
|
44
|
+
async read (offset, length) {
|
|
45
|
+
const buffer = b4a.alloc(length)
|
|
46
|
+
const bytesRead = length === 0 ? 0 : fs.readSync(fd, buffer, 0, length, offset)
|
|
47
|
+
return bytesRead === length ? buffer : buffer.subarray(0, bytesRead)
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
close () {} // not ours to close
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
28
54
|
const handle = await fs.promises.open(path, 'r')
|
|
29
55
|
|
|
30
56
|
return {
|
package/package.json
CHANGED