Skip to content

chore: move to eslint and prettier #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

/*
If you are here to disable rules
to try and fix specific rule sets
use
npm install --no-save eslint-nibble
*/

module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
// There is an issue with @typescript-eslint/parser performance.
// It scales with the number of projects
// see https://github.com/typescript-eslint/typescript-eslint/issues/1192#issuecomment-596741806
project: './tsconfig.lint.json',
tsconfigRootDir: process.cwd(),
},
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
ignorePatterns: ['node_modules/'],
rules: {
// These are the most useful linting rules.
// They rely on types so they are the slowest rules,
// and they are NOT enabled by default on any
// shared plugins that I know of.
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/promise-function-async': 'error',
'@typescript-eslint/no-misused-promises': 'error',
// I disagree with these rules.
// Humans read from less specific to more specific.
// No on puts the outline at the end of the book.
// Since the exported functions should be composed
// of lower level functions,
// it is good for understanding
// for the source files to get more detailed
// as you read down from the top.
'no-use-before-define': ['error', { functions: false }],
'@typescript-eslint/no-use-before-define': ['error', { functions: false }],
// This is used in a few specific ways.
// It may be that adding this to overrides for the tests
// and then manual line overrides would be
// the best way to handle this later.
'@typescript-eslint/no-explicit-any': 'off',
// Minimize churn.
'@typescript-eslint/member-delimiter-style': [
'error',
{
multiline: {
delimiter: 'none',
requireLast: false,
},
singleline: {
delimiter: 'semi',
requireLast: false,
},
},
],
// The ESDK exports some interfaces
// that conflict with this rule.
// At a later date, this might be
// able to be turned on,
// but to ensure ZERO interface changes
// this rule is disabled.
// To be clear this would only impact
// Typescript use of some types/interfaces.
'@typescript-eslint/no-empty-interface': 'off',
// To minimize the source change,
// this is turned of.
'@typescript-eslint/ban-ts-ignore': 'off',
},
// This is a good rule,
// but in many tests,
// we are just looking to mock specific functions.
overrides: [
{
files: ['modules/**/test/**/*.ts'],
rules: {
'@typescript-eslint/no-empty-function': 'off',
},
},
],
}
9 changes: 9 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// I would prefer to use the default configuration
// but then the diff is out of control.
module.exports = {
semi: false,
singleQuote: true
}
12 changes: 12 additions & 0 deletions modules/cache-material/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

module.exports = {
parserOptions: {
// There is an issue with @typescript-eslint/parser performance.
// It scales with the number of projects
// see https://github.com/typescript-eslint/typescript-eslint/issues/1192#issuecomment-596741806
project: '../../tsconfig.lint.json',
tsconfigRootDir: __dirname,
}
}
12 changes: 4 additions & 8 deletions modules/cache-material/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"scripts": {
"prepublishOnly": "npm run build",
"build": "tsc -b tsconfig.json && tsc -b tsconfig.module.json",
"lint": "standard src/*.ts test/**/*.ts",
"lint": "run-s lint-*",
"lint-eslint": "eslint src/*.ts test/**/*.ts",
"lint-prettier": "prettier -c src/*.ts test/**/*.ts",
"mocha": "mocha --require ts-node/register test/**/*test.ts",
"test": "npm run lint && npm run coverage",
"coverage": "nyc -e .ts npm run mocha"
Expand All @@ -28,11 +30,5 @@
"types": "./build/main/index.d.ts",
"files": [
"build/**/*"
],
"standard": {
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,38 @@
// SPDX-License-Identifier: Apache-2.0

import {
SupportedAlgorithmSuites, // eslint-disable-line no-unused-vars
DecryptionRequest, // eslint-disable-line no-unused-vars
EncryptionRequest, // eslint-disable-line no-unused-vars
EncryptedDataKey, // eslint-disable-line no-unused-vars
EncryptionContext // eslint-disable-line no-unused-vars
SupportedAlgorithmSuites,
DecryptionRequest,
EncryptionRequest,
EncryptedDataKey,
EncryptionContext,
} from '@aws-crypto/material-management'
import { serializeFactory, uInt16BE } from '@aws-crypto/serialize'
import { compare } from './portable_compare'

// 512 bits of 0 for padding between hashes in decryption materials cache ID generation.
const BIT_PAD_512 = Buffer.alloc(64)

export function buildCryptographicMaterialsCacheKeyHelpers<S extends SupportedAlgorithmSuites> (
export function buildCryptographicMaterialsCacheKeyHelpers<
S extends SupportedAlgorithmSuites
>(
fromUtf8: (input: string) => Uint8Array,
toUtf8: (input: Uint8Array) => string,
sha512: (...data: ((Uint8Array|string))[]) => Promise<Uint8Array>
sha512: (...data: (Uint8Array | string)[]) => Promise<Uint8Array>
): CryptographicMaterialsCacheKeyHelpersInterface<S> {
const {
serializeEncryptionContext,
serializeEncryptedDataKey
serializeEncryptedDataKey,
} = serializeFactory(fromUtf8)

return {
buildEncryptionMaterialCacheKey,
buildDecryptionMaterialCacheKey,
encryptedDataKeysHash,
encryptionContextHash
encryptionContextHash,
}

async function buildEncryptionMaterialCacheKey (
async function buildEncryptionMaterialCacheKey(
partition: string,
{ suite, encryptionContext }: EncryptionRequest<S>
) {
Expand All @@ -47,7 +49,7 @@ export function buildCryptographicMaterialsCacheKeyHelpers<S extends SupportedAl
return toUtf8(key)
}

async function buildDecryptionMaterialCacheKey (
async function buildDecryptionMaterialCacheKey(
partition: string,
{ suite, encryptedDataKeys, encryptionContext }: DecryptionRequest<S>
) {
Expand All @@ -63,16 +65,18 @@ export function buildCryptographicMaterialsCacheKeyHelpers<S extends SupportedAl
return toUtf8(key)
}

async function encryptedDataKeysHash (encryptedDataKeys: ReadonlyArray<EncryptedDataKey>) {
async function encryptedDataKeysHash(
encryptedDataKeys: ReadonlyArray<EncryptedDataKey>
) {
const hashes = await Promise.all(
encryptedDataKeys
.map(serializeEncryptedDataKey)
.map(edk => sha512(edk))
.map(async (edk) => sha512(edk))
)
return hashes.sort(compare)
}

function encryptionContextHash (context: EncryptionContext) {
async function encryptionContextHash(context: EncryptionContext) {
/* The AAD section is uInt16BE(length) + AAD
* see: https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/message-format.html#header-aad
* However, the RAW Keyring wants _only_ the ADD.
Expand All @@ -83,7 +87,9 @@ export function buildCryptographicMaterialsCacheKeyHelpers<S extends SupportedAl
}
}

export interface CryptographicMaterialsCacheKeyHelpersInterface<S extends SupportedAlgorithmSuites> {
export interface CryptographicMaterialsCacheKeyHelpersInterface<
S extends SupportedAlgorithmSuites
> {
buildEncryptionMaterialCacheKey(
partition: string,
{ suite, encryptionContext }: EncryptionRequest<S>
Expand All @@ -92,6 +98,8 @@ export interface CryptographicMaterialsCacheKeyHelpersInterface<S extends Suppor
partition: string,
{ suite, encryptedDataKeys, encryptionContext }: DecryptionRequest<S>
): Promise<string>
encryptedDataKeysHash(encryptedDataKeys: ReadonlyArray<EncryptedDataKey>): Promise<Uint8Array[]>
encryptedDataKeysHash(
encryptedDataKeys: ReadonlyArray<EncryptedDataKey>
): Promise<Uint8Array[]>
encryptionContextHash(context: EncryptionContext): Promise<Uint8Array>
}
Loading