Skip to content

Commit

Permalink
create @primate/ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
terrablue committed Aug 4, 2024
1 parent 439c88d commit fe56cb8
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 84 deletions.
19 changes: 0 additions & 19 deletions packages/binding/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"license": "MIT",
"files": [
"src/**/*.js",
"src/**/*.json",
"src/**/*.py",
"src/**/*.rb",
"!src/**/*.spec.js"
],
"repository": {
Expand All @@ -28,25 +25,9 @@
"@rcompat/stdio": "^0.4.0"
},
"peerDependencies": {
"@ruby/head-wasm-wasi": "2",
"@ruby/wasm-wasi": "2",
"primate": "workspace:^"
},
"peerDependenciesMeta": {
"@ruby/head-wasm-wasi": {
"optional": true
},
"@ruby/wasm-wasi": {
"optional": true
}
},
"type": "module",
"imports": {
"#*": {
"@primate/lt": "./src/private/*.js",
"default": "./src/private/*.js"
}
},
"exports": {
"./*/common": "./src/server/*/common.js",
"./*/to-request": "./src/server/*/to-request.js",
Expand Down
47 changes: 0 additions & 47 deletions packages/binding/src/server/ruby/common.js

This file was deleted.

7 changes: 0 additions & 7 deletions packages/binding/src/server/ruby/default.js

This file was deleted.

3 changes: 0 additions & 3 deletions packages/binding/src/server/ruby/runtime.js

This file was deleted.

44 changes: 44 additions & 0 deletions packages/ruby/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@primate/ruby",
"version": "0.1.0",
"description": "Primate Ruby module",
"homepage": "https://primatejs.com/modules/ruby",
"bugs": "https://github.com/primatejs/primate/issues",
"license": "MIT",
"files": [
"src/**/*.rb",
"src/**/*.js",
"!src/**/*.spec.js"
],
"repository": {
"type": "git",
"url": "https://github.com/primatejs/primate",
"directory": "packages/ruby"
},
"dependencies": {
"@primate/core": "workspace:^"
},
"peerDependencies": {
"@ruby/head-wasm-wasi": "2",
"@ruby/wasm-wasi": "2",
"primate": "workspace:^"
},
"type": "module",
"imports": {
"#*": {
"@primate/lt": "./src/private/*.js",
"default": "./src/private/*.js"
},
"#error/*": {
"@primate/lt": "./src/private/error/*.js",
"default": "./src/private/error/*.js"
}
},
"exports": {
".": {
"runtime": "./src/runtime.js",
"default": "./src/default.js"
},
"./*": "./src/*.js"
}
}
8 changes: 8 additions & 0 deletions packages/ruby/src/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import build from "#build";
import default_extension from "#extension";
import name from "#name";

export default ({ extension = default_extension, packages = [] } = {}) => ({
name,
build: build({ name, extension, packages }),
});
31 changes: 31 additions & 0 deletions packages/ruby/src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

export default {
wrap(value) {
if (typeof value === "number") {
if (Number.isInteger(value)) {
return "integer";
}
return "float";
}
if (typeof value === "boolean") {
return "boolean";
}
if (typeof value === "string") {
return "string";
}

if (typeof value === "object") {
if (Array.isArray(value)) {
return "array";
}

if (value === "null") {
return "nil";
}

return "object";
}

return undefined;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const routes_re = /def (?<route>get|post|put|delete)/gu;
const get_routes = code => [...code.matchAll(routes_re)]
.map(({ groups: { route } }) => route);

const directory = file(import.meta.url).up(1);
const session_rb = await directory.join("session.rb").text();
const request = await directory.join("./request.rb").text();
const this_directory = file(import.meta.url).up(1);
const session_rb = await this_directory.join("session.rb").text();
const request = await this_directory.join("./request.rb").text();
const make_route = route => `async ${route.toLowerCase()}(request) {
return to_response(await environment.callAsync("run_${route}",
vm.wrap(request), vm.wrap(helpers)));
Expand Down Expand Up @@ -48,11 +48,13 @@ end`);
}

return `
import to_response from "@primate/binding/ruby/to-response";
import { module, rubyvm, helpers } from "@primate/binding/ruby/common";
import to_response from "@primate/ruby/to-response";
import helpers from "@primate/ruby/helpers";
import { DefaultRubyVM } from "@ruby/wasm-wasi/dist/node";
import ruby from "@primate/ruby/ruby";
import file from "@rcompat/fs/file";
const { vm } = await rubyvm(module);
const { vm } = await DefaultRubyVM(ruby);
const code = await file(${JSON.stringify(path)}).text();
const wrappers = ${JSON.stringify(create_ruby_wrappers(routes))};
const request = ${JSON.stringify(request
Expand All @@ -69,8 +71,8 @@ export default {
};

export default ({ extension } = {}) => (app, next) => {
app.bind(extension, async (directory, file) => {
const path = directory.join(file);
app.bind(extension, async (directory, route) => {
const path = directory.join(route);
const base = path.directory;
const js = path.base.concat(".js");
const code = await path.text();
Expand Down
1 change: 1 addition & 0 deletions packages/ruby/src/private/extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ".rb";
1 change: 1 addition & 0 deletions packages/ruby/src/private/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "@primate/ruby";
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions packages/ruby/src/ruby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import file from "@rcompat/fs/file";
import root from "@rcompat/package/root";

const ruby_path = (await root())
.join("node_modules/@ruby/head-wasm-wasi/dist/ruby+stdlib.wasm");
const ruby_wasm = await file(ruby_path).arrayBuffer();

export default await WebAssembly.compile(ruby_wasm);
3 changes: 3 additions & 0 deletions packages/ruby/src/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import name from "#name";

export default () => ({ name });
File renamed without changes.
3 changes: 3 additions & 0 deletions packages/ruby/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.base.json"
}

0 comments on commit fe56cb8

Please sign in to comment.