Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@keiser/metrics-sdk
Advanced tools
This SDK facilitates communication between a client system (ie: phone app, website, server) and Keiser Metrics. The SDK is written in TypeScript and supports both browser and NodeJS platforms.
For a more information about this SDK visit the Keiser Developer Zone.
To play around with the SDK try out the Keiser Metrics SDK REPL.
To see the full SDK API view the Keiser Metrics SDK Documentation.
Install with npm: npm install @keiser/metrics-sdk
Import the SDK package root class and instantiate a Metrics
connection instance. Each instance maintains a connection to the Keiser Metrics servers so only one instance should be created per an application.
This instance handles multiplexing requests, throttling, and request retries automatically. The default implementation for browsers uses a WebSocket connection, but it will fall back to a normal HTTP request strategy if WebSockets are not supported. The NodeJS implementation will always use HTTP requests.
import Metrics from '@keiser/metrics-sdk'
const metrics = new Metrics()
The base Metrics
instance is a connection handler with access to only limited information. To access user specific information a UserSession
must be created by authenticating through one of the available mechanisms:
authenticateWithCredentials
- Use email and passwordauthenticateWithFacebook
- Use Facebook OAuth flowauthenticateWithGoogle
- Use Google OAuth flowauthenticateWithToken
- Use a stored refresh token stringconst userSession = await metrics.authenticateWithCredentials({email: 'demo@keiser.com', password: 'password'})
The result of an authentication request is a UserSession
which contains methods for interacting with the user's data as well as mechanisms for controlling the session.
To log-out a user, call teh logout()
method on the UserSession
.
await userSession.logout()
To restore an authenticated session, store the refresh token string and use the authenticateWithToken
authentication mechanism to restore the session. The refresh token needs to be stored in a secure place that will persist between sessions (ie: Local Storage) and is valid for 90 days from it's creation.
const refreshTokenKey = 'refreshToken'
const userSession = await metrics.authenticateWithCredentials({email: 'demo@keiser.com', password: 'password'})
localStorage.setItem(refreshTokenKey, userSession.refreshToken)
userSession.onRefreshTokenChangeEvent.subscribe(({refreshToken}) => {
// Will update token in local storage each time it is updated
localStorage.setItem(refreshTokenKey, refreshToken)
})
// On next application start
const refreshToken = localStorage.getItem(refreshTokenKey)
const userSession = await metrics.authenticateWithToken({token: refreshToken})
The UserSession
instance contains a user
property accessor for the authenticated user's User
class.
const userSession = await metrics.authenticateWithCredentials({email: 'demo@keiser.com', password: 'password'})
console.log(userSession.user.profile.name)
All properties exposed by the User
class and it's children are instances that are generated on access, so subsequent calls to the same property are not returning the exact same instance. It is recommended to keep accessed instances in scope using local references. This prevents memory leaks and improves performance when dealing with large data sets.
This means that separate instances will also be out of sync as changes to one instance will not be reflected in other instances. The reload()
method available on most classes will bring the instance in sync with the current server state.
let userProfile1 = userSession.user.profile
let userProfile2 = userSession.user.profile
console.log(userProfile1 === userProfile2) // Output: false
console.log(userProfile1.name === userProfile2.name) // Output: true
await userProfile1.update({name: 'Pickle Rick'})
console.log(userProfile1 === userProfile2) // Output: false
console.log(userProfile1.name === userProfile2.name) // Output: false
await userProfile2.reload()
console.log(userProfile1 === userProfile2) // Output: false
console.log(userProfile1.name === userProfile2.name) // Output: true
// Recommended usage example
function generateUsername(user: User) {
const name = user.profile.name
return name ? name.replace(/\s/, '_').toLowerCase() : 'unknown_username'
}
All errors are handled by throwing inside the method call with the expectation of a try/catch
to catch the error.
All errors will be thrown as a typed error instance corresponding to the reason for the error, with the global Error
as the base instance, and an intermediate category type inheritance (for easier bucketing).
let userSession
try {
userSession = await metrics.authenticateWithCredentials({email: 'demo@keiser.com', password: 'wrongPassword'})
} catch (error) {
if (error instanceof RequestError) {
if (error instanceof InvalidCredentialsError) {
this.userCredentialsIncorrect()
} else if (error instanceof ValidationError) {
this.userCredentialsValidationError()
}
} else if (error instanceof ServerError) {
this.serverDown()
}
}
Name | Reason |
---|---|
Request | Issue with the parameters provided for the request |
Session | Issue with the session instance (session is no longer valid) |
Server | Issue with the server (potentially overloaded or offline) |
Connection | Issue with connection to server |
Name | Category | Reason |
---|---|---|
Missing Params | Request | Parameters are missing from action (potentially null or undefined ) |
Invalid Credentials | Request | Invalid login credentials (don't match any active user) |
Validation | Request | Parameters are present but do not pass validation |
Unknown Entity | Request | Request target does not exist (deleted or never existed) |
Duplicate Entity | Request | Cannot create a new instance because identical one exists |
Unauthorized Resource | Request | Insufficient permissions to access the target |
Action Prevented | Request | Request cannot be performed for reason other than those above (edge cases) |
Facility Access Control | Request | Request is prevented due to facility access control limitations |
The base Metrics
instance maintains an active connection until it is disposed, so it is recommended to dispose the connection by calling dispose()
once the connection is no longer needed.
metrics.dispose()
Copyright © 2020 Keiser Corporation.
The Keiser Metrics SDK source code and distributed package are made available through the MIT license.
Using any of the APIs made available through the Keiser Metrics SDK to communicate with Keiser Metrics make you subject to the following agreements. Please read all documents in their entirety as they govern your use of the APIs and Keiser Metrics servers.
FAQs
Keiser Metrics SDK
The npm package @keiser/metrics-sdk receives a total of 1 weekly downloads. As such, @keiser/metrics-sdk popularity was classified as not popular.
We found that @keiser/metrics-sdk demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.