array.js 733 B

1234567891011121314151617181920212223242526272829303132
  1. import {getStreamContents} from './contents.js';
  2. import {identity, noop, getContentsProperty} from './utils.js';
  3. export async function getStreamAsArray(stream, options) {
  4. return getStreamContents(stream, arrayMethods, options);
  5. }
  6. const initArray = () => ({contents: []});
  7. const increment = () => 1;
  8. const addArrayChunk = (convertedChunk, {contents}) => {
  9. contents.push(convertedChunk);
  10. return contents;
  11. };
  12. const arrayMethods = {
  13. init: initArray,
  14. convertChunk: {
  15. string: identity,
  16. buffer: identity,
  17. arrayBuffer: identity,
  18. dataView: identity,
  19. typedArray: identity,
  20. others: identity,
  21. },
  22. getSize: increment,
  23. truncateChunk: noop,
  24. addChunk: addArrayChunk,
  25. getFinalChunk: noop,
  26. finalize: getContentsProperty,
  27. };