remove server_extensions mixin (#76606)

This commit is contained in:
Mikhail Shustov 2020-09-03 14:02:23 +03:00 committed by GitHub
parent e976bddf81
commit 2a547914b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 0 additions and 252 deletions

View file

@ -73,10 +73,6 @@ declare module 'hapi' {
) => void;
getInjectedUiAppVars: (pluginName: string) => { [key: string]: any };
getUiNavLinks(): Array<{ _id: string }>;
addMemoizedFactoryToRequest: (
name: string,
factoryFn: (request: Request) => Record<string, any>
) => void;
logWithMetadata: (tags: string[], message: string, meta: Record<string, any>) => void;
newPlatform: KbnServer['newPlatform'];
}

View file

@ -34,7 +34,6 @@ import configCompleteMixin from './config/complete';
import { optimizeMixin } from '../../optimize';
import * as Plugins from './plugins';
import { savedObjectsMixin } from './saved_objects/saved_objects_mixin';
import { serverExtensionsMixin } from './server_extensions';
import { uiMixin } from '../ui';
import { i18nMixin } from './i18n';
@ -91,8 +90,6 @@ export default class KbnServer {
coreMixin,
// adds methods for extending this.server
serverExtensionsMixin,
loggingMixin,
warningsMixin,
statusMixin,

View file

@ -1,161 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import sinon from 'sinon';
import { serverExtensionsMixin } from './server_extensions_mixin';
describe('server.addMemoizedFactoryToRequest()', () => {
const setup = () => {
class Request {}
class Server {
constructor() {
sinon.spy(this, 'decorate');
}
decorate(type, name, value) {
switch (type) {
case 'request':
return (Request.prototype[name] = value);
case 'server':
return (Server.prototype[name] = value);
default:
throw new Error(`Unexpected decorate type ${type}`);
}
}
}
const server = new Server();
serverExtensionsMixin({}, server);
return { server, Request };
};
it('throws when propertyName is not a string', () => {
const { server } = setup();
expect(() => server.addMemoizedFactoryToRequest()).toThrowError('methodName must be a string');
expect(() => server.addMemoizedFactoryToRequest(null)).toThrowError(
'methodName must be a string'
);
expect(() => server.addMemoizedFactoryToRequest(1)).toThrowError('methodName must be a string');
expect(() => server.addMemoizedFactoryToRequest(true)).toThrowError(
'methodName must be a string'
);
expect(() => server.addMemoizedFactoryToRequest(/abc/)).toThrowError(
'methodName must be a string'
);
expect(() => server.addMemoizedFactoryToRequest(['foo'])).toThrowError(
'methodName must be a string'
);
expect(() => server.addMemoizedFactoryToRequest([1])).toThrowError(
'methodName must be a string'
);
expect(() => server.addMemoizedFactoryToRequest({})).toThrowError(
'methodName must be a string'
);
});
it('throws when factory is not a function', () => {
const { server } = setup();
expect(() => server.addMemoizedFactoryToRequest('name')).toThrowError(
'factory must be a function'
);
expect(() => server.addMemoizedFactoryToRequest('name', null)).toThrowError(
'factory must be a function'
);
expect(() => server.addMemoizedFactoryToRequest('name', 1)).toThrowError(
'factory must be a function'
);
expect(() => server.addMemoizedFactoryToRequest('name', true)).toThrowError(
'factory must be a function'
);
expect(() => server.addMemoizedFactoryToRequest('name', /abc/)).toThrowError(
'factory must be a function'
);
expect(() => server.addMemoizedFactoryToRequest('name', ['foo'])).toThrowError(
'factory must be a function'
);
expect(() => server.addMemoizedFactoryToRequest('name', [1])).toThrowError(
'factory must be a function'
);
expect(() => server.addMemoizedFactoryToRequest('name', {})).toThrowError(
'factory must be a function'
);
});
it('throws when factory takes more than one arg', () => {
const { server } = setup();
/* eslint-disable no-unused-vars */
expect(() => server.addMemoizedFactoryToRequest('name', () => {})).not.toThrowError(
'more than one argument'
);
expect(() => server.addMemoizedFactoryToRequest('name', (a) => {})).not.toThrowError(
'more than one argument'
);
expect(() => server.addMemoizedFactoryToRequest('name', (a, b) => {})).toThrowError(
'more than one argument'
);
expect(() => server.addMemoizedFactoryToRequest('name', (a, b, c) => {})).toThrowError(
'more than one argument'
);
expect(() => server.addMemoizedFactoryToRequest('name', (a, b, c, d) => {})).toThrowError(
'more than one argument'
);
expect(() => server.addMemoizedFactoryToRequest('name', (a, b, c, d, e) => {})).toThrowError(
'more than one argument'
);
/* eslint-enable no-unused-vars */
});
it('decorates request objects with a function at `propertyName`', () => {
const { server, Request } = setup();
expect(new Request()).not.toHaveProperty('decorated');
server.addMemoizedFactoryToRequest('decorated', () => {});
expect(typeof new Request().decorated).toBe('function');
});
it('caches invocations of the factory to the request instance', () => {
const { server, Request } = setup();
const factory = sinon.stub().returnsArg(0);
server.addMemoizedFactoryToRequest('foo', factory);
const request1 = new Request();
const request2 = new Request();
// call `foo()` on both requests a bunch of times, each time
// the return value should be exactly the same
expect(request1.foo()).toBe(request1);
expect(request1.foo()).toBe(request1);
expect(request1.foo()).toBe(request1);
expect(request1.foo()).toBe(request1);
expect(request1.foo()).toBe(request1);
expect(request1.foo()).toBe(request1);
expect(request2.foo()).toBe(request2);
expect(request2.foo()).toBe(request2);
expect(request2.foo()).toBe(request2);
expect(request2.foo()).toBe(request2);
// only two different requests, so factory should have only been
// called twice with the two request objects
sinon.assert.calledTwice(factory);
sinon.assert.calledWithExactly(factory, request1);
sinon.assert.calledWithExactly(factory, request2);
});
});

View file

@ -1,20 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
export { serverExtensionsMixin } from './server_extensions_mixin';

View file

@ -1,64 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
export function serverExtensionsMixin(kbnServer, server) {
/**
* Decorate all request objects with a new method, `methodName`,
* that will call the `factory` on first invocation and return
* the result of the first call to subsequent invocations.
*
* @method server.addMemoizedFactoryToRequest
* @param {string} methodName location on the request this
* factory should be added
* @param {Function} factory the factory to add to the request,
* which will be called once per request
* with a single argument, the request.
* @return {undefined}
*/
server.decorate('server', 'addMemoizedFactoryToRequest', (methodName, factory) => {
if (typeof methodName !== 'string') {
throw new TypeError('methodName must be a string');
}
if (typeof factory !== 'function') {
throw new TypeError('factory must be a function');
}
if (factory.length > 1) {
throw new TypeError(`
factory must not take more than one argument, the request object.
Memoization is done based on the request instance and is cached and reused
regardless of other arguments. If you want to have a per-request cache that
also does some sort of secondary memoization then return an object or function
from the memoized decorator and do secondary memoization there.
`);
}
const requestCache = new WeakMap();
server.decorate('request', methodName, function () {
const request = this;
if (!requestCache.has(request)) {
requestCache.set(request, factory(request));
}
return requestCache.get(request);
});
});
}