mirror of
https://github.com/withastro/astro.git
synced 2025-01-20 22:12:38 -05:00
fix(db): Add a safe db fetch wrapper (#10420)
* fix(db): Add a safe db fetch wrapper * chore: changeset
This commit is contained in:
parent
001f7374d8
commit
2db25c05a4
7 changed files with 188 additions and 131 deletions
5
.changeset/wild-suits-remain.md
Normal file
5
.changeset/wild-suits-remain.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"@astrojs/db": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes some situations where failing requests would not error properly
|
|
@ -7,7 +7,7 @@ import ora from 'ora';
|
||||||
import prompts from 'prompts';
|
import prompts from 'prompts';
|
||||||
import { MISSING_SESSION_ID_ERROR } from '../../../errors.js';
|
import { MISSING_SESSION_ID_ERROR } from '../../../errors.js';
|
||||||
import { PROJECT_ID_FILE, getSessionIdFromFile } from '../../../tokens.js';
|
import { PROJECT_ID_FILE, getSessionIdFromFile } from '../../../tokens.js';
|
||||||
import { getAstroStudioUrl } from '../../../utils.js';
|
import { type Result, getAstroStudioUrl, safeFetch } from '../../../utils.js';
|
||||||
|
|
||||||
export async function cmd() {
|
export async function cmd() {
|
||||||
const sessionToken = await getSessionIdFromFile();
|
const sessionToken = await getSessionIdFromFile();
|
||||||
|
@ -51,16 +51,18 @@ async function linkProject(id: string) {
|
||||||
|
|
||||||
async function getWorkspaceId(): Promise<string> {
|
async function getWorkspaceId(): Promise<string> {
|
||||||
const linkUrl = new URL(getAstroStudioUrl() + '/api/cli/workspaces.list');
|
const linkUrl = new URL(getAstroStudioUrl() + '/api/cli/workspaces.list');
|
||||||
const response = await fetch(linkUrl, {
|
const response = await safeFetch(
|
||||||
|
linkUrl,
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${await getSessionIdFromFile()}`,
|
Authorization: `Bearer ${await getSessionIdFromFile()}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
});
|
},
|
||||||
if (!response.ok) {
|
(res) => {
|
||||||
// Unauthorized
|
// Unauthorized
|
||||||
if (response.status === 401) {
|
if (res.status === 401) {
|
||||||
console.error(
|
console.error(
|
||||||
`${bgRed('Unauthorized')}\n\n Are you logged in?\n Run ${cyan(
|
`${bgRed('Unauthorized')}\n\n Are you logged in?\n Run ${cyan(
|
||||||
'astro db login'
|
'astro db login'
|
||||||
|
@ -68,12 +70,12 @@ async function getWorkspaceId(): Promise<string> {
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
console.error(`Failed to fetch user workspace: ${response.status} ${response.statusText}`);
|
console.error(`Failed to fetch user workspace: ${res.status} ${res.statusText}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
const { data, success } = (await response.json()) as
|
);
|
||||||
| { success: false; data: unknown }
|
|
||||||
| { success: true; data: { id: string }[] };
|
const { data, success } = (await response.json()) as Result<{ id: string }[]>;
|
||||||
if (!success) {
|
if (!success) {
|
||||||
console.error(`Failed to fetch user's workspace.`);
|
console.error(`Failed to fetch user's workspace.`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
@ -91,17 +93,19 @@ export async function createNewProject({
|
||||||
region: string;
|
region: string;
|
||||||
}) {
|
}) {
|
||||||
const linkUrl = new URL(getAstroStudioUrl() + '/api/cli/projects.create');
|
const linkUrl = new URL(getAstroStudioUrl() + '/api/cli/projects.create');
|
||||||
const response = await fetch(linkUrl, {
|
const response = await safeFetch(
|
||||||
|
linkUrl,
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${await getSessionIdFromFile()}`,
|
Authorization: `Bearer ${await getSessionIdFromFile()}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ workspaceId, name, region }),
|
body: JSON.stringify({ workspaceId, name, region }),
|
||||||
});
|
},
|
||||||
if (!response.ok) {
|
(res) => {
|
||||||
// Unauthorized
|
// Unauthorized
|
||||||
if (response.status === 401) {
|
if (res.status === 401) {
|
||||||
console.error(
|
console.error(
|
||||||
`${bgRed('Unauthorized')}\n\n Are you logged in?\n Run ${cyan(
|
`${bgRed('Unauthorized')}\n\n Are you logged in?\n Run ${cyan(
|
||||||
'astro db login'
|
'astro db login'
|
||||||
|
@ -109,12 +113,12 @@ export async function createNewProject({
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
console.error(`Failed to create project: ${response.status} ${response.statusText}`);
|
console.error(`Failed to create project: ${res.status} ${res.statusText}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
const { data, success } = (await response.json()) as
|
);
|
||||||
| { success: false; data: unknown }
|
|
||||||
| { success: true; data: { id: string; idName: string } };
|
const { data, success } = (await response.json()) as Result<{ id: string; idName: string }>;
|
||||||
if (!success) {
|
if (!success) {
|
||||||
console.error(`Failed to create project.`);
|
console.error(`Failed to create project.`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
@ -124,17 +128,18 @@ export async function createNewProject({
|
||||||
|
|
||||||
export async function promptExistingProjectName({ workspaceId }: { workspaceId: string }) {
|
export async function promptExistingProjectName({ workspaceId }: { workspaceId: string }) {
|
||||||
const linkUrl = new URL(getAstroStudioUrl() + '/api/cli/projects.list');
|
const linkUrl = new URL(getAstroStudioUrl() + '/api/cli/projects.list');
|
||||||
const response = await fetch(linkUrl, {
|
const response = await safeFetch(
|
||||||
|
linkUrl,
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${await getSessionIdFromFile()}`,
|
Authorization: `Bearer ${await getSessionIdFromFile()}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ workspaceId }),
|
body: JSON.stringify({ workspaceId }),
|
||||||
});
|
},
|
||||||
if (!response.ok) {
|
(res) => {
|
||||||
// Unauthorized
|
if (res.status === 401) {
|
||||||
if (response.status === 401) {
|
|
||||||
console.error(
|
console.error(
|
||||||
`${bgRed('Unauthorized')}\n\n Are you logged in?\n Run ${cyan(
|
`${bgRed('Unauthorized')}\n\n Are you logged in?\n Run ${cyan(
|
||||||
'astro db login'
|
'astro db login'
|
||||||
|
@ -142,12 +147,12 @@ export async function promptExistingProjectName({ workspaceId }: { workspaceId:
|
||||||
);
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
console.error(`Failed to fetch projects: ${response.status} ${response.statusText}`);
|
console.error(`Failed to fetch projects: ${res.status} ${res.statusText}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
const { data, success } = (await response.json()) as
|
);
|
||||||
| { success: false; data: unknown }
|
|
||||||
| { success: true; data: { id: string; idName: string }[] };
|
const { data, success } = (await response.json()) as Result<{ id: string; idName: string }[]>;
|
||||||
if (!success) {
|
if (!success) {
|
||||||
console.error(`Failed to fetch projects.`);
|
console.error(`Failed to fetch projects.`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|
|
@ -3,7 +3,7 @@ import type { Arguments } from 'yargs-parser';
|
||||||
import { MIGRATION_VERSION } from '../../../consts.js';
|
import { MIGRATION_VERSION } from '../../../consts.js';
|
||||||
import { getManagedAppTokenOrExit } from '../../../tokens.js';
|
import { getManagedAppTokenOrExit } from '../../../tokens.js';
|
||||||
import { type DBConfig, type DBSnapshot } from '../../../types.js';
|
import { type DBConfig, type DBSnapshot } from '../../../types.js';
|
||||||
import { getRemoteDatabaseUrl } from '../../../utils.js';
|
import { type Result, getRemoteDatabaseUrl, safeFetch } from '../../../utils.js';
|
||||||
import {
|
import {
|
||||||
createCurrentSnapshot,
|
createCurrentSnapshot,
|
||||||
createEmptySnapshot,
|
createEmptySnapshot,
|
||||||
|
@ -82,19 +82,23 @@ async function pushSchema({
|
||||||
return new Response(null, { status: 200 });
|
return new Response(null, { status: 200 });
|
||||||
}
|
}
|
||||||
const url = new URL('/db/push', getRemoteDatabaseUrl());
|
const url = new URL('/db/push', getRemoteDatabaseUrl());
|
||||||
const response = await fetch(url, {
|
const response = await safeFetch(
|
||||||
|
url,
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
Authorization: `Bearer ${appToken}`,
|
Authorization: `Bearer ${appToken}`,
|
||||||
}),
|
}),
|
||||||
body: JSON.stringify(requestBody),
|
body: JSON.stringify(requestBody),
|
||||||
});
|
},
|
||||||
if (!response.ok) {
|
async (res) => {
|
||||||
console.error(`${url.toString()} failed: ${response.status} ${response.statusText}`);
|
console.error(`${url.toString()} failed: ${res.status} ${res.statusText}`);
|
||||||
console.error(await response.text());
|
console.error(await res.text());
|
||||||
throw new Error(`/db/push fetch failed: ${response.status} ${response.statusText}`);
|
throw new Error(`/db/push fetch failed: ${res.status} ${res.statusText}`);
|
||||||
}
|
}
|
||||||
const result = (await response.json()) as { success: false } | { success: true };
|
);
|
||||||
|
|
||||||
|
const result = (await response.json()) as Result<never>;
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
console.error(`${url.toString()} unsuccessful`);
|
console.error(`${url.toString()} unsuccessful`);
|
||||||
console.error(await response.text());
|
console.error(await response.text());
|
||||||
|
|
|
@ -32,7 +32,7 @@ import {
|
||||||
type NumberColumn,
|
type NumberColumn,
|
||||||
type TextColumn,
|
type TextColumn,
|
||||||
} from '../types.js';
|
} from '../types.js';
|
||||||
import { getRemoteDatabaseUrl } from '../utils.js';
|
import { type Result, getRemoteDatabaseUrl, safeFetch } from '../utils.js';
|
||||||
|
|
||||||
const sqlite = new SQLiteAsyncDialect();
|
const sqlite = new SQLiteAsyncDialect();
|
||||||
const genTempTableName = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10);
|
const genTempTableName = customAlphabet('abcdefghijklmnopqrstuvwxyz', 10);
|
||||||
|
@ -425,20 +425,22 @@ export async function getProductionCurrentSnapshot({
|
||||||
}): Promise<DBSnapshot | undefined> {
|
}): Promise<DBSnapshot | undefined> {
|
||||||
const url = new URL('/db/schema', getRemoteDatabaseUrl());
|
const url = new URL('/db/schema', getRemoteDatabaseUrl());
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const response = await safeFetch(
|
||||||
|
url,
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
Authorization: `Bearer ${appToken}`,
|
Authorization: `Bearer ${appToken}`,
|
||||||
}),
|
}),
|
||||||
});
|
},
|
||||||
if (!response.ok) {
|
async (res) => {
|
||||||
console.error(`${url.toString()} failed: ${response.status} ${response.statusText}`);
|
console.error(`${url.toString()} failed: ${res.status} ${res.statusText}`);
|
||||||
console.error(await response.text());
|
console.error(await res.text());
|
||||||
throw new Error(`/db/schema fetch failed: ${response.status} ${response.statusText}`);
|
throw new Error(`/db/schema fetch failed: ${res.status} ${res.statusText}`);
|
||||||
}
|
}
|
||||||
const result = (await response.json()) as
|
);
|
||||||
| { success: false; data: undefined }
|
|
||||||
| { success: true; data: DBSnapshot };
|
const result = (await response.json()) as Result<DBSnapshot>;
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
console.error(`${url.toString()} unsuccessful`);
|
console.error(`${url.toString()} unsuccessful`);
|
||||||
console.error(await response.text());
|
console.error(await response.text());
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { homedir } from 'node:os';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
import { pathToFileURL } from 'node:url';
|
import { pathToFileURL } from 'node:url';
|
||||||
import { MISSING_PROJECT_ID_ERROR, MISSING_SESSION_ID_ERROR } from './errors.js';
|
import { MISSING_PROJECT_ID_ERROR, MISSING_SESSION_ID_ERROR } from './errors.js';
|
||||||
import { getAstroStudioEnv, getAstroStudioUrl } from './utils.js';
|
import { getAstroStudioEnv, getAstroStudioUrl, safeFetch } from './utils.js';
|
||||||
|
|
||||||
export const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), '.astro', 'session-token'));
|
export const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), '.astro', 'session-token'));
|
||||||
export const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), '.astro', 'link'));
|
export const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), '.astro', 'link'));
|
||||||
|
@ -31,13 +31,20 @@ class ManagedRemoteAppToken implements ManagedAppToken {
|
||||||
renewTimer: NodeJS.Timeout | undefined;
|
renewTimer: NodeJS.Timeout | undefined;
|
||||||
|
|
||||||
static async create(sessionToken: string, projectId: string) {
|
static async create(sessionToken: string, projectId: string) {
|
||||||
const response = await fetch(new URL(`${getAstroStudioUrl()}/auth/cli/token-create`), {
|
const response = await safeFetch(
|
||||||
|
new URL(`${getAstroStudioUrl()}/auth/cli/token-create`),
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
Authorization: `Bearer ${sessionToken}`,
|
Authorization: `Bearer ${sessionToken}`,
|
||||||
}),
|
}),
|
||||||
body: JSON.stringify({ projectId }),
|
body: JSON.stringify({ projectId }),
|
||||||
});
|
},
|
||||||
|
(res) => {
|
||||||
|
throw new Error(`Failed to create token: ${res.status} ${res.statusText}`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const { token: shortLivedAppToken, ttl } = await response.json();
|
const { token: shortLivedAppToken, ttl } = await response.json();
|
||||||
return new ManagedRemoteAppToken({
|
return new ManagedRemoteAppToken({
|
||||||
token: shortLivedAppToken,
|
token: shortLivedAppToken,
|
||||||
|
@ -56,14 +63,20 @@ class ManagedRemoteAppToken implements ManagedAppToken {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async fetch(url: string, body: unknown) {
|
private async fetch(url: string, body: unknown) {
|
||||||
return fetch(`${getAstroStudioUrl()}${url}`, {
|
return safeFetch(
|
||||||
|
`${getAstroStudioUrl()}${url}`,
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${this.session}`,
|
Authorization: `Bearer ${this.session}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
});
|
},
|
||||||
|
() => {
|
||||||
|
throw new Error(`Failed to fetch ${url}.`);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async renew() {
|
async renew() {
|
||||||
|
|
|
@ -26,3 +26,24 @@ export function getDbDirectoryUrl(root: URL | string) {
|
||||||
export function defineDbIntegration(integration: AstroDbIntegration): AstroIntegration {
|
export function defineDbIntegration(integration: AstroDbIntegration): AstroIntegration {
|
||||||
return integration;
|
return integration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback.
|
||||||
|
*/
|
||||||
|
export async function safeFetch(
|
||||||
|
url: Parameters<typeof fetch>[0],
|
||||||
|
options: Parameters<typeof fetch>[1] = {},
|
||||||
|
onNotOK: (response: Response) => void | Promise<void> = () => {
|
||||||
|
throw new Error(`Request to ${url} returned a non-OK status code.`);
|
||||||
|
}
|
||||||
|
): Promise<Response> {
|
||||||
|
const response = await fetch(url, options);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
await onNotOK(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Result<T> = { success: true; data: T } | { success: false; data: unknown };
|
||||||
|
|
|
@ -4,6 +4,7 @@ import type { LibSQLDatabase } from 'drizzle-orm/libsql';
|
||||||
import { drizzle as drizzleLibsql } from 'drizzle-orm/libsql';
|
import { drizzle as drizzleLibsql } from 'drizzle-orm/libsql';
|
||||||
import { drizzle as drizzleProxy } from 'drizzle-orm/sqlite-proxy';
|
import { drizzle as drizzleProxy } from 'drizzle-orm/sqlite-proxy';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
import { safeFetch } from '../core/utils.js';
|
||||||
|
|
||||||
const isWebContainer = !!process.versions?.webcontainer;
|
const isWebContainer = !!process.versions?.webcontainer;
|
||||||
|
|
||||||
|
@ -29,19 +30,22 @@ export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string
|
||||||
const db = drizzleProxy(
|
const db = drizzleProxy(
|
||||||
async (sql, parameters, method) => {
|
async (sql, parameters, method) => {
|
||||||
const requestBody: InStatement = { sql, args: parameters };
|
const requestBody: InStatement = { sql, args: parameters };
|
||||||
const res = await fetch(url, {
|
const res = await safeFetch(
|
||||||
|
url,
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${appToken}`,
|
Authorization: `Bearer ${appToken}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify(requestBody),
|
body: JSON.stringify(requestBody),
|
||||||
});
|
},
|
||||||
if (!res.ok) {
|
(response) => {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Failed to execute query.\nQuery: ${sql}\nFull error: ${res.status} ${await res.text()}}`
|
`Failed to execute query.\nQuery: ${sql}\nFull error: ${response.status} ${response.statusText}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
let remoteResult: z.infer<typeof remoteResultSchema>;
|
let remoteResult: z.infer<typeof remoteResultSchema>;
|
||||||
try {
|
try {
|
||||||
|
@ -74,19 +78,22 @@ export function createRemoteDatabaseClient(appToken: string, remoteDbURL: string
|
||||||
},
|
},
|
||||||
async (queries) => {
|
async (queries) => {
|
||||||
const stmts: InStatement[] = queries.map(({ sql, params }) => ({ sql, args: params }));
|
const stmts: InStatement[] = queries.map(({ sql, params }) => ({ sql, args: params }));
|
||||||
const res = await fetch(url, {
|
const res = await safeFetch(
|
||||||
|
url,
|
||||||
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${appToken}`,
|
Authorization: `Bearer ${appToken}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify(stmts),
|
body: JSON.stringify(stmts),
|
||||||
});
|
},
|
||||||
if (!res.ok) {
|
(response) => {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Failed to execute batch queries.\nFull error: ${res.status} ${await res.text()}}`
|
`Failed to execute batch queries.\nFull error: ${response.status} ${response.statusText}}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
let remoteResults: z.infer<typeof remoteResultSchema>[];
|
let remoteResults: z.infer<typeof remoteResultSchema>[];
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Add table
Reference in a new issue