Skip to content

Commit

Permalink
some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Dec 13, 2024
1 parent 83e88df commit 3040bcb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5625,6 +5625,7 @@ pub const JSValue = enum(i64) {
}
return JSC.Node.validators.throwErrInvalidArgType(global, property_name, .{}, "string", prop);
},
i32 => return prop.coerce(i32, global),
else => @compileError("TODO:" ++ @typeName(T)),
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/bun.js/webcore/blob.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3574,18 +3574,17 @@ pub const Blob = struct {
const args = callframe.arguments_old(1);
var method: bun.http.Method = .GET;
var expires: usize = 86400; // 1 day
if (args.len == 1) {
if (args.len > 0) {
const options = args.ptr[0];
if (options.isObject()) {
if (try options.getTruthyComptime(globalThis, "method")) |method_| {
method = Method.fromJS(globalThis, method_) orelse {
return globalThis.throwInvalidArguments("method must be GET, PUT, DELETE or HEAD when using s3 protocol", .{});
};
}
if (try options.getTruthyComptime(globalThis, "expiresIn")) |expires_| {
const coerced = expires_.coerce(i32, globalThis);
if (coerced <= 0) return globalThis.throwInvalidArguments("expiresIn must be greather than 0", .{});
expires = @intCast(coerced);
if (try options.getOptional(globalThis, "expiresIn", i32)) |expires_| {
if (expires_ <= 0) return globalThis.throwInvalidArguments("expiresIn must be greather than 0", .{});
expires = @intCast(expires_);
}
}
}
Expand Down
33 changes: 19 additions & 14 deletions src/s3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub const AWSCredentials = struct {
}

const DIGESTED_HMAC_256_LEN = 32;
const ENABLE_SIGNATURE_CACHE = false;
threadlocal var SIGNATURE_CACHE: bun.StringArrayHashMap([DIGESTED_HMAC_256_LEN]u8) = undefined;
threadlocal var SIGNATURE_CACHE_DATE: u64 = 0;

Expand Down Expand Up @@ -165,30 +166,34 @@ pub const AWSCredentials = struct {
const sigDateRegionServiceReq = brk_sign: {
const key = try std.fmt.bufPrint(&tmp_buffer, "{s}{s}{s}", .{ region, service_name, this.secretAccessKey });

if (SIGNATURE_CACHE_DATE == date_result.numeric_day) {
if (SIGNATURE_CACHE.getKey(key)) |cached| {
break :brk_sign cached;
}
} else {
if (SIGNATURE_CACHE_DATE == 0) {
// first request we need a new map instance
SIGNATURE_CACHE = bun.StringArrayHashMap([DIGESTED_HMAC_256_LEN]u8).init(bun.default_allocator);
if (comptime ENABLE_SIGNATURE_CACHE) {
if (SIGNATURE_CACHE_DATE == date_result.numeric_day) {
if (SIGNATURE_CACHE.getKey(key)) |cached| {
break :brk_sign cached;
}
} else {
// day changed so we clean the old cache
for (SIGNATURE_CACHE.keys()) |cached_key| {
bun.default_allocator.free(cached_key);
if (SIGNATURE_CACHE_DATE == 0) {
// first request we need a new map instance
SIGNATURE_CACHE = bun.StringArrayHashMap([DIGESTED_HMAC_256_LEN]u8).init(bun.default_allocator);
} else {
// day changed so we clean the old cache
for (SIGNATURE_CACHE.keys()) |cached_key| {
bun.default_allocator.free(cached_key);
}
SIGNATURE_CACHE.clearRetainingCapacity();
}
SIGNATURE_CACHE.clearRetainingCapacity();
SIGNATURE_CACHE_DATE = date_result.numeric_day;
}
SIGNATURE_CACHE_DATE = date_result.numeric_day;
}
// not cached yet lets generate a new one
const sigDate = bun.hmac.generate(try std.fmt.bufPrint(&tmp_buffer, "AWS4{s}", .{this.secretAccessKey}), amz_day, .sha256, &hmac_sig_service) orelse return error.FailedToGenerateSignature;
const sigDateRegion = bun.hmac.generate(sigDate, region, .sha256, &hmac_sig_service2) orelse return error.FailedToGenerateSignature;
const sigDateRegionService = bun.hmac.generate(sigDateRegion, service_name, .sha256, &hmac_sig_service) orelse return error.FailedToGenerateSignature;
const result = bun.hmac.generate(sigDateRegionService, "aws4_request", .sha256, &hmac_sig_service2) orelse return error.FailedToGenerateSignature;

try SIGNATURE_CACHE.put(try bun.default_allocator.dupe(u8, key), hmac_sig_service2[0..DIGESTED_HMAC_256_LEN].*);
if (comptime ENABLE_SIGNATURE_CACHE) {
try SIGNATURE_CACHE.put(try bun.default_allocator.dupe(u8, key), hmac_sig_service2[0..DIGESTED_HMAC_256_LEN].*);
}
break :brk_sign result;
};
if (signQuery) {
Expand Down
2 changes: 1 addition & 1 deletion src/url.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub const URL = struct {

pub fn s3Path(this: *const URL) string {
// we need to remove protocol if exists and ignore searchParams, should be host + pathname
const href = if (this.protocol.len > 0) this.href[this.protocol.len + 2 ..] else this.href;
const href = if (this.protocol.len > 0 and this.href.len > this.protocol.len + 2) this.href[this.protocol.len + 2 ..] else this.href;
return href[0 .. href.len - (this.search.len + this.hash.len)];
}

Expand Down

0 comments on commit 3040bcb

Please sign in to comment.