From 124ca6d1d5db9083482ee04c312281e1af60135a Mon Sep 17 00:00:00 2001 From: Rifa Achrinza <25147899+achrinza@users.noreply.github.com> Date: Sat, 29 Aug 2020 17:21:53 +0800 Subject: [PATCH] chore: apply feedback Signed-off-by: Rifa Achrinza <25147899+achrinza@users.noreply.github.com> --- bodyparsers/msgpack/package-lock.json | 6 ++-- bodyparsers/msgpack/package.json | 2 +- .../acceptance/bodyparser.acceptance.ts | 9 +---- .../acceptance/component.acceptance.ts | 33 +++++++---------- .../msgpack/src/__tests__/fixtures/index.ts | 6 ---- .../fixtures/stub.parserlibrary.fixture.ts | 11 ------ bodyparsers/msgpack/src/__tests__/index.ts | 6 ---- .../src/__tests__/unit/bodyparser.unit.ts | 2 ++ .../src/__tests__/unit/parserlibrary.unit.ts | 35 ------------------- bodyparsers/msgpack/src/bodyparser.ts | 16 ++------- bodyparsers/msgpack/src/component.ts | 30 ++++++++-------- bodyparsers/msgpack/src/index.ts | 2 -- bodyparsers/msgpack/src/keys.ts | 5 --- bodyparsers/msgpack/src/parserlibrary.ts | 14 -------- bodyparsers/msgpack/src/types.ts | 8 ----- packages/express/src/middleware-registry.ts | 3 ++ 16 files changed, 41 insertions(+), 147 deletions(-) delete mode 100644 bodyparsers/msgpack/src/__tests__/fixtures/index.ts delete mode 100644 bodyparsers/msgpack/src/__tests__/fixtures/stub.parserlibrary.fixture.ts delete mode 100644 bodyparsers/msgpack/src/__tests__/index.ts delete mode 100644 bodyparsers/msgpack/src/__tests__/unit/parserlibrary.unit.ts delete mode 100644 bodyparsers/msgpack/src/parserlibrary.ts delete mode 100644 bodyparsers/msgpack/src/types.ts diff --git a/bodyparsers/msgpack/package-lock.json b/bodyparsers/msgpack/package-lock.json index 8d6ffbc678b1..4a223147180e 100644 --- a/bodyparsers/msgpack/package-lock.json +++ b/bodyparsers/msgpack/package-lock.json @@ -92,9 +92,9 @@ } }, "typescript": { - "version": "3.9.7", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz", - "integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==", + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.2.tgz", + "integrity": "sha512-e4ERvRV2wb+rRZ/IQeb3jm2VxBsirQLpQhdxplZ2MEzGvDkkMmPglecnNDfSUBivMjP93vRbngYYDQqQ/78bcQ==", "dev": true } } diff --git a/bodyparsers/msgpack/package.json b/bodyparsers/msgpack/package.json index 9a6b0ccd9160..39e3c072bb5d 100644 --- a/bodyparsers/msgpack/package.json +++ b/bodyparsers/msgpack/package.json @@ -45,7 +45,7 @@ "@types/msgpack-lite": "0.1.7", "@types/node": "^10.17.28", "@types/type-is": "^1.6.3", - "typescript": "~3.9.7" + "typescript": "~4.0.2" }, "copyright.owner": "IBM Corp.", "publishConfig": { diff --git a/bodyparsers/msgpack/src/__tests__/acceptance/bodyparser.acceptance.ts b/bodyparsers/msgpack/src/__tests__/acceptance/bodyparser.acceptance.ts index 0e80f91ad0e2..dfd13d0a3724 100644 --- a/bodyparsers/msgpack/src/__tests__/acceptance/bodyparser.acceptance.ts +++ b/bodyparsers/msgpack/src/__tests__/acceptance/bodyparser.acceptance.ts @@ -21,11 +21,7 @@ import { stubExpressContext, } from '@loopback/testlab'; import {encode} from 'msgpack-lite'; -import { - DefaultMsgPackParserLibrary, - MsgPackBodyParser, - MsgPackBodyParserBindings, -} from '../..'; +import {MsgPackBodyParser} from '../..'; describe('MessagePack body parser', () => { let requestBodyParser: RequestBodyParser; @@ -131,9 +127,6 @@ describe('MessagePack body parser', () => { const options = {}; const parsers = [new MsgPackBodyParser(options)]; const context = new Context(); - context - .bind(MsgPackBodyParserBindings.PARSER_LIBRARY) - .toClass(DefaultMsgPackParserLibrary); requestBodyParser = new RequestBodyParser(parsers, context); } }); diff --git a/bodyparsers/msgpack/src/__tests__/acceptance/component.acceptance.ts b/bodyparsers/msgpack/src/__tests__/acceptance/component.acceptance.ts index 45ba0a1da3be..8f9365e4ea88 100644 --- a/bodyparsers/msgpack/src/__tests__/acceptance/component.acceptance.ts +++ b/bodyparsers/msgpack/src/__tests__/acceptance/component.acceptance.ts @@ -3,37 +3,30 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {RestApplication} from '@loopback/rest'; +import {RestApplication, RestBindings} from '@loopback/rest'; import {expect} from '@loopback/testlab'; -import {StubParserLibrary} from '../'; import { - DefaultMsgPackParserLibrary, + MsgPackBodyParser, MsgPackBodyParserBindings, MsgPackBodyParserComponent, } from '../..'; describe('MessagePack body parser component', () => { - let restApplication: RestApplication; - beforeEach(givenRestAppWithComponent); + it('binds MessagePack body parser', async () => { + const restApplication = new RestApplication(); + restApplication.component(MsgPackBodyParserComponent); - it('binds default parser library', async () => { expect( - await restApplication.get(MsgPackBodyParserBindings.PARSER_LIBRARY), - ).to.be.instanceOf(DefaultMsgPackParserLibrary); + await restApplication.get(MsgPackBodyParserBindings.BODY_PARSER), + ).to.be.instanceOf(MsgPackBodyParser); }); - it('uses a custom parser library', async () => { - restApplication - .bind(MsgPackBodyParserBindings.PARSER_LIBRARY) - .toClass(StubParserLibrary); + it('throws error without raw body parser', async () => { + const restApplication = new RestApplication(); + restApplication.unbind(RestBindings.REQUEST_BODY_PARSER_RAW); - expect( - await restApplication.get(MsgPackBodyParserBindings.PARSER_LIBRARY), - ).to.be.instanceOf(StubParserLibrary); + expect(() => + restApplication.component(MsgPackBodyParserComponent), + ).to.throw(); }); - - function givenRestAppWithComponent() { - restApplication = new RestApplication(); - restApplication.component(MsgPackBodyParserComponent); - } }); diff --git a/bodyparsers/msgpack/src/__tests__/fixtures/index.ts b/bodyparsers/msgpack/src/__tests__/fixtures/index.ts deleted file mode 100644 index dbc30fbc57ad..000000000000 --- a/bodyparsers/msgpack/src/__tests__/fixtures/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright IBM Corp. 2020. All Rights Reserved. -// Node module: @loopback/bodyparser-msgpack -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -export * from './stub.parserlibrary.fixture'; diff --git a/bodyparsers/msgpack/src/__tests__/fixtures/stub.parserlibrary.fixture.ts b/bodyparsers/msgpack/src/__tests__/fixtures/stub.parserlibrary.fixture.ts deleted file mode 100644 index c4c2a3ecfa56..000000000000 --- a/bodyparsers/msgpack/src/__tests__/fixtures/stub.parserlibrary.fixture.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright IBM Corp. 2020. All Rights Reserved. -// Node module: @loopback/bodyparser-msgpack -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -import {MsgPackParserLibrary} from '../..'; - -export class StubParserLibrary implements MsgPackParserLibrary { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - decode(_: Buffer): any {} -} diff --git a/bodyparsers/msgpack/src/__tests__/index.ts b/bodyparsers/msgpack/src/__tests__/index.ts deleted file mode 100644 index 5c63df7274a1..000000000000 --- a/bodyparsers/msgpack/src/__tests__/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright IBM Corp. 2020. All Rights Reserved. -// Node module: @loopback/bodyparser-msgpack -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -export * from './fixtures'; diff --git a/bodyparsers/msgpack/src/__tests__/unit/bodyparser.unit.ts b/bodyparsers/msgpack/src/__tests__/unit/bodyparser.unit.ts index 3eeb43264ebe..92ad8a4f6525 100644 --- a/bodyparsers/msgpack/src/__tests__/unit/bodyparser.unit.ts +++ b/bodyparsers/msgpack/src/__tests__/unit/bodyparser.unit.ts @@ -12,7 +12,9 @@ describe('MessagePack body parser', () => { 'application/x-msgpack', 'application/subtype+msgpack', ]; + let bodyParser: MsgPackBodyParser; + beforeEach(givenBodyParser); for (const contentType of contentTypes) { diff --git a/bodyparsers/msgpack/src/__tests__/unit/parserlibrary.unit.ts b/bodyparsers/msgpack/src/__tests__/unit/parserlibrary.unit.ts deleted file mode 100644 index ec7caea1cab7..000000000000 --- a/bodyparsers/msgpack/src/__tests__/unit/parserlibrary.unit.ts +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright IBM Corp. 2020. All Rights Reserved. -// Node module: @loopback/bodyparser-msgpack -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -import {expect} from '@loopback/testlab'; -import msgpack from 'msgpack-lite'; -import {DefaultMsgPackParserLibrary, MsgPackParserLibrary} from '../..'; - -describe('Default MessagePack parser library', () => { - let msgPackPayloadLibrary: MsgPackParserLibrary; - - const payloads = [ - 'Hello, World!', - ['Hello', 'World!'], - { - data: 'Hello, World!', - }, - ]; - - beforeEach(givenMsgPackPayloadLibrary); - - for (const originalPayload in payloads) { - it(`decodes ${typeof originalPayload} MessagePack payload`, () => { - const encodedPayload = msgpack.encode(originalPayload); - const decodedPayload = msgPackPayloadLibrary.decode(encodedPayload); - - expect(decodedPayload).to.deepEqual(originalPayload); - }); - } - - function givenMsgPackPayloadLibrary() { - msgPackPayloadLibrary = new DefaultMsgPackParserLibrary(); - } -}); diff --git a/bodyparsers/msgpack/src/bodyparser.ts b/bodyparsers/msgpack/src/bodyparser.ts index 820810b374dd..0a180c7e7209 100644 --- a/bodyparsers/msgpack/src/bodyparser.ts +++ b/bodyparsers/msgpack/src/bodyparser.ts @@ -11,29 +11,17 @@ import { RequestBodyParserOptions, RestBindings, } from '@loopback/rest'; +import msgpack from 'msgpack-lite'; import {is} from 'type-is'; -import { - DefaultMsgPackParserLibrary, - MsgPackBodyParserBindings, - MsgPackParserLibrary, -} from '.'; - -// FIXME(achrinza): Workaround for https://github.com/microsoft/rushstack/pull/1867 -// eslint-disable-next-line @typescript-eslint/no-unused-vars -import * as loopbackBodyParserMsgPack from '.'; export class MsgPackBodyParser extends RawBodyParser { name = Symbol('msgpack'); - private readonly _parserLibrary: MsgPackParserLibrary; constructor( @inject(RestBindings.REQUEST_BODY_PARSER_OPTIONS, {optional: true}) options: RequestBodyParserOptions = {}, - @inject(MsgPackBodyParserBindings.PARSER_LIBRARY) - parserLibrary: MsgPackParserLibrary = new DefaultMsgPackParserLibrary(), ) { super(options); - this._parserLibrary = parserLibrary; } supports(mediaType: string) { @@ -47,7 +35,7 @@ export class MsgPackBodyParser extends RawBodyParser { async parse(request: Request): Promise { const result = await super.parse(request); - const body = this._parserLibrary.decode(result.value); + const body = msgpack.decode(result.value); return { value: body, diff --git a/bodyparsers/msgpack/src/component.ts b/bodyparsers/msgpack/src/component.ts index e2eaf6e26ef8..29ed06d62549 100644 --- a/bodyparsers/msgpack/src/component.ts +++ b/bodyparsers/msgpack/src/component.ts @@ -3,26 +3,28 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {Binding, Component} from '@loopback/core'; -import {createBodyParserBinding} from '@loopback/rest'; +import {Binding, Component, inject} from '@loopback/core'; import { - DefaultMsgPackParserLibrary, - MsgPackBodyParser, - MsgPackBodyParserBindings, -} from '.'; - -// FIXME(achrinza): Workaround for https://github.com/microsoft/rushstack/pull/1867 -// eslint-disable-next-line @typescript-eslint/no-unused-vars -import * as loopbackCore from '@loopback/core'; + createBodyParserBinding, + RawBodyParser, + RestBindings, +} from '@loopback/rest'; +import {MsgPackBodyParser, MsgPackBodyParserBindings} from '.'; export class MsgPackBodyParserComponent implements Component { - bindings = [ + bindings: Binding[] = [ createBodyParserBinding( MsgPackBodyParser, MsgPackBodyParserBindings.BODY_PARSER, ), - Binding.bind(MsgPackBodyParserBindings.PARSER_LIBRARY).toClass( - DefaultMsgPackParserLibrary, - ), ]; + + constructor( + @inject(RestBindings.REQUEST_BODY_PARSER_RAW, {optional: true}) + rawBodyParser?: RawBodyParser, + ) { + if (rawBodyParser == null) { + throw new Error('MessagePack body parser requires raw body parser.'); + } + } } diff --git a/bodyparsers/msgpack/src/index.ts b/bodyparsers/msgpack/src/index.ts index b4acf8916045..6c64539f9efd 100644 --- a/bodyparsers/msgpack/src/index.ts +++ b/bodyparsers/msgpack/src/index.ts @@ -6,5 +6,3 @@ export * from './bodyparser'; export * from './component'; export * from './keys'; -export * from './parserlibrary'; -export * from './types'; diff --git a/bodyparsers/msgpack/src/keys.ts b/bodyparsers/msgpack/src/keys.ts index 19e5b2477012..edc7314a9693 100644 --- a/bodyparsers/msgpack/src/keys.ts +++ b/bodyparsers/msgpack/src/keys.ts @@ -5,14 +5,9 @@ import {BindingKey} from '@loopback/core'; import {BodyParser, RestBindings} from '@loopback/rest'; -import {MsgPackParserLibrary} from '.'; export namespace MsgPackBodyParserBindings { export const BODY_PARSER = BindingKey.create( `${RestBindings.REQUEST_BODY_PARSER}.msgpack`, ); - - export const PARSER_LIBRARY = BindingKey.create( - `${BODY_PARSER}.parserLibrary`, - ); } diff --git a/bodyparsers/msgpack/src/parserlibrary.ts b/bodyparsers/msgpack/src/parserlibrary.ts deleted file mode 100644 index 4f43c3f77706..000000000000 --- a/bodyparsers/msgpack/src/parserlibrary.ts +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright IBM Corp. 2020. All Rights Reserved. -// Node module: @loopback/bodyparser-msgpack -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -import {decode} from 'msgpack-lite'; -import {MsgPackParserLibrary} from '.'; - -export class DefaultMsgPackParserLibrary implements MsgPackParserLibrary { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - decode(msgPackBody: Buffer): any { - return decode(msgPackBody); - } -} diff --git a/bodyparsers/msgpack/src/types.ts b/bodyparsers/msgpack/src/types.ts deleted file mode 100644 index 63cd73e27657..000000000000 --- a/bodyparsers/msgpack/src/types.ts +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright IBM Corp. 2020. All Rights Reserved. -// Node module: @loopback/bodyparser-msgpack -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -export interface MsgPackParserLibrary { - decode(msgPackBody: Buffer): string; -} diff --git a/packages/express/src/middleware-registry.ts b/packages/express/src/middleware-registry.ts index 98b094d00012..adc521151704 100644 --- a/packages/express/src/middleware-registry.ts +++ b/packages/express/src/middleware-registry.ts @@ -18,6 +18,9 @@ import { MiddlewareBindingOptions, } from './types'; +// FIXME(rfeng): Workaround for https://github.com/microsoft/rushstack/pull/1867 +/* eslint-disable @typescript-eslint/no-unused-vars */ +import * as loopbackContext from '@loopback/core'; /* eslint-enable @typescript-eslint/no-unused-vars */ /**