@iannisz/node-cms
Advanced tools
Comparing version 0.0.21 to 0.0.22
{ | ||
"name": "@iannisz/node-cms", | ||
"version": "0.0.21", | ||
"version": "0.0.22", | ||
"description": "Node CMS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -54,53 +54,35 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { | ||
var req, start, stream; | ||
return __generator(this, function (_a) { | ||
req = new XMLHttpRequest(); | ||
req.onreadystatechange = function () { | ||
if (req.readyState == 4) { | ||
if (req.status >= 200 && req.status < 300) { | ||
resolve(req.response); | ||
} | ||
else { | ||
reject({ status: req.status, response: req.responseText }); | ||
} | ||
} | ||
}; | ||
req.open(method, url); | ||
start = Date.now(); | ||
stream = new ReadableStream({ | ||
start: function (controller) { | ||
var e_1, _a; | ||
var _this = this; | ||
var stringifiedBody = JSON.stringify(body); | ||
controller.enqueue(stringToUint8Array(stringifiedBody)); | ||
var _loop_1 = function (file) { | ||
controller.enqueue(stringToUint8Array("\n--------------------file\n" + JSON.stringify({ | ||
name: file.name, | ||
lastModified: file.lastModified, | ||
size: file.size, | ||
type: file.type | ||
}) + "\n")); | ||
var fileStream = file.stream(); | ||
var reader = fileStream.getReader(); | ||
var enqueueNextChunk = function () { return __awaiter(_this, void 0, void 0, function () { | ||
var chunk; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, reader.read()]; | ||
case 1: | ||
chunk = _a.sent(); | ||
if (!chunk.done) { | ||
controller.enqueue(chunk.value); | ||
enqueueNextChunk(); | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
enqueueNextChunk(); | ||
var req, start, size, reqBody, fileMetas, files_1, files_1_1, file, fileMeta, pointer, rawBody, addTobody, i, fileMeta, fileData; | ||
var e_1, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
req = new XMLHttpRequest(); | ||
req.onreadystatechange = function () { | ||
if (req.readyState == 4) { | ||
if (req.status >= 200 && req.status < 300) { | ||
resolve(req.response); | ||
} | ||
else { | ||
reject({ status: req.status, response: req.responseText }); | ||
} | ||
} | ||
}; | ||
req.open(method, url); | ||
start = Date.now(); | ||
size = 0; | ||
reqBody = stringToUint8Array(JSON.stringify(body)); | ||
size += reqBody.byteLength; | ||
fileMetas = []; | ||
try { | ||
for (var files_1 = __values(files), files_1_1 = files_1.next(); !files_1_1.done; files_1_1 = files_1.next()) { | ||
var file = files_1_1.value; | ||
_loop_1(file); | ||
for (files_1 = __values(files), files_1_1 = files_1.next(); !files_1_1.done; files_1_1 = files_1.next()) { | ||
file = files_1_1.value; | ||
fileMeta = stringToUint8Array("\n--------------------file\n" + JSON.stringify({ | ||
name: file.name, | ||
lastModified: file.lastModified, | ||
size: file.size, | ||
type: file.type | ||
}) + "\n"); | ||
size += fileMeta.byteLength + file.size; | ||
fileMetas.push(fileMeta); | ||
} | ||
@@ -115,7 +97,29 @@ } | ||
} | ||
} | ||
}); | ||
console.log('prepared request in ' + (Date.now() - start) + 'ms'); | ||
req.send(stream); | ||
return [2 /*return*/]; | ||
pointer = 0; | ||
rawBody = new Uint8Array(size); | ||
addTobody = function (data) { | ||
rawBody.set(data, pointer); | ||
pointer += data.byteLength; | ||
}; | ||
// Add body | ||
addTobody(reqBody); | ||
i = 0; | ||
_b.label = 1; | ||
case 1: | ||
if (!(i < files.length)) return [3 /*break*/, 4]; | ||
fileMeta = fileMetas[i]; | ||
return [4 /*yield*/, files[i].arrayBuffer()]; | ||
case 2: | ||
fileData = _b.sent(); | ||
addTobody(fileMeta); | ||
addTobody(new Uint8Array(fileData)); | ||
_b.label = 3; | ||
case 3: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 4: | ||
console.log('prepared request in ' + (Date.now() - start) + 'ms'); | ||
req.send(rawBody); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
@@ -122,0 +126,0 @@ }); }); |
@@ -26,38 +26,54 @@ type HTTPMethod = 'GET' | 'POST' | ||
const stream = new ReadableStream<Uint8Array>({ | ||
start(controller) { | ||
const stringifiedBody = JSON.stringify(body) | ||
let size = 0 | ||
controller.enqueue(stringToUint8Array(stringifiedBody)) | ||
// Create body | ||
for (let file of files) { | ||
controller.enqueue(stringToUint8Array( | ||
`\n--------------------file\n${ JSON.stringify({ | ||
name: file.name, | ||
lastModified: file.lastModified, | ||
size: file.size, | ||
type: file.type | ||
}) }\n` | ||
)) | ||
const reqBody = stringToUint8Array(JSON.stringify(body)) | ||
size += reqBody.byteLength | ||
const fileStream = file.stream() | ||
const reader = fileStream.getReader() | ||
// Create file metas | ||
const enqueueNextChunk = async () => { | ||
const chunk = await reader.read() | ||
const fileMetas: Uint8Array[] = [] | ||
if (!chunk.done) { | ||
controller.enqueue(chunk.value) | ||
enqueueNextChunk() | ||
} | ||
} | ||
for (let file of files) { | ||
const fileMeta = stringToUint8Array( | ||
`\n--------------------file\n${ JSON.stringify({ | ||
name: file.name, | ||
lastModified: file.lastModified, | ||
size: file.size, | ||
type: file.type | ||
}) }\n` | ||
) | ||
enqueueNextChunk() | ||
} | ||
} | ||
}) | ||
size += fileMeta.byteLength + file.size | ||
fileMetas.push(fileMeta) | ||
} | ||
// Create raw body | ||
let pointer = 0 | ||
const rawBody = new Uint8Array(size) | ||
const addTobody = (data: Uint8Array) => { | ||
rawBody.set(data, pointer) | ||
pointer += data.byteLength | ||
} | ||
// Add body | ||
addTobody(reqBody) | ||
// Add each file | ||
for (let i = 0; i < files.length; i++) { | ||
const fileMeta = fileMetas[i] | ||
const fileData = await files[i].arrayBuffer() | ||
addTobody(fileMeta) | ||
addTobody(new Uint8Array(fileData)) | ||
} | ||
console.log('prepared request in ' + (Date.now() - start) + 'ms') | ||
req.send(stream) | ||
req.send(rawBody) | ||
}) | ||
@@ -64,0 +80,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
358599
8065