Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

fix: use safe-decode-uri-component #450

Closed
Closed
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"object-hash": "^1.1.8",
"opentracing": "^0.14.0",
"rxjs": "^5.5.0",
"safe-decode-uri-component": "^1.1.3",
"semaphore-async-await": "^1.5.1",
"string-similarity": "^1.1.0",
"typescript": "2.7.2",
Expand Down
2 changes: 1 addition & 1 deletion src/test/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as chai from 'chai'
import chaiAsPromised = require('chai-as-promised')
import * as fs from 'mz/fs'
import * as path from 'path'
import * as rimraf from 'rimraf'
import rimraf from 'rimraf'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import * as temp from 'temp'
import { LocalFileSystem } from '../fs'
import { path2uri } from '../util'
Expand Down
8 changes: 8 additions & 0 deletions src/test/memfs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ describe('memfs.ts', () => {
sinon.assert.calledOnce(listener)
sinon.assert.calledWithExactly(listener, 'file:///foo/bar.txt', undefined)
})
it('should add a URI with `%` character and emit an event', () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test would have been failed before this PR changes.
This is exactly the case #422 referres to.

const listener = sinon.spy()
const fs = new InMemoryFileSystem('/')
fs.on('add', listener)
fs.add('file:///foo/%bar.txt')
sinon.assert.calledOnce(listener)
sinon.assert.calledWithExactly(listener, 'file:///foo/%bar.txt', undefined)
})
it('should add content for a URI and emit an event', () => {
const listener = sinon.spy()
const fs = new InMemoryFileSystem('/')
Expand Down
3 changes: 3 additions & 0 deletions src/typings/safe-decode-uri-component.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'safe-decode-uri-component' {
export default function safeDecodeURIComponent(uriComponent: string): string
}
5 changes: 3 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { escapePathComponent } from 'fast-json-patch'
import { Observable } from 'rxjs'
import safeDecodeURIComponent from 'safe-decode-uri-component'
import { compareTwoStrings } from 'string-similarity'
import * as ts from 'typescript'
import * as url from 'url'
Expand Down Expand Up @@ -43,7 +44,7 @@ export function normalizeUri(uri: string): string {
if (!parts.pathname) {
return uri
}
const pathParts = parts.pathname.split('/').map(segment => encodeURIComponent(decodeURIComponent(segment)))
const pathParts = parts.pathname.split('/').map(segment => encodeURIComponent(safeDecodeURIComponent(segment)))
// Decode Windows drive letter colon
if (/^[a-z]%3A$/i.test(pathParts[1])) {
pathParts[1] = decodeURIComponent(pathParts[1])
Expand Down Expand Up @@ -95,7 +96,7 @@ export function uri2path(uri: string): string {
filePath = filePath.substr(1).replace(/\//g, '\\')
}

return decodeURIComponent(filePath)
return safeDecodeURIComponent(filePath)
}

const jstsPattern = /\.[tj]sx?$/
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"sourceMap": true,
"declaration": true,
"strictFunctionTypes": false,
"esModuleInterop": true,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this because safe-decode-uri-component is written in CommonJS. With this flag safe-decode-uri-component can be imported by:

import safeDecodeURIComponent from 'safe-decode-uri-component'

rather than

import * as safeDecodeURIComponent from 'safe-decode-uri-component'

"strictPropertyInitialization": false,
"plugins": [
{
Expand Down