How to use range-parser - 8 common examples

To help you get started, we’ve selected a few range-parser 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 beakerbrowser / beaker / app / bg / dat / protocol.js View on Github external
//   return respondError(304, 'Not Modified')
  // }

  // fetch the permissions
  // TODO this has been disabled until we can create a better UX -prf
  // var origins
  // try {
  //   origins = await sitedataDb.getNetworkPermissions('dat://' + archiveKey)
  // } catch (e) {
  //   origins = []
  // }

  // handle range
  headers['Accept-Ranges'] = 'bytes'
  var range = request.headers.Range || request.headers.range
  if (range) range = parseRange(entry.size, range)
  if (range && range.type === 'bytes') {
    range = range[0] // only handle first range given
    statusCode = 206
    headers['Content-Range'] = 'bytes ' + range.start + '-' + range.end + '/' + entry.size
    headers['Content-Length'] = range.end - range.start + 1
  } else {
    if (entry.size) {
      headers['Content-Length'] = entry.size
    }
  }

  Object.assign(headers, {
    'Content-Security-Policy': cspHeader,
    'Access-Control-Allow-Origin': '*',
    'Cache-Control': 'no-cache'
  })
github beakerbrowser / beaker / app / background-process / protocols / dat.js View on Github external
// if (request.headers['if-none-match'] === ETag) {
  //   return respondError(304, 'Not Modified')
  // }

  // fetch the permissions
  // TODO this has been disabled until we can create a better UX -prf
  // var origins
  // try {
  //   origins = await sitedataDb.getNetworkPermissions('dat://' + archiveKey)
  // } catch (e) {
  //   origins = []
  // }

  // handle range
  headers['Accept-Ranges'] = 'bytes'
  var range = request.headers.range && parseRange(entry.size, request.headers.range)
  if (range && range.type === 'bytes') {
    range = range[0] // only handle first range given
    statusCode = 206
    headers['Content-Range'] = 'bytes ' + range.start + '-' + range.end + '/' + entry.size
    headers['Content-Length'] = range.end - range.start + 1
    debug('Serving range:', range)
  } else {
    if (entry.size) {
      headers['Content-Length'] = entry.size
    }
  }

  // fetch the entry and stream the response
  debug('Entry found:', entry.path)
  fileReadStream = archiveFS.createReadStream(entry.path, range)
  var dataStream = fileReadStream
github beakerbrowser / beaker / app / lib / bg / scoped-fs-server.js View on Github external
if (!entry) {
      // check for a fallback page
      if (manifest && manifest.fallback_page) {
        await tryStat(manifest.fallback_page)
      }

      if (!entry) {
        return cb(404, 'Not Found')
      }
    }

    // handle range
    let statusCode = 200
    let headers = {}
    let headersSent = false
    let range = request.headers.range && parseRange(entry.size, request.headers.range)
    headers['Accept-Ranges'] = 'bytes'
    if (range && range.type === 'bytes') {
      range = range[0] // only handle first range given
      statusCode = 206
      headers['Content-Range'] = 'bytes ' + range.start + '-' + range.end + '/' + entry.size
      headers['Content-Length'] = range.end - range.start + 1
    } else {
      if (entry.size) {
        headers['Content-Length'] = entry.size
      }
    }

    // fetch the entry and stream the response
    let fileReadStream = scopedFS.createReadStream(entry.path, range)
    var dataStream = fileReadStream
      .pipe(mime.identifyStream(entry.path, mimeType => {
github DefinitelyTyped / DefinitelyTyped / range-parser / range-parser-tests.ts View on Github external
import * as RangeParser from 'range-parser';

declare var console: { assert(b: boolean): void };

console.assert(RangeParser(200, `malformed`) === RangeParser.Result.invaild);
console.assert(RangeParser(200, `bytes=500-20`) === RangeParser.Result.unsatisifiable);

const range = RangeParser(1000, `bytes=0-499`);

if (typeof range !== 'number') {
    console.assert(range.type === `bytes`);
    console.assert(range.length === 1);
}
github DefinitelyTyped / DefinitelyTyped / range-parser / range-parser-tests.ts View on Github external
import * as RangeParser from 'range-parser';

declare var console: { assert(b: boolean): void };

console.assert(RangeParser(200, `malformed`) === RangeParser.Result.invaild);
console.assert(RangeParser(200, `bytes=500-20`) === RangeParser.Result.unsatisifiable);

const range = RangeParser(1000, `bytes=0-499`);

if (typeof range !== 'number') {
    console.assert(range.type === `bytes`);
    console.assert(range.length === 1);
}
github trekjs / trek / lib / request.js View on Github external
range (size, options) {
    const range = this.get('Range')
    if (!range) return
    return parseRange(size, range, options)
  }
github OpusCapita / filemanager / packages / connector-google-drive-v2 / src / api.js View on Github external
async function uploadFileToId(parentId, file, onProgress) {
  const size = file.content.length;
  const sessionUrl = await initResumableUploadSession({ name: file.name, size, parentId });
  let startByte = 0;

  while (startByte < size) {
    const res = await uploadChunk({
      sessionUrl,
      size,
      startByte,
      content: file.content
    });

    if (res.status === 308) {
      const range = parseRange(size, res.headers['range']);
      startByte = range[0].end + 1;

      const progress = startByte / (size / 100);
      onProgress(progress);
    }

    if (res.status === 200 || res.status === 201) {
      return res;
    }
  }

  return null;
}
github qgustavor / direct-mega / src / sw.js View on Github external
const useHttpRange = extraArguments['use-http-range']
      if (useHttpRange) {
        headers['Accept-Ranges'] = 'bytes'
      }

      const date = new Date(file.timestamp * 1000)
      if (!isNaN(date.getTime())) {
        headers['Last-Modified'] = date.toGMTString()
        headers['Date'] = headers['Last-Modified']
      }

      let start = extraArguments.start && (bytes.parse(extraArguments.start) || null)
      let end = extraArguments.end && (bytes.parse(extraArguments.end) || null)

      if (useHttpRange && range) {
        const parsedRange = rangeParser(file.size, range, { combine: true })
        if (parsedRange === -1 || parsedRange === -2 || parsedRange.length > 1) {
          resolve(new self.Response(new self.Blob(['Range Not Satisfiable']), {
            status: 416,
            headers: {
              'Content-Type': 'text/plain; charset=utf-8',
              'Content-Range': '*/0'
            }
          }))
          return
        }

        start = parsedRange[0].start
        end = parsedRange[0].end

        headers['Content-Range'] = `bytes ${start}-${end}/${file.size}`
        headers['Content-Length'] = end - start + 1

range-parser

Range header field string parser

MIT
Latest version published 6 years ago

Package Health Score

71 / 100
Full package analysis

Popular range-parser functions