Skip to content

Commit 956e937

Browse files
committed
[CHANGE] [SVC] is now its own module
1 parent a91733e commit 956e937

13 files changed

+507
-492
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ lint:
66
deno lint --ignore=docs/,debug/
77

88
test: clean
9-
deno test --allow-all --parallel --reload --quiet --coverage=coverage tests/ jetstream/tests kv/tests/ os/tests/
9+
deno test --allow-all --parallel --reload --quiet --coverage=coverage tests/ jetstream/tests kv/tests/ os/tests/ service/tests/
1010
deno test --allow-all --parallel --reload --quiet --unsafely-ignore-certificate-errors --coverage=coverage unsafe_tests/
1111

1212

1313
testw: clean
14-
deno test --allow-all --unstable --reload --parallel --watch --fail-fast tests/ jetstream/ kv/tests/ os/tests/
14+
deno test --allow-all --unstable --reload --parallel --watch --fail-fast tests/ jetstream/ kv/tests/ os/tests/ service/tests/
1515

1616
cover:
1717
deno coverage --unstable ./coverage --lcov > ./coverage/out.lcov
@@ -25,4 +25,4 @@ bundle:
2525
deno bundle --log-level info --unstable src/mod.ts ./nats.js
2626

2727
fmt:
28-
deno fmt src/ doc/ bin/ nats-base-client/ examples/ tests/ debug/ unsafe_tests/ jetstream/ kv/ os/ jetstream.md README.md migration.md services.md
28+
deno fmt src/ doc/ bin/ nats-base-client/ examples/ tests/ debug/ unsafe_tests/ jetstream/ kv/ os/ service/ jetstream.md README.md migration.md services.md

nats-base-client/core.ts

Lines changed: 0 additions & 321 deletions
Original file line numberDiff line numberDiff line change
@@ -444,49 +444,6 @@ export interface Stats {
444444
outMsgs: number;
445445
}
446446

447-
export interface ServiceClient {
448-
/**
449-
* Pings services
450-
* @param name - optional
451-
* @param id - optional
452-
*/
453-
ping(name?: string, id?: string): Promise<QueuedIterator<ServiceIdentity>>;
454-
455-
/**
456-
* Requests all the stats from services
457-
* @param name
458-
* @param id
459-
*/
460-
stats(name?: string, id?: string): Promise<QueuedIterator<ServiceStats>>;
461-
462-
/**
463-
* Requests info from services
464-
* @param name
465-
* @param id
466-
*/
467-
info(name?: string, id?: string): Promise<QueuedIterator<ServiceInfo>>;
468-
}
469-
470-
export interface ServicesAPI {
471-
/**
472-
* Adds a {@link Service}
473-
* @param config
474-
*/
475-
add(config: ServiceConfig): Promise<Service>;
476-
477-
/**
478-
* Returns a {@link ServiceClient} that can be used to interact with the
479-
* observability aspects of the service (discovery, monitoring)
480-
* @param opts
481-
* @param prefix
482-
*/
483-
client(opts?: RequestManyOptions, prefix?: string): ServiceClient;
484-
}
485-
486-
/**
487-
* Options to a JetStream options applied to all JetStream or JetStreamManager requests.
488-
*/
489-
490447
export type Payload = Uint8Array | string;
491448

492449
export interface NatsConnection {
@@ -629,12 +586,6 @@ export interface NatsConnection {
629586
*/
630587
rtt(): Promise<number>;
631588

632-
/**
633-
* Returns a {@link ServicesAPI} which allows you to build services
634-
* using NATS.
635-
*/
636-
services: ServicesAPI;
637-
638589
/**
639590
* Use of this API is experimental, and it is subject to be removed.
640591
*
@@ -823,50 +774,6 @@ export interface PublishOptions {
823774
*/
824775
export type Nanos = number;
825776

826-
export interface ServiceMsg extends Msg {
827-
respondError(
828-
code: number,
829-
description: string,
830-
data?: Uint8Array,
831-
opts?: PublishOptions,
832-
): boolean;
833-
}
834-
835-
export type ServiceHandler = (err: NatsError | null, msg: ServiceMsg) => void;
836-
837-
/**
838-
* A service Endpoint
839-
*/
840-
export type Endpoint = {
841-
/**
842-
* Subject where the endpoint listens
843-
*/
844-
subject: string;
845-
/**
846-
* An optional handler - if not set the service is an iterator
847-
* @param err
848-
* @param msg
849-
*/
850-
handler?: ServiceHandler;
851-
/**
852-
* Optional metadata about the endpoint
853-
*/
854-
metadata?: Record<string, string>;
855-
/**
856-
* Optional queue group to run this particular endpoint in. The service's configuration
857-
* queue configuration will be used. See {@link ServiceConfig}.
858-
*/
859-
queue?: string;
860-
};
861-
export type EndpointOptions = Partial<Endpoint>;
862-
863-
export type EndpointInfo = {
864-
name: string;
865-
subject: string;
866-
metadata?: Record<string, string>;
867-
queue_group?: string;
868-
};
869-
870777
export interface Dispatcher<T> {
871778
push(v: T): void;
872779
}
@@ -883,228 +790,6 @@ export interface QueuedIterator<T> extends Dispatcher<T> {
883790
getReceived(): number;
884791
}
885792

886-
export interface ServiceGroup {
887-
/**
888-
* The name of the endpoint must be a simple subject token with no wildcards
889-
* @param name
890-
* @param opts is either a handler or a more complex options which allows a
891-
* subject, handler, and/or schema
892-
*/
893-
addEndpoint(
894-
name: string,
895-
opts?: ServiceHandler | EndpointOptions,
896-
): QueuedIterator<ServiceMsg>;
897-
898-
/**
899-
* A group is a subject prefix from which endpoints can be added.
900-
* Can be empty to allow for prefixes or tokens that are set at runtime
901-
* without requiring editing of the service.
902-
* Note that an optional queue can be specified, all endpoints added to
903-
* the group, will use the specified queue unless the endpoint overrides it.
904-
* see {@link EndpointOptions} and {@link ServiceConfig}.
905-
* @param subject
906-
* @param queue
907-
*/
908-
addGroup(subject?: string, queue?: string): ServiceGroup;
909-
}
910-
911-
export type ServiceMetadata = {
912-
metadata?: Record<string, string>;
913-
};
914-
915-
export enum ServiceResponseType {
916-
STATS = "io.nats.micro.v1.stats_response",
917-
INFO = "io.nats.micro.v1.info_response",
918-
PING = "io.nats.micro.v1.ping_response",
919-
}
920-
921-
export interface ServiceResponse {
922-
/**
923-
* Response type schema
924-
*/
925-
type: ServiceResponseType;
926-
}
927-
928-
export type ServiceIdentity = ServiceResponse & ServiceMetadata & {
929-
/**
930-
* The kind of the service reporting the stats
931-
*/
932-
name: string;
933-
/**
934-
* The unique ID of the service reporting the stats
935-
*/
936-
id: string;
937-
/**
938-
* A version for the service
939-
*/
940-
version: string;
941-
};
942-
export type NamedEndpointStats = {
943-
/**
944-
* The name of the endpoint
945-
*/
946-
name: string;
947-
/**
948-
* The subject the endpoint is listening on
949-
*/
950-
subject: string;
951-
/**
952-
* The number of requests received by the endpoint
953-
*/
954-
num_requests: number;
955-
/**
956-
* Number of errors that the endpoint has raised
957-
*/
958-
num_errors: number;
959-
/**
960-
* If set, the last error triggered by the endpoint
961-
*/
962-
last_error?: string;
963-
/**
964-
* A field that can be customized with any data as returned by stats handler see {@link ServiceConfig}
965-
*/
966-
data?: unknown;
967-
/**
968-
* Total processing_time for the service
969-
*/
970-
processing_time: Nanos;
971-
/**
972-
* Average processing_time is the total processing_time divided by the num_requests
973-
*/
974-
average_processing_time: Nanos;
975-
/**
976-
* The queue group the endpoint is listening on
977-
*/
978-
queue_group?: string;
979-
};
980-
/**
981-
* Statistics for an endpoint
982-
*/
983-
export type EndpointStats = ServiceIdentity & {
984-
endpoints?: NamedEndpointStats[];
985-
/**
986-
* ISO Date string when the service started
987-
*/
988-
started: string;
989-
};
990-
export type ServiceInfo = ServiceIdentity & {
991-
/**
992-
* Description for the service
993-
*/
994-
description: string;
995-
/**
996-
* Service metadata
997-
*/
998-
metadata?: Record<string, string>;
999-
/**
1000-
* Information about the Endpoints
1001-
*/
1002-
endpoints: EndpointInfo[];
1003-
};
1004-
export type ServiceConfig = {
1005-
/**
1006-
* A type for a service
1007-
*/
1008-
name: string;
1009-
/**
1010-
* A version identifier for the service
1011-
*/
1012-
version: string;
1013-
/**
1014-
* Description for the service
1015-
*/
1016-
description?: string;
1017-
/**
1018-
* A customized handler for the stats of an endpoint. The
1019-
* data returned by the endpoint will be serialized as is
1020-
* @param endpoint
1021-
*/
1022-
statsHandler?: (
1023-
endpoint: Endpoint,
1024-
) => Promise<unknown | null>;
1025-
1026-
/**
1027-
* Optional metadata about the service
1028-
*/
1029-
metadata?: Record<string, string>;
1030-
/**
1031-
* Optional queue group to run the service in. By default,
1032-
* then queue name is "q". Note that this configuration will
1033-
* be the default for all endpoints and groups.
1034-
*/
1035-
queue?: string;
1036-
};
1037-
/**
1038-
* The stats of a service
1039-
*/
1040-
export type ServiceStats = ServiceIdentity & EndpointStats;
1041-
1042-
export interface Service extends ServiceGroup {
1043-
/**
1044-
* A promise that gets resolved to null or Error once the service ends.
1045-
* If an error, then service exited because of an error.
1046-
*/
1047-
stopped: Promise<null | Error>;
1048-
/**
1049-
* True if the service is stopped
1050-
*/
1051-
// FIXME: this would be better as stop - but the queued iterator may be an issue perhaps call it `isStopped`
1052-
isStopped: boolean;
1053-
1054-
/**
1055-
* Returns the stats for the service.
1056-
*/
1057-
stats(): Promise<ServiceStats>;
1058-
1059-
/**
1060-
* Returns a service info for the service
1061-
*/
1062-
info(): ServiceInfo;
1063-
1064-
/**
1065-
* Returns the identity used by this service
1066-
*/
1067-
ping(): ServiceIdentity;
1068-
1069-
/**
1070-
* Resets all the stats
1071-
*/
1072-
reset(): void;
1073-
1074-
/**
1075-
* Stop the service returning a promise once the service completes.
1076-
* If the service was stopped due to an error, that promise resolves to
1077-
* the specified error
1078-
*/
1079-
stop(err?: Error): Promise<null | Error>;
1080-
}
1081-
1082-
export const ServiceErrorHeader = "Nats-Service-Error";
1083-
export const ServiceErrorCodeHeader = "Nats-Service-Error-Code";
1084-
1085-
export class ServiceError extends Error {
1086-
code: number;
1087-
1088-
constructor(code: number, message: string) {
1089-
super(message);
1090-
this.code = code;
1091-
}
1092-
1093-
static isServiceError(msg: Msg): boolean {
1094-
return ServiceError.toServiceError(msg) !== null;
1095-
}
1096-
1097-
static toServiceError(msg: Msg): ServiceError | null {
1098-
const scode = msg?.headers?.get(ServiceErrorCodeHeader) || "";
1099-
if (scode !== "") {
1100-
const code = parseInt(scode) || 400;
1101-
const description = msg?.headers?.get(ServiceErrorHeader) || "";
1102-
return new ServiceError(code, description.length ? description : scode);
1103-
}
1104-
return null;
1105-
}
1106-
}
1107-
1108793
export interface Publisher {
1109794
publish(
1110795
subject: string,
@@ -1400,12 +1085,6 @@ export interface URLParseFn {
14001085
(u: string, encrypted?: boolean): string;
14011086
}
14021087

1403-
export enum ServiceVerb {
1404-
PING = "PING",
1405-
STATS = "STATS",
1406-
INFO = "INFO",
1407-
}
1408-
14091088
export type Context = {
14101089
server: ContextServer;
14111090
data: ContextUser;

0 commit comments

Comments
 (0)