(
namespaces: Record<string, DurableObjectNamespace>,
options: {
host: string;
}
)
| 135 | |
| 136 | // create a "multi-party" object that can be used to connect to other parties |
| 137 | function createMultiParties( |
| 138 | namespaces: Record<string, DurableObjectNamespace>, |
| 139 | options: { |
| 140 | host: string; |
| 141 | } |
| 142 | ): Party.Room["context"]["parties"] { |
| 143 | if (!parties) { |
| 144 | parties = {}; |
| 145 | for (const [key, value] of Object.entries(namespaces)) { |
| 146 | if (typeof value.idFromName === "function") { |
| 147 | parties[key] ||= { |
| 148 | get: (name: string): Party.Stub => { |
| 149 | const docId = value.idFromName(name).toString(); |
| 150 | const id = value.idFromString(docId); |
| 151 | const stub = value.get(id); |
| 152 | return { |
| 153 | fetch( |
| 154 | pathOrInit?: string | RequestInit | Request, |
| 155 | maybeInit?: RequestInit | Request |
| 156 | ) { |
| 157 | let path: RequestInfo | undefined; |
| 158 | let init: RequestInit | Request | undefined; |
| 159 | if (pathOrInit) { |
| 160 | if (typeof pathOrInit === "string") { |
| 161 | path = pathOrInit; |
| 162 | init = maybeInit; |
| 163 | if (path[0] !== "/") { |
| 164 | throw new Error("Path must start with /"); |
| 165 | } |
| 166 | return stub.fetch( |
| 167 | `http://${options.host}/parties/${key}/${name}${path}`, |
| 168 | init |
| 169 | ); |
| 170 | } else { |
| 171 | init = pathOrInit; |
| 172 | return stub.fetch( |
| 173 | `http://${options.host}/parties/${key}/${name}`, |
| 174 | init |
| 175 | ); |
| 176 | } |
| 177 | } else { |
| 178 | return stub.fetch( |
| 179 | `http://${options.host}/parties/${key}/${name}` |
| 180 | ); |
| 181 | } |
| 182 | }, |
| 183 | connect: () => { |
| 184 | return new WebSocket( |
| 185 | `ws://${options.host}/parties/${key}/${name}` |
| 186 | ); |
| 187 | }, |
| 188 | async socket( |
| 189 | pathOrInit?: string | RequestInit, |
| 190 | maybeInit?: RequestInit |
| 191 | ) { |
| 192 | let res: Response; |
| 193 | // This method is better because it doesn't go via the internet |
| 194 | // and doesn't get 522/404/ get caught in CF firewall |
no test coverage detected