Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Nov 6, 2018. It is now read-only.

Commit 6058e64

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

4 files changed

+36-11Lines changed: 36 additions & 11 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/extension/extensionHost.ts‎

Copy file name to clipboardExpand all lines: src/extension/extensionHost.ts
+16-2Lines changed: 16 additions & 2 deletions
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
}
Collapse file

‎src/extension/workerMain.ts‎

Copy file name to clipboardExpand all lines: src/extension/workerMain.ts
+8-8Lines changed: 8 additions & 8 deletions
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

Collapse file

‎src/integration-test/helpers.test.ts‎

Copy file name to clipboardExpand all lines: src/integration-test/helpers.test.ts
+4-1Lines changed: 4 additions & 1 deletion
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
Collapse file

‎src/sourcegraph.d.ts‎

Copy file name to clipboardExpand all lines: src/sourcegraph.d.ts
+8Lines changed: 8 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.