Skip to content

Commit 8182a5b

Browse files
committed
refactor(SDK): refactor artifact upload inside tg_deploy TS SDK
1 parent 3bbd1d8 commit 8182a5b

File tree

1 file changed

+8
-80
lines changed

1 file changed

+8
-80
lines changed

typegraph/node/sdk/src/tg_deploy.ts

Lines changed: 8 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -61,87 +61,15 @@ export async function tgDeploy(
6161
}
6262

6363
// upload the artifacts
64-
const suffix = `${typegraph.name}/artifacts/upload-urls`;
65-
const createUploadUrlEndpoint = new URL(suffix, baseUrl);
66-
67-
const artifacts = refArtifacts.map(({ path, hash, size }) => {
68-
return {
69-
typegraphName: typegraph.name,
70-
relativePath: path,
71-
hash: hash,
72-
sizeInBytes: size,
73-
};
74-
});
75-
76-
const res = await fetch(createUploadUrlEndpoint, {
77-
method: "POST",
64+
const artifactUploader = new ArtifactUploader(
65+
baseUrl,
66+
refArtifacts,
67+
typegraph.name,
68+
auth,
7869
headers,
79-
body: JSON.stringify(artifacts),
80-
} as RequestInit);
81-
82-
if (!res.ok) {
83-
const err = await res.text();
84-
throw new Error(`Failed to get upload URLs for all artifacts: ${err}`);
85-
}
86-
87-
const uploadUrls: Array<string | null> = await res.json();
88-
if (uploadUrls.length !== artifacts.length) {
89-
const diff =
90-
`array length mismatch: ${uploadUrls.length} !== ${artifacts.length}`;
91-
throw new Error(`Failed to get upload URLs for all artifacts: ${diff}`);
92-
}
93-
94-
const uploadHeaders = new Headers({
95-
// TODO match to file extension??
96-
"Content-Type": "application/octet-stream",
97-
});
98-
99-
if (auth) {
100-
uploadHeaders.append("Authorization", auth.asHeaderValue());
101-
}
102-
103-
const results = await Promise.allSettled(uploadUrls.map(async (url, i) => {
104-
if (url == null) {
105-
console.log(`Skipping upload for artifact: ${artifacts[i].relativePath}`);
106-
return;
107-
}
108-
const meta = artifacts[i];
109-
110-
const path = join(dirname(params.typegraphPath), meta.relativePath);
111-
// TODO: stream
112-
const content = await fsp.readFile(path);
113-
const res = await fetch(url, {
114-
method: "POST",
115-
headers: uploadHeaders,
116-
body: new Uint8Array(content),
117-
} as RequestInit);
118-
if (!res.ok) {
119-
const err = await res.text();
120-
throw new Error(
121-
`Failed to upload artifact '${path}' (${res.status}): ${err}`,
122-
);
123-
}
124-
return res.json();
125-
}));
126-
127-
let errors = 0;
128-
129-
for (let i = 0; i < results.length; i++) {
130-
const result = results[i];
131-
const meta = artifacts[i];
132-
if (result.status === "rejected") {
133-
console.error(
134-
`Failed to upload artifact '${meta.relativePath}': ${result.reason}`,
135-
);
136-
errors++;
137-
} else {
138-
console.log(`Successfully uploaded artifact '${meta.relativePath}'`);
139-
}
140-
}
141-
142-
if (errors > 0) {
143-
throw new Error(`Failed to upload ${errors} artifacts`);
144-
}
70+
params.typegraphPath,
71+
);
72+
await artifactUploader.uploadArtifacts();
14573

14674
// deploy the typegraph
14775
const response = await execRequest(new URL("/typegate", baseUrl), {

0 commit comments

Comments
 (0)