Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow collecting signals even if edge function is not defined. #1112

Merged
merged 3 commits into from
Jul 18, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/yellow-camels-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-signals': patch
---

Allow collecting signals from sources without an edge function written yet
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { SandboxSettings, SandboxSettingsConfig } from '../sandbox'

describe(SandboxSettings, () => {
const edgeFnResponseBody = `function processSignal() { console.log('hello world') }`
const baseSettings: SandboxSettingsConfig = {
functionHost: undefined,
processSignal: undefined,
edgeFnDownloadURL: 'http://example.com/download',
edgeFnFetchClient: jest.fn().mockReturnValue(
Promise.resolve({
text: () => edgeFnResponseBody,
})
),
}
test('initializes with provided settings', async () => {
const sandboxSettings = new SandboxSettings({ ...baseSettings })
expect(baseSettings.edgeFnFetchClient).toHaveBeenCalledWith(
baseSettings.edgeFnDownloadURL
)
expect(await sandboxSettings.processSignal).toEqual(edgeFnResponseBody)
})

test('normalizes edgeFnDownloadURL when functionHost is provided', async () => {
const settings: SandboxSettingsConfig = {
...baseSettings,
processSignal: undefined,
functionHost: 'newHost.com',
edgeFnDownloadURL: 'https://original.com/download',
}
new SandboxSettings(settings)
expect(baseSettings.edgeFnFetchClient).toHaveBeenCalledWith(
'https://newHost.com/download'
)
})

test('creates default processSignal when parameters are missing', async () => {
const consoleWarnSpy = jest
.spyOn(console, 'warn')
.mockImplementation(() => {})
const settings: SandboxSettingsConfig = {
...baseSettings,
processSignal: undefined,
edgeFnDownloadURL: undefined,
}
const sandboxSettings = new SandboxSettings(settings)
expect(await sandboxSettings.processSignal).toEqual(
'globalThis.processSignal = function processSignal() {}'
)
expect(baseSettings.edgeFnFetchClient).not.toHaveBeenCalled()
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('processSignal')
)
})
})
9 changes: 8 additions & 1 deletion packages/signals/signals/src/core/processor/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,14 @@ export class SandboxSettings {
: settings.edgeFnDownloadURL

if (!edgeFnDownloadURLNormalized && !settings.processSignal) {
throw new Error('edgeFnDownloadURL or processSignal must be defined')
// user may be onboarding and not have written a signal -- so do a noop so we can collect signals
this.processSignal = Promise.resolve(
`globalThis.processSignal = function processSignal() {}`
)
console.warn(
`No processSignal function found. Have you written a processSignal function on app.segment.com?`
)
return
}

const fetch = settings.edgeFnFetchClient ?? globalThis.fetch
Expand Down
Loading