一,环境
MAC
二, 安装brew
1,命令(不必要)
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
2,安装
2.1,命令
sudo npm install -g protobufjs@6.7.0
2.2,安装位置
/usr/local/lib/node_modules/protobufjs/dist
三,准备一个proto测试文件(test.proto)
package awesomepackage;
// 指定你正在使用 proto3 语法
// 若不设置,protocol buffer编译器会默认为您使用的是 proto2
syntax = "proto3";
message AwesomeMessage {
string awesome_field = 1;
}
四,开始生成
1, 进入到proto路径
2,命令
sudo pbjs -t static-module -w commonjs -o test.js test.proto
3, 生成的test.js
/*eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins*/
"use strict";
var $protobuf = require("protobufjs/minimal");
// Common aliases
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
// Exported root namespace
var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});
$root.awesomepackage = (function() {
/**
* Namespace awesomepackage.
* @exports awesomepackage
* @namespace
*/
var awesomepackage = {};
awesomepackage.AwesomeMessage = (function() {
/**
* Properties of an AwesomeMessage.
* @typedef awesomepackage.AwesomeMessage$Properties
* @type {Object}
* @property {string} [awesomeField] AwesomeMessage awesomeField.
*/
/**
* Constructs a new AwesomeMessage.
* @exports awesomepackage.AwesomeMessage
* @constructor
* @param {awesomepackage.AwesomeMessage$Properties=} [properties] Properties to set
*/
function AwesomeMessage(properties) {
if (properties)
for (var keys = Object.keys(properties), i = 0; i >> 3) {
case 1:
message.awesomeField = reader.string();
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
};
/**
* Decodes an AwesomeMessage message from the specified reader or buffer, length delimited.
* @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
* @returns {awesomepackage.AwesomeMessage} AwesomeMessage
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
AwesomeMessage.decodeDelimited = function decodeDelimited(reader) {
if (!(reader instanceof $Reader))
reader = $Reader(reader);
return this.decode(reader, reader.uint32());
};
/**
* Verifies an AwesomeMessage message.
* @param {Object.} message Plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
AwesomeMessage.verify = function verify(message) {
if (typeof message !== "object" || message === null)
return "object expected";
if (message.awesomeField != null)
if (!$util.isString(message.awesomeField))
return "awesomeField: string expected";
return null;
};
/**
* Creates an AwesomeMessage message from a plain object. Also converts values to their respective internal types.
* @param {Object.} object Plain object
* @returns {awesomepackage.AwesomeMessage} AwesomeMessage
*/
AwesomeMessage.fromObject = function fromObject(object) {
if (object instanceof $root.awesomepackage.AwesomeMessage)
return object;
var message = new $root.awesomepackage.AwesomeMessage();
if (object.awesomeField != null)
message.awesomeField = String(object.awesomeField);
return message;
};
/**
* Creates an AwesomeMessage message from a plain object. Also converts values to their respective internal types.
* This is an alias of {@link awesomepackage.AwesomeMessage.fromObject}.
* @function
* @param {Object.} object Plain object
* @returns {awesomepackage.AwesomeMessage} AwesomeMessage
*/
AwesomeMessage.from = AwesomeMessage.fromObject;
/**
* Creates a plain object from an AwesomeMessage message. Also converts values to other types if specified.
* @param {awesomepackage.AwesomeMessage} message AwesomeMessage
* @param {$protobuf.ConversionOptions} [options] Conversion options
* @returns {Object.} Plain object
*/
AwesomeMessage.toObject = function toObject(message, options) {
if (!options)
options = {};
var object = {};
if (options.defaults)
object.awesomeField = "";
if (message.awesomeField != null && message.hasOwnProperty("awesomeField"))
object.awesomeField = message.awesomeField;
return object;
};
/**
* Creates a plain object from this AwesomeMessage message. Also converts values to other types if specified.
* @param {$protobuf.ConversionOptions} [options] Conversion options
* @returns {Object.} Plain object
*/
AwesomeMessage.prototype.toObject = function toObject(options) {
return this.constructor.toObject(this, options);
};
/**
* Converts this AwesomeMessage to JSON.
* @returns {Object.} JSON object
*/
AwesomeMessage.prototype.toJSON = function toJSON() {
return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
};
return AwesomeMessage;
})();
return awesomepackage;
})();
module.exports = $root;
3.1, 修改test.js的内容
// var $protobuf = require("protobufjs/minimal");
var $protobuf = protobuf
五, 生成.d.ts文件
1,命令
sudo pbts -o test.d.ts .js
2, test.d.ts的内容
import * as $protobuf from "protobufjs";
/**
* Namespace awesomepackage.
* @exports awesomepackage
* @namespace
*/
export namespace awesomepackage {
type AwesomeMessage$Properties = {
awesomeField?: string;
};
/**
* Constructs a new AwesomeMessage.
* @exports awesomepackage.AwesomeMessage
* @constructor
* @param {awesomepackage.AwesomeMessage$Properties=} [properties] Properties to set
*/
class AwesomeMessage {
/**
* Constructs a new AwesomeMessage.
* @exports awesomepackage.AwesomeMessage
* @constructor
* @param {awesomepackage.AwesomeMessage$Properties=} [properties] Properties to set
*/
constructor(properties?: awesomepackage.AwesomeMessage$Properties);
/**
* AwesomeMessage awesomeField.
* @type {string|undefined}
*/
public awesomeField?: string;
/**
* Creates a new AwesomeMessage instance using the specified properties.
* @param {awesomepackage.AwesomeMessage$Properties=} [properties] Properties to set
* @returns {awesomepackage.AwesomeMessage} AwesomeMessage instance
*/
public static create(properties?: awesomepackage.AwesomeMessage$Properties): awesomepackage.AwesomeMessage;
/**
* Encodes the specified AwesomeMessage message. Does not implicitly {@link awesomepackage.AwesomeMessage.verify|verify} messages.
* @param {awesomepackage.AwesomeMessage$Properties} message AwesomeMessage message or plain object to encode
* @param {$protobuf.Writer} [writer] Writer to encode to
* @returns {$protobuf.Writer} Writer
*/
public static encode(message: awesomepackage.AwesomeMessage$Properties, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Encodes the specified AwesomeMessage message, length delimited. Does not implicitly {@link awesomepackage.AwesomeMessage.verify|verify} messages.
* @param {awesomepackage.AwesomeMessage$Properties} message AwesomeMessage message or plain object to encode
* @param {$protobuf.Writer} [writer] Writer to encode to
* @returns {$protobuf.Writer} Writer
*/
public static encodeDelimited(message: awesomepackage.AwesomeMessage$Properties, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes an AwesomeMessage message from the specified reader or buffer.
* @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
* @param {number} [length] Message length if known beforehand
* @returns {awesomepackage.AwesomeMessage} AwesomeMessage
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): awesomepackage.AwesomeMessage;
/**
* Decodes an AwesomeMessage message from the specified reader or buffer, length delimited.
* @param {$protobuf.Reader|Uint8Array} reader Reader or buffer to decode from
服务器托管网 * @returns {awesomepackage.AwesomeMessage} AwesomeMessage服务器托管网
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decodeDelimited(reader: ($protobuf.Reader|Uint8Array)): awesomepackage.AwesomeMessage;
/**
* Verifies an AwesomeMessage message.
* @param {Object.} message Plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
public static verify(message: { [k: string]: any }): string;
/**
* Creates an AwesomeMessage message from a plain object. Also converts values to their respective internal types.
* @param {Object.} object Plain object
* @returns {awesomepackage.AwesomeMessage} AwesomeMessage
*/
public static fromObject(object: { [k: string]: any }): awesomepackage.AwesomeMessage;
/**
* Creates an AwesomeMessage message from a plain object. Also converts values to their respective internal types.
* This is an alias of {@link awesomepackage.AwesomeMessage.fromObject}.
* @function
* @param {Object.} object Plain object
* @returns {awesomepackage.AwesomeMessage} AwesomeMessage
*/
public static from(object: { [k: string]: any }): awesomepackage.AwesomeMessage;
/**
* Creates a plain object from an AwesomeMessage message. Also converts values to other types if specified.
* @param {awesomepackage.AwesomeMessage} message AwesomeMessage
* @param {$protobuf.ConversionOptions} [options] Conversion options
* @returns {Object.} Plain object
*/
public static toObject(message: awesomepackage.AwesomeMessage, options?: $protobuf.ConversionOptions): { [k: string]: any };
/**
* Creates a plain object from this AwesomeMessage message. Also converts values to other types if specified.
* @param {$protobuf.ConversionOptions} [options] Conversion options
* @returns {Object.} Plain object
*/
public toObject(options?: $protobuf.ConversionOptions): { [k: string]: any };
/**
* Converts this AwesomeMessage to JSON.
* @returns {Object.} JSON object
*/
public toJSON(): { [k: string]: any };
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
Hundred Finance 背景知识 Hundred Finance 是 fork Compound 的一个借贷项目,在2023/04/15遭受了黑客攻击。攻击者在发起攻击交易之前执行了两笔准备交易占据了池子,因为发起攻击的前提是池子处于 empty 的状…