@@ -15,8 +15,11 @@ import storage, { defaultStorageItems } from '../../browser/storage'
1515import * as tabs from '../../browser/tabs'
1616import initializeCli from '../../libs/cli'
1717import { resolveClientConfiguration } from '../../shared/backend/server'
18- import { isBackground } from '../../shared/context '
18+ import { ExtensionConnectionInfo , onFirstMessage } from '../../shared/messaging '
1919import { DEFAULT_SOURCEGRAPH_URL , setSourcegraphUrl } from '../../shared/util/context'
20+ import { assertEnv } from '../envAssertion'
21+
22+ assertEnv ( 'BACKGROUND' )
2023
2124let customServerOrigins : string [ ] = [ ]
2225
@@ -439,52 +442,29 @@ const spawnAndConnect = ({
439442 connectPortAndWorker ( port , worker )
440443 } )
441444
442- /**
443- * The information necessary to connect to a Sourcegraph extension.
444- */
445- export interface ExtensionConnectionInfo {
446- extensionID : string
447- jsBundleURL : string
448- }
449-
450- /**
451- * Executes the callback only on the first message that's received on the port.
452- */
453- export const onFirstMessage = ( port : chrome . runtime . Port , callback : ( message : any ) => void ) => {
454- const cb = message => {
455- port . onMessage . removeListener ( cb )
456- callback ( message )
457- }
458- port . onMessage . addListener ( cb )
459- }
460-
461- // This must not execute anywhere but the background script, otherwise messages
462- // will get duplicated and cause all kinds of unintuitive behavior.
463- if ( isBackground ) {
464- // This is the bridge between content scripts (that want to connect to Sourcegraph extensions) and the background
465- // script (that spawns JS bundles or connects to WebSocket endpoints).:
466- chrome . runtime . onConnect . addListener ( port => {
467- // When a content script wants to create a connection to a Sourcegraph extension, it first connects to the
468- // background script on a random port and sends a message containing the platform information for that
469- // Sourcegraph extension (e.g. a JS bundle at localhost:1234/index.js).
470- onFirstMessage ( port , ( connectionInfo : ExtensionConnectionInfo ) => {
471- // The background script receives the message and attempts to spawn the
472- // extension:
473- spawnAndConnect ( { connectionInfo, port } ) . then (
474- // If spawning succeeds, the background script sends {} (so the content script knows it succeeded) and
475- // the port communicates using the internal Sourcegraph extension RPC API after that.
476- ( ) => {
477- // Success is represented by the absence of an error
478- port . postMessage ( { } )
479- } ,
480- // If spawning fails, the background script sends { error } (so the content script knows it failed) and
481- // the port is immediately disconnected. There is always a 1-1 correspondence between ports and content
482- // scripts, so this won't disrupt any other connections.
483- error => {
484- port . postMessage ( { error } )
485- port . disconnect ( )
486- }
487- )
488- } )
445+ // This is the bridge between content scripts (that want to connect to Sourcegraph extensions) and the background
446+ // script (that spawns JS bundles or connects to WebSocket endpoints).:
447+ chrome . runtime . onConnect . addListener ( port => {
448+ // When a content script wants to create a connection to a Sourcegraph extension, it first connects to the
449+ // background script on a random port and sends a message containing the platform information for that
450+ // Sourcegraph extension (e.g. a JS bundle at localhost:1234/index.js).
451+ onFirstMessage ( port , ( connectionInfo : ExtensionConnectionInfo ) => {
452+ // The background script receives the message and attempts to spawn the
453+ // extension:
454+ spawnAndConnect ( { connectionInfo, port } ) . then (
455+ // If spawning succeeds, the background script sends {} (so the content script knows it succeeded) and
456+ // the port communicates using the internal Sourcegraph extension RPC API after that.
457+ ( ) => {
458+ // Success is represented by the absence of an error
459+ port . postMessage ( { } )
460+ } ,
461+ // If spawning fails, the background script sends { error } (so the content script knows it failed) and
462+ // the port is immediately disconnected. There is always a 1-1 correspondence between ports and content
463+ // scripts, so this won't disrupt any other connections.
464+ error => {
465+ port . postMessage ( { error } )
466+ port . disconnect ( )
467+ }
468+ )
489469 } )
490- }
470+ } )
0 commit comments