88 lines
3.0 KiB
JavaScript
88 lines
3.0 KiB
JavaScript
import { promises, readFileSync } from 'fs';
|
|
import { json } from './imports.js';
|
|
import { validateArgument } from './utils/validate.js';
|
|
const supportedReturnAs = ['utf8', 'buffer', 'json', 'jsonWithDates'];
|
|
export function validateInput(methodName, path, returnAs) {
|
|
const methodSignature = methodName + '(path, returnAs)';
|
|
validateArgument(methodSignature, 'path', path, ['string']);
|
|
validateArgument(methodSignature, 'returnAs', returnAs, ['string', 'undefined']);
|
|
if (returnAs && !supportedReturnAs.includes(returnAs)) {
|
|
throw new Error('Argument "returnAs" passed to ' + methodSignature
|
|
+ ' must have one of values: ' + supportedReturnAs.join(', '));
|
|
}
|
|
}
|
|
// Matches strings generated by Date.toJSON()
|
|
// which is called to serialize date to JSON.
|
|
const jsonDateParser = (key, value) => {
|
|
const reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}\.\d*)(?:Z|(\+|-)([\d|:]*))?$/;
|
|
if (typeof value === 'string') {
|
|
if (reISO.test(value)) {
|
|
return new Date(value);
|
|
}
|
|
}
|
|
return value;
|
|
};
|
|
const ErrJson = (path, err) => {
|
|
const nicerError = new Error('JSON parsing failed while reading '
|
|
+ path + ' [' + err + ']');
|
|
nicerError.originalError = err;
|
|
return nicerError;
|
|
};
|
|
// ---------------------------------------------------------
|
|
// SYNC
|
|
// ---------------------------------------------------------
|
|
export function sync(path, returnAs) {
|
|
const retAs = returnAs || 'utf8';
|
|
let data;
|
|
try {
|
|
data = readFileSync(path, { encoding: retAs === 'buffer' ? null : 'utf8' });
|
|
}
|
|
catch (err) {
|
|
if (err.code === 'ENOENT') {
|
|
// If file doesn't exist return undefined instead of throwing.
|
|
return undefined;
|
|
}
|
|
// Otherwise rethrow the error
|
|
throw err;
|
|
}
|
|
try {
|
|
if (retAs === 'json') {
|
|
data = json.parse(data);
|
|
}
|
|
else if (retAs === 'jsonWithDates') {
|
|
data = json.parse(data, jsonDateParser);
|
|
}
|
|
}
|
|
catch (err) {
|
|
throw ErrJson(path, err);
|
|
}
|
|
return data;
|
|
}
|
|
// ---------------------------------------------------------
|
|
// ASYNC
|
|
// ---------------------------------------------------------
|
|
export function async(path, returnAs) {
|
|
return new Promise((resolve, reject) => {
|
|
const retAs = returnAs || 'utf8';
|
|
promises.readFile(path, { encoding: retAs === 'buffer' ? null : 'utf8' })
|
|
.then((data) => {
|
|
// Make final parsing of the data before returning.
|
|
try {
|
|
if (retAs === 'json') {
|
|
resolve(json.parse(data));
|
|
}
|
|
else if (retAs === 'jsonWithDates') {
|
|
resolve(json.parse(data, jsonDateParser));
|
|
}
|
|
else {
|
|
resolve(data);
|
|
}
|
|
}
|
|
catch (err) {
|
|
reject(ErrJson(path, err));
|
|
}
|
|
})
|
|
.catch((err) => (err.code === 'ENOENT' ? resolve(null) : reject(err)));
|
|
});
|
|
}
|