-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
165 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const cds = require('@sap/cds') | ||
const LOG = cds.log('telemetry') | ||
|
||
const { Session } = require('inspector') | ||
const { promisify } = require('util') | ||
const { uuid } = cds.utils | ||
|
||
const locations = new WeakMap() | ||
|
||
module.exports = async fn => { | ||
if (locations.has(fn)) return locations.get(fn) | ||
|
||
let session | ||
const script_urls = {} | ||
const random = `__${uuid().replace(/-/g, '_')}__` | ||
try { | ||
global[random] = fn | ||
session = new Session() | ||
session.connect() | ||
session.on('Debugger.scriptParsed', result => { | ||
script_urls[result.params.scriptId] = result.params.url | ||
}) | ||
const session_post = promisify(session.post).bind(session) | ||
await session_post('Debugger.enable') | ||
const { result: { objectId } } = await session_post('Runtime.evaluate', { expression: random }) | ||
const { internalProperties } = await session_post('Runtime.getProperties', { objectId }) | ||
const function_location = internalProperties.find(({ name }) => name === '[[FunctionLocation]]') | ||
const location = { | ||
url: script_urls[function_location.value.value.scriptId], | ||
line: function_location.value.value.lineNumber + 1, | ||
column: function_location.value.value.columnNumber + 1 | ||
} | ||
locations.set(fn, location) | ||
return location | ||
} catch (err) { | ||
LOG.warn(`Unable to locate function "${fn.name}" due to error:`, err) | ||
} finally { | ||
session?.disconnect() | ||
delete global[random] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.