我将大型(30MB +)JSON文件存储在我的服务器上作为file.json.使用jQuery的getJSON("http://site/file.json")函数,它可以正常工作.但是,正如您可能认为下载30MB的JSON响应需要花费大量时间.
相反,我现在将它们存储为Gzip文件(file.json.gz),将它们降低到不到1MB!我想通过使用getJSON("http://site/file.json.gz")来做同样的事情,但看起来浏览器没有解压缩GZIP的响应,因此解析它不起作用.
那么,有没有办法让jQuery或浏览器解压缩使用GZIP压缩的静态JSON文件,如file.json.gz?
BTW:正确压缩保存的文件.如果我手动解压缩它们,我会得到30MB +有效的JSON文件.
假设我有以下文件error.js:
// this is invalid code
var a: 0;
Run Code Online (Sandbox Code Playgroud)
然后在我的main.js我需要该文件:
require("./error.js");
Run Code Online (Sandbox Code Playgroud)
我从Node.js得到一个简单易懂的错误消息:
$ node main.js
/home/jfischer/error.js:2
var a: 0;
^
SyntaxError: Unexpected token :
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/home/jfischer/main.js:1:63)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
Run Code Online (Sandbox Code Playgroud)
现在让我们尝试将它包装在try/catch块中,并通过更改main.js为:来打印错误:
try {
require("./error.js");
}
catch(err) {
console.log(err.stack);
}
Run Code Online (Sandbox Code Playgroud)
但现在我们得到:
$ node main.js
SyntaxError: Unexpected token :
at exports.runInThisContext (vm.js:53:16) …Run Code Online (Sandbox Code Playgroud) 有没有办法在 node.js 中同步读取 TCP 套接字?
我很清楚如何通过向套接字的“数据”事件添加回调来异步执行此操作:
socket.on('data', function(data) {
// now we have the string data to do whatever with
});
Run Code Online (Sandbox Code Playgroud)
我也知道尝试使用函数调用进行阻塞而不是注册回调有悖于 node 的设计,但是我们正在尝试更新一个旧的 node 模块,该模块充当我大学的客户端,同时保持向后兼容性。所以我们目前有:
var someData = ourModule.getData();
Run Code Online (Sandbox Code Playgroud)
凡getData()以前有一帮其背后的逻辑,但现在我们只想发送到服务器“运行的getData()”,并等待结果。这样所有的逻辑都是服务器端,而不是重复的客户端和服务器端。这个模块已经维护了一个到服务器的 TCP 连接,所以我们只是捎带它。
以下是我尝试过的解决方案:
为隐藏在 node 的 net 模块中类似于 python 的套接字库的某处的套接字找到一个阻塞读取函数。
string_from_tcp = socket.recv(1024)
Run Code Online (Sandbox Code Playgroud)
这里的问题是它似乎不存在(不出所料,因为它违背了 node 的意识形态)。
这个同步网络模块添加了我需要的东西,但没有 Windows 支持;所以我必须补充一点。
找到一个允许节点解除事件循环阻塞的函数,然后返回,这样就可以了:
var theData = null;
clientSocket.on('data', function(data) {
theData = data;
});
clientSocket.write("we want some data");
while(theData === null) {
someNodeFunctionThatUnblocksEventLoopThenReturnsHere(); // in this function node …Run Code Online (Sandbox Code Playgroud)我正在尝试将 TypeScript 中的元组联合转换为对象,而不会丢失任何类型。
以下是它如何工作的示例:
type Tuples = ["foo", string] | ["bar", boolean] | ["baz", null];
/*
ideally the type would be:
{
foo: string;
bar: boolean;
baz: null;
}
*/
type AsObject = DoSomething<Tuples>;
Run Code Online (Sandbox Code Playgroud)
上述问题的一个简单解决方案是:
type TupleToObject<T extends [string, any]> = { [key in T[0]]: T[1] };
/*
type is:
{
foo: string | boolean | null;
bar: string | boolean | null;
baz: string | boolean | null;
}
*/
type TypesLost = TupleToObject<Tuples>;
Run Code Online (Sandbox Code Playgroud)
然而,我们丢失了一些类型信息,因为所有值都被拉到一个联合类型中。
我正在寻找一种使用泛型的解决方案,它不会丢失这种类型信息,并且希望对在 TypeScript 中映射泛型元组有更深入的了解。
javascript ×2
node.js ×2
boost-asio ×1
browser ×1
console.log ×1
generics ×1
gzip ×1
jquery ×1
json ×1
tcp ×1
tuples ×1
typescript ×1