@nats-io/services 3.0.0-2

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/lib/types.d.ts ADDED
@@ -0,0 +1,246 @@
1
+ import type { Msg, Nanos, NatsError, PublishOptions, QueuedIterator } from "@nats-io/nats-core";
2
+ export interface ServiceMsg extends Msg {
3
+ respondError(code: number, description: string, data?: Uint8Array, opts?: PublishOptions): boolean;
4
+ }
5
+ export type ServiceHandler = (err: NatsError | null, msg: ServiceMsg) => void;
6
+ /**
7
+ * A service Endpoint
8
+ */
9
+ export type Endpoint = {
10
+ /**
11
+ * Subject where the endpoint listens
12
+ */
13
+ subject: string;
14
+ /**
15
+ * An optional handler - if not set the service is an iterator
16
+ * @param err
17
+ * @param msg
18
+ */
19
+ handler?: ServiceHandler;
20
+ /**
21
+ * Optional metadata about the endpoint
22
+ */
23
+ metadata?: Record<string, string>;
24
+ /**
25
+ * Optional queue group to run this particular endpoint in. The service's configuration
26
+ * queue configuration will be used. See {@link ServiceConfig}.
27
+ */
28
+ queue?: string;
29
+ };
30
+ export type EndpointOptions = Partial<Endpoint>;
31
+ export type EndpointInfo = {
32
+ name: string;
33
+ subject: string;
34
+ metadata?: Record<string, string>;
35
+ queue_group?: string;
36
+ };
37
+ export interface ServiceGroup {
38
+ /**
39
+ * The name of the endpoint must be a simple subject token with no wildcards
40
+ * @param name
41
+ * @param opts is either a handler or a more complex options which allows a
42
+ * subject, handler, and/or schema
43
+ */
44
+ addEndpoint(name: string, opts?: ServiceHandler | EndpointOptions): QueuedIterator<ServiceMsg>;
45
+ /**
46
+ * A group is a subject prefix from which endpoints can be added.
47
+ * Can be empty to allow for prefixes or tokens that are set at runtime
48
+ * without requiring editing of the service.
49
+ * Note that an optional queue can be specified, all endpoints added to
50
+ * the group, will use the specified queue unless the endpoint overrides it.
51
+ * see {@link EndpointOptions} and {@link ServiceConfig}.
52
+ * @param subject
53
+ * @param queue
54
+ */
55
+ addGroup(subject?: string, queue?: string): ServiceGroup;
56
+ }
57
+ export type ServiceMetadata = {
58
+ metadata?: Record<string, string>;
59
+ };
60
+ export declare enum ServiceResponseType {
61
+ STATS = "io.nats.micro.v1.stats_response",
62
+ INFO = "io.nats.micro.v1.info_response",
63
+ PING = "io.nats.micro.v1.ping_response"
64
+ }
65
+ export interface ServiceResponse {
66
+ /**
67
+ * Response type schema
68
+ */
69
+ type: ServiceResponseType;
70
+ }
71
+ export type ServiceIdentity = ServiceResponse & ServiceMetadata & {
72
+ /**
73
+ * The kind of the service reporting the stats
74
+ */
75
+ name: string;
76
+ /**
77
+ * The unique ID of the service reporting the stats
78
+ */
79
+ id: string;
80
+ /**
81
+ * A version for the service
82
+ */
83
+ version: string;
84
+ };
85
+ export type NamedEndpointStats = {
86
+ /**
87
+ * The name of the endpoint
88
+ */
89
+ name: string;
90
+ /**
91
+ * The subject the endpoint is listening on
92
+ */
93
+ subject: string;
94
+ /**
95
+ * The number of requests received by the endpoint
96
+ */
97
+ num_requests: number;
98
+ /**
99
+ * Number of errors that the endpoint has raised
100
+ */
101
+ num_errors: number;
102
+ /**
103
+ * If set, the last error triggered by the endpoint
104
+ */
105
+ last_error?: string;
106
+ /**
107
+ * A field that can be customized with any data as returned by stats handler see {@link ServiceConfig}
108
+ */
109
+ data?: unknown;
110
+ /**
111
+ * Total processing_time for the service
112
+ */
113
+ processing_time: Nanos;
114
+ /**
115
+ * Average processing_time is the total processing_time divided by the num_requests
116
+ */
117
+ average_processing_time: Nanos;
118
+ /**
119
+ * The queue group the endpoint is listening on
120
+ */
121
+ queue_group?: string;
122
+ };
123
+ /**
124
+ * Statistics for an endpoint
125
+ */
126
+ export type EndpointStats = ServiceIdentity & {
127
+ endpoints?: NamedEndpointStats[];
128
+ /**
129
+ * ISO Date string when the service started
130
+ */
131
+ started: string;
132
+ };
133
+ export type ServiceInfo = ServiceIdentity & {
134
+ /**
135
+ * Description for the service
136
+ */
137
+ description: string;
138
+ /**
139
+ * Service metadata
140
+ */
141
+ metadata?: Record<string, string>;
142
+ /**
143
+ * Information about the Endpoints
144
+ */
145
+ endpoints: EndpointInfo[];
146
+ };
147
+ export type ServiceConfig = {
148
+ /**
149
+ * A type for a service
150
+ */
151
+ name: string;
152
+ /**
153
+ * A version identifier for the service
154
+ */
155
+ version: string;
156
+ /**
157
+ * Description for the service
158
+ */
159
+ description?: string;
160
+ /**
161
+ * A customized handler for the stats of an endpoint. The
162
+ * data returned by the endpoint will be serialized as is
163
+ * @param endpoint
164
+ */
165
+ statsHandler?: (endpoint: Endpoint) => Promise<unknown | null>;
166
+ /**
167
+ * Optional metadata about the service
168
+ */
169
+ metadata?: Record<string, string>;
170
+ /**
171
+ * Optional queue group to run the service in. By default,
172
+ * then queue name is "q". Note that this configuration will
173
+ * be the default for all endpoints and groups.
174
+ */
175
+ queue?: string;
176
+ };
177
+ /**
178
+ * The stats of a service
179
+ */
180
+ export type ServiceStats = ServiceIdentity & EndpointStats;
181
+ export interface Service extends ServiceGroup {
182
+ /**
183
+ * A promise that gets resolved to null or Error once the service ends.
184
+ * If an error, then service exited because of an error.
185
+ */
186
+ stopped: Promise<null | Error>;
187
+ /**
188
+ * True if the service is stopped
189
+ */
190
+ isStopped: boolean;
191
+ /**
192
+ * Returns the stats for the service.
193
+ */
194
+ stats(): Promise<ServiceStats>;
195
+ /**
196
+ * Returns a service info for the service
197
+ */
198
+ info(): ServiceInfo;
199
+ /**
200
+ * Returns the identity used by this service
201
+ */
202
+ ping(): ServiceIdentity;
203
+ /**
204
+ * Resets all the stats
205
+ */
206
+ reset(): void;
207
+ /**
208
+ * Stop the service returning a promise once the service completes.
209
+ * If the service was stopped due to an error, that promise resolves to
210
+ * the specified error
211
+ */
212
+ stop(err?: Error): Promise<null | Error>;
213
+ }
214
+ export declare const ServiceErrorHeader = "Nats-Service-Error";
215
+ export declare const ServiceErrorCodeHeader = "Nats-Service-Error-Code";
216
+ export declare class ServiceError extends Error {
217
+ code: number;
218
+ constructor(code: number, message: string);
219
+ static isServiceError(msg: Msg): boolean;
220
+ static toServiceError(msg: Msg): ServiceError | null;
221
+ }
222
+ export declare enum ServiceVerb {
223
+ PING = "PING",
224
+ STATS = "STATS",
225
+ INFO = "INFO"
226
+ }
227
+ export interface ServiceClient {
228
+ /**
229
+ * Pings services
230
+ * @param name - optional
231
+ * @param id - optional
232
+ */
233
+ ping(name?: string, id?: string): Promise<QueuedIterator<ServiceIdentity>>;
234
+ /**
235
+ * Requests all the stats from services
236
+ * @param name
237
+ * @param id
238
+ */
239
+ stats(name?: string, id?: string): Promise<QueuedIterator<ServiceStats>>;
240
+ /**
241
+ * Requests info from services
242
+ * @param name
243
+ * @param id
244
+ */
245
+ info(name?: string, id?: string): Promise<QueuedIterator<ServiceInfo>>;
246
+ }
package/lib/types.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServiceVerb = exports.ServiceError = exports.ServiceErrorCodeHeader = exports.ServiceErrorHeader = exports.ServiceResponseType = void 0;
4
+ var ServiceResponseType;
5
+ (function (ServiceResponseType) {
6
+ ServiceResponseType["STATS"] = "io.nats.micro.v1.stats_response";
7
+ ServiceResponseType["INFO"] = "io.nats.micro.v1.info_response";
8
+ ServiceResponseType["PING"] = "io.nats.micro.v1.ping_response";
9
+ })(ServiceResponseType || (exports.ServiceResponseType = ServiceResponseType = {}));
10
+ exports.ServiceErrorHeader = "Nats-Service-Error";
11
+ exports.ServiceErrorCodeHeader = "Nats-Service-Error-Code";
12
+ class ServiceError extends Error {
13
+ code;
14
+ constructor(code, message) {
15
+ super(message);
16
+ this.code = code;
17
+ }
18
+ static isServiceError(msg) {
19
+ return ServiceError.toServiceError(msg) !== null;
20
+ }
21
+ static toServiceError(msg) {
22
+ const scode = msg?.headers?.get(exports.ServiceErrorCodeHeader) || "";
23
+ if (scode !== "") {
24
+ const code = parseInt(scode) || 400;
25
+ const description = msg?.headers?.get(exports.ServiceErrorHeader) || "";
26
+ return new ServiceError(code, description.length ? description : scode);
27
+ }
28
+ return null;
29
+ }
30
+ }
31
+ exports.ServiceError = ServiceError;
32
+ var ServiceVerb;
33
+ (function (ServiceVerb) {
34
+ ServiceVerb["PING"] = "PING";
35
+ ServiceVerb["STATS"] = "STATS";
36
+ ServiceVerb["INFO"] = "INFO";
37
+ })(ServiceVerb || (exports.ServiceVerb = ServiceVerb = {}));
38
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../build/src/types.ts"],"names":[],"mappings":";;;AA+EA,IAAY,mBAIX;AAJD,WAAY,mBAAmB;IAC7B,gEAAyC,CAAA;IACzC,8DAAuC,CAAA;IACvC,8DAAuC,CAAA;AACzC,CAAC,EAJW,mBAAmB,mCAAnB,mBAAmB,QAI9B;AAmKY,QAAA,kBAAkB,GAAG,oBAAoB,CAAC;AAC1C,QAAA,sBAAsB,GAAG,yBAAyB,CAAC;AAEhE,MAAa,YAAa,SAAQ,KAAK;IACrC,IAAI,CAAS;IAEb,YAAY,IAAY,EAAE,OAAe;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,MAAM,CAAC,cAAc,CAAC,GAAQ;QAC5B,OAAO,YAAY,CAAC,cAAc,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC;IACnD,CAAC;IAED,MAAM,CAAC,cAAc,CAAC,GAAQ;QAC5B,MAAM,KAAK,GAAG,GAAG,EAAE,OAAO,EAAE,GAAG,CAAC,8BAAsB,CAAC,IAAI,EAAE,CAAC;QAC9D,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC;YACpC,MAAM,WAAW,GAAG,GAAG,EAAE,OAAO,EAAE,GAAG,CAAC,0BAAkB,CAAC,IAAI,EAAE,CAAC;YAChE,OAAO,IAAI,YAAY,CAAC,IAAI,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QAC1E,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AArBD,oCAqBC;AAED,IAAY,WAIX;AAJD,WAAY,WAAW;IACrB,4BAAa,CAAA;IACb,8BAAe,CAAA;IACf,4BAAa,CAAA;AACf,CAAC,EAJW,WAAW,2BAAX,WAAW,QAItB"}
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@nats-io/services",
3
+ "version": "3.0.0-2",
4
+ "files": [
5
+ "lib/",
6
+ "build/src/"
7
+ ],
8
+ "types": "./lib/mod.d.js",
9
+ "exports": {
10
+ ".": "./lib/mod.js",
11
+ "./internal": "./lib/internal_mod.js"
12
+ },
13
+ "license": "Apache-2.0",
14
+ "private": false,
15
+ "scripts": {
16
+ "real-clean": "npm run clean && shx rm -Rf ./node_modules",
17
+ "clean": "shx rm -Rf ./build ./lib ./docs",
18
+ "pre-process": "npm run clean && deno run -A ../bin/cjs-fix-imports.ts -o ./build/src ./src",
19
+ "build-cjs": "npm run pre-process && tsc",
20
+ "build": "npm run build-cjs",
21
+ "doc": "npm run build && node_modules/.bin/typedoc --out ../docs/services && touch ../docs/services/.nojekyll",
22
+ "prepack": "npm run build"
23
+ },
24
+ "keywords": [],
25
+ "author": {
26
+ "name": "The NATS Authors"
27
+ },
28
+ "description": "services library - this library implements all the base functionality for NATS services for javascript clients",
29
+ "dependencies": {
30
+ "@nats-io/nats-core": "~3.0.0-19"
31
+ },
32
+ "devDependencies": {
33
+ "@types/node": "^22.0.0",
34
+ "shx": "^0.3.4",
35
+ "typedoc": "^0.26.5",
36
+ "typescript": "^5.5.4"
37
+ }
38
+ }