New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to import flatgeobuf in nodejs env? #162
Comments
First of all, sorry for the state of the current version of flatgeobuf package and node usage right now. I think it's related to #141 so might be blocked by TypeScript issues but I'll see what I can do. As for use of 3.17.0 I cannot reproduce your problem, I can run the example at https://github.com/flatgeobuf/flatgeobuf/tree/master/examples/node without issues. Can you try that? |
@bjornharrtell thank you for your quick reply, the example at https://github.com/flatgeobuf/flatgeobuf/tree/master/examples/node works well. But I want to try the code at Here is my sample code: server.js, the nodejs and os environment are both same as above:
when I running the with
Is it the error file format when using Thanks |
@jackcjp thanks for getting back, the problem is not the file it's |
@jackcjp it also looks like native support for ReadableStream was added in Node 16. Can you upgrade to it and try that out? |
I now see that you run with Node 16 already, so it's surprising to me that it cannot find ReadableStream. I will try to reproduce. |
I import web-streams-polyfill/ponyfill as below:
If this is the correct usage, it doesn't work either with the same error. |
@jackcjp you will have do |
I've experimented a bit but it won't work by simply polyfilling ReadableStream. This is because node-fetch gives a node Stream as response.body, not a ReadableStream. |
So I don't see any way around that you need some code to convert a node stream to a ReadableStream. This works for me with flatgeobuf 3.17.0: const geojson = require('flatgeobuf/lib/cjs/geojson')
const fetch = require('node-fetch')
const ReadableStream = require('web-streams-polyfill').ReadableStream
function nodeToWeb(nodeStream) {
let destroyed = false;
const listeners = {};
function start(controller) {
listeners['data'] = onData;
listeners['end'] = onData;
listeners['end'] = onDestroy;
listeners['close'] = onDestroy;
listeners['error'] = onDestroy;
for (const name in listeners) nodeStream.on(name, listeners[name]);
nodeStream.pause();
function onData(chunk) {
if (destroyed) return;
controller.enqueue(chunk);
nodeStream.pause();
}
function onDestroy(err) {
if (destroyed) return;
destroyed = true;
for (const name in listeners)
nodeStream.removeListener(name, listeners[name]);
if (err) controller.error(err);
else controller.close();
}
}
function pull() {
if (destroyed) return;
nodeStream.resume();
}
function cancel() {
destroyed = true;
for (const name in listeners)
nodeStream.removeListener(name, listeners[name]);
nodeStream.push(null);
nodeStream.pause();
nodeStream.destroy();
}
return new ReadableStream({ start: start, pull: pull, cancel: cancel });
}
async function streamtest() {
const response = await fetch('https://flatgeobuf.org/test/data/UScounties.fgb')
for await (let feature of geojson.deserialize(nodeToWeb(response.body)))
console.log(JSON.stringify(feature, undefined, 1))
}
streamtest() I hope to have better examples when I can get the current release node usage situation cleared up. I think I will support ES modules import/export only. |
Manwhile I believe I've fixed #141 but by dropping commonjs support entirely so going forward will be with Node 16+ ES modules support. I will try to find time to fixup the node example to it. |
Node examples are now updated to use latest version and included a specific example for how to use with ReadableStream support. |
When I import in this way:
const geojson = require('flatgeobuf/lib/cjs/flatgeobuf.js');
it throw error:
Environment:
Node: v16.13.0
npm: 8.1.4
os: Ubuntu-20.04
"flatgeobuf": "^3.20.1",
"node-fetch": "^2.6.6"
When I downgrade flatgeobuf to 3.17.0, and import it as below:
const geojson = require("flatgeobuf/lib/cjs/geojson")
it throws error too:
Can anyone give me some hint? Thanks! @bjornharrtell
The text was updated successfully, but these errors were encountered: