@travetto/model 2.1.0 → 2.1.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 +6 -6
- package/src/provider/file.ts +16 -15
- package/test-support/stream.ts +5 -4
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/model",
|
|
3
3
|
"displayName": "Data Modeling Support",
|
|
4
|
-
"version": "2.1.
|
|
4
|
+
"version": "2.1.1",
|
|
5
5
|
"description": "Datastore abstraction for core operations.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"datastore",
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
"directory": "module/model"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@travetto/di": "^2.1.
|
|
32
|
-
"@travetto/config": "^2.1.
|
|
33
|
-
"@travetto/registry": "^2.1.
|
|
34
|
-
"@travetto/schema": "^2.1.
|
|
31
|
+
"@travetto/di": "^2.1.1",
|
|
32
|
+
"@travetto/config": "^2.1.1",
|
|
33
|
+
"@travetto/registry": "^2.1.1",
|
|
34
|
+
"@travetto/schema": "^2.1.1"
|
|
35
35
|
},
|
|
36
36
|
"optionalPeerDependencies": {
|
|
37
|
-
"@travetto/cli": "^2.1.
|
|
37
|
+
"@travetto/cli": "^2.1.1"
|
|
38
38
|
},
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|
package/src/provider/file.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
1
|
+
import * as fs from 'fs/promises';
|
|
2
|
+
import { createReadStream } from 'fs';
|
|
2
3
|
import * as os from 'os';
|
|
3
4
|
import * as path from 'path';
|
|
4
5
|
|
|
@@ -47,8 +48,8 @@ export class FileModelConfig {
|
|
|
47
48
|
export class FileModelService implements ModelCrudSupport, ModelStreamSupport, ModelExpirySupport, ModelStorageSupport {
|
|
48
49
|
|
|
49
50
|
private static async * scanFolder(folder: string, suffix: string) {
|
|
50
|
-
for (const sub of await fs.
|
|
51
|
-
for (const file of await fs.
|
|
51
|
+
for (const sub of await fs.readdir(folder)) {
|
|
52
|
+
for (const file of await fs.readdir(PathUtil.resolveUnix(folder, sub))) {
|
|
52
53
|
if (file.endsWith(suffix)) {
|
|
53
54
|
yield [file.replace(suffix, ''), PathUtil.resolveUnix(folder, sub, file)] as [id: string, file: string];
|
|
54
55
|
}
|
|
@@ -79,7 +80,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
79
80
|
dir = path.dirname(resolved);
|
|
80
81
|
}
|
|
81
82
|
if (!await FsUtil.exists(dir)) {
|
|
82
|
-
await fs.
|
|
83
|
+
await fs.mkdir(dir, { recursive: true });
|
|
83
84
|
}
|
|
84
85
|
return resolved;
|
|
85
86
|
}
|
|
@@ -114,7 +115,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
114
115
|
const file = await this.#resolveName(cls, '.json', id);
|
|
115
116
|
|
|
116
117
|
if (await FsUtil.exists(file)) {
|
|
117
|
-
const content = await StreamUtil.streamToBuffer(
|
|
118
|
+
const content = await StreamUtil.streamToBuffer(createReadStream(file));
|
|
118
119
|
return this.checkExpiry(cls, await ModelCrudUtil.load(cls, content));
|
|
119
120
|
}
|
|
120
121
|
|
|
@@ -129,7 +130,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
129
130
|
const file = await this.#resolveName(cls, '.json', item.id);
|
|
130
131
|
|
|
131
132
|
if (await FsUtil.exists(file)) {
|
|
132
|
-
throw new ExistsError(cls, item.id);
|
|
133
|
+
throw new ExistsError(cls, item.id!);
|
|
133
134
|
}
|
|
134
135
|
|
|
135
136
|
return await this.upsert(cls, item);
|
|
@@ -145,7 +146,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
145
146
|
const prepped = await ModelCrudUtil.preStore(cls, item, this);
|
|
146
147
|
|
|
147
148
|
const file = await this.#resolveName(cls, '.json', item.id);
|
|
148
|
-
await fs.
|
|
149
|
+
await fs.writeFile(file, JSON.stringify(item), { encoding: 'utf8' });
|
|
149
150
|
|
|
150
151
|
return prepped;
|
|
151
152
|
}
|
|
@@ -155,14 +156,14 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
155
156
|
const id = item.id;
|
|
156
157
|
item = await ModelCrudUtil.naivePartialUpdate(cls, item, view, () => this.get(cls, id));
|
|
157
158
|
const file = await this.#resolveName(cls, '.json', item.id);
|
|
158
|
-
await fs.
|
|
159
|
+
await fs.writeFile(file, JSON.stringify(item), { encoding: 'utf8' });
|
|
159
160
|
|
|
160
161
|
return item as T;
|
|
161
162
|
}
|
|
162
163
|
|
|
163
164
|
async delete<T extends ModelType>(cls: Class<T>, id: string) {
|
|
164
165
|
const file = await this.#find(cls, '.json', id);
|
|
165
|
-
await fs.
|
|
166
|
+
await fs.unlink(file);
|
|
166
167
|
}
|
|
167
168
|
|
|
168
169
|
async * list<T extends ModelType>(cls: Class<T>) {
|
|
@@ -182,18 +183,18 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
182
183
|
const file = await this.#resolveName(STREAMS, BIN, location);
|
|
183
184
|
await Promise.all([
|
|
184
185
|
StreamUtil.writeToFile(input, file),
|
|
185
|
-
fs.
|
|
186
|
+
fs.writeFile(file.replace(BIN, META), JSON.stringify(meta), 'utf8')
|
|
186
187
|
]);
|
|
187
188
|
}
|
|
188
189
|
|
|
189
190
|
async getStream(location: string) {
|
|
190
191
|
const file = await this.#find(STREAMS, BIN, location);
|
|
191
|
-
return
|
|
192
|
+
return createReadStream(file);
|
|
192
193
|
}
|
|
193
194
|
|
|
194
195
|
async describeStream(location: string) {
|
|
195
196
|
const file = await this.#find(STREAMS, META, location);
|
|
196
|
-
const content = await StreamUtil.streamToBuffer(
|
|
197
|
+
const content = await StreamUtil.streamToBuffer(createReadStream(file));
|
|
197
198
|
const text = JSON.parse(content.toString('utf8'));
|
|
198
199
|
return text as StreamMeta;
|
|
199
200
|
}
|
|
@@ -202,8 +203,8 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
202
203
|
const file = await this.#resolveName(STREAMS, BIN, location);
|
|
203
204
|
if (await FsUtil.exists(file)) {
|
|
204
205
|
await Promise.all([
|
|
205
|
-
fs.
|
|
206
|
-
fs.
|
|
206
|
+
fs.unlink(file),
|
|
207
|
+
fs.unlink(file.replace('.bin', META))
|
|
207
208
|
]);
|
|
208
209
|
} else {
|
|
209
210
|
throw new NotFoundError('Stream', location);
|
|
@@ -224,7 +225,7 @@ export class FileModelService implements ModelCrudSupport, ModelStreamSupport, M
|
|
|
224
225
|
// Storage mgmt
|
|
225
226
|
async createStorage() {
|
|
226
227
|
const dir = PathUtil.resolveUnix(this.config.folder, this.config.namespace);
|
|
227
|
-
await fs.
|
|
228
|
+
await fs.mkdir(dir, { recursive: true });
|
|
228
229
|
}
|
|
229
230
|
|
|
230
231
|
async deleteStorage() {
|
package/test-support/stream.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as assert from 'assert';
|
|
2
|
-
import * as fs from 'fs';
|
|
2
|
+
import * as fs from 'fs/promises';
|
|
3
|
+
import { createReadStream } from 'fs';
|
|
3
4
|
import * as crypto from 'crypto';
|
|
4
5
|
|
|
5
6
|
import { PathUtil } from '@travetto/boot';
|
|
@@ -25,12 +26,12 @@ export abstract class ModelStreamSuite extends BaseModelSuite<ModelStreamSupport
|
|
|
25
26
|
|
|
26
27
|
async getStream(resource: string) {
|
|
27
28
|
const file = await ResourceManager.findAbsolute(resource);
|
|
28
|
-
const stat = await fs.
|
|
29
|
-
const hash = await this.getHash(
|
|
29
|
+
const stat = await fs.stat(file);
|
|
30
|
+
const hash = await this.getHash(createReadStream(file));
|
|
30
31
|
|
|
31
32
|
return [
|
|
32
33
|
{ size: stat.size, contentType: '', hash, filename: resource },
|
|
33
|
-
|
|
34
|
+
createReadStream(file)
|
|
34
35
|
] as const;
|
|
35
36
|
}
|
|
36
37
|
|