@cocreate/usage 1.2.1 → 1.3.0

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.
@@ -1,8 +1,10 @@
1
1
  name: Automated Workflow
2
+
2
3
  on:
3
4
  push:
4
5
  branches:
5
6
  - master
7
+
6
8
  jobs:
7
9
  about:
8
10
  runs-on: ubuntu-latest
@@ -18,52 +20,37 @@ jobs:
18
20
  with:
19
21
  direction: overwrite-github
20
22
  githubToken: "${{ secrets.GITHUB }}"
23
+
21
24
  release:
22
25
  runs-on: ubuntu-latest
23
26
  steps:
24
27
  - name: Checkout
25
- uses: actions/checkout@v3
26
- - name: Setup Node.js
27
- uses: actions/setup-node@v3
28
+ uses: actions/checkout@v4
28
29
  with:
29
- node-version: 14
30
- - name: Semantic Release
31
- uses: cycjimmy/semantic-release-action@v3
32
- id: semantic
30
+ fetch-depth: 0 # Required so semantic-release can trace git tags/history
31
+
32
+ - name: Setup Node.js
33
+ uses: actions/setup-node@v4
33
34
  with:
34
- extra_plugins: |
35
- @semantic-release/changelog
35
+ node-version: 22
36
+
37
+ - name: Install Semantic Release & Plugins
38
+ # Installs semantic-release and its plugins on the runner
39
+ run: |
40
+ npm install -g semantic-release \
41
+ @semantic-release/changelog \
42
+ @semantic-release/npm \
43
+ @semantic-release/github \
36
44
  @semantic-release/git
37
- @semantic-release/github
45
+
46
+ - name: Run Semantic Release (Native)
47
+ id: semantic
48
+ # This will automatically pick up your export default config file in the repository root
49
+ run: npx semantic-release
38
50
  env:
39
- GITHUB_TOKEN: "${{ secrets.GITHUB }}"
51
+ GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
40
52
  NPM_TOKEN: "${{ secrets.NPM_TOKEN }}"
53
+
41
54
  outputs:
42
55
  new_release_published: "${{ steps.semantic.outputs.new_release_published }}"
43
- new_release_version: "${{ steps.semantic.outputs.new_release_version }}"
44
- upload:
45
- runs-on: ubuntu-latest
46
- needs: release
47
- if: needs.release.outputs.new_release_published == 'true'
48
- env:
49
- VERSION: "${{ needs.release.outputs.new_release_version }}"
50
- steps:
51
- - name: Checkout
52
- uses: actions/checkout@v3
53
- - name: Setup Node.js
54
- uses: actions/setup-node@v3
55
- with:
56
- node-version: 16
57
- - name: Set npm registry auth
58
- run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc
59
- - name: Install dependencies
60
- run: yarn install
61
-
62
- - name: Set Environment Variables
63
- run: |
64
- echo "organization_id=${{ secrets.COCREATE_ORGANIZATION_ID }}" >> $GITHUB_ENV
65
- echo "key=${{ secrets.COCREATE_KEY }}" >> $GITHUB_ENV
66
- echo "host=${{ secrets.COCREATE_HOST }}" >> $GITHUB_ENV
67
- - name: CoCreate Upload
68
- run: coc upload
69
-
56
+ new_release_version: "${{ steps.semantic.outputs.new_release_version }}"
package/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
1
+ # [1.3.0](https://github.com/CoCreate-app/CoCreate-usage/compare/v1.2.2...v1.3.0) (2026-07-17)
2
+
3
+
4
+ ### Features
5
+
6
+ * update automated workflow and release configuration for improved semantic release handling ([47aa8c2](https://github.com/CoCreate-app/CoCreate-usage/commit/47aa8c2941f8393a0ca55cbf6f66ae4df1172dea))
7
+
8
+ ## [1.2.2](https://github.com/CoCreate-app/CoCreate-usage/compare/v1.2.1...v1.2.2) (2026-07-14)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * remove unnecessary console logs in init function for cleaner output ([af5f039](https://github.com/CoCreate-app/CoCreate-usage/commit/af5f03958fa5e84008411e99186bcdea438e7087))
14
+ * removed post install ([933f2b6](https://github.com/CoCreate-app/CoCreate-usage/commit/933f2b6b9a7a551afb7e2723bfd02de9a72f2019))
15
+ * semantic version handling. Reorganize .gitignore for improved clarity and structure ([9d67031](https://github.com/CoCreate-app/CoCreate-usage/commit/9d67031f6144a5b767ff0a2b6d487b14cbbd33aa))
16
+ * update module export to ES6 syntax in release.config.js ([8908cb7](https://github.com/CoCreate-app/CoCreate-usage/commit/8908cb7ea61ceb65d1151909e8706c0a54046903))
17
+ * update organization ID reference and host variable in sendUsageUpdate function ([4f4cc11](https://github.com/CoCreate-app/CoCreate-usage/commit/4f4cc11f9d0757b27c71f180ce83895fb2c436cd))
18
+ * update package.json to use ESM and restructure entry points ([c96c5db](https://github.com/CoCreate-app/CoCreate-usage/commit/c96c5db22a621d2af2a151d2173ffaa265c03f6e))
19
+
1
20
  ## [1.2.1](https://github.com/CoCreate-app/CoCreate-usage/compare/v1.2.0...v1.2.1) (2025-05-01)
2
21
 
3
22
 
package/package.json CHANGED
@@ -1,18 +1,10 @@
1
1
  {
2
2
  "name": "@cocreate/usage",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "CoCreate-usage",
5
5
  "keywords": [
6
6
  "cocreate-usage",
7
- "cocreate",
8
- "low-code-framework",
9
- "no-code-framework",
10
- "cocreatejs",
11
- "cocreatejs-component",
12
- "cocreate-framework",
13
- "no-code",
14
7
  "low-code",
15
- "collaborative-framework",
16
8
  "realtime",
17
9
  "realtime-framework",
18
10
  "collaboration",
@@ -20,6 +12,7 @@
20
12
  "html5-framework",
21
13
  "javascript-framework"
22
14
  ],
15
+ "type": "module",
23
16
  "main": "./src/index.js",
24
17
  "publishConfig": {
25
18
  "access": "public"
@@ -36,7 +29,6 @@
36
29
  "homepage": "https://cocreate.app/docs/CoCreate-usage",
37
30
  "scripts": {
38
31
  "demo": "PORT=5000 node demo/server.js",
39
- "test": "echo \"Error: no test specified\" && exit 1",
40
- "postinstall": "node -e \"const { execSync } = require('child_process'); try { execSync('coc --version', { stdio: 'ignore' }); } catch (error) { try { execSync('npm install -g @cocreate/cli', { stdio: 'inherit' }); console.log('Installed \"@cocreate/cli\" globally.'); } catch (error) { console.error('Failed to install \"@cocreate/cli\" globally:', error); } }\""
32
+ "test": "echo \"Error: no test specified\" && exit 1"
41
33
  }
42
34
  }
package/release.config.js CHANGED
@@ -1,4 +1,4 @@
1
- module.exports = {
1
+ export default {
2
2
  dryRun: false,
3
3
  branches: ["master"],
4
4
  plugins: [
@@ -10,12 +10,20 @@ module.exports = {
10
10
  changelogFile: "CHANGELOG.md",
11
11
  },
12
12
  ],
13
- "@semantic-release/npm",
14
- "@semantic-release/github",
13
+ "@semantic-release/npm",
15
14
  [
16
- "@semantic-release/git",
15
+ "@semantic-release/github",
17
16
  {
17
+ successComment: false,
18
+ failTitle: false,
19
+ },
20
+ ],
21
+ [
22
+ "@semantic-release/git",
23
+ {
24
+ // Only stage and commit the changelog and package.json
18
25
  assets: ["CHANGELOG.md", "package.json"],
26
+ message: "chore(release): ${nextRelease.version} [skip ci]",
19
27
  },
20
28
  ],
21
29
  ],
package/src/index.js CHANGED
@@ -1,208 +1,244 @@
1
- class CoCreateUsage {
2
- constructor(crud) {
3
- this.wsManager = crud.wsManager;
4
- this.crud = crud;
5
- this.init();
6
- this.organizations = new Map()
7
- }
8
-
9
- init() {
10
- if (this.wsManager) {
11
- this.wsManager.on('setBandwidth', (data) => this.setBandwidth(data));
12
- }
13
- }
1
+ /********************************************************************************
2
+ * Copyright (C) 2023 CoCreate and Contributors.
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify
5
+ * it under the terms of the GNU Affero General Public License as published
6
+ * by the Free Software Foundation, either version 3 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU Affero General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Affero General Public License
15
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ ********************************************************************************/
18
+
19
+ // Commercial Licensing Information:
20
+ // For commercial use of this software without the copyleft provisions of the AGPLv3,
21
+ // you must obtain a commercial license from CoCreate LLC.
22
+ // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
23
+
24
+ import server from '@cocreate/server';
25
+
26
+ // ==========================================
27
+ // Module-Level Sandbox State (ESM Singleton Container)
28
+ // ==========================================
29
+ const organizations = new Map();
30
+
31
+ /**
32
+ * Normalizes input data types and calculates payload sizing in bytes.
33
+ */
34
+ function getBytes(data) {
35
+ if (typeof data === 'number')
36
+ return data;
37
+ else if (data instanceof Buffer)
38
+ return data.byteLength || 0;
39
+ else if (data instanceof String || typeof data === 'string')
40
+ return Buffer.byteLength(data, 'utf8') || 0;
41
+ else if (typeof data === 'object' && data !== null)
42
+ return Buffer.byteLength(JSON.stringify(data), 'utf8') || 0;
43
+ else return 0;
44
+ }
14
45
 
46
+ /**
47
+ * Determines pricing tier rates based on aggregate historical usage indicators.
48
+ */
49
+ function getRate(totalUsage = 0) {
50
+ const tiers = [
51
+ { limit: 1, rate: 10 },
52
+ { limit: 10, rate: 5 },
53
+ { limit: 100, rate: 2.5 },
54
+ { limit: 1000, rate: 1.125 },
55
+ { limit: 10000, rate: 0.562 },
56
+ { limit: 100000, rate: 0.281 },
57
+ { limit: 1000000, rate: 0.145 }
58
+ ];
59
+
60
+ const matchingTier = tiers.find(tier => totalUsage < tier.limit);
61
+ return matchingTier ? matchingTier.rate : 0.12;
62
+ }
15
63
 
16
- async setBandwidth({ type, data, organization_id }) {
17
- try {
18
- let dataTransfered = this.getBytes(data)
19
-
20
- if (!dataTransfered || !organization_id)
21
- return
22
- // TODO: set duration
23
- let org = this.organizations.get(organization_id)
24
- if (!org) {
25
- let self = this
26
-
27
- org = {}
28
- org.debounce = setTimeout(() => {
29
- self.send(org, organization_id, data.host); // Call the callback to display the count
30
- self.organizations.delete(organization_id)
31
- }, 60000);
32
-
33
- org.dataTransferedIn = 0;
34
- org.dataTransferedInCount = 0;
35
- org.dataTransferedOut = 0;
36
- org.dataTransferedOutCount = 0;
37
-
38
- this.organizations.set(organization_id, org)
39
- }
40
-
41
- if (type === 'in') {
42
- org.dataTransferedIn = dataTransfered;
43
- org.dataTransferedInCount++
44
- } else if (type === 'out') {
45
- org.dataTransferedOut = dataTransfered;
46
- org.dataTransferedOutCount++
47
- } else {
48
- console.log('else')
49
- }
50
-
51
- } catch (error) {
52
- console.log('Usage error', error)
64
+ /**
65
+ * Calculates and writes final bandwidth depletion transactions to the platform database.
66
+ */
67
+ async function sendUsageUpdate(org, organization_id, host) {
68
+ if (!server || !server.crud) return;
69
+ delete org.debounce;
70
+
71
+ org.dataTransferedOut += 250;
72
+ org.dataTransferedOutCount++;
73
+
74
+ const platformOrganization = server.organization_id;
75
+ let organization = await server.crud.send({
76
+ method: 'object.read',
77
+ // host: server.host,
78
+ array: 'organizations',
79
+ object: { _id: organization_id },
80
+ organization_id: platformOrganization
81
+ });
82
+
83
+ org.dataTransferedIn += getBytes(platformOrganization);
84
+ org.dataTransferedInCount++;
85
+
86
+ if (organization && organization.object && organization.object[0]) {
87
+ organization = organization.object[0];
88
+ } else return;
89
+
90
+ if (server.wsManager && server.wsManager.organizations.has(organization_id)) {
91
+ const socketOrg = server.wsManager.organizations.get(organization_id);
92
+ if (organization.balance <= 0) {
93
+ socketOrg.status = false;
94
+ socketOrg.organizationBalance = false;
95
+ socketOrg.error = 'Your balance has fallen below 0';
96
+ } else {
97
+ socketOrg.status = true;
98
+ socketOrg.organizationBalance = true;
99
+ socketOrg.error = '';
53
100
  }
54
101
  }
55
102
 
103
+ let timeStamp = new Date(new Date().toISOString());
104
+ let isExpired = false;
105
+ if (organization.lastDeposit) {
106
+ let lastDeposit = new Date(organization.lastDeposit);
107
+ isExpired = lastDeposit <= timeStamp.setFullYear(timeStamp.getFullYear() - 1);
108
+ }
56
109
 
57
- async send(org, organization_id, host) {
58
- delete org.debounce
59
-
60
- org.dataTransferedOut += 250
61
- org.dataTransferedOutCount++
62
-
63
- const platformOrganization = this.crud.config.organization_id
64
- let organization = await this.crud.send({
65
- method: 'object.read',
66
- host: this.crud.config.host,
67
- array: 'organizations',
68
- object: { _id: organization_id },
69
- organization_id: platformOrganization
70
- })
71
-
72
- org.dataTransferedIn += this.getBytes(platformOrganization)
73
- org.dataTransferedInCount++
74
-
75
- if (organization && organization.object && organization.object[0]) {
76
- organization = organization.object[0]
77
- } else return
78
-
79
- //TODO: if (organization.transactionInterval) // set in global map to use with timeout 1 sec intervals to 3600 sec
80
- if (this.wsManager.organizations.has(organization_id)) {
81
- if (organization.balance <= 0) {
82
- this.wsManager.organizations.get(organization_id).status = false
83
- this.wsManager.organizations.get(organization_id).organizationBalance = false
84
- this.wsManager.organizations.get(organization_id).error = 'Your balance has fallen bellow 0'
85
-
86
- } else {
87
- this.wsManager.organizations.get(organization_id).status = true
88
- this.wsManager.organizations.get(organization_id).organizationBalance = true
89
- this.wsManager.organizations.get(organization_id).error = ''
90
- }
91
- }
92
-
93
- let timeStamp = new Date(new Date().toISOString());
94
- let isExpired = false
95
- if (organization.lastDeposit) {
96
- let lastDeposit = new Date(organization.lastDeposit)
97
- isExpired = lastDeposit <= timeStamp.setFullYear(timeStamp.getFullYear() - 1)
110
+ let isResetDataTransfer = false;
111
+ if (organization.modified && organization.modified.on) {
112
+ let previousTimeStamp = new Date(organization.modified.on);
113
+ if (previousTimeStamp.getMonth() !== timeStamp.getMonth()) {
114
+ isResetDataTransfer = true;
115
+ server.crud.send({
116
+ method: 'object.create',
117
+ // host: server.host,
118
+ array: 'transactions',
119
+ object: {
120
+ organization_id,
121
+ type: "withdrawal",
122
+ dataTransfered: organization.dataTransfered,
123
+ previousTimeStamp
124
+ },
125
+ organization_id: platformOrganization,
126
+ timeStamp
127
+ });
128
+ organization.dataTransfered = 0;
98
129
  }
130
+ }
99
131
 
100
- let isResetDataTransfer = false
101
- if (organization.modified && organization.modified.on) {
102
- let previousTimeStamp = new Date(organization.modified.on)
103
- if (previousTimeStamp.getMonth() !== timeStamp.getMonth()) {
104
- isResetDataTransfer = true
105
- this.crud.send({
106
- method: 'object.create',
107
- host: this.crud.config.host,
108
- array: 'transactions',
109
- object: {
110
- organization_id,
111
- type: "withdrawal", // deposit, credit, withdrawal, debit
112
- dataTransfered: organization.dataTransfered,
113
- previousTimeStamp
114
- },
115
- organization_id: platformOrganization,
116
- timeStamp
117
- });
118
- organization.dataTransfered = 0
119
- }
120
- }
132
+ let dataTransfered = org.dataTransferedIn + org.dataTransferedOut;
133
+ dataTransfered = dataTransfered / 1073741824;
134
+ dataTransfered = dataTransfered.toFixed(32);
135
+ dataTransfered = parseFloat(dataTransfered);
136
+
137
+ let rate = getRate(organization.dataTransfered);
138
+ let amount = dataTransfered * rate;
139
+ amount = -amount;
140
+ amount = amount.toFixed(32);
141
+ amount = parseFloat(amount);
142
+
143
+ let balanceUpdate = {
144
+ method: 'object.update',
145
+ // host: server.host,
146
+ array: 'organizations',
147
+ object: { _id: organization_id },
148
+ organization_id: platformOrganization,
149
+ timeStamp
150
+ };
151
+
152
+ if (isExpired)
153
+ balanceUpdate.object['balance'] = 0;
154
+ else
155
+ balanceUpdate.object.$inc = { balance: amount };
156
+
157
+ if (isResetDataTransfer)
158
+ balanceUpdate.object['dataTransfered'] = 0;
159
+ else if (!balanceUpdate.object.$inc)
160
+ balanceUpdate.object.$inc = { dataTransfered };
161
+ else
162
+ balanceUpdate.object.$inc.dataTransfered = dataTransfered;
163
+
164
+ server.crud.send(balanceUpdate);
165
+
166
+ let transaction = {
167
+ method: 'object.create',
168
+ host,
169
+ array: 'transactions',
170
+ object: {
171
+ organization_id,
172
+ type: "withdrawal",
173
+ amount,
174
+ rate,
175
+ dataTransfered,
176
+ ...org
177
+ },
178
+ organization_id,
179
+ timeStamp
180
+ };
181
+
182
+ server.crud.send(transaction);
183
+ }
121
184
 
122
- let dataTransfered = org.dataTransferedIn + org.dataTransferedOut
123
- dataTransfered = dataTransfered / 1073741824;
124
- dataTransfered = dataTransfered.toFixed(32);
125
- dataTransfered = parseFloat(dataTransfered);
126
-
127
- let rate = this.getRate(organization.dataTransfered)
128
- let amount = dataTransfered * rate
129
- amount = -amount
130
- amount = amount.toFixed(32)
131
- amount = parseFloat(amount)
132
-
133
- let balanceUpdate = {
134
- method: 'object.update',
135
- host: this.crud.config.host,
136
- array: 'organizations',
137
- object: { _id: organization_id },
138
- organization_id: platformOrganization,
139
- timeStamp
185
+ /**
186
+ * Event-driven handler that accumulates payload footprints and manages write debouncing.
187
+ */
188
+ async function setBandwidth({ type, data, organization_id }) {
189
+ try {
190
+ let dataTransfered = getBytes(data);
191
+
192
+ if (!dataTransfered || !organization_id)
193
+ return;
194
+
195
+ let org = organizations.get(organization_id);
196
+ if (!org) {
197
+ org = {};
198
+ org.debounce = setTimeout(() => {
199
+ sendUsageUpdate(org, organization_id, data.host);
200
+ organizations.delete(organization_id);
201
+ }, 60000);
202
+
203
+ org.dataTransferedIn = 0;
204
+ org.dataTransferedInCount = 0;
205
+ org.dataTransferedOut = 0;
206
+ org.dataTransferedOutCount = 0;
207
+
208
+ organizations.set(organization_id, org);
140
209
  }
141
210
 
142
- if (isExpired)
143
- balanceUpdate.object['balance'] = 0
144
- else
145
- balanceUpdate.object.$inc = { balance: amount }
146
-
147
- if (isResetDataTransfer)
148
- balanceUpdate.object['dataTransfered'] = 0
149
- else if (!balanceUpdate.object.$inc)
150
- balanceUpdate.object.$inc = { dataTransfered }
151
- else
152
- balanceUpdate.object.$inc.dataTransfered = dataTransfered
153
-
154
- // console.log('balanceUpdate: ', balanceUpdate)
155
-
156
- this.crud.send(balanceUpdate)
157
-
158
- let transaction = {
159
- method: 'object.create',
160
- host,
161
- array: 'transactions',
162
- object: {
163
- organization_id,
164
- type: "withdrawal", // deposit, credit, withdrawal, debit
165
- amount,
166
- rate,
167
- dataTransfered,
168
- ...org
169
- },
170
- organization_id,
171
- timeStamp
211
+ if (type === 'in') {
212
+ org.dataTransferedIn = dataTransfered;
213
+ org.dataTransferedInCount++;
214
+ } else if (type === 'out') {
215
+ org.dataTransferedOut = dataTransfered;
216
+ org.dataTransferedOutCount++;
217
+ } else {
218
+ console.log('else');
172
219
  }
173
220
 
174
- this.crud.send(transaction);
175
-
176
- }
177
-
178
- getBytes(data) {
179
- if (typeof data === 'number')
180
- return data;
181
- else if (data instanceof Buffer)
182
- return data.byteLength || 0;
183
- else if (data instanceof String || typeof data === 'string')
184
- return Buffer.byteLength(data, 'utf8') || 0;
185
- else if (typeof data === 'object')
186
- return Buffer.byteLength(JSON.stringify(data), 'utf8') || 0;
187
- else return 0
221
+ } catch (error) {
222
+ console.log('Usage error', error);
188
223
  }
224
+ }
189
225
 
190
- getRate(totalUsage = 0) {
191
- const tiers = [
192
- { limit: 1, rate: 10 },
193
- { limit: 10, rate: 5 },
194
- { limit: 100, rate: 2.5 },
195
- { limit: 1000, rate: 1.125 },
196
- { limit: 10000, rate: 0.562 },
197
- { limit: 100000, rate: 0.281 },
198
- { limit: 1000000, rate: 0.145 }
199
- ];
200
-
201
- const matchingTier = tiers.find(tier => totalUsage < tier.limit);
202
-
203
- return matchingTier ? matchingTier.rate : 0.12;
226
+ /**
227
+ * Auto-initialization loop.
228
+ * Silently polls to verify that the unified server orchestrator is fully loaded,
229
+ * and that both the database (crud) and socket manager (wsManager) have completed
230
+ * their initial handshakes, then attaches active bandwidth monitoring triggers.
231
+ */
232
+ function init() {
233
+ if (server && server.isInitialized && server.crud && server.wsManager) {
234
+ server.wsManager.on('setBandwidth', (data) => setBandwidth(data));
235
+ } else {
236
+ // If the server and WebSocket managers are not yet ready, retry in 500ms
237
+ setTimeout(init, 500);
204
238
  }
205
-
206
239
  }
207
240
 
208
- module.exports = CoCreateUsage;
241
+ // Auto-execute initialization upon module load
242
+ init();
243
+
244
+ export default init;