Skip to content

Commit 9e8db61

Browse files
committed
Bundled data transaction for git objects
1 parent d9ae5fc commit 9e8db61

2 files changed

Lines changed: 76 additions & 11 deletions

File tree

bin/helper.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import Arweave from "arweave";
1313
import {
1414
getRefsOnArweave,
1515
pushGitObject,
16+
makeDataItem,
1617
updateRef,
1718
parseArgitRemoteURI,
19+
postBundledTransaction,
1820
} from "./lib/arweave.js";
1921

20-
import * as deepHash from "../node_modules/arweave/node/lib/deephash.js";
21-
import ArweaveData from "../node_modules/arweave-data/pkg/dist-node/index.js";
22+
import * as deepHash from "arweave/node/lib/deepHash.js";
23+
import ArweaveData from "arweave-data/pkg/dist-node/index.js";
2224

2325
const _timeout = async (duration) => {
2426
return new Promise((resolve, reject) => {
@@ -79,11 +81,9 @@ export default class Helper {
7981
const deps = {
8082
utils: Arweave.utils,
8183
crypto: Arweave.crypto,
82-
deepHash: deepHash,
84+
deepHash: deepHash.default.default,
8385
};
84-
console.error(ArweaveData.default);
8586
this.ArData = ArweaveData.default(deps);
86-
console.error(this.ArData);
8787
}
8888

8989
// OK
@@ -131,7 +131,7 @@ export default class Helper {
131131
forPush ? this.debug("cmd", "list", "for-push") : this.debug("cmd, list");
132132

133133
const refs = await this._fetchRefs();
134-
134+
console.error("refs", refs);
135135
// tslint:disable-next-line:forin
136136
for (const ref in refs) {
137137
this._send(refs[ref] + " " + ref);
@@ -255,13 +255,14 @@ export default class Helper {
255255
try {
256256
const refs = await this._fetchRefs();
257257
const remote = refs[dst];
258+
const dataItems = [];
258259

259260
const srcBranch = src.split("/").pop();
260261
const dstBranch = dst.split("/").pop();
261262

262263
const revListCmd = remote
263264
? `git rev-list --objects --left-only ${srcBranch}...${this.name}/${dstBranch}`
264-
: "git rev-list --objects --all";
265+
: `git rev-list --objects ${srcBranch}`;
265266

266267
const objects = shell
267268
.exec(revListCmd, { silent: true })
@@ -299,10 +300,16 @@ export default class Helper {
299300

300301
for (const oid of objects) {
301302
const object = await this.git.load(oid);
302-
// Check object already exists on arweave before pushing
303-
puts.push(
304-
pushGitObject(this._arweave, this.wallet, this.url, oid, object)
303+
304+
const dataItem = await makeDataItem(
305+
this.ArData,
306+
this.wallet,
307+
this.url,
308+
oid,
309+
object
305310
);
311+
console.error("oid", oid);
312+
dataItems.push(dataItem);
306313
}
307314

308315
head = objects[0];
@@ -316,7 +323,12 @@ export default class Helper {
316323
// upload git objects
317324
try {
318325
spinner = ora("Uploading git objects to arweave").start();
319-
await Promise.all(puts);
326+
await postBundledTransaction(
327+
this._arweave,
328+
this.ArData,
329+
this.wallet,
330+
dataItems
331+
);
320332
spinner.succeed("Git objects uploaded to arweave");
321333
} catch (err) {
322334
spinner.fail(

bin/lib/arweave.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as smartweave from "smartweave";
2+
import * as utils from "arweave/node/lib/utils.js";
23

34
// prettier-ignore
45
const argitRemoteURIRegex = '^dgit:\/\/([a-zA-Z0-9-_]{43})\/([A-Za-z0-9_.-]*)'
@@ -111,6 +112,58 @@ export async function pushGitObject(arweave, wallet, remoteURI, oid, object) {
111112
}
112113
}
113114

115+
export const makeDataItem = async (
116+
arData,
117+
wallet,
118+
remoteURI,
119+
oid,
120+
objectBuf
121+
) => {
122+
const { repoName } = parseArgitRemoteURI(remoteURI);
123+
const data = utils.default.bufferTob64Url(objectBuf);
124+
const tags = [
125+
{ name: "App-Name", value: "dgit" },
126+
{ name: "version", value: "0.0.1" },
127+
{ name: "Repo", value: repoName },
128+
{ name: "Type", value: "push-git-object" },
129+
{
130+
name: "Unix-Time",
131+
value: Math.round(new Date().getTime() / 1000).toString(),
132+
},
133+
{ name: "oid", value: oid },
134+
{ name: "Content-Type", value: "application/octet-stream" },
135+
];
136+
137+
const item = await arData.createData({ data, tags }, wallet);
138+
return await arData.sign(item, wallet);
139+
};
140+
141+
export const postBundledTransaction = async (
142+
arweave,
143+
arData,
144+
wallet,
145+
dataItems
146+
) => {
147+
const bundle = await arData.bundleData(dataItems);
148+
const data = JSON.stringify(bundle);
149+
const tx = await arweave.createTransaction({ data }, wallet);
150+
tx.addTag("Bundle-Format", "json");
151+
tx.addTag("Bundle-Version", "1.0.0");
152+
tx.addTag("Content-Type", "application/json");
153+
tx.addTag("App-Name", "dgit");
154+
tx.addTag("Type", "git-objects-bundle");
155+
156+
await arweave.transactions.sign(tx, wallet);
157+
const uploader = await arweave.transactions.getUploader(tx);
158+
159+
while (!uploader.isComplete) {
160+
await uploader.uploadChunk();
161+
console.error(
162+
`${uploader.pctComplete}% complete, ${uploader.uploadedChunks}/${uploader.totalChunks}`
163+
);
164+
}
165+
};
166+
114167
export async function fetchGitObjects(arweave, remoteURI) {
115168
const query = {
116169
op: "and",

0 commit comments

Comments
 (0)