forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server federation (bluesky-social#68)
* start of server federation * repo post/get roots * push/pull & basic subscriptions * working on federation * wip * federation works! * cleanup * remove old user routes
- Loading branch information
Showing
30 changed files
with
454 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,93 @@ | ||
import axios from 'axios' | ||
import { CID } from 'multiformats' | ||
import { assureAxiosError } from './util.js' | ||
import * as check from '../common/check.js' | ||
import { schema as repoSchema } from '../repo/types.js' | ||
|
||
const SERVER_URL = 'http://localhost:2583' | ||
const THIRD_PARTY_URL = 'http://localhost:2584' | ||
|
||
export const register = async ( | ||
car: Uint8Array, | ||
authToken: string, | ||
): Promise<void> => { | ||
await axios.post(`${SERVER_URL}/user/register`, car, { | ||
headers: { | ||
'Content-Type': 'application/octet-stream', | ||
Authorization: `Bearer ${authToken}`, | ||
}, | ||
}) | ||
} | ||
|
||
export const updateUser = async ( | ||
car: Uint8Array, | ||
authToken: string, | ||
): Promise<void> => { | ||
await axios.post(`${SERVER_URL}/user/update`, car, { | ||
headers: { | ||
'Content-Type': 'application/octet-stream', | ||
Authorization: `Bearer ${authToken}`, | ||
}, | ||
}) | ||
} | ||
|
||
export const fetchUser = async (did: string): Promise<Uint8Array> => { | ||
const res = await axios.get(`${SERVER_URL}/user/${did}`, { | ||
responseType: 'arraybuffer', | ||
}) | ||
return new Uint8Array(res.data) | ||
} | ||
|
||
export const fetchUsers = async (): Promise< | ||
{ name: string; did: string }[] | ||
> => { | ||
const res = await axios.get(`${SERVER_URL}/users`) | ||
return res.data | ||
} | ||
|
||
export const getServerDid = async (): Promise<string> => { | ||
const res = await axios.get(`${SERVER_URL}/.well-known/did.json`) | ||
return res.data.id | ||
export const lookupDid = async ( | ||
url: string, | ||
name: string, | ||
): Promise<string | null> => { | ||
const params = { resource: name } | ||
try { | ||
const res = await axios.get(`${url}/.well-known/webfinger`, { | ||
params, | ||
}) | ||
return check.assure(repoSchema.did, res.data.id) | ||
} catch (e) { | ||
const err = assureAxiosError(e) | ||
if (err.response?.status === 404) { | ||
return null | ||
} | ||
throw new Error(err.message) | ||
} | ||
} | ||
|
||
export const fetchUserDid = async ( | ||
username: string, | ||
): Promise<string | null> => { | ||
export const getRemoteRoot = async ( | ||
url: string, | ||
did: string, | ||
): Promise<CID | null> => { | ||
const params = { did } | ||
try { | ||
const res = await axios.get( | ||
`${SERVER_URL}/.well-known/webfinger?resource=${username}`, | ||
) | ||
return res.data.id | ||
} catch (_err) { | ||
return null | ||
const res = await axios.get(`${url}/data/root`, { params }) | ||
return CID.parse(res.data.root) | ||
} catch (e) { | ||
const err = assureAxiosError(e) | ||
if (err.response?.status === 404) { | ||
return null | ||
} | ||
throw new Error(`Could not retrieve server did ${err.message}`) | ||
} | ||
} | ||
|
||
export const getThirdPartyDid = async (): Promise<string> => { | ||
const res = await axios.get(`${THIRD_PARTY_URL}/.well-known/did.json`) | ||
return res.data.id | ||
export const subscribe = async ( | ||
url: string, | ||
did: string, | ||
ownUrl: string, | ||
): Promise<void> => { | ||
const data = { did, host: ownUrl } | ||
try { | ||
await axios.post(`${url}/data/subscribe`, data) | ||
} catch (e) { | ||
const err = assureAxiosError(e) | ||
throw new Error(`Could not retrieve server did ${err.message}`) | ||
} | ||
} | ||
|
||
export const thirdPartyPost = async ( | ||
username: string, | ||
authToken: string, | ||
export const pushRepo = async ( | ||
url: string, | ||
did: string, | ||
car: Uint8Array, | ||
): Promise<void> => { | ||
await axios.post( | ||
`${THIRD_PARTY_URL}/post`, | ||
{ username }, | ||
{ | ||
try { | ||
await axios.post(`${url}/data/repo/${did}`, car, { | ||
headers: { | ||
Authorization: `Bearer ${authToken}`, | ||
'Content-Type': 'application/octet-stream', | ||
}, | ||
}, | ||
) | ||
}) | ||
} catch (e) { | ||
const err = assureAxiosError(e) | ||
throw new Error(`Could not retrieve server did ${err.message}`) | ||
} | ||
} | ||
|
||
export const pullRepo = async ( | ||
url: string, | ||
did: string, | ||
from?: CID, | ||
): Promise<Uint8Array | null> => { | ||
const params = { did, from: from?.toString() } | ||
try { | ||
const res = await axios.get(`${url}/data/repo`, { | ||
params, | ||
responseType: 'arraybuffer', | ||
}) | ||
return new Uint8Array(res.data) | ||
} catch (e) { | ||
const err = assureAxiosError(e) | ||
if (err.response?.status === 404) { | ||
return null | ||
} | ||
throw new Error(`Could not retrieve server did ${err.message}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.