Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support pre caveat in store/list and upload/list #139

Merged
merged 11 commits into from
Feb 16, 2023
538 changes: 374 additions & 164 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@
"@serverless-stack/resources": "^1.18.0",
"@tsconfig/node16": "^1.0.3",
"@types/git-rev-sync": "^2.0.0",
"@ucanto/core": "^4.0.3",
"@ucanto/client": "^4.0.3",
"@ucanto/principal": "^4.0.3",
"@ucanto/transport": "^4.0.3",
"@web3-storage/access": "^9.2.0",
"@web3-storage/w3up-client": "^4.0.1",
"@ucanto/client": "^4.2.3",
"@ucanto/core": "^4.2.3",
"@ucanto/principal": "^4.2.3",
"@ucanto/transport": "^4.2.3",
"@web-std/blob": "^3.0.4",
"@web-std/fetch": "^4.1.0",
"@web3-storage/access": "^9.2.0",
"@web3-storage/w3up-client": "^4.1.0",
"ava": "^4.3.3",
"dotenv": "^16.0.3",
"git-rev-sync": "^3.0.2",
"hd-scripts": "^3.0.2",
"lint-staged": "^13.0.3",
"p-wait-for": "^5.0.0",
"typescript": "^4.9.3"
"typescript": "^4.9.3",
"multiformats": "^11.0.1"
},
"dependencies": {
"@serverless-stack/node": "^1.18.0",
Expand Down
2 changes: 1 addition & 1 deletion replicator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@ipld/car": "^5.0.1",
"@ipld/dag-pb": "^3.0.0",
"ava": "^4.3.3",
"multiformats": "^10.0.2",
"multiformats": "^11.0.0",
"nanoid": "^4.0.0",
"testcontainers": "^8.13.0",
"uint8arrays": "^4.0.2"
Expand Down
2 changes: 1 addition & 1 deletion satnav/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@sentry/serverless": "^7.22.0",
"@ipld/car": "^5.0.1",
"cardex": "^1.0.0",
"multiformats": "^10.0.2",
"multiformats": "^11.0.1",
"uint8arrays": "^4.0.2"
},
"devDependencies": {
Expand Down
12 changes: 6 additions & 6 deletions upload-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
"@ipld/dag-ucan": "^3.0.1",
"@sentry/serverless": "^7.22.0",
"@serverless-stack/node": "^1.18.2",
"@ucanto/client": "^4.0.3",
"@ucanto/interface": "^4.0.3",
"@ucanto/principal": "^4.0.3",
"@ucanto/server": "^4.0.3",
"@ucanto/transport": "^4.0.3",
"@ucanto/client": "^4.2.3",
"@ucanto/interface": "^4.2.3",
"@ucanto/principal": "^4.2.3",
"@ucanto/server": "^4.2.3",
"@ucanto/transport": "^4.2.3",
"@web-std/fetch": "^4.1.0",
"@web3-storage/access": "^9.0.0",
"@web3-storage/capabilities": "^2.0.0",
"multiformats": "^10.0.2",
"multiformats": "^11.0.1",
"p-retry": "^5.1.2",
"uint8arrays": "^4.0.2"
},
Expand Down
5 changes: 3 additions & 2 deletions upload-api/service/store/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ export function storeListProvider(context) {
return Server.provide(
Store.list,
async ({ capability }) => {
const { cursor, size } = capability.nb
const { cursor, size, pre } = capability.nb
const space = Server.DID.parse(capability.with).did()

return await context.storeTable.list(space, {
size,
cursor
cursor,
pre
})
}
)
Expand Down
5 changes: 4 additions & 1 deletion upload-api/service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,14 @@ export interface UploadListItem extends UploadAddResult {

export interface ListOptions {
size?: number,
cursor?: string
cursor?: string,
pre?: boolean
}

export interface ListResponse<R> {
cursor?: string,
startCursor?: string,
endCursor?: string,
size: number,
results: R[]
}
Expand Down
5 changes: 3 additions & 2 deletions upload-api/service/upload/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ export function uploadListProvider(context) {
return Server.provide(
Upload.list,
async ({ capability }) => {
const { cursor, size } = capability.nb
const { cursor, size, pre } = capability.nb
const space = Server.DID.parse(capability.with).did()

return await context.uploadTable.list(space, {
size,
cursor
cursor,
pre
})
})
}
5 changes: 5 additions & 0 deletions upload-api/tables/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,25 @@ export function createStoreTable (region, tableName, options = {}) {
AttributeValueList: [{ S: space }],
},
},
ScanIndexForward: ! options.pre,
ExclusiveStartKey: exclusiveStartKey,
AttributesToGet: ['link', 'size', 'origin', 'insertedAt'],
})
const response = await dynamoDb.send(cmd)

const results = response.Items?.map(i => toStoreListResult(unmarshall(i))) ?? []
const startCursor = results[0] ? results[0].link.toString() : undefined
// Get cursor of the item where list operation stopped (inclusive).
// This value can be used to start a new operation to continue listing.
const lastKey = response.LastEvaluatedKey && unmarshall(response.LastEvaluatedKey)
const cursor = lastKey ? lastKey.link : undefined
const endCursor = cursor

return {
size: results.length,
cursor,
startCursor,
endCursor,
results
}
}
Expand Down
9 changes: 7 additions & 2 deletions upload-api/tables/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,28 @@ export function createUploadTable (region, tableName, options = {}) {
AttributeValueList: [{ S: space }],
},
},
ScanIndexForward: ! options.pre,
ExclusiveStartKey: exclusiveStartKey,
AttributesToGet: ['space', 'root', 'shards', 'insertedAt', 'updatedAt'],
})
const response = await dynamoDb.send(cmd)

/** @type {UploadListItem[]} */
const results = response.Items?.map(i => toUploadListItem(unmarshall(i))) || []
const startCursor = results[0] ? results[0].root.toString() : undefined

// Get cursor of the item where list operation stopped (inclusive).
// This value can be used to start a new operation to continue listing.
const lastKey = response.LastEvaluatedKey && unmarshall(response.LastEvaluatedKey)
const cursor = lastKey ? lastKey.root : undefined
const endCursor = cursor

return {
size: results.length,
results,
cursor
cursor,
startCursor,
endCursor,
results
}
},
}
Expand Down
1 change: 0 additions & 1 deletion upload-api/test/helpers/random.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { webcrypto } from 'crypto'
import { Blob } from '@web-std/blob'

import { CarWriter } from '@ipld/car'
import { CID } from 'multiformats/cid'
import * as raw from 'multiformats/codecs/raw'
Expand Down
9 changes: 5 additions & 4 deletions upload-api/test/service/ucan-invocation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GetObjectCommand } from '@aws-sdk/client-s3'
import * as Signer from '@ucanto/principal/ed25519'
import { CAR } from '@ucanto/transport'
import * as UCAN from '@ipld/dag-ucan'
import * as ucanto from '@ucanto/core';

import { createSpace } from '../helpers/ucanto.js'
import { createS3, createBucket } from '../helpers/resources.js'
Expand All @@ -30,7 +31,7 @@ test('parses ucan invocation request', async t => {
const can = 'store/add'

const request = await CAR.encode([
{
await ucanto.delegate({
issuer: alice,
audience: uploadService,
capabilities: [{
Expand All @@ -39,7 +40,7 @@ test('parses ucan invocation request', async t => {
nb
}],
proofs: [proof],
}
})
])

// @ts-expect-error different type interface in AWS expected request
Expand Down Expand Up @@ -81,7 +82,7 @@ test('persists ucan invocation CAR file', async t => {
const can = 'store/add'

const request = await CAR.encode([
{
await ucanto.delegate({
issuer: alice,
audience: uploadService,
capabilities: [{
Expand All @@ -90,7 +91,7 @@ test('persists ucan invocation CAR file', async t => {
nb
}],
proofs: [proof],
}
})
])

// @ts-expect-error different type interface in AWS expected request
Expand Down