@axium/storage 0.2.1 → 0.2.2

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.
package/dist/server.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type Schema } from '@axium/server/database';
2
- import type { ExpressionBuilder, Generated, Selectable } from 'kysely';
2
+ import type { Generated, Selectable } from 'kysely';
3
3
  import type { StorageItemMetadata, StorageLimits, StorageUsage } from './common.js';
4
4
  import './polyfills.js';
5
5
  declare module '@axium/server/database' {
@@ -53,15 +53,12 @@ declare module '@axium/server/config' {
53
53
  export interface StorageItem extends StorageItemMetadata {
54
54
  data: Uint8Array<ArrayBufferLike>;
55
55
  }
56
- export declare function parseItem<T extends Record<string, unknown> = Record<string, unknown>>(item: Selectable<Schema['storage']> & {
57
- hash: string;
58
- }): StorageItemMetadata<T>;
56
+ export declare function parseItem<T extends Record<string, unknown> = Record<string, unknown>>(item: Selectable<Schema['storage']>): StorageItemMetadata<T>;
59
57
  /**
60
58
  * Returns the current usage of the storage for a user in bytes.
61
59
  */
62
60
  export declare function currentUsage(userId: string): Promise<StorageUsage>;
63
61
  export declare function get<T extends Record<string, unknown> = Record<string, unknown>>(itemId: string): Promise<StorageItemMetadata<T>>;
64
- export declare function withEncodedHash(eb: ExpressionBuilder<Schema, 'storage'>): import("kysely").AliasedExpression<string, "hash">;
65
62
  export type ExternalLimitHandler = (userId?: string) => StorageLimits | Promise<StorageLimits>;
66
63
  /**
67
64
  * Define the handler to get limits for a user externally.
package/dist/server.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Permission } from '@axium/core';
1
+ import { Permission } from '@axium/core/access';
2
2
  import { checkAuthForItem, checkAuthForUser, getSessionAndUser } from '@axium/server/auth';
3
3
  import { addConfigDefaults, config } from '@axium/server/config';
4
4
  import { connect, database, expectedTypes } from '@axium/server/database';
@@ -53,6 +53,7 @@ export function parseItem(item) {
53
53
  ...item,
54
54
  metadata: item.metadata,
55
55
  dataURL: `/raw/storage/${item.id}`,
56
+ hash: item.hash.toHex(),
56
57
  };
57
58
  }
58
59
  /**
@@ -74,13 +75,10 @@ export async function get(itemId) {
74
75
  .selectFrom('storage')
75
76
  .where('id', '=', itemId)
76
77
  .selectAll()
77
- .select(withEncodedHash)
78
+ .$narrowType()
78
79
  .executeTakeFirstOrThrow();
79
80
  return parseItem(result);
80
81
  }
81
- export function withEncodedHash(eb) {
82
- return eb.fn('encode', ['hash', eb.val('hex')]).as('hash');
83
- }
84
82
  let _getLimits = null;
85
83
  /**
86
84
  * Define the handler to get limits for a user externally.
@@ -104,7 +102,7 @@ addRoute({
104
102
  error(503, 'User storage is disabled');
105
103
  const itemId = event.params.id;
106
104
  const { item } = await checkAuthForItem(event, 'storage', itemId, Permission.Read);
107
- return item;
105
+ return parseItem(item);
108
106
  },
109
107
  async PATCH(event) {
110
108
  if (!config.storage.enabled)
@@ -128,7 +126,6 @@ addRoute({
128
126
  .where('id', '=', itemId)
129
127
  .set(values)
130
128
  .returningAll()
131
- .returning(withEncodedHash)
132
129
  .executeTakeFirstOrThrow()
133
130
  .catch(withError('Could not update item')));
134
131
  },
@@ -136,7 +133,8 @@ addRoute({
136
133
  if (!config.storage.enabled)
137
134
  error(503, 'User storage is disabled');
138
135
  const itemId = event.params.id;
139
- const { item } = await checkAuthForItem(event, 'storage', itemId, Permission.Manage);
136
+ const auth = await checkAuthForItem(event, 'storage', itemId, Permission.Manage);
137
+ const item = parseItem(auth.item);
140
138
  await database
141
139
  .deleteFrom('storage')
142
140
  .where('id', '=', itemId)
@@ -168,7 +166,6 @@ addRoute({
168
166
  .where('parentId', '=', itemId)
169
167
  .where('trashedAt', '!=', null)
170
168
  .selectAll()
171
- .select(withEncodedHash)
172
169
  .execute();
173
170
  return items.map(parseItem);
174
171
  },
@@ -217,7 +214,6 @@ addRoute({
217
214
  .insertInto('storage')
218
215
  .values({ userId: userId, hash, name, size, type, immutable: useCAS, parentId })
219
216
  .returningAll()
220
- .returning(withEncodedHash)
221
217
  .executeTakeFirstOrThrow()
222
218
  .catch(withError('Could not create item'));
223
219
  const path = join(config.storage.data, result.id);
@@ -289,7 +285,6 @@ addRoute({
289
285
  .where('id', '=', itemId)
290
286
  .set({ size, modifiedAt: new Date(), hash })
291
287
  .returningAll()
292
- .returning(withEncodedHash)
293
288
  .executeTakeFirstOrThrow()
294
289
  .catch(withError('Could not update item'));
295
290
  await writeFile(join(config.storage.data, result.id), content).catch(withError('Could not write'));
@@ -313,7 +308,7 @@ addRoute({
313
308
  const userId = event.params.id;
314
309
  await checkAuthForUser(event, userId);
315
310
  const [items, usage, limits] = await Promise.all([
316
- database.selectFrom('storage').where('userId', '=', userId).selectAll().select(withEncodedHash).execute(),
311
+ database.selectFrom('storage').where('userId', '=', userId).selectAll().execute(),
317
312
  currentUsage(userId),
318
313
  getLimits(userId),
319
314
  ]).catch(withError('Could not fetch data'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axium/storage",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "author": "James Prevett <axium@jamespre.dev> (https://jamespre.dev)",
5
5
  "description": "User file storage for Axium",
6
6
  "funding": {