UNPKG

ownfiles

Version:

A library to manage files in a Solid User's Pod

220 lines 10 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addToIndex = exports.deleteFromIndex = exports.readIndex = exports.deleteIndex = exports.createIndex = void 0; const urlUtils = __importStar(require("url")); const cuid_1 = __importDefault(require("cuid")); const rdf = __importStar(require("rdflib")); const ns = require('solid-namespace')(rdf); exports.createIndex = async function (url, options) { var _a; const parsedUrl = urlUtils.parse(url); const rootUrl = `${parsedUrl.protocol}//${parsedUrl.host}/`; const indexUrl = rootUrl + this.indexPath; await this.createIfNotExist(indexUrl); this.indexURI = indexUrl; const items = (_a = options === null || options === void 0 ? void 0 : options.items) !== null && _a !== void 0 ? _a : (await this.deepRead(url, { verbose: true, foundCallback: options === null || options === void 0 ? void 0 : options.foundCallback, })); const { ins } = getNewIndexTriples(items, this.graph, indexUrl); await this.updater.put(rdf.sym(indexUrl), ins, 'text/turtle', (_, ok, error) => { if (!ok) throw error; }); return this.readIndex(indexUrl); }; exports.deleteIndex = async function (url) { const parsedUrl = urlUtils.parse(url); const rootUrl = `${parsedUrl.protocol}//${parsedUrl.host}/`; const indexUrl = rootUrl + this.indexPath; return this.delete(indexUrl) .then(() => (this.indexURI = undefined)) .catch((err) => { if (err.response.status === 404) { return; } else { throw err; } }); }; exports.readIndex = function (url) { const parsedUrl = urlUtils.parse(url); const rootUrl = `${parsedUrl.protocol}//${parsedUrl.host}/`; const indexUrl = rootUrl + this.indexPath; return this.fetcher.load(indexUrl).then(() => { const index = readIndexTriples(indexUrl, this.graph); return index; }); }; const readIndexTriples = (indexUrl, graph) => { const index = []; graph .statementsMatching(null, ns.rdf('type'), ns.solid('TypeRegistration'), rdf.sym(indexUrl)) .forEach(({ subject }) => { var _a; const instanceStatement = ((_a = (graph.any(subject, ns.solid('instance')) && graph.statementsMatching(subject, ns.solid('instance')))) !== null && _a !== void 0 ? _a : (graph.any(subject, ns.solid('instanceContainer')) && graph.statementsMatching(subject, ns.solid('instanceContainer'))))[0]; const typeNodes = graph .statementsMatching(subject, ns.solid('forClass')) .map((statement) => statement.object.value); index.push(instanceStatement.predicate.value === ns.solid('instance') ? { url: instanceStatement.object.value, types: typeNodes, } : { url: instanceStatement.object.value, types: typeNodes, }); }); return index; }; exports.deleteFromIndex = async function (item) { const parsedUrl = urlUtils.parse(item); const rootUrl = `${parsedUrl.protocol}//${parsedUrl.host}/`; const indexUrl = rootUrl + this.indexPath; const itemsToDelete = await this.deepRead(item); await this.fetcher.load(indexUrl); if (item === indexUrl) return Promise.resolve([]); return await Promise.all(itemsToDelete.map((item) => { item = item; const rootNode = (this.graph.any(null, ns.solid('instance'), rdf.sym(item)) || this.graph.any(null, ns.solid('instanceContainer'), rdf.sym(item))); return this.updater.update(this.graph.statementsMatching(rootNode), [], (_, ok, err) => { if (!ok) { console.log(err); } }); })); }; exports.addToIndex = async function (item, { force, updateCallback, } = {}) { var _a; const parsedItemUrl = typeof item === 'string' ? urlUtils.parse(item) : urlUtils.parse(item.url); const rootUrl = `${parsedItemUrl.protocol}//${parsedItemUrl.host}/`; const indexUrl = rootUrl + this.indexPath; const itemsToAdd = !force && typeof item === 'string' ? (await this.deepRead(item, { verbose: true, })) : [item]; if (!this.indexURI) { const res = await this.createIfNotExist(indexUrl); this.indexURI = (_a = res === null || res === void 0 ? void 0 : res.headers.get('Location')) !== null && _a !== void 0 ? _a : indexUrl; } const index = !force ? (await this.readIndex(rootUrl)) : []; const itemsToUpdate = index.reduce((itemsToUpdate, entry) => { var _a; const entryUrl = urlUtils.parse(entry.url); return ((_a = parsedItemUrl.pathname) === null || _a === void 0 ? void 0 : _a.includes(entryUrl.pathname)) ? [...itemsToUpdate, entry] : itemsToUpdate; }, []); const { del, ins } = getNewIndexTriples([...(itemsToUpdate !== null && itemsToUpdate !== void 0 ? itemsToUpdate : []), ...itemsToAdd], this.graph, indexUrl); return await this.updater.update(del, ins, updateCallback !== null && updateCallback !== void 0 ? updateCallback : ((_, ok, err) => { if (!ok) { console.log(err); } })); }; const getNewIndexTriples = (items, graph, indexUrl) => { const del = []; const ins = []; items .filter((item) => item.url !== indexUrl) .forEach((item) => { if (item && item.types.includes(ns.ldp('Container').value)) { let rootNode = graph.any(null, ns.solid('instanceContainer'), rdf.sym(item.url)); if (!rootNode) { rootNode = new rdf.NamedNode(`${indexUrl}#${cuid_1.default()}`); ins.push(rdf.st(rootNode, ns.rdf('type'), ns.solid('TypeRegistration'), rdf.sym(indexUrl))); ins.push(rdf.st(rootNode, ns.solid('instanceContainer'), rdf.sym(item.url), rdf.sym(indexUrl))); if (item.types.length === 0) { graph .statementsMatching(rootNode, ns.solid('forClass')) .forEach((st) => del.push(st)); } else { item.types.forEach((type) => { ins.push(rdf.st(rootNode, ns.solid('forClass'), rdf.sym(type), rdf.sym(indexUrl))); }); } } else { const typesFound = graph .each(rootNode, ns.solid('forClass'), null) .map((node) => node.value); const typesToDelete = typesFound.reduce((notFoundTypes, type) => !item.types.includes(type) ? Array.isArray(notFoundTypes) ? [...notFoundTypes, type] : [type] : notFoundTypes, []); const typesToAdd = item.types.reduce((notFoundTypes, type) => !typesFound.includes(type) ? [...notFoundTypes, type] : notFoundTypes, []); typesToDelete.forEach((type) => graph .statementsMatching(rootNode, ns.solid('forClass'), rdf.sym(type)) .forEach((st) => del.push(st))); typesToAdd.forEach((type) => ins.push(rdf.st(rootNode, ns.solid('forClass'), rdf.sym(type), rdf.sym(indexUrl)))); } } else if (item && item.types.includes(ns.ldp('Resource').value)) { let rootNode = graph.any(null, ns.solid('instance'), rdf.sym(item.url)); if (!rootNode) { rootNode = new rdf.NamedNode(`${indexUrl}#${cuid_1.default()}`); ins.push(rdf.st(rootNode, ns.rdf('type'), ns.solid('TypeRegistration'), rdf.sym(indexUrl))); ins.push(rdf.st(rootNode, ns.solid('instance'), rdf.sym(item.url), rdf.sym(indexUrl))); item.types.forEach((type) => { if (type) ins.push(rdf.st(rootNode, ns.solid('forClass'), rdf.sym(type), rdf.sym(indexUrl))); }); } else { const currentTypes = graph .each(rootNode, ns.solid('forClass'), null) .map((node) => node.value); currentTypes.map((type) => { if (!item.types.includes(type)) { graph .statementsMatching(rootNode, ns.solid('forClass'), rdf.sym(type)) .forEach((st) => { del.push(st); }); } }); item.types.forEach((type) => { if (!currentTypes.includes(type)) { ins.push(rdf.st(rootNode, ns.solid('forClass'), rdf.sym(type), rdf.sym(indexUrl))); } }); } } }); return { del, ins }; }; //# sourceMappingURL=indexing.js.map