Skip to content

Instantly share code, notes, and snippets.

@tabjy
tabjy / ws.js
Created March 11, 2021 00:31
WebSocket from scratch. In Node.js
const http = require('http')
const crypto = require('crypto')
const server = http.createServer((req, res) => {
console.log('got request', req.url)
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('okay')
})
server.on('upgrade', function (req, socket) {
@sid24rane
sid24rane / base64-encode-decode.js
Created July 26, 2016 14:26
Node.js Base64 Encode Decode -> Image
var buffer = require('buffer');
var path = require('path');
var fs = require('fs');
function encode_base64(filename){
fs.readFile(path.join(__dirname,'/public/',filename),function(error,data){
if(error){
throw error;
}else{
var buf = Buffer.from(data);
@htp
htp / curl-websocket.sh
Last active November 13, 2024 12:46
Test a WebSocket using curl.
curl --include \
--no-buffer \
--header "Connection: Upgrade" \
--header "Upgrade: websocket" \
--header "Host: example.com:80" \
--header "Origin: http://example.com:80" \
--header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" \
--header "Sec-WebSocket-Version: 13" \
http://example.com:80/
@miguelmota
miguelmota / buffer.js
Created August 24, 2014 18:33
Node.js Buffer to ArrayBuffer
// @credit: http://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer
// From Buffer to ArrayBuffer:
function toArrayBuffer(buffer) {
var ab = new ArrayBuffer(buffer.length);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
view[i] = buffer[i];
}
@thinkphp
thinkphp / gist:5110833
Created March 7, 2013 19:09
Creating sha-1 on Node.js.
var crypto = require('crypto');
function sha1( data ) {
var generator = crypto.createHash('sha1');
generator.update( data )
return generator.digest('hex')
}
console.log( sha1('adrian') )