@travetto/model-file 7.1.3 → 8.0.0-alpha.0

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 (2) hide show
  1. package/package.json +7 -7
  2. package/src/service.ts +21 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/model-file",
3
- "version": "7.1.3",
3
+ "version": "8.0.0-alpha.0",
4
4
  "type": "module",
5
5
  "description": "File system backing for the travetto model module.",
6
6
  "keywords": [
@@ -26,14 +26,14 @@
26
26
  "directory": "module/model-file"
27
27
  },
28
28
  "dependencies": {
29
- "@travetto/config": "^7.1.3",
30
- "@travetto/di": "^7.1.3",
31
- "@travetto/model": "^7.1.3",
32
- "@travetto/schema": "^7.1.3"
29
+ "@travetto/config": "^8.0.0-alpha.0",
30
+ "@travetto/di": "^8.0.0-alpha.0",
31
+ "@travetto/model": "^8.0.0-alpha.0",
32
+ "@travetto/schema": "^8.0.0-alpha.0"
33
33
  },
34
34
  "peerDependencies": {
35
- "@travetto/cli": "^7.1.3",
36
- "@travetto/test": "^7.1.3"
35
+ "@travetto/cli": "^8.0.0-alpha.0",
36
+ "@travetto/test": "^8.0.0-alpha.0"
37
37
  },
38
38
  "peerDependenciesMeta": {
39
39
  "@travetto/cli": {
package/src/service.ts CHANGED
@@ -1,17 +1,18 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import { createReadStream, createWriteStream } from 'node:fs';
3
3
  import os from 'node:os';
4
- import { pipeline } from 'node:stream/promises';
5
4
  import path from 'node:path';
6
5
 
7
- import { type Class, type TimeSpan, Runtime, type BlobMeta, type ByteRange, type BinaryInput, BinaryUtil, JSONUtil } from '@travetto/runtime';
6
+ import {
7
+ type Class, type TimeSpan, Runtime, type BinaryMetadata, type ByteRange, type BinaryType,
8
+ BinaryUtil, JSONUtil, BinaryMetadataUtil
9
+ } from '@travetto/runtime';
8
10
  import { Injectable } from '@travetto/di';
9
11
  import { Config } from '@travetto/config';
10
12
  import { Required } from '@travetto/schema';
11
13
  import {
12
14
  type ModelCrudSupport, type ModelExpirySupport, type ModelStorageSupport, type ModelType, ModelRegistryIndex,
13
- NotFoundError, type OptionalId, ExistsError, type ModelBlobSupport,
14
- ModelCrudUtil, ModelExpiryUtil, ModelBlobUtil
15
+ NotFoundError, type OptionalId, ExistsError, type ModelBlobSupport, ModelCrudUtil, ModelExpiryUtil
15
16
  } from '@travetto/model';
16
17
 
17
18
  type Suffix = '.bin' | '.meta' | '.json' | '.expires';
@@ -138,7 +139,7 @@ export class FileModelService implements ModelCrudSupport, ModelBlobSupport, Mod
138
139
  const prepped = await ModelCrudUtil.preStore(cls, item, this);
139
140
 
140
141
  const file = await this.#resolveName(cls, '.json', item.id);
141
- await fs.writeFile(file, JSON.stringify(item), { encoding: 'utf8' });
142
+ await fs.writeFile(file, BinaryUtil.binaryArrayToUint8Array(JSONUtil.toBinaryArray(item)));
142
143
 
143
144
  return prepped;
144
145
  }
@@ -148,7 +149,7 @@ export class FileModelService implements ModelCrudSupport, ModelBlobSupport, Mod
148
149
  const id = item.id;
149
150
  const full = await ModelCrudUtil.naivePartialUpdate(cls, () => this.get(cls, id), item, view);
150
151
  const file = await this.#resolveName(cls, '.json', full.id);
151
- await fs.writeFile(file, JSON.stringify(full), { encoding: 'utf8' });
152
+ await fs.writeFile(file, BinaryUtil.binaryArrayToUint8Array(JSONUtil.toBinaryArray(full)));
152
153
  return full;
153
154
  }
154
155
 
@@ -170,30 +171,31 @@ export class FileModelService implements ModelCrudSupport, ModelBlobSupport, Mod
170
171
  }
171
172
 
172
173
  // Blob
173
- async upsertBlob(location: string, input: BinaryInput, meta?: BlobMeta, overwrite = true): Promise<void> {
174
- if (!overwrite && await this.getBlobMeta(location).then(() => true, () => false)) {
174
+ async upsertBlob(location: string, input: BinaryType, metadata?: BinaryMetadata, overwrite = true): Promise<void> {
175
+ if (!overwrite && await this.getBlobMetadata(location).then(() => true, () => false)) {
175
176
  return;
176
177
  }
177
-
178
- const [stream, blobMeta] = await ModelBlobUtil.getInput(input, meta);
178
+ const resolved = await BinaryMetadataUtil.compute(input, metadata);
179
179
  const file = await this.#resolveName(ModelBlobNamespace, BIN, location);
180
180
  await Promise.all([
181
- await pipeline(stream, createWriteStream(file)),
182
- fs.writeFile(file.replace(BIN, META), JSON.stringify(blobMeta), 'utf8')
181
+ BinaryUtil.pipeline(input, createWriteStream(file)),
182
+ BinaryUtil.pipeline(
183
+ JSONUtil.toBinaryArray(resolved),
184
+ createWriteStream(file.replace(BIN, META)))
183
185
  ]);
184
186
  }
185
187
 
186
188
  async getBlob(location: string, range?: ByteRange): Promise<Blob> {
187
189
  const file = await this.#find(ModelBlobNamespace, BIN, location);
188
- const meta = await this.getBlobMeta(location);
189
- const final = range ? ModelBlobUtil.enforceRange(range, meta.size!) : undefined;
190
- return BinaryUtil.readableBlob(() => createReadStream(file, { ...range }), { ...meta, range: final });
190
+ const metadata = await this.getBlobMetadata(location);
191
+ const final = range ? BinaryMetadataUtil.enforceRange(range, metadata) : undefined;
192
+ return BinaryMetadataUtil.makeBlob(() => createReadStream(file, final), { ...metadata, range: final });
191
193
  }
192
194
 
193
- async getBlobMeta(location: string): Promise<BlobMeta> {
195
+ async getBlobMetadata(location: string): Promise<BinaryMetadata> {
194
196
  const file = await this.#find(ModelBlobNamespace, META, location);
195
197
  const content = await fs.readFile(file);
196
- const text: BlobMeta = JSONUtil.parseSafe(content.toString('utf8'));
198
+ const text: BinaryMetadata = JSONUtil.fromBinaryArray(content);
197
199
  return text;
198
200
  }
199
201
 
@@ -209,9 +211,9 @@ export class FileModelService implements ModelCrudSupport, ModelBlobSupport, Mod
209
211
  }
210
212
  }
211
213
 
212
- async updateBlobMeta(location: string, meta: BlobMeta): Promise<void> {
214
+ async updateBlobMetadata(location: string, metadata: BinaryMetadata): Promise<void> {
213
215
  const file = await this.#find(ModelBlobNamespace, META, location);
214
- await fs.writeFile(file, JSON.stringify(meta));
216
+ await BinaryUtil.pipeline(JSONUtil.toBinaryArray(metadata), createWriteStream(file));
215
217
  }
216
218
 
217
219
  // Expiry