Skip to content

Commit

Permalink
🚑 Fixes to file import and cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanZhouDev committed Aug 8, 2023
1 parent 9e6cb3c commit 442736f
Show file tree
Hide file tree
Showing 5 changed files with 4,499 additions and 34 deletions.
33 changes: 17 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Bard {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
Origin: this.#bardURL,
Referer: this.#bardURL,
Cookie: (typeof cookie === "object") ? (Object.entries(cookie).map(([key, val]) => `${key}=${val};`).join("")) : ("__Secure-1PSID" + cookie),
Cookie: (typeof cookie === "object") ? (Object.entries(cookie).map(([key, val]) => `${key}=${val};`).join("")) : ("__Secure-1PSID=" + cookie),
};

let responseText;
Expand Down Expand Up @@ -270,7 +270,7 @@ class Bard {
};
}

#parseConfig(config) {
async #parseConfig(config) {
let result = {
useJSON: false,
imageBuffer: undefined, // Returns as {extension, filename}
Expand Down Expand Up @@ -303,17 +303,19 @@ class Bard {
typeof config.image === "string" &&
/\.(jpeg|jpg|png|webp)$/.test(config.image)
) {
import("fs")
.then(fs => {
result.imageBuffer = fs.readFileSync(
config.image,
).buffer;
})
.catch(_ => {
throw new Error(
"Loading from an image file path is not supported in a browser environment.",
);
});
let fs;

try {
fs = await import("fs")
} catch {
throw new Error(
"Loading from an image file path is not supported in a browser environment.",
);
}

result.imageBuffer = fs.readFileSync(
config.image,
).buffer;
} else {
throw new Error(
"Provide your image as a file path to a .jpeg, .jpg, .png, or .webp, or a Buffer."
Expand All @@ -331,13 +333,12 @@ class Bard {
);
}
}

return result;
}

// Ask Bard a question!
async ask(message, config) {
let { useJSON, imageBuffer, ids } = this.#parseConfig(config);
let { useJSON, imageBuffer, ids } = await this.#parseConfig(config);
let response = await this.#query(message, { imageBuffer, ids });
return useJSON ? response : response.content;
}
Expand All @@ -348,7 +349,7 @@ class Bard {
ids = ids;

async ask(message, config) {
let { useJSON, imageBuffer } = bard.#parseConfig(config);
let { useJSON, imageBuffer } = await bard.#parseConfig(config);
let response = await bard.#query(message, {
imageBuffer,
ids: this.ids,
Expand Down
Loading

0 comments on commit 442736f

Please sign in to comment.