Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
plain(options: PlainOptions | undefined = {}): Partial> & { [key: string]: any } {
if (!is.object(options)) {
throw new Error('Options must be an Object');
}
const readAll = !!options.readAll || false;
const virtuals = !!options.virtuals || false;
const showKey = !!options.showKey || false;
if (virtuals) {
// Add any possible virtual properties to the object
this.entityData = this.__getEntityDataWithVirtuals();
}
const data = datastoreSerializer.fromDatastore(this.entityData, this.constructor as Model, {
readAll,
showKey,
});
ranges.push({
start: {
value: lastRowKey,
inclusive: false,
},
});
} else {
// Readjust and/or remove ranges based on previous valid row reads.
// Iterate backward since items may need to be removed.
for (let index = ranges.length - 1; index >= 0; index--) {
const range = ranges[index];
const startValue = is.object(range.start)
? range.start.value
: range.start;
const endValue = is.object(range.end) ? range.end.value : range.end;
const isWithinStart =
!startValue || greaterThanOrEqualTo(startValue, lastRowKey);
const isWithinEnd = !endValue || lessThan(lastRowKey, endValue);
if (isWithinStart) {
if (isWithinEnd) {
// The lastRowKey is within this range, adjust the start
// value.
range.start = {
value: lastRowKey,
inclusive: false,
};
} else {
// The lastRowKey is past this range, remove this range.
ranges.splice(index, 1);
}
}
write(query: string | undefined, data: ObjectType | any[]): any {
if (!data) return;
let cache: any = null;
if (Array.isArray(data)) {
cache = this.writeArray(data);
} else if (is.object(data)) {
cache = this.writeObject(data);
}
if (query) {
this.store[query] = cache;
}
return cache;
}
return function(name, fn){
var objs = {};
if(is.string(name) && is.function(fn)){
obj[name] = fn;
}else if(is.object(name)){
objs = name;
}
Object.keys(objs).forEach(function(name){
if(validator.hasOwnProperty(name)){
throw new Error('validator already have method ' + name);
return;
}
if(dicts.indexOf(name) !== -1){
throw new Error('you have already defined method ' + name);
return;
}
if(!name.match(regex)){
dicts.push(name);
const MAX_DATASTORE_VALUE_LENGTH = 1500;
if (Array.isArray(entities)) {
for (const entry of entities) {
if (entry.name && entry.value) {
if (
is.string(entry.value) &&
Buffer.from(entry.value).length > MAX_DATASTORE_VALUE_LENGTH
) {
entry.excludeFromIndexes = true;
} else {
continue;
}
}
findLargeProperties_(entry, path.concat('[]'), properties);
}
} else if (is.object(entities)) {
const keys = Object.keys(entities);
for (const key of keys) {
findLargeProperties_(
entities[key],
path.concat(`${path ? '.' : ''}${key}`),
properties
);
}
} else if (
is.string(entities) &&
Buffer.from(entities).length > MAX_DATASTORE_VALUE_LENGTH
) {
if (properties.indexOf(path) < 0) {
properties.push(path);
}
}
function COLUMNS(table, value, options = {}) {
if (table != null && !is.string(table)) {
value = table
table = null
}
const { delimiter = ', ' } = options
let keys
if (is.object(value)) {
keys = getDefinedKeys(value)
} else if (is.array(value) && is.object(value[0])) {
keys = getDefinedKeys(value[0])
} else if (is.array(value) && is.string(value[0])) {
keys = value
} else {
throw new Error(`The \`COLUMNS\` SQL helper must be passed an object, an array of objects or an array of strings, but you passed: ${value}`)
}
const idents = keys.map(k => COLUMN(table, k))
const query = sql`${sql.join(idents, delimiter)}`
return query
}
const toJSON = (collection) => {
const json = {};
for (let [name, value] of Object.entries(collection)) {
if (is.primitive(value)) {
json[name] = value;
} else if (is.object(value)) {
if (value.toJSON) {
json[name] = value.toJSON();
} else {
json[name] = toJSON(value);
}
}
}
return json;
}
function COMPOSITE(object) {
if (!is.object(object)) {
throw new Error(`The \`COMPOSITE\` SQL helper must be passed an object, but you passed: ${object}`)
}
const keys = getDefinedKeys(object)
const vals = keys.map(k => sql`${object[k]}`)
const query = sql`(${sql.join(vals, ', ')})`
return query
}
export function koaRequestInformationExtractor(
req: koa.Request,
res: koa.Response
) {
const returnObject = new RequestInformationContainer();
if (
!is.object(req) ||
!is.object(res) ||
is.function(req) ||
is.function(res) ||
is.array(req) ||
is.array(res) ||
!is.object(req.headers)
) {
return returnObject;
}
returnObject
.setMethod(req.method)
.setUrl(req.url)
.setUserAgent(req.headers['user-agent'])
.setReferrer(req.headers.referrer)
.setStatusCode(res.status)
.setRemoteAddress(req.ip);
return returnObject;
}
function extractRemoteAddressFromRequest(req: express.Request) {
if (typeof req.header('x-forwarded-for') !== 'undefined') {
return req.header('x-forwarded-for');
} else if (is.object(req.connection)) {
return req.connection.remoteAddress;
}
return '';
}