UNPKG

@snap/camera-kit

Version:
49 lines 2.91 kB
import { __awaiter } from "tslib"; import { catchError, firstValueFrom, map, mergeMap } from "rxjs"; import { isRecord, isString } from "../../common/typeguards"; import { Injectable } from "../../dependency-injection/Injectable"; import { createArrayBufferParsingHandler } from "../../handlers/arrayBufferParsingHandler"; import { defaultFetchHandlerFactory } from "../../handlers/defaultFetchHandler"; import { HandlerChainBuilder } from "../../handlers/HandlerChainBuilder"; import { remoteConfigurationFactory } from "../../remote-configuration/remoteConfiguration"; import { withRequestPriority } from "../../handlers/utils"; const hasStringValue = (value) => { return isRecord(value) && isString(value.stringValue); }; const isAssetConfig = (value) => { return isRecord(value) && isString(value.url) && (value.checksum === undefined || isString(value.checksum)); }; export const deviceDependentAssetLoaderFactory = Injectable("deviceDependentAssetLoader", [defaultFetchHandlerFactory.token, remoteConfigurationFactory.token], (fetchHandler, remoteConfiguration) => { const assetHandler = new HandlerChainBuilder(fetchHandler).map(createArrayBufferParsingHandler()).handler; return function deviceDependentAssetLoader({ assetDescriptor: { assetId }, lowPriority, }) { return __awaiter(this, void 0, void 0, function* () { const loadingFailed = (reason, cause) => new Error(`Cannot load device-dependent asset ${assetId}. ${reason}`, { cause }); return firstValueFrom(remoteConfiguration.get(assetId).pipe(catchError((error) => { throw loadingFailed("COF config failed to load.", error); }), map((configs) => { if (configs.length === 0) { throw loadingFailed(`No COF config found corresponding to that assetId.`); } const [{ value }] = configs; if (!hasStringValue(value)) throw loadingFailed("COF config malformed (missing stringValue)"); let assetConfig; try { assetConfig = JSON.parse(value.stringValue); } catch (parseError) { throw loadingFailed("COF config malformed (JSON parse error)", parseError); } if (!isAssetConfig(assetConfig)) throw loadingFailed("COF config malformed (missing URL)"); return assetConfig; }), mergeMap(({ url, checksum }) => __awaiter(this, void 0, void 0, function* () { const [data, response] = yield assetHandler(url, withRequestPriority({ cache: "force-cache" }, lowPriority)); if (!response.ok) throw response; return { data, checksum }; })))); }); }; }); //# sourceMappingURL=deviceDependentAssetLoader.js.map