UNPKG

schema-dts-gen

Version:

Generate TypeScript Definitions for Schema.org Schema

106 lines 3.97 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; /** * Copyright 2023 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import https from 'https'; import fs from 'fs/promises'; import { Log } from '../logging/index.js'; import { Parser, Store } from 'n3'; function asQuads(data) { return new Parser({}).parse(data); } /** * Loads schema all Triples from a given Schema file and version. */ export function load(url) { return __awaiter(this, void 0, void 0, function* () { const quads = yield handleUrl(url); return process(quads); }); } /** * does the same as load(), but for a local file */ export function loadFile(path) { return __awaiter(this, void 0, void 0, function* () { const quads = yield handleFile(path); return process(quads); }); } function handleFile(path) { return __awaiter(this, void 0, void 0, function* () { const fileStr = yield fs.readFile(path, { encoding: 'utf8' }); return asQuads(fileStr); }); } function handleUrl(url) { return new Promise((resolve, reject) => { https .get(url, response => { Log(`Got Response ${response.statusCode}: ${response.statusMessage}.`); if (response.statusCode !== 200) { const location = response.headers['location'] || response.headers['content-location']; if (location) { Log(`Handling redirect to ${location}...`); resolve(handleUrl(location)); return; } reject(new Error(`Got Errored Response ${response.statusCode}: ${response.statusMessage}.`)); return; } const data = []; response.on('data', (chunkB) => { const chunk = chunkB.toString('utf-8'); data.push(chunk); }); response.on('end', () => { try { resolve(asQuads(data.join(''))); } catch (error) { Log(`Caught Error on end: ${error}`); reject(error); } }); response.on('error', error => { Log(`Saw error: ${error}`); reject(error); }); }) .on('error', reject); }); } export function process(quads) { const filtered = quads.filter(quad => { if (quad.subject.termType === 'NamedNode' && quad.subject.value.includes('file:///')) { // Inexplicably, local files end up in the public schema for // certain layer overlays. return false; } return true; }); return new Store(filtered); } //# sourceMappingURL=reader.js.map