Skip to content

Commit 558cfb0

Browse files
committed
Returns appropriate stream type depending on web or node environment
1 parent faea737 commit 558cfb0

File tree

8 files changed

+65
-50
lines changed

8 files changed

+65
-50
lines changed

output.pdf

-12.1 KB
Binary file not shown.

outputSnippet.pdf

16.6 KB
Binary file not shown.

output_helper.pdf

-8.68 KB
Binary file not shown.

output_merged.pdf

-20.4 KB
Binary file not shown.

src/Client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class FileForgeClient {
109109
files: File[] | fs.ReadStream[],
110110
request: FileForge.MergeRequest,
111111
requestOptions?: FileForgeClient.RequestOptions
112-
): Promise<stream.Readable> {
112+
): Promise<stream.Readable | any> {
113113
const _request = core.newFormData();
114114
const options = await serializers.GenerateRequestOptions.jsonOrThrow(request.options, {
115115
unrecognizedObjectKeys: "passthrough",

src/core/fetcher/Fetcher.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -148,25 +148,21 @@ async function fetcherImpl<R = unknown>(args: Fetcher.Args): Promise<APIResponse
148148

149149
if (response.headers.get('content-type')!.includes("application/json")){
150150

151-
const chunks: any[] = [];
152-
153-
for await (let chunk of readableStreamAsyncIterator(response.body)) {
154-
chunks.push(chunk);
155-
}
156-
157-
const buffer: Buffer = Buffer.concat(chunks);
158-
const bufferString = buffer.toString();
159-
160-
body = JSON.parse(bufferString)
151+
body = await response.json()
161152

162153
}else{
154+
if(RUNTIME.type === "node"){
155+
const { Readable } = await import('node:stream');
156+
body = { "file":Readable.from(readableStreamAsyncIterator(response.body))}
163157

164-
body = response.body
158+
}else{
159+
body = { "file":response.body }
160+
}
165161

166162
}
167163

168164
} else if (response.body != null && args.responseType === "text") {
169-
console.log("TEXT")
165+
170166
body = await response.text();
171167
} else {
172168
const text = await response.text();

src/helper.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ export interface DocumentInput {
3131
files?: AssetOrPathBuffer[];
3232
}
3333

34+
export interface ResponseURL {
35+
url: string;
36+
}
37+
38+
export interface ResponseStream {
39+
file: stream.Readable;
40+
}
41+
42+
export type ResponseObject = ResponseStream | ResponseURL;
3443

3544
/**
3645
* Generates a PDF document from web assets.
@@ -42,7 +51,7 @@ export interface DocumentInput {
4251
export async function generate_from_html(
4352
client: FileForgeClient,
4453
document: DocumentInput
45-
):Promise<any>{
54+
):Promise<ResponseObject>{
4655

4756
const files: AssetOrPathBuffer[] = document.files ?? [];
4857
files.push({ path: "/index.html", content: document.html });

tests/custom.test.ts

+46-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import stream from "stream";
22
import * as core from "../src/core";
33
import { FileForgeClient } from "../src";
4-
import { generate_from_html } from "../src/Helper";
4+
import { generate_from_html, ResponseStream, ResponseURL } from "../src/Helper";
55
import * as error from "../src/errors/index";
66
import fs from "fs";
77
import { writeFile } from "fs/promises";
@@ -52,18 +52,12 @@ describe("test", () => {
5252
{
5353
options: {}
5454
}
55-
);
56-
57-
const chunks: any[] = [];
58-
59-
for await (let chunk of pdf) {
60-
chunks.push(chunk);
61-
}
62-
63-
const buffer: Buffer = Buffer.concat(chunks);
64-
const bufferString = buffer.toString()
65-
66-
await writeFile("output.pdf", bufferString);
55+
) as ResponseStream;
56+
57+
// Write the PDF stream to a file
58+
const writeStream = fs.createWriteStream('output.pdf');
59+
pdf.file.pipe(writeStream);
60+
6761
}, 10_000_000);
6862

6963

@@ -88,11 +82,11 @@ describe("test", () => {
8882
host: true,
8983
}
9084
}
91-
);
85+
) as ResponseURL;
9286

9387
expect(pdf.url).not.toBeNull();
9488

95-
}, 10_000_000);
89+
}, 10_000_000);
9690

9791
it("should fail because of invalid api key", async () => {
9892
const htmlBlob = new Blob([HTML], {
@@ -150,18 +144,11 @@ describe("test", () => {
150144
test:false
151145
}
152146

153-
);
154-
const chunks: any[] = [];
155-
156-
for await (let chunk of pdf) {
157-
chunks.push(chunk);
158-
}
159-
160-
const buffer: Buffer = Buffer.concat(chunks);
161-
const bufferString = buffer.toString()
162-
163-
164-
await writeFile("output_helper.pdf", bufferString);
147+
) as ResponseStream;
148+
// Write the PDF stream to a file
149+
const writeStream = fs.createWriteStream('output_helper.pdf');
150+
pdf.file.pipe(writeStream);
151+
165152
}, 10_000_000);
166153

167154
it("should generate a PDF url from helper", async () => {
@@ -186,7 +173,7 @@ describe("test", () => {
186173
host:true,
187174
}
188175

189-
);
176+
) as ResponseURL;
190177

191178
expect(pdf.url).not.toBeNull();
192179
}, 10_000_000);
@@ -215,16 +202,39 @@ describe("test", () => {
215202
}
216203
);
217204

218-
const chunks: any[] = [];
205+
// Write the PDF stream to a file
206+
const writeStream = fs.createWriteStream('output_merged.pdf');
207+
pdf.file.pipe(writeStream);
208+
209+
}, 10_000_000);
210+
211+
it("should generate from html snippet", async () => {
212+
try {
213+
214+
const client = new FileForgeClient({
215+
apiKey: FILEFORGE_API_KEY
216+
});
217+
const documentInput = {
218+
html: HTML,
219+
fileName: 'example',
220+
test: false,
221+
host: false,
222+
expiresAt: new Date(Date.now() + 48 * 60 * 60 * 1000),
223+
files: [
224+
{ path: '/style.css', content: CSS },
225+
],
226+
};
219227

220-
for await (let chunk of pdf) {
221-
chunks.push(chunk);
222-
}
223-
224-
const buffer: Buffer = Buffer.concat(chunks);
225-
const bufferString = buffer.toString()
228+
const response = await generate_from_html(client, documentInput) as ResponseStream;
229+
230+
// Write the PDF stream to a file
231+
const writeStream = fs.createWriteStream('outputSnippet.pdf');
232+
response.file.pipe(writeStream);
233+
console.log('PDF generated successfully.');
234+
} catch (error) {
235+
console.error('Error generating PDF:', error);
236+
}
226237

227-
await writeFile("output_merged.pdf", bufferString);
228238
}, 10_000_000);
229239

230240
});

0 commit comments

Comments
 (0)