Skip to content

Commit

Permalink
feat(stream): add callback instead writable to get reponse headers/st…
Browse files Browse the repository at this point in the history
…atusCode too
  • Loading branch information
Dafyh committed Feb 1, 2024
1 parent 62f4cd0 commit 124cc74
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
14 changes: 13 additions & 1 deletion examples/stream.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ const kGithubURL = new URL("https://github.com/");
const cursor = httpie.stream("GET", new URL("NodeSecure/i18n/archive/main.tar.gz", kGithubURL), {
maxRedirections: 1
});
await cursor(fs.createWriteStream(path.join(__dirname, "archive.tar.gz")));

const writable = fs.createWriteStream(path.join(__dirname, "archive.tar.gz"));

let code;
let contentType;
await cursor(({ statusCode, headers }) => {
code = statusCode;
contentType = headers["content-type"];

return writable;
});

console.log(code, contentType);
10 changes: 6 additions & 4 deletions src/stream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import Node.js Dependencies
import { Duplex, Writable } from "stream";
import { Duplex } from "stream";

// Import Third-party Dependencies
import * as undici from "undici";
Expand Down Expand Up @@ -35,7 +35,9 @@ export function pipeline(
}, ({ body }) => body);
}

export type WritableStreamCallback = (writable: Writable) => Promise<undici.Dispatcher.StreamData>;
export type WritableStreamCallback = (
factory: undici.Dispatcher.StreamFactory
) => Promise<undici.Dispatcher.StreamData>;

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

return (writable: Writable) => undici
return (factory) => undici
.stream(
computedURI.url,
{ method: method as HttpMethod, headers, body, dispatcher, maxRedirections },
() => writable
factory
);
}
30 changes: 28 additions & 2 deletions test/stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ afterAll(async() => {
});

describe("stream", () => {
it("should use callback dispatcher to init headers/statusCode etc.", async() => {
const fileDestination = path.join(kDownloadPath, "i18n-main.tar.gz");
const repositoryURL = new URL("NodeSecure/i18n/archive/main.tar.gz", kGithubURL);

const cursor = httpie.stream("GET", repositoryURL, {
headers: {
"User-Agent": "httpie",
"Accept-Encoding": "gzip, deflate"
},
maxRedirections: 1
});

let contentType = "";
let code = 0;
await cursor(({ headers, statusCode }) => {
contentType = headers["content-type"] as string;
code = statusCode;

return createWriteStream(fileDestination);
});

expect(existsSync(fileDestination)).toStrictEqual(true);
expect(contentType).toBe("application/x-gzip");
expect(code).toBe(200);
});

it("should fetch a .tar.gz of a given github repository", async() => {
const fileDestination = path.join(kDownloadPath, "i18n-main.tar.gz");
const repositoryURL = new URL("NodeSecure/i18n/archive/main.tar.gz", kGithubURL);
Expand All @@ -37,15 +63,15 @@ describe("stream", () => {
"Accept-Encoding": "gzip, deflate"
},
maxRedirections: 1
})(createWriteStream(fileDestination));
})(() => createWriteStream(fileDestination));

expect(existsSync(fileDestination)).toStrictEqual(true);
});

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

await httpie.stream("GET", "/stream/home")(createWriteStream(fileDestination));
await httpie.stream("GET", "/stream/home")(() => createWriteStream(fileDestination));

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

0 comments on commit 124cc74

Please sign in to comment.