Promise link = symlinkAsset(hash, path); link->mixin(result); link->ready(completed); } }); }} /*@jsdoc * Source for {@link Assets.queryCacheMeta}. * @typedef {object} Assets.QueryCacheMetaOptions * @property {string} url - The URL of the cached asset to get information on. Must start with <code>"atp:"</code> or * <code>"cache:"</code>. */void AssetScriptingInterface::queryCacheMeta(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) { QString url = options.isString() ? options.toString() : options.property("url").toString(); JS_VERIFY(QUrl(url).isValid(), QString("Invalid URL '%1'").arg(url)); jsPromiseReady(Parent::queryCacheMeta(url), scope, callback);} /*@jsdoc * Source and retrieval options for {@link Assets.loadFromCache}. * @typedef {object} Assets.LoadFromCacheOptions * @property {boolean} [decompress=false] - <code>true</code> to gunzip decompress the cached data. Synonym: * <code>compressed</code>. * @property {Assets.ResponseType} [responseType=text] - The desired result type. * @property {string} url - The URL of the asset to load from cache. Must start with <code>"atp:"</code> or * <code>"cache:"</code>. */void AssetScriptingInterface::loadFromCache(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) { QString url, responseType; bool decompress = false; if (options.isString()) { url = options.toString(); responseType = "text"; } else { url = options.property("url").toString(); responseType = options.property("responseType").isValid() ? options.property("responseType").toString() : "text"; decompress = options.property("decompress").toBool() || options.property("compressed").toBool(); } JS_VERIFY(QUrl(url).isValid(), QString("Invalid URL '%1'").arg(url)); JS_VERIFY(RESPONSE_TYPES.contains(responseType), QString("Invalid responseType: '%1' (expected: %2)").arg(responseType).arg(RESPONSE_TYPES.join(" | "))); jsPromiseReady(Parent::loadFromCache(url, decompress, responseType), scope, callback);} bool AssetScriptingInterface::canWriteCacheValue(const QUrl& url) { Q_ASSERT(engine); auto scriptManager = engine()->manager(); if (!scriptManager) { return false; } // allow cache writes only from Client, EntityServer and Agent scripts bool isAllowedContext = ( scriptManager->isClientScript() || scriptManager->isAgentScript() ); if (!isAllowedContext) { return false; } return true;} /*@jsdoc * The data to save to the cache and cache options for {@link Assets.saveToCache}. * @typedef {object} Assets.SaveToCacheOptions * @property {string|ArrayBuffer} data - The data to save to the cache. * @property {Assets.SaveToCacheHeaders} [headers] - The last-modified and expiry times for the cache item. * @property {string} [url] - The URL to associate with the cache item. Must start with <code>"atp:"</code> or * <code>"cache:"</code>. If not specified, the URL is <code>"atp:"</code> followed by the SHA256 hash of the content. */void AssetScriptingInterface::saveToCache(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) { JS_VERIFY(options.isObject(), QString("expected options object as first parameter not: %1").arg(options.toVariant().typeName())); QString url = options.property("url").toString(); QByteArray data = scriptvalue_cast<QByteArray>(options.property("data")); QVariantMap headers = scriptvalue_cast<QVariantMap>(options.property("headers")); saveToCache(url, data, headers, scope, callback);} void AssetScriptingInterface::saveToCache(const QUrl& rawURL, const QByteArray& data, const QVariantMap& metadata, const ScriptValue& scope, const ScriptValue& callback) { QUrl url = rawURL; if (url.path().isEmpty() && !data.isEmpty()) { // generate a valid ATP URL from the data -- appending any existing fragment or querystring values auto atpURL = AssetUtils::getATPUrl(hashDataHex(data)); atpURL.setQuery(url.query()); atpURL.setFragment(url.fragment()); url = atpURL; } auto hash = AssetUtils::extractAssetHash(url.toDisplayString()); JS_VERIFY(url.isValid(), QString("Invalid URL '%1'").arg(url.toString())); JS_VERIFY(canWriteCacheValue(url), "Invalid cache write URL: " + url.toString()); JS_VERIFY(url.scheme() == "atp" || url.scheme() == "cache", "only 'atp' and 'cache' URL schemes supported"); JS_VERIFY(hash.isEmpty() || hash == hashDataHex(data), QString("invalid checksum hash for atp:HASH style URL (%1 != %2)").arg(hash, hashDataHex(data))); jsPromiseReady(Parent::saveToCache(url, data, metadata), scope, callback);}Lines 501–605 of 605. Previous lines
Promise link = symlinkAsset(hash, path); link->mixin(result); link->ready(completed); } }); } } /*@jsdoc * Source for {@link Assets.queryCacheMeta}. * @typedef {object} Assets.QueryCacheMetaOptions * @property {string} url - The URL of the cached asset to get information on. Must start with <code>"atp:"</code> or * <code>"cache:"</code>. */ void AssetScriptingInterface::queryCacheMeta(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) { QString url = options.isString() ? options.toString() : options.property("url").toString(); JS_VERIFY(QUrl(url).isValid(), QString("Invalid URL '%1'").arg(url)); jsPromiseReady(Parent::queryCacheMeta(url), scope, callback); } /*@jsdoc * Source and retrieval options for {@link Assets.loadFromCache}. * @typedef {object} Assets.LoadFromCacheOptions * @property {boolean} [decompress=false] - <code>true</code> to gunzip decompress the cached data. Synonym: * <code>compressed</code>. * @property {Assets.ResponseType} [responseType=text] - The desired result type. * @property {string} url - The URL of the asset to load from cache. Must start with <code>"atp:"</code> or * <code>"cache:"</code>. */ void AssetScriptingInterface::loadFromCache(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) { QString url, responseType; bool decompress = false; if (options.isString()) { url = options.toString(); responseType = "text"; } else { url = options.property("url").toString(); responseType = options.property("responseType").isValid() ? options.property("responseType").toString() : "text"; decompress = options.property("decompress").toBool() || options.property("compressed").toBool(); } JS_VERIFY(QUrl(url).isValid(), QString("Invalid URL '%1'").arg(url)); JS_VERIFY(RESPONSE_TYPES.contains(responseType), QString("Invalid responseType: '%1' (expected: %2)").arg(responseType).arg(RESPONSE_TYPES.join(" | "))); jsPromiseReady(Parent::loadFromCache(url, decompress, responseType), scope, callback); } bool AssetScriptingInterface::canWriteCacheValue(const QUrl& url) { Q_ASSERT(engine); auto scriptManager = engine()->manager(); if (!scriptManager) { return false; } // allow cache writes only from Client, EntityServer and Agent scripts bool isAllowedContext = ( scriptManager->isClientScript() || scriptManager->isAgentScript() ); if (!isAllowedContext) { return false; } return true; } /*@jsdoc * The data to save to the cache and cache options for {@link Assets.saveToCache}. * @typedef {object} Assets.SaveToCacheOptions * @property {string|ArrayBuffer} data - The data to save to the cache. * @property {Assets.SaveToCacheHeaders} [headers] - The last-modified and expiry times for the cache item. * @property {string} [url] - The URL to associate with the cache item. Must start with <code>"atp:"</code> or * <code>"cache:"</code>. If not specified, the URL is <code>"atp:"</code> followed by the SHA256 hash of the content. */ void AssetScriptingInterface::saveToCache(const ScriptValue& options, const ScriptValue& scope, const ScriptValue& callback) { JS_VERIFY(options.isObject(), QString("expected options object as first parameter not: %1").arg(options.toVariant().typeName())); QString url = options.property("url").toString(); QByteArray data = scriptvalue_cast<QByteArray>(options.property("data")); QVariantMap headers = scriptvalue_cast<QVariantMap>(options.property("headers")); saveToCache(url, data, headers, scope, callback); } void AssetScriptingInterface::saveToCache(const QUrl& rawURL, const QByteArray& data, const QVariantMap& metadata, const ScriptValue& scope, const ScriptValue& callback) { QUrl url = rawURL; if (url.path().isEmpty() && !data.isEmpty()) { // generate a valid ATP URL from the data -- appending any existing fragment or querystring values auto atpURL = AssetUtils::getATPUrl(hashDataHex(data)); atpURL.setQuery(url.query()); atpURL.setFragment(url.fragment()); url = atpURL; } auto hash = AssetUtils::extractAssetHash(url.toDisplayString()); JS_VERIFY(url.isValid(), QString("Invalid URL '%1'").arg(url.toString())); JS_VERIFY(canWriteCacheValue(url), "Invalid cache write URL: " + url.toString()); JS_VERIFY(url.scheme() == "atp" || url.scheme() == "cache", "only 'atp' and 'cache' URL schemes supported"); JS_VERIFY(hash.isEmpty() || hash == hashDataHex(data), QString("invalid checksum hash for atp:HASH style URL (%1 != %2)").arg(hash, hashDataHex(data))); jsPromiseReady(Parent::saveToCache(url, data, metadata), scope, callback); }