From c2cdc2b0cc723515098a2c3abf54082d852d12d5 Mon Sep 17 00:00:00 2001 From: Raphael Flechtner Date: Tue, 8 Jun 2021 16:53:55 +0200 Subject: [PATCH 1/4] feat: two stage build for smaller prod images --- Dockerfile | 18 +++++++++++++++++- docker-compose.yml | 4 +++- package.json | 6 +++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index c04dedf..6e47946 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:14-alpine +FROM node:14-alpine as develop WORKDIR /app @@ -10,5 +10,21 @@ RUN yarn build EXPOSE 3000 +CMD ["yarn", "start"] + +###### + +FROM node:14-alpine as production + +WORKDIR /app + +COPY package.json yarn.lock LICENSE /app/ +RUN yarn install --production --frozen-lockfile + +COPY environment /app/environment +COPY --from=develop /app/dist /app/dist + +EXPOSE 3000 + # $NODE_ENV must be exported with -e CMD ["yarn", "run", "start:provided-env"] diff --git a/docker-compose.yml b/docker-compose.yml index ae960c2..376d049 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -33,7 +33,9 @@ services: - 9944:9944 command: --dev --ws-port 9944 --ws-external services: - build: . + build: + context: . + target: develop image: local/prototype-services env_file: - environment/docker-compose.env diff --git a/package.json b/package.json index 54626d8..4c94183 100644 --- a/package.json +++ b/package.json @@ -34,9 +34,7 @@ "dotenv": "^8.2.0", "mongoose": "^5.11.15", "reflect-metadata": "^0.1.12", - "rimraf": "^3.0.2", "rxjs": "^6.6.3", - "typescript": "^4.1.3", "typescript-optional": "^2.0.1" }, "devDependencies": { @@ -56,7 +54,9 @@ "tsconfig-paths": "^3.5.0", "tslint": "^5.11.0", "tslint-config-prettier": "^1.17.0", - "tslint-plugin-prettier": "^2.0.1" + "tslint-plugin-prettier": "^2.0.1", + "typescript": "^4.1.3", + "rimraf": "^3.0.2" }, "jest": { "moduleFileExtensions": [ From 3866269f3914f0427b22f23f42fe70411f5cbce1 Mon Sep 17 00:00:00 2001 From: Raphael Flechtner Date: Mon, 5 Jul 2021 14:53:11 +0200 Subject: [PATCH 2/4] feat: do not crash when env file does not exist --- src/config/config.module.ts | 4 +++- src/config/config.service.ts | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/config/config.module.ts b/src/config/config.module.ts index 16cfc1f..953e043 100644 --- a/src/config/config.module.ts +++ b/src/config/config.module.ts @@ -6,7 +6,9 @@ import { ConfigService } from './config.service' providers: [ { provide: ConfigService, - useValue: new ConfigService(`./environment/${process.env.NODE_ENV}.env`), + useValue: new ConfigService( + `./environment/${process.env.NODE_ENV || ''}.env` + ), }, ], exports: [ConfigService], diff --git a/src/config/config.service.ts b/src/config/config.service.ts index 5ee976f..4997815 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -5,7 +5,9 @@ export class ConfigService { private readonly envConfig: { [key: string]: string } constructor(filePath: string) { - this.envConfig = dotenv.parse(fs.readFileSync(filePath)) + this.envConfig = fs.existsSync(filePath) + ? dotenv.parse(fs.readFileSync(filePath)) + : {} } public get(key: string): string { From f4b3d2d4f223f03f515e08a87c4a96bacbd5c0d9 Mon Sep 17 00:00:00 2001 From: Raphael Flechtner Date: Mon, 5 Jul 2021 15:24:36 +0200 Subject: [PATCH 3/4] deps: remove additional unused dependencies --- package.json | 5 ++--- src/auth/auth.guard.ts | 2 +- yarn.lock | 12 ------------ 3 files changed, 3 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 4c94183..85b4b42 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,6 @@ "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { - "@godaddy/terminus": "^4.6.0", "@kiltprotocol/chain-helpers": "^0.22.0", "@kiltprotocol/config": "^0.22.0", "@kiltprotocol/core": "^0.22.0", @@ -34,7 +33,6 @@ "dotenv": "^8.2.0", "mongoose": "^5.11.15", "reflect-metadata": "^0.1.12", - "rxjs": "^6.6.3", "typescript-optional": "^2.0.1" }, "devDependencies": { @@ -56,7 +54,8 @@ "tslint-config-prettier": "^1.17.0", "tslint-plugin-prettier": "^2.0.1", "typescript": "^4.1.3", - "rimraf": "^3.0.2" + "rimraf": "^3.0.2", + "rxjs": "^6.6.3" }, "jest": { "moduleFileExtensions": [ diff --git a/src/auth/auth.guard.ts b/src/auth/auth.guard.ts index c880c42..c99f7d2 100644 --- a/src/auth/auth.guard.ts +++ b/src/auth/auth.guard.ts @@ -4,7 +4,7 @@ import { ExecutionContext, Inject, } from '@nestjs/common' -import { Observable } from 'rxjs' +import type { Observable } from 'rxjs' import { ConfigService } from '../config/config.service' @Injectable() diff --git a/yarn.lock b/yarn.lock index a7415db..cca8d4a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -294,13 +294,6 @@ exec-sh "^0.3.2" minimist "^1.2.0" -"@godaddy/terminus@^4.6.0": - version "4.6.0" - resolved "https://registry.yarnpkg.com/@godaddy/terminus/-/terminus-4.6.0.tgz#1e746cf1715cf39866fd4a25569deb88774dda27" - integrity sha512-5OaPBRYIex8YcvK+Jk6/wUNulaQMWt1RWIpfYmRbP+1On5FRf3bumDWBni3V0RQRkm3JIvZiXbIndumEtasOMg== - dependencies: - stoppable "^1.1.0" - "@istanbuljs/load-nyc-config@^1.0.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced" @@ -5707,11 +5700,6 @@ stealthy-require@^1.1.1: resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b" integrity sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks= -stoppable@^1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/stoppable/-/stoppable-1.1.0.tgz#32da568e83ea488b08e4d7ea2c3bcc9d75015d5b" - integrity sha512-KXDYZ9dszj6bzvnEMRYvxgeTHU74QBFL54XKtP3nyMuJ81CFYtABZ3bAzL2EdFUaEwJOBOgENyFj3R7oTzDyyw== - streamsearch@0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-0.1.2.tgz#808b9d0e56fc273d809ba57338e929919a1a9f1a" From 16c8a1e7c2cb35728d2f917ec852b8501ea873e3 Mon Sep 17 00:00:00 2001 From: Raphael Flechtner Date: Mon, 5 Jul 2021 15:48:52 +0200 Subject: [PATCH 4/4] deps: bump prettier to support type imports --- package.json | 8 ++-- .../universal-blockchain.service.ts | 3 +- src/contacts/contacts.module.spec.ts | 38 ++++++++---------- src/contacts/mongodb-contacts.service.ts | 13 +++--- src/ctypes/ctypes.controller.ts | 4 +- src/ctypes/ctypes.module.spec.ts | 12 +++--- src/faucet/exceptions/index.ts | 8 +--- src/faucet/faucet.module.spec.ts | 40 ++++++++----------- src/health/bc.health.ts | 2 +- src/messaging/messaging.controller.ts | 8 ++-- src/messaging/messaging.module.spec.ts | 24 +++++------ yarn.lock | 8 ++-- 12 files changed, 72 insertions(+), 96 deletions(-) diff --git a/package.json b/package.json index 85b4b42..6ac3ab6 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,9 @@ "jest": "26.4.2", "mongodb-memory-server": "^6.6.3", "nodemon": "^1.18.3", - "prettier": "^1.14.2", + "prettier": "^2.0.2", + "rimraf": "^3.0.2", + "rxjs": "^6.6.3", "supertest": "^3.1.0", "ts-jest": "^26.3.0", "ts-loader": "^4.4.2", @@ -53,9 +55,7 @@ "tslint": "^5.11.0", "tslint-config-prettier": "^1.17.0", "tslint-plugin-prettier": "^2.0.1", - "typescript": "^4.1.3", - "rimraf": "^3.0.2", - "rxjs": "^6.6.3" + "typescript": "^4.1.3" }, "jest": { "moduleFileExtensions": [ diff --git a/src/blockchain/universal-blockchain.service.ts b/src/blockchain/universal-blockchain.service.ts index 11c3456..6fb5945 100644 --- a/src/blockchain/universal-blockchain.service.ts +++ b/src/blockchain/universal-blockchain.service.ts @@ -7,7 +7,8 @@ import { BlockchainService } from './interfaces/blockchain.interfaces' @Injectable() export class UniversalBlockchainService - implements BlockchainService, OnApplicationBootstrap { + implements BlockchainService, OnApplicationBootstrap +{ private static instance: Promise constructor( @Inject('ConfigService') private readonly configService: ConfigService diff --git a/src/contacts/contacts.module.spec.ts b/src/contacts/contacts.module.spec.ts index c28acf6..3b31d7d 100644 --- a/src/contacts/contacts.module.spec.ts +++ b/src/contacts/contacts.module.spec.ts @@ -95,24 +95,20 @@ describe('Contact Module', () => { let contactsController: ContactsController let contactsService: ContactsService - const mockedVerifyDidDocumentSignature = require('@kiltprotocol/core/lib/did/Did') - .verifyDidDocumentSignature + const mockedVerifyDidDocumentSignature = + require('@kiltprotocol/core/lib/did/Did').verifyDidDocumentSignature const fakeContactService: ContactsService = { - add: jest.fn( - async (): Promise => { - return - } - ), + add: jest.fn(async (): Promise => { + return + }), findByAddress: jest.fn( async (): Promise> => Optional.ofNullable(null) ), list: jest.fn(async (): Promise => []), - removeAll: jest.fn( - async (): Promise => { - return - } - ), + removeAll: jest.fn(async (): Promise => { + return + }), } beforeEach(async () => { @@ -262,7 +258,7 @@ describe('Contact Module', () => { return }, }) - public static save = jest.fn().mockImplementation(async object => object) + public static save = jest.fn().mockImplementation(async (object) => object) public save = jest .fn() .mockImplementation(async () => ContactModel.save(this)) @@ -310,11 +306,11 @@ describe('Contact Module', () => { .spyOn(contactsService['contactModel'], 'findOne') .mockReturnValueOnce({ exec: async (): Promise => - (({ + ({ ...deprecatedDIDFormat, _id: 0, toObject: () => deprecatedDIDFormat, - } as any) as ContactDB), + } as any as ContactDB), }) const replaceOne = jest.spyOn( contactsService['contactModel'], @@ -355,10 +351,10 @@ describe('Contact Module', () => { }) findOneSpy.mockReturnValue({ exec: async (): Promise => - (({ + ({ ...testContact, toObject: () => testContact, - } as any) as ContactDB), + } as any as ContactDB), }) expect(await contactsService.findByAddress(address)).toEqual( Optional.ofNullable(testContact) @@ -368,10 +364,10 @@ describe('Contact Module', () => { }) findOneSpy.mockReturnValue({ exec: async (): Promise => - (({ + ({ ...deprecatedDIDFormat, toObject: () => deprecatedDIDFormat, - } as any) as ContactDB), + } as any as ContactDB), }) expect(await contactsService.findByAddress(address)).toEqual( Optional.ofNullable(contactWithDid) @@ -387,10 +383,10 @@ describe('Contact Module', () => { .mockReturnValue({ exec: async (): Promise => { return [ - ({ + { ...testContact, toObject: () => testContact, - } as any) as ContactDB, + } as any as ContactDB, ] }, }) diff --git a/src/contacts/mongodb-contacts.service.ts b/src/contacts/mongodb-contacts.service.ts index a02b7c4..b5ebfd2 100644 --- a/src/contacts/mongodb-contacts.service.ts +++ b/src/contacts/mongodb-contacts.service.ts @@ -16,13 +16,12 @@ export class MongoDbMContactsService implements ContactsService { ) {} public async add(contact: Contact): Promise { - const registeredContact: Optional< - ContactDB & { signature?: string } - > = Optional.ofNullable( - await this.contactModel - .findOne({ 'publicIdentity.address': contact.publicIdentity.address }) - .exec() - ) + const registeredContact: Optional = + Optional.ofNullable( + await this.contactModel + .findOne({ 'publicIdentity.address': contact.publicIdentity.address }) + .exec() + ) if (registeredContact.isPresent()) { const registered = registeredContact.get() // If the contact was already registered we want to replace the document, as it could exist in outdated format! Signature is still valid. diff --git a/src/ctypes/ctypes.controller.ts b/src/ctypes/ctypes.controller.ts index ebc07a4..2775e8f 100644 --- a/src/ctypes/ctypes.controller.ts +++ b/src/ctypes/ctypes.controller.ts @@ -65,9 +65,7 @@ export class CTypesController { if (!result) { console.log( - `The CType with hash: ${ - cTypeInput.cType.hash - } already exists in this DB!` + `The CType with hash: ${cTypeInput.cType.hash} already exists in this DB!` ) throw new AlreadyRegisteredException() } diff --git a/src/ctypes/ctypes.module.spec.ts b/src/ctypes/ctypes.module.spec.ts index 4b9ab10..b9ae668 100644 --- a/src/ctypes/ctypes.module.spec.ts +++ b/src/ctypes/ctypes.module.spec.ts @@ -76,8 +76,8 @@ describe('CType Module', () => { const blockchainApi = require('@kiltprotocol/chain-helpers/lib/blockchainApiConnection/BlockchainApiConnection') - const mockedGetOwner = require('@kiltprotocol/core/lib/ctype/CType.chain') - .getOwner + const mockedGetOwner = + require('@kiltprotocol/core/lib/ctype/CType.chain').getOwner const fakeCTypeService: CTypeService = { findByHash: jest.fn( @@ -85,11 +85,9 @@ describe('CType Module', () => { ), findAll: jest.fn(async (): Promise => []), register: jest.fn(async (): Promise => true), - removeAll: jest.fn( - async (): Promise => { - return - } - ), + removeAll: jest.fn(async (): Promise => { + return + }), } beforeAll( diff --git a/src/faucet/exceptions/index.ts b/src/faucet/exceptions/index.ts index 719f3d9..28dd613 100644 --- a/src/faucet/exceptions/index.ts +++ b/src/faucet/exceptions/index.ts @@ -1,7 +1,3 @@ -export { - FaucetDropFailedTransferException, -} from './faucet-drop-failed-transfer' +export { FaucetDropFailedTransferException } from './faucet-drop-failed-transfer' export { FaucetDropThrottledException } from './faucet-drop-throttled.exception' -export { - FaucetDropInvalidAddressException, -} from './faucet-drop-invalid-address.exception' +export { FaucetDropInvalidAddressException } from './faucet-drop-invalid-address.exception' diff --git a/src/faucet/faucet.module.spec.ts b/src/faucet/faucet.module.spec.ts index 5db2d3d..8e1fe7d 100644 --- a/src/faucet/faucet.module.spec.ts +++ b/src/faucet/faucet.module.spec.ts @@ -25,22 +25,20 @@ jest.useFakeTimers() jest.mock('@kiltprotocol/core/lib/balance/Balance.chain', () => { return { - makeTransfer: jest.fn( - async (): Promise => { - return {} as SubmittableExtrinsic - } - ), + makeTransfer: jest.fn(async (): Promise => { + return {} as SubmittableExtrinsic + }), } }) jest.mock('@kiltprotocol/chain-helpers/lib/blockchain/Blockchain.utils', () => { return { __esModule: true, - signAndSubmitTx: jest.fn().mockImplementation( - async (): Promise => { + signAndSubmitTx: jest + .fn() + .mockImplementation(async (): Promise => { return { isInBlock: true } as ISubmittableResult - } - ), + }), } }) @@ -77,24 +75,20 @@ describe('Faucet Module', () => { let faucetController: FaucetController let faucetService: FaucetService - const mockedMakeTransfer = require('@kiltprotocol/core/lib/balance/Balance.chain') - .makeTransfer + const mockedMakeTransfer = + require('@kiltprotocol/core/lib/balance/Balance.chain').makeTransfer - const mockedsubmitSignedTx = require('@kiltprotocol/chain-helpers/lib/blockchain/Blockchain.utils') - .signAndSubmitTx + const mockedsubmitSignedTx = + require('@kiltprotocol/chain-helpers/lib/blockchain/Blockchain.utils').signAndSubmitTx const fakeFaucetService: FaucetService = { drop: jest.fn(async (): Promise => testFaucetDrop), - updateOnTransactionFailure: jest.fn( - async (): Promise => { - return - } - ), - reset: jest.fn( - async (): Promise => { - return - } - ), + updateOnTransactionFailure: jest.fn(async (): Promise => { + return + }), + reset: jest.fn(async (): Promise => { + return + }), } beforeAll(async () => { await cryptoWaitReady() diff --git a/src/health/bc.health.ts b/src/health/bc.health.ts index 5fc1597..8e396a1 100644 --- a/src/health/bc.health.ts +++ b/src/health/bc.health.ts @@ -28,7 +28,7 @@ export class KiltChainConnectionIndicator extends HealthIndicator { } catch (e) { reject(e) } - }).catch(e => + }).catch((e) => Promise.reject( new HealthCheckError( 'Error with Kilt chain connection', diff --git a/src/messaging/messaging.controller.ts b/src/messaging/messaging.controller.ts index e0646d4..1fc4fa0 100644 --- a/src/messaging/messaging.controller.ts +++ b/src/messaging/messaging.controller.ts @@ -18,7 +18,7 @@ import { ForbiddenMessageAccessException } from './exceptions/message-forbidden. import { MessageNotFoundException } from './exceptions/message-not-found.exception' export const uuidv4 = () => { - return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { const r = (Math.random() * 16) | 0 const v = c === 'x' ? r : (r & 0x3) | 0x8 return v.toString(16) @@ -37,9 +37,9 @@ export class MessagingController { @Param('id') id, @Headers('signature') signature ): Promise { - const { receiverAddress } = (await this.messagingService.findById( - id - )).orElseThrow(() => { + const { receiverAddress } = ( + await this.messagingService.findById(id) + ).orElseThrow(() => { throw new MessageNotFoundException() }) diff --git a/src/messaging/messaging.module.spec.ts b/src/messaging/messaging.module.spec.ts index c2e51cf..94157d7 100644 --- a/src/messaging/messaging.module.spec.ts +++ b/src/messaging/messaging.module.spec.ts @@ -35,11 +35,9 @@ describe('Messaging Module', () => { let receiverSignature: string const fakeMessagingService: MessagingService = { - add: jest.fn( - async (): Promise => { - return - } - ), + add: jest.fn(async (): Promise => { + return + }), findById: jest.fn(async () => Optional.ofNullable(null) ), @@ -49,16 +47,12 @@ describe('Messaging Module', () => { findByReceiverAddress: jest.fn( async (): Promise => [] ), - remove: jest.fn( - async (): Promise => { - return - } - ), - removeAll: jest.fn( - async (): Promise => { - return - } - ), + remove: jest.fn(async (): Promise => { + return + }), + removeAll: jest.fn(async (): Promise => { + return + }), } beforeEach(async () => { diff --git a/yarn.lock b/yarn.lock index cca8d4a..164ba55 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4907,10 +4907,10 @@ prepend-http@^1.0.1: resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-1.0.4.tgz#d4f4562b0ce3696e41ac52d0e002e57a635dc6dc" integrity sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw= -prettier@^1.14.2: - version "1.17.0" - resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.17.0.tgz#53b303676eed22cc14a9f0cec09b477b3026c008" - integrity sha512-sXe5lSt2WQlCbydGETgfm1YBShgOX4HxQkFPvbxkcwgDvGDeqVau8h+12+lmSVlP3rHPz0oavfddSZg/q+Szjw== +prettier@^2.0.2: + version "2.3.2" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.3.2.tgz#ef280a05ec253712e486233db5c6f23441e7342d" + integrity sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ== pretty-format@^25.2.1, pretty-format@^25.5.0: version "25.5.0"