@axium/server 0.0.1 → 0.0.3

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.
@@ -0,0 +1,270 @@
1
+ import { type PartialRecursive } from 'utilium';
2
+ import * as z from 'zod';
3
+ export declare const Database: z.ZodObject<{
4
+ host: z.ZodString;
5
+ port: z.ZodNumber;
6
+ password: z.ZodString;
7
+ user: z.ZodString;
8
+ database: z.ZodString;
9
+ }, "strip", z.ZodTypeAny, {
10
+ host: string;
11
+ port: number;
12
+ password: string;
13
+ user: string;
14
+ database: string;
15
+ }, {
16
+ host: string;
17
+ port: number;
18
+ password: string;
19
+ user: string;
20
+ database: string;
21
+ }>;
22
+ export type Database = z.infer<typeof Database>;
23
+ export declare const db: Database;
24
+ export declare const Auth: z.ZodObject<{
25
+ credentials: z.ZodBoolean;
26
+ debug: z.ZodOptional<z.ZodBoolean>;
27
+ secret: z.ZodString;
28
+ secure_cookies: z.ZodBoolean;
29
+ }, "strip", z.ZodTypeAny, {
30
+ credentials: boolean;
31
+ secret: string;
32
+ secure_cookies: boolean;
33
+ debug?: boolean | undefined;
34
+ }, {
35
+ credentials: boolean;
36
+ secret: string;
37
+ secure_cookies: boolean;
38
+ debug?: boolean | undefined;
39
+ }>;
40
+ export type Auth = z.infer<typeof Auth>;
41
+ export declare const auth: Auth;
42
+ export declare const Log: z.ZodObject<{
43
+ level: z.ZodEnum<["error", "warn", "notice", "info", "debug"]>;
44
+ console: z.ZodBoolean;
45
+ }, "strip", z.ZodTypeAny, {
46
+ level: "debug" | "error" | "warn" | "notice" | "info";
47
+ console: boolean;
48
+ }, {
49
+ level: "debug" | "error" | "warn" | "notice" | "info";
50
+ console: boolean;
51
+ }>;
52
+ export type Log = z.infer<typeof Log>;
53
+ export declare const log: Log;
54
+ export declare const Config: z.ZodObject<{
55
+ auth: z.ZodObject<{
56
+ credentials: z.ZodOptional<z.ZodBoolean>;
57
+ debug: z.ZodOptional<z.ZodOptional<z.ZodBoolean>>;
58
+ secret: z.ZodOptional<z.ZodString>;
59
+ secure_cookies: z.ZodOptional<z.ZodBoolean>;
60
+ }, "strip", z.ZodTypeAny, {
61
+ credentials?: boolean | undefined;
62
+ debug?: boolean | undefined;
63
+ secret?: string | undefined;
64
+ secure_cookies?: boolean | undefined;
65
+ }, {
66
+ credentials?: boolean | undefined;
67
+ debug?: boolean | undefined;
68
+ secret?: string | undefined;
69
+ secure_cookies?: boolean | undefined;
70
+ }>;
71
+ debug: z.ZodBoolean;
72
+ db: z.ZodObject<{
73
+ host: z.ZodOptional<z.ZodString>;
74
+ port: z.ZodOptional<z.ZodNumber>;
75
+ password: z.ZodOptional<z.ZodString>;
76
+ user: z.ZodOptional<z.ZodString>;
77
+ database: z.ZodOptional<z.ZodString>;
78
+ }, "strip", z.ZodTypeAny, {
79
+ host?: string | undefined;
80
+ port?: number | undefined;
81
+ password?: string | undefined;
82
+ user?: string | undefined;
83
+ database?: string | undefined;
84
+ }, {
85
+ host?: string | undefined;
86
+ port?: number | undefined;
87
+ password?: string | undefined;
88
+ user?: string | undefined;
89
+ database?: string | undefined;
90
+ }>;
91
+ log: z.ZodObject<{
92
+ level: z.ZodOptional<z.ZodEnum<["error", "warn", "notice", "info", "debug"]>>;
93
+ console: z.ZodOptional<z.ZodBoolean>;
94
+ }, "strip", z.ZodTypeAny, {
95
+ level?: "debug" | "error" | "warn" | "notice" | "info" | undefined;
96
+ console?: boolean | undefined;
97
+ }, {
98
+ level?: "debug" | "error" | "warn" | "notice" | "info" | undefined;
99
+ console?: boolean | undefined;
100
+ }>;
101
+ }, "strip", z.ZodTypeAny, {
102
+ debug: boolean;
103
+ auth: {
104
+ credentials?: boolean | undefined;
105
+ debug?: boolean | undefined;
106
+ secret?: string | undefined;
107
+ secure_cookies?: boolean | undefined;
108
+ };
109
+ db: {
110
+ host?: string | undefined;
111
+ port?: number | undefined;
112
+ password?: string | undefined;
113
+ user?: string | undefined;
114
+ database?: string | undefined;
115
+ };
116
+ log: {
117
+ level?: "debug" | "error" | "warn" | "notice" | "info" | undefined;
118
+ console?: boolean | undefined;
119
+ };
120
+ }, {
121
+ debug: boolean;
122
+ auth: {
123
+ credentials?: boolean | undefined;
124
+ debug?: boolean | undefined;
125
+ secret?: string | undefined;
126
+ secure_cookies?: boolean | undefined;
127
+ };
128
+ db: {
129
+ host?: string | undefined;
130
+ port?: number | undefined;
131
+ password?: string | undefined;
132
+ user?: string | undefined;
133
+ database?: string | undefined;
134
+ };
135
+ log: {
136
+ level?: "debug" | "error" | "warn" | "notice" | "info" | undefined;
137
+ console?: boolean | undefined;
138
+ };
139
+ }>;
140
+ export declare const File: z.ZodObject<z.objectUtil.extendShape<{
141
+ auth: z.ZodOptional<z.ZodObject<{
142
+ credentials: z.ZodOptional<z.ZodOptional<z.ZodBoolean>>;
143
+ debug: z.ZodOptional<z.ZodOptional<z.ZodOptional<z.ZodBoolean>>>;
144
+ secret: z.ZodOptional<z.ZodOptional<z.ZodString>>;
145
+ secure_cookies: z.ZodOptional<z.ZodOptional<z.ZodBoolean>>;
146
+ }, "strip", z.ZodTypeAny, {
147
+ credentials?: boolean | undefined;
148
+ debug?: boolean | undefined;
149
+ secret?: string | undefined;
150
+ secure_cookies?: boolean | undefined;
151
+ }, {
152
+ credentials?: boolean | undefined;
153
+ debug?: boolean | undefined;
154
+ secret?: string | undefined;
155
+ secure_cookies?: boolean | undefined;
156
+ }>>;
157
+ debug: z.ZodOptional<z.ZodBoolean>;
158
+ db: z.ZodOptional<z.ZodObject<{
159
+ host: z.ZodOptional<z.ZodOptional<z.ZodString>>;
160
+ port: z.ZodOptional<z.ZodOptional<z.ZodNumber>>;
161
+ password: z.ZodOptional<z.ZodOptional<z.ZodString>>;
162
+ user: z.ZodOptional<z.ZodOptional<z.ZodString>>;
163
+ database: z.ZodOptional<z.ZodOptional<z.ZodString>>;
164
+ }, "strip", z.ZodTypeAny, {
165
+ host?: string | undefined;
166
+ port?: number | undefined;
167
+ password?: string | undefined;
168
+ user?: string | undefined;
169
+ database?: string | undefined;
170
+ }, {
171
+ host?: string | undefined;
172
+ port?: number | undefined;
173
+ password?: string | undefined;
174
+ user?: string | undefined;
175
+ database?: string | undefined;
176
+ }>>;
177
+ log: z.ZodOptional<z.ZodObject<{
178
+ level: z.ZodOptional<z.ZodOptional<z.ZodEnum<["error", "warn", "notice", "info", "debug"]>>>;
179
+ console: z.ZodOptional<z.ZodOptional<z.ZodBoolean>>;
180
+ }, "strip", z.ZodTypeAny, {
181
+ level?: "debug" | "error" | "warn" | "notice" | "info" | undefined;
182
+ console?: boolean | undefined;
183
+ }, {
184
+ level?: "debug" | "error" | "warn" | "notice" | "info" | undefined;
185
+ console?: boolean | undefined;
186
+ }>>;
187
+ }, {
188
+ include: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
189
+ }>, "strip", z.ZodTypeAny, {
190
+ debug?: boolean | undefined;
191
+ auth?: {
192
+ credentials?: boolean | undefined;
193
+ debug?: boolean | undefined;
194
+ secret?: string | undefined;
195
+ secure_cookies?: boolean | undefined;
196
+ } | undefined;
197
+ db?: {
198
+ host?: string | undefined;
199
+ port?: number | undefined;
200
+ password?: string | undefined;
201
+ user?: string | undefined;
202
+ database?: string | undefined;
203
+ } | undefined;
204
+ log?: {
205
+ level?: "debug" | "error" | "warn" | "notice" | "info" | undefined;
206
+ console?: boolean | undefined;
207
+ } | undefined;
208
+ include?: string[] | undefined;
209
+ }, {
210
+ debug?: boolean | undefined;
211
+ auth?: {
212
+ credentials?: boolean | undefined;
213
+ debug?: boolean | undefined;
214
+ secret?: string | undefined;
215
+ secure_cookies?: boolean | undefined;
216
+ } | undefined;
217
+ db?: {
218
+ host?: string | undefined;
219
+ port?: number | undefined;
220
+ password?: string | undefined;
221
+ user?: string | undefined;
222
+ database?: string | undefined;
223
+ } | undefined;
224
+ log?: {
225
+ level?: "debug" | "error" | "warn" | "notice" | "info" | undefined;
226
+ console?: boolean | undefined;
227
+ } | undefined;
228
+ include?: string[] | undefined;
229
+ }>;
230
+ export type File = z.infer<typeof File>;
231
+ export interface Config extends z.infer<typeof Config> {
232
+ }
233
+ export declare let debug: boolean;
234
+ export declare function get(): Config;
235
+ /**
236
+ * Update the current config
237
+ */
238
+ export declare function set(config: PartialRecursive<Config>): void;
239
+ export declare const files: Map<string, PartialRecursive<Config>>;
240
+ export interface LoadOptions {
241
+ /**
242
+ * If enabled, the config file will be not be loaded if it does not match the schema.
243
+ */
244
+ strict?: boolean;
245
+ /**
246
+ * If enabled, the config file will be skipped if it does not exist.
247
+ */
248
+ optional?: boolean;
249
+ /**
250
+ * If `optional`, this function will be called with the error if the config file is invalid or can't be read.
251
+ */
252
+ onError?(error: Error): void;
253
+ }
254
+ /**
255
+ * Load the config from the provided path
256
+ */
257
+ export declare function load(path: string, options?: LoadOptions): void;
258
+ export declare function loadDefaults(): void;
259
+ /**
260
+ * Update the current config and write the updated config to the appropriate file
261
+ */
262
+ export declare function save(changed: PartialRecursive<Config>, global?: boolean): void;
263
+ /**
264
+ * Update the current config and write the updated config to the provided path
265
+ */
266
+ export declare function saveTo(path: string, changed: PartialRecursive<Config>): void;
267
+ /**
268
+ * Find the path to the config file
269
+ */
270
+ export declare function findPath(global: boolean): string;
package/dist/config.js ADDED
@@ -0,0 +1,122 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
2
+ import { levelText } from 'logzen';
3
+ import { readFileSync, writeFileSync } from 'node:fs';
4
+ import { dirname, join } from 'node:path/posix';
5
+ import { assignWithDefaults } from 'utilium';
6
+ import * as z from 'zod';
7
+ import { findDir, logger, output } from './io.js';
8
+ export const Database = z.object({
9
+ host: z.string(),
10
+ port: z.number(),
11
+ password: z.string(),
12
+ user: z.string(),
13
+ database: z.string(),
14
+ });
15
+ export const db = {
16
+ host: process.env.PGHOST || 'localhost',
17
+ port: process.env.PGPORT && Number.isSafeInteger(parseInt(process.env.PGPORT)) ? parseInt(process.env.PGPORT) : 5432,
18
+ password: process.env.PGPASSWORD || '',
19
+ user: process.env.PGUSER || 'axium',
20
+ database: process.env.PGDATABASE || 'axium',
21
+ };
22
+ export const Auth = z.object({
23
+ credentials: z.boolean(),
24
+ debug: z.boolean().optional(),
25
+ secret: z.string(),
26
+ secure_cookies: z.boolean(),
27
+ });
28
+ export const auth = {
29
+ credentials: false,
30
+ secret: '',
31
+ secure_cookies: false,
32
+ };
33
+ export const Log = z.object({
34
+ level: z.enum(levelText),
35
+ console: z.boolean(),
36
+ });
37
+ export const log = {
38
+ level: 'info',
39
+ console: true,
40
+ };
41
+ export const Config = z.object({
42
+ auth: Auth.partial(),
43
+ debug: z.boolean(),
44
+ db: Database.partial(),
45
+ log: Log.partial(),
46
+ });
47
+ // config from file
48
+ export const File = Config.deepPartial().extend({
49
+ include: z.array(z.string()).optional(),
50
+ });
51
+ export let debug = false;
52
+ export function get() {
53
+ return {
54
+ auth,
55
+ db,
56
+ debug,
57
+ log,
58
+ };
59
+ }
60
+ /**
61
+ * Update the current config
62
+ */
63
+ export function set(config) {
64
+ assignWithDefaults(auth, config.auth ?? {});
65
+ debug = config.debug ?? debug;
66
+ assignWithDefaults(db, config.db ?? {});
67
+ assignWithDefaults(log, config.log ?? {});
68
+ logger.detach(output);
69
+ if (log.console)
70
+ logger.attach(output, { output: log.level });
71
+ }
72
+ export const files = new Map();
73
+ /**
74
+ * Load the config from the provided path
75
+ */
76
+ export function load(path, options = {}) {
77
+ let json;
78
+ try {
79
+ json = JSON.parse(readFileSync(path, 'utf8'));
80
+ }
81
+ catch (e) {
82
+ if (!options.optional)
83
+ throw e;
84
+ logger.debug(`Skipping config at ${path} (${e.message})`);
85
+ return;
86
+ }
87
+ const config = options.strict ? File.parse(json) : json;
88
+ files.set(path, config);
89
+ set(config);
90
+ for (const include of config.include ?? [])
91
+ load(join(dirname(path), include), { optional: true });
92
+ }
93
+ export function loadDefaults() {
94
+ load(findPath(true), { optional: true });
95
+ load(findPath(false), { optional: true });
96
+ }
97
+ /**
98
+ * Update the current config and write the updated config to the appropriate file
99
+ */
100
+ export function save(changed, global = false) {
101
+ saveTo(process.env.AXIUM_CONFIG ?? findPath(global), changed);
102
+ }
103
+ /**
104
+ * Update the current config and write the updated config to the provided path
105
+ */
106
+ export function saveTo(path, changed) {
107
+ set(changed);
108
+ const config = files.get(path) ?? {};
109
+ Object.assign(config, { ...changed, db: { ...config.db, ...changed.db } });
110
+ logger.debug(`Wrote config to ${path}`);
111
+ writeFileSync(path, JSON.stringify(config));
112
+ }
113
+ /**
114
+ * Find the path to the config file
115
+ */
116
+ export function findPath(global) {
117
+ if (process.env.AXIUM_CONFIG)
118
+ return process.env.AXIUM_CONFIG;
119
+ return join(findDir(global), 'config.json');
120
+ }
121
+ if (process.env.AXIUM_CONFIG)
122
+ load(process.env.AXIUM_CONFIG);
@@ -0,0 +1,80 @@
1
+ import type { AdapterAccountType as db } from '@auth/core/adapters';
2
+ import { Kysely, type GeneratedAlways } from 'kysely';
3
+ import * as config from './config.js';
4
+ export interface Schema {
5
+ User: {
6
+ id: GeneratedAlways<string>;
7
+ name: string | null;
8
+ email: string;
9
+ emailVerified: Date | null;
10
+ image: string | null;
11
+ password: string | null;
12
+ salt: string | null;
13
+ };
14
+ Account: {
15
+ id: GeneratedAlways<string>;
16
+ userId: string;
17
+ type: db;
18
+ provider: string;
19
+ providerAccountId: string;
20
+ refresh_token?: string;
21
+ access_token?: string;
22
+ expires_at?: number;
23
+ token_type?: Lowercase<string>;
24
+ scope?: string;
25
+ id_token?: string;
26
+ session_state: string | null;
27
+ };
28
+ Session: {
29
+ id: GeneratedAlways<string>;
30
+ userId: string;
31
+ sessionToken: string;
32
+ expires: Date;
33
+ };
34
+ VerificationToken: {
35
+ identifier: string;
36
+ token: string;
37
+ expires: Date;
38
+ };
39
+ Authenticator: {
40
+ credentialID: string;
41
+ userId: string;
42
+ providerAccountId: string;
43
+ credentialPublicKey: string;
44
+ counter: number;
45
+ credentialDeviceType: string;
46
+ credentialBackedUp: boolean;
47
+ transports: string | null;
48
+ };
49
+ }
50
+ export declare let database: Kysely<Schema> & AsyncDisposable;
51
+ export declare function connect(): Kysely<Schema> & AsyncDisposable;
52
+ export interface Stats {
53
+ users: number;
54
+ accounts: number;
55
+ sessions: number;
56
+ }
57
+ export declare function status(): Promise<Stats>;
58
+ export declare function statusText(): Promise<string>;
59
+ export type OpOutputState = 'done' | 'log' | 'warn' | 'error' | 'start';
60
+ export type OpOutput = {
61
+ (state: 'done'): void;
62
+ (state: Exclude<OpOutputState, 'done'>, message: string): void;
63
+ };
64
+ export interface OpOptions {
65
+ timeout: number;
66
+ force: boolean;
67
+ output?: OpOutput;
68
+ }
69
+ export interface InitOptions extends OpOptions {
70
+ skip: boolean;
71
+ }
72
+ export declare function init(opt: InitOptions): Promise<config.Database>;
73
+ /**
74
+ * Completely remove Axium from the database.
75
+ */
76
+ export declare function uninstall(opt: OpOptions): Promise<void>;
77
+ /**
78
+ * Removes all data from tables.
79
+ */
80
+ export declare function wipe(opt: OpOptions): Promise<void>;