Skip to content

Instantly share code, notes, and snippets.

View marshallswain's full-sized avatar
😄

Marshall Thompson marshallswain

😄
View GitHub Profile
@marshallswain
marshallswain / client.js
Created August 16, 2017 17:29
feathers-authentication Authenticate a socket client at connection time
const feathers = require('feathers/client')
const io = require('socket.io-client')
const axios = require('axios')
const rest = require('feathers-rest/client')
const socketio = require('feathers-socketio/client')
const auth = require('feathers-authentication-client')
const hooks = require('feathers-hooks')
// TODO: make this port dynamic like the server tests
const host = 'http://localhost:3030'
@marshallswain
marshallswain / hook.map-create-to-upsert.js
Last active June 16, 2022 18:29
A Feathers hook to remap service.create to do an upsert
module.exports = function (upsertQuery) {
if (typeof upsertQuery !== 'function') {
console.warn('No `upsertQuery` function was passed to the mapCreateToUpsert hook. Please set params.upsertQuery in the hook context to dynamically declare the function.')
}
return function mapCreateToUpsert (context) {
const { service, data, params } = context // const data = { address: '123', identifier: 'my-identifier' }
upsertQuery = params.upsertQuery || upsertQuery
if (typeof upsertQuery !== 'function') {
@marshallswain
marshallswain / feathers-mongoose-upsert-2.js
Last active November 24, 2021 10:18
Another example of feathers-mongoose upserting
const data = { address: '2', identifier: 'some-other-identifier' }
const params = {
query: { address: '2' },
mongoose: { upsert: true }
}
app.service('address-meta').patch(null, data, params)
@marshallswain
marshallswain / address-map-data.json
Created July 8, 2017 04:05
Example address-map data
[
{"address": "1", "identifier": "xxx"},
{"address": "1", "identifier": "yyy"},
{"address": "1", "identifier": "zzz"},
]
@marshallswain
marshallswain / simple-mongoose-schema.js
Created July 8, 2017 03:58
Simple Mongoose Schema
// address-map-model.js - A mongoose model
//
// See http://mongoosejs.com/docs/models.html for more of what you can do here.
module.exports = function (app) {
const mongooseClient = app.get('mongooseClient')
const addressMap = new mongooseClient.Schema({
identifier: { type: String, required: true },
address: { type: String, required: true }
})
@marshallswain
marshallswain / feathers-mongoose-upsert.js
Last active July 8, 2017 04:08
Upserting with feathers-mongoose's `patch` method
const data = { address: '1', identifier: 'some-identifier' }
const params = {
query: { address: '1' },
mongoose: { upsert: true }
}
app.service('address-meta').patch(null, data, params)
@marshallswain
marshallswain / iv-encrypt.js
Created June 29, 2017 19:47
Encrypt with Initialization Vector
const crypto = require('crypto')
const { Buffer } = require('buffer')
const IV_LENGTH = 16 // For AES, this is always 16
function encrypt (text, key) {
let iv = crypto.randomBytes(IV_LENGTH)
let cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(key), iv)
let encrypted = cipher.update(text)
@marshallswain
marshallswain / generate-block.sh
Last active July 18, 2017 15:35
Starting Bitcoin Core dev
curl --user 'equibit:equibit' -H "Content-Type: application/json" -d '{"method":"generate", "params": [1]}' 127.0.0.1:18332
@marshallswain
marshallswain / .htaccess
Created June 26, 2017 14:39
Feathers socket.io behind Apache reverse proxy - credit to j2l4e
RewriteEngine On
RewriteCond %{HTTP:Connection} Upgrade [NC]
RewriteRule ^/(.*) ws://localhost:3030/$1 [P,L]
ProxyPass / http://localhost:3030/
ProxyPassReverse / http://localhost:3030/
@marshallswain
marshallswain / app.js
Created May 30, 2017 22:28
Listening to user updates and updating the connected sockets
const path = require('path');
const favicon = require('serve-favicon');
const compress = require('compression');
const cors = require('cors');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const feathers = require('feathers');
const configuration = require('feathers-configuration');
const hooks = require('feathers-hooks');