@@ -2,66 +2,39 @@ import fs from 'fs';
2
2
import nock from 'nock' ;
3
3
import os from 'os' ;
4
4
import path from 'path' ;
5
- import { KeyPair } from './key' ;
6
- import {
7
- createRootMeta ,
8
- createSnapshotMeta ,
9
- createTargetsMeta ,
10
- createTimestampMeta ,
11
- } from './metadata' ;
12
- import { Target , collectTargets } from './target' ;
5
+ import { TUFHandlerOptions , tufHandlers } from './handler' ;
6
+ import { mock } from './mock' ;
7
+ import { initializeTUFRepo } from './repo' ;
8
+ import { Target } from './shared.types' ;
13
9
14
- export type { Target } from './target' ;
10
+ export { TUFHandlerOptions , tufHandlers } from './handler' ;
11
+ export { TUFRepo , initializeTUFRepo } from './repo' ;
12
+ export type { Target } from './shared.types' ;
15
13
16
- interface MockRepoOptions {
14
+ type MockRepoOptions = {
17
15
baseURL ?: string ;
18
- metadataPathPrefix ?: string ;
19
- targetPathPrefix ?: string ;
20
16
cachePath ?: string ;
21
- responseCount ?: number ;
22
- }
17
+ } & TUFHandlerOptions ;
23
18
24
19
export function mockRepo (
25
20
baseURL : string ,
26
21
targets : Target [ ] ,
27
- options : Omit < MockRepoOptions , 'baseURL' | 'cachePath' > = { }
22
+ options : TUFHandlerOptions = { }
28
23
) : string {
29
- const metadataPrefix = options . metadataPathPrefix ?? '/metadata' ;
30
- const targetPrefix = options . targetPathPrefix ?? '/targets' ;
31
- const count = options . responseCount ?? 1 ;
32
- const keyPair = new KeyPair ( ) ;
33
-
34
- // Translate the input targets into TUF TargetFile objects
35
- const targetFiles = collectTargets ( targets ) ;
36
-
37
- // Generate all of the TUF metadata objects
38
- const targetsMeta = createTargetsMeta ( targetFiles , keyPair ) ;
39
- const snapshotMeta = createSnapshotMeta ( targetsMeta , keyPair ) ;
40
- const timestampMeta = createTimestampMeta ( snapshotMeta , keyPair ) ;
41
- const rootMeta = createRootMeta ( keyPair ) ;
42
-
43
- // Calculate paths for all of the metadata files
44
- const rootPath = `${ metadataPrefix } /2.root.json` ;
45
- const timestampPath = `${ metadataPrefix } /timestamp.json` ;
46
- const snapshotPath = `${ metadataPrefix } /snapshot.json` ;
47
- const targetsPath = `${ metadataPrefix } /targets.json` ;
48
-
49
- // Mock the metadata endpoints
50
- // Note: the root metadata file request always returns a 404 to indicate that
51
- // the client should use the initial root metadata file from the cache
52
- nock ( baseURL ) . get ( rootPath ) . times ( count ) . reply ( 404 ) ;
53
- nock ( baseURL ) . get ( timestampPath ) . times ( count ) . reply ( 200 , timestampMeta ) ;
54
- nock ( baseURL ) . get ( snapshotPath ) . times ( count ) . reply ( 200 , snapshotMeta ) ;
55
- nock ( baseURL ) . get ( targetsPath ) . times ( count ) . reply ( 200 , targetsMeta ) ;
24
+ const tufRepo = initializeTUFRepo ( targets ) ;
25
+ const handlers = tufHandlers ( tufRepo , options ) ;
26
+
27
+ handlers . forEach ( ( handler ) => {
28
+ // Don't set-up a mock for the 1.root.json file as this should never be
29
+ // fetched in a normal TUF flow.
30
+ if ( handler . path . endsWith ( '1.root.json' ) ) {
31
+ return ;
32
+ }
56
33
57
- // Mock the target endpoints
58
- targets . forEach ( ( target ) => {
59
- nock ( baseURL )
60
- . get ( `${ targetPrefix } /${ target . name } ` )
61
- . reply ( 200 , target . content ) ;
34
+ mock ( baseURL , handler ) ;
62
35
} ) ;
63
36
64
- return JSON . stringify ( rootMeta ) ;
37
+ return JSON . stringify ( tufRepo . rootMeta . toJSON ( ) ) ;
65
38
}
66
39
67
40
export function clearMock ( ) {
0 commit comments