@hahnpro/flow-sdk 9.6.4 → 2025.2.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +904 -0
- package/jest.config.ts +10 -0
- package/package.json +7 -20
- package/project.json +41 -0
- package/src/index.ts +15 -0
- package/src/lib/ContextManager.ts +111 -0
- package/src/lib/FlowApplication.ts +659 -0
- package/src/lib/FlowElement.ts +220 -0
- package/src/lib/FlowEvent.ts +73 -0
- package/src/lib/FlowLogger.ts +131 -0
- package/src/lib/FlowModule.ts +18 -0
- package/src/lib/RpcClient.ts +99 -0
- package/src/lib/TestModule.ts +14 -0
- package/src/lib/__pycache__/rpc_server.cpython-310.pyc +0 -0
- package/src/lib/amqp.ts +32 -0
- package/src/lib/extra-validators.ts +62 -0
- package/src/lib/flow.interface.ts +56 -0
- package/{dist/index.d.ts → src/lib/index.ts} +3 -0
- package/src/lib/nats.ts +140 -0
- package/src/lib/unit-decorators.ts +156 -0
- package/src/lib/unit-utils.ts +163 -0
- package/src/lib/units.ts +587 -0
- package/src/lib/utils.ts +176 -0
- package/test/context-manager-purpose.spec.ts +248 -0
- package/test/context-manager.spec.ts +55 -0
- package/test/context.spec.ts +180 -0
- package/test/event.spec.ts +155 -0
- package/test/extra-validators.spec.ts +84 -0
- package/test/flow-logger.spec.ts +104 -0
- package/test/flow.spec.ts +508 -0
- package/test/input-stream.decorator.spec.ts +379 -0
- package/test/long-rpc.test.py +14 -0
- package/test/long-running-rpc.spec.ts +60 -0
- package/test/message.spec.ts +57 -0
- package/test/mocks/logger.mock.ts +7 -0
- package/test/mocks/nats-connection.mock.ts +135 -0
- package/test/mocks/nats-prepare.reals-nats.ts +15 -0
- package/test/rpc.spec.ts +198 -0
- package/test/rpc.test.py +45 -0
- package/test/rx.spec.ts +92 -0
- package/test/unit-decorator.spec.ts +57 -0
- package/test/utils.spec.ts +210 -0
- package/test/validation.spec.ts +174 -0
- package/tsconfig.json +13 -0
- package/tsconfig.lib.json +22 -0
- package/tsconfig.spec.json +8 -0
- package/LICENSE +0 -21
- package/dist/ContextManager.d.ts +0 -40
- package/dist/ContextManager.js +0 -77
- package/dist/FlowApplication.d.ts +0 -85
- package/dist/FlowApplication.js +0 -500
- package/dist/FlowElement.d.ts +0 -67
- package/dist/FlowElement.js +0 -163
- package/dist/FlowEvent.d.ts +0 -25
- package/dist/FlowEvent.js +0 -71
- package/dist/FlowLogger.d.ts +0 -44
- package/dist/FlowLogger.js +0 -94
- package/dist/FlowModule.d.ts +0 -7
- package/dist/FlowModule.js +0 -13
- package/dist/RpcClient.d.ts +0 -13
- package/dist/RpcClient.js +0 -84
- package/dist/TestModule.d.ts +0 -2
- package/dist/TestModule.js +0 -27
- package/dist/amqp.d.ts +0 -14
- package/dist/amqp.js +0 -12
- package/dist/extra-validators.d.ts +0 -1
- package/dist/extra-validators.js +0 -51
- package/dist/flow.interface.d.ts +0 -48
- package/dist/flow.interface.js +0 -9
- package/dist/index.js +0 -18
- package/dist/nats.d.ts +0 -12
- package/dist/nats.js +0 -109
- package/dist/unit-decorators.d.ts +0 -39
- package/dist/unit-decorators.js +0 -156
- package/dist/unit-utils.d.ts +0 -8
- package/dist/unit-utils.js +0 -143
- package/dist/units.d.ts +0 -31
- package/dist/units.js +0 -570
- package/dist/utils.d.ts +0 -51
- package/dist/utils.js +0 -137
- /package/{dist → src/lib}/rpc_server.py +0 -0
package/dist/flow.interface.d.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { Owner } from '@hahnpro/hpc-api';
|
|
2
|
-
export interface FlowContext {
|
|
3
|
-
deploymentId?: string;
|
|
4
|
-
diagramId?: string;
|
|
5
|
-
flowId?: string;
|
|
6
|
-
owner?: Owner;
|
|
7
|
-
}
|
|
8
|
-
export interface FlowElementContext extends FlowContext {
|
|
9
|
-
id: string;
|
|
10
|
-
name?: string;
|
|
11
|
-
functionFqn?: string;
|
|
12
|
-
inputStreamId?: string;
|
|
13
|
-
}
|
|
14
|
-
export interface DeploymentMessage extends Record<string, any> {
|
|
15
|
-
elementId?: string;
|
|
16
|
-
}
|
|
17
|
-
interface FlowElement {
|
|
18
|
-
id: string;
|
|
19
|
-
name?: string;
|
|
20
|
-
properties?: Record<string, any>;
|
|
21
|
-
module: string;
|
|
22
|
-
functionFqn: string;
|
|
23
|
-
}
|
|
24
|
-
interface FlowConnection {
|
|
25
|
-
id: string;
|
|
26
|
-
name?: string;
|
|
27
|
-
source: string;
|
|
28
|
-
target: string;
|
|
29
|
-
sourceStream?: string;
|
|
30
|
-
targetStream?: string;
|
|
31
|
-
}
|
|
32
|
-
export interface Flow {
|
|
33
|
-
elements: FlowElement[];
|
|
34
|
-
connections: FlowConnection[];
|
|
35
|
-
modules?: string[];
|
|
36
|
-
properties?: Record<string, any>;
|
|
37
|
-
context?: FlowContext;
|
|
38
|
-
}
|
|
39
|
-
export interface StreamOptions {
|
|
40
|
-
concurrent?: number;
|
|
41
|
-
}
|
|
42
|
-
export type ClassType<T> = new (...args: any[]) => T;
|
|
43
|
-
export declare enum LifecycleEvent {
|
|
44
|
-
ACTIVATED = "com.hahnpro.flow_function.activated",
|
|
45
|
-
COMPLETED = "com.hahnpro.flow_function.completed",
|
|
46
|
-
TERMINATED = "com.hahnpro.flow_function.terminated"
|
|
47
|
-
}
|
|
48
|
-
export {};
|
package/dist/flow.interface.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LifecycleEvent = void 0;
|
|
4
|
-
var LifecycleEvent;
|
|
5
|
-
(function (LifecycleEvent) {
|
|
6
|
-
LifecycleEvent["ACTIVATED"] = "com.hahnpro.flow_function.activated";
|
|
7
|
-
LifecycleEvent["COMPLETED"] = "com.hahnpro.flow_function.completed";
|
|
8
|
-
LifecycleEvent["TERMINATED"] = "com.hahnpro.flow_function.terminated";
|
|
9
|
-
})(LifecycleEvent || (exports.LifecycleEvent = LifecycleEvent = {}));
|
package/dist/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IncompatableWith = exports.FlowModule = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
tslib_1.__exportStar(require("@hahnpro/hpc-api"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./flow.interface"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./utils"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./FlowApplication"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./FlowElement"), exports);
|
|
10
|
-
tslib_1.__exportStar(require("./FlowEvent"), exports);
|
|
11
|
-
tslib_1.__exportStar(require("./FlowLogger"), exports);
|
|
12
|
-
var FlowModule_1 = require("./FlowModule");
|
|
13
|
-
Object.defineProperty(exports, "FlowModule", { enumerable: true, get: function () { return FlowModule_1.FlowModule; } });
|
|
14
|
-
tslib_1.__exportStar(require("./TestModule"), exports);
|
|
15
|
-
tslib_1.__exportStar(require("./unit-decorators"), exports);
|
|
16
|
-
tslib_1.__exportStar(require("./ContextManager"), exports);
|
|
17
|
-
var extra_validators_1 = require("./extra-validators");
|
|
18
|
-
Object.defineProperty(exports, "IncompatableWith", { enumerable: true, get: function () { return extra_validators_1.IncompatableWith; } });
|
package/dist/nats.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { ConnectionOptions, NatsConnection } from '@nats-io/nats-core';
|
|
2
|
-
import { CloudEvent } from 'cloudevents';
|
|
3
|
-
import { Consumer, ConsumerConfig, PubAck } from '@nats-io/jetstream';
|
|
4
|
-
import { Logger } from './FlowLogger';
|
|
5
|
-
export type NatsEvent<T> = Pick<CloudEvent<T>, 'type' | 'source' | 'subject' | 'data' | 'datacontenttype' | 'time'>;
|
|
6
|
-
export declare const natsFlowsPrefixFlowDeployment = "fs.flowdeployment";
|
|
7
|
-
export declare const defaultConsumerConfig: ConsumerConfig;
|
|
8
|
-
export declare const FLOWS_STREAM_NAME = "flows";
|
|
9
|
-
export declare function getOrCreateConsumer(logger: Logger, natsConnection: NatsConnection, streamName: string, consumerName: string, options: Partial<ConsumerConfig>): Promise<Consumer>;
|
|
10
|
-
export declare function natsEventListener(nc: NatsConnection, logger: Logger, reconnectHandler: () => void): Promise<void>;
|
|
11
|
-
export declare function publishNatsEvent<T>(logger: Logger, nc: NatsConnection, event: NatsEvent<T>, subject?: string): Promise<PubAck>;
|
|
12
|
-
export declare function createNatsConnection(config: ConnectionOptions): Promise<NatsConnection>;
|
package/dist/nats.js
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FLOWS_STREAM_NAME = exports.defaultConsumerConfig = exports.natsFlowsPrefixFlowDeployment = void 0;
|
|
4
|
-
exports.getOrCreateConsumer = getOrCreateConsumer;
|
|
5
|
-
exports.natsEventListener = natsEventListener;
|
|
6
|
-
exports.publishNatsEvent = publishNatsEvent;
|
|
7
|
-
exports.createNatsConnection = createNatsConnection;
|
|
8
|
-
const transport_node_1 = require("@nats-io/transport-node");
|
|
9
|
-
const cloudevents_1 = require("cloudevents");
|
|
10
|
-
const jetstream_1 = require("@nats-io/jetstream");
|
|
11
|
-
const lodash_1 = require("lodash");
|
|
12
|
-
exports.natsFlowsPrefixFlowDeployment = `fs.flowdeployment`;
|
|
13
|
-
exports.defaultConsumerConfig = {
|
|
14
|
-
ack_policy: jetstream_1.AckPolicy.Explicit,
|
|
15
|
-
ack_wait: 30_000_000_000,
|
|
16
|
-
deliver_policy: jetstream_1.DeliverPolicy.All,
|
|
17
|
-
max_ack_pending: 1000,
|
|
18
|
-
max_deliver: -1,
|
|
19
|
-
max_waiting: 512,
|
|
20
|
-
replay_policy: jetstream_1.ReplayPolicy.Instant,
|
|
21
|
-
num_replicas: 0,
|
|
22
|
-
};
|
|
23
|
-
exports.FLOWS_STREAM_NAME = 'flows';
|
|
24
|
-
async function getOrCreateConsumer(logger, natsConnection, streamName, consumerName, options) {
|
|
25
|
-
if (!natsConnection || natsConnection.isClosed()) {
|
|
26
|
-
throw new Error('NATS connection is not available');
|
|
27
|
-
}
|
|
28
|
-
else if (!streamName) {
|
|
29
|
-
throw new Error('Stream name is not available');
|
|
30
|
-
}
|
|
31
|
-
else if (!consumerName) {
|
|
32
|
-
throw new Error('Consumer name is not available');
|
|
33
|
-
}
|
|
34
|
-
logger.debug(`Creating consumer ${consumerName} for stream ${streamName}`);
|
|
35
|
-
const jsm = await (0, jetstream_1.jetstreamManager)(natsConnection);
|
|
36
|
-
const consumerInfo = await jsm.consumers.info(streamName, consumerName).catch((err) => {
|
|
37
|
-
if (err.status !== 404) {
|
|
38
|
-
logger.error(`Could not get consumer info of stream ${streamName}`, err);
|
|
39
|
-
logger.error(err.message);
|
|
40
|
-
throw err;
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
const consumerConfig = { ...exports.defaultConsumerConfig, ...options };
|
|
44
|
-
if (consumerInfo) {
|
|
45
|
-
const compared = (0, lodash_1.omitBy)(consumerConfig, (value, key) => (0, lodash_1.isEqual)(value, consumerInfo.config[key]));
|
|
46
|
-
if (Object.keys(compared).length !== 0) {
|
|
47
|
-
await jsm.consumers.update(streamName, consumerName, consumerConfig);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
await jsm.consumers.add(streamName, { name: consumerName, ...consumerConfig });
|
|
52
|
-
}
|
|
53
|
-
return await (0, jetstream_1.jetstream)(natsConnection).consumers.get(streamName, consumerName);
|
|
54
|
-
}
|
|
55
|
-
async function natsEventListener(nc, logger, reconnectHandler) {
|
|
56
|
-
const statusAsyncIterator = nc?.status();
|
|
57
|
-
if (!statusAsyncIterator) {
|
|
58
|
-
logger.error('NATS Status-AsyncIterator is not available, cannot listen for events to re-create consumers at reconnects');
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
for await (const status of statusAsyncIterator) {
|
|
62
|
-
if (status.type === 'reconnect') {
|
|
63
|
-
reconnectHandler();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
async function publishNatsEvent(logger, nc, event, subject) {
|
|
68
|
-
if (!nc || nc.isClosed()) {
|
|
69
|
-
logger.error('NATS connection is not available, cannot publish event');
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
const cloudEvent = new cloudevents_1.CloudEvent({ datacontenttype: 'application/json', ...event });
|
|
73
|
-
cloudEvent.validate();
|
|
74
|
-
const js = (0, jetstream_1.jetstream)(nc);
|
|
75
|
-
if (js) {
|
|
76
|
-
return js.publish(subject || `${cloudEvent.type}.${cloudEvent.subject}`, JSON.stringify(cloudEvent.toJSON()), {
|
|
77
|
-
msgID: cloudEvent.id,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
logger.error(`Could not publish nats event, because jetstream is unavailable / undefined`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
async function createNatsConnection(config) {
|
|
85
|
-
const servers = config?.servers ?? process.env.NATS_SERVERS?.split(',') ?? [];
|
|
86
|
-
const reconnect = config?.reconnect ?? (process.env.NATS_RECONNECT ?? 'true') === 'true';
|
|
87
|
-
let maxReconnectAttempts = config?.maxReconnectAttempts ?? parseInt(process.env.NATS_MAX_RECONNECT_ATTEMPTS ?? '-1', 10);
|
|
88
|
-
if (isNaN(maxReconnectAttempts)) {
|
|
89
|
-
maxReconnectAttempts = 10;
|
|
90
|
-
}
|
|
91
|
-
let reconnectTimeWait = config?.reconnectTimeWait ?? parseInt(process.env.NATS_RECONNECT_TIME_WAIT ?? '2000', 10);
|
|
92
|
-
if (isNaN(reconnectTimeWait)) {
|
|
93
|
-
reconnectTimeWait = 2000;
|
|
94
|
-
}
|
|
95
|
-
let timeout = config?.timeout ?? parseInt(process.env.NATS_TIMEOUT ?? '2000', 10);
|
|
96
|
-
if (isNaN(timeout)) {
|
|
97
|
-
timeout = 2000;
|
|
98
|
-
}
|
|
99
|
-
const options = {
|
|
100
|
-
servers,
|
|
101
|
-
reconnect,
|
|
102
|
-
maxReconnectAttempts,
|
|
103
|
-
reconnectTimeWait,
|
|
104
|
-
timeout,
|
|
105
|
-
user: config?.user ?? process.env.NATS_USER,
|
|
106
|
-
pass: config.pass ?? process.env.NATS_PASSWORD,
|
|
107
|
-
};
|
|
108
|
-
return (0, transport_node_1.connect)(options);
|
|
109
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { ValidationOptions } from 'class-validator';
|
|
2
|
-
export declare function IsTime(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
3
|
-
export declare function IsLength(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
4
|
-
export declare function IsMass(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
5
|
-
export declare function IsElectricCurrent(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
6
|
-
export declare function IsThermodynamicTemperature(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
7
|
-
export declare function IsAmountOfSubstance(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
8
|
-
export declare function IsLuminousIntensity(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
9
|
-
export declare function IsVoltage(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
10
|
-
export declare function IsForce(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
11
|
-
export declare function IsEnergy(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
12
|
-
export declare function IsPower(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
13
|
-
export declare function IsPressure(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
14
|
-
export declare function IsFrequency(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
15
|
-
export declare function IsArea(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
16
|
-
export declare function IsVolume(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
17
|
-
export declare function IsAngle(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
18
|
-
export declare function IsTranslationalAcceleration(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
19
|
-
export declare function IsTranslationalVelocity(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
20
|
-
export declare function IsTranslationalDisplacement(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
21
|
-
export declare function IsSpringConstant(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
22
|
-
export declare function IsRotationalAcceleration(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
23
|
-
export declare function IsRotationalVelocity(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
24
|
-
export declare function IsRotationalDisplacement(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
25
|
-
export declare function IsTorque(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
26
|
-
export declare function IsMomentOfInertia(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
27
|
-
export declare function IsRotatingUnbalance(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
28
|
-
export declare function IsMechanicalPower(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
29
|
-
export declare function IsMechanicalEnergy(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
30
|
-
export declare function IsDynamicViscosity(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
31
|
-
export declare function IsVolumeFlow(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
32
|
-
export declare function IsMassFlow(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
33
|
-
export declare function IsHeatFlux(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
34
|
-
export declare function IsThermalEnergy(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
35
|
-
export declare function IsSpecificHeatCapacity(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
36
|
-
export declare function IsThermalTransmittance(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
37
|
-
export declare function IsElectricalPower(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
38
|
-
export declare function IsElectricalEnergy(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
39
|
-
export declare function IsElectricalFrequency(unit?: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
package/dist/unit-decorators.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IsTime = IsTime;
|
|
4
|
-
exports.IsLength = IsLength;
|
|
5
|
-
exports.IsMass = IsMass;
|
|
6
|
-
exports.IsElectricCurrent = IsElectricCurrent;
|
|
7
|
-
exports.IsThermodynamicTemperature = IsThermodynamicTemperature;
|
|
8
|
-
exports.IsAmountOfSubstance = IsAmountOfSubstance;
|
|
9
|
-
exports.IsLuminousIntensity = IsLuminousIntensity;
|
|
10
|
-
exports.IsVoltage = IsVoltage;
|
|
11
|
-
exports.IsForce = IsForce;
|
|
12
|
-
exports.IsEnergy = IsEnergy;
|
|
13
|
-
exports.IsPower = IsPower;
|
|
14
|
-
exports.IsPressure = IsPressure;
|
|
15
|
-
exports.IsFrequency = IsFrequency;
|
|
16
|
-
exports.IsArea = IsArea;
|
|
17
|
-
exports.IsVolume = IsVolume;
|
|
18
|
-
exports.IsAngle = IsAngle;
|
|
19
|
-
exports.IsTranslationalAcceleration = IsTranslationalAcceleration;
|
|
20
|
-
exports.IsTranslationalVelocity = IsTranslationalVelocity;
|
|
21
|
-
exports.IsTranslationalDisplacement = IsTranslationalDisplacement;
|
|
22
|
-
exports.IsSpringConstant = IsSpringConstant;
|
|
23
|
-
exports.IsRotationalAcceleration = IsRotationalAcceleration;
|
|
24
|
-
exports.IsRotationalVelocity = IsRotationalVelocity;
|
|
25
|
-
exports.IsRotationalDisplacement = IsRotationalDisplacement;
|
|
26
|
-
exports.IsTorque = IsTorque;
|
|
27
|
-
exports.IsMomentOfInertia = IsMomentOfInertia;
|
|
28
|
-
exports.IsRotatingUnbalance = IsRotatingUnbalance;
|
|
29
|
-
exports.IsMechanicalPower = IsMechanicalPower;
|
|
30
|
-
exports.IsMechanicalEnergy = IsMechanicalEnergy;
|
|
31
|
-
exports.IsDynamicViscosity = IsDynamicViscosity;
|
|
32
|
-
exports.IsVolumeFlow = IsVolumeFlow;
|
|
33
|
-
exports.IsMassFlow = IsMassFlow;
|
|
34
|
-
exports.IsHeatFlux = IsHeatFlux;
|
|
35
|
-
exports.IsThermalEnergy = IsThermalEnergy;
|
|
36
|
-
exports.IsSpecificHeatCapacity = IsSpecificHeatCapacity;
|
|
37
|
-
exports.IsThermalTransmittance = IsThermalTransmittance;
|
|
38
|
-
exports.IsElectricalPower = IsElectricalPower;
|
|
39
|
-
exports.IsElectricalEnergy = IsElectricalEnergy;
|
|
40
|
-
exports.IsElectricalFrequency = IsElectricalFrequency;
|
|
41
|
-
const unit_utils_1 = require("./unit-utils");
|
|
42
|
-
const units_1 = require("./units");
|
|
43
|
-
function IsTime(unit = units_1.units.time.baseUnit, validationOptions) {
|
|
44
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'time', validationOptions);
|
|
45
|
-
}
|
|
46
|
-
function IsLength(unit = units_1.units.length.baseUnit, validationOptions) {
|
|
47
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'length', validationOptions);
|
|
48
|
-
}
|
|
49
|
-
function IsMass(unit = units_1.units.mass.baseUnit, validationOptions) {
|
|
50
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'mass', validationOptions);
|
|
51
|
-
}
|
|
52
|
-
function IsElectricCurrent(unit = units_1.units.electricCurrent.baseUnit, validationOptions) {
|
|
53
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'electricCurrent', validationOptions);
|
|
54
|
-
}
|
|
55
|
-
function IsThermodynamicTemperature(unit = units_1.units.thermodynamicTemperature.baseUnit, validationOptions) {
|
|
56
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'thermodynamicTemperature', validationOptions);
|
|
57
|
-
}
|
|
58
|
-
function IsAmountOfSubstance(unit = units_1.units.amountOfSubstance.baseUnit, validationOptions) {
|
|
59
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'amountOfSubstance', validationOptions);
|
|
60
|
-
}
|
|
61
|
-
function IsLuminousIntensity(unit = units_1.units.luminousIntensity.baseUnit, validationOptions) {
|
|
62
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'luminousIntensity', validationOptions);
|
|
63
|
-
}
|
|
64
|
-
function IsVoltage(unit = units_1.units.voltage.baseUnit, validationOptions) {
|
|
65
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'voltage', validationOptions);
|
|
66
|
-
}
|
|
67
|
-
function IsForce(unit = units_1.units.force.baseUnit, validationOptions) {
|
|
68
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'force', validationOptions);
|
|
69
|
-
}
|
|
70
|
-
function IsEnergy(unit = units_1.units.energy.baseUnit, validationOptions) {
|
|
71
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'energy', validationOptions);
|
|
72
|
-
}
|
|
73
|
-
function IsPower(unit = units_1.units.power.baseUnit, validationOptions) {
|
|
74
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'power', validationOptions);
|
|
75
|
-
}
|
|
76
|
-
function IsPressure(unit = units_1.units.pressure.baseUnit, validationOptions) {
|
|
77
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'pressure', validationOptions);
|
|
78
|
-
}
|
|
79
|
-
function IsFrequency(unit = units_1.units.frequency.baseUnit, validationOptions) {
|
|
80
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'frequency', validationOptions);
|
|
81
|
-
}
|
|
82
|
-
function IsArea(unit = units_1.units.area.baseUnit, validationOptions) {
|
|
83
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'area', validationOptions);
|
|
84
|
-
}
|
|
85
|
-
function IsVolume(unit = units_1.units.volume.baseUnit, validationOptions) {
|
|
86
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'volume', validationOptions);
|
|
87
|
-
}
|
|
88
|
-
function IsAngle(unit = units_1.units.angle.baseUnit, validationOptions) {
|
|
89
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'angle', validationOptions);
|
|
90
|
-
}
|
|
91
|
-
function IsTranslationalAcceleration(unit = units_1.units.translationalAcceleration.baseUnit, validationOptions) {
|
|
92
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'translationalAcceleration', validationOptions);
|
|
93
|
-
}
|
|
94
|
-
function IsTranslationalVelocity(unit = units_1.units.translationalVelocity.baseUnit, validationOptions) {
|
|
95
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'translationalVelocity', validationOptions);
|
|
96
|
-
}
|
|
97
|
-
function IsTranslationalDisplacement(unit = units_1.units.translationalDisplacement.baseUnit, validationOptions) {
|
|
98
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'translationalDisplacement', validationOptions);
|
|
99
|
-
}
|
|
100
|
-
function IsSpringConstant(unit = units_1.units.springConstant.baseUnit, validationOptions) {
|
|
101
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'springConstant', validationOptions);
|
|
102
|
-
}
|
|
103
|
-
function IsRotationalAcceleration(unit = units_1.units.rotationalAcceleration.baseUnit, validationOptions) {
|
|
104
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'rotationalAcceleration', validationOptions);
|
|
105
|
-
}
|
|
106
|
-
function IsRotationalVelocity(unit = units_1.units.rotationalVelocity.baseUnit, validationOptions) {
|
|
107
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'rotationalVelocity', validationOptions);
|
|
108
|
-
}
|
|
109
|
-
function IsRotationalDisplacement(unit = units_1.units.rotationalDisplacement.baseUnit, validationOptions) {
|
|
110
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'rotationalDisplacement', validationOptions);
|
|
111
|
-
}
|
|
112
|
-
function IsTorque(unit = units_1.units.torque.baseUnit, validationOptions) {
|
|
113
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'torque', validationOptions);
|
|
114
|
-
}
|
|
115
|
-
function IsMomentOfInertia(unit = units_1.units.momentOfInertia.baseUnit, validationOptions) {
|
|
116
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'momentOfInertia', validationOptions);
|
|
117
|
-
}
|
|
118
|
-
function IsRotatingUnbalance(unit = units_1.units.rotatingUnbalance.baseUnit, validationOptions) {
|
|
119
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'rotatingUnbalance', validationOptions);
|
|
120
|
-
}
|
|
121
|
-
function IsMechanicalPower(unit = units_1.units.mechanicalPower.baseUnit, validationOptions) {
|
|
122
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'mechanicalPower', validationOptions);
|
|
123
|
-
}
|
|
124
|
-
function IsMechanicalEnergy(unit = units_1.units.mechanicalEnergy.baseUnit, validationOptions) {
|
|
125
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'mechanicalEnergy', validationOptions);
|
|
126
|
-
}
|
|
127
|
-
function IsDynamicViscosity(unit = units_1.units.dynamicViscosity.baseUnit, validationOptions) {
|
|
128
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'dynamicViscosity', validationOptions);
|
|
129
|
-
}
|
|
130
|
-
function IsVolumeFlow(unit = units_1.units.volumeFlow.baseUnit, validationOptions) {
|
|
131
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'volumeFlow', validationOptions);
|
|
132
|
-
}
|
|
133
|
-
function IsMassFlow(unit = units_1.units.massFlow.baseUnit, validationOptions) {
|
|
134
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'massFlow', validationOptions);
|
|
135
|
-
}
|
|
136
|
-
function IsHeatFlux(unit = units_1.units.heatFlux.baseUnit, validationOptions) {
|
|
137
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'heatFlux', validationOptions);
|
|
138
|
-
}
|
|
139
|
-
function IsThermalEnergy(unit = units_1.units.thermalEnergy.baseUnit, validationOptions) {
|
|
140
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'thermalEnergy', validationOptions);
|
|
141
|
-
}
|
|
142
|
-
function IsSpecificHeatCapacity(unit = units_1.units.specificHeatCapacity.baseUnit, validationOptions) {
|
|
143
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'specificHeatCapacity', validationOptions);
|
|
144
|
-
}
|
|
145
|
-
function IsThermalTransmittance(unit = units_1.units.thermalTransmittance.baseUnit, validationOptions) {
|
|
146
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'thermalTransmittance', validationOptions);
|
|
147
|
-
}
|
|
148
|
-
function IsElectricalPower(unit = units_1.units.electricalPower.baseUnit, validationOptions) {
|
|
149
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'electricalPower', validationOptions);
|
|
150
|
-
}
|
|
151
|
-
function IsElectricalEnergy(unit = units_1.units.electricalEnergy.baseUnit, validationOptions) {
|
|
152
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'electricalEnergy', validationOptions);
|
|
153
|
-
}
|
|
154
|
-
function IsElectricalFrequency(unit = units_1.units.electricalFrequency.baseUnit, validationOptions) {
|
|
155
|
-
return (0, unit_utils_1.makeUnitDecorator)(unit, 'electricalFrequency', validationOptions);
|
|
156
|
-
}
|
package/dist/unit-utils.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { ValidationOptions } from 'class-validator';
|
|
2
|
-
export declare function makeUnitDecorator(unit: string, metric: string, validationOptions?: ValidationOptions): (object: any, propertyName: string) => void;
|
|
3
|
-
export declare function verifyUnit(unit: string, metric: string): number;
|
|
4
|
-
export declare function computeUnitOptions(dimension: string): {
|
|
5
|
-
prefUnit: string;
|
|
6
|
-
convfactor: number;
|
|
7
|
-
offset: number;
|
|
8
|
-
}[];
|
package/dist/unit-utils.js
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeUnitDecorator = makeUnitDecorator;
|
|
4
|
-
exports.verifyUnit = verifyUnit;
|
|
5
|
-
exports.computeUnitOptions = computeUnitOptions;
|
|
6
|
-
const tslib_1 = require("tslib");
|
|
7
|
-
const class_validator_1 = require("class-validator");
|
|
8
|
-
const units_1 = require("./units");
|
|
9
|
-
function makeUnitDecorator(unit, metric, validationOptions) {
|
|
10
|
-
const conversionFactor = verifyUnit(unit, metric);
|
|
11
|
-
if (conversionFactor < 0) {
|
|
12
|
-
throw new Error(`${unit} is not a valid ${metric}.`);
|
|
13
|
-
}
|
|
14
|
-
return (object, propertyName) => {
|
|
15
|
-
Reflect.defineMetadata(`conversionFactor:${propertyName}`, conversionFactor, object.constructor);
|
|
16
|
-
(0, class_validator_1.registerDecorator)({
|
|
17
|
-
target: object.constructor,
|
|
18
|
-
propertyName,
|
|
19
|
-
options: validationOptions,
|
|
20
|
-
constraints: [metric, unit],
|
|
21
|
-
validator: UnitArgsValidator,
|
|
22
|
-
});
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
let UnitArgsValidator = class UnitArgsValidator {
|
|
26
|
-
validate(value, validationArguments) {
|
|
27
|
-
return (0, class_validator_1.isNumber)(value);
|
|
28
|
-
}
|
|
29
|
-
defaultMessage(args) {
|
|
30
|
-
return `${args.property} must be a number conforming to the specified constraints`;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
UnitArgsValidator = tslib_1.__decorate([
|
|
34
|
-
(0, class_validator_1.ValidatorConstraint)({ async: false })
|
|
35
|
-
], UnitArgsValidator);
|
|
36
|
-
const prefixes = [
|
|
37
|
-
{ prefix: 'Y', convfactor: 1e24 },
|
|
38
|
-
{ prefix: 'Z', convfactor: 1e21 },
|
|
39
|
-
{ prefix: 'E', convfactor: 1e18 },
|
|
40
|
-
{ prefix: 'P', convfactor: 1e15 },
|
|
41
|
-
{ prefix: 'T', convfactor: 1e12 },
|
|
42
|
-
{ prefix: 'G', convfactor: 1e9 },
|
|
43
|
-
{ prefix: 'M', convfactor: 1e6 },
|
|
44
|
-
{ prefix: 'k', convfactor: 1e3 },
|
|
45
|
-
{ prefix: 'h', convfactor: 1e2 },
|
|
46
|
-
{ prefix: 'da', convfactor: 1e1 },
|
|
47
|
-
{ prefix: 'd', convfactor: 1e-1 },
|
|
48
|
-
{ prefix: 'c', convfactor: 1e-2 },
|
|
49
|
-
{ prefix: 'm', convfactor: 1e-3 },
|
|
50
|
-
{ prefix: 'μ', convfactor: 1e-6 },
|
|
51
|
-
{ prefix: 'n', convfactor: 1e-9 },
|
|
52
|
-
{ prefix: 'p', convfactor: 1e-12 },
|
|
53
|
-
{ prefix: 'f', convfactor: 1e-15 },
|
|
54
|
-
{ prefix: 'a', convfactor: 1e-18 },
|
|
55
|
-
{ prefix: 'z', convfactor: 1e-21 },
|
|
56
|
-
{ prefix: 'y', convfactor: 1e-24 },
|
|
57
|
-
];
|
|
58
|
-
function verifyUnit(unit, metric) {
|
|
59
|
-
if (unit === '' || metric === '')
|
|
60
|
-
return -1;
|
|
61
|
-
const definition = units_1.units[metric];
|
|
62
|
-
for (const dimension of definition.dimensions) {
|
|
63
|
-
const components = dimension.split('*');
|
|
64
|
-
const nominator = components.filter((v) => !v.includes('-'));
|
|
65
|
-
const denominator = components.filter((v) => v.includes('-'));
|
|
66
|
-
const nomIndices = computeIndices(unit.split('/')[0], nominator);
|
|
67
|
-
const denomIndices = computeIndices(unit.split('/')[1] || '', denominator);
|
|
68
|
-
const nomFactor = verifyIndices(nomIndices, unit.split('/')[0]);
|
|
69
|
-
const denomFactor = verifyIndices(denomIndices, unit.split('/')[1]);
|
|
70
|
-
if (nomFactor > 0 && denomFactor > 0) {
|
|
71
|
-
return nomFactor / denomFactor;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return -1;
|
|
75
|
-
}
|
|
76
|
-
function verifyIndices(indices, unit) {
|
|
77
|
-
if (indices.length === 0 || !unit)
|
|
78
|
-
return 1;
|
|
79
|
-
const filtered = indices
|
|
80
|
-
.map((arr) => arr.filter((v) => v.index !== -1))
|
|
81
|
-
.reduce((previousValue, currentValue) => {
|
|
82
|
-
if (currentValue.length > 1) {
|
|
83
|
-
for (let j = 0; j < currentValue.length - 1; j++) {
|
|
84
|
-
previousValue.forEach((value) => previousValue.push([...value]));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
let index = 0;
|
|
88
|
-
while (index < previousValue.length) {
|
|
89
|
-
previousValue[index].push(currentValue[index % currentValue.length]);
|
|
90
|
-
index++;
|
|
91
|
-
}
|
|
92
|
-
return previousValue;
|
|
93
|
-
}, [[]]);
|
|
94
|
-
outer: for (const filteredElement of filtered) {
|
|
95
|
-
let index = 0;
|
|
96
|
-
let convfactor = 1;
|
|
97
|
-
while (index != unit.length) {
|
|
98
|
-
const find = filteredElement.find((obj) => obj?.index === index);
|
|
99
|
-
if (!find)
|
|
100
|
-
continue outer;
|
|
101
|
-
index += find.length;
|
|
102
|
-
convfactor *= find.convfactor;
|
|
103
|
-
}
|
|
104
|
-
return convfactor;
|
|
105
|
-
}
|
|
106
|
-
return -1;
|
|
107
|
-
}
|
|
108
|
-
function computeIndices(unit, dimensions) {
|
|
109
|
-
return dimensions.map((dimension) => {
|
|
110
|
-
const options = computeUnitOptions(dimension);
|
|
111
|
-
const indices = options.map((preUnit) => unit.indexOf(preUnit.prefUnit));
|
|
112
|
-
return indices.map((ind, index) => ({
|
|
113
|
-
index: ind,
|
|
114
|
-
length: options[index].prefUnit.length,
|
|
115
|
-
convfactor: options[index].convfactor,
|
|
116
|
-
offset: options[index].offset,
|
|
117
|
-
}));
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
function computeUnitOptions(dimension) {
|
|
121
|
-
const definition = units_1.units[units_1.dimensionToUnitMap[dimension.substring(0, 1)]];
|
|
122
|
-
const exponent = dimension.length === 1 ? 1 : Number.parseInt(dimension.substring(dimension.length - 1), 10);
|
|
123
|
-
const withPrefixes = definition.units
|
|
124
|
-
.filter((unit) => unit.SIPrefixes)
|
|
125
|
-
.map((unit) => prefixes.map((prefix) => ({
|
|
126
|
-
prefUnit: prefix.prefix.concat(unit.unit),
|
|
127
|
-
convfactor: Math.pow(prefix.convfactor * unit.conversionFactor, exponent),
|
|
128
|
-
offset: unit.offset || 0,
|
|
129
|
-
})))
|
|
130
|
-
.reduce((prev, curr) => prev.concat(curr), []);
|
|
131
|
-
const allUnits = withPrefixes.concat(definition.units.map((unit) => ({
|
|
132
|
-
prefUnit: unit.unit,
|
|
133
|
-
convfactor: Math.pow(unit.conversionFactor, exponent),
|
|
134
|
-
offset: unit.offset || 0,
|
|
135
|
-
})));
|
|
136
|
-
return dimension.length > 1 && !dimension.endsWith('-1')
|
|
137
|
-
? allUnits.map((unit) => ({
|
|
138
|
-
prefUnit: `${unit.prefUnit}^${dimension.substring(dimension.length - 1)}`,
|
|
139
|
-
convfactor: unit.convfactor,
|
|
140
|
-
offset: unit.offset,
|
|
141
|
-
}))
|
|
142
|
-
: allUnits;
|
|
143
|
-
}
|
package/dist/units.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export declare const dimensionToUnitMap: {
|
|
2
|
-
T: string;
|
|
3
|
-
L: string;
|
|
4
|
-
M: string;
|
|
5
|
-
I: string;
|
|
6
|
-
Θ: string;
|
|
7
|
-
N: string;
|
|
8
|
-
J: string;
|
|
9
|
-
U: string;
|
|
10
|
-
F: string;
|
|
11
|
-
E: string;
|
|
12
|
-
W: string;
|
|
13
|
-
P: string;
|
|
14
|
-
A: string;
|
|
15
|
-
V: string;
|
|
16
|
-
f: string;
|
|
17
|
-
};
|
|
18
|
-
export declare const units: UnitDefinitions;
|
|
19
|
-
export type UnitDefinitions = {
|
|
20
|
-
[key: string]: UnitDefinition;
|
|
21
|
-
};
|
|
22
|
-
export type UnitDefinition = {
|
|
23
|
-
units: {
|
|
24
|
-
unit: string;
|
|
25
|
-
conversionFactor: number;
|
|
26
|
-
SIPrefixes: boolean;
|
|
27
|
-
offset?: number;
|
|
28
|
-
}[];
|
|
29
|
-
baseUnit: string;
|
|
30
|
-
dimensions: string[];
|
|
31
|
-
};
|