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.

68 changes: 68 additions & 0 deletions src/public/modules/forms/helpers/ndjsonStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 (shouldCancel) {
return
}
seaerchin marked this conversation as resolved.
Show resolved Hide resolved

if (result.done) {
data_buf = data_buf.trim()
if (data_buf.length !== 0) {
try {
controller.enqueue(data_buf)
} catch (e) {
controller.error(e)
return
}
}
return controller.close()
karrui marked this conversation as resolved.
Show resolved Hide resolved
}

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

// Only append if there is content available
lines.forEach((line) => {
if (line) {
try {
controller.enqueue(line)
} catch (e) {
controller.error(e)
shouldCancel = true
return reader.cancel()
karrui marked this conversation as resolved.
Show resolved Hide resolved
}
}
})

data_buf = lines[lines.length - 1]
return reader.read().then(processResult)
})
},
cancel: function (reason) {
console.log('Cancel registered due to ', reason)
shouldCancel = true
maybeReader && maybeReader.cancel()
seaerchin marked this conversation as resolved.
Show resolved Hide resolved
},
})
}
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