-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
158 lines (149 loc) · 5.31 KB
/
index.js
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
require('dotenv').config()
const { BlobServiceClient } = require("@azure/storage-blob");
const fs = require("fs/promises");
const { createHash } = require("crypto");
async function streamToBuffer(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data instanceof Buffer ? data : Buffer.from(data));
});
readableStream.on("end", () => {
resolve(Buffer.concat(chunks));
});
readableStream.on("error", reject);
});
}
const parseIfExist = (v) => (v ? JSON.parse(v) : undefined);
const formatName = (n) => n.substring('WorkspaceResourceId=/subscriptions/d3076e5f-e198-4e44-a689-837848a5f2be/resourcegroups/xmcl/providers/microsoft.operationalinsights/workspaces/defaultworkspace-d3076e5f-e198-4e44-a689-837848a5f2be-ea/'.length)
async function main() {
const connectionString =
process.env.AZURE_STORAGE_CONNECTION_STRING;
const client = BlobServiceClient.fromConnectionString(connectionString);
const events = client.getContainerClient("am-appevents");
const blobs = events.listBlobsFlat({ includeTags: true });
const handle = async (blob) => {
const shortBlobname = formatName(blob.name);
try {
const blobClient = events.getBlobClient(blob.name);
const resp = await blobClient.download();
console.log("Processing", shortBlobname);
const stringContent = (
await streamToBuffer(resp.readableStreamBody)
).toString();
const objects = stringContent
.split("\r\n")
.map((v) => v.trim())
.filter((v) => !!v)
.map(JSON.parse);
for (const e of objects) {
if (e.Name === "resource-metadata-v2") {
const { name, sha1, domain, modrinth, curseforge, forge, fabric } =
e.Properties;
const localFile = `./files-v1/${sha1}.json`;
if (!sha1) continue;
const localContent = await fs
.readFile(localFile, "utf-8")
.then(JSON.parse)
.catch(() => ({}));
const mergeForge = (old, newContent) => {
if (!newContent) return old;
if (!old) return [newContent];
const existed = old.find(
(v) =>
v.modId === newContent.modId && v.version === newContent.version
);
if (!existed) {
old.push(newContent);
}
return old;
};
const mergeCurseforge = (old, newContent) => {
if (!newContent) return old;
if (!old) return [newContent];
const existed = old.find(
(v) =>
v.projectId === newContent.projectId &&
v.fileId === newContent.fileId
);
if (!existed) {
old.push(newContent);
}
return old;
};
const mergeModrinth = (old, newContent) => {
if (!newContent) return old;
if (!old) return [newContent];
const existed = old.find(
(v) =>
v.projectId === newContent.projectId &&
v.versionId === newContent.versionId
);
if (!existed) {
old.push(newContent);
}
return old;
};
const mergeFabric = (old, newContent) => {
if (!newContent) return old;
if (!old) return newContent;
for (const c of newContent) {
const existed = old.find(
(v) => v.modId === c.modId && v.version === c.version
);
if (!existed) {
old.push(...newContent);
}
}
return old;
};
// Merge local content
const content = {
name: localContent.name || name,
domain: domain || localContent.domain,
modrinth: mergeModrinth(
localContent.modrinth,
parseIfExist(modrinth)
),
curseforge: mergeCurseforge(
localContent.curseforge,
parseIfExist(curseforge)
),
forge: mergeForge(localContent.forge, parseIfExist(forge)),
fabric: mergeFabric(localContent.fabric, parseIfExist(fabric)),
};
await fs.writeFile(localFile, JSON.stringify(content, null, 2));
} else if (e.Name === "minecraft-run-record-v2") {
const props = e.Properties;
const record = {
mods: props.mods.split(","),
runtime: parseIfExist(props.runtime),
java: parseIfExist(props.java),
};
for (const k in record.runtime) {
if (!record.runtime[k]) {
delete record.runtime[k];
}
}
const rec = JSON.stringify(record, null, 2);
const key = createHash("sha1").update(rec).digest("hex");
await fs.writeFile(`./runs-v1/${key}.json`, rec);
}
}
await blobClient.delete();
console.log("Processed", shortBlobname);
} catch (e) {
console.error("Failed to process blob", shortBlobname);
console.error(e);
}
};
const batch = [];
for await (const blob of blobs) {
batch.push(blob);
if (batch.length === 32) {
await Promise.all(batch.map(handle));
batch.length = 0;
}
}
}
main();