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

generate stream without promise #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { launch, LaunchedChrome } from 'chrome-launcher';
import * as CDP from 'chrome-remote-interface';
import { Readable, Stream } from 'stream';

import { ChromePrintOptions } from './ChromePrintOptions';
import * as CompletionTrigger from './CompletionTrigger';
Expand Down Expand Up @@ -50,6 +51,20 @@ export async function create(html: string, options?: CreateOptions): Promise<Cre
}
}

function createToStream(html: string, options?: CreateOptions): Stream {
const stream = new Readable();
(async () => {
try {
const result = await create(html, options);
stream.push(result.toBase64(), 'base64');
stream.push(null);
} catch (e) {
stream.emit('error', e);
}
})();
return stream;
}

/**
* Connects to Chrome and generates a PDF from HTML or a URL.
*
Expand Down