Skip to content

Commit

Permalink
fix(app): filter out shared worker requests from the service worker G…
Browse files Browse the repository at this point in the history
…ET API requests cache
  • Loading branch information
Andrejs authored and Andrejs committed May 31, 2024
1 parent 9a580a1 commit 82d39e2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
38 changes: 20 additions & 18 deletions src/services/QueueManager/QueueManager.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
import {
BehaviorSubject,
map,
timeout,
throwError,
of,
catchError,
EMPTY,
Observable,
mergeMap,
catchError,
combineLatest,
debounceTime,
merge,
tap,
interval,
filter,
combineLatest,
withLatestFrom,
interval,
map,
merge,
mergeMap,
of,
share,
throwError,
timeout,
withLatestFrom,
} from 'rxjs';

import * as R from 'ramda';

import { fetchIpfsContent } from 'src/services/ipfs/utils/utils-ipfs';
import { CybIpfsNode, IpfsContentSource } from 'src/services/ipfs/types';
import { fetchIpfsContent } from 'src/services/ipfs/utils/utils-ipfs';
import { ParticleCid } from 'src/types/base';

import { promiseToObservable } from '../../utils/rxjs/helpers';

import type {
QueueItem,
QueueItemResult,
QueueItemAsyncResult,
QueueItemCallback,
QueueItemOptions,
QueueStats,
QueueItemResult,
QueueSource,
QueueItemAsyncResult,
} from './types';

import { QueueStrategy } from './QueueStrategy';

import { QueueItemTimeoutError } from './QueueItemTimeoutError';
import { enqueueParticleSave } from '../backend/channels/BackendQueueChannel/backendQueueSenders';
import BroadcastChannelSender from '../backend/channels/BroadcastChannelSender';
import { RuneEngine } from '../scripting/engine';
import { postProcessIpfContent } from '../scripting/services/postProcessing';
import { enqueueParticleSave } from '../backend/channels/BackendQueueChannel/backendQueueSenders';
import { QueueItemTimeoutError } from './QueueItemTimeoutError';
import { CustomHeaders, XCybSourceValues } from './constants';

const QUEUE_DEBOUNCE_MS = 33;
const CONNECTION_KEEPER_RETRY_MS = 5000;
Expand Down Expand Up @@ -75,7 +74,7 @@ const strategies = {
helia: new QueueStrategy(
{
db: { timeout: 5000, maxConcurrentExecutions: 999 },
node: { timeout: 6 * 1000, maxConcurrentExecutions: 50 }, //TODO: set to 60
node: { timeout: 6 * 1000, maxConcurrentExecutions: 50 }, // TODO: set to 60
gateway: { timeout: 3 * 1000, maxConcurrentExecutions: 11 },
},
['db', 'node', 'gateway']
Expand Down Expand Up @@ -175,6 +174,9 @@ class QueueManager {
return fetchIpfsContent(cid, source, {
controller,
node: this.node,
headers: {
[CustomHeaders.XCybSource]: XCybSourceValues.sharedWorker,
},
}).then(async (content) => {
const result = content
? await postProcessIpfContent(item, content, this.rune!, this)
Expand Down
8 changes: 8 additions & 0 deletions src/services/QueueManager/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable import/prefer-default-export */
export const CustomHeaders = {
XCybSource: 'X-Cyb-Source',
};

export enum XCybSourceValues {
sharedWorker = 'shared-worker',
}
11 changes: 7 additions & 4 deletions src/services/ipfs/utils/utils-ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const fetchIPFSContentFromNode = async (
}
default: {
// Get sample of content
const { value: firstChunk, done } = await node
const { value: firstChunk } = await node
.cat(cid, { signal, length: 2048, offset: 0 })
[Symbol.asyncIterator]()
.next();
Expand Down Expand Up @@ -162,7 +162,8 @@ const fetchIPFSContentFromNode = async (
const fetchIPFSContentFromGateway = async (
cid: string,
node?: IpfsNode,
controller?: AbortController
controller?: AbortController,
headers?: Record<string, string>
): Promise<IPFSContentMaybe> => {
// fetch META only from external node(toooo slow), TODO: fetch meta from cybernode
const isExternalNode = node?.nodeType === 'external';
Expand All @@ -178,6 +179,7 @@ const fetchIPFSContentFromGateway = async (
const response = await fetch(contentUrl, {
method: 'GET',
signal: controller?.signal,
headers,
});
if (response && response.body) {
// fetch doesn't provide any headers in our case :(
Expand Down Expand Up @@ -221,14 +223,15 @@ const fetchIPFSContentFromGateway = async (
type fetchContentOptions = {
controller?: AbortController;
node?: IpfsNode;
headers?: Record<string, string>;
};

async function fetchIpfsContent(
cid: string,
source: IpfsContentSource,
options: fetchContentOptions
): Promise<IPFSContentMaybe> {
const { node, controller } = options;
const { node, controller, headers } = options;

try {
switch (source) {
Expand All @@ -237,7 +240,7 @@ async function fetchIpfsContent(
case 'node':
return fetchIPFSContentFromNode(cid, node, controller);
case 'gateway':
return fetchIPFSContentFromGateway(cid, node, controller);
return fetchIPFSContentFromGateway(cid, node, controller, headers);
default:
return undefined;
}
Expand Down
3 changes: 3 additions & 0 deletions src/services/service-worker/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { matchPrecache, precacheAndRoute } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { CacheFirst, NetworkFirst } from 'workbox-strategies';
import { ExpirationPlugin } from 'workbox-expiration';
import { CustomHeaders, XCybSourceValues } from '../QueueManager/constants';

declare const self: ServiceWorkerGlobalScope;

Expand Down Expand Up @@ -68,6 +69,8 @@ registerRoute(
({ request }) =>
request.method === 'GET' &&
request.destination !== 'document' &&
request.headers.get(CustomHeaders.XCybSource) !==
XCybSourceValues.sharedWorker &&
!(
request.destination === 'image' ||
request.destination === 'style' ||
Expand Down

0 comments on commit 82d39e2

Please sign in to comment.