@ohos-ports/stdlib-buffer 0.0.0-stage → 0.4.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +205 -2
- package/SECURITY.md +5 -0
- package/alloc-unsafe/README.md +140 -0
- package/alloc-unsafe/docs/repl.txt +28 -0
- package/alloc-unsafe/docs/types/index.d.ts +47 -0
- package/alloc-unsafe/docs/types/test.ts +44 -0
- package/alloc-unsafe/lib/has_alloc_unsafe.js +34 -0
- package/alloc-unsafe/lib/index.js +52 -0
- package/alloc-unsafe/lib/main.js +56 -0
- package/alloc-unsafe/lib/polyfill.js +56 -0
- package/alloc-unsafe/package.json +67 -0
- package/ctor/README.md +182 -0
- package/ctor/docs/repl.txt +91 -0
- package/ctor/docs/types/index.d.ts +37 -0
- package/ctor/docs/types/test.ts +35 -0
- package/ctor/lib/index.js +52 -0
- package/ctor/lib/main.js +28 -0
- package/ctor/lib/polyfill.js +37 -0
- package/ctor/package.json +62 -0
- package/docs/types/index.d.ts +189 -0
- package/from-array/README.md +141 -0
- package/from-array/docs/repl.txt +22 -0
- package/from-array/docs/types/index.d.ts +42 -0
- package/from-array/docs/types/test.ts +42 -0
- package/from-array/lib/has_from.js +34 -0
- package/from-array/lib/index.js +52 -0
- package/from-array/lib/main.js +53 -0
- package/from-array/lib/polyfill.js +53 -0
- package/from-array/package.json +69 -0
- package/from-arraybuffer/README.md +198 -0
- package/from-arraybuffer/docs/repl.txt +48 -0
- package/from-arraybuffer/docs/types/index.d.ts +64 -0
- package/from-arraybuffer/docs/types/test.ts +72 -0
- package/from-arraybuffer/lib/has_from.js +35 -0
- package/from-arraybuffer/lib/index.js +54 -0
- package/from-arraybuffer/lib/main.js +105 -0
- package/from-arraybuffer/lib/node_version.js +33 -0
- package/from-arraybuffer/lib/polyfill.js +107 -0
- package/from-arraybuffer/package.json +69 -0
- package/from-buffer/README.md +145 -0
- package/from-buffer/docs/repl.txt +23 -0
- package/from-buffer/docs/types/index.d.ts +45 -0
- package/from-buffer/docs/types/test.ts +47 -0
- package/from-buffer/lib/has_from.js +34 -0
- package/from-buffer/lib/index.js +56 -0
- package/from-buffer/lib/main.js +56 -0
- package/from-buffer/lib/polyfill.js +56 -0
- package/from-buffer/package.json +67 -0
- package/from-string/README.md +158 -0
- package/from-string/docs/repl.txt +28 -0
- package/from-string/docs/types/index.d.ts +42 -0
- package/from-string/docs/types/test.ts +55 -0
- package/from-string/lib/has_from.js +34 -0
- package/from-string/lib/index.js +52 -0
- package/from-string/lib/main.js +60 -0
- package/from-string/lib/polyfill.js +60 -0
- package/from-string/package.json +66 -0
- package/lib/index.js +114 -0
- package/package.json +63 -4
- package/reviver/README.md +137 -0
- package/reviver/docs/repl.txt +32 -0
- package/reviver/docs/types/index.d.ts +41 -0
- package/reviver/docs/types/test.ts +49 -0
- package/reviver/lib/index.js +43 -0
- package/reviver/lib/main.js +58 -0
- package/reviver/package.json +73 -0
- package/to-json/README.md +142 -0
- package/to-json/docs/repl.txt +31 -0
- package/to-json/docs/types/index.d.ts +46 -0
- package/to-json/docs/types/test.ts +47 -0
- package/to-json/lib/index.js +44 -0
- package/to-json/lib/main.js +63 -0
- package/to-json/package.json +69 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isCollection = require( '@ohos-ports/stdlib-assert/is-collection' );
|
|
24
|
+
var format = require( '@ohos-ports/stdlib-string/format' );
|
|
25
|
+
var Buffer = require( './../../ctor' );
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// MAIN //
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Allocates a buffer using an octet array.
|
|
32
|
+
*
|
|
33
|
+
* @param {Collection} arr - octet array
|
|
34
|
+
* @throws {TypeError} must provide an array-like object
|
|
35
|
+
* @returns {Buffer} new `Buffer` instance
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* var fromArray = require( '@ohos-ports/stdlib-buffer/from-array' );
|
|
39
|
+
*
|
|
40
|
+
* var buf = fromArray( [ 1, 2, 3, 4 ] );
|
|
41
|
+
* // returns <Buffer>
|
|
42
|
+
*/
|
|
43
|
+
function fromArray( arr ) {
|
|
44
|
+
if ( !isCollection( arr ) ) {
|
|
45
|
+
throw new TypeError( format( 'invalid argument. Must provide an array-like object. Value: `%s`.', arr ) );
|
|
46
|
+
}
|
|
47
|
+
return new Buffer( arr ); // eslint-disable-line no-buffer-constructor
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
// EXPORTS //
|
|
52
|
+
|
|
53
|
+
module.exports = fromArray;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/buffer/from-array",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Allocate a buffer using an octet array.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"directories": {
|
|
18
|
+
"benchmark": "./benchmark",
|
|
19
|
+
"doc": "./docs",
|
|
20
|
+
"example": "./examples",
|
|
21
|
+
"lib": "./lib",
|
|
22
|
+
"test": "./test"
|
|
23
|
+
},
|
|
24
|
+
"types": "./docs/types",
|
|
25
|
+
"scripts": {},
|
|
26
|
+
"homepage": "https://github.com/stdlib-js/stdlib",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git://github.com/stdlib-js/stdlib.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {},
|
|
35
|
+
"devDependencies": {},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=0.10.0",
|
|
38
|
+
"npm": ">2.7.0"
|
|
39
|
+
},
|
|
40
|
+
"os": [
|
|
41
|
+
"aix",
|
|
42
|
+
"darwin",
|
|
43
|
+
"freebsd",
|
|
44
|
+
"linux",
|
|
45
|
+
"macos",
|
|
46
|
+
"openbsd",
|
|
47
|
+
"openharmony",
|
|
48
|
+
"sunos",
|
|
49
|
+
"win32",
|
|
50
|
+
"windows"
|
|
51
|
+
],
|
|
52
|
+
"keywords": [
|
|
53
|
+
"stdlib",
|
|
54
|
+
"stdtypes",
|
|
55
|
+
"types",
|
|
56
|
+
"data",
|
|
57
|
+
"structure",
|
|
58
|
+
"buffer",
|
|
59
|
+
"buf",
|
|
60
|
+
"binary",
|
|
61
|
+
"octets",
|
|
62
|
+
"octet",
|
|
63
|
+
"array",
|
|
64
|
+
"typed",
|
|
65
|
+
"typed-array",
|
|
66
|
+
"allocate",
|
|
67
|
+
"uint8"
|
|
68
|
+
]
|
|
69
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2018 The Stdlib Authors.
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
-->
|
|
20
|
+
|
|
21
|
+
# arraybuffer2buffer
|
|
22
|
+
|
|
23
|
+
> Allocate a [buffer][@stdlib/buffer/ctor] from an [ArrayBuffer][@stdlib/array/buffer].
|
|
24
|
+
|
|
25
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
26
|
+
|
|
27
|
+
<section class="intro">
|
|
28
|
+
|
|
29
|
+
</section>
|
|
30
|
+
|
|
31
|
+
<!-- /.intro -->
|
|
32
|
+
|
|
33
|
+
<!-- Package usage documentation. -->
|
|
34
|
+
|
|
35
|
+
<section class="usage">
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
var arraybuffer2buffer = require( '@stdlib/buffer/from-arraybuffer' );
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### arraybuffer2buffer( buf\[, byteOffset\[, length]] )
|
|
44
|
+
|
|
45
|
+
Allocates a [buffer][@stdlib/buffer/ctor] from an [`ArrayBuffer`][@stdlib/array/buffer].
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
var ArrayBuffer = require( '@stdlib/array/buffer' );
|
|
49
|
+
var ab = new ArrayBuffer( 10 );
|
|
50
|
+
|
|
51
|
+
var buf = arraybuffer2buffer( ab );
|
|
52
|
+
// returns <Buffer>
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
By default, the function allocates a [buffer][@stdlib/buffer/ctor] starting from the first byte of the provided [`ArrayBuffer`][@stdlib/array/buffer]. To specify an alternative starting index, provide a `byteOffset`.
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
var ArrayBuffer = require( '@stdlib/array/buffer' );
|
|
59
|
+
var ab = new ArrayBuffer( 10 );
|
|
60
|
+
|
|
61
|
+
var buf = arraybuffer2buffer( ab, 2 );
|
|
62
|
+
// returns <Buffer>
|
|
63
|
+
|
|
64
|
+
var len = buf.length;
|
|
65
|
+
// returns 8
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
By default, the function allocates a [buffer][@stdlib/buffer/ctor] until the end of the provided [`ArrayBuffer`][@stdlib/array/buffer]. To allocate a specified number of [`ArrayBuffer`][@stdlib/array/buffer] bytes, provide a `length`.
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
var ArrayBuffer = require( '@stdlib/array/buffer' );
|
|
72
|
+
var ab = new ArrayBuffer( 10 );
|
|
73
|
+
|
|
74
|
+
var buf = arraybuffer2buffer( ab, 2, 4 );
|
|
75
|
+
// returns <Buffer>
|
|
76
|
+
|
|
77
|
+
var len = buf.length;
|
|
78
|
+
// returns 4
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
</section>
|
|
82
|
+
|
|
83
|
+
<!-- /.usage -->
|
|
84
|
+
|
|
85
|
+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
86
|
+
|
|
87
|
+
<section class="notes">
|
|
88
|
+
|
|
89
|
+
## Notes
|
|
90
|
+
|
|
91
|
+
The behavior of this function varies across Node.js versions due to changes in the underlying Node.js APIs:
|
|
92
|
+
|
|
93
|
+
- `<3.0.0`: the function copies [`ArrayBuffer`][@stdlib/array/buffer] bytes to a new [`Buffer`][@stdlib/buffer/ctor] instance.
|
|
94
|
+
- `>=3.0.0` and `<5.10.0`: if provided a `byteOffset`, the function copies [`ArrayBuffer`][@stdlib/array/buffer] bytes to a new [`Buffer`][@stdlib/buffer/ctor] instance; otherwise, the function returns a view of an [`ArrayBuffer`][@stdlib/array/buffer] without copying the underlying memory.
|
|
95
|
+
- `<6.0.0`: if provided an empty [`ArrayBuffer`][@stdlib/array/buffer], the function returns an empty [`Buffer`][@stdlib/buffer/ctor] which is **not** an [`ArrayBuffer`][@stdlib/array/buffer] view.
|
|
96
|
+
- `>=6.0.0`: the function returns a view of an [`ArrayBuffer`][@stdlib/array/buffer] without copying the underlying memory.
|
|
97
|
+
|
|
98
|
+
</section>
|
|
99
|
+
|
|
100
|
+
<!-- /.notes -->
|
|
101
|
+
|
|
102
|
+
<!-- Package usage examples. -->
|
|
103
|
+
|
|
104
|
+
<section class="examples">
|
|
105
|
+
|
|
106
|
+
## Examples
|
|
107
|
+
|
|
108
|
+
<!-- eslint no-undef: "error" -->
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
var ArrayBuffer = require( '@stdlib/array/buffer' );
|
|
112
|
+
var Float64Array = require( '@stdlib/array/float64' );
|
|
113
|
+
var randu = require( '@stdlib/random/base/randu' );
|
|
114
|
+
var IS_LITTLE_ENDIAN = require( '@stdlib/assert/is-little-endian' );
|
|
115
|
+
var arraybuffer2buffer = require( '@stdlib/buffer/from-arraybuffer' );
|
|
116
|
+
|
|
117
|
+
var high;
|
|
118
|
+
var view;
|
|
119
|
+
var low;
|
|
120
|
+
var buf;
|
|
121
|
+
var ab;
|
|
122
|
+
var i;
|
|
123
|
+
|
|
124
|
+
// Allocate an ArrayBuffer:
|
|
125
|
+
ab = new ArrayBuffer( 64 );
|
|
126
|
+
|
|
127
|
+
// Create a Float64 view and set random values:
|
|
128
|
+
view = new Float64Array( ab );
|
|
129
|
+
for ( i = 0; i < view.length; i++ ) {
|
|
130
|
+
view[ i ] = randu();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Create a new buffer from the ArrayBuffer:
|
|
134
|
+
buf = arraybuffer2buffer( ab );
|
|
135
|
+
|
|
136
|
+
// Read the high and low words for each double:
|
|
137
|
+
for ( i = 0; i < view.length; i++ ) {
|
|
138
|
+
if ( IS_LITTLE_ENDIAN ) {
|
|
139
|
+
high = buf.readUInt32LE( (8*i)+4 );
|
|
140
|
+
low = buf.readUInt32LE( 8*i );
|
|
141
|
+
} else {
|
|
142
|
+
high = buf.readUInt32BE( 8*i );
|
|
143
|
+
low = buf.readUInt32BE( (8*i)+4 );
|
|
144
|
+
}
|
|
145
|
+
console.log( 'Value: %d. High: %d. Low: %d.', view[ i ], high, low );
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
</section>
|
|
150
|
+
|
|
151
|
+
<!-- /.examples -->
|
|
152
|
+
|
|
153
|
+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
154
|
+
|
|
155
|
+
<section class="references">
|
|
156
|
+
|
|
157
|
+
</section>
|
|
158
|
+
|
|
159
|
+
<!-- /.references -->
|
|
160
|
+
|
|
161
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
162
|
+
|
|
163
|
+
<section class="related">
|
|
164
|
+
|
|
165
|
+
* * *
|
|
166
|
+
|
|
167
|
+
## See Also
|
|
168
|
+
|
|
169
|
+
- <span class="package-name">[`@stdlib/buffer/ctor`][@stdlib/buffer/ctor]</span><span class="delimiter">: </span><span class="description">Buffer.</span>
|
|
170
|
+
- <span class="package-name">[`@stdlib/buffer/from-array`][@stdlib/buffer/from-array]</span><span class="delimiter">: </span><span class="description">allocate a buffer using an octet array.</span>
|
|
171
|
+
- <span class="package-name">[`@stdlib/buffer/from-buffer`][@stdlib/buffer/from-buffer]</span><span class="delimiter">: </span><span class="description">copy buffer data to a new Buffer instance.</span>
|
|
172
|
+
- <span class="package-name">[`@stdlib/buffer/from-string`][@stdlib/buffer/from-string]</span><span class="delimiter">: </span><span class="description">allocate a buffer containing a provided string.</span>
|
|
173
|
+
|
|
174
|
+
</section>
|
|
175
|
+
|
|
176
|
+
<!-- /.related -->
|
|
177
|
+
|
|
178
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
179
|
+
|
|
180
|
+
<section class="links">
|
|
181
|
+
|
|
182
|
+
[@stdlib/array/buffer]: https://www.npmjs.com/package/@stdlib/array-buffer
|
|
183
|
+
|
|
184
|
+
<!-- <related-links> -->
|
|
185
|
+
|
|
186
|
+
[@stdlib/buffer/ctor]: https://github.com/stdlib-js/buffer/tree/main/ctor
|
|
187
|
+
|
|
188
|
+
[@stdlib/buffer/from-array]: https://github.com/stdlib-js/buffer/tree/main/from-array
|
|
189
|
+
|
|
190
|
+
[@stdlib/buffer/from-buffer]: https://github.com/stdlib-js/buffer/tree/main/from-buffer
|
|
191
|
+
|
|
192
|
+
[@stdlib/buffer/from-string]: https://github.com/stdlib-js/buffer/tree/main/from-string
|
|
193
|
+
|
|
194
|
+
<!-- </related-links> -->
|
|
195
|
+
|
|
196
|
+
</section>
|
|
197
|
+
|
|
198
|
+
<!-- /.links -->
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
|
|
2
|
+
{{alias}}( buf[, byteOffset[, length]] )
|
|
3
|
+
Allocates a buffer from an ArrayBuffer.
|
|
4
|
+
|
|
5
|
+
The behavior of this function varies across Node.js versions due to changes
|
|
6
|
+
in the underlying Node.js APIs:
|
|
7
|
+
|
|
8
|
+
- <3.0.0: the function copies ArrayBuffer bytes to a new Buffer instance.
|
|
9
|
+
- >=3.0.0 and <5.10.0: if provided a byte offset, the function copies
|
|
10
|
+
ArrayBuffer bytes to a new Buffer instance; otherwise, the function
|
|
11
|
+
returns a view of an ArrayBuffer without copying the underlying memory.
|
|
12
|
+
- <6.0.0: if provided an empty ArrayBuffer, the function returns an empty
|
|
13
|
+
Buffer which is not an ArrayBuffer view.
|
|
14
|
+
- >=6.0.0: the function returns a view of an ArrayBuffer without copying
|
|
15
|
+
the underlying memory.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
buf: ArrayBuffer
|
|
20
|
+
Input array buffer.
|
|
21
|
+
|
|
22
|
+
byteOffset: integer (optional)
|
|
23
|
+
Index offset specifying the location of the first byte.
|
|
24
|
+
|
|
25
|
+
length: integer (optional)
|
|
26
|
+
Number of bytes to expose from the underlying ArrayBuffer.
|
|
27
|
+
|
|
28
|
+
Returns
|
|
29
|
+
-------
|
|
30
|
+
out: Buffer
|
|
31
|
+
Buffer instance.
|
|
32
|
+
|
|
33
|
+
Examples
|
|
34
|
+
--------
|
|
35
|
+
> var ab = new {{alias:@stdlib/array/buffer}}( 10 )
|
|
36
|
+
<ArrayBuffer>
|
|
37
|
+
> var buf = {{alias}}( ab )
|
|
38
|
+
<Buffer>
|
|
39
|
+
> var len = buf.length
|
|
40
|
+
10
|
|
41
|
+
> buf = {{alias}}( ab, 2, 6 )
|
|
42
|
+
<Buffer>
|
|
43
|
+
> len = buf.length
|
|
44
|
+
6
|
|
45
|
+
|
|
46
|
+
See Also
|
|
47
|
+
--------
|
|
48
|
+
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// TypeScript Version: 4.1
|
|
20
|
+
|
|
21
|
+
/// <reference types="node"/>
|
|
22
|
+
|
|
23
|
+
import { Buffer } from 'buffer';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Allocates a buffer from an `ArrayBuffer`.
|
|
27
|
+
*
|
|
28
|
+
* ## Notes
|
|
29
|
+
*
|
|
30
|
+
* The behavior of this function varies across Node.js versions due to changes in the underlying Node.js APIs:
|
|
31
|
+
*
|
|
32
|
+
* - `<6.0.0`: if provided an empty ArrayBuffer, the function returns an empty Buffer which is **not** an ArrayBuffer view.
|
|
33
|
+
* - otherwise, the function returns a view of an ArrayBuffer without copying the underlying memory.
|
|
34
|
+
*
|
|
35
|
+
*
|
|
36
|
+
* @param buf - ArrayBuffer instance
|
|
37
|
+
* @param byteOffset - index specifying the location of the first buffer byte (default: 0)
|
|
38
|
+
* @param length - number of buffer bytes (default: buf.byteLength)
|
|
39
|
+
* @throws second argument must be a nonnegative integer
|
|
40
|
+
* @throws second argument must not exceed number of bytes in input ArrayBuffer
|
|
41
|
+
* @throws last argument must be a nonnegative integer
|
|
42
|
+
* @throws last argument must not exceed number of bytes in input ArrayBuffer
|
|
43
|
+
* @returns new `Buffer` instance
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* var ArrayBuffer = require( '@stdlib/array/buffer' );
|
|
47
|
+
* var ab = new ArrayBuffer( 10 );
|
|
48
|
+
*
|
|
49
|
+
* var buf = fromArrayBuffer( ab );
|
|
50
|
+
* // returns <Buffer>
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* var ArrayBuffer = require( '@stdlib/array/buffer' );
|
|
54
|
+
* var ab = new ArrayBuffer( 10 );
|
|
55
|
+
*
|
|
56
|
+
* var buf = fromArrayBuffer( ab, 2, 4 );
|
|
57
|
+
* // returns <Buffer>
|
|
58
|
+
*/
|
|
59
|
+
declare function fromArrayBuffer( buf: ArrayBuffer, byteOffset?: number, length?: number ): Buffer;
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
// EXPORTS //
|
|
63
|
+
|
|
64
|
+
export = fromArrayBuffer;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import fromArrayBuffer = require( './index' );
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
// TESTS //
|
|
23
|
+
|
|
24
|
+
// The function returns a buffer...
|
|
25
|
+
{
|
|
26
|
+
const buf = new ArrayBuffer( 10 );
|
|
27
|
+
fromArrayBuffer( buf ); // $ExpectType Buffer
|
|
28
|
+
fromArrayBuffer( buf, 2 ); // $ExpectType Buffer
|
|
29
|
+
fromArrayBuffer( buf, 2, 4 ); // $ExpectType Buffer
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// The compiler throws an error if the function is provided a first argument which is not an array buffer...
|
|
33
|
+
{
|
|
34
|
+
fromArrayBuffer( 'abc' ); // $ExpectError
|
|
35
|
+
fromArrayBuffer( 123 ); // $ExpectError
|
|
36
|
+
fromArrayBuffer( false ); // $ExpectError
|
|
37
|
+
fromArrayBuffer( true ); // $ExpectError
|
|
38
|
+
fromArrayBuffer( null ); // $ExpectError
|
|
39
|
+
fromArrayBuffer( {} ); // $ExpectError
|
|
40
|
+
fromArrayBuffer( ( x: number ): number => x ); // $ExpectError
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// The compiler throws an error if the function is provided a second argument which is not a number...
|
|
44
|
+
{
|
|
45
|
+
const buf = new ArrayBuffer( 10 );
|
|
46
|
+
fromArrayBuffer( buf, 'abc' ); // $ExpectError
|
|
47
|
+
fromArrayBuffer( buf, false ); // $ExpectError
|
|
48
|
+
fromArrayBuffer( buf, true ); // $ExpectError
|
|
49
|
+
fromArrayBuffer( buf, null ); // $ExpectError
|
|
50
|
+
fromArrayBuffer( buf, [] ); // $ExpectError
|
|
51
|
+
fromArrayBuffer( buf, {} ); // $ExpectError
|
|
52
|
+
fromArrayBuffer( buf, ( x: number ): number => x ); // $ExpectError
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// The compiler throws an error if the function is provided a third argument which is not a number...
|
|
56
|
+
{
|
|
57
|
+
const buf = new ArrayBuffer( 10 );
|
|
58
|
+
fromArrayBuffer( buf, 2, 'abc' ); // $ExpectError
|
|
59
|
+
fromArrayBuffer( buf, 2, false ); // $ExpectError
|
|
60
|
+
fromArrayBuffer( buf, 2, true ); // $ExpectError
|
|
61
|
+
fromArrayBuffer( buf, 2, null ); // $ExpectError
|
|
62
|
+
fromArrayBuffer( buf, 2, [] ); // $ExpectError
|
|
63
|
+
fromArrayBuffer( buf, 2, {} ); // $ExpectError
|
|
64
|
+
fromArrayBuffer( buf, 2, ( x: number ): number => x ); // $ExpectError
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// The compiler throws an error if the function is provided an invalid number of arguments...
|
|
68
|
+
{
|
|
69
|
+
const buf = new ArrayBuffer( 10 );
|
|
70
|
+
fromArrayBuffer(); // $ExpectError
|
|
71
|
+
fromArrayBuffer( buf, 2, 4, 2 ); // $ExpectError
|
|
72
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isFunction = require( '@ohos-ports/stdlib-assert/is-function' );
|
|
24
|
+
var Buffer = require( './../../ctor' );
|
|
25
|
+
var version = require( './node_version.js' );
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// MAIN //
|
|
29
|
+
|
|
30
|
+
var bool = isFunction( Buffer.from ) && version >= 5;
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
// EXPORTS //
|
|
34
|
+
|
|
35
|
+
module.exports = bool;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Allocate a buffer from an `ArrayBuffer`.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/buffer/from-arraybuffer
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var ArrayBuffer = require( '@ohos-ports/stdlib-array/buffer' );
|
|
28
|
+
* var arraybuffer2buffer = require( '@ohos-ports/stdlib-buffer/from-arraybuffer' );
|
|
29
|
+
*
|
|
30
|
+
* var ab = new ArrayBuffer( 10 );
|
|
31
|
+
* var buf = arraybuffer2buffer( ab );
|
|
32
|
+
* // returns <Buffer>
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
// MODULES //
|
|
36
|
+
|
|
37
|
+
var hasFrom = require( './has_from.js' );
|
|
38
|
+
var main = require( './main.js' );
|
|
39
|
+
var polyfill = require( './polyfill.js' );
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
// MAIN //
|
|
43
|
+
|
|
44
|
+
var arraybuffer2buffer;
|
|
45
|
+
if ( hasFrom ) {
|
|
46
|
+
arraybuffer2buffer = main;
|
|
47
|
+
} else {
|
|
48
|
+
arraybuffer2buffer = polyfill;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
// EXPORTS //
|
|
53
|
+
|
|
54
|
+
module.exports = arraybuffer2buffer;
|