Skip to content
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

refactor(ts-migration): migrates ndjsonstream to typescript #2053

Merged
70 changes: 0 additions & 70 deletions src/public/modules/forms/helpers/ndjsonStream.js

This file was deleted.

73 changes: 73 additions & 0 deletions src/public/modules/forms/helpers/ndjsonStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Modified fork of https://github.com/canjs/can-ndjson-stream to enqueue
// the string immediately without a JSON.parse() step, as the stream payload
// is to be decrypted by the decryption worker.

// Note that this code assumes a polyfill of TextDecoder is available to run in IE11.

export const ndjsonStream = (
response: ReadableStream<Uint8Array>,
): ReadableStream => {
// For cancellation
let maybeReader: ReadableStreamDefaultReader<Uint8Array> | undefined
let shouldCancel = false
return new ReadableStream({
start: function (controller) {
const reader = response.getReader()
maybeReader = reader
const decoder = new TextDecoder()
let data_buf = ''

return reader
.read()
.then(function processResult(result): Promise<void> | undefined | void {
if (result.done && shouldCancel) {
return
}

if (result.done) {
data_buf = data_buf.trim()
if (data_buf.length !== 0) {
try {
controller.enqueue(data_buf)
} catch (e) {
controller.error(e)
return
}
}
controller.close()
return
}

// Read the input in as a stream and split by newline and trim
data_buf += decoder.decode(result.value, { stream: true })
const lines = data_buf.split('\n')

// Reads in every line BUT the last
// Trims the line and queues it in the controller if there is content in the line
for (let i = 0; i < lines.length - 1; ++i) {
const l = lines[i].trim()
if (l.length > 0) {
try {
controller.enqueue(l)
} catch (e) {
controller.error(e)
shouldCancel = true
void reader.cancel()
return
}
}
}
data_buf = lines[lines.length - 1]

return reader.read().then(processResult)
})
},
cancel: function (reason) {
console.log('Cancel registered due to ', reason)
shouldCancel = true
if (maybeReader) {
void maybeReader.cancel()
}
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const CsvMHGenerator = require('../helpers/CsvMergedHeadersGenerator')
const DecryptionWorker = require('../helpers/decryption.worker.js')
const { fixParamsToUrl, triggerFileDownload } = require('../helpers/util')
const ndjsonStream = require('../helpers/ndjsonStream')
const { ndjsonStream } = require('../helpers/ndjsonStream')
const fetchStream = require('fetch-readablestream')
const { decode: decodeBase64 } = require('@stablelib/base64')
const JSZip = require('jszip')
Expand Down