@ocfl/ocfl 0.4.0 → 1.0.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.
Files changed (138) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +24 -9
  3. package/dist/constants.d.ts +15 -0
  4. package/dist/constants.d.ts.map +1 -0
  5. package/dist/constants.js +14 -0
  6. package/dist/constants.js.map +1 -0
  7. package/dist/digest.d.ts +58 -0
  8. package/dist/digest.d.ts.map +1 -0
  9. package/dist/digest.js +204 -0
  10. package/dist/digest.js.map +1 -0
  11. package/dist/error.d.ts +5 -0
  12. package/dist/error.d.ts.map +1 -0
  13. package/dist/error.js +9 -0
  14. package/dist/error.js.map +1 -0
  15. package/dist/extension.d.ts +83 -0
  16. package/dist/extension.d.ts.map +1 -0
  17. package/dist/extension.js +170 -0
  18. package/dist/extension.js.map +1 -0
  19. package/dist/extensions/0002-flat-direct-storage-layout.d.ts +12 -0
  20. package/dist/extensions/0002-flat-direct-storage-layout.d.ts.map +1 -0
  21. package/dist/extensions/0002-flat-direct-storage-layout.js +34 -0
  22. package/dist/extensions/0002-flat-direct-storage-layout.js.map +1 -0
  23. package/dist/extensions/0003-hash-and-id-n-tuple-storage-layout.d.ts +15 -0
  24. package/dist/extensions/0003-hash-and-id-n-tuple-storage-layout.d.ts.map +1 -0
  25. package/dist/extensions/0003-hash-and-id-n-tuple-storage-layout.js +49 -0
  26. package/dist/extensions/0003-hash-and-id-n-tuple-storage-layout.js.map +1 -0
  27. package/dist/extensions/0004-hashed-n-tuple-storage-layout.d.ts +17 -0
  28. package/dist/extensions/0004-hashed-n-tuple-storage-layout.d.ts.map +1 -0
  29. package/dist/extensions/0004-hashed-n-tuple-storage-layout.js +54 -0
  30. package/dist/extensions/0004-hashed-n-tuple-storage-layout.js.map +1 -0
  31. package/dist/extensions/0006-flat-omit-prefix-storage-layout.d.ts +6 -0
  32. package/dist/extensions/0006-flat-omit-prefix-storage-layout.d.ts.map +1 -0
  33. package/dist/extensions/0006-flat-omit-prefix-storage-layout.js +8 -0
  34. package/dist/extensions/0006-flat-omit-prefix-storage-layout.js.map +1 -0
  35. package/dist/extensions/0007-n-tuple-omit-prefix-storage-layout.d.ts +6 -0
  36. package/dist/extensions/0007-n-tuple-omit-prefix-storage-layout.d.ts.map +1 -0
  37. package/dist/extensions/0007-n-tuple-omit-prefix-storage-layout.js +8 -0
  38. package/dist/extensions/0007-n-tuple-omit-prefix-storage-layout.js.map +1 -0
  39. package/dist/extensions/0009-digest-algorithms.d.ts +9 -0
  40. package/dist/extensions/0009-digest-algorithms.d.ts.map +1 -0
  41. package/dist/extensions/0009-digest-algorithms.js +11 -0
  42. package/dist/extensions/0009-digest-algorithms.js.map +1 -0
  43. package/dist/extensions/000N-path-direct-storage-layout.d.ts +16 -0
  44. package/dist/extensions/000N-path-direct-storage-layout.d.ts.map +1 -0
  45. package/dist/extensions/000N-path-direct-storage-layout.js +46 -0
  46. package/dist/extensions/000N-path-direct-storage-layout.js.map +1 -0
  47. package/dist/extensions/index.d.ts +23 -0
  48. package/dist/extensions/index.d.ts.map +1 -0
  49. package/dist/extensions/index.js +8 -0
  50. package/dist/extensions/index.js.map +1 -0
  51. package/dist/file.d.ts +87 -0
  52. package/dist/file.d.ts.map +1 -0
  53. package/dist/file.js +118 -0
  54. package/dist/file.js.map +1 -0
  55. package/dist/index.d.ts +61 -0
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +73 -0
  58. package/dist/index.js.map +1 -0
  59. package/dist/inventory.d.ts +155 -0
  60. package/dist/inventory.d.ts.map +1 -0
  61. package/dist/inventory.js +591 -0
  62. package/dist/inventory.js.map +1 -0
  63. package/dist/object.d.ts +188 -0
  64. package/dist/object.d.ts.map +1 -0
  65. package/dist/object.js +425 -0
  66. package/dist/object.js.map +1 -0
  67. package/dist/storage.d.ts +94 -0
  68. package/dist/storage.d.ts.map +1 -0
  69. package/dist/storage.js +302 -0
  70. package/dist/storage.js.map +1 -0
  71. package/dist/store.d.ts +100 -0
  72. package/dist/store.d.ts.map +1 -0
  73. package/dist/store.js +157 -0
  74. package/dist/store.js.map +1 -0
  75. package/dist/transaction.d.ts +155 -0
  76. package/dist/transaction.d.ts.map +1 -0
  77. package/dist/transaction.js +395 -0
  78. package/dist/transaction.js.map +1 -0
  79. package/dist/types.d.ts +107 -0
  80. package/dist/types.d.ts.map +1 -0
  81. package/dist/types.js +2 -0
  82. package/dist/types.js.map +1 -0
  83. package/dist/utils.d.ts +24 -0
  84. package/dist/utils.d.ts.map +1 -0
  85. package/dist/utils.js +85 -0
  86. package/dist/utils.js.map +1 -0
  87. package/dist/validation.d.ts +32 -0
  88. package/dist/validation.d.ts.map +1 -0
  89. package/dist/validation.js +31 -0
  90. package/dist/validation.js.map +1 -0
  91. package/package.json +31 -51
  92. package/src/constants.ts +16 -0
  93. package/src/digest.ts +258 -0
  94. package/src/error.ts +9 -0
  95. package/src/extension.ts +206 -0
  96. package/{lib/extensions/0002-flat-direct-storage-layout.js → src/extensions/0002-flat-direct-storage-layout.ts} +15 -22
  97. package/src/extensions/0003-hash-and-id-n-tuple-storage-layout.ts +56 -0
  98. package/src/extensions/0004-hashed-n-tuple-storage-layout.ts +59 -0
  99. package/src/extensions/0006-flat-omit-prefix-storage-layout.ts +9 -0
  100. package/src/extensions/0007-n-tuple-omit-prefix-storage-layout.ts +9 -0
  101. package/src/extensions/0009-digest-algorithms.ts +12 -0
  102. package/src/extensions/000N-path-direct-storage-layout.ts +51 -0
  103. package/src/extensions/index.ts +24 -0
  104. package/src/file.ts +161 -0
  105. package/src/index.ts +92 -0
  106. package/src/inventory.ts +634 -0
  107. package/src/object.ts +473 -0
  108. package/src/storage.ts +342 -0
  109. package/src/store.ts +199 -0
  110. package/src/transaction.ts +456 -0
  111. package/{lib/types.d.ts → src/types.ts} +28 -74
  112. package/src/utils.ts +94 -0
  113. package/src/validation.ts +46 -0
  114. package/index.js +0 -129
  115. package/index.mjs +0 -18
  116. package/lib/constants.js +0 -24
  117. package/lib/digest.js +0 -307
  118. package/lib/enum.js +0 -90
  119. package/lib/error.js +0 -19
  120. package/lib/extension.js +0 -194
  121. package/lib/extensions/0003-hash-and-id-n-tuple-storage-layout.js +0 -60
  122. package/lib/extensions/0004-hashed-n-tuple-storage-layout.js +0 -63
  123. package/lib/extensions/0005-mutable-head.js +0 -0
  124. package/lib/extensions/0006-flat-omit-prefix-storage-layout.js +0 -7
  125. package/lib/extensions/0007-n-tuple-omit-prefix-storage-layout.js +0 -7
  126. package/lib/extensions/0008-schema-registry.js +0 -0
  127. package/lib/extensions/0009-digest-algorithms.js +0 -18
  128. package/lib/extensions/000N-path-direct-storage-layout.js +0 -61
  129. package/lib/extensions/index.js +0 -13
  130. package/lib/file.js +0 -147
  131. package/lib/index.js +0 -13
  132. package/lib/inventory.js +0 -564
  133. package/lib/object.js +0 -499
  134. package/lib/storage.js +0 -338
  135. package/lib/store.js +0 -191
  136. package/lib/transaction.js +0 -448
  137. package/lib/utils.js +0 -122
  138. package/lib/validation.js +0 -174
package/src/storage.ts ADDED
@@ -0,0 +1,342 @@
1
+ //@todo add default layout
2
+ import path from 'node:path';
3
+ import { OcflConstants } from './constants.ts';
4
+ import { NotImplementedError } from './error.ts';
5
+ import { createByName, OcflStorageLayout } from './extension.ts';
6
+ import { HashedNTupleStorageLayout } from './extensions/0004-hashed-n-tuple-storage-layout.ts';
7
+ import { OcflObject } from './object.ts';
8
+ import { findNamasteVersion, isDirEmpty } from './utils.ts';
9
+ import { createError } from './validation.ts';
10
+
11
+ const { OCFL_VERSION, OCFL_LAYOUT, EXTENSIONS_DIR, EXTENSION_CONFIG, NAMASTE_PREFIX_STORAGE, NAMASTE_PREFIX_OBJECT, NAMASTE_T } = OcflConstants;
12
+
13
+ import { OcflDigest } from './digest.ts';
14
+
15
+ const DEFAULT_LAYOUT = HashedNTupleStorageLayout;
16
+
17
+ import type { OcflStore } from './store.ts';
18
+ import type { OcflExtensionConfig, OcflObjectConfig, OcflStorageConfig, OcflVersion } from './types.ts';
19
+
20
+ /**
21
+ * An abstraction to represent the OCFL Storage Root, which is
22
+ * a base directory used to store OCFL Objects, identified by a [NAMASTE] file “0=ocfl_1.1”.
23
+ */
24
+
25
+ class OcflStorage {
26
+ ocflVersion: OcflVersion = OCFL_VERSION;
27
+
28
+ /**
29
+ * Get the storage root
30
+ */
31
+ get root(): string {
32
+ throw new NotImplementedError();
33
+ }
34
+
35
+ /**
36
+ * Construct the path of the object root relative to the storage root
37
+ * The path is mapped from the object identifier using the algorithm defined in the chosen
38
+ * storage layout extension (specified in {@link OcflStorageConfig}).
39
+ * @param id - Identifier of the object
40
+ */
41
+ objectRoot(_id: string): string {
42
+ throw new NotImplementedError();
43
+ }
44
+
45
+ /**
46
+ * Absolute path to the object root
47
+ * @param id - Identifier of the object
48
+ */
49
+ objectRootAbs(id: string) {
50
+ return path.join(this.root, this.objectRoot(id));
51
+ }
52
+
53
+ /**
54
+ * Get an existing object in the Storage or create a new one.
55
+ * @param opt - A unique identifier for the OCFL Object, should be a URI
56
+ */
57
+ object(_opt: string | { id?: string | undefined; root?: string | undefined }): OcflObject {
58
+ throw new NotImplementedError();
59
+ }
60
+
61
+ /**
62
+ * Check if the storage has already the object with the given id.
63
+ */
64
+ async has(id: string) {
65
+ return this.object(id).exists();
66
+ }
67
+
68
+ /**
69
+ * Create the storage root in the underlying storage backend
70
+ * @param options - Additional options for creating storage root
71
+ */
72
+ async create(_options?: { ocflSpecText?: boolean | undefined }): Promise<OcflStorage> {
73
+ throw new NotImplementedError();
74
+ }
75
+
76
+ /**
77
+ * Check namaste, retrieve storage layout extension, and read its config file.
78
+ */
79
+ async load(): Promise<OcflStorage> {
80
+ throw new NotImplementedError();
81
+ }
82
+
83
+ /**
84
+ * Check if the storage root path points to an existing file or non-empty directory in the underlying backend store.
85
+ * The existing directory may or may not be a valid OCFL Storage.
86
+ */
87
+ async exists(): Promise<boolean> {
88
+ throw new NotImplementedError();
89
+ }
90
+
91
+ [Symbol.asyncIterator](): AsyncIterableIterator<OcflObject> {
92
+ throw new NotImplementedError();
93
+ }
94
+
95
+ /**
96
+ * Return a new AsyncIterator that contains all the OCFL objects in this Storage.
97
+ */
98
+ objects(): AsyncIterableIterator<OcflObject> {
99
+ return this[Symbol.asyncIterator]();
100
+ // const objects = [];
101
+ // for await (const o of this) objects.push(o);
102
+ // return objects;
103
+ }
104
+
105
+ async delete(id: string) {
106
+ return this.remove(id);
107
+ }
108
+
109
+ async purge(id: string) {
110
+ return this.remove(id);
111
+ }
112
+
113
+ async remove(_id: string): Promise<void> {
114
+ throw new NotImplementedError();
115
+ }
116
+ }
117
+
118
+ /**
119
+ * General implementation of {@link OcflStorage} that can uses different datastore backends.
120
+ * This class provides common functionalities for the subclasses and
121
+ * at the same provide encapsulation emulating private and protected methods.
122
+ */
123
+ class OcflStorageImpl extends OcflStorage {
124
+ #root: string;
125
+ #workspace: string;
126
+ #layout: OcflStorageLayout | undefined;
127
+ #store: OcflStore;
128
+ #objectConfig: Omit<OcflObjectConfig, 'root' | 'workspace' | 'id' | 'ocflVersion'>;
129
+
130
+ constructor(config: OcflStorageConfig, store: OcflStore) {
131
+ super();
132
+ if (!store) {
133
+ throw new TypeError('[OcflStorage] store is required.');
134
+ }
135
+ if (!config.root) {
136
+ throw new TypeError('[OcflStorage] config.root is required.');
137
+ }
138
+ this.#store = store;
139
+ const { root, workspace, layout, ocflVersion, ...objectConfig } = config;
140
+ this.#root = path.resolve(root);
141
+ this.#workspace = workspace ? path.resolve(workspace) : path.join(this.#root, 'extensions/workspace');
142
+ if (this.#workspace?.startsWith(this.#root + path.sep) && !this.#workspace.startsWith(`${this.#root + path.sep}extensions${path.sep}`)) {
143
+ throw new Error('[OcflStorage] config.workspace cannot be the same as or a subpath of config.root');
144
+ }
145
+ if (layout) {
146
+ this.#layout = layout instanceof OcflStorageLayout ? layout : this.#createLayout(layout);
147
+ }
148
+ if (ocflVersion) {
149
+ this.ocflVersion = ocflVersion;
150
+ }
151
+ this.#objectConfig = objectConfig;
152
+ }
153
+
154
+ #createLayout(option: OcflExtensionConfig | string | undefined) {
155
+ const name = typeof option === 'string' ? option : option?.extensionName;
156
+ const config = typeof option === 'object' ? option : undefined;
157
+ if (name) {
158
+ return createByName<OcflStorageLayout>(OcflStorageLayout, 'Layout', name, config);
159
+ }
160
+ // else if (useDefault) {
161
+ // return DEFAULT_LAYOUT.create(config);
162
+ // }
163
+ }
164
+
165
+ get root() {
166
+ return this.#root;
167
+ }
168
+
169
+ objectRoot(id: string) {
170
+ if (!this.#layout) {
171
+ throw new Error('[OcflStorage] No layout defined');
172
+ }
173
+ return this.#layout.map(id);
174
+ }
175
+
176
+ /**
177
+ * The root param is a relative path to the storage root.
178
+ */
179
+ object(opt: string | { id?: string | undefined; root?: string | undefined }) {
180
+ let id: string | undefined;
181
+ let root: string | undefined;
182
+ if (typeof opt === 'string') {
183
+ id = opt;
184
+ } else {
185
+ ({ id, root } = opt);
186
+ }
187
+ const relObjectRoot = root || (id && this.objectRoot(id));
188
+ if (!relObjectRoot) {
189
+ throw new TypeError('[OcflStorage] object id or root is required.');
190
+ }
191
+ return new OcflObject(
192
+ {
193
+ id,
194
+ root: path.join(this.root, relObjectRoot),
195
+ // @todo:use extension workspace if not defined
196
+ workspace: this.#workspace ? path.join(this.#workspace, relObjectRoot) : undefined,
197
+ ocflVersion: this.ocflVersion,
198
+ ...this.#objectConfig,
199
+ },
200
+ this.#store,
201
+ );
202
+ }
203
+
204
+ async exists() {
205
+ return !(await isDirEmpty(this.#store, this.root));
206
+ }
207
+
208
+ async load() {
209
+ const prefix = NAMASTE_PREFIX_STORAGE;
210
+ const [ocflVersion, layout] = (
211
+ await Promise.allSettled([findNamasteVersion(this.#store, prefix, this.root), this.#store.readFile(path.join(this.root, OCFL_LAYOUT), 'utf8')])
212
+ ).map((r) => (r.status === 'fulfilled' ? r.value : undefined));
213
+ if (ocflVersion === '') {
214
+ throw createError('E080', this.root);
215
+ }
216
+ if (!ocflVersion) {
217
+ throw createError('E069', this.root);
218
+ }
219
+ let layoutName: string | undefined;
220
+ let layoutConfig: OcflExtensionConfig | undefined;
221
+ // load layout
222
+ try {
223
+ if (layout) {
224
+ layoutName = JSON.parse(layout).extension;
225
+ }
226
+ } catch (_error) {}
227
+ // load ext config if exists
228
+ if (layoutName) {
229
+ try {
230
+ const cfgstr = await this.#store.readFile(path.join(this.root, EXTENSIONS_DIR, layoutName, EXTENSION_CONFIG), 'utf8');
231
+ layoutConfig = JSON.parse(cfgstr as string);
232
+ } catch (_error) {}
233
+ }
234
+ this.#layout = this.#createLayout(layoutConfig || layoutName);
235
+ const layoutDigestAlgorithm = this.#layout?.get('digestAlgorithm');
236
+ if (layoutDigestAlgorithm) {
237
+ await OcflDigest.initSync(layoutDigestAlgorithm);
238
+ }
239
+ //@todo: load storage level extensions
240
+
241
+ return this;
242
+ }
243
+
244
+ /**
245
+ * @param options - Additional options for creating storage root
246
+ */
247
+ async create(options?: { ocflSpecText?: boolean | undefined }) {
248
+ if (await this.exists()) {
249
+ //cannot create, storage root already exists .
250
+ throw new Error('[OcflStorage] Cannot create storage root in a non-empty directory');
251
+ } else {
252
+ await this.#store.mkdir(this.root);
253
+ }
254
+ // create namaste
255
+ const prefix = NAMASTE_PREFIX_STORAGE;
256
+ const filenamePrefix = NAMASTE_T + prefix;
257
+ const filePath = path.join(this.root, filenamePrefix + this.ocflVersion);
258
+ await this.#store.writeFile(filePath, `${prefix + this.ocflVersion}\n`, 'utf8');
259
+
260
+ // create layout
261
+ if (!this.#layout) {
262
+ this.#layout = new DEFAULT_LAYOUT();
263
+ }
264
+ const layoutDigestAlgorithm = this.#layout.get('digestAlgorithm');
265
+ if (layoutDigestAlgorithm) {
266
+ await OcflDigest.initSync(layoutDigestAlgorithm);
267
+ }
268
+ const layout = { extension: this.#layout.name, description: this.#layout.description };
269
+ await this.#store.writeFile(path.join(this.root, OCFL_LAYOUT), JSON.stringify(layout, null, 2), 'utf8');
270
+ if (this.#layout.config) {
271
+ await this.#store.writeFile(
272
+ path.join(this.root, EXTENSIONS_DIR, this.#layout.name, EXTENSION_CONFIG),
273
+ JSON.stringify(this.#layout.config, null, 2),
274
+ 'utf8',
275
+ );
276
+ }
277
+
278
+ // todo: create other extensions
279
+ if (options?.ocflSpecText) {
280
+ }
281
+ return this;
282
+ }
283
+
284
+ /**
285
+ * Allow async iteration over all OCFL objects in this storage.
286
+ */
287
+ [Symbol.asyncIterator](): AsyncIterableIterator<OcflObject> {
288
+ const store = this.#store;
289
+ const workspace = this.#workspace;
290
+ const config = this.#objectConfig;
291
+ const root = this.root;
292
+ const stack = [this.#store.opendir(this.root)];
293
+ return {
294
+ [Symbol.asyncIterator]() {
295
+ return this;
296
+ },
297
+ async next(): Promise<IteratorResult<OcflObject>> {
298
+ for (let dirp = stack.pop(); dirp; dirp = stack.pop()) {
299
+ const dir = await dirp;
300
+ const dirent = await dir.read();
301
+ if (!dirent) {
302
+ await dir.close();
303
+ continue;
304
+ }
305
+ stack.push(dirp);
306
+ if (!dirent.isDirectory()) {
307
+ continue;
308
+ }
309
+ const basePath = path.join(dir.path, dirent.name);
310
+ let nv: OcflVersion | '' | undefined;
311
+ try {
312
+ nv = await findNamasteVersion(store, NAMASTE_PREFIX_OBJECT, basePath);
313
+ } catch (_err) {}
314
+ if (nv) {
315
+ //ocfl object exists
316
+ // inventory exists
317
+ const object = new OcflObject(
318
+ {
319
+ root: basePath,
320
+ workspace: workspace ? path.join(workspace, path.relative(root, basePath)) : undefined,
321
+ ocflVersion: nv,
322
+ ...config,
323
+ },
324
+ store,
325
+ );
326
+ //await object.load();
327
+ return { done: false, value: object };
328
+ } else {
329
+ //ocfl object does not exist, process subdir
330
+ stack.push(store.opendir(basePath));
331
+ }
332
+ }
333
+ return { done: true, value: null };
334
+ },
335
+ // return() {
336
+ // return { done: true };
337
+ // }
338
+ };
339
+ }
340
+ }
341
+
342
+ export { OcflStorage, OcflStorageImpl };
package/src/store.ts ADDED
@@ -0,0 +1,199 @@
1
+ import type { Dir, ReadStream, Stats, WriteStream } from 'node:fs';
2
+ import path from 'node:path';
3
+ import { joinTypedArray } from './utils.ts';
4
+
5
+ const emptyOptions = {};
6
+
7
+ type Encoding = BufferEncoding | { encoding: BufferEncoding };
8
+
9
+ export type WriteFileData = string | Uint8Array | DataView | ArrayBuffer | ReadableStream;
10
+
11
+ export interface ListedFile {
12
+ name: string;
13
+ path: string;
14
+ size: number;
15
+ lastModified: Date;
16
+ }
17
+
18
+ export interface ListOptions {
19
+ encoding?: BufferEncoding;
20
+ bufferSize?: number;
21
+ recursive?: boolean;
22
+ }
23
+
24
+ /**
25
+ * Abstract class to provide common APIs to access and modify the actual data store backend
26
+ * such as local file system and cloud storage
27
+ */
28
+ class OcflStore {
29
+ static instances: Map<object, OcflStore>;
30
+
31
+ static getInstance<T extends OcflStore>(this: new (options?: object) => T, options: object = emptyOptions): T {
32
+ // biome-ignore lint/complexity/noThisInStatic: reads the subclass's own instances map
33
+ const instances = (this as unknown as typeof OcflStore).instances as Map<object, T> | undefined;
34
+ if (!instances) {
35
+ return new this(options);
36
+ }
37
+ let store = instances.get(options);
38
+ if (!store) {
39
+ store = new this(options);
40
+ instances.set(options, store);
41
+ }
42
+ return store;
43
+ }
44
+
45
+ // biome-ignore lint/complexity/noUselessConstructor: documents the constructor signature subclasses receive from getInstance()
46
+ constructor(_options?: object) {}
47
+
48
+ /**
49
+ * Get file information.
50
+ */
51
+ async stat(_filePath: string): Promise<Stats> {
52
+ throw new Error('Not Implemented');
53
+ }
54
+
55
+ /**
56
+ * Check if file exists
57
+ */
58
+ async exists(filePath: string): Promise<boolean> {
59
+ try {
60
+ await this.stat(filePath);
61
+ return true;
62
+ } catch {
63
+ return false;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Create a readable stream to get the content of a file
69
+ * @deprecated Use {@link OcflStore.createReadable} instead
70
+ */
71
+ async createReadStream(_filePath: string, _options?: unknown): Promise<ReadStream> {
72
+ throw new Error('Not Implemented');
73
+ }
74
+
75
+ /**
76
+ * Create a stream that writes data to `filePath`.
77
+ * @deprecated Use {@link OcflStore.createWritable} instead
78
+ */
79
+ async createWriteStream(_filePath: string, _options?: unknown): Promise<WriteStream> {
80
+ throw new Error('Not Implemented');
81
+ }
82
+
83
+ /**
84
+ * Create a ReadableStream to get the content of a file
85
+ * @param filePath - Absolute path
86
+ */
87
+ async createReadable(_filePath: string, _options?: unknown): Promise<ReadableStream> {
88
+ throw new Error('Not Implemented');
89
+ }
90
+
91
+ /**
92
+ * Create a WritableStream that writes data to `filePath`.
93
+ */
94
+ async createWritable(_filePath: string, _options?: unknown): Promise<WritableStream> {
95
+ throw new Error('Not Implemented');
96
+ }
97
+
98
+ /**
99
+ * Provide a common interface to read a file inside an object.
100
+ * A concrete subclass MAY implement this method to provide read access to its underlying storage backend.
101
+ * The default implementation uses the {@linkcode OcflStore#createReadable} method.
102
+ */
103
+ async readFile(filePath: string, options: Encoding): Promise<string>;
104
+ async readFile(filePath: string, options?: unknown): Promise<Uint8Array>;
105
+ async readFile(filePath: string, options?: unknown): Promise<string | Uint8Array> {
106
+ const rs = await this.createReadable(filePath, options);
107
+ let buffer: string | Uint8Array =
108
+ typeof options === 'string' || (options as { encoding?: string } | undefined)?.encoding
109
+ ? ''
110
+ : new Uint8Array(new ArrayBuffer(0, { maxByteLength: 16 * 1024 }));
111
+ for await (const chunk of rs) {
112
+ if (typeof chunk === 'string') {
113
+ if (typeof buffer !== 'string') {
114
+ buffer = new TextDecoder().decode(buffer);
115
+ }
116
+ buffer += chunk;
117
+ } else {
118
+ if (typeof buffer === 'string') {
119
+ buffer = new TextEncoder().encode(buffer);
120
+ }
121
+ buffer = joinTypedArray(buffer, chunk) as Uint8Array;
122
+ }
123
+ }
124
+ return buffer;
125
+ }
126
+
127
+ /**
128
+ * Provide a common interface to write a file inside an object.
129
+ * A concrete subclass MAY implement this method to provide write access to its underlying storage backend.
130
+ * The default implementation uses the {@link OcflStore.createWritable} method.
131
+ * @param filePath - Absolute path to the file.
132
+ * @param data - The data to be written to the file.
133
+ */
134
+ async writeFile(filePath: string, data: WriteFileData, options?: object | string): Promise<void> {
135
+ await this.mkdir(path.dirname(filePath));
136
+ const target = await this.createWritable(filePath, options);
137
+ if (data instanceof ReadableStream) {
138
+ await data.pipeTo(target);
139
+ } else {
140
+ const w = target.getWriter();
141
+ await w.write(data);
142
+ await w.close();
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Copy file from `source` to `target`.
148
+ * By default, target is overwritten if it already exists.
149
+ */
150
+ async copyFile(source: string, target: string): Promise<void> {
151
+ await this.writeFile(target, await this.createReadable(source));
152
+ }
153
+
154
+ /**
155
+ * Open a directory
156
+ */
157
+ async opendir(_filePath: string, _options?: unknown): Promise<Dir> {
158
+ throw new Error('Not Implemented');
159
+ }
160
+
161
+ /**
162
+ * Read a directory
163
+ */
164
+ async readdir(_filePath: string, _options?: unknown): Promise<string[]> {
165
+ throw new Error('Not Implemented');
166
+ }
167
+
168
+ /**
169
+ * List all files contained in a directory including the metadata such as size and last modified date.
170
+ * The returned file.path is a relative path to the dirPath
171
+ */
172
+ async list(_dirPath: string, _options?: ListOptions): Promise<AsyncIterableIterator<ListedFile>> {
173
+ throw new Error('Not Implemented');
174
+ }
175
+
176
+ /**
177
+ * Recursively create a directory if the storage backend supports it, otherwise do nothing
178
+ * @returns The first directory path created
179
+ */
180
+ async mkdir(_filePath: string, _options?: unknown): Promise<string | undefined> {
181
+ throw new Error('Not Implemented');
182
+ }
183
+
184
+ /**
185
+ * Rename or move a file or directory
186
+ */
187
+ async move(_source: string, _target: string): Promise<void> {
188
+ throw new Error('Not Implemented');
189
+ }
190
+
191
+ /**
192
+ * Remove a file or directory recursively
193
+ */
194
+ async remove(_filePath: string): Promise<void> {
195
+ throw new Error('Not Implemented');
196
+ }
197
+ }
198
+
199
+ export { OcflStore };