@travetto/model 5.0.0-rc.9 → 5.0.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.
@@ -1,113 +0,0 @@
1
- import fs from 'node:fs/promises';
2
- import assert from 'node:assert';
3
- import crypto from 'node:crypto';
4
- import { Readable } from 'node:stream';
5
- import { pipeline } from 'node:stream/promises';
6
- import { text as toText } from 'node:stream/consumers';
7
-
8
- import { Suite, Test, TestFixtures } from '@travetto/test';
9
-
10
- import { BaseModelSuite } from './base';
11
- import { ModelStreamSupport } from '../../src/service/stream';
12
- import { enforceRange } from '../../src/internal/service/stream';
13
-
14
- @Suite()
15
- export abstract class ModelStreamSuite extends BaseModelSuite<ModelStreamSupport> {
16
-
17
- fixture = new TestFixtures(['@travetto/model']);
18
-
19
- async getHash(stream: Readable): Promise<string> {
20
- const hash = crypto.createHash('sha1').setEncoding('hex');
21
- await pipeline(stream, hash);
22
- return hash.read().toString();
23
- }
24
-
25
- async getStream(resource: string): Promise<readonly [{ size: number, contentType: string, hash: string, filename: string }, Readable]> {
26
- const file = await this.fixture.resolve(resource);
27
- const { size } = await fs.stat(file);
28
- const hash = await this.getHash(await this.fixture.readStream(resource));
29
-
30
- return [
31
- { size, contentType: '', hash, filename: resource },
32
- await this.fixture.readStream(resource)
33
- ] as const;
34
- }
35
-
36
- @Test()
37
- async writeBasic(): Promise<void> {
38
- const service = await this.service;
39
- const [meta, stream] = await this.getStream('/asset.yml');
40
-
41
- await service.upsertStream(meta.hash, stream, meta);
42
-
43
- const retrieved = await service.describeStream(meta.hash);
44
- assert(meta === retrieved);
45
- }
46
-
47
- @Test()
48
- async writeStream(): Promise<void> {
49
- const service = await this.service;
50
- const [meta, stream] = await this.getStream('/asset.yml');
51
-
52
- await service.upsertStream(meta.hash, stream, meta);
53
-
54
- const retrieved = await service.getStream(meta.hash);
55
- assert(await this.getHash(retrieved) === meta.hash);
56
- }
57
-
58
- @Test()
59
- async writeAndDelete(): Promise<void> {
60
- const service = await this.service;
61
- const [meta, stream] = await this.getStream('/asset.yml');
62
-
63
- await service.upsertStream(meta.hash, stream, meta);
64
-
65
- await service.deleteStream(meta.hash);
66
-
67
- await assert.rejects(async () => {
68
- await service.getStream(meta.hash);
69
- });
70
- }
71
-
72
- @Test()
73
- async partialStream(): Promise<void> {
74
- const service = await this.service;
75
- const [meta, stream] = await this.getStream('/text.txt');
76
-
77
- await service.upsertStream(meta.hash, stream, meta);
78
-
79
- const retrieved = await service.getStream(meta.hash);
80
- const content = await toText(retrieved);
81
- assert(content.startsWith('abc'));
82
- assert(content.endsWith('xyz'));
83
-
84
- const partial = await service.getStream(meta.hash, { start: 10, end: 20 });
85
- const subContent = await toText(partial);
86
- const range = await enforceRange({ start: 10, end: 20 }, meta.size);
87
- assert(subContent.length === (range.end - range.start) + 1);
88
-
89
- const og = await this.fixture.read('/text.txt');
90
-
91
- assert(subContent === og.substring(10, 21));
92
-
93
- const partialUnbounded = await service.getStream(meta.hash, { start: 10 });
94
- const subContent2 = await toText(partialUnbounded);
95
- const range2 = await enforceRange({ start: 10 }, meta.size);
96
- assert(subContent2.length === (range2.end - range2.start) + 1);
97
- assert(subContent2.startsWith('klm'));
98
- assert(subContent2.endsWith('xyz'));
99
-
100
- const partialSingle = await service.getStream(meta.hash, { start: 10, end: 10 });
101
- const subContent3 = await toText(partialSingle);
102
- assert(subContent3.length === 1);
103
- assert(subContent3 === 'k');
104
-
105
- const partialOverBounded = await service.getStream(meta.hash, { start: 20, end: 40 });
106
- const subContent4 = await toText(partialOverBounded);
107
- assert(subContent4.length === 6);
108
- assert(subContent4.endsWith('xyz'));
109
-
110
- await assert.rejects(() => service.getStream(meta.hash, { start: -10, end: 10 }));
111
- await assert.rejects(() => service.getStream(meta.hash, { start: 30, end: 37 }));
112
- }
113
- }