@colyseus/sdk 0.17.1 → 0.17.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/build/cjs/3rd_party/discord.js +1 -1
  2. package/build/cjs/Auth.js +1 -1
  3. package/build/cjs/Client.js +1 -1
  4. package/build/cjs/Connection.js +1 -1
  5. package/build/cjs/HTTP.js +1 -1
  6. package/build/cjs/Protocol.js +1 -1
  7. package/build/cjs/Room.js +1 -1
  8. package/build/cjs/Storage.js +1 -1
  9. package/build/cjs/core/nanoevents.js +1 -1
  10. package/build/cjs/core/signal.js +1 -1
  11. package/build/cjs/core/utils.js +1 -1
  12. package/build/cjs/errors/Errors.js +1 -1
  13. package/build/cjs/index.js +1 -1
  14. package/build/cjs/legacy.js +1 -1
  15. package/build/cjs/serializer/NoneSerializer.js +1 -1
  16. package/build/cjs/serializer/SchemaSerializer.js +1 -1
  17. package/build/cjs/serializer/Serializer.js +1 -1
  18. package/build/cjs/transport/H3Transport.js +1 -1
  19. package/build/cjs/transport/WebSocketTransport.js +1 -1
  20. package/build/esm/3rd_party/discord.mjs +1 -1
  21. package/build/esm/Auth.mjs +1 -1
  22. package/build/esm/Client.mjs +1 -1
  23. package/build/esm/Connection.mjs +1 -1
  24. package/build/esm/HTTP.mjs +1 -1
  25. package/build/esm/Protocol.mjs +1 -1
  26. package/build/esm/Room.mjs +1 -1
  27. package/build/esm/Storage.mjs +1 -1
  28. package/build/esm/core/nanoevents.mjs +1 -1
  29. package/build/esm/core/signal.mjs +1 -1
  30. package/build/esm/core/utils.mjs +1 -1
  31. package/build/esm/errors/Errors.mjs +1 -1
  32. package/build/esm/index.mjs +1 -1
  33. package/build/esm/legacy.mjs +1 -1
  34. package/build/esm/serializer/NoneSerializer.mjs +1 -1
  35. package/build/esm/serializer/SchemaSerializer.mjs +1 -1
  36. package/build/esm/serializer/Serializer.mjs +1 -1
  37. package/build/esm/transport/H3Transport.mjs +1 -1
  38. package/build/esm/transport/WebSocketTransport.mjs +1 -1
  39. package/dist/colyseus-cocos-creator.js +1 -1
  40. package/dist/colyseus.js +1 -1
  41. package/dist/debug.js +1 -1
  42. package/lib/core/http_bkp.d.ts +10 -10
  43. package/package.json +7 -6
  44. package/src/3rd_party/discord.ts +48 -0
  45. package/src/Auth.ts +177 -0
  46. package/src/Client.ts +459 -0
  47. package/src/Connection.ts +51 -0
  48. package/src/HTTP.ts +545 -0
  49. package/src/HTTP_bkp.ts +67 -0
  50. package/src/Protocol.ts +25 -0
  51. package/src/Room.ts +505 -0
  52. package/src/Storage.ts +94 -0
  53. package/src/core/http_bkp.ts +358 -0
  54. package/src/core/nanoevents.ts +38 -0
  55. package/src/core/signal.ts +62 -0
  56. package/src/core/utils.ts +3 -0
  57. package/src/debug.ts +2743 -0
  58. package/src/errors/Errors.ts +29 -0
  59. package/src/index.ts +18 -0
  60. package/src/legacy.ts +29 -0
  61. package/src/serializer/FossilDeltaSerializer.ts +39 -0
  62. package/src/serializer/NoneSerializer.ts +9 -0
  63. package/src/serializer/SchemaSerializer.ts +61 -0
  64. package/src/serializer/Serializer.ts +23 -0
  65. package/src/transport/H3Transport.ts +199 -0
  66. package/src/transport/ITransport.ts +18 -0
  67. package/src/transport/WebSocketTransport.ts +53 -0
@@ -0,0 +1,358 @@
1
+ import type { Router, HasRequiredKeys, Prettify, UnionToIntersection, Endpoint, HTTPMethod } from "@colyseus/better-call";
2
+
3
+ type HasRequired<
4
+ T extends {
5
+ body?: any;
6
+ query?: any;
7
+ params?: any;
8
+ },
9
+ > = HasRequiredKeys<T["body"]> extends true
10
+ ? true
11
+ : HasRequiredKeys<T["query"]> extends true
12
+ ? true
13
+ : HasRequiredKeys<T["params"]> extends true
14
+ ? true
15
+ : false;
16
+
17
+ type InferContext<T> = T extends (ctx: infer Ctx) => any
18
+ ? Ctx extends object
19
+ ? Ctx
20
+ : never
21
+ : never;
22
+
23
+ type WithRequired<T, K> = T & {
24
+ [P in K extends string ? K : never]-?: T[P extends keyof T ? P : never];
25
+ };
26
+
27
+ type WithoutServerOnly<T extends Record<string, Endpoint>> = {
28
+ [K in keyof T]: T[K] extends Endpoint<any, infer O>
29
+ ? O extends { metadata: { SERVER_ONLY: true } }
30
+ ? never
31
+ : T[K]
32
+ : T[K];
33
+ };
34
+
35
+ // Method-specific options type
36
+ type MethodOptions<API, M extends HTTPMethod> = API extends { [key: string]: infer T; }
37
+ ? T extends Endpoint<any, infer O>
38
+ ? O["method"] extends M
39
+ ? { [key in T["path"]]: T; }
40
+ : O["method"] extends M[]
41
+ ? M extends O["method"][number]
42
+ ? { [key in T["path"]]: T; }
43
+ : {}
44
+ : O["method"] extends "*"
45
+ ? { [key in T["path"]]: T; }
46
+ : {}
47
+ : {}
48
+ : {};
49
+
50
+ export type RequiredOptionKeys<
51
+ C extends {
52
+ body?: any;
53
+ query?: any;
54
+ params?: any;
55
+ },
56
+ > = (undefined extends C["body"]
57
+ ? {}
58
+ : {
59
+ body: true;
60
+ }) &
61
+ (undefined extends C["query"]
62
+ ? {}
63
+ : {
64
+ query: true;
65
+ }) &
66
+ (undefined extends C["params"]
67
+ ? {}
68
+ : {
69
+ params: true;
70
+ });
71
+
72
+
73
+ export interface ClientOptions extends FetchRequestOptions {
74
+ baseURL: string;
75
+ }
76
+
77
+ type CommonHeaders = {
78
+ accept: "application/json" | "text/plain" | "application/octet-stream";
79
+ "content-type": "application/json" | "text/plain" | "application/x-www-form-urlencoded" | "multipart/form-data" | "application/octet-stream";
80
+ authorization: "Bearer" | "Basic";
81
+ };
82
+
83
+ type FetchRequestOptions<
84
+ Body = any,
85
+ Query extends Record<string, any> = any,
86
+ Params extends Record<string, any> | Array<string> | undefined = any, Res = any,
87
+ ExtraOptions extends Record<string, any> = {}
88
+ > = Prettify<ExtraOptions & Omit<RequestInit, "body"> & {
89
+ baseURL?: string;
90
+
91
+ /**
92
+ * Headers
93
+ */
94
+ headers?: CommonHeaders | Headers | HeadersInit;
95
+
96
+ /**
97
+ * Body
98
+ */
99
+ body?: Body;
100
+
101
+ /**
102
+ * Query parameters (key-value pairs)
103
+ */
104
+ query?: Query;
105
+
106
+ /**
107
+ * Dynamic parameters.
108
+ *
109
+ * If url is defined as /path/:id, params will be { id: string }
110
+ */
111
+ params?: Params;
112
+ }>
113
+
114
+ type ResponseData<T> = {
115
+ ok: true;
116
+ data: T;
117
+ error: null,
118
+ response: Response;
119
+ headers: Headers;
120
+ status: number;
121
+ statusText: string;
122
+ };
123
+
124
+ type ResponseError<E> = {
125
+ ok: false,
126
+ data: null,
127
+ error: Prettify<(E extends Record<string, any> ? E : {
128
+ message?: string;
129
+ }) & {
130
+ code?: string;
131
+ }>;
132
+ response: Response;
133
+ headers: Headers;
134
+ status: number;
135
+ statusText: string;
136
+ };
137
+
138
+ type FetchResponse<T, E extends Record<string, unknown> | unknown = unknown, Throw extends boolean = false> =
139
+ Throw extends true
140
+ ? T
141
+ : ResponseData<T> | ResponseError<E>;
142
+
143
+ // type dd = BetterFetchOption;
144
+
145
+ export function isJSONSerializable(value: any) {
146
+ if (value === undefined) {
147
+ return false;
148
+ }
149
+ const t = typeof value;
150
+ if (t === "string" || t === "number" || t === "boolean" || t === null) {
151
+ return true;
152
+ }
153
+ if (t !== "object") {
154
+ return false;
155
+ }
156
+ if (Array.isArray(value)) {
157
+ return true;
158
+ }
159
+ if (value.buffer) {
160
+ return false;
161
+ }
162
+ return (
163
+ (value.constructor && value.constructor.name === "Object") ||
164
+ typeof value.toJSON === "function"
165
+ );
166
+ }
167
+
168
+ function getBody(body: any, options?: FetchRequestOptions) {
169
+ if (!body) { return null; }
170
+
171
+ const headers = new Headers(options?.headers);
172
+ if (isJSONSerializable(body) && !headers.has("content-type")) {
173
+ options?.headers
174
+ return JSON.stringify(body);
175
+ }
176
+
177
+ return body;
178
+ }
179
+
180
+ const JSON_RE = /^application\/(?:[\w!#$%&*.^`~-]*\+)?json(;.+)?$/i;
181
+
182
+ export type ResponseType = "json" | "text" | "blob";
183
+ export function detectResponseType(request: Response): ResponseType {
184
+ const _contentType = request.headers.get("content-type");
185
+ const textTypes = new Set([
186
+ "image/svg",
187
+ "application/xml",
188
+ "application/xhtml",
189
+ "application/html",
190
+ ]);
191
+ if (!_contentType) {
192
+ return "json";
193
+ }
194
+ const contentType = _contentType.split(";").shift() || "";
195
+ if (JSON_RE.test(contentType)) {
196
+ return "json";
197
+ }
198
+ if (textTypes.has(contentType) || contentType.startsWith("text/")) {
199
+ return "text";
200
+ }
201
+ return "blob";
202
+ }
203
+
204
+ function getURL(url: string, option?: FetchRequestOptions) {
205
+ let { baseURL, params, query } = option || {
206
+ query: {},
207
+ params: {},
208
+ baseURL: "",
209
+ };
210
+ let basePath = url.startsWith("http")
211
+ ? url.split("/").slice(0, 3).join("/")
212
+ : baseURL || "";
213
+
214
+ if (!basePath.endsWith("/")) basePath += "/";
215
+ let [path, urlQuery] = url.replace(basePath, "").split("?");
216
+ const queryParams = new URLSearchParams(urlQuery);
217
+ for (const [key, value] of Object.entries(query || {})) {
218
+ if (value == null) continue;
219
+ queryParams.set(key, String(value));
220
+ }
221
+ if (params) {
222
+ if (Array.isArray(params)) {
223
+ const paramPaths = path.split("/").filter((p) => p.startsWith(":"));
224
+ for (const [index, key] of paramPaths.entries()) {
225
+ const value = params[index];
226
+ path = path.replace(key, value);
227
+ }
228
+ } else {
229
+ for (const [key, value] of Object.entries(params)) {
230
+ path = path.replace(`:${key}`, String(value));
231
+ }
232
+ }
233
+ }
234
+
235
+ path = path.split("/").map(encodeURIComponent).join("/");
236
+ if (path.startsWith("/")) path = path.slice(1);
237
+ let queryParamString = queryParams.toString();
238
+ queryParamString =
239
+ queryParamString.length > 0 ? `?${queryParamString}`.replace(/\+/g, "%20") : "";
240
+ if (!basePath.startsWith("http")) {
241
+ return `${basePath}${path}${queryParamString}`;
242
+ }
243
+ return new URL(`${path}${queryParamString}`, basePath);
244
+ }
245
+
246
+ export const createClient = <R extends Router | Router["endpoints"]>(baseOptions: ClientOptions) => {
247
+ type API = WithoutServerOnly<
248
+ R extends { endpoints: Record<string, Endpoint> }
249
+ ? R["endpoints"]
250
+ : R
251
+ >;
252
+
253
+ function createVerbMethod<M extends HTTPMethod>(method: M) {
254
+ type O = Prettify<UnionToIntersection<MethodOptions<API, M>>>;
255
+
256
+ return async <OPT extends O, K extends keyof OPT, C extends InferContext<OPT[K]>>(
257
+ path: K,
258
+ ...options: HasRequired<C> extends true
259
+ ? [
260
+ WithRequired<
261
+ FetchRequestOptions<C["body"], C["query"], C["params"]>,
262
+ keyof RequiredOptionKeys<C>
263
+ >,
264
+ ]
265
+ : [FetchRequestOptions<C["body"], C["query"], C["params"]>?]
266
+ ): Promise<
267
+ FetchResponse<Awaited<ReturnType<OPT[K] extends Endpoint ? OPT[K] : never>>>
268
+ > => {
269
+ //
270
+ // FIXME: if FormData is provided, merging "baseOptions.body" with
271
+ // "options.body" will not work as intended
272
+ //
273
+ let body = (baseOptions.body)
274
+ ? { ...baseOptions.body, ...(options[0]?.body as object || {}) }
275
+ : options[0]?.body;
276
+
277
+ const query = (baseOptions.query)
278
+ ? { ...baseOptions.query, ...(options[0]?.query as object || {}) }
279
+ : options[0]?.query;
280
+
281
+ const params = (baseOptions.params)
282
+ ? { ...baseOptions.params, ...(options[0]?.params as object || {}) }
283
+ : options[0]?.params;
284
+
285
+ const headers = new Headers(
286
+ (baseOptions.headers)
287
+ ? { ...baseOptions.headers, ...(options[0]?.headers || {}) }
288
+ : options[0]?.headers
289
+ );
290
+
291
+ if (isJSONSerializable(body) && !headers.has("content-type")) {
292
+ headers.set("content-type", "application/json");
293
+ for (const [key, value] of Object.entries(body)) {
294
+ if (value instanceof Date) {
295
+ body[key] = value.toISOString();
296
+ }
297
+ }
298
+ body = JSON.stringify(body);
299
+ }
300
+
301
+ const mergedOptions = {
302
+ credentials: options[0]?.credentials || "include",
303
+ ...baseOptions,
304
+ ...options[0],
305
+ query,
306
+ params,
307
+ headers,
308
+ body,
309
+ method,
310
+ };
311
+
312
+ mergedOptions.body = getBody(body, mergedOptions);
313
+
314
+ const url = getURL(path.toString(), mergedOptions);
315
+
316
+ const response = await fetch(url, mergedOptions);
317
+ const contentType = response.headers.get("content-type");
318
+
319
+ let data: any;
320
+ let error = null;
321
+
322
+ // TODO: improve content-type detection here!
323
+ if (contentType?.indexOf("json")) {
324
+ data = await response.json();
325
+
326
+ } else if (contentType?.indexOf("text")) {
327
+ data = await response.text();
328
+
329
+ } else {
330
+ data = await response.blob();
331
+ }
332
+
333
+ if (!response.ok) {
334
+ // TODO: throw error here?!
335
+ error = data;
336
+ data = null;
337
+ }
338
+
339
+ return {
340
+ ok: response.ok,
341
+ headers: response.headers,
342
+ data,
343
+ error,
344
+ status: response.status,
345
+ statusText: response.statusText,
346
+ response,
347
+ } as any;
348
+ };
349
+ };
350
+
351
+ return {
352
+ get: createVerbMethod("GET"),
353
+ post: createVerbMethod("POST"),
354
+ delete: createVerbMethod("DELETE"),
355
+ patch: createVerbMethod("PATCH"),
356
+ put: createVerbMethod("PUT"),
357
+ };
358
+ };
@@ -0,0 +1,38 @@
1
+ /**
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright 2016 Andrey Sitnik <andrey@sitnik.ru>
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
7
+ * this software and associated documentation files (the "Software"), to deal in
8
+ * the Software without restriction, including without limitation the rights to
9
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10
+ * the Software, and to permit persons to whom the Software is furnished to do so,
11
+ * subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ export const createNanoEvents = () => ({
25
+ emit(event: string, ...args: any[]) {
26
+ let callbacks = this.events[event] || []
27
+ for (let i = 0, length = callbacks.length; i < length; i++) {
28
+ callbacks[i](...args)
29
+ }
30
+ },
31
+ events: {},
32
+ on(event: string, cb: (...args: any[]) => void) {
33
+ this.events[event]?.push(cb) || (this.events[event] = [cb])
34
+ return () => {
35
+ this.events[event] = this.events[event]?.filter(i => cb !== i)
36
+ }
37
+ }
38
+ });
@@ -0,0 +1,62 @@
1
+ type FunctionParameters<T extends (...args: any[]) => any> =
2
+ T extends (...args: infer P) => any
3
+ ? P
4
+ : never;
5
+
6
+ export class EventEmitter<CallbackSignature extends (...args: any[]) => any> {
7
+ handlers: Array<CallbackSignature> = [];
8
+
9
+ register(cb: CallbackSignature, once: boolean = false) {
10
+ this.handlers.push(cb);
11
+ return this;
12
+ }
13
+
14
+ invoke(...args: FunctionParameters<CallbackSignature>) {
15
+ this.handlers.forEach((handler) => handler.apply(this, args));
16
+ }
17
+
18
+ invokeAsync(...args: FunctionParameters<CallbackSignature>) {
19
+ return Promise.all(this.handlers.map((handler) => handler.apply(this, args)));
20
+ }
21
+
22
+ remove (cb: CallbackSignature) {
23
+ const index = this.handlers.indexOf(cb);
24
+ this.handlers[index] = this.handlers[this.handlers.length - 1];
25
+ this.handlers.pop();
26
+ }
27
+
28
+ clear() {
29
+ this.handlers = [];
30
+ }
31
+ }
32
+
33
+ export function createSignal<CallbackSignature extends (...args: any[]) => void | Promise<any>>()
34
+ :
35
+ {
36
+ once: (cb: CallbackSignature) => void;
37
+ remove: (cb: CallbackSignature) => void,
38
+ invoke: (...args: FunctionParameters<CallbackSignature>) => void,
39
+ invokeAsync: (...args: FunctionParameters<CallbackSignature>) => Promise<any[]>,
40
+ clear: () => void,
41
+ } & ((this: any, cb: CallbackSignature) => EventEmitter<CallbackSignature> )
42
+ {
43
+ const emitter = new EventEmitter<CallbackSignature>();
44
+
45
+ function register(this: any, cb: CallbackSignature): EventEmitter<CallbackSignature> {
46
+ return emitter.register(cb, this === null);
47
+ };
48
+
49
+ register.once = (cb: CallbackSignature) => {
50
+ const callback: any = function (...args: any[]) {
51
+ cb.apply(this, args);
52
+ emitter.remove(callback);
53
+ }
54
+ emitter.register(callback);
55
+ }
56
+ register.remove = (cb: CallbackSignature) => emitter.remove(cb)
57
+ register.invoke = (...args: FunctionParameters<CallbackSignature>) => emitter.invoke(...args);
58
+ register.invokeAsync = (...args: FunctionParameters<CallbackSignature>) => emitter.invokeAsync(...args);
59
+ register.clear = () => emitter.clear();
60
+
61
+ return register;
62
+ }
@@ -0,0 +1,3 @@
1
+ export function now(): number {
2
+ return typeof(performance) !== 'undefined' ? performance.now() : Date.now();
3
+ }