@primate/python 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/package.json +39 -0
- package/src/default.js +8 -0
- package/src/private/build.js +42 -0
- package/src/private/extension.js +1 -0
- package/src/private/pkgname.js +1 -0
- package/src/runtime.js +3 -0
- package/src/to-request.js +78 -0
- package/src/to-response.js +31 -0
- package/src/unwrap.js +17 -0
- package/src/wrap.js +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) Terrablue <terrablue@proton.me> and contributors.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
16
|
+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@primate/python",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Primate Python backend",
|
|
5
|
+
"homepage": "https://primatejs.com/modules/python",
|
|
6
|
+
"bugs": "https://github.com/primatejs/primate/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"files": [
|
|
9
|
+
"src/**/*.js",
|
|
10
|
+
"!src/**/*.spec.js"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/primatejs/primate",
|
|
15
|
+
"directory": "packages/python"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@rcompat/fs": "^0.4.0",
|
|
19
|
+
"@primate/core": "^0.1.0"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"pyodide": "0.26",
|
|
23
|
+
"primate": "^0.32.0"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"imports": {
|
|
27
|
+
"#*": {
|
|
28
|
+
"@primate/lt": "./src/private/*.js",
|
|
29
|
+
"default": "./src/private/*.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"runtime": "./src/runtime.js",
|
|
35
|
+
"default": "./src/default.js"
|
|
36
|
+
},
|
|
37
|
+
"./*": "./src/*.js"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/default.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const routes_re = /def (?<route>get|post|put|delete)/gu;
|
|
2
|
+
const get_routes = code => [...code.matchAll(routes_re)]
|
|
3
|
+
.map(({ groups: { route } }) => route);
|
|
4
|
+
|
|
5
|
+
const make_route = route => `async ${route.toLowerCase()}(request) {
|
|
6
|
+
const ${route}_fn = pyodide.globals.get("${route}");
|
|
7
|
+
return to_response(await ${route}_fn(to_request(pyodide.toPy, request)));
|
|
8
|
+
}`;
|
|
9
|
+
|
|
10
|
+
const make_package = pkg => `await pyodide.loadPackage("${pkg}", {
|
|
11
|
+
messageCallback: _ => _,
|
|
12
|
+
});\n`;
|
|
13
|
+
|
|
14
|
+
const js_wrapper = async (path, routes, packages) => `
|
|
15
|
+
import file from "@rcompat/fs/file";
|
|
16
|
+
import to_request from "@primate/python/to-request";
|
|
17
|
+
import to_response from "@primate/python/to-response";
|
|
18
|
+
import wrap from "@primate/python/wrap";
|
|
19
|
+
import { loadPyodide as load } from "pyodide";
|
|
20
|
+
|
|
21
|
+
const pyodide = await load({ indexURL: "./node_modules/pyodide" });
|
|
22
|
+
const python_route = await file(${JSON.stringify(path)}).text();
|
|
23
|
+
${packages.map(make_package)}
|
|
24
|
+
pyodide.runPython(wrap(python_route));
|
|
25
|
+
export default {
|
|
26
|
+
${routes.map(route => make_route(route)).join(",\n")}
|
|
27
|
+
};
|
|
28
|
+
`;
|
|
29
|
+
|
|
30
|
+
export default ({ extension, packages }) => async (app, next) => {
|
|
31
|
+
app.bind(extension, async (directory, file) => {
|
|
32
|
+
const path = directory.join(file);
|
|
33
|
+
const base = path.directory;
|
|
34
|
+
const js = path.base.concat(".js");
|
|
35
|
+
const code = await path.text();
|
|
36
|
+
const routes = get_routes(code);
|
|
37
|
+
// write .js wrapper
|
|
38
|
+
await base.join(js).write(await js_wrapper(`${path}`, routes, packages));
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return next(app);
|
|
42
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default ".py";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default "@primate/python";
|
package/src/runtime.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { unwrap, unwrap_async } from "./unwrap.js";
|
|
2
|
+
|
|
3
|
+
const dispatchers = ["path", "query", "headers", "cookies"];
|
|
4
|
+
|
|
5
|
+
const wrap_dispatchers = (toPy, request) =>
|
|
6
|
+
Object.fromEntries(dispatchers.map(dispatcher =>
|
|
7
|
+
[dispatcher, {
|
|
8
|
+
...request[dispatcher],
|
|
9
|
+
json() {
|
|
10
|
+
return toPy(request[dispatcher].json());
|
|
11
|
+
},
|
|
12
|
+
}]));
|
|
13
|
+
|
|
14
|
+
const wrap_store = (toPy, store) => {
|
|
15
|
+
return {
|
|
16
|
+
...store,
|
|
17
|
+
async validate(input) {
|
|
18
|
+
return toPy(await store.validate(unwrap_async(input)));
|
|
19
|
+
},
|
|
20
|
+
async get(value) {
|
|
21
|
+
return toPy(await store.get(unwrap_async(value)));
|
|
22
|
+
},
|
|
23
|
+
async count(criteria) {
|
|
24
|
+
return store.count(unwrap_async(criteria));
|
|
25
|
+
},
|
|
26
|
+
async find(criteria) {
|
|
27
|
+
return toPy(await store.find(unwrap_async(criteria)));
|
|
28
|
+
},
|
|
29
|
+
async exists(criteria) {
|
|
30
|
+
return store.exists(unwrap_async(criteria));
|
|
31
|
+
},
|
|
32
|
+
async insert(document) {
|
|
33
|
+
return toPy(await store.insert(unwrap_async(document)));
|
|
34
|
+
},
|
|
35
|
+
async update(criteria, document) {
|
|
36
|
+
const ua = unwrap_async;
|
|
37
|
+
return toPy(await store.update(ua(criteria), ua(document)));
|
|
38
|
+
},
|
|
39
|
+
async save(document) {
|
|
40
|
+
return toPy(await store.save(unwrap_async(document)));
|
|
41
|
+
},
|
|
42
|
+
async delete(criteria) {
|
|
43
|
+
return store.delete(unwrap_async(criteria));
|
|
44
|
+
},
|
|
45
|
+
schema: {
|
|
46
|
+
async create(description) {
|
|
47
|
+
return store.schema.create(unwrap_async(description));
|
|
48
|
+
},
|
|
49
|
+
async delete() {
|
|
50
|
+
return store.schema.delete();
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const is_store = value => value.connection !== undefined;
|
|
57
|
+
const wrap_stores = (toPy, object) => Object.entries(object)
|
|
58
|
+
.reduce((reduced, [key, value]) => ({
|
|
59
|
+
...reduced,
|
|
60
|
+
[key]: is_store(value) ? wrap_store(toPy, value) : wrap_stores(toPy, value),
|
|
61
|
+
}), {});
|
|
62
|
+
|
|
63
|
+
const wrap_session = (toPy, { session }) => ({
|
|
64
|
+
...session,
|
|
65
|
+
create(data) {
|
|
66
|
+
session.create(unwrap(data));
|
|
67
|
+
},
|
|
68
|
+
json() {
|
|
69
|
+
return toPy(session.json());
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
export default (toPy, request) => ({
|
|
74
|
+
...request,
|
|
75
|
+
...wrap_dispatchers(toPy, request),
|
|
76
|
+
session: wrap_session(toPy, request),
|
|
77
|
+
store: wrap_stores(toPy, request.store),
|
|
78
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import error from "@primate/core/handler/error";
|
|
2
|
+
import redirect from "@primate/core/handler/redirect";
|
|
3
|
+
import view from "@primate/core/handler/view";
|
|
4
|
+
|
|
5
|
+
import { unwrap } from "./unwrap.js";
|
|
6
|
+
|
|
7
|
+
const handlers = {
|
|
8
|
+
view({ name, props = {}, options = {} }) {
|
|
9
|
+
return view(name, props, options);
|
|
10
|
+
},
|
|
11
|
+
redirect({ location, options }) {
|
|
12
|
+
return redirect(location, options);
|
|
13
|
+
},
|
|
14
|
+
error({ body, options }) {
|
|
15
|
+
return error(body, options);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const handle_handler = response => {
|
|
20
|
+
const { __handler__ : handler, ...args } = response;
|
|
21
|
+
return handlers[handler]?.(args) ?? error();
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const handle_other = response => response;
|
|
25
|
+
|
|
26
|
+
export default raw_response => {
|
|
27
|
+
const response = unwrap(raw_response);
|
|
28
|
+
return response.__handler__ === undefined
|
|
29
|
+
? handle_other(response)
|
|
30
|
+
: handle_handler(response);
|
|
31
|
+
};
|
package/src/unwrap.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const dict_converter = value => Object.fromEntries(value);
|
|
2
|
+
|
|
3
|
+
const normalize = response =>
|
|
4
|
+
response instanceof Map ? Object.fromEntries(response.entries()) : response;
|
|
5
|
+
const qualify = (response, destroy = true) => {
|
|
6
|
+
if (response?.toJs !== undefined) {
|
|
7
|
+
const py_proxy = response;
|
|
8
|
+
const converted = py_proxy.toJs({ dict_converter });
|
|
9
|
+
destroy && py_proxy.destroy();
|
|
10
|
+
return converted;
|
|
11
|
+
}
|
|
12
|
+
return response;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const unwrap = response => normalize(qualify(response));
|
|
16
|
+
|
|
17
|
+
export const unwrap_async = response => normalize(qualify(response, false));
|
package/src/wrap.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export default code => `
|
|
2
|
+
class Primate():
|
|
3
|
+
@staticmethod
|
|
4
|
+
def view(name, props = None, options = None):
|
|
5
|
+
return {
|
|
6
|
+
"__handler__": "view",
|
|
7
|
+
"name": name,
|
|
8
|
+
"props": props,
|
|
9
|
+
"options": options,
|
|
10
|
+
}
|
|
11
|
+
@staticmethod
|
|
12
|
+
def redirect(location, options = None):
|
|
13
|
+
return {
|
|
14
|
+
"__handler__": "redirect",
|
|
15
|
+
"location": location,
|
|
16
|
+
"options": options,
|
|
17
|
+
}
|
|
18
|
+
@staticmethod
|
|
19
|
+
def error(body, options = None):
|
|
20
|
+
return {
|
|
21
|
+
"__handler__": "error",
|
|
22
|
+
"body": body,
|
|
23
|
+
"options": options,
|
|
24
|
+
}\n${code}`;
|