Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stream): add callback instead writable to get reponse headers/statusCode too #227

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading