Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/extensions/scratch3_mesh_v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const formatMessage = require('format-message');
const log = require('../../util/log');
const {v4: uuidv4} = require('uuid');
const Variable = require('../../engine/variable');
const {getDomainFromUrl} = require('./utils');
const {getDomain, saveDomainToLocalStorage} = require('./utils');
const {createClient} = require('./mesh-client');
const MeshV2Service = require('./mesh-service');

Expand Down Expand Up @@ -52,7 +52,12 @@ class Scratch3MeshV2Blocks {
constructor (runtime) {
log.info('Loading NEW Mesh V2 extension (GraphQL)');
this.runtime = runtime;
this.domain = getDomainFromUrl();
try {
this.domain = getDomain();
} catch (e) {
log.warn(`Mesh V2: Failed to get domain from URL/localStorage: ${e}`);
this.domain = null;
}
this.nodeId = uuidv4().replaceAll('-', '');
this.connectionState = 'disconnected';
this.isExplicitDisconnect = false;
Expand Down Expand Up @@ -80,6 +85,28 @@ class Scratch3MeshV2Blocks {
this.runtime.registerPeripheralExtension(Scratch3MeshV2Blocks.EXTENSION_ID, this);
}

/**
* Set the domain for the mesh service.
* @param {string} domain - The new domain.
* @returns {string|null} - Error message if failed, null if success.
*/
setDomain (domain) {
if (this.connectionState === 'connected' || this.connectionState === 'connecting') {
return formatMessage({
id: 'mesh.domainConnectedAlert',
default: 'Mesh V2 is connected. To change the domain, please disconnect first.',
description: 'Alert message when trying to change domain while connected'
});
}
this.domain = domain;
saveDomainToLocalStorage(domain);
if (this.meshService) {
this.meshService.domain = domain;
}
log.info(`Mesh V2: Domain set to ${domain || 'null (auto)'}`);
return null;
}

makeMeshIdLabel (meshId) {
if (!meshId) return '';
const label = meshId.slice(0, 6);
Expand Down
51 changes: 40 additions & 11 deletions src/extensions/scratch3_mesh_v2/utils.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,62 @@
const log = require('../../util/log');

/* istanbul ignore next */
const getDomainFromUrl = () => {
/* istanbul ignore next */
if (typeof window === 'undefined') return null;
const urlParams = new URLSearchParams(window.location.search);
const domain = urlParams.get('mesh');
const MESH_DOMAIN_STORAGE_KEY = 'mesh_v2_domain';

const validateDomain = domain => {
if (!domain) return null;

if (domain.length > 256) {
/* istanbul ignore next */
log.warn(`Mesh domain too long: ${domain.length} characters (max 256)`);
/* istanbul ignore next */
return null;
}

// Allow alphanumeric, hyphen, underscore, dot.
const validPattern = /^[a-zA-Z0-9-._]+$/;
if (!validPattern.test(domain)) {
/* istanbul ignore next */
log.warn(`Mesh domain contains invalid characters: ${domain}`);
/* istanbul ignore next */
return null;
}

return domain;
};

/* istanbul ignore next */
const getDomainFromUrl = () => {
/* istanbul ignore next */
if (typeof window === 'undefined') return null;
const urlParams = new URLSearchParams(window.location.search);
const domain = urlParams.get('mesh');

return validateDomain(domain);
};

/* istanbul ignore next */
const getDomainFromLocalStorage = () => {
/* istanbul ignore next */
if (typeof window === 'undefined' || !window.localStorage) return null;
const domain = window.localStorage.getItem(MESH_DOMAIN_STORAGE_KEY);

return validateDomain(domain);
};

/* istanbul ignore next */
const saveDomainToLocalStorage = domain => {
/* istanbul ignore next */
if (typeof window === 'undefined' || !window.localStorage) return;
if (domain) {
window.localStorage.setItem(MESH_DOMAIN_STORAGE_KEY, domain);
} else {
window.localStorage.removeItem(MESH_DOMAIN_STORAGE_KEY);
}
};

/* istanbul ignore next */
const getDomain = () => getDomainFromUrl() || getDomainFromLocalStorage();

module.exports = {
getDomainFromUrl
getDomainFromUrl,
getDomainFromLocalStorage,
saveDomainToLocalStorage,
getDomain,
validateDomain
};
56 changes: 56 additions & 0 deletions test/unit/extension_mesh_v2_domain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const test = require('tap').test;
const Scratch3MeshV2Blocks = require('../../src/extensions/scratch3_mesh_v2/index');
const {validateDomain} = require('../../src/extensions/scratch3_mesh_v2/utils');

test('validateDomain', t => {
t.equal(validateDomain('example.com'), 'example.com');
t.equal(validateDomain('my-domain_123.test'), 'my-domain_123.test');
t.equal(validateDomain(''), null);
t.equal(validateDomain(null), null);
t.equal(validateDomain('a'.repeat(257)), null);
t.equal(validateDomain('invalid!char'), null);
t.end();
});

test('setDomain', t => {
const runtime = {
registerPeripheralExtension: () => {},
emit: () => {},
extensionManager: {
isExtensionLoaded: () => false
}
};
const blocks = new Scratch3MeshV2Blocks(runtime);

// Mock localStorage
global.window = {
localStorage: {
getItem: () => null,
setItem: (key, val) => {
t.equal(key, 'mesh_v2_domain');
t.equal(val, 'new-domain');
},
removeItem: key => {
t.equal(key, 'mesh_v2_domain');
}
},
location: {
search: ''
}
};

t.equal(blocks.domain, null);

const err = blocks.setDomain('new-domain');
t.equal(err, null);
t.equal(blocks.domain, 'new-domain');

// Test connected state
blocks.connectionState = 'connected';
const errConnected = blocks.setDomain('another-domain');
t.notEqual(errConnected, null);
t.equal(blocks.domain, 'new-domain'); // Should not change

delete global.window;
t.end();
});
Loading