@dxos/functions 0.1.52-main.1744ed2
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 +8 -0
- package/README.md +21 -0
- package/dist/lib/browser/index.mjs +97 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node/index.cjs +125 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/types/src/functions.test.d.ts +2 -0
- package/dist/types/src/functions.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/interface.d.ts +13 -0
- package/dist/types/src/interface.d.ts.map +1 -0
- package/dist/types/src/runtime/index.d.ts +7 -0
- package/dist/types/src/runtime/index.d.ts.map +1 -0
- package/package.json +34 -0
- package/src/functions.test.ts +7 -0
- package/src/index.ts +6 -0
- package/src/interface.ts +20 -0
- package/src/runtime/index.ts +94 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
Copyright (c) 2022 DXOS
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
5
|
+
|
|
6
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# @dxos/echo-db
|
|
2
|
+
|
|
3
|
+
ECHO database.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm i @dxos/echo-db
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## DXOS Resources
|
|
12
|
+
|
|
13
|
+
- [Website](https://dxos.org)
|
|
14
|
+
- [Developer Documentation](https://docs.dxos.org)
|
|
15
|
+
- Talk to us on [Discord](https://discord.gg/eXVfryv3sW)
|
|
16
|
+
|
|
17
|
+
## Contributions
|
|
18
|
+
|
|
19
|
+
Your ideas, issues, and code are most welcome. Please take a look at our [community code of conduct](https://github.com/dxos/dxos/blob/main/CODE_OF_CONDUCT.md), the [issue guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-issues), and the [PR contribution guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-prs).
|
|
20
|
+
|
|
21
|
+
License: [MIT](./LICENSE) Copyright 2022 © DXOS
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import "@dxos/node-std/globals"
|
|
2
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
3
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
4
|
+
}) : x)(function(x) {
|
|
5
|
+
if (typeof require !== "undefined")
|
|
6
|
+
return require.apply(this, arguments);
|
|
7
|
+
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
// packages/core/functions/src/runtime/index.ts
|
|
11
|
+
import express from "express";
|
|
12
|
+
import { readdir } from "@dxos/node-std/fs/promises";
|
|
13
|
+
import { extname, join } from "@dxos/node-std/path";
|
|
14
|
+
import { getPortPromise } from "portfinder";
|
|
15
|
+
import { log } from "@dxos/log";
|
|
16
|
+
var FUNCTION_EXTENSIONS = [
|
|
17
|
+
".js",
|
|
18
|
+
".ts"
|
|
19
|
+
];
|
|
20
|
+
var runFunctions = async (options) => {
|
|
21
|
+
const files = await readdir(options.functionsDirectory);
|
|
22
|
+
const functionHandlers = {};
|
|
23
|
+
for (const file of files) {
|
|
24
|
+
if (!FUNCTION_EXTENSIONS.some((ext) => extname(file) === ext)) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const module = __require(join(options.functionsDirectory, file));
|
|
29
|
+
const handler = module.default;
|
|
30
|
+
if (typeof handler !== "function") {
|
|
31
|
+
throw new Error(`Function ${file} does not export a default function`);
|
|
32
|
+
}
|
|
33
|
+
const functionName = file.slice(0, -extname(file).length);
|
|
34
|
+
functionHandlers[functionName] = handler;
|
|
35
|
+
} catch (e) {
|
|
36
|
+
console.error(e);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const port = await getPortPromise({
|
|
40
|
+
startPort: 7e3
|
|
41
|
+
});
|
|
42
|
+
const app = express();
|
|
43
|
+
app.use(express.json());
|
|
44
|
+
app.post("/:functionName", async (req, res) => {
|
|
45
|
+
const functionName = req.params.functionName;
|
|
46
|
+
const replyBuilder = {
|
|
47
|
+
status: (code) => {
|
|
48
|
+
res.statusCode = code;
|
|
49
|
+
return replyBuilder;
|
|
50
|
+
},
|
|
51
|
+
succeed: (result) => {
|
|
52
|
+
res.end(JSON.stringify(result));
|
|
53
|
+
return replyBuilder;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const context = {
|
|
57
|
+
client: options.client,
|
|
58
|
+
status: replyBuilder.status.bind(replyBuilder)
|
|
59
|
+
};
|
|
60
|
+
void (async () => {
|
|
61
|
+
try {
|
|
62
|
+
await functionHandlers[functionName](req.body, context);
|
|
63
|
+
} catch (err) {
|
|
64
|
+
res.statusCode = 500;
|
|
65
|
+
res.end(err.message);
|
|
66
|
+
}
|
|
67
|
+
})();
|
|
68
|
+
});
|
|
69
|
+
app.listen(port);
|
|
70
|
+
const functionNames = Object.keys(functionHandlers);
|
|
71
|
+
const { registrationId } = await options.client.services.services.FunctionRegistryService.register({
|
|
72
|
+
endpoint: `http://localhost:${port}`,
|
|
73
|
+
functions: functionNames.map((name) => ({
|
|
74
|
+
name
|
|
75
|
+
}))
|
|
76
|
+
});
|
|
77
|
+
process.on("SIGINT", async () => {
|
|
78
|
+
await options.client.services.services.FunctionRegistryService.unregister({
|
|
79
|
+
registrationId
|
|
80
|
+
});
|
|
81
|
+
process.exit();
|
|
82
|
+
});
|
|
83
|
+
log.info("functions runtime started", {
|
|
84
|
+
port,
|
|
85
|
+
functionNames,
|
|
86
|
+
registrationId
|
|
87
|
+
}, {
|
|
88
|
+
file: "index.ts",
|
|
89
|
+
line: 93,
|
|
90
|
+
scope: void 0,
|
|
91
|
+
callSite: (f, a) => f(...a)
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
export {
|
|
95
|
+
runFunctions
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/runtime/index.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport express from 'express';\nimport { readdir } from 'node:fs/promises';\nimport { extname, join } from 'node:path';\nimport { getPortPromise } from 'portfinder';\n\nimport { Client } from '@dxos/client';\nimport { log } from '@dxos/log';\n\nimport { FunctionContext, FunctionHandler, Reply } from '../interface';\n\nconst FUNCTION_EXTENSIONS = ['.js', '.ts'];\n\nexport type FunctionsRuntimeParams = {\n client: Client;\n functionsDirectory: string;\n};\n\nexport const runFunctions = async (options: FunctionsRuntimeParams) => {\n const files = await readdir(options.functionsDirectory);\n\n const functionHandlers: Record<string, FunctionHandler> = {};\n\n for (const file of files) {\n if (!FUNCTION_EXTENSIONS.some((ext) => extname(file) === ext)) {\n continue;\n }\n\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const module = require(join(options.functionsDirectory, file));\n const handler = module.default;\n if (typeof handler !== 'function') {\n throw new Error(`Function ${file} does not export a default function`);\n }\n\n const functionName = file.slice(0, -extname(file).length);\n\n functionHandlers[functionName] = handler;\n } catch (e) {\n console.error(e);\n }\n }\n\n const port = await getPortPromise({ startPort: 7000 });\n\n const app = express();\n app.use(express.json());\n\n app.post('/:functionName', async (req, res) => {\n const functionName = req.params.functionName;\n\n const replyBuilder: Reply = {\n status: (code: number) => {\n res.statusCode = code;\n return replyBuilder;\n },\n succeed: (result: any) => {\n res.end(JSON.stringify(result));\n return replyBuilder;\n },\n };\n const context: FunctionContext = {\n client: options.client,\n status: replyBuilder.status.bind(replyBuilder),\n };\n\n void (async () => {\n try {\n await functionHandlers[functionName](req.body, context);\n } catch (err: any) {\n res.statusCode = 500;\n res.end(err.message);\n }\n })();\n });\n app.listen(port);\n\n const functionNames = Object.keys(functionHandlers);\n const { registrationId } = await options.client.services.services.FunctionRegistryService!.register({\n endpoint: `http://localhost:${port}`,\n functions: functionNames.map((name) => ({ name })),\n });\n\n process.on('SIGINT', async () => {\n await options.client.services.services.FunctionRegistryService!.unregister({ registrationId });\n process.exit();\n });\n\n log.info('functions runtime started', { port, functionNames, registrationId });\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;AAIA,OAAOA,aAAa;AACpB,SAASC,eAAe;AACxB,SAASC,SAASC,YAAY;AAC9B,SAASC,sBAAsB;AAG/B,SAASC,WAAW;AAIpB,IAAMC,sBAAsB;EAAC;EAAO;;AAO7B,IAAMC,eAAe,OAAOC,YAAoC;AACrE,QAAMC,QAAQ,MAAMC,QAAQF,QAAQG,kBAAkB;AAEtD,QAAMC,mBAAoD,CAAC;AAE3D,a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ClC,QAAM,EAAE+C,eAAc,IAAK,MAAMnD,QAAQ0C,OAAOU,SAASA,SAASC,wBAAyBC,SAAS;IAClGC,UAAU,oBAAoBlC;IAC9BmC,WAAWR,cAAcS,IAAI,CAACC,UAAU;MAAEA;IAAK,EAAA;EACjD,CAAA;AAEAC,UAAQC,GAAG,UAAU,YAAY;AAC/B,UAAM5D,QAAQ0C,OAAOU,SAASA,SAASC,wBAAyBQ,WAAW;MAAEV;IAAe,CAAA;AAC5FQ,YAAQG,KAAI;EACd,CAAA;AAEAC,MAAIC,KAAK,6BAA6B;IAAE3C;IAAM2B;IAAeG;EAAe,GAAA;;;;;;AAC9E;",
|
|
6
|
+
"names": ["express", "readdir", "extname", "join", "getPortPromise", "log", "FUNCTION_EXTENSIONS", "runFunctions", "options", "files", "readdir", "functionsDirectory", "functionHandlers", "file", "some", "ext", "extname", "module", "require", "join", "handler", "default", "Error", "functionName", "slice", "length", "e", "console", "error", "port", "getPortPromise", "startPort", "app", "express", "use", "json", "post", "req", "res", "params", "replyBuilder", "status", "code", "statusCode", "succeed", "result", "end", "JSON", "stringify", "context", "client", "bind", "body", "err", "message", "listen", "functionNames", "Object", "keys", "registrationId", "services", "FunctionRegistryService", "register", "endpoint", "functions", "map", "name", "process", "on", "unregister", "exit", "log", "info"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/core/functions/src/interface.ts":{"bytes":756,"imports":[]},"packages/core/functions/src/runtime/index.ts":{"bytes":9991,"imports":[{"path":"express","kind":"import-statement","external":true},{"path":"@dxos/node-std/fs/promises","kind":"import-statement","external":true},{"path":"@dxos/node-std/path","kind":"import-statement","external":true},{"path":"portfinder","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"<runtime>","kind":"import-statement","external":true}]},"packages/core/functions/src/index.ts":{"bytes":464,"imports":[{"path":"packages/core/functions/src/interface.ts","kind":"import-statement","original":"./interface"},{"path":"packages/core/functions/src/runtime/index.ts","kind":"import-statement","original":"./runtime"}]}},"outputs":{"packages/core/functions/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5283},"packages/core/functions/dist/lib/browser/index.mjs":{"imports":[{"path":"express","kind":"import-statement","external":true},{"path":"@dxos/node-std/fs/promises","kind":"import-statement","external":true},{"path":"@dxos/node-std/path","kind":"import-statement","external":true},{"path":"portfinder","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true}],"exports":["runFunctions"],"entryPoint":"packages/core/functions/src/index.ts","inputs":{"packages/core/functions/src/index.ts":{"bytesInOutput":0},"packages/core/functions/src/runtime/index.ts":{"bytesInOutput":2358}},"bytes":2874}}}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// packages/core/functions/src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
runFunctions: () => runFunctions
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// packages/core/functions/src/runtime/index.ts
|
|
38
|
+
var import_express = __toESM(require("express"));
|
|
39
|
+
var import_promises = require("node:fs/promises");
|
|
40
|
+
var import_node_path = require("node:path");
|
|
41
|
+
var import_portfinder = require("portfinder");
|
|
42
|
+
var import_log = require("@dxos/log");
|
|
43
|
+
var FUNCTION_EXTENSIONS = [
|
|
44
|
+
".js",
|
|
45
|
+
".ts"
|
|
46
|
+
];
|
|
47
|
+
var runFunctions = async (options) => {
|
|
48
|
+
const files = await (0, import_promises.readdir)(options.functionsDirectory);
|
|
49
|
+
const functionHandlers = {};
|
|
50
|
+
for (const file of files) {
|
|
51
|
+
if (!FUNCTION_EXTENSIONS.some((ext) => (0, import_node_path.extname)(file) === ext)) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const module2 = require((0, import_node_path.join)(options.functionsDirectory, file));
|
|
56
|
+
const handler = module2.default;
|
|
57
|
+
if (typeof handler !== "function") {
|
|
58
|
+
throw new Error(`Function ${file} does not export a default function`);
|
|
59
|
+
}
|
|
60
|
+
const functionName = file.slice(0, -(0, import_node_path.extname)(file).length);
|
|
61
|
+
functionHandlers[functionName] = handler;
|
|
62
|
+
} catch (e) {
|
|
63
|
+
console.error(e);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const port = await (0, import_portfinder.getPortPromise)({
|
|
67
|
+
startPort: 7e3
|
|
68
|
+
});
|
|
69
|
+
const app = (0, import_express.default)();
|
|
70
|
+
app.use(import_express.default.json());
|
|
71
|
+
app.post("/:functionName", async (req, res) => {
|
|
72
|
+
const functionName = req.params.functionName;
|
|
73
|
+
const replyBuilder = {
|
|
74
|
+
status: (code) => {
|
|
75
|
+
res.statusCode = code;
|
|
76
|
+
return replyBuilder;
|
|
77
|
+
},
|
|
78
|
+
succeed: (result) => {
|
|
79
|
+
res.end(JSON.stringify(result));
|
|
80
|
+
return replyBuilder;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const context = {
|
|
84
|
+
client: options.client,
|
|
85
|
+
status: replyBuilder.status.bind(replyBuilder)
|
|
86
|
+
};
|
|
87
|
+
void (async () => {
|
|
88
|
+
try {
|
|
89
|
+
await functionHandlers[functionName](req.body, context);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
res.statusCode = 500;
|
|
92
|
+
res.end(err.message);
|
|
93
|
+
}
|
|
94
|
+
})();
|
|
95
|
+
});
|
|
96
|
+
app.listen(port);
|
|
97
|
+
const functionNames = Object.keys(functionHandlers);
|
|
98
|
+
const { registrationId } = await options.client.services.services.FunctionRegistryService.register({
|
|
99
|
+
endpoint: `http://localhost:${port}`,
|
|
100
|
+
functions: functionNames.map((name) => ({
|
|
101
|
+
name
|
|
102
|
+
}))
|
|
103
|
+
});
|
|
104
|
+
process.on("SIGINT", async () => {
|
|
105
|
+
await options.client.services.services.FunctionRegistryService.unregister({
|
|
106
|
+
registrationId
|
|
107
|
+
});
|
|
108
|
+
process.exit();
|
|
109
|
+
});
|
|
110
|
+
import_log.log.info("functions runtime started", {
|
|
111
|
+
port,
|
|
112
|
+
functionNames,
|
|
113
|
+
registrationId
|
|
114
|
+
}, {
|
|
115
|
+
file: "index.ts",
|
|
116
|
+
line: 93,
|
|
117
|
+
scope: void 0,
|
|
118
|
+
callSite: (f, a) => f(...a)
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
122
|
+
0 && (module.exports = {
|
|
123
|
+
runFunctions
|
|
124
|
+
});
|
|
125
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/runtime/index.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nexport * from './interface';\nexport * from './runtime';\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport express from 'express';\nimport { readdir } from 'node:fs/promises';\nimport { extname, join } from 'node:path';\nimport { getPortPromise } from 'portfinder';\n\nimport { Client } from '@dxos/client';\nimport { log } from '@dxos/log';\n\nimport { FunctionContext, FunctionHandler, Reply } from '../interface';\n\nconst FUNCTION_EXTENSIONS = ['.js', '.ts'];\n\nexport type FunctionsRuntimeParams = {\n client: Client;\n functionsDirectory: string;\n};\n\nexport const runFunctions = async (options: FunctionsRuntimeParams) => {\n const files = await readdir(options.functionsDirectory);\n\n const functionHandlers: Record<string, FunctionHandler> = {};\n\n for (const file of files) {\n if (!FUNCTION_EXTENSIONS.some((ext) => extname(file) === ext)) {\n continue;\n }\n\n try {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const module = require(join(options.functionsDirectory, file));\n const handler = module.default;\n if (typeof handler !== 'function') {\n throw new Error(`Function ${file} does not export a default function`);\n }\n\n const functionName = file.slice(0, -extname(file).length);\n\n functionHandlers[functionName] = handler;\n } catch (e) {\n console.error(e);\n }\n }\n\n const port = await getPortPromise({ startPort: 7000 });\n\n const app = express();\n app.use(express.json());\n\n app.post('/:functionName', async (req, res) => {\n const functionName = req.params.functionName;\n\n const replyBuilder: Reply = {\n status: (code: number) => {\n res.statusCode = code;\n return replyBuilder;\n },\n succeed: (result: any) => {\n res.end(JSON.stringify(result));\n return replyBuilder;\n },\n };\n const context: FunctionContext = {\n client: options.client,\n status: replyBuilder.status.bind(replyBuilder),\n };\n\n void (async () => {\n try {\n await functionHandlers[functionName](req.body, context);\n } catch (err: any) {\n res.statusCode = 500;\n res.end(err.message);\n }\n })();\n });\n app.listen(port);\n\n const functionNames = Object.keys(functionHandlers);\n const { registrationId } = await options.client.services.services.FunctionRegistryService!.register({\n endpoint: `http://localhost:${port}`,\n functions: functionNames.map((name) => ({ name })),\n });\n\n process.on('SIGINT', async () => {\n await options.client.services.services.FunctionRegistryService!.unregister({ registrationId });\n process.exit();\n });\n\n log.info('functions runtime started', { port, functionNames, registrationId });\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACIA,qBAAoB;AACpB,sBAAwB;AACxB,uBAA8B;AAC9B,wBAA+B;AAG/B,iBAAoB;AAIpB,IAAMA,sBAAsB;EAAC;EAAO;;AAO7B,IAAMC,eAAe,OAAOC,YAAoC;AACrE,QAAMC,QAAQ,UAAMC,yBAAQF,QAAQG,kBAAkB;AAEtD,QAAMC,mBAAoD,CAAC;AAE3D,aAAWC,QAAQJ,OAAO;AACxB,QAAI,CAACH,oBAAoBQ,KAAK,CAACC,YAAQC,0BAAQH,IAAAA,MAAUE,GAAAA,GAAM;AAC7D;IACF;AAEA,QAAI;AAEF,YAAME,UAASC,YAAQC,uBAAKX,QAAQG,oBAAoBE,IAAAA,CAAAA;AACxD,YAAMO,UAAUH,QAAOI;AACvB,UAAI,OAAOD,YAAY,YAAY;AACjC,cAAM,IAAIE,MAAM,YAAYT,yCAAyC;MACvE;AAEA,YAAMU,eAAeV,KAAKW,MAAM,GAAG,KAACR,0BAAQH,IAAAA,EAAMY,MAAM;AAExDb,uBAAiBW,YAAAA,IAAgBH;IACnC,SAASM,GAAP;AACAC,cAAQC,MAAMF,CAAAA;IAChB;EACF;AAEA,QAAMG,OAAO,UAAMC,kCAAe;IAAEC,WAAW;E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"some", "ext", "extname", "module", "require", "join", "handler", "default", "Error", "functionName", "slice", "length", "e", "console", "error", "port", "getPortPromise", "startPort", "app", "express", "use", "json", "post", "req", "res", "params", "replyBuilder", "status", "code", "statusCode", "succeed", "result", "end", "JSON", "stringify", "context", "client", "bind", "body", "err", "message", "listen", "functionNames", "Object", "keys", "registrationId", "services", "FunctionRegistryService", "register", "endpoint", "functions", "map", "name", "process", "on", "unregister", "exit", "log", "info"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/core/functions/src/interface.ts":{"bytes":756,"imports":[]},"packages/core/functions/src/runtime/index.ts":{"bytes":9991,"imports":[{"path":"express","kind":"import-statement","external":true},{"path":"node:fs/promises","kind":"import-statement","external":true},{"path":"node:path","kind":"import-statement","external":true},{"path":"portfinder","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true}]},"packages/core/functions/src/index.ts":{"bytes":464,"imports":[{"path":"packages/core/functions/src/interface.ts","kind":"import-statement","original":"./interface"},{"path":"packages/core/functions/src/runtime/index.ts","kind":"import-statement","original":"./runtime"}]}},"outputs":{"packages/core/functions/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5363},"packages/core/functions/dist/lib/node/index.cjs":{"imports":[{"path":"express","kind":"require-call","external":true},{"path":"node:fs/promises","kind":"require-call","external":true},{"path":"node:path","kind":"require-call","external":true},{"path":"portfinder","kind":"require-call","external":true},{"path":"@dxos/log","kind":"require-call","external":true}],"exports":[],"entryPoint":"packages/core/functions/src/index.ts","inputs":{"packages/core/functions/src/index.ts":{"bytesInOutput":129},"packages/core/functions/src/runtime/index.ts":{"bytesInOutput":2530}},"bytes":4286}}}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"functions.test.d.ts","sourceRoot":"","sources":["../../../src/functions.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Client } from '@dxos/client';
|
|
2
|
+
export interface FunctionContext {
|
|
3
|
+
client: Client;
|
|
4
|
+
status(code: number): Reply;
|
|
5
|
+
}
|
|
6
|
+
export interface Reply {
|
|
7
|
+
status(code: number): Reply;
|
|
8
|
+
succeed(data: any): Reply;
|
|
9
|
+
}
|
|
10
|
+
export interface FunctionHandler {
|
|
11
|
+
(event: any, context: FunctionContext): Promise<Reply>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../../src/interface.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IAEf,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC;CAC7B;AAED,MAAM,WAAW,KAAK;IACpB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC;IAC5B,OAAO,CAAC,IAAI,EAAE,GAAG,GAAG,KAAK,CAAC;CAC3B;AAED,MAAM,WAAW,eAAe;IAC9B,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC;CACxD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/runtime/index.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAOtC,MAAM,MAAM,sBAAsB,GAAG;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,kBAAkB,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,eAAO,MAAM,YAAY,YAAmB,sBAAsB,kBAwEjE,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/functions",
|
|
3
|
+
"version": "0.1.52-main.1744ed2",
|
|
4
|
+
"description": "Functions SDK.",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "info@dxos.org",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
12
|
+
"node": "./dist/lib/node/index.cjs",
|
|
13
|
+
"default": "./dist/lib/node/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"types": "dist/types/src/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"src"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"express": "4.17.1",
|
|
23
|
+
"portfinder": "^1.0.32",
|
|
24
|
+
"@dxos/client": "0.1.52-main.1744ed2",
|
|
25
|
+
"@dxos/log": "0.1.52-main.1744ed2",
|
|
26
|
+
"@dxos/node-std": "0.1.52-main.1744ed2"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/express": "^4.17.17"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/index.ts
ADDED
package/src/interface.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Client } from '@dxos/client';
|
|
6
|
+
|
|
7
|
+
export interface FunctionContext {
|
|
8
|
+
client: Client;
|
|
9
|
+
|
|
10
|
+
status(code: number): Reply;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Reply {
|
|
14
|
+
status(code: number): Reply;
|
|
15
|
+
succeed(data: any): Reply;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface FunctionHandler {
|
|
19
|
+
(event: any, context: FunctionContext): Promise<Reply>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import express from 'express';
|
|
6
|
+
import { readdir } from 'node:fs/promises';
|
|
7
|
+
import { extname, join } from 'node:path';
|
|
8
|
+
import { getPortPromise } from 'portfinder';
|
|
9
|
+
|
|
10
|
+
import { Client } from '@dxos/client';
|
|
11
|
+
import { log } from '@dxos/log';
|
|
12
|
+
|
|
13
|
+
import { FunctionContext, FunctionHandler, Reply } from '../interface';
|
|
14
|
+
|
|
15
|
+
const FUNCTION_EXTENSIONS = ['.js', '.ts'];
|
|
16
|
+
|
|
17
|
+
export type FunctionsRuntimeParams = {
|
|
18
|
+
client: Client;
|
|
19
|
+
functionsDirectory: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const runFunctions = async (options: FunctionsRuntimeParams) => {
|
|
23
|
+
const files = await readdir(options.functionsDirectory);
|
|
24
|
+
|
|
25
|
+
const functionHandlers: Record<string, FunctionHandler> = {};
|
|
26
|
+
|
|
27
|
+
for (const file of files) {
|
|
28
|
+
if (!FUNCTION_EXTENSIONS.some((ext) => extname(file) === ext)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
34
|
+
const module = require(join(options.functionsDirectory, file));
|
|
35
|
+
const handler = module.default;
|
|
36
|
+
if (typeof handler !== 'function') {
|
|
37
|
+
throw new Error(`Function ${file} does not export a default function`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const functionName = file.slice(0, -extname(file).length);
|
|
41
|
+
|
|
42
|
+
functionHandlers[functionName] = handler;
|
|
43
|
+
} catch (e) {
|
|
44
|
+
console.error(e);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const port = await getPortPromise({ startPort: 7000 });
|
|
49
|
+
|
|
50
|
+
const app = express();
|
|
51
|
+
app.use(express.json());
|
|
52
|
+
|
|
53
|
+
app.post('/:functionName', async (req, res) => {
|
|
54
|
+
const functionName = req.params.functionName;
|
|
55
|
+
|
|
56
|
+
const replyBuilder: Reply = {
|
|
57
|
+
status: (code: number) => {
|
|
58
|
+
res.statusCode = code;
|
|
59
|
+
return replyBuilder;
|
|
60
|
+
},
|
|
61
|
+
succeed: (result: any) => {
|
|
62
|
+
res.end(JSON.stringify(result));
|
|
63
|
+
return replyBuilder;
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
const context: FunctionContext = {
|
|
67
|
+
client: options.client,
|
|
68
|
+
status: replyBuilder.status.bind(replyBuilder),
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
void (async () => {
|
|
72
|
+
try {
|
|
73
|
+
await functionHandlers[functionName](req.body, context);
|
|
74
|
+
} catch (err: any) {
|
|
75
|
+
res.statusCode = 500;
|
|
76
|
+
res.end(err.message);
|
|
77
|
+
}
|
|
78
|
+
})();
|
|
79
|
+
});
|
|
80
|
+
app.listen(port);
|
|
81
|
+
|
|
82
|
+
const functionNames = Object.keys(functionHandlers);
|
|
83
|
+
const { registrationId } = await options.client.services.services.FunctionRegistryService!.register({
|
|
84
|
+
endpoint: `http://localhost:${port}`,
|
|
85
|
+
functions: functionNames.map((name) => ({ name })),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
process.on('SIGINT', async () => {
|
|
89
|
+
await options.client.services.services.FunctionRegistryService!.unregister({ registrationId });
|
|
90
|
+
process.exit();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
log.info('functions runtime started', { port, functionNames, registrationId });
|
|
94
|
+
};
|