mezon-js 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Copyright 2020 The Mezon Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * An interface used by Mezon's web socket to determine the payload protocol.
18
+ */
19
+ export interface WebSocketAdapter {
20
+ /**
21
+ * Dispatched when the web socket closes.
22
+ */
23
+ onClose: SocketCloseHandler | null;
24
+ /**
25
+ * Dispatched when the web socket receives an error.
26
+ */
27
+ onError: SocketErrorHandler | null;
28
+ /**
29
+ * Dispatched when the web socket receives a normal message.
30
+ */
31
+ onMessage: SocketMessageHandler | null;
32
+ /**
33
+ * Dispatched when the web socket opens.
34
+ */
35
+ onOpen: SocketOpenHandler | null;
36
+ isOpen(): boolean;
37
+ close(): void;
38
+ connect(scheme: string, host: string, port: string, createStatus: boolean, token: string): void;
39
+ send(message: any): void;
40
+ }
41
+ /**
42
+ * SocketCloseHandler defines a lambda that handles WebSocket close events.
43
+ */
44
+ export interface SocketCloseHandler {
45
+ (this: WebSocket, evt: CloseEvent): void;
46
+ }
47
+ /**
48
+ * SocketErrorHandler defines a lambda that handles responses from the server via WebSocket
49
+ * that indicate an error.
50
+ */
51
+ export interface SocketErrorHandler {
52
+ (this: WebSocket, evt: Event): void;
53
+ }
54
+ /**
55
+ * SocketMessageHandler defines a lambda that handles valid WebSocket messages.
56
+ */
57
+ export interface SocketMessageHandler {
58
+ (message: any): void;
59
+ }
60
+ /**
61
+ * SocketOpenHandler defines a lambda that handles WebSocket open events.
62
+ */
63
+ export interface SocketOpenHandler {
64
+ (this: WebSocket, evt: Event): void;
65
+ }
66
+ /**
67
+ * A text-based socket adapter that accepts and transmits payloads over UTF-8.
68
+ */
69
+ export declare class WebSocketAdapterText implements WebSocketAdapter {
70
+ private _socket?;
71
+ get onClose(): SocketCloseHandler | null;
72
+ set onClose(value: SocketCloseHandler | null);
73
+ get onError(): SocketErrorHandler | null;
74
+ set onError(value: SocketErrorHandler | null);
75
+ get onMessage(): SocketMessageHandler | null;
76
+ set onMessage(value: SocketMessageHandler | null);
77
+ get onOpen(): SocketOpenHandler | null;
78
+ set onOpen(value: SocketOpenHandler | null);
79
+ isOpen(): boolean;
80
+ connect(scheme: string, host: string, port: string, createStatus: boolean, token: string): void;
81
+ close(): void;
82
+ send(msg: any): void;
83
+ }
package/index.ts ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Copyright 2020 The Mezon Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import "whatwg-fetch";
18
+
19
+ export * from "./client";
20
+ export * from "./session";
21
+ export * from "./socket";
22
+ export * from "./web_socket_adapter";
23
+
24
+ /**
25
+ * Reexported due to duplicate definition of ChannelMessage in [Client]{@link ./client.ts} and [Session]{@link ./session.ts}
26
+ */
27
+ export { ChannelMessage } from "./client";
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "mezon-js",
3
+ "version": "2.7.1",
4
+ "scripts": {
5
+ "build": "npx tsc && npx rollup -c --bundleConfigAsCjs && node build.mjs"
6
+ },
7
+ "description": "JavaScript client for Mezon server written in TypeScript.",
8
+ "main": "dist/mezon-js.cjs.js",
9
+ "module": "dist/mezon-js.esm.mjs",
10
+ "types": "dist/index.d.ts",
11
+ "exports": {
12
+ "./package.json": "./package.json",
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/mezon-js.esm.mjs",
16
+ "require": "./dist/mezon-js.cjs.js"
17
+ }
18
+ },
19
+ "keywords": [
20
+ "app server",
21
+ "client library",
22
+ "game server",
23
+ "mezon",
24
+ "realtime",
25
+ "realtime chat"
26
+ ],
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/heroiclabs/mezon-js.git"
30
+ },
31
+ "homepage": "https://heroiclabs.com",
32
+ "bugs": {
33
+ "url": "https://github.com/heroiclabs/mezon-js/issues"
34
+ },
35
+ "author": "Chris Molozian <chris@heroiclabs.com>",
36
+ "contributors": [
37
+ "Andrei Mihu <andrei@heroiclabs.com>",
38
+ "Mo Firouz <mo@heroiclabs.com>"
39
+ ],
40
+ "license": "Apache-2.0",
41
+ "dependencies": {
42
+ "@scarf/scarf": "^1.1.1",
43
+ "base64-arraybuffer": "^1.0.2",
44
+ "esbuild": "^0.19.11",
45
+ "js-base64": "^3.7.4",
46
+ "whatwg-fetch": "^3.6.2"
47
+ },
48
+ "devDependencies": {
49
+ "@rollup/plugin-node-resolve": "^15.0.1",
50
+ "@rollup/plugin-typescript": "^11.0.0",
51
+ "rollup": "^3.10.0",
52
+ "tslib": "^2.4.1"
53
+ }
54
+ }
@@ -0,0 +1,40 @@
1
+ /*
2
+ * Copyright 2020 Heroic Labs
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ // Rollup is the legacy build system for mezon-js and is only used for cocos2d-x-js support.
18
+
19
+ import typescript from '@rollup/plugin-typescript';
20
+ import { nodeResolve } from '@rollup/plugin-node-resolve';
21
+
22
+ export default {
23
+ input: './index.ts',
24
+ output: {
25
+ format: 'umd',
26
+ name: 'mezonjs',
27
+ dir: "dist",
28
+ entryFileNames: "mezon-js.umd.js" // workaround for TS requirement that dir is specified in config
29
+ },
30
+ plugins: [
31
+ typescript({
32
+ include: ["**/*.ts"],
33
+ target: "es5"
34
+ }),
35
+ nodeResolve()
36
+ ],
37
+ moduleContext: {
38
+ [require.resolve('whatwg-fetch')]: 'window'
39
+ }
40
+ };
package/session.ts ADDED
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Copyright 2022 The Mezon Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+
18
+ import * as base64 from "js-base64"
19
+
20
+ /** A session authenticated for a user with Mezon server. */
21
+ export interface ISession {
22
+ /** Claims */
23
+ /** The authorization token used to construct this session. */
24
+ token: string;
25
+ /** If the user account for this session was just created. */
26
+ created: boolean
27
+ /** The UNIX timestamp when this session was created. */
28
+ readonly created_at: number;
29
+ /** The UNIX timestamp when this session will expire. */
30
+ expires_at?: number;
31
+ /** The UNIX timestamp when the refresh token will expire. */
32
+ refresh_expires_at?: number;
33
+ /** Refresh token that can be used for session token renewal. */
34
+ refresh_token: string;
35
+ /** The username of the user who owns this session. */
36
+ username?: string;
37
+ /** The ID of the user who owns this session. */
38
+ user_id?: string;
39
+ /** Any custom properties associated with this session. */
40
+ vars?: object;
41
+
42
+ /** Validate token */
43
+ /** If the session has expired. */
44
+ isexpired(currenttime: number): boolean;
45
+ /** If the refresh token has expired. */
46
+ isrefreshexpired(currenttime: number): boolean;
47
+ }
48
+
49
+ export class Session implements ISession {
50
+
51
+ token : string;
52
+ readonly created_at: number;
53
+ expires_at?: number;
54
+ refresh_expires_at?: number;
55
+ refresh_token: string;
56
+ username?: string;
57
+ user_id?: string;
58
+ vars?: object;
59
+
60
+ constructor(
61
+ token: string,
62
+ refresh_token: string,
63
+ readonly created: boolean) {
64
+ this.token = token;
65
+ this.refresh_token = refresh_token;
66
+ this.created_at = Math.floor(new Date().getTime() / 1000);
67
+ this.update(token, refresh_token);
68
+ }
69
+
70
+ isexpired(currenttime: number): boolean {
71
+ return (this.expires_at! - currenttime) < 0;
72
+ }
73
+
74
+ isrefreshexpired(currenttime: number): boolean {
75
+ return (this.refresh_expires_at! - currenttime) < 0;
76
+ }
77
+
78
+ update(token: string, refreshToken: string) {
79
+
80
+ const tokenParts = token.split('.');
81
+ if (tokenParts.length != 3) {
82
+ throw 'jwt is not valid.';
83
+ }
84
+
85
+ const tokenDecoded = JSON.parse(base64.atob(tokenParts[1]));
86
+ const tokenExpiresAt = Math.floor(parseInt(tokenDecoded['exp']));
87
+
88
+ /** clients that have just updated to the refresh tokens */
89
+ /** client release will not have a cached refresh token */
90
+ if (refreshToken) {
91
+
92
+ const refreshTokenParts = refreshToken.split('.');
93
+
94
+ if (refreshTokenParts.length != 3) {
95
+ throw 'refresh jwt is not valid.';
96
+ }
97
+
98
+ const refreshTokenDecoded = JSON.parse(base64.atob(refreshTokenParts[1]))
99
+ const refreshTokenExpiresAt = Math.floor(parseInt(refreshTokenDecoded['exp']));
100
+ this.refresh_expires_at = refreshTokenExpiresAt;
101
+ this.refresh_token = refreshToken;
102
+ }
103
+
104
+ this.token = token;
105
+ this.expires_at = tokenExpiresAt;
106
+ this.username = tokenDecoded['usn'];
107
+ this.user_id = tokenDecoded['uid'];
108
+ this.vars = tokenDecoded['vrs'];
109
+ }
110
+
111
+ static restore(token: string, refreshToken: string): Session {
112
+ return new Session(token, refreshToken, false);
113
+ }
114
+ }