Class Bytebox

java.lang.Object
dev.gmitch215.bytebox.Bytebox

public final class Bytebox extends Object
Builds the platform types a handler returns, and reaches the platform functions that are not bindings.
 public Response fetch(Request request, Env env, ExecutionCtx ctx) {
 	if (!request.getMethod().equals("GET")) return Bytebox.status(405);
 	return Bytebox.json("{\"ok\":true}");
 }
Since:
1.0.0
  • Method Details

    • response

      public static Response response(String body)
      A 200 carrying text.
      Parameters:
      body - the body
      Returns:
      the response
    • response

      public static Response response(String body, int status)
      A response with an explicit status.
      Parameters:
      body - the body
      status - the status code
      Returns:
      the response
    • response

      public static Response response(String body, int status, TSObject headers)
      A response with a status and headers.
      Parameters:
      body - the body
      status - the status code
      headers - the headers, as an object of name to value
      Returns:
      the response
    • bytes

      public static Response bytes(org.teavm.jso.typedarrays.ArrayBuffer body, String contentType)
      A response carrying bytes.
      Parameters:
      body - the body
      contentType - the media type to serve it as
      Returns:
      the response
    • bytes

      public static Response bytes(byte[] body, String contentType)
      A response carrying bytes.
      Parameters:
      body - the body
      contentType - the media type to serve it as
      Returns:
      the response
    • upgrade

      public static Response upgrade(WebSocket client)
      The 101 that completes a WebSocket upgrade.

      Carries the client end of a pair whose server end the Worker kept. A response with a webSocket and any other status is refused by the runtime.

      Parameters:
      client - the client end, from WebSocketPair.client()
      Returns:
      the response
    • json

      public static Response json(String json)
      A 200 carrying already-serialised JSON.
      Parameters:
      json - the JSON text
      Returns:
      the response
    • json

      public static Response json(String json, int status)
      A JSON response with an explicit status.
      Parameters:
      json - the JSON text
      status - the status code
      Returns:
      the response
    • json

      public static <T> Response json(T value, Class<T> type)
      A 200 carrying a Java value as JSON.

      Needs a codec, which the Gradle plugin generates for any type annotated JSONType.

      Type Parameters:
      T - the type
      Parameters:
      value - the value
      type - the value's type
      Returns:
      the response
    • json

      public static Response json(TSObject value)
      A JSON response built from a JavaScript value, for anything with no codec.
      Parameters:
      value - the value
      Returns:
      the response
    • status

      public static Response status(int status)
      An empty response with a status, for the cases where the status is the whole answer.
      Parameters:
      status - the status code
      Returns:
      the response
    • redirect

      public static Response redirect(String location, int status)
      A redirect.
      Parameters:
      location - where to send the client
      status - 301, 302, 307 or 308
      Returns:
      the response
    • request

      public static Request request(String url, String method, String body, TSObject headers)
      Builds a request, for sending somewhere rather than for answering.
      Parameters:
      url - the URL
      method - the HTTP method
      body - the body, or null
      headers - the headers, or null
      Returns:
      the request
    • fetch

      public static Response fetch(String url)
      Sends a request to the internet.

      Counts against the subrequest limit, which is 50 per invocation on the free plan and 10,000 on paid, and against the six simultaneous outgoing connections every plan allows.

      Parameters:
      url - the URL
      Returns:
      the response
    • fetch

      public static Response fetch(Request request)
      Sends a request to the internet.
      Parameters:
      request - the request
      Returns:
      the response
    • fetch

      public static Response fetch(String url, TSObject options)
      Sends a request with options.
      Parameters:
      url - the URL
      options - method, headers, body, cf
      Returns:
      the response
    • headers

      public static Headers headers()
      Returns a header collection nothing has been put in yet.
      Returns:
      a header collection nothing has been put in yet
    • uuid

      public static String uuid()
      Returns a cryptographically random UUID.
      Returns:
      a cryptographically random UUID
    • random

      public static org.teavm.jso.typedarrays.ArrayBuffer random(int bytes)
      Fills a buffer with cryptographically random bytes.
      Parameters:
      bytes - how many
      Returns:
      the bytes
    • log

      public static void log(String message)
      Writes a line to the Worker's log, which wrangler tail shows.
      Parameters:
      message - the message