UNPKG

realm-object-server

Version:

Realm Object Server

57 lines 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path = require("path"); function mapVirtToRealmPath(rootPath, virtPath) { if (virtPath === "" || !virtPath.startsWith("/")) { return { error: "path is empty or doesn't start with slash" }; } let result = rootPath; const [head, ...tail] = virtPath.split("/"); for (const segment of tail) { const { isValid, error } = validVirtPathSegment(segment); if (!isValid) { return { error: `encountered an invalid segment: '${segment}'. Error: ${error}` }; } result = path.resolve(result, segment); } return { path: result + ".realm", }; } exports.mapVirtToRealmPath = mapVirtToRealmPath; const badSuffixes = [".realm", ".realm.lock", ".realm.management"]; const alphanumRegex = /^[0-9a-zA-Z\-_\.]+$/; function validVirtPathSegment(segment) { if (segment === "") { return { isValid: false, error: "segment is empty", }; } if (segment.startsWith(".")) { return { isValid: false, error: "segment starts with dot (.)", }; } if (badSuffixes.some(s => segment.endsWith(s))) { return { isValid: false, error: `segment ends with one of ${badSuffixes.join(", ")}`, }; } if (!alphanumRegex.test(segment)) { return { isValid: false, error: "segment is not composed of alphanumeric characters", }; } return { isValid: true, }; } //# sourceMappingURL=virtPath.js.map