through.obj(function(file, enc, next) { if (file.isNull() || file.isDirectory()) { next(null, file); return; } if (file.extname !== '.md' && path.extname(file.history[0]) !== '.md') { next(null, file); return; } try { let str = file.contents.toString(); str = str.replace(/(\{{2,4})([^}]+)(\}{2,4})/g, (m, open, inner, close) => { return open + unescape(inner) + close; }); file.contents = Buffer.from(str); } catch (err) { this.emit('error', new PluginError('unescape', err, { fileName: file.path })); return; } next(null, file); })
generateHOTP(secret, counter) { if (!secret) { throw new Error('Secret is required'); } if (!counter) { throw new Error('Counter is required'); } const decodedSecret = base32.decode.asBytes(secret); const buffer = Buffer.alloc(8); for (let i = 0; i < 8; i++) { buffer[7 - i] = counter & 0xff; counter = counter >> 8; } // Step 1: Generate an HMAC-SHA-1 value const hmac = crypto.createHmac(this.hmacAlgorithm, Buffer.from(decodedSecret)); hmac.update(buffer); const hmacResult = hmac.digest(); // Step 2: Generate a 4-byte string (Dynamic Truncation) const code = this.dynamicTruncationFn(hmacResult); // Step 3: Compute an HOTP value return code % 10 ** this.otpLength; }
through.obj(function(file, enc, next) { if (file.isNull() || file.isDirectory()) { next(null, file); return; } if (file.extname !== '.md' && path.extname(file.history[0]) !== '.md') { next(null, file); return; } try { let md = new Remarkable(opts); file.contents = Buffer.from(md.render(file.contents.toString())); file.extname = '.html'; } catch (err) { this.emit('error', new PluginError('remarkable', err, { fileName: file.path })); return; } next(null, file); })
let encodeBody = function (server: any, route: string, msgBody: any) { // encode use protobuf if (!!server.protobuf && !!server.protobuf.getProtos().server[route]) { msgBody = server.protobuf.encode(route, msgBody); } else if (!!server.decodeIO_protobuf && !!server.decodeIO_protobuf.check(Constants.RESERVED.SERVER, route)) { msgBody = server.decodeIO_protobuf.encode(route, msgBody); } else { msgBody = Buffer.from(JSON.stringify(msgBody), 'utf8'); } return msgBody; }
/** * Send byte data package to client. * * @param {Buffer} msg byte data */ send(msg: any) { if (this.state !== ST_WORKING) { return; } if (msg instanceof String) { msg = Buffer.from(msg as string); } else if (!(msg instanceof Buffer)) { msg = Buffer.from(JSON.stringify(msg)); } this.sendRaw(Package.encode(Package.TYPE_DATA, msg)); }
ondata(chunk: Buffer) { if (this.state === ST_CLOSED) { throw new Error('socket has closed'); } if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk)) { throw new Error('invalid data'); } if (typeof chunk === 'string') { chunk = Buffer.from(chunk, 'utf8'); } let offset = 0, end = chunk.length; while (offset < end && this.state !== ST_CLOSED) { if (this.state === ST_HEAD) { offset = this.readHead(chunk, offset); } if (this.state === ST_BODY) { offset = this.readBody(chunk, offset); } } return true; }