@foxtware/mineral 0.1.5 → 0.1.7
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/.creds.yml.sample +4 -1
- package/.env.sample +2 -0
- package/.gcloudignore +1 -4
- package/README.md +6 -1
- package/_build_scripts/createNewFunction.js +248 -38
- package/api/shopify/shopifyDecodeSessionToken.js +206 -0
- package/api/slack/slack.constants.js +5 -0
- package/api/slack/slack.utils.js +46 -0
- package/api/slack/slackMessagePost.js +85 -0
- package/api/utils.js +2 -4
- package/bin/mineral.js +1 -1
- package/{.hosting.yml.sample → hosting/.hosting.yml.sample} +4 -7
- package/hosting/copyCredsToEnv.js +48 -0
- package/{_deploy_scripts → hosting}/deployFromHostingYml.js +16 -37
- package/hosting/generateHosted.js +87 -0
- package/{handlerPaths.js → hosting/handlerPaths.js} +1 -1
- package/{hosting.utils.js → hosting/hosting.utils.js} +47 -52
- package/{wrappers.js → hosting/wrappers.js} +2 -2
- package/package.json +2 -2
- package/server.js +3 -6
- package/server.utils.js +41 -7
- package/_build_scripts/copyCredsToEnv.js +0 -45
- package/_deploy_scripts/generateHosted.js +0 -58
- /package/{_deploy_scripts → hosting}/execCommand.js +0 -0
- /package/{_deploy_scripts → hosting}/setEnvVarsGcloud.js +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foxtware/mineral",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"bin": {
|
|
5
5
|
"mineral": "bin/mineral.js"
|
|
6
6
|
},
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"access": "public"
|
|
10
10
|
},
|
|
11
11
|
"scripts": {
|
|
12
|
-
"creds_to_env": "node
|
|
12
|
+
"creds_to_env": "node hosting/copyCredsToEnv.js",
|
|
13
13
|
"dev": "node --watch --watch-path=./api --watch-path=./server.js --watch-path=./server.utils.js --watch-path=./.creds.yml server.js",
|
|
14
14
|
"new": "node _build_scripts/createNewFunction.js",
|
|
15
15
|
"serve": "PORT=8100 node server.js",
|
package/server.js
CHANGED
|
@@ -3,8 +3,8 @@ const http = require('http');
|
|
|
3
3
|
const { respondJson, errorToReadable, getRequestBody, argsFromBody, funcApi, statusCodeFromResult } = require('./server.utils');
|
|
4
4
|
const { getWorkspace, setWorkspace, loadWorkspaceEnv, toAbsolutePath } = require('./api/workspace');
|
|
5
5
|
const { getApiDirs, readCliFlag } = require('./cli');
|
|
6
|
-
const { getFuncApiConfig } = require('./hosting.utils');
|
|
7
|
-
const { MINERAL_API_DIR
|
|
6
|
+
const { getFuncApiConfig } = require('./hosting/hosting.utils');
|
|
7
|
+
const { MINERAL_API_DIR } = require('./hosting/handlerPaths');
|
|
8
8
|
|
|
9
9
|
const getConfig = (options = {}) => ({
|
|
10
10
|
port: Number(options.port ?? process.env.PORT ?? 8000),
|
|
@@ -78,8 +78,6 @@ const directoriesToScan = ({
|
|
|
78
78
|
return [MINERAL_API_DIR, ...extraDirs];
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
-
const getFuncApiConfigFromModule = getFuncApiConfig;
|
|
82
|
-
|
|
83
81
|
const addHandlerFromFile = (filePath, handlers) => {
|
|
84
82
|
const moduleExports = require(filePath);
|
|
85
83
|
if (!moduleExports || typeof moduleExports !== 'object') {
|
|
@@ -92,7 +90,7 @@ const addHandlerFromFile = (filePath, handlers) => {
|
|
|
92
90
|
return;
|
|
93
91
|
}
|
|
94
92
|
|
|
95
|
-
const funcApiConfig =
|
|
93
|
+
const funcApiConfig = getFuncApiConfig({
|
|
96
94
|
moduleExports,
|
|
97
95
|
routeName,
|
|
98
96
|
});
|
|
@@ -219,7 +217,6 @@ const startServer = (options = {}) => {
|
|
|
219
217
|
module.exports = {
|
|
220
218
|
startServer,
|
|
221
219
|
loadHandlers,
|
|
222
|
-
getRequirePathForHandler,
|
|
223
220
|
MINERAL_API_DIR,
|
|
224
221
|
get server() {
|
|
225
222
|
if (!server) {
|
package/server.utils.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
const { logDeep } = require('./api/utils');
|
|
2
|
+
const { HOSTED } = require('./api/constants');
|
|
2
3
|
const { StringDecoder } = require('string_decoder');
|
|
3
4
|
|
|
4
5
|
const respondJson = (res, statusCode, payload) => {
|
|
5
|
-
logDeep(payload);
|
|
6
|
+
!HOSTED && logDeep(payload);
|
|
6
7
|
const body = JSON.stringify(payload);
|
|
7
8
|
res.writeHead(statusCode, {
|
|
8
9
|
'Content-Type': 'application/json',
|
|
@@ -120,15 +121,48 @@ const runRequestHandler = async (requestHandler, requestContext) => {
|
|
|
120
121
|
return mergeRequestContext(requestContext, handlerOutput);
|
|
121
122
|
};
|
|
122
123
|
|
|
123
|
-
const wrapFunction = (func,
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
124
|
+
const wrapFunction = (func, {
|
|
125
|
+
beforeWrappers = [],
|
|
126
|
+
afterWrappers = [],
|
|
127
|
+
} = {}) => async (req, res, ...rest) => {
|
|
128
|
+
for (const beforeWrapper of beforeWrappers) {
|
|
129
|
+
const beforeResult = await beforeWrapper(req, res);
|
|
130
|
+
if (beforeResult?.handled) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (beforeResult) {
|
|
134
|
+
return beforeResult;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (!afterWrappers.length) {
|
|
139
|
+
return func(req, res, ...rest);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let result;
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
result = await func(req, res, ...rest);
|
|
146
|
+
} catch (error) {
|
|
147
|
+
result = {
|
|
148
|
+
ok: false,
|
|
149
|
+
error: {
|
|
150
|
+
code: 'UNHANDLED_ERROR',
|
|
151
|
+
message: 'Unhandled server error.',
|
|
152
|
+
details: errorToReadable(error),
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
for (const afterWrapper of afterWrappers) {
|
|
158
|
+
try {
|
|
159
|
+
await afterWrapper(req, res, result);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
console.log('wrapFunction afterWrapper error', error);
|
|
128
162
|
}
|
|
129
163
|
}
|
|
130
164
|
|
|
131
|
-
return
|
|
165
|
+
return result;
|
|
132
166
|
};
|
|
133
167
|
|
|
134
168
|
const statusCodeFromResult = (result) => {
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const fs = require('fs').promises;
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const yaml = require('yaml');
|
|
4
|
-
|
|
5
|
-
const mineralRoot = path.join(__dirname, '..');
|
|
6
|
-
const credsYmlPath = path.join(mineralRoot, '.creds.yml');
|
|
7
|
-
const envPath = path.join(mineralRoot, '.env');
|
|
8
|
-
|
|
9
|
-
(async () => {
|
|
10
|
-
const credsText = await fs.readFile(credsYmlPath, 'utf8');
|
|
11
|
-
const credsFromYml = yaml.parse(credsText);
|
|
12
|
-
const credsJsonString = JSON.stringify(credsFromYml);
|
|
13
|
-
const newCredsLine = `CREDS=${ credsJsonString }`;
|
|
14
|
-
|
|
15
|
-
let envFileContents = '';
|
|
16
|
-
try {
|
|
17
|
-
envFileContents = await fs.readFile(envPath, 'utf8');
|
|
18
|
-
} catch (err) {
|
|
19
|
-
if (err.code !== 'ENOENT') {
|
|
20
|
-
throw err;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (!envFileContents) {
|
|
25
|
-
await fs.writeFile(envPath, `${ newCredsLine }\n`);
|
|
26
|
-
console.log('Created .env with CREDS');
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (/^CREDS=/m.test(envFileContents)) {
|
|
31
|
-
const updatedFileContents = envFileContents.replace(/^CREDS=.*$/m, newCredsLine);
|
|
32
|
-
|
|
33
|
-
if (updatedFileContents === envFileContents) {
|
|
34
|
-
console.log('CREDS already up to date');
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
await fs.writeFile(envPath, updatedFileContents);
|
|
39
|
-
console.log('Updated CREDS in .env');
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
await fs.appendFile(envPath, `\n\n${ newCredsLine }\n`);
|
|
44
|
-
console.log('Appended CREDS to .env');
|
|
45
|
-
})();
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const { wrapHostedFunction } = require('../hosting.utils');
|
|
3
|
-
|
|
4
|
-
const formatResolvedWrapperForHostedJs = ({ modulePath, wrapperName }) => (
|
|
5
|
-
`require('${ modulePath }').${ wrapperName }`
|
|
6
|
-
);
|
|
7
|
-
|
|
8
|
-
const formatWrappersArg = (resolvedWrappers = []) => {
|
|
9
|
-
if (!resolvedWrappers.length) {
|
|
10
|
-
return '';
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const wrapperLines = resolvedWrappers.map((resolvedWrapper) => (
|
|
14
|
-
` ${ formatResolvedWrapperForHostedJs(resolvedWrapper) },`
|
|
15
|
-
));
|
|
16
|
-
return `, [\n${ wrapperLines.join('\n') }\n ]`;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const generateHostedJs = ({
|
|
20
|
-
hostedHandlers,
|
|
21
|
-
}) => {
|
|
22
|
-
const exportLines = [];
|
|
23
|
-
|
|
24
|
-
for (const hostedHandler of hostedHandlers) {
|
|
25
|
-
const { hostedName, handlerName, resolvedWrappers = [], requirePath } = hostedHandler;
|
|
26
|
-
const wrappersArg = formatWrappersArg(resolvedWrappers);
|
|
27
|
-
|
|
28
|
-
exportLines.push(
|
|
29
|
-
` ${ hostedName }: wrapHostedFunction(() => require('${ requirePath }'), '${ handlerName }'${ wrappersArg }),`,
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return `// Generated by mineral — do not edit
|
|
34
|
-
const { wrapHostedFunction } = require('@foxtware/mineral/hosting.utils');
|
|
35
|
-
|
|
36
|
-
module.exports = {
|
|
37
|
-
${ exportLines.join('\n') }
|
|
38
|
-
};
|
|
39
|
-
`;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const writeHostedJs = ({
|
|
43
|
-
workspace,
|
|
44
|
-
hostedHandlers,
|
|
45
|
-
}) => {
|
|
46
|
-
const hostedPath = `${ workspace.replace(/\/$/, '') }/hosted.js`;
|
|
47
|
-
const content = generateHostedJs({
|
|
48
|
-
hostedHandlers,
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
fs.writeFileSync(hostedPath, content);
|
|
52
|
-
return hostedPath;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
module.exports = {
|
|
56
|
-
generateHostedJs,
|
|
57
|
-
writeHostedJs,
|
|
58
|
-
};
|
|
File without changes
|
|
File without changes
|