handle Uint8Array

This commit is contained in:
Robert Brignull 2020-06-23 17:17:11 +01:00
parent 350bf488da
commit 52e52435f7
3 changed files with 45 additions and 9 deletions

23
lib/testing-utils.js generated
View file

@ -1,9 +1,26 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function wrapOutput(context) {
return (str) => {
if (typeof str === 'string') {
context.testOutput += str;
// Function signature taken from Socket.write.
// Note there are two overloads:
// write(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean;
// write(str: Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean;
return (chunk, encoding, cb) => {
// Work out which method overload we are in
if (cb === undefined && typeof encoding === 'function') {
cb = encoding;
encoding = undefined;
}
// Record the output
if (typeof chunk === 'string') {
context.testOutput += chunk;
}
else {
context.testOutput += new TextDecoder(encoding || 'utf-8').decode(chunk);
}
// Satisfy contract by calling callback when done
if (cb !== undefined && typeof cb === 'function') {
cb();
}
return true;
};