From 6557df29122c328745b169ed7fe57f4333b40bc8 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Sat, 28 Jan 2023 01:00:26 -0800 Subject: [PATCH] Fixes #1913 --- src/bun.js/bindings/JSStringDecoder.cpp | 116 +++- test/bun.js/string-decoder.test.js | 498 ++++++++++-------- .../third-party/body-parser-test/bun.lockb | Bin 0 -> 20925 bytes .../express-body-parser-test.test.ts | 61 +++ .../third-party/body-parser-test/package.json | 9 + 5 files changed, 451 insertions(+), 233 deletions(-) create mode 100755 test/bun.js/third-party/body-parser-test/bun.lockb create mode 100644 test/bun.js/third-party/body-parser-test/express-body-parser-test.test.ts create mode 100644 test/bun.js/third-party/body-parser-test/package.json diff --git a/src/bun.js/bindings/JSStringDecoder.cpp b/src/bun.js/bindings/JSStringDecoder.cpp index c73378fdf2..96fbc3a570 100644 --- a/src/bun.js/bindings/JSStringDecoder.cpp +++ b/src/bun.js/bindings/JSStringDecoder.cpp @@ -21,6 +21,46 @@ static JSC_DECLARE_CUSTOM_GETTER(jsStringDecoder_lastChar); static JSC_DECLARE_CUSTOM_GETTER(jsStringDecoder_lastNeed); static JSC_DECLARE_CUSTOM_GETTER(jsStringDecoder_lastTotal); +static inline EncodedJSValue jsStringDecoderCast(JSGlobalObject* globalObject, JSValue stringDecoderValue) +{ + if (LIKELY(jsDynamicCast(stringDecoderValue))) + return JSValue::encode(stringDecoderValue); + + auto& vm = globalObject->vm(); + auto throwScope = DECLARE_THROW_SCOPE(vm); + + if (stringDecoderValue.isEmpty() || stringDecoderValue.isUndefinedOrNull()) { + return JSC::JSValue::encode(jsUndefined()); + } + + if (!stringDecoderValue.isObject()) { + return throwThisTypeError(*globalObject, throwScope, JSStringDecoder::info()->className, "write"); + } + + JSC::JSObject* thisObject = JSC::asObject(stringDecoderValue); + JSStringDecoder* castedThis = nullptr; + auto clientData = WebCore::clientData(vm); + if (JSValue existingDecoderValue = thisObject->getIfPropertyExists(globalObject, clientData->builtinNames().decodePrivateName())) { + castedThis = jsDynamicCast(existingDecoderValue); + } + + if (!castedThis) { + BufferEncodingType encoding = BufferEncodingType::utf8; + if (JSValue encodingValue = thisObject->getIfPropertyExists(globalObject, clientData->builtinNames().encodingPrivateName())) { + if (encodingValue.isString()) { + std::optional opt = parseEnumeration(*globalObject, encodingValue); + if (opt.has_value()) { + encoding = opt.value(); + } + } + } + castedThis = JSStringDecoder::create(globalObject->vm(), globalObject, reinterpret_cast(globalObject)->JSStringDecoderStructure(), encoding); + thisObject->putDirect(vm, clientData->builtinNames().decodePrivateName(), castedThis, 0); + } + + return JSValue::encode(castedThis); +} + void JSStringDecoder::finishCreation(JSC::VM& vm, JSC::JSGlobalObject* globalObject) { Base::finishCreation(vm); @@ -299,7 +339,7 @@ JSC::GCClient::IsoSubspace* JSStringDecoder::subspaceForImpl(JSC::VM& vm) STATIC_ASSERT_ISO_SUBSPACE_SHARABLE(JSStringDecoderPrototype, JSStringDecoderPrototype::Base); -static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_writeBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation::ClassParameter castedThis) +static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_writeBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, JSStringDecoder* castedThis) { auto& vm = JSC::getVM(lexicalGlobalObject); auto throwScope = DECLARE_THROW_SCOPE(vm); @@ -324,7 +364,7 @@ static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_writeBody(JSC } RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(castedThis->write(vm, lexicalGlobalObject, reinterpret_cast(view->vector()), view->byteLength()))); } -static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_endBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation::ClassParameter castedThis) +static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_endBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, JSStringDecoder* castedThis) { auto& vm = JSC::getVM(lexicalGlobalObject); auto throwScope = DECLARE_THROW_SCOPE(vm); @@ -340,7 +380,7 @@ static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_endBody(JSC:: } RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(castedThis->end(vm, lexicalGlobalObject, reinterpret_cast(view->vector()), view->byteLength()))); } -static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_textBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation::ClassParameter castedThis) +static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_textBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, JSStringDecoder* castedThis) { auto& vm = JSC::getVM(lexicalGlobalObject); auto throwScope = DECLARE_THROW_SCOPE(vm); @@ -366,24 +406,44 @@ static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_textBody(JSC: static JSC_DEFINE_HOST_FUNCTION(jsStringDecoderPrototypeFunction_write, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) { - return IDLOperation::call(*globalObject, *callFrame, "write"); + JSValue stringDecoderValue = JSValue::decode(jsStringDecoderCast(globalObject, callFrame->thisValue())); + if (stringDecoderValue.isEmpty() || !stringDecoderValue.isCell()) { + return JSValue::encode(stringDecoderValue); + } + JSStringDecoder* castedThis = jsCast(stringDecoderValue); + return jsStringDecoderPrototypeFunction_writeBody(globalObject, callFrame, castedThis); } static JSC_DEFINE_HOST_FUNCTION(jsStringDecoderPrototypeFunction_end, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) { - return IDLOperation::call(*globalObject, *callFrame, "end"); + JSValue stringDecoderValue = JSValue::decode(jsStringDecoderCast(globalObject, callFrame->thisValue())); + if (stringDecoderValue.isEmpty() || !stringDecoderValue.isCell()) { + return JSValue::encode(stringDecoderValue); + } + JSStringDecoder* castedThis = jsCast(stringDecoderValue); + return jsStringDecoderPrototypeFunction_endBody(globalObject, callFrame, castedThis); } static JSC_DEFINE_HOST_FUNCTION(jsStringDecoderPrototypeFunction_text, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) { - return IDLOperation::call(*globalObject, *callFrame, "text"); + JSValue stringDecoderValue = JSValue::decode(jsStringDecoderCast(globalObject, callFrame->thisValue())); + if (stringDecoderValue.isEmpty() || !stringDecoderValue.isCell()) { + return JSValue::encode(stringDecoderValue); + } + JSStringDecoder* castedThis = jsCast(stringDecoderValue); + + return jsStringDecoderPrototypeFunction_textBody(globalObject, callFrame, castedThis); } static JSC_DEFINE_CUSTOM_GETTER(jsStringDecoder_lastChar, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) { auto& vm = JSC::getVM(lexicalGlobalObject); + JSValue stringDecoderValue = JSValue::decode(jsStringDecoderCast(lexicalGlobalObject, JSValue::decode(thisValue))); + if (stringDecoderValue.isEmpty() || !stringDecoderValue.isCell()) { + return JSValue::encode(stringDecoderValue); + } + JSStringDecoder* thisObject = jsCast(stringDecoderValue); auto throwScope = DECLARE_THROW_SCOPE(vm); - JSStringDecoder* thisObject = jsCast(JSValue::decode(thisValue)); auto buffer = ArrayBuffer::createFromBytes(thisObject->m_lastChar, 4, nullptr); auto* globalObject = reinterpret_cast(lexicalGlobalObject); JSC::JSUint8Array* uint8Array = JSC::JSUint8Array::create(lexicalGlobalObject, globalObject->JSBufferSubclassStructure(), WTFMove(buffer), 0, 4); @@ -392,15 +452,23 @@ static JSC_DEFINE_CUSTOM_GETTER(jsStringDecoder_lastChar, (JSGlobalObject * lexi static JSC_DEFINE_CUSTOM_GETTER(jsStringDecoder_lastNeed, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) { auto& vm = JSC::getVM(lexicalGlobalObject); + JSValue stringDecoderValue = JSValue::decode(jsStringDecoderCast(lexicalGlobalObject, JSValue::decode(thisValue))); + if (stringDecoderValue.isEmpty() || !stringDecoderValue.isCell()) { + return JSValue::encode(stringDecoderValue); + } + JSStringDecoder* thisObject = jsCast(stringDecoderValue); auto throwScope = DECLARE_THROW_SCOPE(vm); - JSStringDecoder* thisObject = jsCast(JSValue::decode(thisValue)); RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(JSC::jsNumber(thisObject->m_lastNeed))); } static JSC_DEFINE_CUSTOM_GETTER(jsStringDecoder_lastTotal, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName attributeName)) { auto& vm = JSC::getVM(lexicalGlobalObject); + JSValue stringDecoderValue = JSValue::decode(jsStringDecoderCast(lexicalGlobalObject, JSValue::decode(thisValue))); + if (stringDecoderValue.isEmpty() || !stringDecoderValue.isCell()) { + return JSValue::encode(stringDecoderValue); + } + JSStringDecoder* thisObject = jsCast(stringDecoderValue); auto throwScope = DECLARE_THROW_SCOPE(vm); - JSStringDecoder* thisObject = jsCast(JSValue::decode(thisValue)); RELEASE_AND_RETURN(throwScope, JSC::JSValue::encode(JSC::jsNumber(thisObject->m_lastTotal))); } @@ -452,13 +520,37 @@ JSC::EncodedJSValue JSStringDecoderConstructor::construct(JSC::JSGlobalObject* l } } } - JSStringDecoder* stringDecoder = JSStringDecoder::create( - vm, lexicalGlobalObject, reinterpret_cast(lexicalGlobalObject)->JSStringDecoderStructure(), encoding); - return JSC::JSValue::encode(stringDecoder); + JSValue thisValue = callFrame->newTarget(); + auto* globalObject = JSC::jsCast(lexicalGlobalObject); + JSObject* newTarget = asObject(thisValue); + auto* constructor = globalObject->JSStringDecoder(); + Structure* structure = globalObject->JSStringDecoderStructure(); + + JSStringDecoder* jsObject = JSStringDecoder::create( + vm, lexicalGlobalObject, structure, encoding); + + // StringDecodeer is a Weird one + // This is a hack to make express' body-parser work + // It does something weird with the prototype + // Not exactly a subclass + if (constructor != newTarget && callFrame->thisValue().isObject()) { + auto clientData = WebCore::clientData(vm); + JSObject* thisObject = asObject(callFrame->thisValue()); + + thisObject->putDirect(vm, clientData->builtinNames().decodePrivateName(), jsObject, JSC::PropertyAttribute::DontEnum | 0); + return JSC::JSValue::encode(thisObject); + } + + return JSC::JSValue::encode(jsObject); } void JSStringDecoderConstructor::initializeProperties(VM& vm, JSC::JSGlobalObject* globalObject, JSStringDecoderPrototype* prototype) { + putDirect(vm, vm.propertyNames->length, jsNumber(1), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum); + JSString* nameString = jsNontrivialString(vm, "StringDecoder"_s); + m_originalName.set(vm, this, nameString); + putDirect(vm, vm.propertyNames->name, nameString, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum); + putDirect(vm, vm.propertyNames->prototype, prototype, JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete); } const ClassInfo JSStringDecoderConstructor::s_info = { "StringDecoder"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSStringDecoderConstructor) }; diff --git a/test/bun.js/string-decoder.test.js b/test/bun.js/string-decoder.test.js index 3d330ffcd0..449f356204 100644 --- a/test/bun.js/string-decoder.test.js +++ b/test/bun.js/string-decoder.test.js @@ -1,245 +1,301 @@ import { describe, expect, it } from "bun:test"; import { withoutAggressiveGC } from "gc"; -var { StringDecoder } = require("string_decoder"); + +const RealStringDecoder = require("string_decoder").StringDecoder; it("require('string_decoder')", async () => { - expect((await import("string_decoder")).StringDecoder).toBe(StringDecoder); -}); - -it("StringDecoder-utf8", () => { - test("utf-8", Buffer.from("$", "utf-8"), "$"); - test("utf-8", Buffer.from("¢", "utf-8"), "¢"); - test("utf-8", Buffer.from("€", "utf-8"), "€"); - test("utf-8", Buffer.from("𤭢", "utf-8"), "𤭢"); - // A mixed ascii and non-ascii string - // Test stolen from deps/v8/test/cctest/test-strings.cc - // U+02E4 -> CB A4 - // U+0064 -> 64 - // U+12E4 -> E1 8B A4 - // U+0030 -> 30 - // U+3045 -> E3 81 85 - test( - "utf-8", - Buffer.from([0xcb, 0xa4, 0x64, 0xe1, 0x8b, 0xa4, 0x30, 0xe3, 0x81, 0x85]), - "\u02e4\u0064\u12e4\u0030\u3045", + expect((await import("string_decoder")).StringDecoder).toBe( + RealStringDecoder, ); }); -it("StringDecoder-ucs-2", () => { - test("ucs2", Buffer.from("ababc", "ucs2"), "ababc"); +it("Bun.inspect(StringDecoder)", async () => { + expect((await Bun.inspect(RealStringDecoder).length) > 0).toBe(true); }); -it("StringDecoder-utf16le", () => { - test("utf16le", Buffer.from("3DD84DDC", "hex"), "\ud83d\udc4d"); -}); +function FakeStringDecoderCall() { + RealStringDecoder.apply(this, arguments); +} +require("util").inherits(FakeStringDecoderCall, RealStringDecoder); -it("StringDecoder-utf8-additional", () => { - let decoder = new StringDecoder("utf8"); - expect(decoder.write(Buffer.from("E18B", "hex"))).toBe(""); - expect(decoder.end()).toBe("\ufffd"); +// extending StringDecoder is not supported +for (const StringDecoder of [FakeStringDecoderCall, RealStringDecoder]) { + describe(StringDecoder.name, () => { + it("StringDecoder-utf8", () => { + test("utf-8", Buffer.from("$", "utf-8"), "$"); + test("utf-8", Buffer.from("¢", "utf-8"), "¢"); + test("utf-8", Buffer.from("€", "utf-8"), "€"); + test("utf-8", Buffer.from("𤭢", "utf-8"), "𤭢"); + // A mixed ascii and non-ascii string + // Test stolen from deps/v8/test/cctest/test-strings.cc + // U+02E4 -> CB A4 + // U+0064 -> 64 + // U+12E4 -> E1 8B A4 + // U+0030 -> 30 + // U+3045 -> E3 81 85 + test( + "utf-8", + Buffer.from([ + 0xcb, 0xa4, 0x64, 0xe1, 0x8b, 0xa4, 0x30, 0xe3, 0x81, 0x85, + ]), + "\u02e4\u0064\u12e4\u0030\u3045", + ); + }); - decoder = new StringDecoder("utf8"); - expect(decoder.write(Buffer.from("\ufffd"))).toBe("\ufffd"); - expect(decoder.end()).toBe(""); + it("StringDecoder-ucs-2", () => { + test("ucs2", Buffer.from("ababc", "ucs2"), "ababc"); + }); - decoder = new StringDecoder("utf8"); - expect(decoder.write(Buffer.from("\ufffd\ufffd\ufffd"))).toBe( - "\ufffd\ufffd\ufffd", - ); - expect(decoder.end()).toBe(""); + it("StringDecoder-utf16le", () => { + test("utf16le", Buffer.from("3DD84DDC", "hex"), "\ud83d\udc4d"); + }); - decoder = new StringDecoder("utf8"); - expect(decoder.write(Buffer.from("EFBFBDE2", "hex"))).toBe("\ufffd"); - expect(decoder.end()).toBe("\ufffd"); + it("StringDecoder-utf8-additional", () => { + let decoder = new StringDecoder("utf8"); + expect(decoder.write(Buffer.from("E18B", "hex"))).toBe(""); + expect(decoder.end()).toBe("\ufffd"); - decoder = new StringDecoder("utf8"); - expect(decoder.write(Buffer.from("F1", "hex"))).toBe(""); - expect(decoder.write(Buffer.from("41F2", "hex"))).toBe("\ufffdA"); - expect(decoder.end()).toBe("\ufffd"); + decoder = new StringDecoder("utf8"); + expect(decoder.write(Buffer.from("\ufffd"))).toBe("\ufffd"); + expect(decoder.end()).toBe(""); - // Additional utf8Text test - decoder = new StringDecoder("utf8"); - expect(decoder.text(Buffer.from([0x41]), 2)).toBe(""); -}); + decoder = new StringDecoder("utf8"); + expect(decoder.write(Buffer.from("\ufffd\ufffd\ufffd"))).toBe( + "\ufffd\ufffd\ufffd", + ); + expect(decoder.end()).toBe(""); -it("StringDecoder-utf16le-additional", () => { - // Additional UTF-16LE surrogate pair tests - let decoder = new StringDecoder("utf16le"); - expect(decoder.write(Buffer.from("3DD8", "hex"))).toBe(""); - expect(decoder.write(Buffer.from("4D", "hex"))).toBe(""); - expect(decoder.write(Buffer.from("DC", "hex"))).toBe("\ud83d\udc4d"); - expect(decoder.end()).toBe(""); + decoder = new StringDecoder("utf8"); + expect(decoder.write(Buffer.from("EFBFBDE2", "hex"))).toBe("\ufffd"); + expect(decoder.end()).toBe("\ufffd"); - decoder = new StringDecoder("utf16le"); - expect(decoder.write(Buffer.from("3DD8", "hex"))).toBe(""); - expect(decoder.end()).toBe("\ud83d"); + decoder = new StringDecoder("utf8"); + expect(decoder.write(Buffer.from("F1", "hex"))).toBe(""); + expect(decoder.write(Buffer.from("41F2", "hex"))).toBe("\ufffdA"); + expect(decoder.end()).toBe("\ufffd"); - decoder = new StringDecoder("utf16le"); - expect(decoder.write(Buffer.from("3DD8", "hex"))).toBe(""); - expect(decoder.write(Buffer.from("4D", "hex"))).toBe(""); - expect(decoder.end()).toBe("\ud83d"); + // Additional utf8Text test + decoder = new StringDecoder("utf8"); + expect(decoder.text(Buffer.from([0x41]), 2)).toBe(""); + }); - decoder = new StringDecoder("utf16le"); - expect(decoder.write(Buffer.from("3DD84D", "hex"))).toBe("\ud83d"); - expect(decoder.end()).toBe(""); -}); + it("StringDecoder-utf16le-additional", () => { + // Additional UTF-16LE surrogate pair tests + let decoder = new StringDecoder("utf16le"); + expect(decoder.write(Buffer.from("3DD8", "hex"))).toBe(""); + expect(decoder.write(Buffer.from("4D", "hex"))).toBe(""); + expect(decoder.write(Buffer.from("DC", "hex"))).toBe("\ud83d\udc4d"); + expect(decoder.end()).toBe(""); -// Test verifies that StringDecoder will correctly decode the given input -// buffer with the given encoding to the expected output. It will attempt all -// possible ways to write() the input buffer, see writeSequences(). The -// singleSequence allows for easy debugging of a specific sequence which is -// useful in case of test failures. -function test(encoding, input, expected, singleSequence) { - withoutAggressiveGC(() => { - let sequences; - if (!singleSequence) { - sequences = writeSequences(input.length); - } else { - sequences = [singleSequence]; - } - sequences.forEach((sequence) => { - const decoder = new StringDecoder(encoding); - let output = ""; - sequence.forEach((write) => { - output += decoder.write(input.slice(write[0], write[1])); + decoder = new StringDecoder("utf16le"); + expect(decoder.write(Buffer.from("3DD8", "hex"))).toBe(""); + expect(decoder.end()).toBe("\ud83d"); + + decoder = new StringDecoder("utf16le"); + expect(decoder.write(Buffer.from("3DD8", "hex"))).toBe(""); + expect(decoder.write(Buffer.from("4D", "hex"))).toBe(""); + expect(decoder.end()).toBe("\ud83d"); + + decoder = new StringDecoder("utf16le"); + expect(decoder.write(Buffer.from("3DD84D", "hex"))).toBe("\ud83d"); + expect(decoder.end()).toBe(""); + }); + + // Test verifies that StringDecoder will correctly decode the given input + // buffer with the given encoding to the expected output. It will attempt all + // possible ways to write() the input buffer, see writeSequences(). The + // singleSequence allows for easy debugging of a specific sequence which is + // useful in case of test failures. + function test(encoding, input, expected, singleSequence) { + withoutAggressiveGC(() => { + let sequences; + if (!singleSequence) { + sequences = writeSequences(input.length); + } else { + sequences = [singleSequence]; + } + sequences.forEach((sequence) => { + const decoder = new StringDecoder(encoding); + let output = ""; + sequence.forEach((write) => { + output += decoder.write(input.slice(write[0], write[1])); + }); + output += decoder.end(); + expect(output).toBe(expected); + }); }); - output += decoder.end(); - expect(output).toBe(expected); + } + + // writeSequences returns an array of arrays that describes all possible ways a + // buffer of the given length could be split up and passed to sequential write + // calls. + // + // e.G. writeSequences(3) will return: [ + // [ [ 0, 3 ] ], + // [ [ 0, 2 ], [ 2, 3 ] ], + // [ [ 0, 1 ], [ 1, 3 ] ], + // [ [ 0, 1 ], [ 1, 2 ], [ 2, 3 ] ] + // ] + function writeSequences(length, start, sequence) { + if (start === undefined) { + start = 0; + sequence = []; + } else if (start === length) { + return [sequence]; + } + let sequences = []; + for (let end = length; end > start; end--) { + const subSequence = sequence.concat([[start, end]]); + const subSequences = writeSequences( + length, + end, + subSequence, + sequences, + ); + sequences = sequences.concat(subSequences); + } + return sequences; + } + + describe("StringDecoder.end", () => { + const encodings = [ + "base64", + "base64url", + "hex", + "utf8", + "utf16le", + "ucs2", + ]; + + const bufs = ["☃💩", "asdf"].map((b) => Buffer.from(b)); + + // Also test just arbitrary bytes from 0-15. + for (let i = 1; i <= 16; i++) { + const bytes = "." + .repeat(i - 1) + .split(".") + .map((_, j) => j + 0x78); + bufs.push(Buffer.from(bytes)); + } + + encodings.forEach(testEncoding); + + testEnd("utf8", Buffer.of(0xe2), Buffer.of(0x61), "\uFFFDa"); + testEnd("utf8", Buffer.of(0xe2), Buffer.of(0x82), "\uFFFD\uFFFD"); + testEnd("utf8", Buffer.of(0xe2), Buffer.of(0xe2), "\uFFFD\uFFFD"); + testEnd("utf8", Buffer.of(0xe2, 0x82), Buffer.of(0x61), "\uFFFDa"); + testEnd("utf8", Buffer.of(0xe2, 0x82), Buffer.of(0xac), "\uFFFD\uFFFD"); + testEnd("utf8", Buffer.of(0xe2, 0x82), Buffer.of(0xe2), "\uFFFD\uFFFD"); + testEnd("utf8", Buffer.of(0xe2, 0x82, 0xac), Buffer.of(0x61), "€a"); + + testEnd("utf16le", Buffer.of(0x3d), Buffer.of(0x61, 0x00), "a"); + testEnd( + "utf16le", + Buffer.of(0x3d), + Buffer.of(0xd8, 0x4d, 0xdc), + "\u4DD8", + ); + testEnd("utf16le", Buffer.of(0x3d, 0xd8), Buffer.of(), "\uD83D"); + testEnd( + "utf16le", + Buffer.of(0x3d, 0xd8), + Buffer.of(0x61, 0x00), + "\uD83Da", + ); + testEnd( + "utf16le", + Buffer.of(0x3d, 0xd8), + Buffer.of(0x4d, 0xdc), + "\uD83D\uDC4D", + ); + testEnd("utf16le", Buffer.of(0x3d, 0xd8, 0x4d), Buffer.of(), "\uD83D"); + testEnd( + "utf16le", + Buffer.of(0x3d, 0xd8, 0x4d), + Buffer.of(0x61, 0x00), + "\uD83Da", + ); + testEnd( + "utf16le", + Buffer.of(0x3d, 0xd8, 0x4d), + Buffer.of(0xdc), + "\uD83D", + ); + testEnd( + "utf16le", + Buffer.of(0x3d, 0xd8, 0x4d, 0xdc), + Buffer.of(0x61, 0x00), + "👍a", + ); + + testEnd("base64", Buffer.of(0x61), Buffer.of(), "YQ=="); + testEnd("base64", Buffer.of(0x61), Buffer.of(0x61), "YQ==YQ=="); + testEnd("base64", Buffer.of(0x61, 0x61), Buffer.of(), "YWE="); + testEnd("base64", Buffer.of(0x61, 0x61), Buffer.of(0x61), "YWE=YQ=="); + testEnd("base64", Buffer.of(0x61, 0x61, 0x61), Buffer.of(), "YWFh"); + testEnd( + "base64", + Buffer.of(0x61, 0x61, 0x61), + Buffer.of(0x61), + "YWFhYQ==", + ); + + testEnd("base64url", Buffer.of(0x61), Buffer.of(), "YQ"); + testEnd("base64url", Buffer.of(0x61), Buffer.of(0x61), "YQYQ"); + testEnd("base64url", Buffer.of(0x61, 0x61), Buffer.of(), "YWE"); + testEnd("base64url", Buffer.of(0x61, 0x61), Buffer.of(0x61), "YWEYQ"); + testEnd("base64url", Buffer.of(0x61, 0x61, 0x61), Buffer.of(), "YWFh"); + testEnd( + "base64url", + Buffer.of(0x61, 0x61, 0x61), + Buffer.of(0x61), + "YWFhYQ", + ); + + function testEncoding(encoding) { + it(encoding + " testbuf", () => { + bufs.forEach((buf) => { + testBuf(encoding, buf); + }); + }); + } + + function testBuf(encoding, buf) { + // Write one byte at a time. + let s = new StringDecoder(encoding); + let res1 = ""; + for (let i = 0; i < buf.length; i++) { + res1 += s.write(buf.slice(i, i + 1)); + } + res1 += s.end(); + + // Write the whole buffer at once. + let res2 = ""; + s = new StringDecoder(encoding); + res2 += s.write(buf); + res2 += s.end(); + + // .toString() on the buffer + const res3 = buf.toString(encoding); + + // One byte at a time should match toString + expect(res1).toEqual(res3); + // All bytes at once should match toString + expect(res2).toEqual(res3); + } + + function testEnd(encoding, incomplete, next, expected) { + it(`${encoding} partial ${JSON.stringify(expected)}`, () => { + let res = ""; + const s = new StringDecoder(encoding); + res += s.write(incomplete); + res += s.end(); + res += s.write(next); + res += s.end(); + + expect(res).toEqual(expected); + }); + } }); }); } - -// writeSequences returns an array of arrays that describes all possible ways a -// buffer of the given length could be split up and passed to sequential write -// calls. -// -// e.G. writeSequences(3) will return: [ -// [ [ 0, 3 ] ], -// [ [ 0, 2 ], [ 2, 3 ] ], -// [ [ 0, 1 ], [ 1, 3 ] ], -// [ [ 0, 1 ], [ 1, 2 ], [ 2, 3 ] ] -// ] -function writeSequences(length, start, sequence) { - if (start === undefined) { - start = 0; - sequence = []; - } else if (start === length) { - return [sequence]; - } - let sequences = []; - for (let end = length; end > start; end--) { - const subSequence = sequence.concat([[start, end]]); - const subSequences = writeSequences(length, end, subSequence, sequences); - sequences = sequences.concat(subSequences); - } - return sequences; -} - -describe("StringDecoder.end", () => { - const encodings = ["base64", "base64url", "hex", "utf8", "utf16le", "ucs2"]; - - const bufs = ["☃💩", "asdf"].map((b) => Buffer.from(b)); - - // Also test just arbitrary bytes from 0-15. - for (let i = 1; i <= 16; i++) { - const bytes = "." - .repeat(i - 1) - .split(".") - .map((_, j) => j + 0x78); - bufs.push(Buffer.from(bytes)); - } - - encodings.forEach(testEncoding); - - testEnd("utf8", Buffer.of(0xe2), Buffer.of(0x61), "\uFFFDa"); - testEnd("utf8", Buffer.of(0xe2), Buffer.of(0x82), "\uFFFD\uFFFD"); - testEnd("utf8", Buffer.of(0xe2), Buffer.of(0xe2), "\uFFFD\uFFFD"); - testEnd("utf8", Buffer.of(0xe2, 0x82), Buffer.of(0x61), "\uFFFDa"); - testEnd("utf8", Buffer.of(0xe2, 0x82), Buffer.of(0xac), "\uFFFD\uFFFD"); - testEnd("utf8", Buffer.of(0xe2, 0x82), Buffer.of(0xe2), "\uFFFD\uFFFD"); - testEnd("utf8", Buffer.of(0xe2, 0x82, 0xac), Buffer.of(0x61), "€a"); - - testEnd("utf16le", Buffer.of(0x3d), Buffer.of(0x61, 0x00), "a"); - testEnd("utf16le", Buffer.of(0x3d), Buffer.of(0xd8, 0x4d, 0xdc), "\u4DD8"); - testEnd("utf16le", Buffer.of(0x3d, 0xd8), Buffer.of(), "\uD83D"); - testEnd("utf16le", Buffer.of(0x3d, 0xd8), Buffer.of(0x61, 0x00), "\uD83Da"); - testEnd( - "utf16le", - Buffer.of(0x3d, 0xd8), - Buffer.of(0x4d, 0xdc), - "\uD83D\uDC4D", - ); - testEnd("utf16le", Buffer.of(0x3d, 0xd8, 0x4d), Buffer.of(), "\uD83D"); - testEnd( - "utf16le", - Buffer.of(0x3d, 0xd8, 0x4d), - Buffer.of(0x61, 0x00), - "\uD83Da", - ); - testEnd("utf16le", Buffer.of(0x3d, 0xd8, 0x4d), Buffer.of(0xdc), "\uD83D"); - testEnd( - "utf16le", - Buffer.of(0x3d, 0xd8, 0x4d, 0xdc), - Buffer.of(0x61, 0x00), - "👍a", - ); - - testEnd("base64", Buffer.of(0x61), Buffer.of(), "YQ=="); - testEnd("base64", Buffer.of(0x61), Buffer.of(0x61), "YQ==YQ=="); - testEnd("base64", Buffer.of(0x61, 0x61), Buffer.of(), "YWE="); - testEnd("base64", Buffer.of(0x61, 0x61), Buffer.of(0x61), "YWE=YQ=="); - testEnd("base64", Buffer.of(0x61, 0x61, 0x61), Buffer.of(), "YWFh"); - testEnd("base64", Buffer.of(0x61, 0x61, 0x61), Buffer.of(0x61), "YWFhYQ=="); - - testEnd("base64url", Buffer.of(0x61), Buffer.of(), "YQ"); - testEnd("base64url", Buffer.of(0x61), Buffer.of(0x61), "YQYQ"); - testEnd("base64url", Buffer.of(0x61, 0x61), Buffer.of(), "YWE"); - testEnd("base64url", Buffer.of(0x61, 0x61), Buffer.of(0x61), "YWEYQ"); - testEnd("base64url", Buffer.of(0x61, 0x61, 0x61), Buffer.of(), "YWFh"); - testEnd("base64url", Buffer.of(0x61, 0x61, 0x61), Buffer.of(0x61), "YWFhYQ"); - - function testEncoding(encoding) { - it(encoding + " testbuf", () => { - bufs.forEach((buf) => { - testBuf(encoding, buf); - }); - }); - } - - function testBuf(encoding, buf) { - // Write one byte at a time. - let s = new StringDecoder(encoding); - let res1 = ""; - for (let i = 0; i < buf.length; i++) { - res1 += s.write(buf.slice(i, i + 1)); - } - res1 += s.end(); - - // Write the whole buffer at once. - let res2 = ""; - s = new StringDecoder(encoding); - res2 += s.write(buf); - res2 += s.end(); - - // .toString() on the buffer - const res3 = buf.toString(encoding); - - // One byte at a time should match toString - expect(res1).toEqual(res3); - // All bytes at once should match toString - expect(res2).toEqual(res3); - } - - function testEnd(encoding, incomplete, next, expected) { - it(`${encoding} partial ${JSON.stringify(expected)}`, () => { - let res = ""; - const s = new StringDecoder(encoding); - res += s.write(incomplete); - res += s.end(); - res += s.write(next); - res += s.end(); - - expect(res).toEqual(expected); - }); - } -}); diff --git a/test/bun.js/third-party/body-parser-test/bun.lockb b/test/bun.js/third-party/body-parser-test/bun.lockb new file mode 100755 index 0000000000000000000000000000000000000000..6fdd6d093d359dbee3946fa2b70b11397c488b5f GIT binary patch literal 20925 zcmeHvc|4Wd_y56BD3KJ+N=oIJ=S(Thkw~*TI5->!=NuIcl$6SCo+J%aq@saqXpSq2 zlyp^SP@$sIaGUG5*5hoR?)`Ao=ljR+`+eQr>#?7`pZ#8Y?X}k4d+q169?b!IAwq$k z2bZtM;V;ne2;nQh#o`5e`g?PE9F}*WAb>4mEznnxW-u6;)z0p{O%fk@D_bQvaTC@~ zihHj%=;*vX3ZmX!$}(zpvz(z3q!CRSLr$D`6z37*e5IcFhRT!~jELTFrvP~{F6E3K@%m9c&-K4dAf6JQzVx#^*v2TNEPX2v0)$ zE>J!MInony_+Ch7gSgBS)8oJpxdNdmK!{Hn45Wwo6!G&A*(N-f@5>QzMM5u55Jkru zxbuZ}91+_G+gpg+>q9ON{dsYOB0*p%!;9k)f(;1$Ahtlr2@&w(#O;H{{aS*kkk8$a zBi|b!SA^UKh~c<=*-Z@%pp5ma;5qga1$0mj3*Zly1H}14s7E>h9FcFJmyj_V%3Yy6 z8glF>gdfBWf{|)K8RhR880gQ1va0yG6y)8Y9-dMC3)l-;9)Vt=L{A=}A`Wu&7B1w6 z?di!05(yc5p;P4N-Rd{i8m(cNy}E7pX3hhW{ne##Fjyvhn= zqTL?8Fg`X$cbjnDm%bZ6M32;y+Oos_D*J8j9|s!0<{#vH%uHH-yk>*>!`_$PuJo5` zTxj!>bAh)}H*SfJ?33L04^4~fER#~}zILb^P&8Sg^9geYPJ)Mw+HBObZO#-H}->3LNudsw0E-J)~Rap~#F)nQ6zsc+ZwH(kpMePpsel41O?*Oh!iU!Z(G z^YzC$8wXVAxesw4P^-2m(mNpjO|fE+53^pyJXq=wYQO9BMF++DH0%AU8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A+YpfW>!|)FsL8 zAI>6$|C<9zX+h%XY2GLbfK~*&WEnJ_cL3RRL-i?XP5Qf+4CudCQFKftXs!v=g2uf> za^Fo3Jo=OrvZYA%DQUO+4+j7m8QQUc+Sw#gqn)3C3ax~)m%L?@gD_ypEleLc3QG`N z>&GxWlp~ma4r{}Dsk1Y3j^Br4QSTchbp69=3XngPVbqdW9Pzj35bH^Og$_{glLq$7 i=lBGQxNJB*wZU^sBOuXI0DZy98mTlLga7~g`~Lu!?PP-h literal 0 HcmV?d00001 diff --git a/test/bun.js/third-party/body-parser-test/express-body-parser-test.test.ts b/test/bun.js/third-party/body-parser-test/express-body-parser-test.test.ts new file mode 100644 index 0000000000..905cc4ce2e --- /dev/null +++ b/test/bun.js/third-party/body-parser-test/express-body-parser-test.test.ts @@ -0,0 +1,61 @@ +import { test, expect } from "bun:test"; +import express, { Application, Request, Response } from "express"; +import { json } from "body-parser"; + +// Express uses iconv-lite +test("iconv works", () => { + var iconv = require("iconv-lite"); + + // Convert from an encoded buffer to a js string. + var str = iconv.decode( + Buffer.from([0x68, 0x65, 0x6c, 0x6c, 0x6f]), + "win1251", + ); + + // Convert from a js string to an encoded buffer. + var buf = iconv.encode("Sample input string", "win1251"); + expect(str).toBe("hello"); + expect(iconv.decode(buf, "win1251")).toBe("Sample input string"); + + // Check if encoding is supported + expect(iconv.encodingExists("us-ascii")).toBe(true); +}); + +// https://github.com/oven-sh/bun/issues/1913 +test("httpServer", async (done) => { + // Constants + const PORT = 8412; + + // App handlers + const app: Application = express(); + const httpServer = require("http").createServer(app); + + app.on("error", (err) => { + done(err); + }); + app.use(json()); + + var reached = false; + // This throws a TypeError since it uses body-parser.json + app.post("/ping", (request: Request, response: Response) => { + expect(request.body).toEqual({ hello: "world" }); + reached = true; + response.status(200).send("POST - pong"); + httpServer.close(); + done(); + }); + + httpServer.listen(PORT); + const resp = await fetch(`http://localhost:${PORT}/ping`, { + method: "POST", + body: JSON.stringify({ hello: "world" }), + headers: { + "Content-Type": "application/json", + }, + }); + + expect(resp.status).toBe(200); + expect(await resp.text()).toBe("POST - pong"); + expect(reached).toBe(true); + done(); +}); diff --git a/test/bun.js/third-party/body-parser-test/package.json b/test/bun.js/third-party/body-parser-test/package.json new file mode 100644 index 0000000000..0dfa98c593 --- /dev/null +++ b/test/bun.js/third-party/body-parser-test/package.json @@ -0,0 +1,9 @@ +{ + "name": "body-parser-test", + "version": "1.0.0", + "dependencies": { + "express": "4.18.2", + "body-parser": "1.20.1", + "iconv-lite": "0.6.3" + } +}