Skip to content

Commit

Permalink
fix(analytics): setup analytics worker (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz authored Jun 12, 2024
1 parent 712952a commit d015261
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
19 changes: 7 additions & 12 deletions src/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'))
Expand All @@ -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'))
Expand All @@ -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: {
Expand All @@ -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'
Expand Down
4 changes: 3 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -74,6 +74,7 @@ export default defineNuxtModule<ModuleOptions>({

setupBase(nuxt, hub)
setupOpenAPI(nuxt)
hub.analytics && setupAnalytics(nuxt)
hub.blob && setupBlob(nuxt)
hub.cache && setupCache(nuxt)
hub.database && setupDatabase(nuxt)
Expand All @@ -91,6 +92,7 @@ export default defineNuxtModule<ModuleOptions>({

// 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'
Expand Down
24 changes: 19 additions & 5 deletions src/runtime/analytics/server/utils/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,38 @@ import { useRuntimeConfig } from '#imports'

const _datasets: Record<string, AnalyticsEngineDataset> = {}

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')

Expand Down

0 comments on commit d015261

Please sign in to comment.