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

Some request fields are omitted in durable object #44

Open
roy-armis opened this issue Dec 26, 2023 · 0 comments
Open

Some request fields are omitted in durable object #44

roy-armis opened this issue Dec 26, 2023 · 0 comments

Comments

@roy-armis
Copy link

I'm trying to pass the request as an argument from my worker's router to the durable object's function, but most of it is getting empty.

Specifically in my case, i need the headers to be passed for authentication purposes, and it is not serializable.

current mock:
worker.js

import { error, json, Router } from "itty-router";
import { withDurables } from "itty-durable";

export { Counter } from "./Counter.durable.js";

const router = Router({ base: "/api/v1" });

router
  .all("*", withDurables())
  .get("/Counter/:CounterId", async (request, env) => {
    const counter = Counter.get(id);
    return counter.handleRequest(request);
  })
  .all("*", () => error(404, "Not found"));

export default {
  fetch: async (request, env, ctx) =>
    router
      .handle(request, env, ctx)
      .then(json)
      .catch(error)
};

Counter.durable.js:

import { createDurable } from "itty-durable";

export class Counter extends createDurable({
  autoReturn: true,
  autoPersist: true
}) {
  constructor(state, env) {
    super(state, env);

    this.storage = state.storage;
    this.env = env;
    this.sessions = [];
  }

  async handleRequest(request) {
    const requestId = request.headers.get("X-REQUEST-ID");
    ... <some authentication>

Made an ugly workaround that works, but i think it's better to solve internally:
worker.js

return counter.handleRequest(request) -> return counter.handleRequest({...request, headers: Object.fromEntries(request.headers)});

and then on my durable object's file:

  async handleRequest(request) {
    request.headers = new Headers(request.headers);
    const requestId = request.headers.get("X-REQUEST-ID");
    ...
@roy-armis roy-armis changed the title Some requestv fields are omitted in durable object Some request fields are omitted in durable object Dec 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant