network.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.userData = '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. })(Schemas || (Schemas = {}));
  76. class RemoteAuthoritiesImpl {
  77. constructor() {
  78. this._hosts = Object.create(null);
  79. this._ports = Object.create(null);
  80. this._connectionTokens = Object.create(null);
  81. this._preferredWebSchema = 'http';
  82. this._delegate = null;
  83. }
  84. setPreferredWebSchema(schema) {
  85. this._preferredWebSchema = schema;
  86. }
  87. rewrite(uri) {
  88. if (this._delegate) {
  89. return this._delegate(uri);
  90. }
  91. const authority = uri.authority;
  92. let host = this._hosts[authority];
  93. if (host && host.indexOf(':') !== -1) {
  94. host = `[${host}]`;
  95. }
  96. const port = this._ports[authority];
  97. const connectionToken = this._connectionTokens[authority];
  98. let query = `path=${encodeURIComponent(uri.path)}`;
  99. if (typeof connectionToken === 'string') {
  100. query += `&tkn=${encodeURIComponent(connectionToken)}`;
  101. }
  102. return URI.from({
  103. scheme: platform.isWeb ? this._preferredWebSchema : Schemas.vscodeRemoteResource,
  104. authority: `${host}:${port}`,
  105. path: `/vscode-remote-resource`,
  106. query
  107. });
  108. }
  109. }
  110. export const RemoteAuthorities = new RemoteAuthoritiesImpl();
  111. class FileAccessImpl {
  112. asBrowserUri(uriOrModule, moduleIdToUrl) {
  113. const uri = this.toUri(uriOrModule, moduleIdToUrl);
  114. // Handle remote URIs via `RemoteAuthorities`
  115. if (uri.scheme === Schemas.vscodeRemote) {
  116. return RemoteAuthorities.rewrite(uri);
  117. }
  118. // Convert to `vscode-file` resource..
  119. if (
  120. // ...only ever for `file` resources
  121. uri.scheme === Schemas.file &&
  122. (
  123. // ...and we run in native environments
  124. platform.isNative ||
  125. // ...or web worker extensions on desktop
  126. (typeof platform.globals.importScripts === 'function' && platform.globals.origin === `${Schemas.vscodeFileResource}://${FileAccessImpl.FALLBACK_AUTHORITY}`))) {
  127. return uri.with({
  128. scheme: Schemas.vscodeFileResource,
  129. // We need to provide an authority here so that it can serve
  130. // as origin for network and loading matters in chromium.
  131. // If the URI is not coming with an authority already, we
  132. // add our own
  133. authority: uri.authority || FileAccessImpl.FALLBACK_AUTHORITY,
  134. query: null,
  135. fragment: null
  136. });
  137. }
  138. return uri;
  139. }
  140. toUri(uriOrModule, moduleIdToUrl) {
  141. if (URI.isUri(uriOrModule)) {
  142. return uriOrModule;
  143. }
  144. return URI.parse(moduleIdToUrl.toUrl(uriOrModule));
  145. }
  146. }
  147. FileAccessImpl.FALLBACK_AUTHORITY = 'vscode-app';
  148. export const FileAccess = new FileAccessImpl();