UNPKG

wttp-handler

Version:

WTTP handler for fetching data from WTTP sites

102 lines 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wttpGET = wttpGET; exports.wttpLOCATE = wttpLOCATE; exports.wttpHEAD = wttpHEAD; exports.wttpOPTIONS = wttpOPTIONS; const ethers_1 = require("ethers"); // function processHEADResponse(head: HEADResponseStruct, contentLength: number) { // return { // "Content-Length": contentLength.toString(), // size is total size, structure gives returned content length // "Content-Version": head.metadata.version.toString(), // size is total size, structure gives returned content length // "Last-Modified": head.metadata.lastModified.toString(), // "ETag": head.etag.toString(), // "Content-Type": `${decodeMimeType(head.metadata.properties.mimeType as any)}; charset=${decodeCharset(head.metadata.properties.charset as any)}` || "", // "Content-Encoding": decodeEncoding(head.metadata.properties.encoding as any) || "", // "Content-Language": decodeLanguage(head.metadata.properties.language as any) || "", // "Content-Security-Policy": head.headerInfo.cors.custom || "", // "Access-Control-Preset": head.headerInfo.cors.preset.toString() || "", // "Access-Control-Allow-Origin": head.headerInfo.cors.origins.join(", "), // "Access-Control-Allow-Methods": bitmaskToMethods(Number(head.headerInfo.cors.methods)).join(", "), // "Cache-Control": head.headerInfo.cache.custom || "", // "Cache-Preset": head.headerInfo.cache.preset.toString() || "", // "Immutable-Flag": head.headerInfo.cache.immutableFlag.toString(), // "Location": head.headerInfo.redirect.location || "", // } // } async function wttpGET(gateway, siteAddress, path, options) { // function processGETResponse(response: GETResponseStruct) { // if () // return { // status: Number(response.head.status), // headers: processHEADResponse(response.head, Number(response.body.sizes.totalSize)), // body: response.body.data, // } // } const getRequest = { locate: { head: { path, ifModifiedSince: options?.ifModifiedSince || 0n, ifNoneMatch: options?.ifNoneMatch || ethers_1.ethers.ZeroHash, }, rangeChunks: options?.rangeChunks || { start: 0n, end: 0n }, }, rangeBytes: options?.rangeBytes || { start: 0n, end: 0n }, }; try { const response = await gateway.GET(siteAddress, getRequest); return response; } catch (error) { console.error(error); throw error; } } async function wttpLOCATE(gateway, siteAddress, path, options) { const locateRequest = { head: { path, ifModifiedSince: options?.ifModifiedSince || 0n, ifNoneMatch: options?.ifNoneMatch || ethers_1.ethers.ZeroHash, }, rangeChunks: options?.rangeChunks || { start: 0n, end: 0n, }, }; try { const response = await gateway.LOCATE(siteAddress, locateRequest); return response; } catch (error) { console.error(error); throw error; } } async function wttpHEAD(gateway, siteAddress, path, options) { const headRequest = { path, ifModifiedSince: options?.ifModifiedSince || 0n, ifNoneMatch: options?.ifNoneMatch || ethers_1.ethers.ZeroHash, }; try { const response = await gateway.HEAD(siteAddress, headRequest); return response; } catch (error) { console.error(error); throw error; } } async function wttpOPTIONS(gateway, siteAddress, path) { try { const response = await gateway.OPTIONS(siteAddress, path); return response; } catch (error) { console.error(error); throw error; } } //# sourceMappingURL=methods.js.map