Skip to content

Commit

Permalink
feat: Make Vault NuxtHub compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoRCD committed Jan 26, 2025
1 parent eedb016 commit e9a57c7
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 271 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,5 @@ dist

# Turborepo
.turbo

.data
2 changes: 2 additions & 0 deletions apps/vault/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ logs
.env
.env.*
!.env.example

.wrangler
11 changes: 6 additions & 5 deletions apps/vault/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ export default defineNuxtConfig({
compatibilityDate: '2024-11-01',

routeRules: {
'/': { isr: true, prerender: true }
'/': { prerender: true }
},

modules: ['@nuxt/scripts'],
modules: ['@nuxt/scripts', '@nuxthub/core'],

runtimeConfig: {
private: {
encryptionKey: '',
vault: {
url: ''
},
},
},

hub: {
kv: true,
},

devtools: { enabled: true },

future: {
Expand Down
4 changes: 4 additions & 0 deletions apps/vault/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@nuxthub/core": "0.8.14",
"nuxt": "^3.15.3",
"vue": "latest"
},
"devDependencies": {
"wrangler": "^3.105.1"
}
}
12 changes: 0 additions & 12 deletions apps/vault/server/plugins/storage.ts

This file was deleted.

21 changes: 11 additions & 10 deletions apps/vault/server/services/vault.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { Storage, StorageValue } from 'unstorage'
import type { H3Event } from 'h3'
import type { DecryptResponse, EncryptRequest, StoredData, TTLFormat } from '../../../../packages/types'

export class VaultService {

private readonly storage: Storage<StorageValue>
private readonly encryptionKey: string
private readonly siteUrl: string
private readonly PREFIX = 'vault:'
private readonly PREFIX = 'cache:'

private readonly TTL_MAP = {
'1d': 24 * 60 * 60, // 1 day in seconds
Expand All @@ -20,7 +18,6 @@ export class VaultService {
const url = getRequestURL(event)
this.encryptionKey = config.private.encryptionKey
this.siteUrl = url.origin
this.storage = useStorage('vault')
}

private generateKey(id: string): string {
Expand Down Expand Up @@ -67,7 +64,7 @@ export class VaultService {

async decrypt(id: string): Promise<DecryptResponse> {
const key = this.generateKey(id)
const storedData = await this.storage.getItem<StoredData>(key)
const storedData = await hubKV().get<StoredData>(key)

if (!storedData) {
throw createError({
Expand All @@ -80,15 +77,15 @@ export class VaultService {
const timeLeft = this.calculateTimeLeft(createdAt, ttl)

if (timeLeft <= 0) {
await this.storage.removeItem(key)
await hubKV().del(key)
throw createError({
statusCode: 400,
statusMessage: 'Link has expired'
})
}

if (reads <= 0) {
await this.storage.removeItem(key)
await hubKV().del(key)
throw createError({
statusCode: 400,
statusMessage: 'Maximum number of reads reached'
Expand All @@ -98,13 +95,17 @@ export class VaultService {
const decryptedValue = await unseal(encryptedValue, this.encryptionKey) as string

const updatedReads = reads - 1
await this.storage.setItem(key, {
/*await this.storage.setItem(key, {
...storedData,
reads: updatedReads
})*/
await hubKV().set(key, {
...storedData,
reads: updatedReads
})

if (updatedReads === 0) {
await this.storage.removeItem(key)
await hubKV().del(key)
}

return {
Expand All @@ -126,7 +127,7 @@ export class VaultService {
ttl: data.ttl
}

await this.storage.setItem(key, storedData)
await hubKV().set(key, storedData)
return this.generateShareUrl(randomId)
}

Expand Down
Loading

0 comments on commit e9a57c7

Please sign in to comment.