11import * as smartweave from "smartweave" ;
2- import * as utils from "arweave/node/lib/utils.js" ;
32
43// prettier-ignore
54const argitRemoteURIRegex = '^dgit:\/\/([a-zA-Z0-9-_]{43})\/([A-Za-z0-9_.-]*)'
@@ -43,54 +42,29 @@ function addTransactionTags(tx, repo, txType) {
4342 return tx ;
4443}
4544
46- export async function updateRef ( arweave , wallet , remoteURI , name , ref ) {
45+ export async function makeUpdateRefDataItem (
46+ arData ,
47+ wallet ,
48+ remoteURI ,
49+ name ,
50+ ref
51+ ) {
4752 const { repoName } = parseArgitRemoteURI ( remoteURI ) ;
48- let tx = await arweave . createTransaction ( { data : ref } , wallet ) ;
49- tx = addTransactionTags ( tx , repoName , "update-ref" ) ;
50- tx . addTag ( "ref" , name ) ;
51- tx . addTag ( "Content-Type" , "text/plain" ) ;
52-
53- await arweave . transactions . sign ( tx , wallet ) ; // Sign transaction
54- return arweave . transactions . post ( tx ) ; // Post transaction
55- }
56-
57- export async function getRef ( arweave , remoteURI , name ) {
58- const query = {
59- op : "and" ,
60- expr1 : repoQuery ( remoteURI ) ,
61- expr2 : {
62- op : "and" ,
63- expr1 : { op : "equals" , expr1 : "Type" , expr2 : "update-ref" } ,
64- expr2 : { op : "equals" , expr1 : "ref" , expr2 : name } ,
53+ const tags = [
54+ { name : "App-Name" , value : "dgit" } ,
55+ { name : "version" , value : "0.0.1" } ,
56+ { name : "Repo" , value : repoName } ,
57+ { name : "Type" , value : "update-ref" } ,
58+ {
59+ name : "Unix-Time" ,
60+ value : Math . round ( new Date ( ) . getTime ( ) / 1000 ) . toString ( ) ,
6561 } ,
66- } ;
67- const txids = await arweave . arql ( query ) ;
68- const tx_rows = await Promise . all (
69- txids . map ( async ( txid ) => {
70- let tx_row = { } ;
71- const tx = await arweave . transactions . get ( txid ) ;
72- tx . get ( "tags" ) . forEach ( ( tag ) => {
73- const key = tag . get ( "name" , { decode : true , string : true } ) ;
74- const value = tag . get ( "value" , { decode : true , string : true } ) ;
75- if ( key === "Unix-Time" ) tx_row . unixTime = value ;
76- } ) ;
77-
78- tx_row . oid = await arweave . transactions . getData ( txid , {
79- decode : true ,
80- string : true ,
81- } ) ;
82-
83- return tx_row ;
84- } )
85- ) ;
86-
87- if ( tx_rows . length === 0 ) return "0000000000000000000000000000000000000000" ;
62+ { name : "ref" , value : name } ,
63+ { name : "Content-Type" , value : "text/plain" } ,
64+ ] ;
8865
89- // descending order
90- tx_rows . sort ( ( a , b ) => {
91- Number ( b . unixTime ) - Number ( a . unixTime ) ;
92- } ) ;
93- return tx_rows [ 0 ] . oid ;
66+ const item = await arData . createData ( { data : ref , tags } , wallet ) ;
67+ return await arData . sign ( item , wallet ) ;
9468}
9569
9670export async function pushGitObject ( arweave , wallet , remoteURI , oid , object ) {
@@ -120,7 +94,6 @@ export const makeDataItem = async (
12094 objectBuf
12195) => {
12296 const { repoName } = parseArgitRemoteURI ( remoteURI ) ;
123- const data = utils . default . bufferTob64Url ( objectBuf ) ;
12497 const tags = [
12598 { name : "App-Name" , value : "dgit" } ,
12699 { name : "version" , value : "0.0.1" } ,
@@ -134,16 +107,18 @@ export const makeDataItem = async (
134107 { name : "Content-Type" , value : "application/octet-stream" } ,
135108 ] ;
136109
137- const item = await arData . createData ( { data, tags } , wallet ) ;
110+ const item = await arData . createData ( { data : objectBuf , tags } , wallet ) ;
138111 return await arData . sign ( item , wallet ) ;
139112} ;
140113
141114export const postBundledTransaction = async (
142115 arweave ,
143116 arData ,
144117 wallet ,
118+ remoteURI ,
145119 dataItems
146120) => {
121+ const { repoName } = parseArgitRemoteURI ( remoteURI ) ;
147122 const bundle = await arData . bundleData ( dataItems ) ;
148123 const data = JSON . stringify ( bundle ) ;
149124 const tx = await arweave . createTransaction ( { data } , wallet ) ;
@@ -152,6 +127,7 @@ export const postBundledTransaction = async (
152127 tx . addTag ( "Content-Type" , "application/json" ) ;
153128 tx . addTag ( "App-Name" , "dgit" ) ;
154129 tx . addTag ( "Type" , "git-objects-bundle" ) ;
130+ tx . addTag ( "Repo" , repoName ) ;
155131
156132 await arweave . transactions . sign ( tx , wallet ) ;
157133 const uploader = await arweave . transactions . getUploader ( tx ) ;
@@ -162,56 +138,6 @@ export const postBundledTransaction = async (
162138 `${ uploader . pctComplete } % complete, ${ uploader . uploadedChunks } /${ uploader . totalChunks } `
163139 ) ;
164140 }
165- } ;
166-
167- export async function fetchGitObjects ( arweave , remoteURI ) {
168- const query = {
169- op : "and" ,
170- expr1 : repoQuery ( remoteURI ) ,
171- expr2 : { op : "equals" , expr1 : "Type" , expr2 : "push-git-object" } ,
172- } ;
173- const txids = await arweave . arql ( query ) ;
174- const objects = await Promise . all (
175- txids . map ( async ( txid ) => {
176- const tx = await arweave . transactions . get ( txid ) ;
177- let oid = "" ;
178- tx . get ( "tags" ) . forEach ( ( tag ) => {
179- const key = tag . get ( "name" , { decode : true , string : true } ) ;
180- const value = tag . get ( "value" , { decode : true , string : true } ) ;
181- if ( key === "oid" ) oid = value ;
182- } ) ;
183- const data = await arweave . transactions . getData ( txid , { decode : true } ) ;
184- return { data, oid } ;
185- } )
186- ) ;
187- return objects ;
188- }
189-
190- export async function pushPackfile (
191- arweave ,
192- wallet ,
193- remoteURI ,
194- oldoid ,
195- oid ,
196- packfile
197- ) {
198- const { repoName } = parseArgitRemoteURI ( remoteURI ) ;
199-
200- let tx = await arweave . createTransaction ( { data : packfile . packfile } , wallet ) ;
201- tx = addTransactionTags ( tx , repoName , "send-pack" ) ;
202- tx . addTag ( "oid" , oid ) ;
203- tx . addTag ( "oldoid" , oldoid ) ;
204- tx . addTag ( "filename" , packfile . filename ) ;
205-
206- await arweave . transactions . sign ( tx , wallet ) ;
207- let uploader = await arweave . transactions . getUploader ( tx ) ;
208-
209- while ( ! uploader . isComplete ) {
210- await uploader . uploadChunk ( ) ;
211- console . log (
212- `${ uploader . pctComplete } % complete, ${ uploader . uploadedChunks } /${ uploader . totalChunks } `
213- ) ;
214- }
215141
216142 // Send fee to PST holders
217143 const contractState = await smartweave . readContract ( arweave , contractId ) ;
@@ -223,70 +149,31 @@ export async function pushPackfile(
223149 ) ;
224150 pstTx . addTag ( "App-Name" , "dgit" ) ;
225151 pstTx . addTag ( "version" , "0.0.1" ) ;
152+ pstTx . addTag ( "Repo" , repoName ) ;
226153
227154 await arweave . transactions . sign ( pstTx , wallet ) ;
228155 await arweave . transactions . post ( pstTx ) ;
229- }
156+ } ;
230157
231- export async function fetchPackfiles ( arweave , remoteURI ) {
158+ export async function fetchGitObjects ( arweave , arData , remoteURI ) {
232159 const query = {
233160 op : "and" ,
234161 expr1 : repoQuery ( remoteURI ) ,
235- expr2 : { op : "equals" , expr1 : "Type" , expr2 : "send-pack " } ,
162+ expr2 : { op : "equals" , expr1 : "Type" , expr2 : "push-git-object " } ,
236163 } ;
237164 const txids = await arweave . arql ( query ) ;
238- const packfiles = await Promise . all (
165+ const objects = await Promise . all (
239166 txids . map ( async ( txid ) => {
240167 const tx = await arweave . transactions . get ( txid ) ;
241- let filename = "" ;
168+ let oid = "" ;
242169 tx . get ( "tags" ) . forEach ( ( tag ) => {
243170 const key = tag . get ( "name" , { decode : true , string : true } ) ;
244171 const value = tag . get ( "value" , { decode : true , string : true } ) ;
245- if ( key === "filename " ) filename = value ;
172+ if ( key === "oid " ) oid = value ;
246173 } ) ;
247174 const data = await arweave . transactions . getData ( txid , { decode : true } ) ;
248- return { data, filename } ;
249- } )
250- ) ;
251- return packfiles ;
252- }
253-
254- export async function getRefsOnArweave ( arweave , remoteURI ) {
255- const refs = new Map ( ) ;
256- const query = {
257- op : "and" ,
258- expr1 : repoQuery ( remoteURI ) ,
259- expr2 : { op : "equals" , expr1 : "Type" , expr2 : "update-ref" } ,
260- } ;
261- const txids = await arweave . arql ( query ) ;
262- const tx_rows = await Promise . all (
263- txids . map ( async ( txid ) => {
264- let ref = { } ;
265- const tx = await arweave . transactions . get ( txid ) ;
266- tx . get ( "tags" ) . forEach ( ( tag ) => {
267- const key = tag . get ( "name" , { decode : true , string : true } ) ;
268- const value = tag . get ( "value" , { decode : true , string : true } ) ;
269- if ( key === "Unix-Time" ) ref . unixTime = value ;
270- else if ( key === "ref" ) ref . name = value ;
271- } ) ;
272-
273- ref . oid = await arweave . transactions . getData ( txid , {
274- decode : true ,
275- string : true ,
276- } ) ;
277-
278- return ref ;
175+ return { data, oid } ;
279176 } )
280177 ) ;
281-
282- // descending order
283- tx_rows . sort ( ( a , b ) => {
284- Number ( b . unixTime ) - Number ( a . unixTime ) ;
285- } ) ;
286-
287- tx_rows . forEach ( ( ref ) => {
288- if ( ! refs . has ( ref . name ) ) refs . set ( ref . name , ref . oid ) ;
289- } ) ;
290-
291- return refs ;
178+ return objects ;
292179}
0 commit comments