@sanity/runtime-cli 4.3.4 → 4.3.5-bundle.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 +17 -17
- package/dist/actions/blueprints/assets.d.ts +1 -0
- package/dist/actions/blueprints/assets.js +21 -4
- package/dist/actions/functions/test.d.ts +2 -2
- package/dist/actions/functions/test.js +2 -2
- package/dist/commands/blueprints/deploy.js +3 -4
- package/dist/commands/functions/test.js +3 -6
- package/dist/server/app.js +80 -12
- package/dist/server/static/api.js +24 -3
- package/dist/server/static/components/app.css +74 -23
- package/dist/server/static/components/filters.js +1 -1
- package/dist/server/static/components/function-list.js +5 -5
- package/dist/server/static/components/payload-panel.js +4 -3
- package/dist/server/static/components/response-panel.js +37 -35
- package/dist/server/static/index.html +5 -3
- package/dist/server/static/vendor/vendor.bundle.d.ts +2 -2
- package/dist/utils/build-payload.d.ts +1 -1
- package/dist/utils/build-payload.js +3 -3
- package/dist/utils/bundle/bundle-function.d.ts +8 -0
- package/dist/utils/bundle/bundle-function.js +125 -0
- package/dist/utils/bundle/cleanup-source-maps.d.ts +10 -0
- package/dist/utils/bundle/cleanup-source-maps.js +53 -0
- package/dist/utils/bundle/find-up.d.ts +16 -0
- package/dist/utils/bundle/find-up.js +39 -0
- package/dist/utils/bundle/verify-handler.d.ts +2 -0
- package/dist/utils/bundle/verify-handler.js +13 -0
- package/dist/utils/child-process-wrapper.js +8 -6
- package/dist/utils/functions/find-entry-point.d.ts +11 -0
- package/dist/utils/functions/find-entry-point.js +75 -0
- package/dist/utils/functions/should-bundle.d.ts +2 -0
- package/dist/utils/functions/should-bundle.js +23 -0
- package/dist/utils/invoke-local.d.ts +2 -2
- package/dist/utils/invoke-local.js +48 -7
- package/dist/utils/is-record.d.ts +1 -0
- package/dist/utils/is-record.js +3 -0
- package/dist/utils/parse-json-object.d.ts +1 -0
- package/dist/utils/parse-json-object.js +10 -0
- package/dist/utils/types.d.ts +3 -1
- package/oclif.manifest.json +1 -1
- package/package.json +4 -1
- package/dist/utils/is-json.d.ts +0 -1
- package/dist/utils/is-json.js +0 -12
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
import {EditorView, basicSetup, json} from '../vendor/vendor.bundle.js'
|
|
3
3
|
import {ApiBaseElement} from './api-base.js'
|
|
4
4
|
|
|
5
|
-
const template = `<div class="gutter-gradient relative h-100 max-h-100 y-scroll">
|
|
5
|
+
const template = `<div class="gutter-gradient relative h-100 max-h-100 y-scroll border-top border-top-none-l">
|
|
6
6
|
<div class="bg gutter-gradient sticky top-0 right-0 left-0 z-100">
|
|
7
7
|
<div class="flex items-center space-between" style="padding: 8px 20px 8px 48px;">
|
|
8
8
|
<h2 class="config-label mar-t-0 mar-b-0">Event</h2>
|
|
9
9
|
<button ord="primary" class="sanity-button">
|
|
10
10
|
<span>Run</span>
|
|
11
|
-
|
|
11
|
+
<svg data-sanity-icon="play" width="1em" height="1em" viewBox="0 0 25 25" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M7.5 18.5V6.5L17.5 12.5L7.5 18.5Z" fill="currentColor" stroke="currentColor" stroke-width="1.2" stroke-linejoin="round"></path></svg>
|
|
12
12
|
</button>
|
|
13
13
|
</div>
|
|
14
14
|
<hr class='hr-border' style='margin-left: 31px; ' />
|
|
@@ -35,7 +35,8 @@ class PayloadPanel extends ApiBaseElement {
|
|
|
35
35
|
this.button.innerHTML = '<network-spinner></network-spinner>'
|
|
36
36
|
} else {
|
|
37
37
|
this.button.removeAttribute('disabled')
|
|
38
|
-
this.button.
|
|
38
|
+
this.button.innerHTML = `<span>Run</span>
|
|
39
|
+
<svg data-sanity-icon="play" width="1em" height="1em" viewBox="0 0 25 25" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M7.5 18.5V6.5L17.5 12.5L7.5 18.5Z" fill="currentColor" stroke="currentColor" stroke-width="1.2" stroke-linejoin="round"></path></svg>`
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
|
|
@@ -10,43 +10,35 @@ import {
|
|
|
10
10
|
} from '../vendor/vendor.bundle.js'
|
|
11
11
|
import {ApiBaseElement} from './api-base.js'
|
|
12
12
|
|
|
13
|
-
const template = `<div class="
|
|
14
|
-
|
|
15
|
-
<
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
const template = `<div class="border-left border-top border-top-none-l h-100 gutter-gradient" style='height: 100%; max-height: 100%; overflow: hidden; display:grid;grid-template-rows: 50% 50%; grid-template-columns: 1fr;' >
|
|
14
|
+
<!-- Response Section -->
|
|
15
|
+
<div style='overflow-y:scroll;'>
|
|
16
|
+
<div style='padding-bottom: var(--space-6)'>
|
|
17
|
+
<h3 class="config-label" style='display: none; margin-top: 0;'>Response</h3>
|
|
18
|
+
<header class='flex space-between'>
|
|
19
|
+
<dl class='slab-stat'>
|
|
20
|
+
<dt style='white-space:nowrap;'>Response size</dt>
|
|
21
|
+
<dd id="size" style='white-space:nowrap;'></dd>
|
|
22
|
+
</dl>
|
|
23
|
+
<dl class='slab-stat'>
|
|
24
|
+
<dt>Time</dt>
|
|
25
|
+
<dd><time id="time" datetime=""></time></dd>
|
|
26
|
+
</dl>
|
|
27
|
+
</header>
|
|
28
|
+
<div id="response" name="response" class="cm-s-dracula"></div>
|
|
22
29
|
</div>
|
|
23
|
-
|
|
30
|
+
</div>
|
|
31
|
+
|
|
32
|
+
<!-- Console Section -->
|
|
33
|
+
<div style='position: relative; border-top: 1px solid var(--card-border-color); background: var(--base-background-color); padding: var(--space-0) var(--space-3) var(--space-6) var(--space-4);overflow-y:scroll;'>
|
|
34
|
+
<h3 class="config-label" style="padding-top: var(--space-3); padding-bottom: var(--space-3); z-index: 32; background: var(--base-background-color); position: sticky; top: 0; left: 0; right: 0; margin-top:0;margin-bottom:0;">Console</h3>
|
|
35
|
+
<pre style="padding: 0; margin: 0; white-space: pre-wrap; word-wrap: break-word;"></pre>
|
|
24
36
|
</div>
|
|
25
|
-
<div role="tabpanel" data-tab-id="b" class="pad-t-sm h-100" hidden><pre></pre></div>
|
|
26
37
|
</div>
|
|
27
38
|
`
|
|
28
39
|
class ResponsePanel extends ApiBaseElement {
|
|
29
|
-
switchTab = (e) => {
|
|
30
|
-
const tabElement = e.target.closest('[role=tab]')
|
|
31
|
-
// If the click wasn't on a tab button, do nothing
|
|
32
|
-
if (!tabElement) {
|
|
33
|
-
return
|
|
34
|
-
}
|
|
35
|
-
const selectedTabId = tabElement.id
|
|
36
|
-
|
|
37
|
-
// Select the tab and its panel
|
|
38
|
-
// Use e.currentTarget which refers to the element the listener was attached to (m-tabs)
|
|
39
|
-
for (const tab of e.currentTarget.querySelectorAll('[role=tab]')) {
|
|
40
|
-
tab.ariaSelected = tab.id === selectedTabId
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Find panels relative to the component itself to avoid affecting other potential tab panels on the page
|
|
44
|
-
for (const panel of this.querySelectorAll('[role=tabpanel]')) {
|
|
45
|
-
panel.hidden = panel.dataset.tabId !== selectedTabId
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
40
|
updateResponse = ({result}) => {
|
|
49
|
-
const {error, json, logs, time} = result
|
|
41
|
+
const {error, json, logs, time, timings} = result
|
|
50
42
|
if (!error) {
|
|
51
43
|
const transaction = this.api.store.response.state.update({
|
|
52
44
|
changes: {
|
|
@@ -58,7 +50,20 @@ class ResponsePanel extends ApiBaseElement {
|
|
|
58
50
|
this.api.store.response.dispatch(transaction)
|
|
59
51
|
|
|
60
52
|
this.size.innerText = json ? prettyBytes(JSON.stringify(json).length) : ''
|
|
61
|
-
|
|
53
|
+
|
|
54
|
+
if (timings && 'bundle' in timings && 'execute' in timings) {
|
|
55
|
+
const bundleTime = prettyMilliseconds(timings.bundle)
|
|
56
|
+
const executeTime = prettyMilliseconds(timings.execute)
|
|
57
|
+
this.time.innerText = `${executeTime} (+${bundleTime} bundle time)`
|
|
58
|
+
this.time.dateTime = `PT${executeTime / 1000}S`
|
|
59
|
+
} else if (timings && 'execute' in timings) {
|
|
60
|
+
this.time.innerText = prettyMilliseconds(timings.execute)
|
|
61
|
+
this.time.dateTime = `PT${timings.execute / 1000}S`
|
|
62
|
+
} else {
|
|
63
|
+
this.time.innerText = prettyMilliseconds(time)
|
|
64
|
+
this.time.dateTime = `PT${time / 1000}S`
|
|
65
|
+
}
|
|
66
|
+
|
|
62
67
|
this.consoleTab.innerText = logs
|
|
63
68
|
} else {
|
|
64
69
|
this.consoleTab.innerText = error?.details?.error
|
|
@@ -71,8 +76,6 @@ class ResponsePanel extends ApiBaseElement {
|
|
|
71
76
|
this.size = this.querySelector('#size')
|
|
72
77
|
this.time = this.querySelector('#time')
|
|
73
78
|
this.consoleTab = this.querySelector('pre')
|
|
74
|
-
this.tabs = this.querySelector('m-tabs')
|
|
75
|
-
this.tabs.addEventListener('click', this.switchTab)
|
|
76
79
|
this.api.subscribe(this.updateResponse, ['result'])
|
|
77
80
|
|
|
78
81
|
this.api.store.response = new EditorView({
|
|
@@ -83,7 +86,6 @@ class ResponsePanel extends ApiBaseElement {
|
|
|
83
86
|
}
|
|
84
87
|
|
|
85
88
|
disconnectedCallback() {
|
|
86
|
-
this.tabs.removeEventListener('click', this.switchTab)
|
|
87
89
|
this.api.unsubscribe(this.updateResponse)
|
|
88
90
|
}
|
|
89
91
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
<!DOCTYPE html>
|
|
2
2
|
<html>
|
|
3
3
|
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
4
5
|
<link href="https://unpkg.com/m-@3.2.0/dist/m-.woff2" rel="preload" as="font" crossorigin>
|
|
5
|
-
<link href="./components/app.css" rel="stylesheet">
|
|
6
6
|
<link href="https://unpkg.com/m-@3.2.0/dist/m-.css" rel="stylesheet">
|
|
7
|
+
<link href="./components/app.css" rel="stylesheet">
|
|
8
|
+
|
|
7
9
|
</head>
|
|
8
10
|
<body>
|
|
9
11
|
<header data-slot="header">
|
|
@@ -16,10 +18,10 @@
|
|
|
16
18
|
</header>
|
|
17
19
|
<filters-component></filters-component>
|
|
18
20
|
<nav style="padding-top: var(--size-5); border-right: 1px solid var(--card-border-color);max-height: 100%; overflow-y: scroll;">
|
|
19
|
-
<h4 class="config-label mar-t-0" style="padding-left:
|
|
21
|
+
<h4 class="config-label mar-t-0" style="padding-left: var(--space-3); margin-bottom:var(--space-3);">Functions</h4>
|
|
20
22
|
<function-list></function-list>
|
|
21
23
|
</nav>
|
|
22
|
-
<main style='display: grid; grid-template-columns: 1fr 1fr;grid-template-rows: 1fr;'>
|
|
24
|
+
<main style='display: grid; grid-template-columns: 1fr 1fr;grid-template-rows: 1fr;max-height: 100%;overflow:hidden;'>
|
|
23
25
|
<payload-panel></payload-panel>
|
|
24
26
|
<response-panel></response-panel>
|
|
25
27
|
</main>
|
|
@@ -1018,9 +1018,9 @@ declare class ViewState {
|
|
|
1018
1018
|
viewport: Viewport | undefined;
|
|
1019
1019
|
lineGaps: any[];
|
|
1020
1020
|
lineGapDeco: any;
|
|
1021
|
-
updateForViewport():
|
|
1021
|
+
updateForViewport(): 2 | 0;
|
|
1022
1022
|
viewports: (Viewport | undefined)[] | undefined;
|
|
1023
|
-
updateScaler():
|
|
1023
|
+
updateScaler(): 2 | 0;
|
|
1024
1024
|
updateViewportLines(): void;
|
|
1025
1025
|
viewportLines: any[] | undefined;
|
|
1026
1026
|
update(update: any, scrollTarget?: null): void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { InvokePayloadOptions } from './types.js';
|
|
2
|
-
export default function buildPayload(options: InvokePayloadOptions):
|
|
2
|
+
export default function buildPayload(options: InvokePayloadOptions): Record<string, unknown> | null;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { readFileSync } from 'node:fs';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { cwd } from 'node:process';
|
|
4
|
-
import
|
|
4
|
+
import { parseJsonObject } from './parse-json-object.js';
|
|
5
5
|
export default function buildPayload(options) {
|
|
6
6
|
const { data, file } = options;
|
|
7
7
|
let payload = {};
|
|
8
8
|
if (data) {
|
|
9
|
-
payload =
|
|
9
|
+
payload = parseJsonObject(data);
|
|
10
10
|
}
|
|
11
11
|
else if (file) {
|
|
12
|
-
payload =
|
|
12
|
+
payload = parseJsonObject(readFileSync(join(cwd(), file), 'utf8'));
|
|
13
13
|
}
|
|
14
14
|
return payload;
|
|
15
15
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { LocalFunctionResource } from '../types.js';
|
|
2
|
+
export declare function bundleFunction(resource: LocalFunctionResource): Promise<{
|
|
3
|
+
type: string;
|
|
4
|
+
outputDir: string;
|
|
5
|
+
warnings: string[];
|
|
6
|
+
cleanup: () => Promise<void>;
|
|
7
|
+
timings: Record<string, number>;
|
|
8
|
+
}>;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { performance } from 'node:perf_hooks';
|
|
4
|
+
import { cwd } from 'node:process';
|
|
5
|
+
import { build as viteBuild } from 'vite';
|
|
6
|
+
import tsConfigPaths from 'vite-tsconfig-paths';
|
|
7
|
+
import { findFunctionEntryPoint } from '../functions/find-entry-point.js';
|
|
8
|
+
import { cleanupSourceMaps } from './cleanup-source-maps.js';
|
|
9
|
+
import { findDirUp } from './find-up.js';
|
|
10
|
+
import { verifyHandler } from './verify-handler.js';
|
|
11
|
+
export async function bundleFunction(resource) {
|
|
12
|
+
if (!resource.src)
|
|
13
|
+
throw new Error('Resource src is required');
|
|
14
|
+
if (!resource.name)
|
|
15
|
+
throw new Error('Resource name is required');
|
|
16
|
+
const timings = {};
|
|
17
|
+
const bundleStart = performance.now();
|
|
18
|
+
const sourcePath = path.resolve(cwd(), resource.src);
|
|
19
|
+
const stats = await stat(sourcePath);
|
|
20
|
+
const fnDisplayName = resource.displayName ?? resource.name;
|
|
21
|
+
const findEntryStart = performance.now();
|
|
22
|
+
const entry = await findFunctionEntryPoint(sourcePath, fnDisplayName);
|
|
23
|
+
timings['bundle:findEntry'] = performance.now() - findEntryStart;
|
|
24
|
+
const entryDir = stats.isFile() ? path.dirname(sourcePath) : sourcePath;
|
|
25
|
+
const outputPathStart = performance.now();
|
|
26
|
+
const outputDir = await getBundleOutputPath(entryDir, resource.name);
|
|
27
|
+
const outputFile = path.join(outputDir, getOutputFilename(entry));
|
|
28
|
+
const fnRootDir = (await findDirUp('node_modules', entryDir)) || entryDir;
|
|
29
|
+
timings['bundle:setupOutput'] = performance.now() - outputPathStart;
|
|
30
|
+
async function cleanupTmpDir() {
|
|
31
|
+
// Feel a certain way about leaving things uncleaned, but helps with debugging for now
|
|
32
|
+
// await rm(outputDir, {recursive: true, force: true}).catch(logCleanupFailure)
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const viteStart = performance.now();
|
|
36
|
+
const result = await viteBuild({
|
|
37
|
+
root: fnRootDir,
|
|
38
|
+
logLevel: 'silent',
|
|
39
|
+
build: {
|
|
40
|
+
target: 'node20',
|
|
41
|
+
outDir: outputDir,
|
|
42
|
+
emptyOutDir: false,
|
|
43
|
+
minify: false,
|
|
44
|
+
sourcemap: true,
|
|
45
|
+
ssr: true,
|
|
46
|
+
rollupOptions: {
|
|
47
|
+
input: entry,
|
|
48
|
+
output: {
|
|
49
|
+
format: 'esm',
|
|
50
|
+
entryFileNames: getOutputFilename(entry),
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
ssr: {
|
|
55
|
+
noExternal: true,
|
|
56
|
+
resolve: {
|
|
57
|
+
conditions: ['sanity-function', 'vite'],
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
plugins: [tsConfigPaths()],
|
|
61
|
+
});
|
|
62
|
+
timings['bundle:build'] = performance.now() - viteStart;
|
|
63
|
+
const verifyStart = performance.now();
|
|
64
|
+
await verifyHandler(result);
|
|
65
|
+
timings['bundle:verify'] = performance.now() - verifyStart;
|
|
66
|
+
const pkgStart = performance.now();
|
|
67
|
+
await writeBundledPackageJson(entryDir, outputFile);
|
|
68
|
+
timings['bundle:writePackage'] = performance.now() - pkgStart;
|
|
69
|
+
const cleanupStart = performance.now();
|
|
70
|
+
await cleanupSourceMaps(sourcePath, outputDir);
|
|
71
|
+
timings['bundle:cleanupMaps'] = performance.now() - cleanupStart;
|
|
72
|
+
timings.bundle = performance.now() - bundleStart;
|
|
73
|
+
return {
|
|
74
|
+
type: 'success',
|
|
75
|
+
outputDir,
|
|
76
|
+
warnings: [],
|
|
77
|
+
cleanup: cleanupTmpDir,
|
|
78
|
+
timings,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
await cleanupTmpDir();
|
|
83
|
+
throw new Error(`Bundling of function failed: ${err instanceof Error ? err.message : err}`, {
|
|
84
|
+
cause: err,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async function writeBundledPackageJson(inputDir, outputFilePath) {
|
|
89
|
+
const baseName = path.basename(outputFilePath);
|
|
90
|
+
let original;
|
|
91
|
+
try {
|
|
92
|
+
const pkgJsonPath = path.join(inputDir, 'package.json');
|
|
93
|
+
original = JSON.parse(await readFile(pkgJsonPath, 'utf-8'));
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
original = undefined;
|
|
97
|
+
}
|
|
98
|
+
const bundled = {
|
|
99
|
+
// One could argue that we should strip this down significantly.
|
|
100
|
+
// Theoretically though, a function may reach into it to get dependency versions
|
|
101
|
+
// and whatnot, so maybe it makes more sense to keep it as close to the original
|
|
102
|
+
// as possible?
|
|
103
|
+
...original,
|
|
104
|
+
main: baseName, // This should never be the input file, always the built output name
|
|
105
|
+
type: 'module', // We explicitly create ESM output
|
|
106
|
+
};
|
|
107
|
+
const pkgJsonOutputPath = path.join(path.dirname(outputFilePath), 'package.json');
|
|
108
|
+
await writeFile(pkgJsonOutputPath, JSON.stringify(bundled, null, 2));
|
|
109
|
+
}
|
|
110
|
+
async function getBundleOutputPath(entryDir, fnName) {
|
|
111
|
+
const tmpPath = path.resolve(entryDir, '.build', `function-${fnName}`);
|
|
112
|
+
await rm(tmpPath, { recursive: true, force: true }).catch(logCleanupFailure);
|
|
113
|
+
await mkdir(tmpPath, { recursive: true });
|
|
114
|
+
return tmpPath;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Minor convenience/niceness to keep the same input filename, but change the extension
|
|
118
|
+
*/
|
|
119
|
+
function getOutputFilename(entryFileName) {
|
|
120
|
+
const baseName = path.basename(entryFileName, path.extname(entryFileName));
|
|
121
|
+
return baseName ? `${baseName}.js` : 'index.js';
|
|
122
|
+
}
|
|
123
|
+
function logCleanupFailure(err) {
|
|
124
|
+
console.warn(`[warn] Failed to clean up temporary files: ${err instanceof Error ? err.message : err}`);
|
|
125
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Clean up" source maps by removing absolute paths and making paths relative to the
|
|
3
|
+
* _input_ (eg source) rather than the _output_ (eg bundle) directory. Note that this
|
|
4
|
+
* process is not critical since the source content is inlined, but it helps with
|
|
5
|
+
* debugging to have (approximate) paths to the original source files.
|
|
6
|
+
*
|
|
7
|
+
* @param inputDir - The directory where the source files are located
|
|
8
|
+
* @param outputDir - The directory where the bundled files are located
|
|
9
|
+
*/
|
|
10
|
+
export declare function cleanupSourceMaps(inputDir: string, outputDir: string): Promise<void>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs';
|
|
2
|
+
import { isAbsolute, join, relative } from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* "Clean up" source maps by removing absolute paths and making paths relative to the
|
|
5
|
+
* _input_ (eg source) rather than the _output_ (eg bundle) directory. Note that this
|
|
6
|
+
* process is not critical since the source content is inlined, but it helps with
|
|
7
|
+
* debugging to have (approximate) paths to the original source files.
|
|
8
|
+
*
|
|
9
|
+
* @param inputDir - The directory where the source files are located
|
|
10
|
+
* @param outputDir - The directory where the bundled files are located
|
|
11
|
+
*/
|
|
12
|
+
export async function cleanupSourceMaps(inputDir, outputDir) {
|
|
13
|
+
const entries = await fs.readdir(outputDir, { withFileTypes: true });
|
|
14
|
+
const sourceMaps = entries.filter((entry) => entry.isFile() && entry.name.endsWith('.map'));
|
|
15
|
+
for (const entry of sourceMaps) {
|
|
16
|
+
const filePath = join(outputDir, entry.name);
|
|
17
|
+
let raw;
|
|
18
|
+
try {
|
|
19
|
+
raw = await fs.readFile(filePath, 'utf8');
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
let map;
|
|
25
|
+
try {
|
|
26
|
+
const json = JSON.parse(raw);
|
|
27
|
+
map = isRelevantSourceMap(json) ? json : undefined;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (!map) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
map.sources = map.sources.map((source) => {
|
|
36
|
+
const fullPath = isAbsolute(source) ? source : join(outputDir, source);
|
|
37
|
+
return relative(inputDir, fullPath);
|
|
38
|
+
});
|
|
39
|
+
try {
|
|
40
|
+
await fs.writeFile(filePath, JSON.stringify(map));
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// ignore write errors
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function isRelevantSourceMap(map) {
|
|
48
|
+
return (typeof map === 'object' &&
|
|
49
|
+
map !== null &&
|
|
50
|
+
'sources' in map &&
|
|
51
|
+
Array.isArray(map.sources) &&
|
|
52
|
+
map.sources.every((source) => typeof source === 'string'));
|
|
53
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walks up the directory tree from a starting point to find a given file/directory.
|
|
3
|
+
*
|
|
4
|
+
* @param fileName - The name of the file/directory to find.
|
|
5
|
+
* @param startDir - The directory to start searching from (default is the current working directory).
|
|
6
|
+
* @returns The path to the file if found, otherwise undefined.
|
|
7
|
+
*/
|
|
8
|
+
export declare function findUp(fileName: string, startDir?: string): Promise<string | undefined>;
|
|
9
|
+
/**
|
|
10
|
+
* Finds the directory containing a specific file/directory by walking up the directory tree.
|
|
11
|
+
*
|
|
12
|
+
* @param fileName - The name of the file/directory to find.
|
|
13
|
+
* @param startDir - The directory to start searching from (default is the current working directory).
|
|
14
|
+
* @returns The directory containing the file if found, otherwise undefined.
|
|
15
|
+
*/
|
|
16
|
+
export declare function findDirUp(fileName: string, startDir?: string): Promise<string | undefined>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { access } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* Walks up the directory tree from a starting point to find a given file/directory.
|
|
5
|
+
*
|
|
6
|
+
* @param fileName - The name of the file/directory to find.
|
|
7
|
+
* @param startDir - The directory to start searching from (default is the current working directory).
|
|
8
|
+
* @returns The path to the file if found, otherwise undefined.
|
|
9
|
+
*/
|
|
10
|
+
export async function findUp(fileName, startDir = process.cwd()) {
|
|
11
|
+
let dir = path.resolve(startDir);
|
|
12
|
+
while (true) {
|
|
13
|
+
const candidate = path.join(dir, fileName);
|
|
14
|
+
try {
|
|
15
|
+
await access(candidate);
|
|
16
|
+
return candidate;
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
const parent = path.dirname(dir);
|
|
20
|
+
if (parent === dir)
|
|
21
|
+
break; // Reached root
|
|
22
|
+
dir = parent;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Finds the directory containing a specific file/directory by walking up the directory tree.
|
|
29
|
+
*
|
|
30
|
+
* @param fileName - The name of the file/directory to find.
|
|
31
|
+
* @param startDir - The directory to start searching from (default is the current working directory).
|
|
32
|
+
* @returns The directory containing the file if found, otherwise undefined.
|
|
33
|
+
*/
|
|
34
|
+
export async function findDirUp(fileName, startDir = process.cwd()) {
|
|
35
|
+
const filePath = await findUp(fileName, startDir);
|
|
36
|
+
if (!filePath)
|
|
37
|
+
return undefined;
|
|
38
|
+
return path.dirname(filePath);
|
|
39
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export async function verifyHandler(result) {
|
|
2
|
+
if ('close' in result) {
|
|
3
|
+
throw new Error('Incorrect build output, got watcher');
|
|
4
|
+
}
|
|
5
|
+
const outputs = (Array.isArray(result) ? result : [result]).flatMap(({ output }) => output);
|
|
6
|
+
const bundledIndex = outputs.find((output) => output.type === 'chunk' && output.isEntry && output.name === 'index');
|
|
7
|
+
if (!bundledIndex || bundledIndex.type !== 'chunk') {
|
|
8
|
+
throw new Error('Unexpected build output, no bundled index found');
|
|
9
|
+
}
|
|
10
|
+
if (!bundledIndex.exports.includes('handler')) {
|
|
11
|
+
throw new Error('Unexpected build output, no `handler` export found');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import {existsSync, statSync} from 'node:fs'
|
|
2
|
-
import {join} from 'node:path'
|
|
2
|
+
import {isAbsolute, join} from 'node:path'
|
|
3
3
|
import process from 'node:process'
|
|
4
4
|
|
|
5
5
|
export function getFunctionSource(src) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
const pathToCheck = isAbsolute(src) ? src : join(process.cwd(), src)
|
|
7
|
+
|
|
8
|
+
if (statSync(pathToCheck).isDirectory()) {
|
|
9
|
+
const indexPath = join(pathToCheck, 'index.js')
|
|
8
10
|
if (!existsSync(indexPath)) {
|
|
9
|
-
throw Error(`Function directory ${
|
|
11
|
+
throw Error(`Function directory ${pathToCheck} has no index.js`)
|
|
10
12
|
}
|
|
11
13
|
return indexPath
|
|
12
14
|
}
|
|
13
|
-
return
|
|
15
|
+
return pathToCheck
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
// Start when payload data arrives from parent process
|
|
@@ -28,7 +30,7 @@ process.on('message', async (data) => {
|
|
|
28
30
|
let json = null
|
|
29
31
|
|
|
30
32
|
// Import the function code
|
|
31
|
-
const {handler} = await import(getFunctionSource(
|
|
33
|
+
const {handler} = await import(getFunctionSource(srcPath))
|
|
32
34
|
|
|
33
35
|
// backup stdout
|
|
34
36
|
const originalStdoutWrite = process.stdout.write.bind(process.stdout)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the source path to an executable entry file path.
|
|
3
|
+
*
|
|
4
|
+
* If the source path is a directory, it looks for `package.json#main`, then `index.ts`, then `index.js`.
|
|
5
|
+
*
|
|
6
|
+
* @param srcPath - The source path (can be a file or directory).
|
|
7
|
+
* @param displayName - Optional display name for the function, used in error messages.
|
|
8
|
+
* @returns The absolute path to the entry file.
|
|
9
|
+
* @throws If the entry file cannot be determined.
|
|
10
|
+
*/
|
|
11
|
+
export declare function findFunctionEntryPoint(srcPath: string, displayName?: string): Promise<string>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
2
|
+
import { join, resolve } from 'node:path';
|
|
3
|
+
import { cwd } from 'node:process';
|
|
4
|
+
/**
|
|
5
|
+
* Resolves the source path to an executable entry file path.
|
|
6
|
+
*
|
|
7
|
+
* If the source path is a directory, it looks for `package.json#main`, then `index.ts`, then `index.js`.
|
|
8
|
+
*
|
|
9
|
+
* @param srcPath - The source path (can be a file or directory).
|
|
10
|
+
* @param displayName - Optional display name for the function, used in error messages.
|
|
11
|
+
* @returns The absolute path to the entry file.
|
|
12
|
+
* @throws If the entry file cannot be determined.
|
|
13
|
+
*/
|
|
14
|
+
export async function findFunctionEntryPoint(srcPath, displayName) {
|
|
15
|
+
const absolutePath = resolve(cwd(), srcPath);
|
|
16
|
+
let stats;
|
|
17
|
+
try {
|
|
18
|
+
stats = await stat(absolutePath);
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
throw new Error(`Source path not found or inaccessible: ${srcPath}`, { cause: err });
|
|
22
|
+
}
|
|
23
|
+
if (stats.isFile()) {
|
|
24
|
+
// It's already an entry file path
|
|
25
|
+
return absolutePath;
|
|
26
|
+
}
|
|
27
|
+
if (stats.isDirectory()) {
|
|
28
|
+
// 1. Check package.json#main
|
|
29
|
+
try {
|
|
30
|
+
const pkgJsonPath = join(absolutePath, 'package.json');
|
|
31
|
+
const pkgJsonContent = await readFile(pkgJsonPath, 'utf8');
|
|
32
|
+
const pkgJson = JSON.parse(pkgJsonContent);
|
|
33
|
+
if (pkgJson.main) {
|
|
34
|
+
const mainPath = resolve(absolutePath, pkgJson.main);
|
|
35
|
+
if (await fileExists(mainPath)) {
|
|
36
|
+
return mainPath;
|
|
37
|
+
}
|
|
38
|
+
// If pkgJson.main points to a non-existent file, we continue checking index files
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// Ignore errors (missing package.json, invalid JSON, etc.)
|
|
43
|
+
// Consider warning the user on invalid JSON though?
|
|
44
|
+
}
|
|
45
|
+
// 2. Check index.ts
|
|
46
|
+
const indexTs = join(absolutePath, 'index.ts');
|
|
47
|
+
if (await fileExists(indexTs)) {
|
|
48
|
+
return indexTs;
|
|
49
|
+
}
|
|
50
|
+
// 3. Check index.js
|
|
51
|
+
const indexJs = join(absolutePath, 'index.js');
|
|
52
|
+
if (await fileExists(indexJs)) {
|
|
53
|
+
return indexJs;
|
|
54
|
+
}
|
|
55
|
+
const nameHint = displayName ? ` for function "${displayName}"` : '';
|
|
56
|
+
throw new Error(`Could not determine entry file${nameHint} in directory: ${srcPath}. Looked for package.json#main, index.ts, index.js.`);
|
|
57
|
+
}
|
|
58
|
+
// Should not happen if stat succeeded, but defensively handle
|
|
59
|
+
throw new Error(`Source path is neither a file nor a directory: ${srcPath}`);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Checks if a file exists and is a file.
|
|
63
|
+
*/
|
|
64
|
+
async function fileExists(filePath) {
|
|
65
|
+
try {
|
|
66
|
+
const stats = await stat(filePath);
|
|
67
|
+
return stats.isFile();
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
if (err instanceof Error && 'code' in err && err.code === 'ENOENT') {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
throw err; // Re-throw other errors
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { findFunctionEntryPoint } from './find-entry-point.js';
|
|
2
|
+
export async function shouldBundleFunction(resource) {
|
|
3
|
+
// 1. Explicit configuration takes precedence
|
|
4
|
+
if (typeof resource.bundle === 'boolean') {
|
|
5
|
+
return resource.bundle;
|
|
6
|
+
}
|
|
7
|
+
if (!resource.src) {
|
|
8
|
+
// Cannot determine without a source path
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
// 2. Find the actual entry point
|
|
13
|
+
const entryPoint = await findFunctionEntryPoint(resource.src, resource.displayName ?? resource.name);
|
|
14
|
+
// 3. Check if the resolved entry point is a TypeScript file
|
|
15
|
+
return entryPoint.endsWith('.ts');
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
// If we cannot find the entry point, we cannot determine if it's TS.
|
|
19
|
+
// Log a warning and default to false (don't bundle).
|
|
20
|
+
console.warn(`[warn] Could not determine entry point for function "${resource.displayName ?? resource.name}" while checking if bundling is needed: ${err instanceof Error ? err.message : err}`);
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { InvocationResponse, InvokeContextOptions } from './types.js';
|
|
1
|
+
import type { InvocationResponse, InvokeContextOptions, LocalFunctionResource } from './types.js';
|
|
2
2
|
export declare function sanitizeLogs(logs: string): string;
|
|
3
|
-
export default function invoke(
|
|
3
|
+
export default function invoke(resource: LocalFunctionResource, data: Record<string, unknown> | null, context: InvokeContextOptions, timeout?: number): Promise<InvocationResponse>;
|