Fix issues with the store on NodeJS

This commit is contained in:
yflory 2025-04-24 10:43:14 +02:00
parent c7d8750bc0
commit 0f74469b52
5 changed files with 61 additions and 10 deletions

7
package-lock.json generated
View File

@ -4189,9 +4189,10 @@
}
},
"node_modules/netflux-websocket": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/netflux-websocket/-/netflux-websocket-1.2.1.tgz",
"integrity": "sha512-8oQOyEyh0MCnifNIfyvkNXpEELlVRzDleRivRgsDGh+5ZDG109axweVZ0RIOIpkQmTZk4/xVsBk/oLXpMSWEOw=="
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/netflux-websocket/-/netflux-websocket-1.2.2.tgz",
"integrity": "sha512-sroG7pBW4QctLtgW55eYNLfzXTbwHBTp7jMXBcY2Dk043PFBL+dpymVgJJWvb2EEbxGFFoaoqc6a8M9w1hIYMQ==",
"license": "LGPL-2.1"
},
"node_modules/node-releases": {
"version": "2.0.18",

View File

@ -36,7 +36,8 @@ getApi('config', ApiConfig => {
Messages,
Broadcast
}).then((store) => {
console.log(store);
console.log('API added to global "CP"');
globalThis.CP = store.api;
});
});
});

View File

@ -14,7 +14,7 @@ const factory = (ApiConfig = {}) => {
var path = ApiConfig.websocketPath || '/cryptpad_websocket';
if (/^ws{1,2}:\/\//.test(path)) { return path; }
var l = new URL(origin || self?.location?.href);
var l = new URL(origin || globalThis?.location?.href || ApiConfig.httpUnsafeOrigin);
if (origin) {
l.href = origin;
}

View File

@ -3,12 +3,53 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
(() => {
const factory = function () {
const factory = function (Channel, NodeWS) {
let USE_MIN = false;
let path = '/common/worker.bundle.js?';
if (USE_MIN) { path = '/common/worker.bundle.min.js?'; }
const commands = {
pad: {
join: 'JOIN_PAD',
leave: 'LEAVE_PAD',
sendMsg: 'SEND_PAD_MSG',
destroy: 'REMOVE_OWNED_CHANNEL',
setMetadata: 'SET_PAD_METADATA',
getMetadata: 'GET_PAD_METADATA'
}
};
const makeApi = (postMsg, msgEv, cb) => {
Channel.create(msgEv, postMsg, chan => {
const postMessage = (cmd, data, cb, opts) => {
cb ||= () => {};
chan.query(cmd, data, (err, res) => {
if (err) { return void cb ({error: err}); }
cb(res);
}, opts);
};
const api = {};
const make = (base, cmd) => {
Object.keys(cmd).forEach(k => {
const v = cmd[k];
if (!v) { return; }
if (typeof(v) === "string") {
base[k] = (data, cb, opts) => {
postMessage(v, data, cb, opts);
};
return;
}
base[k] = {};
make(base[k], v);
});
};
make(api, commands);
cb(api);
});
};
let create = function (cfg = {}) {
let { noWorker, noSharedWorker, AppConfig,
ApiConfig, Messages, Broadcast } = cfg;
@ -138,7 +179,12 @@ const factory = function () {
Messages,
Broadcast
});
resolve({postMsg, msgEv});
globalThis.WebSocket = NodeWS.WebSocket;
makeApi(postMsg, msgEv, api => {
resolve({api});
});
};
return new Promise((resolve, reject) => {
@ -182,8 +228,11 @@ const factory = function () {
if (typeof(module) !== 'undefined' && module.exports) {
module.exports = factory();
module.exports = factory(
require('../../src/common/events-channel'),
require('ws')
);
} else if ((typeof(define) !== 'undefined' && define !== null) && (define.amd !== null)) {
define([], factory);
define(['/common/events-channel.js'], factory);
}
})();

File diff suppressed because one or more lines are too long