Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
7f0f3103
V
vscode
项目概览
xxadev
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7f0f3103
编写于
3月 01, 2017
作者:
B
Benjamin Pasero
提交者:
GitHub
3月 01, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #21582 from katainaka0503/refactoring
Refactor to use Promise
上级
526b4e93
38943f4d
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
227 addition
and
284 deletion
+227
-284
src/vs/base/node/encoding.ts
src/vs/base/node/encoding.ts
+4
-9
src/vs/base/node/mime.ts
src/vs/base/node/mime.ts
+16
-30
src/vs/base/node/stream.ts
src/vs/base/node/stream.ts
+131
-118
src/vs/base/test/node/encoding/encoding.test.ts
src/vs/base/test/node/encoding/encoding.test.ts
+15
-25
src/vs/base/test/node/mime/mime.test.ts
src/vs/base/test/node/mime/mime.test.ts
+17
-29
src/vs/base/test/node/stream/stream.test.ts
src/vs/base/test/node/stream/stream.test.ts
+22
-33
src/vs/workbench/parts/git/node/git.lib.ts
src/vs/workbench/parts/git/node/git.lib.ts
+7
-13
src/vs/workbench/parts/git/node/rawGitService.ts
src/vs/workbench/parts/git/node/rawGitService.ts
+6
-12
src/vs/workbench/services/backup/node/backupFileService.ts
src/vs/workbench/services/backup/node/backupFileService.ts
+4
-9
src/vs/workbench/services/files/node/fileService.ts
src/vs/workbench/services/files/node/fileService.ts
+2
-2
src/vs/workbench/services/files/test/node/fileService.test.ts
...vs/workbench/services/files/test/node/fileService.test.ts
+2
-3
src/vs/workbench/services/search/node/worker/searchWorker.ts
src/vs/workbench/services/search/node/worker/searchWorker.ts
+1
-1
未找到文件。
src/vs/base/node/encoding.ts
浏览文件 @
7f0f3103
...
...
@@ -7,6 +7,7 @@
import
stream
=
require
(
'
vs/base/node/stream
'
);
import
iconv
=
require
(
'
iconv-lite
'
);
import
{
TPromise
}
from
'
vs/base/common/winjs.base
'
;
export
const
UTF8
=
'
utf8
'
;
export
const
UTF8_with_bom
=
'
utf8bom
'
;
...
...
@@ -87,14 +88,8 @@ export function detectEncodingByBOMFromBuffer(buffer: NodeBuffer, bytesRead: num
/**
* Detects the Byte Order Mark in a given file.
* If no BOM is detected,
`encoding` will be null
.
* If no BOM is detected,
null will be passed to callback
.
*/
export
function
detectEncodingByBOM
(
file
:
string
,
callback
:
(
error
:
Error
,
encoding
:
string
)
=>
void
):
void
{
stream
.
readExactlyByFile
(
file
,
3
,
(
err
:
Error
,
buffer
:
NodeBuffer
,
bytesRead
:
number
)
=>
{
if
(
err
)
{
return
callback
(
err
,
null
);
}
return
callback
(
null
,
detectEncodingByBOMFromBuffer
(
buffer
,
bytesRead
));
});
export
function
detectEncodingByBOM
(
file
:
string
):
TPromise
<
string
>
{
return
stream
.
readExactlyByFile
(
file
,
3
).
then
(({
buffer
,
bytesRead
})
=>
detectEncodingByBOMFromBuffer
(
buffer
,
bytesRead
));
}
\ No newline at end of file
src/vs/base/node/mime.ts
浏览文件 @
7f0f3103
...
...
@@ -8,6 +8,7 @@
import
streams
=
require
(
'
stream
'
);
import
mime
=
require
(
'
vs/base/common/mime
'
);
import
{
TPromise
}
from
'
vs/base/common/winjs.base
'
;
import
stream
=
require
(
'
vs/base/node/stream
'
);
import
encoding
=
require
(
'
vs/base/node/encoding
'
);
...
...
@@ -57,27 +58,15 @@ export interface IMimeAndEncoding {
mimes
:
string
[];
}
function
doDetectMimesFromStream
(
instream
:
streams
.
Readable
,
callback
:
(
error
:
Error
,
result
:
IMimeAndEncoding
)
=>
void
):
void
{
stream
.
readExactlyByStream
(
instream
,
BUFFER_READ_MAX_LEN
,
(
err
,
buffer
,
bytesRead
)
=>
{
handleReadResult
(
err
,
buffer
,
bytesRead
,
callback
);
});
function
doDetectMimesFromStream
(
instream
:
streams
.
Readable
):
TPromise
<
IMimeAndEncoding
>
{
return
stream
.
readExactlyByStream
(
instream
,
BUFFER_READ_MAX_LEN
).
then
(
detectMimeAndEncodingFromBuffer
);
}
function
doDetectMimesFromFile
(
absolutePath
:
string
,
callback
:
(
error
:
Error
,
result
:
IMimeAndEncoding
)
=>
void
):
void
{
stream
.
readExactlyByFile
(
absolutePath
,
BUFFER_READ_MAX_LEN
,
(
err
,
buffer
,
bytesRead
)
=>
{
handleReadResult
(
err
,
buffer
,
bytesRead
,
callback
);
});
function
doDetectMimesFromFile
(
absolutePath
:
string
):
TPromise
<
IMimeAndEncoding
>
{
return
stream
.
readExactlyByFile
(
absolutePath
,
BUFFER_READ_MAX_LEN
).
then
(
detectMimeAndEncodingFromBuffer
);
}
function
handleReadResult
(
err
:
Error
,
buffer
:
NodeBuffer
,
bytesRead
:
number
,
callback
:
(
error
:
Error
,
result
:
IMimeAndEncoding
)
=>
void
):
void
{
if
(
err
)
{
return
callback
(
err
,
null
);
}
return
callback
(
null
,
detectMimeAndEncodingFromBuffer
(
buffer
,
bytesRead
));
}
export
function
detectMimeAndEncodingFromBuffer
(
buffer
:
NodeBuffer
,
bytesRead
:
number
):
IMimeAndEncoding
{
export
function
detectMimeAndEncodingFromBuffer
({
buffer
,
bytesRead
}:
stream
.
ReadResult
):
IMimeAndEncoding
{
let
enc
=
encoding
.
detectEncodingByBOMFromBuffer
(
buffer
,
bytesRead
);
// Detect 0 bytes to see if file is binary (ignore for UTF 16 though)
...
...
@@ -127,29 +116,26 @@ function filterAndSortMimes(detectedMimes: string[], guessedMimes: string[]): st
* @param instream the readable stream to detect the mime types from.
* @param nameHint an additional hint that can be used to detect a mime from a file extension.
*/
export
function
detectMimesFromStream
(
instream
:
streams
.
Readable
,
nameHint
:
string
,
callback
:
(
error
:
Error
,
result
:
IMimeAndEncoding
)
=>
void
):
void
{
doDetectMimesFromStream
(
instream
,
(
error
:
Error
,
result
:
IMimeAndEncoding
)
=>
{
handleMimeResult
(
nameHint
,
e
rror
,
result
,
callback
);
}
);
export
function
detectMimesFromStream
(
instream
:
streams
.
Readable
,
nameHint
:
string
):
TPromise
<
IMimeAndEncoding
>
{
return
doDetectMimesFromStream
(
instream
).
then
(
encoding
=>
handleMimeResult
(
nameHint
,
e
ncoding
)
);
}
/**
* Opens the given file to detect its mime type. Returns an array of mime types sorted from most specific to unspecific.
* @param absolutePath the absolute path of the file.
*/
export
function
detectMimesFromFile
(
absolutePath
:
string
,
callback
:
(
error
:
Error
,
result
:
IMimeAndEncoding
)
=>
void
):
void
{
doDetectMimesFromFile
(
absolutePath
,
(
error
:
Error
,
result
:
IMimeAndEncoding
)
=>
{
handleMimeResult
(
absolutePath
,
e
rror
,
result
,
callback
);
}
);
export
function
detectMimesFromFile
(
absolutePath
:
string
):
TPromise
<
IMimeAndEncoding
>
{
return
doDetectMimesFromFile
(
absolutePath
).
then
(
encoding
=>
handleMimeResult
(
absolutePath
,
e
ncoding
)
);
}
function
handleMimeResult
(
nameHint
:
string
,
error
:
Error
,
result
:
IMimeAndEncoding
,
callback
:
(
error
:
Error
,
result
:
IMimeAndEncoding
)
=>
void
):
void
{
if
(
error
)
{
return
callback
(
error
,
null
);
}
function
handleMimeResult
(
nameHint
:
string
,
result
:
IMimeAndEncoding
):
IMimeAndEncoding
{
let
filterAndSortedMimes
=
filterAndSortMimes
(
result
.
mimes
,
mime
.
guessMimeTypes
(
nameHint
));
result
.
mimes
=
filterAndSortedMimes
;
callback
(
null
,
result
)
;
return
result
;
}
\ No newline at end of file
src/vs/base/node/stream.ts
浏览文件 @
7f0f3103
...
...
@@ -8,93 +8,104 @@
import
fs
=
require
(
'
fs
'
);
import
stream
=
require
(
'
stream
'
);
import
{
TPromise
}
from
'
vs/base/common/winjs.base
'
;
export
interface
ReadResult
{
buffer
:
NodeBuffer
;
bytesRead
:
number
;
}
/**
* Reads up to total bytes from the provided stream.
*/
export
function
readExactlyByStream
(
stream
:
stream
.
Readable
,
totalBytes
:
number
,
callback
:
(
err
:
Error
,
buffer
:
NodeBuffer
,
bytesRead
:
number
)
=>
void
):
void
{
let
done
=
false
;
let
buffer
=
new
Buffer
(
totalBytes
);
let
bytesRead
=
0
;
stream
.
on
(
'
data
'
,
(
data
:
NodeBuffer
)
=>
{
let
bytesToRead
=
Math
.
min
(
totalBytes
-
bytesRead
,
data
.
length
);
data
.
copy
(
buffer
,
bytesRead
,
0
,
bytesToRead
);
bytesRead
+=
bytesToRead
;
if
(
bytesRead
===
totalBytes
)
{
stream
.
destroy
();
// Will trigger the close event eventually
}
});
export
function
readExactlyByStream
(
stream
:
stream
.
Readable
,
totalBytes
:
number
):
TPromise
<
ReadResult
>
{
return
new
TPromise
((
complete
,
error
)
=>
{
let
done
=
false
;
let
buffer
=
new
Buffer
(
totalBytes
);
let
bytesRead
=
0
;
stream
.
on
(
'
error
'
,
(
e
:
Error
)
=>
{
if
(
!
done
)
{
done
=
true
;
callback
(
e
,
null
,
null
);
}
stream
.
on
(
'
data
'
,
(
data
:
NodeBuffer
)
=>
{
let
bytesToRead
=
Math
.
min
(
totalBytes
-
bytesRead
,
data
.
length
);
data
.
copy
(
buffer
,
bytesRead
,
0
,
bytesToRead
);
bytesRead
+=
bytesToRead
;
if
(
bytesRead
===
totalBytes
)
{
stream
.
destroy
();
// Will trigger the close event eventually
}
});
stream
.
on
(
'
error
'
,
(
e
:
Error
)
=>
{
if
(
!
done
)
{
done
=
true
;
error
(
e
);
}
});
let
onSuccess
=
()
=>
{
if
(
!
done
)
{
done
=
true
;
complete
({
buffer
,
bytesRead
});
}
};
stream
.
on
(
'
close
'
,
onSuccess
);
});
let
onSuccess
=
()
=>
{
if
(
!
done
)
{
done
=
true
;
callback
(
null
,
buffer
,
bytesRead
);
}
};
stream
.
on
(
'
close
'
,
onSuccess
);
}
/**
* Reads totalBytes from the provided file.
*/
export
function
readExactlyByFile
(
file
:
string
,
totalBytes
:
number
,
callback
:
(
error
:
Error
,
buffer
:
NodeBuffer
,
bytesRead
:
number
)
=>
void
):
void
{
fs
.
open
(
file
,
'
r
'
,
null
,
(
err
,
fd
)
=>
{
if
(
err
)
{
return
callback
(
err
,
null
,
0
);
}
function
end
(
err
:
Error
,
resultBuffer
:
NodeBuffer
,
bytesRead
:
number
):
void
{
fs
.
close
(
fd
,
(
closeError
:
Error
)
=>
{
if
(
closeError
)
{
return
callback
(
closeError
,
null
,
bytesRead
);
}
if
(
err
&&
(
<
any
>
err
).
code
===
'
EISDIR
'
)
{
return
callback
(
err
,
null
,
bytesRead
);
// we want to bubble this error up (file is actually a folder)
}
return
callback
(
null
,
resultBuffer
,
bytesRead
);
});
}
export
function
readExactlyByFile
(
file
:
string
,
totalBytes
:
number
):
TPromise
<
ReadResult
>
{
return
new
TPromise
((
complete
,
error
)
=>
{
fs
.
open
(
file
,
'
r
'
,
null
,
(
err
,
fd
)
=>
{
if
(
err
)
{
return
error
(
err
);
}
function
end
(
err
:
Error
,
resultBuffer
:
NodeBuffer
,
bytesRead
:
number
):
void
{
fs
.
close
(
fd
,
(
closeError
:
Error
)
=>
{
if
(
closeError
)
{
return
error
(
closeError
);
}
let
buffer
=
new
Buffer
(
totalBytes
);
let
bytesRead
=
0
;
let
zeroAttempts
=
0
;
function
loop
():
void
{
fs
.
read
(
fd
,
buffer
,
bytesRead
,
totalBytes
-
bytesRead
,
null
,
(
err
,
moreBytesRead
)
=>
{
if
(
err
)
{
return
end
(
err
,
null
,
0
);
}
// Retry up to N times in case 0 bytes where read
if
(
moreBytesRead
===
0
)
{
if
(
++
zeroAttempts
===
10
)
{
return
end
(
null
,
buffer
,
bytesRead
);
if
(
err
&&
(
<
any
>
err
).
code
===
'
EISDIR
'
)
{
return
error
(
err
);
// we want to bubble this error up (file is actually a folder)
}
return
loop
();
}
return
complete
({
buffer
:
resultBuffer
,
bytesRead
});
});
}
let
buffer
=
new
Buffer
(
totalBytes
);
let
bytesRead
=
0
;
let
zeroAttempts
=
0
;
function
loop
():
void
{
fs
.
read
(
fd
,
buffer
,
bytesRead
,
totalBytes
-
bytesRead
,
null
,
(
err
,
moreBytesRead
)
=>
{
if
(
err
)
{
return
end
(
err
,
null
,
0
);
}
// Retry up to N times in case 0 bytes where read
if
(
moreBytesRead
===
0
)
{
if
(
++
zeroAttempts
===
10
)
{
return
end
(
null
,
buffer
,
bytesRead
);
}
return
loop
();
}
bytesRead
+=
moreBytesRead
;
bytesRead
+=
moreBytesRead
;
if
(
bytesRead
===
totalBytes
)
{
return
end
(
null
,
buffer
,
bytesRead
);
}
if
(
bytesRead
===
totalBytes
)
{
return
end
(
null
,
buffer
,
bytesRead
);
}
return
loop
();
});
}
return
loop
();
});
}
loop
();
loop
();
});
});
}
...
...
@@ -107,59 +118,61 @@ export function readExactlyByFile(file: string, totalBytes: number, callback: (e
* @param maximumBytesToRead The maximum number of bytes to read before giving up.
* @param callback The finished callback.
*/
export
function
readToMatchingString
(
file
:
string
,
matchingString
:
string
,
chunkBytes
:
number
,
maximumBytesToRead
:
number
,
callback
:
(
error
:
Error
,
result
:
string
)
=>
void
):
void
{
fs
.
open
(
file
,
'
r
'
,
null
,
(
err
,
fd
)
=>
{
if
(
err
)
{
return
callback
(
err
,
null
);
}
function
end
(
err
:
Error
,
result
:
string
):
void
{
fs
.
close
(
fd
,
(
closeError
:
Error
)
=>
{
if
(
closeError
)
{
return
callback
(
closeError
,
null
);
}
if
(
err
&&
(
<
any
>
err
).
code
===
'
EISDIR
'
)
{
return
callback
(
err
,
null
);
// we want to bubble this error up (file is actually a folder)
}
return
callback
(
null
,
result
);
});
}
let
buffer
=
new
Buffer
(
maximumBytesToRead
);
let
bytesRead
=
0
;
let
zeroAttempts
=
0
;
function
loop
():
void
{
fs
.
read
(
fd
,
buffer
,
bytesRead
,
chunkBytes
,
null
,
(
err
,
moreBytesRead
)
=>
{
if
(
err
)
{
return
end
(
err
,
null
);
}
// Retry up to N times in case 0 bytes where read
if
(
moreBytesRead
===
0
)
{
if
(
++
zeroAttempts
===
10
)
{
return
end
(
null
,
null
);
export
function
readToMatchingString
(
file
:
string
,
matchingString
:
string
,
chunkBytes
:
number
,
maximumBytesToRead
:
number
):
TPromise
<
string
>
{
return
new
TPromise
((
complete
,
error
)
=>
fs
.
open
(
file
,
'
r
'
,
null
,
(
err
,
fd
)
=>
{
if
(
err
)
{
return
error
(
err
);
}
function
end
(
err
:
Error
,
result
:
string
):
void
{
fs
.
close
(
fd
,
(
closeError
:
Error
)
=>
{
if
(
closeError
)
{
return
error
(
closeError
);
}
return
loop
();
}
if
(
err
&&
(
<
any
>
err
).
code
===
'
EISDIR
'
)
{
return
error
(
err
);
// we want to bubble this error up (file is actually a folder)
}
return
complete
(
result
);
});
}
let
buffer
=
new
Buffer
(
maximumBytesToRead
);
let
bytesRead
=
0
;
let
zeroAttempts
=
0
;
function
loop
():
void
{
fs
.
read
(
fd
,
buffer
,
bytesRead
,
chunkBytes
,
null
,
(
err
,
moreBytesRead
)
=>
{
if
(
err
)
{
return
end
(
err
,
null
);
}
bytesRead
+=
moreBytesRead
;
// Retry up to N times in case 0 bytes where read
if
(
moreBytesRead
===
0
)
{
if
(
++
zeroAttempts
===
10
)
{
return
end
(
null
,
null
);
}
const
newLineIndex
=
buffer
.
indexOf
(
matchingString
);
if
(
newLineIndex
>=
0
)
{
return
end
(
null
,
buffer
.
toString
(
'
utf8
'
).
substr
(
0
,
newLineIndex
));
}
return
loop
();
}
if
(
bytesRead
>=
maximumBytesToRead
)
{
return
end
(
new
Error
(
`Could not find
${
matchingString
}
in first
${
maximumBytesToRead
}
bytes of
${
file
}
`
),
null
);
}
bytesRead
+=
moreBytesRead
;
return
loop
();
});
}
const
newLineIndex
=
buffer
.
indexOf
(
matchingString
);
if
(
newLineIndex
>=
0
)
{
return
end
(
null
,
buffer
.
toString
(
'
utf8
'
).
substr
(
0
,
newLineIndex
));
}
loop
();
});
if
(
bytesRead
>=
maximumBytesToRead
)
{
return
end
(
new
Error
(
`Could not find
${
matchingString
}
in first
${
maximumBytesToRead
}
bytes of
${
file
}
`
),
null
);
}
return
loop
();
});
}
loop
();
})
);
}
\ No newline at end of file
src/vs/base/test/node/encoding/encoding.test.ts
浏览文件 @
7f0f3103
...
...
@@ -10,58 +10,48 @@ import assert = require('assert');
import
encoding
=
require
(
'
vs/base/node/encoding
'
);
suite
(
'
Encoding
'
,
()
=>
{
test
(
'
detectBOM UTF-8
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
detectBOM UTF-8
'
,
(
done
:
(
err
?:
any
)
=>
void
)
=>
{
const
file
=
require
.
toUrl
(
'
./fixtures/some_utf8.css
'
);
encoding
.
detectEncodingByBOM
(
file
,
(
error
:
Error
,
encoding
:
string
)
=>
{
assert
.
equal
(
error
,
null
);
encoding
.
detectEncodingByBOM
(
file
).
then
((
encoding
:
string
)
=>
{
assert
.
equal
(
encoding
,
'
utf8
'
);
done
();
});
}
,
done
);
});
test
(
'
detectBOM UTF-16 LE
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
detectBOM UTF-16 LE
'
,
(
done
:
(
err
?:
any
)
=>
void
)
=>
{
const
file
=
require
.
toUrl
(
'
./fixtures/some_utf16le.css
'
);
encoding
.
detectEncodingByBOM
(
file
,
(
error
:
Error
,
encoding
:
string
)
=>
{
assert
.
equal
(
error
,
null
);
encoding
.
detectEncodingByBOM
(
file
).
then
((
encoding
:
string
)
=>
{
assert
.
equal
(
encoding
,
'
utf16le
'
);
done
();
});
}
,
done
);
});
test
(
'
detectBOM UTF-16 BE
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
detectBOM UTF-16 BE
'
,
(
done
:
(
err
?:
any
)
=>
void
)
=>
{
const
file
=
require
.
toUrl
(
'
./fixtures/some_utf16be.css
'
);
encoding
.
detectEncodingByBOM
(
file
,
(
error
:
Error
,
encoding
:
string
)
=>
{
assert
.
equal
(
error
,
null
);
encoding
.
detectEncodingByBOM
(
file
).
then
((
encoding
:
string
)
=>
{
assert
.
equal
(
encoding
,
'
utf16be
'
);
done
();
});
}
,
done
);
});
test
(
'
detectBOM ANSI
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
detectBOM ANSI
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/some_ansi.css
'
);
encoding
.
detectEncodingByBOM
(
file
,
(
error
:
Error
,
encoding
:
string
)
=>
{
assert
.
equal
(
error
,
null
);
encoding
.
detectEncodingByBOM
(
file
).
then
((
encoding
:
string
)
=>
{
assert
.
equal
(
encoding
,
null
);
done
();
});
}
,
done
);
});
test
(
'
detectBOM ANSI
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
detectBOM ANSI
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/empty.txt
'
);
encoding
.
detectEncodingByBOM
(
file
,
(
error
:
Error
,
encoding
:
string
)
=>
{
assert
.
equal
(
error
,
null
);
encoding
.
detectEncodingByBOM
(
file
).
then
((
encoding
:
string
)
=>
{
assert
.
equal
(
encoding
,
null
);
done
();
});
}
,
done
);
});
});
src/vs/base/test/node/mime/mime.test.ts
浏览文件 @
7f0f3103
...
...
@@ -12,64 +12,52 @@ import mime = require('vs/base/node/mime');
suite
(
'
Mime
'
,
()
=>
{
test
(
'
detectMimesFromFile (JSON saved as PNG)
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
detectMimesFromFile (JSON saved as PNG)
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/some.json.png
'
);
mime
.
detectMimesFromFile
(
file
,
(
error
,
mimes
)
=>
{
assert
.
equal
(
error
,
null
);
mime
.
detectMimesFromFile
(
file
).
then
(
mimes
=>
{
assert
.
deepEqual
(
mimes
.
mimes
,
[
'
text/plain
'
]);
done
();
});
}
,
done
);
});
test
(
'
detectMimesFromFile (PNG saved as TXT)
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
detectMimesFromFile (PNG saved as TXT)
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
mimeCommon
.
registerTextMime
({
id
:
'
text
'
,
mime
:
'
text/plain
'
,
extension
:
'
.txt
'
});
const
file
=
require
.
toUrl
(
'
./fixtures/some.png.txt
'
);
mime
.
detectMimesFromFile
(
file
,
(
error
,
mimes
)
=>
{
assert
.
equal
(
error
,
null
);
mime
.
detectMimesFromFile
(
file
).
then
(
mimes
=>
{
assert
.
deepEqual
(
mimes
.
mimes
,
[
'
text/plain
'
,
'
application/octet-stream
'
]);
done
();
});
}
,
done
);
});
test
(
'
detectMimesFromFile (XML saved as PNG)
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
detectMimesFromFile (XML saved as PNG)
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/some.xml.png
'
);
mime
.
detectMimesFromFile
(
file
,
(
error
,
mimes
)
=>
{
assert
.
equal
(
error
,
null
);
mime
.
detectMimesFromFile
(
file
).
then
(
mimes
=>
{
assert
.
deepEqual
(
mimes
.
mimes
,
[
'
text/plain
'
]);
done
();
});
}
,
done
);
});
test
(
'
detectMimesFromFile (QWOFF saved as TXT)
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
detectMimesFromFile (QWOFF saved as TXT)
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/some.qwoff.txt
'
);
mime
.
detectMimesFromFile
(
file
,
(
error
,
mimes
)
=>
{
assert
.
equal
(
error
,
null
);
mime
.
detectMimesFromFile
(
file
).
then
(
mimes
=>
{
assert
.
deepEqual
(
mimes
.
mimes
,
[
'
text/plain
'
,
'
application/octet-stream
'
]);
done
();
});
}
,
done
);
});
test
(
'
detectMimesFromFile (CSS saved as QWOFF)
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
detectMimesFromFile (CSS saved as QWOFF)
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/some.css.qwoff
'
);
mime
.
detectMimesFromFile
(
file
,
(
error
,
mimes
)
=>
{
assert
.
equal
(
error
,
null
);
mime
.
detectMimesFromFile
(
file
).
then
(
mimes
=>
{
assert
.
deepEqual
(
mimes
.
mimes
,
[
'
text/plain
'
]);
done
();
});
}
,
done
);
});
test
(
'
detectMimesFromFile (PDF)
'
,
function
(
done
:
()
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/some.pdf
'
);
mime
.
detectMimesFromFile
(
file
,
(
error
,
mimes
)
=>
{
assert
.
equal
(
error
,
null
);
mime
.
detectMimesFromFile
(
file
).
then
(
mimes
=>
{
assert
.
deepEqual
(
mimes
.
mimes
,
[
'
application/octet-stream
'
]);
done
();
});
}
,
done
);
});
});
src/vs/base/test/node/stream/stream.test.ts
浏览文件 @
7f0f3103
...
...
@@ -11,72 +11,61 @@ import fs = require('fs');
import
stream
=
require
(
'
vs/base/node/stream
'
);
suite
(
'
Stream
'
,
()
=>
{
test
(
'
readExactlyByFile - ANSI
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
readExactlyByFile - ANSI
'
,
function
(
done
:
(
err
?
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/file.css
'
);
stream
.
readExactlyByFile
(
file
,
10
,
(
error
:
Error
,
buffer
:
NodeBuffer
,
count
:
number
)
=>
{
assert
.
equal
(
error
,
null
);
assert
.
equal
(
count
,
10
);
stream
.
readExactlyByFile
(
file
,
10
).
then
(({
buffer
,
bytesRead
})
=>
{
assert
.
equal
(
bytesRead
,
10
);
assert
.
equal
(
buffer
.
toString
(),
'
/*--------
'
);
done
();
});
}
,
done
);
});
test
(
'
readExactlyByFile - empty
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
readExactlyByFile - empty
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/empty.txt
'
);
stream
.
readExactlyByFile
(
file
,
10
,
(
error
:
Error
,
buffer
:
NodeBuffer
,
count
:
number
)
=>
{
assert
.
equal
(
error
,
null
);
assert
.
equal
(
count
,
0
);
stream
.
readExactlyByFile
(
file
,
10
).
then
(({
bytesRead
})
=>
{
assert
.
equal
(
bytesRead
,
0
);
done
();
});
}
,
done
);
});
test
(
'
readExactlyByStream - ANSI
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
readExactlyByStream - ANSI
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/file.css
'
);
stream
.
readExactlyByStream
(
fs
.
createReadStream
(
file
),
10
,
(
error
:
Error
,
buffer
:
NodeBuffer
,
count
:
number
)
=>
{
assert
.
equal
(
error
,
null
);
assert
.
equal
(
count
,
10
);
stream
.
readExactlyByStream
(
fs
.
createReadStream
(
file
),
10
).
then
(({
buffer
,
bytesRead
})
=>
{
assert
.
equal
(
bytesRead
,
10
);
assert
.
equal
(
buffer
.
toString
(),
'
/*--------
'
);
done
();
});
}
,
done
);
});
test
(
'
readExactlyByStream - empty
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
readExactlyByStream - empty
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/empty.txt
'
);
stream
.
readExactlyByStream
(
fs
.
createReadStream
(
file
),
10
,
(
error
:
Error
,
buffer
:
NodeBuffer
,
count
:
number
)
=>
{
assert
.
equal
(
error
,
null
);
assert
.
equal
(
count
,
0
);
stream
.
readExactlyByStream
(
fs
.
createReadStream
(
file
),
10
).
then
(({
bytesRead
})
=>
{
assert
.
equal
(
bytesRead
,
0
);
done
();
});
}
,
done
);
});
test
(
'
readToMatchingString - ANSI
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
readToMatchingString - ANSI
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/file.css
'
);
stream
.
readToMatchingString
(
file
,
'
\n
'
,
10
,
100
,
(
error
:
Error
,
result
:
string
)
=>
{
assert
.
equal
(
error
,
null
);
stream
.
readToMatchingString
(
file
,
'
\n
'
,
10
,
100
).
then
((
result
:
string
)
=>
{
// \r may be present on Windows
assert
.
equal
(
result
.
replace
(
'
\r
'
,
''
),
'
/*---------------------------------------------------------------------------------------------
'
);
done
();
});
}
,
done
);
});
test
(
'
readToMatchingString - empty
'
,
function
(
done
:
()
=>
void
)
{
test
(
'
readToMatchingString - empty
'
,
function
(
done
:
(
err
?:
any
)
=>
void
)
{
const
file
=
require
.
toUrl
(
'
./fixtures/empty.txt
'
);
stream
.
readToMatchingString
(
file
,
'
\n
'
,
10
,
100
,
(
error
:
Error
,
result
:
string
)
=>
{
assert
.
equal
(
error
,
null
);
stream
.
readToMatchingString
(
file
,
'
\n
'
,
10
,
100
).
then
((
result
:
string
)
=>
{
assert
.
equal
(
result
,
null
);
done
();
});
}
,
done
);
});
});
\ No newline at end of file
src/vs/workbench/parts/git/node/git.lib.ts
浏览文件 @
7f0f3103
...
...
@@ -327,19 +327,13 @@ export class Repository {
return
TPromise
.
wrapError
(
localize
(
'
errorBuffer
'
,
"
Can't open file from git
"
));
}
return
new
Promise
((
c
,
e
)
=>
{
detectMimesFromStream
(
child
.
stdout
,
null
,
(
err
,
result
)
=>
{
if
(
err
)
{
e
(
err
);
}
else
if
(
isBinaryMime
(
result
.
mimes
))
{
e
(
<
IFileOperationResult
>
{
message
:
localize
(
'
fileBinaryError
'
,
"
File seems to be binary and cannot be opened as text
"
),
fileOperationResult
:
FileOperationResult
.
FILE_IS_BINARY
});
}
else
{
c
(
this
.
doBuffer
(
object
));
}
});
return
detectMimesFromStream
(
child
.
stdout
,
null
).
then
(
result
=>
{
return
isBinaryMime
(
result
.
mimes
)
?
TPromise
.
wrapError
<
string
>
(
<
IFileOperationResult
>
{
message
:
localize
(
'
fileBinaryError
'
,
"
File seems to be binary and cannot be opened as text
"
),
fileOperationResult
:
FileOperationResult
.
FILE_IS_BINARY
})
:
this
.
doBuffer
(
object
);
});
}
...
...
src/vs/workbench/parts/git/node/rawGitService.ts
浏览文件 @
7f0f3103
...
...
@@ -165,22 +165,16 @@ export class RawGitService implements IRawGitService {
detectMimetypes
(
filePath
:
string
,
treeish
?:
string
):
TPromise
<
string
[]
>
{
return
exists
(
join
(
this
.
repo
.
path
,
filePath
)).
then
((
exists
)
=>
{
if
(
exists
)
{
return
new
TPromise
<
string
[]
>
((
c
,
e
)
=>
{
detectMimesFromFile
(
join
(
this
.
repo
.
path
,
filePath
),
(
err
,
result
)
=>
{
if
(
err
)
{
e
(
err
);
}
else
{
c
(
result
.
mimes
);
}
});
});
return
detectMimesFromFile
(
join
(
this
.
repo
.
path
,
filePath
))
.
then
(
result
=>
result
.
mimes
);
}
const
child
=
this
.
repo
.
show
(
treeish
+
'
:
'
+
filePath
);
return
new
TPromise
<
string
[]
>
((
c
,
e
)
=>
{
detectMimesFromStream
(
child
.
stdout
,
filePath
,
(
err
,
result
)
=>
{
if
(
err
)
{
e
(
err
);
}
else
{
c
(
result
.
mimes
);
}
});
});
return
new
TPromise
<
string
[]
>
((
c
,
e
)
=>
detectMimesFromStream
(
child
.
stdout
,
filePath
)
.
then
(
result
=>
result
.
mimes
)
);
});
}
...
...
src/vs/workbench/services/backup/node/backupFileService.ts
浏览文件 @
7f0f3103
...
...
@@ -229,15 +229,10 @@ export class BackupFileService implements IBackupFileService {
const
readPromises
:
TPromise
<
Uri
>
[]
=
[];
model
.
get
().
forEach
(
fileBackup
=>
{
readPromises
.
push
(
new
TPromise
<
Uri
>
((
c
,
e
)
=>
{
readToMatchingString
(
fileBackup
.
fsPath
,
BackupFileService
.
META_MARKER
,
2000
,
10000
,
(
error
,
result
)
=>
{
if
(
result
===
null
)
{
e
(
error
);
}
c
(
Uri
.
parse
(
result
));
});
}));
readPromises
.
push
(
readToMatchingString
(
fileBackup
.
fsPath
,
BackupFileService
.
META_MARKER
,
2000
,
10000
)
.
then
(
Uri
.
parse
)
);
});
return
TPromise
.
join
(
readPromises
);
...
...
src/vs/workbench/services/files/node/fileService.ts
浏览文件 @
7f0f3103
...
...
@@ -205,7 +205,7 @@ export class FileService implements IFileService {
}
// 2.) detect mimes
return
nfcall
(
mime
.
detectMimesFromFile
,
absolutePath
).
then
((
detected
:
mime
.
IMimeAndEncoding
):
TPromise
<
IStreamContent
>
=>
{
return
mime
.
detectMimesFromFile
(
absolutePath
).
then
((
detected
:
mime
.
IMimeAndEncoding
)
=>
{
const
isText
=
detected
.
mimes
.
indexOf
(
baseMime
.
MIME_BINARY
)
===
-
1
;
// Return error early if client only accepts text and this is not text
...
...
@@ -300,7 +300,7 @@ export class FileService implements IFileService {
if
(
options
.
overwriteEncoding
)
{
addBomPromise
=
TPromise
.
as
(
false
);
// if we are to overwrite the encoding, we do not preserve it if found
}
else
{
addBomPromise
=
nfcall
(
encoding
.
detectEncodingByBOM
,
absolutePath
).
then
(
enc
=>
enc
===
encoding
.
UTF8
);
// otherwise preserve it if found
addBomPromise
=
encoding
.
detectEncodingByBOM
(
absolutePath
).
then
(
enc
=>
enc
===
encoding
.
UTF8
);
// otherwise preserve it if found
}
}
...
...
src/vs/workbench/services/files/test/node/fileService.test.ts
浏览文件 @
7f0f3103
...
...
@@ -13,7 +13,6 @@ import assert = require('assert');
import
{
TPromise
}
from
'
vs/base/common/winjs.base
'
;
import
{
FileService
,
IEncodingOverride
}
from
'
vs/workbench/services/files/node/fileService
'
;
import
{
FileOperation
,
FileOperationEvent
,
FileChangesEvent
,
FileOperationResult
,
IFileOperationResult
}
from
'
vs/platform/files/common/files
'
;
import
{
nfcall
}
from
'
vs/base/common/async
'
;
import
uri
from
'
vs/base/common/uri
'
;
import
uuid
=
require
(
'
vs/base/common/uuid
'
);
import
extfs
=
require
(
'
vs/base/node/extfs
'
);
...
...
@@ -542,7 +541,7 @@ suite('FileService', () => {
c
.
encoding
=
encoding
;
return
service
.
updateContent
(
c
.
resource
,
c
.
value
,
{
encoding
:
encoding
}).
then
(
c
=>
{
return
nfcall
(
encodingLib
.
detectEncodingByBOM
,
c
.
resource
.
fsPath
).
then
((
enc
)
=>
{
return
encodingLib
.
detectEncodingByBOM
(
c
.
resource
.
fsPath
).
then
((
enc
)
=>
{
assert
.
equal
(
enc
,
encodingLib
.
UTF16be
);
return
service
.
resolveContent
(
resource
).
then
(
c
=>
{
...
...
@@ -565,7 +564,7 @@ suite('FileService', () => {
c
.
value
=
'
Some updates
'
;
return
service
.
updateContent
(
c
.
resource
,
c
.
value
,
{
encoding
:
encoding
}).
then
(
c
=>
{
return
nfcall
(
encodingLib
.
detectEncodingByBOM
,
c
.
resource
.
fsPath
).
then
((
enc
)
=>
{
return
encodingLib
.
detectEncodingByBOM
(
c
.
resource
.
fsPath
).
then
((
enc
)
=>
{
assert
.
equal
(
enc
,
encodingLib
.
UTF16le
);
return
service
.
resolveContent
(
resource
).
then
(
c
=>
{
...
...
src/vs/workbench/services/search/node/worker/searchWorker.ts
浏览文件 @
7f0f3103
...
...
@@ -209,7 +209,7 @@ export class SearchWorkerEngine {
// Detect encoding and mime when this is the beginning of the file
if
(
isFirstRead
)
{
const
mimeAndEncoding
=
detectMimeAndEncodingFromBuffer
(
buffer
,
bytesRead
);
const
mimeAndEncoding
=
detectMimeAndEncodingFromBuffer
(
{
buffer
,
bytesRead
}
);
if
(
mimeAndEncoding
.
mimes
[
mimeAndEncoding
.
mimes
.
length
-
1
]
!==
baseMime
.
MIME_TEXT
)
{
return
clb
(
null
);
// skip files that seem binary
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录