node:net: implement BlockList (#19277)

This commit is contained in:
Meghan Denny
2025-05-01 15:09:44 -08:00
committed by GitHub
parent 5874cc44d3
commit 9e201eff9e
24 changed files with 840 additions and 116 deletions

View File

@@ -202,7 +202,8 @@ export class ClassDefinition {
configurable?: boolean;
enumerable?: boolean;
structuredClone?: boolean | { transferable: boolean; tag: number };
structuredClone?: { transferable: boolean; tag: number };
customInspect?: boolean;
callbacks?: Record<string, string>;
@@ -245,7 +246,7 @@ export function define(
estimatedSize = false,
call = false,
construct = false,
structuredClone = false,
structuredClone,
...rest
} = {} as Partial<ClassDefinition>,
): ClassDefinition {

View File

@@ -426,6 +426,15 @@ JSC_DECLARE_CUSTOM_GETTER(js${typeName}Constructor);
"onStructuredCloneDeserialize",
)}(JSC::JSGlobalObject*, const uint8_t*, const uint8_t*);` + "\n";
}
if (obj.customInspect) {
externs += `extern JSC_CALLCONV JSC::EncodedJSValue JSC_HOST_CALL_ATTRIBUTES ${protoSymbolName(typeName, "customInspect")}(JSC::JSGlobalObject*, JSC::CallFrame*);\n`;
specialSymbols += `
this->putDirect(vm, builtinNames(vm).inspectCustomPublicName(), JSFunction::create(vm, globalObject, 2, String("[nodejs.util.inspect.custom]"_s), ${protoSymbolName(
typeName,
"customInspect",
)}, ImplementationVisibility::Public), PropertyAttribute::Function | 0);`;
}
if (obj.finalize) {
externs +=
`extern JSC_CALLCONV void JSC_HOST_CALL_ATTRIBUTES ${classSymbolName(typeName, "finalize")}(void*);` + "\n";
@@ -1778,6 +1787,7 @@ function generateZig(
values = [],
hasPendingActivity = false,
structuredClone = false,
customInspect = false,
getInternalProperties = false,
callbacks = {},
} = {} as ClassDefinition,
@@ -1802,6 +1812,10 @@ function generateZig(
exports.set("onStructuredCloneDeserialize", symbolName(typeName, "onStructuredCloneDeserialize"));
}
if (customInspect) {
exports.set("customInspect", symbolName(typeName, "customInspect"));
}
proto = {
...Object.fromEntries(Object.entries(own || {}).map(([name, getterName]) => [name, { getter: getterName }])),
...proto,
@@ -2059,6 +2073,19 @@ const JavaScriptCoreBindings = struct {
`;
}
if (customInspect) {
// TODO: perhaps exposing this on classes directly isn't the best API choice long term
// it would be better to make a different signature that accepts a writer, then a generated-only function that returns a js string
// the writer function can integrate with our native console.log implementation, the generated function can call the writer version and collect the result
exports.set("customInspect", protoSymbolName(typeName, "customInspect"));
output += `
pub fn ${protoSymbolName(typeName, "customInspect")}(thisValue: *${typeName}, globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(JSC.conv) JSC.JSValue {
if (comptime Environment.enable_logs) JSC.markBinding(@src());
return @call(.always_inline, JSC.toJSHostValue, .{globalObject, @call(.always_inline, ${typeName}.customInspect, .{thisValue, globalObject, callFrame})});
}
`;
}
return (
output.trim() +
`

View File

@@ -12,8 +12,9 @@ const extra_count = NodeErrors.map(x => x.slice(3))
.reduce((ac, cv) => ac + cv.length, 0);
const count = NodeErrors.length + extra_count;
if (count > 65536) {
throw new Error("NodeError count exceeds u16");
if (count > 1 << 16) {
// increase size of the enums below to have more tags
throw new Error(`NodeError can't fit ${count} codes in a u16`);
}
let enumHeader = ``;
@@ -166,9 +167,9 @@ for (const [code, constructor, name, ...other_constructors] of NodeErrors) {
? `${constructor.name} & { name: "${name}", code: "${code}" }`
: `${constructor.name} & { code: "${code}" }`;
dts += `
/**
/**
* Construct an {@link ${constructor.name} ${constructor.name}} with the \`"${code}"\` error code.
*
*
* To override this, update ErrorCode.cpp. To remove this generated type, mention \`"${code}"\` in builtins.d.ts.
*/
declare function $${code}(message: string): ${namedError};\n`;