-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hex): Add protobuf definitions for registry v2 support (#32222)
- Loading branch information
Showing
7 changed files
with
742 additions
and
1 deletion.
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,92 @@ | ||
import protobuf from 'protobufjs'; | ||
import upath from 'upath'; | ||
import { Package } from './package'; | ||
import { Signed } from './signed'; | ||
|
||
function protobufLoad(file: string): Promise<protobuf.Root> { | ||
const resolvedFile = upath.join(__dirname, file); | ||
return new Promise((resolve, reject) => { | ||
protobuf.load(resolvedFile, (err, root) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
|
||
if (!root) { | ||
reject(new Error('Root is empty')); | ||
return; | ||
} | ||
|
||
resolve(root); | ||
}); | ||
}); | ||
} | ||
|
||
describe('modules/datasource/hex/v2/index', () => { | ||
describe('Signed', () => { | ||
async function encodeSigned(input: unknown): Promise<Buffer> { | ||
const message = Signed.fromJSON(input); | ||
const root = await protobufLoad('signed.proto'); | ||
const x = root.lookupType('Signed').encode(message).finish(); | ||
return Buffer.from(x); | ||
} | ||
|
||
it('roundtrip', async () => { | ||
const input = { | ||
payload: Buffer.from('foo'), | ||
signature: Buffer.from('bar'), | ||
}; | ||
const encodedBuf = await encodeSigned(input); | ||
|
||
const output = Signed.decode(encodedBuf); | ||
|
||
expect(output).toEqual(input); | ||
}); | ||
}); | ||
|
||
describe('Package', () => { | ||
async function encodePackage(input: unknown): Promise<Buffer> { | ||
const message = Package.fromJSON(input); | ||
const root = await protobufLoad('package.proto'); | ||
const x = root.lookupType('Package').encode(message).finish(); | ||
return Buffer.from(x); | ||
} | ||
|
||
it('roundtrip', async () => { | ||
const input: Package = { | ||
name: 'foo', | ||
repository: 'hexpm', | ||
releases: [ | ||
{ | ||
version: '1.0.0', | ||
innerChecksum: new Uint8Array(), | ||
dependencies: [], | ||
}, | ||
{ | ||
version: '2.0.0', | ||
innerChecksum: new Uint8Array(), | ||
dependencies: [], | ||
}, | ||
{ | ||
version: '3.0.0', | ||
innerChecksum: new Uint8Array(), | ||
dependencies: [], | ||
}, | ||
], | ||
}; | ||
const encodedBuf = await encodePackage(input); | ||
|
||
const output = Package.decode(encodedBuf); | ||
|
||
expect(output).toMatchObject({ | ||
name: 'foo', | ||
repository: 'hexpm', | ||
releases: [ | ||
{ version: '1.0.0' }, | ||
{ version: '2.0.0' }, | ||
{ version: '3.0.0' }, | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
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,55 @@ | ||
// https://github.com/hexpm/specifications/blob/9c411dbe96c87ccee438d40838d02b5fa6abb60b/registry/package.proto | ||
|
||
syntax = "proto2"; | ||
|
||
message Package { | ||
// All releases of the package | ||
repeated Release releases = 1; | ||
// Name of package | ||
required string name = 2; | ||
// Name of repository | ||
required string repository = 3; | ||
} | ||
|
||
message Release { | ||
// Release version | ||
required string version = 1; | ||
// sha256 checksum of "inner" package tarball | ||
// deprecated in favor of outer_checksum | ||
required bytes inner_checksum = 2; | ||
// All dependencies of the release | ||
repeated Dependency dependencies = 3; | ||
// If set the release is retired, a retired release should only be | ||
// resolved if it has already been locked in a project | ||
optional RetirementStatus retired = 4; | ||
// sha256 checksum of outer package tarball | ||
// required when encoding but optional when decoding | ||
optional bytes outer_checksum = 5; | ||
} | ||
|
||
message RetirementStatus { | ||
required RetirementReason reason = 1; | ||
optional string message = 2; | ||
} | ||
|
||
enum RetirementReason { | ||
RETIRED_OTHER = 0; | ||
RETIRED_INVALID = 1; | ||
RETIRED_SECURITY = 2; | ||
RETIRED_DEPRECATED = 3; | ||
RETIRED_RENAMED = 4; | ||
} | ||
|
||
message Dependency { | ||
// Package name of dependency | ||
required string package = 1; | ||
// Version requirement of dependency | ||
required string requirement = 2; | ||
// If set and true the package is optional (see dependency resolution) | ||
optional bool optional = 3; | ||
// If set is the OTP application name of the dependency, if not set the | ||
// application name is the same as the package name | ||
optional string app = 4; | ||
// If set, the repository where the dependency is located | ||
optional string repository = 5; | ||
} |
Oops, something went wrong.