dev: add service to test images through drivers

This commit is contained in:
KernelDeimos
2024-07-29 15:05:55 -04:00
committed by Eric Dubé
parent 1e753843d2
commit c28f2cb4df
10 changed files with 141 additions and 5 deletions

View File

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

View File

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

View File

@@ -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
};

View File

@@ -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,
};

View File

@@ -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
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 950 KiB

View File

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

View File

@@ -111,6 +111,7 @@ class DriverService extends BaseService {
}
async _call ({ driver, iface, method, args }) {
console.log('??', driver, iface, method, args);
const processed_args = await this._process_args(iface, method, args);
if ( Context.get('test_mode') ) {
processed_args.test_mode = true;
@@ -293,7 +294,7 @@ class DriverService extends BaseService {
{
name: 'enforce logical rate-limit',
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_rateLimit = this.services.get('rate-limit');
await svc_su.sudo(policy_holder, async () => {
@@ -309,7 +310,7 @@ class DriverService extends BaseService {
{
name: 'enforce monthly usage limit',
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 count = await svc_monthlyUsage.check_2(
actor, method_key
@@ -356,9 +357,18 @@ class DriverService extends BaseService {
name: 'result coercion',
on_return: async (result) => {
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);
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');
result = await svc_coercion.coerce(desired_type, result);
}

View File

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