Skip to content

Commit 26b5c94

Browse files
authored
Merge pull request #19 from kingjosht/develop
added isStandalone to allow running solo running Block Compiler
2 parents 9b67d9c + ac0be09 commit 26b5c94

2 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/lib/storage.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import defaultProject from './default-project';
44

55
// eslint-disable-next-line import/no-commonjs
66
const {TRUSTED_IFRAME_HOST} = require('./brand.js');
7+
const isStandAlone = process.env.IS_STANDLALONE === 'true';
78

89
/**
910
* Wrapper for ScratchStorage which adds default web sources.
@@ -69,30 +70,40 @@ class Storage extends ScratchStorage {
6970
return {accessToken, customAchievements};
7071
}
7172
async loadAccessToken () {
72-
const trustedOrigin = TRUSTED_IFRAME_HOST;
73+
let creds = null;
74+
// eslint-disable-next-line no-negated-condition
75+
if (!isStandAlone) {
76+
const trustedOrigin = TRUSTED_IFRAME_HOST;
7377

74-
window.parent.postMessage({type: 'block-compiler-action', action: 'JWT_AUTH_REQUEST'}, trustedOrigin);
78+
window.parent.postMessage({type: 'block-compiler-action', action: 'JWT_AUTH_REQUEST'}, trustedOrigin);
7579

76-
const creds = await new Promise(resolve => {
80+
creds = await new Promise(resolve => {
7781
// eslint-disable-next-line require-jsdoc, func-style
78-
function handleMessage (event) {
79-
if (event.origin !== trustedOrigin) {
80-
console.warn('Ignored message from untrusted origin:', event.origin);
81-
return;
82-
}
82+
function handleMessage (event) {
83+
if (event.origin !== trustedOrigin) {
84+
console.warn('Ignored message from untrusted origin:', event.origin);
85+
return;
86+
}
8387

84-
if (event.data?.type === 'JWT_AUTH CREDS' && event.data?.token) {
85-
window.removeEventListener('message', handleMessage);
86-
resolve({
87-
token: event.data.token,
88-
username: event.data.username || '',
89-
accessKey: event.data.accessKey || null
90-
});
88+
if (event.data?.type === 'JWT_AUTH CREDS' && event.data?.token) {
89+
window.removeEventListener('message', handleMessage);
90+
resolve({
91+
token: event.data.token,
92+
username: event.data.username || '',
93+
accessKey: event.data.accessKey || null
94+
});
95+
}
9196
}
92-
}
9397

94-
window.addEventListener('message', handleMessage);
95-
});
98+
window.addEventListener('message', handleMessage);
99+
});
100+
} else {
101+
creds = {
102+
token: 'token',
103+
username: '',
104+
accessKey: null
105+
};
106+
}
96107

97108
this.projectToken = creds.token;
98109
this.username = creds.username;

webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ module.exports = [
196196
'process.env.TRUSTED_IFRAME_HOST': JSON.stringify(process.env.TRUSTED_IFRAME_HOST || 'https://codetorch.net'),
197197
'process.env.FORCE_EMBED': JSON.stringify(process.env.FORCE_EMBED || 'true'),
198198
'process.env.COLLABORATION_HOST': JSON.stringify(process.env.COLLABORATION_HOST || 'wss://collaborator.codetorch.net/'),
199-
'process.env.COLLABORATION_DEV_MODE': JSON.stringify(process.env.COLLABORATION_DEV_MODE || 'false')
199+
'process.env.COLLABORATION_DEV_MODE': JSON.stringify(process.env.COLLABORATION_DEV_MODE || 'false'),
200+
'process.env.IS_STANDLALONE': JSON.stringify(process.env.IS_STANDLALONE || 'false')
200201
}),
201202
new HtmlWebpackPlugin({
202203
chunks: ['editor'],

0 commit comments

Comments
 (0)