From 6cf9e0b42a4b3e85b9c5e12d4f91187637c4e911 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 17 Apr 2024 17:39:56 +0100 Subject: [PATCH] fix: close realtime sockets --- .travis.yml | 4 ++-- README.md | 2 +- package.json | 2 +- src/client.ts | 8 ++++++-- src/id.ts | 27 +++++++++++++++++++++++---- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index aae2399..564159b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ node_js: jobs: include: - stage: NPM RC Release - if: tag == *-rc* + if: tag =~ /-(rc|RC)/ node_js: "14.16" script: - npm install @@ -17,7 +17,7 @@ jobs: api_key: $NPM_API_KEY tag: next - stage: NPM Release - if: tag != *-rc* + if: not tag =~ /-(rc|RC)/ node_js: "14.16" script: - npm install diff --git a/README.md b/README.md index 3877566..28273f5 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ import { Client, Account } from "appwrite"; To install with a CDN (content delivery network) add the following scripts to the bottom of your tag, but before you use any Appwrite services: ```html - + ``` diff --git a/package.json b/package.json index 4ad4a3c..9929e7d 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "appwrite", "homepage": "https://appwrite.io/support", "description": "Appwrite is an open-source self-hosted backend server that abstract and simplify complex and repetitive development tasks behind a very simple REST API", - "version": "14.0.0", + "version": "14.0.1", "license": "BSD-3-Clause", "main": "dist/cjs/sdk.js", "exports": { diff --git a/src/client.ts b/src/client.ts index 39cead4..5c1df24 100644 --- a/src/client.ts +++ b/src/client.ts @@ -103,7 +103,7 @@ class Client { 'x-sdk-name': 'Web', 'x-sdk-platform': 'client', 'x-sdk-language': 'web', - 'x-sdk-version': '14.0.0', + 'x-sdk-version': '14.0.1', 'X-Appwrite-Response-Format': '1.5.0', }; @@ -224,7 +224,11 @@ class Client { } }, createSocket: () => { - if (this.realtime.channels.size < 1) return; + if (this.realtime.channels.size < 1) { + this.realtime.reconnect = false; + this.realtime.socket?.close(); + return; + } const channels = new URLSearchParams(); channels.set('project', this.config.project); diff --git a/src/id.ts b/src/id.ts index 9577e4b..199f00a 100644 --- a/src/id.ts +++ b/src/id.ts @@ -1,9 +1,28 @@ export class ID { + // Generate an hex ID based on timestamp + // Recreated from https://www.php.net/manual/en/function.uniqid.php + static #hexTimestamp(): string { + const now = new Date(); + const sec = Math.floor(now.getTime() / 1000); + const msec = now.getMilliseconds(); + + // Convert to hexadecimal + const hexTimestamp = sec.toString(16) + msec.toString(16).padStart(5, '0'); + return hexTimestamp; + } + public static custom(id: string): string { return id } - - public static unique(): string { - return 'unique()' + + public static unique(padding: number = 7): string { + // Generate a unique ID with padding to have a longer ID + const baseId = ID.#hexTimestamp(); + let randomPadding = ''; + for (let i = 0; i < padding; i++) { + const randomHexDigit = Math.floor(Math.random() * 16).toString(16); + randomPadding += randomHexDigit; + } + return baseId + randomPadding; } -} \ No newline at end of file +}