From d0152617de33af3ba8b7589be4dd4e412eeda0c3 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Wed, 12 Jun 2024 11:09:52 +0200 Subject: [PATCH] fix(analytics): setup analytics worker (#162) --- src/features.ts | 19 ++++++--------- src/module.ts | 4 +++- .../analytics/server/utils/analytics.ts | 24 +++++++++++++++---- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/features.ts b/src/features.ts index d7e9ad7e..5ca695d4 100644 --- a/src/features.ts +++ b/src/features.ts @@ -8,6 +8,7 @@ import { $fetch } from 'ofetch' import { addDevtoolsCustomTabs } from './utils/devtools' const log = logger.withTag('nuxt:hub') +const { resolve } = createResolver(import.meta.url) export interface HubConfig { remote: string | boolean @@ -34,8 +35,6 @@ export interface HubConfig { } export function setupBase(nuxt: Nuxt, hub: HubConfig) { - const { resolve } = createResolver(import.meta.url) - // Add Server scanning addServerScanDir(resolve('./runtime/base/server')) addServerImportsDir(resolve('./runtime/base/server/utils')) @@ -46,9 +45,13 @@ export function setupBase(nuxt: Nuxt, hub: HubConfig) { } } -export function setupBlob(_nuxt: Nuxt) { - const { resolve } = createResolver(import.meta.url) +export function setupAnalytics(_nuxt: Nuxt) { + // Add Server scanning + addServerScanDir(resolve('./runtime/analytics/server')) + addServerImportsDir(resolve('./runtime/analytics/server/utils')) +} +export function setupBlob(_nuxt: Nuxt) { // Add Server scanning addServerScanDir(resolve('./runtime/blob/server')) addServerImportsDir(resolve('./runtime/blob/server/utils')) @@ -58,8 +61,6 @@ export function setupBlob(_nuxt: Nuxt) { } export function setupCache(nuxt: Nuxt) { - const { resolve } = createResolver(import.meta.url) - // Add Server caching (Nitro) nuxt.options.nitro = defu(nuxt.options.nitro, { storage: { @@ -82,24 +83,18 @@ export function setupCache(nuxt: Nuxt) { } export function setupDatabase(_nuxt: Nuxt) { - const { resolve } = createResolver(import.meta.url) - // Add Server scanning addServerScanDir(resolve('./runtime/database/server')) addServerImportsDir(resolve('./runtime/database/server/utils')) } export function setupKV(_nuxt: Nuxt) { - const { resolve } = createResolver(import.meta.url) - // Add Server scanning addServerScanDir(resolve('./runtime/kv/server')) addServerImportsDir(resolve('./runtime/kv/server/utils')) } export function setupOpenAPI(nuxt: Nuxt) { - const { resolve } = createResolver(import.meta.url) - // Fallback to custom placeholder when openAPI is disabled nuxt.options.alias['#hub/openapi'] = nuxt.options.nitro?.experimental?.openAPI === true ? '#internal/nitro/routes/openapi' diff --git a/src/module.ts b/src/module.ts index 830e4ddb..803d94ef 100644 --- a/src/module.ts +++ b/src/module.ts @@ -7,7 +7,7 @@ import { findWorkspaceDir } from 'pkg-types' import { parseArgs } from 'citty' import { version } from '../package.json' import { generateWrangler } from './utils/wrangler' -import { setupCache, setupBlob, setupOpenAPI, setupDatabase, setupKV, setupBase, setupRemote } from './features' +import { setupCache, setupAnalytics, setupBlob, setupOpenAPI, setupDatabase, setupKV, setupBase, setupRemote } from './features' import type { ModuleOptions } from './types/module' export * from './types' @@ -74,6 +74,7 @@ export default defineNuxtModule({ setupBase(nuxt, hub) setupOpenAPI(nuxt) + hub.analytics && setupAnalytics(nuxt) hub.blob && setupBlob(nuxt) hub.cache && setupCache(nuxt) hub.database && setupDatabase(nuxt) @@ -91,6 +92,7 @@ export default defineNuxtModule({ // Folowing lines are only executed when remove storage is disabled + // Production mode without remote storage if (!nuxt.options.dev) { // Make sure to fallback to cloudflare-pages preset let preset = nuxt.options.nitro.preset = nuxt.options.nitro.preset || 'cloudflare-pages' diff --git a/src/runtime/analytics/server/utils/analytics.ts b/src/runtime/analytics/server/utils/analytics.ts index c3937175..352417f8 100644 --- a/src/runtime/analytics/server/utils/analytics.ts +++ b/src/runtime/analytics/server/utils/analytics.ts @@ -7,24 +7,38 @@ import { useRuntimeConfig } from '#imports' const _datasets: Record = {} -function getAnalyticsBinding(name: string = 'ANALYTICS') { +function getAnalyticsBinding(name: string = 'ANALYTICS'): AnalyticsEngineDataset | undefined { // @ts-expect-error globalThis.__env__ is injected by the runtime return process.env[name] || globalThis.__env__?.[name] || globalThis[name] } -function _useDataset(name: string = 'ANALYTICS') { +function _useDataset(name: string = 'ANALYTICS'): AnalyticsEngineDataset { if (_datasets[name]) { - return _datasets[name] + return _datasets[name] as AnalyticsEngineDataset } const binding = getAnalyticsBinding() if (binding) { - _datasets[name] = binding as AnalyticsEngineDataset - return _datasets[name] + _datasets[name] = binding + return _datasets[name] as AnalyticsEngineDataset } throw createError(`Missing Cloudflare ${name} binding (Analytics Engine)`) } +/** + * Access the Workers Analytics Engine + * + * @example ```ts + * const analytics = useAnalytics() + * await analytics.put({ + * blobs: ['Seattle', 'USA', 'pro_sensor_9000'], + * doubles: [1.1, 2.2, 3.3], + * indexes: ['a3cd45'] + * }) + * ``` + * + * @see https://developers.cloudflare.com/analytics/analytics-engine/get-started/ + */ export function hubAnalytics() { requireNuxtHubFeature('analytics')