Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Clean up extensions approach #274

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/worker/vm/extensions/google.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as BigQuery from '@google-cloud/bigquery'

type DummyCloud = {
export interface DummyCloud {
bigquery: typeof BigQuery
}

type DummyGoogle = {
export interface DummyGoogle {
cloud: DummyCloud
}

Expand Down
29 changes: 29 additions & 0 deletions src/worker/vm/extensions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CacheExtension, ConsoleExtension, StorageExtension } from '@posthog/plugin-scaffold'
import fetch, { RequestInfo, RequestInit, Response } from 'node-fetch'

import { PluginConfig, PluginsServer } from '../../../types'
import { createCache } from './cache'
import { createConsole } from './console'
import { createGoogle, DummyGoogle } from './google'
import { createPosthog, DummyPostHog } from './posthog'
import { createStorage } from './storage'

export interface Extensions {
fetch: (url: RequestInfo, init?: RequestInit) => Promise<Response>
cache: CacheExtension
console: ConsoleExtension
google: DummyGoogle
posthog: DummyPostHog
storage: StorageExtension
}

export function createExtensions(server: PluginsServer, pluginConfig: PluginConfig): Extensions {
return {
fetch: async (url, init) => await fetch(url, init),
cache: createCache(server, pluginConfig.plugin_id, pluginConfig.team_id),
console: createConsole(),
google: createGoogle(),
posthog: createPosthog(server, pluginConfig),
storage: createStorage(server, pluginConfig),
}
}
15 changes: 6 additions & 9 deletions src/worker/vm/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from 'node-fetch'
import { VM } from 'vm2'

import { PluginConfig, PluginConfigVMReponse, PluginsServer } from '../../types'
import { createExtensions } from './extensions'
import { createCache } from './extensions/cache'
import { createConsole } from './extensions/console'
import { createGoogle } from './extensions/google'
Expand All @@ -23,13 +24,10 @@ export async function createPluginConfigVM(
sandbox: {},
})

// Add PostHog utilities to virtual machine
vm.freeze(createConsole(), 'console')
vm.freeze(createPosthog(server, pluginConfig), 'posthog')

// Add non-PostHog utilities to virtual machine
vm.freeze(fetch, 'fetch')
vm.freeze(createGoogle(), 'google')
const extensions = createExtensions(server, pluginConfig)
for (const [key, extension] of Object.entries(extensions)) {
vm.freeze(extension, key) // Inject extensions as globals
}

if (process.env.NODE_ENV === 'test') {
vm.freeze(setTimeout, '__jestSetTimeout')
Expand Down Expand Up @@ -57,10 +55,9 @@ export async function createPluginConfigVM(

vm.freeze(
{
cache: createCache(server, pluginConfig.plugin_id, pluginConfig.team_id),
config: pluginConfig.config,
attachments: pluginConfig.attachments,
storage: createStorage(server, pluginConfig),
...extensions, // Inject extensions into meta
},
'__pluginHostMeta'
)
Expand Down
4 changes: 4 additions & 0 deletions tests/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ test('plugin meta has what it should have', async () => {
'attachments',
'cache',
'config',
'console',
'fetch',
'global',
'google',
'posthog',
'storage',
])
expect(returnedEvent!.properties!['attachments']).toEqual({
Expand Down
2 changes: 1 addition & 1 deletion tests/postgres/vm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ test('fetch', async () => {
}

await vm.methods.processEvent(event)
expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=fetched')
expect(fetch).toHaveBeenCalledWith('https://google.com/results.json?query=fetched', undefined)

expect(event.properties).toEqual({ count: 2, query: 'bla', results: [true, true] })
})
Expand Down