@travetto/model-file 7.1.4 → 8.0.0-alpha.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.
- package/package.json +7 -7
- package/src/service.ts +26 -22
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model-file",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.1",
|
|
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": "^
|
|
30
|
-
"@travetto/di": "^
|
|
31
|
-
"@travetto/model": "^
|
|
32
|
-
"@travetto/schema": "^
|
|
29
|
+
"@travetto/config": "^8.0.0-alpha.1",
|
|
30
|
+
"@travetto/di": "^8.0.0-alpha.1",
|
|
31
|
+
"@travetto/model": "^8.0.0-alpha.1",
|
|
32
|
+
"@travetto/schema": "^8.0.0-alpha.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"@travetto/cli": "^
|
|
36
|
-
"@travetto/test": "^
|
|
35
|
+
"@travetto/cli": "^8.0.0-alpha.1",
|
|
36
|
+
"@travetto/test": "^8.0.0-alpha.1"
|
|
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 {
|
|
8
|
-
|
|
6
|
+
import {
|
|
7
|
+
type Class, type TimeSpan, Runtime, type BinaryMetadata, type ByteRange, type BinaryType,
|
|
8
|
+
BinaryUtil, JSONUtil, BinaryMetadataUtil
|
|
9
|
+
} from '@travetto/runtime';
|
|
10
|
+
import { Injectable, PostConstruct } 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';
|
|
@@ -29,7 +30,8 @@ export class FileModelConfig {
|
|
|
29
30
|
modifyStorage?: boolean;
|
|
30
31
|
cullRate?: number | TimeSpan;
|
|
31
32
|
|
|
32
|
-
|
|
33
|
+
@PostConstruct()
|
|
34
|
+
async finalizeConfig(): Promise<void> {
|
|
33
35
|
this.folder ??= path.resolve(os.tmpdir(), `trv_file_${Runtime.main.name.replace(/[^a-z]/ig, '_')}`);
|
|
34
36
|
}
|
|
35
37
|
}
|
|
@@ -89,7 +91,8 @@ export class FileModelService implements ModelCrudSupport, ModelBlobSupport, Mod
|
|
|
89
91
|
return file;
|
|
90
92
|
}
|
|
91
93
|
|
|
92
|
-
|
|
94
|
+
@PostConstruct()
|
|
95
|
+
initializeClient(): void {
|
|
93
96
|
ModelExpiryUtil.registerCull(this);
|
|
94
97
|
}
|
|
95
98
|
|
|
@@ -138,7 +141,7 @@ export class FileModelService implements ModelCrudSupport, ModelBlobSupport, Mod
|
|
|
138
141
|
const prepped = await ModelCrudUtil.preStore(cls, item, this);
|
|
139
142
|
|
|
140
143
|
const file = await this.#resolveName(cls, '.json', item.id);
|
|
141
|
-
await fs.writeFile(file,
|
|
144
|
+
await fs.writeFile(file, BinaryUtil.binaryArrayToUint8Array(JSONUtil.toBinaryArray(item)));
|
|
142
145
|
|
|
143
146
|
return prepped;
|
|
144
147
|
}
|
|
@@ -148,7 +151,7 @@ export class FileModelService implements ModelCrudSupport, ModelBlobSupport, Mod
|
|
|
148
151
|
const id = item.id;
|
|
149
152
|
const full = await ModelCrudUtil.naivePartialUpdate(cls, () => this.get(cls, id), item, view);
|
|
150
153
|
const file = await this.#resolveName(cls, '.json', full.id);
|
|
151
|
-
await fs.writeFile(file,
|
|
154
|
+
await fs.writeFile(file, BinaryUtil.binaryArrayToUint8Array(JSONUtil.toBinaryArray(full)));
|
|
152
155
|
return full;
|
|
153
156
|
}
|
|
154
157
|
|
|
@@ -170,30 +173,31 @@ export class FileModelService implements ModelCrudSupport, ModelBlobSupport, Mod
|
|
|
170
173
|
}
|
|
171
174
|
|
|
172
175
|
// Blob
|
|
173
|
-
async upsertBlob(location: string, input:
|
|
174
|
-
if (!overwrite && await this.
|
|
176
|
+
async upsertBlob(location: string, input: BinaryType, metadata?: BinaryMetadata, overwrite = true): Promise<void> {
|
|
177
|
+
if (!overwrite && await this.getBlobMetadata(location).then(() => true, () => false)) {
|
|
175
178
|
return;
|
|
176
179
|
}
|
|
177
|
-
|
|
178
|
-
const [stream, blobMeta] = await ModelBlobUtil.getInput(input, meta);
|
|
180
|
+
const resolved = await BinaryMetadataUtil.compute(input, metadata);
|
|
179
181
|
const file = await this.#resolveName(ModelBlobNamespace, BIN, location);
|
|
180
182
|
await Promise.all([
|
|
181
|
-
|
|
182
|
-
|
|
183
|
+
BinaryUtil.pipeline(input, createWriteStream(file)),
|
|
184
|
+
BinaryUtil.pipeline(
|
|
185
|
+
JSONUtil.toBinaryArray(resolved),
|
|
186
|
+
createWriteStream(file.replace(BIN, META)))
|
|
183
187
|
]);
|
|
184
188
|
}
|
|
185
189
|
|
|
186
190
|
async getBlob(location: string, range?: ByteRange): Promise<Blob> {
|
|
187
191
|
const file = await this.#find(ModelBlobNamespace, BIN, location);
|
|
188
|
-
const
|
|
189
|
-
const final = range ?
|
|
190
|
-
return
|
|
192
|
+
const metadata = await this.getBlobMetadata(location);
|
|
193
|
+
const final = range ? BinaryMetadataUtil.enforceRange(range, metadata) : undefined;
|
|
194
|
+
return BinaryMetadataUtil.makeBlob(() => createReadStream(file, final), { ...metadata, range: final });
|
|
191
195
|
}
|
|
192
196
|
|
|
193
|
-
async
|
|
197
|
+
async getBlobMetadata(location: string): Promise<BinaryMetadata> {
|
|
194
198
|
const file = await this.#find(ModelBlobNamespace, META, location);
|
|
195
199
|
const content = await fs.readFile(file);
|
|
196
|
-
const text:
|
|
200
|
+
const text: BinaryMetadata = JSONUtil.fromBinaryArray(content);
|
|
197
201
|
return text;
|
|
198
202
|
}
|
|
199
203
|
|
|
@@ -209,9 +213,9 @@ export class FileModelService implements ModelCrudSupport, ModelBlobSupport, Mod
|
|
|
209
213
|
}
|
|
210
214
|
}
|
|
211
215
|
|
|
212
|
-
async
|
|
216
|
+
async updateBlobMetadata(location: string, metadata: BinaryMetadata): Promise<void> {
|
|
213
217
|
const file = await this.#find(ModelBlobNamespace, META, location);
|
|
214
|
-
await
|
|
218
|
+
await BinaryUtil.pipeline(JSONUtil.toBinaryArray(metadata), createWriteStream(file));
|
|
215
219
|
}
|
|
216
220
|
|
|
217
221
|
// Expiry
|