forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
159 lines (145 loc) · 3.37 KB
/
Copy pathutils.js
File metadata and controls
159 lines (145 loc) · 3.37 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
'use strict';
const { promisify } = require('util');
const {
accessSync, openSync, readSync, closeSync,
access, open, read, close
} = require('fs');
const { normalize } = require('path');
const BUFFER_LENGTH = 262;
const accessAsync = promisify(access);
const openAsync = promisify(open);
const readAsync = promisify(read);
const closeAsync = promisify(close);
/**
* Returns true if value is a String
* @param {*} val
* @returns {Boolean}
*/
function isString(val) {
return typeof val === 'string';
}
/**
* Returns true if value is an Object
* @param {*} val
* @returns {Boolean}
*/
function isObject(val) {
return Object.prototype.toString.call(val) === '[object Object]';
}
/**
* Returns true if value is a Buffer, UInt8Array or ArrayBuffer
* @param {*} val
* @returns {Boolean}
*/
function isBuffer(val) {
return Buffer.isBuffer(val) || val instanceof Uint8Array || val instanceof ArrayBuffer;
}
/**
* Returns true if value is a Readable Stream
* @param {*} val
* @returns {Boolean}
*/
function isStream(val) {
return typeof val === 'object' && typeof val.pipe === 'function'
&& val.readable !== false && typeof val._read === 'function' && !val.closed && !val.destroyed;
}
/**
* Tests whether or not the given path exists
* @param {String} path
* @returns {Boolean}
*/
function isExistsSync(path) {
try {
accessSync(normalize(path));
return true;
} catch (e) {
return false;
}
}
/**
* Tests whether or not the given path exists
* @param {String} path
* @returns {Promise<Boolean>}
* @async
*/
async function isExists(path) {
try {
await accessAsync(normalize(path));
return true;
} catch (e) {
return false;
}
}
/**
* Read a chunk from a file
* @param {Object} data
* @param {String} [data.path]
* @param {String} [data.flags]
* @param {String} [data.mode]
* @returns {{bytesRead: Number, buffer: Buffer}}
*/
function chunkSync(data) {
if (!data.fd && data.path) {
data.path = normalize(data.path);
if (isExistsSync(data.path)) {
data.fd = openSync(data.path, data.flags, data.mode);
}
}
if (data.fd) {
try {
const buffer = Buffer.alloc(BUFFER_LENGTH);
const bytesRead = readSync(data.fd, buffer, 0, BUFFER_LENGTH);
return { bytesRead, buffer };
} finally {
closeSync(data.fd);
}
}
throw new Error('The file must be local and exists.');
}
/**
* Read a chunk from a file
* @param {Object} data
* @param {String} data.path
* @param {String} data.flags
* @param {String} [data.mode]
* @returns {Promise<{bytesRead: Number, buffer: Buffer}>}
* @async
*/
async function chunkAsync(data) {
const fd = await openAsync(normalize(data.path), data.flags, data.mode);
try {
return await readAsync(fd, Buffer.alloc(BUFFER_LENGTH), 0, BUFFER_LENGTH, 0);
} finally {
await closeAsync(fd);
}
}
/**
* Read a chunk from a stream
* @param {ReadableStream} stream
* @returns {Promise<Buffer>}
* @async
*/
async function streamChunk(stream) {
return new Promise((resolve, reject) => {
stream.once('error', reject);
stream.once('readable', () => {
const chunk = stream.read(BUFFER_LENGTH) || stream.read();
if (chunk) {
stream.unshift(chunk);
}
return resolve(chunk);
});
});
}
module.exports = {
isString,
isObject,
isBuffer,
isStream,
isExists,
isExistsSync,
chunkAsync,
chunkSync,
streamChunk,
BUFFER_LENGTH
};