MCPcopy
hub / github.com/partykit/partykit / createMultiParties

Function createMultiParties

packages/partykit/facade/source.ts:137–252  ·  view source on GitHub ↗
(
  namespaces: Record<string, DurableObjectNamespace>,
  options: {
    host: string;
  }
)

Source from the content-addressed store, hash-verified

135
136// create a "multi-party" object that can be used to connect to other parties
137function 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

Callers 3

#initializePartyMethod · 0.85
fetchFunction · 0.85
scheduledFunction · 0.85

Calls 2

getMethod · 0.65
toStringMethod · 0.45

Tested by

no test coverage detected