Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modified imports to work when esModuleInterop is disabled #132

Merged
merged 5 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/classes/child-pool.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import childProcess, { ChildProcess } from 'child_process';
import path from 'path';
import { ChildProcess, fork } from 'child_process';
import * as path from 'path';
import { forEach, values, flatten } from 'lodash';
import getPort from 'get-port';
import fs from 'fs';
import * as getPort from 'get-port';
import * as fs from 'fs';
import { promisify } from 'util';

const stat = promisify(fs.stat);
const fork = childProcess.fork;

export interface ChildProcessExt extends ChildProcess {
processFile?: string;
Expand Down
8 changes: 4 additions & 4 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import IORedis from 'ioredis';
import * as Redis from 'ioredis';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't we just do:

import { Redis } from 'ioredis'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will work for all cases where it's used as an interface. However, this usage won't work (or at least I can't figure out how to make it work):

new Redis();
'Redis' only refers to a type, but is being used as a value here.ts(2693)

I'm not sure why the ioredis typings are written this way.

The import in this particular file, src/classes/job.ts, could be changed to this since there are no instantiations here:

import { Redis, Pipeline } from 'ioredis';

I used the same import style across all of the files for consistency and to account for files that need to instantiate an instance. I suppose it's a matter of opinion. I could change all of the files that don't instantiate Redis to the named import style if you'd prefer.

This reply should apply to all of your comments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but in many places we are not instantiating, just using it as an interface. Besides that, I think that we should change the name of the import to IORedis to make it explicitly that it is the module name, so it will become IORedis.Redis instead of the slightly more awkward Redis.Redis.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points. I will make those changes.

import { debuglog } from 'util';
import { RetryErrors } from '../enums';
import { BackoffOptions, JobsOptions, WorkerOptions } from '../interfaces';
Expand Down Expand Up @@ -92,7 +92,7 @@ export class Job<T = any, R = any> {
const multi = client.multi();

for (const job of jobInstances) {
job.addJob(<IORedis.Redis>(multi as unknown));
job.addJob(<Redis.Redis>(multi as unknown));
}

const result = (await multi.exec()) as [null | Error, string][];
Expand Down Expand Up @@ -454,15 +454,15 @@ export class Job<T = any, R = any> {
);
}

private addJob(client: IORedis.Redis): string {
private addJob(client: Redis.Redis): string {
const queue = this.queue;

const jobData = this.asJSON();

return Scripts.addJob(client, queue, jobData, this.opts, this.id);
}

private saveAttempt(multi: IORedis.Pipeline, err: Error) {
private saveAttempt(multi: Redis.Pipeline, err: Error) {
this.attemptsMade++;
this.stacktrace = this.stacktrace || [];

Expand Down
14 changes: 7 additions & 7 deletions src/classes/redis-connection.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { EventEmitter } from 'events';
import IORedis, { Redis } from 'ioredis';
import * as Redis from 'ioredis';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above

import * as semver from 'semver';
import { load } from '../commands';
import { ConnectionOptions, RedisOptions } from '../interfaces';
import { isRedisInstance } from '../utils';

export class RedisConnection extends EventEmitter {
static minimumVersion = '5.0.0';
private _client: Redis;
private initializing: Promise<Redis>;
private _client: Redis.Redis;
private initializing: Promise<Redis.Redis>;
private closing: boolean;

constructor(private opts?: ConnectionOptions) {
Expand All @@ -24,7 +24,7 @@ export class RedisConnection extends EventEmitter {
...opts,
};
} else {
this._client = <Redis>opts;
this._client = <Redis.Redis>opts;
}

this.initializing = this.init();
Expand All @@ -38,7 +38,7 @@ export class RedisConnection extends EventEmitter {
* Waits for a redis client to be ready.
* @param {Redis} redis client
*/
static async waitUntilReady(client: IORedis.Redis) {
static async waitUntilReady(client: Redis.Redis) {
return new Promise(function(resolve, reject) {
if (client.status === 'ready') {
resolve();
Expand All @@ -60,14 +60,14 @@ export class RedisConnection extends EventEmitter {
});
}

get client(): Promise<Redis> {
get client(): Promise<Redis.Redis> {
return this.initializing;
}

private async init() {
const opts = this.opts as RedisOptions;
if (!this._client) {
this._client = new IORedis(opts);
this._client = new Redis(opts);
}

await RedisConnection.waitUntilReady(this._client);
Expand Down
2 changes: 1 addition & 1 deletion src/classes/timer-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uuid from 'uuid';
import * as uuid from 'uuid';

/**
* Keeps track on timers created with setTimeout to help clearTimeout
Expand Down
10 changes: 5 additions & 5 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import fs from 'fs';
import { Redis } from 'ioredis';
import path from 'path';
import * as fs from 'fs';
import * as Redis from 'ioredis';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above

import * as path from 'path';
import { Processor, WorkerOptions } from '../interfaces';
import { QueueBase, Repeat } from './';
import { ChildPool, pool } from './child-pool';
import { Job } from './job';
import { RedisConnection } from './redis-connection';
import sandbox from './sandbox';
import { Scripts } from './scripts';
import uuid from 'uuid';
import * as uuid from 'uuid';
import { TimerManager } from './timer-manager';
import { isRedisInstance } from '../utils';

Expand Down Expand Up @@ -53,7 +53,7 @@ export class Worker<T = any> extends QueueBase {

this.blockingConnection = new RedisConnection(
isRedisInstance(opts.connection)
? (<Redis>opts.connection).duplicate()
? (<Redis.Redis>opts.connection).duplicate()
: opts.connection,
);
this.blockingConnection.on('error', this.emit.bind(this));
Expand Down
4 changes: 2 additions & 2 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
'use strict';

import IORedis from 'ioredis';
import * as Redis from 'ioredis';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


const path = require('path');
const util = require('util');
Expand All @@ -30,7 +30,7 @@ interface Command {
};
}

export const load = async function(client: IORedis.Redis) {
export const load = async function(client: Redis.Redis) {
const scripts = await loadScripts(__dirname);

scripts.forEach((command: Command) => {
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/queue-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JobsOptions } from '../interfaces';

import IORedis from 'ioredis';
import * as Redis from 'ioredis';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

import { ConnectionOptions } from './redis-options';

export enum ClientType {
Expand All @@ -10,7 +10,7 @@ export enum ClientType {

export interface QueueBaseOptions {
connection?: ConnectionOptions;
client?: IORedis.Redis;
client?: Redis.Redis;
prefix?: string; // prefix for all queue keys.
}

Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/redis-options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import IORedis from 'ioredis';
import * as Redis from 'ioredis';

export type RedisOptions = IORedis.RedisOptions & {
export type RedisOptions = Redis.RedisOptions & {
skipVersionCheck?: boolean;
};

export type ConnectionOptions = RedisOptions | IORedis.Redis;
export type ConnectionOptions = RedisOptions | Redis.Redis;
4 changes: 2 additions & 2 deletions src/test/test_bulk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Queue, Worker, Job } from '../classes';
import { expect } from 'chai';
import IORedis from 'ioredis';
import * as Redis from 'ioredis';
import { beforeEach, describe, it } from 'mocha';
import { v4 } from 'uuid';
import { removeAllQueueData } from '../utils';
Expand All @@ -16,7 +16,7 @@ describe('bulk jobs', () => {

afterEach(async function() {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

it('should process jobs', async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/test/test_clean.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Queue, QueueEvents, Worker } from '../classes';
import { delay, removeAllQueueData } from '@src/utils';
import { expect } from 'chai';
import IORedis from 'ioredis';
import * as Redis from 'ioredis';
import { after } from 'lodash';
import { beforeEach, describe, it } from 'mocha';
import { v4 } from 'uuid';
Expand All @@ -21,7 +21,7 @@ describe('Cleaner', () => {
afterEach(async function() {
await queue.close();
await queueEvents.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its late now, so I am confused why we can instantiate Redis here and not just import the Redis class by this (as commented above)

import { Redis } from 'ioredis'

});

it('should clean an empty queue', async () => {
Expand Down Expand Up @@ -127,7 +127,7 @@ describe('Cleaner', () => {
});
await worker.waitUntilReady();

const client = new IORedis();
const client = new Redis();

await queue.add('test', { some: 'data' });
await queue.add('test', { some: 'data' });
Expand Down
8 changes: 4 additions & 4 deletions src/test/test_compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Job, Worker } from '@src/classes';
import { Queue3 } from '@src/classes/compat';
import { delay, removeAllQueueData } from '@src/utils';
import { expect } from 'chai';
import IORedis from 'ioredis';
import * as Redis from 'ioredis';
import { after } from 'lodash';
import { afterEach, beforeEach, describe, it } from 'mocha';
import { v4 } from 'uuid';
Expand All @@ -24,7 +24,7 @@ describe('Compat', function() {

afterEach(async function() {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

it('should get waiting jobs', async function() {
Expand Down Expand Up @@ -294,7 +294,7 @@ describe('Compat', function() {

afterEach(async function() {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

it('should emit waiting when a job has been added', function(done) {
Expand Down Expand Up @@ -403,7 +403,7 @@ describe('Compat', function() {

afterEach(async function() {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

// it('should pause a queue until resumed', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/test/test_connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import IORedis from 'ioredis';
import * as Redis from 'ioredis';
import { Queue, QueueEvents, Job, Worker } from '@src/classes';

import { v4 } from 'uuid';
Expand All @@ -16,7 +16,7 @@ describe('connection', () => {

afterEach(async () => {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

it('should recover from a connection loss', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/test/test_delay.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Queue, Job } from '@src/classes';
import { describe, beforeEach, it } from 'mocha';
import { expect } from 'chai';
import IORedis from 'ioredis';
import * as Redis from 'ioredis';
import { v4 } from 'uuid';
import { Worker } from '@src/classes/worker';
import { QueueEvents } from '@src/classes/queue-events';
Expand All @@ -21,7 +21,7 @@ describe('Delayed jobs', function() {

afterEach(async function() {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

it('should process a delayed job only after delayed time', async function() {
Expand Down
8 changes: 4 additions & 4 deletions src/test/test_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Queue } from '@src/classes';
import { QueueEvents } from '@src/classes/queue-events';
import { Worker } from '@src/classes/worker';
import { expect } from 'chai';
import IORedis from 'ioredis';
import * as Redis from 'ioredis';
import { beforeEach, describe, it } from 'mocha';
import { v4 } from 'uuid';
import { delay, removeAllQueueData } from '@src/utils';
Expand All @@ -23,7 +23,7 @@ describe('events', function() {
afterEach(async function() {
await queue.close();
await queueEvents.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

it('should emit waiting when a job has been added', async function() {
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('events', function() {
expect(eventsLength).to.be.equal(1);

await trimmedQueue.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

it('should trim events manually', async () => {
Expand All @@ -194,6 +194,6 @@ describe('events', function() {
expect(eventsLength).to.be.equal(0);

await trimmedQueue.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});
});
4 changes: 2 additions & 2 deletions src/test/test_getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { Queue, Job } from '@src/classes';
import { describe, beforeEach, it } from 'mocha';
import { expect } from 'chai';
import IORedis from 'ioredis';
import * as Redis from 'ioredis';
import { v4 } from 'uuid';
import { Worker } from '@src/classes/worker';
import { after } from 'lodash';
Expand All @@ -23,7 +23,7 @@ describe('Jobs getters', function() {

afterEach(async function() {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

it('should get waiting jobs', async function() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/test_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Worker } from '@src/classes/worker';
import { JobsOptions } from '@src/interfaces';
import { delay, removeAllQueueData } from '@src/utils';
import { expect } from 'chai';
import IORedis from 'ioredis';
import * as Redis from 'ioredis';
import { after } from 'lodash';
import { afterEach, beforeEach, describe, it } from 'mocha';
import { v4 } from 'uuid';
Expand All @@ -23,7 +23,7 @@ describe('Job', function() {

afterEach(async () => {
await queue.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

describe('.create', function() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/test_pause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Job, Queue } from '@src/classes';
import { QueueEvents, QueueScheduler } from '../classes';
import { Worker } from '@src/classes/worker';
import { expect } from 'chai';
import IORedis from 'ioredis';
import * as Redis from 'ioredis';
import { beforeEach, describe, it } from 'mocha';
import { v4 } from 'uuid';
import { delay, removeAllQueueData } from '@src/utils';
Expand All @@ -22,7 +22,7 @@ describe('Pause', function() {
afterEach(async function() {
await queue.close();
await queueEvents.close();
await removeAllQueueData(new IORedis(), queueName);
await removeAllQueueData(new Redis(), queueName);
});

// Skipped since some side effect makes this test fail
Expand Down
Loading