Skip to content
This repository was archived by the owner on Apr 13, 2026. It is now read-only.

Commit 2b2fe8d

Browse files
authored
feat(stream): add callback instead writable to get reponse headers/statusCode too (#227)
1 parent 514bbd3 commit 2b2fe8d

3 files changed

Lines changed: 47 additions & 7 deletions

File tree

examples/stream.mjs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,16 @@ const kGithubURL = new URL("https://github.com/");
1414
const cursor = httpie.stream("GET", new URL("NodeSecure/i18n/archive/main.tar.gz", kGithubURL), {
1515
maxRedirections: 1
1616
});
17-
await cursor(fs.createWriteStream(path.join(__dirname, "archive.tar.gz")));
17+
18+
const writable = fs.createWriteStream(path.join(__dirname, "archive.tar.gz"));
19+
20+
let code;
21+
let contentType;
22+
await cursor(({ statusCode, headers }) => {
23+
code = statusCode;
24+
contentType = headers["content-type"];
25+
26+
return writable;
27+
});
28+
29+
console.log(code, contentType);

src/stream.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import Node.js Dependencies
2-
import { Duplex, Writable } from "stream";
2+
import { Duplex } from "stream";
33

44
// Import Third-party Dependencies
55
import * as undici from "undici";
@@ -35,7 +35,9 @@ export function pipeline(
3535
}, ({ body }) => body);
3636
}
3737

38-
export type WritableStreamCallback = (writable: Writable) => Promise<undici.Dispatcher.StreamData>;
38+
export type WritableStreamCallback = (
39+
factory: undici.Dispatcher.StreamFactory
40+
) => Promise<undici.Dispatcher.StreamData>;
3941

4042
export function stream(
4143
method: HttpMethod | WebDavMethod,
@@ -49,10 +51,10 @@ export function stream(
4951
const headers = Utils.createHeaders({ headers: options.headers, authorization: options.authorization });
5052
const body = Utils.createBody(options.body, headers);
5153

52-
return (writable: Writable) => undici
54+
return (factory) => undici
5355
.stream(
5456
computedURI.url,
5557
{ method: method as HttpMethod, headers, body, dispatcher, maxRedirections },
56-
() => writable
58+
factory
5759
);
5860
}

test/stream.spec.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ afterAll(async() => {
2727
});
2828

2929
describe("stream", () => {
30+
it("should use callback dispatcher to init headers/statusCode etc.", async() => {
31+
const fileDestination = path.join(kDownloadPath, "i18n-main.tar.gz");
32+
const repositoryURL = new URL("NodeSecure/i18n/archive/main.tar.gz", kGithubURL);
33+
34+
const cursor = httpie.stream("GET", repositoryURL, {
35+
headers: {
36+
"User-Agent": "httpie",
37+
"Accept-Encoding": "gzip, deflate"
38+
},
39+
maxRedirections: 1
40+
});
41+
42+
let contentType = "";
43+
let code = 0;
44+
await cursor(({ headers, statusCode }) => {
45+
contentType = headers["content-type"] as string;
46+
code = statusCode;
47+
48+
return createWriteStream(fileDestination);
49+
});
50+
51+
expect(existsSync(fileDestination)).toStrictEqual(true);
52+
expect(contentType).toBe("application/x-gzip");
53+
expect(code).toBe(200);
54+
});
55+
3056
it("should fetch a .tar.gz of a given github repository", async() => {
3157
const fileDestination = path.join(kDownloadPath, "i18n-main.tar.gz");
3258
const repositoryURL = new URL("NodeSecure/i18n/archive/main.tar.gz", kGithubURL);
@@ -37,15 +63,15 @@ describe("stream", () => {
3763
"Accept-Encoding": "gzip, deflate"
3864
},
3965
maxRedirections: 1
40-
})(createWriteStream(fileDestination));
66+
})(() => createWriteStream(fileDestination));
4167

4268
expect(existsSync(fileDestination)).toStrictEqual(true);
4369
});
4470

4571
it("should fetch the HTML home from the local fastify server", async() => {
4672
const fileDestination = path.join(kDownloadPath, "home.html");
4773

48-
await httpie.stream("GET", "/stream/home")(createWriteStream(fileDestination));
74+
await httpie.stream("GET", "/stream/home")(() => createWriteStream(fileDestination));
4975

5076
expect(existsSync(fileDestination)).toStrictEqual(true);
5177
const [contentA, contentB] = await Promise.all([

0 commit comments

Comments
 (0)