Skip to content

Commit

Permalink
fix: expose metadata headers to scripts running in the browser (#6952)
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig authored and wemeetagain committed Jul 15, 2024
1 parent 4751dfe commit 4696307
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/api/src/utils/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ export enum HttpHeader {
ContentType = "content-type",
Accept = "accept",
Authorization = "authorization",
/**
* Used to indicate which response headers should be made available to
* scripts running in the browser, in response to a cross-origin request.
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
*/
ExposeHeaders = "access-control-expose-headers",
}

export enum MediaType {
Expand Down
7 changes: 7 additions & 0 deletions packages/api/src/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {StringType, ssz, stringType} from "@lodestar/types";
import {ResponseMetadataCodec} from "./types.js";
import {toBoolean} from "./serdes.js";
import {toForkName} from "./fork.js";
import {HttpHeader} from "./headers.js";

export const VersionType = new ContainerType({
/**
Expand Down Expand Up @@ -90,6 +91,7 @@ export const ExecutionOptimisticCodec: ResponseMetadataCodec<ExecutionOptimistic
fromJson: (val) => ExecutionOptimisticType.fromJson(val),
toHeadersObject: (val) => ({
[MetaHeader.ExecutionOptimistic]: val.executionOptimistic.toString(),
[HttpHeader.ExposeHeaders]: MetaHeader.ExecutionOptimistic,
}),
fromHeaders: (headers) => ({
executionOptimistic: toBoolean(headers.getOrDefault(MetaHeader.ExecutionOptimistic, "false")),
Expand All @@ -101,6 +103,7 @@ export const VersionCodec: ResponseMetadataCodec<VersionMeta> = {
fromJson: (val) => VersionType.fromJson(val),
toHeadersObject: (val) => ({
[MetaHeader.Version]: val.version,
[HttpHeader.ExposeHeaders]: MetaHeader.Version,
}),
fromHeaders: (headers) => ({
version: toForkName(headers.getRequired(MetaHeader.Version)),
Expand All @@ -113,6 +116,7 @@ export const ExecutionOptimisticAndVersionCodec: ResponseMetadataCodec<Execution
toHeadersObject: (val) => ({
[MetaHeader.ExecutionOptimistic]: val.executionOptimistic.toString(),
[MetaHeader.Version]: val.version,
[HttpHeader.ExposeHeaders]: [MetaHeader.ExecutionOptimistic, MetaHeader.Version].toString(),
}),
fromHeaders: (headers) => ({
executionOptimistic: toBoolean(headers.getOrDefault(MetaHeader.ExecutionOptimistic, "false")),
Expand All @@ -126,6 +130,7 @@ export const ExecutionOptimisticAndFinalizedCodec: ResponseMetadataCodec<Executi
toHeadersObject: (val) => ({
[MetaHeader.ExecutionOptimistic]: val.executionOptimistic.toString(),
[MetaHeader.Finalized]: val.finalized.toString(),
[HttpHeader.ExposeHeaders]: [MetaHeader.ExecutionOptimistic, MetaHeader.Finalized].toString(),
}),
fromHeaders: (headers) => ({
executionOptimistic: toBoolean(headers.getOrDefault(MetaHeader.ExecutionOptimistic, "false")),
Expand All @@ -141,6 +146,7 @@ export const ExecutionOptimisticFinalizedAndVersionCodec: ResponseMetadataCodec<
[MetaHeader.ExecutionOptimistic]: val.executionOptimistic.toString(),
[MetaHeader.Finalized]: val.finalized.toString(),
[MetaHeader.Version]: val.version,
[HttpHeader.ExposeHeaders]: [MetaHeader.ExecutionOptimistic, MetaHeader.Finalized, MetaHeader.Version].toString(),
}),
fromHeaders: (headers) => ({
executionOptimistic: toBoolean(headers.getOrDefault(MetaHeader.ExecutionOptimistic, "false")),
Expand All @@ -156,6 +162,7 @@ export const ExecutionOptimisticAndDependentRootCodec: ResponseMetadataCodec<Exe
toHeadersObject: (val) => ({
[MetaHeader.ExecutionOptimistic]: val.executionOptimistic.toString(),
[MetaHeader.DependentRoot]: val.dependentRoot,
[HttpHeader.ExposeHeaders]: [MetaHeader.ExecutionOptimistic, MetaHeader.DependentRoot].toString(),
}),
fromHeaders: (headers) => ({
executionOptimistic: toBoolean(headers.getOrDefault(MetaHeader.ExecutionOptimistic, "false")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {describe, it, beforeEach, afterEach, expect} from "vitest";
import bls from "@chainsafe/bls";
import {createBeaconConfig, ChainConfig} from "@lodestar/config";
import {chainConfig as chainConfigDef} from "@lodestar/config/default";
import {getClient, routes} from "@lodestar/api";
import {getClient, HttpHeader, routes} from "@lodestar/api";
import {sleep} from "@lodestar/utils";
import {ForkName, SYNC_COMMITTEE_SIZE} from "@lodestar/params";
import {Validator} from "@lodestar/validator";
Expand Down Expand Up @@ -102,6 +102,8 @@ describe("lightclient api", function () {
expect(update.attestedHeader.beacon.slot).toBe(slot - 1);
// version is set
expect(res.meta().version).toBe(ForkName.altair);
// Ensure version header is made available to scripts running in the browser
expect(res.headers.get(HttpHeader.ExposeHeaders)?.includes("Eth-Consensus-Version")).toBe(true);
});

it.skip("getLightClientFinalityUpdate()", async function () {
Expand Down

0 comments on commit 4696307

Please sign in to comment.