Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Dec 30, 2024
1 parent ee35109 commit 0769ca9
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/bun.js/api/BunObject.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const conv = std.builtin.CallingConvention.Unspecified;

const S3File = @import("../webcore/S3File.zig");
const S3Bucket = @import("../webcore/S3Bucket.zig");
/// How to add a new function or property to the Bun global
///
/// - Add a callback or property to the below struct
Expand Down Expand Up @@ -31,7 +32,8 @@ pub const BunObject = struct {
pub const registerMacro = toJSCallback(Bun.registerMacro);
pub const resolve = toJSCallback(Bun.resolve);
pub const resolveSync = toJSCallback(Bun.resolveSync);
pub const s3 = toJSCallback(WebCore.Blob.constructS3File);
pub const s3 = toJSCallback(S3File.constructS3File);
pub const S3 = S3Bucket.createJSS3Bucket;
pub const serve = toJSCallback(Bun.serve);
pub const sha = toJSCallback(JSC.wrapStaticMethod(Crypto.SHA512_256, "hash_", true));
pub const shellEscape = toJSCallback(Bun.shellEscape);
Expand All @@ -57,7 +59,6 @@ pub const BunObject = struct {
pub const SHA384 = toJSGetter(Crypto.SHA384.getter);
pub const SHA512 = toJSGetter(Crypto.SHA512.getter);
pub const SHA512_256 = toJSGetter(Crypto.SHA512_256.getter);
pub const S3 = toJSGetter(JSC.WebCore.Blob.getJSS3FileConstructor);
pub const TOML = toJSGetter(Bun.getTOMLObject);
pub const Transpiler = toJSGetter(Bun.getTranspilerConstructor);
pub const argv = toJSGetter(Bun.getArgv);
Expand Down Expand Up @@ -110,7 +111,6 @@ pub const BunObject = struct {
@export(BunObject.FileSystemRouter, .{ .name = getterName("FileSystemRouter") });
@export(BunObject.MD4, .{ .name = getterName("MD4") });
@export(BunObject.MD5, .{ .name = getterName("MD5") });
@export(BunObject.S3, .{ .name = getterName("S3") });
@export(BunObject.SHA1, .{ .name = getterName("SHA1") });
@export(BunObject.SHA224, .{ .name = getterName("SHA224") });
@export(BunObject.SHA256, .{ .name = getterName("SHA256") });
Expand Down Expand Up @@ -160,6 +160,7 @@ pub const BunObject = struct {
@export(BunObject.resolveSync, .{ .name = callbackName("resolveSync") });
@export(BunObject.serve, .{ .name = callbackName("serve") });
@export(BunObject.s3, .{ .name = callbackName("s3") });
@export(BunObject.S3, .{ .name = callbackName("S3") });
@export(BunObject.sha, .{ .name = callbackName("sha") });
@export(BunObject.shellEscape, .{ .name = callbackName("shellEscape") });
@export(BunObject.shrink, .{ .name = callbackName("shrink") });
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/bindings/BunObject+exports.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
macro(SHA512_256) \
macro(TOML) \
macro(Transpiler) \
macro(S3) \
macro(argv) \
macro(assetPrefix) \
macro(cwd) \
Expand Down Expand Up @@ -59,6 +58,7 @@
macro(resolve) \
macro(resolveSync) \
macro(s3) \
macro(S3) \
macro(serve) \
macro(sha) \
macro(shrink) \
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/bindings/BunObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,6 @@ JSC_DEFINE_HOST_FUNCTION(functionFileURLToPath, (JSC::JSGlobalObject * globalObj
Glob BunObject_getter_wrap_Glob DontDelete|PropertyCallback
MD4 BunObject_getter_wrap_MD4 DontDelete|PropertyCallback
MD5 BunObject_getter_wrap_MD5 DontDelete|PropertyCallback
S3 BunObject_getter_wrap_S3 DontDelete|PropertyCallback
SHA1 BunObject_getter_wrap_SHA1 DontDelete|PropertyCallback
SHA224 BunObject_getter_wrap_SHA224 DontDelete|PropertyCallback
SHA256 BunObject_getter_wrap_SHA256 DontDelete|PropertyCallback
Expand Down Expand Up @@ -639,6 +638,7 @@ JSC_DEFINE_HOST_FUNCTION(functionFileURLToPath, (JSC::JSGlobalObject * globalObj
revision constructBunRevision ReadOnly|DontDelete|PropertyCallback
semver BunObject_getter_wrap_semver ReadOnly|DontDelete|PropertyCallback
s3 BunObject_callback_s3 DontDelete|Function 1
S3 BunObject_callback_S3 DontDelete|Function 1
sql constructBunSQLObject DontDelete|PropertyCallback
serve BunObject_callback_serve DontDelete|Function 1
sha BunObject_callback_sha DontDelete|Function 1
Expand Down
40 changes: 38 additions & 2 deletions src/bun.js/bindings/JSS3Bucket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ SYSV_ABI EncodedJSValue JSS3Bucket__call(void* ptr, JSC::JSGlobalObject*, JSC::C
SYSV_ABI EncodedJSValue JSS3Bucket__unlink(void* ptr, JSC::JSGlobalObject*, JSC::CallFrame* callframe);
SYSV_ABI EncodedJSValue JSS3Bucket__write(void* ptr, JSC::JSGlobalObject*, JSC::CallFrame* callframe);
SYSV_ABI EncodedJSValue JSS3Bucket__presign(void* ptr, JSC::JSGlobalObject*, JSC::CallFrame* callframe);
SYSV_ABI EncodedJSValue JSS3Bucket__exists(void* ptr, JSC::JSGlobalObject*, JSC::CallFrame* callframe);
SYSV_ABI EncodedJSValue JSS3Bucket__size(void* ptr, JSC::JSGlobalObject*, JSC::CallFrame* callframe);
SYSV_ABI void* JSS3Bucket__deinit(void* ptr);
}

// Forward declarations
JSC_DECLARE_HOST_FUNCTION(functionS3Bucket_unlink);
JSC_DECLARE_HOST_FUNCTION(functionS3Bucket_write);
JSC_DECLARE_HOST_FUNCTION(functionS3Bucket_presign);
JSC_DECLARE_HOST_FUNCTION(functionS3Bucket_exists);
JSC_DECLARE_HOST_FUNCTION(functionS3Bucket_size);

static const HashTableValue JSS3BucketPrototypeTableValues[] = {
{ "unlink"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, functionS3Bucket_unlink, 0 } },
{ "write"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, functionS3Bucket_write, 1 } },
{ "presign"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, functionS3Bucket_presign, 1 } },
{ "exists"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, functionS3Bucket_exists, 1 } },
{ "size"_s, static_cast<unsigned>(PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, functionS3Bucket_size, 1 } },
};

class JSS3BucketPrototype final : public JSC::JSNonFinalObject {
Expand Down Expand Up @@ -106,8 +112,8 @@ JSC::GCClient::IsoSubspace* JSS3Bucket::subspaceForImpl(JSC::VM& vm)
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForJSS3Bucket.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForJSS3Bucket = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForS3m_clientSubspaceForJSS3Bucket.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForS3m_clientSubspaceForJSS3Bucket = std::forward<decltype(space)>(space); },
[](auto& spaces) { return spaces.m_subspaceForJSS3Bucket.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForJSS3Bucket = std::forward<decltype(space)>(space); },
[](auto& server) -> JSC::HeapCellType& { return server.m_heapCellTypeForJSWorkerGlobalScope; });
}

Expand Down Expand Up @@ -191,6 +197,32 @@ JSC_DEFINE_HOST_FUNCTION(functionS3Bucket_presign, (JSGlobalObject * globalObjec
return JSS3Bucket__presign(thisObject->ptr, globalObject, callframe);
}

JSC_DEFINE_HOST_FUNCTION(functionS3Bucket_exists, (JSGlobalObject * globalObject, CallFrame* callframe))
{
auto* thisObject = jsDynamicCast<JSS3Bucket*>(callframe->thisValue());
auto& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!thisObject) {
Bun::throwError(globalObject, scope, Bun::ErrorCode::ERR_INVALID_THIS, "Expected a S3Bucket instance"_s);
return {};
}

return JSS3Bucket__exists(thisObject->ptr, globalObject, callframe);
}

JSC_DEFINE_HOST_FUNCTION(functionS3Bucket_size, (JSGlobalObject * globalObject, CallFrame* callframe))
{
auto* thisObject = jsDynamicCast<JSS3Bucket*>(callframe->thisValue());
auto& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!thisObject) {
Bun::throwError(globalObject, scope, Bun::ErrorCode::ERR_INVALID_THIS, "Expected a S3Bucket instance"_s);
return {};
}

return JSS3Bucket__size(thisObject->ptr, globalObject, callframe);
}

JSValue constructS3Bucket(JSC::JSGlobalObject* globalObject, JSC::CallFrame* callframe)
{
auto& vm = globalObject->vm();
Expand All @@ -201,6 +233,10 @@ JSValue constructS3Bucket(JSC::JSGlobalObject* globalObject, JSC::CallFrame* cal

return JSS3Bucket::create(vm, defaultGlobalObject(globalObject), ptr);
}
SYSV_ABI JSValue BUN__createJSS3Bucket(JSC::JSGlobalObject* globalObject, JSC::CallFrame* callframe)
{
return constructS3Bucket(globalObject, callframe);
};

Structure* createJSS3BucketStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject)
{
Expand Down
3 changes: 2 additions & 1 deletion src/bun.js/bindings/JSS3Bucket.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ using namespace JSC;
class JSS3Bucket : public JSC::JSFunction {
using Base = JSC::JSFunction;
static constexpr unsigned StructureFlags = Base::StructureFlags;
static constexpr bool needsDestruction = true;

public:
static constexpr bool needsDestruction = true;

JSS3Bucket(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure, NativeExecutable* executable, void* ptr);

DECLARE_INFO;
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/bindings/JSS3File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ JSC::JSObject* createJSS3FileStaticObject(JSC::VM& vm, JSC::JSGlobalObject* glob
}
extern "C" {

JSC::EncodedJSValue BUN__createJSS3FileConstructor(JSGlobalObject* lexicalGlobalObject)
JSC::EncodedJSValue SYSV_ABI BUN__createJSS3FileConstructor(JSGlobalObject* lexicalGlobalObject)
{

return JSValue::encode(defaultGlobalObject(lexicalGlobalObject)->JSS3FileConstructor());
Expand Down
92 changes: 88 additions & 4 deletions src/bun.js/webcore/S3Bucket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,108 @@ pub fn presign(ptr: *AWSCredentials, globalThis: *JSC.JSGlobalObject, callframe:
var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments);
defer args.deinit();
const path: JSC.Node.PathLike = try JSC.Node.PathLike.fromJS(globalThis, &args) orelse {
return globalThis.throwInvalidArguments("S3Bucket.prototype..presign(pathOrS3, options) expects a path to presign", .{});
return globalThis.throwInvalidArguments("S3Bucket.prototype..presign(path, options) expects a path to presign", .{});
};
defer path.deinit();

const options = args.nextEat();
var blob = try S3File.constructS3FileWithAWSCredentials(globalThis, path, options, ptr.*);
var blob = try S3File.constructS3FileWithAWSCredentialsNoCloneIfPossible(globalThis, path, options, ptr.*);
defer blob.detach();
return S3File.getPresignUrlFrom(&blob, globalThis, options);
}

pub fn exists(ptr: *AWSCredentials, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue {
const arguments = callframe.arguments_old(2).slice();
var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments);
defer args.deinit();
const path: JSC.Node.PathLike = try JSC.Node.PathLike.fromJS(globalThis, &args) orelse {
return globalThis.throwInvalidArguments("S3Bucket.prototype..exists(path) expects a path to check if it exists", .{});
};
defer path.deinit();
const options = args.nextEat();
var blob = try S3File.constructS3FileWithAWSCredentialsNoCloneIfPossible(globalThis, path, options, ptr.*);
defer blob.detach();
return Blob.getExists(blob, globalThis, callframe);
}

pub fn size(ptr: *AWSCredentials, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue {
const arguments = callframe.arguments_old(2).slice();
var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments);
defer args.deinit();
const path: JSC.Node.PathLike = try JSC.Node.PathLike.fromJS(globalThis, &args) orelse {
return globalThis.throwInvalidArguments("S3Bucket.prototype..size(path) expects a path to check the size of", .{});
};
defer path.deinit();
const options = args.nextEat();
var blob = try S3File.constructS3FileWithAWSCredentialsNoCloneIfPossible(globalThis, path, options, ptr.*);
defer blob.detach();
return Blob.getSize(blob, globalThis, callframe);
}

pub fn write(ptr: *AWSCredentials, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue {
const arguments = callframe.arguments_old(2).slice();
var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments);
defer args.deinit();
const path: JSC.Node.PathLike = try JSC.Node.PathLike.fromJS(globalThis, &args) orelse {
return globalThis.throwInvalidArguments("S3Bucket.prototype..write(path, data) expects a path to write to", .{});
};
defer path.deinit();
const data = args.nextEat() orelse {
return globalThis.throwInvalidArguments("S3Bucket.prototype..write(path, data) expects a Blob-y thing to write", .{});
};

const options = args.nextEat();
//TODO: replace this because we dont wanna to clone the AWS credentials we wanna to ref/unref
var blob = try S3File.constructS3FileWithAWSCredentialsNoCloneIfPossible(globalThis, path, options, ptr.*);
defer blob.detach();

return Blob.writeFileInternal(globalThis, &blob, data, .{
.mkdirp_if_not_exists = false,
.extra_options = options,
});
}

pub fn unlink(ptr: *AWSCredentials, globalThis: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) bun.JSError!JSValue {
const arguments = callframe.arguments_old(2).slice();
var args = JSC.Node.ArgumentsSlice.init(globalThis.bunVM(), arguments);
defer args.deinit();
const path: JSC.Node.PathLike = try JSC.Node.PathLike.fromJS(globalThis, &args) orelse {
return globalThis.throwInvalidArguments("S3Bucket.prototype..unlink(path) expects a path to unlink", .{});
};
defer path.deinit();
const options = args.nextEat();
//TODO: replace this because we dont wanna to clone the AWS credentials we wanna to ref/unref
var blob = try S3File.constructS3FileWithAWSCredentialsNoCloneIfPossible(globalThis, path, options, ptr.*);
defer blob.detach();
return blob.store.?.data.s3.unlink(globalThis, options);
}

// Rest of the methods ...

pub fn finalize(ptr: *AWSCredentials) void {
ptr.deref();
}

pub const exports = struct {};
pub const exports = struct {
pub const JSS3Bucket__exists = JSC.toJSHostFunction(exists);
pub const JSS3Bucket__size = JSC.toJSHostFunction(size);
pub const JSS3Bucket__write = JSC.toJSHostFunction(write);
pub const JSS3Bucket__unlink = JSC.toJSHostFunction(unlink);
pub const JSS3Bucket__presign = JSC.toJSHostFunction(presign);
pub const JSS3Bucket__deinit = JSC.toJSHostFunction(finalize);
};

extern fn BUN__createJSS3Bucket(*JSC.JSGlobalObject, *JSC.CallFrame) callconv(JSC.conv) JSValue;
pub fn createJSS3Bucket(
globalObject: *JSC.JSGlobalObject,
callframe: *JSC.CallFrame,
) callconv(JSC.conv) JSValue {
return BUN__createJSS3Bucket(globalObject, callframe);
}
comptime {
// ...each of the exports
@export(exports.JSS3Bucket__exists, .{ .name = "JSS3Bucket__exists" });
@export(exports.JSS3Bucket__size, .{ .name = "JSS3Bucket__size" });
@export(exports.JSS3Bucket__write, .{ .name = "JSS3Bucket__write" });
@export(exports.JSS3Bucket__unlink, .{ .name = "JSS3Bucket__unlink" });
@export(exports.JSS3Bucket__presign, .{ .name = "JSS3Bucket__presign" });
}
Loading

0 comments on commit 0769ca9

Please sign in to comment.