Skip to content

Commit

Permalink
Merge pull request #3 from fumeapp/add-hash-utils
Browse files Browse the repository at this point in the history
✨ move our hash utils to the module
  • Loading branch information
acidjazz authored Jan 26, 2025
2 parents b8a045d + 0ed1226 commit 165c8bd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineNuxtModule, createResolver, addServerImportsDir } from '@nuxt/kit'
import { defineNuxtModule, createResolver, addImports, addServerImportsDir } from '@nuxt/kit'

// Module options TypeScript interface definition
export interface ModuleOptions {
Expand All @@ -15,6 +15,12 @@ export default defineNuxtModule<ModuleOptions>({
const resolver = createResolver(import.meta.url)
nuxt.options.alias['#api-utils'] = resolver.resolve('./runtime/types/index.d.ts')

const composables = [
{ name: 'useApiFetch', from: resolver.resolve('./runtime/app/composables/apiFetch') },
]

addImports(composables)

addServerImportsDir(resolver.resolve('./runtime/server/utils'))
},
})
1 change: 1 addition & 0 deletions src/runtime/app/composables/apiFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const useApiFetch = $fetch.create({})
2 changes: 2 additions & 0 deletions src/runtime/server/utils/authorize.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createError } from 'h3'

type PolicyFunction<T extends object> = (args: T) => boolean | Promise<boolean>

export async function authorize<T extends object>(
Expand Down
56 changes: 56 additions & 0 deletions src/runtime/server/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { randomBytes } from 'node:crypto'

/**
* Generate a unique hash from a string
* @param payload - The string to hash
*/
export async function hashFromPayload(payload: string): Promise<string> {
const encoder = new TextEncoder()
const data = encoder.encode(payload)
const hashBuffer = await crypto.subtle.digest('SHA-256', data)
const hashArray = Array.from(new Uint8Array(hashBuffer))
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
}

/**
* Generate a unique hash from a file
* @param file
*/
export async function hashFromFile(file: File) {
return await crypto.subtle
.digest('SHA-256', await file.arrayBuffer())
.then(buf => Array.from(new Uint8Array(buf)).map(b => b.toString(16).padStart(2, '0')).join(''))
}

/**
* Generates a hash from the content of a URL
* Returns the hash and the file a Blob
* @param url
*/

export async function hashFromUrl(url: string): Promise<{ imageHash: string, imageFile: File }> {
const response = await fetch(url)
const imageBlob = await response.blob()
const imageFile = new File([imageBlob], 'avatar', { type: imageBlob.type })
const imageHash = await hashFromFile(imageFile)
return { imageHash, imageFile }
}

/**
* Generates a random hash from a set of characters
* @param length The length of the hash
* @param lower Whether to use only lowercase characters
*/
export function hashGenerate(length: number = 32, lower = false): string {
const charsetAll = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const charsetLower = 'abcdefghijklmnopqrstuvwxyz0123456789'
const charset = lower ? charsetLower : charsetAll
const bytes = randomBytes(length)
let result = ''

for (let i = 0; i < length; i++) {
result += charset[bytes[i] % charset.length]
}

return result
}

0 comments on commit 165c8bd

Please sign in to comment.