swarpc - v0.10.0
    Preparing search index...

    Function Server

    • Internal

      Creates a sw&rpc server instance.

      Type Parameters

      Parameters

      • procedures: Procedures

        procedures the server will implement, see ProceduresMap

      • options: {
            scope?: WorkerGlobalScope;
            loglevel?: "debug" | "info" | "warn" | "error";
            _scopeType?: "dedicated" | "shared" | "service";
        } = {}

        various options

        • Optionalscope?: WorkerGlobalScope

          The worker scope to use, defaults to the self of the file where Server() is called.

        • Optionalloglevel?: "debug" | "info" | "warn" | "error"

          Maximum log level to use, defaults to "debug" (shows everything). "info" will not show debug messages, "warn" will only show warnings and errors, "error" will only show errors.

        • Optional_scopeType?: "dedicated" | "shared" | "service"

          Don't touch, this is used in testing environments because the mock is subpar. Manually overrides worker scope type detection.

      Returns SwarpcServer<Procedures>

      a SwarpcServer instance. Each property of the procedures map will be a method, that accepts a function implementing the procedure (see ProcedureImplementation). There is also .start(), to be called after implementing all procedures.

      An example of defining a server:

      import fetchProgress from "fetch-progress"
      import { Server } from "swarpc"
      import { procedures } from "./lib/procedures"

      // 1. Give yourself a server instance
      const swarpc = Server(procedures)

      // 2. Implement your procedures
      swarpc.getClassmapping(async ({ ref, delay }, onProgress, tools) => {
      let aborted = false
      tools.abortSignal?.addEventListener("abort", () => {
      aborted = true
      })

      for (let i = 0; i < delay * 1e3; i += 100) {
      if (!aborted) {
      onProgress({ transferred: i, total: delay * 1e3 })
      await new Promise((resolve) => setTimeout(resolve, 100))
      }
      }

      return fetch(
      `https://raw.githubusercontent.com/cigaleapp/models/${ref}/polymny-17k-classmapping.txt`,
      { signal: tools.abortSignal }
      )
      .then(fetchProgress({ onProgress }))
      .then((response) => response.text())
      .then((text) => text.split("\n"))
      })

      // Start the server
      swarpc.start(self)