-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ts] Use native node fetch removing polyfills
- Loading branch information
1 parent
2828e07
commit 9ec0a31
Showing
6 changed files
with
364 additions
and
573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,10 @@ | ||
/* eslint-disable no-undef */ | ||
import { geojson } from 'flatgeobuf' | ||
import fetch from 'node-fetch' | ||
import { ReadableStream } from 'web-streams-polyfill' | ||
|
||
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))) | ||
for await (let feature of geojson.deserialize(response.body)) | ||
console.log(JSON.stringify(feature, undefined, 1)) | ||
} | ||
|
||
streamtest() | ||
streamtest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.