Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
f701dd50
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,发现更多精彩内容 >>
提交
f701dd50
编写于
3月 01, 2017
作者:
K
katainaka0503
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor stream.ts
上级
9ee29afe
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
178 addition
and
213 deletion
+178
-213
src/vs/base/node/encoding.ts
src/vs/base/node/encoding.ts
+1
-10
src/vs/base/node/mime.ts
src/vs/base/node/mime.ts
+3
-26
src/vs/base/node/stream.ts
src/vs/base/node/stream.ts
+148
-135
src/vs/base/test/node/stream/stream.test.ts
src/vs/base/test/node/stream/stream.test.ts
+22
-33
src/vs/workbench/services/backup/node/backupFileService.ts
src/vs/workbench/services/backup/node/backupFileService.ts
+4
-9
未找到文件。
src/vs/base/node/encoding.ts
浏览文件 @
f701dd50
...
...
@@ -91,14 +91,5 @@ export function detectEncodingByBOMFromBuffer(buffer: NodeBuffer, bytesRead: num
* If no BOM is detected, null will be passed to callback.
*/
export
function
detectEncodingByBOM
(
file
:
string
):
TPromise
<
string
>
{
return
new
TPromise
((
complete
,
error
)
=>
{
stream
.
readExactlyByFile
(
file
,
3
,
(
err
:
Error
,
buffer
:
NodeBuffer
,
bytesRead
:
number
)
=>
{
if
(
err
)
{
error
(
err
);
}
else
{
complete
(
detectEncodingByBOMFromBuffer
(
buffer
,
bytesRead
));
}
});
});
return
stream
.
readExactlyByFile
(
file
,
3
).
then
(({
buffer
,
bytesRead
})
=>
detectEncodingByBOMFromBuffer
(
buffer
,
bytesRead
));
}
\ No newline at end of file
src/vs/base/node/mime.ts
浏览文件 @
f701dd50
...
...
@@ -59,37 +59,14 @@ export interface IMimeAndEncoding {
}
function
doDetectMimesFromStream
(
instream
:
streams
.
Readable
):
TPromise
<
IMimeAndEncoding
>
{
return
new
TPromise
((
complete
,
error
)
=>
stream
.
readExactlyByStream
(
instream
,
BUFFER_READ_MAX_LEN
,
(
err
,
buffer
,
bytesRead
)
=>
{
if
(
err
)
{
error
(
error
);
}
else
{
complete
(
detectMimeAndEncodingFromBuffer
({
buffer
,
bytesRead
}));
}
})
);
return
stream
.
readExactlyByStream
(
instream
,
BUFFER_READ_MAX_LEN
).
then
(
detectMimeAndEncodingFromBuffer
);
}
function
doDetectMimesFromFile
(
absolutePath
:
string
):
TPromise
<
IMimeAndEncoding
>
{
return
new
TPromise
((
complete
,
error
)
=>
stream
.
readExactlyByFile
(
absolutePath
,
BUFFER_READ_MAX_LEN
,
(
err
,
buffer
,
bytesRead
)
=>
{
if
(
err
)
{
error
(
error
);
}
else
{
complete
(
detectMimeAndEncodingFromBuffer
({
buffer
,
bytesRead
}));
}
})
);
}
export
interface
ReadResult
{
buffer
:
NodeBuffer
;
bytesRead
:
number
;
return
stream
.
readExactlyByFile
(
absolutePath
,
BUFFER_READ_MAX_LEN
).
then
(
detectMimeAndEncodingFromBuffer
);
}
export
function
detectMimeAndEncodingFromBuffer
({
buffer
,
bytesRead
}:
ReadResult
):
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)
...
...
src/vs/base/node/stream.ts
浏览文件 @
f701dd50
...
...
@@ -8,94 +8,105 @@
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
}
});
stream
.
on
(
'
error
'
,
(
e
:
Error
)
=>
{
if
(
!
done
)
{
done
=
true
;
callback
(
e
,
null
,
null
);
}
});
let
onSuccess
=
()
=>
{
if
(
!
done
)
{
done
=
true
;
callback
(
null
,
buffer
,
bytesRead
);
}
};
stream
.
on
(
'
close
'
,
onSuccess
);
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
(
'
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
);
});
}
/**
* 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
);
});
}
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
;
if
(
bytesRead
===
totalBytes
)
{
return
end
(
null
,
buffer
,
bytesRead
);
}
return
loop
();
});
}
loop
();
});
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
);
}
if
(
err
&&
(
<
any
>
err
).
code
===
'
EISDIR
'
)
{
return
error
(
err
);
// we want to bubble this error up (file is actually a folder)
}
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
;
if
(
bytesRead
===
totalBytes
)
{
return
end
(
null
,
buffer
,
bytesRead
);
}
return
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
);
}
return
loop
();
}
bytesRead
+=
moreBytesRead
;
const
newLineIndex
=
buffer
.
indexOf
(
matchingString
);
if
(
newLineIndex
>=
0
)
{
return
end
(
null
,
buffer
.
toString
(
'
utf8
'
).
substr
(
0
,
newLineIndex
));
}
if
(
bytesRead
>=
maximumBytesToRead
)
{
return
end
(
new
Error
(
`Could not find
${
matchingString
}
in first
${
maximumBytesToRead
}
bytes of
${
file
}
`
),
null
);
}
return
loop
();
});
}
loop
();
});
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
);
}
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
);
}
// Retry up to N times in case 0 bytes where read
if
(
moreBytesRead
===
0
)
{
if
(
++
zeroAttempts
===
10
)
{
return
end
(
null
,
null
);
}
return
loop
();
}
bytesRead
+=
moreBytesRead
;
const
newLineIndex
=
buffer
.
indexOf
(
matchingString
);
if
(
newLineIndex
>=
0
)
{
return
end
(
null
,
buffer
.
toString
(
'
utf8
'
).
substr
(
0
,
newLineIndex
));
}
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/stream/stream.test.ts
浏览文件 @
f701dd50
...
...
@@ -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/services/backup/node/backupFileService.ts
浏览文件 @
f701dd50
...
...
@@ -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
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录