git-documentdb
Version:
Offline-first database that syncs with Git
231 lines • 7.58 kB
JavaScript
;
/**
* GitDocumentDB
* Copyright (c) Hidekazu Kubota
*
* This source code is licensed under the Mozilla Public License Version 2.0
* found in the LICENSE file in the root directory of this source tree.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSameFatDoc = exports.readLatestBlob = exports.readBlobByOid = exports.blobToBinary = exports.blobToText = exports.blobToJsonDocWithoutOverwrittenId = exports.blobToJsonDoc = exports.textToJsonDoc = void 0;
const fs_1 = __importDefault(require("fs"));
const js_yaml_1 = __importDefault(require("js-yaml"));
const isomorphic_git_1 = require("isomorphic-git");
const const_1 = require("../const");
const utils_1 = require("../utils");
const error_1 = require("../error");
/**
* textToJsonDoc
* @throws {@link Err.InvalidJsonObjectError}
*/
// eslint-disable-next-line complexity
function textToJsonDoc(text, serializeFormat, extension, shortId) {
let jsonDoc;
if (serializeFormat.format === 'front-matter') {
if (extension === const_1.YAML_POSTFIX) {
try {
jsonDoc = js_yaml_1.default.load(text);
}
catch {
throw new error_1.Err.InvalidJsonObjectError(shortId);
}
if (jsonDoc === undefined) {
if (shortId !== undefined) {
jsonDoc = {
_id: shortId,
};
}
else {
jsonDoc = {};
}
}
}
else {
const mdArray = text.split('\n');
let yamlText = '';
let markdownText = '';
let startFrontMatter = false;
let endFrontMatter = false;
for (let i = 0; i < mdArray.length; i++) {
if (mdArray[i] === '---') {
// eslint-disable-next-line max-depth
if (!startFrontMatter) {
startFrontMatter = true;
continue;
}
else if (!endFrontMatter) {
endFrontMatter = true;
continue;
}
}
if (startFrontMatter && !endFrontMatter) {
if (yamlText !== '') {
yamlText += '\n';
}
yamlText += mdArray[i];
}
else if (endFrontMatter) {
if (markdownText !== '') {
markdownText += '\n';
}
markdownText += mdArray[i];
}
}
if (!endFrontMatter) {
markdownText = text;
if (shortId !== undefined) {
jsonDoc = {
_id: shortId,
};
}
else {
jsonDoc = {};
}
}
else {
try {
jsonDoc = js_yaml_1.default.load(yamlText);
}
catch {
throw new error_1.Err.InvalidJsonObjectError(shortId);
}
if (jsonDoc === undefined) {
if (shortId !== undefined) {
jsonDoc = {
_id: shortId,
};
}
else {
jsonDoc = {};
}
}
}
if (markdownText !== '' || extension === const_1.FRONT_MATTER_POSTFIX) {
jsonDoc._body = markdownText;
}
}
}
else {
try {
jsonDoc = JSON.parse(text);
}
catch {
throw new error_1.Err.InvalidJsonObjectError(shortId);
}
}
return jsonDoc;
}
exports.textToJsonDoc = textToJsonDoc;
/**
* blobToJsonDoc
*
* @throws {@link Err.InvalidJsonObjectError}
*/
function blobToJsonDoc(shortId, readBlobResult, withMetadata, serializeFormat, extension) {
const text = (0, utils_1.utf8decode)(readBlobResult.blob);
const jsonDoc = textToJsonDoc(text, serializeFormat, extension, shortId);
if (jsonDoc._id !== undefined) {
// Overwrite _id property by shortId (_id without collectionPath) if JsonDoc is created by GitDocumentedDB (_id !== undefined).
jsonDoc._id = shortId;
}
if (withMetadata) {
const fatJsonDoc = {
_id: shortId,
name: shortId + serializeFormat.extension(jsonDoc),
fileOid: readBlobResult.oid,
type: 'json',
doc: jsonDoc,
};
return fatJsonDoc;
}
return jsonDoc;
}
exports.blobToJsonDoc = blobToJsonDoc;
/**
* blobToJsonDocWithoutOverwrittenId
*
* @throws {@link Err.InvalidJsonObjectError}
*/
// eslint-disable-next-line complexity
function blobToJsonDocWithoutOverwrittenId(readBlobResult, serializeFormat, extension) {
const text = (0, utils_1.utf8decode)(readBlobResult.blob);
const jsonDoc = textToJsonDoc(text, serializeFormat, extension);
return jsonDoc;
}
exports.blobToJsonDocWithoutOverwrittenId = blobToJsonDocWithoutOverwrittenId;
/**
* blobToText
*/
function blobToText(shortName, readBlobResult, withMetadata) {
const text = (0, utils_1.utf8decode)(readBlobResult.blob);
if (withMetadata) {
const fatTextDoc = {
name: shortName,
fileOid: readBlobResult.oid,
type: 'text',
doc: text,
};
return fatTextDoc;
}
return text;
}
exports.blobToText = blobToText;
/**
* blobToBinary
*/
function blobToBinary(shortName, readBlobResult, withMetadata) {
if (withMetadata) {
const fatBinaryDoc = {
name: shortName,
fileOid: readBlobResult.oid,
type: 'binary',
doc: readBlobResult.blob,
};
return fatBinaryDoc;
}
return readBlobResult.blob;
}
exports.blobToBinary = blobToBinary;
/**
* readBlobByOid
*/
async function readBlobByOid(workingDir, oid) {
return await (0, isomorphic_git_1.readBlob)({
fs: fs_1.default,
dir: workingDir,
oid,
}).catch(() => undefined);
}
exports.readBlobByOid = readBlobByOid;
/**
* readLatestBlob
*/
async function readLatestBlob(workingDir, fullDocPath) {
const commitOid = await (0, isomorphic_git_1.resolveRef)({ fs: fs_1.default, dir: workingDir, ref: 'HEAD' }).catch(() => undefined);
if (commitOid === undefined)
return undefined;
return await (0, isomorphic_git_1.readBlob)({
fs: fs_1.default,
dir: workingDir,
oid: commitOid,
filepath: fullDocPath,
}).catch(() => undefined);
}
exports.readLatestBlob = readLatestBlob;
/**
* Check if two FatDocs are the same.
*/
function isSameFatDoc(a, b) {
if (a.type !== b.type) {
return false;
}
if (a.type === 'json') {
return JSON.stringify(a) === JSON.stringify(b);
}
return a === b;
}
exports.isSameFatDoc = isSameFatDoc;
//# sourceMappingURL=blob.js.map