Skip to content

Commit

Permalink
feat: encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu-dixit committed Feb 11, 2025
1 parent 64edf89 commit e24bfc3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
20 changes: 0 additions & 20 deletions js/src/sdk/index2.spec.ts

This file was deleted.

12 changes: 12 additions & 0 deletions js/src/sdk/models/Entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ describe("Entity class tests", () => {
expect(connection?.appUniqueId).toBe(app);
});

it("get connection for rand", async () => {
const entity2 = new Entity(backendClient, "ckemvy" + Date.now());
let hasError = false;
try {
const connection = await entity2.getConnection({ app: "gmail" });
expect(connection?.appUniqueId).toBe("gmail");
} catch (error) {

Check warning on line 52 in js/src/sdk/models/Entity.spec.ts

View workflow job for this annotation

GitHub Actions / lint-and-prettify

'error' is defined but never used. Allowed unused caught errors must match /^_/u

Check warning on line 52 in js/src/sdk/models/Entity.spec.ts

View workflow job for this annotation

GitHub Actions / lint-and-prettify

'error' is defined but never used. Allowed unused caught errors must match /^_/u
hasError = true;
}
expect(hasError).toBe(true);
});

it("execute action", async () => {
const connectedAccount = await entity.getConnection({ app: "github" });

Expand Down
4 changes: 0 additions & 4 deletions js/src/sdk/models/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,6 @@ export class Entity {
user_uuid: this.id!,
});

if (!connectedAccounts.items || connectedAccounts.items.length === 0) {
return null;
}

for (const account of connectedAccounts.items!) {
if (account?.labels && account?.labels.includes(LABELS.PRIMARY)) {
latestAccount = account;
Expand Down
39 changes: 28 additions & 11 deletions js/src/sdk/utils/processor/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ const readFileContent = async (
path: string
): Promise<{ content: string; mimeType: string }> => {
try {
const content = require("fs").readFileSync(path, "utf-8");
return { content, mimeType: "text/plain" };
const content = require("fs").readFileSync(path);

Check warning on line 10 in js/src/sdk/utils/processor/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint-and-prettify

A `require()` style import is forbidden

Check warning on line 10 in js/src/sdk/utils/processor/fileUtils.ts

View workflow job for this annotation

GitHub Actions / lint-and-prettify

A `require()` style import is forbidden
return {
content: content.toString("base64"),
mimeType: "application/octet-stream",
};
} catch (error) {
throw new Error(`Error reading file at ${path}: ${error}`);
}
Expand All @@ -17,10 +20,16 @@ const readFileContent = async (
const readFileContentFromURL = async (
path: string
): Promise<{ content: string; mimeType: string }> => {
const response = await fetch(path);
const content = await response.text();
const mimeType = response.headers.get("content-type") || "text/plain";
return { content, mimeType };
const response = await axios.get(path, {
responseType: "arraybuffer",
});
const content = Buffer.from(response.data);
const mimeType =
response.headers["content-type"] || "application/octet-stream";
return {
content: content.toString("base64"),
mimeType,
};
};

const uploadFileToS3 = async (
Expand All @@ -29,13 +38,17 @@ const uploadFileToS3 = async (
appName: string,
mimeType: string
): Promise<string> => {
const extension = mimeType.split("/")[1] || "bin";
const response = await apiClient.actionsV2.createFileUploadUrl({
body: {
action: actionName,
app: appName,
filename: `${actionName}_${Date.now()}`,
filename: `${actionName}_${Date.now()}.${extension}`,
mimetype: mimeType,
md5: crypto.createHash("md5").update(content).digest("hex"),
md5: crypto
.createHash("md5")
.update(Buffer.from(content, "base64"))
.digest("hex"),
},
path: {
fileType: "request",
Expand All @@ -47,11 +60,15 @@ const uploadFileToS3 = async (
const s3key = data!.key;

try {
// Upload the file to the S3 bucket
await axios.put(signedURL, content);
const buffer = Buffer.from(content, "base64");
await axios.put(signedURL, buffer, {
headers: {
"Content-Type": mimeType,
"Content-Length": buffer.length,
},
});
} catch (e) {
const error = e as AxiosError;
// if error is 403, then continue
if (error instanceof AxiosError && error.response?.status === 403) {
return signedURL;
}
Expand Down

0 comments on commit e24bfc3

Please sign in to comment.