@travetto/model 5.0.0-rc.8 → 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,110 +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 { buffer as toBuffer } 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 hasher = crypto.createHash('sha1').setEncoding('hex');
21
- await pipeline(stream, hasher);
22
- return hasher.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 toBuffer(retrieved)).toString('utf8');
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 toBuffer(partial)).toString('utf8');
86
- const range = await enforceRange({ start: 10, end: 20 }, meta.size);
87
- assert(subContent.length === (range.end - range.start) + 1);
88
- assert(subContent === 'klmnopqrstu');
89
-
90
- const partialUnbounded = await service.getStream(meta.hash, { start: 10 });
91
- const subContent2 = (await toBuffer(partialUnbounded)).toString('utf8');
92
- const range2 = await enforceRange({ start: 10 }, meta.size);
93
- assert(subContent2.length === (range2.end - range2.start) + 1);
94
- assert(subContent2.startsWith('klm'));
95
- assert(subContent2.endsWith('xyz'));
96
-
97
- const partialSingle = await service.getStream(meta.hash, { start: 10, end: 10 });
98
- const subContent3 = (await toBuffer(partialSingle)).toString('utf8');
99
- assert(subContent3.length === 1);
100
- assert(subContent3 === 'k');
101
-
102
- const partialOverbounded = await service.getStream(meta.hash, { start: 20, end: 40 });
103
- const subContent4 = (await toBuffer(partialOverbounded)).toString('utf8');
104
- assert(subContent4.length === 6);
105
- assert(subContent4.endsWith('xyz'));
106
-
107
- await assert.rejects(() => service.getStream(meta.hash, { start: -10, end: 10 }));
108
- await assert.rejects(() => service.getStream(meta.hash, { start: 30, end: 37 }));
109
- }
110
- }