Skip to content

Commit 6332a33

Browse files
committed
fix: dag-json encode egress events
1 parent 28dc194 commit 6332a33

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/middleware/withEgressTracker.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { Space } from '@storacha/capabilities'
88
import { SpaceDID } from '@storacha/capabilities/utils'
99
import { DID } from '@ucanto/core'
10+
import * as dagJSON from '@ipld/dag-json'
1011

1112
/**
1213
* The egress tracking handler must be enabled after the rate limiting, authorized space,
@@ -83,11 +84,11 @@ export function withEgressTracker (handler) {
8384

8485
// Non-blocking call to queue the invocation
8586
ctx.waitUntil(
86-
env.EGRESS_QUEUE.send({
87+
env.EGRESS_QUEUE.send(dagJSON.encode({
8788
messageId: delegation.cid,
8889
invocation: serializedInvocation,
8990
timestamp: Date.now()
90-
})
91+
}))
9192
)
9293
} catch (error) {
9394
console.error('Failed to create or queue egress invocation:', error)

test/unit/middleware/withEgressTracker.spec.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Builder, toBlobKey } from '../../helpers/builder.js'
1313
import { CARReaderStream } from 'carstream'
1414
import { SpaceDID } from '@storacha/capabilities/utils'
1515
import { ed25519 } from '@ucanto/principal'
16+
import * as dagJSON from '@ipld/dag-json'
1617

1718
/**
1819
* @typedef {import('../../../src/middleware/withEgressTracker.types.js').Environment} EgressTrackerEnvironment
@@ -145,7 +146,10 @@ describe('withEgressTracker', async () => {
145146
expect(queueSendMock.calledOnce, 'queue.send should be called once').to.be
146147
.true
147148
// Verify the queued invocation contains the expected data
148-
const queuedData = queueSendMock.args[0][0]
149+
// Queue receives DAG-JSON encoded data
150+
const queuedBytes = queueSendMock.args[0][0]
151+
expect(queuedBytes).to.be.instanceOf(Uint8Array)
152+
const queuedData = dagJSON.decode(queuedBytes)
149153
expect(queuedData).to.have.property('invocation')
150154
expect(queuedData.invocation).to.be.instanceOf(Uint8Array)
151155
})
@@ -175,7 +179,10 @@ describe('withEgressTracker', async () => {
175179
expect(queueSendMock.calledOnce, 'queue.send should be called once').to.be
176180
.true
177181
// Verify the queued invocation contains the expected data
178-
const queuedData = queueSendMock.args[0][0]
182+
// Queue receives DAG-JSON encoded data
183+
const queuedBytes = queueSendMock.args[0][0]
184+
expect(queuedBytes).to.be.instanceOf(Uint8Array)
185+
const queuedData = dagJSON.decode(queuedBytes)
179186
expect(queuedData).to.have.property('invocation')
180187
expect(queuedData.invocation).to.be.instanceOf(Uint8Array)
181188
})
@@ -209,7 +216,10 @@ describe('withEgressTracker', async () => {
209216
expect(queueSendMock.calledOnce, 'queue.send should be called once').to.be
210217
.true
211218
// Verify the queued invocation contains the expected data
212-
const queuedData = queueSendMock.args[0][0]
219+
// Queue receives DAG-JSON encoded data
220+
const queuedBytes = queueSendMock.args[0][0]
221+
expect(queuedBytes).to.be.instanceOf(Uint8Array)
222+
const queuedData = dagJSON.decode(queuedBytes)
213223
expect(queuedData).to.have.property('invocation')
214224
expect(queuedData.invocation).to.be.instanceOf(Uint8Array)
215225
})
@@ -268,7 +278,10 @@ describe('withEgressTracker', async () => {
268278
expect(queueSendMock.calledOnce, 'queue.send should be called once').to.be
269279
.true
270280
// Verify the queued invocation contains the expected data
271-
const queuedData = queueSendMock.args[0][0]
281+
// Queue receives DAG-JSON encoded data
282+
const queuedBytes = queueSendMock.args[0][0]
283+
expect(queuedBytes).to.be.instanceOf(Uint8Array)
284+
const queuedData = dagJSON.decode(queuedBytes)
272285
expect(queuedData).to.have.property('invocation')
273286
expect(queuedData.invocation).to.be.instanceOf(Uint8Array)
274287
})
@@ -302,7 +315,10 @@ describe('withEgressTracker', async () => {
302315
expect(queueSendMock.calledOnce, 'queue.send should be called once').to.be
303316
.true
304317
// Verify the queued invocation contains the expected data
305-
const queuedData = queueSendMock.args[0][0]
318+
// Queue receives DAG-JSON encoded data
319+
const queuedBytes = queueSendMock.args[0][0]
320+
expect(queuedBytes).to.be.instanceOf(Uint8Array)
321+
const queuedData = dagJSON.decode(queuedBytes)
306322
expect(queuedData).to.have.property('invocation')
307323
expect(queuedData.invocation).to.be.instanceOf(Uint8Array)
308324
}).timeout(5000)
@@ -412,11 +428,15 @@ describe('withEgressTracker', async () => {
412428
2
413429
)
414430
// Verify both queued invocations
415-
const queuedData1 = queueSendMock.args[0][0]
431+
const queuedBytes1 = queueSendMock.args[0][0]
432+
expect(queuedBytes1).to.be.instanceOf(Uint8Array)
433+
const queuedData1 = dagJSON.decode(queuedBytes1)
416434
expect(queuedData1).to.have.property('invocation')
417435
expect(queuedData1.invocation).to.be.instanceOf(Uint8Array)
418436

419-
const queuedData2 = queueSendMock.args[1][0]
437+
const queuedBytes2 = queueSendMock.args[1][0]
438+
expect(queuedBytes2).to.be.instanceOf(Uint8Array)
439+
const queuedData2 = dagJSON.decode(queuedBytes2)
420440
expect(queuedData2).to.have.property('invocation')
421441
expect(queuedData2.invocation).to.be.instanceOf(Uint8Array)
422442
})

0 commit comments

Comments
 (0)