Skip to content

Commit 1f68f0e

Browse files
authored
Allow collecting signals even if edge function is not defined. (#1112)
1 parent 44b5d06 commit 1f68f0e

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

.changeset/yellow-camels-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-signals': patch
3+
---
4+
5+
Allow collecting signals from sources without an edge function written yet
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { SandboxSettings, SandboxSettingsConfig } from '../sandbox'
2+
3+
describe(SandboxSettings, () => {
4+
const edgeFnResponseBody = `function processSignal() { console.log('hello world') }`
5+
const baseSettings: SandboxSettingsConfig = {
6+
functionHost: undefined,
7+
processSignal: undefined,
8+
edgeFnDownloadURL: 'http://example.com/download',
9+
edgeFnFetchClient: jest.fn().mockReturnValue(
10+
Promise.resolve({
11+
text: () => edgeFnResponseBody,
12+
})
13+
),
14+
}
15+
test('initializes with provided settings', async () => {
16+
const sandboxSettings = new SandboxSettings({ ...baseSettings })
17+
expect(baseSettings.edgeFnFetchClient).toHaveBeenCalledWith(
18+
baseSettings.edgeFnDownloadURL
19+
)
20+
expect(await sandboxSettings.processSignal).toEqual(edgeFnResponseBody)
21+
})
22+
23+
test('normalizes edgeFnDownloadURL when functionHost is provided', async () => {
24+
const settings: SandboxSettingsConfig = {
25+
...baseSettings,
26+
processSignal: undefined,
27+
functionHost: 'newHost.com',
28+
edgeFnDownloadURL: 'https://original.com/download',
29+
}
30+
new SandboxSettings(settings)
31+
expect(baseSettings.edgeFnFetchClient).toHaveBeenCalledWith(
32+
'https://newHost.com/download'
33+
)
34+
})
35+
36+
test('creates default processSignal when parameters are missing', async () => {
37+
const consoleWarnSpy = jest
38+
.spyOn(console, 'warn')
39+
.mockImplementation(() => {})
40+
const settings: SandboxSettingsConfig = {
41+
...baseSettings,
42+
processSignal: undefined,
43+
edgeFnDownloadURL: undefined,
44+
}
45+
const sandboxSettings = new SandboxSettings(settings)
46+
expect(await sandboxSettings.processSignal).toEqual(
47+
'globalThis.processSignal = function processSignal() {}'
48+
)
49+
expect(baseSettings.edgeFnFetchClient).not.toHaveBeenCalled()
50+
expect(consoleWarnSpy).toHaveBeenCalledWith(
51+
expect.stringContaining('processSignal')
52+
)
53+
})
54+
})

packages/signals/signals/src/core/processor/sandbox.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,14 @@ export class SandboxSettings {
145145
: settings.edgeFnDownloadURL
146146

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

151158
const fetch = settings.edgeFnFetchClient ?? globalThis.fetch

0 commit comments

Comments
 (0)