Skip to content

Commit

Permalink
chore: rename parseProviders
Browse files Browse the repository at this point in the history
per PR comment, make the name better reflect the implementation
  • Loading branch information
travis committed Aug 14, 2023
1 parent 7d7b51b commit 54d3950
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions upload-api/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ export function getServiceSigner(config) {
/**
* Given a string, parse into provider ServiceDIDs.
*
* @param {string} providersEnvVar
* @param {string} serviceDids a comma-separated string of ServiceDIDs
* @returns {import('@web3-storage/upload-api').ServiceDID[]}
*/
export function parseProviders(providersEnvVar) {
export function parseServiceDids(serviceDids) {
return /** @type {import('@web3-storage/upload-api').ServiceDID[]} */(
providersEnvVar.split(',').map(s => {
serviceDids.split(',').map(s => {
const did = DID.parse(s.trim()).did()
if (!did.startsWith('did:web:')) {
throw new Error(`Invalid ServiceDID - ServiceDID must be a did:web: ${did}`)
Expand Down
4 changes: 2 additions & 2 deletions upload-api/functions/ucan-invocation-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { createTaskStore } from '../buckets/task-store.js'
import { createWorkflowStore } from '../buckets/workflow-store.js'
import { createStoreTable } from '../tables/store.js'
import { createUploadTable } from '../tables/upload.js'
import { getServiceSigner, parseProviders } from '../config.js'
import { getServiceSigner, parseServiceDids } from '../config.js'
import { createUcantoServer } from '../service.js'
import { Config } from '@serverless-stack/node/config/index.js'
import { CAR, Legacy, Codec } from '@ucanto/transport'
Expand Down Expand Up @@ -125,7 +125,7 @@ export async function ucanInvocationRouter(request) {
endpoint: dbEndpoint
});
const rateLimitsStorage = createRateLimitTable(AWS_REGION, rateLimitTableName)
const provisionsStorage = useProvisionStore(subscriptionTable, consumerTable, parseProviders(providers))
const provisionsStorage = useProvisionStore(subscriptionTable, consumerTable, parseServiceDids(providers))
const delegationsStorage = createDelegationsTable(AWS_REGION, delegationTableName, { bucket: delegationBucket, invocationBucket, workflowBucket })

const server = createUcantoServer(serviceSigner, {
Expand Down
4 changes: 2 additions & 2 deletions upload-api/functions/validate-email.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Sentry from '@sentry/serverless'
import { authorize } from '@web3-storage/upload-api/validate'
import { Config } from '@serverless-stack/node/config/index.js'
import { getServiceSigner, parseProviders } from '../config.js'
import { getServiceSigner, parseServiceDids } from '../config.js'
import { Email } from '../email.js'
import { createDelegationsTable } from '../tables/delegations.js'
import { createDelegationsStore } from '../buckets/delegations-store.js'
Expand Down Expand Up @@ -100,7 +100,7 @@ function createAuthorizeContext () {
email: new Email({ token: POSTMARK_TOKEN }),
signer: getServiceSigner({ UPLOAD_API_DID, PRIVATE_KEY }),
delegationsStorage: createDelegationsTable(AWS_REGION, DELEGATION_TABLE_NAME, { bucket: delegationBucket, invocationBucket, workflowBucket }),
provisionsStorage: useProvisionStore(subscriptionTable, consumerTable, parseProviders(PROVIDERS)),
provisionsStorage: useProvisionStore(subscriptionTable, consumerTable, parseServiceDids(PROVIDERS)),
rateLimitsStorage: createRateLimitTable(AWS_REGION, RATE_LIMIT_TABLE_NAME)
}
}
Expand Down
24 changes: 12 additions & 12 deletions upload-api/test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,47 +58,47 @@ test('getServiceSigner errors if config.UPLOAD_API_DID is provided but not a DID
}, { message: /^Invalid DID/ })
})

test('parseProviders parses one DID', async (t) => {
test('parseServiceDids parses one DID', async (t) => {
t.deepEqual(
configModule.parseProviders('did:web:example.com'),
configModule.parseServiceDids('did:web:example.com'),
['did:web:example.com']
)
})

test('parseProviders parses more than one DID', async (t) => {
test('parseServiceDids parses more than one DID', async (t) => {
t.deepEqual(
configModule.parseProviders('did:web:example.com,did:web:two.example.com'),
configModule.parseServiceDids('did:web:example.com,did:web:two.example.com'),
['did:web:example.com', 'did:web:two.example.com']
)

t.deepEqual(
configModule.parseProviders('did:web:example.com,did:web:two.example.com,did:web:three.example.com'),
configModule.parseServiceDids('did:web:example.com,did:web:two.example.com,did:web:three.example.com'),
['did:web:example.com', 'did:web:two.example.com', 'did:web:three.example.com']
)
})

test('parseProviders trims space around dids', async (t) => {
test('parseServiceDids trims space around dids', async (t) => {
t.deepEqual(
configModule.parseProviders(' did:web:example.com, did:web:two.example.com '),
configModule.parseServiceDids(' did:web:example.com, did:web:two.example.com '),
['did:web:example.com', 'did:web:two.example.com']
)
})

test('parseProviders throws an exception if a non-DID is provided', async (t) => {
test('parseServiceDids throws an exception if a non-DID is provided', async (t) => {
t.throws(
() => configModule.parseProviders('http://example.com'),
() => configModule.parseServiceDids('http://example.com'),
{ message: /^Invalid DID/}
)
})

test('parseProviders throws an exception if a non-ServiceDID is provided', async (t) => {
test('parseServiceDids throws an exception if a non-ServiceDID is provided', async (t) => {
t.throws(
() => configModule.parseProviders('did:mailto:abc123'),
() => configModule.parseServiceDids('did:mailto:abc123'),
{ message: /^Invalid ServiceDID/}
)

t.throws(
() => configModule.parseProviders('did:key:z6Mkfy8k2JJUdNWCJtvzYrko5QRc7GXP6pksKDG19gxYzyi4'),
() => configModule.parseServiceDids('did:key:z6Mkfy8k2JJUdNWCJtvzYrko5QRc7GXP6pksKDG19gxYzyi4'),
{ message: /^Invalid ServiceDID/}
)
})

0 comments on commit 54d3950

Please sign in to comment.