Skip to content

Commit 73d28d1

Browse files
bergundymjameswh
andauthored
Improve regex for extracting source map (#899)
Co-authored-by: James Watkins-Harvey <mjameswh@gmail.com>
1 parent 4b757eb commit 73d28d1

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ jobs:
291291

292292
# Runs the sdk features repo tests with this repo's current SDK code
293293
sdk-features-tests:
294-
uses: temporalio/sdk-features/.github/workflows/typescript.yaml@protoc
294+
uses: temporalio/sdk-features/.github/workflows/typescript.yaml@main
295295
with:
296296
typescript-repo-path: ${{github.event.pull_request.head.repo.full_name}}
297297
typescript-repo-ref: ${{github.event.pull_request.head.ref}}

packages/worker/src/worker.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,11 +1659,7 @@ export interface WorkflowBundleWithSourceMapAndFilename {
16591659
}
16601660

16611661
export function parseWorkflowCode(code: string, codePath?: string): WorkflowBundleWithSourceMapAndFilename {
1662-
const sourceMappingUrlDataRegex = /\s*\n[/][/][#]\s+sourceMappingURL=data:(?:[^,]*;)base64,([0-9A-Za-z+/=]+)\s*$/;
1663-
const sourceMapMatcher = code.match(sourceMappingUrlDataRegex);
1664-
if (!sourceMapMatcher) throw new Error("Can't extract inlined source map from the provided Workflow Bundle");
1665-
1666-
const sourceMapJson = Buffer.from(sourceMapMatcher[1], 'base64').toString();
1662+
const [actualCode, sourceMapJson] = extractSourceMap(code);
16671663
const sourceMap: RawSourceMap = JSON.parse(sourceMapJson);
16681664

16691665
// JS debuggers (at least VSCode's) have a few requirements regarding the script and its source map, notably:
@@ -1675,7 +1671,7 @@ export function parseWorkflowCode(code: string, codePath?: string): WorkflowBund
16751671
sourceMap.file = filename;
16761672
const patchedSourceMapJson = Buffer.from(JSON.stringify(sourceMap)).toString('base64');
16771673
const fixedSourceMappingUrl = `\n//# sourceMappingURL=data:application/json;base64,${patchedSourceMapJson}`;
1678-
code = code.slice(0, -sourceMapMatcher[1].length) + fixedSourceMappingUrl;
1674+
code = actualCode + fixedSourceMappingUrl;
16791675
}
16801676

16811677
// Preloading the script makes breakpoints significantly more reliable and more responsive
@@ -1697,6 +1693,20 @@ export function parseWorkflowCode(code: string, codePath?: string): WorkflowBund
16971693
return { code, sourceMap, filename };
16981694
}
16991695

1696+
function extractSourceMap(code: string) {
1697+
const sourceMapCommentPos = code.lastIndexOf('//# sourceMappingURL=data:');
1698+
if (sourceMapCommentPos > 0) {
1699+
const base64TagIndex = code.indexOf('base64,', sourceMapCommentPos);
1700+
if (base64TagIndex > 0) {
1701+
const sourceMapJson = Buffer.from(code.slice(base64TagIndex + 'base64,'.length).trimEnd(), 'base64').toString();
1702+
const actualCode = code.slice(0, sourceMapCommentPos);
1703+
return [actualCode, sourceMapJson];
1704+
}
1705+
}
1706+
1707+
throw new Error("Can't extract inlined source map from the provided Workflow Bundle");
1708+
}
1709+
17001710
type NonNullableObject<T> = { [P in keyof T]-?: NonNullable<T[P]> };
17011711

17021712
/**

0 commit comments

Comments
 (0)