Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/w3up-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
"types": "./dist/src/stores/memory.d.ts",
"import": "./dist/src/stores/memory.js"
},
"./service": {
"types": "./dist/src/service.d.ts",
"import": "./dist/src/service.js"
},
"./types": "./dist/src/types.js"
},
"publishConfig": {
Expand Down Expand Up @@ -148,16 +152,17 @@
"dependencies": {
"@ipld/dag-ucan": "^3.4.0",
"@ucanto/client": "^9.0.1",
"@ucanto/core": "^10.0.1",
"@ucanto/interface": "^10.0.1",
"@ucanto/principal": "^9.0.1",
"@ucanto/transport": "^9.1.1",
"@ucanto/core": "^10.3.1",
"@ucanto/interface": "^10.2.0",
"@ucanto/principal": "^9.0.2",
"@ucanto/transport": "^9.2.0",
"@web3-storage/access": "workspace:^",
"@web3-storage/blob-index": "workspace:^",
"@web3-storage/capabilities": "workspace:^",
"@web3-storage/did-mailto": "workspace:^",
"@web3-storage/filecoin-client": "workspace:^",
"@web3-storage/upload-client": "workspace:^"
"@web3-storage/upload-client": "workspace:^",
"environment": "^1.1.0"
},
"devDependencies": {
"@ipld/car": "^5.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/w3up-client/src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Base {
* @param {URL} [options.receiptsEndpoint]
*/
constructor(agentData, options = {}) {
this._serviceConf = options.serviceConf ?? serviceConf
this._serviceConf = options.serviceConf ?? serviceConf()
this._agent = new Agent(agentData, {
servicePrincipal: this._serviceConf.access.id,
// @ts-expect-error I know but it will be HTTP for the forseeable.
Expand Down
109 changes: 79 additions & 30 deletions packages/w3up-client/src/service.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isBrowser, isNode, isBun, isDeno, isElectron } from 'environment'

import * as client from '@ucanto/client'
import { CAR, HTTP } from '@ucanto/transport'
import * as DID from '@ipld/dag-ucan/did'
Expand All @@ -6,44 +8,91 @@ import { receiptsEndpoint } from '@web3-storage/upload-client'
export const accessServiceURL = new URL('https://up.web3.storage')
export const accessServicePrincipal = DID.parse('did:web:web3.storage')

export const accessServiceConnection = client.connect({
id: accessServicePrincipal,
codec: CAR.outbound,
channel: HTTP.open({
url: accessServiceURL,
method: 'POST',
}),
})
/* c8 ignore start */
const envName = isBrowser
? 'Browser'
: isNode
? 'Node'
: isBun
? 'Bun'
: isDeno
? 'Deno'
: isElectron
? 'Electron'
: 'Unknown'
export const defaultHeaders = {
'X-Client': `Storacha/1 (js; ${envName})`,
}
/* c8 ignore end */

/**
* Create a connection to the access service.
*
* @param {object} [options]
* @param {Record<string, string>} [options.headers]
* @param {import('./types.js').Principal} [options.id]
* @param {URL} [options.url]
*/
export const accessServiceConnection = (options = {}) =>
client.connect({
id: options.id ?? accessServicePrincipal,
codec: CAR.outbound,
channel: HTTP.open({
url: options.url ?? accessServiceURL,
method: 'POST',
headers: { ...defaultHeaders, ...options.headers },
}),
})

export const uploadServiceURL = new URL('https://up.web3.storage')
export const uploadServicePrincipal = DID.parse('did:web:web3.storage')

export const uploadServiceConnection = client.connect({
id: uploadServicePrincipal,
codec: CAR.outbound,
channel: HTTP.open({
url: uploadServiceURL,
method: 'POST',
}),
})
/**
* Create a connection to the upload service.
*
* @param {object} [options]
* @param {Record<string, string>} [options.headers]
* @param {import('./types.js').Principal} [options.id]
* @param {URL} [options.url]
*/
export const uploadServiceConnection = (options = {}) =>
client.connect({
id: options.id ?? uploadServicePrincipal,
codec: CAR.outbound,
channel: HTTP.open({
url: options.url ?? uploadServiceURL,
method: 'POST',
headers: { ...defaultHeaders, ...options.headers },
}),
})

export const filecoinServiceURL = new URL('https://up.web3.storage')
export const filecoinServicePrincipal = DID.parse('did:web:web3.storage')

export const filecoinServiceConnection = client.connect({
id: filecoinServicePrincipal,
codec: CAR.outbound,
channel: HTTP.open({
url: filecoinServiceURL,
method: 'POST',
}),
})
/**
* Create a connection to the filecoin service.
*
* @param {object} [options]
* @param {Record<string, string>} [options.headers]
* @param {import('./types.js').Principal} [options.id]
* @param {URL} [options.url]
*/
export const filecoinServiceConnection = (options = {}) =>
client.connect({
id: options.id ?? filecoinServicePrincipal,
codec: CAR.outbound,
channel: HTTP.open({
url: options.url ?? filecoinServiceURL,
method: 'POST',
headers: { ...defaultHeaders, ...options.headers },
}),
})

/** @type {import('./types.js').ServiceConf} */
export const serviceConf = {
access: accessServiceConnection,
upload: uploadServiceConnection,
filecoin: filecoinServiceConnection,
}
/** @type {() => import('./types.js').ServiceConf} */
export const serviceConf = () => ({
access: accessServiceConnection(),
upload: uploadServiceConnection(),
filecoin: filecoinServiceConnection(),
})

export { receiptsEndpoint }
Loading
Loading