Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion core/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ HONEYCOMB_API_KEY="xxx"
# KYSELY_DEBUG="true"

GCLOUD_KEY_FILE='xxx'

4 changes: 2 additions & 2 deletions core/actions/_lib/runActionInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { logger } from "logger";
import type { ActionSuccess } from "../types";
import type { ClientException, ClientExceptionOptions } from "~/lib/serverActions";
import { db } from "~/kysely/database";
import { env } from "~/lib/env/env.mjs";
import { flags } from "~/lib/env/env";
import { hydratePubValues } from "~/lib/fields/utils";
import { createLastModifiedBy } from "~/lib/lastModifiedBy";
import { getPubsWithRelatedValues } from "~/lib/server";
Expand Down Expand Up @@ -237,7 +237,7 @@ export async function runActionInstance(args: RunActionInstanceArgs, trx = db) {
};
}

if (env.DISABLED_ACTIONS?.includes(actionInstance.action)) {
if (flags.get("disabled-actions").includes(actionInstance.action)) {
return {
error: "Action is disabled",
stack: args.stack,
Expand Down
2 changes: 1 addition & 1 deletion core/actions/datacite/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { assert, AssertionError, expect } from "utils";
import type { ActionPub } from "../types";
import type { action } from "./action";
import type { components } from "./types";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { getPubsWithRelatedValues, updatePub } from "~/lib/server";
import { isClientExceptionOptions } from "~/lib/serverActions";
import { defineRun } from "../types";
Expand Down
2 changes: 1 addition & 1 deletion core/actions/googleDriveImport/getGDriveFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { JWT } from "google-auth-library";

import { logger } from "logger";

import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";

// Load the service account key JSON file
// const keyFilePath = path.join(process.cwd(), 'src/utils/google/keyFile.json');
Expand Down
2 changes: 1 addition & 1 deletion core/app/(user)/magic-link/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { AuthTokenType } from "db/public";
import { logger } from "logger";

import { lucia } from "~/lib/authentication/lucia";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { InvalidTokenError, TokenFailureReason, validateToken } from "~/lib/server/token";

const redirectToURL = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { runInstancesForEvent } from "~/actions/_lib/runActionInstance";
import { scheduleActionInstances } from "~/actions/_lib/scheduleActionInstance";
import { runActionInstance } from "~/actions/api/server";
import { compareAPIKeys, getBearerToken } from "~/lib/authentication/api";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { NotFoundError, tsRestHandleErrors } from "~/lib/server";
import { findCommunityBySlug } from "~/lib/server/community";

Expand Down Expand Up @@ -97,4 +97,4 @@ const handler = createNextHandler(
}
);

export { handler as GET, handler as POST, handler as PUT, handler as PATCH, handler as DELETE };
export { handler as DELETE, handler as GET, handler as PATCH, handler as POST, handler as PUT };
2 changes: 1 addition & 1 deletion core/app/c/[communitySlug]/developers/docs/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { redirect } from "next/navigation";

import { getPageLoginData } from "~/lib/authentication/loginData";
import { isCommunityAdmin } from "~/lib/authentication/roles";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";

export const dynamic = "force-dynamic";

Expand Down
1 change: 1 addition & 0 deletions core/app/c/[communitySlug]/pubs/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Metadata } from "next";
import type { CommunitiesId } from "db/public";

import { getPageLoginData } from "~/lib/authentication/loginData";
import { env } from "~/lib/env/env";
import { findCommunityBySlug } from "~/lib/server/community";
import PubHeader from "./PubHeader";
import { PaginatedPubList } from "./PubList";
Expand Down
2 changes: 1 addition & 1 deletion core/instrumentation.edge.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Sentry from "@sentry/nextjs";

import { logger } from "logger";

import { env } from "./lib/env/env.mjs";
import { env } from "./lib/env/env";

logger.info("Running instrumentation hook for edge...");

Expand Down
2 changes: 1 addition & 1 deletion core/instrumentation.node.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Sentry from "@sentry/nextjs";

import { logger } from "logger";

import { env } from "./lib/env/env.mjs";
import { env } from "./lib/env/env";

// function hook() {
logger.info("Running instrumentation hook for nodejs...");
Expand Down
2 changes: 1 addition & 1 deletion core/kysely/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { createDatabase } from "./database-init";

export const db = createDatabase({
Expand Down
2 changes: 1 addition & 1 deletion core/lib/__tests__/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { databaseTables } from "db/table-names";
import { logger } from "logger";

import { UpdatedAtPlugin } from "~/kysely/updated-at-plugin";
import { env } from "../env/env.mjs";
import { env } from "../env/env";

const int8TypeId = 20;

Expand Down
2 changes: 1 addition & 1 deletion core/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { initTsrReactQuery } from "@ts-rest/react-query/v5";

import { siteApi } from "contracts";

import { env } from "./env/env.mjs";
import { env } from "./env/env";

export const client = initTsrReactQuery(siteApi, {
baseUrl: typeof window === "undefined" ? env.PUBPUB_URL : window.location.origin,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/authentication/createMagicLink.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AuthTokenType, UsersId } from "db/public";

import { db } from "~/kysely/database";
import { env } from "../env/env.mjs";
import { env } from "../env/env";
import { createToken } from "../server/token";

type NativeMagicLinkOptions = {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/authentication/lucia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { AuthTokenType } from "db/public";
import { logger } from "logger";

import { db } from "~/kysely/database";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";

type UserWithMembersShips = Omit<Users, "passwordHash"> & {
memberships: (CommunityMemberships & {
Expand Down
35 changes: 7 additions & 28 deletions core/lib/env/env.mjs → core/lib/env/env.ts
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the important changes are in this file!

Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
// @ts-check
import type { ZodTypeAny } from "zod";

import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";

/**
* Parameters which are optional if the app is self-hosted
* but we do want checked for our AWS deploys
*
* @template {import("zod").ZodTypeAny} Z
* @param {Z} schema
*/
const selfHostedOptional = (schema) => {
import { createFlags, flagsSchema } from "./flags";

const selfHostedOptional = (schema: ZodTypeAny) => {
return process.env.SELF_HOSTED ? schema.optional() : schema;
};

Expand Down Expand Up @@ -52,25 +47,7 @@ export const env = createEnv({
DATACITE_REPOSITORY_ID: z.string().optional(),
DATACITE_PASSWORD: z.string().optional(),
SENTRY_AUTH_TOKEN: z.string().optional(),
DISABLED_ACTIONS: z
.string()
.transform((value) => value.split(","))
.pipe(
z
// TODO: Figure out how to get `import {actionSchema} from "db/public"` working
.enum([
"log",
"pdf",
"email",
"pushToV6",
"http",
"move",
"googleDriveImport",
"datacite",
])
.array()
)
.optional(),
FLAGS: flagsSchema,
},
client: {},
experimental__runtimeEnv: {
Expand All @@ -79,3 +56,5 @@ export const env = createEnv({
skipValidation: Boolean(process.env.SKIP_VALIDATION),
emptyStringAsUndefined: true,
});

export const flags = createFlags(env.FLAGS);
79 changes: 79 additions & 0 deletions core/lib/env/flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { z, ZodError } from "zod";

import { actionSchema } from "db/public";

const flagStateToBoolean = (flagState: string, ctx: z.RefinementCtx) => {
switch (flagState) {
case "on":
case "true":
return true;
case "off":
case "false":
return false;
}
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Invalid flag state",
fatal: true,
});
return z.NEVER;
};

const flagSchema = z.union([
z.tuple([
z.literal("disabled-actions"),
z
.string()
.optional()
.transform((s) => (s ? s.split("+").map((a) => a.trim()) : []))
.pipe(actionSchema.array()),
]),
z.tuple([
z.literal("invites"),
z.string().transform(flagStateToBoolean).optional().default("on"),
]),
z.tuple([
z.literal("uploads"),
z.string().transform(flagStateToBoolean).optional().default("on"),
]),
]);

export const flagsSchema = z
.string()
.optional()
.transform((s) => (s ? s.split(",") : []))
.transform((flagStrings, ctx) => {
const parsedFlags: FlagSchema[] = [];
for (const flagString of flagStrings) {
if (flagString === "") {
continue;
}
try {
const [flagName, flagArgs] = flagString.split(":");
const parsedFlag = flagSchema.parse([flagName, flagArgs]);
parsedFlags.push(parsedFlag);
} catch (error) {
if (error instanceof ZodError) {
error.issues.forEach(ctx.addIssue);
}
}
}
return parsedFlags;
});

type FlagSchema = z.infer<typeof flagSchema>;
type FlagName = FlagSchema[0];
type FlagArgs<F extends FlagName> = Extract<FlagSchema, [F, unknown]>[1];

class Flags {
#flags;
constructor(flags: FlagSchema[]) {
this.#flags = new Map(flags as [string, unknown][]);
}
get<F extends FlagName>(flagName: F): FlagArgs<F> {
return (this.#flags.get(flagName) ??
flagSchema.parse([flagName, undefined])[1]) as FlagArgs<F>;
}
}

export const createFlags = (flags: FlagSchema[]) => new Flags(flags);
2 changes: 1 addition & 1 deletion core/lib/server/assets.db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from "fs/promises";
import { beforeAll, describe, expect, it } from "vitest";

import { mockServerCode } from "~/lib/__tests__/utils";
import { env } from "../env/env.mjs";
import { env } from "../env/env";

const { createForEachMockedTransaction } = await mockServerCode();

Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import type { PubsId } from "db/public";
import { logger } from "logger";

import { env } from "../env/env.mjs";
import { env } from "../env/env";

let s3Client: S3Client;

Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/cache/autoRevalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from "logger";

import type { autoCache } from "./autoCache";
import type { AutoRevalidateOptions, DirectAutoOutput, ExecuteFn, QB } from "./types";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { getCommunitySlug } from "./getCommunitySlug";
import { revalidateTagsForCommunity } from "./revalidate";
import { cachedFindTables, directAutoOutput } from "./sharedAuto";
Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/cache/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { unstable_cache } from "next/cache";
import { logger } from "logger";

import type { CacheTag } from "./cacheTags";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { ONE_YEAR } from "./constants";

type Callback<Parameters extends unknown[], ReturnType> = (
Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/cache/revalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { revalidateTag } from "next/cache";
import { logger } from "logger";

import type { CacheScope } from "./cacheTags";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { createCommunityCacheTags } from "./cacheTags";
import { getCommunitySlug } from "./getCommunitySlug";

Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { XOR } from "../types";
import type { FormInviteLinkProps } from "./form";
import { db } from "~/kysely/database";
import { createMagicLink } from "~/lib/authentication/createMagicLink";
import { env } from "../env/env.mjs";
import { env } from "../env/env";
import { createFormInviteLink } from "./form";
import { getSmtpClient } from "./mailgun";

Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { makeWorkerUtils } from "graphile-worker";
import { logger } from "logger";

import type { ClientException, ClientExceptionOptions } from "../serverActions";
import { env } from "../env/env.mjs";
import { env } from "../env/env";

import "date-fns";

Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/mailgun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type SMTPPool from "nodemailer/lib/smtp-pool";

import nodemailer from "nodemailer";

import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";

let smtpclient: nodemailer.Transporter;

Expand Down
11 changes: 2 additions & 9 deletions core/lib/server/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ import type {
UsersId,
} from "db/public";
import type { LastModifiedBy, StageConstraint } from "db/types";
import {
Capabilities,
CoreSchemaType,
MemberRole,
MembershipType,
OperationType,
pubTypesIdSchema,
} from "db/public";
import { Capabilities, CoreSchemaType, MemberRole, MembershipType, OperationType } from "db/public";
import { NO_STAGE_OPTION } from "db/types";
import { logger } from "logger";
import { assert, expect } from "utils";
Expand All @@ -53,7 +46,7 @@ import type { DefinitelyHas, MaybeHas, XOR } from "../types";
import type { SafeUser } from "./user";
import { db } from "~/kysely/database";
import { isUniqueConstraintError } from "~/kysely/errors";
import { env } from "../env/env.mjs";
import { env } from "../env/env";
import { parseRichTextForPubFieldsAndRelatedPubs } from "../fields/richText";
import { hydratePubValues, mergeSlugsWithFields } from "../fields/utils";
import { parseLastModifiedBy } from "../lastModifiedBy";
Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/render/pub/renderWithPubUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CoreSchemaType } from "db/public";
import { expect } from "utils";

import { db } from "~/kysely/database";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { autoCache } from "~/lib/server/cache/autoCache";
import { addMemberToForm, createFormInviteLink } from "../../form";

Expand Down
Loading
Loading