Skip to content

Commit d5e9ecc

Browse files
authored
Merge branch 'main' into bricksroo/html-rewriter-docs-fix
2 parents 0d892ff + 51aad82 commit d5e9ecc

15 files changed

+786
-92
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# mapAndLogError
8+
9+
The **`mapAndLogError()`** function calls `mapError` on an `Error` object and sends the output to standard error. This includes the error name, message, and a call stack.
10+
11+
If `--enable-stack-traces` is specified during the build, the call stack will be mapped using source maps.
12+
13+
If `--enable-stack-traces` is specified and `--exclude-sources` is not specified during the build, then this will also include a code dump of neighboring lines of user code.
14+
15+
## Syntax
16+
17+
```js
18+
mapAndLogError(error)
19+
```
20+
21+
### Parameters
22+
23+
- `error` _: Error _ or _string_
24+
- The error to retrieve information about. If a `string` is provided, then it is first converted to an `Error`.
25+
26+
### Return value
27+
28+
`undefined`.
29+
30+
## Examples
31+
32+
In this example, build the application using the `--enable-stack-traces` flag.
33+
34+
```js
35+
addEventListener('fetch', e => e.respondWith(handler(e)));
36+
async function handler(event) {
37+
try {
38+
throw new TypeError('foo');
39+
} catch (err) {
40+
mapAndLogError(mapError(err));
41+
}
42+
return new Response('ok');
43+
}
44+
```
45+
46+
The following is output to the error log.
47+
48+
```
49+
TypeError: foo
50+
at handler (src/index.ts:4:11)
51+
1 | addEventListener('fetch', e => e.respondWith(handler(e)));
52+
2 | async function handler(event) {
53+
3 | try {
54+
> 4 | throw new TypeError('foo');
55+
^
56+
5 | } catch (err) {
57+
6 | mapAndLogError(mapError(err));
58+
7 | }
59+
at src/index.ts:1:45
60+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# mapError
8+
9+
The **`mapError()`** function extracts information from an `Error` object as a human-readable array of strings. This includes the error name, message, and a call stack.
10+
11+
If `--enable-stack-traces` is specified during the build, the call stack will be mapped using source maps.
12+
13+
If `--enable-stack-traces` is specified and `--exclude-sources` is not specified during the build, then this will also include a code dump of neighboring lines of user code.
14+
15+
## Syntax
16+
17+
```js
18+
mapError(error)
19+
```
20+
21+
### Parameters
22+
23+
- `error` _: Error _ or _string_
24+
- The error to retrieve information about. If a `string` is provided, then it is first converted to an `Error`.
25+
26+
### Return value
27+
28+
Returns an array of `string`s.
29+
30+
## Examples
31+
32+
In this example, build the application using the `--enable-stack-traces` flag.
33+
34+
```js
35+
addEventListener('fetch', e => e.respondWith(handler(e)));
36+
async function handler(event) {
37+
try {
38+
throw new TypeError('foo');
39+
} catch (err) {
40+
console.error(mapError(err));
41+
}
42+
return new Response('ok');
43+
}
44+
```
45+
46+
The following is output to the error log.
47+
48+
```
49+
TypeError: foo
50+
at handler (src/index.ts:4:11)
51+
1 | addEventListener('fetch', e => e.respondWith(handler(e)));
52+
2 | async function handler(event) {
53+
3 | try {
54+
> 4 | throw new TypeError('foo');
55+
^
56+
5 | } catch (err) {
57+
6 | console.error(mapError(err));
58+
7 | }
59+
at src/index.ts:1:45
60+
```

js-compute-runtime-cli.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const {
1212
enableExperimentalHighResolutionTimeMethods,
1313
moduleMode,
1414
bundle,
15+
enableStackTraces,
16+
excludeSources,
17+
debugIntermediateFilesDir,
1518
wasmEngine,
1619
input,
1720
output,
@@ -34,17 +37,20 @@ if (version) {
3437
const { compileApplicationToWasm } = await import(
3538
'./src/compileApplicationToWasm.js'
3639
);
37-
await compileApplicationToWasm(
40+
await compileApplicationToWasm({
3841
input,
3942
output,
4043
wasmEngine,
4144
enableHttpCache,
4245
enableExperimentalHighResolutionTimeMethods,
4346
enableAOT,
4447
aotCache,
48+
enableStackTraces,
49+
excludeSources,
50+
debugIntermediateFilesDir,
4551
moduleMode,
46-
bundle,
52+
doBundle: bundle,
4753
env,
48-
);
54+
});
4955
await addSdkMetadataField(output, enableAOT);
5056
}

package-lock.json

Lines changed: 19 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@
5757
},
5858
"dependencies": {
5959
"@bytecodealliance/jco": "^1.7.0",
60-
"@bytecodealliance/wizer": "^7.0.5",
6160
"@bytecodealliance/weval": "^0.3.2",
61+
"@bytecodealliance/wizer": "^7.0.5",
62+
"@jridgewell/remapping": "^2.3.5",
63+
"@jridgewell/trace-mapping": "^0.3.31",
6264
"acorn": "^8.13.0",
6365
"acorn-walk": "^8.3.4",
6466
"esbuild": "^0.25.0",
6567
"magic-string": "^0.30.12",
68+
"picomatch": "^4.0.3",
6669
"regexpu-core": "^6.1.1"
6770
},
6871
"peerDependencies": {

src/bundle.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
import { rename } from 'node:fs/promises';
2+
import { dirname, basename, resolve } from 'node:path';
3+
import { fileURLToPath } from 'node:url';
14
import { build } from 'esbuild';
25

3-
let fastlyPlugin = {
6+
import { swallowTopLevelExportsPlugin } from './swallowTopLevelExportsPlugin.js';
7+
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = dirname(__filename);
10+
11+
const fastlyPlugin = {
412
name: 'fastly',
513
setup(build) {
614
build.onResolve({ filter: /^fastly:.*/ }, (args) => {
@@ -74,6 +82,8 @@ export const setBaseURL = Object.getOwnPropertyDescriptor(globalThis.fastly, 'ba
7482
export const setDefaultBackend = Object.getOwnPropertyDescriptor(globalThis.fastly, 'defaultBackend').set;
7583
export const allowDynamicBackends = Object.getOwnPropertyDescriptor(globalThis.fastly, 'allowDynamicBackends').set;
7684
export const sdkVersion = globalThis.fastly.sdkVersion;
85+
export const mapAndLogError = (e) => globalThis.__fastlyMapAndLogError(e);
86+
export const mapError = (e) => globalThis.__fastlyMapError(e);
7787
`,
7888
};
7989
}
@@ -143,14 +153,40 @@ export const TransactionCacheEntry = globalThis.TransactionCacheEntry;
143153
},
144154
};
145155

146-
export async function bundle(input, moduleMode = false) {
147-
return await build({
156+
export async function bundle(
157+
input,
158+
outfile,
159+
{ moduleMode = false, enableStackTraces = false },
160+
) {
161+
// Build output file in cwd first to build sourcemap with correct paths
162+
const bundle = resolve(basename(outfile));
163+
164+
const plugins = [fastlyPlugin];
165+
if (moduleMode) {
166+
plugins.push(swallowTopLevelExportsPlugin({ entry: input }));
167+
}
168+
169+
const inject = [];
170+
if (enableStackTraces) {
171+
inject.push(resolve(__dirname, './rsrc/trace-mapping.inject.js'));
172+
}
173+
174+
await build({
148175
conditions: ['fastly'],
149176
entryPoints: [input],
150177
bundle: true,
151-
write: false,
178+
write: true,
179+
outfile: bundle,
180+
sourcemap: 'external',
181+
sourcesContent: true,
152182
format: moduleMode ? 'esm' : 'iife',
153183
tsconfig: undefined,
154-
plugins: [fastlyPlugin],
184+
plugins,
185+
inject,
155186
});
187+
188+
await rename(bundle, outfile);
189+
if (enableStackTraces) {
190+
await rename(bundle + '.map', outfile + '.map');
191+
}
156192
}

0 commit comments

Comments
 (0)