Skip to content
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

Add startup lifecycle hook #1531

Merged
merged 5 commits into from
Mar 7, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add onInit callback function for global variable initialization (#1531)
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
"v2": [
"lib/v2"
],
"v2/core": [
"lib/v2/core"
],
"v2/alerts": [
"lib/v2/providers/alerts"
],
Expand Down Expand Up @@ -250,4 +253,4 @@
"engines": {
"node": ">=14.10.0"
}
}
}
29 changes: 29 additions & 0 deletions spec/v1/cloud-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import { expect } from "chai";

import {
onInit,
Event,
EventContext,
makeCloudFunction,
Expand All @@ -41,6 +42,34 @@ describe("makeCloudFunction", () => {
legacyEventType: "providers/provider/eventTypes/event",
};

it("calls init function", async () => {
const test: Event = {
context: {
eventId: "00000",
timestamp: "2016-11-04T21:29:03.496Z",
eventType: "provider.event",
resource: {
service: "provider",
name: "resource",
},
},
data: "data",
};
const cf = makeCloudFunction({
provider: "mock.provider",
eventType: "mock.event",
service: "service",
triggerResource: () => "resource",
handler: () => null,
});

let hello;
onInit(() => (hello = "world"));
expect(hello).is.undefined;
await cf(test.data, test.context);
expect(hello).equals("world");
});

it("should put a __trigger/__endpoint on the returned CloudFunction", () => {
const cf = makeCloudFunction({
provider: "mock.provider",
Expand Down
4 changes: 2 additions & 2 deletions spec/v1/providers/analytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("Analytics Functions", () => {
});

describe("#dataConstructor", () => {
it("should handle an event with the appropriate fields", () => {
it("should handle an event with the appropriate fields", async () => {
const cloudFunction = analytics
.event("first_open")
.onLog((data: analytics.AnalyticsEvent) => data);
Expand All @@ -109,7 +109,7 @@ describe("Analytics Functions", () => {
},
};

return expect(cloudFunction(event.data, event.context)).to.eventually.deep.equal({
await expect(cloudFunction(event.data, event.context)).to.eventually.deep.equal({
params: {},
user: {
userId: "hi!",
Expand Down
45 changes: 43 additions & 2 deletions spec/v1/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
import { runHandler } from "../../helper";
import { MINIMAL_V1_ENDPOINT } from "../../fixtures";
import { CALLABLE_AUTH_HEADER, ORIGINAL_AUTH_HEADER } from "../../../src/common/providers/https";
import { onInit } from "../../../src/v1";

describe("CloudHttpsBuilder", () => {
describe("#onRequest", () => {
Expand Down Expand Up @@ -70,6 +71,26 @@ describe("CloudHttpsBuilder", () => {
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
expect(fn.__endpoint.httpsTrigger.invoker).to.deep.equal(["private"]);
});

it("calls init function", async () => {
let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
const fn = functions.https.onRequest((_req, res) => {
res.send(200);
});
const req = new MockRequest(
{
data: { foo: "bar" },
},
{
"content-type": "application/json",
}
);
req.method = "POST";
await runHandler(fn, req as any);
expect(hello).to.equal("world");
});
});
});

Expand Down Expand Up @@ -114,7 +135,7 @@ describe("#onCall", () => {
expect(fn.__endpoint.timeoutSeconds).to.deep.equal(90);
});

it("has a .run method", () => {
it("has a .run method", async () => {
const cf = https.onCall((d, c) => {
return { data: d, context: c };
});
Expand All @@ -127,7 +148,8 @@ describe("#onCall", () => {
token: "token",
},
};
expect(cf.run(data, context)).to.deep.equal({ data, context });

await expect(cf.run(data, context)).to.eventually.deep.equal({ data, context });
});

// Regression test for firebase-functions#947
Expand All @@ -152,6 +174,25 @@ describe("#onCall", () => {
expect(gotData).to.deep.equal({ foo: "bar" });
});

it("should call initializer", async () => {
const func = https.onCall(() => null);
const req = new MockRequest(
{
data: {},
},
{
"content-type": "application/json",
}
);
req.method = "POST";

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await runHandler(func, req as any);
expect(hello).to.equal("world");
});

// Test for firebase-tools#5210
it("should create context.auth for v1 emulated functions", async () => {
sinon.stub(debug, "isDebugFeatureEnabled").withArgs("skipTokenVerification").returns(true);
Expand Down
14 changes: 8 additions & 6 deletions spec/v1/providers/remoteConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ describe("RemoteConfig Functions", () => {
delete process.env.GCLOUD_PROJECT;
});

it("should unwrap the version in the event", () => {
return Promise.all([
cloudFunctionUpdate(event.data, event.context).then((data: any) => {
expect(data).to.deep.equal(constructVersion());
}),
]);
it("should unwrap the version in the event", async () => {
let hello;
functions.onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await cloudFunctionUpdate(event.data, event.context).then((data: any) => {
expect(data).to.deep.equal(constructVersion());
});
expect(hello).to.equal("world");
});
});
});
19 changes: 18 additions & 1 deletion spec/v2/providers/alerts/alerts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import { CloudEvent } from "../../../../src/v2";
import { CloudEvent, onInit } from "../../../../src/v2";
import * as options from "../../../../src/v2/options";
import * as alerts from "../../../../src/v2/providers/alerts";
import { FULL_OPTIONS } from "../fixtures";
Expand Down Expand Up @@ -211,4 +211,21 @@ describe("alerts", () => {
});
});
});

it("calls init function", async () => {
const event: CloudEvent<string> = {
specversion: "1.0",
id: "id",
source: "source",
type: "type",
time: "now",
data: "data",
};

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await alerts.onAlertPublished("alert", () => null)(event);
expect(hello).to.equal("world");
});
});
21 changes: 21 additions & 0 deletions spec/v2/providers/alerts/appDistribution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as alerts from "../../../../src/v2/providers/alerts";
import * as appDistribution from "../../../../src/v2/providers/alerts/appDistribution";
import { FULL_OPTIONS } from "../fixtures";
import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT } from "../../../fixtures";
import { onInit } from "../../../../src/v2/core";

const APPID = "123456789";
const myHandler = () => 42;
Expand Down Expand Up @@ -91,6 +92,16 @@ describe("appDistribution", () => {

expect(res).to.equal("input");
});

it("calls init function", async () => {
const func = appDistribution.onNewTesterIosDevicePublished(APPID, (event) => event);

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await func({ data: "test" } as any);
expect(hello).to.equal("world");
});
});

describe("onInAppfeedbackPublished", () => {
Expand Down Expand Up @@ -172,6 +183,16 @@ describe("appDistribution", () => {

expect(res).to.equal("input");
});

it("calls init function", async () => {
const func = appDistribution.onInAppFeedbackPublished(APPID, (event) => event);

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await func({ data: "test" } as any);
expect(hello).to.equal("world");
});
});

describe("getOptsAndApp", () => {
Expand Down
31 changes: 31 additions & 0 deletions spec/v2/providers/alerts/billing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as alerts from "../../../../src/v2/providers/alerts";
import * as billing from "../../../../src/v2/providers/alerts/billing";
import { FULL_OPTIONS } from "../fixtures";
import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT } from "../../../fixtures";
import { onInit } from "../../../../src/v2/core";

const ALERT_TYPE = "new-alert-type";
const myHandler = () => 42;
Expand Down Expand Up @@ -41,6 +42,16 @@ describe("billing", () => {
},
});
});

it("calls init function", async () => {
const func = billing.onPlanAutomatedUpdatePublished((event) => event);

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await func({ data: "test" } as any);
expect(hello).to.equal("world");
});
});

describe("onPlanAutomatedUpdatePublished", () => {
Expand Down Expand Up @@ -76,6 +87,16 @@ describe("billing", () => {
},
});
});

it("calls init function", async () => {
const func = billing.onPlanAutomatedUpdatePublished((event) => event);

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await func({ data: "test" } as any);
expect(hello).to.equal("world");
});
});

describe("onOperation", () => {
Expand Down Expand Up @@ -119,5 +140,15 @@ describe("billing", () => {

expect(res).to.equal("input");
});

it("calls init function", async () => {
const func = billing.onOperation(ALERT_TYPE, (event) => event, undefined);

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await func({ data: "test" } as any);
expect(hello).to.equal("world");
});
});
});
11 changes: 11 additions & 0 deletions spec/v2/providers/alerts/crashlytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as alerts from "../../../../src/v2/providers/alerts";
import * as crashlytics from "../../../../src/v2/providers/alerts/crashlytics";
import { FULL_OPTIONS } from "../fixtures";
import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT } from "../../../fixtures";
import { onInit } from "../../../../src/v2/core";

const ALERT_TYPE = "new-alert-type";
const APPID = "123456789";
Expand Down Expand Up @@ -104,6 +105,16 @@ describe("crashlytics", () => {
},
});
});

it("calls init function", async () => {
const func = crashlytics[method](APPID, myHandler);

let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await func({ data: "crash" } as any);
expect(hello).to.equal("world");
});
});
}

Expand Down
18 changes: 18 additions & 0 deletions spec/v2/providers/alerts/performance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as alerts from "../../../../src/v2/providers/alerts";
import * as performance from "../../../../src/v2/providers/alerts/performance";
import { FULL_OPTIONS } from "../fixtures";
import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT } from "../../../fixtures";
import { CloudEvent, onInit } from "../../../../src/v2/core";

const APPID = "123456789";
const myHandler = () => 42;
Expand Down Expand Up @@ -45,6 +46,23 @@ describe("performance", () => {
retry: false,
},
});

it("calls init function", async () => {
const event: CloudEvent<string> = {
specversion: "1.0",
id: "id",
source: "source",
type: "type",
time: "now",
data: "data",
};

let hello: string;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await performance.onThresholdAlertPublished(() => null)(event);
expect(hello).to.equal("world");
});
});

it("should create a function with appid in opts", () => {
Expand Down
Loading
Loading