-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Brian DeHamer <[email protected]>
- Loading branch information
Showing
8 changed files
with
169 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@tufjs/repo-mock': minor | ||
--- | ||
|
||
Export new helpers: `initializeTUFRepo` and `tufHandlers` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Metadata } from '@tufjs/models'; | ||
import { TUFRepo } from './repo'; | ||
import { Handler, HandlerFn } from './shared.types'; | ||
|
||
export interface TUFHandlerOptions { | ||
metadataPathPrefix?: string; | ||
targetPathPrefix?: string; | ||
} | ||
|
||
export function tufHandlers( | ||
tufRepo: TUFRepo, | ||
opts: TUFHandlerOptions | ||
): Handler[] { | ||
const metadataPrefix = opts.metadataPathPrefix ?? '/metadata'; | ||
const targetPrefix = opts.targetPathPrefix ?? '/targets'; | ||
|
||
const handlers: Handler[] = [ | ||
{ | ||
path: `${metadataPrefix}/1.root.json`, | ||
fn: respondWithMetadata(tufRepo.rootMeta), | ||
}, | ||
{ | ||
path: `${metadataPrefix}/timestamp.json`, | ||
fn: respondWithMetadata(tufRepo.timestampMeta), | ||
}, | ||
{ | ||
path: `${metadataPrefix}/snapshot.json`, | ||
fn: respondWithMetadata(tufRepo.snapshotMeta), | ||
}, | ||
{ | ||
path: `${metadataPrefix}/targets.json`, | ||
fn: respondWithMetadata(tufRepo.targetsMeta), | ||
}, | ||
{ | ||
path: `${metadataPrefix}/2.root.json`, | ||
fn: () => ({ statusCode: 404, response: '' }), | ||
}, | ||
]; | ||
|
||
tufRepo.targets.forEach((target) => { | ||
handlers.push({ | ||
path: `${targetPrefix}/${target.name}`, | ||
fn: () => ({ statusCode: 200, response: target.content }), | ||
}); | ||
}); | ||
|
||
return handlers; | ||
} | ||
|
||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ | ||
function respondWithMetadata(meta: Metadata<any>): HandlerFn { | ||
return () => ({ | ||
statusCode: 200, | ||
response: JSON.stringify(meta.toJSON()), | ||
contentType: 'application/json', | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import nock from 'nock'; | ||
import type { Handler, HandlerFn } from './shared.types'; | ||
|
||
type NockHandler = (uri: string, request: nock.Body) => nock.ReplyFnResult; | ||
|
||
// Sets-up nock-based mocking for the given handler | ||
export function mock(base: string, handler: Handler): void { | ||
nock(base).get(handler.path).reply(adapt(handler.fn)); | ||
} | ||
|
||
// Adapts our HandlerFn to nock's NockHandler format | ||
function adapt(handler: HandlerFn): NockHandler { | ||
/* istanbul ignore next */ | ||
return (): nock.ReplyFnResult => { | ||
const { statusCode, response, contentType } = handler(); | ||
|
||
return [ | ||
statusCode, | ||
response, | ||
{ 'Content-Type': contentType || 'text/plain' }, | ||
]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Metadata, Root, Snapshot, Targets, Timestamp } from '@tufjs/models'; | ||
import { KeyPair } from './key'; | ||
import { | ||
createRootMeta, | ||
createSnapshotMeta, | ||
createTargetsMeta, | ||
createTimestampMeta, | ||
} from './metadata'; | ||
import { collectTargets } from './target'; | ||
|
||
import type { Target } from './shared.types'; | ||
|
||
export interface TUFRepo { | ||
rootMeta: Metadata<Root>; | ||
timestampMeta: Metadata<Timestamp>; | ||
snapshotMeta: Metadata<Snapshot>; | ||
targetsMeta: Metadata<Targets>; | ||
targets: Target[]; | ||
} | ||
|
||
export function initializeTUFRepo(targets: Target[]): TUFRepo { | ||
const keyPair = new KeyPair(); | ||
// Translate the input targets into TUF TargetFile objects | ||
const targetFiles = collectTargets(targets); | ||
|
||
// Generate all of the TUF metadata objects | ||
const targetsMeta = createTargetsMeta(targetFiles, keyPair); | ||
const snapshotMeta = createSnapshotMeta(targetsMeta, keyPair); | ||
const timestampMeta = createTimestampMeta(snapshotMeta, keyPair); | ||
const rootMeta = createRootMeta(keyPair); | ||
|
||
return { | ||
rootMeta, | ||
snapshotMeta, | ||
timestampMeta, | ||
targetsMeta, | ||
targets, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface Target { | ||
name: string; | ||
content: string | Buffer; | ||
} | ||
|
||
export type HandlerFn = () => HandlerFnResult; | ||
|
||
export type HandlerFnResult = { | ||
statusCode: number; | ||
response: string | Buffer; | ||
contentType?: string; | ||
}; | ||
|
||
export type Handler = { | ||
path: string; | ||
fn: HandlerFn; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters