Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve registered file system #434

Merged
merged 7 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion demo/src/setup.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ while (variable < 5000) {
}`
))

fileSystemProvider.registerFile(new RegisteredReadOnlyFile(vscode.Uri.file('/workspace/test_readonly.js'), async () => 'This is a readonly static file'))
const content = new TextEncoder().encode('This is a readonly static file')
fileSystemProvider.registerFile(new RegisteredReadOnlyFile(vscode.Uri.file('/workspace/test_readonly.js'), async () => content, content.length))

fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/workspace/jsconfig.json'), `{
"compilerOptions": {
Expand Down
282 changes: 134 additions & 148 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@
"@types/vscode-semver": "npm:@types/semver@=5.5.0",
"@types/yargs": "^17.0.32",
"@types/yauzl": "^2.10.3",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/parser": "^7.10.0",
"@web/rollup-plugin-import-meta-assets": "^2.2.1",
"change-package-name": "^1.0.5",
"eslint": "^8.57.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^17.6.0",
"eslint-plugin-n": "^17.7.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-unused-imports": "^3.2.0",
"fast-glob": "^3.3.2",
Expand All @@ -83,9 +83,9 @@
"pascal-case": "^3.1.2",
"patch-package": "^8.0.0",
"recast": "^0.23.7",
"rollup": "^4.17.2",
"rollup": "^4.18.0",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-styles": "^4.0.0",
"semantic-release": "23.1.1",
"semver": "^7.6.2",
Expand All @@ -96,7 +96,7 @@
"vscode-marked": "npm:marked@=3.0.2",
"vscode-semver": "npm:semver@=5.5.0",
"yargs": "^17.7.2",
"zx": "^8.0.2"
"zx": "^8.1.0"
},
"dependencies": {
"@microsoft/1ds-core-js": "^3.2.13",
Expand Down
4 changes: 2 additions & 2 deletions rollup/rollup.default-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export default rollup.defineConfig([
output: [{
minifyInternalExports: false,
assetFileNames: chunkInfo => {
if (chunkInfo.name != null && chunkInfo.name.endsWith('d.ts')) {
// append .txt at the end of d.ts files: those file are required by the typescript extension and are just expected to be loaded as simple text
if (chunkInfo.name != null && (chunkInfo.name.endsWith('d.ts') || chunkInfo.name.endsWith('.css'))) {
// append .txt at the end of d.ts and css files: those file are expected to be loaded as simple text while some bundlers like vite tries to transform them
return 'resources/[name][extname].txt'
}
return 'resources/[name][extname]'
Expand Down
21 changes: 15 additions & 6 deletions src/extension-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ export interface ExtensionResource {
path: string
extensionPaths: string[]
mimeType?: string
size: number
}

const plainTextExtensions = [
'.d.ts'
]

function lookupMime (path: string) {
if (plainTextExtensions.some(e => path.endsWith(e))) {
return 'text/plain'
}
const mimeType = mime.lookup(path)
if (mimeType === false) {
return undefined
Expand All @@ -31,11 +39,12 @@ function getPaths (filePath: string) {
return paths
}

export function toResource (path: string): ExtensionResource {
export function toResource (resourcePath: string, fs: typeof nodeFs, cwd: string): ExtensionResource {
return {
path,
extensionPaths: getPaths(path),
mimeType: lookupMime(path)
path: resourcePath,
extensionPaths: getPaths(resourcePath),
mimeType: lookupMime(resourcePath),
size: fs.statSync(path.resolve(cwd, resourcePath)).size
}
}

Expand Down Expand Up @@ -150,9 +159,9 @@ export async function getExtensionResources (manifest: IExtensionManifest, fs: t
fs: <FileSystemAdapter>fs,
cwd,
onlyFiles: true
})).map(toResource)
})).map(p => toResource(p, fs, cwd))
} else {
return (await extractResourcesFromExtensionManifest(manifest, fs, cwd)).map(toResource)
return (await extractResourcesFromExtensionManifest(manifest, fs, cwd)).map(p => toResource(p, fs, cwd))
}
}

Expand Down
28 changes: 9 additions & 19 deletions src/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { StandaloneServices } from 'vs/editor/standalone/browser/standaloneServi
import { ExtensionManifestTranslator, NlsConfiguration } from 'vs/platform/extensionManagement/common/extensionsScannerService'
import * as platform from 'vs/base/common/platform'
import { IExtensionWithExtHostKind, ExtensionServiceOverride } from './service-override/extensions'
import { CustomSchemas, registerExtensionFile } from './service-override/files'
import { CustomSchemas, ExtensionFileMetadata, RegisteredUriFile, registerExtensionFile } from './service-override/files'
import { waitServicesReady } from './lifecycle'
import { throttle, memoized } from './tools'
import { throttle } from './tools'
import { setDefaultApi } from './extension.api'
import { getBuiltInExtensionTranslationsUris } from './l10n'

Expand Down Expand Up @@ -60,23 +60,12 @@ interface RegisterLocalProcessExtensionResult extends RegisterLocalExtensionResu
setAsDefaultApi (): Promise<void>
}

function registerExtensionFileUrl (extensionLocation: URI, filePath: string, url: string, mimeType?: string): IDisposable {
function registerExtensionFileUrl (extensionLocation: URI, filePath: string, url: string, metadataOrMimeType?: string | ExtensionFileMetadata): IDisposable {
const fileDisposable = new DisposableStore()
fileDisposable.add(FileAccess.registerStaticBrowserUri(joinPath(extensionLocation, filePath), URI.parse(url)))

fileDisposable.add(registerExtensionFile(extensionLocation, filePath, memoized(async () => {
const response = await fetch(url, {
headers: mimeType != null
? {
Accept: mimeType
}
: {}
})
if (response.status !== 200) {
throw new Error(response.statusText)
}
return new Uint8Array(await response.arrayBuffer())
})))
const metadata: ExtensionFileMetadata | undefined = typeof metadataOrMimeType === 'string' ? { mimeType: metadataOrMimeType } : metadataOrMimeType
fileDisposable.add(registerExtensionFile(new RegisteredUriFile(joinPath(extensionLocation, filePath), url, metadata)))
return fileDisposable
}

Expand Down Expand Up @@ -191,8 +180,8 @@ export function registerExtension (manifest: IExtensionManifest, extHostKind?: E
}

if (extHostKind !== ExtensionHostKind.Remote) {
function registerFileUrl (path: string, url: string, mimeType?: string) {
return registerExtensionFileUrl(location, path, url, mimeType)
function registerFileUrl (path: string, url: string, metadataOrMimeType?: string | ExtensionFileMetadata) {
return registerExtensionFileUrl(location, path, url, metadataOrMimeType)
}
api = <RegisterLocalExtensionResult>{
...api,
Expand Down Expand Up @@ -225,5 +214,6 @@ export {
IExtensionManifest,
ITranslations,
IExtensionContributions,
ExtensionHostKind
ExtensionHostKind,
ExtensionFileMetadata
}
10 changes: 7 additions & 3 deletions src/rollup-extension-directory-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { IExtensionManifest } from 'vs/platform/extensions/common/extension
import * as path from 'path'
import * as fs from 'fs'
import { getExtensionResources, parseJson } from './extension-tools.js'
import { ExtensionFileMetadata } from './extensions.js'

interface Options {
include?: FilterPattern
Expand Down Expand Up @@ -59,8 +60,11 @@ export default function plugin ({
const readmePath = resourcePaths.filter(child => /^readme(\.txt|\.md|)$/i.test(child))[0]
const changelogPath = resourcePaths.filter(child => /^changelog(\.txt|\.md|)$/i.test(child))[0]

function generateFileRegistrationInstruction (filePath: string, importPath: string, mimeType?: string) {
return `registerFileUrl('${filePath}', new URL('${importPath}', import.meta.url).toString()${mimeType != null ? `, '${mimeType}'` : ''})`
function generateFileRegistrationInstruction (filePath: string, importPath: string, mimeType?: string, size?: number) {
return `registerFileUrl('${filePath}', new URL('${importPath}', import.meta.url).toString(), ${JSON.stringify(<ExtensionFileMetadata>{
mimeType,
size
})})`
}

return `
Expand All @@ -70,7 +74,7 @@ import { registerExtension } from 'vscode/extensions'
const { registerFileUrl, whenReady } = registerExtension(manifest, undefined, ${JSON.stringify({ system: true, readmePath, changelogPath })})
${resources.map(resource => {
const lines: string[] = resource.extensionPaths.map(extensionPath =>
generateFileRegistrationInstruction(extensionPath, path.resolve(id, resource.path), resource.mimeType)
generateFileRegistrationInstruction(extensionPath, path.resolve(id, resource.path), resource.mimeType, resource.size)
)

return lines.join('\n')
Expand Down
11 changes: 8 additions & 3 deletions src/rollup-vsix-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Readable } from 'stream'
import * as path from 'path'
import type nodeFs from 'node:fs'
import { getExtensionResources, parseJson } from './extension-tools.js'
import { ExtensionFileMetadata } from './extensions.js'

interface Options {
include?: FilterPattern
Expand Down Expand Up @@ -104,7 +105,8 @@ export default function plugin ({
return resource.extensionPaths.map(extensionPath => ({
pathInExtension: getVsixPath(extensionPath),
url,
mimeType: resource.mimeType
mimeType: resource.mimeType,
size: resource.size
}))
}))).flat()

Expand All @@ -115,8 +117,11 @@ const manifest = ${JSON.stringify(manifest)}

const { registerFileUrl, whenReady } = registerExtension(manifest, undefined, ${JSON.stringify({ system: true, readmePath, changelogPath })})

${pathMapping.map(({ pathInExtension, url, mimeType }) => (`
registerFileUrl('${pathInExtension}', ${url}${mimeType != null ? `, '${mimeType}'` : ''})`)).join('\n')}
${pathMapping.map(({ pathInExtension, url, mimeType, size }) => (`
registerFileUrl('${pathInExtension}', ${url}, ${JSON.stringify(<ExtensionFileMetadata>{
mimeType,
size
})})`)).join('\n')}

export { whenReady }
`
Expand Down
Loading