Tools for learning Japanese.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

26 行
732 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://www.edrdg.org/kanjidic/kanjidic2.xml.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. };