Internal
procedures the server will implement, see ProceduresMap
various options
Optional
scope?: WorkerGlobalScopeThe worker scope to use, defaults to the self
of the file where Server() is called.
Optional
loglevel?: "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.
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)
Creates a sw&rpc server instance.