-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
142 additions
and
8 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,77 @@ | ||
import { DecodeFailure, EncodeFailure, Schema } from './lib.js' | ||
|
||
/** | ||
* @typedef {import('../lib/api').EgressEvent} EgressEvent | ||
* @typedef {import('../types').InferStoreRecord<EgressEvent> & { pk: string, sk: string }} EgressEventStoreRecord | ||
* @typedef {import('../types').StoreRecord} StoreRecord | ||
* @typedef {import('../lib/api').EgressEventListKey} EgressEventListKey | ||
* @typedef {{ pk: string, sk: string }} EgressEventListStoreRecord | ||
*/ | ||
|
||
export const egressSchema = Schema.struct({ | ||
customerId: Schema.did({ method: 'mailto' }), | ||
resourceId: Schema.text(), | ||
timestamp: Schema.date(), | ||
}) | ||
|
||
/** @type {import('../lib/api').Validator<EgressEvent>} */ | ||
export const validate = input => egressSchema.read(input) | ||
|
||
/** @type {import('../lib/api').Encoder<EgressEvent, EgressEventStoreRecord>} */ | ||
export const encode = input => { | ||
try { | ||
return { | ||
ok: { | ||
pk: `${input.timestamp.toISOString()}#${input.customerId}`, | ||
sk: `${input.timestamp.toISOString()}#${input.customerId}#${input.resourceId}`, | ||
customerId: input.customerId, | ||
resourceId: input.resourceId, | ||
timestamp: input.timestamp.toISOString(), | ||
} | ||
} | ||
} catch (/** @type {any} */ err) { | ||
return { | ||
error: new EncodeFailure(`encoding egress event: ${err.message}`, { cause: err }) | ||
} | ||
} | ||
} | ||
|
||
/** @type {import('../lib/api').Encoder<EgressEvent, string>} */ | ||
export const encodeStr = input => { | ||
try { | ||
const data = encode(input) | ||
if (data.error) throw data.error | ||
return { ok: JSON.stringify(data.ok) } | ||
} catch (/** @type {any} */ err) { | ||
return { | ||
error: new EncodeFailure(`encoding string egress event: ${err.message}`, { cause: err }) | ||
} | ||
} | ||
} | ||
|
||
/** @type {import('../lib/api').Decoder<StoreRecord, EgressEvent>} */ | ||
export const decode = input => { | ||
try { | ||
return { | ||
ok: { | ||
customerId: Schema.did({ method: 'mailto' }).from(input.customerId), | ||
resourceId: /** @type {string} */ (input.resourceId), | ||
timestamp: new Date(input.timestamp), | ||
} | ||
} | ||
} catch (/** @type {any} */ err) { | ||
return { | ||
error: new DecodeFailure(`decoding egress event: ${err.message}`, { cause: err }) | ||
} | ||
} | ||
} | ||
|
||
export const lister = { | ||
/** @type {import('../lib/api').Encoder<EgressEventListKey, EgressEventListStoreRecord>} */ | ||
encodeKey: input => ({ | ||
ok: { | ||
pk: `${input.from.toISOString()}#${input.customerId}`, | ||
sk: `${input.from.toISOString()}#${input.customerId}#${input.resourceId}` | ||
} | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createQueueAdderClient } from './client.js' | ||
import { encodeStr, validate } from '../data/egress.js' | ||
|
||
/** | ||
* @param {{ region: string } | import('@aws-sdk/client-sqs').SQSClient} conf | ||
* @param {{ url: URL }} context | ||
*/ | ||
export const createEgressEventQueue = (conf, { url }) => | ||
createQueueAdderClient(conf, { url, encode:encodeStr, validate }) |
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,33 @@ | ||
import { createStorePutterClient, createStoreListerClient } from './client.js' | ||
import { validate, encode, lister, decode } from '../data/egress.js' | ||
|
||
/** | ||
* Stores egress events for tracking requests served to customers. | ||
* | ||
* @type {import('sst/constructs').TableProps} | ||
*/ | ||
export const egressTableProps = { | ||
fields: { | ||
/** Composite key with format: "customerId" */ | ||
pk: 'string', | ||
/** Composite key with format: "timestamp#customerId#resourceId" */ | ||
sk: 'string', | ||
/** Customer DID (did:mailto:...). */ | ||
customerId: 'string', | ||
/** Resource CID. */ | ||
resourceId: 'string', | ||
/** ISO timestamp of the event. */ | ||
timestamp: 'string', | ||
}, | ||
primaryIndex: { partitionKey: 'pk', sortKey: 'sk' } | ||
} | ||
|
||
/** | ||
* @param {{ region: string } | import('@aws-sdk/client-dynamodb').DynamoDBClient} conf | ||
* @param {{ tableName: string }} context | ||
* @returns {import('../lib/api.js').EgressEventStore} | ||
*/ | ||
export const createEgressEventStore = (conf, { tableName }) => ({ | ||
...createStorePutterClient(conf, { tableName, validate, encode }), | ||
...createStoreListerClient(conf, { tableName, encodeKey: lister.encodeKey, decode }) | ||
}) |