A decentralized music tracking and discovery platform built on AT Protocol 🎵

Fetch repository CAR once and reuse CarReader

+35 -38
+35 -38
apps/cli/src/cmd/sync.ts
··· 39 39 logger.info` DID: ${did}`; 40 40 logger.info` Handle: ${handle}`; 41 41 42 + const carReader = await downloadCarFile(agent); 43 + 42 44 const [artists, albums, songs, scrobbles] = await Promise.all([ 43 - getRockskyUserArtists(agent), 44 - getRockskyUserAlbums(agent), 45 - getRockskyUserSongs(agent), 46 - getRockskyUserScrobbles(agent), 45 + getRockskyUserArtists(agent, carReader), 46 + getRockskyUserAlbums(agent, carReader), 47 + getRockskyUserSongs(agent, carReader), 48 + getRockskyUserScrobbles(agent, carReader), 47 49 ]); 48 50 49 51 logger.info` Artists: ${artists.length}`; ··· 880 882 ); 881 883 }; 882 884 883 - const getRockskyUserSongs = async (agent: Agent): Promise<Songs> => { 885 + const downloadCarFile = async (agent: Agent) => { 886 + logger.info(`Fetching repository CAR file ...`); 887 + 888 + const repoRes = await agent.com.atproto.sync.getRepo({ 889 + did: agent.assertDid, 890 + }); 891 + 892 + return CarReader.fromBytes(new Uint8Array(repoRes.data)); 893 + }; 894 + 895 + const getRockskyUserSongs = async ( 896 + agent: Agent, 897 + carReader: CarReader, 898 + ): Promise<Songs> => { 884 899 const results: { 885 900 value: Song.Record; 886 901 uri: string; ··· 888 903 }[] = []; 889 904 890 905 try { 891 - logger.info(`Fetching repository CAR file for songs...`); 892 - 893 - const repoRes = await agent.com.atproto.sync.getRepo({ 894 - did: agent.assertDid, 895 - }); 896 - 897 - const carReader = await CarReader.fromBytes(new Uint8Array(repoRes.data)); 898 906 const collection = "app.rocksky.song"; 899 907 900 908 for await (const { cid, bytes } of carReader.blocks()) { ··· 932 940 return results; 933 941 }; 934 942 935 - const getRockskyUserAlbums = async (agent: Agent): Promise<Albums> => { 943 + const getRockskyUserAlbums = async ( 944 + agent: Agent, 945 + carReader: CarReader, 946 + ): Promise<Albums> => { 936 947 const results: { 937 948 value: Album.Record; 938 949 uri: string; ··· 940 951 }[] = []; 941 952 942 953 try { 943 - logger.info(`Fetching repository CAR file for albums...`); 944 - 945 - // Use getRepo to fetch the entire repository as a CAR file 946 - const repoRes = await agent.com.atproto.sync.getRepo({ 947 - did: agent.assertDid, 948 - }); 949 - 950 - // Parse the CAR file 951 - const carReader = await CarReader.fromBytes(new Uint8Array(repoRes.data)); 952 954 const collection = "app.rocksky.album"; 955 + logger.info`Extracting ${collection} records from CAR file ...`; 953 956 954 957 for await (const { cid, bytes } of carReader.blocks()) { 955 958 try { ··· 984 987 return results; 985 988 }; 986 989 987 - const getRockskyUserArtists = async (agent: Agent): Promise<Artists> => { 990 + const getRockskyUserArtists = async ( 991 + agent: Agent, 992 + carReader: CarReader, 993 + ): Promise<Artists> => { 988 994 const results: { 989 995 value: Artist.Record; 990 996 uri: string; ··· 992 998 }[] = []; 993 999 994 1000 try { 995 - logger.info(`Fetching repository CAR file for artists...`); 996 - 997 - const repoRes = await agent.com.atproto.sync.getRepo({ 998 - did: agent.assertDid, 999 - }); 1000 - 1001 - const carReader = await CarReader.fromBytes(new Uint8Array(repoRes.data)); 1002 1001 const collection = "app.rocksky.artist"; 1002 + logger.info`Extracting ${collection} records from CAR file ...`; 1003 1003 1004 1004 for await (const { cid, bytes } of carReader.blocks()) { 1005 1005 try { ··· 1034 1034 return results; 1035 1035 }; 1036 1036 1037 - const getRockskyUserScrobbles = async (agent: Agent): Promise<Scrobbles> => { 1037 + const getRockskyUserScrobbles = async ( 1038 + agent: Agent, 1039 + carReader: CarReader, 1040 + ): Promise<Scrobbles> => { 1038 1041 const results: { 1039 1042 value: Scrobble.Record; 1040 1043 uri: string; ··· 1042 1045 }[] = []; 1043 1046 1044 1047 try { 1045 - logger.info(`Fetching repository CAR file for scrobbles...`); 1046 - 1047 - const repoRes = await agent.com.atproto.sync.getRepo({ 1048 - did: agent.assertDid, 1049 - }); 1050 - 1051 - const carReader = await CarReader.fromBytes(new Uint8Array(repoRes.data)); 1052 1048 const collection = "app.rocksky.scrobble"; 1049 + logger.info`Extracting ${collection} records from CAR file ...`; 1053 1050 1054 1051 for await (const { cid, bytes } of carReader.blocks()) { 1055 1052 try {