Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sprucebot-node

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sprucebot-node - npm Package Compare versions

Comparing version 0.0.10 to 0.1.0

86

index.js

@@ -37,2 +37,3 @@ const context = require('./factories/context')

this.marketingUrl = interfaceUrl + '/marketing'
this._mutexes = {}

@@ -169,21 +170,10 @@ this.version = '1.0'

async metas(
{ key, locationId, userId, sortBy, limit } = {},
{ key, locationId, userId, sortBy, order, limit, value } = {},
suppressParseErrors = true
) {
// Get results as an array where values are JSON strings
const metas = await this.https.get('/data', Array.from(arguments)[0])
metas.forEach(meta => {
try {
meta.value = JSON.parse(meta.value)
} catch (err) {
if (suppressParseErrors) {
console.error('Failed to parse JSON value for meta.', err)
} else {
throw err
}
}
})
return metas
const query = Array.from(arguments)[0] || {}
if (query.value) {
query.value = JSON.stringify(query.value)
}
return this.https.get('/data', query)
}

@@ -198,3 +188,7 @@

*/
async meta(key, { locationId, userId } = {}, suppressParseErrors = true) {
async meta(
key,
{ locationId, userId, value, sortBy, order } = {},
suppressParseErrors = true
) {
const args = Array.from(arguments)

@@ -223,3 +217,2 @@ const query = args[1] || {}

const meta = await this.https.post('/data', data)
meta.value = JSON.parse(meta.value)
return meta

@@ -244,3 +237,2 @@ }

const meta = await this.https.patch(`/data/${id}`, data)
meta.value = JSON.parse(meta.value)
return meta

@@ -312,4 +304,58 @@ }

}
/**
* To stop race conditions, you can have requests wait before starting the next.
*
* @param {String} key
*/
async wait(key) {
if (!this._mutexes[key]) {
this._mutexes[key] = {
promises: [],
resolvers: [],
count: 0
}
}
//track which we are on
this._mutexes[key].count++
//first is always auto resolved
if (this._mutexes[key].count === 1) {
this._mutexes[key].promises.push(new Promise(resolve => resolve()))
this._mutexes[key].resolvers.push(() => {})
} else {
let resolver = resolve => {
this._mutexes[key].resolvers.push(resolve)
}
let promise = new Promise(resolver)
this._mutexes[key].promises.push(promise)
}
return this._mutexes[key].promises[this._mutexes[key].count - 1]
}
/**
* Long operation is complete, start up again.
*
* @param {String} key
*/
async go(key) {
if (this._mutexes[key]) {
//remove this promise
this._mutexes[key].promises.shift()
this._mutexes[key].resolvers.shift()
this._mutexes[key].count--
//if we are done, clear
if (this._mutexes[key].count === 0) {
delete this._mutexes[key]
} else {
//otherwise resolve the next promise
this._mutexes[key].resolvers[0]()
}
}
}
}
module.exports = Sprucebot

@@ -12,3 +12,3 @@ const Sprucebot = require('./index')

apiKey: 'DD16373A-9482-4E27-A4A3-77B2664F6C82',
host: 'dev-api.sprucebot.com',
host: 'local-api.sprucebot.com',
name: `Unit Test Skill - ${TIMESTAMP}`,

@@ -59,2 +59,12 @@ description: `This skill is for the tests that are run on pre-commit. ${TIMESTAMP}`,

afterAll(async () => {
const sb = new Sprucebot(SKILL)
const metas = await sb.metas({ limit: 200 })
await Promise.all(
metas.map(meta => {
return sb.deleteMeta(meta.id)
})
)
})
test(`Sprucebot should be able to change it's name`, async () => {

@@ -217,2 +227,130 @@ expect.assertions(1)

})
test('Sprucebot should be able to create simple meta and fetch it', async () => {
expect.assertions(1)
const sb = new Sprucebot(SKILL)
const meta = await sb.createMeta('test-1', true)
const found = await sb.meta('test-1')
expect(meta.value).toEqual(found.value)
})
test('Sprucebot should be able to create complex meta and search against it', async () => {
expect.assertions(1)
const sb = new Sprucebot(SKILL)
const meta1 = await sb.createMeta('test-2', {
foo: 'bar'
})
const meta2 = await sb.createMeta('test-2', {
hello: 'world'
})
const matches = await sb.metas({
value: {
hello: 'world'
}
})
expect(meta2.id).toEqual(matches[0].id)
})
test('Sprucebot should be able to create even more complex meta and search against $contains', async () => {
expect.assertions(3)
const sb = new Sprucebot(SKILL)
const meta1 = await sb.createMeta('test-2', {
foo: 'bar',
bar: 'foo'
})
const meta2 = await sb.createMeta('test-2', {
hello: 'world',
world: 'hello'
})
const meta3 = await sb.createMeta('test-2', {
hello: 'world',
again: 'hello'
})
const matches = await sb.metas({
value: {
hello: 'world'
}
})
const matches2 = await sb.metas({
value: {
hello: 'world',
again: 'hello'
}
})
expect(matches).toHaveLength(2)
expect(matches2).toHaveLength(1)
expect(matches2[0].id).toEqual(meta3.id)
})
test('Sprucebot should be able to create a few metas and find some with $or with exact match', async () => {
expect.assertions(3)
const sb = new Sprucebot(SKILL)
const meta1 = await sb.createMeta('test-2', {
foo: 'bar'
})
const meta2 = await sb.createMeta('test-2', {
foo: 'world'
})
const meta3 = await sb.createMeta('test-2', {
foo: 'go'
})
const matches = await sb.metas({
value: {
$or: [
{
foo: 'bar'
},
{
foo: 'world'
}
]
}
})
expect(matches).toHaveLength(2)
expect(matches[0].id).toEqual(meta1.id)
expect(matches[1].id).toEqual(meta2.id)
})
test('Sprucebot should be able to create a few metas and find some with $or with $contains match', async () => {
expect.assertions(3)
const sb = new Sprucebot(SKILL)
const meta1 = await sb.createMeta('test-2', {
foo: 'bar',
hello: 'world'
})
const meta2 = await sb.createMeta('test-2', {
foo: 'world',
plus: 'one'
})
const meta3 = await sb.createMeta('test-2', {
foo: 'go',
bananas: 'apples'
})
const matches = await sb.metas({
value: {
$or: [
{
foo: 'bar'
},
{
foo: 'world'
}
]
}
})
expect(matches).toHaveLength(2)
expect(matches[0].id).toEqual(meta1.id)
expect(matches[1].id).toEqual(meta2.id)
})
})
{
"name": "sprucebot-node",
"version": "0.0.10",
"version": "0.1.0",
"scripts": {

@@ -5,0 +5,0 @@ "lint": "eslint '**/**/*{.js}'",

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