-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
258 lines (229 loc) · 6.98 KB
/
Copy pathindex.js
File metadata and controls
258 lines (229 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const arbundles = require("arbundles");
const processStream = require("arbundles/stream");
const {
createReadStream,
createWriteStream,
existsSync,
mkdirSync,
openSync,
readFileSync,
readdirSync,
writeFileSync,
closeSync,
readSync,
} = require("fs");
//Needed as Arweave redirects with a 302
const https = require("follow-redirects/https");
const { exit } = require("process");
const inputFolder = "./bundles";
const outputFolder = "./output";
const txIdRegex = /^(\w|-){43}$/;
const startsWithArDriveRegex = /^ArDrive/;
const formatJSON = (object) => JSON.stringify(object, null, "\t");
const isMetadataTx = (item) => item.tags.some((tag) => tag.name === "ArFS");
const isArFSDataTx = (item) => {
return (
!isMetadataTx(item) &&
item.tags.some(
(tag) =>
tag.name === "App-Name" &&
tag.value.match(startsWithArDriveRegex)
)
);
};
//Handle arguments from STDIN
const myArgs = process.argv.slice(2);
const downloadBundle = (txId) => {
if (existsSync(`${inputFolder}/${txId}`)) {
console.log(
`Bundle with txId ${txId} already exists in ${inputFolder}`
);
return;
}
console.log(`Downloading bundle with txId ${txId}...`);
let reqURL = `https://arweave.net/${txId}`;
//Need to promisify
return new Promise(function (resolve, reject) {
let promise = https.get(reqURL, (res) => {
const writeStream = createWriteStream(`${inputFolder}/${txId}`);
res.pipe(writeStream);
writeStream.on("finish", () => {
writeStream.close();
console.log(`Finished downloading bundle with txId ${txId}`);
resolve(promise);
});
});
promise.onerror = reject;
});
};
const checkTxArg = async (myArgs) => {
if (myArgs.length) {
if (myArgs[0].match(txIdRegex) != null) {
let txId = myArgs[0].match(txIdRegex)[0];
await downloadBundle(txId);
} else {
console.log("Invalid Arweave TX");
exit;
}
}
};
const createOutputFolderFor = (bundleList) => {
// Create output folder if not exists
if (!existsSync(outputFolder)) {
mkdirSync(outputFolder);
}
bundleList.forEach((name) => {
const outputFilePath = `${outputFolder}/${name}`;
if (!existsSync(outputFilePath)) {
mkdirSync(outputFilePath);
}
});
};
const getBundlesFiles = (bundlesFolder) =>
readdirSync(bundlesFolder).filter((file) => file !== ".gitkeep");
const getTxTags = (item) =>
item.tags.reduce((prev, curr) => {
return {
...prev,
[curr.name]: curr.value,
};
}, {});
const getFileDataBuffer = (bundlePath, dataItem) => {
const { dataOffset, dataSize } = dataItem;
console.log(
`... unpacking item data with size ${dataSize} at offset ${dataOffset}...`
);
const dataBuffer = Buffer.alloc(dataSize);
const fd = openSync(bundlePath);
readSync(fd, dataBuffer, 0, dataSize, dataOffset);
closeSync(fd);
return dataBuffer;
};
const run = async () => {
//Check arguments for a TxID
await checkTxArg(myArgs);
let bundleFiles = getBundlesFiles(inputFolder);
if (myArgs[0] != undefined) {
const txIdArg = myArgs[0].match(txIdRegex) ? myArgs[0] : undefined;
bundleFiles = bundleFiles.filter((path) => path.includes(txIdArg));
}
//Check if there are bundles to unpack
if (!getBundlesFiles(inputFolder).length) {
console.log("No bundles provided");
return;
}
createOutputFolderFor(bundleFiles);
const arFSDataTxIDToTagsMap = {};
const arFSDataTxIDToMetadataMap = {};
for await (const bundleFileName of bundleFiles) {
console.log(`Working on bundle with txId ${bundleFileName}...`);
const bundlePath = `${inputFolder}/${bundleFileName}`;
const outputPath = `${outputFolder}/${bundleFileName}`;
const stream = createReadStream(bundlePath);
const dataItemsIterable = await processStream.default(stream);
const dataItemCount = dataItemsIterable.length;
console.log(`Data item count: ${dataItemCount}`);
let currentItemIndex = 0;
dataItemsIterable.forEach((item) => {
const txId = item.id;
console.log(
`(${++currentItemIndex}/${dataItemCount}) Unpacking data item with txId ${txId}...`
);
let tagsOutput = {
dataItemTxId: txId,
tags: getTxTags(item),
};
const dataItemBuffer = getFileDataBuffer(bundlePath, item);
// Write out a specialized .TAGS.json file when it's an ArFS dataTx
if (isArFSDataTx(item)) {
// If there's a metadata for this dataTx cached, write it out with tags and purge it
if (arFSDataTxIDToMetadataMap[txId]) {
writeFileSync(
`${outputPath}/${txId}.TAGS.json`,
formatJSON({
...tagsOutput,
...arFSDataTxIDToMetadataMap[txId],
})
);
delete arFSDataTxIDToMetadataMap[txId];
} else {
// Else cache the dataTx's tags for write out when the metadataTx appears later
console.log(`...Caching tags for dataTxID ${txId}...`);
arFSDataTxIDToTagsMap[txId] = tagsOutput;
}
} else {
// Write out ordinary data item tx tags
writeFileSync(
`${outputPath}/${txId}.TAGS.json`,
formatJSON(tagsOutput)
);
}
// Discover and extract ArFS metadata if possible
const { arFSMetadata, isPrivateArFSData } = (() => {
let isPrivateArFSData = false;
if (!isMetadataTx(item)) {
return { undefined, isPrivateArFSData };
}
// Stub out private metadata if necessary
isPrivateArFSData = item.tags.some(
(tag) => tag.name === "Cipher"
);
const arFSMetadata = isPrivateArFSData
? { encrypted: "encrypted" }
: JSON.parse(dataItemBuffer.toString());
return { arFSMetadata, isPrivateArFSData };
})();
// Match ArFS metadata with an ArFS dataTx
if (arFSMetadata) {
// If there's a dataTx seeking metadata for its tags, finally write them out
const dataTxId = arFSMetadata.dataTxId;
if (dataTxId) {
if (arFSDataTxIDToTagsMap[dataTxId]) {
writeFileSync(
`${outputPath}/${dataTxId}.TAGS.json`,
formatJSON({
...arFSDataTxIDToTagsMap[dataTxId],
metaDataItemTxId: txId,
metadata: arFSMetadata,
})
);
delete arFSDataTxIDToTagsMap[dataTxId];
} else {
// Else enqueue the metadata for later output alongside the dataTx's tags
console.log(
`...Enqueuing metadata for dataTxID ${dataTxId}...`
);
arFSDataTxIDToMetadataMap[dataTxId] = {
metadataTxId: txId,
metadata: arFSMetadata,
};
}
} else {
// Is a standalone metadata. Nothing to do.
}
}
// Write out the data item data
writeFileSync(
`${outputPath}/${txId}`,
arFSMetadata && !isPrivateArFSData
? formatJSON(arFSMetadata)
: dataItemBuffer
);
});
// Cleanup any unexpected "orphans"
for (dataTxId of Object.keys(arFSDataTxIDToTagsMap)) {
orphanTags = arFSDataTxIDToTagsMap[dataTxId];
console.log(
`Writing out orphan tags for txId ${orphanTags.dataItemTxId}...`
);
writeFileSync(
`${outputPath}/${orphanTags.dataItemTxId}.TAGS.json`,
formatJSON(orphanTags)
);
}
// You could also write out dataTx tag files here for orphaned metadata
// NOTE: This wouldn't make sense for folder metadata or an entity rename metadata
}
console.log("Bundles unpacked to ./output folder");
};
run();