protobufjs 8.0.3-experimental → 8.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +39 -39
- package/README.md +727 -727
- package/dist/light/protobuf.js +7724 -7352
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +2606 -2546
- package/dist/minimal/protobuf.js.map +1 -1
- package/dist/minimal/protobuf.min.js +3 -3
- package/dist/minimal/protobuf.min.js.map +1 -1
- package/dist/protobuf.js +9588 -9131
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/debug/README.md +4 -4
- package/ext/debug/index.js +71 -71
- package/ext/descriptor/README.md +72 -72
- package/ext/descriptor/index.d.ts +195 -191
- package/ext/descriptor/index.js +1181 -1052
- package/ext/descriptor/test.js +54 -54
- package/google/LICENSE +27 -27
- package/google/README.md +1 -1
- package/google/api/annotations.json +82 -82
- package/google/api/annotations.proto +10 -10
- package/google/api/http.json +85 -85
- package/google/api/http.proto +30 -30
- package/google/protobuf/api.json +117 -117
- package/google/protobuf/api.proto +33 -33
- package/google/protobuf/descriptor.json +1381 -738
- package/google/protobuf/descriptor.proto +535 -286
- package/google/protobuf/source_context.json +19 -19
- package/google/protobuf/source_context.proto +7 -7
- package/google/protobuf/type.json +201 -201
- package/google/protobuf/type.proto +89 -89
- package/index.d.ts +2817 -2792
- package/index.js +4 -4
- package/light.d.ts +2 -2
- package/light.js +3 -3
- package/minimal.d.ts +2 -2
- package/minimal.js +4 -4
- package/package.json +96 -112
- package/scripts/postinstall.js +32 -32
- package/src/common.js +399 -399
- package/src/converter.js +310 -301
- package/src/decoder.js +135 -127
- package/src/encoder.js +100 -100
- package/src/enum.js +226 -241
- package/src/field.js +453 -432
- package/src/index-light.js +104 -104
- package/src/index-minimal.js +36 -36
- package/src/index.js +12 -12
- package/src/mapfield.js +126 -126
- package/src/message.js +139 -139
- package/src/method.js +160 -160
- package/src/namespace.js +550 -434
- package/src/object.js +385 -330
- package/src/oneof.js +222 -222
- package/src/parse.js +1024 -944
- package/src/reader.js +426 -416
- package/src/reader_buffer.js +51 -51
- package/src/root.js +404 -384
- package/src/roots.js +17 -17
- package/src/rpc/service.js +142 -142
- package/src/rpc.js +36 -36
- package/src/service.js +193 -168
- package/src/tokenize.js +421 -416
- package/src/type.js +625 -590
- package/src/types.js +196 -196
- package/src/typescript.jsdoc +15 -15
- package/src/util/aspromise.d.ts +13 -0
- package/src/util/aspromise.js +52 -0
- package/src/util/base64.d.ts +32 -0
- package/src/util/base64.js +139 -0
- package/src/util/codegen.d.ts +31 -0
- package/src/util/codegen.js +113 -0
- package/src/util/eventemitter.d.ts +45 -0
- package/src/util/eventemitter.js +84 -0
- package/src/util/fetch.d.ts +56 -0
- package/src/util/fetch.js +114 -0
- package/src/util/float.d.ts +83 -0
- package/src/util/float.js +335 -0
- package/src/util/inquire.d.ts +9 -0
- package/src/util/inquire.js +37 -0
- package/src/util/longbits.js +200 -200
- package/src/util/minimal.js +461 -438
- package/src/util/path.d.ts +22 -0
- package/src/util/path.js +72 -0
- package/src/util/patterns.js +8 -0
- package/src/util/pool.d.ts +32 -0
- package/src/util/pool.js +48 -0
- package/src/util/utf8.d.ts +24 -0
- package/src/util/utf8.js +104 -0
- package/src/util.js +215 -213
- package/src/verifier.js +180 -177
- package/src/wrappers.js +103 -102
- package/src/writer.js +465 -465
- package/src/writer_buffer.js +85 -85
- package/tsconfig.json +8 -8
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export = codegen;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Appends code to the function's body.
|
|
5
|
+
* @param [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
|
|
6
|
+
* @param [formatParams] Format parameters
|
|
7
|
+
* @returns Itself or the generated function if finished
|
|
8
|
+
* @throws {Error} If format parameter counts do not match
|
|
9
|
+
*/
|
|
10
|
+
type Codegen = (formatStringOrScope?: (string|{ [k: string]: any }), ...formatParams: any[]) => (Codegen|Function);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Begins generating a function.
|
|
14
|
+
* @param functionParams Function parameter names
|
|
15
|
+
* @param [functionName] Function name if not anonymous
|
|
16
|
+
* @returns Appender that appends code to the function's body
|
|
17
|
+
*/
|
|
18
|
+
declare function codegen(functionParams: string[], functionName?: string): Codegen;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Begins generating a function.
|
|
22
|
+
* @param [functionName] Function name if not anonymous
|
|
23
|
+
* @returns Appender that appends code to the function's body
|
|
24
|
+
*/
|
|
25
|
+
declare function codegen(functionName?: string): Codegen;
|
|
26
|
+
|
|
27
|
+
declare namespace codegen {
|
|
28
|
+
|
|
29
|
+
/** When set to `true`, codegen will log generated code to console. Useful for debugging. */
|
|
30
|
+
let verbose: boolean;
|
|
31
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = codegen;
|
|
3
|
+
|
|
4
|
+
var patterns = require("./patterns");
|
|
5
|
+
var reservedRe = patterns.reservedRe;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Begins generating a function.
|
|
9
|
+
* @memberof util
|
|
10
|
+
* @param {string[]} functionParams Function parameter names
|
|
11
|
+
* @param {string} [functionName] Function name if not anonymous
|
|
12
|
+
* @returns {Codegen} Appender that appends code to the function's body
|
|
13
|
+
*/
|
|
14
|
+
function codegen(functionParams, functionName) {
|
|
15
|
+
|
|
16
|
+
/* istanbul ignore if */
|
|
17
|
+
if (typeof functionParams === "string") {
|
|
18
|
+
functionName = functionParams;
|
|
19
|
+
functionParams = undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
var body = [];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Appends code to the function's body or finishes generation.
|
|
26
|
+
* @typedef Codegen
|
|
27
|
+
* @type {function}
|
|
28
|
+
* @param {string|Object.<string,*>} [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any
|
|
29
|
+
* @param {...*} [formatParams] Format parameters
|
|
30
|
+
* @returns {Codegen|Function} Itself or the generated function if finished
|
|
31
|
+
* @throws {Error} If format parameter counts do not match
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
function Codegen(formatStringOrScope) {
|
|
35
|
+
// note that explicit array handling below makes this ~50% faster
|
|
36
|
+
|
|
37
|
+
// finish the function
|
|
38
|
+
if (typeof formatStringOrScope !== "string") {
|
|
39
|
+
var source = toString();
|
|
40
|
+
if (codegen.verbose)
|
|
41
|
+
console.log("codegen: " + source); // eslint-disable-line no-console
|
|
42
|
+
source = "return " + source;
|
|
43
|
+
if (formatStringOrScope) {
|
|
44
|
+
var scopeKeys = Object.keys(formatStringOrScope),
|
|
45
|
+
scopeParams = new Array(scopeKeys.length + 1),
|
|
46
|
+
scopeValues = new Array(scopeKeys.length),
|
|
47
|
+
scopeOffset = 0;
|
|
48
|
+
while (scopeOffset < scopeKeys.length) {
|
|
49
|
+
scopeParams[scopeOffset] = scopeKeys[scopeOffset];
|
|
50
|
+
scopeValues[scopeOffset] = formatStringOrScope[scopeKeys[scopeOffset++]];
|
|
51
|
+
}
|
|
52
|
+
scopeParams[scopeOffset] = source;
|
|
53
|
+
return Function.apply(null, scopeParams).apply(null, scopeValues); // eslint-disable-line no-new-func
|
|
54
|
+
}
|
|
55
|
+
return Function(source)(); // eslint-disable-line no-new-func
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// otherwise append to body
|
|
59
|
+
var formatParams = new Array(arguments.length - 1),
|
|
60
|
+
formatOffset = 0;
|
|
61
|
+
while (formatOffset < formatParams.length)
|
|
62
|
+
formatParams[formatOffset] = arguments[++formatOffset];
|
|
63
|
+
formatOffset = 0;
|
|
64
|
+
formatStringOrScope = formatStringOrScope.replace(/%([%dfijs])/g, function replace($0, $1) {
|
|
65
|
+
var value = formatParams[formatOffset++];
|
|
66
|
+
switch ($1) {
|
|
67
|
+
case "d": case "f": return String(Number(value));
|
|
68
|
+
case "i": return String(Math.floor(value));
|
|
69
|
+
case "j": return JSON.stringify(value);
|
|
70
|
+
case "s": return String(value);
|
|
71
|
+
}
|
|
72
|
+
return "%";
|
|
73
|
+
});
|
|
74
|
+
if (formatOffset !== formatParams.length)
|
|
75
|
+
throw Error("parameter count mismatch");
|
|
76
|
+
body.push(formatStringOrScope);
|
|
77
|
+
return Codegen;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function toString(functionNameOverride) {
|
|
81
|
+
return "function " + safeFunctionName(functionNameOverride || functionName) + "(" + (functionParams && functionParams.join(",") || "") + "){\n " + body.join("\n ") + "\n}";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
Codegen.toString = toString;
|
|
85
|
+
return Codegen;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Begins generating a function.
|
|
90
|
+
* @memberof util
|
|
91
|
+
* @function codegen
|
|
92
|
+
* @param {string} [functionName] Function name if not anonymous
|
|
93
|
+
* @returns {Codegen} Appender that appends code to the function's body
|
|
94
|
+
* @variation 2
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* When set to `true`, codegen will log generated code to console. Useful for debugging.
|
|
99
|
+
* @name util.codegen.verbose
|
|
100
|
+
* @type {boolean}
|
|
101
|
+
*/
|
|
102
|
+
codegen.verbose = false;
|
|
103
|
+
|
|
104
|
+
function safeFunctionName(name) {
|
|
105
|
+
if (!name)
|
|
106
|
+
return "";
|
|
107
|
+
name = String(name).replace(/[^\w$]/g, "");
|
|
108
|
+
if (!name)
|
|
109
|
+
return "";
|
|
110
|
+
if (/^\d/.test(name))
|
|
111
|
+
name = "_" + name;
|
|
112
|
+
return reservedRe.test(name) ? name + "_" : name;
|
|
113
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export = EventEmitter;
|
|
2
|
+
|
|
3
|
+
type EventEmitterListener = (...args: any[]) => {};
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Constructs a new event emitter instance.
|
|
7
|
+
* @classdesc A minimal event emitter.
|
|
8
|
+
* @memberof util
|
|
9
|
+
* @constructor
|
|
10
|
+
*/
|
|
11
|
+
declare class EventEmitter {
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Constructs a new event emitter instance.
|
|
15
|
+
* @classdesc A minimal event emitter.
|
|
16
|
+
* @memberof util
|
|
17
|
+
* @constructor
|
|
18
|
+
*/
|
|
19
|
+
constructor();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Registers an event listener.
|
|
23
|
+
* @param {string} evt Event name
|
|
24
|
+
* @param {EventEmitterListener} fn Listener
|
|
25
|
+
* @param {*} [ctx] Listener context
|
|
26
|
+
* @returns {this} `this`
|
|
27
|
+
*/
|
|
28
|
+
public on(evt: string, fn: EventEmitterListener, ctx?: any): EventEmitter;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Removes an event listener or any matching listeners if arguments are omitted.
|
|
32
|
+
* @param {string} [evt] Event name. Removes all listeners if omitted.
|
|
33
|
+
* @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
|
|
34
|
+
* @returns {this} `this`
|
|
35
|
+
*/
|
|
36
|
+
public off(evt?: string, fn?: EventEmitterListener): EventEmitter;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Emits an event by calling its listeners with the specified arguments.
|
|
40
|
+
* @param {string} evt Event name
|
|
41
|
+
* @param {...*} args Arguments
|
|
42
|
+
* @returns {this} `this`
|
|
43
|
+
*/
|
|
44
|
+
public emit(evt: string, ...args: any[]): EventEmitter;
|
|
45
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = EventEmitter;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Constructs a new event emitter instance.
|
|
6
|
+
* @classdesc A minimal event emitter.
|
|
7
|
+
* @memberof util
|
|
8
|
+
* @constructor
|
|
9
|
+
*/
|
|
10
|
+
function EventEmitter() {
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Registered listeners.
|
|
14
|
+
* @type {Object.<string,*>}
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
this._listeners = {};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Event listener as used by {@link util.EventEmitter}.
|
|
22
|
+
* @typedef EventEmitterListener
|
|
23
|
+
* @type {function}
|
|
24
|
+
* @param {...*} args Arguments
|
|
25
|
+
* @returns {undefined}
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Registers an event listener.
|
|
30
|
+
* @param {string} evt Event name
|
|
31
|
+
* @param {EventEmitterListener} fn Listener
|
|
32
|
+
* @param {*} [ctx] Listener context
|
|
33
|
+
* @returns {this} `this`
|
|
34
|
+
*/
|
|
35
|
+
EventEmitter.prototype.on = function on(evt, fn, ctx) {
|
|
36
|
+
(this._listeners[evt] || (this._listeners[evt] = [])).push({
|
|
37
|
+
fn : fn,
|
|
38
|
+
ctx : ctx || this
|
|
39
|
+
});
|
|
40
|
+
return this;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Removes an event listener or any matching listeners if arguments are omitted.
|
|
45
|
+
* @param {string} [evt] Event name. Removes all listeners if omitted.
|
|
46
|
+
* @param {EventEmitterListener} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
|
|
47
|
+
* @returns {this} `this`
|
|
48
|
+
*/
|
|
49
|
+
EventEmitter.prototype.off = function off(evt, fn) {
|
|
50
|
+
if (evt === undefined)
|
|
51
|
+
this._listeners = {};
|
|
52
|
+
else {
|
|
53
|
+
if (fn === undefined)
|
|
54
|
+
this._listeners[evt] = [];
|
|
55
|
+
else {
|
|
56
|
+
var listeners = this._listeners[evt];
|
|
57
|
+
for (var i = 0; i < listeners.length;)
|
|
58
|
+
if (listeners[i].fn === fn)
|
|
59
|
+
listeners.splice(i, 1);
|
|
60
|
+
else
|
|
61
|
+
++i;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return this;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Emits an event by calling its listeners with the specified arguments.
|
|
69
|
+
* @param {string} evt Event name
|
|
70
|
+
* @param {...*} args Arguments
|
|
71
|
+
* @returns {this} `this`
|
|
72
|
+
*/
|
|
73
|
+
EventEmitter.prototype.emit = function emit(evt) {
|
|
74
|
+
var listeners = this._listeners[evt];
|
|
75
|
+
if (listeners) {
|
|
76
|
+
var args = [],
|
|
77
|
+
i = 1;
|
|
78
|
+
for (; i < arguments.length;)
|
|
79
|
+
args.push(arguments[i++]);
|
|
80
|
+
for (i = 0; i < listeners.length;)
|
|
81
|
+
listeners[i].fn.apply(listeners[i++].ctx, args);
|
|
82
|
+
}
|
|
83
|
+
return this;
|
|
84
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export = fetch;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Node-style callback as used by {@link util.fetch}.
|
|
5
|
+
* @typedef FetchCallback
|
|
6
|
+
* @type {function}
|
|
7
|
+
* @param {?Error} error Error, if any, otherwise `null`
|
|
8
|
+
* @param {string} [contents] File contents, if there hasn't been an error
|
|
9
|
+
* @returns {undefined}
|
|
10
|
+
*/
|
|
11
|
+
type FetchCallback = (error: Error, contents?: string) => void;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Options as used by {@link util.fetch}.
|
|
15
|
+
* @typedef IFetchOptions
|
|
16
|
+
* @type {Object}
|
|
17
|
+
* @property {boolean} [binary=false] Whether expecting a binary response
|
|
18
|
+
* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
interface IFetchOptions {
|
|
22
|
+
binary?: boolean;
|
|
23
|
+
xhr?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Fetches the contents of a file.
|
|
28
|
+
* @memberof util
|
|
29
|
+
* @param {string} filename File path or url
|
|
30
|
+
* @param {IFetchOptions} options Fetch options
|
|
31
|
+
* @param {FetchCallback} callback Callback function
|
|
32
|
+
* @returns {undefined}
|
|
33
|
+
*/
|
|
34
|
+
declare function fetch(filename: string, options: IFetchOptions, callback: FetchCallback): void;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Fetches the contents of a file.
|
|
38
|
+
* @name util.fetch
|
|
39
|
+
* @function
|
|
40
|
+
* @param {string} path File path or url
|
|
41
|
+
* @param {FetchCallback} callback Callback function
|
|
42
|
+
* @returns {undefined}
|
|
43
|
+
* @variation 2
|
|
44
|
+
*/
|
|
45
|
+
declare function fetch(path: string, callback: FetchCallback): void;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Fetches the contents of a file.
|
|
49
|
+
* @name util.fetch
|
|
50
|
+
* @function
|
|
51
|
+
* @param {string} path File path or url
|
|
52
|
+
* @param {IFetchOptions} [options] Fetch options
|
|
53
|
+
* @returns {Promise<string|Uint8Array>} Promise
|
|
54
|
+
* @variation 3
|
|
55
|
+
*/
|
|
56
|
+
declare function fetch(path: string, options?: IFetchOptions): Promise<(string|Uint8Array)>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = fetch;
|
|
3
|
+
|
|
4
|
+
var asPromise = require("./aspromise"),
|
|
5
|
+
inquire = require("./inquire");
|
|
6
|
+
|
|
7
|
+
var fs = inquire("fs");
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Node-style callback as used by {@link util.fetch}.
|
|
11
|
+
* @typedef FetchCallback
|
|
12
|
+
* @type {function}
|
|
13
|
+
* @param {?Error} error Error, if any, otherwise `null`
|
|
14
|
+
* @param {string} [contents] File contents, if there hasn't been an error
|
|
15
|
+
* @returns {undefined}
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Options as used by {@link util.fetch}.
|
|
20
|
+
* @interface IFetchOptions
|
|
21
|
+
* @property {boolean} [binary=false] Whether expecting a binary response
|
|
22
|
+
* @property {boolean} [xhr=false] If `true`, forces the use of XMLHttpRequest
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Fetches the contents of a file.
|
|
27
|
+
* @memberof util
|
|
28
|
+
* @param {string} filename File path or url
|
|
29
|
+
* @param {IFetchOptions} options Fetch options
|
|
30
|
+
* @param {FetchCallback} callback Callback function
|
|
31
|
+
* @returns {undefined}
|
|
32
|
+
*/
|
|
33
|
+
function fetch(filename, options, callback) {
|
|
34
|
+
if (typeof options === "function") {
|
|
35
|
+
callback = options;
|
|
36
|
+
options = {};
|
|
37
|
+
} else if (!options)
|
|
38
|
+
options = {};
|
|
39
|
+
|
|
40
|
+
if (!callback)
|
|
41
|
+
return asPromise(fetch, this, filename, options); // eslint-disable-line no-invalid-this
|
|
42
|
+
|
|
43
|
+
// if a node-like filesystem is present, try it first but fall back to XHR if nothing is found.
|
|
44
|
+
if (!options.xhr && fs && fs.readFile)
|
|
45
|
+
return fs.readFile(filename, function fetchReadFileCallback(err, contents) {
|
|
46
|
+
return err && typeof XMLHttpRequest !== "undefined"
|
|
47
|
+
? fetch.xhr(filename, options, callback)
|
|
48
|
+
: err
|
|
49
|
+
? callback(err)
|
|
50
|
+
: callback(null, options.binary ? contents : contents.toString("utf8"));
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// use the XHR version otherwise.
|
|
54
|
+
return fetch.xhr(filename, options, callback);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Fetches the contents of a file.
|
|
59
|
+
* @name util.fetch
|
|
60
|
+
* @function
|
|
61
|
+
* @param {string} path File path or url
|
|
62
|
+
* @param {FetchCallback} callback Callback function
|
|
63
|
+
* @returns {undefined}
|
|
64
|
+
* @variation 2
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Fetches the contents of a file.
|
|
69
|
+
* @name util.fetch
|
|
70
|
+
* @function
|
|
71
|
+
* @param {string} path File path or url
|
|
72
|
+
* @param {IFetchOptions} [options] Fetch options
|
|
73
|
+
* @returns {Promise<string|Uint8Array>} Promise
|
|
74
|
+
* @variation 3
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
/**/
|
|
78
|
+
fetch.xhr = function fetch_xhr(filename, options, callback) {
|
|
79
|
+
var xhr = new XMLHttpRequest();
|
|
80
|
+
xhr.onreadystatechange /* works everywhere */ = function fetchOnReadyStateChange() {
|
|
81
|
+
|
|
82
|
+
if (xhr.readyState !== 4)
|
|
83
|
+
return undefined;
|
|
84
|
+
|
|
85
|
+
// local cors security errors return status 0 / empty string, too. afaik this cannot be
|
|
86
|
+
// reliably distinguished from an actually empty file for security reasons. feel free
|
|
87
|
+
// to send a pull request if you are aware of a solution.
|
|
88
|
+
if (xhr.status !== 0 && xhr.status !== 200)
|
|
89
|
+
return callback(Error("status " + xhr.status));
|
|
90
|
+
|
|
91
|
+
// if binary data is expected, make sure that some sort of array is returned, even if
|
|
92
|
+
// ArrayBuffers are not supported. the binary string fallback, however, is unsafe.
|
|
93
|
+
if (options.binary) {
|
|
94
|
+
var buffer = xhr.response;
|
|
95
|
+
if (!buffer) {
|
|
96
|
+
buffer = [];
|
|
97
|
+
for (var i = 0; i < xhr.responseText.length; ++i)
|
|
98
|
+
buffer.push(xhr.responseText.charCodeAt(i) & 255);
|
|
99
|
+
}
|
|
100
|
+
return callback(null, typeof Uint8Array !== "undefined" ? new Uint8Array(buffer) : buffer);
|
|
101
|
+
}
|
|
102
|
+
return callback(null, xhr.responseText);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
if (options.binary) {
|
|
106
|
+
// ref: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data#Receiving_binary_data_in_older_browsers
|
|
107
|
+
if ("overrideMimeType" in xhr)
|
|
108
|
+
xhr.overrideMimeType("text/plain; charset=x-user-defined");
|
|
109
|
+
xhr.responseType = "arraybuffer";
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
xhr.open("GET", filename);
|
|
113
|
+
xhr.send();
|
|
114
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes a 32 bit float to a buffer using little endian byte order.
|
|
3
|
+
* @name writeFloatLE
|
|
4
|
+
* @function
|
|
5
|
+
* @param {number} val Value to write
|
|
6
|
+
* @param {Uint8Array} buf Target buffer
|
|
7
|
+
* @param {number} pos Target buffer offset
|
|
8
|
+
* @returns {undefined}
|
|
9
|
+
*/
|
|
10
|
+
export function writeFloatLE(val: number, buf: Uint8Array, pos: number): void;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Writes a 32 bit float to a buffer using big endian byte order.
|
|
14
|
+
* @name writeFloatBE
|
|
15
|
+
* @function
|
|
16
|
+
* @param {number} val Value to write
|
|
17
|
+
* @param {Uint8Array} buf Target buffer
|
|
18
|
+
* @param {number} pos Target buffer offset
|
|
19
|
+
* @returns {undefined}
|
|
20
|
+
*/
|
|
21
|
+
export function writeFloatBE(val: number, buf: Uint8Array, pos: number): void;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Reads a 32 bit float from a buffer using little endian byte order.
|
|
25
|
+
* @name readFloatLE
|
|
26
|
+
* @function
|
|
27
|
+
* @param {Uint8Array} buf Source buffer
|
|
28
|
+
* @param {number} pos Source buffer offset
|
|
29
|
+
* @returns {number} Value read
|
|
30
|
+
*/
|
|
31
|
+
export function readFloatLE(buf: Uint8Array, pos: number): number;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Reads a 32 bit float from a buffer using big endian byte order.
|
|
35
|
+
* @name readFloatBE
|
|
36
|
+
* @function
|
|
37
|
+
* @param {Uint8Array} buf Source buffer
|
|
38
|
+
* @param {number} pos Source buffer offset
|
|
39
|
+
* @returns {number} Value read
|
|
40
|
+
*/
|
|
41
|
+
export function readFloatBE(buf: Uint8Array, pos: number): number;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Writes a 64 bit double to a buffer using little endian byte order.
|
|
45
|
+
* @name writeDoubleLE
|
|
46
|
+
* @function
|
|
47
|
+
* @param {number} val Value to write
|
|
48
|
+
* @param {Uint8Array} buf Target buffer
|
|
49
|
+
* @param {number} pos Target buffer offset
|
|
50
|
+
* @returns {undefined}
|
|
51
|
+
*/
|
|
52
|
+
export function writeDoubleLE(val: number, buf: Uint8Array, pos: number): void;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Writes a 64 bit double to a buffer using big endian byte order.
|
|
56
|
+
* @name writeDoubleBE
|
|
57
|
+
* @function
|
|
58
|
+
* @param {number} val Value to write
|
|
59
|
+
* @param {Uint8Array} buf Target buffer
|
|
60
|
+
* @param {number} pos Target buffer offset
|
|
61
|
+
* @returns {undefined}
|
|
62
|
+
*/
|
|
63
|
+
export function writeDoubleBE(val: number, buf: Uint8Array, pos: number): void;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Reads a 64 bit double from a buffer using little endian byte order.
|
|
67
|
+
* @name readDoubleLE
|
|
68
|
+
* @function
|
|
69
|
+
* @param {Uint8Array} buf Source buffer
|
|
70
|
+
* @param {number} pos Source buffer offset
|
|
71
|
+
* @returns {number} Value read
|
|
72
|
+
*/
|
|
73
|
+
export function readDoubleLE(buf: Uint8Array, pos: number): number;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Reads a 64 bit double from a buffer using big endian byte order.
|
|
77
|
+
* @name readDoubleBE
|
|
78
|
+
* @function
|
|
79
|
+
* @param {Uint8Array} buf Source buffer
|
|
80
|
+
* @param {number} pos Source buffer offset
|
|
81
|
+
* @returns {number} Value read
|
|
82
|
+
*/
|
|
83
|
+
export function readDoubleBE(buf: Uint8Array, pos: number): number;
|