Skip to content

Commit 237f98e

Browse files
committed
chore(docs): cloudflare backend
* switch DB * switch blob provider * remove vercel postgres * adapt template env (and chore cleanup)
1 parent 50e830e commit 237f98e

File tree

6 files changed

+312
-625
lines changed

6 files changed

+312
-625
lines changed

.env.example

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ DDOCS_ALGOLIA_KEY=
88
DTYPES_ALGOLIA_APP=
99
DTYPES_ALGOLIA_KEY=
1010

11-
DATABASE_URL=
11+
ORAMA_KEY=
12+
ORAMA_ID=
13+
ORAMA_CONTAINER=
14+
1215
DJS_DOCS_BEARER=
1316

1417
DISCORD_PUBKEY=
1518
DISCORD_CLIENT_ID=
1619
DISCORD_TOKEN=
1720
DISCORD_DEVGUILD_ID=
1821

19-
DJS_BLOB_STORAGE_BASE=
20-
POSTGRES_URL=
22+
CF_STORAGE_BASE=
23+
CF_ACCOUNT_ID=
24+
CF_D1_DOCS_ID=
25+
CF_D1_DOCS_API_KEY=
26+
27+
ENVIRONMENT=debug

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
"@discordjs/rest": "^2.3.0",
3434
"@hapi/boom": "^10.0.1",
3535
"@ltd/j-toml": "^1.38.0",
36-
"@vercel/postgres": "^0.9.0",
3736
"algoliasearch": "^4.19.1",
3837
"cheerio": "^1.0.0-rc.12",
38+
"cloudflare": "^4.2.0",
3939
"discord-api-types": "^0.37.83",
4040
"dotenv": "^16.3.1",
4141
"he": "^1.2.0",

src/functions/autocomplete/docsAutoComplete.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ApplicationCommandOptionType, InteractionResponseType } from 'discord-a
77
import type { Response } from 'polka';
88
import { AUTOCOMPLETE_MAX_ITEMS, AUTOCOMPLETE_MAX_NAME_LENGTH, DJS_QUERY_SEPARATOR } from '../../util/constants.js';
99
import { getCurrentMainPackageVersion, getDjsVersions } from '../../util/djsdocs.js';
10+
import { logger } from '../../util/logger.js';
1011
import { truncate } from '../../util/truncate.js';
1112

1213
/**
@@ -65,11 +66,9 @@ export const djsDocsDependencies = new Map<string, any>();
6566
*/
6667
export async function fetchDjsDependencies(version: string) {
6768
const hit = djsDocsDependencies.get(version);
68-
const dependencies =
69-
hit ??
70-
(await fetch(`${process.env.DJS_BLOB_STORAGE_BASE}/rewrite/discord.js/${version}.dependencies.api.json`).then(
71-
async (res) => res.json(),
72-
));
69+
const url = `${process.env.CF_STORAGE_BASE}/discord.js/${version}.dependencies.api.json`;
70+
logger.debug(`Requesting dependencies from CF: ${url}`);
71+
const dependencies = hit ?? (await fetch(url).then(async (res) => res.json()));
7372

7473
if (!hit) {
7574
djsDocsDependencies.set(version, dependencies);

src/functions/docs.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import { prepareErrorResponse, prepareResponse } from '../util/respond.js';
2727
import { truncate } from '../util/truncate.js';
2828

2929
/**
30-
* Vercel blob store format
30+
* Bucket format
3131
*
3232
* Format: path/pkg/item
3333
* Item: branch.itemName.itemKind.api.json
34-
* Example: https://bpwrdvqzqnllsihg.public.blob.vercel-storage.com/rewrite/discord.js/main.actionrow.class.api.json
34+
* Key Example: discord.js/main.actionrow.class.api.json
3535
*/
3636

3737
type CacheEntry = {
@@ -57,15 +57,15 @@ export async function fetchDocItem(
5757
itemKind: string,
5858
): Promise<any | null> {
5959
try {
60-
const key = `rewrite/${_package}/${version}.${itemName}.${itemKind}`;
60+
const key = `${_package}/${version}.${itemName}.${itemKind}`;
6161
const hit = docsCache.get(key);
6262

6363
if (hit) {
6464
return hit.value;
6565
}
6666

67-
const resourceLink = `${process.env.DJS_BLOB_STORAGE_BASE!}/${key}.api.json`;
68-
logger.debug(`Requesting documentation from vercel: ${resourceLink}`);
67+
const resourceLink = `${process.env.CF_STORAGE_BASE!}/${key}.api.json`;
68+
logger.debug(`Requesting documentation from CF: ${resourceLink}`);
6969
const value = await fetch(resourceLink).then(async (result) => result.json());
7070

7171
docsCache.set(key, {

src/util/djsdocs.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import process from 'node:process';
2-
import { sql } from '@vercel/postgres';
2+
import Cloudflare from 'cloudflare';
33
import { container } from 'tsyringe';
44
import { logger } from './logger.js';
55

@@ -35,11 +35,30 @@ export async function fetchDjsVersions(): Promise<DjsVersions> {
3535
}
3636

3737
try {
38-
const { rows } = await sql<DjsVersionEntry>`select version, name from documentation order by version desc`;
38+
const client = new Cloudflare({
39+
apiToken: process.env.CF_D1_DOCS_API_KEY,
40+
});
41+
42+
const page = await client.d1.database.query(process.env.CF_D1_DOCS_ID!, {
43+
account_id: process.env.CF_ACCOUNT_ID!,
44+
sql: `select version, name from documentation order by version desc;`,
45+
});
3946

4047
const packages = new Set<string>();
4148
const versions = new Map<string, string[]>();
49+
const res = page.result[0];
50+
51+
if (!res?.results) {
52+
logger.error('No results for version lookup');
53+
54+
return {
55+
rows: [],
56+
versions: new Map<string, string[]>(),
57+
packages: [],
58+
};
59+
}
4260

61+
const rows = res.results as DjsVersionEntry[];
4362
for (const row of rows) {
4463
packages.add(row.name);
4564
const currentVersions = versions.get(row.name);

0 commit comments

Comments
 (0)