Skip to content
This repository was archived by the owner on Nov 6, 2018. It is now read-only.

Commit 6058e64

Browse files
committed
feat: sourcegraphURL for extensions, passing through init data
1 parent 68094f4 commit 6058e64

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

src/extension/extensionHost.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,37 @@ const consoleLogger: Logger = {
3030
},
3131
}
3232

33+
/**
34+
* Required information when initializing an extension host.
35+
*/
36+
export interface InitData {
37+
/** The URL to the JavaScript source file (that exports an `activate` function) for the extension. */
38+
bundleURL: string
39+
40+
/** @see {@link module:sourcegraph.internal.sourcegraphURL} */
41+
sourcegraphURL: string
42+
}
43+
3344
/**
3445
* Creates the Sourcegraph extension host and the extension API handle (which extensions access with `import
3546
* sourcegraph from 'sourcegraph'`).
3647
*
48+
* @param initData The information to initialize this extension host.
3749
* @param transports The message reader and writer to use for communication with the client. Defaults to
3850
* communicating using self.postMessage and MessageEvents with the parent (assuming that it is
3951
* called in a Web Worker).
4052
* @return The extension API.
4153
*/
4254
export function createExtensionHost(
55+
initData: InitData,
4356
transports: MessageTransports = createWebWorkerMessageTransports()
4457
): typeof sourcegraph {
4558
const connection = createConnection(transports, consoleLogger)
4659
connection.listen()
47-
return createExtensionHandle(connection)
60+
return createExtensionHandle(initData, connection)
4861
}
4962

50-
function createExtensionHandle(connection: Connection): typeof sourcegraph {
63+
function createExtensionHandle(initData: InitData, connection: Connection): typeof sourcegraph {
5164
const subscription = new Subscription()
5265
subscription.add(connection)
5366

@@ -127,6 +140,7 @@ function createExtensionHandle(connection: Connection): typeof sourcegraph {
127140
internal: {
128141
sync,
129142
updateContext: updates => context.updateContext(updates),
143+
sourcegraphURL: new URI(initData.sourcegraphURL),
130144
},
131145
}
132146
}

src/extension/workerMain.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { tryCatchPromise } from '../util'
2-
import { createExtensionHost } from './extensionHost'
2+
import { createExtensionHost, InitData } from './extensionHost'
33

44
interface MessageEvent {
55
data: any
@@ -23,8 +23,8 @@ interface DedicatedWorkerGlobalScope {
2323
/**
2424
* The entrypoint for Web Workers that are spawned to run an extension.
2525
*
26-
* To initialize the worker, the parent sends it a message whose data is a URL of the extension's JavaScript
27-
* bundle.
26+
* To initialize the worker, the parent sends it a message whose data is an object conforming to the
27+
* {@link InitData} interface. Among other things, this contains the URL of the extension's JavaScript bundle.
2828
*
2929
* @param self The worker's `self` global scope.
3030
*/
@@ -40,13 +40,13 @@ export function extensionHostWorkerMain(self: DedicatedWorkerGlobalScope): void
4040
self.close()
4141
}
4242

43-
const bundleURL: string = ev.data
44-
if (typeof bundleURL !== 'string' || !bundleURL.startsWith('blob:')) {
45-
console.error(`Invalid extension bundle URL: ${bundleURL}`)
43+
const initData: InitData = ev.data
44+
if (typeof initData.bundleURL !== 'string' || !initData.bundleURL.startsWith('blob:')) {
45+
console.error(`Invalid extension bundle URL: ${initData.bundleURL}`)
4646
self.close()
4747
}
4848

49-
const api = createExtensionHost()
49+
const api = createExtensionHost(initData)
5050
// Make `import 'sourcegraph'` or `require('sourcegraph')` return the extension host's
5151
// implementation of the `sourcegraph` module.
5252
;(self as any).require = (modulePath: string): any => {
@@ -60,7 +60,7 @@ export function extensionHostWorkerMain(self: DedicatedWorkerGlobalScope): void
6060
// `module` property.
6161
;(self as any).exports = {}
6262
;(self as any).module = {}
63-
self.importScripts(bundleURL)
63+
self.importScripts(initData.bundleURL)
6464
const extensionExports = (self as any).module.exports
6565
delete (self as any).module
6666

src/integration-test/helpers.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ export async function integrationTestContext(): Promise<
4040
// Ack all configuration updates.
4141
clientController.configurationUpdates.subscribe(({ resolve }) => resolve(Promise.resolve()))
4242

43-
const extensionHost = createExtensionHost(serverTransports)
43+
const extensionHost = createExtensionHost(
44+
{ bundleURL: '', sourcegraphURL: 'https://example.com' },
45+
serverTransports
46+
)
4447

4548
// Wait for client to be ready.
4649
await clientController.clientEntries

src/sourcegraph.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,14 @@ declare module 'sourcegraph' {
829829
* @param updates The updates to apply to the context. If a context property's value is null, it is deleted from the context.
830830
*/
831831
export function updateContext(updates: ContextValues): void
832+
833+
/**
834+
* The URL to the Sourcegraph site that the user's session is associated with. This refers to
835+
* Sourcegraph.com (`https://sourcegraph.com`) by default, or a self-hosted instance of Sourcegraph.
836+
*
837+
* @example `https://sourcegraph.com`
838+
*/
839+
export const sourcegraphURL: URI
832840
}
833841

834842
/**

0 commit comments

Comments
 (0)