|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | + |
| 8 | +export function activate(_context: vscode.ExtensionContext) { |
| 9 | + vscode.workspace.registerRemoteAuthorityResolver('test', { |
| 10 | + async resolve(_authority: string): Promise<vscode.ResolverResult> { |
| 11 | + console.log(`Resolving ${_authority}`); |
| 12 | + return new vscode.ManagedResolvedAuthority(async () => { |
| 13 | + return new InitialManagedMessagePassing(); |
| 14 | + }); |
| 15 | + } |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * The initial message passing is a bit special because we need to |
| 21 | + * wait for the HTTP headers to arrive before we can create the |
| 22 | + * actual WebSocket. |
| 23 | + */ |
| 24 | +class InitialManagedMessagePassing implements vscode.ManagedMessagePassing { |
| 25 | + private readonly dataEmitter = new vscode.EventEmitter<Uint8Array>(); |
| 26 | + private readonly closeEmitter = new vscode.EventEmitter<Error | undefined>(); |
| 27 | + private readonly endEmitter = new vscode.EventEmitter<void>(); |
| 28 | + |
| 29 | + public readonly onDidReceiveMessage = this.dataEmitter.event; |
| 30 | + public readonly onDidClose = this.closeEmitter.event; |
| 31 | + public readonly onDidEnd = this.endEmitter.event; |
| 32 | + |
| 33 | + private _actual: OpeningManagedMessagePassing | null = null; |
| 34 | + private _isDisposed = false; |
| 35 | + |
| 36 | + public send(d: Uint8Array): void { |
| 37 | + if (this._actual) { |
| 38 | + // we already got the HTTP headers |
| 39 | + this._actual.send(d); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (this._isDisposed) { |
| 44 | + // got disposed in the meantime, ignore |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + // we now received the HTTP headers |
| 49 | + const decoder = new TextDecoder(); |
| 50 | + const str = decoder.decode(d); |
| 51 | + |
| 52 | + // example str GET ws://localhost/oss-dev?reconnectionToken=4354a323-a45a-452c-b5d7-d8d586e1cd5c&reconnection=false&skipWebSocketFrames=true HTTP/1.1 |
| 53 | + const match = str.match(/GET\s+(\S+)\s+HTTP/); |
| 54 | + if (!match) { |
| 55 | + console.error(`Coult not parse ${str}`); |
| 56 | + this.closeEmitter.fire(new Error(`Coult not parse ${str}`)); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // example url ws://localhost/oss-dev?reconnectionToken=4354a323-a45a-452c-b5d7-d8d586e1cd5c&reconnection=false&skipWebSocketFrames=true |
| 61 | + const url = new URL(match[1]); |
| 62 | + |
| 63 | + // extract path and query from url using browser's URL |
| 64 | + const parsedUrl = new URL(url); |
| 65 | + this._actual = new OpeningManagedMessagePassing(parsedUrl, this.dataEmitter, this.closeEmitter, this.endEmitter); |
| 66 | + } |
| 67 | + |
| 68 | + public end(): void { |
| 69 | + if (this._actual) { |
| 70 | + this._actual.end(); |
| 71 | + return; |
| 72 | + } |
| 73 | + this._isDisposed = true; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +class OpeningManagedMessagePassing { |
| 78 | + |
| 79 | + private readonly socket: WebSocket; |
| 80 | + private isOpen = false; |
| 81 | + private bufferedData: Uint8Array[] = []; |
| 82 | + |
| 83 | + constructor( |
| 84 | + url: URL, |
| 85 | + dataEmitter: vscode.EventEmitter<Uint8Array>, |
| 86 | + closeEmitter: vscode.EventEmitter<Error | undefined>, |
| 87 | + _endEmitter: vscode.EventEmitter<void> |
| 88 | + ) { |
| 89 | + this.socket = new WebSocket(`ws://localhost:9888${url.pathname}${url.search.replace(/skipWebSocketFrames=true/, 'skipWebSocketFrames=false')}`); |
| 90 | + this.socket.addEventListener('close', () => closeEmitter.fire(undefined)); |
| 91 | + this.socket.addEventListener('error', (e) => closeEmitter.fire(new Error(String(e)))); |
| 92 | + this.socket.addEventListener('message', async (e) => { |
| 93 | + const arrayBuffer = await e.data.arrayBuffer(); |
| 94 | + dataEmitter.fire(new Uint8Array(arrayBuffer)); |
| 95 | + }); |
| 96 | + this.socket.addEventListener('open', () => { |
| 97 | + while (this.bufferedData.length > 0) { |
| 98 | + const first = this.bufferedData.shift()!; |
| 99 | + this.socket.send(first); |
| 100 | + } |
| 101 | + this.isOpen = true; |
| 102 | + |
| 103 | + // https://tools.ietf.org/html/rfc6455#section-4 |
| 104 | + // const requestNonce = req.headers['sec-websocket-key']; |
| 105 | + // const hash = crypto.createHash('sha1'); |
| 106 | + // hash.update(requestNonce + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'); |
| 107 | + // const responseNonce = hash.digest('base64'); |
| 108 | + const responseHeaders = [ |
| 109 | + `HTTP/1.1 101 Switching Protocols`, |
| 110 | + `Upgrade: websocket`, |
| 111 | + `Connection: Upgrade`, |
| 112 | + `Sec-WebSocket-Accept: TODO` |
| 113 | + ]; |
| 114 | + const textEncoder = new TextEncoder(); |
| 115 | + textEncoder.encode(responseHeaders.join('\r\n') + '\r\n\r\n'); |
| 116 | + dataEmitter.fire(textEncoder.encode(responseHeaders.join('\r\n') + '\r\n\r\n')); |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + public send(d: Uint8Array): void { |
| 121 | + if (!this.isOpen) { |
| 122 | + this.bufferedData.push(d); |
| 123 | + return; |
| 124 | + } |
| 125 | + this.socket.send(d); |
| 126 | + } |
| 127 | + |
| 128 | + public end(): void { |
| 129 | + this.socket.close(); |
| 130 | + } |
| 131 | +} |
0 commit comments