Browse Source

dev: add service to test images through drivers

KernelDeimos 9 tháng trước cách đây
mục cha
commit
c28f2cb4df

+ 2 - 0
src/backend/exports.js

@@ -25,6 +25,7 @@ const PuterDriversModule = require("./src/PuterDriversModule.js");
 const { testlaunch } = require("./src/index.js");
 const { testlaunch } = require("./src/index.js");
 const BaseService = require("./src/services/BaseService.js");
 const BaseService = require("./src/services/BaseService.js");
 const { Context } = require("./src/util/context.js");
 const { Context } = require("./src/util/context.js");
+const { TestDriversModule } = require("./src/modules/test-drivers/TestDriversModule.js");
 
 
 
 
 module.exports = {
 module.exports = {
@@ -46,4 +47,5 @@ module.exports = {
     PuterDriversModule,
     PuterDriversModule,
     LocalDiskStorageModule,
     LocalDiskStorageModule,
     SelfHostedModule,
     SelfHostedModule,
+    TestDriversModule,
 };
 };

+ 3 - 0
src/backend/src/data/hardcoded-permissions.js

@@ -72,6 +72,9 @@ const policy_perm = selector => ({
 
 
 const hardcoded_user_group_permissions = {
 const hardcoded_user_group_permissions = {
     system: {
     system: {
+        'ca342a5e-b13d-4dee-9048-58b11a57cc55': {
+            'service': {},
+        },
         'b7220104-7905-4985-b996-649fdcdb3c8f': {
         'b7220104-7905-4985-b996-649fdcdb3c8f': {
             'service:hello-world:ii:hello-world': policy_perm('temp.es'),
             'service:hello-world:ii:hello-world': policy_perm('temp.es'),
             'driver:puter-kvstore': policy_perm('temp.kv'),
             'driver:puter-kvstore': policy_perm('temp.kv'),

+ 16 - 0
src/backend/src/modules/test-drivers/TestAssetHostService.js

@@ -0,0 +1,16 @@
+const BaseService = require("../../services/BaseService");
+
+class TestAssetHostService extends BaseService {
+    async ['__on_install.routes'] () {
+        const { app } = this.services.get('web-server');
+        const path_ = require('node:path');
+        
+        app.use('/test-assets', require('express').static(
+            path_.join(__dirname, 'assets')
+        ));
+    }
+}
+
+module.exports = {
+    TestAssetHostService
+};

+ 17 - 0
src/backend/src/modules/test-drivers/TestDriversModule.js

@@ -0,0 +1,17 @@
+const { AdvancedBase } = require("@heyputer/puter-js-common");
+
+class TestDriversModule extends AdvancedBase {
+    async install (context) {
+        const services = context.get('services');
+        
+        const { TestAssetHostService } = require('./TestAssetHostService')
+        services.registerService('__test-assets', TestAssetHostService);
+        
+        const { TestImageService } = require('./TestImageService');
+        services.registerService('test-image', TestImageService);
+    }
+}
+
+module.exports = {
+    TestDriversModule,
+};

+ 85 - 0
src/backend/src/modules/test-drivers/TestImageService.js

@@ -0,0 +1,85 @@
+const config = require("../../config");
+const BaseService = require("../../services/BaseService");
+const { TypedValue } = require("../../services/drivers/meta/Runtime");
+const { buffer_to_stream } = require("../../util/streamutil");
+
+const PUBLIC_DOMAIN_IMAGES = [
+    {
+        name: 'starry-night',
+        url: 'https://upload.wikimedia.org/wikipedia/commons/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg',
+        file: 'starry.jpg',
+    },
+];
+
+class TestImageService extends BaseService {
+    async ['__on_driver.register.interfaces'] () {
+        const svc_registry = this.services.get('registry');
+        const col_interfaces = svc_registry.get('interfaces');
+        
+        col_interfaces.set('test-image', {
+            methods: {
+                echo_image: {
+                    parameters: {
+                        source: {
+                            type: 'file',
+                        },
+                    },
+                    result: {
+                        type: {
+                            $: 'stream',
+                            content_type: 'image'
+                        },
+                    },
+                },
+                get_image: {
+                    parameters: {
+                        source_type: {
+                            type: 'string'
+                        },
+                    },
+                    result: {
+                        type: {
+                            $: 'stream',
+                            content_type: 'image'
+                        }
+                    }
+                }
+            }
+        });
+    }
+
+    static IMPLEMENTS = {
+        ['version']: {
+            get_version () {
+                return 'v1.0.0';
+            }
+        },
+        ['test-image']: {
+            async echo_image ({
+                source,
+            }) {
+                const stream = await source.get('stream');
+                return new TypedValue({
+                    $: 'stream',
+                    content_type: 'image/jpeg'
+                }, stream);
+            },
+            async get_image ({
+                source_type,
+            }) {
+                const image = PUBLIC_DOMAIN_IMAGES[0];
+                if ( source_type === 'string:url:web' ) {
+                    return new TypedValue({
+                        $: 'string:url:web',
+                        content_type: 'image',
+                    }, `${config.origin}/test-assets/${image.file}`);
+                }
+                throw new Error('not implemented yet');
+            }
+        },
+    }
+}
+
+module.exports = {
+    TestImageService
+};

BIN
src/backend/src/modules/test-drivers/assets/starry.jpg


BIN
src/backend/src/modules/test-drivers/assets/wave.jpg


+ 1 - 0
src/backend/src/services/drivers/CoercionService.js

@@ -40,6 +40,7 @@ class CoercionService extends BaseService {
                 content_type: 'image'
                 content_type: 'image'
             },
             },
             coerce: async typed_value => {
             coerce: async typed_value => {
+                this.log.noticeme('coercion is running!');
                 const response = await CoercionService.MODULES.axios.get(typed_value.value, {
                 const response = await CoercionService.MODULES.axios.get(typed_value.value, {
                     responseType: 'stream',
                     responseType: 'stream',
                 });
                 });

+ 14 - 4
src/backend/src/services/drivers/DriverService.js

@@ -111,6 +111,7 @@ class DriverService extends BaseService {
     }
     }
 
 
     async _call ({ driver, iface, method, args }) {
     async _call ({ driver, iface, method, args }) {
+        console.log('??', driver, iface, method, args);
         const processed_args = await this._process_args(iface, method, args);
         const processed_args = await this._process_args(iface, method, args);
         if ( Context.get('test_mode') ) {
         if ( Context.get('test_mode') ) {
             processed_args.test_mode = true;
             processed_args.test_mode = true;
@@ -293,7 +294,7 @@ class DriverService extends BaseService {
                 {
                 {
                     name: 'enforce logical rate-limit',
                     name: 'enforce logical rate-limit',
                     on_call: async args => {
                     on_call: async args => {
-                        if ( ! effective_policy['rate-limit'] ) return args;
+                        if ( ! effective_policy?.['rate-limit'] ) return args;
                         const svc_su = this.services.get('su');
                         const svc_su = this.services.get('su');
                         const svc_rateLimit = this.services.get('rate-limit');
                         const svc_rateLimit = this.services.get('rate-limit');
                         await svc_su.sudo(policy_holder, async () => {
                         await svc_su.sudo(policy_holder, async () => {
@@ -309,7 +310,7 @@ class DriverService extends BaseService {
                 {
                 {
                     name: 'enforce monthly usage limit',
                     name: 'enforce monthly usage limit',
                     on_call: async args => {
                     on_call: async args => {
-                        if ( ! effective_policy['monthly-limit'] ) return args;
+                        if ( ! effective_policy?.['monthly-limit'] ) return args;
                         const svc_monthlyUsage = services.get('monthly-usage');
                         const svc_monthlyUsage = services.get('monthly-usage');
                         const count = await svc_monthlyUsage.check_2(
                         const count = await svc_monthlyUsage.check_2(
                             actor, method_key
                             actor, method_key
@@ -356,9 +357,18 @@ class DriverService extends BaseService {
                     name: 'result coercion',
                     name: 'result coercion',
                     on_return: async (result) => {
                     on_return: async (result) => {
                         if ( result instanceof TypedValue ) {
                         if ( result instanceof TypedValue ) {
+                            const svc_registry = this.services.get('registry');
+                            const c_interfaces = svc_registry.get('interfaces');
+
+                            console.log('????--1', iface);
                             const interface_ = c_interfaces.get(iface);
                             const interface_ = c_interfaces.get(iface);
-                            let desired_type = interface_.methods[method]
-                                .result_choices[0].type;
+                            console.log('????--2', interface_);
+                            const method_spec = interface_.methods[method];
+                            let desired_type =
+                                method_spec.result_choices
+                                    ? method_spec.result_choices[0].type
+                                    : method_spec.result.type
+                                    ;
                             const svc_coercion = services.get('coercion');
                             const svc_coercion = services.get('coercion');
                             result = await svc_coercion.coerce(desired_type, result);
                             result = await svc_coercion.coerce(desired_type, result);
                         }
                         }

+ 3 - 1
tools/run-selfhosted.js

@@ -83,7 +83,8 @@ const main = async () => {
         CoreModule,
         CoreModule,
         DatabaseModule,
         DatabaseModule,
         LocalDiskStorageModule,
         LocalDiskStorageModule,
-        SelfHostedModule
+        SelfHostedModule,
+        TestDriversModule,
     } = (await import('@heyputer/backend')).default;
     } = (await import('@heyputer/backend')).default;
 
 
     const k = new Kernel({
     const k = new Kernel({
@@ -93,6 +94,7 @@ const main = async () => {
     k.add_module(new DatabaseModule());
     k.add_module(new DatabaseModule());
     k.add_module(new LocalDiskStorageModule());
     k.add_module(new LocalDiskStorageModule());
     k.add_module(new SelfHostedModule());
     k.add_module(new SelfHostedModule());
+    k.add_module(new TestDriversModule());
     k.boot();
     k.boot();
 };
 };