Skip to content

Commit

Permalink
feat: pre-esm changes
Browse files Browse the repository at this point in the history
* add js suffix for all relative imports
* use type import and export

Note this is not marked as breaking change since it still compiles on commonjs.

Part of #218
  • Loading branch information
myfreeer committed Oct 5, 2024
1 parent 4a231e0 commit df152cb
Show file tree
Hide file tree
Showing 41 changed files with 292 additions and 284 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
],
"require-atomic-updates": [
0
]
],
"@typescript-eslint/consistent-type-imports": "warn"
}
}
4 changes: 2 additions & 2 deletions src/downloader/adjust-concurrency.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {adjustConcurrency as logger} from '../logger/logger';
import type {DownloaderWithMeta} from './types';
import {adjustConcurrency as logger} from '../logger/logger.js';
import type {DownloaderWithMeta} from './types.js';

export function adjust(downloader: DownloaderWithMeta): void {
const {meta} = downloader;
Expand Down
16 changes: 8 additions & 8 deletions src/downloader/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export {adjust} from './adjust-concurrency';
export {AbstractDownloader} from './main';
export {MultiThreadDownloader} from './multi';
export {PipelineExecutorImpl} from './pipeline-executor-impl';
export {SingleThreadDownloader} from './single';
export * as types from './types';
export {WorkerPool} from './worker-pool';
export * as workerType from './worker-type';
export {adjust} from './adjust-concurrency.js';
export {AbstractDownloader} from './main.js';
export {MultiThreadDownloader} from './multi.js';
export {PipelineExecutorImpl} from './pipeline-executor-impl.js';
export {SingleThreadDownloader} from './single.js';
export * as types from './types.js';
export {WorkerPool} from './worker-pool.js';
export * as workerType from './worker-type.js';
23 changes: 8 additions & 15 deletions src/downloader/main.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import PQueue from 'p-queue';
import type {HTTPError} from 'got';
import URI from 'urijs';
import {
DownloadOptions,
mergeOverrideOptions,
StaticDownloadOptions
} from '../options';
import {
normalizeResource,
RawResource,
Resource,
ResourceType
} from '../resource';
import {error, notFound, skip} from '../logger/logger';
import {importDefaultFromPath} from '../util';
import type {DownloaderStats, DownloaderWithMeta} from './types';
import {PipelineExecutorImpl} from './pipeline-executor-impl';
import type {DownloadOptions, StaticDownloadOptions} from '../options.js';
import {mergeOverrideOptions} from '../options.js';
import type {RawResource, Resource} from '../resource.js';
import {normalizeResource, ResourceType} from '../resource.js';
import {error, notFound, skip} from '../logger/logger.js';
import {importDefaultFromPath} from '../util.js';
import type {DownloaderStats, DownloaderWithMeta} from './types.js';
import {PipelineExecutorImpl} from './pipeline-executor-impl.js';

export abstract class AbstractDownloader implements DownloaderWithMeta {
readonly queue: PQueue;
Expand Down
15 changes: 8 additions & 7 deletions src/downloader/multi.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import path from 'path';
import {WorkerPool, WorkerFactory} from './worker-pool';
import type {RawResource, Resource} from '../resource';
import type {DownloadWorkerMessage} from './types';
import type {StaticDownloadOptions} from '../options';
import type {DownloadResource} from '../life-cycle/types';
import {skip} from '../logger/logger';
import {AbstractDownloader} from './main';
import type {WorkerFactory} from './worker-pool.js';
import {WorkerPool} from './worker-pool.js';
import type {RawResource, Resource} from '../resource.js';
import type {DownloadWorkerMessage} from './types.js';
import type {StaticDownloadOptions} from '../options.js';
import type {DownloadResource} from '../life-cycle/types.js';
import {skip} from '../logger/logger.js';
import {AbstractDownloader} from './main.js';

export interface MultiThreadDownloaderOptions extends StaticDownloadOptions {
pathToWorker?: string;
Expand Down
14 changes: 7 additions & 7 deletions src/downloader/pipeline-executor-impl.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import type {StaticDownloadOptions} from '../options';
import type {StaticDownloadOptions} from '../options.js';
import type {
CreateResourceArgument,
Resource,
ResourceEncoding,
ResourceType
} from '../resource';
} from '../resource.js';
import type {
DownloadResource,
ProcessingLifeCycle,
RequestOptions,
SubmitResourceFunc
} from '../life-cycle/types';
} from '../life-cycle/types.js';
// noinspection ES6PreferShortImport
import type {PipelineExecutor} from '../life-cycle/pipeline-executor';
import type {Cheerio} from '../types';
import type {DownloaderWithMeta} from './types';
import type {WorkerInfo} from './worker-pool';
import type {PipelineExecutor} from '../life-cycle/pipeline-executor.js';
import type {Cheerio} from '../types.js';
import type {DownloaderWithMeta} from './types.js';
import type {WorkerInfo} from './worker-pool.js';

/**
* Pipeline executor
Expand Down
13 changes: 8 additions & 5 deletions src/downloader/single.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {AbstractDownloader} from './main';
import type {Resource} from '../resource';
import type {StaticDownloadOptions} from '../options';
import {skip} from '../logger/logger';
import type {DownloadResource, SubmitResourceFunc} from '../life-cycle/types';
import {AbstractDownloader} from './main.js';
import type {Resource} from '../resource.js';
import type {StaticDownloadOptions} from '../options.js';
import {skip} from '../logger/logger.js';
import type {
DownloadResource,
SubmitResourceFunc
} from '../life-cycle/types.js';

export class SingleThreadDownloader extends AbstractDownloader {
readonly init: Promise<void>;
Expand Down
4 changes: 2 additions & 2 deletions src/downloader/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {MessagePort} from 'worker_threads';
import type {DownloadOptions} from '../options';
import type {RawResource} from '../resource';
import type {DownloadOptions} from '../options.js';
import type {RawResource} from '../resource.js';

export interface DownloaderStats {
firstPeriodCount: number;
Expand Down
14 changes: 7 additions & 7 deletions src/downloader/worker-pool.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {MessagePort, WorkerOptions} from 'worker_threads';
import type {URL} from 'url';
import {Worker} from 'worker_threads';
import * as logger from '../logger/logger';
import type {LogWorkerMessage} from './worker-type';
import {
import type {URL} from 'url';
import * as logger from '../logger/logger.js';
import type {LogWorkerMessage} from './worker-type.js';
import type {
PendingPromise,
PendingPromiseWithBody,
WorkerMessage,
WorkerMessageType
} from './types';
WorkerMessage
} from './types.js';
import {WorkerMessageType} from './types.js';

export interface WorkerInfo {
readonly id: number;
Expand Down
6 changes: 3 additions & 3 deletions src/downloader/worker-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {logLevels} from '../logger/logger-worker';
import type * as logger from '../logger/logger';
import {WorkerMessage, WorkerMessageType} from './types';
import type {logLevels} from '../logger/logger-worker.js';
import type * as logger from '../logger/logger.js';
import type {WorkerMessage, WorkerMessageType} from './types.js';

export interface WorkerLog<T = unknown> {
logger: keyof typeof logger;
Expand Down
33 changes: 15 additions & 18 deletions src/downloader/worker.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import {parentPort, workerData} from 'worker_threads';
import {
DownloadOptions,
mergeOverrideOptions,
StaticDownloadOptions
} from '../options';
import type {DownloadResource, SubmitResourceFunc} from '../life-cycle/types';
import {
normalizeResource,
prepareResourceForClone,
RawResource,
Resource
} from '../resource';
import {skip} from '../logger/logger';
import {importDefaultFromPath} from '../util';
import {DownloadWorkerMessage, WorkerMessageType} from './types';
import {PipelineExecutorImpl} from './pipeline-executor-impl';
import type {DownloadOptions, StaticDownloadOptions} from '../options.js';
import {mergeOverrideOptions} from '../options.js';
import type {
DownloadResource,
SubmitResourceFunc
} from '../life-cycle/types.js';
import type {RawResource, Resource} from '../resource.js';
import {normalizeResource, prepareResourceForClone} from '../resource.js';
import {skip} from '../logger/logger.js';
import {importDefaultFromPath} from '../util.js';
import type {DownloadWorkerMessage} from './types.js';
import {WorkerMessageType} from './types.js';
import {PipelineExecutorImpl} from './pipeline-executor-impl.js';
// noinspection ES6PreferShortImport
import type {PipelineExecutor} from '../life-cycle/pipeline-executor';
import type {WorkerTaskMessage} from './worker-type';
import type {PipelineExecutor} from '../life-cycle/pipeline-executor.js';
import type {WorkerTaskMessage} from './worker-type.js';

const {pathToOptions, overrideOptions}: {
pathToOptions: string,
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * as logger from './logger/logger';
export * as downloader from './downloader/index';
export * as lifeCycle from './life-cycle/index';
export * as io from './io';
export * as options from './options';
export * as resource from './resource';
export {SourceDefinition} from './sources';
export * as util from './util';
export * as logger from './logger/logger.js';
export * as downloader from './downloader/index.js';
export * as lifeCycle from './life-cycle/index.js';
export * as io from './io.js';
export * as options from './options.js';
export * as resource from './resource.js';
export type {SourceDefinition} from './sources.js';
export * as util from './util.js';
4 changes: 2 additions & 2 deletions src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type {ObjectEncodingOptions} from 'fs';
import fs from 'fs';
import {dirname} from 'path';
import {mkdirp} from 'mkdirp';
import type {ResourceBody, ResourceEncoding} from './resource';
import {error as errorLogger, mkdir as mkdirLogger} from './logger/logger';
import type {ResourceBody, ResourceEncoding} from './resource.js';
import {error as errorLogger, mkdir as mkdirLogger} from './logger/logger.js';

export const mkdirRetry = async (dir: string, retry = 3): Promise<void> => {
let error: unknown | void;
Expand Down
13 changes: 7 additions & 6 deletions src/life-cycle/adapters.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import {load} from 'cheerio';
import {Resource, ResourceEncoding, ResourceType} from '../resource';
import type {Resource, ResourceEncoding} from '../resource.js';
import {ResourceType} from '../resource.js';
import type {
AsyncResult,
DownloadResource,
LinkRedirectFunc,
ProcessResourceAfterDownloadFunc,
ProcessResourceBeforeDownloadFunc,
SubmitResourceFunc
} from './types';
import {toString} from '../util';
import type {StaticDownloadOptions} from '../options';
import type {PipelineExecutor} from './pipeline-executor';
import type {Cheerio, CheerioStatic} from '../types';
} from './types.js';
import {toString} from '../util.js';
import type {StaticDownloadOptions} from '../options.js';
import type {PipelineExecutor} from './pipeline-executor.js';
import type {Cheerio, CheerioStatic} from '../types.js';

export interface SkipProcessFunc {
(url: string, element: Cheerio | null, parent: Resource | null): boolean;
Expand Down
30 changes: 15 additions & 15 deletions src/life-cycle/default-life-cycle.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type {ProcessingLifeCycle} from './types';
import {skipLinks} from './skip-links';
import {detectResourceType} from './detect-resource-type';
import {createResource} from '../resource';
import {downloadResource} from './download-resource';
import {processHtml} from './process-html';
import {processHtmlMetaRefresh} from './process-html-meta';
import {processCss} from './process-css';
import {processSiteMap} from './process-site-map';
import {processSvg} from './process-svg';
import {saveHtmlToDisk} from './save-html-to-disk';
import {saveResourceToDisk} from './save-resource-to-disk';
import {processRedirectedUrl} from './adapters';
import {downloadStreamingResource} from './download-streaming-resource';
import {readOrCopyLocalResource} from './read-or-copy-local-resource';
import type {ProcessingLifeCycle} from './types.js';
import {skipLinks} from './skip-links.js';
import {detectResourceType} from './detect-resource-type.js';
import {createResource} from '../resource.js';
import {downloadResource} from './download-resource.js';
import {processHtml} from './process-html.js';
import {processHtmlMetaRefresh} from './process-html-meta.js';
import {processCss} from './process-css.js';
import {processSiteMap} from './process-site-map.js';
import {processSvg} from './process-svg.js';
import {saveHtmlToDisk} from './save-html-to-disk.js';
import {saveResourceToDisk} from './save-resource-to-disk.js';
import {processRedirectedUrl} from './adapters.js';
import {downloadStreamingResource} from './download-streaming-resource.js';
import {readOrCopyLocalResource} from './read-or-copy-local-resource.js';

/**
* Get a copy of default life cycle
Expand Down
4 changes: 2 additions & 2 deletions src/life-cycle/detect-resource-type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ResourceType} from '../resource';
import {arrayToMap, isSiteMap} from '../util';
import {ResourceType} from '../resource.js';
import {arrayToMap, isSiteMap} from '../util.js';

// immutable
export const binaryExtension = arrayToMap([
Expand Down
17 changes: 9 additions & 8 deletions src/life-cycle/download-resource.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import got, {
import type {
BeforeRetryHook,
NormalizedOptions,
Options,
RequestError,
TimeoutError
RequestError
} from 'got';
import got, {TimeoutError} from 'got';
import type {Response} from 'got/dist/source/as-promise';
import type {DownloadResource, RequestOptions} from './types';
import {generateSavePath, Resource, ResourceType} from '../resource';
import type {StaticDownloadOptions} from '../options';
import * as logger from '../logger/logger';
import {isUrlHttp, sleep} from '../util';
import type {DownloadResource, RequestOptions} from './types.js';
import type {Resource} from '../resource.js';
import {generateSavePath, ResourceType} from '../resource.js';
import type {StaticDownloadOptions} from '../options.js';
import * as logger from '../logger/logger.js';
import {isUrlHttp, sleep} from '../util.js';
import URI from 'urijs';

/** Take logs before retry */
Expand Down
21 changes: 12 additions & 9 deletions src/life-cycle/download-streaming-resource.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import got, {HTTPError, RequestError} from 'got';
import type {RequestError} from 'got';
import got, {HTTPError} from 'got';
import type {Response} from 'got/dist/source/core';
import path from 'path';
import {constants, createWriteStream, promises as fs, WriteStream} from 'fs';
import {Resource, ResourceType} from '../resource';
import type {WriteStream} from 'fs';
import {constants, createWriteStream, promises as fs} from 'fs';
import type {Resource} from '../resource.js';
import {ResourceType} from '../resource.js';
import type {
AsyncResult,
DownloadResource,
DownloadResourceFunc,
RequestOptions
} from './types';
import {mkdirRetry} from '../io';
} from './types.js';
import {mkdirRetry} from '../io.js';
import {pipeline} from 'stream';
import {promisify} from 'util';
import {error as errorLogger, retry as retryLogger} from '../logger/logger';
import type {StaticDownloadOptions} from '../options';
import type {PipelineExecutor} from './pipeline-executor';
import {isUrlHttp} from '../util';
import {error as errorLogger, retry as retryLogger} from '../logger/logger.js';
import type {StaticDownloadOptions} from '../options.js';
import type {PipelineExecutor} from './pipeline-executor.js';
import {isUrlHttp} from '../util.js';

const promisifyPipeline = promisify(pipeline);

Expand Down
Loading

0 comments on commit df152cb

Please sign in to comment.