Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1206 error responses occasionally being cached by cached utility (Revert Reversion) #1376

12 changes: 3 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"husky": "^9.0.11",
"jsdom": "^24.1.0",
"knip": "^5.34.1",
"mdsvex": "^0.11.2",
"mime-types": "^2.1.35",
"node-fetch": "^3.3.2",
"playwright": "^1.44.1",
Expand Down Expand Up @@ -103,6 +102,7 @@
"isomorphic-fetch": "^3.0.0",
"jimp": "^0.22.12",
"lodash": "^4.17.21",
"mdsvex": "^0.11.2",
"puppeteer": "^23.6.0",
"redis": "^4.6.14",
"sanitize-html": "^2.13.0",
Expand Down
2 changes: 1 addition & 1 deletion src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ PuppeteerManager.launch({
}),
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
})
});

export const handle = sequence(sentryHandle());
export const handleError = handleErrorWithSentry();
2 changes: 1 addition & 1 deletion src/lib/components/collect-button/collect-button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
loading ||
amountToShow?.fiatEstimateCents === 0 ||
amountToShow?.includesUnknownPrice === true;
const newWidth = shouldHide ? 0 : (amountElem?.getBoundingClientRect().width ?? 0);
const newWidth = shouldHide ? 0 : amountElem?.getBoundingClientRect().width ?? 0;

if (newWidth === 24) {
amountElemWidth.set(0);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/flows/top-up-flow/enter-amount.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
export let backButton: boolean;

$: tokenAddress = $context.tokenAddress;
$: tokenInfo = tokenAddress ? (tokens.getByAddress(tokenAddress) ?? unreachable()) : undefined;
$: tokenInfo = tokenAddress ? tokens.getByAddress(tokenAddress) ?? unreachable() : undefined;

let amount: bigint | undefined = undefined;

Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/base-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import getOptionalEnvVar from './get-optional-env-var/public';

const envBaseUrl = getOptionalEnvVar('PUBLIC_BASE_URL');

export const BASE_URL = browser ? window.location.origin : (envBaseUrl ?? 'http://localhost:5173');
export const BASE_URL = browser ? window.location.origin : envBaseUrl ?? 'http://localhost:5173';
46 changes: 46 additions & 0 deletions src/lib/utils/blog-posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { compile } from 'mdsvex';
import assert from '$lib/utils/assert';
import { metadataSchema } from '../../routes/api/blog/posts/schema';

export const getSlug = (path: string): string => {
const slug = path.split('/').pop()?.slice(0, -3);
assert(slug);

return slug;
};

export const fetchRawBlogPosts = async () => {
return Promise.all(
Object.entries(import.meta.glob('/src/blog-posts/*.md', { as: 'raw' })).map(
async ([path, resolver]) => {
const resolved = await resolver();
const compiled = await compile(resolved);

// Get and assert slug
const slug = getSlug(path);

// Get the frontmatter
const fm = (compiled?.data && 'fm' in compiled.data && compiled.data.fm) ?? {};

return { ...fm, slug };
},
),
);
};

export const fetchBlogPosts = async () => {
return Promise.all(
Object.entries(import.meta.glob('/src/blog-posts/*.md')).map(async ([path, resolver]) => {
const resolved = await resolver();

assert(typeof resolved === 'object' && resolved && 'metadata' in resolved);

const metadata = metadataSchema.parse(resolved.metadata);

// Get and assert slug
const slug = getSlug(path);

return { ...metadata, slug };
}),
);
};
6 changes: 4 additions & 2 deletions src/lib/utils/cmc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export const getCmcPrices = async (tokenAddresses: string[], fetch = window.fetc
}

try {
// get id map and ensure the response does not indicate an error
const idMapResponse = await fetch('/api/fiat-estimates/id-map');
// get response of known token address => token id
const idMapRes = await (await fetch('/api/fiat-estimates/id-map')).json();
const idMapJson = await idMapResponse.json();
// produce map of response
const tokenAddressToId = z.record(z.string(), z.number()).parse(idMapRes);
const tokenAddressToId = z.record(z.string(), z.number()).parse(idMapJson);
// create parameter for /api/fiat-estimates/price endpoint, removing unknown token ids
const tokenIdsString = tokenAddresses
.reduce((memo, address) => {
Expand Down
13 changes: 13 additions & 0 deletions src/lib/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { error } from '@sveltejs/kit';

export async function ensureResponseOk(responsePromise: Promise<Response>): Promise<Response> {
const intermediateResponse = await responsePromise;
if (!intermediateResponse.ok) {
const text = await intermediateResponse.text();
throw error(intermediateResponse.status, {
message: `Fetch response was not ok: ${intermediateResponse.url} ${text}`,
});
}

return responsePromise;
}
2 changes: 1 addition & 1 deletion src/lib/utils/get-connected-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default function getConnectedAddress() {

return isTest()
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
((window as any).playwrightAddress ?? '0x433220a86126eFe2b8C98a723E73eBAd2D0CbaDc')
(window as any).playwrightAddress ?? '0x433220a86126eFe2b8C98a723E73eBAd2D0CbaDc'
: get(walletStore).address;
}
22 changes: 10 additions & 12 deletions src/lib/utils/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import puppeteer, { type Browser, type PuppeteerNodeLaunchOptions } from 'puppet
*/
export class PuppeteerManager {
static browser: Browser | undefined;
static browserConfig: PuppeteerNodeLaunchOptions | undefined
static browserConfig: PuppeteerNodeLaunchOptions | undefined;

static #onBrowserExit = () => {
// eslint-disable-next-line no-console
console.warn('PuppeteerManager: browser exited')
console.warn('PuppeteerManager: browser exited');
// if the browser process exits for any reason, mark the browser as dead
// so that we can perform re-launch on the next call to this.launch
this.browser = undefined;
}
};

/**
* Launch a Puppeteer Browser with the specified configuration. Subsequent calls to
Expand All @@ -28,20 +28,18 @@ export class PuppeteerManager {
*/
static async launch(browserConfig?: PuppeteerNodeLaunchOptions): Promise<Browser> {
if (browserConfig && this.browser) {
await this.browser.close()
await this.browser.close();
}

if (!this.browser) {
this.browserConfig = this.browserConfig && !browserConfig ? this.browserConfig : browserConfig
this.browser = await puppeteer.launch(this.browserConfig)
this.browserConfig =
this.browserConfig && !browserConfig ? this.browserConfig : browserConfig;
this.browser = await puppeteer.launch(this.browserConfig);

const childProcess = this.browser.process()
childProcess?.on('exit', this.#onBrowserExit)
const childProcess = this.browser.process();
childProcess?.on('exit', this.#onBrowserExit);
}

return this.browser
return this.browser;
}
}



2 changes: 2 additions & 0 deletions src/lib/utils/total-dripped-approx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export const cachedTotalDrippedPrices = (
);

return cached(redis, TOTAL_DRIPPED_PRICES_CACHE_KEY, 60 * 60 * 6, async () => {
// if the underlying getCmcPrices function fails, {} is returned
// and will become cached
return totalDrippedPrices(fetch);
});
};
19 changes: 2 additions & 17 deletions src/routes/(pages)/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { cachedTotalDrippedPrices } from '$lib/utils/total-dripped-approx';
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
import { redis } from '../api/redis';
import { metadataSchema } from '../api/blog/posts/schema';
import assert from '$lib/utils/assert';
import type { DripListQuery, DripListQueryVariables } from './__generated__/gql.generated';
import query from '$lib/graphql/dripsQL';
import { DRIP_LIST_CARD_FRAGMENT } from '$lib/components/drip-list-card/drip-list-card.svelte';
import { gql } from 'graphql-request';
import { PUBLIC_NETWORK } from '$env/static/public';
import network from '$lib/stores/wallet/network';
import { fetchBlogPosts } from '$lib/utils/blog-posts';

const FEATURED_DRIP_LISTS =
{
Expand Down Expand Up @@ -72,21 +71,7 @@ export const load = (async ({ fetch, request }) => {
const [prices, featuredLists, blogPosts] = await Promise.all([
cachedTotalDrippedPrices(redis, fetch),
fetchFeaturedLists(),
Promise.all(
Object.entries(import.meta.glob('/src/blog-posts/*.md')).map(async ([path, resolver]) => {
const resolved = await resolver();

assert(typeof resolved === 'object' && resolved && 'metadata' in resolved);

const metadata = metadataSchema.parse(resolved.metadata);

const slug = path.split('/').pop()?.slice(0, -3);

assert(slug);

return { ...metadata, slug };
}),
),
fetchBlogPosts(),
]);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
SortDirection,
} from '$lib/graphql/__generated__/base-types';
import network from '$lib/stores/wallet/network';
import { fetchBlogPosts } from '../../../../../lib/utils/blog-posts';

const FEATURED_DRIP_LISTS =
{
Expand Down Expand Up @@ -80,31 +81,43 @@ export default async function loadDefaultExplorePageData(f: typeof fetch) {
f,
);

// eslint-disable-next-line no-console
console.log('fetchProjects', projectsRes);

return projectsRes.projects;
};

const fetchFeaturedLists = async () => {
const results = await Promise.all(
FEATURED_DRIP_LISTS.map((id) =>
query<FeaturedDripListQuery, FeaturedDripListQueryVariables>(
featuredDripListQuery,
{ id, chain: network.gqlName },
f,
),
),
FEATURED_DRIP_LISTS.map(async (id) => {
try {
const result = await query<FeaturedDripListQuery, FeaturedDripListQueryVariables>(
featuredDripListQuery,
{ id, chain: network.gqlName },
f,
);
return result;
} catch (error) {
// eslint-disable-next-line no-console
console.error('fetchFeaturedLists', error);
throw error;
}
}),
);

return results.map((res) => res.dripList);
};

const fetchBlogPosts = async () => {
return (await f('/api/blog/posts')).json();
};

const fetchTlv = async () => {
return (await f('/api/tlv')).json();
const response = await f('/api/tlv');
if (!response.ok) {
return null;
}

return response.json();
};

// TODO: fetch is failing here for some reason!!!
const [blogPosts, projects, featuredDripLists, totalDrippedPrices, tlv] = await cached(
redis,
cacheKey,
Expand Down
20 changes: 2 additions & 18 deletions src/routes/(pages)/blog/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
import network from '$lib/stores/wallet/network';
import assert from '$lib/utils/assert';
import { redirect } from '@sveltejs/kit';
import { metadataSchema } from '../../api/blog/posts/schema';
import { fetchBlogPosts } from '$lib/utils/blog-posts';

export const load = async ({ route }) => {
if (network.alternativeChainMode) {
// Serve from the `mainnet` instance
return redirect(308, `https://drips.network${route.id}`);
}

const posts = await Promise.all(
Object.entries(import.meta.glob('/src/blog-posts/*.md')).map(async ([path, resolver]) => {
const resolved = await resolver();

assert(typeof resolved === 'object' && resolved && 'metadata' in resolved);

const metadata = metadataSchema.parse(resolved.metadata);

const slug = path.split('/').pop()?.slice(0, -3);

assert(slug);

return { ...metadata, slug };
}),
);

const posts = await fetchBlogPosts();
const sortedPosts = posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());

return {
Expand Down
Loading
Loading