瀏覽代碼

Update protobuf.js

Lutz Roeder 4 年之前
父節點
當前提交
98191ca35a
共有 2 個文件被更改,包括 39 次插入31 次删除
  1. 38 30
      source/protobuf.js
  2. 1 1
      source/view.js

+ 38 - 30
source/protobuf.js

@@ -497,12 +497,34 @@ protobuf.BinaryReader = class {
 
 protobuf.TextReader = class {
 
-    static open(buffer) {
+    static open(data) {
+        const buffer = data instanceof Uint8Array ? data : data.peek();
+        const decoder = base.TextDecoder.open(buffer);
+        for (let i = 0; i < 0x100; i++) {
+            const c = decoder.decode();
+            if (c === undefined || c === '\0') {
+                if (i === 0) {
+                    return null;
+                }
+                break;
+            }
+            if (c < ' ' && c !== '\n' && c !== '\r' && c !== '\t') {
+                return null;
+            }
+            if (i === 0) {
+                if (c === '#' || c === '[') {
+                    continue;
+                }
+                if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
+                    continue;
+                }
+                return null;
+            }
+        }
         return new protobuf.TextReader(buffer);
     }
 
-    constructor(data) {
-        const buffer = data instanceof Uint8Array ? data : data.peek();
+    constructor(buffer) {
         this._decoder = base.TextDecoder.open(buffer);
         this.reset();
     }
@@ -511,36 +533,22 @@ protobuf.TextReader = class {
         const tags = new Map();
         this.reset();
         try {
-            let text = true;
-            for (let i = 0; i < 0x100; i++) {
-                const c = this._decoder.decode();
-                if (c === undefined || c === '\0') {
-                    break;
-                }
-                if (c < ' ' && c !== '\n' && c !== '\r' && c !== '\t') {
-                    text = false;
-                    break;
-                }
-            }
-            if (text) {
-                this.reset();
-                this.start(false);
-                while (!this.end(false)) {
-                    const tag = this.tag();
-                    tags.set(tag, true);
-                    if (this.token() === '{') {
-                        this.start();
-                        while (!this.end()) {
-                            const subtag = this.tag();
-                            tags.set(tag + '.' + subtag, true);
-                            this.skip();
-                            this.match(',');
-                        }
-                    }
-                    else {
+            this.start(false);
+            while (!this.end(false)) {
+                const tag = this.tag();
+                tags.set(tag, true);
+                if (this.token() === '{') {
+                    this.start();
+                    while (!this.end()) {
+                        const subtag = this.tag();
+                        tags.set(tag + '.' + subtag, true);
                         this.skip();
+                        this.match(',');
                     }
                 }
+                else {
+                    this.skip();
+                }
             }
         }
         catch (err) {

+ 1 - 1
source/view.js

@@ -1299,7 +1299,7 @@ view.ModelContext = class {
                         switch (type) {
                             case 'pbtxt': {
                                 const reader = protobuf.TextReader.open(stream);
-                                tags = reader.signature();
+                                tags = reader ? reader.signature() : tags;
                                 break;
                             }
                             case 'pb': {