@axium/server 0.4.1 → 0.4.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.
@@ -2,7 +2,7 @@ import type { AdapterAccountType as db } from '@auth/core/adapters';
2
2
  import { Kysely, type GeneratedAlways } from 'kysely';
3
3
  import type { Preferences } from './auth.js';
4
4
  import * as config from './config.js';
5
- import { type MaybeOutput } from './io.js';
5
+ import { type MaybeOutput, type WithOutput } from './io.js';
6
6
  export interface Schema {
7
7
  User: {
8
8
  id: GeneratedAlways<string>;
@@ -66,6 +66,7 @@ export interface OpOptions extends MaybeOutput {
66
66
  export interface InitOptions extends OpOptions {
67
67
  skip: boolean;
68
68
  }
69
+ export declare function shouldRecreate(opt: InitOptions & WithOutput): boolean;
69
70
  export declare function init(opt: InitOptions): Promise<config.Database>;
70
71
  /**
71
72
  * Completely remove Axium from the database.
package/dist/database.js CHANGED
@@ -54,7 +54,7 @@ import { Kysely, PostgresDialect, sql } from 'kysely';
54
54
  import { randomBytes } from 'node:crypto';
55
55
  import pg from 'pg';
56
56
  import * as config from './config.js';
57
- import { _fixOutput, run } from './io.js';
57
+ import { _fixOutput, run, someWarnings } from './io.js';
58
58
  export let database;
59
59
  export function connect() {
60
60
  if (database)
@@ -86,7 +86,7 @@ export async function statusText() {
86
86
  throw typeof error == 'object' && 'message' in error ? error.message : error;
87
87
  }
88
88
  }
89
- function shouldRecreate(opt) {
89
+ export function shouldRecreate(opt) {
90
90
  if (opt.skip) {
91
91
  opt.output('warn', 'already exists. (skipped)\n');
92
92
  return true;
@@ -107,6 +107,7 @@ export async function init(opt) {
107
107
  opt.output('debug', 'Generated password and wrote to global config');
108
108
  }
109
109
  const _sql = (command, message) => run(opt, message, `sudo -u postgres psql -c "${command}"`);
110
+ const relationExists = someWarnings(opt.output, [/relation "\w+" already exists/, 'already exists.']);
110
111
  await _sql('CREATE DATABASE axium', 'Creating database').catch(async (error) => {
111
112
  if (error != 'database "axium" already exists')
112
113
  throw error;
@@ -130,13 +131,6 @@ export async function init(opt) {
130
131
  await _sql('ALTER DATABASE axium OWNER TO axium', 'Setting database owner');
131
132
  await _sql('SELECT pg_reload_conf()', 'Reloading configuration');
132
133
  const db = __addDisposableResource(env_1, connect(), true);
133
- const relationExists = (table) => (error) => {
134
- error = typeof error == 'object' && 'message' in error ? error.message : error;
135
- if (error == `relation "${table}" already exists`)
136
- opt.output('warn', 'already exists.');
137
- else
138
- throw error;
139
- };
140
134
  opt.output('start', 'Creating table User');
141
135
  await db.schema
142
136
  .createTable('User')
@@ -149,7 +143,7 @@ export async function init(opt) {
149
143
  .addColumn('salt', 'text')
150
144
  .addColumn('preferences', 'jsonb', col => col.notNull().defaultTo(sql `'{}'::jsonb`))
151
145
  .execute()
152
- .catch(relationExists('User'));
146
+ .catch(relationExists);
153
147
  opt.output('done');
154
148
  opt.output('start', 'Creating table Account');
155
149
  await db.schema
@@ -167,10 +161,10 @@ export async function init(opt) {
167
161
  .addColumn('id_token', 'text')
168
162
  .addColumn('session_state', 'text')
169
163
  .execute()
170
- .catch(relationExists('Account'));
164
+ .catch(relationExists);
171
165
  opt.output('done');
172
166
  opt.output('start', 'Creating index for Account.userId');
173
- db.schema.createIndex('Account_userId_index').on('Account').column('userId').execute().catch(relationExists('Account_userId_index'));
167
+ db.schema.createIndex('Account_userId_index').on('Account').column('userId').execute().catch(relationExists);
174
168
  opt.output('done');
175
169
  opt.output('start', 'Creating table Session');
176
170
  await db.schema
@@ -180,10 +174,10 @@ export async function init(opt) {
180
174
  .addColumn('sessionToken', 'text', col => col.notNull().unique())
181
175
  .addColumn('expires', 'timestamptz', col => col.notNull())
182
176
  .execute()
183
- .catch(relationExists('Session'));
177
+ .catch(relationExists);
184
178
  opt.output('done');
185
179
  opt.output('start', 'Creating index for Session.userId');
186
- db.schema.createIndex('Session_userId_index').on('Session').column('userId').execute().catch(relationExists('Session_userId_index'));
180
+ db.schema.createIndex('Session_userId_index').on('Session').column('userId').execute().catch(relationExists);
187
181
  opt.output('done');
188
182
  opt.output('start', 'Creating table VerificationToken');
189
183
  await db.schema
@@ -192,7 +186,7 @@ export async function init(opt) {
192
186
  .addColumn('token', 'text', col => col.notNull().unique())
193
187
  .addColumn('expires', 'timestamptz', col => col.notNull())
194
188
  .execute()
195
- .catch(relationExists('VerificationToken'));
189
+ .catch(relationExists);
196
190
  opt.output('done');
197
191
  opt.output('start', 'Creating table Authenticator');
198
192
  await db.schema
@@ -206,10 +200,10 @@ export async function init(opt) {
206
200
  .addColumn('credentialBackedUp', 'boolean', col => col.notNull())
207
201
  .addColumn('transports', 'text')
208
202
  .execute()
209
- .catch(relationExists('Authenticator'));
203
+ .catch(relationExists);
210
204
  opt.output('done');
211
205
  opt.output('start', 'Creating index for Authenticator.credentialID');
212
- db.schema.createIndex('Authenticator_credentialID_key').on('Authenticator').column('credentialID').execute().catch(relationExists('Authenticator_credentialID_key'));
206
+ db.schema.createIndex('Authenticator_credentialID_key').on('Authenticator').column('credentialID').execute().catch(relationExists);
213
207
  opt.output('done');
214
208
  return config.db;
215
209
  }
package/dist/io.d.ts CHANGED
@@ -64,3 +64,9 @@ export interface PortOptions extends MaybeOutput {
64
64
  * If the origin has a port, passkeys do not work correctly with some password managers.
65
65
  */
66
66
  export declare function restrictedPorts(opt: PortOptions): Promise<void>;
67
+ /**
68
+ * This is a factory for handling errors when performing operations.
69
+ * The handler will allow the parent scope to continue if a relation already exists,
70
+ * rather than fatally exiting.
71
+ */
72
+ export declare function someWarnings(output: Output, ...allowList: [RegExp, string?][]): (error: string | Error) => void;
package/dist/io.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Logger } from 'logzen';
2
- import { exec, execSync } from 'node:child_process';
2
+ import { exec } from 'node:child_process';
3
3
  import * as fs from 'node:fs';
4
4
  import { homedir } from 'node:os';
5
5
  import { join } from 'node:path/posix';
@@ -167,3 +167,18 @@ export async function restrictedPorts(opt) {
167
167
  }
168
168
  }
169
169
  }
170
+ /**
171
+ * This is a factory for handling errors when performing operations.
172
+ * The handler will allow the parent scope to continue if a relation already exists,
173
+ * rather than fatally exiting.
174
+ */
175
+ export function someWarnings(output, ...allowList) {
176
+ return (error) => {
177
+ error = typeof error == 'object' && 'message' in error ? error.message : error;
178
+ for (const [pattern, message = error] of allowList) {
179
+ if (pattern.test(error))
180
+ output('warn', message);
181
+ }
182
+ throw error;
183
+ };
184
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axium/server",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "author": "James Prevett <axium@jamespre.dev> (https://jamespre.dev)",
5
5
  "funding": {
6
6
  "type": "individual",