@basthon/kernel-javascript 0.0.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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { expose } from "@basthon/kernel-base/worker";
2
+ import { JavaScriptKernelWorker } from "./worker";
3
+ expose(JavaScriptKernelWorker);
package/lib/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./kernel";
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./kernel";
@@ -0,0 +1,11 @@
1
+ import { KernelMainBase } from "@basthon/kernel-base/worker";
2
+ import { JavaScriptKernelWorker } from "./worker";
3
+ export declare class JavaScriptKernel extends KernelMainBase<JavaScriptKernelWorker> {
4
+ constructor(options: any);
5
+ protected newWorker(): Worker;
6
+ language(): string;
7
+ languageName(): string;
8
+ moduleExts(): string[];
9
+ ps1(): string;
10
+ ps2(): string;
11
+ }
package/lib/kernel.js ADDED
@@ -0,0 +1,27 @@
1
+ import { KernelMainBase } from "@basthon/kernel-base/worker";
2
+ export class JavaScriptKernel extends KernelMainBase {
3
+ constructor(options) {
4
+ super(options);
5
+ }
6
+ newWorker() {
7
+ return new Worker(new URL("./comlink-worker.js", import.meta.url), {
8
+ //@ts-ignore
9
+ sandboxed: true, // removing this line will cause security issues
10
+ });
11
+ }
12
+ language() {
13
+ return "javascript";
14
+ }
15
+ languageName() {
16
+ return "Javascript";
17
+ }
18
+ moduleExts() {
19
+ return ["js"];
20
+ }
21
+ ps1() {
22
+ return " js> ";
23
+ }
24
+ ps2() {
25
+ return "...> ";
26
+ }
27
+ }
@@ -0,0 +1,11 @@
1
+ import { KernelWorkerBase } from "@basthon/kernel-base/worker";
2
+ export declare class JavaScriptKernelWorker extends KernelWorkerBase {
3
+ constructor(options?: any);
4
+ language(): string;
5
+ protected _init(): Promise<void>;
6
+ protected _eval(data: any, code: string): Promise<any>;
7
+ putFile(filename: string, content: ArrayBuffer): void;
8
+ putModule(filename: string, content: ArrayBuffer): Promise<void>;
9
+ complete(code: string): Promise<[string[], number] | []>;
10
+ more(code: string): Promise<boolean>;
11
+ }
package/lib/worker.js ADDED
@@ -0,0 +1,57 @@
1
+ import objectInspect from "object-inspect";
2
+ import { KernelWorkerBase } from "@basthon/kernel-base/worker";
3
+ export class JavaScriptKernelWorker extends KernelWorkerBase {
4
+ constructor(options) {
5
+ // do not forget to call the parent constructor
6
+ super(options);
7
+ }
8
+ language() {
9
+ return "javascript";
10
+ }
11
+ /*
12
+ * Initialize the kernel.
13
+ */
14
+ async _init() {
15
+ console.info = (...args) => console.log(...args);
16
+ console.warn = (...args) => console.error(...args);
17
+ globalThis.addEventListener("error", (e) => console.error(e.toString()));
18
+ }
19
+ async _eval(data, code) {
20
+ console.log = (...args) => {
21
+ this.sendStdoutStream(data, args.join(" ") + "\n");
22
+ };
23
+ console.error = (...args) => {
24
+ this.sendStderrStream(data, args.join(" ") + "\n");
25
+ };
26
+ const result = await globalThis.eval(code); // if a promise is returned, wait for it
27
+ return typeof result === "undefined"
28
+ ? undefined
29
+ : { "text/plain": objectInspect(result) };
30
+ }
31
+ putFile(filename, content) {
32
+ console.error(`File ${filename} not added since putFile has no mean in the JS context.`);
33
+ }
34
+ async putModule(filename, content) {
35
+ const ext = filename.split(".").pop();
36
+ switch (ext) {
37
+ case "js":
38
+ const decoder = new TextDecoder("utf-8");
39
+ const _content = decoder.decode(new Uint8Array(content));
40
+ await globalThis.eval(_content);
41
+ break;
42
+ default:
43
+ throw { message: "Only '.js' files supported." };
44
+ }
45
+ }
46
+ async complete(code) {
47
+ // basic completion based on global object
48
+ const vars = Object.getOwnPropertyNames(self);
49
+ const words = code.match(/(\w+)$/) ?? [];
50
+ const word = words[0] ?? "";
51
+ const matches = vars.filter((v) => v.startsWith(word));
52
+ return [matches, code.length - word.length];
53
+ }
54
+ async more(code) {
55
+ return false;
56
+ }
57
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@basthon/kernel-javascript",
3
+ "version": "0.0.0",
4
+ "description": "Basthon - JavaScript Kernel",
5
+ "homepage": "https://basthon.fr",
6
+ "bugs": {
7
+ "url": "https://forge.apps.education.fr/basthon/basthon-kernel/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://forge.apps.education.fr/basthon/basthon-kernel.git"
12
+ },
13
+ "license": "GPL-3.0-or-later",
14
+ "author": "Romain Casati",
15
+ "main": "lib/index.js",
16
+ "types": "lib/index.d.ts",
17
+ "directories": {
18
+ "lib": "lib/"
19
+ },
20
+ "files": [
21
+ "lib"
22
+ ],
23
+ "scripts": {
24
+ "build": "tsc",
25
+ "clean": "rimraf lib/"
26
+ },
27
+ "dependencies": {
28
+ "@basthon/kernel-base": "*",
29
+ "comlink": "^4.4.2",
30
+ "object-inspect": "^1.13.4"
31
+ },
32
+ "devDependencies": {
33
+ "@types/object-inspect": "^1.13.0",
34
+ "rimraf": "^6.0.1",
35
+ "typescript": "^5.9.2"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ }
40
+ }