✨ add generateUUID, jsonStringify, byte constants, timer utils, throttle to @datadog/js-core/util#4843
Draft
thomas-lebeau wants to merge 2 commits into
Draft
✨ add generateUUID, jsonStringify, byte constants, timer utils, throttle to @datadog/js-core/util#4843thomas-lebeau wants to merge 2 commits into
thomas-lebeau wants to merge 2 commits into
Conversation
…rottle to @datadog/js-core/util Ports the remaining TODO items from the js-core RFC's util table: - generateUUID (electron-sdk: session/view/error/operation/resource event IDs) - jsonStringify (electron-sdk: formatting non-Error values in error paths) - ONE_KIBI_BYTE / ONE_MEBI_BYTE (electron-sdk: batch size constants) - setTimeout/clearTimeout/setInterval/clearInterval + TimeoutId (electron-sdk: batch upload scheduling) - throttle (electron-sdk: throttled view update emission) The timer utilities keep browser-core's Zone.js-unwrapping behavior (safe in Node.js: it only checks for a global `Zone` object before falling back to a plain property lookup) but drop browser-core's implicit monitor() wrapping, since js-core's monitor is a per-consumer factory (createMonitor) rather than a global singleton — callback errors now propagate like a native timer would.
Bundles Sizes Evolution
|
…Stringify, byte constants, and timer Deletes the duplicated implementations now that they live in @datadog/js-core/util, and points every internal consumer plus browser-core's own public re-export in index.ts at the js-core version instead. - generateUUID: removed from tools/utils/stringUtils.ts (other exports there are untouched), all 6 internal call sites updated. - jsonStringify: removed from tools/serialisation/jsonStringify.ts, which now only keeps detachToJsonMethod/ObjectWithToJsonMethod (still used by sanitize.ts). Its now-redundant spec file is deleted since jsonStringify has identical coverage in js-core. - ONE_KIBI_BYTE/ONE_MEBI_BYTE: removed from tools/utils/byteUtils.ts (computeBytesCount/concatBuffers/Uint8ArrayBuffer stay, they're not migrated), 6 internal call sites updated. - timer.ts: rewritten as a thin wrapper. js-core's setTimeout/ clearTimeout/setInterval/clearInterval already bypass Zone.js patching, so the duplicated getZoneJsOriginalValue-based logic is gone; browser-core's own monitor() wrapping is kept on top so callback errors are still caught and reported exactly as before. throttle (functionUtils.ts) needed no changes: it already goes through this same timer.ts wrapper. - Deduplicated two near-identical tests in timer.spec.ts that were colliding by name with js-core's own timer.spec.ts. Downstream packages (browser-rum-core, browser-logs, ...) are unaffected: they still import these from '@datadog/browser-core', which now just re-exports the js-core versions under the hood.
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: 071b24a | Docs | Datadog PR Page | Give us feedback! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Per the
@datadog/js-coreRFC, severalutilsub-path APIs consumed byelectron-sdkwere still marked as TODO:generateUUID,jsonStringify,ONE_KIBI_BYTE/ONE_MEBI_BYTE,setTimeout(and friends), andthrottle.Changes
generateUUID— ported verbatim frombrowser-core/tools/utils/stringUtils.ts(self-contained, no deps).jsonStringify— ported frombrowser-core/tools/serialisation/jsonStringify.ts; itsnoop/detachToJsonMethodhelpers are kept private (not part of the public surface, onlyjsonStringifywas requested).ONE_KIBI_BYTE,ONE_MEBI_BYTE— ported verbatim frombrowser-core/tools/utils/byteUtils.ts(the byte-counting/buffer-concat helpers in that same file were left out — not in scope).setTimeout,clearTimeout,setInterval,clearInterval,TimeoutId— ported frombrowser-core/tools/timer.ts, including the Zone.js-unwrapping behavior (safe in Node: it only probes for a globalZoneobject before falling back to a plain property lookup). Deliberately dropped browser-core's implicitmonitor()wrapping of the callback: js-core'smonitoris a per-consumer factory (createMonitor) rather than a global singleton, so there's no monitor instance to hook into without changing the function signature. Callback errors now propagate like a native timer would — consumers who want error isolation can wrap their callback with their own@datadog/js-core/monitorinstance before passing it in.throttle— ported verbatim frombrowser-core/tools/utils/functionUtils.ts, now built on the localtimer.ts.api/util.api.md(API Extractor golden file) accordingly.Pure additive change (new exports only), no existing behavior changes.
Test instructions
yarn workspace @datadog/js-core build && yarn api:checkpasses.yarn typecheckpasses.yarn test:unit --spec 'packages/js-core/src/**/*.spec.ts'passes (81/81), including new specs forgenerateUUID,jsonStringify,timer, andthrottle(usingjasmine.clock()instead of browser-core's internalmockClocktest helper, which isn't available to js-core).Checklist