network.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. import * as platform from './platform.js';
  6. import { URI } from './uri.js';
  7. export var Schemas;
  8. (function (Schemas) {
  9. /**
  10. * A schema that is used for models that exist in memory
  11. * only and that have no correspondence on a server or such.
  12. */
  13. Schemas.inMemory = 'inmemory';
  14. /**
  15. * A schema that is used for setting files
  16. */
  17. Schemas.vscode = 'vscode';
  18. /**
  19. * A schema that is used for internal private files
  20. */
  21. Schemas.internal = 'private';
  22. /**
  23. * A walk-through document.
  24. */
  25. Schemas.walkThrough = 'walkThrough';
  26. /**
  27. * An embedded code snippet.
  28. */
  29. Schemas.walkThroughSnippet = 'walkThroughSnippet';
  30. Schemas.http = 'http';
  31. Schemas.https = 'https';
  32. Schemas.file = 'file';
  33. Schemas.mailto = 'mailto';
  34. Schemas.untitled = 'untitled';
  35. Schemas.data = 'data';
  36. Schemas.command = 'command';
  37. Schemas.vscodeRemote = 'vscode-remote';
  38. Schemas.vscodeRemoteResource = 'vscode-remote-resource';
  39. Schemas.vscodeUserData = 'vscode-userdata';
  40. Schemas.vscodeCustomEditor = 'vscode-custom-editor';
  41. Schemas.vscodeNotebook = 'vscode-notebook';
  42. Schemas.vscodeNotebookCell = 'vscode-notebook-cell';
  43. Schemas.vscodeNotebookCellMetadata = 'vscode-notebook-cell-metadata';
  44. Schemas.vscodeNotebookCellOutput = 'vscode-notebook-cell-output';
  45. Schemas.vscodeInteractive = 'vscode-interactive';
  46. Schemas.vscodeInteractiveInput = 'vscode-interactive-input';
  47. Schemas.vscodeSettings = 'vscode-settings';
  48. Schemas.vscodeWorkspaceTrust = 'vscode-workspace-trust';
  49. Schemas.vscodeTerminal = 'vscode-terminal';
  50. /**
  51. * Scheme used internally for webviews that aren't linked to a resource (i.e. not custom editors)
  52. */
  53. Schemas.webviewPanel = 'webview-panel';
  54. /**
  55. * Scheme used for loading the wrapper html and script in webviews.
  56. */
  57. Schemas.vscodeWebview = 'vscode-webview';
  58. /**
  59. * Scheme used for extension pages
  60. */
  61. Schemas.extension = 'extension';
  62. /**
  63. * Scheme used as a replacement of `file` scheme to load
  64. * files with our custom protocol handler (desktop only).
  65. */
  66. Schemas.vscodeFileResource = 'vscode-file';
  67. /**
  68. * Scheme used for temporary resources
  69. */
  70. Schemas.tmp = 'tmp';
  71. /**
  72. * Scheme used vs live share
  73. */
  74. Schemas.vsls = 'vsls';
  75. /**
  76. * Scheme used for the Source Control commit input's text document
  77. */
  78. Schemas.vscodeSourceControl = 'vscode-scm';
  79. })(Schemas || (Schemas = {}));
  80. export const connectionTokenQueryName = 'tkn';
  81. class RemoteAuthoritiesImpl {
  82. constructor() {
  83. this._hosts = Object.create(null);
  84. this._ports = Object.create(null);
  85. this._connectionTokens = Object.create(null);
  86. this._preferredWebSchema = 'http';
  87. this._delegate = null;
  88. this._remoteResourcesPath = `/${Schemas.vscodeRemoteResource}`;
  89. }
  90. setPreferredWebSchema(schema) {
  91. this._preferredWebSchema = schema;
  92. }
  93. rewrite(uri) {
  94. if (this._delegate) {
  95. return this._delegate(uri);
  96. }
  97. const authority = uri.authority;
  98. let host = this._hosts[authority];
  99. if (host && host.indexOf(':') !== -1) {
  100. host = `[${host}]`;
  101. }
  102. const port = this._ports[authority];
  103. const connectionToken = this._connectionTokens[authority];
  104. let query = `path=${encodeURIComponent(uri.path)}`;
  105. if (typeof connectionToken === 'string') {
  106. query += `&${connectionTokenQueryName}=${encodeURIComponent(connectionToken)}`;
  107. }
  108. return URI.from({
  109. scheme: platform.isWeb ? this._preferredWebSchema : Schemas.vscodeRemoteResource,
  110. authority: `${host}:${port}`,
  111. path: this._remoteResourcesPath,
  112. query
  113. });
  114. }
  115. }
  116. export const RemoteAuthorities = new RemoteAuthoritiesImpl();
  117. class FileAccessImpl {
  118. asBrowserUri(uriOrModule, moduleIdToUrl) {
  119. const uri = this.toUri(uriOrModule, moduleIdToUrl);
  120. // Handle remote URIs via `RemoteAuthorities`
  121. if (uri.scheme === Schemas.vscodeRemote) {
  122. return RemoteAuthorities.rewrite(uri);
  123. }
  124. // Convert to `vscode-file` resource..
  125. if (
  126. // ...only ever for `file` resources
  127. uri.scheme === Schemas.file &&
  128. (
  129. // ...and we run in native environments
  130. platform.isNative ||
  131. // ...or web worker extensions on desktop
  132. (platform.isWebWorker && platform.globals.origin === `${Schemas.vscodeFileResource}://${FileAccessImpl.FALLBACK_AUTHORITY}`))) {
  133. return uri.with({
  134. scheme: Schemas.vscodeFileResource,
  135. // We need to provide an authority here so that it can serve
  136. // as origin for network and loading matters in chromium.
  137. // If the URI is not coming with an authority already, we
  138. // add our own
  139. authority: uri.authority || FileAccessImpl.FALLBACK_AUTHORITY,
  140. query: null,
  141. fragment: null
  142. });
  143. }
  144. return uri;
  145. }
  146. toUri(uriOrModule, moduleIdToUrl) {
  147. if (URI.isUri(uriOrModule)) {
  148. return uriOrModule;
  149. }
  150. return URI.parse(moduleIdToUrl.toUrl(uriOrModule));
  151. }
  152. }
  153. FileAccessImpl.FALLBACK_AUTHORITY = 'vscode-app';
  154. export const FileAccess = new FileAccessImpl();