UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

43 lines 2.13 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import URI from "urijs"; export class LocalUploadSource { constructor(path, name = "") { this.ensureNonBrowserEnvironment(); this._uri = new URI(path); if (this._uri.protocol() !== "file") { throw new Error("Invalid protocol. Only paths using the file protocol are supported."); } this.name = name.length > 0 ? name : this._uri.filename(); } getReadableSourceAsync() { return __awaiter(this, void 0, void 0, function* () { this.ensureNonBrowserEnvironment(); const fs = require("fs"); const buffer = fs.readFileSync(this._uri.path()); return Promise.resolve(buffer); }); } // https://github.com/axios/axios/blob/16aa2ce7fa42e7c46407b78966b7521d8e588a72/lib/utils.js#L208-L218 isStandardBrowserEnv() { if (typeof navigator !== "undefined" && // eslint-disable-next-line @typescript-eslint/no-deprecated (navigator.product === "ReactNative" || navigator.product === "NativeScript" || navigator.product === "NS")) { return false; } return typeof window !== "undefined" && typeof document !== "undefined"; } ensureNonBrowserEnvironment() { if (this.isStandardBrowserEnv()) { throw Error("LocalUploadSource is not supported when running in a browser environment."); } } } //# sourceMappingURL=local-upload-source.js.map