box-node-sdk
Version:
Official SDK for Box Platform APIs
280 lines • 9.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultPrivateKeyDecryptor = exports.Hash = exports.utilLib = exports.FormData = exports.Buffer = void 0;
exports.generateByteBuffer = generateByteBuffer;
exports.generateReadableStreamFromFile = generateReadableStreamFromFile;
exports.generateByteStreamFromBuffer = generateByteStreamFromBuffer;
exports.decodeBase64ByteStream = decodeBase64ByteStream;
exports.stringToByteStream = stringToByteStream;
exports.getEnvVar = getEnvVar;
exports.setEnvVar = setEnvVar;
exports.readByteStream = readByteStream;
exports.iterateChunks = iterateChunks;
exports.createJwtAssertion = createJwtAssertion;
exports.readTextFromFile = readTextFromFile;
exports.createAgent = createAgent;
exports.jsonStringifyWithEscapedUnicode = jsonStringifyWithEscapedUnicode;
exports.computeWebhookSignature = computeWebhookSignature;
exports.compareSignatures = compareSignatures;
exports.calculateMD5Hash = calculateMD5Hash;
const buffer_1 = require("buffer");
Object.defineProperty(exports, "Buffer", { enumerable: true, get: function () { return buffer_1.Buffer; } });
const hash_wasm_1 = require("hash-wasm");
class FormData {
}
exports.FormData = FormData;
class utilLib {
static inspect = {
custom: 'inspect.custom',
};
}
exports.utilLib = utilLib;
class Hash {
hash;
algorithm;
constructor({ algorithm }) {
this.algorithm = algorithm;
this.hash = undefined;
}
async initializeBrowserHash() {
switch (this.algorithm) {
case 'sha1':
this.hash = await (0, hash_wasm_1.createSHA1)();
this.hash.init();
break;
default:
throw new Error(`Unsupported algorithm: ${this.algorithm}`);
}
}
async updateHash(data) {
if (!this.hash) {
await this.initializeBrowserHash();
}
this.hash.update(data);
}
async digestHash(encoding = 'base64') {
if (!this.hash) {
await this.initializeBrowserHash();
}
const d = this.hash.digest('binary');
switch (encoding) {
case 'base64':
return buffer_1.Buffer.from(d).toString('base64');
default:
throw new Error(`Unsupported encoding: ${encoding}`);
}
}
}
exports.Hash = Hash;
function generateByteBuffer(size) {
// Maximum size for crypto.getRandomValues is 65536 bytes
const MAX_CHUNK_SIZE = 65536;
const buffer = new Uint8Array(size);
for (let offset = 0; offset < size; offset += MAX_CHUNK_SIZE) {
const length = Math.min(MAX_CHUNK_SIZE, size - offset);
const chunk = new Uint8Array(length);
window.crypto.getRandomValues(chunk);
buffer.set(chunk, offset);
}
return buffer_1.Buffer.from(buffer);
}
function generateReadableStreamFromFile(file, chunkSize = 1024 * 1024) {
let offset = 0;
return new ReadableStream({
async pull(controller) {
if (offset >= file.size) {
controller.close();
return;
}
const chunk = file.slice(offset, offset + chunkSize);
const arrayBuffer = await chunk.arrayBuffer();
controller.enqueue(new Uint8Array(arrayBuffer));
offset += chunkSize;
},
});
}
function generateByteStreamFromBuffer(buffer) {
return new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array(buffer));
controller.close();
},
});
}
function decodeBase64ByteStream(data) {
return new ReadableStream({
start(controller) {
const decodedStr = atob(data);
const buffer = new ArrayBuffer(decodedStr.length);
const array = new Uint8Array(buffer);
for (let i = 0; i < decodedStr.length; i++) {
array[i] = decodedStr.charCodeAt(i);
}
controller.enqueue(array);
controller.close();
},
});
}
function stringToByteStream(data) {
return new ReadableStream({
start(controller) {
const buffer = new ArrayBuffer(data.length);
const array = new Uint8Array(buffer);
for (let i = 0; i < data.length; i++) {
array[i] = data.charCodeAt(i);
}
controller.enqueue(array);
controller.close();
},
});
}
function getEnvVar(name) {
if (typeof window !== 'undefined' &&
window.env &&
window.env[name]) {
return window.env[name];
}
return '';
}
function setEnvVar(name, value) {
if (typeof window === 'undefined') {
throw new Error('This function requires a browser environment');
}
if (!window.env) {
window.env = {};
}
window.env[name] = value;
}
async function readByteStream(byteStream) {
const buffers = [];
// Browser ReadableStream
const reader = byteStream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done)
break;
buffers.push(buffer_1.Buffer.from(value));
}
return buffer_1.Buffer.concat(buffers);
}
async function* iterateChunks(stream, chunkSize, fileSize) {
let buffers = [];
let totalSize = 0;
let consumedSize = 0;
const reader = stream.getReader();
while (consumedSize < fileSize) {
const { done, value } = await reader.read();
if (done)
break;
const data = buffer_1.Buffer.from(value);
consumedSize += data.length;
buffers.push(data);
totalSize += data.length;
yield* yieldChunks();
}
if (consumedSize !== fileSize) {
throw new Error(`Stream size ${consumedSize} does not match expected file size ${fileSize}`);
}
if (totalSize > 0) {
yield await generateByteStreamFromBuffer(buffer_1.Buffer.concat(buffers));
}
/**
* Yields chunks of the desired `chunkSize` if enough data is available.
*/
async function* yieldChunks() {
if (totalSize < chunkSize)
return;
const buffer = buffer_1.Buffer.concat(buffers);
let start = 0;
while (totalSize >= chunkSize) {
yield await generateByteStreamFromBuffer(buffer.subarray(start, start + chunkSize));
start += chunkSize;
totalSize -= chunkSize;
}
buffers = totalSize > 0 ? [buffer.subarray(start)] : [];
}
}
class DefaultPrivateKeyDecryptor {
constructor(fields) { }
}
exports.DefaultPrivateKeyDecryptor = DefaultPrivateKeyDecryptor;
/**
* Creates a JWT assertion.
*
* @param claims
* @param key
* @param options
* @returns
*/
async function createJwtAssertion(claims, key, options) {
throw new Error('This function is not supported in the browser');
}
/**
* Reads a text file and returns its content.
*/
function readTextFromFile(filepath) {
throw new Error('This function is not supported in the browser');
}
/**
* Create web agent from proxy agent options.
*/
function createAgent(options, proxyConfig) {
return undefined;
}
/**
* Stringify JSON with escaped multibyte Unicode characters to ensure computed signatures match PHP's default behavior
*
* @param {Object} body - The parsed JSON object
* @returns {string} - Stringified JSON with escaped multibyte Unicode characters
* @private
*/
function jsonStringifyWithEscapedUnicode(body) {
return body
.replace(/[\u007f-\uffff]/g, (char) => `\\u${`0000${char.charCodeAt(0).toString(16)}`.slice(-4)}`)
.replace(/(?<!\\)\//g, '\\/');
}
/**
* Compute the message signature
* @see {@Link https://developer.box.com/en/guides/webhooks/handle/setup-signatures/}
*
* @param {string} body - The request body of the webhook message
* @param {Object} headers - The request headers of the webhook message
* @param {string} signatureKey - The signature to verify the message with
* @param {string} escapeBody - Indicates if payload should be escaped or left as is
* @returns {?string} - The message signature (or null, if it can't be computed)
* @private
*/
async function computeWebhookSignature(body, headers, signatureKey, escapeBody = false) {
if (headers['box-signature-version'] !== '1') {
return null;
}
if (headers['box-signature-algorithm'] !== 'HmacSHA256') {
return null;
}
let signature = null;
const escapedBody = escapeBody ? jsonStringifyWithEscapedUnicode(body) : body;
const hashFunc = (0, hash_wasm_1.createSHA256)();
const hmac = await (0, hash_wasm_1.createHMAC)(hashFunc, signatureKey);
hmac.init();
hmac.update(escapedBody);
hmac.update(headers['box-delivery-timestamp']);
const result = await hmac.digest('binary');
signature = buffer_1.Buffer.from(result).toString('base64');
return signature;
}
async function compareSignatures(expectedSignature, receivedSignature) {
if (!expectedSignature || !receivedSignature) {
return false;
}
if (expectedSignature.length !== receivedSignature.length)
return false;
let result = 0;
for (let i = 0; i < expectedSignature.length; i++) {
result |= expectedSignature.charCodeAt(i) ^ receivedSignature.charCodeAt(i);
}
return result === 0;
}
async function calculateMD5Hash(data) {
return await (0, hash_wasm_1.sha1)(data);
}
//# sourceMappingURL=utilsBrowser.js.map