Tools for learning Japanese.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

26 lines
728 B

  1. import fetchPonyfill from 'fetch-ponyfill';
  2. import { PassThrough } from 'stream';
  3. import { createGunzip } from 'zlib';
  4. import { SOURCE_ID } from './common';
  5. export interface CreateDownloaderParams {
  6. type: typeof SOURCE_ID;
  7. url?: string;
  8. }
  9. const DEFAULT_SOURCE_URL = 'http://ftp.edrdg.org/pub/Nihongo/JMdict.gz' as const;
  10. export const createDownloader = async (params: Omit<CreateDownloaderParams, 'type'>) => {
  11. const { url = DEFAULT_SOURCE_URL } = params;
  12. const { fetch } = fetchPonyfill();
  13. const response = await fetch(url);
  14. if (!response.ok) {
  15. throw new Error(`Failed to download: ${url}`);
  16. }
  17. const rawStream = response.body as unknown as PassThrough;
  18. return rawStream
  19. .pipe(createGunzip());
  20. };