spiceflow 0.0.6 → 1.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.
- package/README.md +1 -171
- package/dist/client/errors.d.ts +7 -0
- package/dist/client/errors.d.ts.map +1 -0
- package/dist/client/errors.js +18 -0
- package/dist/client/errors.js.map +1 -0
- package/dist/client/index.d.ts +14 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +376 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +87 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/client/utils.d.ts +2 -0
- package/dist/client/utils.d.ts.map +1 -0
- package/dist/client/utils.js +9 -0
- package/dist/client/utils.js.map +1 -0
- package/dist/client/ws.d.ts +15 -0
- package/dist/client/ws.d.ts.map +1 -0
- package/dist/client/ws.js +51 -0
- package/dist/client/ws.js.map +1 -0
- package/dist/client.test.d.ts +2 -0
- package/dist/client.test.d.ts.map +1 -0
- package/dist/client.test.js +237 -0
- package/dist/client.test.js.map +1 -0
- package/dist/elysia-fork/context.d.ts +87 -0
- package/dist/elysia-fork/context.d.ts.map +1 -0
- package/dist/elysia-fork/context.js +2 -0
- package/dist/elysia-fork/context.js.map +1 -0
- package/dist/elysia-fork/error.d.ts +246 -0
- package/dist/elysia-fork/error.d.ts.map +1 -0
- package/dist/elysia-fork/error.js +195 -0
- package/dist/elysia-fork/error.js.map +1 -0
- package/dist/elysia-fork/types.d.ts +652 -0
- package/dist/elysia-fork/types.d.ts.map +1 -0
- package/dist/elysia-fork/types.js +3 -0
- package/dist/elysia-fork/types.js.map +1 -0
- package/dist/elysia-fork/utils.d.ts +134 -0
- package/dist/elysia-fork/utils.d.ts.map +1 -0
- package/dist/elysia-fork/utils.js +70 -0
- package/dist/elysia-fork/utils.js.map +1 -0
- package/dist/spiceflow.d.ts +253 -0
- package/dist/spiceflow.d.ts.map +1 -0
- package/dist/spiceflow.js +500 -0
- package/dist/spiceflow.js.map +1 -0
- package/dist/spiceflow.test.d.ts +2 -0
- package/dist/spiceflow.test.d.ts.map +1 -0
- package/dist/spiceflow.test.js +225 -0
- package/dist/spiceflow.test.js.map +1 -0
- package/dist/stream.test.d.ts +2 -0
- package/dist/stream.test.d.ts.map +1 -0
- package/dist/stream.test.js +286 -0
- package/dist/stream.test.js.map +1 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +4 -20
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +17 -46
- package/dist/utils.js.map +1 -1
- package/package.json +12 -36
- package/src/client/errors.ts +21 -0
- package/src/client/index.ts +539 -0
- package/src/client/types.ts +233 -0
- package/src/client/utils.ts +7 -0
- package/src/client/ws.ts +99 -0
- package/src/client.test.ts +235 -0
- package/src/elysia-fork/context.ts +196 -0
- package/src/elysia-fork/error.ts +293 -0
- package/src/elysia-fork/types.ts +1454 -0
- package/src/elysia-fork/utils.ts +85 -0
- package/src/spiceflow.test.ts +290 -0
- package/src/spiceflow.ts +1266 -0
- package/src/stream.test.ts +342 -0
- package/src/types.ts +0 -0
- package/src/utils.ts +21 -70
- package/context.d.ts +0 -2
- package/context.js +0 -1
- package/dist/babel.test.d.ts +0 -2
- package/dist/babel.test.d.ts.map +0 -1
- package/dist/babel.test.js +0 -27
- package/dist/babel.test.js.map +0 -1
- package/dist/babelDebugOutputs.d.ts +0 -9
- package/dist/babelDebugOutputs.d.ts.map +0 -1
- package/dist/babelDebugOutputs.js +0 -34
- package/dist/babelDebugOutputs.js.map +0 -1
- package/dist/babelTransformRpc.d.ts +0 -19
- package/dist/babelTransformRpc.d.ts.map +0 -1
- package/dist/babelTransformRpc.js +0 -285
- package/dist/babelTransformRpc.js.map +0 -1
- package/dist/browser.d.ts +0 -8
- package/dist/browser.d.ts.map +0 -1
- package/dist/browser.js +0 -126
- package/dist/browser.js.map +0 -1
- package/dist/build.d.ts +0 -13
- package/dist/build.d.ts.map +0 -1
- package/dist/build.js +0 -230
- package/dist/build.js.map +0 -1
- package/dist/cli.d.ts +0 -3
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js +0 -111
- package/dist/cli.js.map +0 -1
- package/dist/context-internal.d.ts +0 -20
- package/dist/context-internal.d.ts.map +0 -1
- package/dist/context-internal.js +0 -16
- package/dist/context-internal.js.map +0 -1
- package/dist/context.d.ts +0 -2
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js +0 -2
- package/dist/context.js.map +0 -1
- package/dist/expose.d.ts +0 -6
- package/dist/expose.d.ts.map +0 -1
- package/dist/expose.js +0 -32
- package/dist/expose.js.map +0 -1
- package/dist/headers.d.ts +0 -2
- package/dist/headers.d.ts.map +0 -1
- package/dist/headers.js +0 -18
- package/dist/headers.js.map +0 -1
- package/dist/index.d.ts +0 -8
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -23
- package/dist/index.js.map +0 -1
- package/dist/jsonRpc.d.ts +0 -32
- package/dist/jsonRpc.d.ts.map +0 -1
- package/dist/jsonRpc.js +0 -3
- package/dist/jsonRpc.js.map +0 -1
- package/dist/server.d.ts +0 -32
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js +0 -292
- package/dist/server.js.map +0 -1
- package/headers.d.ts +0 -2
- package/headers.js +0 -1
- package/sdk-template/package.json +0 -22
- package/sdk-template/src/index.ts +0 -2
- package/sdk-template/src/v1/example.ts +0 -5
- package/sdk-template/src/v1/generator.ts +0 -12
- package/sdk-template/tsconfig.json +0 -16
- package/src/babel.test.ts +0 -35
- package/src/babelDebugOutputs.ts +0 -56
- package/src/babelTransformRpc.ts +0 -394
- package/src/browser.ts +0 -141
- package/src/build.ts +0 -298
- package/src/cli.ts +0 -132
- package/src/context-internal.ts +0 -36
- package/src/context.ts +0 -5
- package/src/expose.ts +0 -34
- package/src/headers.ts +0 -19
- package/src/index.ts +0 -34
- package/src/jsonRpc.ts +0 -43
- package/src/server.ts +0 -384
package/src/build.ts
DELETED
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
import pico from 'picocolors';
|
|
2
|
-
import fsx from 'fs-extra';
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
ConsoleMessageId,
|
|
6
|
-
Extractor,
|
|
7
|
-
ExtractorConfig,
|
|
8
|
-
ExtractorLogLevel,
|
|
9
|
-
} from '@microsoft/api-extractor';
|
|
10
|
-
import chokidar from 'chokidar';
|
|
11
|
-
import { getPackages } from '@manypkg/get-packages';
|
|
12
|
-
|
|
13
|
-
import { transform } from '@babel/core';
|
|
14
|
-
import { exec } from 'child_process';
|
|
15
|
-
import globSync from 'fast-glob';
|
|
16
|
-
import fs from 'fs';
|
|
17
|
-
import path from 'path';
|
|
18
|
-
import { plugins } from './index.js';
|
|
19
|
-
import { camelCaseCapitalized, directive, removeExtension } from './utils.js';
|
|
20
|
-
import { WrapMethodMeta } from './server.js';
|
|
21
|
-
|
|
22
|
-
type BuildOptions = {
|
|
23
|
-
openapi?: boolean;
|
|
24
|
-
rootDir: string;
|
|
25
|
-
url: string;
|
|
26
|
-
watch?: boolean;
|
|
27
|
-
};
|
|
28
|
-
export async function buildOnce({
|
|
29
|
-
openapi = false,
|
|
30
|
-
rootDir,
|
|
31
|
-
url,
|
|
32
|
-
}: BuildOptions) {
|
|
33
|
-
console.log();
|
|
34
|
-
console.log('building functions');
|
|
35
|
-
if (url && !url.endsWith('/')) {
|
|
36
|
-
// make sure that new URL uses the last portion of the path too
|
|
37
|
-
url += '/';
|
|
38
|
-
}
|
|
39
|
-
try {
|
|
40
|
-
new URL(url);
|
|
41
|
-
} catch (e) {
|
|
42
|
-
throw new Error(`Invalid url ${url}`);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
let browserOutDir = path.resolve('client');
|
|
46
|
-
fs.mkdirSync(browserOutDir, { recursive: true });
|
|
47
|
-
|
|
48
|
-
// await fs.promises.rm(browserOutDir, { recursive: true }).catch(() => null);
|
|
49
|
-
|
|
50
|
-
const cwd = process.cwd();
|
|
51
|
-
const serverEntrypoint = path.resolve(rootDir, 'server.ts');
|
|
52
|
-
|
|
53
|
-
try {
|
|
54
|
-
const globBase = path.relative(cwd, rootDir);
|
|
55
|
-
const globs = [path.posix.join(globBase, '**/*.{ts,tsx,js,jsx}')];
|
|
56
|
-
// console.log({ globs });
|
|
57
|
-
const allPossibleFiles = globSync.globSync(globs, {
|
|
58
|
-
onlyFiles: true,
|
|
59
|
-
absolute: true,
|
|
60
|
-
});
|
|
61
|
-
const actionFilesRelativePaths = allPossibleFiles
|
|
62
|
-
.filter((file) => {
|
|
63
|
-
const content = fs.readFileSync(file, 'utf8');
|
|
64
|
-
return content.includes(directive);
|
|
65
|
-
})
|
|
66
|
-
.map((x) => {
|
|
67
|
-
return path.relative(rootDir, x);
|
|
68
|
-
});
|
|
69
|
-
const importsCode = [...new Set(actionFilesRelativePaths)]
|
|
70
|
-
.map((filePath) => {
|
|
71
|
-
filePath = removeExtension(filePath);
|
|
72
|
-
return `${JSON.stringify(
|
|
73
|
-
'/' + filePath,
|
|
74
|
-
)}: () => import('./${filePath}.js')`;
|
|
75
|
-
})
|
|
76
|
-
.join(',');
|
|
77
|
-
const serverExposeContent =
|
|
78
|
-
`// this file was generated\n` +
|
|
79
|
-
`import { internalEdgeHandler, internalNodeJsHandler } from 'spiceflow/dist/server.js';\n` +
|
|
80
|
-
`export const methodsMap = {${importsCode}} as any\n` +
|
|
81
|
-
`export const edgeHandler = internalEdgeHandler({ methodsMap });\n` +
|
|
82
|
-
`export const nodeJsHandler = internalNodeJsHandler({ methodsMap });\n`;
|
|
83
|
-
|
|
84
|
-
if (!actionFilesRelativePaths.length) {
|
|
85
|
-
throw new Error('No functions files found!');
|
|
86
|
-
}
|
|
87
|
-
fs.writeFileSync(serverEntrypoint, serverExposeContent, 'utf8');
|
|
88
|
-
|
|
89
|
-
if (fs.existsSync('src/index.ts')) {
|
|
90
|
-
fs.copyFileSync('src/index.ts', 'client/index.ts');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
for (let actionFile of actionFilesRelativePaths) {
|
|
94
|
-
const abs = path.resolve(rootDir, actionFile);
|
|
95
|
-
const content = fs.readFileSync(abs, 'utf8');
|
|
96
|
-
|
|
97
|
-
const actionName = path.basename(actionFile, path.extname(actionFile));
|
|
98
|
-
|
|
99
|
-
let methods = [] as WrapMethodMeta[];
|
|
100
|
-
const res = transform(content || '', {
|
|
101
|
-
babelrc: false,
|
|
102
|
-
sourceType: 'module',
|
|
103
|
-
plugins: [
|
|
104
|
-
...plugins({
|
|
105
|
-
isServer: false,
|
|
106
|
-
url,
|
|
107
|
-
onMethod(meta) {
|
|
108
|
-
methods.push(meta);
|
|
109
|
-
},
|
|
110
|
-
rootDir,
|
|
111
|
-
}),
|
|
112
|
-
],
|
|
113
|
-
filename: abs,
|
|
114
|
-
|
|
115
|
-
sourceMaps: false,
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
if (!res || !res.code) {
|
|
119
|
-
console.error(
|
|
120
|
-
`Error transforming ${actionFile}, returned nothing, maybe not an action?`,
|
|
121
|
-
);
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
const importPath =
|
|
125
|
-
'./' +
|
|
126
|
-
path.posix.join(path.posix.dirname(actionFile), actionName + '.ts');
|
|
127
|
-
const outFile = path.resolve(browserOutDir, importPath);
|
|
128
|
-
|
|
129
|
-
console.log(`processed ${importPath}`);
|
|
130
|
-
|
|
131
|
-
fs.mkdirSync(path.dirname(outFile), {
|
|
132
|
-
recursive: true,
|
|
133
|
-
});
|
|
134
|
-
fs.writeFileSync(outFile, res.code, 'utf-8');
|
|
135
|
-
}
|
|
136
|
-
} finally {
|
|
137
|
-
// await fs.promises.unlink(serverEntrypoint).catch(() => null);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export async function bundleTypes({ rootDir }) {
|
|
142
|
-
const browserIndexFile = path.resolve('dist/client/index.d.ts');
|
|
143
|
-
|
|
144
|
-
if (!fs.existsSync(browserIndexFile)) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const bundledPackages = (await getPackages(process.cwd())).packages.map(
|
|
149
|
-
(x) => x.packageJson.name,
|
|
150
|
-
);
|
|
151
|
-
if (!bundledPackages.length) {
|
|
152
|
-
console.log('no workspace packages found, skipping types bundling');
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
rollupDtsFile({
|
|
157
|
-
bundledPackages,
|
|
158
|
-
inputFilePath: browserIndexFile,
|
|
159
|
-
outputFilePath: browserIndexFile,
|
|
160
|
-
tsconfigFilePath: 'tsconfig.json',
|
|
161
|
-
});
|
|
162
|
-
console.log(`types bundled in ${browserIndexFile}`);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const logger = console;
|
|
166
|
-
|
|
167
|
-
let isBuilding = { ref: false };
|
|
168
|
-
let missedWatch = { ref: false };
|
|
169
|
-
|
|
170
|
-
export async function build(options: BuildOptions) {
|
|
171
|
-
await buildOnce(options);
|
|
172
|
-
if (!options.watch) {
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
const { rootDir, url } = options;
|
|
176
|
-
const watcher = chokidar.watch(rootDir, {
|
|
177
|
-
// ignored: /(^|[\/\\])\../, // ignore dotfiles
|
|
178
|
-
ignored: [
|
|
179
|
-
'**/node_modules/**',
|
|
180
|
-
'**/dist/**',
|
|
181
|
-
path.resolve(`src/server.ts`),
|
|
182
|
-
'client/**',
|
|
183
|
-
],
|
|
184
|
-
persistent: true,
|
|
185
|
-
});
|
|
186
|
-
console.log('watching for changes');
|
|
187
|
-
watcher.on('change', async (path, stats) => {
|
|
188
|
-
if (isBuilding.ref) {
|
|
189
|
-
missedWatch.ref = true;
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
isBuilding.ref = true;
|
|
193
|
-
try {
|
|
194
|
-
logger.log(`detected change in ${path}`);
|
|
195
|
-
await buildOnce(options);
|
|
196
|
-
if (missedWatch.ref) {
|
|
197
|
-
// logger.log('missed a change, rebuilding');
|
|
198
|
-
await buildOnce(options);
|
|
199
|
-
missedWatch.ref = false;
|
|
200
|
-
}
|
|
201
|
-
} finally {
|
|
202
|
-
isBuilding.ref = false;
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function rollupDtsFile({
|
|
208
|
-
inputFilePath,
|
|
209
|
-
outputFilePath,
|
|
210
|
-
tsconfigFilePath,
|
|
211
|
-
bundledPackages,
|
|
212
|
-
}: {
|
|
213
|
-
inputFilePath: string;
|
|
214
|
-
outputFilePath: string;
|
|
215
|
-
tsconfigFilePath: string;
|
|
216
|
-
bundledPackages: string[];
|
|
217
|
-
}) {
|
|
218
|
-
let cwd = process.cwd();
|
|
219
|
-
if (!fs.existsSync(tsconfigFilePath)) {
|
|
220
|
-
throw new Error(`tsconfig.json not found at ${tsconfigFilePath}`);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
let packageJsonFullPath = path.join(cwd, 'package.json');
|
|
224
|
-
|
|
225
|
-
const extractorConfig = ExtractorConfig.prepare({
|
|
226
|
-
configObject: {
|
|
227
|
-
mainEntryPointFilePath: inputFilePath,
|
|
228
|
-
bundledPackages,
|
|
229
|
-
apiReport: {
|
|
230
|
-
enabled: false,
|
|
231
|
-
|
|
232
|
-
// `reportFileName` is not been used. It's just to fit the requirement of API Extractor.
|
|
233
|
-
reportFileName: 'report.html',
|
|
234
|
-
},
|
|
235
|
-
docModel: { apiJsonFilePath: 'api.json', enabled: false },
|
|
236
|
-
dtsRollup: {
|
|
237
|
-
enabled: true,
|
|
238
|
-
untrimmedFilePath: outputFilePath,
|
|
239
|
-
},
|
|
240
|
-
tsdocMetadata: { enabled: false, tsdocMetadataFilePath: 'another.json' },
|
|
241
|
-
compiler: {
|
|
242
|
-
tsconfigFilePath: tsconfigFilePath,
|
|
243
|
-
},
|
|
244
|
-
|
|
245
|
-
projectFolder: cwd,
|
|
246
|
-
},
|
|
247
|
-
configObjectFullPath: undefined,
|
|
248
|
-
packageJsonFullPath,
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
// Invoke API Extractor
|
|
252
|
-
const extractorResult = Extractor.invoke(extractorConfig, {
|
|
253
|
-
// Equivalent to the "--local" command-line parameter
|
|
254
|
-
localBuild: true,
|
|
255
|
-
|
|
256
|
-
messageCallback: (message) => {
|
|
257
|
-
switch (message.messageId) {
|
|
258
|
-
case ConsoleMessageId.ApiReportCreated:
|
|
259
|
-
message.logLevel = ExtractorLogLevel.None;
|
|
260
|
-
break;
|
|
261
|
-
case ConsoleMessageId.Preamble:
|
|
262
|
-
message.logLevel = ExtractorLogLevel.None;
|
|
263
|
-
break;
|
|
264
|
-
}
|
|
265
|
-
},
|
|
266
|
-
showDiagnostics: false,
|
|
267
|
-
// Equivalent to the "--verbose" command-line parameter
|
|
268
|
-
showVerboseMessages: false,
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
if (!extractorResult.succeeded) {
|
|
272
|
-
throw new Error(
|
|
273
|
-
`API Extractor completed with ${extractorResult.errorCount} errors and ${extractorResult.warningCount} warnings when processing ${inputFilePath}`,
|
|
274
|
-
);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
function runCommand(command: string) {
|
|
279
|
-
return new Promise((resolve, reject) => {
|
|
280
|
-
exec(command, {}, (error, stdout, stderr) => {
|
|
281
|
-
if (error) {
|
|
282
|
-
console.log();
|
|
283
|
-
console.error(pico.red(stdout));
|
|
284
|
-
console.error(pico.red(stderr));
|
|
285
|
-
reject(error);
|
|
286
|
-
} else {
|
|
287
|
-
resolve(stdout);
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
function openapiTypesPath(outFile) {
|
|
294
|
-
return path.resolve(
|
|
295
|
-
path.dirname(outFile),
|
|
296
|
-
`${path.basename(outFile, path.extname(outFile))}-schema.d.ts`,
|
|
297
|
-
);
|
|
298
|
-
}
|
package/src/cli.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
#! /usr/bin/env node
|
|
2
|
-
import os from 'os';
|
|
3
|
-
import fsx from 'fs-extra';
|
|
4
|
-
|
|
5
|
-
import fs from 'fs-extra';
|
|
6
|
-
|
|
7
|
-
import { cac } from 'cac';
|
|
8
|
-
import { build, buildOnce, bundleTypes } from './build.js';
|
|
9
|
-
import { findRootDir } from './index.js';
|
|
10
|
-
import { exec, execSync, spawn } from 'child_process';
|
|
11
|
-
import path from 'path';
|
|
12
|
-
import { fileURLToPath } from 'url';
|
|
13
|
-
|
|
14
|
-
export const cli = cac();
|
|
15
|
-
|
|
16
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
17
|
-
const __dirname = path.dirname(__filename);
|
|
18
|
-
|
|
19
|
-
cli
|
|
20
|
-
.command('', 'Generate an SDK package for your functions')
|
|
21
|
-
.alias('build')
|
|
22
|
-
.option('--watch', 'Watch for changes')
|
|
23
|
-
.option('--url <url>', 'URL of the package, including the base path', {
|
|
24
|
-
default: 'http://localhost:3333',
|
|
25
|
-
})
|
|
26
|
-
.option(
|
|
27
|
-
'--openapi',
|
|
28
|
-
'[experimental] Creates an openapi.json schema based on your functions',
|
|
29
|
-
)
|
|
30
|
-
.action(async (options) => {
|
|
31
|
-
const { url, watch, openapi } = options;
|
|
32
|
-
const rootDir = await findRootDir(process.cwd());
|
|
33
|
-
await build({ rootDir, url, openapi, watch });
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
cli
|
|
37
|
-
.command('types', 'Bundle browser types')
|
|
38
|
-
|
|
39
|
-
.action(async (options) => {
|
|
40
|
-
const rootDir = await findRootDir(process.cwd());
|
|
41
|
-
await bundleTypes({ rootDir });
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
cli
|
|
45
|
-
.command('init', 'Generate a new spiceflow project')
|
|
46
|
-
.option('--name <name>', 'Name of this project')
|
|
47
|
-
.action(async (options) => {
|
|
48
|
-
// copy contents of the template dir here
|
|
49
|
-
const { name } = options;
|
|
50
|
-
if (!name) {
|
|
51
|
-
throw new Error('--name is required');
|
|
52
|
-
}
|
|
53
|
-
fsx.copySync(path.resolve(__dirname, '../sdk-template'), name, {
|
|
54
|
-
filter(pathname) {
|
|
55
|
-
return !pathname.endsWith('.tsbuildinfo');
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
// replace the package.json name
|
|
59
|
-
const packageJsonPath = path.resolve(name, 'package.json');
|
|
60
|
-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
61
|
-
packageJson.name = name;
|
|
62
|
-
delete packageJson.private;
|
|
63
|
-
// replace workspace:* with * from the package.json
|
|
64
|
-
for (const key of Object.keys(packageJson.dependencies || {})) {
|
|
65
|
-
const value = packageJson.dependencies[key];
|
|
66
|
-
if (value && value.startsWith('workspace:')) {
|
|
67
|
-
packageJson.dependencies[key] = '*';
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
72
|
-
});
|
|
73
|
-
cli
|
|
74
|
-
.command('serve', 'Expose a server for your functions')
|
|
75
|
-
.option('--basePath', 'base path for the server', { default: '/' })
|
|
76
|
-
.option('--port <port>', 'Port to listen on', { default: '3333' })
|
|
77
|
-
.option('--watch', 'Watch for changes')
|
|
78
|
-
.option(
|
|
79
|
-
'--skip-build',
|
|
80
|
-
'Skip building the server and SDK, you must build it yourself first',
|
|
81
|
-
)
|
|
82
|
-
.action(async (options) => {
|
|
83
|
-
let { basePath, skipBuild, watch, port } = options;
|
|
84
|
-
const nodePath = process.execPath || 'node';
|
|
85
|
-
const rootDir = await findRootDir(process.cwd());
|
|
86
|
-
if (!skipBuild) {
|
|
87
|
-
await build({ rootDir, watch, url: `http://127.0.0.1:${port}` });
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const tempFilePath = path.resolve('_main.mjs');
|
|
91
|
-
|
|
92
|
-
const code = `import { methodsMap } from './server/index.js'; import { exposeNodeServer } from 'spiceflow/dist/expose.js'; exposeNodeServer({ methodsMap, basePath: '${basePath}', port: ${port} });`;
|
|
93
|
-
fs.writeFileSync(tempFilePath, code);
|
|
94
|
-
process.on('SIGINT', () => {
|
|
95
|
-
try {
|
|
96
|
-
fs.unlinkSync(tempFilePath);
|
|
97
|
-
} catch {}
|
|
98
|
-
process.exit(0);
|
|
99
|
-
});
|
|
100
|
-
// only enable watch if it's supported by node version
|
|
101
|
-
const major = parseInt(process.version.replace('v', '').split('.')[0]);
|
|
102
|
-
if (major && major < 18) {
|
|
103
|
-
console.log(`node version ${process.version} does not support --watch`);
|
|
104
|
-
watch = false;
|
|
105
|
-
}
|
|
106
|
-
try {
|
|
107
|
-
await new Promise<void>((resolve, reject) => {
|
|
108
|
-
const p = spawn(
|
|
109
|
-
`${nodePath} --no-warnings ${watch ? '--watch' : ''} ${JSON.stringify(
|
|
110
|
-
tempFilePath,
|
|
111
|
-
)}`,
|
|
112
|
-
{
|
|
113
|
-
stdio: 'inherit',
|
|
114
|
-
shell: true,
|
|
115
|
-
},
|
|
116
|
-
);
|
|
117
|
-
p.on('close', (code) => {
|
|
118
|
-
if (code === 0) {
|
|
119
|
-
resolve();
|
|
120
|
-
} else {
|
|
121
|
-
reject();
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
} finally {
|
|
126
|
-
fs.unlinkSync(tempFilePath);
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
cli.help();
|
|
131
|
-
|
|
132
|
-
cli.parse();
|
package/src/context-internal.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
|
-
import type { IncomingMessage, ServerResponse } from 'http';
|
|
3
|
-
|
|
4
|
-
interface CommonContext {
|
|
5
|
-
// cookies(): ReadonlyRequestCookies;
|
|
6
|
-
// headers(): ReadonlyHeaders;
|
|
7
|
-
}
|
|
8
|
-
interface NodejsContext extends CommonContext {
|
|
9
|
-
req?: IncomingMessage;
|
|
10
|
-
res?: ServerResponse;
|
|
11
|
-
}
|
|
12
|
-
interface EdgeContext extends CommonContext {
|
|
13
|
-
req?: Request;
|
|
14
|
-
res?: Response;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const DEFAULT_CONTEXT = {
|
|
18
|
-
// headers() {},
|
|
19
|
-
// cookies() {},
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export const asyncLocalStorage = new AsyncLocalStorage<
|
|
23
|
-
NodejsContext | EdgeContext
|
|
24
|
-
>();
|
|
25
|
-
|
|
26
|
-
export function getNodejsContext(): NodejsContext {
|
|
27
|
-
return (asyncLocalStorage.getStore() as NodejsContext) || DEFAULT_CONTEXT;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function getEdgeContext(): EdgeContext {
|
|
31
|
-
return (asyncLocalStorage.getStore() as EdgeContext) || DEFAULT_CONTEXT;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function getContext(): CommonContext {
|
|
35
|
-
return asyncLocalStorage.getStore() || DEFAULT_CONTEXT;
|
|
36
|
-
}
|
package/src/context.ts
DELETED
package/src/expose.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import { internalNodeJsHandler } from './server.js';
|
|
3
|
-
|
|
4
|
-
export async function exposeNodeServer({ methodsMap, basePath, port }) {
|
|
5
|
-
const handler = internalNodeJsHandler({ methodsMap });
|
|
6
|
-
|
|
7
|
-
const server = http.createServer(async (req, res) => {
|
|
8
|
-
let ended = false;
|
|
9
|
-
res.on('close', () => {
|
|
10
|
-
ended = true;
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
if (req.method === 'GET' && req.url === '/') {
|
|
15
|
-
res.statusCode = 200;
|
|
16
|
-
res.end('ok');
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
console.log(`[spiceflow] ${req.url}`);
|
|
20
|
-
await handler({ req, res, basePath });
|
|
21
|
-
} catch (error) {
|
|
22
|
-
if (ended) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
console.error(error);
|
|
26
|
-
|
|
27
|
-
res.statusCode = 500;
|
|
28
|
-
res.end('Internal server error');
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
server.listen(port, () => {
|
|
32
|
-
console.log(`server listening on http://127.0.0.1:${port}`);
|
|
33
|
-
});
|
|
34
|
-
}
|
package/src/headers.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
// import { asyncLocalStorage } from './context-internal';
|
|
2
|
-
|
|
3
|
-
// function myHeaders() {
|
|
4
|
-
// const res = asyncLocalStorage.getStore()?.headers();
|
|
5
|
-
// if (!res) {
|
|
6
|
-
// throw new Error('Called headers() outside of app dir or rpc actions');
|
|
7
|
-
// }
|
|
8
|
-
// return res;
|
|
9
|
-
// }
|
|
10
|
-
|
|
11
|
-
// function myCookies() {
|
|
12
|
-
// const res = asyncLocalStorage.getStore()?.cookies();
|
|
13
|
-
// if (!res) {
|
|
14
|
-
// throw new Error('Called cookies() outside of app dir or rpc actions');
|
|
15
|
-
// }
|
|
16
|
-
// return res;
|
|
17
|
-
// }
|
|
18
|
-
|
|
19
|
-
// export { myCookies as cookies, myHeaders as headers };
|
package/src/index.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import { PluginOptions } from './babelTransformRpc.js';
|
|
4
|
-
|
|
5
|
-
import { WrapMethod, WrapMethodMeta } from './server.js';
|
|
6
|
-
|
|
7
|
-
export interface WithRpcConfig {}
|
|
8
|
-
|
|
9
|
-
export { WrapMethod };
|
|
10
|
-
|
|
11
|
-
import pluginSyntaxJsx from '@babel/plugin-syntax-jsx';
|
|
12
|
-
import pluginTransformTypescript from '@babel/plugin-syntax-typescript';
|
|
13
|
-
import babelTransformRpc from './babelTransformRpc.js';
|
|
14
|
-
import babelDebugOutputs from './babelDebugOutputs.js';
|
|
15
|
-
|
|
16
|
-
export function plugins(options: PluginOptions) {
|
|
17
|
-
return [
|
|
18
|
-
pluginSyntaxJsx,
|
|
19
|
-
[pluginTransformTypescript, { isTSX: true }],
|
|
20
|
-
[babelTransformRpc, options],
|
|
21
|
-
process.env.DEBUG_ACTIONS && [babelDebugOutputs, options],
|
|
22
|
-
].filter(Boolean);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function findRootDir(dir: string): string {
|
|
26
|
-
{
|
|
27
|
-
let curDir = path.resolve(dir, 'src');
|
|
28
|
-
if (fs.existsSync(curDir)) return path.resolve(curDir);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
throw new Error(
|
|
32
|
-
"Couldn't find a src directory. Please create one under the project root",
|
|
33
|
-
);
|
|
34
|
-
}
|
package/src/jsonRpc.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
// https://www.jsonrpc.org/specification
|
|
2
|
-
|
|
3
|
-
export type JsonRpcRequestId = string | number | null;
|
|
4
|
-
|
|
5
|
-
export type JsonValue =
|
|
6
|
-
| string
|
|
7
|
-
| number
|
|
8
|
-
| boolean
|
|
9
|
-
| null
|
|
10
|
-
| JsonValue[]
|
|
11
|
-
| { [key: string]: JsonValue };
|
|
12
|
-
|
|
13
|
-
export interface JsonRpcRequest {
|
|
14
|
-
jsonrpc: '2.0';
|
|
15
|
-
method: string;
|
|
16
|
-
params: JsonValue[];
|
|
17
|
-
meta?: any
|
|
18
|
-
id: JsonRpcRequestId;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface JsonRpcError {
|
|
22
|
-
code: number;
|
|
23
|
-
message: string;
|
|
24
|
-
data?: JsonValue;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface JsonRpcSuccessResponse {
|
|
28
|
-
jsonrpc: '2.0';
|
|
29
|
-
result: JsonValue;
|
|
30
|
-
error?: undefined;
|
|
31
|
-
id: JsonRpcRequestId;
|
|
32
|
-
meta?: any;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface JsonRpcErrorResponse {
|
|
36
|
-
jsonrpc: '2.0';
|
|
37
|
-
result?: undefined;
|
|
38
|
-
error: JsonRpcError;
|
|
39
|
-
id: JsonRpcRequestId;
|
|
40
|
-
meta?: any;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export type JsonRpcResponse = JsonRpcSuccessResponse | JsonRpcErrorResponse;
|