Skip to content

Commit 8b399f7

Browse files
authored
Read Only Mode (#565)
* Read Only Mode * updates * updates
1 parent 6163920 commit 8b399f7

File tree

7 files changed

+24
-9
lines changed

7 files changed

+24
-9
lines changed

docker-compose-infra.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: "3.8"
2-
31
services:
42
db:
53
image: postgres:latest

docker-compose-prod.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: "3.8"
21
services:
32
engine:
43
build:

src/db/transactions/getAllTxs.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,13 @@ export const getAllTxs = async ({
7373
const totalCountPromise: Promise<number> = prisma.transactions.count({
7474
where: {
7575
...filterQuery,
76-
queuedAt: {
77-
gte: new Date(Date.now() - 24 * 60 * 60 * 1000),
78-
},
7976
},
8077
});
8178

8279
// TODO: Cleaning should be handled by zod
8380
const txsPromise: Promise<Transactions[]> = prisma.transactions.findMany({
8481
where: {
8582
...filterQuery,
86-
queuedAt: {
87-
gte: new Date(Date.now() - 24 * 60 * 60 * 1000),
88-
},
8983
},
9084
orderBy: [
9185
{

src/server/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { withServerUsageReporting } from "../utils/usage";
1010
import { updateTxListener } from "./listeners/updateTxListener";
1111
import { withAuth } from "./middleware/auth";
1212
import { withCors } from "./middleware/cors";
13+
import { withEnforceEngineMode } from "./middleware/engine-mode";
1314
import { withErrorHandler } from "./middleware/error";
1415
import { withExpress } from "./middleware/express";
1516
import { withRequestLogs } from "./middleware/logs";
@@ -57,6 +58,7 @@ export const initServer = async () => {
5758
await withCors(server);
5859
await withRequestLogs(server);
5960
await withErrorHandler(server);
61+
await withEnforceEngineMode(server);
6062
await withWebSocket(server);
6163
await withAuth(server);
6264
await withExpress(server);

src/server/middleware/auth.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ export const withAuth = async (server: FastifyInstance) => {
106106
// Note: in the onRequest hook, request.body will always be undefined, because the body parsing happens before the preValidation hook.
107107
// https://fastify.dev/docs/latest/Reference/Hooks/#onrequest
108108
server.addHook("preValidation", async (req, res) => {
109+
// Skip auth check in sandbox mode
110+
if (env.ENGINE_MODE === "sandbox") {
111+
return;
112+
}
109113
let message =
110114
"Please provide a valid access token or other authentication. See: https://portal.thirdweb.com/engine/features/access-tokens";
111115

src/server/middleware/engine-mode.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { FastifyInstance } from "fastify";
2+
import { env } from "../../utils/env";
3+
4+
export const withEnforceEngineMode = async (server: FastifyInstance) => {
5+
if (env.ENGINE_MODE === "sandbox") {
6+
server.addHook("onRequest", async (request, reply) => {
7+
if (request.method !== "GET") {
8+
return reply.status(405).send({
9+
statusCode: 405,
10+
error: "Read Only Mode. Method Not Allowed",
11+
message: "Read Only Mode. Method Not Allowed",
12+
});
13+
}
14+
});
15+
}
16+
};

src/utils/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const env = createEnv({
7272
.nonnegative()
7373
.default(0),
7474
REDIS_URL: z.string(),
75+
ENGINE_MODE: z.enum(["sandbox", "unrestricted"]).default("unrestricted"),
7576
},
7677
clientPrefix: "NEVER_USED",
7778
client: {},
@@ -96,6 +97,7 @@ export const env = createEnv({
9697
CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS:
9798
process.env.CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS,
9899
REDIS_URL: process.env.REDIS_URL,
100+
ENGINE_MODE: process.env.ENGINE_MODE,
99101
},
100102
onValidationError: (error: ZodError) => {
101103
console.error(

0 commit comments

Comments
 (0)