Skip to content

Commit

Permalink
fix(upload-api): fix incompatibilities with w3infra (#504)
Browse files Browse the repository at this point in the history
This PR makes changes needed for
storacha/w3infra#154 namely:

1. Fixes type mappings in `package.json`, otherwise TS is confused in
w3infra.
2. Removes obsolete `index.js` file that lives in w3infra now.
3. Exports types so they can be used from w3infra.
4. Updates return type from `undefined` to `null` to match what w3infra
implementation returns.
  • Loading branch information
Gozala authored Mar 8, 2023
1 parent 70525de commit d3dcf34
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 140 deletions.
18 changes: 12 additions & 6 deletions packages/upload-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
"name": "@web3-storage/upload-api",
"version": "1.0.1",
"type": "module",
"main": "./src/lib.js",
"typesVersions": {
"*": {
"*": [
"dist/src/*"
],
"dist/src/lib.d.ts": [
"src/lib.js": [
"dist/src/lib.d.ts"
],
"store": [
"dist/src/store.d.ts"
],
"upload": [
"dist/src/upload.d.ts"
],
"types": [
"dist/src/types.d.ts"
],
"test": [
"dist/test/*"
"dist/test/lib.d.ts"
]
}
},
Expand Down Expand Up @@ -45,7 +52,6 @@
"test-watch": "pnpm build && mocha --bail --timeout 10s --watch --parallel -n no-warnings -n experimental-vm-modules --watch-files src,test"
},
"dependencies": {
"@sentry/serverless": "^7.22.0",
"@ucanto/client": "^5.1.0",
"@ucanto/interface": "^6.0.0",
"@ucanto/principal": "^5.1.0",
Expand Down
27 changes: 0 additions & 27 deletions packages/upload-api/src/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/upload-api/src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as CAR from '@ucanto/transport/car'
import * as CBOR from '@ucanto/transport/cbor'
import { createService as createStoreService } from './store.js'
import { createService as createUploadService } from './upload.js'
export * from './types.js'

/**
* @param {Types.UcantoServerContext} options
Expand Down
28 changes: 12 additions & 16 deletions packages/upload-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface Service {
upload: {
add: ServiceMethod<UploadAdd, UploadAddOk, Failure>
// @todo - Use proper type when no item was removed instead of undefined
remove: ServiceMethod<UploadRemove, UploadRemoveOk | undefined, Failure>
remove: ServiceMethod<UploadRemove, UploadRemoveOk | null, Failure>
list: ServiceMethod<UploadList, UploadListOk, Failure>
}
}
Expand Down Expand Up @@ -70,7 +70,6 @@ export interface UcantoServerTestContext

export interface StoreTestContext {
testStoreTable: TestStoreTable
testUploadTable: TestUploadTable
testSpaceRegistry: TestSpaceRegistry
}

Expand Down Expand Up @@ -129,20 +128,13 @@ export interface TestStoreTable {
export interface UploadTable {
exists: (space: DID, root: UnknownLink) => Promise<boolean>
insert: (item: UploadAddInput) => Promise<UploadAddOk>
remove: (space: DID, root: UnknownLink) => Promise<UploadRemoveOk | undefined>
remove: (space: DID, root: UnknownLink) => Promise<UploadRemoveOk | null>
list: (
space: DID,
options?: ListOptions
) => Promise<ListResponse<UploadListItem>>
}

export interface TestUploadTable {
get(
space: DID,
root: UnknownLink
): Promise<(UploadAddInput & UploadListItem) | undefined>
}

export interface StoreAddInput {
space: DID
link: UnknownLink
Expand Down Expand Up @@ -232,12 +224,16 @@ export interface SpaceUnknown extends Failure {
}

export interface Assert {
equal: (actual: unknown, expected: unknown, message?: string | Error) => void
deepEqual: (
actual: unknown,
expected: unknown,
message?: string | Error
) => void
equal: <Actual, Expected extends Actual>(
actual: Actual,
expected: Expected,
message?: string
) => unknown
deepEqual: <Actual, Expected extends Actual>(
actual: Actual,
expected: Expected,
message?: string
) => unknown
}

export type Test = (assert: Assert, context: UcantoServerTestContext) => unknown
Expand Down
2 changes: 1 addition & 1 deletion packages/upload-api/src/upload/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as API from '../types.js'

/**
* @param {API.UploadServiceContext} context
* @returns {API.ServiceMethod<API.UploadRemove, API.UploadRemoveOk | undefined, API.Failure>}
* @returns {API.ServiceMethod<API.UploadRemove, API.UploadRemoveOk | null, API.Failure>}
*/
export function uploadRemoveProvider(context) {
return Server.provide(Upload.remove, async ({ capability }) => {
Expand Down
19 changes: 8 additions & 11 deletions packages/upload-api/test/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const test = {
assert.equal(storeAdd.with, spaceDid)
assert.deepEqual(storeAdd.link, link)

assert.deepEqual(storeAdd.headers?.['content-length'], size)
assert.equal(storeAdd.headers?.['content-length'], String(size))
assert.deepEqual(
storeAdd.headers?.['x-amz-checksum-sha256'],
base64pad.baseEncode(link.multihash.digest)
Expand Down Expand Up @@ -80,17 +80,17 @@ export const test = {
}

assert.deepEqual(
{
space: item.space,
link: item.link,
size: item.size,
issuer: item.issuer,
},
{
space: spaceDid,
link,
size: data.byteLength,
issuer: alice.did(),
},
{
space: item?.space,
link: item?.link,
size: item?.size,
issuer: item?.issuer,
}
)

Expand Down Expand Up @@ -262,10 +262,7 @@ export const test = {
}
)

assert.equal(
Link.parse(item.invocation.toString()),
item.invocation.toString()
)
assert.deepEqual(Link.parse(item.invocation.toString()), item.invocation)

assert.equal(
Date.now() - new Date(item.insertedAt).getTime() < 60_000,
Expand Down
1 change: 0 additions & 1 deletion packages/upload-api/test/store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe('store/*', () => {
storeTable,
testStoreTable: storeTable,
uploadTable,
testUploadTable: uploadTable,
carStoreBucket,
dudewhereBucket,
access,
Expand Down
2 changes: 1 addition & 1 deletion packages/upload-api/test/upload-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class UploadTable {
this.items.splice(this.items.indexOf(item), 1)
}

return item
return item || null
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/upload-api/test/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,15 @@ export const test = {
)
}

if (uploadRemove.error) {
if (uploadRemove?.error) {
throw new Error(
'expected upload/remove response to include the upload object removed',
{ cause: uploadRemove.error }
)
}

assert.equal(uploadRemove.root.toString(), car.roots[0].toString())
assert.equal(uploadRemove.shards?.[0].toString(), car.cid.toString())
assert.equal(uploadRemove?.root.toString(), car.roots[0].toString())
assert.equal(uploadRemove?.shards?.[0].toString(), car.cid.toString())
},

'upload/remove does not fail for non existent upload': async (
Expand Down Expand Up @@ -345,7 +345,7 @@ export const test = {

assert.equal(
uploadRemove,
undefined,
null,
'expect falsy response when removing an upload you do not have'
)
},
Expand Down
1 change: 0 additions & 1 deletion packages/upload-api/test/upload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe('upload/*', () => {
storeTable,
testStoreTable: storeTable,
uploadTable,
testUploadTable: uploadTable,
carStoreBucket,
dudewhereBucket,
access,
Expand Down
Loading

0 comments on commit d3dcf34

Please sign in to comment.