@primate/python 0.5.0 → 0.7.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/lib/private/default.d.ts +4 -0
- package/lib/private/default.js +81 -0
- package/lib/private/errors.d.ts +12 -0
- package/lib/private/errors.js +24 -0
- package/lib/private/module.d.ts +13 -0
- package/lib/private/module.js +11 -0
- package/lib/private/runtime.d.ts +4 -0
- package/lib/private/runtime.js +10 -0
- package/lib/private/to-request.d.ts +28 -31
- package/lib/private/to-request.js +46 -82
- package/lib/private/to-response.d.ts +3 -3
- package/lib/private/to-response.js +15 -8
- package/lib/private/unwrap.d.ts +3 -2
- package/lib/private/wrapper.d.ts +1 -1
- package/lib/private/wrapper.js +21 -13
- package/lib/public/default.d.ts +1 -3
- package/lib/public/default.js +1 -2
- package/lib/public/runtime.d.ts +1 -3
- package/lib/public/runtime.js +1 -2
- package/package.json +12 -10
- package/lib/private/Default.d.ts +0 -7
- package/lib/private/Default.js +0 -74
- package/lib/private/Runtime.d.ts +0 -6
- package/lib/private/Runtime.js +0 -6
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import E from "#errors";
|
|
2
|
+
import module from "#module";
|
|
3
|
+
import server from "@primate/core/server";
|
|
4
|
+
import assert from "@rcompat/assert";
|
|
5
|
+
import fs from "@rcompat/fs";
|
|
6
|
+
import io from "@rcompat/io";
|
|
7
|
+
const PACKAGE = "primate-run";
|
|
8
|
+
const [MAJOR, MINOR] = server.TAG.split(".").map(Number);
|
|
9
|
+
async function show_package(app) {
|
|
10
|
+
function run(command) {
|
|
11
|
+
return io.run(command, { cwd: app.root.path })
|
|
12
|
+
.then(out => out.trim())
|
|
13
|
+
.catch(() => "");
|
|
14
|
+
}
|
|
15
|
+
let out = await run(`pip show ${PACKAGE} 2>/dev/null`);
|
|
16
|
+
if (out.length > 0)
|
|
17
|
+
return out;
|
|
18
|
+
out = await run(`uv pip show ${PACKAGE} 2>/dev/null`);
|
|
19
|
+
if (out.length > 0)
|
|
20
|
+
return out;
|
|
21
|
+
out = await run(`python -m pip show ${PACKAGE} 2>/dev/null`);
|
|
22
|
+
if (out.length > 0)
|
|
23
|
+
return out;
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
async function check_version(app) {
|
|
27
|
+
const output = await show_package(app);
|
|
28
|
+
if (output === null)
|
|
29
|
+
throw E.pkg_not_found();
|
|
30
|
+
const version_match = output.match(/Version:\s*(\d+)\.(\d+)\.(\d+)/);
|
|
31
|
+
if (version_match === null)
|
|
32
|
+
throw E.pkg_not_found();
|
|
33
|
+
const [major, minor] = version_match.slice(1).map(Number);
|
|
34
|
+
if (major !== MAJOR || minor !== MINOR)
|
|
35
|
+
throw E.pkg_mismatch(major, minor);
|
|
36
|
+
app.log.info `using ${PACKAGE} package ${major}.${minor}.x`;
|
|
37
|
+
}
|
|
38
|
+
function wrap(source, id, packages_str, primate_run) {
|
|
39
|
+
return `
|
|
40
|
+
import route from "primate/route";
|
|
41
|
+
import wrapper from "@primate/python/wrapper";
|
|
42
|
+
import i18n from "app:config:i18n";
|
|
43
|
+
import session from "app:config:session";
|
|
44
|
+
const handlers = await wrapper(
|
|
45
|
+
${JSON.stringify(source)},
|
|
46
|
+
${packages_str},
|
|
47
|
+
${JSON.stringify(primate_run)},
|
|
48
|
+
${JSON.stringify(id)},
|
|
49
|
+
{ i18n, session }
|
|
50
|
+
);
|
|
51
|
+
export default route(handlers);
|
|
52
|
+
`;
|
|
53
|
+
}
|
|
54
|
+
export default function default_module(input = {}) {
|
|
55
|
+
const { extension = module.extension } = module.schema.parse(input);
|
|
56
|
+
return {
|
|
57
|
+
name: module.name,
|
|
58
|
+
setup({ onBuild }) {
|
|
59
|
+
onBuild(async (app) => {
|
|
60
|
+
await check_version(app);
|
|
61
|
+
const requirements_txt = app.root.join("requirements.txt");
|
|
62
|
+
let packages = [];
|
|
63
|
+
if (await requirements_txt.exists()) {
|
|
64
|
+
const requirements = await fs.text(requirements_txt);
|
|
65
|
+
packages = requirements
|
|
66
|
+
.split("\n")
|
|
67
|
+
.filter(line => line.trim().length > 0 && !line.startsWith("#"))
|
|
68
|
+
.map(p => p.trim());
|
|
69
|
+
}
|
|
70
|
+
const packages_str = JSON.stringify(packages);
|
|
71
|
+
app.bind(extension, async (file, { context }) => {
|
|
72
|
+
assert.true(context === "routes", E.only_route_files());
|
|
73
|
+
const relative = file.debase(app.path.routes).path.replace(/^\//, "");
|
|
74
|
+
const source = await file.text();
|
|
75
|
+
return wrap(source, relative, packages_str, `${PACKAGE}~=${server.TAG}.0`);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=default.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare function only_route_files(): import("@rcompat/error").TemplateError;
|
|
2
|
+
declare function pkg_not_found(): import("@rcompat/error").TemplateError;
|
|
3
|
+
declare function pkg_mismatch(major: number, minor: number): import("@rcompat/error").TemplateError;
|
|
4
|
+
declare const errors: {
|
|
5
|
+
only_route_files: typeof only_route_files;
|
|
6
|
+
pkg_not_found: typeof pkg_not_found;
|
|
7
|
+
pkg_mismatch: typeof pkg_mismatch;
|
|
8
|
+
};
|
|
9
|
+
export type Code = keyof typeof errors;
|
|
10
|
+
export declare const Code: { [K in Code]: K; };
|
|
11
|
+
export default errors;
|
|
12
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import server from "@primate/core/server";
|
|
2
|
+
import error from "@rcompat/error";
|
|
3
|
+
const PACKAGE = "primate-run";
|
|
4
|
+
const t = error.template;
|
|
5
|
+
function only_route_files() {
|
|
6
|
+
return t `python: only route files are supported`;
|
|
7
|
+
}
|
|
8
|
+
function pkg_not_found() {
|
|
9
|
+
const command = `pip install ${PACKAGE}~=${server.TAG}.0`;
|
|
10
|
+
return t `package not found, run ${command}`;
|
|
11
|
+
}
|
|
12
|
+
function pkg_mismatch(major, minor) {
|
|
13
|
+
const range = `~> ${server.TAG}.x`;
|
|
14
|
+
const version = `${major}.${minor}.x`;
|
|
15
|
+
return t `${PACKAGE} package version ${version} not in range ${range}`;
|
|
16
|
+
}
|
|
17
|
+
const errors = error.coded({
|
|
18
|
+
only_route_files,
|
|
19
|
+
pkg_not_found,
|
|
20
|
+
pkg_mismatch,
|
|
21
|
+
});
|
|
22
|
+
export const Code = Object.fromEntries(Object.keys(errors).map(k => [k, k]));
|
|
23
|
+
export default errors;
|
|
24
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare const module: {
|
|
2
|
+
schema: import("pema").ObjectType<import("pema").NormalizeSchemaObject<{
|
|
3
|
+
readonly extension: import("pema").OptionalType<import("pema").StringType>;
|
|
4
|
+
}>, {
|
|
5
|
+
extension: string | undefined;
|
|
6
|
+
}>;
|
|
7
|
+
name: string;
|
|
8
|
+
extension: string;
|
|
9
|
+
};
|
|
10
|
+
type Input = typeof module.schema.input;
|
|
11
|
+
export type { Input };
|
|
12
|
+
export default module;
|
|
13
|
+
//# sourceMappingURL=module.d.ts.map
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { RequestFacade } from "@primate/core
|
|
2
|
-
import type { Dict } from "@rcompat/type";
|
|
1
|
+
import type { RequestContentType, RequestFacade } from "@primate/core";
|
|
3
2
|
type FileEntry = {
|
|
4
3
|
bytes: Uint8Array;
|
|
5
4
|
field: string;
|
|
@@ -7,35 +6,33 @@ type FileEntry = {
|
|
|
7
6
|
size: number;
|
|
8
7
|
type: string;
|
|
9
8
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
headers: Dict;
|
|
38
|
-
cookies: Dict;
|
|
9
|
+
type BridgedBody = {
|
|
10
|
+
type: "json";
|
|
11
|
+
jsonSync: () => string;
|
|
12
|
+
} | {
|
|
13
|
+
type: "text";
|
|
14
|
+
textSync: () => string;
|
|
15
|
+
} | {
|
|
16
|
+
type: "form";
|
|
17
|
+
formSync: () => string;
|
|
18
|
+
} | {
|
|
19
|
+
type: "multipart";
|
|
20
|
+
formSync: () => string;
|
|
21
|
+
filesSync: () => FileEntry[];
|
|
22
|
+
} | {
|
|
23
|
+
type: "blob";
|
|
24
|
+
blobSync: () => Uint8Array;
|
|
25
|
+
blobTypeSync: () => string;
|
|
26
|
+
} | {
|
|
27
|
+
type: "none";
|
|
28
|
+
};
|
|
29
|
+
export default function to_request(request: RequestFacade, contentType: RequestContentType | ""): Promise<{
|
|
30
|
+
url: URL;
|
|
31
|
+
body: BridgedBody;
|
|
32
|
+
path: any;
|
|
33
|
+
query: any;
|
|
34
|
+
headers: any;
|
|
35
|
+
cookies: any;
|
|
39
36
|
}>;
|
|
40
37
|
export {};
|
|
41
38
|
//# sourceMappingURL=to-request.d.ts.map
|
|
@@ -1,25 +1,45 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
async function bridge_body(body, contentType) {
|
|
2
|
+
switch (contentType) {
|
|
3
|
+
case "text/plain": {
|
|
4
|
+
const s = await body.text();
|
|
5
|
+
return { type: "text", textSync: () => s };
|
|
6
|
+
}
|
|
7
|
+
case "application/json": {
|
|
8
|
+
const val = await body.json();
|
|
9
|
+
return { type: "json", jsonSync: () => JSON.stringify(val) };
|
|
10
|
+
}
|
|
11
|
+
case "application/x-www-form-urlencoded": {
|
|
12
|
+
const form = await body.form();
|
|
13
|
+
return { type: "form", formSync: () => JSON.stringify(form) };
|
|
14
|
+
}
|
|
15
|
+
case "multipart/form-data": {
|
|
16
|
+
const { form, files } = await body.multipart();
|
|
17
|
+
const pending = [];
|
|
18
|
+
const bridgedFiles = [];
|
|
19
|
+
for (const [field, file] of Object.entries(files)) {
|
|
20
|
+
const name = file.name;
|
|
21
|
+
const type = file.type;
|
|
22
|
+
const size = file.size;
|
|
23
|
+
pending.push(file.arrayBuffer().then(buffer => {
|
|
24
|
+
bridgedFiles.push({ bytes: new Uint8Array(buffer), field, name, size, type });
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
await Promise.all(pending);
|
|
28
|
+
return { type: "multipart", formSync: () => JSON.stringify(form), filesSync: () => bridgedFiles };
|
|
29
|
+
}
|
|
30
|
+
case "application/octet-stream": {
|
|
31
|
+
const blob = await body.blob();
|
|
32
|
+
const buf = new Uint8Array(await blob.arrayBuffer());
|
|
33
|
+
return { type: "blob", blobSync: () => buf, blobTypeSync: () => blob.type || "application/octet-stream" };
|
|
34
|
+
}
|
|
35
|
+
default:
|
|
36
|
+
return { type: "none" };
|
|
37
|
+
}
|
|
15
38
|
}
|
|
16
|
-
function
|
|
39
|
+
function to_dict(obj) {
|
|
17
40
|
const out = Object.create(null);
|
|
18
41
|
if (obj instanceof URLSearchParams) {
|
|
19
|
-
obj.forEach((v, k) => {
|
|
20
|
-
if (!(k in out))
|
|
21
|
-
out[k] = v ?? "";
|
|
22
|
-
});
|
|
42
|
+
obj.forEach((v, k) => { out[k] = v; });
|
|
23
43
|
}
|
|
24
44
|
else {
|
|
25
45
|
for (const [k, v] of Object.entries(obj)) {
|
|
@@ -28,71 +48,15 @@ function toDict(obj) {
|
|
|
28
48
|
}
|
|
29
49
|
return out;
|
|
30
50
|
}
|
|
31
|
-
async function
|
|
32
|
-
const
|
|
33
|
-
const files = [];
|
|
34
|
-
const pending = [];
|
|
35
|
-
for (const [k, v] of Object.entries(body.form())) {
|
|
36
|
-
form[k] = v;
|
|
37
|
-
}
|
|
38
|
-
for (const [k, v] of Object.entries(body.files())) {
|
|
39
|
-
const name = v.name;
|
|
40
|
-
const type = v.type;
|
|
41
|
-
const size = v.size;
|
|
42
|
-
pending.push(v.arrayBuffer().then(buffer => {
|
|
43
|
-
files.push({
|
|
44
|
-
bytes: new Uint8Array(buffer),
|
|
45
|
-
field: k,
|
|
46
|
-
name,
|
|
47
|
-
size,
|
|
48
|
-
type,
|
|
49
|
-
});
|
|
50
|
-
}));
|
|
51
|
-
}
|
|
52
|
-
await Promise.all(pending);
|
|
53
|
-
return {
|
|
54
|
-
form: () => form,
|
|
55
|
-
files: () => files,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
async function bridgeBody(body) {
|
|
59
|
-
let form, buffer, blob;
|
|
60
|
-
if (body.type === "form") {
|
|
61
|
-
form = await bridge_form(body);
|
|
62
|
-
}
|
|
63
|
-
if (body.type === "binary") {
|
|
64
|
-
blob = body.binary();
|
|
65
|
-
buffer = new Uint8Array(await blob.arrayBuffer());
|
|
66
|
-
}
|
|
67
|
-
return {
|
|
68
|
-
text: () => body.text(),
|
|
69
|
-
json: () => {
|
|
70
|
-
return JSON.stringify(body.json());
|
|
71
|
-
},
|
|
72
|
-
form: () => form.form(),
|
|
73
|
-
files: () => form.files(),
|
|
74
|
-
binary: () => {
|
|
75
|
-
const mime = blob.type || "application/octet-stream";
|
|
76
|
-
return {
|
|
77
|
-
buffer,
|
|
78
|
-
mime,
|
|
79
|
-
};
|
|
80
|
-
},
|
|
81
|
-
none: () => null,
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
export default async function to_request(request) {
|
|
85
|
-
const body = await bridgeBody(request.body);
|
|
86
|
-
const cookies = toDict(request.cookies.toJSON());
|
|
87
|
-
const headers = toDict(request.headers.toJSON());
|
|
88
|
-
const path = toDict(request.path.toJSON());
|
|
89
|
-
const query = toDict(request.query.toJSON());
|
|
90
|
-
const u = request.url;
|
|
91
|
-
const url = plainUrl(new URL(String(u)));
|
|
51
|
+
export default async function to_request(request, contentType) {
|
|
52
|
+
const body = await bridge_body(request.body, contentType);
|
|
92
53
|
return {
|
|
93
|
-
url,
|
|
54
|
+
url: request.url,
|
|
94
55
|
body,
|
|
95
|
-
path
|
|
56
|
+
path: request.path.toJSON(),
|
|
57
|
+
query: request.query.toJSON(),
|
|
58
|
+
headers: request.headers.toJSON(),
|
|
59
|
+
cookies: request.cookies.toJSON(),
|
|
96
60
|
};
|
|
97
61
|
}
|
|
98
62
|
//# sourceMappingURL=to-request.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ResponseLike } from "@primate/core";
|
|
2
2
|
import type { Dict } from "@rcompat/type";
|
|
3
3
|
import type { PyProxy } from "pyodide/ffi";
|
|
4
|
-
declare
|
|
5
|
-
export default
|
|
4
|
+
declare function to_response(args: Dict | PyProxy | undefined): ResponseLike;
|
|
5
|
+
export default to_response;
|
|
6
6
|
//# sourceMappingURL=to-response.d.ts.map
|
|
@@ -1,26 +1,33 @@
|
|
|
1
1
|
import HANDLER_PROPERTY from "#handler-property";
|
|
2
2
|
import unwrap from "#unwrap";
|
|
3
3
|
import response from "@primate/core/response";
|
|
4
|
+
import is from "@rcompat/is";
|
|
4
5
|
const { error, redirect, view } = response;
|
|
5
6
|
const handlers = { error, redirect, view };
|
|
6
|
-
const handle_handler = (handler,
|
|
7
|
+
const handle_handler = (handler, args) => {
|
|
7
8
|
if (handler === "view") {
|
|
8
|
-
const { name, options, props } =
|
|
9
|
+
const { name, options, props } = args;
|
|
9
10
|
return view(name, props, options);
|
|
10
11
|
}
|
|
11
12
|
if (handler === "redirect") {
|
|
12
|
-
const { location, status } =
|
|
13
|
+
const { location, status } = args;
|
|
13
14
|
return redirect(location, status);
|
|
14
15
|
}
|
|
15
|
-
const { options } =
|
|
16
|
+
const { options } = args;
|
|
16
17
|
return error(options);
|
|
17
18
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
function is_handler(x) {
|
|
20
|
+
return is.string(x) && Object.keys(handlers).includes(x);
|
|
21
|
+
}
|
|
22
|
+
function to_response(args) {
|
|
23
|
+
if (is.undefined(args))
|
|
24
|
+
return null;
|
|
25
|
+
const unwrapped = unwrap(args);
|
|
21
26
|
const handler = unwrapped[HANDLER_PROPERTY];
|
|
22
27
|
return is_handler(handler)
|
|
23
28
|
? handle_handler(handler, unwrapped)
|
|
24
29
|
: unwrapped;
|
|
25
|
-
}
|
|
30
|
+
}
|
|
31
|
+
;
|
|
32
|
+
export default to_response;
|
|
26
33
|
//# sourceMappingURL=to-response.js.map
|
package/lib/private/unwrap.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Dict } from "@rcompat/type";
|
|
2
1
|
import type { PyProxy } from "pyodide/ffi";
|
|
3
|
-
export default function unwrap(response: PyProxy):
|
|
2
|
+
export default function unwrap(response: PyProxy): {
|
|
3
|
+
[k: string]: unknown;
|
|
4
|
+
};
|
|
4
5
|
//# sourceMappingURL=unwrap.d.ts.map
|
package/lib/private/wrapper.d.ts
CHANGED
package/lib/private/wrapper.js
CHANGED
|
@@ -3,7 +3,6 @@ import helpers from "@primate/python/helpers";
|
|
|
3
3
|
import pyodide from "@primate/python/pyodide";
|
|
4
4
|
import to_request from "@primate/python/to-request";
|
|
5
5
|
import to_response from "@primate/python/to-response";
|
|
6
|
-
import route from "primate/route";
|
|
7
6
|
const messageCallback = () => { };
|
|
8
7
|
let _micropip = null;
|
|
9
8
|
const installed = new Set();
|
|
@@ -48,6 +47,16 @@ function wrap_i18n(i18n) {
|
|
|
48
47
|
set(locale) { i18n.locale.set(locale); },
|
|
49
48
|
};
|
|
50
49
|
}
|
|
50
|
+
async function get_entries(python, route_id) {
|
|
51
|
+
const result = python.runPython(`
|
|
52
|
+
[{"verb": verb, "content_type": entry["content_type"] or ""}
|
|
53
|
+
for verb, entry in Route.registry(${JSON.stringify(route_id)}).items()]
|
|
54
|
+
`).toJs({ dict_converter: Object.fromEntries });
|
|
55
|
+
return result.map((e) => ({
|
|
56
|
+
verb: String(e.verb),
|
|
57
|
+
content_type: String(e.content_type),
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
51
60
|
export default async function wrapper(source, packages, primate_run, id, context = {}) {
|
|
52
61
|
const python = await pyodide();
|
|
53
62
|
const micropip = await load_micropip(python);
|
|
@@ -62,24 +71,23 @@ export default async function wrapper(source, packages, primate_run, id, context
|
|
|
62
71
|
Route.scope(${route_id})
|
|
63
72
|
`);
|
|
64
73
|
await python.runPython(source);
|
|
65
|
-
const
|
|
66
|
-
from primate import Route
|
|
67
|
-
list(Route.registry(${route_id}).keys())
|
|
68
|
-
`).toJs();
|
|
74
|
+
const entries = await get_entries(python, id);
|
|
69
75
|
const wrapped_session = wrap_session(context.session);
|
|
70
76
|
const wrapped_i18n = wrap_i18n(context.i18n);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
return Object.fromEntries(entries.map(({ verb, content_type }) => [
|
|
78
|
+
verb.toLowerCase(),
|
|
79
|
+
async (request) => {
|
|
80
|
+
const jsReq = await to_request(request, content_type);
|
|
81
|
+
python.globals.set("js_req", jsReq);
|
|
74
82
|
python.globals.set("helpers_obj", helpers);
|
|
75
83
|
python.globals.set("session_obj", wrapped_session);
|
|
76
84
|
python.globals.set("i18n_obj", wrapped_i18n);
|
|
77
85
|
const verb_str = JSON.stringify(verb);
|
|
78
86
|
try {
|
|
79
87
|
const result = await python.runPythonAsync(`
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
from primate import Route
|
|
89
|
+
await Route.call_js(${route_id}, ${verb_str}, js_req, helpers_obj, session_obj, i18n_obj)
|
|
90
|
+
`);
|
|
83
91
|
return to_response(result);
|
|
84
92
|
}
|
|
85
93
|
catch (e) {
|
|
@@ -92,7 +100,7 @@ export default async function wrapper(source, packages, primate_run, id, context
|
|
|
92
100
|
python.globals.delete("session_obj");
|
|
93
101
|
python.globals.delete("i18n_obj");
|
|
94
102
|
}
|
|
95
|
-
}
|
|
96
|
-
|
|
103
|
+
},
|
|
104
|
+
]));
|
|
97
105
|
}
|
|
98
106
|
//# sourceMappingURL=wrapper.js.map
|
package/lib/public/default.d.ts
CHANGED
package/lib/public/default.js
CHANGED
package/lib/public/runtime.d.ts
CHANGED
package/lib/public/runtime.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@primate/python",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Python backend for Primate",
|
|
5
5
|
"homepage": "https://primate.run/docs/backend/python",
|
|
6
6
|
"bugs": "https://github.com/primate-run/primate/issues",
|
|
@@ -17,22 +17,24 @@
|
|
|
17
17
|
"!/**/*.spec.*"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@rcompat/assert": "^0.
|
|
21
|
-
"@rcompat/
|
|
22
|
-
"@rcompat/
|
|
20
|
+
"@rcompat/assert": "^0.13.0",
|
|
21
|
+
"@rcompat/error": "^0.8.0",
|
|
22
|
+
"@rcompat/fs": "^0.33.2",
|
|
23
|
+
"@rcompat/io": "^0.10.0",
|
|
24
|
+
"@rcompat/is": "^0.11.0",
|
|
23
25
|
"pyodide": "^0.29.3",
|
|
24
|
-
"@primate/core": "^0.
|
|
25
|
-
"pema": "^0.
|
|
26
|
+
"@primate/core": "^0.7.0",
|
|
27
|
+
"pema": "^0.7.0"
|
|
26
28
|
},
|
|
27
29
|
"devDependencies": {
|
|
28
|
-
"@rcompat/type": "^0.
|
|
30
|
+
"@rcompat/type": "^0.17.0"
|
|
29
31
|
},
|
|
30
32
|
"peerDependencies": {
|
|
31
|
-
"primate": "^0.
|
|
33
|
+
"primate": "^0.38.0"
|
|
32
34
|
},
|
|
33
35
|
"imports": {
|
|
34
36
|
"#*": {
|
|
35
|
-
"
|
|
37
|
+
"@primate/source": "./src/private/*.ts",
|
|
36
38
|
"default": "./lib/private/*.js"
|
|
37
39
|
}
|
|
38
40
|
},
|
|
@@ -42,7 +44,7 @@
|
|
|
42
44
|
"default": "./lib/public/default.js"
|
|
43
45
|
},
|
|
44
46
|
"./*": {
|
|
45
|
-
"
|
|
47
|
+
"@primate/source": "./src/public/*.ts",
|
|
46
48
|
"default": "./lib/public/*.js"
|
|
47
49
|
}
|
|
48
50
|
},
|
package/lib/private/Default.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import Runtime from "#Runtime";
|
|
2
|
-
import type BuildApp from "@primate/core/BuildApp";
|
|
3
|
-
import type NextBuild from "@primate/core/NextBuild";
|
|
4
|
-
export default class Default extends Runtime {
|
|
5
|
-
build(app: BuildApp, next: NextBuild): Promise<BuildApp>;
|
|
6
|
-
}
|
|
7
|
-
//# sourceMappingURL=Default.d.ts.map
|
package/lib/private/Default.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import Runtime from "#Runtime";
|
|
2
|
-
import TAG from "@primate/core/backend/TAG";
|
|
3
|
-
import fail from "@primate/core/fail";
|
|
4
|
-
import log from "@primate/core/log";
|
|
5
|
-
import assert from "@rcompat/assert";
|
|
6
|
-
import fs from "@rcompat/fs";
|
|
7
|
-
import io from "@rcompat/io";
|
|
8
|
-
const PACKAGE = "primate-run";
|
|
9
|
-
const [MAJOR, MINOR] = TAG.split(".").map(Number);
|
|
10
|
-
function package_not_found() {
|
|
11
|
-
return fail("package not found - run 'pip install {0}~={1}.0'", PACKAGE, TAG);
|
|
12
|
-
}
|
|
13
|
-
function pkg_mismatch(major, minor) {
|
|
14
|
-
return fail("installed {0} package version {1}.{2}.x not in range ~> {3}.x", PACKAGE, major, minor, TAG);
|
|
15
|
-
}
|
|
16
|
-
async function show_package() {
|
|
17
|
-
const str0 = () => "";
|
|
18
|
-
let out = await io.run(`pip show ${PACKAGE} 2>/dev/null`).catch(str0);
|
|
19
|
-
if (out.trim())
|
|
20
|
-
return out;
|
|
21
|
-
out = await io.run(`uv pip show ${PACKAGE} 2>/dev/null`).catch(str0);
|
|
22
|
-
if (out.trim())
|
|
23
|
-
return out;
|
|
24
|
-
out = await io.run(`python -m pip show ${PACKAGE} 2>/dev/null`).catch(str0);
|
|
25
|
-
if (out.trim())
|
|
26
|
-
return out;
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
async function check_version() {
|
|
30
|
-
const output = await show_package();
|
|
31
|
-
if (!output)
|
|
32
|
-
throw package_not_found();
|
|
33
|
-
const version_match = output.match(/Version:\s*(\d+)\.(\d+)\.(\d+)/);
|
|
34
|
-
if (!version_match)
|
|
35
|
-
throw package_not_found();
|
|
36
|
-
const [major, minor] = version_match.slice(1).map(Number);
|
|
37
|
-
if (major !== MAJOR || minor !== MINOR)
|
|
38
|
-
throw pkg_mismatch(major, minor);
|
|
39
|
-
log.info("using {0} package {1}.{2}.x", PACKAGE, major, minor);
|
|
40
|
-
}
|
|
41
|
-
export default class Default extends Runtime {
|
|
42
|
-
async build(app, next) {
|
|
43
|
-
await check_version();
|
|
44
|
-
const requirements_txt = app.root.join("requirements.txt");
|
|
45
|
-
let packages = [];
|
|
46
|
-
if (await requirements_txt.exists()) {
|
|
47
|
-
const requirements = await fs.text(requirements_txt);
|
|
48
|
-
packages = requirements
|
|
49
|
-
.split("\n")
|
|
50
|
-
.filter(line => line.trim() && !line.startsWith("#"))
|
|
51
|
-
.map(p => p.trim());
|
|
52
|
-
}
|
|
53
|
-
const packages_str = JSON.stringify(packages);
|
|
54
|
-
app.bind(this.fileExtension, async (file, { context }) => {
|
|
55
|
-
assert.true(context === "routes", "python: only route files are supported");
|
|
56
|
-
const relative = file.debase(app.path.routes).path.replace(/^\//, "");
|
|
57
|
-
const source = await file.text();
|
|
58
|
-
return `
|
|
59
|
-
import wrapper from "@primate/python/wrapper";
|
|
60
|
-
import i18n from "app:config:i18n";
|
|
61
|
-
import session from "app:config:session";
|
|
62
|
-
await wrapper(
|
|
63
|
-
${JSON.stringify(source)},
|
|
64
|
-
${packages_str},
|
|
65
|
-
"${PACKAGE}~=${TAG}.0",
|
|
66
|
-
"${relative}",
|
|
67
|
-
{ i18n, session }
|
|
68
|
-
);
|
|
69
|
-
`;
|
|
70
|
-
});
|
|
71
|
-
return next(app);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
//# sourceMappingURL=Default.js.map
|
package/lib/private/Runtime.d.ts
DELETED