Skip to content

Commit

Permalink
feat: override fetch to use file service if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Jun 16, 2023
1 parent b521f69 commit 0bc6736
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@
"@octokit/rest": "^19.0.11",
"@rollup/plugin-commonjs": "^25.0.1",
"@rollup/plugin-dynamic-import-vars": "^2.0.3",
"@rollup/plugin-inject": "^5.0.3",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/plugin-replace": "^5.0.2",
Expand Down Expand Up @@ -301,11 +302,11 @@
"vscode-textmate": "^9.0.0",
"xterm": "5.2.1",
"xterm-addon-canvas": "0.4.0",
"xterm-addon-image": "0.4.1",
"xterm-addon-search": "0.12.0",
"xterm-addon-serialize": "0.10.0",
"xterm-addon-unicode11": "0.5.0",
"xterm-addon-webgl": "0.15.0",
"xterm-addon-image": "0.4.1",
"yauzl": "^2.10.0"
},
"overrides": {
Expand Down
5 changes: 5 additions & 0 deletions rollup/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import externalAssets from 'rollup-plugin-external-assets'
import globImport from 'rollup-plugin-glob-import'
import terser from '@rollup/plugin-terser'
import styles from 'rollup-plugin-styles'
import inject from '@rollup/plugin-inject'
import * as fs from 'fs'
import * as path from 'path'
import { fileURLToPath } from 'url'
Expand Down Expand Up @@ -452,6 +453,10 @@ export default (args: Record<string, string>): rollup.RollupOptions[] => {
}],
input,
plugins: [
inject({
fetch: path.resolve('src/custom-fetch.ts'),
include: [/extHostExtensionService/]
}),
commonjs(),
extensionDirectoryPlugin({
include: `${DEFAULT_EXTENSIONS_PATH}/**/*`,
Expand Down
37 changes: 37 additions & 0 deletions src/custom-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { IFileService } from 'vs/platform/files/common/files'
import { StandaloneServices } from 'vs/editor/standalone/browser/standaloneServices'
import { URI } from 'vs/base/common/uri'
import { unsupported } from './tools'

const fetch: typeof window.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
if (typeof input === 'string') {
const uri = URI.parse(input)
const fileService = StandaloneServices.get(IFileService)
if (fileService.hasProvider(uri)) {
const response: Response = {
get headers () { return unsupported() },
ok: true,
redirected: false,
status: 200,
statusText: 'Ok',
type: 'basic',
url: input,
clone: unsupported,
get body () { return unsupported() },
bodyUsed: false,
arrayBuffer: unsupported,
blob: unsupported,
formData: unsupported,
json: unsupported,
text: async function (): Promise<string> {
const content = await fileService.readFile(URI.parse(input))
return content.value.toString()
}
}
return response
}
}
return window.fetch(input, init)
}

export default fetch

0 comments on commit 0bc6736

Please sign in to comment.