@nats-io/obj 3.0.0-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.
@@ -0,0 +1,340 @@
1
+ /*
2
+ * Copyright 2023-2024 The NATS Authors
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+ import type {
16
+ Placement,
17
+ PubAck,
18
+ PurgeResponse,
19
+ StorageType,
20
+ StreamInfo,
21
+ StreamInfoRequestOptions,
22
+ } from "@nats-io/jetstream";
23
+ import type { MsgHdrs, Nanos, QueuedIterator } from "@nats-io/nats-core";
24
+
25
+ export type ObjectStoreLink = {
26
+ /**
27
+ * name of object store storing the data
28
+ */
29
+ bucket: string;
30
+ /**
31
+ * link to single object, when empty this means the whole store
32
+ */
33
+ name?: string;
34
+ };
35
+ export type ObjectStoreMetaOptions = {
36
+ /**
37
+ * If set, the object is a reference to another entry.
38
+ */
39
+ link?: ObjectStoreLink;
40
+ /**
41
+ * The maximum size in bytes for each chunk.
42
+ * Note that if the size exceeds the maximum size of a stream
43
+ * entry, the number will be clamped to the streams maximum.
44
+ */
45
+ max_chunk_size?: number;
46
+ };
47
+ export type ObjectStoreMeta = {
48
+ name: string;
49
+ description?: string;
50
+ headers?: MsgHdrs;
51
+ options?: ObjectStoreMetaOptions;
52
+ metadata?: Record<string, string>;
53
+ };
54
+
55
+ export interface ObjectInfo extends ObjectStoreMeta {
56
+ /**
57
+ * The name of the bucket where the object is stored.
58
+ */
59
+ bucket: string;
60
+ /**
61
+ * The current ID of the entries holding the data for the object.
62
+ */
63
+ nuid: string;
64
+ /**
65
+ * The size in bytes of the object.
66
+ */
67
+ size: number;
68
+ /**
69
+ * The number of entries storing the object.
70
+ */
71
+ chunks: number;
72
+ /**
73
+ * A cryptographic checksum of the data as a whole.
74
+ */
75
+ digest: string;
76
+ /**
77
+ * True if the object was deleted.
78
+ */
79
+ deleted: boolean;
80
+ /**
81
+ * An UTC timestamp
82
+ */
83
+ mtime: string;
84
+ /**
85
+ * The revision number for the entry
86
+ */
87
+ revision: number;
88
+ }
89
+
90
+ /**
91
+ * A link reference
92
+ */
93
+ export interface ObjectLink {
94
+ /**
95
+ * The object store the source data
96
+ */
97
+ bucket: string;
98
+ /**
99
+ * The name of the entry holding the data. If not
100
+ * set it is a complete object store reference.
101
+ */
102
+ name?: string;
103
+ }
104
+
105
+ export type ObjectStoreStatus = {
106
+ /**
107
+ * The bucket name
108
+ */
109
+ bucket: string;
110
+ /**
111
+ * the description associated with the object store.
112
+ */
113
+ description: string;
114
+ /**
115
+ * The time to live for entries in the object store in nanoseconds.
116
+ * Convert to millis using the `millis()` function.
117
+ */
118
+ ttl: Nanos;
119
+ /**
120
+ * The object store's underlying stream storage type.
121
+ */
122
+ storage: StorageType;
123
+ /**
124
+ * The number of replicas associated with this object store.
125
+ */
126
+ replicas: number;
127
+ /**
128
+ * Set to true if the object store is sealed and will reject edits.
129
+ */
130
+ sealed: boolean;
131
+ /**
132
+ * The size in bytes that the object store occupies.
133
+ */
134
+ size: number;
135
+ /**
136
+ * The underlying storage for the object store. Currently, this always
137
+ * returns "JetStream".
138
+ */
139
+ backingStore: string;
140
+ /**
141
+ * The StreamInfo backing up the ObjectStore
142
+ */
143
+ streamInfo: StreamInfo;
144
+ /**
145
+ * Metadata the object store. Note that
146
+ * keys starting with `_nats` are reserved. This feature only supported on servers
147
+ * 2.10.x and better.
148
+ */
149
+ metadata?: Record<string, string> | undefined;
150
+ /**
151
+ * Compression level of the stream. This feature is only supported in
152
+ * servers 2.10.x and better.
153
+ */
154
+ compression: boolean;
155
+ };
156
+ /**
157
+ * @deprecated {@link ObjectStoreStatus}
158
+ */
159
+ export type ObjectStoreInfo = ObjectStoreStatus;
160
+ export type ObjectStoreOptions = {
161
+ /**
162
+ * A description for the object store
163
+ */
164
+ description?: string;
165
+ /**
166
+ * The time to live for entries in the object store specified
167
+ * as nanoseconds. Use the `nanos()` function to convert millis to
168
+ * nanos.
169
+ */
170
+ ttl?: Nanos;
171
+ /**
172
+ * The underlying stream storage type for the object store.
173
+ */
174
+ storage: StorageType;
175
+ /**
176
+ * The number of replicas to create.
177
+ */
178
+ replicas: number;
179
+ /**
180
+ * The maximum amount of data that the object store should store in bytes.
181
+ */
182
+ "max_bytes": number;
183
+ /**
184
+ * Placement hints for the underlying object store stream
185
+ */
186
+ placement: Placement; /**
187
+ * Metadata field to store additional information about the stream. Note that
188
+ * keys starting with `_nats` are reserved. This feature only supported on servers
189
+ * 2.10.x and better.
190
+ */
191
+ metadata?: Record<string, string>;
192
+ /**
193
+ * Sets the compression level of the stream. This feature is only supported in
194
+ * servers 2.10.x and better.
195
+ */
196
+ compression?: boolean;
197
+ };
198
+ /**
199
+ * An object that allows reading the object stored under a specified name.
200
+ */
201
+ export type ObjectResult = {
202
+ /**
203
+ * The info of the object that was retrieved.
204
+ */
205
+ info: ObjectInfo;
206
+ /**
207
+ * The readable stream where you can read the data.
208
+ */
209
+ data: ReadableStream<Uint8Array>;
210
+ /**
211
+ * A promise that will resolve to an error if the readable stream failed
212
+ * to process the entire response. Should be checked when the readable stream
213
+ * has finished yielding data.
214
+ */
215
+ error: Promise<Error | null>;
216
+ };
217
+ export type ObjectStorePutOpts = {
218
+ /**
219
+ * maximum number of millis for the put requests to succeed
220
+ */
221
+ timeout?: number;
222
+ /**
223
+ * If set the ObjectStore must be at the current sequence or the
224
+ * put will fail. Note the sequence accounts where the metadata
225
+ * for the entry is stored.
226
+ */
227
+ previousRevision?: number;
228
+ };
229
+
230
+ export interface ObjectStore {
231
+ /**
232
+ * Returns the ObjectInfo of the named entry. Or null if the
233
+ * entry doesn't exist.
234
+ * @param name
235
+ */
236
+ info(name: string): Promise<ObjectInfo | null>;
237
+
238
+ /**
239
+ * Returns a list of the entries in the ObjectStore
240
+ */
241
+ list(): Promise<ObjectInfo[]>;
242
+
243
+ /**
244
+ * Returns an object you can use for reading the data from the
245
+ * named stored object or null if the entry doesn't exist.
246
+ * @param name
247
+ */
248
+ get(name: string): Promise<ObjectResult | null>;
249
+
250
+ /**
251
+ * Returns the data stored for the named entry.
252
+ * @param name
253
+ */
254
+ getBlob(name: string): Promise<Uint8Array | null>;
255
+
256
+ /**
257
+ * Adds an object to the store with the specified meta
258
+ * and using the specified ReadableStream to stream the data.
259
+ * @param meta
260
+ * @param rs
261
+ * @param opts
262
+ */
263
+ put(
264
+ meta: ObjectStoreMeta,
265
+ rs: ReadableStream<Uint8Array>,
266
+ opts?: ObjectStorePutOpts,
267
+ ): Promise<ObjectInfo>;
268
+
269
+ /**
270
+ * Puts the specified bytes into the store with the specified meta.
271
+ * @param meta
272
+ * @param data
273
+ * @param opts
274
+ */
275
+ putBlob(
276
+ meta: ObjectStoreMeta,
277
+ data: Uint8Array | null,
278
+ opts?: ObjectStorePutOpts,
279
+ ): Promise<ObjectInfo>;
280
+
281
+ /**
282
+ * Deletes the specified entry from the object store.
283
+ * @param name
284
+ */
285
+ delete(name: string): Promise<PurgeResponse>;
286
+
287
+ /**
288
+ * Adds a link to another object in the same store or a different one.
289
+ * Note that links of links are rejected.
290
+ * object.
291
+ * @param name
292
+ * @param meta
293
+ */
294
+ link(name: string, meta: ObjectInfo): Promise<ObjectInfo>;
295
+
296
+ /**
297
+ * Add a link to another object store
298
+ * @param name
299
+ * @param bucket
300
+ */
301
+ linkStore(name: string, bucket: ObjectStore): Promise<ObjectInfo>;
302
+
303
+ /**
304
+ * Watch an object store and receive updates of modifications via
305
+ * an iterator.
306
+ * @param opts
307
+ */
308
+ watch(
309
+ opts?: Partial<
310
+ {
311
+ ignoreDeletes?: boolean;
312
+ includeHistory?: boolean;
313
+ }
314
+ >,
315
+ ): Promise<QueuedIterator<ObjectInfo | null>>;
316
+
317
+ /**
318
+ * Seals the object store preventing any further modifications.
319
+ */
320
+ seal(): Promise<ObjectStoreStatus>;
321
+
322
+ /**
323
+ * Returns the runtime status of the object store.
324
+ * @param opts
325
+ */
326
+ status(opts?: Partial<StreamInfoRequestOptions>): Promise<ObjectStoreStatus>;
327
+
328
+ /**
329
+ * Update the metadata for an object. If the name is modified, the object
330
+ * is effectively renamed and will only be accessible by its new name.
331
+ * @param name
332
+ * @param meta
333
+ */
334
+ update(name: string, meta: Partial<ObjectStoreMeta>): Promise<PubAck>;
335
+
336
+ /**
337
+ * Destroys the object store and all its entries.
338
+ */
339
+ destroy(): Promise<boolean>;
340
+ }
@@ -0,0 +1,2 @@
1
+ export type { ObjectInfo, ObjectResult, ObjectStore, ObjectStoreLink, ObjectStoreMeta, ObjectStoreMetaOptions, ObjectStoreOptions, ObjectStorePutOpts, ObjectStoreStatus, } from "./types";
2
+ export { Objm } from "./objectstore";
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Objm = void 0;
4
+ var objectstore_1 = require("./objectstore");
5
+ Object.defineProperty(exports, "Objm", { enumerable: true, get: function () { return objectstore_1.Objm; } });
6
+ //# sourceMappingURL=internal_mod.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal_mod.js","sourceRoot":"","sources":["../build/src/internal_mod.ts"],"names":[],"mappings":";;;AAYA,6CAAqC;AAA5B,mGAAA,IAAI,OAAA"}
package/lib/mod.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export type { ObjectInfo, ObjectResult, ObjectStore, ObjectStoreLink, ObjectStoreMeta, ObjectStoreMetaOptions, ObjectStoreOptions, ObjectStorePutOpts, ObjectStoreStatus, } from "./internal_mod";
2
+ export { Objm } from "./objectstore";
package/lib/mod.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Objm = void 0;
4
+ var objectstore_1 = require("./objectstore");
5
+ Object.defineProperty(exports, "Objm", { enumerable: true, get: function () { return objectstore_1.Objm; } });
6
+ //# sourceMappingURL=mod.js.map
package/lib/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sourceRoot":"","sources":["../build/src/mod.ts"],"names":[],"mappings":";;;AAYA,6CAAqC;AAA5B,mGAAA,IAAI,OAAA"}
@@ -0,0 +1,101 @@
1
+ import type { NatsConnection, QueuedIterator } from "@nats-io/nats-core/internal";
2
+ import type { JetStreamClient, JetStreamClientImpl, JetStreamManager, Lister, PubAck, PurgeResponse, StorageType, StreamInfo, StreamInfoRequestOptions } from "@nats-io/jetstream/internal";
3
+ import type { ObjectInfo, ObjectResult, ObjectStore, ObjectStoreMeta, ObjectStoreMetaOptions, ObjectStoreOptions, ObjectStorePutOpts, ObjectStoreStatus } from "./types";
4
+ export declare const osPrefix = "OBJ_";
5
+ export declare const digestType = "SHA-256=";
6
+ export declare function objectStoreStreamName(bucket: string): string;
7
+ export declare function objectStoreBucketName(stream: string): string;
8
+ /**
9
+ * The entry point to creating and managing new ObjectStore instances.
10
+ */
11
+ export declare class Objm {
12
+ #private;
13
+ js: JetStreamClientImpl;
14
+ /**
15
+ * Creates an instance of the Objm that allows you to create and access ObjectStore.
16
+ * Note that if the argument is a NatsConnection, default JetStream Options are
17
+ * used. If you want to set some options, please provide a JetStreamClient instead.
18
+ * @param nc
19
+ */
20
+ constructor(nc: JetStreamClient | NatsConnection);
21
+ /**
22
+ * Creates and opens the specified ObjectStore. If the Object already exists, it opens the existing ObjectStore.
23
+ * @param name
24
+ * @param opts
25
+ */
26
+ create(name: string, opts?: Partial<ObjectStoreOptions>): Promise<ObjectStore>;
27
+ /**
28
+ * Returns a list of ObjectStoreInfo for all streams that are identified as
29
+ * being a ObjectStore (that is having names that have the prefix `OBJ_`)
30
+ */
31
+ list(): Lister<ObjectStoreStatus>;
32
+ }
33
+ export declare class ObjectStoreStatusImpl implements ObjectStoreStatus {
34
+ si: StreamInfo;
35
+ backingStore: string;
36
+ constructor(si: StreamInfo);
37
+ get bucket(): string;
38
+ get description(): string;
39
+ get ttl(): number;
40
+ get storage(): StorageType;
41
+ get replicas(): number;
42
+ get sealed(): boolean;
43
+ get size(): number;
44
+ get streamInfo(): StreamInfo;
45
+ get metadata(): Record<string, string> | undefined;
46
+ get compression(): boolean;
47
+ }
48
+ export declare function validateBucket(name: string): void;
49
+ export type ServerObjectStoreMeta = {
50
+ name: string;
51
+ description?: string;
52
+ headers?: Record<string, string[]>;
53
+ options?: ObjectStoreMetaOptions;
54
+ };
55
+ export type ServerObjectInfo = {
56
+ bucket: string;
57
+ nuid: string;
58
+ size: number;
59
+ chunks: number;
60
+ digest: string;
61
+ deleted?: boolean;
62
+ mtime: string;
63
+ revision: number;
64
+ metadata?: Record<string, string>;
65
+ } & ServerObjectStoreMeta;
66
+ export declare class ObjectStoreImpl implements ObjectStore {
67
+ jsm: JetStreamManager;
68
+ js: JetStreamClient;
69
+ stream: string;
70
+ name: string;
71
+ constructor(name: string, jsm: JetStreamManager, js: JetStreamClient);
72
+ _checkNotEmpty(name: string): {
73
+ name: string;
74
+ error?: Error;
75
+ };
76
+ info(name: string): Promise<ObjectInfo | null>;
77
+ list(): Promise<ObjectInfo[]>;
78
+ rawInfo(name: string): Promise<ServerObjectInfo | null>;
79
+ _si(opts?: Partial<StreamInfoRequestOptions>): Promise<StreamInfo | null>;
80
+ seal(): Promise<ObjectStoreStatus>;
81
+ status(opts?: Partial<StreamInfoRequestOptions>): Promise<ObjectStoreStatus>;
82
+ destroy(): Promise<boolean>;
83
+ _put(meta: ObjectStoreMeta, rs: ReadableStream<Uint8Array> | null, opts?: ObjectStorePutOpts): Promise<ObjectInfo>;
84
+ putBlob(meta: ObjectStoreMeta, data: Uint8Array | null, opts?: ObjectStorePutOpts): Promise<ObjectInfo>;
85
+ put(meta: ObjectStoreMeta, rs: ReadableStream<Uint8Array> | null, opts?: ObjectStorePutOpts): Promise<ObjectInfo>;
86
+ getBlob(name: string): Promise<Uint8Array | null>;
87
+ get(name: string): Promise<ObjectResult | null>;
88
+ linkStore(name: string, bucket: ObjectStore): Promise<ObjectInfo>;
89
+ link(name: string, info: ObjectInfo): Promise<ObjectInfo>;
90
+ delete(name: string): Promise<PurgeResponse>;
91
+ update(name: string, meta?: Partial<ObjectStoreMeta>): Promise<PubAck>;
92
+ watch(opts?: Partial<{
93
+ ignoreDeletes?: boolean;
94
+ includeHistory?: boolean;
95
+ }>): Promise<QueuedIterator<ObjectInfo | null>>;
96
+ _chunkSubject(id: string): string;
97
+ _metaSubject(n: string): string;
98
+ _metaSubjectAll(): string;
99
+ init(opts?: Partial<ObjectStoreOptions>): Promise<void>;
100
+ static create(js: JetStreamClient, name: string, opts?: Partial<ObjectStoreOptions>): Promise<ObjectStore>;
101
+ }