Skip to content

Eric/test drivers #647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/backend/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -46,4 +47,5 @@ module.exports = {
PuterDriversModule,
LocalDiskStorageModule,
SelfHostedModule,
TestDriversModule,
};
3 changes: 3 additions & 0 deletions src/backend/src/data/hardcoded-permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
16 changes: 16 additions & 0 deletions src/backend/src/modules/test-drivers/TestAssetHostService.js
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions src/backend/src/modules/test-drivers/TestDriversModule.js
Original file line number Diff line number Diff line change
@@ -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 changes: 85 additions & 0 deletions src/backend/src/modules/test-drivers/TestImageService.js
Original file line number Diff line number Diff line change
@@ -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
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions src/backend/src/modules/test-drivers/doc/requests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
```javascript
blob = await (await fetch("http://api.puter.localhost:4100/drivers/call", {
"headers": {
"Content-Type": "application/json",
"Authorization": `Bearer ${puter.authToken}`,
},
"body": JSON.stringify({
interface: 'test-image',
method: 'get_image',
args: {
source_type: 'string:url:web'
}
}),
"method": "POST",
})).blob();
dataurl = await new Promise((y, n) => {
a = new FileReader();
a.onload = _ => y(a.result);
a.onerror = _ => n(a.error);
a.readAsDataURL(blob)
});
URL.createObjectURL(await (await fetch("http://api.puter.localhost:4100/drivers/call", {
"headers": {
"Content-Type": "application/json",
"Authorization": `Bearer ${puter.authToken}`,
},
"body": JSON.stringify({
interface: 'test-image',
method: 'echo_image',
args: {
source: dataurl,
}
}),
"method": "POST",
})).blob());
```

```javascript
await(async () => {

blob = await (await fetch("http://api.puter.localhost:4100/drivers/call", {
"headers": {
"Content-Type": "application/json",
"Authorization": `Bearer ${puter.authToken}`,
},
"body": JSON.stringify({
interface: 'test-image',
method: 'get_image',
args: {
source_type: 'string:url:web'
}
}),
"method": "POST",
})).blob();

const endpoint = 'http://api.puter.localhost:4100/drivers/call';

const body = {
object: {
interface: 'test-image',
method: 'echo_image',
['args.source']: {
$: 'file',
size: blob.size,
type: blob.type,
},
},
file: [
blob,
]
};

const formData = new FormData();
for ( const k in body ) {
console.log('k', k);
const append = v => {
if ( v instanceof Blob ) {
formData.append(k, v, 'filename');
} else {
formData.append(k, JSON.stringify(v));
}
};
if ( Array.isArray(body[k]) ) {
for ( const v of body[k] ) append(v);
} else {
append(body[k]);
}
}
const response = await fetch(endpoint, {
method: 'POST',
headers: { 'Authorization': `Bearer ${puter.authToken}` },
body: formData
});
const echo_blob = await response.blob();
const echo_url = URL.createObjectURL(echo_blob);
return echo_url;
})();
```
Loading