@@ -2978,6 +2978,51 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
2978
2978
}
2979
2979
}
2980
2980
2981
+ static void GetFormatOfExtensionlessFile (
2982
+ const FunctionCallbackInfo<Value>& args) {
2983
+ CHECK_EQ (args.Length (), 1 );
2984
+ CHECK (args[0 ]->IsString ());
2985
+
2986
+ Environment* env = Environment::GetCurrent (args);
2987
+ node::Utf8Value input (args.GetIsolate (), args[0 ]);
2988
+
2989
+ THROW_IF_INSUFFICIENT_PERMISSIONS (
2990
+ env, permission::PermissionScope::kFileSystemRead , input.ToStringView ());
2991
+
2992
+ uv_fs_t req;
2993
+ FS_SYNC_TRACE_BEGIN (open )
2994
+ uv_file file = uv_fs_open (nullptr , &req, input.out (), O_RDONLY, 0 , nullptr );
2995
+ FS_SYNC_TRACE_END (open );
2996
+
2997
+ if (req.result < 0 ) {
2998
+ return args.GetReturnValue ().Set (EXTENSIONLESS_FORMAT_JAVASCRIPT);
2999
+ }
3000
+
3001
+ auto cleanup = OnScopeLeave ([&req, &file]() {
3002
+ FS_SYNC_TRACE_BEGIN (close );
3003
+ CHECK_EQ (0 , uv_fs_close (nullptr , &req, file, nullptr ));
3004
+ FS_SYNC_TRACE_END (close );
3005
+ uv_fs_req_cleanup (&req);
3006
+ });
3007
+
3008
+ char buffer[4 ];
3009
+ uv_buf_t buf = uv_buf_init (buffer, sizeof (buffer));
3010
+ int err = uv_fs_read (nullptr , &req, file, &buf, 1 , 0 , nullptr );
3011
+
3012
+ if (err < 0 ) {
3013
+ return args.GetReturnValue ().Set (EXTENSIONLESS_FORMAT_JAVASCRIPT);
3014
+ }
3015
+
3016
+ // We do this by taking advantage of the fact that all Wasm files start with
3017
+ // the header `0x00 0x61 0x73 0x6d`
3018
+ if (buffer[0 ] == 0x00 && buffer[1 ] == 0x61 && buffer[2 ] == 0x73 &&
3019
+ buffer[3 ] == 0x6d ) {
3020
+ return args.GetReturnValue ().Set (EXTENSIONLESS_FORMAT_WASM);
3021
+ }
3022
+
3023
+ return args.GetReturnValue ().Set (EXTENSIONLESS_FORMAT_JAVASCRIPT);
3024
+ }
3025
+
2981
3026
static bool FileURLToPath (
2982
3027
Environment* env,
2983
3028
const ada::url_aggregator& file_url,
@@ -3390,6 +3435,10 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
3390
3435
Local<ObjectTemplate> target) {
3391
3436
Isolate* isolate = isolate_data->isolate ();
3392
3437
3438
+ SetMethod (isolate,
3439
+ target,
3440
+ " getFormatOfExtensionlessFile" ,
3441
+ GetFormatOfExtensionlessFile);
3393
3442
SetMethod (isolate, target, " access" , Access);
3394
3443
SetMethod (isolate, target, " accessSync" , AccessSync);
3395
3444
SetMethod (isolate, target, " close" , Close);
@@ -3518,6 +3567,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
3518
3567
StatWatcher::RegisterExternalReferences (registry);
3519
3568
BindingData::RegisterExternalReferences (registry);
3520
3569
3570
+ registry->Register (GetFormatOfExtensionlessFile);
3521
3571
registry->Register (Close);
3522
3572
registry->Register (CloseSync);
3523
3573
registry->Register (ExistsSync);
0 commit comments