How to use stream-json - 10 common examples

To help you get started, we’ve selected a few stream-json examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ekalinin / sitemap.js / examples / express.example.js View on Github external
app.get('/sitemap.xml', function(req, res) {
  res.header('Content-Type', 'application/xml');
  res.header('Content-Encoding', 'gzip');
  // if we have a cached entry send it
  if (sitemap) {
    res.send(sitemap)
    return
  }
  try {
    // this could just as easily be a db response
    const gzippedStream = fs.createReadStream(resolve(__dirname, '..', 'tests', 'mocks', 'perf-data.json'))
    .pipe(parser())
    .pipe(streamArray()) // replace with streamValues for JSONStream
    .pipe(map.obj(chunk => chunk.value))
    .pipe(new SitemapStream({ hostname: 'https://example.com/' }))
    .pipe(createGzip())

    // cache the response
    streamToPromise(gzippedStream).then(sm => sitemap = sm)
    // stream the response
    gzippedStream.pipe(res).on('error', (e) => {throw e})
  } catch (e) {
    console.error(e)
    res.status(500).end()
  }
});
github ekalinin / sitemap.js / examples / express.example.js View on Github external
app.get('/sitemap.xml', function(req, res) {
  res.header('Content-Type', 'application/xml');
  res.header('Content-Encoding', 'gzip');
  // if we have a cached entry send it
  if (sitemap) {
    res.send(sitemap)
    return
  }
  try {
    // this could just as easily be a db response
    const gzippedStream = fs.createReadStream(resolve(__dirname, '..', 'tests', 'mocks', 'perf-data.json'))
    .pipe(parser())
    .pipe(streamArray()) // replace with streamValues for JSONStream
    .pipe(map.obj(chunk => chunk.value))
    .pipe(new SitemapStream({ hostname: 'https://example.com/' }))
    .pipe(createGzip())

    // cache the response
    streamToPromise(gzippedStream).then(sm => sitemap = sm)
    // stream the response
    gzippedStream.pipe(res).on('error', (e) => {throw e})
  } catch (e) {
    console.error(e)
    res.status(500).end()
  }
});
github ravendb / ravendb-nodejs-client / src / Documents / Commands / FacetQueryCommand.ts View on Github external
public static async parseQueryResultResponseAsync(
        bodyStream: stream.Stream,
        conventions: DocumentConventions,
        fromCache: boolean,
        bodyCallback?: (body: string) => void): Promise {

        const resultsPromise = RavenCommandResponsePipeline.create()
            .collectBody(bodyCallback)
            .parseJsonAsync([
                pick({ filter: "Results" }),
                streamArray()
            ])
            .streamKeyCaseTransform("camel", "DOCUMENT_LOAD") // we don't care about the case in facets
            .collectResult((result, next) => [...result, next["value"]], [])
            .process(bodyStream);

        const includesPromise = parseDocumentIncludes(bodyStream, conventions);
        const restPromise = parseRestOfOutput(bodyStream, /^Results|Includes$/);

        const [results, includes, rest] = await Promise.all([resultsPromise, includesPromise, restPromise]);
        const rawResult = Object.assign({} as any, rest, { results, includes }) as QueryResult;
        const queryResult = conventions.objectMapper.fromObjectLiteral(rawResult, {
            typeName: QueryResult.name,
            nestedTypes: {
                indexTimestamp: "date",
                lastQueryTime: "date"
github ravendb / ravendb-nodejs-client / src / Documents / Commands / MultiGet / MultiGetCommand.ts View on Github external
.streamKeyCaseTransform({
                defaultTransform: "camel",
                ignorePaths: [/\./],
            })
            .collectResult({
                initResult: [] as GetResponse[],
                reduceResults: (result: GetResponse[], next) => {
                    return [...result, next["value"]];
                }
            })
            .process(bodyStream);

        const responsesResultsPromise = this._pipeline()
            .parseJsonAsync([
                pick({ filter: "Results" }),
                pick({ filter: /^\d+\.Result\b/i }),
                streamValues()
            ])
            .collectResult({
                initResult: [] as string[],
                reduceResults: (result: string[], next) => {
                    // TODO try read it another way
                    const resResult = JSON.stringify(next["value"]);
                    return [...result, resResult];
                }
            })
            .process(bodyStream);

        const [responses, responsesResults] = await Promise.all([responsesPromise, responsesResultsPromise]);
        for (let i = 0; i < responses.length; i++) {
            const res = responses[i];
            res.result = responsesResults[i];
github ravendb / ravendb-nodejs-client / src / Documents / Commands / FacetQueryCommand.ts View on Github external
public static async parseQueryResultResponseAsync(
        bodyStream: stream.Stream,
        conventions: DocumentConventions,
        fromCache: boolean,
        bodyCallback?: (body: string) => void): Promise {

        const resultsPromise = RavenCommandResponsePipeline.create()
            .collectBody(bodyCallback)
            .parseJsonAsync([
                pick({ filter: "Results" }),
                streamArray()
            ])
            .streamKeyCaseTransform("camel", "DOCUMENT_LOAD") // we don't care about the case in facets
            .collectResult((result, next) => [...result, next["value"]], [])
            .process(bodyStream);

        const includesPromise = parseDocumentIncludes(bodyStream, conventions);
        const restPromise = parseRestOfOutput(bodyStream, /^Results|Includes$/);

        const [results, includes, rest] = await Promise.all([resultsPromise, includesPromise, restPromise]);
        const rawResult = Object.assign({} as any, rest, { results, includes }) as QueryResult;
        const queryResult = conventions.objectMapper.fromObjectLiteral(rawResult, {
            typeName: QueryResult.name,
            nestedTypes: {
                indexTimestamp: "date",
                lastQueryTime: "date"
            }
github ravendb / ravendb-nodejs-client / src / Documents / Commands / MultiGet / MultiGetCommand.ts View on Github external
defaultTransform: "camel",
                ignorePaths: [/\./],
            })
            .collectResult({
                initResult: [] as GetResponse[],
                reduceResults: (result: GetResponse[], next) => {
                    return [...result, next["value"]];
                }
            })
            .process(bodyStream);

        const responsesResultsPromise = this._pipeline()
            .parseJsonAsync([
                pick({ filter: "Results" }),
                pick({ filter: /^\d+\.Result\b/i }),
                streamValues()
            ])
            .collectResult({
                initResult: [] as string[],
                reduceResults: (result: string[], next) => {
                    // TODO try read it another way
                    const resResult = JSON.stringify(next["value"]);
                    return [...result, resResult];
                }
            })
            .process(bodyStream);

        const [responses, responsesResults] = await Promise.all([responsesPromise, responsesResultsPromise]);
        for (let i = 0; i < responses.length; i++) {
            const res = responses[i];
            res.result = responsesResults[i];
            const command = this._commands[i];
github embark-framework / embark / src / lib / modules / blockchain_process / proxy.js View on Github external
const server = http.createServer((req, res) => {
      if (req.method === 'POST') {
        // messages TO the target
        Asm.connectTo(
          pump(req, jsonParser())
        ).on('done', ({current: object}) => {
          this.trackRequest(object);
        });
      }

      if (!ws) {
        proxy.web(req, res);
      }
    });
github embark-framework / embark / packages / embark-proxy / src / old_proxy.js View on Github external
const server = http.createServer((req, res) => {
      if (req.method === 'POST') {
        // messages TO the target
        Asm.connectTo(
          pump(req, jsonParser())
        ).on('done', ({current: object}) => {
          this.trackRequest({ ws: false, data: object});
        });
      }

      if (!ws) {
        proxy.web(req, res);
      }
    });
github embark-framework / embark / packages / embark-blockchain-process / src / proxy.js View on Github external
const server = http.createServer((req, res) => {
      if (req.method === 'POST') {
        // messages TO the target
        Asm.connectTo(
          pump(req, jsonParser())
        ).on('done', ({current: object}) => {
          this.trackRequest({ ws: false, data: object});
        });
      }

      if (!ws) {
        proxy.web(req, res);
      }
    });
github embark-framework / embark / packages / embark-proxy / src / old_proxy.js View on Github external
const server = http.createServer((req, res) => {
      if (req.method === 'POST') {
        // messages TO the target
        Asm.connectTo(
          pump(req, jsonParser())
        ).on('done', ({current: object}) => {
          this.trackRequest({ ws: false, data: object});
        });
      }

      if (!ws) {
        proxy.web(req, res);
      }
    });

stream-json

stream-json is the micro-library of Node.js stream components for creating custom JSON processing pipelines with a minimal memory footprint. It can parse JSON files far exceeding available memory streaming individual primitives using a SAX-inspired API. I

BSD-3-Clause
Latest version published 11 months ago

Package Health Score

71 / 100
Full package analysis