-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathsentryMetroSerializer.ts
199 lines (174 loc) · 7.05 KB
/
sentryMetroSerializer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import * as crypto from 'crypto';
// eslint-disable-next-line import/no-extraneous-dependencies
import type { MixedOutput, Module, ReadOnlyGraph } from 'metro';
// eslint-disable-next-line import/no-extraneous-dependencies
import * as countLines from 'metro/src/lib/countLines';
import type { Bundle, MetroSerializer, MetroSerializerOutput, SerializedBundle, VirtualJSOutput } from './utils';
import { createDebugIdSnippet, createSet, determineDebugIdFromBundleSource, stringToUUID } from './utils';
import { createDefaultMetroSerializer } from './vendor/metro/utils';
type SourceMap = Record<string, unknown>;
const DEBUG_ID_PLACE_HOLDER = '__debug_id_place_holder__';
const DEBUG_ID_MODULE_PATH = '__debugid__';
const PRELUDE_MODULE_PATH = '__prelude__';
const SOURCE_MAP_COMMENT = '//# sourceMappingURL=';
const DEBUG_ID_COMMENT = '//# debugId=';
/**
* Adds Sentry Debug ID polyfill module to the bundle.
*/
export function unstable_beforeAssetSerializationPlugin({
premodules,
debugId,
}: {
graph: ReadOnlyGraph<MixedOutput>;
premodules: Module[];
debugId?: string;
}): Module[] {
if (!debugId) {
return premodules;
}
const debugIdModuleExists = premodules.findIndex(module => module.path === DEBUG_ID_MODULE_PATH) != -1;
if (debugIdModuleExists) {
// eslint-disable-next-line no-console
console.warn('\n\nDebug ID module found. Skipping Sentry Debug ID module...\n\n');
return premodules;
}
const debugIdModule = createDebugIdModule(debugId);
return [...addDebugIdModule(premodules, debugIdModule)];
}
/**
* Creates a Metro serializer that adds Debug ID module to the plain bundle.
* The Debug ID module is a virtual module that provides a debug ID in runtime.
*
* RAM Bundles do not support custom serializers.
*/
export const createSentryMetroSerializer = (customSerializer?: MetroSerializer): MetroSerializer => {
const serializer = customSerializer || createDefaultMetroSerializer();
return async function (entryPoint, preModules, graph, options) {
if (graph.transformOptions.hot) {
return serializer(entryPoint, preModules, graph, options);
}
const debugIdModuleExists = preModules.findIndex(module => module.path === DEBUG_ID_MODULE_PATH) != -1;
if (debugIdModuleExists) {
// eslint-disable-next-line no-console
console.warn('Debug ID module found. Skipping Sentry Debug ID module...');
return serializer(entryPoint, preModules, graph, options);
}
const debugIdModule = createDebugIdModule(DEBUG_ID_PLACE_HOLDER);
options.sentryBundleCallback = createSentryBundleCallback(debugIdModule);
const modifiedPreModules = addDebugIdModule(preModules, debugIdModule);
// Run wrapped serializer
const serializerResult = serializer(entryPoint, modifiedPreModules, graph, options);
const { code: bundleCode, map: bundleMapString } = await extractSerializerResult(serializerResult);
// Add debug id comment to the bundle
const debugId = determineDebugIdFromBundleSource(bundleCode);
if (!debugId) {
throw new Error(
'Debug ID was not found in the bundle. Call `options.sentryBundleCallback` if you are using a custom serializer.',
);
}
// Only print debug id for command line builds => not hot reload from dev server
// eslint-disable-next-line no-console
console.log('info ' + `Bundle Debug ID: ${debugId}`);
const debugIdComment = `${DEBUG_ID_COMMENT}${debugId}`;
const indexOfSourceMapComment = bundleCode.lastIndexOf(SOURCE_MAP_COMMENT);
const bundleCodeWithDebugId =
indexOfSourceMapComment === -1
? // If source map comment is missing lets just add the debug id comment
`${bundleCode}\n${debugIdComment}`
: // If source map comment is present lets add the debug id comment before it
`${bundleCode.substring(0, indexOfSourceMapComment) + debugIdComment}\n${bundleCode.substring(
indexOfSourceMapComment,
)}`;
const bundleMap: SourceMap = JSON.parse(bundleMapString);
// For now we write both fields until we know what will become the standard - if ever.
bundleMap['debug_id'] = debugId;
bundleMap['debugId'] = debugId;
return {
code: bundleCodeWithDebugId,
map: JSON.stringify(bundleMap),
};
};
};
/**
* This function is expected to be called after serializer creates the final bundle object
* and before the source maps are generated.
*
* It injects a debug ID into the bundle and returns the modified bundle.
*
* Access it via `options.sentryBundleCallback` in your custom serializer.
*/
function createSentryBundleCallback(debugIdModule: Module<VirtualJSOutput> & { setSource: (code: string) => void }) {
return (bundle: Bundle) => {
const debugId = calculateDebugId(bundle);
debugIdModule.setSource(injectDebugId(debugIdModule.getSource().toString(), debugId));
bundle.pre = injectDebugId(bundle.pre, debugId);
return bundle;
};
}
function addDebugIdModule(
preModules: readonly Module<MixedOutput>[],
debugIdModule: Module<VirtualJSOutput>,
): readonly Module<MixedOutput>[] {
const modifiedPreModules = [...preModules];
if (
modifiedPreModules.length > 0 &&
modifiedPreModules[0] !== undefined &&
modifiedPreModules[0].path === PRELUDE_MODULE_PATH
) {
// prelude module must be first as it measures the bundle startup time
modifiedPreModules.unshift(preModules[0] as Module<VirtualJSOutput>);
modifiedPreModules[1] = debugIdModule;
} else {
modifiedPreModules.unshift(debugIdModule);
}
return modifiedPreModules;
}
async function extractSerializerResult(serializerResult: MetroSerializerOutput): Promise<SerializedBundle> {
if (typeof serializerResult === 'string') {
return { code: serializerResult, map: '{}' };
}
if ('map' in serializerResult) {
return { code: serializerResult.code, map: serializerResult.map };
}
const awaitedResult = await serializerResult;
if (typeof awaitedResult === 'string') {
return { code: awaitedResult, map: '{}' };
}
return { code: awaitedResult.code, map: awaitedResult.map };
}
function createDebugIdModule(debugId: string): Module<VirtualJSOutput> & { setSource: (code: string) => void } {
let debugIdCode = createDebugIdSnippet(debugId);
return {
setSource: (code: string) => {
debugIdCode = code;
},
dependencies: new Map(),
getSource: () => Buffer.from(debugIdCode),
inverseDependencies: createSet(),
path: DEBUG_ID_MODULE_PATH,
output: [
{
type: 'js/script/virtual',
data: {
code: debugIdCode,
lineCount: countLines(debugIdCode),
map: [],
},
},
],
};
}
function calculateDebugId(bundle: Bundle): string {
const hash = crypto.createHash('md5');
hash.update(bundle.pre);
for (const [, code] of bundle.modules) {
hash.update(code);
}
hash.update(bundle.post);
const debugId = stringToUUID(hash.digest('hex'));
return debugId;
}
function injectDebugId(code: string, debugId: string): string {
// eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor
return code.replace(new RegExp(DEBUG_ID_PLACE_HOLDER, 'g'), debugId);
}