-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #751 from cloudflare/jspspike/analytics-engine
Add AnalyticsEngine binding type to workerd
- Loading branch information
Showing
6 changed files
with
159 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as assert from 'node:assert'; | ||
|
||
let written = false; | ||
async function isWritten(timeout) { | ||
const start = Date.now(); | ||
do { | ||
if (written) return true; | ||
await scheduler.wait(100); | ||
} while ((Date.now() - start) < timeout); | ||
throw new Error("Test never received request from analytics engine handler"); | ||
} | ||
|
||
export default { | ||
async fetch(ctrl, env, ctx) { | ||
written = true | ||
return new Response(""); | ||
}, | ||
async test(ctrl, env, ctx) { | ||
env.aebinding.writeDataPoint({ | ||
'blobs': ["TestBlob"], | ||
'doubles': [25], | ||
'indexes': ["testindex"], | ||
}); | ||
|
||
assert.equal(await isWritten(5000), true); | ||
return new Response(""); | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Workerd = import "/workerd/workerd.capnp"; | ||
|
||
const analyticsWorker :Workerd.Worker = ( | ||
modules = [ | ||
(name = "worker", esModule = | ||
`import * as assert from 'node:assert'; | ||
`export default { | ||
` async fetch(request, env, ctx) { | ||
` let val = await request.json(); | ||
` console.log(JSON.stringify(val)); | ||
` assert.deepStrictEqual(val['dataset'], [97,110,97,108,121,116,105,99,115]); | ||
` assert.deepStrictEqual(val['double1'], 25); | ||
` assert.deepStrictEqual(val['blob1'], [84,101,115,116,66,108,111,98]); | ||
` await env.main.fetch("http://w/"); | ||
` return new Response(''); | ||
` }, | ||
`}; | ||
), | ||
], | ||
compatibilityFlags = ["experimental", "nodejs_compat"], | ||
compatibilityDate = "2023-02-28", | ||
bindings = [ ( name = "main", service = "main" ) ] | ||
); | ||
|
||
const mainWorker :Workerd.Worker = ( | ||
modules = [ | ||
(name = "worker", esModule = embed "analytics-engine-test.js"), | ||
], | ||
compatibilityFlags = ["experimental", "nodejs_compat"], | ||
compatibilityDate = "2023-02-28", | ||
|
||
bindings = [ ( name = "aebinding", analyticsEngine = "analytics") ] | ||
); | ||
|
||
const unitTests :Workerd.Config = ( | ||
services = [ (name = "main", worker = .mainWorker), (name = "analytics", worker = .analyticsWorker) ], | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters