Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a rum-core package #673

Merged
merged 3 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/core/src/boot/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { areCookiesAuthorized, CookieOptions } from '../browser/cookie'
import { buildConfiguration, UserConfiguration } from '../domain/configuration'
import { setDebugMode, startInternalMonitoring } from '../domain/internalMonitoring'

export function makeGlobal<T>(stub: T): T & { onReady(callback: () => void): void } {
const global = {
export function makePublicApi<T>(stub: T): T & { onReady(callback: () => void): void } {
const publicApi = {
...stub,

// This API method is intentionally not monitored, since the only thing executed is the
Expand All @@ -16,14 +16,14 @@ export function makeGlobal<T>(stub: T): T & { onReady(callback: () => void): voi

// Add an "hidden" property to set debug mode. We define it that way to hide it
// as much as possible but of course it's not a real protection.
Object.defineProperty(global, '_setDebug', {
Object.defineProperty(publicApi, '_setDebug', {
get() {
return setDebugMode
},
enumerable: false,
})

return global
return publicApi
}

export function defineGlobal<Global, Name extends keyof Global>(global: Global, name: Name, api: Global[Name]) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export {
BuildMode,
Datacenter,
defineGlobal,
makeGlobal,
makePublicApi,
commonInit,
checkCookiesAuthorized,
checkIsNotLocalFile,
Expand Down
18 changes: 9 additions & 9 deletions packages/logs/src/boot/logs.entry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, monitor, ONE_SECOND } from '@datadog/browser-core'

import { HandlerType, LogsMessage, StatusType } from '../domain/logger'
import { LogsGlobal, makeLogsGlobal, StartLogs } from './logs.entry'
import { LogsPublicApi, makeLogsPublicApi, StartLogs } from './logs.entry'

const DEFAULT_INIT_CONFIGURATION = { clientToken: 'xxx' }

Expand All @@ -28,17 +28,17 @@ describe('logs entry', () => {
startLogsGetGlobalContext = undefined
})

it('should set global with init', () => {
const LOGS = makeLogsGlobal(startLogs)
it('should define the public API with init', () => {
const LOGS = makeLogsPublicApi(startLogs)
expect(!!LOGS).toEqual(true)
expect(!!LOGS.init).toEqual(true)
})

describe('configuration validation', () => {
let LOGS: LogsGlobal
let LOGS: LogsPublicApi

beforeEach(() => {
LOGS = makeLogsGlobal(startLogs)
LOGS = makeLogsPublicApi(startLogs)
})

it('init should log an error with no public api key', () => {
Expand Down Expand Up @@ -123,10 +123,10 @@ describe('logs entry', () => {
})

describe('pre-init API usages', () => {
let LOGS: LogsGlobal
let LOGS: LogsPublicApi

beforeEach(() => {
LOGS = makeLogsGlobal(startLogs)
LOGS = makeLogsPublicApi(startLogs)
jasmine.clock().install()
jasmine.clock().mockDate()
})
Expand Down Expand Up @@ -196,10 +196,10 @@ describe('logs entry', () => {
})

describe('post-init API usages', () => {
let LOGS: LogsGlobal
let LOGS: LogsPublicApi

beforeEach(() => {
LOGS = makeLogsGlobal(startLogs)
LOGS = makeLogsPublicApi(startLogs)
LOGS.init(DEFAULT_INIT_CONFIGURATION)
})

Expand Down
12 changes: 6 additions & 6 deletions packages/logs/src/boot/logs.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
defineGlobal,
getGlobalObject,
isPercentage,
makeGlobal,
makePublicApi,
monitor,
UserConfiguration,
} from '@datadog/browser-core'
Expand All @@ -26,18 +26,18 @@ export interface LoggerConfiguration {
context?: object
}

export type LogsGlobal = ReturnType<typeof makeLogsGlobal>
export type LogsPublicApi = ReturnType<typeof makeLogsPublicApi>

export const datadogLogs = makeLogsGlobal(startLogs)
export const datadogLogs = makeLogsPublicApi(startLogs)

interface BrowserWindow extends Window {
DD_LOGS?: LogsGlobal
DD_LOGS?: LogsPublicApi
}
defineGlobal(getGlobalObject<BrowserWindow>(), 'DD_LOGS', datadogLogs)

export type StartLogs = typeof startLogs

export function makeLogsGlobal(startLogsImpl: StartLogs) {
export function makeLogsPublicApi(startLogsImpl: StartLogs) {
let isAlreadyInitialized = false

const globalContextManager = createContextManager()
Expand All @@ -49,7 +49,7 @@ export function makeLogsGlobal(startLogsImpl: StartLogs) {
}
const logger = new Logger(sendLog)

return makeGlobal({
return makePublicApi({
logger,

init: monitor((userConfiguration: LogsUserConfiguration) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/logs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { Logger, LogsMessage, StatusType, HandlerType } from './domain/logger'
export { LogsUserConfiguration, LoggerConfiguration, LogsGlobal, datadogLogs } from './boot/logs.entry'
export { LogsUserConfiguration, LoggerConfiguration, LogsPublicApi as LogsGlobal, datadogLogs } from './boot/logs.entry'
export { LogsEvent } from './logsEvent.types'
4 changes: 4 additions & 0 deletions packages/rum-core/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
!/cjs/**/*
!/esm/**/*
!/src/**/*
2 changes: 2 additions & 0 deletions packages/rum-core/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
save-exact true

3 changes: 3 additions & 0 deletions packages/rum-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `rum-core`

Datadog browser RUM core utilities.
26 changes: 26 additions & 0 deletions packages/rum-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@datadog/browser-rum-core",
"version": "2.1.2",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
"types": "cjs/index.d.ts",
"scripts": {
"build": "run-p build:cjs build:esm",
"build:cjs": "rm -rf cjs && tsc -p tsconfig.cjs.json && yarn replace-build-env cjs",
"build:esm": "rm -rf esm && tsc -p tsconfig.esm.json && yarn replace-build-env esm",
"replace-build-env": "node ../../scripts/replace-build-env.js"
},
"dependencies": {
"@datadog/browser-core": "2.1.2",
"tslib": "^1.10.0"
},
"devDependencies": {
"ajv": "6.12.6"
},
"repository": {
"type": "git",
"url": "https://github.com/DataDog/browser-sdk.git",
"directory": "packages/rum-core"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { CommonContext } from '../rawRumEvent.types'
import { startRumBatch } from '../transport/batch'

import { buildEnv } from './buildEnv'
import { RumUserConfiguration } from './rum.entry'
import { RumUserConfiguration } from './rumPublicApi'

export function startRum(userConfiguration: RumUserConfiguration, getCommonContext: () => CommonContext) {
const lifeCycle = new LifeCycle()
Expand Down
Loading