diff options
author | 2023-01-28 01:00:26 -0800 | |
---|---|---|
committer | 2023-01-28 01:00:26 -0800 | |
commit | 6557df29122c328745b169ed7fe57f4333b40bc8 (patch) | |
tree | 5ed54d18274b3c0d98c9e3df8eed787b477f5f0d | |
parent | aff91436c0998b0368f0970fa0da42afa09640fc (diff) | |
download | bun-6557df29122c328745b169ed7fe57f4333b40bc8.tar.gz bun-6557df29122c328745b169ed7fe57f4333b40bc8.tar.zst bun-6557df29122c328745b169ed7fe57f4333b40bc8.zip |
Fixes #1913
-rw-r--r-- | src/bun.js/bindings/JSStringDecoder.cpp | 116 | ||||
-rw-r--r-- | test/bun.js/string-decoder.test.js | 498 | ||||
-rwxr-xr-x | test/bun.js/third-party/body-parser-test/bun.lockb | bin | 0 -> 20925 bytes | |||
-rw-r--r-- | test/bun.js/third-party/body-parser-test/express-body-parser-test.test.ts | 61 | ||||
-rw-r--r-- | test/bun.js/third-party/body-parser-test/package.json | 9 |
5 files changed, 451 insertions, 233 deletions
diff --git a/src/bun.js/bindings/JSStringDecoder.cpp b/src/bun.js/bindings/JSStringDecoder.cpp index c73378fdf..96fbc3a57 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<JSStringDecoder*>(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<JSStringDecoder*>(existingDecoderValue); + } + + if (!castedThis) { + BufferEncodingType encoding = BufferEncodingType::utf8; + if (JSValue encodingValue = thisObject->getIfPropertyExists(globalObject, clientData->builtinNames().encodingPrivateName())) { + if (encodingValue.isString()) { + std::optional<BufferEncodingType> opt = parseEnumeration<BufferEncodingType>(*globalObject, encodingValue); + if (opt.has_value()) { + encoding = opt.value(); + } + } + } + castedThis = JSStringDecoder::create(globalObject->vm(), globalObject, reinterpret_cast<Zig::GlobalObject*>(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<JSStringDecoder>::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<uint8_t*>(view->vector()), view->byteLength()))); } -static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_endBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSStringDecoder>::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<uint8_t*>(view->vector()), view->byteLength()))); } -static inline JSC::EncodedJSValue jsStringDecoderPrototypeFunction_textBody(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callFrame, typename IDLOperation<JSStringDecoder>::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<JSStringDecoder>::call<jsStringDecoderPrototypeFunction_writeBody>(*globalObject, *callFrame, "write"); + JSValue stringDecoderValue = JSValue::decode(jsStringDecoderCast(globalObject, callFrame->thisValue())); + if (stringDecoderValue.isEmpty() || !stringDecoderValue.isCell()) { + return JSValue::encode(stringDecoderValue); + } + JSStringDecoder* castedThis = jsCast<JSStringDecoder*>(stringDecoderValue); + return jsStringDecoderPrototypeFunction_writeBody(globalObject, callFrame, castedThis); } static JSC_DEFINE_HOST_FUNCTION(jsStringDecoderPrototypeFunction_end, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) { - return IDLOperation<JSStringDecoder>::call<jsStringDecoderPrototypeFunction_endBody>(*globalObject, *callFrame, "end"); + JSValue stringDecoderValue = JSValue::decode(jsStringDecoderCast(globalObject, callFrame->thisValue())); + if (stringDecoderValue.isEmpty() || !stringDecoderValue.isCell()) { + return JSValue::encode(stringDecoderValue); + } + JSStringDecoder* castedThis = jsCast<JSStringDecoder*>(stringDecoderValue); + return jsStringDecoderPrototypeFunction_endBody(globalObject, callFrame, castedThis); } static JSC_DEFINE_HOST_FUNCTION(jsStringDecoderPrototypeFunction_text, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame)) { - return IDLOperation<JSStringDecoder>::call<jsStringDecoderPrototypeFunction_textBody>(*globalObject, *callFrame, "text"); + JSValue stringDecoderValue = JSValue::decode(jsStringDecoderCast(globalObject, callFrame->thisValue())); + if (stringDecoderValue.isEmpty() || !stringDecoderValue.isCell()) { + return JSValue::encode(stringDecoderValue); + } + JSStringDecoder* castedThis = jsCast<JSStringDecoder*>(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<JSStringDecoder*>(stringDecoderValue); auto throwScope = DECLARE_THROW_SCOPE(vm); - JSStringDecoder* thisObject = jsCast<JSStringDecoder*>(JSValue::decode(thisValue)); auto buffer = ArrayBuffer::createFromBytes(thisObject->m_lastChar, 4, nullptr); auto* globalObject = reinterpret_cast<Zig::GlobalObject*>(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<JSStringDecoder*>(stringDecoderValue); auto throwScope = DECLARE_THROW_SCOPE(vm); - JSStringDecoder* thisObject = jsCast<JSStringDecoder*>(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<JSStringDecoder*>(stringDecoderValue); auto throwScope = DECLARE_THROW_SCOPE(vm); - JSStringDecoder* thisObject = jsCast<JSStringDecoder*>(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<Zig::GlobalObject*>(lexicalGlobalObject)->JSStringDecoderStructure(), encoding); - return JSC::JSValue::encode(stringDecoder); + JSValue thisValue = callFrame->newTarget(); + auto* globalObject = JSC::jsCast<Zig::GlobalObject*>(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 3d330ffcd..449f35620 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"); -it("require('string_decoder')", async () => { - expect((await import("string_decoder")).StringDecoder).toBe(StringDecoder); -}); +const RealStringDecoder = require("string_decoder").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", +it("require('string_decoder')", async () => { + 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("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(""); -}); + decoder = new StringDecoder("utf8"); + expect(decoder.write(Buffer.from("EFBFBDE2", "hex"))).toBe("\ufffd"); + expect(decoder.end()).toBe("\ufffd"); -// 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); + 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"); + + // Additional utf8Text test + decoder = new StringDecoder("utf8"); + expect(decoder.text(Buffer.from([0x41]), 2)).toBe(""); }); - }); -} -// 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; -} + 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(""); -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", - ); + decoder = new StringDecoder("utf16le"); + expect(decoder.write(Buffer.from("3DD8", "hex"))).toBe(""); + expect(decoder.end()).toBe("\ud83d"); - 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); - }); + 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(""); }); - } - - 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)); + + // 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); + }); + }); + } + + // 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; } - 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); + + 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 Binary files differnew file mode 100755 index 000000000..6fdd6d093 --- /dev/null +++ b/test/bun.js/third-party/body-parser-test/bun.lockb 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 000000000..905cc4ce2 --- /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 000000000..0dfa98c59 --- /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" + } +} |