Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private _onMessage = async ({ data }: MessageEvent): Promise => {
if (!isPlainObject(data)) return;
const { context, method, result, type } = data as MessageResponsePayload;
if (type !== GRAPHQL_BOX || !isPlainObject(result)) return;
const { _cacheMetadata, ...otherProps } = result;
const response: MaybeRequestResult = { ...otherProps };
if (_cacheMetadata) response._cacheMetadata = rehydrateCacheMetadata(_cacheMetadata);
if (method === REQUEST) {
const pending = this._pending.get(context.boxID);
if (!pending) return;
pending.resolve(response);
} else if (method === SUBSCRIBE) {
this._eventEmitter.emit(context.boxID, response);
}
};
private _createCacheMetadata(
{ _cacheMetadata, headers }: RawResponseDataWithMaybeCacheMetadata,
{ operation }: RequestContext,
): CacheMetadata {
const cacheMetadata = new Map();
const cacheControl = (headers && headers.get(HEADER_CACHE_CONTROL)) || this._fallbackOperationCacheability;
const cacheability = new Cacheability({ cacheControl });
cacheMetadata.set(operation, cacheability);
if (_cacheMetadata) rehydrateCacheMetadata(_cacheMetadata, cacheMetadata);
return cacheMetadata;
}
public async checkQueryResponseCacheEntry(
hash: string,
options: RequestOptions,
context: RequestContext,
): Promise {
const result = await this._checkCacheEntry(QUERY_RESPONSES, hash, options, context);
if (!result) return false;
const { cacheMetadata, data } = result.entry as QueryResponseCacheEntry;
return {
cacheMetadata: rehydrateCacheMetadata(cacheMetadata),
data,
};
}