Repository navigation
fix(storage-resize-images): publish the 1st gen context on onCompletion #3279
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
Open
IzaakGough
wants to merge
3
commits into
kits
Choose a base branch
from
fix/kits-resize-images-oncompletion-context
base: kits
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
bdc2203
fix(storage-resize-images): publish the 1st gen context on onCompletion
IzaakGough 0cff221
fix(storage-resize-images): match the extension's onCompletion timest…
IzaakGough 1c7af8e
test(storage-resize-images): check onCompletion context against a cap…
IzaakGough File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * https://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 type { StorageEvent } from "firebase-functions/v2/storage"; | ||
|
|
||
| /** | ||
| * The 1st gen `EventContext` shape that the extension published inside its | ||
| * `onCompletion` payload. | ||
| */ | ||
| export interface EventContext { | ||
| eventId: string; | ||
| timestamp: string; | ||
| eventType: string; | ||
| resource: { | ||
| service: string; | ||
| name: string; | ||
| type?: string; | ||
| }; | ||
| params: Record<string, string>; | ||
| } | ||
|
|
||
| /** | ||
| * The extension's only trigger was `onFinalize`, so subscribers matching on | ||
| * `context.eventType` keep matching this value. | ||
| */ | ||
| const STORAGE_FINALIZE_EVENT_TYPE = "google.storage.object.finalize"; | ||
|
|
||
| const STORAGE_SERVICE = "storage.googleapis.com"; | ||
|
|
||
| /** | ||
| * The extension's timestamps had millisecond precision with trailing zeros | ||
| * trimmed (`...36.06Z`), while 2nd gen event times carry microseconds. | ||
| */ | ||
| function toLegacyTimestamp(time: string | undefined): string { | ||
| return new Date(time ?? Date.now()).toISOString().replace(/\.?0+Z$/, "Z"); | ||
| } | ||
|
|
||
| /** | ||
| * Rebuilds the 1st gen `EventContext` from a 2nd gen `StorageEvent`, so the | ||
| * `onCompletion` payload keeps the shape the extension published and | ||
| * `resource.name` still names the uploaded object. | ||
| * | ||
| * `params` is always empty: the extension registered | ||
| * `functions.storage.object()`, whose trigger path has no wildcards. | ||
| */ | ||
| export function toEventContext(event: StorageEvent): EventContext { | ||
| return { | ||
| eventId: event.id, | ||
| timestamp: toLegacyTimestamp(event.time), | ||
| eventType: STORAGE_FINALIZE_EVENT_TYPE, | ||
| resource: { | ||
| service: STORAGE_SERVICE, | ||
| name: `projects/_/buckets/${event.data.bucket}/objects/${event.data.name}`, | ||
| type: event.data.kind, | ||
| }, | ||
| params: {}, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /** | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * https://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 { describe, expect, test } from "vitest"; | ||
| import type { StorageEvent } from "firebase-functions/v2/storage"; | ||
| import { toEventContext } from "../src/event-context"; | ||
|
|
||
| const BUCKET = "demo-bucket"; | ||
| const OBJECT_NAME = "images/nested/photo.jpg"; | ||
|
|
||
| const event = { | ||
| id: "event-1", | ||
| type: "google.cloud.storage.object.v1.finalized", | ||
| source: `//storage.googleapis.com/projects/_/buckets/${BUCKET}`, | ||
| subject: `objects/${OBJECT_NAME}`, | ||
| time: "2026-01-01T00:00:36.898303Z", | ||
| specversion: "1.0", | ||
| bucket: BUCKET, | ||
| data: { | ||
| kind: "storage#object", | ||
| bucket: BUCKET, | ||
| name: OBJECT_NAME, | ||
| contentType: "image/jpeg", | ||
| }, | ||
| } as unknown as StorageEvent; | ||
|
|
||
| describe("toEventContext", () => { | ||
| test("rebuilds the 1st gen event context from a 2nd gen event", () => { | ||
| expect(toEventContext(event)).toEqual({ | ||
| eventId: "event-1", | ||
| timestamp: "2026-01-01T00:00:36.898Z", | ||
| eventType: "google.storage.object.finalize", | ||
| resource: { | ||
| service: "storage.googleapis.com", | ||
| name: `projects/_/buckets/${BUCKET}/objects/${OBJECT_NAME}`, | ||
| type: "storage#object", | ||
| }, | ||
| params: {}, | ||
| }); | ||
| }); | ||
|
|
||
| test.each([ | ||
| ["2026-01-01T00:00:36.898303Z", "2026-01-01T00:00:36.898Z"], | ||
| ["2026-01-01T00:00:36.060999Z", "2026-01-01T00:00:36.06Z"], | ||
| ["2026-01-01T00:00:36.000400Z", "2026-01-01T00:00:36Z"], | ||
| ])( | ||
| "formats event time %s as the extension's timestamp %s", | ||
| (time, timestamp) => { | ||
| expect(toEventContext({ ...event, time }).timestamp).toBe(timestamp); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| /** | ||
| * One upload of `in/img-7.jpg` captured from both sides: the 2nd gen finalize | ||
| * event the kit receives, and the `context` storage-resize-images 0.3.6 | ||
| * published. The extension took `eventId` and `timestamp` from a separate | ||
| * delivery of the notification, so only their format can match. | ||
| */ | ||
| const CAPTURED_STORAGE_EVENT = { | ||
| id: "22480302742407839", | ||
| type: "google.cloud.storage.object.v1.finalized", | ||
| source: "//storage.googleapis.com/projects/_/buckets/izaak-testing-pairc-ext", | ||
| subject: "objects/in/img-7.jpg", | ||
| time: "2026-10-26T15:20:21.630831Z", | ||
| specversion: "1.0", | ||
| bucket: "izaak-testing-pairc-ext", | ||
| data: { | ||
| kind: "storage#object", | ||
| bucket: "izaak-testing-pairc-ext", | ||
| name: "in/img-7.jpg", | ||
| contentType: "image/jpeg", | ||
| }, | ||
| } as unknown as StorageEvent; | ||
|
|
||
| const CAPTURED_EXTENSION_CONTEXT = { | ||
| eventId: "22480386067619997", | ||
| eventType: "google.storage.object.finalize", | ||
| resource: { | ||
| name: "projects/_/buckets/izaak-testing-pairc-ext/objects/in/img-7.jpg", | ||
| service: "storage.googleapis.com", | ||
| type: "storage#object", | ||
| }, | ||
| timestamp: "2026-10-26T15:20:21.676Z", | ||
| params: {}, | ||
| }; | ||
|
|
||
| const LEGACY_TIMESTAMP = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d{0,2}[1-9])?Z$/; | ||
| const NUMERIC_EVENT_ID = /^\d+$/; | ||
|
|
||
| describe("the context the extension actually published", () => { | ||
| test("matches the extension's eventType, resource and params exactly", () => { | ||
| const { eventType, resource, params } = toEventContext( | ||
| CAPTURED_STORAGE_EVENT | ||
| ); | ||
|
|
||
| expect({ eventType, resource, params }).toEqual({ | ||
| eventType: CAPTURED_EXTENSION_CONTEXT.eventType, | ||
| resource: CAPTURED_EXTENSION_CONTEXT.resource, | ||
| params: CAPTURED_EXTENSION_CONTEXT.params, | ||
| }); | ||
| }); | ||
|
|
||
| test("matches the format of the extension's eventId and timestamp", () => { | ||
| const { eventId, timestamp } = toEventContext(CAPTURED_STORAGE_EVENT); | ||
|
|
||
| expect(eventId).toBe("22480302742407839"); | ||
| expect(timestamp).toBe("2026-10-26T15:20:21.63Z"); | ||
| expect(CAPTURED_EXTENSION_CONTEXT.eventId).toMatch(NUMERIC_EVENT_ID); | ||
| expect(eventId).toMatch(NUMERIC_EVENT_ID); | ||
| expect(CAPTURED_EXTENSION_CONTEXT.timestamp).toMatch(LEGACY_TIMESTAMP); | ||
| expect(timestamp).toMatch(LEGACY_TIMESTAMP); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.