Skip to content

Commit

Permalink
Auto-update typed envelopes version metadata (#3)
Browse files Browse the repository at this point in the history
* Added version bumping and tracking machinery
* Removed ctix, added index.* files
* prepublish -> prepack hook
* Added semver range support for AADMeta + test
  • Loading branch information
zblach authored Apr 20, 2021
1 parent a33e5d9 commit ac06797
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 1,120 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ jobs:
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}

- run: yarn install --dev
- run: yarn postversion
- run: yarn test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ dist

# intellij
.idea

# automatically generated by build & package scripts
src/version.ts
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src'
1 change: 0 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ export default {
testMatch: ['<rootDir>/test/**/*.ts'],
testPathIgnorePatterns: ['/node_modules/'],
globals: { 'ts-jest': { diagnostics: false } },
transform: {},
};
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@skiff-org/typed-envelopes",
"version": "0.1.0",
"version": "0.1.1",
"license": "AGPL-3.0",
"author": "skiff-org",
"main": "./dist/index.js",
Expand All @@ -16,11 +16,11 @@
"@types/jest": "^26.0.22",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.21.0",
"ctix": "^0.3.1",
"eslint": "^7.23.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.3.4",
"genversion": "^2.3.1",
"jest": "^26.6.3",
"ts-jest": "^26.5.5",
"ts-node": "^9.1.1",
Expand All @@ -30,13 +30,13 @@
"./": "./dist/"
},
"files": [
"./dist/index.*",
"./dist/src/"
"/dist/*index*",
"/dist/src/*"
],
"scripts": {
"prepublish": "yarn clean && yarn index && yarn build",
"prepack": "yarn clean && yarn postversion && yarn build",
"postversion": "genversion -es ./src/version.ts",
"clean": "rm -rf ./dist/*",
"index": "yarn ctix ./tsconfig.json -b false",
"build": "tsc",
"test": "yarn jest"
},
Expand Down
9 changes: 5 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// eslint-disable-next-line max-classes-per-file
import { Range } from 'semver';
import { version as AADMETA_VERSION } from './version';

const varint = require('varint');

Expand Down Expand Up @@ -52,7 +53,7 @@ export type Wrapped<T> = Versioned & Typed & { data: T };
* AADMeta is a class that encapsulates the additional metadata included in these envelope implementations.
*/
export class AADMeta implements Datagram<AADMeta> {
static readonly METADATA_VERSION = '0.1.0';
private static readonly SUPPORTED_VERSIONS = new Range(`0.1.* - ${AADMETA_VERSION}`);

constructor(readonly version: string, readonly type: string, readonly nonce: Uint8Array) {}

Expand All @@ -66,7 +67,7 @@ export class AADMeta implements Datagram<AADMeta> {
const metadata = Object.create(AADMeta.prototype).deserialize(header.copyWithin(0, 0));

const metadataVersion = _decoder.decode(extractVarintPrefixed(headerBuf));
if (metadataVersion !== AADMeta.METADATA_VERSION) {
if (!AADMeta.SUPPORTED_VERSIONS.test(metadataVersion)) {
throw new Error('unrecognized metadata version');
}

Expand All @@ -81,7 +82,7 @@ export class AADMeta implements Datagram<AADMeta> {
deserialize(data: Uint8Array): AADMeta | null {
const buf = { bs: data };
const metadataVersion = _decoder.decode(extractVarintPrefixed(buf));
if (metadataVersion !== AADMeta.METADATA_VERSION) {
if (!AADMeta.SUPPORTED_VERSIONS.test(metadataVersion)) {
return null;
}
const metadata = new AADMeta(
Expand Down Expand Up @@ -123,7 +124,7 @@ export class AADMeta implements Datagram<AADMeta> {
*
*/
const data: Uint8Array = concatUint8Arrays(
varintPrefixed(_encoder.encode(AADMeta.METADATA_VERSION)),
varintPrefixed(_encoder.encode(AADMETA_VERSION)),
varintPrefixed(_encoder.encode(this.version)),
varintPrefixed(_encoder.encode(this.type)),
varintPrefixed(this.nonce),
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './common';
export * from './secretbox';
export * from './version';
10 changes: 9 additions & 1 deletion test/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { concatUint8Arrays, varintPrefixed } from '../src/';
import { concatUint8Arrays, varintPrefixed, AADMeta } from '../src';

describe('test concatenation', () => {
test('simple', () => {
Expand All @@ -15,3 +15,11 @@ describe('test concatenation', () => {
expect(varintPrefixed(a)).toStrictEqual(Uint8Array.of(6, 1, 2, 3, 4, 5, 6));
});
});

describe('allows previous versions', () => {
const v010 = new AADMeta('0.1.0', 'arbitrary string', Uint8Array.of(1, 2, 3, 4, 5));

const dat = AADMeta.unpack(v010.serialize());
expect(dat).not.toBeNull();
expect(dat?.metadata.version).toStrictEqual('0.1.0');
});
3 changes: 1 addition & 2 deletions test/secretbox.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// eslint-disable-next-line max-classes-per-file
import { Range } from 'semver';
import { randomBytes } from 'crypto';
import { Datagram, Wrapper } from '../src';
import { SecretBox } from '../src';
import { Datagram, Wrapper, SecretBox } from '../src';

const _encoder = new TextEncoder();
const _decoder = new TextDecoder();
Expand Down
Loading

0 comments on commit ac06797

Please sign in to comment.