Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
f0a60620
D
deno
项目概览
张重言
/
deno
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deno
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
f0a60620
编写于
1月 30, 2020
作者:
C
cknight
提交者:
GitHub
1月 29, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Partial implementation of node os polyfill based on currently available Deno functionality (#3821)
上级
161adfc5
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
505 addition
and
1 deletion
+505
-1
std/node/README.md
std/node/README.md
+1
-1
std/node/module.ts
std/node/module.ts
+2
-0
std/node/module_test.ts
std/node/module_test.ts
+6
-0
std/node/os.ts
std/node/os.ts
+221
-0
std/node/os_test.ts
std/node/os_test.ts
+275
-0
未找到文件。
std/node/README.md
浏览文件 @
f0a60620
...
...
@@ -23,7 +23,7 @@ deno standard library as it's a compatiblity module.
-
[ ] https
-
[x] module
-
[ ] net
-
[
] os
-
[
x] os _partly_
-
[x] path
-
[ ] perf_hooks
-
[x] process _partly_
...
...
std/node/module.ts
浏览文件 @
f0a60620
...
...
@@ -25,6 +25,7 @@ import * as nodeFS from "./fs.ts";
import
*
as
nodeUtil
from
"
./util.ts
"
;
import
*
as
nodePath
from
"
./path.ts
"
;
import
*
as
nodeTimers
from
"
./timers.ts
"
;
import
*
as
nodeOs
from
"
./os.ts
"
;
import
*
as
path
from
"
../path/mod.ts
"
;
import
{
assert
}
from
"
../testing/asserts.ts
"
;
...
...
@@ -582,6 +583,7 @@ nativeModulePolyfill.set("fs", createNativeModule("fs", nodeFS));
nativeModulePolyfill
.
set
(
"
util
"
,
createNativeModule
(
"
util
"
,
nodeUtil
));
nativeModulePolyfill
.
set
(
"
path
"
,
createNativeModule
(
"
path
"
,
nodePath
));
nativeModulePolyfill
.
set
(
"
timers
"
,
createNativeModule
(
"
timers
"
,
nodeTimers
));
nativeModulePolyfill
.
set
(
"
os
"
,
createNativeModule
(
"
os
"
,
nodeOs
));
function
loadNativeModule
(
_filename
:
string
,
request
:
string
...
...
std/node/module_test.ts
浏览文件 @
f0a60620
...
...
@@ -42,3 +42,9 @@ test(function requireIndexJS() {
const
{
isIndex
}
=
require_
(
"
./tests/cjs
"
);
assert
(
isIndex
);
});
test
(
function
requireNodeOs
()
{
const
os
=
require_
(
"
os
"
);
assert
(
os
.
arch
);
assert
(
typeof
os
.
arch
()
==
"
string
"
);
});
std/node/os.ts
0 → 100644
浏览文件 @
f0a60620
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
import
{
notImplemented
}
from
"
./_utils.ts
"
;
const
SEE_GITHUB_ISSUE
=
"
See https://github.com/denoland/deno/issues/3802
"
;
interface
CPUTimes
{
/** The number of milliseconds the CPU has spent in user mode */
user
:
number
;
/** The number of milliseconds the CPU has spent in nice mode */
nice
:
number
;
/** The number of milliseconds the CPU has spent in sys mode */
sys
:
number
;
/** The number of milliseconds the CPU has spent in idle mode */
idle
:
number
;
/** The number of milliseconds the CPU has spent in irq mode */
irq
:
number
;
}
interface
CPUCoreInfo
{
model
:
string
;
/** in MHz */
speed
:
number
;
times
:
CPUTimes
;
}
interface
NetworkAddress
{
/** The assigned IPv4 or IPv6 address */
address
:
string
;
/** The IPv4 or IPv6 network mask */
netmask
:
string
;
family
:
"
IPv4
"
|
"
IPv6
"
;
/** The MAC address of the network interface */
mac
:
string
;
/** true if the network interface is a loopback or similar interface that is not remotely accessible; otherwise false */
internal
:
boolean
;
/** The numeric IPv6 scope ID (only specified when family is IPv6) */
scopeid
?:
number
;
/** The assigned IPv4 or IPv6 address with the routing prefix in CIDR notation. If the netmask is invalid, this property is set to null. */
cidr
:
string
;
}
interface
NetworkInterfaces
{
[
key
:
string
]:
NetworkAddress
[];
}
export
interface
UserInfoOptions
{
encoding
:
string
;
}
interface
UserInfo
{
username
:
string
;
uid
:
number
;
gid
:
number
;
shell
:
string
;
homedir
:
string
;
}
/** Returns the operating system CPU architecture for which the Deno binary was compiled */
export
function
arch
():
string
{
return
Deno
.
build
.
arch
;
}
/** Not yet implemented */
export
function
cpus
():
CPUCoreInfo
[]
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
endianness
():
"
BE
"
|
"
LE
"
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
freemem
():
number
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
getPriority
(
pid
=
0
):
number
{
validateInt32
(
pid
,
"
pid
"
);
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Returns the string path of the current user's home directory. */
export
function
homedir
():
string
{
return
Deno
.
dir
(
"
home
"
);
}
/** Returns the host name of the operating system as a string. */
export
function
hostname
():
string
{
return
Deno
.
hostname
();
}
/** Not yet implemented */
export
function
loadavg
():
number
[]
{
if
(
Deno
.
build
.
os
==
"
win
"
)
{
return
[
0
,
0
,
0
];
}
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
networkInterfaces
():
NetworkInterfaces
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
platform
():
string
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
release
():
string
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
setPriority
(
pid
:
number
,
priority
?:
number
):
void
{
/* The node API has the 'pid' as the first parameter and as optional.
This makes for a problematic implementation in Typescript. */
if
(
priority
===
undefined
)
{
priority
=
pid
;
pid
=
0
;
}
validateInt32
(
pid
,
"
pid
"
);
validateInt32
(
priority
,
"
priority
"
,
-
20
,
19
);
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
tmpdir
():
string
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
totalmem
():
number
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
type
():
string
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
uptime
():
number
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
/** Not yet implemented */
export
function
userInfo
(
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
options
:
UserInfoOptions
=
{
encoding
:
"
utf-8
"
}
):
UserInfo
{
notImplemented
(
SEE_GITHUB_ISSUE
);
}
export
const
constants
=
{
// UV_UDP_REUSEADDR: 4, //see https://nodejs.org/docs/latest-v12.x/api/os.html#os_libuv_constants
dlopen
:
{
// see https://nodejs.org/docs/latest-v12.x/api/os.html#os_dlopen_constants
},
errno
:
{
// see https://nodejs.org/docs/latest-v12.x/api/os.html#os_error_constants
},
signals
:
Deno
.
Signal
,
priority
:
{
// see https://nodejs.org/docs/latest-v12.x/api/os.html#os_priority_constants
}
};
export
const
EOL
=
Deno
.
build
.
os
==
"
win
"
?
"
\r\n
"
:
"
\n
"
;
const
validateInt32
=
(
value
:
number
,
name
:
string
,
min
=
-
2147483648
,
max
=
2147483647
):
void
=>
{
// The defaults for min and max correspond to the limits of 32-bit integers.
if
(
!
Number
.
isInteger
(
value
))
{
throw
new
Error
(
`
${
name
}
must be 'an integer' but was
${
value
}
`
);
}
if
(
value
<
min
||
value
>
max
)
{
throw
new
Error
(
`
${
name
}
must be >=
${
min
}
&& <=
${
max
}
. Value was
${
value
}
`
);
}
};
std/node/os_test.ts
0 → 100644
浏览文件 @
f0a60620
import
{
test
}
from
"
../testing/mod.ts
"
;
import
{
assert
,
assertThrows
,
assertEquals
,
AssertionError
}
from
"
../testing/asserts.ts
"
;
import
*
as
os
from
"
./os.ts
"
;
test
({
name
:
"
build architecture is a string
"
,
fn
()
{
assertEquals
(
typeof
os
.
arch
(),
"
string
"
);
}
});
test
({
name
:
"
home directory is a string
"
,
fn
()
{
assertEquals
(
typeof
os
.
homedir
(),
"
string
"
);
}
});
test
({
name
:
"
hostname is a string
"
,
fn
()
{
assertEquals
(
typeof
os
.
hostname
(),
"
string
"
);
}
});
test
({
name
:
"
getPriority(): PID must be a 32 bit integer
"
,
fn
()
{
assertThrows
(
()
=>
{
os
.
getPriority
(
3.15
);
},
Error
,
"
pid must be 'an integer'
"
);
assertThrows
(
()
=>
{
os
.
getPriority
(
9999999999
);
},
Error
,
"
must be >= -2147483648 && <= 2147483647
"
);
}
});
test
({
name
:
"
setPriority(): PID must be a 32 bit integer
"
,
fn
()
{
assertThrows
(
()
=>
{
os
.
setPriority
(
3.15
,
0
);
},
Error
,
"
pid must be 'an integer'
"
);
assertThrows
(
()
=>
{
os
.
setPriority
(
9999999999
,
0
);
},
Error
,
"
pid must be >= -2147483648 && <= 2147483647
"
);
}
});
test
({
name
:
"
setPriority(): priority must be an integer between -20 and 19
"
,
fn
()
{
assertThrows
(
()
=>
{
os
.
setPriority
(
0
,
3.15
);
},
Error
,
"
priority must be 'an integer'
"
);
assertThrows
(
()
=>
{
os
.
setPriority
(
0
,
-
21
);
},
Error
,
"
priority must be >= -20 && <= 19
"
);
assertThrows
(
()
=>
{
os
.
setPriority
(
0
,
20
);
},
Error
,
"
priority must be >= -20 && <= 19
"
);
assertThrows
(
()
=>
{
os
.
setPriority
(
0
,
9999999999
);
},
Error
,
"
priority must be >= -20 && <= 19
"
);
}
});
test
({
name
:
"
setPriority(): if only one argument specified, then this is the priority, NOT the pid
"
,
fn
()
{
assertThrows
(
()
=>
{
os
.
setPriority
(
3.15
);
},
Error
,
"
priority must be 'an integer'
"
);
assertThrows
(
()
=>
{
os
.
setPriority
(
-
21
);
},
Error
,
"
priority must be >= -20 && <= 19
"
);
assertThrows
(
()
=>
{
os
.
setPriority
(
20
);
},
Error
,
"
priority must be >= -20 && <= 19
"
);
assertThrows
(
()
=>
{
os
.
setPriority
(
9999999999
);
},
Error
,
"
priority must be >= -20 && <= 19
"
);
}
});
test
({
name
:
"
Signals are as expected
"
,
fn
()
{
// Test a few random signals for equality
assertEquals
(
os
.
constants
.
signals
.
SIGKILL
,
Deno
.
Signal
.
SIGKILL
);
assertEquals
(
os
.
constants
.
signals
.
SIGCONT
,
Deno
.
Signal
.
SIGCONT
);
assertEquals
(
os
.
constants
.
signals
.
SIGXFSZ
,
Deno
.
Signal
.
SIGXFSZ
);
}
});
test
({
name
:
"
EOL is as expected
"
,
fn
()
{
assert
(
os
.
EOL
==
"
\r\n
"
||
os
.
EOL
==
"
\n
"
);
}
});
// Method is currently implemented correctly for windows but not for any other os
test
({
name
:
"
Load average is an array of 3 numbers
"
,
fn
()
{
try
{
const
result
=
os
.
loadavg
();
assert
(
result
.
length
==
3
);
assertEquals
(
typeof
result
[
0
],
"
number
"
);
assertEquals
(
typeof
result
[
1
],
"
number
"
);
assertEquals
(
typeof
result
[
2
],
"
number
"
);
}
catch
(
error
)
{
if
(
!
(
Object
.
getPrototypeOf
(
error
)
===
Error
.
prototype
))
{
const
errMsg
=
`Unexpected error class:
${
error
.
name
}
`
;
throw
new
AssertionError
(
errMsg
);
}
else
if
(
!
error
.
message
.
includes
(
"
Not implemented
"
))
{
throw
new
AssertionError
(
"
Expected this error to contain 'Not implemented'
"
);
}
}
}
});
test
({
name
:
"
APIs not yet implemented
"
,
fn
()
{
assertThrows
(
()
=>
{
os
.
cpus
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
endianness
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
freemem
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
getPriority
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
networkInterfaces
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
platform
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
release
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
setPriority
(
0
);
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
tmpdir
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
totalmem
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
type
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
uptime
();
},
Error
,
"
Not implemented
"
);
assertThrows
(
()
=>
{
os
.
userInfo
();
},
Error
,
"
Not implemented
"
);
}
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录