Skip to content

Commit

Permalink
#1500@minor: Adds support for Blob.stream().
Browse files Browse the repository at this point in the history
  • Loading branch information
PlopAndRun committed Aug 4, 2024
1 parent e6f9127 commit 41c2830
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/happy-dom/src/file/Blob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer';
import { ReadableStream } from 'stream/web';
import * as PropertySymbol from '../PropertySymbol.js';

/**
Expand Down Expand Up @@ -136,6 +137,21 @@ export default class Blob {
return this[PropertySymbol.buffer].toString();
}

/**
* Returns returns a ReadableStream which upon reading returns the data contained within the Blob.
*
* @returns ReadableStream
*/
public stream(): ReadableStream {
let buffer = this[PropertySymbol.buffer];

Check warning on line 146 in packages/happy-dom/src/file/Blob.ts

View workflow job for this annotation

GitHub Actions / build (16)

'buffer' is never reassigned. Use 'const' instead

Check warning on line 146 in packages/happy-dom/src/file/Blob.ts

View workflow job for this annotation

GitHub Actions / build (18)

'buffer' is never reassigned. Use 'const' instead

Check warning on line 146 in packages/happy-dom/src/file/Blob.ts

View workflow job for this annotation

GitHub Actions / build (20)

'buffer' is never reassigned. Use 'const' instead
return new ReadableStream({
start(controller) {
controller.enqueue(buffer)

Check warning on line 149 in packages/happy-dom/src/file/Blob.ts

View workflow job for this annotation

GitHub Actions / build (16)

Insert `;`

Check warning on line 149 in packages/happy-dom/src/file/Blob.ts

View workflow job for this annotation

GitHub Actions / build (18)

Insert `;`

Check warning on line 149 in packages/happy-dom/src/file/Blob.ts

View workflow job for this annotation

GitHub Actions / build (20)

Insert `;`
controller.close();
}
});
}

/**
* Returns the object converted to string.
*
Expand Down
18 changes: 18 additions & 0 deletions packages/happy-dom/test/file/Blob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ describe('Blob', () => {
});
});

describe('stream()', () => {
it('Returns all data in a ReadableStream.', async () => {
const data = 'Test1Test2';
const blob = new Blob(['Test1', 'Test2']);
const stream = blob.stream();

let i = 0;
for await (let chunk of stream.values()) {

Check warning on line 48 in packages/happy-dom/test/file/Blob.test.ts

View workflow job for this annotation

GitHub Actions / build (16)

'chunk' is never reassigned. Use 'const' instead

Check warning on line 48 in packages/happy-dom/test/file/Blob.test.ts

View workflow job for this annotation

GitHub Actions / build (18)

'chunk' is never reassigned. Use 'const' instead

Check warning on line 48 in packages/happy-dom/test/file/Blob.test.ts

View workflow job for this annotation

GitHub Actions / build (20)

'chunk' is never reassigned. Use 'const' instead
for (let value of chunk) {

Check warning on line 49 in packages/happy-dom/test/file/Blob.test.ts

View workflow job for this annotation

GitHub Actions / build (16)

'value' is never reassigned. Use 'const' instead

Check warning on line 49 in packages/happy-dom/test/file/Blob.test.ts

View workflow job for this annotation

GitHub Actions / build (18)

'value' is never reassigned. Use 'const' instead

Check warning on line 49 in packages/happy-dom/test/file/Blob.test.ts

View workflow job for this annotation

GitHub Actions / build (20)

'value' is never reassigned. Use 'const' instead
expect(i).toBeLessThan(data.length);
expect(value).toBe(data[i].charCodeAt(0));
++i;
}
}
expect(i).toBe(data.length);
});
});

describe('toString()', () => {
it('Returns "[object Blob]".', () => {
const blob = new Blob(['TEST']);
Expand Down

0 comments on commit 41c2830

Please sign in to comment.