From dcdaccf62d22e39377cb92dfa27909002cd081dc Mon Sep 17 00:00:00 2001 From: Chris Jordan Date: Fri, 7 Jul 2023 13:11:33 -0400 Subject: [PATCH] rough state of base branch for new ng-extend --- config/bundle-config.js | 6 +- config/esbuild.js | 22 +++-- package.json | 1 + .../middleauth/credentials_provider.ts | 99 ++++++++++++++----- 4 files changed, 92 insertions(+), 36 deletions(-) diff --git a/config/bundle-config.js b/config/bundle-config.js index f01afe4caf..2ee8be9da5 100644 --- a/config/bundle-config.js +++ b/config/bundle-config.js @@ -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 = [ @@ -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 || []), @@ -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; diff --git a/config/esbuild.js b/config/esbuild.js index eb9423ea60..004511f33d 100644 --- a/config/esbuild.js +++ b/config/esbuild.js @@ -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)) { @@ -56,7 +59,11 @@ 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; @@ -64,7 +71,7 @@ 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 = {}, @@ -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; @@ -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) }', @@ -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'), diff --git a/package.json b/package.json index 14bcc36e11..9cef1adba3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/neuroglancer/datasource/middleauth/credentials_provider.ts b/src/neuroglancer/datasource/middleauth/credentials_provider.ts index bbf148fc0b..e9ca5efcd4 100644 --- a/src/neuroglancer/datasource/middleauth/credentials_provider.ts +++ b/src/neuroglancer/datasource/middleauth/credentials_provider.ts @@ -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 = { @@ -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 { - 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 | undefined, cancellationToken?: CancellationToken | undefined) => { + return this.cachedGet(invalidCredentials, cancellationToken); + } } export class UnverifiedApp extends Error { @@ -143,20 +166,44 @@ export class MiddleAuthAppCredentialsProvider extends CredentialsProvider { + // 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 | undefined, cancellationToken?: CancellationToken | undefined) => { + return this.cachedGet(invalidCredentials, cancellationToken); + } }