Skip to content

Commit 9b00db3

Browse files
committed
[CHANGE] [KV] is now its own module
1 parent c14b90f commit 9b00db3

18 files changed

+977
-776
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
9+
deno test --allow-all --parallel --reload --quiet --coverage=coverage tests/ jetstream/tests kv/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/
14+
deno test --allow-all --unstable --reload --parallel --watch --fail-fast tests/ jetstream/ kv/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/ jetstream.md README.md services.md
28+
deno fmt src/ doc/ bin/ nats-base-client/ examples/ tests/ debug/ unsafe_tests/ jetstream/ kv/ jetstream.md README.md migration.md services.md

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A Deno client for the [NATS messaging system](https://nats.io).
1111
You can get the latest release version like this:
1212

1313
```typescript
14-
import * as nats from "https://deno.land/x/nats/src/mod.ts";
14+
import * as nats from "https://deno.land/x/nats/src/types.ts";
1515
```
1616

1717
To specify a specific released version, simply replace nats with
@@ -20,7 +20,7 @@ nats@_versionTag_.
2020
You can get the current development version by:
2121

2222
```typescript
23-
import * as nats from "https://raw.githubusercontent.com/nats-io/nats.deno/main/src/mod.ts";
23+
import * as nats from "https://raw.githubusercontent.com/nats-io/nats.deno/main/src/types.ts";
2424
```
2525

2626
## Documentation
@@ -54,7 +54,7 @@ is working.
5454

5555
```typescript
5656
// import the connect function
57-
import { connect } from "../../src/mod.ts";
57+
import { connect } from "../../src/types.ts";
5858

5959
const servers = [
6060
{},
@@ -123,7 +123,7 @@ the server.
123123

124124
```typescript
125125
// import the connect function
126-
import { connect, StringCodec } from "../../src/mod.ts";
126+
import { connect, StringCodec } from "../../src/types.ts";
127127

128128
// to create a connection to a nats-server:
129129
const nc = await connect({ servers: "demo.nats.io:4222" });
@@ -191,7 +191,7 @@ All subscriptions are independent. If two different subscriptions match a
191191
subject, both will get to process the message:
192192

193193
```javascript
194-
import { connect, StringCodec, Subscription } from "../../src/mod.ts";
194+
import { connect, StringCodec, Subscription } from "../../src/types.ts";
195195
const nc = await connect({ servers: "demo.nats.io:4222" });
196196
const sc = StringCodec();
197197

@@ -244,7 +244,7 @@ simply to illustrate not only how to create responses, but how the subject
244244
itself is used to dispatch different behaviors.
245245

246246
```typescript
247-
import { connect, StringCodec, Subscription } from "../../src/mod.ts";
247+
import { connect, StringCodec, Subscription } from "../../src/types.ts";
248248

249249
// create a connection
250250
const nc = await connect({ servers: "demo.nats.io" });
@@ -319,7 +319,7 @@ Here's a simple example of a client making a simple request from the service
319319
above:
320320

321321
```typescript
322-
import { connect, Empty, StringCodec } from "../../src/mod.ts";
322+
import { connect, Empty, StringCodec } from "../../src/types.ts";
323323

324324
// create a connection
325325
const nc = await connect({ servers: "demo.nats.io:4222" });
@@ -357,7 +357,7 @@ import {
357357
NatsConnection,
358358
StringCodec,
359359
Subscription,
360-
} from "../../src/mod.ts";
360+
} from "../../src/types.ts";
361361

362362
// this is the definition of a service with `count` members in them.
363363
// if the queue is specified, the they will be part of a queue
@@ -436,7 +436,7 @@ are publishing a message with a header, it is possible for the recipient to not
436436
support them.
437437

438438
```typescript
439-
import { connect, createInbox, Empty, headers } from "../../src/mod.ts";
439+
import { connect, createInbox, Empty, headers } from "../../src/types.ts";
440440
import { nuid } from "../../nats-base-client/nuid.ts";
441441

442442
const nc = await connect(
@@ -525,7 +525,7 @@ Setting the `user`/`pass` or `token` options, simply initializes an
525525
```typescript
526526
// if the connection requires authentication, provide `user` and `pass` or
527527
// `token` options in the NatsConnectionOptions
528-
import { connect } from "src/mod.ts";
528+
import { connect } from "src/types.ts";
529529

530530
const nc1 = await connect({
531531
servers: "127.0.0.1:4222",

jetstream/internal_mod.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,10 @@ export {
2222
DirectMsgHeaders,
2323
isConsumerOptsBuilder,
2424
JsHeaders,
25-
KvWatchInclude,
2625
RepublishHeaders,
2726
} from "./types.ts";
2827

29-
export { jetstream } from "./jsclient.ts";
30-
31-
export {
32-
Base64KeyCodec,
33-
Bucket,
34-
defaultBucketOpts,
35-
NoopKvCodecs,
36-
} from "./kv.ts";
28+
export { jetstream, JetStreamClientImpl } from "./jsclient.ts";
3729

3830
export type {
3931
AbortOnMissingResource,
@@ -54,6 +46,7 @@ export type {
5446
Consumers,
5547
ConsumerStatus,
5648
Destroyable,
49+
DirectStreamAPI,
5750
Expires,
5851
FetchBytes,
5952
FetchMessages,
@@ -69,16 +62,6 @@ export type {
6962
JetStreamSubscriptionInfoable,
7063
JetStreamSubscriptionOptions,
7164
JsMsgCallback,
72-
KV,
73-
KvCodec,
74-
KvCodecs,
75-
KvDeleteOptions,
76-
KvEntry,
77-
KvLimits,
78-
KvOptions,
79-
KvPutOptions,
80-
KvStatus,
81-
KvWatchOptions,
8265
Lister,
8366
MaxBytes,
8467
MaxMessages,
@@ -95,7 +78,6 @@ export type {
9578
OrderedConsumerOptions,
9679
PubAck,
9780
Pullable,
98-
RoKV,
9981
StoredMsg,
10082
Stream,
10183
StreamAPI,
@@ -149,6 +131,7 @@ export {
149131
AckPolicy,
150132
DeliverPolicy,
151133
DiscardPolicy,
134+
PubHeaders,
152135
ReplayPolicy,
153136
RetentionPolicy,
154137
StorageType,

jetstream/jsbaseclient_api.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ export class BaseApiClientImpl implements BaseClient {
120120
const err = checkJsErrorCode(r.error.code, r.error.description);
121121
if (err !== null) {
122122
err.api_error = r.error;
123+
if (r.error.description !== "") {
124+
err.message = r.error.description;
125+
}
123126
throw err;
124127
}
125128
}

jetstream/jsclient.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import {
4545
timeout,
4646
} from "../nats-base-client/util.ts";
4747
import { headers } from "../nats-base-client/headers.ts";
48-
import { Bucket } from "./kv.ts";
4948
import { Feature } from "../nats-base-client/semver.ts";
5049
import { ObjectStoreImpl } from "./objectstore.ts";
5150
import { IdleHeartbeatMonitor } from "../nats-base-client/idleheartbeat_monitor.ts";
@@ -73,8 +72,6 @@ import {
7372
JetStreamSubscriptionInfoable,
7473
JetStreamSubscriptionOptions,
7574
JsHeaders,
76-
KV,
77-
KvOptions,
7875
ObjectStore,
7976
ObjectStoreOptions,
8077
PubAck,
@@ -122,15 +119,14 @@ export function jetstream(
122119
return new JetStreamClientImpl(nc, opts);
123120
}
124121

125-
126122
/**
127123
* Returns a {@link JetStreamManager} supported by the specified NatsConnection
128124
* @param nc
129125
* @param opts
130126
*/
131127
export async function jetstreamManager(
132-
nc: NatsConnection,
133-
opts: JetStreamOptions | JetStreamManagerOptions = {},
128+
nc: NatsConnection,
129+
opts: JetStreamOptions | JetStreamManagerOptions = {},
134130
): Promise<JetStreamManager> {
135131
const adm = new JetStreamManagerImpl(nc, opts);
136132
if ((opts as JetStreamManagerOptions).checkAPI !== false) {
@@ -152,20 +148,20 @@ class ViewsImpl implements Views {
152148
constructor(js: JetStreamClientImpl) {
153149
this.js = js;
154150
}
155-
kv(name: string, opts: Partial<KvOptions> = {}): Promise<KV> {
156-
const jsi = this.js as JetStreamClientImpl;
157-
const { ok, min } = jsi.nc.features.get(Feature.JS_KV);
158-
if (!ok) {
159-
return Promise.reject(
160-
new Error(`kv is only supported on servers ${min} or better`),
161-
);
162-
}
163-
if (opts.bindOnly) {
164-
return Bucket.bind(this.js, name, opts);
165-
}
166-
167-
return Bucket.create(this.js, name, opts);
168-
}
151+
// kv(name: string, opts: Partial<KvOptions> = {}): Promise<KV> {
152+
// const jsi = this.js as JetStreamClientImpl;
153+
// const { ok, min } = jsi.nc.features.get(Feature.JS_KV);
154+
// if (!ok) {
155+
// return Promise.reject(
156+
// new Error(`kv is only supported on servers ${min} or better`),
157+
// );
158+
// }
159+
// if (opts.bindOnly) {
160+
// return Bucket.bind(this.js, name, opts);
161+
// }
162+
//
163+
// return Bucket.create(this.js, name, opts);
164+
// }
169165
os(
170166
name: string,
171167
opts: Partial<ObjectStoreOptions> = {},

jetstream/jsmstream_api.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { BaseApiClientImpl, StreamNames } from "./jsbaseclient_api.ts";
1818
import { ListerImpl } from "./jslister.ts";
1919
import { validateStreamName } from "./jsutil.ts";
2020
import { headers, MsgHdrsImpl } from "../nats-base-client/headers.ts";
21-
import { KvStatusImpl } from "./kv.ts";
2221
import { ObjectStoreStatusImpl, osPrefix } from "./objectstore.ts";
2322
import { Codec, JSONCodec } from "../nats-base-client/codec.ts";
2423
import { TD } from "../nats-base-client/encoders.ts";
@@ -29,8 +28,6 @@ import {
2928
ConsumerAPI,
3029
Consumers,
3130
JetStreamOptions,
32-
kvPrefix,
33-
KvStatus,
3431
Lister,
3532
ListerFieldFilter,
3633
ObjectStoreStatus,
@@ -451,29 +448,6 @@ export class StreamAPIImpl extends BaseApiClientImpl implements StreamAPI {
451448
return this.findStream(subject);
452449
}
453450

454-
listKvs(): Lister<KvStatus> {
455-
const filter: ListerFieldFilter<KvStatus> = (
456-
v: unknown,
457-
): KvStatus[] => {
458-
const slr = v as StreamListResponse;
459-
const kvStreams = slr.streams.filter((v) => {
460-
return v.config.name.startsWith(kvPrefix);
461-
});
462-
kvStreams.forEach((si) => {
463-
this._fixInfo(si);
464-
});
465-
let cluster = "";
466-
if (kvStreams.length) {
467-
cluster = this.nc.info?.cluster ?? "";
468-
}
469-
return kvStreams.map((si) => {
470-
return new KvStatusImpl(si, cluster);
471-
});
472-
};
473-
const subj = `${this.prefix}.STREAM.LIST`;
474-
return new ListerImpl<KvStatus>(subj, filter, this);
475-
}
476-
477451
listObjectStores(): Lister<ObjectStoreStatus> {
478452
const filter: ListerFieldFilter<ObjectStoreStatus> = (
479453
v: unknown,

jetstream/mod.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export {
2929
DirectMsgHeaders,
3030
DiscardPolicy,
3131
JsHeaders,
32-
KvWatchInclude,
3332
ReplayPolicy,
3433
RepublishHeaders,
3534
RetentionPolicy,
@@ -82,16 +81,6 @@ export type {
8281
JetStreamUsageAccountLimits,
8382
JsMsg,
8483
JsMsgCallback,
85-
KV,
86-
KvCodec,
87-
KvCodecs,
88-
KvDeleteOptions,
89-
KvEntry,
90-
KvLimits,
91-
KvOptions,
92-
KvPutOptions,
93-
KvStatus,
94-
KvWatchOptions,
9584
LastForMsgRequest,
9685
Lister,
9786
LostStreamData,
@@ -121,7 +110,6 @@ export type {
121110
PurgeResponse,
122111
PurgeTrimOpts,
123112
Republish,
124-
RoKV,
125113
SeqMsgRequest,
126114
SequenceInfo,
127115
StoredMsg,

jetstream/objectstore.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { validateBucket } from "./kv.ts";
1716
import { Base64UrlPaddedCodec } from "../nats-base-client/base64.ts";
1817
import { JSONCodec } from "../nats-base-client/codec.ts";
1918
import { nuid } from "../nats-base-client/nuid.ts";
@@ -114,6 +113,12 @@ export class ObjectStoreStatusImpl implements ObjectStoreStatus {
114113
return false;
115114
}
116115
}
116+
export function validateBucket(name: string) {
117+
const validBucketRe = /^[-\w]+$/;
118+
if (!validBucketRe.test(name)) {
119+
throw new Error(`invalid bucket name: ${name}`);
120+
}
121+
}
117122

118123
export type ServerObjectStoreMeta = {
119124
name: string;

0 commit comments

Comments
 (0)