sailfork / t3code-github / .repos/ alchemy-effect/ packages/ integration-tests/ test/ AWS/ Local/ fixtures/ raw.ts

/**
 * Raw (non-distilled) HTTP helpers against the floci emulator gateway —
 * out-of-band proof that a resource exists IN THE EMULATOR, not the real
 * cloud. The dummy SigV4 Authorization header is never verified by the
 * emulator; it only carries the service/region/account scoping the gateway
 * routes by.
 */
import * as Effect from "effect/Effect";
import * as HttpBody from "effect/http/HttpBody";
import * as HttpClient from "effect/http/HttpClient";
import * as HttpClientRequest from "effect/http/HttpClientRequest";

export const FLOCI_ENDPOINT = "http://localhost:4566";

/** POST an AWS-JSON operation straight at the emulator gateway. */
export const rawAwsJson = Effect.fn(function* (options: {
  service: string;
  region: string;
  target: string;
  contentType: string;
  body: Record<string, unknown>;
}) {
  const client = yield* HttpClient.HttpClient;
  return yield* client.execute(
    HttpClientRequest.post(`${FLOCI_ENDPOINT}/`).pipe(
      HttpClientRequest.setHeaders({
        "content-type": options.contentType,
        "x-amz-target": options.target,
        "x-amz-date": "20260101T000000Z",
        authorization: `AWS4-HMAC-SHA256 Credential=test/20260101/${options.region}/${options.service}/aws4_request, SignedHeaders=host;x-amz-date, Signature=dummy`,
      }),
      HttpClientRequest.setBody(HttpBody.text(JSON.stringify(options.body), options.contentType)),
    ),
  );
});

/** POST an AWS query-protocol operation (SNS, …) straight at the gateway. */
export const rawAwsQuery = Effect.fn(function* (options: {
  service: string;
  region: string;
  params: Record<string, string>;
}) {
  const client = yield* HttpClient.HttpClient;
  return yield* client.execute(
    HttpClientRequest.post(`${FLOCI_ENDPOINT}/`).pipe(
      HttpClientRequest.setHeaders({
        "content-type": "application/x-www-form-urlencoded",
        "x-amz-date": "20260101T000000Z",
        authorization: `AWS4-HMAC-SHA256 Credential=test/20260101/${options.region}/${options.service}/aws4_request, SignedHeaders=host;x-amz-date, Signature=dummy`,
      }),
      HttpClientRequest.setBody(
        HttpBody.text(
          new URLSearchParams(options.params).toString(),
          "application/x-www-form-urlencoded",
        ),
      ),
    ),
  );
});

/** Path-style S3 GET against the gateway (list the bucket's objects). */
export const rawS3GetBucket = Effect.fn(function* (bucketName: string) {
  const client = yield* HttpClient.HttpClient;
  return yield* client.execute(
    HttpClientRequest.get(`${FLOCI_ENDPOINT}/${bucketName}`).pipe(
      HttpClientRequest.setHeaders({
        "x-amz-date": "20260101T000000Z",
        authorization:
          "AWS4-HMAC-SHA256 Credential=test/20260101/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-date, Signature=dummy",
      }),
    ),
  );
});

/** Path-style S3 object GET against the gateway. */
export const rawS3GetObject = Effect.fn(function* (bucketName: string, key: string) {
  const client = yield* HttpClient.HttpClient;
  return yield* client.execute(
    HttpClientRequest.get(`${FLOCI_ENDPOINT}/${bucketName}/${key}`).pipe(
      HttpClientRequest.setHeaders({
        "x-amz-date": "20260101T000000Z",
        authorization:
          "AWS4-HMAC-SHA256 Credential=test/20260101/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-date, Signature=dummy",
      }),
    ),
  );
});

/** Region segment of an ARN (`arn:aws:sqs:REGION:ACCOUNT:name`). */
export const regionOfArn = (arn: string) => arn.split(":")[3]!;