Skip to content

Commit

Permalink
refactor!: rename cloudevent to CloudEvent
Browse files Browse the repository at this point in the history
This commit is a refactor of all our cloudevent identifiers to be more
consistent. Depending on the context we will always prefer: `CloudEvent`,
`cloudEvent`, or `cloud_event`. We will avoid `cloudevent`

Fixes #378
  • Loading branch information
matthewrobertson committed Nov 5, 2021
1 parent 292ade9 commit c6389bd
Show file tree
Hide file tree
Showing 38 changed files with 59 additions and 59 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -21,7 +21,7 @@
"conformance": "./run_conformance_tests.sh",
"check": "gts check",
"clean": "gts clean",
"compile": "tsc -p .",
"compile": "rm -rf ./build && tsc -p .",
"fix": "gts fix",
"predocs": "npm run compile",
"docs": "api-extractor run --local --verbose",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/function_registry.ts
Expand Up @@ -64,10 +64,10 @@ export const http = (functionName: string, handler: HttpFunction): void => {
/**
* Register a function that handles CloudEvents.
* @param functionName - the name of the function
* @param handler - the function to trigger when handling cloudevents
* @param handler - the function to trigger when handling CloudEvents
* @public
*/
export const cloudevent = (
export const cloudEvent = (
functionName: string,
handler: CloudEventFunction
): void => {
Expand Down
26 changes: 13 additions & 13 deletions src/function_wrappers.ts
Expand Up @@ -17,7 +17,7 @@ import * as domain from 'domain';
import {Request, Response, RequestHandler} from 'express';
import {sendCrashResponse} from './logger';
import {sendResponse} from './invoker';
import {isBinaryCloudEvent, getBinaryCloudEventContext} from './cloudevents';
import {isBinaryCloudEvent, getBinaryCloudEventContext} from './cloud_events';
import {
HttpFunction,
EventFunction,
Expand Down Expand Up @@ -56,17 +56,17 @@ const getOnDoneCallback = (res: Response): OnDoneCallback => {
};

/**
* Helper function to parse a cloudevent object from an HTTP request.
* Helper function to parse a CloudEvent object from an HTTP request.
* @param req an Express HTTP request
* @returns a cloudevent parsed from the request
* @returns a CloudEvent parsed from the request
*/
const parseCloudEventRequest = (req: Request): CloudEventsContext => {
let cloudevent = req.body;
let cloudEvent = req.body;
if (isBinaryCloudEvent(req)) {
cloudevent = getBinaryCloudEventContext(req);
cloudevent.data = req.body;
cloudEvent = getBinaryCloudEventContext(req);
cloudEvent.data = req.body;
}
return cloudevent;
return cloudEvent;
};

/**
Expand Down Expand Up @@ -119,7 +119,7 @@ const wrapHttpFunction = (execute: HttpFunction): RequestHandler => {
};

/**
* Wraps an async cloudevent function in an express RequestHandler.
* Wraps an async CloudEvent function in an express RequestHandler.
* @param userFunction User's function.
* @return An Express hander function that invokes the user function.
*/
Expand All @@ -128,9 +128,9 @@ const wrapCloudEventFunction = (
): RequestHandler => {
const httpHandler = (req: Request, res: Response) => {
const callback = getOnDoneCallback(res);
const cloudevent = parseCloudEventRequest(req);
const cloudEvent = parseCloudEventRequest(req);
Promise.resolve()
.then(() => userFunction(cloudevent))
.then(() => userFunction(cloudEvent))
.then(
result => callback(null, result),
err => callback(err, undefined)
Expand All @@ -140,7 +140,7 @@ const wrapCloudEventFunction = (
};

/**
* Wraps callback style cloudevent function in an express RequestHandler.
* Wraps callback style CloudEvent function in an express RequestHandler.
* @param userFunction User's function.
* @return An Express hander function that invokes the user function.
*/
Expand All @@ -149,8 +149,8 @@ const wrapCloudEventFunctionWithCallback = (
): RequestHandler => {
const httpHandler = (req: Request, res: Response) => {
const callback = getOnDoneCallback(res);
const cloudevent = parseCloudEventRequest(req);
return userFunction(cloudevent, callback);
const cloudEvent = parseCloudEventRequest(req);
return userFunction(cloudEvent, callback);
};
return wrapHttpFunction(httpHandler);
};
Expand Down
8 changes: 4 additions & 4 deletions src/functions.ts
Expand Up @@ -55,18 +55,18 @@ export interface EventFunctionWithCallback {
(data: {}, context: Context, callback: Function): any;
}
/**
* A cloudevent function handler.
* A CloudEvent function handler.
* @public
*/
export interface CloudEventFunction {
(cloudevent: CloudEventsContext): any;
(cloudEvent: CloudEventsContext): any;
}
/**
* A cloudevent function handler with callback.
* A CloudEvent function handler with callback.
* @public
*/
export interface CloudEventFunctionWithCallback {
(cloudevent: CloudEventsContext, callback: Function): any;
(cloudEvent: CloudEventsContext, callback: Function): any;
}
/**
* A function handler.
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -20,4 +20,4 @@ export * from './functions';
/**
* @public
*/
export {http, cloudevent} from './function_registry';
export {http, cloudEvent} from './function_registry';
Expand Up @@ -16,8 +16,8 @@ import {
CE_SERVICE,
EventConversionError,
isBinaryCloudEvent,
} from '../cloudevents';
import {CE_TO_BACKGROUND_TYPE} from './cloudevent_to_background_event';
} from '../cloud_events';
import {CE_TO_BACKGROUND_TYPE} from './cloud_event_to_background_event';
import {CloudFunctionsContext, LegacyEvent} from '../functions';

// Maps GCF Event types to the equivalent CloudEventType
Expand Down Expand Up @@ -193,7 +193,7 @@ export const backgroundEventToCloudEventMiddleware = (

if (service === CE_SERVICE.FIREBASE_AUTH) {
if ('metadata' in data) {
// Some metadata are not consistent between cloudevents and background events
// Some metadata are not consistent between CloudEvents and background events
const metadata: object = data.metadata;
data.metadata = {};
// eslint-disable-next-line prefer-const
Expand All @@ -213,15 +213,15 @@ export const backgroundEventToCloudEventMiddleware = (
const domain = req.body['domain'];
if (!domain) {
throw new EventConversionError(
`Unable convert to ${CE_SERVICE.FIREBASE_DB} cloudevent: invalid domain`
`Unable convert to ${CE_SERVICE.FIREBASE_DB} CloudEvent: invalid domain`
);
}
let location = 'us-central1';
if (domain !== 'firebaseio.com') {
const match = domain.match(/^([\w-]+)\.firebasedatabase\.app$/);
if (!match) {
throw new EventConversionError(
`Unable convert to ${CE_SERVICE.FIREBASE_DB} cloudevent: invalid domain`
`Unable convert to ${CE_SERVICE.FIREBASE_DB} CloudEvent: invalid domain`
);
}
location = match[1];
Expand Down
Expand Up @@ -17,7 +17,7 @@ import {
isBinaryCloudEvent,
getBinaryCloudEventContext,
EventConversionError,
} from '../cloudevents';
} from '../cloud_events';

// Maps CloudEvent types to the equivalent GCF Event type
export const CE_TO_BACKGROUND_TYPE: {[k: string]: string} = {
Expand Down Expand Up @@ -132,7 +132,7 @@ const marshalConvertableCloudEvent = (
// FirebaseAuth resource format
resource = name;
if ('metadata' in data) {
// Some metadata are not consistent between cloudevents and legacy events
// Some metadata are not consistent between CloudEvents and legacy events
const metadata: object = data.metadata;
data.metadata = {};
// eslint-disable-next-line prefer-const
Expand Down Expand Up @@ -177,7 +177,7 @@ const marshalConvertableCloudEvent = (
* @param res express response object
* @param next function used to pass control to the next middle middleware function in the stack
*/
export const cloudeventToBackgroundEventMiddleware = (
export const cloudEventToBackgroundEventMiddleware = (
req: Request,
res: Response,
next: NextFunction
Expand Down
2 changes: 1 addition & 1 deletion src/pubsub_middleware.ts
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import {Request, Response, NextFunction} from 'express';
import {isBinaryCloudEvent} from './cloudevents';
import {isBinaryCloudEvent} from './cloud_events';

const PUBSUB_EVENT_TYPE = 'google.pubsub.topic.publish';
const PUBSUB_MESSAGE_TYPE =
Expand Down
6 changes: 3 additions & 3 deletions src/server.ts
Expand Up @@ -20,8 +20,8 @@ import {HandlerFunction, Request, Response} from './functions';
import {SignatureType} from './types';
import {setLatestRes} from './invoker';
import {legacyPubSubEventMiddleware} from './pubsub_middleware';
import {cloudeventToBackgroundEventMiddleware} from './middleware/cloudevent_to_background_event';
import {backgroundEventToCloudEventMiddleware} from './middleware/background_event_to_cloudevent';
import {cloudEventToBackgroundEventMiddleware} from './middleware/cloud_event_to_background_event';
import {backgroundEventToCloudEventMiddleware} from './middleware/background_event_to_cloud_event';
import {wrapUserFunction} from './function_wrappers';

/**
Expand Down Expand Up @@ -108,7 +108,7 @@ export function getServer(
}

if (functionSignatureType === 'event') {
app.use(cloudeventToBackgroundEventMiddleware);
app.use(cloudEventToBackgroundEventMiddleware);
}
if (functionSignatureType === 'cloudevent') {
app.use(backgroundEventToCloudEventMiddleware);
Expand Down
12 changes: 6 additions & 6 deletions test/conformance/function.js
Expand Up @@ -8,19 +8,19 @@ functions.http('writeHttpDeclarative', (req, res) => {
res.end(200);
});

functions.cloudevent('writeCloudEventDeclarative', cloudevent => {
cloudevent.datacontenttype = 'application/json';
writeJson(cloudevent);
functions.cloudEvent('writeCloudEventDeclarative', cloudEvent => {
cloudEvent.datacontenttype = 'application/json';
writeJson(cloudEvent);
});

function writeHttp(req, res) {
writeJson(req.body);
res.end(200);
}

function writeCloudEvent(cloudevent) {
cloudevent.datacontenttype = 'application/json';
writeJson(cloudevent);
function writeCloudEvent(cloudEvent) {
cloudEvent.datacontenttype = 'application/json';
writeJson(cloudEvent);
}

function writeLegacyEvent(data, context) {
Expand Down
4 changes: 2 additions & 2 deletions test/function_registry.ts
Expand Up @@ -25,8 +25,8 @@ describe('function_registry', () => {
assert.deepStrictEqual((userFunction as () => string)(), 'HTTP_PASS');
});

it('can register cloudevent functions', () => {
FunctionRegistry.cloudevent('ceFunction', () => 'CE_PASS');
it('can register CloudEvent functions', () => {
FunctionRegistry.cloudEvent('ceFunction', () => 'CE_PASS');
const {
userFunction,
signatureType,
Expand Down
12 changes: 6 additions & 6 deletions test/function_wrappers.ts
Expand Up @@ -74,26 +74,26 @@ describe('wrapUserFunction', () => {
func(request, response, () => {});
});

it('correctly wraps an async cloudevent function', done => {
it('correctly wraps an async CloudEvent function', done => {
const request = createRequest(CLOUD_EVENT);
const response = createResponse();
const func = wrapUserFunction(async (cloudevent: CloudEventsContext) => {
assert.deepStrictEqual(cloudevent, CLOUD_EVENT);
const func = wrapUserFunction(async (cloudEvent: CloudEventsContext) => {
assert.deepStrictEqual(cloudEvent, CLOUD_EVENT);
// await to make sure wrapper handles async code
await new Promise(resolve => setTimeout(resolve, 20));
done();
}, 'cloudevent');
func(request, response, () => {});
});

it('correctly wraps a cloudevent function with callback', done => {
it('correctly wraps a CloudEvent function with callback', done => {
const request = createRequest(CLOUD_EVENT);
const response = createResponse();
const func = wrapUserFunction(
(cloudevent: CloudEventsContext, callback: Function) => {
(cloudEvent: CloudEventsContext, callback: Function) => {
// timeout to make sure wrapper waits for callback
setTimeout(() => {
assert.deepStrictEqual(cloudevent, CLOUD_EVENT);
assert.deepStrictEqual(cloudEvent, CLOUD_EVENT);
callback();
done();
}, 20);
Expand Down
4 changes: 2 additions & 2 deletions test/integration/cloudevent.ts
Expand Up @@ -234,8 +234,8 @@ describe('CloudEvent Function', () => {
testData.forEach(test => {
it(`${test.name}`, async () => {
let receivedCloudEvent: functions.CloudEventsContext | null = null;
const server = getServer((cloudevent: functions.CloudEventsContext) => {
receivedCloudEvent = cloudevent as functions.CloudEventsContext;
const server = getServer((cloudEvent: functions.CloudEventsContext) => {
receivedCloudEvent = cloudEvent as functions.CloudEventsContext;
}, 'cloudevent');
await supertest(server)
.post('/')
Expand Down
2 changes: 1 addition & 1 deletion test/loader.ts
Expand Up @@ -121,7 +121,7 @@ describe('loading function', () => {
});

it('respects the registered signature type', async () => {
FunctionRegistry.cloudevent('registeredFunction', () => {});
FunctionRegistry.cloudEvent('registeredFunction', () => {});
const loadedFunction = await loader.getUserFunction(
process.cwd() + '/test/data/with_main',
'registeredFunction',
Expand Down
Expand Up @@ -5,9 +5,9 @@ import {Response, Request} from 'express';
import {
splitResource,
backgroundEventToCloudEventMiddleware,
} from '../../src/middleware/background_event_to_cloudevent';
} from '../../src/middleware/background_event_to_cloud_event';
import {CloudFunctionsContext} from '../../src/functions';
import {EventConversionError} from '../../src/cloudevents';
import {EventConversionError} from '../../src/cloud_events';

describe('splitResource', () => {
const testData = [
Expand Down
Expand Up @@ -3,10 +3,10 @@ import * as sinon from 'sinon';
import {Response, Request} from 'express';

import {
cloudeventToBackgroundEventMiddleware,
cloudEventToBackgroundEventMiddleware,
parseSource,
} from '../../src/middleware/cloudevent_to_background_event';
import {EventConversionError} from '../../src/cloudevents';
} from '../../src/middleware/cloud_event_to_background_event';
import {EventConversionError} from '../../src/cloud_events';

const ceHeaders = (eventType: string, source: string) => ({
'ce-id': 'my-id',
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('parseSource', () => {
});
});

describe('cloudeventToBackgroundEventMiddleware', () => {
describe('cloudEventToBackgroundEventMiddleware', () => {
const testData = [
{
name: 'Non-CE-Request is not altered',
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('cloudeventToBackgroundEventMiddleware', () => {
headers: test.headers as object,
header: (key: string) => (test.headers as {[key: string]: string})[key],
};
cloudeventToBackgroundEventMiddleware(
cloudEventToBackgroundEventMiddleware(
request as Request,
{} as Response,
next
Expand Down
4 changes: 2 additions & 2 deletions test/system-test/pack_n_play.ts
Expand Up @@ -7,8 +7,8 @@ describe('📦 pack-n-play test', () => {
const options = {
packageDir: process.cwd(),
sample: {
description: 'JavaScript user can use the cloudevents file',
js: readFileSync('./build/src/cloudevents.js').toString(),
description: 'JavaScript user can use the cloud_events file',
js: readFileSync('./build/src/cloud_events.js').toString(),
},
};
await packNTest(options);
Expand Down

0 comments on commit c6389bd

Please sign in to comment.