Skip to content

Commit d162003

Browse files
authored
fix: update generate image buffer handling for node 22 (#624)
1 parent 39663c3 commit d162003

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

images/package-lock.json

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

images/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"tool": "node --no-warnings --loader ts-node/esm src/tools.ts"
55
},
66
"devDependencies": {
7-
"@types/node": "^20.16.11",
7+
"@types/node": "^22.15.2",
88
"@typescript-eslint/eslint-plugin": "^6.21.0",
99
"eslint": "^8.56.0",
1010
"eslint-config-standard-with-typescript": "^43.0.1",

images/src/generate.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function generateImages(
4545
});
4646

4747
// Download all images concurrently
48-
const imageUrls = response.data.map(image => image.url).filter(url => url != null)
48+
const imageUrls = response.data?.map(image => image.url).filter(url => url != null) ?? []
4949
const client = new gptscript.GPTScript()
5050
const generatedImages= await Promise.all(
5151
imageUrls.map(async (url: string) => {
@@ -80,14 +80,21 @@ async function download(client: gptscript.GPTScript, imageUrl: string): Promise<
8080
const response = await axios.get(imageUrl, {
8181
responseType: 'arraybuffer'
8282
})
83-
let content = Buffer.from(response.data, 'binary')
83+
84+
// Process the image data
85+
const imageBuffer = Buffer.from(response.data)
86+
8487
// Convert the image to webp format
85-
content = Buffer.from(await sharp(content).webp({ quality: 100 }).toBuffer())
88+
const webpBuffer = await sharp(imageBuffer).webp({ quality: 100 }).toBuffer()
8689

8790
// Generate a SHA-256 hash of the imageURL to use as the filename
8891
const filePath = `generated_image_${createHash('sha256').update(imageUrl).digest('hex').substring(0, 8)}.webp`;
8992

90-
await client.writeFileInWorkspace(`${threadId ? 'files/' : ''}${filePath}`, content.buffer);
93+
// Write the file to workspace
94+
await client.writeFileInWorkspace(
95+
`${threadId ? 'files/' : ''}${filePath}`,
96+
new Uint8Array(webpBuffer).buffer
97+
);
9198

9299
return filePath
93100
}

0 commit comments

Comments
 (0)