network.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. Schemas.webviewPanel = 'webview-panel';
  51. /**
  52. * Scheme used for loading the wrapper html and script in webviews.
  53. */
  54. Schemas.vscodeWebview = 'vscode-webview';
  55. /**
  56. * Scheme used for extension pages
  57. */
  58. Schemas.extension = 'extension';
  59. /**
  60. * Scheme used as a replacement of `file` scheme to load
  61. * files with our custom protocol handler (desktop only).
  62. */
  63. Schemas.vscodeFileResource = 'vscode-file';
  64. /**
  65. * Scheme used for temporary resources
  66. */
  67. Schemas.tmp = 'tmp';
  68. })(Schemas || (Schemas = {}));
  69. class RemoteAuthoritiesImpl {
  70. constructor() {
  71. this._hosts = Object.create(null);
  72. this._ports = Object.create(null);
  73. this._connectionTokens = Object.create(null);
  74. this._preferredWebSchema = 'http';
  75. this._delegate = null;
  76. }
  77. setPreferredWebSchema(schema) {
  78. this._preferredWebSchema = schema;
  79. }
  80. rewrite(uri) {
  81. if (this._delegate) {
  82. return this._delegate(uri);
  83. }
  84. const authority = uri.authority;
  85. let host = this._hosts[authority];
  86. if (host && host.indexOf(':') !== -1) {
  87. host = `[${host}]`;
  88. }
  89. const port = this._ports[authority];
  90. const connectionToken = this._connectionTokens[authority];
  91. let query = `path=${encodeURIComponent(uri.path)}`;
  92. if (typeof connectionToken === 'string') {
  93. query += `&tkn=${encodeURIComponent(connectionToken)}`;
  94. }
  95. return URI.from({
  96. scheme: platform.isWeb ? this._preferredWebSchema : Schemas.vscodeRemoteResource,
  97. authority: `${host}:${port}`,
  98. path: `/vscode-remote-resource`,
  99. query
  100. });
  101. }
  102. }
  103. export const RemoteAuthorities = new RemoteAuthoritiesImpl();
  104. class FileAccessImpl {
  105. asBrowserUri(uriOrModule, moduleIdToUrl) {
  106. const uri = this.toUri(uriOrModule, moduleIdToUrl);
  107. // Handle remote URIs via `RemoteAuthorities`
  108. if (uri.scheme === Schemas.vscodeRemote) {
  109. return RemoteAuthorities.rewrite(uri);
  110. }
  111. // Convert to `vscode-file` resource..
  112. if (
  113. // ...only ever for `file` resources
  114. uri.scheme === Schemas.file &&
  115. (
  116. // ...and we run in native environments
  117. platform.isNative ||
  118. // ...or web worker extensions on desktop
  119. (typeof platform.globals.importScripts === 'function' && platform.globals.origin === `${Schemas.vscodeFileResource}://${FileAccessImpl.FALLBACK_AUTHORITY}`))) {
  120. return uri.with({
  121. scheme: Schemas.vscodeFileResource,
  122. // We need to provide an authority here so that it can serve
  123. // as origin for network and loading matters in chromium.
  124. // If the URI is not coming with an authority already, we
  125. // add our own
  126. authority: uri.authority || FileAccessImpl.FALLBACK_AUTHORITY,
  127. query: null,
  128. fragment: null
  129. });
  130. }
  131. return uri;
  132. }
  133. toUri(uriOrModule, moduleIdToUrl) {
  134. if (URI.isUri(uriOrModule)) {
  135. return uriOrModule;
  136. }
  137. return URI.parse(moduleIdToUrl.toUrl(uriOrModule));
  138. }
  139. }
  140. FileAccessImpl.FALLBACK_AUTHORITY = 'vscode-app';
  141. export const FileAccess = new FileAccessImpl();