Skip to content

Commit

Permalink
fix!: remove websocket support (#966)
Browse files Browse the repository at this point in the history
when you try to use `@web3-storage/access` in a new next.js repository
you get errors like this:

```
Import trace for requested module:
./node_modules/ipfs-utils/src/http/fetch.js
./node_modules/@web3-storage/upload-client/src/store.js
./node_modules/@web3-storage/upload-client/src/index.js
./node_modules/@web3-storage/w3up-client/src/client.js
./node_modules/@web3-storage/w3up-client/src/index.node.js
./app/utils/utils.ts
./app/page.tsx

./node_modules/ws/lib/buffer-util.js
Module not found: Can't resolve 'bufferutil' in '/Users/travis/dev/pl/sample-w3/node_modules/ws/lib'

Import trace for requested module:
./node_modules/ws/lib/buffer-util.js
./node_modules/ws/lib/websocket.js
./node_modules/ws/index.js
./node_modules/isomorphic-ws/node.js
./node_modules/@web3-storage/access/src/utils/ws.js
./node_modules/@web3-storage/access/src/agent-use-cases.js
./node_modules/@web3-storage/access/src/agent.js
./node_modules/@web3-storage/w3up-client/src/index.node.js
./app/utils/utils.ts
./app/page.tsx

./node_modules/ws/lib/validation.js
Module not found: Can't resolve 'utf-8-validate' in '/Users/travis/dev/pl/sample-w3/node_modules/ws/lib'

Import trace for requested module:
./node_modules/ws/lib/validation.js
./node_modules/ws/lib/receiver.js
./node_modules/ws/index.js
./node_modules/isomorphic-ws/node.js
./node_modules/@web3-storage/access/src/utils/ws.js
./node_modules/@web3-storage/access/src/agent-use-cases.js
./node_modules/@web3-storage/access/src/agent.js
./node_modules/@web3-storage/w3up-client/src/index.node.js
./app/utils/utils.ts
./app/page.tsx
```

Which looks like the same thing as
netlify/netlify-lambda#179 and shows up a few
other times in similar repos.

While we could instruct our users to add some build configuration to go
away, we don't use websockets in production in any of our flows and imho
we should just remove this dependency. The easiest way to do this is to
remove the cli as well, and since we don't use that anywhere that also
seems like a good idea.
  • Loading branch information
travis authored Oct 10, 2023
1 parent 91f52c6 commit 77bf7ea
Show file tree
Hide file tree
Showing 20 changed files with 30 additions and 1,818 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"javascript.format.insertSpaceBeforeFunctionParenthesis": false,
"typescript.format.insertSpaceBeforeFunctionParenthesis": false,
"typescript.tsdk": "node_modules/typescript/lib"
}
13 changes: 1 addition & 12 deletions packages/access-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
"type": "module",
"types": "dist/src/index.d.ts",
"main": "src/index.js",
"bin": {
"w3access": "./src/cli/index.js"
},
"scripts": {
"lint": "tsc --build && eslint '**/*.{js,ts}' && prettier --check '**/*.{js,ts,yml,json}' --ignore-path ../../.gitignore",
"build": "tsc --build",
Expand Down Expand Up @@ -71,18 +68,11 @@
"@web3-storage/did-mailto": "workspace:^",
"bigint-mod-arith": "^3.1.2",
"conf": "10.2.0",
"inquirer": "^9.1.4",
"isomorphic-ws": "^5.0.0",
"kysely": "^0.23.4",
"multiformats": "^12.1.2",
"one-webcrypto": "^1.0.3",
"ora": "^6.1.2",
"p-defer": "^4.0.0",
"p-wait-for": "^5.0.0",
"type-fest": "^3.3.0",
"uint8arrays": "^4.0.6",
"ws": "^8.12.0",
"zod": "^3.20.2"
"uint8arrays": "^4.0.6"
},
"devDependencies": {
"@types/assert": "^1.5.6",
Expand All @@ -97,7 +87,6 @@
"hd-scripts": "^4.0.0",
"mocha": "^10.2.0",
"playwright-test": "^12.3.4",
"sade": "^1.8.1",
"sinon": "^15.0.3",
"typescript": "5.2.2",
"watch": "^1.0.2"
Expand Down
71 changes: 1 addition & 70 deletions packages/access-client/src/agent-use-cases.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { addSpacesFromDelegations, Agent as AccessAgent } from './agent.js'
import * as Ucanto from '@ucanto/interface'
import * as Access from '@web3-storage/capabilities/access'
import { bytesToDelegations, stringToDelegation } from './encoding.js'
import { bytesToDelegations } from './encoding.js'
import { Provider } from '@web3-storage/capabilities'
import * as w3caps from '@web3-storage/capabilities'
import { Websocket, AbortError } from './utils/ws.js'
import { AgentData, isSessionProof } from './agent-data.js'
import * as ucanto from '@ucanto/core'
import { DID as DIDValidator } from '@ucanto/validator'
Expand Down Expand Up @@ -130,43 +129,6 @@ export async function pollAccessClaimUntil(
}
}

/**
* @param {AccessAgent} access
* @param {object} [opts]
* @param {AbortSignal} [opts.signal]
* @deprecated - use waitForAuthorizationOnSocket
*/
export async function waitForDelegationOnSocket(access, opts) {
const ws = new Websocket(access.url, 'validate-ws')
await ws.open(opts)

ws.send({
did: access.did(),
})

try {
const msg = await ws.awaitMsg(opts)

if (msg.type === 'timeout') {
await ws.close(1000, 'agent got timeout waiting for validation')
throw new Error('Email validation timed out.')
}

if (msg.type === 'delegation') {
const delegation = stringToDelegation(msg.delegation)
await ws.close(1000, 'received delegation, agent is done with ws')
return delegation
}
} catch (error) {
if (error instanceof AbortError) {
await ws.close(1000, 'AbortError: agent failed to get delegation')
throw new TypeError('Failed to get delegation', { cause: error })
}
throw error
}
throw new TypeError('Failed to get delegation')
}

/**
* @template [T={}]
* @typedef {{ signal?: AbortSignal } & T} AuthorizationWaiterOpts
Expand All @@ -176,19 +138,6 @@ export async function waitForDelegationOnSocket(access, opts) {
* @typedef {(accessAgent: AccessAgent, opts: AuthorizationWaiterOpts<U>) => Promise<Iterable<Ucanto.Delegation>>} AuthorizationWaiter
*/

/**
* Wait for the authorization process to complete by waiting on a
* well-known websocket endpoint for the access-api server to
* receive and forward a session delegation from the authorization
* email flow.
*
* @type AuthorizationWaiter
*/
export async function waitForAuthorizationOnSocket(access, opts = {}) {
const delegation = await waitForDelegationOnSocket(access, opts)
return [delegation]
}

/**
* Wait for authorization process to complete by polling executions of the
* `access/claim` capability and waiting for the result to include
Expand Down Expand Up @@ -260,24 +209,6 @@ export async function authorizeWaitAndClaim(accessAgent, email, opts) {
})
}

/**
* Request authorization of a session allowing this agent to issue UCANs
* signed by the passed email address.
*
* @param {AccessAgent} access
* @param {`${string}@${string}`} email
* @param {object} [opts]
* @param {AbortSignal} [opts.signal]
* @param {Iterable<{ can: Ucanto.Ability }>} [opts.capabilities]
* @deprecated use authorizeWaitAndClaim directly going forward, passing it the expectAuthorization: waitForAuthorizationOnSocket to replicate this function's behavior
*/
export async function authorizeWithSocket(access, email, opts) {
return authorizeWaitAndClaim(access, email, {
...opts,
expectAuthorization: waitForAuthorizationOnSocket,
})
}

/**
* Invokes voucher/redeem for the free tier, wait on the websocket for the voucher/claim and invokes it
*
Expand Down
9 changes: 0 additions & 9 deletions packages/access-client/src/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as Ucanto from '@ucanto/interface'
import * as CAR from '@ucanto/transport/car'
import * as HTTP from '@ucanto/transport/http'
import * as ucanto from '@ucanto/core'
import { Peer } from './awake/peer.js'
import * as Space from '@web3-storage/capabilities/space'
import * as Access from '@web3-storage/capabilities/access'

Expand Down Expand Up @@ -562,14 +561,6 @@ export class Agent {
)
}

/**
*
* @param {import('../src/awake/types').Channel} channel
*/
peer(channel) {
return new Peer({ agent: this, channel })
}

/**
* Get Space information from Access service
*
Expand Down
Loading

0 comments on commit 77bf7ea

Please sign in to comment.