Skip to content

Commit

Permalink
rough state of base branch for new ng-extend
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisj committed Nov 9, 2023
1 parent 1f34198 commit dcdaccf
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 36 deletions.
6 changes: 3 additions & 3 deletions config/bundle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function getBundleSources(options) {
'global': 'window',
};
let extraDefines = options.defines || {};
let srcDir = resolveReal(__dirname, '..', 'src');
let srcDir = resolveReal(process.cwd(), 'src');
let extraChunkWorkerModules = options.chunkWorkerModules || [];
let extraAsyncComputationModules = options.asyncComputationModules || [];
let chunkWorkerModules = [
Expand Down Expand Up @@ -222,7 +222,7 @@ function getBundleSources(options) {
exports.getBundleSources = getBundleSources;

function makePythonClientOptions(options) {
const srcDir = resolveReal(__dirname, '..', 'src');
const srcDir = resolveReal(process.cwd(), 'src');
options = Object.assign({}, options);
options.extraDataSources = [
...(options.extraDataSources || []),
Expand All @@ -241,7 +241,7 @@ exports.getViewerOptions = function (baseConfig, options = {}) {
baseConfig = makePythonClientOptions(baseConfig);
}
if (options.module) {
const srcDir = resolveReal(__dirname, '..', 'src');
const srcDir = resolveReal(process.cwd(), 'src');
baseConfig.frontendModules = [resolveReal(srcDir, 'main_module.ts')];
}
return baseConfig;
Expand Down
22 changes: 15 additions & 7 deletions config/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ const path = require('path');
const fs = require('fs');
const bundleConfig = require('./bundle-config');
const {spawn} = require('child_process');
const alias = require('esbuild-plugin-alias');
const vuePlugin = require("esbuild-plugin-vue3");


function createEntryPointFile(cacheId, bundleName, sources) {
const tempEntryPointDir =
path.resolve(__dirname, '..', 'node_modules', '.cache', 'esbuild-entry-points', cacheId);
path.resolve(process.cwd(), 'node_modules', '.cache', 'esbuild-entry-points', cacheId);
sources = sources.map(x => {
// Ensure all paths are relative and use forward slashes.
if (path.isAbsolute(x)) {
Expand Down Expand Up @@ -56,15 +59,19 @@ function createEntryPointFile(cacheId, bundleName, sources) {
exports.createEntryPointFile = createEntryPointFile;

function getCommonPlugins() {
return [svgInlineLoader({removeSVGTagAttrs: false, removeTags: true})];
return [
svgInlineLoader({removeSVGTagAttrs: false, removeTags: true}),
alias({'vue': path.resolve(__dirname, '../../vue/dist/vue.esm-bundler.js')}),
vuePlugin(),
];
}
exports.getCommonPlugins = getCommonPlugins;

class Builder {
constructor(options = {}) {
const {id = 'min'} = options;
const {
outDir = path.resolve(__dirname, '..', 'dist', id),
outDir = path.resolve(process.cwd(), 'dist', id),
python = false,
module: moduleBuild = false,
define = {},
Expand All @@ -83,7 +90,7 @@ class Builder {
this.bundleSources = bundleConfig.getBundleSources(viewerConfig);
this.minify = minify;
this.python = options.python;
this.srcDir = path.resolve(__dirname, '..', 'src');
this.srcDir = path.resolve(process.cwd(), 'src');
this.plugins = getCommonPlugins();
this.define = define;
this.inject = inject;
Expand Down Expand Up @@ -151,7 +158,7 @@ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
minify: this.minify,
target: 'es2019',
plugins: this.plugins,
loader: {'.wasm': 'dataurl'},
loader: {'.wasm': 'dataurl', '.png': 'file'},
// TODO(jbms): Remove this workaround once evanw/esbuild#1202 is fixed.
banner: {
js: 'function require(x) { throw new Error(\'Cannot require \' + x) }',
Expand Down Expand Up @@ -186,10 +193,11 @@ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
}

async buildNonModule() {
await this.writeIndex();
// await this.writeIndex();
await fs.promises.copyFile(path.resolve(this.srcDir, 'index.html'), path.resolve(this.outDir, 'index.html'));
if (!this.python) {
await fs.promises.copyFile(
path.resolve(this.srcDir, 'neuroglancer/datasource/boss/bossauth.html'),
path.resolve(this.srcDir, '../third_party/', 'neuroglancer/datasource/boss/bossauth.html'),
path.resolve(this.outDir, 'bossauth.html'));
await fs.promises.copyFile(
path.resolve(this.srcDir, 'neuroglancer/util/google_oauth2_redirect.html'),
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"clang-format": "^1.5.0",
"colors": "^1.4.0",
"connect": "^3.7.0",
"esbuild-plugin-alias": "^0.2.1",
"eslint": "^7.28.0",
"event-stream": "^4.0.1",
"faye-websocket": "^0.11.4",
Expand Down
99 changes: 73 additions & 26 deletions src/neuroglancer/datasource/middleauth/credentials_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import {CredentialsManager, CredentialsProvider, CredentialsWithGeneration, makeCredentialsGetter} from 'neuroglancer/credentials_provider';
import {StatusMessage} from 'neuroglancer/status';
import {verifyObject, verifyObjectProperty, verifyString, verifyStringArray} from 'neuroglancer/util/json';
import {CancellationToken} from 'neuroglancer/util/cancellation';
import {Signal} from 'neuroglancer/util/signal';


export type MiddleAuthToken = {
Expand Down Expand Up @@ -106,27 +108,48 @@ function saveAuthTokenToLocalStorage(authURL: string, value: MiddleAuthToken) {
localStorage.setItem(`${LOCAL_STORAGE_AUTH_KEY}_${authURL}`, JSON.stringify(value));
}

const middleAuthLoginEvent = new Event("middleauthlogin");

export class MiddleAuthCredentialsProvider extends CredentialsProvider<MiddleAuthToken> {
alreadyTriedLocalStorage: Boolean = false;
// alreadyTriedLocalStorage: Boolean = false;

updated = new Signal();

constructor(private serverUrl: string) {
super();
console.log('MiddleAuthCredentialsProvider constructor', serverUrl);
}
get = makeCredentialsGetter(async () => {
let token = undefined;

if (!this.alreadyTriedLocalStorage) {
this.alreadyTriedLocalStorage = true;
token = getAuthTokenFromLocalStorage(this.serverUrl);
}

if (!token) {
token = await waitForLogin(this.serverUrl);
saveAuthTokenToLocalStorage(this.serverUrl, token);
}
private cachedGet = this.updateCachedGet();

updateCachedGet() {
let alreadyTriedLocalStorage = false;
const res = makeCredentialsGetter(async () => {
console.log('MiddleAuthCredentialsProvider get');
let token = undefined;

if (!alreadyTriedLocalStorage) {
alreadyTriedLocalStorage = true;
token = getAuthTokenFromLocalStorage(this.serverUrl);
}

if (!token) {
console.log('requesting login');
token = await waitForLogin(this.serverUrl);
saveAuthTokenToLocalStorage(this.serverUrl, token);
window.dispatchEvent(middleAuthLoginEvent);
}

return token;
});
this.cachedGet = res;
this.updated.dispatch();
return res;
}

return token;
});
get = (invalidCredentials?: CredentialsWithGeneration<MiddleAuthToken> | undefined, cancellationToken?: CancellationToken | undefined) => {
return this.cachedGet(invalidCredentials, cancellationToken);
}
}

export class UnverifiedApp extends Error {
Expand All @@ -143,20 +166,44 @@ export class MiddleAuthAppCredentialsProvider extends CredentialsProvider<Middle

constructor(private serverUrl: string, private credentialsManager: CredentialsManager) {
super();

console.log('MiddleAuthAppCredentialsProvider constructor', serverUrl);

// setInterval(() => {
// console.log('throwing away old credentials')
// this.updateCachedGet();
// }, 10000);
}

get = makeCredentialsGetter(async () => {
const authInfo = await fetch(`${this.serverUrl}/auth_info`).then((res) => res.json());
const provider = this.credentialsManager.getCredentialsProvider('middleauth', authInfo.login_url) as MiddleAuthCredentialsProvider;
updateCachedGet() {
const res = makeCredentialsGetter(async () => {
console.log('MiddleAuthAppCredentialsProvider geta');
const authInfo = await fetch(`${this.serverUrl}/auth_info`).then((res) => res.json());
const provider = this.credentialsManager.getCredentialsProvider('middleauth', authInfo.login_url) as MiddleAuthCredentialsProvider;
// provider.cachedGet = provider.updateCachedGet();

const removeHandler = this.registerDisposer(provider.updated.add(() => {
removeHandler();
this.updateCachedGet();
}));

this.credentials = await provider.get(this.credentials);

if (this.credentials.credentials.appUrls.includes(this.serverUrl)) {
return this.credentials.credentials;
} else {
const status = new StatusMessage(/*delay=*/ false);
status.setText(`middleauth: unverified app ${this.serverUrl}`);
throw new UnverifiedApp(this.serverUrl);
}
});
this.cachedGet = res;
return res;
}

this.credentials = await provider.get(this.credentials);
private cachedGet = this.updateCachedGet();

if (this.credentials.credentials.appUrls.includes(this.serverUrl)) {
return this.credentials.credentials;
} else {
const status = new StatusMessage(/*delay=*/ false);
status.setText(`middleauth: unverified app ${this.serverUrl}`);
throw new UnverifiedApp(this.serverUrl);
}
});
get = (invalidCredentials?: CredentialsWithGeneration<MiddleAuthToken> | undefined, cancellationToken?: CancellationToken | undefined) => {
return this.cachedGet(invalidCredentials, cancellationToken);
}
}

0 comments on commit dcdaccf

Please sign in to comment.