Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@graphql-tools/json-file-loader

Package Overview
Dependencies
Maintainers
3
Versions
1028
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@graphql-tools/json-file-loader - npm Package Compare versions

Comparing version 5.0.1-alpha-cf1d5c6.0 to 5.0.1-alpha-cf46456.0

34

index.cjs.js

@@ -6,2 +6,5 @@ 'use strict';

const utils = require('@graphql-tools/utils');
const path = require('path');
const fsExtra = require('fs-extra');
const process = require('process');

@@ -14,11 +17,15 @@ const FILE_EXTENSIONS = ['.json'];

async canLoad(pointer, options) {
return this.canLoadSync(pointer, options);
if (utils.isValidPath(pointer)) {
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer);
return new Promise(resolve => fsExtra.exists(normalizedFilePath, resolve));
}
}
return false;
}
canLoadSync(pointer, options) {
if (utils.isValidPath(pointer) && options.path && options.fs) {
const { resolve, isAbsolute } = options.path;
if (utils.isValidPath(pointer)) {
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || process.cwd(), pointer);
const { existsSync } = options.fs;
if (existsSync(normalizedFilePath)) {
const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer);
if (fsExtra.existsSync(normalizedFilePath)) {
return true;

@@ -31,10 +38,15 @@ }

async load(pointer, options) {
return this.loadSync(pointer, options);
const normalizedFilePath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer);
try {
const jsonContent = await fsExtra.readFile(normalizedFilePath, { encoding: 'utf8' });
return utils.parseGraphQLJSON(pointer, jsonContent, options);
}
catch (e) {
throw new Error(`Unable to read JSON file: ${normalizedFilePath}: ${e.message || e}`);
}
}
loadSync(pointer, options) {
const { resolve: resolvePath, isAbsolute } = options.path;
const normalizedFilepath = isAbsolute(pointer) ? pointer : resolvePath(options.cwd || process.cwd(), pointer);
const normalizedFilepath = path.isAbsolute(pointer) ? pointer : path.resolve(options.cwd || process.cwd(), pointer);
try {
const { readFileSync } = options.fs;
const jsonContent = readFileSync(normalizedFilepath, 'utf8');
const jsonContent = fsExtra.readFileSync(normalizedFilepath, 'utf8');
return utils.parseGraphQLJSON(pointer, jsonContent, options);

@@ -41,0 +53,0 @@ }

import { Source, SchemaPointerSingle, DocumentLoader, SingleFileOptions } from '@graphql-tools/utils';
export interface JsonFileLoaderOptions extends SingleFileOptions {
fs?: typeof import('fs');
path?: typeof import('path');
}

@@ -6,0 +4,0 @@ export declare class JsonFileLoader implements DocumentLoader {

import { isValidPath, parseGraphQLJSON } from '@graphql-tools/utils';
import { isAbsolute, resolve } from 'path';
import { exists, existsSync, readFile, readFileSync } from 'fs-extra';
import { cwd } from 'process';

@@ -9,10 +12,14 @@ const FILE_EXTENSIONS = ['.json'];

async canLoad(pointer, options) {
return this.canLoadSync(pointer, options);
if (isValidPath(pointer)) {
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
return new Promise(resolve => exists(normalizedFilePath, resolve));
}
}
return false;
}
canLoadSync(pointer, options) {
if (isValidPath(pointer) && options.path && options.fs) {
const { resolve, isAbsolute } = options.path;
if (isValidPath(pointer)) {
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || process.cwd(), pointer);
const { existsSync } = options.fs;
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
if (existsSync(normalizedFilePath)) {

@@ -26,9 +33,14 @@ return true;

async load(pointer, options) {
return this.loadSync(pointer, options);
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
try {
const jsonContent = await readFile(normalizedFilePath, { encoding: 'utf8' });
return parseGraphQLJSON(pointer, jsonContent, options);
}
catch (e) {
throw new Error(`Unable to read JSON file: ${normalizedFilePath}: ${e.message || e}`);
}
}
loadSync(pointer, options) {
const { resolve: resolvePath, isAbsolute } = options.path;
const normalizedFilepath = isAbsolute(pointer) ? pointer : resolvePath(options.cwd || process.cwd(), pointer);
const normalizedFilepath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer);
try {
const { readFileSync } = options.fs;
const jsonContent = readFileSync(normalizedFilepath, 'utf8');

@@ -35,0 +47,0 @@ return parseGraphQLJSON(pointer, jsonContent, options);

{
"name": "@graphql-tools/json-file-loader",
"version": "5.0.1-alpha-cf1d5c6.0",
"version": "5.0.1-alpha-cf46456.0",
"description": "A set of utils for faster development of GraphQL tools",

@@ -9,3 +9,4 @@ "peerDependencies": {

"dependencies": {
"@graphql-tools/utils": "5.0.1-alpha-cf1d5c6.0",
"@graphql-tools/utils": "5.0.1-alpha-cf46456.0",
"fs-extra": "9.0.0",
"tslib": "1.11.1"

@@ -22,2 +23,5 @@ },

},
"devDependencies": {
"@types/fs-extra": "8.1.0"
},
"publishConfig": {

@@ -24,0 +28,0 @@ "access": "public"

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc