@@ -3245,8 +3245,11 @@ size_t KeyObjectData::GetSymmetricKeySize() const {
32453245 return symmetric_key_len_;
32463246}
32473247
3248- Local<Function> KeyObjectHandle::Initialize (Environment* env,
3249- Local<Object> target) {
3248+ Local<Function> KeyObjectHandle::Initialize (Environment* env) {
3249+ Local<Function> templ = env->crypto_key_object_handle_constructor ();
3250+ if (!templ.IsEmpty ()) {
3251+ return templ;
3252+ }
32503253 Local<FunctionTemplate> t = env->NewFunctionTemplate (New);
32513254 t->InstanceTemplate ()->SetInternalFieldCount (
32523255 KeyObjectHandle::kInternalFieldCount );
@@ -3260,20 +3263,16 @@ Local<Function> KeyObjectHandle::Initialize(Environment* env,
32603263 env->SetProtoMethod (t, " export" , Export);
32613264
32623265 auto function = t->GetFunction (env->context ()).ToLocalChecked ();
3263- target->Set (env->context (),
3264- FIXED_ONE_BYTE_STRING (env->isolate (), " KeyObjectHandle" ),
3265- function).Check ();
3266-
3267- return function;
3266+ env->set_crypto_key_object_handle_constructor (function);
3267+ return KeyObjectHandle::Initialize (env);
32683268}
32693269
32703270MaybeLocal<Object> KeyObjectHandle::Create (
32713271 Environment* env,
32723272 std::shared_ptr<KeyObjectData> data) {
32733273 Local<Object> obj;
3274- if (!env->crypto_key_object_handle_constructor ()
3275- ->NewInstance (env->context (), 0 , nullptr )
3276- .ToLocal (&obj)) {
3274+ Local<Function> fctun = KeyObjectHandle::Initialize (env);
3275+ if (!fctun->NewInstance (env->context (), 0 , nullptr ).ToLocal (&obj)) {
32773276 return MaybeLocal<Object>();
32783277 }
32793278
@@ -3446,6 +3445,11 @@ BaseObjectPtr<BaseObject> NativeKeyObject::KeyObjectTransferData::Deserialize(
34463445
34473446 Local<Value> handle = KeyObjectHandle::Create (env, data_).ToLocalChecked ();
34483447 Local<Function> key_ctor;
3448+ Local<Value> arg = FIXED_ONE_BYTE_STRING (env->isolate (),
3449+ " internal/crypto/keys" );
3450+ if (env->native_module_require ()->
3451+ Call (context, Null (env->isolate ()), 1 , &arg).IsEmpty ())
3452+ return {};
34493453 switch (data_->GetKeyType ()) {
34503454 case kKeyTypeSecret :
34513455 key_ctor = env->crypto_key_object_secret_constructor ();
@@ -6976,8 +6980,9 @@ void Initialize(Local<Object> target,
69766980
69776981 Environment* env = Environment::GetCurrent (context);
69786982 SecureContext::Initialize (env, target);
6979- env->set_crypto_key_object_handle_constructor (
6980- KeyObjectHandle::Initialize (env, target));
6983+ target->Set (env->context (),
6984+ FIXED_ONE_BYTE_STRING (env->isolate (), " KeyObjectHandle" ),
6985+ KeyObjectHandle::Initialize (env)).Check ();
69816986 env->SetMethod (target, " createNativeKeyObjectClass" ,
69826987 CreateNativeKeyObjectClass);
69836988 CipherBase::Initialize (env, target);
0 commit comments