Skip to content

Commit

Permalink
lifting works!
Browse files Browse the repository at this point in the history
  • Loading branch information
eladb committed Jan 24, 2024
1 parent 2645311 commit 24e65b5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
8 changes: 6 additions & 2 deletions typescript/example/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Handler } from "./index.wing";

export const handler: Handler = async ({ myBucket, myQueue }, event: any) => {
// await myBucket.put("hello.txt", "world");
// await myQueue.push("hello");
await myBucket.put("hello.txt", "from typescript");
await myQueue.push("hello");

console.log("Welcome to TypeScript");
console.log("event:", event);

const output = await myBucket.list();
console.log(output);

return {
status: 200,
body: JSON.stringify({
Expand Down
17 changes: 13 additions & 4 deletions typescript/inflight.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ bring "./inflight.w" as typescript;
let myBucket = new cloud.Bucket();
let myQueue = new cloud.Queue();

new cloud.Function(inflight () => {
myBucket.put("bing.txt", "123");
}) as "write";

let handler = new typescript.Inflight(
src: "./example/index",
lift: {
myBucket: myBucket,
myQueue: myQueue
myBucket: { obj: myBucket, ops: ["put"] },
myQueue: { obj: myQueue, ops: ["push"] },
}
);

let fn = new cloud.Function(handler.forFunction());
myQueue.setConsumer(inflight (msg) => {
log("queue received {msg}");
});

myQueue.setConsumer(handler.forQueueConsumer());
let fn = new cloud.Function(handler.forFunction()) as "typescript function";

let api = new cloud.Api();
api.get("/foo", handler.forApiEndpoint());
Expand Down Expand Up @@ -50,4 +56,7 @@ test "function" {
expect.equal(body.get("event").asStr(), "18993487");
expect.equal(body.get("message").asStr(), "Hello, TypeScript");

// expect the bucket to have an object
expect.equal(myBucket.get("hello.txt"), "from typescript");

}
20 changes: 14 additions & 6 deletions typescript/inflight.w
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
bring cloud;
bring fs;

pub struct Lift {
obj: std.IResource;
ops: Array<str>;
}

pub struct InflightProps {
src: str;
lift: Map<std.IResource>?;
lift: Map<Lift>?;
}


pub class Inflight {
code: str;
lift: Map<Lift>;

new(props: InflightProps) {
let var src = props.src;
let lift = props?.lift ?? {};

let postfix = "/index";
if !src.endsWith(postfix) {
Expand All @@ -20,6 +26,8 @@ pub class Inflight {

src = src.substring(0, src.length - postfix.length);

this.lift = props?.lift ?? {};

let tmpdir = fs.mkdtemp();
let bundle = Util.createBundle(src, [], tmpdir);
this.code = fs.readFile(bundle.entrypointPath);
Expand All @@ -29,21 +37,21 @@ pub class Inflight {

pub forFunction(): cloud.IFunctionHandler {
let h: cloud.IFunctionHandler = inflight (event: str): str? => { };
Util.patchToInflight(h, this.code);
Util.patchToInflight(h, this.lift, this.code);
return h;
}

pub forQueueConsumer(): cloud.IQueueSetConsumerHandler {
let h: cloud.IQueueSetConsumerHandler = inflight (event: str): str? => { };
Util.patchToInflight(h, this.code);
Util.patchToInflight(h, this.lift, this.code);
return h;
}

pub forApiEndpoint(): cloud.IApiEndpointHandler {
let h: cloud.IApiEndpointHandler = inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
return { status: 500 };
};
Util.patchToInflight(h, this.code);
Util.patchToInflight(h, this.lift, this.code);
return h;
}
}
Expand All @@ -58,7 +66,7 @@ struct Bundle {

class Util {
extern "./util.js"
pub static patchToInflight(h: std.IInflight, code: str): void;
pub static patchToInflight(h: std.IInflight, lift: Map<Lift>, code: str): void;

extern "./util.js"
pub static createBundle(entrypoint: str, external: Array<str>?, outputDir: str?): Bundle;
Expand Down
24 changes: 22 additions & 2 deletions typescript/util.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
const core = require('@winglang/sdk').core;

exports.createBundle = require('@winglang/sdk/lib/shared/bundling').createBundle;

exports.patchToInflight = (handler, code) => {
exports.patchToInflight = (handler, lifts, code) => {
const lifted = {};
const permissions = [];

for (const [name, lift] of Object.entries(lifts)) {
lifted[name] = core.liftObject(lift.obj);
permissions.push([lift.obj, lift.ops]);
}

const handleMethod = "handle";

handler.onLift = (host, ops) => {
core.onLiftMatrix(host, ops, {
[handleMethod]: permissions,
});
};

handler._toInflight = () => `
(function() {
const m = { exports: { } };
const x = (module, exports) => {
${code}
};
x(m, m.exports);
const lifted = {};
${Object.entries(lifted).map(([name, value]) => `lifted["${name}"] = ${value};`).join("\n")}
return {
handle: (...args) => m.exports.handler({}, ...args),
${handleMethod}: (...args) => m.exports.handler(lifted, ...args),
};
})()
`;
Expand Down

0 comments on commit 24e65b5

Please sign in to comment.