Skip to content
Merged

V3 #31

Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
export * from './src';
export * from './src/index.js';
export * from '@imqueue/core';
15 changes: 9 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@imqueue/rpc",
"version": "3.2.0",
"version": "3.2.1",
"description": "RPC-like client-service implementation over messaging queue",
"keywords": [
"redis",
Expand Down Expand Up @@ -43,7 +43,7 @@
"author": "imqueue.com <support@imqueue.com> (https://imqueue.com)",
"license": "GPL-3.0-only",
"dependencies": {
"@imqueue/core": "^3.2.0",
"@imqueue/core": "^3.2.1",
"acorn": "^8.17.0",
"typescript": "^7.0.2"
},
Expand All @@ -59,5 +59,9 @@
"types": "./index.d.ts",
"default": "./index.js"
}
},
"type": "module",
"engines": {
"node": ">=22.12.0"
}
}
26 changes: 22 additions & 4 deletions src/IMQCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
import { ICache, ICacheAdapter, ICacheConstructor } from '.';
import { RedisCache } from './cache/index.js';
import {
type ICache,
type ICacheAdapter,
type ICacheConstructor,
} from './index.js';

// ES modules provide no synchronous dynamic loading, so the built-in cache
// adapters registrable by name are enumerated statically; custom adapters
// are still registrable by class or instance
const BUILT_IN_ADAPTERS: { [name: string]: ICacheConstructor } = {
RedisCache: RedisCache as unknown as ICacheConstructor,
};

/**
* Generic cache registry
Expand All @@ -46,9 +58,15 @@ export class IMQCache {

if (typeof adapter === 'string') {
if (!self.adapters[adapter]) {
self.adapters[adapter] = <ICache>(
new (require(`${__dirname}/cache/${adapter}.js`)[adapter])()
);
const Adapter = BUILT_IN_ADAPTERS[adapter];

if (!Adapter) {
throw new TypeError(
`IMQCache: unknown cache adapter requested: ${adapter}`,
);
}

self.adapters[adapter] = new Adapter() as unknown as ICache;
}
} else {
if (!self.adapters[(adapter as ICacheConstructor).name]) {
Expand Down
38 changes: 21 additions & 17 deletions src/IMQClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@
* <support@imqueue.com> to get commercial licensing options.
*/
import IMQ, {
IMessageQueue,
ILogger,
JsonObject,
AnyJson,
type IMessageQueue,
type ILogger,
type JsonObject,
type AnyJson,
IMQ_SHUTDOWN_TIMEOUT,
} from '@imqueue/core';
import {
DEFAULT_IMQ_CLIENT_OPTIONS,
IMQClientOptions,
IMQRPCResponse,
IMQRPCRequest,
type IMQClientOptions,
type IMQRPCResponse,
type IMQRPCRequest,
IMQDelay,
IMQError,
remote,
Description,
IMQMetadata,
BEFORE_HOOK_ERROR,
AFTER_HOOK_ERROR,
} from '.';
} from './index.js';
import {
pid,
forgetPid,
Expand All @@ -49,7 +49,7 @@ import {
mkdir,
writeFile,
SIGNALS,
} from './helpers';
} from './helpers/index.js';
import { EventEmitter } from 'node:events';
import { Script } from 'node:vm';
import { spawnSync } from 'node:child_process';
Expand All @@ -60,16 +60,20 @@ import {
existsSync,
rmSync,
} from 'node:fs';
import { createRequire } from 'node:module';
import { tmpdir } from 'node:os';
import { join, dirname } from 'node:path';
import { IMQBeforeCall, IMQAfterCall } from './IMQRPCOptions';
import { type IMQBeforeCall, type IMQAfterCall } from './IMQRPCOptions.js';

// Loaded via require (not a static import) so that consumers which type-check
// CommonJS require scoped to this module: resolves the typescript package
// and loads generated CommonJS client modules from the ESM host
const cjsRequire = createRequire(import.meta.url);

// Read as a file (not a static import) so that consumers which type-check
// this source through a file: link do not need `resolveJsonModule` enabled.
const tsOptions = require('../tsconfig.json').compilerOptions as Record<
string,
unknown
>;
const tsOptions = JSON.parse(
readFileSync(new URL('../tsconfig.json', import.meta.url), 'utf8'),
).compilerOptions as Record<string, unknown>;
const RX_SEMICOLON: RegExp = /;+$/g;

/**
Expand Down Expand Up @@ -815,7 +819,7 @@ function transpileClient(src: string): string {
// blocks `typescript/lib/*`, so build a raw path that Node runs
// directly)
const tscJs = join(
dirname(require.resolve('typescript/package.json')),
dirname(cjsRequire.resolve('typescript/package.json')),
'lib',
'tsc.js',
);
Expand Down Expand Up @@ -865,7 +869,7 @@ async function compile(

if (options.compile) {
const script = new Script(js);
const context = { exports: {}, require };
const context = { exports: {}, require: cjsRequire };

script.runInNewContext(context, { filename: jsFile });

Expand Down
4 changes: 2 additions & 2 deletions src/IMQLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
import { ILogger } from '@imqueue/core';
import { type ILogger } from '@imqueue/core';

export type AcquiredLock<T> = T | boolean;
export type IMQLockTask = [(...args: any[]) => any, (...args: any[]) => any];
Expand All @@ -41,7 +41,7 @@ export interface IMQLockMetadata {
*
* @example
* ~~~typescript
* import { IMQLock, AcquiredLock } from '.';
* import { IMQLock, AcquiredLock } from './index.js';
*
* async function doSomething(): Promise<number | AcquiredLock<number>> {
* const lock: AcquiredLock<number> = await IMQLock.acquire<number>('doSomething');
Expand Down
2 changes: 1 addition & 1 deletion src/IMQMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
import { AnyJson, JsonObject } from '@imqueue/core';
import { type AnyJson, type JsonObject } from '@imqueue/core';

/**
* Arbitrary, JSON-serializable metadata bag carried alongside an IMQ request.
Expand Down
2 changes: 1 addition & 1 deletion src/IMQRPCError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
import { JsonObject } from '@imqueue/core';
import { type JsonObject } from '@imqueue/core';

export const BEFORE_HOOK_ERROR = 'Before call hook error:';
export const AFTER_HOOK_ERROR = 'After call hook error:';
Expand Down
10 changes: 5 additions & 5 deletions src/IMQRPCOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
import { DEFAULT_IMQ_OPTIONS, IMQOptions } from '@imqueue/core';
import { IMQRPCRequest } from './IMQRPCRequest';
import { IMQRPCResponse } from './IMQRPCResponse';
import { IMQService } from './IMQService';
import { IMQClient } from './IMQClient';
import { DEFAULT_IMQ_OPTIONS, type IMQOptions } from '@imqueue/core';
import { type IMQRPCRequest } from './IMQRPCRequest.js';
import { type IMQRPCResponse } from './IMQRPCResponse.js';
import { IMQService } from './IMQService.js';
import { IMQClient } from './IMQClient.js';

/**
* Hook invoked before a service method call is dispatched.
Expand Down
4 changes: 2 additions & 2 deletions src/IMQRPCRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
import { JsonObject } from '@imqueue/core';
import { IMQMetadata } from './IMQMetadata';
import { type JsonObject } from '@imqueue/core';
import { IMQMetadata } from './IMQMetadata.js';

/**
* Request message data structure to be handled by a service.
Expand Down
4 changes: 2 additions & 2 deletions src/IMQRPCResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
import { JsonObject } from '@imqueue/core';
import { IMQRPCError, IMQRPCRequest } from '.';
import { type JsonObject } from '@imqueue/core';
import { type IMQRPCError, type IMQRPCRequest } from './index.js';

/**
* Response message data structure that a service replies with to handled
Expand Down
4 changes: 2 additions & 2 deletions src/IMQRequestContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* <support@imqueue.com> to get commercial licensing options.
*/
import { AsyncLocalStorage } from 'node:async_hooks';
import { IMQMetadata } from './IMQMetadata';
import { IMQRPCRequest } from './IMQRPCRequest';
import { IMQMetadata } from './IMQMetadata.js';
import { type IMQRPCRequest } from './IMQRPCRequest.js';

const storage = new AsyncLocalStorage<IMQRPCRequest>();

Expand Down
30 changes: 15 additions & 15 deletions src/IMQService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,34 @@
* <support@imqueue.com> to get commercial licensing options.
*/
import IMQ, {
JsonObject,
ILogger,
IMessageQueue,
type JsonObject,
type ILogger,
type IMessageQueue,
profile,
IMQ_SHUTDOWN_TIMEOUT,
} from '@imqueue/core';
import {
TypesDescription,
type TypesDescription,
IMQRPCDescription,
IMQRPCRequest,
IMQRPCResponse,
IMQServiceOptions,
type IMQRPCRequest,
type IMQRPCResponse,
type IMQServiceOptions,
IMQError,
expose,
ICache,
ServiceClassDescription,
MethodsCollectionDescription,
type ICache,
type ServiceClassDescription,
type MethodsCollectionDescription,
DEFAULT_IMQ_SERVICE_OPTIONS,
AFTER_HOOK_ERROR,
BEFORE_HOOK_ERROR,
DEFAULT_IMQ_METRICS_SERVER_OPTIONS,
} from '.';
import { SIGNALS } from './helpers';
} from './index.js';
import { SIGNALS } from './helpers/index.js';
import { cpus } from 'node:os';
import cluster, { type Worker } from 'node:cluster';
import { ArgDescription } from './IMQRPCDescription';
import { IMQBeforeCall, IMQAfterCall } from './IMQRPCOptions';
import { runWithRequest } from './IMQRequestContext';
import { type ArgDescription } from './IMQRPCDescription.js';
import { type IMQBeforeCall, type IMQAfterCall } from './IMQRPCOptions.js';
import { runWithRequest } from './IMQRequestContext.js';
import { createServer, type Server } from 'node:http';

export class Description {
Expand Down
10 changes: 5 additions & 5 deletions src/cache/RedisCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@
* <support@imqueue.com> to get commercial licensing options.
*/
import {
ILogger,
type ILogger,
DEFAULT_IMQ_OPTIONS,
IRedisClient,
IMQOptions,
type IRedisClient,
type IMQOptions,
Redis,
} from '@imqueue/core';
import { hostname } from 'node:os';
import { ICache } from '.';
import { type ICache } from './index.js';

export interface IRedisCacheOptions extends Partial<IMQOptions> {
conn?: IRedisClient;
}

export const DEFAULT_REDIS_CACHE_OPTIONS = {
export const DEFAULT_REDIS_CACHE_OPTIONS: IMQOptions = {
...DEFAULT_IMQ_OPTIONS,
prefix: 'imq-cache',
};
Expand Down
4 changes: 2 additions & 2 deletions src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
export * from './ICache';
export * from './RedisCache';
export * from './ICache.js';
export * from './RedisCache.js';
11 changes: 9 additions & 2 deletions src/decorators/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
import { IMQCache, ICache, RedisCache, ICacheConstructor } from '..';
import { signature } from '../helpers';
// cache classes are imported from their defining modules (not the package
// barrel) to avoid a circular-import TDZ on the module-scope default options
import {
type ICache,
type ICacheConstructor,
RedisCache,
} from '../cache/index.js';
import { IMQCache } from '../IMQCache.js';
import { signature } from '../helpers/index.js';

export interface CacheDecoratorOptions {
adapter?: string | ICache | ICacheConstructor;
Expand Down
2 changes: 1 addition & 1 deletion src/decorators/classType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
import { registerType } from './property';
import { registerType } from './property.js';

/**
* Implements the '@classType' decorator factory.
Expand Down
Loading
Loading