Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"./lib/es-utils/reduce": "./src/lib/es-utils/reduce.js",
"./lib/es-utils/keys": "./src/lib/es-utils/keys.js",
"./lib/derecursify": "./src/lib/derecursify.js",
"./lib/metadata-delegate": "./src/lib/metadata-delegate.js",
"./lib/callback-runner": "./src/lib/callback-runner.js",
"./lib/path-normalizer": "./src/lib/path-normalizer.js",
"./lib/validators/string-with-length": "./src/lib/validators/string-with-length.js",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ export default class Client<T extends Config = Config> {
}
}

addMetadata (section: string, keyOrObj: any, maybeVal?: any) {
addMetadata (section: string, keyOrObj: object | string, maybeVal?: any) {
return metadataDelegate.add(this._metadata, section, keyOrObj, maybeVal)
}

getMetadata (section: string, key?: any) {
getMetadata (section: string, key?: string) {
return metadataDelegate.get(this._metadata, section, key)
}

clearMetadata (section: string, key?: any) {
clearMetadata (section: string, key?: string) {
return metadataDelegate.clear(this._metadata, section, key)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class Event {
/* this.attemptImmediateDelivery, default: true */
}

addMetadata (section: string, keyOrObj?: any, maybeVal?: any) {
addMetadata (section: string, keyOrObj?: object | string, maybeVal?: any) {
return metadataDelegate.add(this._metadata, section, keyOrObj, maybeVal)
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { default as cloneClient } from './lib/clone-client'
export { default as jsonPayload } from './lib/json-payload'
export { default as intRange } from './lib/validators/int-range'
export { default as isError } from './lib/iserror'
export { default as metadataDelegate } from './lib/metadata-delegate'
export { default as nodeFallbackStack } from './lib/node-fallback-stack'
export { default as runSyncCallbacks } from './lib/sync-callback-runner'

Expand Down
60 changes: 0 additions & 60 deletions packages/core/src/lib/metadata-delegate.js

This file was deleted.

70 changes: 70 additions & 0 deletions packages/core/src/lib/metadata-delegate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import assign from './es-utils/assign'

interface MetadataDelegate {
add: (state: { [key: string]: any }, section: string, keyOrObj?: object | string, maybeVal?: any) => any
get: (state: { [key: string]: any }, section: string, key?: string) => any
clear: (state: { [key: string]: any }, section: string, key?: string) => any
}

const metadataDelegate: MetadataDelegate = {
add: (state, section, keyOrObj, maybeVal) => {
if (!section) return
let updates

// addMetadata("section", null) -> clears section
if (keyOrObj === null) return metadataDelegate.clear(state, section);

// normalise the two supported input types into object form
if (typeof keyOrObj === 'object') updates = keyOrObj
if (typeof keyOrObj === 'string') updates = { [keyOrObj]: maybeVal }

// exit if we don't have an updates object at this point
if (!updates) return

// preventing the __proto__ property from being used as a key
if (section === '__proto__' || section === 'constructor' || section === 'prototype') {
return
}

// ensure a section with this name exists
if (!state[section]) state[section] = {}

// merge the updates with the existing section
state[section] = assign({}, state[section], updates)
},

get: (state, section, key) => {
if (typeof section !== 'string') return undefined
if (!key) {
return state[section]
}
if (state[section]) {
return state[section][key]
}
return undefined
},

clear: (state, section, key) => {
if (typeof section !== 'string') return

// clear an entire section
if (!key) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete state[section]
return
}

// preventing the __proto__ property from being used as a key
if (section === '__proto__' || section === 'constructor' || section === 'prototype') {
return
}

// clear a single value from a section
if (state[section]) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete state[section][key]
}
}
}

export default metadataDelegate;
6 changes: 3 additions & 3 deletions packages/core/test/metadata-delegate.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { add, clear } from '../src/lib/metadata-delegate'
import metadataDelegate from '../src/lib/metadata-delegate'

// it doesn't seem easy or even impossible to check whether __proto__ keys can be overwritten
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
Expand All @@ -17,7 +17,7 @@ describe('metadata delegate', () => {
}
])('should not add $key keys', ({ key, expected }) => {
const state = {}
add(state, key, 'foo', 'bar')
metadataDelegate.add(state, key, "foo", "bar");
expect(state).toEqual(expected)
})
})
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('metadata delegate', () => {
}
}
])('should not overwrite $key keys', ({ key, state, expected }) => {
clear(state, key, 'foo')
metadataDelegate.clear(state, key, "foo");
expect(state).toEqual(expected)
})
})
Expand Down