Socket
Socket
Sign inDemoInstall

scrubbr

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

scrubbr - npm Package Compare versions

Comparing version 0.0.1-alpha.8 to 0.0.1-alpha.9

7

dist/helpers.d.ts

@@ -6,5 +6,8 @@ /**

* @example
* // Convert all `User` types to `RestrictedUser`
* scrubbr.typeSerializer('User', (data, state) => useType('RestrictedUser'));
* ```
* // Convert all User types to PublicUser
* scrubbr.typeSerializer('User', (data, state) =\> useType('PublicUser'));
* ```
*
* @public
*/

@@ -11,0 +14,0 @@ export declare function useType(typeName: string): UseType;

@@ -9,5 +9,8 @@ "use strict";

* @example
* // Convert all `User` types to `RestrictedUser`
* scrubbr.typeSerializer('User', (data, state) => useType('RestrictedUser'));
* ```
* // Convert all User types to PublicUser
* scrubbr.typeSerializer('User', (data, state) =\> useType('PublicUser'));
* ```
*
* @public
*/

@@ -14,0 +17,0 @@ function useType(typeName) {

import { ScrubbrOptions } from './';
/**
* The scrubbr log level.
* Setting the log level will enabled logging for that level and below.
*/
export declare enum LogLevel {
/**
* Log additional debug information.
* Set logNesting to true to also indent the logs with the data nesting.
*/
DEBUG = 4,
/**
* Display basic information as well as warnings and errors.
*/
INFO = 3,
/**
* Log warnings and errors.
*/
WARN = 2,
/**
* Log errors
*/
ERROR = 1,
/**
* Disable logging.
*/
NONE = 0
}
/**
* Used by Scrubbr for logging.
* @internal
*/
export declare class Logger {

@@ -10,0 +34,0 @@ logLevel: LogLevel;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = exports.LogLevel = void 0;
/**
* The scrubbr log level.
* Setting the log level will enabled logging for that level and below.
*/
var LogLevel;
(function (LogLevel) {
/**
* Log additional debug information.
* Set logNesting to true to also indent the logs with the data nesting.
*/
LogLevel[LogLevel["DEBUG"] = 4] = "DEBUG";
/**
* Display basic information as well as warnings and errors.
*/
LogLevel[LogLevel["INFO"] = 3] = "INFO";
/**
* Log warnings and errors.
*/
LogLevel[LogLevel["WARN"] = 2] = "WARN";
/**
* Log errors
*/
LogLevel[LogLevel["ERROR"] = 1] = "ERROR";
/**
* Disable logging.
*/
LogLevel[LogLevel["NONE"] = 0] = "NONE";
})(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
/**
* Used by Scrubbr for logging.
* @internal
*/
var Logger = /** @class */ (function () {

@@ -13,0 +37,0 @@ function Logger(options, nestLevel) {

@@ -15,3 +15,3 @@ import { JSONSchema7 } from 'json-schema';

*/
constructor(schema: string | JSONSchemaDefinitions, options: ScrubbrOptions);
constructor(schema: string | JSONSchemaDefinitions, options?: ScrubbrOptions);
/**

@@ -92,2 +92,3 @@ * Create a duplicate version of this scrubbr with all the same serializers, global context, and options.

* @param options - Set options for just this serialization.
* @public
*/

@@ -97,3 +98,3 @@ serialize<Type = any>(schemaType: string, data: object, context?: object, options?: ScrubbrOptions): Promise<Type>;

* Traverse into a node of data on an object to serialize.
* @param node: The data object to start from
* @param node - The data object to start from
* @param state - The serializing state.

@@ -110,3 +111,3 @@ */

* Serialize all the items of an array.
* @param node[] - The array to serialize
* @param node - The array to serialize
* @param state - The serializing state.

@@ -113,0 +114,0 @@ */

@@ -88,2 +88,3 @@ "use strict";

function Scrubbr(schema, options) {
if (options === void 0) { options = {}; }
this.schema = {};

@@ -93,3 +94,3 @@ this.typeSerializers = new Map();

this.globalContext = {};
this.options = __assign(__assign({}, defaultOptions), (options || {}));
this.options = __assign(__assign({}, defaultOptions), options);
this.logger = new Logger_1.Logger(options);

@@ -238,2 +239,3 @@ this.loadSchema(schema);

* @param options - Set options for just this serialization.
* @public
*/

@@ -244,3 +246,3 @@ Scrubbr.prototype.serialize = function (schemaType, data, context, options) {

return __awaiter(this, void 0, void 0, function () {
var schema, state, cloned, serialized;
var schema, state, clonedData, serialized;
return __generator(this, function (_a) {

@@ -254,13 +256,13 @@ switch (_a.label) {

this.logger.info("Serializing data with TS type: '" + schemaType + "'");
// Merge contexts
context = __assign(__assign({}, this.globalContext), context);
this.logger.debug("Using context: '" + JSON.stringify(context, null, ' ') + "'");
// Create state
options = __assign(__assign({}, this.options), options);
state = new ScrubbrState_1.ScrubbrState(data, schema, options, context);
cloned = JSON.parse(JSON.stringify(data));
// Merge contexts
context = __assign(__assign({}, this.globalContext), context);
this.logger.debug("Using context: '" + JSON.stringify(context, null, ' ') + "'");
// Serialize
state.rootSchemaType = schemaType;
state.schemaType = schemaType;
return [4 /*yield*/, this.walkData(cloned, state)];
clonedData = JSON.parse(JSON.stringify(data));
return [4 /*yield*/, this.walkData(clonedData, state)];
case 1:

@@ -275,3 +277,3 @@ serialized = (_a.sent());

* Traverse into a node of data on an object to serialize.
* @param node: The data object to start from
* @param node - The data object to start from
* @param state - The serializing state.

@@ -347,3 +349,3 @@ */

* Serialize all the items of an array.
* @param node[] - The array to serialize
* @param node - The array to serialize
* @param state - The serializing state.

@@ -350,0 +352,0 @@ */

@@ -6,2 +6,3 @@ import { JSONSchema7 } from 'json-schema';

* Serializer function called for a specific TypeScript type.
* @public
*/

@@ -11,2 +12,3 @@ export declare type TypeSerializer = (data: any, state: ScrubbrState) => any | Promise<any>;

* Serializer called for each node within the data object that is being serialized.
* @public
*/

@@ -20,3 +22,4 @@ export declare type GenericSerializer = (data: any, state: ScrubbrState) => any | Promise<any>;

* Set the logger level: LogLevel.NONE, LogLevel.ERROR, LogLevel.WARN, LogLevel.INFO, LogLevel.DEBUG
* @default LogLevel.NONE
* @defaultValue LogLevel.NONE
* @public
*/

@@ -27,3 +30,4 @@ logLevel?: LogLevel;

* This is most useful when logLevel is set to LogLevel.DEBUG.
* @default false
* @defaultValue false
* @public
*/

@@ -33,3 +37,4 @@ logNesting?: boolean | string;

* A string prefix you want to precede all log messages
* @default ""
* @defaultValue ""
* @public
*/

@@ -40,3 +45,4 @@ logPrefix?: string;

* Otherwise, the error will just be logged if the log level is set to LogLevel.ERROR or above.
* @default true
* @defaultValue true
* @public
*/

@@ -51,2 +57,3 @@ throwOnError?: boolean;

* (i.e. no external references)
* @public
*/

@@ -53,0 +60,0 @@ export declare type JSONSchemaDefinitions = JSONSchema7 | {

# Scrubbr
Serializing data in node shouldn't be hard. You shouldn't have to worry about accidentally leaking private information through your APIs. If you're using TypeScript, you already have everythign you need to effortlessly serialize and transform your data.
Serializing data in node shouldn't be hard. You shouldn't have to worry about accidentally leaking private information through your APIs.
If you're using TypeScript, you already have everythign you need to effortlessly serialize and transform your data.
![](images/example.png)

@@ -6,0 +8,0 @@

{
"name": "scrubbr",
"version": "0.0.1-alpha.8",
"version": "0.0.1-alpha.9",
"description": "Serialize and sanitize JSON data using TypeScript.",

@@ -5,0 +5,0 @@ "repository": "https://github.com/jgillick/scrubbr",

@@ -6,5 +6,8 @@ /**

* @example
* // Convert all `User` types to `RestrictedUser`
* scrubbr.typeSerializer('User', (data, state) => useType('RestrictedUser'));
* ```
* // Convert all User types to PublicUser
* scrubbr.typeSerializer('User', (data, state) =\> useType('PublicUser'));
* ```
*
* @public
*/

@@ -11,0 +14,0 @@ export function useType(typeName: string): UseType {

import { ScrubbrOptions } from './';
/**
* The scrubbr log level.
* Setting the log level will enabled logging for that level and below.
*/
export enum LogLevel {
/**
* Log additional debug information.
* Set logNesting to true to also indent the logs with the data nesting.
*/
DEBUG = 4,
/**
* Display basic information as well as warnings and errors.
*/
INFO = 3,
/**
* Log warnings and errors.
*/
WARN = 2,
/**
* Log errors
*/
ERROR = 1,
/**
* Disable logging.
*/
NONE = 0,
}
/**
* Used by Scrubbr for logging.
* @internal
*/
export class Logger {

@@ -15,5 +43,5 @@ logLevel: LogLevel;

nestingString: string | boolean = false;
nestLevel: number = 0;
nestLevel = 0;
constructor(options: ScrubbrOptions, nestLevel: number = 0) {
constructor(options: ScrubbrOptions, nestLevel = 0) {
this.logLevel = options.logLevel || LogLevel.NONE;

@@ -20,0 +48,0 @@ this.nestLevel = nestLevel;

@@ -39,7 +39,7 @@ import * as fs from 'fs';

schema: string | JSONSchemaDefinitions,
options: ScrubbrOptions
options: ScrubbrOptions = {}
) {
this.options = {
...defaultOptions,
...(options || {}),
...options,
};

@@ -204,2 +204,3 @@ this.logger = new Logger(options);

* @param options - Set options for just this serialization.
* @public
*/

@@ -218,2 +219,9 @@ async serialize<Type = any>(

// Merge contexts
context = {
...this.globalContext,
...context,
}
this.logger.debug(`Using context: '${JSON.stringify(context, null, ' ')}'`);
// Create state

@@ -231,16 +239,7 @@ options = {

// Validate JSON and clone
const cloned = JSON.parse(JSON.stringify(data));
// Merge contexts
context = {
...this.globalContext,
...context,
}
this.logger.debug(`Using context: '${JSON.stringify(context, null, ' ')}'`);
// Serialize
state.rootSchemaType = schemaType;
state.schemaType = schemaType;
const serialized = (await this.walkData(cloned, state)) as Type;
const clonedData = JSON.parse(JSON.stringify(data));
const serialized = (await this.walkData(clonedData, state)) as Type;
return serialized;

@@ -251,3 +250,3 @@ }

* Traverse into a node of data on an object to serialize.
* @param node: The data object to start from
* @param node - The data object to start from
* @param state - The serializing state.

@@ -305,3 +304,3 @@ */

* Serialize all the items of an array.
* @param node[] - The array to serialize
* @param node - The array to serialize
* @param state - The serializing state.

@@ -308,0 +307,0 @@ */

@@ -7,2 +7,3 @@ import { JSONSchema7 } from 'json-schema';

* Serializer function called for a specific TypeScript type.
* @public
*/

@@ -16,2 +17,3 @@ export type TypeSerializer = (

* Serializer called for each node within the data object that is being serialized.
* @public
*/

@@ -29,3 +31,4 @@ export type GenericSerializer = (

* Set the logger level: LogLevel.NONE, LogLevel.ERROR, LogLevel.WARN, LogLevel.INFO, LogLevel.DEBUG
* @default LogLevel.NONE
* @defaultValue LogLevel.NONE
* @public
*/

@@ -37,3 +40,4 @@ logLevel?: LogLevel;

* This is most useful when logLevel is set to LogLevel.DEBUG.
* @default false
* @defaultValue false
* @public
*/

@@ -44,3 +48,4 @@ logNesting?: boolean | string;

* A string prefix you want to precede all log messages
* @default ""
* @defaultValue ""
* @public
*/

@@ -52,3 +57,4 @@ logPrefix?: string;

* Otherwise, the error will just be logged if the log level is set to LogLevel.ERROR or above.
* @default true
* @defaultValue true
* @public
*/

@@ -64,2 +70,3 @@ throwOnError?: boolean;

* (i.e. no external references)
* @public
*/

@@ -66,0 +73,0 @@ export type JSONSchemaDefinitions =

@@ -175,3 +175,3 @@ {

"canonicalReference": "scrubbr!GenericSerializer:type",
"docComment": "/**\n * Serializer called for each node within the data object that is being serialized.\n */\n",
"docComment": "/**\n * Serializer called for each node within the data object that is being serialized.\n *\n * @public\n */\n",
"excerptTokens": [

@@ -219,3 +219,3 @@ {

"canonicalReference": "scrubbr!JSONSchemaDefinitions:type",
"docComment": "/**\n * JSON Schema object that can be passed into Scrubbr. This needs to follow the format that `ts-json-schema-generator` uses: * All types are defined in the root `definitions` block. * All references (`$ref`) point to definitions within the JSON object. (i.e. no external references)\n */\n",
"docComment": "/**\n * JSON Schema object that can be passed into Scrubbr. This needs to follow the format that `ts-json-schema-generator` uses: * All types are defined in the root `definitions` block. * All references (`$ref`) point to definitions within the JSON object. (i.e. no external references)\n *\n * @public\n */\n",
"excerptTokens": [

@@ -259,3 +259,3 @@ {

"canonicalReference": "scrubbr!LogLevel:enum",
"docComment": "",
"docComment": "/**\n * The scrubbr log level. Setting the log level will enabled logging for that level and below.\n */\n",
"excerptTokens": [

@@ -273,3 +273,3 @@ {

"canonicalReference": "scrubbr!LogLevel.DEBUG:member",
"docComment": "",
"docComment": "/**\n * Log additional debug information. Set logNesting to true to also indent the logs with the data nesting.\n */\n",
"excerptTokens": [

@@ -295,3 +295,3 @@ {

"canonicalReference": "scrubbr!LogLevel.ERROR:member",
"docComment": "",
"docComment": "/**\n * Log errors\n */\n",
"excerptTokens": [

@@ -317,3 +317,3 @@ {

"canonicalReference": "scrubbr!LogLevel.INFO:member",
"docComment": "",
"docComment": "/**\n * Display basic information as well as warnings and errors.\n */\n",
"excerptTokens": [

@@ -339,3 +339,3 @@ {

"canonicalReference": "scrubbr!LogLevel.NONE:member",
"docComment": "",
"docComment": "/**\n * Disable logging.\n */\n",
"excerptTokens": [

@@ -361,3 +361,3 @@ {

"canonicalReference": "scrubbr!LogLevel.WARN:member",
"docComment": "",
"docComment": "/**\n * Log warnings and errors.\n */\n",
"excerptTokens": [

@@ -415,3 +415,3 @@ {

"kind": "Content",
"text": ", options: "
"text": ", options?: "
},

@@ -791,3 +791,3 @@ {

"canonicalReference": "scrubbr!Scrubbr#serialize:member(1)",
"docComment": "/**\n * Serialize data based on a TypeScript type.\n *\n * You can influence the return type by using the generic angle brackets:\n *\n * @param schemaType - The name of the typescript type to serialize the data with.\n *\n * @param data - The data to serialize\n *\n * @param context - Any data you want sent to the custom serializer functions.\n *\n * @param options - Set options for just this serialization.\n *\n * @example\n * ```\n * scrubbr.serialize<UserList>('UserList', data);\n * ```\n *\n */\n",
"docComment": "/**\n * Serialize data based on a TypeScript type.\n *\n * You can influence the return type by using the generic angle brackets:\n *\n * @param schemaType - The name of the typescript type to serialize the data with.\n *\n * @param data - The data to serialize\n *\n * @param context - Any data you want sent to the custom serializer functions.\n *\n * @param options - Set options for just this serialization.\n *\n * @example\n * ```\n * scrubbr.serialize<UserList>('UserList', data);\n * ```\n *\n * @public\n */\n",
"excerptTokens": [

@@ -1419,3 +1419,3 @@ {

"canonicalReference": "scrubbr!ScrubbrState#nesting:member",
"docComment": "/**\n * The nesting level at this node of the data being serialized\n */\n",
"docComment": "/**\n * The nesting level at this node of the data being serialized.\n */\n",
"excerptTokens": [

@@ -1673,3 +1673,3 @@ {

"canonicalReference": "scrubbr!TypeSerializer:type",
"docComment": "/**\n * Serializer function called for a specific TypeScript type.\n */\n",
"docComment": "/**\n * Serializer function called for a specific TypeScript type.\n *\n * @public\n */\n",
"excerptTokens": [

@@ -1717,3 +1717,3 @@ {

"canonicalReference": "scrubbr!useType:function(1)",
"docComment": "/**\n * Override the type that's being serialized. The return value from this function should be used by custom serializers.\n *\n * @example\n *\n * // Convert all `User` types to `RestrictedUser` scrubbr.typeSerializer('User', (data, state) => useType('RestrictedUser'));\n */\n",
"docComment": "/**\n * Override the type that's being serialized. The return value from this function should be used by custom serializers.\n *\n * @example\n * ```\n * // Convert all User types to PublicUser\n * scrubbr.typeSerializer('User', (data, state) =\\> useType('PublicUser'));\n * ```\n *\n * @public\n */\n",
"excerptTokens": [

@@ -1720,0 +1720,0 @@ {

@@ -19,13 +19,8 @@ ## API Report File for "scrubbr"

// @public (undocumented)
// @public
export enum LogLevel {
// (undocumented)
DEBUG = 4,
// (undocumented)
ERROR = 1,
// (undocumented)
INFO = 3,
// (undocumented)
NONE = 0,
// (undocumented)
WARN = 2

@@ -36,3 +31,3 @@ }

class Scrubbr {
constructor(schema: string | JSONSchemaDefinitions, options: ScrubbrOptions);
constructor(schema: string | JSONSchemaDefinitions, options?: ScrubbrOptions);
addGenericSerializer(serializer: GenericSerializer): void;

@@ -39,0 +34,0 @@ addTypeSerializer(typeName: string | string[], serializer: TypeSerializer): void;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc