refactor(server): rate limit and permission (#4198)

Co-authored-by: LongYinan <lynweklm@gmail.com>
This commit is contained in:
DarkSky
2023-09-07 14:32:41 -07:00
committed by GitHub
co-authored by LongYinan
parent 4c4bb65be8
commit f4340da478
25 changed files with 555 additions and 272 deletions
+16 -17
View File
@@ -215,7 +215,7 @@ export const gqlFetcherFactory = (endpoint: string) => {
return gqlFetch;
};
export const fetchWithTraceReport = (
export const fetchWithTraceReport = async (
input: RequestInfo | URL,
init?: RequestInit,
traceOptions?: { event: string }
@@ -241,21 +241,20 @@ export const fetchWithTraceReport = (
return fetch(input, init);
}
return fetch(input, init)
.then(response => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
traceReporter!.cacheTrace(traceId, spanId, startTime, {
requestId,
...(event ? { event } : {}),
});
return response;
})
.catch(err => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
traceReporter!.uploadTrace(traceId, spanId, startTime, {
requestId,
...(event ? { event } : {}),
});
return Promise.reject(err);
try {
const response = await fetch(input, init);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
traceReporter!.cacheTrace(traceId, spanId, startTime, {
requestId,
...(event ? { event } : {}),
});
return response;
} catch (err) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
traceReporter!.uploadTrace(traceId, spanId, startTime, {
requestId,
...(event ? { event } : {}),
});
return await Promise.reject(err);
}
};
@@ -0,0 +1,5 @@
query checkBlobSizes($workspaceId: String!, $size: Float!) {
checkBlobSize(workspaceId: $workspaceId, size: $size) {
size
}
}
+13
View File
@@ -7,6 +7,19 @@ export interface GraphQLQuery {
containsFile?: boolean;
}
export const checkBlobSizesQuery = {
id: 'checkBlobSizesQuery' as const,
operationName: 'checkBlobSizes',
definitionName: 'checkBlobSize',
containsFile: false,
query: `
query checkBlobSizes($workspaceId: String!, $size: Float!) {
checkBlobSize(workspaceId: $workspaceId, size: $size) {
size
}
}`,
};
export const deleteBlobMutation = {
id: 'deleteBlobMutation' as const,
operationName: 'deleteBlob',
+15
View File
@@ -50,6 +50,16 @@ export interface UpdateWorkspaceInput {
public: InputMaybe<Scalars['Boolean']['input']>;
}
export type CheckBlobSizesQueryVariables = Exact<{
workspaceId: Scalars['String']['input'];
size: Scalars['Float']['input'];
}>;
export type CheckBlobSizesQuery = {
__typename?: 'Query';
checkBlobSize: { __typename?: 'WorkspaceBlobSizes'; size: number };
};
export type DeleteBlobMutationVariables = Exact<{
workspaceId: Scalars['String']['input'];
hash: Scalars['String']['input'];
@@ -448,6 +458,11 @@ export type AcceptInviteByWorkspaceIdMutation = {
};
export type Queries =
| {
name: 'checkBlobSizesQuery';
variables: CheckBlobSizesQueryVariables;
response: CheckBlobSizesQuery;
}
| {
name: 'listBlobsQuery';
variables: ListBlobsQueryVariables;
@@ -1,13 +1,13 @@
import {
checkBlobSizesQuery,
deleteBlobMutation,
fetchWithTraceReport,
listBlobsQuery,
setBlobMutation,
} from '@affine/graphql';
import { fetcher } from '@affine/workspace/affine/gql';
import type { BlobStorage } from '@blocksuite/store';
import { fetcher } from '../affine/gql';
export const createCloudBlobStorage = (workspaceId: string): BlobStorage => {
return {
crud: {
@@ -18,6 +18,20 @@ export const createCloudBlobStorage = (workspaceId: string): BlobStorage => {
).then(res => res.blob());
},
set: async (key, value) => {
const {
checkBlobSize: { size },
} = await fetcher({
query: checkBlobSizesQuery,
variables: {
workspaceId,
size: value.size,
},
});
if (size <= 0) {
throw new Error('Blob size limit exceeded');
}
const result = await fetcher({
query: setBlobMutation,
variables: {