Skip to content

feat: allow passing a stream to KeyValueStore.setRecord#1325

Merged
B4nan merged 5 commits intoapify:masterfrom
gahabeen:master
Apr 7, 2022
Merged

feat: allow passing a stream to KeyValueStore.setRecord#1325
B4nan merged 5 commits intoapify:masterfrom
gahabeen:master

Conversation

@gahabeen
Copy link
Copy Markdown
Contributor

@gahabeen gahabeen commented Apr 6, 2022

Right now the only way for a stream to be save in a KV store is using a buffer but (correct me if I'm wrong), that would mean using the memory (while passing the stream would directly send it via the network to the s3 bucket).

Current working solution:

async function concatStreamToBuffer(stream) {
    return new Promise((resolve, reject) => {
        const chunks = [];
        stream
            .on('data', (chunk) => {
                chunks.push(chunk);
            })
            .on('error', (e) => reject(e))
            .on('end', () => {
                const buffer = Buffer.concat(chunks);
                return resolve(buffer);
            });
    });
}

Apify.main(async () => {
    const store = await Apify.openKeyValueStore();

    const videoStream = new Stream();
    // ...
    const videoBuffer = await concatStreamToBuffer(videoStream);
    await store.setValue('video', videoBuffer, { contentType: 'video/mp4' });
});

The PR would let us do:

Apify.main(async () => {
    const store = await Apify.openKeyValueStore();
    const videoStream = new Stream();
    // ...
    await store.setValue('movie', videoStream, { contentType: 'video/mp4' });
});

Thoughts?


We uncovered this issue while working on a project with @levent91.

@B4nan
Copy link
Copy Markdown
Member

B4nan commented Apr 6, 2022

What about some tests? :]

Copy link
Copy Markdown
Member

@mnmkng mnmkng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The underlying setRecord call already supports streams and I think it even validates them. I would skip the extra dependency and just relax the validation here in the SDK so that streams pass it. Or replicate the validation in the client, so that we don't get two different validations.

But a test would be nice anyway! 😄

@gahabeen
Copy link
Copy Markdown
Contributor Author

gahabeen commented Apr 6, 2022

Of course. Added tests. First wanted to discuss the matter :)

@mnmkng I feel the test is required as some restrictions are being applied to contentType for other value types (which I think is to allow saving objects as JSON without providing contentType).

Client lib implementation for reference: https://github.com/apify/apify-client-js/blob/master/src/resource_clients/key_value_store.ts

@gustavotr
Copy link
Copy Markdown
Contributor

Nice timing, I am also waiting for this PR.

Comment thread src/storages/key_value_store.js Outdated
@B4nan B4nan changed the title Allow to pass a stream for KV.setRecord value feat: allow passing a stream to KeyValueStore.setRecord Apr 7, 2022
@gahabeen
Copy link
Copy Markdown
Contributor Author

gahabeen commented Apr 7, 2022

@B4nan This should be fixed. I like it better that way too 👍

@B4nan
Copy link
Copy Markdown
Member

B4nan commented Apr 7, 2022

Not that it would matter much in here (given how small the PR is), but please dont rebase after you get a review, so we can see what you changed. We will squash merge the PR, so there will be just a single commit in master (and will have the commit message based on PR title).

@B4nan
Copy link
Copy Markdown
Member

B4nan commented Apr 7, 2022

Weird, tests started to fail? Hard to say why as I dont see what you changed because of the rebase :P

@gahabeen
Copy link
Copy Markdown
Contributor Author

gahabeen commented Apr 7, 2022

I see that we have this tests that now fails as we've changed the behaviour.
CleanShot 2022-04-07 at 11 32 03@2x

And it's not the only one, the function one fails as well.
Returning Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream which I presume comes back from outside the SDK.

@gahabeen
Copy link
Copy Markdown
Contributor Author

gahabeen commented Apr 7, 2022

Always beautiful when all lights turn 🟢 😎

@B4nan B4nan merged commit 31ebd5c into apify:master Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants