Skip to content

Commit 0b0c8f9

Browse files
authored
Add DataView support, update RecognizedString type (#1258)
1 parent 1c03fa0 commit 0b0c8f9

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

docs/index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ export interface AppDescriptor {
4747
* This because we expect UTF-8, which is packed in 8-byte chars. JavaScript strings are UTF-16 internally meaning extra copies and reinterpretation are required.
4848
*
4949
* That's why all events pass data by ArrayBuffer and not JavaScript strings, as they allow zero-copy data passing.
50-
*
5150
* You can always do Buffer.from(arrayBuffer).toString(), but keeping things binary and as ArrayBuffer is preferred.
51+
*
52+
* The ArrayBufferView type includes Node.js Buffer, DataView, and TypedArray (Uint8Array, Uint16Array, ...).
5253
*/
53-
export type RecognizedString = string | ArrayBuffer | Uint8Array | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array | Float32Array | Float64Array;
54+
export type RecognizedString = string | ArrayBuffer | SharedArrayBuffer | ArrayBufferView;
5455

5556
/** A WebSocket connection that is valid from open to close event.
5657
* Read more about this in the user manual.

docs/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"files": [ "index.d.ts" ],
3+
"compilerOptions": {
4+
"lib": [ "es2017" ]
5+
},
36
"typedocOptions": {
47
"name":"uWebSockets.js v20.61.0 documentation",
58
"entryPoints": [ "index.d.ts" ],

src/Utilities.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class NativeString {
133133
utf8Value = new (utf8ValueMemory) String::Utf8Value(isolate, value);
134134
data = (**utf8Value);
135135
length = utf8Value->length();
136-
} else if (value->IsTypedArray()) {
136+
} else if (value->IsArrayBufferView()) { /* DataView or TypedArray */
137137
Local<ArrayBufferView> arrayBufferView = Local<ArrayBufferView>::Cast(value);
138138
auto contents = arrayBufferView->Buffer()->GetBackingStore();
139139
length = arrayBufferView->ByteLength();
@@ -155,7 +155,7 @@ class NativeString {
155155

156156
bool isInvalid(const FunctionCallbackInfo<Value> &args) {
157157
if (invalid) {
158-
args.GetReturnValue().Set(args.GetIsolate()->ThrowException(v8::Exception::Error(String::NewFromUtf8(args.GetIsolate(), "Text and data can only be passed by String, ArrayBuffer or TypedArray.", NewStringType::kNormal).ToLocalChecked())));
158+
args.GetReturnValue().Set(args.GetIsolate()->ThrowException(v8::Exception::Error(String::NewFromUtf8(args.GetIsolate(), "Text and data can only be passed by String, ArrayBuffer, SharedArrayBuffer or ArrayBufferView.", NewStringType::kNormal).ToLocalChecked())));
159159
}
160160
return invalid;
161161
}

0 commit comments

Comments
 (0)