@better-auth/core 1.4.0-beta.6 → 1.4.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/.turbo/turbo-build.log +22 -9
  2. package/build.config.ts +8 -1
  3. package/dist/async_hooks/index.cjs +27 -0
  4. package/dist/async_hooks/index.d.cts +10 -0
  5. package/dist/async_hooks/index.d.mts +10 -0
  6. package/dist/async_hooks/index.d.ts +10 -0
  7. package/dist/async_hooks/index.mjs +25 -0
  8. package/dist/db/adapter/index.cjs +2 -0
  9. package/dist/db/adapter/index.d.cts +23 -0
  10. package/dist/db/adapter/index.d.mts +23 -0
  11. package/dist/db/adapter/index.d.ts +23 -0
  12. package/dist/db/adapter/index.mjs +1 -0
  13. package/dist/db/index.cjs +89 -0
  14. package/dist/db/index.d.cts +102 -105
  15. package/dist/db/index.d.mts +102 -105
  16. package/dist/db/index.d.ts +102 -105
  17. package/dist/db/index.mjs +69 -0
  18. package/dist/env/index.cjs +315 -0
  19. package/dist/env/index.d.cts +85 -0
  20. package/dist/env/index.d.mts +85 -0
  21. package/dist/env/index.d.ts +85 -0
  22. package/dist/env/index.mjs +300 -0
  23. package/dist/index.d.cts +114 -1
  24. package/dist/index.d.mts +114 -1
  25. package/dist/index.d.ts +114 -1
  26. package/dist/oauth2/index.cjs +368 -0
  27. package/dist/oauth2/index.d.cts +277 -0
  28. package/dist/oauth2/index.d.mts +277 -0
  29. package/dist/oauth2/index.d.ts +277 -0
  30. package/dist/oauth2/index.mjs +357 -0
  31. package/dist/shared/core.DeNN5HMO.d.cts +143 -0
  32. package/dist/shared/core.DeNN5HMO.d.mts +143 -0
  33. package/dist/shared/core.DeNN5HMO.d.ts +143 -0
  34. package/package.json +52 -1
  35. package/src/async_hooks/index.ts +43 -0
  36. package/src/db/adapter/index.ts +24 -0
  37. package/src/db/index.ts +13 -0
  38. package/src/db/plugin.ts +11 -0
  39. package/src/db/schema/account.ts +34 -0
  40. package/src/db/schema/rate-limit.ts +21 -0
  41. package/src/db/schema/session.ts +17 -0
  42. package/src/db/schema/shared.ts +7 -0
  43. package/src/db/schema/user.ts +16 -0
  44. package/src/db/schema/verification.ts +15 -0
  45. package/src/db/type.ts +50 -0
  46. package/src/env/color-depth.ts +171 -0
  47. package/src/env/env-impl.ts +123 -0
  48. package/src/env/index.ts +23 -0
  49. package/src/env/logger.test.ts +33 -0
  50. package/src/env/logger.ts +145 -0
  51. package/src/index.ts +3 -1
  52. package/src/oauth2/client-credentials-token.ts +102 -0
  53. package/src/oauth2/create-authorization-url.ts +85 -0
  54. package/src/oauth2/index.ts +22 -0
  55. package/src/oauth2/oauth-provider.ts +194 -0
  56. package/src/oauth2/refresh-access-token.ts +124 -0
  57. package/src/oauth2/utils.ts +36 -0
  58. package/src/oauth2/validate-authorization-code.ts +156 -0
  59. package/src/types/helper.ts +5 -0
  60. package/src/types/index.ts +2 -1
  61. package/src/types/init-options.ts +119 -0
  62. package/tsconfig.json +1 -1
@@ -0,0 +1,143 @@
1
+ import { ZodType } from 'zod';
2
+
3
+ type Primitive = string | number | symbol | bigint | boolean | null | undefined;
4
+ type LiteralString = "" | (string & Record<never, never>);
5
+ type LiteralUnion<LiteralType, BaseType extends Primitive> = LiteralType | (BaseType & Record<never, never>);
6
+
7
+ declare module "../index" {
8
+ interface BetterAuthMutators<O, C> {
9
+ "better-auth/db": {};
10
+ }
11
+ }
12
+ type Models = "user" | "account" | "session" | "verification" | "rate-limit" | "organization" | "member" | "invitation" | "jwks" | "passkey" | "two-factor";
13
+ type DBFieldType = "string" | "number" | "boolean" | "date" | "json" | `${"string" | "number"}[]` | Array<LiteralString>;
14
+ type DBPrimitive = string | number | boolean | Date | null | undefined | string[] | number[];
15
+ type DBFieldAttributeConfig = {
16
+ /**
17
+ * If the field should be required on a new record.
18
+ * @default true
19
+ */
20
+ required?: boolean;
21
+ /**
22
+ * If the value should be returned on a response body.
23
+ * @default true
24
+ */
25
+ returned?: boolean;
26
+ /**
27
+ * If a value should be provided when creating a new record.
28
+ * @default true
29
+ */
30
+ input?: boolean;
31
+ /**
32
+ * Default value for the field
33
+ *
34
+ * Note: This will not create a default value on the database level. It will only
35
+ * be used when creating a new record.
36
+ */
37
+ defaultValue?: DBPrimitive | (() => DBPrimitive);
38
+ /**
39
+ * Update value for the field
40
+ *
41
+ * Note: This will create an onUpdate trigger on the database level for supported adapters.
42
+ * It will be called when updating a record.
43
+ */
44
+ onUpdate?: () => DBPrimitive;
45
+ /**
46
+ * transform the value before storing it.
47
+ */
48
+ transform?: {
49
+ input?: (value: DBPrimitive) => DBPrimitive | Promise<DBPrimitive>;
50
+ output?: (value: DBPrimitive) => DBPrimitive | Promise<DBPrimitive>;
51
+ };
52
+ /**
53
+ * Reference to another model.
54
+ */
55
+ references?: {
56
+ /**
57
+ * The model to reference.
58
+ */
59
+ model: string;
60
+ /**
61
+ * The field on the referenced model.
62
+ */
63
+ field: string;
64
+ /**
65
+ * The action to perform when the reference is deleted.
66
+ * @default "cascade"
67
+ */
68
+ onDelete?: "no action" | "restrict" | "cascade" | "set null" | "set default";
69
+ };
70
+ unique?: boolean;
71
+ /**
72
+ * If the field should be a bigint on the database instead of integer.
73
+ */
74
+ bigint?: boolean;
75
+ /**
76
+ * A zod schema to validate the value.
77
+ */
78
+ validator?: {
79
+ input?: ZodType;
80
+ output?: ZodType;
81
+ };
82
+ /**
83
+ * The name of the field on the database.
84
+ */
85
+ fieldName?: string;
86
+ /**
87
+ * If the field should be sortable.
88
+ *
89
+ * applicable only for `text` type.
90
+ * It's useful to mark fields varchar instead of text.
91
+ */
92
+ sortable?: boolean;
93
+ };
94
+ type DBFieldAttribute<T extends DBFieldType = DBFieldType> = {
95
+ type: T;
96
+ } & DBFieldAttributeConfig;
97
+ type BetterAuthDBSchema = Record<string, {
98
+ /**
99
+ * The name of the table in the database
100
+ */
101
+ modelName: string;
102
+ /**
103
+ * The fields of the table
104
+ */
105
+ fields: Record<string, DBFieldAttribute>;
106
+ /**
107
+ * Whether to disable migrations for this table
108
+ * @default false
109
+ */
110
+ disableMigrations?: boolean;
111
+ /**
112
+ * The order of the table
113
+ */
114
+ order?: number;
115
+ }>;
116
+ interface SecondaryStorage {
117
+ /**
118
+ *
119
+ * @param key - Key to get
120
+ * @returns - Value of the key
121
+ */
122
+ get: (key: string) => Promise<unknown> | unknown;
123
+ set: (
124
+ /**
125
+ * Key to store
126
+ */
127
+ key: string,
128
+ /**
129
+ * Value to store
130
+ */
131
+ value: string,
132
+ /**
133
+ * Time to live in seconds
134
+ */
135
+ ttl?: number) => Promise<void | null | unknown> | void;
136
+ /**
137
+ *
138
+ * @param key - Key to delete
139
+ */
140
+ delete: (key: string) => Promise<void | null | string> | void;
141
+ }
142
+
143
+ export type { BetterAuthDBSchema as B, DBFieldAttribute as D, LiteralUnion as L, Models as M, SecondaryStorage as S, LiteralString as a, DBFieldAttributeConfig as b, DBFieldType as c, DBPrimitive as d };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/core",
3
- "version": "1.4.0-beta.6",
3
+ "version": "1.4.0-beta.8",
4
4
  "description": "The most comprehensive authentication library for TypeScript.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -16,6 +16,26 @@
16
16
  "default": "./dist/index.cjs"
17
17
  }
18
18
  },
19
+ "./async_hooks": {
20
+ "import": {
21
+ "types": "./dist/async_hooks/index.d.ts",
22
+ "default": "./dist/async_hooks/index.mjs"
23
+ },
24
+ "require": {
25
+ "types": "./dist/async_hooks/index.d.cts",
26
+ "default": "./dist/async_hooks/index.cjs"
27
+ }
28
+ },
29
+ "./env": {
30
+ "import": {
31
+ "types": "./dist/env/index.d.ts",
32
+ "default": "./dist/env/index.mjs"
33
+ },
34
+ "require": {
35
+ "types": "./dist/env/index.d.cts",
36
+ "default": "./dist/env/index.cjs"
37
+ }
38
+ },
19
39
  "./db": {
20
40
  "import": {
21
41
  "types": "./dist/db/index.d.ts",
@@ -25,6 +45,26 @@
25
45
  "types": "./dist/db/index.d.cts",
26
46
  "default": "./dist/db/index.cjs"
27
47
  }
48
+ },
49
+ "./db/adapter": {
50
+ "import": {
51
+ "types": "./dist/db/adapter/index.d.ts",
52
+ "default": "./dist/db/adapter/index.mjs"
53
+ },
54
+ "require": {
55
+ "types": "./dist/db/adapter/index.d.cts",
56
+ "default": "./dist/db/adapter/index.cjs"
57
+ }
58
+ },
59
+ "./oauth2": {
60
+ "import": {
61
+ "types": "./dist/oauth2/index.d.ts",
62
+ "default": "./dist/oauth2/index.mjs"
63
+ },
64
+ "require": {
65
+ "types": "./dist/oauth2/index.d.cts",
66
+ "default": "./dist/oauth2/index.cjs"
67
+ }
28
68
  }
29
69
  },
30
70
  "typesVersions": {
@@ -32,18 +72,29 @@
32
72
  "index": [
33
73
  "dist/index.d.ts"
34
74
  ],
75
+ "async_hooks": [
76
+ "dist/async_hooks.d.ts"
77
+ ],
35
78
  "db": [
36
79
  "dist/db.d.ts"
37
80
  ]
38
81
  }
39
82
  },
40
83
  "devDependencies": {
84
+ "@better-auth/utils": "0.3.0",
85
+ "@better-fetch/fetch": "1.1.18",
86
+ "jose": "^6.1.0",
41
87
  "unbuild": "3.6.1"
42
88
  },
43
89
  "dependencies": {
44
90
  "better-call": "1.0.19",
45
91
  "zod": "^4.1.5"
46
92
  },
93
+ "peerDependencies": {
94
+ "@better-auth/utils": "0.3.0",
95
+ "@better-fetch/fetch": "1.1.18",
96
+ "jose": "^6.1.0"
97
+ },
47
98
  "scripts": {
48
99
  "build": "unbuild --clean",
49
100
  "stub": "unbuild --stub",
@@ -0,0 +1,43 @@
1
+ /**
2
+ * AsyncLocalStorage will be import directly in 1.5.x
3
+ */
4
+ import type { AsyncLocalStorage } from "node:async_hooks";
5
+
6
+ // We only export the type here to avoid issues in environments where AsyncLocalStorage is not available.
7
+ export type { AsyncLocalStorage };
8
+
9
+ /**
10
+ * Dynamically import AsyncLocalStorage to avoid issues in environments where it's not available.
11
+ *
12
+ * Right now, this is primarily for Cloudflare Workers.
13
+ *
14
+ */
15
+ let moduleName: string = "node:async_hooks";
16
+
17
+ const AsyncLocalStoragePromise: Promise<typeof AsyncLocalStorage> = import(
18
+ /* @vite-ignore */
19
+ /* webpackIgnore: true */
20
+ moduleName
21
+ )
22
+ .then((mod) => mod.AsyncLocalStorage)
23
+ .catch((err) => {
24
+ if ("AsyncLocalStorage" in globalThis) {
25
+ return (globalThis as any).AsyncLocalStorage;
26
+ }
27
+ console.warn(
28
+ "[better-auth] Warning: AsyncLocalStorage is not available in this environment. Some features may not work as expected.",
29
+ );
30
+ console.warn(
31
+ "[better-auth] Please read more about this warning at https://better-auth.com/docs/installation#mount-handler",
32
+ );
33
+ console.warn(
34
+ "[better-auth] If you are using Cloudflare Workers, please see: https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag",
35
+ );
36
+ throw err;
37
+ });
38
+
39
+ export async function getAsyncLocalStorage(): Promise<
40
+ typeof AsyncLocalStorage
41
+ > {
42
+ return AsyncLocalStoragePromise;
43
+ }
@@ -0,0 +1,24 @@
1
+ export type DBAdapterDebugLogOption =
2
+ | boolean
3
+ | {
4
+ /**
5
+ * Useful when you want to log only certain conditions.
6
+ */
7
+ logCondition?: (() => boolean) | undefined;
8
+ create?: boolean;
9
+ update?: boolean;
10
+ updateMany?: boolean;
11
+ findOne?: boolean;
12
+ findMany?: boolean;
13
+ delete?: boolean;
14
+ deleteMany?: boolean;
15
+ count?: boolean;
16
+ }
17
+ | {
18
+ /**
19
+ * Only used for adapter tests to show debug logs if a test fails.
20
+ *
21
+ * @deprecated Not actually deprecated. Doing this for IDEs to show this option at the very bottom and stop end-users from using this.
22
+ */
23
+ isRunningAdapterTests: boolean;
24
+ };
package/src/db/index.ts CHANGED
@@ -5,6 +5,15 @@ import type {
5
5
  DBPrimitive,
6
6
  BetterAuthDBSchema,
7
7
  } from "./type";
8
+ import type { BetterAuthPluginDBSchema } from "./plugin";
9
+ export type { BetterAuthPluginDBSchema } from "./plugin";
10
+ export type { SecondaryStorage } from "./type";
11
+ export { coreSchema } from "./schema/shared";
12
+ export { userSchema, type User } from "./schema/user";
13
+ export { accountSchema, type Account } from "./schema/account";
14
+ export { sessionSchema, type Session } from "./schema/session";
15
+ export { verificationSchema, type Verification } from "./schema/verification";
16
+ export { rateLimitSchema, type RateLimit } from "./schema/rate-limit";
8
17
 
9
18
  export type {
10
19
  DBFieldAttribute,
@@ -14,6 +23,10 @@ export type {
14
23
  BetterAuthDBSchema,
15
24
  };
16
25
 
26
+ /**
27
+ * @deprecated Backport for 1.3.x, we will remove this in 1.4.x
28
+ */
29
+ export type AuthPluginSchema = BetterAuthPluginDBSchema;
17
30
  /**
18
31
  * @deprecated Backport for 1.3.x, we will remove this in 1.4.x
19
32
  */
@@ -0,0 +1,11 @@
1
+ import type { DBFieldAttribute } from "./type";
2
+
3
+ export type BetterAuthPluginDBSchema = {
4
+ [table in string]: {
5
+ fields: {
6
+ [field in string]: DBFieldAttribute;
7
+ };
8
+ disableMigration?: boolean;
9
+ modelName?: string;
10
+ };
11
+ };
@@ -0,0 +1,34 @@
1
+ import * as z from "zod";
2
+ import { coreSchema } from "./shared";
3
+
4
+ export const accountSchema = coreSchema.extend({
5
+ providerId: z.string(),
6
+ accountId: z.string(),
7
+ userId: z.coerce.string(),
8
+ accessToken: z.string().nullish(),
9
+ refreshToken: z.string().nullish(),
10
+ idToken: z.string().nullish(),
11
+ /**
12
+ * Access token expires at
13
+ */
14
+ accessTokenExpiresAt: z.date().nullish(),
15
+ /**
16
+ * Refresh token expires at
17
+ */
18
+ refreshTokenExpiresAt: z.date().nullish(),
19
+ /**
20
+ * The scopes that the user has authorized
21
+ */
22
+ scope: z.string().nullish(),
23
+ /**
24
+ * Password is only stored in the credential provider
25
+ */
26
+ password: z.string().nullish(),
27
+ });
28
+
29
+ /**
30
+ * Account schema type used by better-auth, note that it's possible that account could have additional fields
31
+ *
32
+ * todo: we should use generics to extend this type with additional fields from plugins and options in the future
33
+ */
34
+ export type Account = z.infer<typeof accountSchema>;
@@ -0,0 +1,21 @@
1
+ import * as z from "zod";
2
+
3
+ export const rateLimitSchema = z.object({
4
+ /**
5
+ * The key to use for rate limiting
6
+ */
7
+ key: z.string(),
8
+ /**
9
+ * The number of requests made
10
+ */
11
+ count: z.number(),
12
+ /**
13
+ * The last request time in milliseconds
14
+ */
15
+ lastRequest: z.number(),
16
+ });
17
+
18
+ /**
19
+ * Rate limit schema type used by better-auth for rate limiting
20
+ */
21
+ export type RateLimit = z.infer<typeof rateLimitSchema>;
@@ -0,0 +1,17 @@
1
+ import * as z from "zod";
2
+ import { coreSchema } from "./shared";
3
+
4
+ export const sessionSchema = coreSchema.extend({
5
+ userId: z.coerce.string(),
6
+ expiresAt: z.date(),
7
+ token: z.string(),
8
+ ipAddress: z.string().nullish(),
9
+ userAgent: z.string().nullish(),
10
+ });
11
+
12
+ /**
13
+ * Session schema type used by better-auth, note that it's possible that session could have additional fields
14
+ *
15
+ * todo: we should use generics to extend this type with additional fields from plugins and options in the future
16
+ */
17
+ export type Session = z.infer<typeof sessionSchema>;
@@ -0,0 +1,7 @@
1
+ import * as z from "zod";
2
+
3
+ export const coreSchema = z.object({
4
+ id: z.string(),
5
+ createdAt: z.date().default(() => new Date()),
6
+ updatedAt: z.date().default(() => new Date()),
7
+ });
@@ -0,0 +1,16 @@
1
+ import * as z from "zod";
2
+ import { coreSchema } from "./shared";
3
+
4
+ export const userSchema = coreSchema.extend({
5
+ email: z.string().transform((val) => val.toLowerCase()),
6
+ emailVerified: z.boolean().default(false),
7
+ name: z.string(),
8
+ image: z.string().nullish(),
9
+ });
10
+
11
+ /**
12
+ * User schema type used by better-auth, note that it's possible that user could have additional fields
13
+ *
14
+ * todo: we should use generics to extend this type with additional fields from plugins and options in the future
15
+ */
16
+ export type User = z.infer<typeof userSchema>;
@@ -0,0 +1,15 @@
1
+ import * as z from "zod";
2
+ import { coreSchema } from "./shared";
3
+
4
+ export const verificationSchema = coreSchema.extend({
5
+ value: z.string(),
6
+ expiresAt: z.date(),
7
+ identifier: z.string(),
8
+ });
9
+
10
+ /**
11
+ * Verification schema type used by better-auth, note that it's possible that verification could have additional fields
12
+ *
13
+ * todo: we should use generics to extend this type with additional fields from plugins and options in the future
14
+ */
15
+ export type Verification = z.infer<typeof verificationSchema>;
package/src/db/type.ts CHANGED
@@ -1,6 +1,28 @@
1
1
  import type { ZodType } from "zod";
2
2
  import type { LiteralString } from "../types";
3
3
 
4
+ declare module "../index" {
5
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
6
+ interface BetterAuthMutators<O, C> {
7
+ "better-auth/db": {
8
+ // todo: we should infer the schema from the adapter
9
+ };
10
+ }
11
+ }
12
+
13
+ export type Models =
14
+ | "user"
15
+ | "account"
16
+ | "session"
17
+ | "verification"
18
+ | "rate-limit"
19
+ | "organization"
20
+ | "member"
21
+ | "invitation"
22
+ | "jwks"
23
+ | "passkey"
24
+ | "two-factor";
25
+
4
26
  export type DBFieldType =
5
27
  | "string"
6
28
  | "number"
@@ -131,3 +153,31 @@ export type BetterAuthDBSchema = Record<
131
153
  order?: number;
132
154
  }
133
155
  >;
156
+
157
+ export interface SecondaryStorage {
158
+ /**
159
+ *
160
+ * @param key - Key to get
161
+ * @returns - Value of the key
162
+ */
163
+ get: (key: string) => Promise<unknown> | unknown;
164
+ set: (
165
+ /**
166
+ * Key to store
167
+ */
168
+ key: string,
169
+ /**
170
+ * Value to store
171
+ */
172
+ value: string,
173
+ /**
174
+ * Time to live in seconds
175
+ */
176
+ ttl?: number,
177
+ ) => Promise<void | null | unknown> | void;
178
+ /**
179
+ *
180
+ * @param key - Key to delete
181
+ */
182
+ delete: (key: string) => Promise<void | null | string> | void;
183
+ }
@@ -0,0 +1,171 @@
1
+ // Source code copied & modified from node internals: https://github.com/nodejs/node/blob/5b32bb1573dace2dd058c05ac4fab1e4e446c775/lib/internal/tty.js#L123
2
+ import { env, getEnvVar } from "./env-impl";
3
+
4
+ const COLORS_2 = 1;
5
+ const COLORS_16 = 4;
6
+ const COLORS_256 = 8;
7
+ const COLORS_16m = 24;
8
+
9
+ const TERM_ENVS: Record<string, number> = {
10
+ eterm: COLORS_16,
11
+ cons25: COLORS_16,
12
+ console: COLORS_16,
13
+ cygwin: COLORS_16,
14
+ dtterm: COLORS_16,
15
+ gnome: COLORS_16,
16
+ hurd: COLORS_16,
17
+ jfbterm: COLORS_16,
18
+ konsole: COLORS_16,
19
+ kterm: COLORS_16,
20
+ mlterm: COLORS_16,
21
+ mosh: COLORS_16m,
22
+ putty: COLORS_16,
23
+ st: COLORS_16,
24
+ // http://lists.schmorp.de/pipermail/rxvt-unicode/2016q2/002261.html
25
+ "rxvt-unicode-24bit": COLORS_16m,
26
+ // https://bugs.launchpad.net/terminator/+bug/1030562
27
+ terminator: COLORS_16m,
28
+ "xterm-kitty": COLORS_16m,
29
+ };
30
+
31
+ const CI_ENVS_MAP = new Map(
32
+ Object.entries({
33
+ APPVEYOR: COLORS_256,
34
+ BUILDKITE: COLORS_256,
35
+ CIRCLECI: COLORS_16m,
36
+ DRONE: COLORS_256,
37
+ GITEA_ACTIONS: COLORS_16m,
38
+ GITHUB_ACTIONS: COLORS_16m,
39
+ GITLAB_CI: COLORS_256,
40
+ TRAVIS: COLORS_256,
41
+ }),
42
+ );
43
+
44
+ const TERM_ENVS_REG_EXP = [
45
+ /ansi/,
46
+ /color/,
47
+ /linux/,
48
+ /direct/,
49
+ /^con[0-9]*x[0-9]/,
50
+ /^rxvt/,
51
+ /^screen/,
52
+ /^xterm/,
53
+ /^vt100/,
54
+ /^vt220/,
55
+ ];
56
+
57
+ // The `getColorDepth` API got inspired by multiple sources such as
58
+ // https://github.com/chalk/supports-color,
59
+ // https://github.com/isaacs/color-support.
60
+ export function getColorDepth(): number {
61
+ // Use level 0-3 to support the same levels as `chalk` does. This is done for
62
+ // consistency throughout the ecosystem.
63
+ if (getEnvVar("FORCE_COLOR") !== undefined) {
64
+ switch (getEnvVar("FORCE_COLOR")) {
65
+ case "":
66
+ case "1":
67
+ case "true":
68
+ return COLORS_16;
69
+ case "2":
70
+ return COLORS_256;
71
+ case "3":
72
+ return COLORS_16m;
73
+ default:
74
+ return COLORS_2;
75
+ }
76
+ }
77
+
78
+ if (
79
+ (getEnvVar("NODE_DISABLE_COLORS") !== undefined &&
80
+ getEnvVar("NODE_DISABLE_COLORS") !== "") ||
81
+ // See https://no-color.org/
82
+ (getEnvVar("NO_COLOR") !== undefined && getEnvVar("NO_COLOR") !== "") ||
83
+ // The "dumb" special terminal, as defined by terminfo, doesn't support
84
+ // ANSI color control codes.
85
+ // See https://invisible-island.net/ncurses/terminfo.ti.html#toc-_Specials
86
+ getEnvVar("TERM") === "dumb"
87
+ ) {
88
+ return COLORS_2;
89
+ }
90
+
91
+ if (typeof process !== "undefined" && process.platform === "win32") {
92
+ // Windows 10 build 14931 (from 2016) has true color support
93
+ return COLORS_16m;
94
+ }
95
+
96
+ if (getEnvVar("TMUX")) {
97
+ return COLORS_16m;
98
+ }
99
+
100
+ // Azure DevOps
101
+ if ("TF_BUILD" in env && "AGENT_NAME" in env) {
102
+ return COLORS_16;
103
+ }
104
+
105
+ if ("CI" in env) {
106
+ for (const { 0: envName, 1: colors } of CI_ENVS_MAP) {
107
+ if (envName in env) {
108
+ return colors;
109
+ }
110
+ }
111
+ if (getEnvVar("CI_NAME") === "codeship") {
112
+ return COLORS_256;
113
+ }
114
+ return COLORS_2;
115
+ }
116
+
117
+ if ("TEAMCITY_VERSION" in env) {
118
+ return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.exec(
119
+ getEnvVar("TEAMCITY_VERSION"),
120
+ ) !== null
121
+ ? COLORS_16
122
+ : COLORS_2;
123
+ }
124
+
125
+ switch (getEnvVar("TERM_PROGRAM")) {
126
+ case "iTerm.app":
127
+ if (
128
+ !getEnvVar("TERM_PROGRAM_VERSION") ||
129
+ /^[0-2]\./.exec(getEnvVar("TERM_PROGRAM_VERSION")) !== null
130
+ ) {
131
+ return COLORS_256;
132
+ }
133
+ return COLORS_16m;
134
+ case "HyperTerm":
135
+ case "MacTerm":
136
+ return COLORS_16m;
137
+ case "Apple_Terminal":
138
+ return COLORS_256;
139
+ }
140
+
141
+ if (
142
+ getEnvVar("COLORTERM") === "truecolor" ||
143
+ getEnvVar("COLORTERM") === "24bit"
144
+ ) {
145
+ return COLORS_16m;
146
+ }
147
+
148
+ if (getEnvVar("TERM")) {
149
+ if (/truecolor/.exec(getEnvVar("TERM")) !== null) {
150
+ return COLORS_16m;
151
+ }
152
+
153
+ if (/^xterm-256/.exec(getEnvVar("TERM")) !== null) {
154
+ return COLORS_256;
155
+ }
156
+
157
+ const termEnv = getEnvVar("TERM").toLowerCase();
158
+
159
+ if (TERM_ENVS[termEnv]) {
160
+ return TERM_ENVS[termEnv];
161
+ }
162
+ if (TERM_ENVS_REG_EXP.some((term) => term.exec(termEnv) !== null)) {
163
+ return COLORS_16;
164
+ }
165
+ }
166
+ // Move 16 color COLORTERM below 16m and 256
167
+ if (getEnvVar("COLORTERM")) {
168
+ return COLORS_16;
169
+ }
170
+ return COLORS_2;
171
+ }