mirror of
https://github.com/oven-sh/bun
synced 2026-02-11 03:18:53 +00:00
feat(runtime): implement server.requestIp + node:http socket.address() (#6165)
* [server] requestIp and AnyRequestContext Changed Request.uws_request to the new AnyRequestContext. This allows grabbing the IP from a Request. Unfinished. * [server] basic `requestIp` implementation Currently using uws's requestIpAsText, which always returns a ipv6 string. We should return a `SocketAddress` object to the user instead, which will contain the formatted address string and what type it is. We'll have to use requestIpAsBinary and parse that ourselves. * TypeScript docs, use `bun.String`, return `undefined` instead of `null` if we can't get the ip. * binary address formatting * uws getRemoteAddress binding * remove dead code * working * final touches✨ * I will abide by the results of this poll. --------- Co-authored-by: Parzival-3141 <29632054+Parzival-3141@users.noreply.github.com>
This commit is contained in:
61
src/bun.js/bindings/JSSocketAddress.cpp
Normal file
61
src/bun.js/bindings/JSSocketAddress.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include "JSSocketAddress.h"
|
||||
#include "ZigGlobalObject.h"
|
||||
#include "JavaScriptCore/JSObjectInlines.h"
|
||||
#include "JavaScriptCore/ObjectConstructor.h"
|
||||
#include "JavaScriptCore/JSCast.h"
|
||||
|
||||
using namespace JSC;
|
||||
|
||||
namespace Bun {
|
||||
namespace JSSocketAddress {
|
||||
|
||||
// Using a structure with inlined offsets will be more lightweight than a class.
|
||||
|
||||
Structure* createStructure(VM& vm, JSGlobalObject* globalObject)
|
||||
{
|
||||
JSC::Structure* structure = globalObject->structureCache().emptyObjectStructureForPrototype(
|
||||
globalObject,
|
||||
globalObject->objectPrototype(),
|
||||
3);
|
||||
|
||||
JSC::PropertyOffset offset;
|
||||
structure = structure->addPropertyTransition(
|
||||
vm,
|
||||
structure,
|
||||
JSC::Identifier::fromString(vm, "address"_s),
|
||||
0,
|
||||
offset);
|
||||
|
||||
structure = structure->addPropertyTransition(
|
||||
vm,
|
||||
structure,
|
||||
JSC::Identifier::fromString(vm, "family"_s),
|
||||
0,
|
||||
offset);
|
||||
|
||||
structure = structure->addPropertyTransition(
|
||||
vm,
|
||||
structure,
|
||||
JSC::Identifier::fromString(vm, "port"_s),
|
||||
0,
|
||||
offset);
|
||||
|
||||
return structure;
|
||||
}
|
||||
|
||||
} // namespace JSSocketAddress
|
||||
} // namespace Bun
|
||||
|
||||
extern "C" JSObject* JSSocketAddress__create(JSGlobalObject* globalObject, JSString* value, int32_t port, bool isIPv6)
|
||||
{
|
||||
VM& vm = globalObject->vm();
|
||||
|
||||
auto* global = jsCast<Zig::GlobalObject*>(globalObject);
|
||||
|
||||
JSObject* thisObject = constructEmptyObject(vm, global->JSSocketAddressStructure());
|
||||
thisObject->putDirectOffset(vm, 0, value);
|
||||
thisObject->putDirectOffset(vm, 1, isIPv6 ? jsString(vm, Bun::JSSocketAddress::IPv6) : jsString(vm, Bun::JSSocketAddress::IPv4));
|
||||
thisObject->putDirectOffset(vm, 2, jsNumber(port));
|
||||
|
||||
return thisObject;
|
||||
}
|
||||
Reference in New Issue
Block a user